Skip to content

Commit f267df4

Browse files
committed
test(plugin-audit): seed the comment gate's window with fewer docs, reuse the leaf gate's at-rest reads
Slice 20 of the server-lane test budget. comment-parent-pair writes its 2,600 comments ten to a doc (the old probe's window was over comment rows), so 260 docs pay the CRUD mirror instead of 2,600. parent-record-read-leaf reads each object at rest once and reuses it, re-reading on a moved count. Both pay their first kernel boot at module scope. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibz25B8NfvsjxU1ttwC
1 parent 3ba5206 commit f267df4

2 files changed

Lines changed: 53 additions & 12 deletions

File tree

‎packages/plugins/plugin-audit/src/comment-parent-pair.integration.test.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,19 @@ const DELEGATED_A = { ...MEMBER_A, onBehalfOf: { userId: 'u_delegator' } };
5555
/** The delegator's own leg on `e7_doc` admits every record but these (by name). */
5656
const WITHHELD_FROM_DELEGATOR = ['a0', 'a1', 'a2'];
5757

58-
/** Past the 2,000-row candidate window the leaf replaces. */
59-
const DOCS_OF_A = 2_300;
60-
const DOCS_OF_B = 300;
58+
/**
59+
* Past the 2,000-row candidate window the leaf replaces: 2,300 comments member
60+
* A may read, 300 member B may. The window was over COMMENT rows (the thread
61+
* probe's candidate pre-scan reads `sys_comment`, `READ_SCAN_LIMIT` in
62+
* `comment-access-hooks.ts`), never over parent records, so the comments sit
63+
* ten to a doc. [#23181] One comment per doc wrote 2,600 docs, and each doc
64+
* write pays the CRUD mirror's ledger row and activity row, which nothing in
65+
* this file reads: about 6 s of the file's clock, against well under 1 s for
66+
* the comments themselves.
67+
*/
68+
const COMMENTS_PER_DOC = 10;
69+
const DOCS_OF_A = 230;
70+
const DOCS_OF_B = 30;
6171
const BATCH = 200;
6272

6373
const READ_OPS = new Set(['find', 'findOne', 'count', 'aggregate']);
@@ -145,6 +155,12 @@ const commentOn = (object: string, id: string, body: string): Row => ({
145155
author_id: 'u_fixture',
146156
});
147157

158+
// [#23181] Pay the first kernel boot at module scope, never in a hook: vitest
159+
// clocks a hook as it clocks a test, and the first boot is the slow one. The
160+
// throwaway kernel registers no fixture object, writes nothing and is shut down
161+
// before any hook runs.
162+
await (await bootKernel(new SqliteWasmDriver({ filename: ':memory:' }))).kernel.shutdown();
163+
148164
describe('[ADR-0139 E7] the comment-thread read gate over the stamped parent pair', () => {
149165
let kernel: ObjectKernel;
150166
let engine: ObjectQL;
@@ -209,7 +225,8 @@ describe('[ADR-0139 E7] the comment-thread read gate over the stamped parent pai
209225
for (let i = 0; i < DOCS_OF_A; i++) docs.push({ name: `a${i}`, owner: MEMBER_A.userId });
210226
for (let i = 0; i < DOCS_OF_B; i++) docs.push({ name: `b${i}`, owner: MEMBER_B.userId });
211227
const docIds = await insertInBatches(engine, DOC, docs);
212-
await insertInBatches(engine, COMMENT, docIds.map((id, i) => commentOn(DOC, id, `c${i}`)));
228+
await insertInBatches(engine, COMMENT, docIds.flatMap((id, i) =>
229+
Array.from({ length: COMMENTS_PER_DOC }, (_, k) => commentOn(DOC, id, `c${i * COMMENTS_PER_DOC + k}`))));
213230

214231
ids.gone = String((await engine.insert(DOC, { name: 'gone', owner: MEMBER_A.userId }, { context: SYS })).id);
215232
ids.vault = String((await engine.insert(VAULT, { name: 'vault' }, { context: SYS })).id);

‎packages/plugins/plugin-audit/src/parent-record-read-leaf.integration.test.ts‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ async function insertInBatches(engine: ObjectQL, object: string, rows: Row[]): P
180180
}
181181
}
182182

183+
// [#23181] Pay the first kernel boot at module scope, never in a hook: vitest
184+
// clocks a hook as it clocks a test, and the first boot is the slow one. The
185+
// throwaway kernel registers no fixture object, writes nothing and is shut down
186+
// before any hook runs.
187+
await (await bootKernel(new SqliteWasmDriver({ filename: ':memory:' }))).kernel.shutdown();
188+
183189
describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
184190
let kernel: ObjectKernel;
185191
let engine: ObjectQL;
@@ -201,12 +207,30 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
201207
const ledgerOrganizationBound: boolean[] = [];
202208
const ids: Record<string, string> = {};
203209

210+
/**
211+
* [#23181] Every row of an object at rest, as the system reads it — read once
212+
* and reused. No case in this describe writes after `beforeAll`, so each such
213+
* read answered the same rows: the cases made about sixty of them, over tables
214+
* of 2,600 to 7,000 rows. Each reuse counts the object first and re-reads on a
215+
* moved count, so a case that adds or removes rows is never judged against an
216+
* older ground truth.
217+
*/
218+
const atRestCache = new Map<string, { count: number; rows: Row[] }>();
219+
const atRestOf = async (object: string): Promise<Row[]> => {
220+
const count = await engine.count(object, {}, { context: SYS });
221+
const hit = atRestCache.get(object);
222+
if (hit && hit.count === count) return hit.rows;
223+
const rows: Row[] = await engine.find(object, { limit: 100_000, context: SYS });
224+
atRestCache.set(object, { count, rows });
225+
return rows;
226+
};
227+
204228
/** Every row at rest, and the ground truth of who may read which, judged by the stand-in's own rules. */
205229
const truth = async (object: string, reader: any): Promise<string[]> => {
206-
const atRest: Row[] = await engine.find(object, { limit: 100_000, context: SYS });
230+
const atRest = await atRestOf(object);
207231
const live = new Map<string, Map<string, Row>>();
208232
for (const parent of [DOC, VAULT, GATED, FAR]) {
209-
const records: Row[] = await engine.find(parent, { limit: 100_000, context: SYS });
233+
const records = await atRestOf(parent);
210234
live.set(parent, new Map(records.map((r) => [String(r.id), r])));
211235
}
212236
const holder = Array.isArray(reader?.systemPermissions) && reader.systemPermissions.includes(LEDGER_AUDIT_CAPABILITY);
@@ -298,12 +322,12 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
298322
}, 30_000);
299323

300324
it('control: the fixture is past the window, and every row came from its producer', async () => {
301-
const activity: Row[] = await engine.find(ACTIVITY, { limit: 100_000, context: SYS });
325+
const activity = await atRestOf(ACTIVITY);
302326
expect(activity.length).toBeGreaterThan(PARENT_GATE_SCAN_LIMIT);
303327
expect(activity.filter((r) => r.object_name === DOC && r.type === 'created').length).toBe(DOCS_OF_A + DOCS_OF_B + 1);
304328
expect(activity.some((r) => r.object_name === DOC && r.record_id === ids.gone && r.type === 'deleted')).toBe(true);
305329
expect((await truth(ACTIVITY, MEMBER_A)).length).toBeGreaterThan(PARENT_GATE_SCAN_LIMIT);
306-
const ledger: Row[] = await engine.find(LEDGER, { limit: 100_000, context: SYS });
330+
const ledger = await atRestOf(LEDGER);
307331
expect(ledger.filter((r) => r.action === 'config_change').length).toBe(ROWS_ABOUT_NO_RECORD);
308332
});
309333

@@ -328,7 +352,7 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
328352
});
329353

