@@ -152,6 +152,7 @@ import * as Equal from "effect/Equal";
152152import * as Exit from "effect/Exit";
153153import * as Fiber from "effect/Fiber";
154154import * as Schedule from "effect/Schedule";
155+ import * as Scheduler from "effect/Scheduler";
155156import * as FileSystem from "effect/FileSystem";
156157import * as Layer from "effect/Layer";
157158import * as Option from "effect/Option";
@@ -2747,6 +2748,44 @@ function isClaudeGoalAcknowledgement(message: SDKMessage): boolean {
27472748const providerGoalsEqual = Schema.toEquivalence(Schema.NullOr(OrchestrationV2ProviderGoal));
27482749const providerThreadsEqual = Schema.toEquivalence(OrchestrationV2ProviderThread);
27492750
2751+ type ProviderThreadUpdate = Extract<
2752+ ProviderAdapter.ProviderAdapterV2Event,
2753+ { type: "provider_thread.updated" }
2754+ >;
2755+
2756+ export function makeClaudeThreadUpdatePublisher(
2757+ events: Queue.Enqueue<ProviderAdapter.ProviderAdapterV2Event>,
2758+ ) {
2759+ const lastProviderThreadUpdates = new Map<string, ProviderThreadUpdate>();
2760+ return {
2761+ reset: (id: OrchestrationV2ProviderThread["id"]) => lastProviderThreadUpdates.delete(id),
2762+ publish: Effect.fnUntraced(
2763+ function* (event: ProviderThreadUpdate, mode: "always" | "ifChanged") {
2764+ const { providerThread } = event;
2765+ const last = lastProviderThreadUpdates.get(providerThread.id);
2766+ // Claude reports a change both as a task event and as a roster level, and a level also
2767+ // lists subagents, which the roster leaves out.
2768+ if (
2769+ mode === "ifChanged" &&
2770+ last !== undefined &&
2771+ (last.pendingBackgroundWork === true) === (event.pendingBackgroundWork === true) &&
2772+ providerThreadsEqual(last.providerThread, {
2773+ ...providerThread,
2774+ updatedAt: last.providerThread.updatedAt,
2775+ })
2776+ ) {
2777+ return;
2778+ }
2779+ if (yield* Queue.offer(events, event)) {
2780+ lastProviderThreadUpdates.set(providerThread.id, event);
2781+ }
2782+ },
2783+ // The session queue is unbounded. Keep the comparison, offer, and write in one scheduler slice.
2784+ Effect.provideService(Scheduler.PreventSchedulerYield, true),
2785+ ),
2786+ };
2787+ }
2788+
27502789function normalizeClaudeResultText(text: string): string {
27512790 return text.replace(/\s+/g, " ").trim();
27522791}
@@ -6481,15 +6520,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
64816520 nativeConversationHeadRef: null,
64826521 };
64836522 };
6484- const lastProviderThreadUpdates = new Map<
6485- string,
6486- Extract<ProviderAdapter.ProviderAdapterV2Event, { type: "provider_thread.updated" }>
6487- >();
6488-
6523+ const threadUpdates = makeClaudeThreadUpdatePublisher(events);
64896524 const emitProviderEvent = (event: ProviderAdapter.ProviderAdapterV2Event) => {
64906525 if (event.type === "provider_thread.updated") {
6491- event = { ...event, providerThread: publishedProviderThread(event.providerThread) };
6492- lastProviderThreadUpdates.set(event.providerThread.id, event);
6526+ return threadUpdates.publish(
6527+ { ...event, providerThread: publishedProviderThread(event.providerThread) },
6528+ "always",
6529+ );
64936530 }
64946531 return Queue.offer(events, event).pipe(Effect.asVoid);
64956532 };
@@ -6897,25 +6934,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
68976934 updatedAt: now,
68986935 };
68996936 yield* rememberProviderThread(providerThread);
6900- // Claude reports a change both as a task event and as a roster level, and a level also
6901- // lists subagents, which the roster leaves out.
6902- const last = lastProviderThreadUpdates.get(providerThread.id);
6903- if (
6904- last !== undefined &&
6905- (last.pendingBackgroundWork === true) === pendingBackgroundWork &&
6906- providerThreadsEqual(last.providerThread, {
6907- ...publishedProviderThread(providerThread),
6908- updatedAt: last.providerThread.updatedAt,
6909- })
6910- ) {
6911- return;
6912- }
6913- yield* emitProviderEvent({
6914- type: "provider_thread.updated",
6915- driver: CLAUDE_PROVIDER,
6916- providerThread,
6917- pendingBackgroundWork,
6918- });
6937+ yield* threadUpdates.publish(
6938+ {
6939+ type: "provider_thread.updated",
6940+ driver: CLAUDE_PROVIDER,
6941+ providerThread: publishedProviderThread(providerThread),
6942+ pendingBackgroundWork,
6943+ },
6944+ "ifChanged",
6945+ );
69196946 });
69206947
69216948 const trackClaudeGoal = Effect.fnUntraced(function* (input: {
@@ -13553,7 +13580,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1355313580 return updated;
1355413581 });
1355513582 // Events reach the server only while a run listens, so a turn compares from scratch.
13556- lastProviderThreadUpdates.delete (turnInput.providerThread.id);
13583+ threadUpdates.reset (turnInput.providerThread.id);
1355713584 // A queued turn's snapshot can predate another task's exit.
1355813585 const owedWakeTasks = yield* owedWakeTasksForNativeThread(nativeThreadId);
1355913586 yield* rememberProviderThread(
@@ -14945,6 +14972,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1494514972 yield* closeLiveQueryForNativeThread(nativeThreadId);
1494614973 // The rewound history may no longer hold the turns that carried app notes.
1494714974 deliveredAppContext.delete(nativeThreadId);
14975+ threadUpdates.reset(rollbackInput.providerThread.id);
1494814976 const now = yield* DateTime.now;
1494914977 const resumeSessionAt = yield* resolveClaudeRollbackResumeSessionAt(rollbackInput);
1495014978
0 commit comments