Skip to content

Commit e3b6e99

Browse files
t3dotggbraulioboclaude
committed
test(server): shell read plans and stored item counts hold through rewrites and rebuilds
One plan case now covers every shell lookup that used to read thread history: the active run lookups, the last executed run, and the item counts, which must not read turn items. The item count case writes an item again, moves it between a live run, a rolled back run and no run, rolls a run back and forward, and writes an item before its run exists. These cases come from #34666. The rebuild case checks the shell count after an item rewrite and after a projection rebuild. Co-authored-by: Braulio Oliveira <[email protected]> Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent f00e0fe commit e3b6e99

2 files changed

Lines changed: 48 additions & 29 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3956,6 +3956,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
39563956
});
39573957
const afterUpdate = yield* projectionStore.getThreadProjection(threadId);
39583958
assert.equal(afterUpdate.turnItems[0]?.ordinal, ordinals[0]);
3959+
assert.equal((yield* projectionStore.getThreadShell(threadId))?.itemCount, 151);
39593960
assert.equal((yield* maintenance.verify).valid, true);
39603961

39613962
yield* sql`
@@ -3970,6 +3971,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
39703971
const rebuilt = yield* maintenance.rebuild;
39713972
assert.isTrue(rebuilt.valid);
39723973
assert.lengthOf((yield* projectionStore.getThreadProjection(threadId)).turnItems, 151);
3974+
assert.equal((yield* projectionStore.getThreadShell(threadId))?.itemCount, 151);
39733975
}),
39743976
);
39753977
});

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

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5221,7 +5221,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
52215221
}),
52225222
);
52235223

5224-
it.effect("looks up shell stop requests and active runs through their indexes", () =>
5224+
it.effect("reads shell runs and item counts without walking a thread's history", () =>
52255225
Effect.gen(function* () {
52265226
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
52275227
const sql = yield* SqlClient.SqlClient;
@@ -5230,14 +5230,16 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
52305230
yield* projectionStore
52315231
.getShellSnapshot({ projectId, location: "active" })
52325232
.pipe(Effect.withTracer(tracer));
5233+
const planOf = (statement: string) =>
5234+
sql
5235+
.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${statement}`)
5236+
.pipe(Effect.map((rows) => rows.map((row) => row.detail)));
52335237

52345238
const threadQuery = statements.find((statement) =>
52355239
statement.includes("AS latest_run_stop_requested"),
52365240
);
52375241
assert.isDefined(threadQuery);
5238-
const plan = (yield* sql.unsafe<{ readonly detail: string }>(
5239-
`EXPLAIN QUERY PLAN ${threadQuery}`,
5240-
)).map((row) => row.detail);
5242+
const plan = yield* planOf(threadQuery);
52415243
const stopRequestLookups = plan.filter((detail) => detail.startsWith("SEARCH stop_request "));
52425244
assert.lengthOf(stopRequestLookups, 2);
52435245
for (const lookup of stopRequestLookups) {
@@ -5252,32 +5254,20 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
52525254
for (const lookup of activeRunLookups) {
52535255
assert.include(lookup, "orchestration_v2_projection_runs_thread_status_idx");
52545256
}
5255-
}),
5256-
);
5257-
5258-
it.effect("reads a thread's last executed run through the last executed run index", () =>
5259-
Effect.gen(function* () {
5260-
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
5261-
const sql = yield* SqlClient.SqlClient;
5262-
const { projectId } = yield* assertProjectScopedShells("sql-shell-last-executed-index");
5263-
const { statements, tracer } = traceSqlStatements();
5264-
yield* projectionStore
5265-
.getShellSnapshot({ projectId, location: "active" })
5266-
.pipe(Effect.withTracer(tracer));
5267-
5268-
const threadQuery = statements.find((statement) =>
5269-
statement.includes("AS latest_run_stop_requested"),
5270-
);
5271-
assert.isDefined(threadQuery);
5272-
const plan = (yield* sql.unsafe<{ readonly detail: string }>(
5273-
`EXPLAIN QUERY PLAN ${threadQuery}`,
5274-
)).map((row) => row.detail);
52755257
const lastExecutedIndex = plan.findIndex((detail) =>
52765258
detail.includes("orchestration_v2_projection_runs_last_executed_idx"),
52775259
);
52785260
assert.notStrictEqual(lastExecutedIndex, -1);
52795261
// The lookup that follows the search is the sort it no longer needs.
52805262
assert.notInclude(plan[lastExecutedIndex + 1] ?? "", "USE TEMP B-TREE");
5263+
5264+
const countQueries = statements.filter((statement) => statement.includes("item_count"));
5265+
assert.isNotEmpty(countQueries);
5266+
for (const countQuery of countQueries) {
5267+
for (const detail of yield* planOf(countQuery)) {
5268+
assert.notInclude(detail, "orchestration_v2_projection_turn_items");
5269+
}
5270+
}
52815271
}),
52825272
);
52835273

