|
| 1 | +import * as NodeSqlite from "node:sqlite"; |
| 2 | + |
1 | 3 | import { assert, it, vi } from "@effect/vitest"; |
2 | 4 | import { |
3 | 5 | EnvironmentId, |
@@ -57,6 +59,13 @@ import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts"; |
57 | 59 |
|
58 | 60 | import { layerReaderDatabase, traceSqlRouting } from "../persistence/testkit/SqliteRouting.ts"; |
59 | 61 |
|
| 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 | + |
60 | 69 | const layerTest = Layer.mergeAll( |
61 | 70 | ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)), |
62 | 71 | SqlitePersistence.layerMemory, |
@@ -3367,13 +3376,94 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { |
3367 | 3376 | userTurnLimit: 10, |
3368 | 3377 | deferWork: true, |
3369 | 3378 | }); |
| 3379 | + const straddledOrdinals = [ |
| 3380 | + ...Array.from({ length: sqlPageLimit }, (_, index) => 900 - sqlPageLimit + index), |
| 3381 | + 1000, |
| 3382 | + ]; |
3370 | 3383 | assert.deepEqual( |
3371 | 3384 | 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, |
3373 | 3401 | ); |
| 3402 | + yield* moveFirstItemToRun(sharedRunId); |
3374 | 3403 | yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution', |
3375 | 3404 | payload_json = json_set(payload_json, '$.type', 'command_execution') |
3376 | 3405 | 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`; |
3377 | 3467 | yield* sql` |
3378 | 3468 | UPDATE orchestration_v2_projection_turn_items |
3379 | 3469 | SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution') |
|
0 commit comments