Skip to content

Commit f34ebbd

Browse files
id0Schclaude
andcommitted
fix(server): a failed Claude reopen still lets buffered subagent results land
Share one cleanup between turn start and a failed open, so a subagent whose completion is already buffered (kept across a CLI crash) finishes from the buffer instead of being marked interrupted. Also restores the upstream formatting of toSessionPermissionUpdates that a local formatter rewrapped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a32ef0d commit f34ebbd

1 file changed

Lines changed: 17 additions & 27 deletions

File tree

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

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,12 +2437,10 @@ function toSessionPermissionUpdates(
24372437
toolName: string,
24382438
suggestions: ReadonlyArray<PermissionUpdate> | undefined,
24392439
): Array<PermissionUpdate> {
2440-
const updates = (suggestions ?? []).map(
2441-
(suggestion): PermissionUpdate => ({
2442-
...suggestion,
2443-
destination: "session",
2444-
}),
2445-
);
2440+
const updates = (suggestions ?? []).map((suggestion): PermissionUpdate => ({
2441+
...suggestion,
2442+
destination: "session",
2443+
}));
24462444
if (updates.length > 0) {
24472445
return updates;
24482446
}
@@ -8151,18 +8149,19 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
81518149
return false;
81528150
});
81538151

8154-
// Subagents an earlier, now closed process was running never report
8155-
// their end, so a new process interrupts them. One whose completion is
8156-
// already buffered finishes when the buffer drains.
8157-
const interruptSubagentsFromEarlierProcesses = Effect.fnUntraced(function* (
8158-
live: ClaudeLiveQueryContext,
8152+
// Subagents of a closed process never report their end, so they are
8153+
// interrupted. One whose completion is already buffered finishes when
8154+
// the buffer drains.
8155+
const interruptSubagentsOfClosedProcesses = Effect.fnUntraced(function* (
8156+
nativeThreadId: string,
81598157
context: ActiveClaudeTurnContext,
8158+
ranInClosedProcess: (subagent: ActiveClaudeSubagent) => boolean,
81608159
) {
8161-
const buffered = (yield* Ref.get(wakeBuffers)).get(live.nativeThreadId)?.messages ?? [];
8160+
const buffered = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
81628161
for (const [taskId, subagent] of yield* Ref.get(sessionSubagentsByTaskId)) {
81638162
if (
81648163
subagent.task.status === "running" &&
8165-
live.subagentsFromEarlierProcesses.has(subagent) &&
8164+
ranInClosedProcess(subagent) &&
81668165
!buffered.some(
81678166
(message) =>
81688167
message.type === "system" &&
@@ -8306,20 +8305,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
83068305
})
83078306
: Effect.void,
83088307
),
8309-
// No process of this session is left to report a subagent's
8310-
// end, and every earlier process's wake buffer is already gone.
8308+
// No process of this session is left to run any subagent.
83118309
Effect.tapError(() =>
8312-
Effect.gen(function* () {
8313-
for (const [taskId, subagent] of yield* Ref.get(sessionSubagentsByTaskId)) {
8314-
if (subagent.task.status === "running") {
8315-
yield* updateClaudeSubagentNode({
8316-
context: turnContext,
8317-
taskId,
8318-
status: "interrupted",
8319-
});
8320-
}
8321-
}
8322-
}),
8310+
interruptSubagentsOfClosedProcesses(nativeThreadId, turnContext, () => true),
83238311
),
83248312
);
83258313
// Marked only after a successful open: a failed create must not
@@ -8528,7 +8516,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
85288516
}),
85298517
});
85308518
});
8531-
yield* interruptSubagentsFromEarlierProcesses(querySession, context);
8519+
yield* interruptSubagentsOfClosedProcesses(nativeThreadId, context, (subagent) =>
8520+
querySession.subagentsFromEarlierProcesses.has(subagent),
8521+
);
85328522
if (userMessage !== null) {
85338523
yield* attachTurn;
85348524
// A user turn that races a wake leaves the buffer alone: the

0 commit comments

Comments
 (0)