@@ -5532,6 +5522,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
55325522
},
55335523
});
55345524
}
5525+
let runWrites = 0;
55355526
const applyRun = (
55365527
threadId: ThreadId,
55375528
suffix: string,
@@ -5540,9 +5531,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
55405531
) => {
55415532
const runId = RunId.make(`run:shell-item-counts:${suffix}`);
55425533
const rootNodeId = NodeId.make(`node:shell-item-counts:${suffix}`);
5534+
runWrites += 1;
55435535
return projectionStore
55445536
.apply({
5545-
id: EventId.make(`event:shell-item-counts:${suffix}:run`),
5537+
id: EventId.make(`event:shell-item-counts:${suffix}:run:${runWrites}`),
55465538
type: "run.created",
55475539
threadId,
55485540
runId,
@@ -5569,11 +5561,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
55695561
})
55705562
.pipe(Effect.as(runId));
55715563
};
5572-
let ordinal = 0;
5573-
const applyItem = (threadId: ThreadId, runId: RunId | null) => {
5574-
ordinal += 1;
5564+
let items = 0;
5565+
let writes = 0;
5566+
// Passing `rewrite` writes that earlier item again, possibly under another run.
5567+
const applyItem = (threadId: ThreadId, runId: RunId | null, rewrite?: number) => {
5568+
const ordinal = rewrite ?? (items += 1);
5569+
writes += 1;
55755570
return projectionStore.apply({
5576-
id: EventId.make(`event:shell-item-counts:item:${ordinal}`),
5571+
id: EventId.make(`event:shell-item-counts:write:${writes}`),
55775572
type: "turn-item.updated",
55785573
threadId,
55795574
...(runId === null ? {} : { runId }),
@@ -5626,6 +5621,28 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
56265621
);
56275622
assert.equal(shell?.itemCount, itemCount);
56285623
}
5624+
5625+
// Writing an item again keeps its count; moving it to another run moves the count.
5626+
const itemCountOf = (threadId: ThreadId) =>
5627+
projectionStore.getThreadShell(threadId).pipe(Effect.map((shell) => shell?.itemCount));
5628+
yield* applyItem(busyThreadId, completedRunId, 3);
5629+
assert.equal(yield* itemCountOf(busyThreadId), 4);
5630+
yield* applyItem(busyThreadId, rolledBackRunId, 3);
5631+
assert.equal(yield* itemCountOf(busyThreadId), 3);
5632+
yield* applyItem(busyThreadId, null, 3);
5633+
assert.equal(yield* itemCountOf(busyThreadId), 4);
5634+
5635+
// A run's rollback uncounts its items, and writing the run again counts them back.
5636+
yield* applyRun(busyThreadId, "busy-completed", 1, true);
5637+
assert.equal(yield* itemCountOf(busyThreadId), 3);
5638+
yield* applyRun(busyThreadId, "busy-completed", 1, false);
5639+
assert.equal(yield* itemCountOf(busyThreadId), 4);
5640+
// Items written before their run exist count once it does.
5641+
const lateRunId = RunId.make("run:shell-item-counts:late");
5642+
yield* applyItem(otherThreadId, lateRunId);
5643+
assert.equal(yield* itemCountOf(otherThreadId), 1);
5644+
yield* applyRun(otherThreadId, "late", 2, false);
5645+
assert.equal(yield* itemCountOf(otherThreadId), 2);
56295646
}),
56305647
);
56315648

0 commit comments

Comments
 (0)