Skip to content

Commit 813da49

Browse files
fix(server): Stop always ends the background work a thread shows
The Waiting strip's Stop was accepted but could be dropped: the control service skipped any settled turn whose adapter has no per-thread pending-work probe, the Codex probe missed work its process no longer tracked, and with no live session the Stop was rejected. The strip kept showing the work. A settled Stop now always reaches the adapter, which stops what it still runs or reports nothing is left. Afterwards a settle command marks interrupted whatever the thread still shows on that provider thread, or on provider threads with no live session; with no live session the Stop settles directly. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 0fbd755 commit 813da49

18 files changed

Lines changed: 558 additions & 44 deletions

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3987,6 +3987,126 @@ describe("CodexAdapterV2 post-settle continuation", () => {
39873987
}
39883988
}
39893989

3990+
// The app-server exits after the root turn, before the command's own
3991+
// item/completed (Codex always sends one, so only a lost notification or a
3992+
// gone process leaves it running). Nothing tracks the command any more, yet
3993+
// the thread still shows it, and Stop is the only way to clear it.
3994+
it.effect("Stop ends a background command no Codex process tracks any more", () =>
3995+
Effect.scoped(
3996+
Effect.gen(function* () {
3997+
const fs = yield* FileSystem.FileSystem;
3998+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-bg-stale-workspace-" });
3999+
const staleTranscript = makeCodexReplayTranscript({
4000+
scenario: "codex-bg-stop-untracked",
4001+
entries: [
4002+
...backgroundExecTranscript.entries.slice(0, -1),
4003+
{ type: "runtime_exit", status: "success" },
4004+
],
4005+
});
4006+
const localTranscript = yield* decodeReplayTranscriptJson(
4007+
(yield* encodeReplayTranscriptJson(staleTranscript)).replaceAll(
4008+
yield* encodeStringJson("/workspace"),
4009+
yield* encodeStringJson(cwd),
4010+
),
4011+
);
4012+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
4013+
for (const args of [
4014+
["init", "--quiet"],
4015+
[
4016+
"-c",
4017+
"user.name=Test",
4018+
"-c",
4019+
4020+
"commit",
4021+
"--allow-empty",
4022+
"--quiet",
4023+
"-m",
4024+
"Initial commit",
4025+
],
4026+
]) {
4027+
assert.equal(Number(yield* spawner.exitCode(ChildProcess.make("git", args, { cwd }))), 0);
4028+
}
4029+
yield* Effect.gen(function* () {
4030+
const orchestrator = yield* Orchestrator.OrchestratorV2;
4031+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
4032+
const threadId = ThreadId.make("thread:background-stop-untracked");
4033+
yield* orchestrator.dispatch({
4034+
type: "thread.create",
4035+
commandId: CommandId.make("create-background-stop-untracked"),
4036+
threadId,
4037+
projectId: ProjectId.make("project:background-stop-untracked"),
4038+
title: "Background stop untracked",
4039+
modelSelection: CODEX_TEST_MODEL_SELECTION,
4040+
runtimeMode: "full-access",
4041+
interactionMode: "default",
4042+
branch: null,
4043+
worktreePath: cwd,
4044+
createdBy: "user",
4045+
creationSource: "web",
4046+
});
4047+
const settled = yield* orchestrator.streamDomainEvents.pipe(
4048+
Stream.filter(
4049+
(event) =>
4050+
event.type === "run.updated" &&
4051+
(event.payload.status === "waiting" || event.payload.status === "completed"),
4052+
),
4053+
Stream.runHead,
4054+
Effect.forkChild({ startImmediately: true }),
4055+
);
4056+
yield* orchestrator.dispatch({
4057+
type: "message.dispatch",
4058+
commandId: CommandId.make("start-background-stop-untracked"),
4059+
threadId,
4060+
messageId: MessageId.make("message:background-stop-untracked"),
4061+
text: BG_PROMPT,
4062+
attachments: [],
4063+
createdBy: "user",
4064+
creationSource: "web",
4065+
dispatchMode: { type: "start_immediately" },
4066+
});
4067+
yield* worker.drain();
4068+
yield* Fiber.join(settled);
4069+
yield* worker.drain();
4070+
const before = yield* orchestrator.getThreadShell(threadId);
4071+
assert.deepEqual(
4072+
before?.pendingBackgroundTasks?.map((task) => task.kind),
4073+
["command"],
4074+
"the thread still shows the command the gone process never finished",
4075+
);
4076+
const run = (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)!;
4077+
yield* orchestrator.dispatch({
4078+
type: "run.interrupt",
4079+
commandId: CommandId.make("stop-background-untracked"),
4080+
threadId,
4081+
runId: run.id,
4082+
holdQueue: true,
4083+
});
4084+
yield* worker.drain();
4085+
const projection = yield* orchestrator.getThreadProjection(threadId);
4086+
assert.deepEqual(
4087+
projection.turnItems.flatMap((item) =>
4088+
item.type === "command_execution" ? [item.status] : [],
4089+
),
4090+
["interrupted"],
4091+
);
4092+
assert.equal(projection.runs.at(-1)?.status, "completed");
4093+
assert.deepEqual(
4094+
(yield* orchestrator.getThreadShell(threadId))?.pendingBackgroundTasks,
4095+
[],
4096+
);
4097+
}).pipe(
4098+
Effect.provide(
4099+
makeOrchestratorV2ReplayLayerWithRegistry(
4100+
{ name: "codex-background-stop-untracked", runtimePolicyOverride: { cwd } },
4101+
makeCodexProviderAdapterRegistryReplayLayer({ transcript: localTranscript }),
4102+
{ runEffectWorker: false },
4103+
),
4104+
),
4105+
);
4106+
}).pipe(Effect.provide(NodeServices.layer)),
4107+
),
4108+
);
4109+
39904110
const PRE_SETTLE_SCENARIO = "codex-bg-exec-pre-settle";
39914111
const PRE_SETTLE_NATIVE_THREAD = "native-codex-pre-settle-thread";
39924112
const PRE_SETTLE_NATIVE_TURN = "native-codex-pre-settle-turn";

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,10 +2042,19 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
20422042

