Skip to content

Commit 7140af8

Browse files
t3dotggclaude
andcommitted
fix(server): keep runless rows when the tail run starts late
The work-grouping start could land above the newest visible rows when a run started after them, so runless rows below it dropped out of the window and older history looked empty. Clamp each run start to that point and scope the fallback lookup to the thread. Reuse DEFERRED_WORK_ITEM_TYPES, name the bound query, and cover the wake-run fallback, the runless prefix, and a VM-step scaling check. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 020cc78 commit 7140af8

3 files changed

Lines changed: 109 additions & 29 deletions

File tree

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

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as NodeSqlite from "node:sqlite";
2+
13
import { assert, it, vi } from "@effect/vitest";
24
import {
35
EnvironmentId,
@@ -57,6 +59,13 @@ import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";
5759

5860
import { layerReaderDatabase, traceSqlRouting } from "../persistence/testkit/SqliteRouting.ts";
5961

62+
declare module "node:sqlite" {
63+
interface StatementSync {
64+
// Node 26 has this; @types/node 24 does not declare it yet.
65+
stat(counter: "vmStep"): number;
66+
}
67+
}
68+
6069
const layerTest = Layer.mergeAll(
6170
ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
6271
SqlitePersistence.layerMemory,
@@ -3367,13 +3376,94 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
33673376
userTurnLimit: 10,
33683377
deferWork: true,
33693378
});
3379+
const straddledOrdinals = [
3380+
...Array.from({ length: sqlPageLimit }, (_, index) => 900 - sqlPageLimit + index),
3381+
1000,
3382+
];
33703383
assert.deepEqual(
33713384
straddledWindow.projection.turnItems.map((item) => item.ordinal),
3372-
[...Array.from({ length: sqlPageLimit }, (_, index) => 900 - sqlPageLimit + index), 1000],
3385+
straddledOrdinals,
3386+
);
3387+
// A run that starts with work (a wake) has no segment start, so the group begins at its first item.
3388+
const moveFirstItemToRun = (runId: string) => sql`
3389+
UPDATE orchestration_v2_projection_turn_items SET run_id = ${runId},
3390+
payload_json = json_set(payload_json, '$.runId', ${runId})
3391+
WHERE thread_id = ${threadId} AND ordinal = 1`;
3392+
yield* moveFirstItemToRun("run:bounded-sql-history:2");
3393+
const wakeWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
3394+
rowLimit: sqlPageLimit,
3395+
userTurnLimit: 10,
3396+
deferWork: true,
3397+
});
3398+
assert.deepEqual(
3399+
wakeWindow.projection.turnItems.map((item) => item.ordinal),
3400+
straddledOrdinals,
33733401
);
3402+
yield* moveFirstItemToRun(sharedRunId);
33743403
yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution',
33753404
payload_json = json_set(payload_json, '$.type', 'command_execution')
33763405
WHERE thread_id = ${threadId} AND ordinal BETWEEN 300 AND 899`;
3406+
3407+
// Runless rows below a run that starts after the newest 502 visible rows stay in the window.
3408+
yield* sql`UPDATE orchestration_v2_projection_turn_items
3409+
SET run_id = CASE WHEN ordinal > 990 THEN 'run:bounded-sql-history:991' END,
3410+
type = CASE WHEN ordinal = 991 THEN 'user_message' ELSE 'assistant_message' END,
3411+
payload_json = CASE WHEN ordinal = 991
3412+
THEN json_set(payload_json, '$.type', 'user_message', '$.runId', 'run:bounded-sql-history:991',
3413+
'$.createdBy', 'user', '$.creationSource', 'web', '$.inputIntent', 'turn_start',
3414+
'$.messageId', turn_item_id, '$.text', 'Turn 991', '$.attachments', json('[]'))
3415+
ELSE json_set(payload_json, '$.type', 'assistant_message',
3416+
'$.runId', CASE WHEN ordinal > 990 THEN 'run:bounded-sql-history:991' END,
3417+
'$.text', 'Working', '$.messageId', turn_item_id, '$.streaming', json('false'))
3418+
END
3419+
WHERE thread_id = ${threadId} AND ordinal > 1`;
3420+
const windowStatements: Array<readonly [string, ReadonlyArray<unknown>]> = [];
3421+
const runlessWindow = yield* projectionStore
3422+
.getThreadSnapshotWindow(threadId, {
3423+
rowLimit: sqlPageLimit,
3424+
userTurnLimit: 10,
3425+
deferWork: true,
3426+
})
3427+
.pipe(
3428+
Effect.provideService(Statement.CurrentTransformer, (statement) => {
3429+
windowStatements.push(statement.compile());
3430+
return Effect.succeed(statement);
3431+
}),
3432+
);
3433+
// The window read must not grow with history below the newest visible rows. VM steps are
3434+
// deterministic; reading the whole history here cost 65% more steps.
3435+
const copyPath = "file:/thread-window-steps?vfs=memdb";
3436+
{
3437+
using copy = new NodeSqlite.DatabaseSync(copyPath);
3438+
yield* sql`VACUUM INTO ${copyPath}`;
3439+
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);
3447+
const fullSteps = windowSteps();
3448+
copy.exec(
3449+
"DELETE FROM orchestration_v2_projection_turn_items WHERE ordinal BETWEEN 2 AND 498",
3450+
);
3451+
assert.isAtMost(fullSteps, windowSteps() * 1.1);
3452+
}
3453+
assert.deepEqual(
3454+
runlessWindow.projection.turnItems.map((item) => item.ordinal),
3455+
Array.from({ length: THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT }, (_, index) => 499 + index),
3456+
);
3457+
const runlessPage = buildBoundedThreadProjection({
3458+
projection: runlessWindow.projection,
3459+
snapshotSequence: runlessWindow.snapshotSequence,
3460+
});
3461+
assert.isTrue(runlessPage.hasMoreHistory);
3462+
assert.isNotNull(runlessPage.historyCursor);
3463+
yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = ${sharedRunId},
3464+
type = 'command_execution',
3465+
payload_json = json_set(payload_json, '$.type', 'command_execution', '$.runId', ${sharedRunId})
3466+
WHERE thread_id = ${threadId} AND ordinal > 1`;
33773467
yield* sql`
33783468
UPDATE orchestration_v2_projection_turn_items
33793469
SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution')

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

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import {
101101
isConversationHistoryItem,
102102
THREAD_HISTORY_MAX_RAW_TURNS,
103103
missingInterruptRequests,
104+
DEFERRED_WORK_ITEM_TYPES,
104105
deferWorkHistoryRows,
105106
selectDeferredWorkPage,
106107
encodeThreadHistoryCursor,
@@ -735,16 +736,6 @@ export class ProjectionStoreV2 extends Context.Service<ProjectionStoreV2, Projec
735736

736737
export const ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION = 2;
737738

738-
// Completed items of these types can fold into one deferred-work row in snapshot windows.
739-
const DEFERRABLE_WORK_TYPES = [
740-
"command_execution",
741-
"reasoning",
742-
"file_change",
743-
"file_search",
744-
"web_search",
745-
"dynamic_tool",
746-
] as const;
747-
748739
function upsertById<T extends { readonly id: string }>(items: ReadonlyArray<T>, next: T): Array<T> {
749740
const index = items.findIndex((item) => item.id === next.id);
750741
if (index === -1) {
@@ -3042,21 +3033,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30423033
SELECT ordinal, (SELECT MAX(ordinal) FROM user_anchors) AS latest FROM boundary
30433034
`;
30443035
const boundary = anchors[0]!;
3045-
// Folding work needs whole segments, so the window would otherwise walk every item
3046-
// since the boundary. Types outside the deferrable set always keep their own row,
3047-
// so the window cannot reach below the newest `tailRows` of them; start at the
3048-
// latest segment of each run that reaches that point so its groups stay whole.
3036+
// Folding needs whole segments, so the window would walk every item since the boundary.
3037+
// Non-deferrable types always keep their own row, so the window stays above the newest
3038+
// `tailRows` of them. Start at the latest segment start of each run that reaches that
3039+
// point, clamped to it so runless rows below stay in the scan.
30493040
const tailRows = Math.max(window.rowLimit, THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT);
3050-
const workStart =
3051-
window.deferWork !== true || boundary.latest === null
3052-
? boundary.ordinal
3053-
: Math.max(
3054-
boundary.ordinal,
3055-
(yield* sql<{ readonly ordinal: number | null }>`
3041+
const tailWorkStart = sql<{ readonly ordinal: number | null }>`
30563042
${eligible}, visible_tail AS (
30573043
SELECT ordinal FROM eligible
30583044
WHERE ordinal >= ${boundary.ordinal}
3059-
AND type NOT IN ${sql.in(DEFERRABLE_WORK_TYPES)}
3045+
AND type NOT IN ${sql.in(DEFERRED_WORK_ITEM_TYPES)}
30603046
ORDER BY ordinal DESC
30613047
LIMIT ${tailRows}
30623048
), tail AS (
@@ -3066,7 +3052,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30663052
FROM tail CROSS JOIN eligible AS item
30673053
WHERE item.ordinal >= tail.ordinal AND item.run_id IS NOT NULL
30683054
)
3069-
SELECT MIN(COALESCE(
3055+
SELECT MIN(MIN(COALESCE(
30703056
(
30713057
SELECT start.ordinal FROM eligible AS start
30723058
WHERE start.run_id = tail_runs.run_id AND start.type = 'user_message'
@@ -3075,13 +3061,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30753061
),
30763062
(
30773063
SELECT MIN(first.ordinal) FROM orchestration_v2_projection_turn_items AS first
3078-
WHERE first.run_id = tail_runs.run_id
3064+
WHERE first.thread_id = ${threadId} AND first.run_id = tail_runs.run_id
30793065
),
30803066
tail.ordinal
3081-
)) AS ordinal
3067+
), tail.ordinal)) AS ordinal
30823068
FROM tail LEFT JOIN tail_runs
3083-
`)[0]?.ordinal ?? 0,
3084-
);
3069+
`;
3070+
const workStart =
3071+
window.deferWork === true && boundary.latest !== null
3072+
? Math.max(boundary.ordinal, (yield* tailWorkStart)[0]?.ordinal ?? 0)
3073+
: boundary.ordinal;
30853074
return sql`
30863075
${eligible}, blocked_runs AS MATERIALIZED (
30873076
SELECT DISTINCT run_id FROM orchestration_v2_projection_turn_items
@@ -3098,7 +3087,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30983087
MAX(CASE WHEN type = 'user_message' THEN ordinal ELSE 0 END)
30993088
OVER (PARTITION BY run_id ORDER BY ordinal) AS segment,
31003089
CASE WHEN status = 'completed' AND (run_status = 'completed' OR attempt_status = 'superseded')
3101-
AND type IN ${sql.in(DEFERRABLE_WORK_TYPES)}
3090+
AND type IN ${sql.in(DEFERRED_WORK_ITEM_TYPES)}
31023091
AND NOT (${inlineWorkOutput})
31033092
AND run_id NOT IN (SELECT run_id FROM blocked_runs)
31043093
THEN 1 ELSE 0 END AS can_defer
@@ -3140,6 +3129,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
31403129
FROM window_items
31413130
WHERE ordinal >= ${boundary.ordinal}
31423131
ORDER BY ordinal DESC, turn_item_id DESC
3132+
-- Every branch must stay <= tailRows, or rows below workStart go missing.
31433133
LIMIT CASE
31443134
WHEN ${window.rowLimit} = 0 THEN 0
31453135
WHEN (SELECT row_count FROM latest_user_turn)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function isThreadHistoryTurnStart(item: HistoryRow["item"]): boolean {
183183
);
184184
}
185185

186-
const DEFERRED_WORK_ITEM_TYPES = [
186+
export const DEFERRED_WORK_ITEM_TYPES = [
187187
"command_execution",
188188
"reasoning",
189189
"file_change",

0 commit comments

Comments
 (0)