Skip to content

Commit ab3b82b

Browse files
committed
fix(server): require confirmed Claude subagent outcomes
1 parent b41d6ee commit ab3b82b

10 files changed

Lines changed: 520 additions & 209 deletions

File tree

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

Lines changed: 192 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8885,93 +8885,205 @@ describe("ClaudeAdapterV2 background wake turns", () => {
88858885
[true, "no notification"],
88868886
[false, "stop fails"],
88878887
[false, "stop hangs"],
8888-
] as const)("settles a native subagent when owner settled %s and %s", ([ownerSettled, failure]) =>
8889-
Effect.scoped(
8890-
Effect.gen(function* () {
8891-
const requested = yield* Deferred.make<void>();
8892-
const harness = yield* makeWakeHarnessWithOptions({
8893-
stopTask: () =>
8894-
Deferred.succeed(requested, undefined).pipe(
8895-
Effect.andThen(
8896-
failure === "stop fails"
8897-
? Effect.fail(
8898-
new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
8899-
method: "stopTask",
8900-
cause: "Task is no longer live",
8901-
}),
8902-
)
8903-
: failure === "stop hangs"
8904-
? Effect.never
8905-
: Effect.void,
8888+
] as const)(
8889+
"does not settle an unconfirmed native subagent Stop, owner settled %s and %s",
8890+
([ownerSettled, failure]) =>
8891+
Effect.scoped(
8892+
Effect.gen(function* () {
8893+
const requested = yield* Deferred.make<void>();
8894+
const harness = yield* makeWakeHarnessWithOptions({
8895+
stopTask: () =>
8896+
Deferred.succeed(requested, undefined).pipe(
8897+
Effect.andThen(
8898+
failure === "stop fails"
8899+
? Effect.fail(
8900+
new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
8901+
method: "stopTask",
8902+
cause: "Task is no longer live",
8903+
}),
8904+
)
8905+
: failure === "stop hangs"
8906+
? Effect.never
8907+
: Effect.void,
8908+
),
89068909
),
8907-
),
8908-
interrupt: Effect.die("Stopping a subagent must not interrupt its owner"),
8909-
});
8910-
yield* harness.runtime.startTurn(
8911-
makeClaudeTestTurnInput({
8912-
threadId: harness.threadId,
8910+
interrupt: Effect.die("Stopping a subagent must not interrupt its owner"),
8911+
});
8912+
yield* harness.runtime.startTurn(
8913+
makeClaudeTestTurnInput({
8914+
threadId: harness.threadId,
8915+
providerThread: harness.providerThread,
8916+
now: yield* DateTime.now,
8917+
attemptId: RunAttemptId.make("attempt-stop-no-outcome"),
8918+
text: "Start an audit.",
8919+
attachments: [],
8920+
}),
8921+
);
8922+
yield* harness.offerAndWait(
8923+
makeSubagentTaskStartedFrame({
8924+
taskId: "agent-no-outcome",
8925+
toolUseId: "toolu-no-outcome",
8926+
uuid: "00000000-0000-4000-8000-000000004101",
8927+
}),
8928+
);
8929+
const launched = yield* Queue.take(harness.subagentReceipts);
8930+
if (ownerSettled) {
8931+
yield* harness.offerAndWait(
8932+
makeResultFrame({
8933+
uuid: "00000000-0000-4000-8000-000000004102",
8934+
result: "Audit launched.",
8935+
}),
8936+
);
8937+
yield* Queue.take(harness.terminalReceipts);
8938+
}
8939+
const stopping = yield* harness.runtime.stopSubagent!({
89138940
providerThread: harness.providerThread,
8914-
now: yield* DateTime.now,
8915-
attemptId: RunAttemptId.make("attempt-stop-no-outcome"),
8916-
text: "Start an audit.",
8917-
attachments: [],
8918-
}),
8919-
);
8920-
yield* harness.offerAndWait(
8921-
makeSubagentTaskStartedFrame({
8922-
taskId: "agent-no-outcome",
8923-
toolUseId: "toolu-no-outcome",
8924-
uuid: "00000000-0000-4000-8000-000000004101",
8925-
}),
8926-
);
8927-
const launched = yield* Queue.take(harness.subagentReceipts);
8928-
if (ownerSettled) {
8941+
nativeTaskId: "agent-no-outcome",
8942+
}).pipe(Effect.exit, Effect.forkScoped);
8943+
yield* Deferred.await(requested);
8944+
yield* TestClock.adjust("2 seconds");
8945+
const stopResult = yield* Fiber.join(stopping);
8946+
assert.isTrue(Exit.isFailure(stopResult));
8947+
const updates = harness.events.filter((event) => event.type === "subagent.updated");
8948+
assert.equal(updates.at(-1)?.subagent.status, "running");
8949+
assert.isNull(updates.at(-1)?.subagent.completedAt);
8950+
const childRoot = harness.events.findLast(
8951+
(event) =>
8952+
event.type === "node.updated" &&
8953+
event.node.threadId === launched.subagent.childThreadId &&
8954+
event.node.kind === "root_turn",
8955+
);
8956+
assert.equal(
8957+
childRoot?.type === "node.updated" ? childRoot.node.status : undefined,
8958+
"running",
8959+
);
8960+
const item = harness.events.findLast(
8961+
(event) =>
8962+
event.type === "turn_item.updated" &&
8963+
event.turnItem.type === "subagent" &&
8964+
event.turnItem.subagentId === launched.subagent.id,
8965+
);
8966+
assert.equal(
8967+
item?.type === "turn_item.updated" ? item.turnItem.status : undefined,
8968+
"running",
8969+
);
8970+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
8971+
assert.lengthOf(harness.terminalEvents(), ownerSettled ? 1 : 0);
8972+
// The task remains live, and its actual later outcome still clears it.
8973+
yield* harness.offerAndWait(
8974+
makeSubagentNotificationFrame({
8975+
taskId: "agent-no-outcome",
8976+
toolUseId: "toolu-no-outcome",
8977+
summary: "Actual completed audit.",
8978+
uuid: "00000000-0000-4000-8000-000000004103",
8979+
}),
8980+
);
8981+
const completed = yield* Queue.take(harness.subagentReceipts);
8982+
assert.equal(completed.subagent.status, "completed");
8983+
assert.isNotNull(completed.subagent.completedAt);
8984+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
8985+
}).pipe(
8986+
Effect.provide(
8987+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
8988+
),
8989+
),
8990+
),
8991+
);
8992+
8993+
it.effect.each(["completed", "failed", "stopped"] as const)(
8994+
"applies an idle native subagent %s notification before replay and confirms its real outcome",
8995+
(status) =>
8996+
Effect.scoped(
8997+
Effect.gen(function* () {
8998+
let stopCalls = 0;
8999+
const harness = yield* makeWakeHarnessWithOptions({
9000+
stopTask: () =>
9001+
Effect.sync(() => {
9002+
stopCalls++;
9003+
}),
9004+
});
9005+
yield* harness.runtime.startTurn(
9006+
makeClaudeTestTurnInput({
9007+
threadId: harness.threadId,
9008+
providerThread: harness.providerThread,
9009+
now: yield* DateTime.now,
9010+
attemptId: RunAttemptId.make("attempt-idle-outcome"),
9011+
text: "Run an audit.",
9012+
attachments: [],
9013+
}),
9014+
);
9015+
yield* harness.offerAndWait(
9016+
makeSubagentTaskStartedFrame({
9017+
taskId: "idle-outcome",
9018+
toolUseId: "toolu-idle-outcome",
9019+
uuid: "00000000-0000-4000-8000-000000004201",
9020+
}),
9021+
);
9022+
yield* Queue.take(harness.subagentReceipts);
89299023
yield* harness.offerAndWait(
89309024
makeResultFrame({
8931-
uuid: "00000000-0000-4000-8000-000000004102",
9025+
uuid: "00000000-0000-4000-8000-000000004202",
89329026
result: "Audit launched.",
89339027
}),
89349028
);
89359029
yield* Queue.take(harness.terminalReceipts);
8936-
}
8937-
const stopping = yield* harness.runtime.stopSubagent!({
8938-
providerThread: harness.providerThread,
8939-
nativeTaskId: "agent-no-outcome",
8940-
}).pipe(Effect.forkScoped);
8941-
yield* Deferred.await(requested);
8942-
yield* TestClock.adjust("2 seconds");
8943-
yield* Fiber.join(stopping);
8944-
const cancelled = yield* Queue.take(harness.subagentReceipts);
8945-
assert.equal(cancelled.subagent.status, "cancelled");
8946-
assert.isNotNull(cancelled.subagent.completedAt);
8947-
const childRoot = harness.events.findLast(
8948-
(event) =>
8949-
event.type === "node.updated" &&
8950-
event.node.threadId === launched.subagent.childThreadId &&
8951-
event.node.kind === "root_turn",
8952-
);
8953-
assert.equal(
8954-
childRoot?.type === "node.updated" ? childRoot.node.status : undefined,
8955-
"cancelled",
8956-
);
8957-
const item = harness.events.findLast(
8958-
(event) =>
8959-
event.type === "turn_item.updated" &&
8960-
event.turnItem.type === "subagent" &&
8961-
event.turnItem.subagentId === launched.subagent.id,
8962-
);
8963-
assert.equal(
8964-
item?.type === "turn_item.updated" ? item.turnItem.status : undefined,
8965-
"cancelled",
8966-
);
8967-
assert.isFalse(yield* harness.hasPendingBackgroundWork);
8968-
assert.lengthOf(harness.terminalEvents(), ownerSettled ? 1 : 0);
8969-
}).pipe(
8970-
Effect.provide(
8971-
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9030+
yield* TestClock.adjust("1 second");
9031+
const completedAt = yield* DateTime.now;
9032+
yield* harness.offerAndWait(
9033+
claudeSdkFrame({
9034+
...makeSubagentNotificationFrame({
9035+
taskId: "idle-outcome",
9036+
toolUseId: "toolu-idle-outcome",
9037+
summary: "Actual provider outcome.",
9038+
uuid: "00000000-0000-4000-8000-000000004203",
9039+
}),
9040+
status,
9041+
}),
9042+
);
9043+
const completed = yield* Queue.take(harness.subagentReceipts);
9044+
const expected = status === "stopped" ? "cancelled" : status;
9045+
assert.equal(completed.subagent.status, expected);
9046+
assert.equal(
9047+
DateTime.toEpochMillis(completed.subagent.completedAt!),
9048+
DateTime.toEpochMillis(completedAt),
9049+
);
9050+
assert.lengthOf(harness.offeredMessages, 1);
9051+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
9052+
// Stop repairs a phantom using the recorded outcome, without another native stop.
9053+
const outcome = yield* harness.runtime.stopSubagent!({
9054+
providerThread: harness.providerThread,
9055+
nativeTaskId: "idle-outcome",
9056+
});
9057+
assert.deepEqual(outcome, {
9058+
status: expected,
9059+
completedAt: completed.subagent.completedAt!,
9060+
result: "Actual provider outcome.",
9061+
});
9062+
assert.equal(stopCalls, 0);
9063+
yield* TestClock.adjust("1 second");
9064+
yield* harness.offerAndWait(
9065+
makeSubagentTaskStartedFrame({
9066+
taskId: "idle-outcome",
9067+
toolUseId: "toolu-idle-resume",
9068+
uuid: "00000000-0000-4000-8000-000000004204",
9069+
}),
9070+
);
9071+
// A retried Stop from before this resume must not stop the new task.
9072+
assert.isUndefined(
9073+
yield* harness.runtime.stopSubagent!({
9074+
providerThread: harness.providerThread,
9075+
nativeTaskId: "idle-outcome",
9076+
requestedAt: completedAt,
9077+
}),
9078+
);
9079+
assert.equal(stopCalls, 0);
9080+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
9081+
}).pipe(
9082+
Effect.provide(
9083+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9084+
),
89729085
),
89739086
),
8974-
),
89759087
);
89769088

89779089
it.effect.each([
@@ -10600,7 +10712,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1060010712
() => events.filter((event) => event.type === "turn.terminal").length === 2,
1060110713
"user turn terminal after replace",
1060210714
);
10603-
assert.isTrue(yield* hasPendingBackgroundWork);
10715+
// The actual completion was applied while idle, before wake replay.
10716+
assert.isFalse(yield* hasPendingBackgroundWork);
10717+
assert.equal(subagentEvents().at(-1)?.subagent.status, "completed");
1060410718
assert.lengthOf(continuationRequests, 1);
1060510719

1060610720
yield* Queue.offer(

0 commit comments

Comments
 (0)