@devries-cameron20 / t3code Mirror · read-only / commits / 25a1afe7d5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 12:15 UTC

fix(client-runtime): opening an old search result keeps messages in order (#29711)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 14, 2026 at 20:49 UTC 25a1afe7d5ee47246015ef4d56e08b227bb282c9
2 files changed +72 -4
packages/client-runtime/src/state/threadHistoryMerge.test.ts
+38
@@ -175,6 +175,44 @@ describe("threadHistoryMerge", () => {
175 ]);
176 });
177
178 + it("keeps an older answer before a full page that only covers the later part of a conversation page", () => {
179 + const [question, olderAnswer, reasoningA, reasoningB, reasoningC, newerAnswer] = [
180 + row(0),
181 + row(1),
182 + row(2),
183 + row(3),
184 + row(4),
185 + row(5),
186 + ];
187 + const window = [row(6), row(7)];
188 + const projection = {
189 + ...v2Projection,
190 + turnItems: window.map((entry) => entry.item),
191 + visibleTurnItems: window.map((entry, position) => ({ ...entry, position })),
192 + };
193 + const conversation = mergeOlderHistoryIntoProjection(projection, [
194 + question,
195 + olderAnswer,
196 + newerAnswer,
197 + ]);
198 + const full = mergeOlderHistoryIntoProjection(conversation, [
199 + reasoningA,
200 + reasoningB,
201 + reasoningC,
202 + newerAnswer,
203 + ]);
204 + expect(full.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
205 + "item-0",
206 + "item-1",
207 + "item-2",
208 + "item-3",
209 + "item-4",
210 + "item-5",
211 + "item-6",
212 + "item-7",
213 + ]);
214 + });
215 +
216 it("does not resurrect a local item hidden while an older page was in flight", () => {
217 const stalePageRow = row(1);
218 const currentItem = {
packages/client-runtime/src/state/threadHistoryMerge.ts
+34 -4
@@ -49,6 +49,39 @@ function renumberVisible(
49 return rows.map((row, position) => (row.position === position ? row : { ...row, position }));
50 }
51
52 +// A page is a contiguous history range, so current rows before its first
53 +// visible row are older than the page start and new page rows sit just
54 +// before the next row both lists share.
55 +function interleavePage(
56 + current: ReadonlyArray<OrchestrationV2ProjectedTurnItem>,
57 + page: ReadonlyArray<OrchestrationV2ProjectedTurnItem>,
58 + pageKeys: ReadonlySet<string>,
59 +): OrchestrationV2ProjectedTurnItem[] {
60 + const currentIndex = new Map(current.map((row, index) => [projectedItemKey(row), index]));
61 + const merged: OrchestrationV2ProjectedTurnItem[] = [];
62 + let pending: OrchestrationV2ProjectedTurnItem[] = [];
63 + let next = 0;
64 + const emitCurrentThrough = (end: number) => {
65 + for (; next < end; next += 1) {
66 + const row = current[next]!;
67 + if (!pageKeys.has(projectedItemKey(row))) merged.push(row);
68 + }
69 + };
70 + for (const row of page) {
71 + const index = currentIndex.get(projectedItemKey(row));
72 + if (index === undefined) {
73 + pending.push(row);
74 + continue;
75 + }
76 + emitCurrentThrough(index + 1);
77 + merged.push(...pending, row);
78 + pending = [];
79 + }
80 + merged.push(...pending);
81 + emitCurrentThrough(current.length);
82 + return merged;
83 +}
84 +
85 export function mergeOlderHistoryIntoProjection(
86 projection: OrchestrationV2ThreadProjection,
87 olderItems: ReadonlyArray<OrchestrationV2ProjectedTurnItem>,
@@ -149,10 +182,7 @@ export function mergeOlderHistoryIntoProjection(
182 ];
183 if (anchor.visibility === "local") turnItemById.set(String(updated.item.id), updated.item);
184 } else {
152 - visible = [
153 - ...page,
154 - ...projection.visibleTurnItems.filter((row) => !pageKeys.has(projectedItemKey(row))),
155 - ];
185 + visible = interleavePage(projection.visibleTurnItems, page, pageKeys);
186 }
187 if (
188 visible.length === projection.visibleTurnItems.length &&