Skip to content

Commit c54bf84

Browse files
committed
fix(server): apply buffered subagent outcomes only once
1 parent 69a9a9c commit c54bf84

2 files changed

Lines changed: 59 additions & 8 deletions

File tree

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9074,11 +9074,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
90749074
Effect.scoped(
90759075
Effect.gen(function* () {
90769076
let stopCalls = 0;
9077+
let stopRequested = yield* Deferred.make<void>();
90779078
const harness = yield* makeWakeHarnessWithOptions({
90789079
stopTask: () =>
90799080
Effect.sync(() => {
90809081
stopCalls++;
9081-
}),
9082+
}).pipe(Effect.andThen(Deferred.succeed(stopRequested, undefined)), Effect.asVoid),
90829083
});
90839084
yield* harness.runtime.startTurn(
90849085
makeClaudeTestTurnInput({
@@ -9162,10 +9163,19 @@ describe("ClaudeAdapterV2 background wake turns", () => {
91629163
providerThread: harness.providerThread,
91639164
nativeTaskId: "idle-outcome",
91649165
}).pipe(Effect.exit, Effect.forkScoped);
9166+
yield* Deferred.await(stopRequested);
91659167
yield* TestClock.adjust("2 seconds");
91669168
assert.isTrue(Exit.isFailure(yield* Fiber.join(stopping)));
91679169
assert.equal(stopCalls, 1);
91689170
assert.isTrue(yield* harness.hasPendingBackgroundWork);
9171+
// Keep a fresh Stop pending while replay drains the old outcome before the resume.
9172+
stopRequested = yield* Deferred.make<void>();
9173+
const stopDuringReplay = yield* harness.runtime.stopSubagent!({
9174+
providerThread: harness.providerThread,
9175+
nativeTaskId: "idle-outcome",
9176+
}).pipe(Effect.exit, Effect.forkScoped);
9177+
yield* Deferred.await(stopRequested);
9178+
const beforeReplay = harness.events.length;
91699179
yield* harness.runtime.startTurn(
91709180
makeClaudeTestTurnInput({
91719181
threadId: harness.threadId,
@@ -9185,6 +9195,33 @@ describe("ClaudeAdapterV2 background wake turns", () => {
91859195
}
91869196
assert.equal(reopened.subagent.nativeTaskRef?.nativeId, "idle-outcome");
91879197
assert.isNull(reopened.subagent.completedAt);
9198+
assert.isFalse(
9199+
harness.events
9200+
.slice(beforeReplay)
9201+
.some(
9202+
(event) => event.type === "subagent.updated" && event.subagent.status !== "running",
9203+
),
9204+
);
9205+
yield* TestClock.adjust("2 seconds");
9206+
assert.isTrue(Exit.isFailure(yield* Fiber.join(stopDuringReplay)));
9207+
assert.equal(stopCalls, 2);
9208+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
9209+
// Only the resumed task's new notification can settle it.
9210+
yield* harness.offerAndWait(
9211+
makeSubagentNotificationFrame({
9212+
taskId: "idle-outcome",
9213+
toolUseId: "toolu-idle-resume",
9214+
summary: "Resumed audit outcome.",
9215+
uuid: "00000000-0000-4000-8000-000000004205",
9216+
}),
9217+
);
9218+
const resumedOutcome = yield* Queue.take(harness.subagentReceipts);
9219+
assert.equal(resumedOutcome.subagent.status, "completed");
9220+
assert.equal(resumedOutcome.subagent.result, "Resumed audit outcome.");
9221+
assert.isAbove(
9222+
DateTime.toEpochMillis(resumedOutcome.subagent.completedAt!),
9223+
DateTime.toEpochMillis(completedAt),
9224+
);
91889225
}).pipe(
91899226
Effect.provide(
91909227
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3500,6 +3500,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
35003500
// instead of waiting in the wake buffer for a continuation run.
35013501
const settledTurnByNativeThread = new Map<string, ActiveClaudeTurnContext>();
35023502
const subagentStopOutcomes = new Map<string, Deferred.Deferred<void>>();
3503+
// An idle outcome stays buffered for the parent's wake, but must not
3504+
// settle the child again after that child resumes.
3505+
const appliedSubagentOutcomes = new WeakSet<SDKMessage>();
35033506
// Subagent frames can precede the task_started that registers their
35043507
// subagent (the same race rememberPendingClaudeSubagentLaunch covers).
35053508
// They wait here and replay once task_started registers the owner.
@@ -4864,7 +4867,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
48644867
"running" | "completed" | "failed" | "cancelled"
48654868
>;
48664869
readonly reopen?: boolean;
4867-
readonly nativeOutcome?: boolean;
4870+
readonly nativeOutcome?: Extract<
4871+
SDKMessage,
4872+
{ readonly type: "system"; readonly subtype: "task_notification" }
4873+
>;
48684874
readonly onlyIfRunningInRun?: RunId | null;
48694875
}) {
48704876
// The session registry lets a wake-replay turn (fresh context maps)
@@ -5032,7 +5038,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
50325038
const subagent = {
50335039
task,
50345040
confirmedOutcome:
5035-
input.nativeOutcome === true
5041+
input.nativeOutcome !== undefined
50365042
? confirmedSubagentOutcome(task)
50375043
: isReopen
50385044
? undefined
@@ -5081,6 +5087,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
50815087
return [true, new Map(current).set(input.taskId, subagent)] as const;
50825088
});
50835089
if (!accepted) return;
5090+
if (input.nativeOutcome !== undefined) appliedSubagentOutcomes.add(input.nativeOutcome);
50845091
// A call the subagent left open never returns once it ends. It ends
50855092
// first, while the run that owns the child thread still takes its
50865093
// events.
@@ -5254,7 +5261,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
52545261
},
52555262
});
52565263

5257-
if (input.nativeOutcome === true && subagent.confirmedOutcome !== undefined) {
5264+
if (input.nativeOutcome !== undefined && subagent.confirmedOutcome !== undefined) {
52585265
const outcome = subagentStopOutcomes.get(input.taskId);
52595266
if (outcome !== undefined) yield* Deferred.succeed(outcome, undefined);
52605267
}
@@ -6778,7 +6785,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
67786785
taskId: message.task_id,
67796786
...(message.tool_use_id === undefined ? {} : { toolUseId: message.tool_use_id }),
67806787
workflowFrame: message,
6781-
nativeOutcome: true,
6788+
nativeOutcome: message,
67826789
result: message.summary,
67836790
status: claudeTaskOutcome(message.status),
67846791
onlyIfRunningInRun: registered.task.runId,
@@ -7273,7 +7280,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
72737280
if (message.type === "system" && message.subtype === "task_notification") {
72747281
// A live outcome supersedes earlier idle starts. During replay only
72757282
// remove frames before this notification, preserving a later resume.
7276-
if ((yield* Ref.get(sessionSubagentsByTaskId)).has(message.task_id)) {
7283+
if (
7284+
!appliedSubagentOutcomes.has(message) &&
7285+
(yield* Ref.get(sessionSubagentsByTaskId)).has(message.task_id)
7286+
) {
72777287
yield* discardBufferedSubagentStarts(
72787288
liveQuery.nativeThreadId,
72797289
message.task_id,
@@ -7322,14 +7332,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
73227332
message,
73237333
activeContext: context,
73247334
});
7325-
if (!wasBackgroundTask && !context.ignoredTaskIds.has(message.task_id)) {
7335+
if (
7336+
!wasBackgroundTask &&
7337+
!context.ignoredTaskIds.has(message.task_id) &&
7338+
!appliedSubagentOutcomes.has(message)
7339+
) {
73267340
yield* updateClaudeSubagentNode({
73277341
context,
73287342
taskId: message.task_id,
73297343
...(message.tool_use_id === undefined ? {} : { toolUseId: message.tool_use_id }),
73307344
// Terminal usage totals arrive here, after the last snapshot.
73317345
workflowFrame: message,
7332-
nativeOutcome: true,
7346+
nativeOutcome: message,
73337347
result: message.summary,
73347348
status:
73357349
message.status === "completed"

0 commit comments

Comments
 (0)