Skip to content

Commit ee041dc

Browse files
committed
fix(server): preserve sibling wakes and finish delivery retries
1 parent 1a483b0 commit ee041dc

8 files changed

Lines changed: 289 additions & 43 deletions

File tree

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

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11042,10 +11042,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1104211042
],
1104311043
);
1104411044

11045-
// Once the task leaves the roster, a reused id starts unmarked.
11045+
// Once the task ends, a reused id starts unmarked.
1104611046
yield* Queue.offer(
1104711047
harness.sdkMessages,
11048-
roster(["dev-server"], "00000000-0000-4000-8000-000000000303"),
11048+
claudeSdkFrame({ ...wakeNotification, uuid: "00000000-0000-4000-8000-000000000303" }),
1104911049
);
1105011050
yield* nextRoster((tasks) => tasks.length === 1);
1105111051
yield* Queue.offer(
@@ -13881,6 +13881,13 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1388113881
const failed = harness.continuationRequests[0]!;
1388213882
assert.isDefined(failed.clearIfCurrent);
1388313883
yield* failed.clearIfCurrent!();
13884+
yield* harness.until(
13885+
() =>
13886+
!(
13887+
providerThreadRosterEvents(harness.events).at(-1)?.providerThread
13888+
.pendingBackgroundTasks ?? []
13889+
).some((task) => task.taskId === WAKE_TASK_ID),
13890+
);
1388413891

1388513892
yield* harness.offerAndWait(
1388613893
claudeSdkFrame({
@@ -14604,13 +14611,15 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1460414611
),
1460514612
);
1460614613

14607-
it.effect.each(["none", "before", "after"] as const)(
14614+
it.effect.each(["none", "before", "after", "sibling"] as const)(
1460814615
"keeps an exited background command on the roster until its wake run starts (empty level %s notification)",
1460914616
(emptyLevel) =>
1461014617
Effect.scoped(
1461114618
Effect.gen(function* () {
1461214619
const harness = yield* makeWakeHarness;
1461314620
const now = yield* DateTime.now;
14621+
const secondTaskId = "second-owed-wake";
14622+
const taskIds = emptyLevel === "sibling" ? [WAKE_TASK_ID, secondTaskId] : [WAKE_TASK_ID];
1461414623
const wakeAttemptId = RunAttemptId.make("attempt-claude-wake-roster-2");
1461514624
const emptyRoster = claudeSdkFrame({
1461614625
type: "system",
@@ -14631,17 +14640,27 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1463114640
}),
1463214641
);
1463314642
yield* harness.offerAndWait(wakeTaskStarted);
14643+
if (emptyLevel === "sibling")
14644+
yield* harness.offerAndWait(
14645+
claudeSdkFrame({
14646+
...wakeTaskStarted,
14647+
task_id: secondTaskId,
14648+
tool_use_id: "toolu_second_owed_wake",
14649+
uuid: "00000000-0000-4000-8000-000000000205",
14650+
}),
14651+
);
1463414652
const awaitBackgroundTasks = harness.runtime.awaitBackgroundTasks;
1463514653
assert.isDefined(awaitBackgroundTasks);
1463614654
yield* awaitBackgroundTasks!({
1463714655
providerThread: harness.providerThread,
14638-
taskIds: [WAKE_TASK_ID],
14656+
taskIds,
1463914657
awaited: true,
1464014658
});
1464114659
yield* Queue.offer(harness.sdkMessages, turnOneResult);
1464214660
yield* harness.awaitTerminal(1);
1464314661
const afterFirstTurn = harness.events.length;
14644-
if (emptyLevel === "before") yield* harness.offerAndWait(emptyRoster);
14662+
if (emptyLevel === "before" || emptyLevel === "sibling")
14663+
yield* harness.offerAndWait(emptyRoster);
1464514664
yield* harness.offerAndWait(wakeNotification);
1464614665
if (emptyLevel === "after") yield* harness.offerAndWait(emptyRoster);
1464714666
// Query replacement reads the internal roster, which the exit already cleared.
@@ -14688,12 +14707,63 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1468814707
);
1468914708
assert.deepEqual(
1469014709
rosters.filter((roster) => roster.beforeWake).map((roster) => roster.taskIds),
14691-
rosters.filter((roster) => roster.beforeWake).map(() => [[WAKE_TASK_ID, true]]),
14710+
rosters
14711+
.filter((roster) => roster.beforeWake)
14712+
.map(() => taskIds.map((id) => [id, true])),
1469214713
);
1469314714
assert.isTrue(
1469414715
rosters.filter((roster) => roster.beforeWake).every((roster) => roster.pendingWork),
1469514716
);
14696-
assert.deepEqual(rosters.at(-1)?.taskIds, []);
14717+
assert.deepEqual(
14718+
rosters.at(-1)?.taskIds,
14719+
emptyLevel === "sibling" ? [[secondTaskId, true]] : [],
14720+
);
14721+
if (emptyLevel === "sibling") {
14722+
assert.isTrue(rosters.at(-1)?.pendingWork);
14723+
yield* harness.offerAndWait(
14724+
claudeSdkFrame({
14725+
...wakeNotification,
14726+
task_id: secondTaskId,
14727+
tool_use_id: "toolu_second_owed_wake",
14728+
uuid: "00000000-0000-4000-8000-000000000206",
14729+
}),
14730+
);
14731+
yield* harness.offerAndWait(
14732+
claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000207" }),
14733+
);
14734+
yield* harness.runtime.startTurn(
14735+
makeClaudeTestTurnInput({
14736+
threadId: harness.threadId,
14737+
providerThread: harness.providerThread,
14738+
now,
14739+
attemptId: RunAttemptId.make("attempt-claude-owed-sibling-3"),
14740+
text: "Second command completed.",
14741+
attachments: [],
14742+
providerTurnOrdinal: 3,
14743+
messageCreatedBy: "agent",
14744+
messageCreationSource: "provider",
14745+
}),
14746+
);
14747+
yield* harness.offerAndWait(
14748+
makeAssistantTextFrame({
14749+
uuid: "00000000-0000-4000-8000-000000000208",
14750+
text: "Second command finished.",
14751+
}),
14752+
);
14753+
yield* harness.offerAndWait(
14754+
makeResultFrame({
14755+
uuid: "00000000-0000-4000-8000-000000000209",
14756+
result: "Second command finished.",
14757+
origin: { kind: "task-notification" },
14758+
}),
14759+
);
14760+
yield* harness.awaitTerminal(3);
14761+
assert.deepEqual(
14762+
providerThreadRosterEvents(harness.events).at(-1)?.providerThread
14763+
.pendingBackgroundTasks,
14764+
[],
14765+
);
14766+
}
1469714767
}).pipe(
1469814768
Effect.provide(
1469914769
Layer.mergeAll(

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

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6040,11 +6040,28 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
60406040
taskId,
60416041
);
60426042

6043+
// Untraced with roster emission: this runs for each streamed roster update.
6044+
const owedWakeTasksForNativeThread = Effect.fnUntraced(function* (nativeThreadId: string) {
6045+
const previous = (yield* Ref.get(lastProviderThreadByNativeThread)).get(nativeThreadId);
6046+
const roster = rosterForNativeThread(
6047+
yield* Ref.get(pendingBackgroundTasksByNativeThread),
6048+
nativeThreadId,
6049+
);
6050+
const tasks: OrchestrationV2PendingBackgroundTask[] = [];
6051+
for (const task of previous?.pendingBackgroundTasks ?? []) {
6052+
if (
6053+
!roster.has(task.taskId) &&
6054+
(yield* isKnownOpaqueBackgroundTaskOnNativeThread(nativeThreadId, task.taskId))
6055+
)
6056+
tasks.push(task);
6057+
}
6058+
return tasks;
6059+
});
6060+
60436061
const emitProviderThreadRoster = Effect.fnUntraced(function* (input: {
60446062
readonly nativeThreadId: string;
60456063
readonly providerThread: OrchestrationV2ProviderThread;
60466064
readonly status?: OrchestrationV2ProviderThread["status"];
6047-
readonly owedWakeTasks?: ReadonlyArray<OrchestrationV2PendingBackgroundTask>;
60486065
}) {
60496066
const roster = rosterForNativeThread(
60506067
yield* Ref.get(pendingBackgroundTasksByNativeThread),
@@ -6054,14 +6071,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
60546071
yield* Ref.get(awaitedBackgroundTasksByNativeThread),
60556072
input.nativeThreadId,
60566073
);
6074+
const owedWakeTasks = yield* owedWakeTasksForNativeThread(input.nativeThreadId);
60576075
const now = yield* DateTime.now;
60586076
const providerThread: OrchestrationV2ProviderThread = {
60596077
...input.providerThread,
60606078
providerSessionId: session.id,
60616079
...(input.status === undefined ? {} : { status: input.status }),
60626080
pendingBackgroundTasks: [
60636081
...publishedPendingBackgroundTasks(input.nativeThreadId, roster, awaitedTaskIds),
6064-
...(input.owedWakeTasks ?? []).filter((task) => !roster.has(task.taskId)),
6082+
...owedWakeTasks,
60656083
],
60666084
...(goalsByNativeThread.has(input.nativeThreadId)
60676085
? { goal: goalsByNativeThread.get(input.nativeThreadId) ?? null }
@@ -6073,7 +6091,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
60736091
type: "provider_thread.updated",
60746092
driver: CLAUDE_PROVIDER,
60756093
providerThread,
6076-
pendingBackgroundWork: (input.owedWakeTasks?.length ?? 0) > 0,
6094+
pendingBackgroundWork: owedWakeTasks.length > 0,
60776095
});
60786096
});
60796097

@@ -8666,6 +8684,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
86668684
yield* Ref.get(awaitedBackgroundTasksByNativeThread),
86678685
nativeThreadId,
86688686
);
8687+
const owedWakeTasks =
8688+
nativeThreadId === null
8689+
? []
8690+
: yield* owedWakeTasksForNativeThread(nativeThreadId);
86698691
// A stopped turn that wrote to the transcript moved past the pinned head.
86708692
const clearConversationHead =
86718693
(input.status === "completed" || input.context.nativeMessageCursor !== null) &&
@@ -8704,11 +8726,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
87048726
input.context.input.providerThread.firstRunOrdinal ??
87058727
input.context.input.runOrdinal,
87068728
lastRunOrdinal: input.context.input.runOrdinal,
8707-
pendingBackgroundTasks: publishedPendingBackgroundTasks(
8708-
nativeThreadId,
8709-
roster,
8710-
awaitedTaskIds,
8711-
),
8729+
pendingBackgroundTasks: [
8730+
...publishedPendingBackgroundTasks(nativeThreadId, roster, awaitedTaskIds),
8731+
...owedWakeTasks,
8732+
],
87128733
status: input.status === "completed" ? "active" : "idle",
87138734
updatedAt: input.completedAt,
87148735
};
@@ -8717,6 +8738,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
87178738
type: "provider_thread.updated" as const,
87188739
driver: CLAUDE_PROVIDER,
87198740
providerThread,
8741+
pendingBackgroundWork: owedWakeTasks.length > 0,
87208742
});
87218743
}),
87228744
emitProviderEvent(terminalEvent),
@@ -9236,6 +9258,21 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92369258
...later,
92379259
];
92389260
if (!(yield* replaceWakeBuffer(nativeThreadId, buffered, kept, retiredTaskIds))) return;
9261+
for (const taskId of retiredTaskIds) {
9262+
yield* clearPendingBackgroundTask(nativeThreadId, taskId);
9263+
yield* clearTaskIdFromNativeThreadSet(
9264+
wakeEligibleBackgroundTasksByNativeThread,
9265+
nativeThreadId,
9266+
taskId,
9267+
);
9268+
yield* clearOpaqueBackgroundTaskReplayTombstone(nativeThreadId, taskId);
9269+
}
9270+
yield* endClaudeMonitorTasks((taskId) => retiredTaskIds.has(taskId));
9271+
const providerThread = (yield* Ref.get(lastProviderThreadByNativeThread)).get(
9272+
nativeThreadId,
9273+
);
9274+
if (providerThread !== undefined)
9275+
yield* emitProviderThreadRoster({ nativeThreadId, providerThread });
92399276
if (latch.end === undefined) {
92409277
// A lifecycle result closes the root frames before it without opening the wake's turn.
92419278
const sinceLifecycleResult = retired.slice(
@@ -9350,16 +9387,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
93509387
if (baseThread === undefined) {
93519388
return;
93529389
}
9353-
// An exited task still owes its wake turn; publishing an empty roster settles the
9354-
// delegated task with the previous turn's interim answer before that wake starts.
9355-
const owedWakeTasks: OrchestrationV2PendingBackgroundTask[] = [];
9356-
if (activeContext === null) {
9357-
for (const task of baseThread.pendingBackgroundTasks ?? []) {
9358-
if (yield* isKnownOpaqueBackgroundTaskOnNativeThread(nativeThreadId, task.taskId)) {
9359-
owedWakeTasks.push(task);
9360-
}
9361-
}
9362-
}
93639390
const status =
93649391
activeContext === null
93659392
? ("idle" as const)
@@ -9370,7 +9397,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
93709397
nativeThreadId,
93719398
providerThread: baseThread,
93729399
status,
9373-
owedWakeTasks,
93749400
});
93759401
});
93769402

@@ -11977,7 +12003,24 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1197712003
});
1197812004
return updated;
1197912005
});
11980-
yield* rememberProviderThread(turnInput.providerThread);
12006+
// A queued turn's snapshot can predate another task's exit.
12007+
const owedWakeTasks = yield* owedWakeTasksForNativeThread(nativeThreadId);
12008+
yield* rememberProviderThread(
12009+
owedWakeTasks.length === 0
12010+
? turnInput.providerThread
12011+
: {
12012+
...turnInput.providerThread,
12013+
pendingBackgroundTasks: [
12014+
...(turnInput.providerThread.pendingBackgroundTasks ?? []),
12015+
...owedWakeTasks.filter(
12016+
(task) =>
12017+
!(turnInput.providerThread.pendingBackgroundTasks ?? []).some(
12018+
(current) => current.taskId === task.taskId,
12019+
),
12020+
),
12021+
],
12022+
},
12023+
);
1198112024
if (!goalsByNativeThread.has(nativeThreadId)) {
1198212025
goalsByNativeThread.set(nativeThreadId, turnInput.providerThread.goal ?? null);
1198312026
}

0 commit comments

Comments
 (0)