refactor(agent): make runtime identity explicit

This commit is contained in:
_Kerman
2026-08-28 17:44:56 +08:00
parent 375af94454
commit ebce3a5f04
61 changed files with 281 additions and 396 deletions
+6 -6
View File
@@ -380,12 +380,14 @@ export class ApiSessionAgentController {
readonly setup: AgentSetup
}> {
const presets = this.ctx.get('agentPresets')
if (presets === undefined) return { setup: (agentCtx) => { this.installSelection(agentCtx) } }
if (presets === undefined) {
return { setup: (_agentCtx, agent) => { this.installSelection(agent) } }
}
const resolvedId = (await presets.resolve(presetId)).id
return {
agentPreset: resolvedId,
setup: async (agentCtx) => {
this.installSelection(agentCtx)
setup: async (agentCtx, agent) => {
this.installSelection(agent)
await presets.mount(agentCtx, resolvedId)
},
}
@@ -494,9 +496,7 @@ export class ApiSessionAgentController {
return { provider, model }
}
private installSelection(agentCtx: Context): void {
const agent = agentCtx.agent
if (agent === undefined) throw new Error('api-session: Agent setup has no scoped Agent')
private installSelection(agent: Agent): void {
this.selectionFor(agent)
}
@@ -421,15 +421,12 @@ describe('ApiSession create or adoption', () => {
.rejects.toBeInstanceOf(ApiSessionCwdConflict)
})
it('surfaces directory creation failure and rejects setup without a scoped Agent', async () => {
it('surfaces directory creation failure', async () => {
const { agents } = await harness()
const parent = mkdtempSync(join(tmpdir(), 'dsh-session-controller-file-'))
const file = join(parent, 'file')
writeFileSync(file, 'not a directory')
await expect(agents.ensureSession(SessionId('mkdir-failure'), join(file, 'child'), false))
.rejects.toThrow('failed to ensure project directory')
const composition = await agents.composeAgent(undefined)
expect(() => composition.setup(new Context())).toThrow('Agent setup has no scoped Agent')
})
})
@@ -34,9 +34,9 @@ async function composed(workspaces: readonly Workspace[] = []): Promise<Context>
...options.meta === undefined ? {} : { meta: options.meta },
})
const agent = {} as Agent
const agentCtx = ownerCtx.extend({ agent })
const agentCtx = ownerCtx
Object.assign(agent, { id: session.id, session, status: 'idle', ctx: agentCtx })
await options.setup?.(agentCtx)
await options.setup?.(agentCtx, agent)
ctx.agents.register(agent)
return { agent, dispose: () => Promise.resolve() }
},
@@ -49,9 +49,8 @@ async function harness(presets?: readonly string[]) {
options.meta === undefined ? {} : { meta: options.meta },
)
const agent = stubAgent(session)
const agentCtx = ctx.extend({ agent })
;(agent as { ctx?: Context }).ctx = agentCtx
await options.setup?.(agentCtx)
;(agent as { ctx?: Context }).ctx = ctx
await options.setup?.(ctx, agent)
const unregister = ctx.agents.register(agent)
return { agent, dispose: () => { unregister(); return Promise.resolve() } }
},