330354
it('no row is served whose parent the reader may not read, or that no longer exists', async () => {
331-
const all: Row[] = await engine.find(ACTIVITY, { limit: 100_000, context: SYS });
355+
const all = await atRestOf(ACTIVITY);
332356
const byId = new Map(all.map((r) => [String(r.id), r]));
333357
for (const id of await served(ACTIVITY, MEMBER_A)) {
334358
const row = byId.get(id)!;
@@ -379,7 +403,7 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
379403
const named = { object_name: { $in: NAMED } };
380404
// The same rows, in a spelling the gate does not read as naming its parents: it keeps every branch.
381405
const spelledOut = { $or: NAMED.map((object_name) => ({ object_name })) };
382-
const atRest: Row[] = await engine.find(ACTIVITY, { limit: 100_000, context: SYS });
406+
const atRest = await atRestOf(ACTIVITY);
383407
const parentOf = new Map(atRest.map((r) => [String(r.id), r.object_name]));
384408
const gateConjunct = (where: any) => where.$and[1];
385409
seenWhere[ACTIVITY].length = 0;
@@ -431,7 +455,7 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
431455
// per parent object through the engine middleware chain, with the
432456
// delegated context. Asked here for every parent pair at rest, so no
433457
// window cuts it; the leaf must serve exactly the rows it admits.
434-
const atRest: Row[] = await engine.find(ACTIVITY, { limit: 100_000, context: SYS });
458+
const atRest = await atRestOf(ACTIVITY);
435459
const pairs = new Map<string, Set<string>>();
436460
for (const row of atRest) {
437461
const parent = parseParentRecord(row, ACTIVITY);
@@ -488,7 +512,7 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
488512
return Math.round((performance.now() - t0) * 10) / 10;
489513
};
490514
const reading = {
491-
rowsAtRest: (await engine.find(ACTIVITY, { limit: 100_000, context: SYS })).length,
515+
rowsAtRest: (await atRestOf(ACTIVITY)).length,
492516
leafMs: {
493517
memberFind: await time(() => served(ACTIVITY, MEMBER_A)),
494518
memberCount: await time(() => engine.count(ACTIVITY, {}, { context: MEMBER_A })),

0 commit comments

Comments
 (0)