diff --git a/packages/core/src/session/subagent-completion.ts b/packages/core/src/session/subagent-completion.ts index a06e5fdc4df..2b81afd652b 100644 --- a/packages/core/src/session/subagent-completion.ts +++ b/packages/core/src/session/subagent-completion.ts @@ -34,6 +34,8 @@ export function text(message: SessionMessage.Info | undefined) { const clean = (value: string) => value.replace(/\s+/g, " ").trim() +const STILL_RUNNING = "\n\nBackground work still running:\n" + export function formatBody( body: string, childSessionID: SessionSchema.ID, @@ -47,7 +49,7 @@ export function formatBody( return `- image (${item.model}${item.sessionID !== childSessionID ? `, in ${item.sessionID}` : ""}): ${item.path}` return `- subagent (${item.agent}, sessionID: ${item.childSessionID}): ${clean(item.description)}` }) - return `${body}\n\nBackground work still running:\n${lines.join("\n")}` + return `${body}${STILL_RUNNING}${lines.join("\n")}` } export const deliver = Effect.fnUntraced(function* ( @@ -65,11 +67,14 @@ export const deliver = Effect.fnUntraced(function* ( : input.status === "error" ? (input.error ?? "Subagent failed") : (input.output ?? input.error ?? "Subagent cancelled") + // The body is all a recovered outbox row keeps, so the still-running list is how a replay tells this period apart. + // Only the model-facing tag says "running": the job and the message metadata keep the period's real status. + const state = input.status === "completed" && body.includes(STILL_RUNNING) ? "running" : input.status yield* sessions.synthetic({ ...(input.notificationID ? { id: input.notificationID } : {}), sessionID: recovery.parentSessionID, description: recovery.description, - text: `\n${body}\n`, + text: `\n${body}\n`, metadata: { source: "subagent", childID: recovery.childSessionID, agent: recovery.agent, state: input.status }, }) if (input.notificationID) yield* jobs.completeBackground(input.notificationID) diff --git a/packages/core/test/session-execution.test.ts b/packages/core/test/session-execution.test.ts index 710d42367d8..b8ceea4e2de 100644 --- a/packages/core/test/session-execution.test.ts +++ b/packages/core/test/session-execution.test.ts @@ -1495,13 +1495,13 @@ describe("SessionRestart background recovery", () => { yield* Context.get(context, SessionRestart.Service).resumeSuspendedSessions yield* Deferred.await(parentWoken) - // Replay delivers the formatted row verbatim; the completed child never runs again. + // Replay delivers the formatted row verbatim, its still-running list labelling the tag; the child never runs again. expect(drained).toEqual([parent]) expect(yield* SessionInbox.list(database.db, parent)).toMatchObject([ { type: "synthetic", payload: { - text: `\n${output}\n`, + text: `\n${output}\n`, metadata: { source: "subagent", childID: child, agent: "explore", state: "completed" }, }, }, diff --git a/packages/core/test/subagent-completion.test.ts b/packages/core/test/subagent-completion.test.ts index e45e7bf6637..0abda3d1d21 100644 --- a/packages/core/test/subagent-completion.test.ts +++ b/packages/core/test/subagent-completion.test.ts @@ -59,7 +59,7 @@ describe("SubagentCompletion", () => { }), ) - it.effect("delivers completed, failed, and cancelled reports with pending background work", () => + it.effect("labels a completed report with pending background work as running, only in the model text", () => Effect.gen(function* () { const notificationID = SessionMessage.ID.create() const sent: SyntheticInput[] = [] @@ -88,11 +88,12 @@ describe("SubagentCompletion", () => { yield* SubagentCompletion.deliver(sessions, jobs, { status: "cancelled", recovery }) // A running job has nothing to report and must not be acknowledged. yield* SubagentCompletion.deliver(sessions, jobs, { status: "running", recovery }) + yield* SubagentCompletion.deliver(sessions, jobs, { status: "completed", output: "All done", recovery }) - expect(sent).toHaveLength(3) + expect(sent).toHaveLength(4) expect(sent[0]?.text).toBe( [ - '', + '', "All green", "", "Background work still running:", @@ -100,6 +101,12 @@ describe("SubagentCompletion", () => { "", ].join("\n"), ) + expect(sent[0]?.metadata?.state).toBe("completed") + expect(sent[3]?.text).toBe( + ['', "All done", ""].join( + "\n", + ), + ) expect(sent[1]?.text).toBe( [ '', diff --git a/packages/core/test/subagent-report.test.ts b/packages/core/test/subagent-report.test.ts index bec0c8d59d6..1051b7a567d 100644 --- a/packages/core/test/subagent-report.test.ts +++ b/packages/core/test/subagent-report.test.ts @@ -153,7 +153,7 @@ describe("SubagentReport settlement", () => { { payload: { text: [ - ``, + ``, "Started tests", "", "Background work still running:", @@ -162,6 +162,7 @@ describe("SubagentReport settlement", () => { `- subagent (subagent, sessionID: ${active}): ${active}`, "", ].join("\n"), + metadata: { state: "completed" }, }, }, ])