Skip to content

Commit 52fb7da

Browse files
t3dotggclaude
andcommitted
fix(server): Load earlier turns no longer drops the middle of threads an agent started
A snapshot folds no work in a thread with no user turn, so its cursor can name a row that an older page folds into a work group. The page then fell back to the cursor's position, which is relative to the snapshot, and returned the first rows of the thread with no more history. The page now finds the cursor in the unfolded rows and ends at the first row it keeps from there on. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent f03db4f commit 52fb7da

3 files changed

Lines changed: 103 additions & 23 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
encodeThreadHistoryCursor,
5555
selectHistoryPageFromCursor,
5656
THREAD_HISTORY_PAGE_POLICY,
57+
THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
5758
THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
5859
} from "./threadHistoryPaging.ts";
5960
import { buildBoundedThreadStreamSnapshot } from "./ThreadStream.ts";
@@ -5555,6 +5556,79 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
55555556
)
55565557
`;
55575558
assert.strictEqual((yield* forkPage()).totalItems, forkTotal + 2);
5559+
5560+
// An agent started this thread, so the snapshot folds no work. Its cursor then names a
5561+
// command that an older page folds into the run's one work group.
5562+
yield* sql`
5563+
UPDATE orchestration_v2_projection_turn_items
5564+
SET type = CASE WHEN ordinal = 0 THEN type ELSE 'assistant_message' END,
5565+
payload_json = CASE
5566+
WHEN ordinal = 0 THEN json_set(payload_json, '$.createdBy', 'agent')
5567+
ELSE json_set(payload_json, '$.type', 'assistant_message',
5568+
'$.messageId', turn_item_id, '$.streaming', json('false'))
5569+
END
5570+
WHERE thread_id = ${threadId} AND type = 'user_message'
5571+
`;
5572+
yield* sql`
5573+
INSERT INTO orchestration_events (
5574+
event_id, aggregate_kind, stream_id, stream_version, event_type,
5575+
occurred_at, actor_kind, payload_json, metadata_json, application_event_version
5576+
) VALUES (
5577+
'event:paged-scrollback:agent-started', 'thread', ${threadId}, 2,
5578+
'turn-item.updated', ${nowIso}, 'user', '{}', '{}', 2
5579+
)
5580+
`;
5581+
const window = yield* projectionStore.getThreadSnapshotWindow(threadId, {
5582+
rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
5583+
userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns,
5584+
deferWork: true,
5585+
});
5586+
const bounded = buildBoundedThreadProjection({
5587+
projection: window.projection,
5588+
snapshotSequence: window.snapshotSequence,
5589+
});
5590+
const loaded = [bounded.projection.visibleTurnItems];
5591+
let earlier = bounded.hasMoreHistory ? bounded.historyCursor : null;
5592+
while (earlier !== null) {
5593+
assert.isBelow(loaded.length, 20);
5594+
const page: OrchestrationV2ThreadHistoryPage = yield* projectionStore.getThreadHistoryPage(
5595+
threadId,
5596+
earlier,
5597+
undefined,
5598+
false,
5599+
"deferred",
5600+
);
5601+
loaded.unshift(page.items);
5602+
earlier = page.hasMoreHistory ? page.nextCursor : null;
5603+
}
5604+
const held = loaded.flat();
5605+
const heldOrdinals = held.map((row) => row.item.ordinal);
5606+
assert.deepEqual(
5607+
heldOrdinals,
5608+
[...new Set(heldOrdinals)].toSorted((left, right) => left - right),
5609+
);
5610+
const folded = held.find((row) => row.item.deferredWork !== undefined)?.item;
5611+
assert.isAbove(folded?.deferredWork?.itemCount ?? 0, 150);
5612+
const everyRow = yield* projectionStore.getTimelinePage(threadId, {
5613+
view: "activity",
5614+
limit: itemCount + 2,
5615+
});
5616+
// Every row is on the client, or is work that the folded row stands for.
5617+
assert.deepEqual(
5618+
everyRow.items
5619+
.filter(
5620+
({ item }) =>
5621+
!heldOrdinals.includes(item.ordinal) &&
5622+
!(
5623+
item.type === "command_execution" &&
5624+
folded?.deferredWork !== undefined &&
5625+
item.ordinal > folded.ordinal &&
5626+
item.ordinal <= folded.deferredWork.lastOrdinal
5627+
),
5628+
)
5629+
.map(({ item }) => item.ordinal),
5630+
[],
5631+
);
55585632
}),
55595633
);
55605634

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6475,35 +6475,39 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
64756475
position,
64766476
canonicalPosition: position,
64776477
}));
6478-
const { olderPolicy, cursorPosition } = yield* Effect.try({
6478+
const { olderPolicy, cursorEnd } = yield* Effect.try({
64796479
try: () => ({
64806480
olderPolicy: olderHistoryPagePolicy({ items: indexed, cursor, throughEntryId }),
6481-
// The cursor's own row, or the row its stale position falls back to.
6482-
cursorPosition: Math.min(
6483-
findCursorEndIndex(indexed, decodeThreadHistoryCursor(cursor)),
6484-
indexed.length - 1,
6485-
),
6481+
// Past the cursor's own row, or at the row its stale position falls back to.
6482+
cursorEnd: findCursorEndIndex(indexed, decodeThreadHistoryCursor(cursor)),
64866483
}),
64876484
catch: (cause) => new ProjectionStoreReadError({ threadId, cause }),
64886485
});
6486+
const cursorPosition = Math.min(cursorEnd, indexed.length - 1);
64896487
const selectPage = (rows: typeof indexed) =>
64906488
Effect.try({
6491-
try: () =>
6492-
workMode === "work"
6493-
? selectDeferredWorkPage({
6494-
items: rows,
6495-
cursor,
6496-
throughEntryId,
6497-
snapshotSequence,
6498-
rowEncodedBytes: () => 0,
6499-
})
6500-
: selectHistoryPageFromCursor({
6501-
items: workMode === "deferred" ? deferWorkHistoryRows(rows) : rows,
6502-
cursor,
6503-
snapshotSequence,
6504-
rowEncodedBytes: () => 0,
6505-
policy: olderPolicy,
6506-
}),
6489+
try: () => {
6490+
if (workMode === "work")
6491+
return selectDeferredWorkPage({
6492+
items: rows,
6493+
cursor,
6494+
throughEntryId,
6495+
snapshotSequence,
6496+
rowEncodedBytes: () => 0,
6497+
});
6498+
const items = workMode === "deferred" ? deferWorkHistoryRows(rows) : rows;
6499+
// Folding can hide the cursor's row: a snapshot folds no work in a thread with no
6500+
// user turn. The page then ends at the first row kept from the cursor on.
6501+
const end = items.findIndex((row) => row.canonicalPosition >= cursorEnd);
6502+
return selectHistoryPageFromCursor({
6503+
items,
6504+
cursor,
6505+
exclusiveEndIndex: end === -1 ? items.length : end,
6506+
snapshotSequence,
6507+
rowEncodedBytes: () => 0,
6508+
policy: olderPolicy,
6509+
});
6510+
},
65076511
catch: (cause) => new ProjectionStoreReadError({ threadId, cause }),
65086512
});
65096513
let page = yield* selectPage(indexed);

‎apps/server/src/orchestration-v2/threadHistoryPaging.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,13 @@ export function selectHistoryPageFromCursor<Row extends HistoryRow>(input: {
458458
readonly policy?: ThreadHistoryPagePolicy | undefined;
459459
readonly rowEncodedBytes?: ((row: Row) => number) | undefined;
460460
readonly throughEntryId?: string | undefined;
461+
/** Where the page ends in `items`. Set when `items` can lack the cursor's row. */
462+
readonly exclusiveEndIndex?: number | undefined;
461463
}): SelectTimelinePageResult<Row> {
462464
const decoded = decodeThreadHistoryCursor(input.cursor);
463465
return selectOlderTimelinePage({
464466
items: input.items,
465-
exclusiveEndIndex: findCursorEndIndex(input.items, decoded),
467+
exclusiveEndIndex: input.exclusiveEndIndex ?? findCursorEndIndex(input.items, decoded),
466468
snapshotSequence: input.snapshotSequence,
467469
...(input.rowEncodedBytes === undefined ? {} : { rowEncodedBytes: input.rowEncodedBytes }),
468470
policy: olderHistoryPagePolicy(input),

0 commit comments

Comments
 (0)