20432043
const terminateBackgroundTerminal = Effect.fn("CodexAdapterV2.terminateBackgroundTerminal")(
20442044
function* (nativeThreadId: string, processId: string) {
2045-
const response = yield* client.raw.request("thread/backgroundTerminals/terminate", {
2046-
threadId: nativeThreadId,
2047-
processId,
2048-
});
2045+
const response = yield* client.raw
2046+
.request("thread/backgroundTerminals/terminate", {
2047+
threadId: nativeThreadId,
2048+
processId,
2049+
})
2050+
.pipe(
2051+
// The app-server that ran the terminal is gone, and with it
2052+
// the only handle to the terminal: nothing is left to stop.
2053+
Effect.catchTags({
2054+
CodexAppServerProcessExitedError: () => Effect.succeed({ terminated: true }),
2055+
CodexAppServerInputStreamEndedError: () => Effect.succeed({ terminated: true }),
2056+
}),
2057+
);
20492058
const result = yield* decodeCodexBackgroundTerminalTerminateResponse(response);
20502059
if (result.terminated) return;
20512060
let cursor: string | null = null;
@@ -5653,6 +5662,9 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
56535662
)
56545663
: undefined);
56555664
if (activeTurn === undefined) {
5665+
// Stop on a settled turn this process retains nothing for
5666+
// (released, restarted, or every command already reported).
5667+
if (turnInput.requestRuntimeRestart === true) return;
56565668
return yield* toProtocolError(
56575669
`Provider turn ${turnInput.providerTurnId} is not active and cannot be interrupted.`,
56585670
);

‎apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,9 @@ export function makeCursorAdapterV2(
24292429
interruptTurn: Effect.fn("CursorAdapterV2.interruptTurn")(
24302430
function* (turnInput: ProviderAdapter.ProviderAdapterV2InterruptInput) {
24312431
const context = yield* Ref.get(activeTurn);
2432+
// Stop on a settled turn: finalization already ended its tools
2433+
// and subagents, so nothing of it is left running to stop.
2434+
if (context === null && turnInput.requestRuntimeRestart === true) return;
24322435
if (context?.providerTurnId !== turnInput.providerTurnId) {
24332436
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
24342437
driver: CursorAgentSdk.CURSOR_PROVIDER,

‎apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3385,6 +3385,17 @@ export function makeOpenCodeAdapterV2(
33853385
const sessionId = nativeThreadId(interruptInput.providerThread);
33863386
const state = threads.get(sessionId);
33873387
const turn = state?.activeTurn;
3388+
// Stop on a settled turn: a background task child can outlive
3389+
// it (busySessionIds), and aborting the descendants ends it.
3390+
if (
3391+
(turn === undefined || turn === null) &&
3392+
interruptInput.requestRuntimeRestart === true
3393+
) {
3394+
for (const controller of commandControllers.get(sessionId) ?? [])
3395+
controller.abort();
3396+
yield* abortDescendants(sessionId);
3397+
return;
3398+
}
33883399
if (
33893400
turn === undefined ||
33903401
turn === null ||

‎apps/server/src/orchestration-v2/Adapters/PiAdapterV2.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,6 +2466,9 @@ export function makePiAdapterV2(
24662466
interruptTurn: (interruptInput) =>
24672467
Effect.gen(function* () {
24682468
const turn = threadState?.activeTurn ?? null;
2469+
// Stop on a settled turn: Pi runs nothing between prompts, so
2470+
// nothing of that turn is left to stop.
2471+
if (turn === null && interruptInput.requestRuntimeRestart === true) return;
24692472
if (turn === null || turn.providerTurn.id !== interruptInput.providerTurnId) {
24702473
return yield* protocolError(`Pi turn ${interruptInput.providerTurnId} is not active`);
24712474
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ export const executorLayer: Layer.Layer<
166166
providerTurnId: effect.request.providerTurnId,
167167
})
168168
.pipe(
169+
// The provider has stopped what it still ran and reported it.
170+
// Whatever the thread still shows on that provider thread is
171+
// work no process will report on, so the Stop ends it too.
172+
Effect.andThen(
173+
threads.dispatch({
174+
type: "thread.background-work.settle",
175+
commandId: CommandId.make(`${effect.commandId}:background-work-settled`),
176+
threadId: effect.threadId,
177+
providerThreadId: effect.request.providerThreadId,
178+
}),
179+
),
169180
Effect.mapError(
170181
(cause) =>
171182
new OrchestrationEffectExecutionError({

0 commit comments

Comments
 (0)