@devries-cameron20 / t3code-1 / commits / 7d87be3596
Fork 0

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

fix(server): interrupts started by an agent no longer say "by user" in the timeline (#24299)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 23, 2026 at 19:22 UTC 7d87be359696acbc068d3c09d956acd2e4aff14b
5 files changed +82 -6
apps/server/src/mcp/OrchestratorMcpService.ts
+8 -3
@@ -278,6 +278,11 @@ function threadManagementFailure(error: unknown): OrchestratorMcpFailure {
278 }
279 }
280
281 +// Without a reason, a Stop reads as the user's (see RunExecutionService.USER_STOP_REASON).
282 +function agentStopReason(reason: string | undefined): string {
283 + return reason === undefined ? "Stopped by an agent" : `Stopped by an agent: ${reason}`;
284 +}
285 +
286 function errorMessage(error: unknown): string {
287 return error instanceof Error ? error.message : String(error);
288 }
@@ -2379,14 +2384,14 @@ const make = Effect.gen(function* () {
2384 );
2385 const stopChild = Effect.gen(function* () {
2386 const commandId = stableCommandId({ scope, requestKey: key, operation: "cancel-task" });
2382 - const reason = input.reason;
2387 + const reason = agentStopReason(input.reason);
2388 yield* threadManagement
2389 .dispatch({
2390 type: "thread.stop",
2391 commandId,
2392 threadId: current.childThreadId,
2393 stopDelegatedTasks: false,
2389 - ...(reason === undefined ? {} : { reason }),
2394 + reason,
2395 })
2396 .pipe(
2397 Effect.mapError((error) =>
@@ -2907,7 +2912,7 @@ const make = Effect.gen(function* () {
2912 }),
2913 threadId: input.threadId,
2914 ...(input.runId === undefined ? {} : { runId: input.runId }),
2910 - ...(input.reason === undefined ? {} : { reason: input.reason }),
2915 + reason: agentStopReason(input.reason),
2916 })
2917 .pipe(
2918 Effect.mapError((error) =>
apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts
+8
@@ -491,6 +491,14 @@ describe("orchestrator MCP toolkit", () => {
491 expect(finalProjection.runs.find((run) => run.id === queuedFollowup.runId)?.status).toBe(
492 "completed",
493 );
494 + expect(
495 + finalProjection.turnItems.find(
496 + (item) =>
497 + item.runId === runningFollowup.runId && item.type === "run_interrupt_result",
498 + ),
499 + ).toMatchObject({
500 + message: "Stopped by an agent: Allow the queued replay follow-up to run.",
501 + });
502 yield* orchestrator
503 .streamStoredEventsFrom({
504 threadId: parentThreadId,
apps/server/src/orchestration-v2/Orchestrator.ts
+28 -2
@@ -8977,6 +8977,31 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
8977 }
8978 });
8979
8980 + const runInterruptReason = Effect.fnUntraced(function* (
8981 + command: Extract<
8982 + OrchestrationV2ServerCommand,
8983 + { readonly type: "run.interrupt" | "thread.background-work.settle" }
8984 + >,
8985 + events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
8986 + runId: RunId,
8987 + ) {
8988 + const isRequest = (
8989 + item: OrchestrationV2TurnItem,
8990 + ): item is Extract<OrchestrationV2TurnItem, { readonly type: "run_interrupt_request" }> =>
8991 + item.runId === runId && item.type === "run_interrupt_request";
8992 + const pending = (yield* Ref.get(events))
8993 + .flatMap((event) => (event.type === "turn-item.updated" ? [event.payload] : []))
8994 + .findLast(isRequest);
8995 + if (pending !== undefined) return pending.message;
8996 + const { turnItems } = yield* projectionStore
8997 + .getThreadRecords(command.threadId, ["turnItems"], {
8998 + turnItemTypes: ["run_interrupt_request"],
8999 + turnItemRunIds: [runId],
9000 + })
9001 + .pipe(mapDispatchError(command));
9002 + return turnItems.find(isRequest)?.message;
9003 + });
9004 +
9005 // A successful Stop may have no terminal event to ingest: the session or
9006 // native turn already died, or its final write failed when storage filled.
9007 const settleInterruptedRun = Effect.fn("Orchestrator.settleInterruptedRun")(function* (input: {
@@ -9127,6 +9152,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
9152 run,
9153 rootNode,
9154 providerThread,
9155 + reason: yield* runInterruptReason(input.command, input.events, run.id),
9156 completedAt: input.now,
9157 }),
9158 });
@@ -9226,7 +9252,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
9252 completedAt: now,
9253 updatedAt: now,
9254 type: "run_interrupt_request",
9229 - message: command.reason ?? "Interrupt requested",
9255 + message: command.reason ?? RunExecutionService.USER_STOP_REASON,
9256 },
9257 });
9258 });
@@ -9755,7 +9781,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
9781 completedAt: now,
9782 updatedAt: now,
9783 type: "run_interrupt_request",
9758 - message: command.reason ?? "Interrupt requested",
9784 + message: command.reason ?? RunExecutionService.USER_STOP_REASON,
9785 };
9786
9787 const otherProviderInterrupts: Array<PendingOrchestrationEffectV2> = [];
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+14
@@ -191,6 +191,19 @@ export const layer: Layer.Layer<
191 }),
192 )
193 .pipe(Effect.catchCause(() => Effect.succeed(false))),
194 + loadRunInterruptReason: () =>
195 + projectionStore
196 + .getThreadRecords(input.threadId, ["turnItems"], {
197 + turnItemTypes: ["run_interrupt_request"],
198 + turnItemRunIds: [input.runId],
199 + })
200 + .pipe(
201 + Effect.map(({ turnItems }) => {
202 + const request = turnItems.find((item) => item.type === "run_interrupt_request");
203 + return request?.type === "run_interrupt_request" ? request.message : undefined;
204 + }),
205 + Effect.catchCause(() => Effect.succeed(undefined)),
206 + ),
207 };
208 };
209
@@ -1319,6 +1332,7 @@ export const layer: Layer.Layer<
1332 shouldStartProviderTurn: runControls.shouldStartProviderTurn,
1333 shouldFinalizeRun: runControls.shouldFinalizeRun,
1334 hasUnpairedRunInterruptRequest: runControls.hasUnpairedRunInterruptRequest,
1335 + loadRunInterruptReason: runControls.loadRunInterruptReason,
1336 message: {
1337 messageId: message.id,
1338 text: userText,
apps/server/src/orchestration-v2/RunExecutionService.ts
+24 -1
@@ -573,6 +573,7 @@ export interface RunExecutionServiceV2StartRootRunInput {
573 readonly shouldStartProviderTurn?: () => Effect.Effect<boolean, ProjectionStoreV2Error>;
574 readonly shouldFinalizeRun?: () => Effect.Effect<boolean, ProjectionStoreV2Error>;
575 readonly hasUnpairedRunInterruptRequest?: () => Effect.Effect<boolean, never>;
576 + readonly loadRunInterruptReason?: () => Effect.Effect<string | undefined, never>;
577 readonly message: ProviderAdapter.ProviderAdapterV2TurnMessage;
578 readonly modelSelection: ModelSelection;
579 readonly runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
@@ -621,6 +622,7 @@ export const layer: Layer.Layer<
622 readonly attempt: OrchestrationV2RunAttempt;
623 readonly shouldFinalizeRun?: () => Effect.Effect<boolean, ProjectionStoreV2Error>;
624 readonly hasUnpairedRunInterruptRequest?: () => Effect.Effect<boolean, never>;
625 + readonly loadRunInterruptReason?: () => Effect.Effect<string | undefined, never>;
626 readonly openRunOwnedSubagents?: OpenRunOwnedSubagentProjection;
627 readonly interruptedProviderTurn?: OrchestrationV2ProviderTurn;
628 readonly terminal: ProviderTerminalEvent;
@@ -647,6 +649,10 @@ export const layer: Layer.Layer<
649 ? false
650 : yield* input.hasUnpairedRunInterruptRequest();
651 if (hasUnpairedRequest) {
652 + const reason =
653 + input.loadRunInterruptReason === undefined
654 + ? undefined
655 + : yield* input.loadRunInterruptReason();
656 yield* eventSink.writeWithEffects({
657 effects: [],
658 events: [
@@ -663,6 +669,7 @@ export const layer: Layer.Layer<
669 run: input.run,
670 rootNode: input.rootNode,
671 providerThread: input.providerThread,
672 + reason,
673 completedAt,
674 }),
675 },
@@ -674,6 +681,10 @@ export const layer: Layer.Layer<
681 return;
682 }
683 const allocateEventId = () => idAllocator.allocate.event({ threadId: input.run.threadId });
684 + const reason =
685 + input.terminal.status === "interrupted" && input.loadRunInterruptReason !== undefined
686 + ? yield* input.loadRunInterruptReason()
687 + : undefined;
688 const open = input.openRunOwnedSubagents ?? emptyOpenRunOwnedSubagentProjection();
689 const hasOpenSubagentProjection =
690 open.subagents.size > 0 ||
@@ -791,6 +802,7 @@ export const layer: Layer.Layer<
802 run: input.run,
803 rootNode: input.rootNode,
804 providerThread: input.providerThread,
805 + reason,
806 completedAt,
807 }),
808 },
@@ -1060,6 +1072,9 @@ export const layer: Layer.Layer<
1072 : {
1073 hasUnpairedRunInterruptRequest: input.hasUnpairedRunInterruptRequest,
1074 }),
1075 + ...(input.loadRunInterruptReason === undefined
1076 + ? {}
1077 + : { loadRunInterruptReason: input.loadRunInterruptReason }),
1078 openRunOwnedSubagents: openSubagents,
1079 terminal,
1080 failureItemPersisted: terminal.status === "failed",
@@ -1684,11 +1699,19 @@ export const layer: Layer.Layer<
1699 }),
1700 );
1701
1702 +/**
1703 + * Clients send Stop without a reason; server callers acting for an agent
1704 + * always set one, so a missing reason means a person stopped the run.
1705 + */
1706 +export const USER_STOP_REASON = "Stopped by user";
1707 +
1708 export function makeInterruptResultTurnItem(input: {
1709 readonly idAllocator: IdAllocator.IdAllocatorV2["Service"];
1710 readonly run: OrchestrationV2Run;
1711 readonly rootNode: OrchestrationV2ExecutionNode;
1712 readonly providerThread: OrchestrationV2ProviderThread;
1713 + /** The Stop's own reason, from its run_interrupt_request item. */
1714 + readonly reason: string | undefined;
1715 readonly completedAt: DateTime.Utc;
1716 }): OrchestrationV2TurnItem {
1717 return {
@@ -1713,6 +1736,6 @@ export function makeInterruptResultTurnItem(input: {
1736 completedAt: input.completedAt,
1737 updatedAt: input.completedAt,
1738 type: "run_interrupt_result",
1716 - message: "Run interrupted by user",
1739 + message: input.reason ?? "Stopped by the provider",
1740 };
1741 }