Skip to content

Commit fe61ac4

Browse files
committed
fix(server): remember Claude roster updates after enqueue
1 parent ebb0ba4 commit fe61ac4

2 files changed

Lines changed: 112 additions & 27 deletions

File tree

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import * as Path from "effect/Path";
7171
import * as PubSub from "effect/PubSub";
7272
import * as Queue from "effect/Queue";
7373
import * as Ref from "effect/Ref";
74+
import * as Scheduler from "effect/Scheduler";
7475
import * as Schema from "effect/Schema";
7576
import * as Scope from "effect/Scope";
7677
import * as Stream from "effect/Stream";
@@ -13369,6 +13370,61 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1336913370
),
1337013371
);
1337113372

13373+
it.effect(
13374+
"records roster updates only after enqueue and keeps concurrent publishes ordered",
13375+
() =>
13376+
Effect.gen(function* () {
13377+
const { providerThread } = yield* makeWakeHarness;
13378+
const event = {
13379+
type: "provider_thread.updated",
13380+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
13381+
providerThread,
13382+
} as const;
13383+
13384+
// Unexecuted publish must not populate the dedupe cache.
13385+
const unexecutedEvents = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
13386+
const unexecutedPublisher =
13387+
ClaudeAdapterV2.makeClaudeThreadUpdatePublisher(unexecutedEvents);
13388+
void unexecutedPublisher.publish(event, "always");
13389+
assert.deepEqual(yield* Queue.poll(unexecutedEvents), Option.none());
13390+
yield* unexecutedPublisher.publish(event, "ifChanged");
13391+
assert.deepEqual(yield* Queue.poll(unexecutedEvents), Option.some(event));
13392+
13393+
// An interrupted Queue.offer must not suppress an identical retry.
13394+
const boundedEvents = yield* Queue.bounded<ProviderAdapter.ProviderAdapterV2Event>(1);
13395+
const boundedPublisher = ClaudeAdapterV2.makeClaudeThreadUpdatePublisher(boundedEvents);
13396+
yield* Queue.offer(boundedEvents, event);
13397+
const blocked = yield* boundedPublisher.publish(event, "always").pipe(Effect.forkChild);
13398+
yield* Effect.yieldNow;
13399+
yield* Fiber.interrupt(blocked);
13400+
yield* Queue.take(boundedEvents);
13401+
yield* boundedPublisher.publish(event, "ifChanged");
13402+
assert.deepEqual(yield* Queue.poll(boundedEvents), Option.some(event));
13403+
13404+
// Competing always + ifChanged publishes under a tight yield budget enqueue only once.
13405+
const racedEvents = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
13406+
const racedPublisher = ClaudeAdapterV2.makeClaudeThreadUpdatePublisher(racedEvents);
13407+
const first = yield* racedPublisher
13408+
.publish(event, "always")
13409+
.pipe(Effect.provideService(Scheduler.MaxOpsBeforeYield, 2), Effect.forkChild);
13410+
const second = yield* racedPublisher
13411+
.publish(event, "ifChanged")
13412+
.pipe(Effect.provideService(Scheduler.MaxOpsBeforeYield, 2), Effect.forkChild);
13413+
yield* Fiber.join(first);
13414+
yield* Fiber.join(second);
13415+
assert.strictEqual((yield* Queue.takeAll(racedEvents)).length, 1);
13416+
}).pipe(
13417+
Effect.provide(
13418+
Layer.mergeAll(
13419+
IdAllocator.layer,
13420+
McpProviderSessions.layer,
13421+
NodeServices.layer,
13422+
NodeCrypto.layer,
13423+
),
13424+
),
13425+
),
13426+
);
13427+
1337213428
it.effect("publishes each background roster change once", () =>
1337313429
Effect.scoped(
1337413430
Effect.gen(function* () {
@@ -13421,6 +13477,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1342113477
);
1342213478
// Claude Code reports each start and end both as a task event and as a roster level.
1342313479
yield* harness.offerAndWait(wakeTaskStarted);
13480+
yield* TestClock.adjust("1 millis");
1342413481
yield* harness.offerAndWait(roster([shell], "00000000-0000-4000-8000-000000000502"));
1342513482
yield* harness.offerAndWait(
1342613483
roster([shell, subagent], "00000000-0000-4000-8000-000000000503"),

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

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ import * as Equal from "effect/Equal";
152152
import * as Exit from "effect/Exit";
153153
import * as Fiber from "effect/Fiber";
154154
import * as Schedule from "effect/Schedule";
155+
import * as Scheduler from "effect/Scheduler";
155156
import * as FileSystem from "effect/FileSystem";
156157
import * as Layer from "effect/Layer";
157158
import * as Option from "effect/Option";
@@ -2747,6 +2748,44 @@ function isClaudeGoalAcknowledgement(message: SDKMessage): boolean {
27472748
const providerGoalsEqual = Schema.toEquivalence(Schema.NullOr(OrchestrationV2ProviderGoal));
27482749
const 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+
27502789
function 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

Comments
 (0)