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, 19:45 UTC
fix(server): Load earlier turns no longer drops the middle of threads an agent started (#35405)
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Theo Browne committed
Nov 12, 2026 at 11:05 UTC
17ddf292aafc0f186fdc907c5ee7423693299ab5
3 files changed
+88
-1
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+74
@@ -54,6 +54,7 @@ import {
54
encodeThreadHistoryCursor,
55
selectHistoryPageFromCursor,
56
THREAD_HISTORY_PAGE_POLICY,
57
+ THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
58
THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
59
} from "./threadHistoryPaging.ts";
60
import { buildBoundedThreadStreamSnapshot } from "./ThreadStream.ts";
@@ -5688,6 +5689,79 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5689
)
5690
`;
5691
assert.strictEqual((yield* forkPage()).totalItems, forkTotal + 2);
5692
+
5693
+ // An agent started this thread, so the snapshot folds no work. Its cursor then names a
5694
+ // command that an older page folds into the run's one work group.
5695
+ yield* sql`
5696
+ UPDATE orchestration_v2_projection_turn_items
5697
+ SET type = CASE WHEN ordinal = 0 THEN type ELSE 'assistant_message' END,
5698
+ payload_json = CASE
5699
+ WHEN ordinal = 0 THEN json_set(payload_json, '$.createdBy', 'agent')
5700
+ ELSE json_set(payload_json, '$.type', 'assistant_message',
5701
+ '$.messageId', turn_item_id, '$.streaming', json('false'))
5702
+ END
5703
+ WHERE thread_id = ${threadId} AND type = 'user_message'
5704
+ `;
5705
+ yield* sql`
5706
+ INSERT INTO orchestration_events (
5707
+ event_id, aggregate_kind, stream_id, stream_version, event_type,
5708
+ occurred_at, actor_kind, payload_json, metadata_json, application_event_version
5709
+ ) VALUES (
5710
+ 'event:paged-scrollback:agent-started', 'thread', ${threadId}, 2,
5711
+ 'turn-item.updated', ${nowIso}, 'user', '{}', '{}', 2
5712
+ )
5713
+ `;
5714
+ const window = yield* projectionStore.getThreadSnapshotWindow(threadId, {
5715
+ rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
5716
+ userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns,
5717
+ deferWork: true,
5718
+ });
5719
+ const bounded = buildBoundedThreadProjection({
5720
+ projection: window.projection,
5721
+ snapshotSequence: window.snapshotSequence,
5722
+ });
5723
+ const loaded = [bounded.projection.visibleTurnItems];
5724
+ let earlier = bounded.hasMoreHistory ? bounded.historyCursor : null;
5725
+ while (earlier !== null) {
5726
+ assert.isBelow(loaded.length, 20);
5727
+ const page: OrchestrationV2ThreadHistoryPage = yield* projectionStore.getThreadHistoryPage(
5728
+ threadId,
5729
+ earlier,
5730
+ undefined,
5731
+ false,
5732
+ "deferred",
5733
+ );
5734
+ loaded.unshift(page.items);
5735
+ earlier = page.hasMoreHistory ? page.nextCursor : null;
5736
+ }
5737
+ const held = loaded.flat();
5738
+ const heldOrdinals = held.map((row) => row.item.ordinal);
5739
+ assert.deepEqual(
5740
+ heldOrdinals,
5741
+ [...new Set(heldOrdinals)].toSorted((left, right) => left - right),
5742
+ );
5743
+ const folded = held.find((row) => row.item.deferredWork !== undefined)?.item;
5744
+ assert.isAbove(folded?.deferredWork?.itemCount ?? 0, 150);
5745
+ const everyRow = yield* projectionStore.getTimelinePage(threadId, {
5746
+ view: "activity",
5747
+ limit: itemCount + 2,
5748
+ });
5749
+ // Every row is on the client, or is work that the folded row stands for.
5750
+ assert.deepEqual(
5751
+ everyRow.items
5752
+ .filter(
5753
+ ({ item }) =>
5754
+ !heldOrdinals.includes(item.ordinal) &&
5755
+ !(
5756
+ item.type === "command_execution" &&
5757
+ folded?.deferredWork !== undefined &&
5758
+ item.ordinal > folded.ordinal &&
5759
+ item.ordinal <= folded.deferredWork.lastOrdinal
5760
+ ),
5761
+ )
5762
+ .map(({ item }) => item.ordinal),
5763
+ [],
5764
+ );
5765
}),
5766
);
5767
apps/server/src/orchestration-v2/ProjectionStore.ts
+11
@@ -6943,6 +6943,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6943
const historyItems =
6944
workMode === "deferred" ? deferWorkHistoryRows(indexed) : indexed;
6945
const policy = olderHistoryPagePolicy({ items: indexed, cursor, throughEntryId });
6946
+ // Folding can hide the cursor's row: a snapshot folds no work in a thread with no
6947
+ // user turn. The page then ends at the first row kept from the cursor on.
6948
+ const cursorRow =
6949
+ indexed[findCursorEndIndex(indexed, decodeThreadHistoryCursor(cursor))];
6950
+ const end =
6951
+ cursorRow === undefined
6952
+ ? -1
6953
+ : historyItems.findIndex(
6954
+ (row) => row.canonicalPosition >= cursorRow.canonicalPosition,
6955
+ );
6956
const page =
6957
workMode === "work"
6958
? selectDeferredWorkPage({
@@ -6955,6 +6965,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6965
: selectHistoryPageFromCursor({
6966
items: historyItems,
6967
cursor,
6968
+ exclusiveEndIndex: end === -1 ? historyItems.length : end,
6969
snapshotSequence,
6970
rowEncodedBytes: () => 0,
6971
policy,
apps/server/src/orchestration-v2/threadHistoryPaging.ts
+3
-1
@@ -485,13 +485,15 @@ export function selectHistoryPageFromCursor<Row extends HistoryRow>(input: {
485
readonly policy?: ThreadHistoryPagePolicy | undefined;
486
readonly rowEncodedBytes?: ((row: Row) => number) | undefined;
487
readonly throughEntryId?: string | undefined;
488
+ /** Where the page ends in `items`. Set when `items` can lack the cursor's row. */
489
+ readonly exclusiveEndIndex?: number | undefined;
490
/** Set when `items` holds only the newest part of the history. */
491
readonly prefix?: ThreadHistoryPrefix | undefined;
492
}): SelectTimelinePageResult<Row> {
493
const decoded = decodeThreadHistoryCursor(input.cursor);
494
return selectOlderTimelinePage({
495
items: input.items,
494
- exclusiveEndIndex: findCursorEndIndex(input.items, decoded),
496
+ exclusiveEndIndex: input.exclusiveEndIndex ?? findCursorEndIndex(input.items, decoded),
497
snapshotSequence: input.snapshotSequence,
498
...(input.rowEncodedBytes === undefined ? {} : { rowEncodedBytes: input.rowEncodedBytes }),
499
policy: olderHistoryPagePolicy(input),