Skip to content

Commit a1351d1

Browse files
t3dotggclaude
andcommitted
fix(server): window tail bound only runs for whole-number row limits
Also covers a blocked run below the tail and guards the step-count test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 47d4475 commit a1351d1

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,6 +3400,25 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
34003400
straddledOrdinals,
34013401
);
34023402
yield* moveFirstItemToRun(sharedRunId);
3403+
// A failed error below the tail still blocks folding for its run. Only the SQL columns
3404+
// change; the row sits below the window, so its payload is never decoded.
3405+
const setSecondItem = (type: string, status: string) => sql`
3406+
UPDATE orchestration_v2_projection_turn_items SET type = ${type}, status = ${status}
3407+
WHERE thread_id = ${threadId} AND ordinal = 2`;
3408+
yield* setSecondItem("error", "failed");
3409+
const blockedWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
3410+
rowLimit: sqlPageLimit,
3411+
userTurnLimit: 10,
3412+
deferWork: true,
3413+
});
3414+
assert.isFalse(
3415+
blockedWindow.projection.visibleTurnItems.some((row) => row.item.deferredWork),
3416+
);
3417+
assert.deepEqual(
3418+
blockedWindow.projection.turnItems.map((item) => item.ordinal),
3419+
Array.from({ length: sqlPageLimit }, (_, index) => 1001 - sqlPageLimit + index),
3420+
);
3421+
yield* setSecondItem("command_execution", "completed");
34033422
yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution',
34043423
payload_json = json_set(payload_json, '$.type', 'command_execution')
34053424
WHERE thread_id = ${threadId} AND ordinal BETWEEN 300 AND 899`;
@@ -3436,15 +3455,15 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
34363455
{
34373456
using copy = new NodeSqlite.DatabaseSync(copyPath);
34383457
yield* sql`VACUUM INTO ${copyPath}`;
3458+
assert.isNotEmpty(windowStatements);
34393459
const windowSteps = () =>
3440-
windowStatements
3441-
.filter(([query]) => /blocked_runs AS MATERIALIZED|visible_tail AS/.test(query))
3442-
.reduce((steps, [query, params]) => {
3443-
const statement = copy.prepare(query);
3444-
statement.all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>));
3445-
return steps + statement.stat("vmStep");
3446-
}, 0);
3460+
windowStatements.reduce((steps, [query, params]) => {
3461+
const statement = copy.prepare(query);
3462+
statement.all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>));
3463+
return steps + statement.stat("vmStep");
3464+
}, 0);
34473465
const fullSteps = windowSteps();
3466+
assert.isAbove(fullSteps, 0);
34483467
copy.exec(
34493468
"DELETE FROM orchestration_v2_projection_turn_items WHERE ordinal BETWEEN 2 AND 498",
34503469
);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3067,8 +3067,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30673067
), tail.ordinal)) AS ordinal
30683068
FROM tail LEFT JOIN tail_runs
30693069
`;
3070+
// A negative or fractional limit makes SQLite's LIMIT disagree with `tailRows`; keep the old start.
30703071
const workStart =
3071-
window.deferWork === true && boundary.latest !== null
3072+
window.deferWork === true &&
3073+
boundary.latest !== null &&
3074+
Number.isSafeInteger(window.rowLimit) &&
3075+
window.rowLimit >= 0
30723076
? Math.max(boundary.ordinal, (yield* tailWorkStart)[0]?.ordinal ?? 0)
30733077
: boundary.ordinal;
30743078
return sql`

0 commit comments

Comments
 (0)