@devries-cameron20 / t3code Mirror · read-only / commits / c02b98bb3f
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 09:45 UTC

fix(server): a Codex hook that stops a delegated child no longer reads as a silent success (#37738)

Julius Marminge committed Nov 16, 2026 at 11:50 UTC c02b98bb3fdcbf02a5ca4db2d81d09226c4755ec
9 files changed +147 -5
apps/server/src/mcp/OrchestratorMcpService.ts
+1 -1
@@ -1746,7 +1746,7 @@ const make = Effect.gen(function* () {
1746 messageRoles: ["assistant"],
1747 messageRunIds: resultRunIds,
1748 turnItemRunIds: resultRunIds,
1749 - turnItemTypes: ["assistant_message", "error"],
1749 + turnItemTypes: ["assistant_message", "error", "system_notice"],
1750 })
1751 .pipe(Effect.mapError(threadManagementFailure));
1752 const childProjection = {
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+1 -1
@@ -3743,7 +3743,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3743 { hook: "wrong-turn", status: "interrupted", expected: false },
3744 { hook: "wrong-thread", status: "interrupted", expected: false },
3745 { hook: "unscoped", status: "interrupted", expected: false },
3746 - { hook: "stopped", status: "completed", expected: false },
3746 + { hook: "stopped", status: "completed", expected: true },
3747 { hook: "stopped", status: "failed", expected: false },
3748 ] as const)(
3749 "correlates $hook hook output with a $status turn only",
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+1 -1
@@ -6435,7 +6435,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6435 runOrdinal: input.context.input.runOrdinal,
6436 status: terminalStatus,
6437 failure: null,
6438 - ...(terminalStatus === "interrupted" && input.context.hookStopReason !== undefined
6438 + ...(input.context.hookStopReason !== undefined
6439 ? { reason: input.context.hookStopReason }
6440 : {}),
6441 threadDisposition: "reusable",
apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
+43
@@ -57,6 +57,7 @@ import * as ProviderTurnStartServiceTestkit from "./ProviderTurnStartService.tes
57 import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
58 import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
59 import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
60 +import { PROVIDER_STOP_REASON_SIGNAL } from "@t3tools/provider-core/server/subagentProjection";
61 import * as ProviderContinuationService from "./ProviderContinuationService.ts";
62
63 const layerPlatformTest = Layer.merge(
@@ -4652,6 +4653,7 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
4653 it.effect("cancelling a task whose child already finished keeps its completed result", () =>
4654 Effect.gen(function* () {
4655 const orchestrator = yield* Orchestrator.OrchestratorV2;
4656 + const eventSink = yield* EventSink.EventSinkV2;
4657 const now = yield* DateTime.now;
4658 const threadId = ThreadId.make("thread:cancel-finished-parent");
4659 const projectId = ProjectId.make("project:cancel-finished-parent");
@@ -4678,6 +4680,43 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
4680 runStatus: "completed",
4681 now,
4682 });
4683 + const stopReason = "sessionStart hook (user): Plugin guard blocked this session.";
4684 + yield* eventSink.write({
4685 + commandId: CommandId.make("command:cancel-finished-child:stop-reason"),
4686 + events: [
4687 + {
4688 + id: EventId.make("event:cancel-finished-child:stop-reason"),
4689 + type: "turn-item.updated",
4690 + threadId: child.childThreadId,
4691 + runId: child.childRunId,
4692 + nodeId: rootNodeId,
4693 + providerInstanceId: modelSelection.instanceId,
4694 + occurredAt: now,
4695 + payload: {
4696 + id: IdAllocator.derive.runSignalTurnItem({
4697 + runId: child.childRunId,
4698 + signal: PROVIDER_STOP_REASON_SIGNAL,
4699 + }),
4700 + type: "system_notice",
4701 + level: "warning",
4702 + threadId: child.childThreadId,
4703 + runId: child.childRunId,
4704 + nodeId: rootNodeId,
4705 + providerThreadId: null,
4706 + providerTurnId: null,
4707 + nativeItemRef: null,
4708 + parentItemId: null,
4709 + ordinal: 98,
4710 + status: "completed",
4711 + title: "Stopped by the provider",
4712 + startedAt: now,
4713 + completedAt: now,
4714 + updatedAt: now,
4715 + message: stopReason,
4716 + },
4717 + },
4718 + ],
4719 + });
4720 yield* orchestrator.cancelDelegatedTask({
4721 parentThreadId: threadId,
4722 taskId: child.taskId,
@@ -4693,6 +4732,10 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
4732 (row) => row.id === child.taskId,
4733 );
4734 assert.equal(task?.status, "completed");
4735 + assert.equal(
4736 + task?.result,
4737 + `Child task completed without an assistant result.\n\n${stopReason}`,
4738 + );
4739 assert.isUndefined(task?.cancelledRunId);
4740 }),
4741 );
apps/server/src/orchestration-v2/Orchestrator.ts
+1 -1
@@ -14428,7 +14428,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14428 messageRoles: ["assistant"],
14429 messageRunIds: [childRun.id],
14430 turnItemRunId: childRun.id,
14431 - turnItemTypes: ["assistant_message", "error"],
14431 + turnItemTypes: ["assistant_message", "error", "system_notice"],
14432 },
14433 );
14434 const childProjection = {
apps/server/src/orchestration-v2/RunExecutionService.test.ts
+15
@@ -3183,6 +3183,21 @@ it.effect.each([
3183 }),
3184 );
3185
3186 +it.effect("keeps a provider's stop reason on a completed run", () =>
3187 + Effect.gen(function* () {
3188 + const reason = "sessionStart hook (user): Plugin guard blocked this session.";
3189 + const { written } = yield* captureRootRunTermination({
3190 + key: "completed-stop-reason",
3191 + shouldFinalizeRun: () => Effect.succeed(true),
3192 + events: (ids) => Stream.make({ ...rootTerminalEvent(ids, "completed"), reason }),
3193 + });
3194 + assert.deepEqual(
3195 + written.map((item) => (item.type === "system_notice" ? item.message : item.type)),
3196 + [reason],
3197 + );
3198 + }),
3199 +);
3200 +
3201 it.effect("records a finished run as failed when its ownership check cannot be read", () =>
3202 Effect.gen(function* () {
3203 const { observed } = yield* captureRootRunTermination({
apps/server/src/orchestration-v2/RunExecutionService.ts
+37
@@ -46,6 +46,7 @@ import * as CheckpointService from "./CheckpointService.ts";
46 import * as EventSink from "./EventSink.ts";
47 import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
48 import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
49 +import { PROVIDER_STOP_REASON_SIGNAL } from "@t3tools/provider-core/server/subagentProjection";
50 import * as ProviderEventIngestor from "./ProviderEventIngestor.ts";
51 import * as ProviderUsageAnalytics from "../telemetry/ProviderUsageAnalytics.ts";
52 import type { ProjectionStoreV2Error } from "./ProjectionStore.ts";
@@ -915,6 +916,42 @@ export const layer: Layer.Layer<
916 },
917 ]
918 : []),
919 + // Codex reports a hook that stops the turn as completed; keep its reason visible.
920 + ...(input.terminal.status === "completed" && input.terminal.reason !== undefined
921 + ? [
922 + {
923 + id: yield* allocateEventId(),
924 + type: "turn-item.updated" as const,
925 + threadId: input.run.threadId,
926 + runId: input.run.id,
927 + nodeId: input.rootNode.id,
928 + providerInstanceId: input.run.providerInstanceId,
929 + occurredAt: completedAt,
930 + payload: {
931 + id: idAllocator.derive.runSignalTurnItem({
932 + runId: input.run.id,
933 + signal: PROVIDER_STOP_REASON_SIGNAL,
934 + }),
935 + threadId: input.run.threadId,
936 + runId: input.run.id,
937 + nodeId: input.rootNode.id,
938 + providerThreadId: input.providerThread.id,
939 + providerTurnId: input.rootNode.providerTurnId,
940 + nativeItemRef: null,
941 + parentItemId: null,
942 + ordinal: input.run.ordinal * 100 + 98,
943 + status: "completed" as const,
944 + title: "Stopped by the provider",
945 + startedAt: completedAt,
946 + completedAt,
947 + updatedAt: completedAt,
948 + type: "system_notice" as const,
949 + level: "warning" as const,
950 + message: input.terminal.reason,
951 + },
952 + },
953 + ]
954 + : []),
955 ...(input.terminal.status === "failed" && !input.failureItemPersisted
956 ? [
957 {
apps/server/src/orchestration-v2/SubagentProjection.test.ts
+33
@@ -10,6 +10,7 @@ import {
10 EventId,
11 TurnItemId,
12 type OrchestrationV2Run,
13 + type OrchestrationV2TurnItem,
14 type OrchestrationV2AppThread,
15 ProjectId,
16 ProviderInstanceId,
@@ -27,7 +28,9 @@ import {
28 makeSubagentConversationArtifacts,
29 endOrphanedNativeSubagent,
30 endRunlessRootTurns,
31 + PROVIDER_STOP_REASON_SIGNAL,
32 } from "@t3tools/provider-core/server/subagentProjection";
33 +import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
34
35 import * as SqlitePersistence from "../persistence/Sqlite.ts";
36 import * as ProjectionStore from "./ProjectionStore.ts";
@@ -400,6 +403,36 @@ it("exposes the provider failure rather than a progress message from the failed
403 assert.isNull(result.messageId);
404 });
405
406 +it("reports a provider's stop reason for a completed run without an assistant result", () => {
407 + const { projection, run } = taskFixture();
408 + const notice = {
409 + id: IdAllocator.derive.runSignalTurnItem({
410 + runId: run.id,
411 + signal: PROVIDER_STOP_REASON_SIGNAL,
412 + }),
413 + threadId: parentThreadId,
414 + runId: run.id,
415 + nodeId: NodeId.make("root"),
416 + providerThreadId: null,
417 + providerTurnId: null,
418 + nativeItemRef: null,
419 + parentItemId: null,
420 + ordinal: 98,
421 + status: "completed",
422 + title: "Stopped by the provider",
423 + startedAt: childCreatedAt,
424 + completedAt: childCreatedAt,
425 + updatedAt: childCreatedAt,
426 + type: "system_notice",
427 + level: "warning",
428 + message: "sessionStart hook (user): Plugin guard blocked this session.",
429 + } satisfies OrchestrationV2TurnItem;
430 + assert.equal(
431 + subagentResultForRun({ ...projection, turnItems: [notice] }, run).text,
432 + `Child task completed without an assistant result.\n\n${notice.message}`,
433 + );
434 +});
435 +
436 it("keeps the final report of a run that failed after its turn ended", () => {
437 const { projection, run } = taskFixture();
438 const failedRun = { ...run, status: "failed" as const };
packages/provider-core/src/server/subagentProjection.ts
+15 -1
@@ -22,6 +22,7 @@ import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2Pe
22 import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
23 import * as DateTime from "effect/DateTime";
24 import * as Effect from "effect/Effect";
25 +import { derive } from "./IdAllocator.ts";
26 import { isOrchestrationV2WorkActive, latestProviderTurnForAttempt } from "@t3tools/contracts";
27
28 function trimmed(value: string | null | undefined): string | undefined {
@@ -189,6 +190,19 @@ export function withExecutedModelNote(
190 : `Requested model ${requestedModel}; the provider reported running ${executedModel}.\n\n${result}`;
191 }
192
193 +/** Run signal of the notice that carries a provider's stop reason on a completed run. */
194 +export const PROVIDER_STOP_REASON_SIGNAL = "provider-stop-reason";
195 +
196 +function withStopNotice(
197 + projection: Pick<OrchestrationV2ThreadProjection, "turnItems">,
198 + run: OrchestrationV2Run,
199 + text: string,
200 +): string {
201 + const id = derive.runSignalTurnItem({ runId: run.id, signal: PROVIDER_STOP_REASON_SIGNAL });
202 + const notice = projection.turnItems.find((item) => item.id === id);
203 + return notice?.type === "system_notice" ? `${text}\n\n${notice.message}` : text;
204 +}
205 +
206 export function subagentResultForRun(
207 projection: Pick<OrchestrationV2ThreadProjection, "messages" | "turnItems" | "providerTurns">,
208 run: OrchestrationV2Run,
@@ -242,7 +256,7 @@ export function subagentResultForRun(
256 message?.text ??
257 turnItem?.text ??
258 (run.status === "completed"
245 - ? "Child task completed without an assistant result."
259 + ? withStopNotice(projection, run, "Child task completed without an assistant result.")
260 : `Child task ended with status ${run.status}.`);
261 const executedModel = latestProviderTurnForAttempt(
262 projection.providerTurns,