From 2462fbad2f9dbb9398f54622b8ab0972d4311269 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:35:15 +0100 Subject: [PATCH 1/7] test(server): delegated task result keeps the hook stop reason after progress Co-authored-by: Claude Opus 5.5 --- .../SubagentProjection.test.ts | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/SubagentProjection.test.ts b/apps/server/src/orchestration-v2/SubagentProjection.test.ts index 2f764233ba97b..3e53008c1af74 100644 --- a/apps/server/src/orchestration-v2/SubagentProjection.test.ts +++ b/apps/server/src/orchestration-v2/SubagentProjection.test.ts @@ -1,4 +1,5 @@ import { assert, it } from "@effect/vitest"; +import { expect } from "vite-plus/test"; import { CommandId, EnvironmentId, @@ -403,7 +404,7 @@ it("exposes the provider failure rather than a progress message from the failed assert.isNull(result.messageId); }); -it("reports a provider's stop reason for a completed run without an assistant result", () => { +it("reports a provider's stop reason with and without assistant progress", () => { const { projection, run } = taskFixture(); const notice = { id: IdAllocator.derive.runSignalTurnItem({ @@ -425,12 +426,35 @@ it("reports a provider's stop reason for a completed run without an assistant re updatedAt: childCreatedAt, type: "system_notice", level: "warning", - message: "sessionStart hook (user): Plugin guard blocked this session.", + message: "preCompact hook (project): Save a current task checkpoint before compaction.", } satisfies OrchestrationV2TurnItem; assert.equal( subagentResultForRun({ ...projection, turnItems: [notice] }, run).text, `Child task completed without an assistant result.\n\n${notice.message}`, ); + const progress = makeSubagentConversationArtifacts({ + messageId: MessageId.make("before-hook-stop"), + turnItemId: TurnItemId.make("before-hook-stop"), + threadId: parentThreadId, + rootNodeId: NodeId.make("root"), + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + role: "assistant", + text: "I am checking the remaining files.", + ordinal: 1, + now: childCreatedAt, + }); + const result = subagentResultForRun( + { + ...projection, + messages: [{ ...progress.message, runId: run.id }], + turnItems: [{ ...progress.turnItem, runId: run.id }, notice], + }, + run, + ); + expect(result.text).toContain(progress.message.text); + expect(result.text).toContain(notice.message); }); it("keeps the final report of a run that failed after its turn ended", () => { From ffe42b0cbdcd5206b5b8fbc04163b4dc54c5880c Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:35:51 +0100 Subject: [PATCH 2/7] fix(server): delegated task result keeps the hook stop reason after progress Co-authored-by: Claude Opus 5.5 --- .../provider-core/src/server/subagentProjection.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/provider-core/src/server/subagentProjection.ts b/packages/provider-core/src/server/subagentProjection.ts index f1b0a41905fb3..4afe6b86ae810 100644 --- a/packages/provider-core/src/server/subagentProjection.ts +++ b/packages/provider-core/src/server/subagentProjection.ts @@ -247,17 +247,20 @@ export function subagentResultForRun( failure !== undefined && turnItem !== null && turnItem.ordinal < failure.ordinal ? (message?.text ?? turnItem.text) : undefined; + const report = message?.text ?? turnItem?.text; const text = (failure?.type === "error" ? reportBeforeFailure === undefined ? failure.failure.message : `${failure.failure.message}\n\nLast assistant message before the run failed:\n\n${reportBeforeFailure}` : undefined) ?? - message?.text ?? - turnItem?.text ?? (run.status === "completed" - ? withStopNotice(projection, run, "Child task completed without an assistant result.") - : `Child task ended with status ${run.status}.`); + ? withStopNotice( + projection, + run, + report ?? "Child task completed without an assistant result.", + ) + : (report ?? `Child task ended with status ${run.status}.`)); const executedModel = latestProviderTurnForAttempt( projection.providerTurns, run.activeAttemptId, From 30dbae71ebddb0e6df99b2440242c4e64b2fa540 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:48:25 +0100 Subject: [PATCH 3/7] chore: credit the models that worked on this change Co-authored-by: GPT 6.1 Sol From 0f1d2992d21c5aeba0a4f0c306fa569ca236f0b6 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 01:37:52 +0000 Subject: [PATCH 4/7] test(server): assert the exact delegated result with progress and a stop notice Co-Authored-By: Claude Opus 5.5 --- .../SubagentProjection.test.ts | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/server/src/orchestration-v2/SubagentProjection.test.ts b/apps/server/src/orchestration-v2/SubagentProjection.test.ts index 3e53008c1af74..f7a4b754c60ca 100644 --- a/apps/server/src/orchestration-v2/SubagentProjection.test.ts +++ b/apps/server/src/orchestration-v2/SubagentProjection.test.ts @@ -1,5 +1,4 @@ import { assert, it } from "@effect/vitest"; -import { expect } from "vite-plus/test"; import { CommandId, EnvironmentId, @@ -426,7 +425,7 @@ it("reports a provider's stop reason with and without assistant progress", () => updatedAt: childCreatedAt, type: "system_notice", level: "warning", - message: "preCompact hook (project): Save a current task checkpoint before compaction.", + message: "sessionStart hook (user): Plugin guard blocked this session.", } satisfies OrchestrationV2TurnItem; assert.equal( subagentResultForRun({ ...projection, turnItems: [notice] }, run).text, @@ -445,16 +444,17 @@ it("reports a provider's stop reason with and without assistant progress", () => ordinal: 1, now: childCreatedAt, }); - const result = subagentResultForRun( - { - ...projection, - messages: [{ ...progress.message, runId: run.id }], - turnItems: [{ ...progress.turnItem, runId: run.id }, notice], - }, - run, + assert.equal( + subagentResultForRun( + { + ...projection, + messages: [{ ...progress.message, runId: run.id }], + turnItems: [{ ...progress.turnItem, runId: run.id }, notice], + }, + run, + ).text, + `${progress.message.text}\n\n${notice.message}`, ); - expect(result.text).toContain(progress.message.text); - expect(result.text).toContain(notice.message); }); it("keeps the final report of a run that failed after its turn ended", () => { From acf802cbb8a2edf4b2a4262e07cbc4712834f50b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 02:17:30 +0000 Subject: [PATCH 5/7] fix(server): delegated task result keeps a hook stop reason when Codex reports the turn interrupted Codex 0.169 ends a hook-stopped turn as interrupted, so the reason lands on the run's interrupt result rather than the provider stop notice. The delegated result now appends whichever of the two the run has. Co-Authored-By: Claude Opus 5.5 --- .../SubagentProjection.test.ts | 18 +++++++++++++ .../src/server/subagentProjection.ts | 27 ++++++++++++------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/apps/server/src/orchestration-v2/SubagentProjection.test.ts b/apps/server/src/orchestration-v2/SubagentProjection.test.ts index f7a4b754c60ca..038aac7642b17 100644 --- a/apps/server/src/orchestration-v2/SubagentProjection.test.ts +++ b/apps/server/src/orchestration-v2/SubagentProjection.test.ts @@ -455,6 +455,24 @@ it("reports a provider's stop reason with and without assistant progress", () => ).text, `${progress.message.text}\n\n${notice.message}`, ); + const interruptResult = { + ...notice, + id: IdAllocator.derive.runSignalTurnItem({ runId: run.id, signal: "interrupt-result" }), + status: "interrupted", + title: "Interrupted", + type: "run_interrupt_result", + } satisfies OrchestrationV2TurnItem; + assert.equal( + subagentResultForRun( + { + ...projection, + messages: [{ ...progress.message, runId: run.id }], + turnItems: [{ ...progress.turnItem, runId: run.id }, interruptResult], + }, + { ...run, status: "interrupted" }, + ).text, + `${progress.message.text}\n\n${notice.message}`, + ); }); it("keeps the final report of a run that failed after its turn ended", () => { diff --git a/packages/provider-core/src/server/subagentProjection.ts b/packages/provider-core/src/server/subagentProjection.ts index 4afe6b86ae810..3a7d54d7e6d64 100644 --- a/packages/provider-core/src/server/subagentProjection.ts +++ b/packages/provider-core/src/server/subagentProjection.ts @@ -198,9 +198,15 @@ function withStopNotice( run: OrchestrationV2Run, text: string, ): string { - const id = derive.runSignalTurnItem({ runId: run.id, signal: PROVIDER_STOP_REASON_SIGNAL }); - const notice = projection.turnItems.find((item) => item.id === id); - return notice?.type === "system_notice" ? `${text}\n\n${notice.message}` : text; + // Codex ends a hook-stopped turn as completed or interrupted, depending on its version. + const ids = [ + derive.runSignalTurnItem({ runId: run.id, signal: PROVIDER_STOP_REASON_SIGNAL }), + derive.runSignalTurnItem({ runId: run.id, signal: "interrupt-result" }), + ]; + const notice = projection.turnItems.find((item) => ids.includes(item.id)); + return notice?.type === "system_notice" || notice?.type === "run_interrupt_result" + ? `${text}\n\n${notice.message}` + : text; } export function subagentResultForRun( @@ -254,13 +260,14 @@ export function subagentResultForRun( ? failure.failure.message : `${failure.failure.message}\n\nLast assistant message before the run failed:\n\n${reportBeforeFailure}` : undefined) ?? - (run.status === "completed" - ? withStopNotice( - projection, - run, - report ?? "Child task completed without an assistant result.", - ) - : (report ?? `Child task ended with status ${run.status}.`)); + withStopNotice( + projection, + run, + report ?? + (run.status === "completed" + ? "Child task completed without an assistant result." + : `Child task ended with status ${run.status}.`), + ); const executedModel = latestProviderTurnForAttempt( projection.providerTurns, run.activeAttemptId, From f978152f6d871f90a1e8dc6d54eff80a4c99a261 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 02:33:29 +0000 Subject: [PATCH 6/7] fix(server): delegated results read the interrupt result that carries a hook stop reason Delegated completion and task_status load only the turn item types the result reads, and run_interrupt_result was not among them, so an interrupted child's hook reason never reached the result. Both reads now share one list next to subagentResultForRun. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/mcp/OrchestratorMcpService.ts | 3 +- .../DelegatedCompletionDelivery.test.ts | 68 ++++++++++++++++++- .../src/orchestration-v2/Orchestrator.ts | 3 +- .../src/server/subagentProjection.ts | 8 +++ 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index b7908d1d74878..5bdf5ab12ea58 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -91,6 +91,7 @@ import * as Stream from "effect/Stream"; import * as ProviderAdapterRegistry from "../orchestration-v2/ProviderAdapterRegistry.ts"; import { + SUBAGENT_RESULT_TURN_ITEM_TYPES, subagentResultForRun, delegatedTaskProgress, } from "@t3tools/provider-core/server/subagentProjection"; @@ -1746,7 +1747,7 @@ const make = Effect.gen(function* () { messageRoles: ["assistant"], messageRunIds: resultRunIds, turnItemRunIds: resultRunIds, - turnItemTypes: ["assistant_message", "error", "system_notice"], + turnItemTypes: SUBAGENT_RESULT_TURN_ITEM_TYPES, }) .pipe(Effect.mapError(threadManagementFailure)); const childProjection = { diff --git a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts index 72ea19a442dba..d048e8b26bd13 100644 --- a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts +++ b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts @@ -1406,7 +1406,7 @@ const seedRestartCancelledChild = (input: { readonly name: string; readonly completionWake: "always" | "settled_only"; readonly continuationPending: boolean; - readonly runStatus?: "cancelled" | "completed"; + readonly runStatus?: "cancelled" | "completed" | "interrupted"; readonly lostWork?: OrchestrationV2Run["restartCancelledBackgroundWork"]; readonly now: DateTime.Utc; }) => @@ -5061,6 +5061,72 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => { `Child task completed without an assistant result.\n\n${stopReason}`, ); assert.isUndefined(task?.cancelledRunId); + + // Codex 0.169 reports a hook-stopped turn as interrupted; its reason is the interrupt result. + const interrupted = yield* seedRestartCancelledChild({ + parentThreadId: threadId, + projectId, + parentRunId: runId, + rootNodeId, + name: "cancel-finished-interrupted-child", + completionWake: "always", + continuationPending: false, + runStatus: "interrupted", + now, + }); + yield* eventSink.write({ + commandId: CommandId.make("command:cancel-finished-interrupted-child:result"), + events: [ + { + id: EventId.make("event:cancel-finished-interrupted-child:result"), + type: "turn-item.updated", + threadId: interrupted.childThreadId, + runId: interrupted.childRunId, + nodeId: rootNodeId, + providerInstanceId: modelSelection.instanceId, + occurredAt: now, + payload: { + id: IdAllocator.derive.runSignalTurnItem({ + runId: interrupted.childRunId, + signal: "interrupt-result", + }), + type: "run_interrupt_result", + threadId: interrupted.childThreadId, + runId: interrupted.childRunId, + nodeId: rootNodeId, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 98, + status: "interrupted", + title: "Interrupted", + startedAt: now, + completedAt: now, + updatedAt: now, + message: stopReason, + }, + }, + ], + }); + yield* orchestrator.cancelDelegatedTask({ + parentThreadId: threadId, + taskId: interrupted.taskId, + command: { + type: "thread.stop", + commandId: CommandId.make("cancel-finished-interrupted-child"), + threadId: interrupted.childThreadId, + stopDelegatedTasks: false, + }, + stopDescendants: () => Effect.void, + }); + const interruptedTask = (yield* orchestrator.getThreadProjection(threadId)).subagents.find( + (row) => row.id === interrupted.taskId, + ); + assert.equal( + interruptedTask?.result, + `Child task ended with status interrupted.\n\n${stopReason}`, + ); }), ); diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 27b7a2e12878d..2eb719602e90c 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -191,6 +191,7 @@ import { endOrphanedNativeSubagent, endRunlessRootTurns, makeSubagentChildThread, + SUBAGENT_RESULT_TURN_ITEM_TYPES, subagentResultForRun, delegatedTaskProgress, subagentThreadTitle, @@ -14415,7 +14416,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio messageRoles: ["assistant"], messageRunIds: [childRun.id], turnItemRunId: childRun.id, - turnItemTypes: ["assistant_message", "error", "system_notice"], + turnItemTypes: SUBAGENT_RESULT_TURN_ITEM_TYPES, }, ); const childProjection = { diff --git a/packages/provider-core/src/server/subagentProjection.ts b/packages/provider-core/src/server/subagentProjection.ts index 3a7d54d7e6d64..90789f63266de 100644 --- a/packages/provider-core/src/server/subagentProjection.ts +++ b/packages/provider-core/src/server/subagentProjection.ts @@ -193,6 +193,14 @@ export function withExecutedModelNote( /** Run signal of the notice that carries a provider's stop reason on a completed run. */ export const PROVIDER_STOP_REASON_SIGNAL = "provider-stop-reason"; +/** Turn item types `subagentResultForRun` reads; load at least these for its projection. */ +export const SUBAGENT_RESULT_TURN_ITEM_TYPES = [ + "assistant_message", + "error", + "system_notice", + "run_interrupt_result", +] as const satisfies ReadonlyArray; + function withStopNotice( projection: Pick, run: OrchestrationV2Run, From 3e7dfce1274120f94b750c694c13a9dfa68cf795 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 02:53:49 +0000 Subject: [PATCH 7/7] fix(server): reading a child thread acknowledges its result only with the stop reason on the page A delegated result now ends with the run's stop reason, so a thread read that shows the final message but not the stop notice no longer acknowledges delivery. The read also loads the notice types the result uses. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/mcp/OrchestratorMcpService.ts | 32 +++++++++++++------ .../src/server/subagentProjection.ts | 29 +++++++++++++---- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 5bdf5ab12ea58..7f88078963acb 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -93,6 +93,7 @@ import * as ProviderAdapterRegistry from "../orchestration-v2/ProviderAdapterReg import { SUBAGENT_RESULT_TURN_ITEM_TYPES, subagentResultForRun, + subagentStopNotice, delegatedTaskProgress, } from "@t3tools/provider-core/server/subagentProjection"; import { @@ -699,15 +700,26 @@ function pageIncludesTerminalTaskResult(input: { const result = subagentResultForRun(input.target, run); if (result.messageId === null && result.turnItemId === null) return false; - return input.items.some((item) => { - if (item.sourceThreadId !== input.target.thread.id) return false; - const matchesResult = - (result.turnItemId !== null && item.itemId === result.turnItemId) || - (result.messageId !== null && - item.type === "assistant_message" && - item.messageId === result.messageId); - return matchesResult && item.text !== null && !item.textTruncated; - }); + const onPage = (matches: (item: OrchestratorMcpThreadTimelineItem) => boolean) => + input.items.some( + (item) => + item.sourceThreadId === input.target.thread.id && + matches(item) && + item.text !== null && + !item.textTruncated, + ); + // The result ends with the stop reason, so the page shows it only when it has both. + const notice = subagentStopNotice(input.target, run); + return ( + onPage( + (item) => + (result.turnItemId !== null && item.itemId === result.turnItemId) || + (result.messageId !== null && + item.type === "assistant_message" && + item.messageId === result.messageId), + ) && + (notice === undefined || onPage((item) => item.itemId === notice.id)) + ); } function latestTerminalResultRun( @@ -3393,7 +3405,7 @@ const make = Effect.gen(function* () { messageRoles: ["assistant"], messageRunIds: resultRunIds, turnItemRunIds: resultRunIds, - turnItemTypes: ["assistant_message", "error"], + turnItemTypes: SUBAGENT_RESULT_TURN_ITEM_TYPES, }) .pipe(Effect.mapError(threadManagementFailure)); if ( diff --git a/packages/provider-core/src/server/subagentProjection.ts b/packages/provider-core/src/server/subagentProjection.ts index 90789f63266de..6a35d8b281078 100644 --- a/packages/provider-core/src/server/subagentProjection.ts +++ b/packages/provider-core/src/server/subagentProjection.ts @@ -201,20 +201,35 @@ export const SUBAGENT_RESULT_TURN_ITEM_TYPES = [ "run_interrupt_result", ] as const satisfies ReadonlyArray; -function withStopNotice( +/** The item carrying a provider's stop reason for a run, which its delegated result ends with. */ +export function subagentStopNotice( projection: Pick, run: OrchestrationV2Run, - text: string, -): string { +) { // Codex ends a hook-stopped turn as completed or interrupted, depending on its version. const ids = [ derive.runSignalTurnItem({ runId: run.id, signal: PROVIDER_STOP_REASON_SIGNAL }), derive.runSignalTurnItem({ runId: run.id, signal: "interrupt-result" }), ]; - const notice = projection.turnItems.find((item) => ids.includes(item.id)); - return notice?.type === "system_notice" || notice?.type === "run_interrupt_result" - ? `${text}\n\n${notice.message}` - : text; + return projection.turnItems.find( + ( + item, + ): item is Extract< + OrchestrationV2TurnItem, + { readonly type: "system_notice" | "run_interrupt_result" } + > => + ids.includes(item.id) && + (item.type === "system_notice" || item.type === "run_interrupt_result"), + ); +} + +function withStopNotice( + projection: Pick, + run: OrchestrationV2Run, + text: string, +): string { + const notice = subagentStopNotice(projection, run); + return notice === undefined ? text : `${text}\n\n${notice.message}`; } export function subagentResultForRun(