Skip to content

Commit af3c182

Browse files
t3dotggclaude
andcommitted
fix(server): a history page scans up to the position a missing cursor row falls back to
Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 91a5afd commit af3c182

3 files changed

Lines changed: 38 additions & 13 deletions

File tree

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3838,12 +3838,37 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
38383838
[1, 2, 3],
38393839
);
38403840
}
3841+
// A cursor whose row is gone falls back to its position. Inline output between the page
3842+
// and that position can belong to a run with no other row there.
3843+
yield* sql`UPDATE orchestration_v2_projection_turn_items
3844+
SET status = CASE WHEN ordinal <= 700 THEN 'running' ELSE 'completed' END,
3845+
run_id = CASE WHEN ordinal <= 700 THEN 'run:bounded-sql-history:300' ELSE ${sharedRunId} END,
3846+
payload_json = json_set(payload_json,
3847+
'$.status', CASE WHEN ordinal <= 700 THEN 'running' ELSE 'completed' END,
3848+
'$.runId', CASE WHEN ordinal <= 700 THEN 'run:bounded-sql-history:300' ELSE ${sharedRunId} END)
3849+
WHERE thread_id = ${threadId} AND ordinal >= 100`;
3850+
yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool',
3851+
payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task')
3852+
WHERE thread_id = ${threadId} AND ordinal = 720`;
3853+
const beforeMissingCursor = yield* historyPage(
3854+
threadId,
3855+
encodeThreadHistoryCursor({
3856+
snapshotSequence: 0,
3857+
sourceThreadId: threadId,
3858+
sourceItemId: "turn-item:bounded-sql-history:gone",
3859+
position: 800,
3860+
}),
3861+
undefined,
3862+
false,
3863+
"deferred",
3864+
);
3865+
assert.strictEqual(beforeMissingCursor.items.at(-1)?.item.ordinal, 720);
38413866
yield* sql`UPDATE orchestration_v2_projection_turn_items SET status = 'completed',
38423867
run_id = ${sharedRunId},
38433868
payload_json = json_set(payload_json, '$.status', 'completed', '$.runId', ${sharedRunId})
3844-
WHERE thread_id = ${threadId} AND ordinal >= 300`;
3869+
WHERE thread_id = ${threadId} AND ordinal >= 100`;
38453870
yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution',
3846-
payload_json = json_set(payload_json, '$.type', 'command_execution') WHERE thread_id = ${threadId} AND ordinal IN (2, 4)`;
3871+
payload_json = json_set(payload_json, '$.type', 'command_execution') WHERE thread_id = ${threadId} AND ordinal IN (2, 4, 720)`;
38473872
for (const [ordinal, status] of [
38483873
[0, "completed"],
38493874
[1, "superseded"],

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ import {
105105
DEFERRED_WORK_ITEM_TYPES,
106106
decodeThreadHistoryCursor,
107107
deferWorkHistoryRows,
108+
findCursorEndIndex,
108109
selectDeferredWorkPage,
109110
encodeThreadHistoryCursor,
110111
olderHistoryPagePolicy,
@@ -6393,15 +6394,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
63936394
canonicalPosition: position,
63946395
}));
63956396
const { olderPolicy, cursorPosition } = yield* Effect.try({
6396-
try: () => {
6397-
const { st, si } = decodeThreadHistoryCursor(cursor);
6398-
return {
6399-
olderPolicy: olderHistoryPagePolicy({ items: indexed, cursor, throughEntryId }),
6400-
cursorPosition: indexed.findLastIndex(
6401-
(row) => row.sourceThreadId === st && row.sourceItemId === si,
6402-
),
6403-
};
6404-
},
6397+
try: () => ({
6398+
olderPolicy: olderHistoryPagePolicy({ items: indexed, cursor, throughEntryId }),
6399+
// The cursor's own row, or the row its stale position falls back to.
6400+
cursorPosition: Math.min(
6401+
findCursorEndIndex(indexed, decodeThreadHistoryCursor(cursor)),
6402+
indexed.length - 1,
6403+
),
6404+
}),
64056405
catch: (cause) => new ProjectionStoreReadError({ threadId, cause }),
64066406
});
64076407
const selectPage = (rows: typeof indexed) =>
@@ -6430,7 +6430,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
64306430
const scannedRuns = new Set<string>();
64316431
const persistentOutputs = new Set<string>();
64326432
while (workMode !== undefined) {
6433-
// A cursor row hidden in a work group moves the page, so the scan always covers it.
6433+
// Rows hidden in a work group move the page, so the scan always reaches the cursor.
64346434
const span = [...page.items.slice(0, 1), ...page.items.slice(-1)].map(
64356435
(row) => row.canonicalPosition,
64366436
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export function selectRecentTimelineWindow(input: {
394394
});
395395
}
396396

397-
function findCursorEndIndex(
397+
export function findCursorEndIndex(
398398
items: ReadonlyArray<HistoryRow>,
399399
cursor: ThreadHistoryCursorPayload,
400400
): number {

0 commit comments

Comments
 (0)