Skip to content

Commit 92f03ee

Browse files
yaacovcorcoscodexclaude
committed
fix(client): late answers no longer vanish when only part of a thread is loaded
With partial thread history, an item that arrived after a later item of the same run had raised the ordinal watermark was dropped as old history, so an answer could arrive and never appear. Keep a missing item when the loaded window already shows an earlier local item of the same run. Items from runs outside the window are still dropped. Co-Authored-By: Codex (gpt-6.1-sol) <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 9bd1d80 commit 92f03ee

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎packages/client-runtime/src/state/orchestrationV2Projection.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,53 @@ describe("applyOrchestrationV2ProjectionEvent", () => {
275275
expect(next?.visibleTurnItems.map((row) => row.position)).toEqual([0, 1]);
276276
});
277277

278+
it.each([
279+
{ name: "keeps an item from a loaded run", itemRunId: runId, kept: true },
280+
{
281+
name: "drops an item from an unloaded run",
282+
itemRunId: RunId.make("run-unloaded"),
283+
kept: false,
284+
},
285+
])("partial timeline: $name that arrives after a later sibling", ({ itemRunId, kept }) => {
286+
// Only this run is loaded. Its later item already advanced the watermark.
287+
const first = commandItem("item-first", "first", 11);
288+
const later = commandItem("item-later", "later", 13);
289+
const late = { ...commandItem("item-late", "late", 12), runId: itemRunId };
290+
const projection = {
291+
...emptyProjection,
292+
runs: [run, { ...run, id: RunId.make("run-unloaded") }],
293+
turnItems: [first, later],
294+
visibleTurnItems: [first, later].map((item, position) => ({
295+
position,
296+
visibility: "local" as const,
297+
sourceThreadId: threadId,
298+
sourceItemId: item.id,
299+
item,
300+
})),
301+
};
302+
const event = {
303+
id: "event-late-item",
304+
type: "turn-item.updated",
305+
threadId,
306+
occurredAt: now,
307+
payload: late,
308+
} as OrchestrationV2DomainEvent;
309+
310+
const next = applyOrchestrationV2ProjectionEvent(projection, event, {
311+
partialTimeline: true,
312+
latestLocalTurnOrdinal: later.ordinal,
313+
});
314+
if (kept) {
315+
expect(next?.visibleTurnItems.map((row) => row.item.id)).toEqual([
316+
first.id,
317+
late.id,
318+
later.id,
319+
]);
320+
} else {
321+
expect(next).toBe(projection);
322+
}
323+
});
324+
278325
it("removes only hidden local items while preserving inherited rows", () => {
279326
const inherited = commandItem("item-inherited");
280327
const local = commandItem("item-local");

‎packages/client-runtime/src/state/orchestrationV2Projection.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ function shouldDropMissingPartialTurnItem(
9494
if (projection.visibleTurnItems.some((row) => row.sourceItemId === item.id)) {
9595
return false;
9696
}
97+
// A later item of the same run can raise the watermark first. Keep items
98+
// that land inside a loaded run instead of treating them as old history.
99+
if (
100+
item.runId !== null &&
101+
projection.visibleTurnItems.some(
102+
(row) =>
103+
row.visibility === "local" &&
104+
row.item.runId === item.runId &&
105+
row.item.ordinal <= item.ordinal,
106+
)
107+
) {
108+
return false;
109+
}
97110
if (
98111
latestLocalTurnOrdinal !== null &&
99112
latestLocalTurnOrdinal !== undefined &&

0 commit comments

Comments
 (0)