|
| 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, |
@@ -3356,6 +3365,137 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { |
3356 | 3365 | yield* sql`UPDATE orchestration_v2_projection_turn_items SET ordinal = 999, |
3357 | 3366 | payload_json = json_set(payload_json, '$.ordinal', 999) |
3358 | 3367 | WHERE turn_item_id = 'turn-item:bounded-sql-history:999'`; |
| 3368 | + // Work folded below the newest 502 visible rows still counts its whole group, so the |
| 3369 | + // 100 commands after the messages stay hidden in the 398-item group. |
| 3370 | + yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'assistant_message', |
| 3371 | + payload_json = json_set(payload_json, '$.type', 'assistant_message', '$.text', 'Working', |
| 3372 | + '$.messageId', turn_item_id, '$.streaming', json('false')) |
| 3373 | + WHERE thread_id = ${threadId} AND ordinal BETWEEN 300 AND 899`; |
| 3374 | + const straddledWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, { |
| 3375 | + rowLimit: sqlPageLimit, |
| 3376 | + userTurnLimit: 10, |
| 3377 | + deferWork: true, |
| 3378 | + }); |
| 3379 | + const straddledOrdinals = [ |
| 3380 | + ...Array.from({ length: sqlPageLimit }, (_, index) => 900 - sqlPageLimit + index), |
| 3381 | + 1000, |
| 3382 | + ]; |
| 3383 | + assert.deepEqual( |
| 3384 | + straddledWindow.projection.turnItems.map((item) => item.ordinal), |
| 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, |
| 3401 | + ); |
| 3402 | + 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 | + // A steer at 100 starts a later segment, so the error sits below the scan start too. |
| 3406 | + const setItem = (ordinal: number, type: string, status: string) => sql` |
| 3407 | + UPDATE orchestration_v2_projection_turn_items SET type = ${type}, status = ${status} |
| 3408 | + WHERE thread_id = ${threadId} AND ordinal = ${ordinal}`; |
| 3409 | + yield* setItem(100, "user_message", "completed"); |
| 3410 | + yield* setItem(2, "error", "failed"); |
| 3411 | + const blockedWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, { |
| 3412 | + rowLimit: sqlPageLimit, |
| 3413 | + userTurnLimit: 10, |
| 3414 | + deferWork: true, |
| 3415 | + }); |
| 3416 | + assert.isFalse( |
| 3417 | + blockedWindow.projection.visibleTurnItems.some((row) => row.item.deferredWork), |
| 3418 | + ); |
| 3419 | + assert.deepEqual( |
| 3420 | + blockedWindow.projection.turnItems.map((item) => item.ordinal), |
| 3421 | + Array.from({ length: sqlPageLimit }, (_, index) => 1001 - sqlPageLimit + index), |
| 3422 | + ); |
| 3423 | + yield* setItem(2, "command_execution", "completed"); |
| 3424 | + yield* setItem(100, "command_execution", "completed"); |
| 3425 | + yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution', |
| 3426 | + payload_json = json_set(payload_json, '$.type', 'command_execution') |
| 3427 | + WHERE thread_id = ${threadId} AND ordinal BETWEEN 300 AND 899`; |
| 3428 | + |
| 3429 | + // Runless rows below a run that starts after the newest 502 visible rows stay in the window. |
| 3430 | + yield* sql`UPDATE orchestration_v2_projection_turn_items |
| 3431 | + SET run_id = CASE WHEN ordinal > 990 THEN 'run:bounded-sql-history:991' END, |
| 3432 | + type = CASE WHEN ordinal = 991 THEN 'user_message' ELSE 'assistant_message' END, |
| 3433 | + payload_json = CASE WHEN ordinal = 991 |
| 3434 | + THEN json_set(payload_json, '$.type', 'user_message', '$.runId', 'run:bounded-sql-history:991', |
| 3435 | + '$.createdBy', 'user', '$.creationSource', 'web', '$.inputIntent', 'turn_start', |
| 3436 | + '$.messageId', turn_item_id, '$.text', 'Turn 991', '$.attachments', json('[]')) |
| 3437 | + ELSE json_set(payload_json, '$.type', 'assistant_message', |
| 3438 | + '$.runId', CASE WHEN ordinal > 990 THEN 'run:bounded-sql-history:991' END, |
| 3439 | + '$.text', 'Working', '$.messageId', turn_item_id, '$.streaming', json('false')) |
| 3440 | + END |
| 3441 | + WHERE thread_id = ${threadId} AND ordinal > 1`; |
| 3442 | + const windowStatements: Array<readonly [string, ReadonlyArray<unknown>]> = []; |
| 3443 | + const runlessWindow = yield* projectionStore |
| 3444 | + .getThreadSnapshotWindow(threadId, { |
| 3445 | + rowLimit: sqlPageLimit, |
| 3446 | + userTurnLimit: 10, |
| 3447 | + deferWork: true, |
| 3448 | + }) |
| 3449 | + .pipe( |
| 3450 | + Effect.provideService(Statement.CurrentTransformer, (statement) => { |
| 3451 | + windowStatements.push(statement.compile()); |
| 3452 | + return Effect.succeed(statement); |
| 3453 | + }), |
| 3454 | + ); |
| 3455 | + // The window read must not grow with history below the newest visible rows. VM steps are |
| 3456 | + // deterministic; reading the whole history here cost 65% more steps. |
| 3457 | + const copyPath = "file:/thread-window-steps?vfs=memdb"; |
| 3458 | + { |
| 3459 | + using copy = new NodeSqlite.DatabaseSync(copyPath); |
| 3460 | + yield* sql`VACUUM INTO ${copyPath}`; |
| 3461 | + assert.isNotEmpty(windowStatements); |
| 3462 | + const windowSteps = () => |
| 3463 | + windowStatements.reduce((steps, [query, params]) => { |
| 3464 | + const statement = copy.prepare(query); |
| 3465 | + statement.all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>)); |
| 3466 | + return steps + statement.stat("vmStep"); |
| 3467 | + }, 0); |
| 3468 | + const fullSteps = windowSteps(); |
| 3469 | + assert.isAbove(fullSteps, 0); |
| 3470 | + copy.exec( |
| 3471 | + "DELETE FROM orchestration_v2_projection_turn_items WHERE ordinal BETWEEN 2 AND 498", |
| 3472 | + ); |
| 3473 | + assert.isAtMost(fullSteps, windowSteps() * 1.1); |
| 3474 | + } |
| 3475 | + assert.deepEqual( |
| 3476 | + runlessWindow.projection.turnItems.map((item) => item.ordinal), |
| 3477 | + Array.from({ length: THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT }, (_, index) => 499 + index), |
| 3478 | + ); |
| 3479 | + const runlessPage = buildBoundedThreadProjection({ |
| 3480 | + projection: runlessWindow.projection, |
| 3481 | + snapshotSequence: runlessWindow.snapshotSequence, |
| 3482 | + }); |
| 3483 | + assert.isTrue(runlessPage.hasMoreHistory); |
| 3484 | + assert.isNotNull(runlessPage.historyCursor); |
| 3485 | + // Limits SQLite reads differently from `tailRows` skip the tail bound and match main. |
| 3486 | + const windowOrdinalCount = (rowLimit: number) => |
| 3487 | + projectionStore |
| 3488 | + .getThreadSnapshotWindow(threadId, { rowLimit, userTurnLimit: 10, deferWork: true }) |
| 3489 | + .pipe(Effect.map((window) => window.projection.turnItems.length)); |
| 3490 | + assert.strictEqual(yield* windowOrdinalCount(502.5), THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT); |
| 3491 | + yield* sql`UPDATE orchestration_v2_projection_turn_items |
| 3492 | + SET payload_json = json_set(payload_json, '$.inputIntent', 'steer') |
| 3493 | + WHERE thread_id = ${threadId} AND ordinal = 991`; |
| 3494 | + assert.strictEqual(yield* windowOrdinalCount(-1), 1000); |
| 3495 | + yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = ${sharedRunId}, |
| 3496 | + type = 'command_execution', |
| 3497 | + payload_json = json_set(payload_json, '$.type', 'command_execution', '$.runId', ${sharedRunId}) |
| 3498 | + WHERE thread_id = ${threadId} AND ordinal > 1`; |
3359 | 3499 | yield* sql` |
3360 | 3500 | UPDATE orchestration_v2_projection_turn_items |
3361 | 3501 | SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution') |
|
0 commit comments