Skip to content

Commit c0d7a59

Browse files
Kiri110Kclaude
andcommitted
fix(server): choose snapshot alignment by streamed-prefix compatibility
Cursor Bugbot on the PR found the corner pure tail alignment misses: when the stream dies before every snapshot text block was opened, the last-N window swallows completed blocks of earlier messages and each paragraph lands one block off — the stalled block gets the wrong paragraph and the first one is dropped. Pick the alignment by content instead of arithmetic: try each shift, require every bound open block's streamed prefix to match its snapshot text, and take the shift binding the most open blocks. Ties keep the first such shift so a fully-completed message's late snapshot still dedupes instead of re-synthesizing as a duplicate. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent de2a8e7 commit c0d7a59

3 files changed

Lines changed: 189 additions & 8 deletions

File tree

‎apps/server/src/provider/Layers/ClaudeAdapter.test.ts‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,6 +3116,161 @@ describe("ClaudeAdapterLive", () => {
31163116
},
31173117
);
31183118

3119+
it.effect(
3120+
"aligns a multi-block snapshot by streamed prefix when the stall predates later blocks",
3121+
() => {
3122+
const harness = makeHarness();
3123+
const firstParagraph = "Fixing the adapter now.";
3124+
const secondParagraph = "Second paragraph the stream never reached.";
3125+
return Effect.gen(function* () {
3126+
const adapter = yield* ClaudeAdapter;
3127+
3128+
const runtimeEventsFiber = yield* Stream.take(adapter.streamEvents, 12).pipe(
3129+
Stream.runCollect,
3130+
Effect.forkChild,
3131+
);
3132+
3133+
const session = yield* adapter.startSession({
3134+
threadId: THREAD_ID,
3135+
provider: ProviderDriverKind.make("claudeAgent"),
3136+
runtimeMode: "full-access",
3137+
});
3138+
3139+
yield* adapter.sendTurn({
3140+
threadId: session.threadId,
3141+
input: "go",
3142+
attachments: [],
3143+
});
3144+
3145+
// An earlier message of the same turn completes normally, leaving a completed block
3146+
// at the head of the turn-wide block list.
3147+
harness.query.emit({
3148+
type: "stream_event",
3149+
session_id: "sdk-session-misaligned-snapshot",
3150+
uuid: "misaligned-start-1",
3151+
parent_tool_use_id: null,
3152+
event: {
3153+
type: "content_block_start",
3154+
index: 0,
3155+
content_block: { type: "text", text: "" },
3156+
},
3157+
} as unknown as SDKMessage);
3158+
3159+
harness.query.emit({
3160+
type: "stream_event",
3161+
session_id: "sdk-session-misaligned-snapshot",
3162+
uuid: "misaligned-delta-1",
3163+
parent_tool_use_id: null,
3164+
event: {
3165+
type: "content_block_delta",
3166+
index: 0,
3167+
delta: { type: "text_delta", text: "Earlier answer." },
3168+
},
3169+
} as unknown as SDKMessage);
3170+
3171+
harness.query.emit({
3172+
type: "stream_event",
3173+
session_id: "sdk-session-misaligned-snapshot",
3174+
uuid: "misaligned-stop-1",
3175+
parent_tool_use_id: null,
3176+
event: { type: "content_block_stop", index: 0 },
3177+
} as unknown as SDKMessage);
3178+
3179+
// The next message stalls after one delta of its FIRST text block; its second text
3180+
// block never starts streaming. Naive tail alignment would slide the snapshot's last
3181+
// block onto the stalled one and attach the wrong paragraph.
3182+
harness.query.emit({
3183+
type: "stream_event",
3184+
session_id: "sdk-session-misaligned-snapshot",
3185+
uuid: "misaligned-start-2",
3186+
parent_tool_use_id: null,
3187+
event: {
3188+
type: "content_block_start",
3189+
index: 0,
3190+
content_block: { type: "text", text: "" },
3191+
},
3192+
} as unknown as SDKMessage);
3193+
3194+
harness.query.emit({
3195+
type: "stream_event",
3196+
session_id: "sdk-session-misaligned-snapshot",
3197+
uuid: "misaligned-delta-2",
3198+
parent_tool_use_id: null,
3199+
event: {
3200+
type: "content_block_delta",
3201+
index: 0,
3202+
delta: { type: "text_delta", text: "Fixing" },
3203+
},
3204+
} as unknown as SDKMessage);
3205+
3206+
harness.query.emit({
3207+
type: "assistant",
3208+
session_id: "sdk-session-misaligned-snapshot",
3209+
uuid: "misaligned-assistant-snapshot",
3210+
parent_tool_use_id: null,
3211+
message: {
3212+
id: "assistant-message-misaligned",
3213+
content: [
3214+
{ type: "text", text: firstParagraph },
3215+
{ type: "text", text: secondParagraph },
3216+
],
3217+
},
3218+
} as unknown as SDKMessage);
3219+
3220+
harness.query.emit({
3221+
type: "result",
3222+
subtype: "success",
3223+
is_error: false,
3224+
errors: [],
3225+
session_id: "sdk-session-misaligned-snapshot",
3226+
uuid: "result-misaligned",
3227+
} as unknown as SDKMessage);
3228+
3229+
const runtimeEvents = Array.from(yield* Fiber.join(runtimeEventsFiber));
3230+
3231+
const assistantDeltas = runtimeEvents.filter(
3232+
(event) =>
3233+
event.type === "content.delta" && event.payload.streamKind === "assistant_text",
3234+
);
3235+
const deltaText = (event: (typeof assistantDeltas)[number]) =>
3236+
event.type === "content.delta" ? String(event.payload.delta) : "";
3237+
3238+
// Stalled block: streamed prefix plus the repaired suffix, exactly once.
3239+
const stalledItemId = String(assistantDeltas[1]?.itemId);
3240+
const stalledText = assistantDeltas
3241+
.filter((event) => String(event.itemId) === stalledItemId)
3242+
.map(deltaText)
3243+
.join("");
3244+
assert.equal(stalledText, firstParagraph);
3245+
3246+
// The paragraph that never streamed is synthesized as its own block, not glued onto
3247+
// the stalled one and not dropped.
3248+
const synthesizedDelta = assistantDeltas.find(
3249+
(event) => deltaText(event) === secondParagraph,
3250+
);
3251+
assert.ok(synthesizedDelta);
3252+
assert.notEqual(String(synthesizedDelta?.itemId), stalledItemId);
3253+
3254+
const assistantCompletions = runtimeEvents.filter(
3255+
(event) =>
3256+
event.type === "item.completed" && event.payload.itemType === "assistant_message",
3257+
);
3258+
// Completion order: the streamed first message (no detail — nothing was backfilled),
3259+
// then the synthesized paragraph (closed right at the snapshot), and the stalled block
3260+
// last — it stays open for late deltas until the turn result force-completes it.
3261+
assert.deepEqual(
3262+
assistantCompletions.map((event) =>
3263+
event.type === "item.completed" ? event.payload.detail : undefined,
3264+
),
3265+
[undefined, secondParagraph, firstParagraph],
3266+
);
3267+
}).pipe(
3268+
Effect.provideService(Random.Random, makeDeterministicRandomService()),
3269+
Effect.provide(harness.layer),
3270+
);
3271+
},
3272+
);
3273+
31193274
it.effect("segments Claude assistant text blocks around tool calls", () => {
31203275
const harness = makeHarness();
31213276
return Effect.gen(function* () {

‎apps/server/src/provider/Layers/ClaudeAdapter.ts‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,14 +1951,38 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
19511951
}));
19521952

19531953
// A snapshot describes ONE assistant message, but `assistantTextBlockOrder` accumulates
1954-
// every text block of the whole turn and is never reset between messages. Matching from
1955-
// the head therefore poured the snapshot's text into blocks of *earlier* messages once a
1956-
// turn contained more than one of them, leaving the message whose stream actually broke
1957-
// unrepaired. The snapshot's blocks are the most recent ones, so skip the leftovers of
1958-
// earlier messages. Never shift past the head: when the snapshot has MORE text blocks than
1959-
// the turn recorded, some block never started streaming at all, and the trailing entries
1960-
// are the ones missing — those still have to be synthesized head-first, as before.
1961-
const tailOffset = Math.max(0, orderedBlocks.length - snapshotTextBlocks.length);
1954+
// every text block of the whole turn and is never reset between messages, so position
1955+
// alone cannot say which trailing blocks are the snapshot's. Pure tail alignment breaks
1956+
// the moment the stream died before every snapshot block was opened: the window then
1957+
// swallows completed blocks of earlier messages and each paragraph lands one block off.
1958+
// The streamed prefixes disambiguate — the snapshot must extend whatever its open blocks
1959+
// already streamed. Pick the shift that binds the most open blocks compatibly. Ties keep
1960+
// the FIRST such shift: preferring a later one would slide past a fully-completed message
1961+
// whose snapshot arrives after the fact and re-synthesize its text as a duplicate block.
1962+
let tailOffset = Math.max(0, orderedBlocks.length - snapshotTextBlocks.length);
1963+
let bestBoundOpenBlocks = -1;
1964+
for (let shift = 0; shift <= orderedBlocks.length; shift++) {
1965+
let boundOpenBlocks = 0;
1966+
let compatible = true;
1967+
for (const [position, text] of snapshotTextBlocks.entries()) {
1968+
const candidate = orderedBlocks[shift + position];
1969+
if (!candidate) {
1970+
break;
1971+
}
1972+
if (candidate.block.completionEmitted) {
1973+
continue;
1974+
}
1975+
if (!text.startsWith(candidate.block.streamedText)) {
1976+
compatible = false;
1977+
break;
1978+
}
1979+
boundOpenBlocks += 1;
1980+
}
1981+
if (compatible && boundOpenBlocks > bestBoundOpenBlocks) {
1982+
bestBoundOpenBlocks = boundOpenBlocks;
1983+
tailOffset = shift;
1984+
}
1985+
}
19621986

19631987
for (const [position, text] of snapshotTextBlocks.entries()) {
19641988
const existingEntry = orderedBlocks[tailOffset + position];

‎pnpm-lock.yaml‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)