Skip to content

Commit 4743813

Browse files
fix(server): Stop settles leftover work even when Claude has no process left
A settled-turn Stop on a Claude session whose CLI process had already exited failed with "has no live query", so the settle follow-up never ran and the Waiting strip kept the work. A settled-turn Stop with nothing live is now a no-op success in the Claude adapter too, as in Codex, Cursor, Pi, OpenCode and ACP; it also drops the gone process's roster. The settle follow-up now carries the stopped provider turn and ends only that run's and older runs' background work, so a later run's work survives an earlier Stop. A settle that ends nothing records its receipt, so a replayed Stop effect hits it instead of settling work that appeared since. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 813da49 commit 4743813

6 files changed

Lines changed: 333 additions & 25 deletions

File tree

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3492,6 +3492,51 @@ describe("ClaudeAdapterV2 background wake turns", () => {
34923492
),
34933493
);
34943494

3495+
// The CLI process exits on its own after the turn settled (idle, crash),
3496+
// leaving its background task on the roster. Stop must succeed so the
3497+
// orchestrator goes on to settle what the thread still shows.
3498+
it.effect("a settled Stop with no CLI process left succeeds and clears the roster", () =>
3499+
Effect.scoped(
3500+
Effect.gen(function* () {
3501+
const harness = yield* makeWakeHarnessWithOptions();
3502+
const now = yield* DateTime.now;
3503+
yield* harness.runtime.startTurn(
3504+
makeClaudeTestTurnInput({
3505+
threadId: harness.threadId,
3506+
providerThread: harness.providerThread,
3507+
now,
3508+
attemptId: RunAttemptId.make("attempt-claude-settled-stop-no-process"),
3509+
text: "Run the build in the background.",
3510+
attachments: [],
3511+
}),
3512+
);
3513+
yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
3514+
yield* Queue.offer(harness.sdkMessages, turnOneResult);
3515+
yield* awaitUntil(() => harness.terminalEvents().length === 1, "first turn terminal");
3516+
const settledThread = providerThreadRosterEvents(harness.events).at(-1)?.providerThread;
3517+
assert.equal(settledThread?.pendingBackgroundTasks?.[0]?.taskId, WAKE_TASK_ID);
3518+
3519+
yield* Queue.shutdown(harness.sdkMessages);
3520+
let quietYields = 0;
3521+
yield* awaitUntil(() => quietYields++ >= 50, "query exit");
3522+
3523+
yield* harness.runtime.interruptTurn({
3524+
providerThread: settledThread ?? harness.providerThread,
3525+
providerTurnId: harness.terminalEvents()[0]!.providerTurnId,
3526+
requestRuntimeRestart: true,
3527+
});
3528+
yield* awaitUntil(
3529+
() =>
3530+
(providerThreadRosterEvents(harness.events).at(-1)?.providerThread
3531+
.pendingBackgroundTasks?.length ?? 0) === 0,
3532+
"roster clear after Stop",
3533+
);
3534+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
3535+
assert.lengthOf(harness.continuationRequests, 0);
3536+
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
3537+
),
3538+
);
3539+
34953540
it.effect("a settled Stop leaves a turn that replaced the closing process alone", () =>
34963541
Effect.scoped(
34973542
Effect.gen(function* () {

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6940,22 +6940,23 @@ export function makeClaudeAdapterV2(
69406940
const interruptTurn = Effect.fn("ClaudeAdapterV2.interruptTurn")(
69416941
function* (turnInput: ProviderAdapter.ProviderAdapterV2InterruptInput) {
69426942
const existing = yield* Ref.get(queryContext);
6943-
if (existing === null) {
6944-
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
6945-
driver: CLAUDE_PROVIDER,
6946-
detail: `Claude provider thread ${turnInput.providerThread.id} has no live query.`,
6947-
});
6948-
}
69496943
const currentTurn = yield* Ref.get(activeTurn);
69506944
const nativeThreadId = turnInput.providerThread.nativeThreadRef?.nativeId ?? null;
6951-
if (
6952-
currentTurn === null &&
6953-
turnInput.requestRuntimeRestart === true &&
6954-
nativeThreadId !== null &&
6955-
existing.nativeThreadId === nativeThreadId
6956-
) {
6957-
// Stop after the turn settled: the background shells belong to
6958-
// the CLI process, so closing its query is what stops them.
6945+
if (currentTurn === null && turnInput.requestRuntimeRestart === true) {
6946+
// Stop after the turn settled. With no CLI process of this
6947+
// native thread left, nothing it started is still running: its
6948+
// roster is not authoritative any more, and the orchestrator
6949+
// settles the items the thread still shows.
6950+
if (nativeThreadId === null) return;
6951+
if (existing === null || existing.nativeThreadId !== nativeThreadId) {
6952+
yield* clearWakeStateForNativeThread(nativeThreadId);
6953+
yield* resetBackgroundTaskStateForNativeThreadProcess(nativeThreadId, {
6954+
status: "idle",
6955+
});
6956+
return;
6957+
}
6958+
// The background shells belong to the CLI process, so closing
6959+
// its query is what stops them.
69596960
yield* closeLiveQueryForNativeThread(nativeThreadId);
69606961
// A turn started while the close was pending may have opened a
69616962
// replacement process. Its Waiting and wake state are its own.
@@ -6968,6 +6969,12 @@ export function makeClaudeAdapterV2(
69686969
}
69696970
return;
69706971
}
6972+
if (existing === null) {
6973+
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
6974+
driver: CLAUDE_PROVIDER,
6975+
detail: `Claude provider thread ${turnInput.providerThread.id} has no live query.`,
6976+
});
6977+
}
69716978
if (currentTurn?.providerTurnId !== turnInput.providerTurnId) {
69726979
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
69736980
driver: CLAUDE_PROVIDER,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ export const executorLayer: Layer.Layer<
175175
commandId: CommandId.make(`${effect.commandId}:background-work-settled`),
176176
threadId: effect.threadId,
177177
providerThreadId: effect.request.providerThreadId,
178+
providerTurnId: effect.request.providerTurnId,
178179
}),
179180
),
180181
Effect.mapError(

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import {
99
ProviderDriverKind,
1010
ProviderInstanceId,
1111
ProviderSessionId,
12+
ProviderThreadId,
13+
ProviderTurnId,
14+
RunAttemptId,
15+
RunId,
1216
RuntimeRequestId,
1317
ThreadId,
1418
TurnItemId,
@@ -329,3 +333,204 @@ it.effect("implements a proposed plan that the command projection leaves out", (
329333
assert.equal((yield* projections.getPlan(threadId, planId))?.status, "completed");
330334
}).pipe(Effect.provide(testLayer)),
331335
);
336+
337+
// Stop's settle follow-up runs after the provider interrupt returns, possibly
338+
// long after the Stop (retries) or again (an effect replayed after a crash).
339+
// A later run's background work is not that Stop's to end.
340+
it.effect("settles only the stopped run's background work, once", () =>
341+
Effect.gen(function* () {
342+
const orchestrator = yield* Orchestrator.OrchestratorV2;
343+
const projections = yield* ProjectionStore.ProjectionStoreV2;
344+
const threadId = ThreadId.make("thread:settle-binding");
345+
const providerThreadId = ProviderThreadId.make("provider-thread:settle-binding");
346+
const now = yield* DateTime.now;
347+
yield* orchestrator.dispatch({
348+
type: "thread.create",
349+
commandId: CommandId.make("create-settle-binding"),
350+
threadId,
351+
projectId: ProjectId.make("project:settle-binding"),
352+
title: "Settle binding",
353+
modelSelection,
354+
runtimeMode: "full-access",
355+
interactionMode: "default",
356+
branch: null,
357+
worktreePath: null,
358+
createdBy: "user",
359+
creationSource: "web",
360+
});
361+
yield* projections.apply({
362+
id: EventId.make("settle-binding:provider-thread"),
363+
type: "provider-thread.updated",
364+
threadId,
365+
occurredAt: now,
366+
payload: {
367+
id: providerThreadId,
368+
driver: adapter.driver,
369+
providerInstanceId: instanceId,
370+
providerSessionId: null,
371+
appThreadId: threadId,
372+
ownerNodeId: null,
373+
nativeThreadRef: null,
374+
nativeConversationHeadRef: null,
375+
status: "idle",
376+
firstRunOrdinal: 1,
377+
lastRunOrdinal: 2,
378+
handoffIds: [],
379+
forkedFrom: null,
380+
createdAt: now,
381+
updatedAt: now,
382+
},
383+
});
384+
const commandItem = (ordinal: number) => TurnItemId.make(`turn-item:settle-binding:${ordinal}`);
385+
for (const ordinal of [1, 2]) {
386+
const runId = RunId.make(`run:settle-binding:${ordinal}`);
387+
const attemptId = RunAttemptId.make(`attempt:settle-binding:${ordinal}`);
388+
const nodeId = NodeId.make(`node:settle-binding:${ordinal}`);
389+
const providerTurnId = ProviderTurnId.make(`provider-turn:settle-binding:${ordinal}`);
390+
yield* projections.apply({
391+
id: EventId.make(`settle-binding:run:${ordinal}`),
392+
type: "run.created",
393+
threadId,
394+
occurredAt: now,
395+
payload: {
396+
id: runId,
397+
threadId,
398+
ordinal,
399+
providerInstanceId: instanceId,
400+
modelSelection,
401+
providerThreadId,
402+
userMessageId: MessageId.make(`message:settle-binding:${ordinal}`),
403+
rootNodeId: nodeId,
404+
activeAttemptId: attemptId,
405+
status: "completed",
406+
requestedAt: now,
407+
startedAt: now,
408+
completedAt: now,
409+
checkpointId: null,
410+
contextHandoffId: null,
411+
},
412+
});
413+
yield* projections.apply({
414+
id: EventId.make(`settle-binding:attempt:${ordinal}`),
415+
type: "run-attempt.created",
416+
threadId,
417+
occurredAt: now,
418+
payload: {
419+
id: attemptId,
420+
runId,
421+
attemptOrdinal: 1,
422+
rootNodeId: nodeId,
423+
providerInstanceId: instanceId,
424+
providerThreadId,
425+
providerTurnId,
426+
reason: "initial",
427+
status: "completed",
428+
startedAt: now,
429+
completedAt: now,
430+
},
431+
});
432+
yield* projections.apply({
433+
id: EventId.make(`settle-binding:turn:${ordinal}`),
434+
type: "provider-turn.updated",
435+
threadId,
436+
occurredAt: now,
437+
payload: {
438+
id: providerTurnId,
439+
providerThreadId,
440+
nodeId,
441+
runAttemptId: attemptId,
442+
nativeTurnRef: null,
443+
ordinal,
444+
status: "completed",
445+
startedAt: now,
446+
completedAt: now,
447+
},
448+
});
449+
yield* projections.apply({
450+
id: EventId.make(`settle-binding:item:${ordinal}`),
451+
type: "turn-item.updated",
452+
threadId,
453+
runId,
454+
occurredAt: now,
455+
payload: {
456+
id: commandItem(ordinal),
457+
threadId,
458+
runId,
459+
nodeId,
460+
providerThreadId,
461+
providerTurnId,
462+
nativeItemRef: null,
463+
parentItemId: null,
464+
ordinal: ordinal * 10,
465+
status: "running",
466+
title: `Background command ${ordinal}`,
467+
startedAt: now,
468+
completedAt: null,
469+
updatedAt: now,
470+
type: "command_execution",
471+
input: `sleep ${ordinal}`,
472+
},
473+
});
474+
}
475+
const itemStatuses = Effect.map(projections.getThreadProjection(threadId), (projection) =>
476+
projection.turnItems
477+
.flatMap((item) => (item.type === "command_execution" ? [`${item.id}:${item.status}`] : []))
478+
.toSorted(),
479+
);
480+
// The settle that followed a Stop of run 1's turn, dispatched only after
481+
// run 2 had settled with work of its own.
482+
const settle = {
483+
type: "thread.background-work.settle",
484+
commandId: CommandId.make("stop-run-1:background-work-settled"),
485+
threadId,
486+
providerThreadId,
487+
providerTurnId: ProviderTurnId.make("provider-turn:settle-binding:1"),
488+
} as const;
489+
yield* orchestrator.dispatch(settle);
490+
assert.deepEqual(yield* itemStatuses, [
491+
`${commandItem(1)}:interrupted`,
492+
`${commandItem(2)}:running`,
493+
]);
494+
495+
// A settle that found nothing to end replays as a no-op, even after work
496+
// it would match appears: its receipt is recorded with no events.
497+
const emptySettle = {
498+
...settle,
499+
commandId: CommandId.make("stop-run-1-again:background-work-settled"),
500+
};
501+
const first = yield* orchestrator.dispatch(emptySettle);
502+
assert.lengthOf(first.storedEvents, 0);
503+
yield* projections.apply({
504+
id: EventId.make("settle-binding:item:late"),
505+
type: "turn-item.updated",
506+
threadId,
507+
runId: RunId.make("run:settle-binding:1"),
508+
occurredAt: now,
509+
payload: {
510+
id: commandItem(3),
511+
threadId,
512+
runId: RunId.make("run:settle-binding:1"),
513+
nodeId: NodeId.make("node:settle-binding:1"),
514+
providerThreadId,
515+
providerTurnId: ProviderTurnId.make("provider-turn:settle-binding:1"),
516+
nativeItemRef: null,
517+
parentItemId: null,
518+
ordinal: 30,
519+
status: "running",
520+
title: "Late background command",
521+
startedAt: now,
522+
completedAt: null,
523+
updatedAt: now,
524+
type: "command_execution",
525+
input: "sleep 3",
526+
},
527+
});
528+
const replayed = yield* orchestrator.dispatch(emptySettle);
529+
assert.lengthOf(replayed.storedEvents, 0);
530+
assert.deepEqual(yield* itemStatuses, [
531+
`${commandItem(1)}:interrupted`,
532+
`${commandItem(2)}:running`,
533+
`${commandItem(3)}:running`,
534+
]);
535+
}).pipe(Effect.provide(testLayer)),
536+
);

0 commit comments

Comments
 (0)