Skip to content

Commit f7ef465

Browse files
committed
Merge remote-tracking branch 'origin/main' into claude/project-thread-i6sx7k
2 parents 9a6804d + 63a9bcb commit f7ef465

2 files changed

Lines changed: 55 additions & 12 deletions

File tree

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
// test-budget: 40 — spawns the real producer once per single-cause fixture: three in-process kernel boots on a real SQL driver, the first seeding 2,600 comments on 260 docs through the real writers to sit past the 2,000-row window the comment gate replaced; slowest CI reading 28.07 s (run 40204927516, refresh PR #25960's window (hourly runs 40200279306 to 40272142430)) x 1.25, rounded up; read before this file seeded its comments ten to a doc and paid its first boot at module scope
23

34
/**
45
* [ADR-0139 E7] The comment-thread read gate on the LEAF path, over
@@ -55,9 +56,19 @@ const DELEGATED_A = { ...MEMBER_A, onBehalfOf: { userId: 'u_delegator' } };
5556
/** The delegator's own leg on `e7_doc` admits every record but these (by name). */
5657
const WITHHELD_FROM_DELEGATOR = ['a0', 'a1', 'a2'];
5758

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

6374
const READ_OPS = new Set(['find', 'findOne', 'count', 'aggregate']);
@@ -145,6 +156,12 @@ const commentOn = (object: string, id: string, body: string): Row => ({
145156
author_id: 'u_fixture',
146157
});
147158

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

214232
ids.gone = String((await engine.insert(DOC, { name: 'gone', owner: MEMBER_A.userId }, { context: SYS })).id);
215233
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: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
// test-budget: 50 — spawns the real producer once per single-cause fixture: two in-process kernel boots on a real SQL driver, the first seeding 2,600 records through the real CRUD mirror (each pays its activity row and its ledger row) to sit past the 2,000-row window the leaf gate replaced; slowest CI reading 35.78 s (run 40204927516, refresh PR #25960's window (hourly runs 40200279306 to 40272142430)) x 1.25, rounded up; read before this file reused its at-rest reads and paid its first boot at module scope
23

34
/**
45
* [ADR-0139 E5] The parent-record read gates on the LEAF path, through a real
@@ -180,6 +181,12 @@ async function insertInBatches(engine: ObjectQL, object: string, rows: Row[]): P
180181
}
181182
}
182183

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

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

300325
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 });
326+
const activity = await atRestOf(ACTIVITY);
302327
expect(activity.length).toBeGreaterThan(PARENT_GATE_SCAN_LIMIT);
303328
expect(activity.filter((r) => r.object_name === DOC && r.type === 'created').length).toBe(DOCS_OF_A + DOCS_OF_B + 1);
304329
expect(activity.some((r) => r.object_name === DOC && r.record_id === ids.gone && r.type === 'deleted')).toBe(true);
305330
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 });
331+
const ledger = await atRestOf(LEDGER);
307332
expect(ledger.filter((r) => r.action === 'config_change').length).toBe(ROWS_ABOUT_NO_RECORD);
308333
});
309334

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

330355
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 });
356+
const all = await atRestOf(ACTIVITY);
332357
const byId = new Map(all.map((r) => [String(r.id), r]));
333358
for (const id of await served(ACTIVITY, MEMBER_A)) {
334359
const row = byId.get(id)!;
@@ -379,7 +404,7 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
379404
const named = { object_name: { $in: NAMED } };
380405
// The same rows, in a spelling the gate does not read as naming its parents: it keeps every branch.
381406
const spelledOut = { $or: NAMED.map((object_name) => ({ object_name })) };
382-
const atRest: Row[] = await engine.find(ACTIVITY, { limit: 100_000, context: SYS });
407+
const atRest = await atRestOf(ACTIVITY);
383408
const parentOf = new Map(atRest.map((r) => [String(r.id), r.object_name]));
384409
const gateConjunct = (where: any) => where.$and[1];
385410
seenWhere[ACTIVITY].length = 0;
@@ -431,7 +456,7 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
431456
// per parent object through the engine middleware chain, with the
432457
// delegated context. Asked here for every parent pair at rest, so no
433458
// 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 });
459+
const atRest = await atRestOf(ACTIVITY);
435460
const pairs = new Map<string, Set<string>>();
436461
for (const row of atRest) {
437462
const parent = parseParentRecord(row, ACTIVITY);
@@ -488,7 +513,7 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
488513
return Math.round((performance.now() - t0) * 10) / 10;
489514
};
490515
const reading = {
491-
rowsAtRest: (await engine.find(ACTIVITY, { limit: 100_000, context: SYS })).length,
516+
rowsAtRest: (await atRestOf(ACTIVITY)).length,
492517
leafMs: {
493518
memberFind: await time(() => served(ACTIVITY, MEMBER_A)),
494519
memberCount: await time(() => engine.count(ACTIVITY, {}, { context: MEMBER_A })),

0 commit comments

Comments
 (0)