From b35b8d532f1df4723dbbd05de784ee18027e690a Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Wed, 4 Nov 2026 18:56:24 -0800 Subject: [PATCH] test(core): cover silent subagent restart replay Restart recovery already clears a silent child's durable marker without delivering a report. Pin that invariant: a suspended silent child resumed after a restart never reports or wakes its parent, and replaying a silent child's finished durable row admits nothing while still clearing the marker. --- packages/core/test/session-execution.test.ts | 56 ++++++++++++++ packages/core/test/tool-subagent.test.ts | 78 ++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/packages/core/test/session-execution.test.ts b/packages/core/test/session-execution.test.ts index bb51e486fbe..132c44b5313 100644 --- a/packages/core/test/session-execution.test.ts +++ b/packages/core/test/session-execution.test.ts @@ -1156,6 +1156,62 @@ describe("SessionRestart background recovery", () => { }), ) + it.effect("resumes a silent child after restart without reporting or waking its parent", () => + Effect.gen(function* () { + const database = yield* Database.Service + const jobs = yield* Job.Service + const bus = yield* Bus.Service + const parent = Session.ID.make("ses_subagent_silent_resume_parent") + const child = Session.ID.make("ses_subagent_silent_resume_child") + yield* seedSessions(database, [parent]) + yield* seedSessions(database, [child], { + parent_id: parent, + time_suspended: Date.now(), + metadata: { "subagent.notify": false }, + }) + yield* jobs.start({ + id: child, + type: "subagent", + recovery: { + kind: "subagent", + parentSessionID: parent, + childSessionID: child, + agent: "explore", + description: "Silent shutdown inspection", + }, + run: Effect.never, + }) + yield* jobs.background(child) + const marker = (yield* jobs.pendingBackground)[0] + if (!marker) return yield* Effect.die("background record missing") + + const answered = yield* Deferred.make() + const drained: Session.ID[] = [] + const scope = yield* Scope.make() + yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void)) + const restarted = yield* Job.make.pipe(Scope.provide(scope)) + const context = yield* buildExecution( + scope, + ({ sessionID }) => + sessionID === child + ? answer(bus, child, "Silent resumed result").pipe(Effect.andThen(Deferred.succeed(answered, undefined))) + : Effect.sync(() => void drained.push(sessionID)), + undefined, + restarted, + ) + const restart = Context.get(context, SessionRestart.Service) + yield* restart.resumeSuspendedSessions + yield* Deferred.await(answered) + yield* pendingEmpty(restarted) + + expect(drained).toEqual([]) + expect(yield* SessionInbox.list(database.db, parent)).toEqual([]) + expect(yield* restarted.pendingBackground).toEqual([]) + yield* restart.resumeSuspendedSessions + expect(yield* SessionInbox.list(database.db, parent)).toEqual([]) + }), + ) + it.effect("retains a subagent completion marker when synthetic admission conflicts", () => Effect.gen(function* () { const database = yield* Database.Service diff --git a/packages/core/test/tool-subagent.test.ts b/packages/core/test/tool-subagent.test.ts index fcdb0c6ec2d..cf37e473ae8 100644 --- a/packages/core/test/tool-subagent.test.ts +++ b/packages/core/test/tool-subagent.test.ts @@ -308,6 +308,84 @@ describe("SubagentTool", () => { ), ) + completionIt.live("replays a silent child's durable row without reporting to the parent", () => + tmpdirScoped().pipe( + Effect.flatMap((dir) => + Effect.gen(function* () { + const sessions = yield* Session.Service + const parent = yield* sessions.create({ + location: Location.Ref.make({ directory: AbsolutePath.make(dir.path) }), + model: parentModel, + title: "Silent recipient", + }) + yield* withSubagent(parent.location) + const locations = yield* LocationServiceMap.Service + const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location))) + const jobs = yield* Job.Service + const database = yield* Database.Service + + const result = yield* executeTool(registry, { + sessionID: parent.id, + ...toolIdentity, + call: { + type: "tool-call", + id: "call-silent-replay", + name: SubagentTool.name, + input: { + agent: "reviewer", + description: "quiet review", + prompt: "review", + background: true, + notify: false, + }, + }, + }) + const child = outputSessionID(result.metadata) + yield* jobs.wait({ id: child }) + yield* jobs.pendingBackground.pipe(Effect.repeat({ until: (pending) => pending.length === 0 })) + const synthetic = () => + sessions + .context(parent.id) + .pipe(Effect.map((messages) => messages.filter((message) => message.type === "synthetic"))) + expect(yield* synthetic()).toEqual([]) + + // The crash durable row a silent child's observer leaves behind: replaying it must clear + // the marker without admitting a report for the parent. + const notificationID = SessionMessage.ID.create() + yield* database.db + .insert(JobTable) + .values({ + notification_id: notificationID, + id: child, + session_id: parent.id, + child_session_id: child, + kind: "subagent", + title: "quiet review", + status: "completed", + output: "Silent replayed result", + error: null, + recovery: { + kind: "subagent", + parentSessionID: parent.id, + childSessionID: child, + agent: "reviewer", + description: "quiet review", + }, + }) + .run() + expect(yield* jobs.pendingBackground).toHaveLength(1) + const restart = yield* SessionRestart.Service + yield* restart.resumeSuspendedSessions + yield* jobs.pendingBackground.pipe(Effect.repeat({ until: (pending) => pending.length === 0 })) + + expect(yield* synthetic()).toEqual([]) + expect(yield* jobs.pendingBackground).toEqual([]) + expect(yield* SessionInbox.list(database.db, parent.id)).toEqual([]) + }), + ), + ), + ) + completionIt.live("returns a foreground child inline without reporting to the parent", () => tmpdirScoped().pipe( Effect.flatMap((dir) =>