@@ -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