Skip to content

Commit 176a3eb

Browse files
Alb11747codexclaude
committed
fix(claude): a wake handled by a queued message no longer stalls the thread
Mark buffered wake output settled when a user turn consumes its result. The queued continuation replays that output and finishes, while a later wake reopens the buffer. Preserve all visible output. Co-Authored-By: GPT-6.1-Sol at high effort <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Co-Authored-By: Codex, investigation and reproduction subagent in an earlier session <[email protected]> Co-Authored-By: GPT-6.1-Sol at xhigh effort, review subagent for the wake stall fix <[email protected]>
1 parent 4774336 commit 176a3eb

2 files changed

Lines changed: 132 additions & 7 deletions

File tree

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,22 @@ const captureSdkExecutablePaths = Effect.fn("captureSdkExecutablePaths")(functio
12881288
});
12891289

12901290
describe("ClaudeAdapterV2 executable path", () => {
1291+
it.effect("does not mutate the shared host environment when defaulting the API timeout", () => {
1292+
const sharedEnvironment = { PATH: "/test/bin" };
1293+
return Effect.scoped(
1294+
Effect.gen(function* () {
1295+
const { apiTimeoutEnvironment } = yield* captureSdkExecutablePaths("claude");
1296+
assert.deepEqual(apiTimeoutEnvironment, ["3600000"]);
1297+
assert.deepEqual(sharedEnvironment, { PATH: "/test/bin" });
1298+
}),
1299+
).pipe(
1300+
Effect.provideService(HostProcess.Environment, sharedEnvironment),
1301+
Effect.provide(
1302+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
1303+
),
1304+
);
1305+
});
1306+
12911307
it.effect.each([
12921308
{ source: "the default", environment: [], hostEnvironment: {}, expected: "3600000" },
12931309
{
@@ -8730,6 +8746,84 @@ describe("ClaudeAdapterV2 background wake turns", () => {
87308746
),
87318747
);
87328748

8749+
it.effect("settles a deferred wake whose init was consumed by an intervening prompt", () =>
8750+
Effect.scoped(
8751+
Effect.gen(function* () {
8752+
const harness = yield* makeWakeHarness;
8753+
const now = yield* DateTime.now;
8754+
const start = (
8755+
attemptId: string,
8756+
providerTurnOrdinal: number,
8757+
messageCreationSource: "server" | "provider" = "server",
8758+
) =>
8759+
harness.runtime.startTurn(
8760+
makeClaudeTestTurnInput({
8761+
threadId: harness.threadId,
8762+
providerThread: harness.providerThread,
8763+
now,
8764+
attemptId: RunAttemptId.make(attemptId),
8765+
text: "Check the completed background work.",
8766+
attachments: [],
8767+
providerTurnOrdinal,
8768+
messageCreatedBy: "agent",
8769+
messageCreationSource,
8770+
}),
8771+
);
8772+
8773+
yield* start("attempt-stale-init-first", 1);
8774+
yield* harness.offerAndWait(wakeTaskStarted);
8775+
// The native background command completes during the original turn.
8776+
yield* harness.offerAndWait(wakeNotification);
8777+
yield* harness.offerAndWait(turnOneResult);
8778+
yield* Queue.take(harness.terminalReceipts);
8779+
8780+
// Claude opens its native wake and starts answering while the T3
8781+
// continuation is still queued.
8782+
yield* harness.offerAndWait(wakeTurnInit);
8783+
yield* Queue.take(harness.continuationReceipts);
8784+
yield* harness.offerAndWait(wakeAssistant);
8785+
8786+
// A normal queued completion message gets to the native process first.
8787+
yield* start("attempt-stale-init-intervening", 2);
8788+
const combinedResult = claudeSdkFrame({
8789+
...makeResultFrame({
8790+
uuid: "00000000-0000-4000-8000-000000000121",
8791+
result: "The wake and completion message were handled together.",
8792+
numTurns: 7,
8793+
origin: { kind: "task-notification" },
8794+
}),
8795+
user_message_uuid: harness.promptUuid(1),
8796+
});
8797+
yield* harness.offerAndWait(combinedResult);
8798+
const combinedTerminal = yield* Queue.take(harness.terminalReceipts);
8799+
assert.equal(combinedTerminal.status, "completed");
8800+
8801+
// The delayed continuation still shows the wake's buffered output, but
8802+
// must not wait for a result the intervening turn already received: it
8803+
// settles at once, so the next start is accepted instead of failing
8804+
// with "still active".
8805+
yield* start("attempt-stale-init-continuation", 3, "provider");
8806+
const continuationTerminal = yield* Queue.take(harness.terminalReceipts);
8807+
assert.equal(continuationTerminal.status, "completed");
8808+
yield* awaitUntil(
8809+
() =>
8810+
harness.events.some(
8811+
(event) =>
8812+
event.type === "turn_item.updated" &&
8813+
event.turnItem.type === "assistant_message" &&
8814+
event.turnItem.text === WAKE_ASSISTANT_TEXT,
8815+
),
8816+
"buffered wake output replayed",
8817+
);
8818+
yield* start("attempt-stale-init-probe", 4);
8819+
}).pipe(
8820+
Effect.provide(
8821+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
8822+
),
8823+
),
8824+
),
8825+
);
8826+
87338827
it.effect("terminalizes an agent server wake from a positive task-notification result", () =>
87348828
Effect.scoped(
87358829
Effect.gen(function* () {

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

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3813,7 +3813,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
38133813
const wakeBuffers = yield* Ref.make(
38143814
new Map<
38153815
string,
3816-
{ readonly messages: ReadonlyArray<SDKMessage>; readonly detail: string | null }
3816+
{
3817+
readonly messages: ReadonlyArray<SDKMessage>;
3818+
readonly detail: string | null;
3819+
// A user turn already ran the buffered wake to its result.
3820+
readonly settled: boolean;
3821+
}
38173822
>(),
38183823
);
38193824
// Claude's wake result carries no task id, and its wake can run after a prompt the user
@@ -3870,8 +3875,21 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
38703875
});
38713876
const clearWakeReports = (nativeThreadId: string) =>
38723877
takeWakeReports(nativeThreadId).pipe(Effect.asVoid);
3873-
// An empty roster level can land before the task's notification, and
3874-
// the wake still needs to name the task.
3878+
// A user turn ran the wake to its result. Its work is reported there, but
3879+
// whatever Claude emitted before that turn began (its `init`, sometimes
3880+
// output) stays buffered. A continuation still queued replays it and
3881+
// would wait for a result that already arrived, so mark it settled.
3882+
const settleWakeInUserTurn = (nativeThreadId: string) =>
3883+
Effect.gen(function* () {
3884+
yield* clearWakeReports(nativeThreadId);
3885+
yield* Ref.update(wakeBuffers, (current) => {
3886+
const entry = current.get(nativeThreadId);
3887+
if (entry === undefined || entry.settled) return current;
3888+
return new Map(current).set(nativeThreadId, { ...entry, settled: true });
3889+
});
3890+
});
3891+
// Last roster entry per opaque task. An empty roster level can land before
3892+
// the task's notification, and the wake still needs to name the task.
38753893
const lastKnownOpaqueTasks = yield* Ref.make(
38763894
new Map<string, OrchestrationV2PendingBackgroundTask>(),
38773895
);
@@ -6843,6 +6861,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
68436861
updated.set(wakeInput.nativeThreadId, {
68446862
messages: [...(existing?.messages ?? []), message],
68456863
detail: notificationSummary ?? existing?.detail ?? null,
6864+
// A new wake's `init` reopens the buffer for its own result.
6865+
settled: !isWakeTurnStart && (existing?.settled ?? false),
68466866
});
68476867
return updated;
68486868
});
@@ -8170,7 +8190,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
81708190
isClaudeTaskNotificationOriginResult(message) &&
81718191
!isClaudeProviderContinuationTurn(context.input)
81728192
) {
8173-
yield* clearWakeReports(liveQuery.nativeThreadId);
8193+
// This user turn ran the wake itself, so no offer will name its work.
8194+
yield* settleWakeInUserTurn(liveQuery.nativeThreadId);
81748195
yield* Effect.logInfo("orchestration-v2.claude-task-notification-result-accepted", {
81758196
providerTurnId: context.providerTurnId,
81768197
num_turns: message.num_turns,
@@ -8444,7 +8465,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
84448465
// stays out: settling here would end the turn before its prompt
84458466
// runs. The prompt's own turn ends it with an echoing result.
84468467
if (isClaudeTaskNotificationOriginResult(message)) {
8447-
yield* clearWakeReports(liveQuery.nativeThreadId);
8468+
// This turn showed the wake, so no offer will name its work.
8469+
yield* settleWakeInUserTurn(liveQuery.nativeThreadId);
84488470
}
84498471
yield* Effect.logInfo("orchestration-v2.claude-result-for-other-turn", {
84508472
providerTurnId: context.providerTurnId,
@@ -9208,7 +9230,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92089230
});
92099231
}
92109232
yield* attachTurn;
9211-
const drained = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
9233+
// Exit and interrupt cleanup can run during transcript reads.
9234+
// Keep pending outcomes visible until replay applies them.
9235+
const buffer = (yield* Ref.get(wakeBuffers)).get(nativeThreadId);
9236+
const drained = buffer?.messages ?? [];
92129237
yield* Ref.update(requestedContinuations, (current) => {
92139238
const updated = new Set(current);
92149239
updated.delete(nativeThreadId);
@@ -9273,13 +9298,19 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92739298
yield* handleSdkMessage({ query: querySession.query, message: lastResult });
92749299
return;
92759300
}
9301+
// A drained `init` means Claude began the wake turn, so its output
9302+
// may still be on the way: stay open for it, unless a user turn
9303+
// already received its result.
92769304
const hasNativeWakeFrame = drained.some(
92779305
(entry) =>
92789306
entry.type === "user" ||
92799307
entry.type === "assistant" ||
92809308
isClaudeTurnStartMessage(entry),
92819309
);
9282-
if (hasOpaqueTaskNotification && !hasNativeWakeFrame) {
9310+
if (
9311+
buffer?.settled === true ||
9312+
(hasOpaqueTaskNotification && !hasNativeWakeFrame)
9313+
) {
92839314
const completedAt = yield* DateTime.now;
92849315
yield* finalizeActiveTurn({ context, status: "completed", completedAt });
92859316
}

0 commit comments

Comments
 (0)