Skip to content

Commit 5e9d5e0

Browse files
fix(server): interrupts started by an agent no longer say "by user" in the timeline
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4fef307 commit 5e9d5e0

5 files changed

Lines changed: 82 additions & 6 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ function threadManagementFailure(error: unknown): OrchestratorMcpFailure {
278278
}
279279
}
280280

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+
281286
function errorMessage(error: unknown): string {
282287
return error instanceof Error ? error.message : String(error);
283288
}
@@ -2379,14 +2384,14 @@ const make = Effect.gen(function* () {
23792384
);
23802385
const stopChild = Effect.gen(function* () {
23812386
const commandId = stableCommandId({ scope, requestKey: key, operation: "cancel-task" });
2382-
const reason = input.reason;
2387+
const reason = agentStopReason(input.reason);
23832388
yield* threadManagement
23842389
.dispatch({
23852390
type: "thread.stop",
23862391
commandId,
23872392
threadId: current.childThreadId,
23882393
stopDelegatedTasks: false,
2389-
...(reason === undefined ? {} : { reason }),
2394+
reason,
23902395
})
23912396
.pipe(
23922397
Effect.mapError((error) =>
@@ -2907,7 +2912,7 @@ const make = Effect.gen(function* () {
29072912
}),
29082913
threadId: input.threadId,
29092914
...(input.runId === undefined ? {} : { runId: input.runId }),
2910-
...(input.reason === undefined ? {} : { reason: input.reason }),
2915+
reason: agentStopReason(input.reason),
29112916
})
29122917
.pipe(
29132918
Effect.mapError((error) =>

‎apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,14 @@ describe("orchestrator MCP toolkit", () => {
491491
expect(finalProjection.runs.find((run) => run.id === queuedFollowup.runId)?.status).toBe(
492492
"completed",
493493
);
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+
});
494502
yield* orchestrator
495503
.streamStoredEventsFrom({
496504
threadId: parentThreadId,

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8977,6 +8977,31 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
89778977
}
89788978
});
89798979

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+
89809005
// A successful Stop may have no terminal event to ingest: the session or
89819006
// native turn already died, or its final write failed when storage filled.
89829007
const settleInterruptedRun = Effect.fn("Orchestrator.settleInterruptedRun")(function* (input: {
@@ -9127,6 +9152,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
91279152
run,
91289153
rootNode,
91299154
providerThread,
9155+
reason: yield* runInterruptReason(input.command, input.events, run.id),
91309156
completedAt: input.now,
91319157
}),
91329158
});
@@ -9226,7 +9252,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
92269252
completedAt: now,
92279253
updatedAt: now,
92289254
type: "run_interrupt_request",
9229-
message: command.reason ?? "Interrupt requested",
9255+
message: command.reason ?? RunExecutionService.USER_STOP_REASON,
92309256
},
92319257
});
92329258
});
@@ -9755,7 +9781,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
97559781
completedAt: now,
97569782
updatedAt: now,
97579783
type: "run_interrupt_request",
9758-
message: command.reason ?? "Interrupt requested",
9784+
message: command.reason ?? RunExecutionService.USER_STOP_REASON,
97599785
};
97609786

97619787
const otherProviderInterrupts: Array<PendingOrchestrationEffectV2> = [];

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ export const layer: Layer.Layer<
191191
}),
192192
)
193193
.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+
),
194207
};
195208
};
196209

