Skip to content

Commit 09e6020

Browse files
author
dev agent
committed
fix(core): only release a spawn when the session really ends
`onSessionEnded` treated every `failed` lifecycle as the end of a session. `AcpClient.ensureSession` also emits `{outcome: 'failed', reason: undefined}` when a *restore* throws (`resumeSession`/`loadSession`), and it does so between minting the session's mcpServers and falling through to `newSession` with those same servers — so the release killed the spawn the fresh session was about to use. Every `zooid_*` call in that session then answered `binding not owned by caller`, with the session cached and no way to re-mint. Gate on `reason`, which only the idle/clear close paths set, and pin it with a test that fires `{failed, recoveryMethod: 'resume'|'load'}` (must not release) before `{failed, reason: 'idle'}` (must release). Red without the fix: that test fails and nothing else changes. Also record in the changeset that releasing per idle close mints a new spawn and a new `zooid-context-<id>` per idle cycle until anomalyco/opencode#40978 lands — this stops stale spawns acting, it does not remove them from the registry. Verified: core 378 tests, cli build-registry.context 7, core+cli `tsc --noEmit`.
1 parent bf1a7b7 commit 09e6020

3 files changed

Lines changed: 46 additions & 5 deletions

File tree

‎.changeset/release-context-spawn-on-session-end.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,12 @@ and task. That is the cross-thread leak seen live.
2424
spawn's tools fail loudly with the daemon's existing `binding not owned by
2525
caller` instead of acting.
2626

27+
Each idle close now releases its spawn, so the *next* prompt mints a fresh one
28+
and a fresh `zooid-context-<spawnId>` name. Until
29+
[anomalyco/opencode#40978](https://github.com/anomalyco/opencode/issues/40978)
30+
lands, opencode's directory-scoped registry still gains one entry (and one
31+
subprocess) per idle cycle: this change stops stale spawns from *acting*, it does
32+
not make them disappear.
33+
2734
Also rejects a whitespace-only `zooid_complete_task` summary
2835
(`z.string().trim().min(1)`); `" "` previously passed `min(1)`.

‎packages/core/src/acp-registry.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,33 @@ describe('AcpAgentRegistry', () => {
202202
expect(forwarded).toHaveBeenCalledWith('triage', expect.objectContaining({ outcome: 'closed' }))
203203
})
204204

205+
it('keeps the binding when a session restore fails before the new session starts', async () => {
206+
const { AcpClient } = (await import('@zooid/acp-client')) as unknown as {
207+
AcpClient: ReturnType<typeof vi.fn>
208+
}
209+
const ended = vi.fn()
210+
registry = new AcpAgentRegistry({
211+
runtime,
212+
agents: registry.opts.agents,
213+
onSessionEnded: ended,
214+
})
215+
await registry.prompt('triage', { threadId: 't', content: [] })
216+
const opts = AcpClient.mock.calls[0][0] as { onLifecycle: (e: unknown) => void }
217+
const base = { agentId: 'triage', sessionKey: 't', sessionId: 'acp-session-1' }
218+
219+
// `AcpClient.ensureSession` emits this when resume/load throws, *before* it
220+
// falls through to newSession with the mcpServers minted a moment earlier.
221+
// The spawn is not dead — releasing it here would leave the fresh session
222+
// with a context server nobody owns, for the rest of its life.
223+
opts.onLifecycle({ ...base, outcome: 'failed', recoveryMethod: 'resume' })
224+
opts.onLifecycle({ ...base, outcome: 'failed', recoveryMethod: 'load' })
225+
expect(ended).not.toHaveBeenCalled()
226+
227+
// A failed *close* is a real end: that path sets `reason`.
228+
opts.onLifecycle({ ...base, outcome: 'failed', reason: 'idle' })
229+
expect(ended).toHaveBeenCalledExactlyOnceWith('triage', 't')
230+
})
231+
205232
it('stopAll() invokes stop on every started client', async () => {
206233
const { AcpClient } = (await import('@zooid/acp-client')) as unknown as {
207234
AcpClient: ReturnType<typeof vi.fn>

‎packages/core/src/acp-registry.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,18 @@ export class AcpAgentRegistry implements AcpRegistry {
464464
onTap: this.opts.onTap ? (e) => this.opts.onTap!(name, e) : undefined,
465465
onLifecycle: (e) => {
466466
this.opts.onLifecycle?.(name, e)
467-
// A closed session no longer owns its context spawn. `failed` means the
468-
// close attempt errored, but the client has already dropped the session
469-
// and will never prompt it again — the binding is equally dead. An
470-
// `unsupported` idle close keeps the session alive, so it is left alone.
471-
if (e.outcome === 'closed' || e.outcome === 'failed') {
467+
// A closed session no longer owns its context spawn. An `unsupported`
468+
// idle close keeps the session alive, so it is left alone.
469+
//
470+
// `failed` is only the end of a session when the *close* failed, which
471+
// is the path that sets `reason` ('idle' | 'clear'). The client also
472+
// emits `{ outcome: 'failed', reason: undefined }` when a restore
473+
// (`resumeSession`/`loadSession`) throws: it then falls straight through
474+
// to `newSession` with the mcpServers it minted moments earlier, so
475+
// releasing that spawn here would leave the fresh session with a dead
476+
// context server for its whole life — every `zooid_*` call answering
477+
// `binding not owned by caller`, with no way to repair it.
478+
if (e.outcome === 'closed' || (e.outcome === 'failed' && e.reason !== undefined)) {
472479
this.opts.onSessionEnded?.(name, e.sessionKey)
473480
}
474481
},

0 commit comments

Comments
 (0)