The best way to control Claude Code, Codex and more from any computer or phone
fix(server): stop threads without resolving a run (#22052)
Yash Singh committed
Oct 19, 2026 at 19:50 UTC
fb990765a49414a5bfbdc73365455fc8b5fbe691
9 files changed
+77
-9
apps/server/src/environment/ServerEnvironment.ts
+1
@@ -255,6 +255,7 @@ export const make = Effect.gen(function* () {
255
pullRequestStackActions: true,
256
threadPullRequestLinking: true,
257
serverResolvedCommandContext: true,
258
+ threadStop: true,
259
environmentIcon: true,
260
projectCloneTracking: true,
261
...(serverSelfUpdate === null ? {} : { serverSelfUpdate }),
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+2
-3
@@ -4993,7 +4993,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
4993
);
4994
},
4995
);
4996
- it.effect("interrupts a completed run's background command through orchestration", () => {
4996
+ it.effect("stops a completed run's background command without a client-supplied run", () => {
4997
const transcript = makeBackgroundStopTranscript(true);
4998
return Effect.scoped(
4999
Effect.gen(function* () {
@@ -5091,10 +5091,9 @@ describe("CodexAdapterV2 post-settle continuation", () => {
5091
Effect.forkChild({ startImmediately: true }),
5092
);
5093
yield* orchestrator.dispatch({
5094
- type: "run.interrupt",
5094
+ type: "thread.stop",
5095
commandId: CommandId.make("stop-background-command"),
5096
threadId,
5097
- runId: run.id,
5097
});
5098
yield* worker.drain();
5099
yield* Fiber.join(stopped);
apps/server/src/orchestration-v2/testkit/OrchestratorScenario.ts
+1
@@ -131,6 +131,7 @@ export class OrchestratorV2ScenarioStepError extends Schema.TaggedError<Orchestr
131
function commandThreadIds(command: OrchestrationV2Command): ReadonlyArray<ThreadId> {
132
switch (command.type) {
133
case "thread.create":
134
+ case "thread.stop":
135
case "thread.archive":
136
case "thread.unarchive":
137
case "thread.delete":
packages/client-runtime/src/operations/commands.test.ts
+31
@@ -79,6 +79,7 @@ const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(funct
79
readonly projection?: OrchestrationV2ThreadProjection;
80
readonly projectionRequests?: ThreadId[];
81
readonly advertiseServerResolvedCommandContext?: boolean;
82
+ readonly advertiseThreadStop?: boolean;
83
}) {
84
const client = {
85
[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) =>
@@ -126,6 +127,7 @@ const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(funct
127
environment: {
128
capabilities: {
129
repositoryIdentity: true,
130
+ ...(input.advertiseThreadStop === true ? { threadStop: true } : {}),
131
...(input.advertiseServerResolvedCommandContext === false
132
? {}
133
: { serverResolvedCommandContext: true }),
@@ -1049,6 +1051,34 @@ describe("V2 environment commands", () => {
1051
}).pipe(Effect.provide(layerTestCrypto)),
1052
);
1053
1054
+ it.effect("stops a thread without relying on background work in the history window", () =>
1055
+ Effect.gen(function* () {
1056
+ const commands: OrchestrationV2Command[] = [];
1057
+ const projectionRequests: ThreadId[] = [];
1058
+ const supervisor = yield* makeSupervisor({
1059
+ commands,
1060
+ projects: [],
1061
+ projectionRequests,
1062
+ advertiseThreadStop: true,
1063
+ });
1064
+
1065
+ const result = yield* interruptThreadTurn({
1066
+ commandId: CommandId.make("stop-outside-history-window"),
1067
+ threadId: v2ThreadId,
1068
+ }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
1069
+
1070
+ expect(result).toEqual({ sequence: 1 });
1071
+ expect(projectionRequests).toEqual([]);
1072
+ expect(commands).toEqual([
1073
+ {
1074
+ type: "thread.stop",
1075
+ commandId: "stop-outside-history-window",
1076
+ threadId: v2ThreadId,
1077
+ },
1078
+ ]);
1079
+ }).pipe(Effect.provide(layerTestCrypto)),
1080
+ );
1081
+
1082
it.effect("interrupts a known run without fetching the full projection", () =>
1083
Effect.gen(function* () {
1084
const commands: OrchestrationV2Command[] = [];
@@ -1057,6 +1087,7 @@ describe("V2 environment commands", () => {
1087
commands,
1088
projects: [],
1089
projectionRequests,
1090
+ advertiseThreadStop: true,
1091
});
1092
1093
yield* interruptThreadTurn({
packages/client-runtime/src/operations/commands.ts
+11
@@ -782,6 +782,17 @@ export const interruptThreadTurn = Effect.fn("EnvironmentCommands.interruptThrea
782
});
783
}
784
let runId = input.runId ?? (input.turnId as RunId | undefined);
785
+ if (runId === undefined) {
786
+ const config = yield* getInitialServerConfig();
787
+ if (config.environment.capabilities.threadStop === true) {
788
+ // A paginated projection can omit commands still running from earlier turns.
789
+ return yield* dispatch({
790
+ type: "thread.stop",
791
+ commandId: yield* allocateCommandId(input),
792
+ threadId: input.threadId,
793
+ });
794
+ }
795
+ }
796
if (runId === undefined) {
797
const projection = yield* getProjection(input.threadId);
798
runId = projection.runs.findLast(
packages/contracts/src/environment.test.ts
+10
@@ -110,4 +110,14 @@ describe("ExecutionEnvironmentDescriptor", () => {
110
}).capabilities.serverResolvedCommandContext,
111
).toBe(true);
112
});
113
+
114
+ it("enables thread Stop only when advertised by the server", () => {
115
+ expect(decodeDescriptor(descriptor).capabilities.threadStop).toBeUndefined();
116
+ expect(
117
+ decodeDescriptor({
118
+ ...descriptor,
119
+ capabilities: { ...descriptor.capabilities, threadStop: true },
120
+ }).capabilities.threadStop,
121
+ ).toBe(true);
122
+ });
123
});
packages/contracts/src/environment.ts
+1
@@ -125,6 +125,7 @@ export const ExecutionEnvironmentCapabilities = Schema.Struct({
125
threadVisitedTracking: Schema.optionalKey(Schema.Boolean),
126
threadPullRequestLinking: Schema.optionalKey(Schema.Boolean),
127
serverResolvedCommandContext: Schema.optionalKey(Schema.Boolean),
128
+ threadStop: Schema.optionalKey(Schema.Boolean),
129
threadPullRequests: Schema.optionalKey(Schema.Boolean),
130
threadPullRequestWatch: Schema.optionalKey(Schema.Boolean),
131
pullRequestStackActions: Schema.optionalKey(Schema.Boolean),
packages/contracts/src/orchestrationV2.test.ts
+14
@@ -625,6 +625,20 @@ describe("orchestration V2 contracts", () => {
625
expect(event.payload.id).toBe(RunId.make("run-1"));
626
});
627
628
+ it("accepts a thread Stop without a client-resolved run", () => {
629
+ expect(
630
+ decodeOrchestrationV2Command({
631
+ type: "thread.stop",
632
+ commandId: "stop-thread",
633
+ threadId: "thread-1",
634
+ }),
635
+ ).toEqual({
636
+ type: "thread.stop",
637
+ commandId: "stop-thread",
638
+ threadId: "thread-1",
639
+ });
640
+ });
641
+
642
it("decodes app-owned delegated task commands", () => {
643
const command = decodeOrchestrationV2Command({
644
type: "delegated_task.request",
packages/contracts/src/orchestrationV2.ts
+6
-6
@@ -2553,6 +2553,12 @@ export const OrchestrationV2StoredEventJson = Schema.Struct({
2553
export type OrchestrationV2StoredEventJson = typeof OrchestrationV2StoredEventJson.Type;
2554
2555
export const OrchestrationV2Command = Schema.Union([
2556
+ Schema.Struct({
2557
+ type: Schema.Literal("thread.stop"),
2558
+ commandId: CommandId,
2559
+ threadId: ThreadId,
2560
+ reason: Schema.optional(Schema.String),
2561
+ }),
2562
Schema.Struct({
2563
type: Schema.Literal("thread.create"),
2564
...OrchestrationV2CreationFields,
@@ -3036,12 +3042,6 @@ const OrchestrationV2InternalCommand = Schema.Union([
3042
}),
3043
),
3044
}),
3039
- Schema.Struct({
3040
- type: Schema.Literal("thread.stop"),
3041
- commandId: CommandId,
3042
- threadId: ThreadId,
3043
- reason: Schema.optional(Schema.String),
3044
- }),
3045
/**
3046
* Internal so no client can mark a request saved without the value being
3047
* stored.