@@ -1319,6 +1332,7 @@ export const layer: Layer.Layer<
13191332
shouldStartProviderTurn: runControls.shouldStartProviderTurn,
13201333
shouldFinalizeRun: runControls.shouldFinalizeRun,
13211334
hasUnpairedRunInterruptRequest: runControls.hasUnpairedRunInterruptRequest,
1335+
loadRunInterruptReason: runControls.loadRunInterruptReason,
13221336
message: {
13231337
messageId: message.id,
13241338
text: userText,

‎apps/server/src/orchestration-v2/RunExecutionService.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ export interface RunExecutionServiceV2StartRootRunInput {
573573
readonly shouldStartProviderTurn?: () => Effect.Effect<boolean, ProjectionStoreV2Error>;
574574
readonly shouldFinalizeRun?: () => Effect.Effect<boolean, ProjectionStoreV2Error>;
575575
readonly hasUnpairedRunInterruptRequest?: () => Effect.Effect<boolean, never>;
576+
readonly loadRunInterruptReason?: () => Effect.Effect<string | undefined, never>;
576577
readonly message: ProviderAdapter.ProviderAdapterV2TurnMessage;
577578
readonly modelSelection: ModelSelection;
578579
readonly runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
@@ -621,6 +622,7 @@ export const layer: Layer.Layer<
621622
readonly attempt: OrchestrationV2RunAttempt;
622623
readonly shouldFinalizeRun?: () => Effect.Effect<boolean, ProjectionStoreV2Error>;
623624
readonly hasUnpairedRunInterruptRequest?: () => Effect.Effect<boolean, never>;
625+
readonly loadRunInterruptReason?: () => Effect.Effect<string | undefined, never>;
624626
readonly openRunOwnedSubagents?: OpenRunOwnedSubagentProjection;
625627
readonly interruptedProviderTurn?: OrchestrationV2ProviderTurn;
626628
readonly terminal: ProviderTerminalEvent;
@@ -647,6 +649,10 @@ export const layer: Layer.Layer<
647649
? false
648650
: yield* input.hasUnpairedRunInterruptRequest();
649651
if (hasUnpairedRequest) {
652+
const reason =
653+
input.loadRunInterruptReason === undefined
654+
? undefined
655+
: yield* input.loadRunInterruptReason();
650656
yield* eventSink.writeWithEffects({
651657
effects: [],
652658
events: [
@@ -663,6 +669,7 @@ export const layer: Layer.Layer<
663669
run: input.run,
664670
rootNode: input.rootNode,
665671
providerThread: input.providerThread,
672+
reason,
666673
completedAt,
667674
}),
668675
},
@@ -674,6 +681,10 @@ export const layer: Layer.Layer<
674681
return;
675682
}
676683
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;
677688
const open = input.openRunOwnedSubagents ?? emptyOpenRunOwnedSubagentProjection();
678689
const hasOpenSubagentProjection =
679690
open.subagents.size > 0 ||
@@ -791,6 +802,7 @@ export const layer: Layer.Layer<
791802
run: input.run,
792803
rootNode: input.rootNode,
793804
providerThread: input.providerThread,
805+
reason,
794806
completedAt,
795807
}),
796808
},
@@ -1060,6 +1072,9 @@ export const layer: Layer.Layer<
10601072
: {
10611073
hasUnpairedRunInterruptRequest: input.hasUnpairedRunInterruptRequest,
10621074
}),
1075+
...(input.loadRunInterruptReason === undefined
1076+
? {}
1077+
: { loadRunInterruptReason: input.loadRunInterruptReason }),
10631078
openRunOwnedSubagents: openSubagents,
10641079
terminal,
10651080
failureItemPersisted: terminal.status === "failed",
@@ -1684,11 +1699,19 @@ export const layer: Layer.Layer<
16841699
}),
16851700
);
16861701

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+
16871708
export function makeInterruptResultTurnItem(input: {
16881709
readonly idAllocator: IdAllocator.IdAllocatorV2["Service"];
16891710
readonly run: OrchestrationV2Run;
16901711
readonly rootNode: OrchestrationV2ExecutionNode;
16911712
readonly providerThread: OrchestrationV2ProviderThread;
1713+
/** The Stop's own reason, from its run_interrupt_request item. */
1714+
readonly reason: string | undefined;
16921715
readonly completedAt: DateTime.Utc;
16931716
}): OrchestrationV2TurnItem {
16941717
return {
@@ -1713,6 +1736,6 @@ export function makeInterruptResultTurnItem(input: {
17131736
completedAt: input.completedAt,
17141737
updatedAt: input.completedAt,
17151738
type: "run_interrupt_result",
1716-
message: "Run interrupted by user",
1739+
message: input.reason ?? "Stopped by the provider",
17171740
};
17181741
}

0 commit comments

Comments
 (0)