Skip to content

Commit 6927b02

Browse files
test(cli): boot-preparation parity reads each module's masked source once (#26224)
<!-- claude-projects-attribution: {"github_login":"os-project-manager"} --> _Requested by **Leehom** · [project thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1X4H4YUDsH1nSMT4SyCqVDtv)_ Fixes #26147 Clause-②: no (test-only) Metadata-protocol: none ## What changed One file, test-only: `packages/cli/src/utils/boot-preparation-parity.test.ts`. The parity pin's readers (`bindings`, `uses`, `declares`) re-read and re-masked the module they were asked about on every call. `serve.ts` is 7,197 lines, and on this box `maskComments` takes about 55 ms on it and `maskCommentsAndLiterals` about 29 ms. The SERVE_ONLY case calls `uses(serve.ts, …)` once per binding `serve.ts` imports, so it spent its whole budget re-masking the same file. A small `perModule` memo now computes each module's source, its comment mask, its code-only text (comments and literals masked, import statements dropped) and its binding list on the first ask, and every later ask shares it. This is the triage's route (6417925610): the scan is computed once per file and shared across entries. No timeout was raised, nothing moved project, nothing was deleted. The predicates are the same expressions over the same text. `uses()` still removes the asked name's own `function NAME` declaration from the code-only text, then tests the same word-boundary pattern. `declares()` still tests the same export pattern on the comment-masked text. `bindings()` runs the same two `matchAll` passes. Its result is now typed `readonly Binding[]`, because the array is shared. Every caller only filters or iterates it. ## Measured Same box (4 cores, shared, under `os-verify-lock`). BASE is `c7aa9cde`'s file, copied next to HEAD's as `zzbase.boot-preparation-parity.test.ts` so both run under the same conditions. The copy was deleted before commit. Durations come from the json reporter's per-case `duration`. Both legs ran with `--testTimeout=120000` so the BASE case's real cost is read rather than cut off at 5 s; the committed file declares no timeout. **Alone** (`vitest run --project unit --maxWorkers=2 FILE`, interleaved BASE/HEAD, 3 runs): | run | BASE: SERVE_ONLY case | HEAD: SERVE_ONLY case | BASE: closure case | HEAD: closure case | BASE: sum of 37 cases | HEAD: sum of 37 cases | |---|---|---|---|---|---|---| | 1 | 3140 ms | 19 ms | 850 ms | 193 ms | 5790 ms | 634 ms | | 2 | 3481 ms | 21 ms | 1043 ms | 205 ms | 6270 ms | 650 ms | | 3 | 3172 ms | 30 ms | 1021 ms | 238 ms | 6001 ms | 717 ms | **Under load**: the whole `@objectstack/cli` unit tier at `--maxWorkers=4` (the core count), with both copies in the same run, 3 runs, about 410 s each: | run | BASE: SERVE_ONLY case | HEAD: SERVE_ONLY case | BASE: closure case | HEAD: closure case | BASE: sum of 37 cases | HEAD: sum of 37 cases | |---|---|---|---|---|---|---| | 1 | 4332 ms | 20 ms | 1264 ms | 202 ms | 7899 ms | 746 ms | | 2 | 5217 ms | 20 ms | 1106 ms | 257 ms | 8628 ms | 946 ms | | 3 | 5435 ms | 40 ms | 960 ms | 205 ms | 8361 ms | 855 ms | Under load, BASE runs 2 and 3 are over vitest's 5000 ms default, so at the default timeout they would have failed. HEAD's case takes 20 to 40 ms. In each run, 37/37 cases passed in both copies. The sorted full names and statuses of the 37 cases are identical between BASE and HEAD. (The load runs exited 1 only because `test/published-subpath-console.pin.test.ts` and `published-subpath-hook-body.pin.test.ts` refused to start: `packages/cli/dist` was not built yet at that point. No test failed. The cli unit tier below ran after `pnpm --filter @objectstack/cli build`.) ## The assertions still prove what they proved Ablation at HEAD `95e00887`, through `scripts/ablation-replace.mjs` (wrap mode, under the lock). In `packages/cli/src/commands/serve.ts`, the one use of the SERVE_ONLY entry `isHostConfig`, `&& isHostConfig(config)`, was replaced by `&& Boolean(config)`. That leaves the import in place but nothing using it. The anchor hit 1 time and then 0, and the blob went `80458419a165` to `031d0e05cbe4`. With both copies run: - HEAD: `× every SERVE_ONLY entry is still used by serve.ts, and is not also a step 40ms`, `AssertionError: isHostConfig: serve.ts no longer uses it — drop the entry` - BASE: the same case and the same message, in 3446 ms - `Test Files 2 failed (2)`, `Tests 2 failed | 72 passed (74)`; restore: blob after restore `80458419a1651ceb0f0835034f17a5c9a14a3b50` equals the blob at HEAD, and `git diff HEAD` is empty The test reads `serve.ts` from `src/` with `readFileSync`, so no build sits between the mutation and the reading. ## Verification (head `95e00887`) - `pnpm --filter @objectstack/cli build` exit 0, `pnpm --filter @objectstack/cli typecheck` exit 0 - `pnpm --filter @objectstack/cli exec vitest run --project unit`: `Test Files 333 passed (333)`, `Tests 4854 passed (4854)`, exit 0. The integration tier is left to CI, because the diff touches no integration-tier file and no spawn entry. - `eslint --no-inline-config packages/cli` exit 0 - `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` derived 58 commands, and every one exits 0. Three first exited 3 (PREREQUISITE NOT MET) and were re-run green after their prerequisite was met: `check-plugin-teardown-shape --self-test` after fetching its positive-control commit `621a4876`, and `check:dual-build-cjs-loads` and `check:i18n-coverage` after building 11 packages that had no `dist`. `--ran`: `58 derived, 58 run, 0 NOT-MEASURED, 0 UNRUN`, exit 0. - `skip-changeset`: the cli package ships only `files: [dist, README.md, CHANGELOG.md]`. After the build, `perModule` matches 0 files in `packages/cli/dist` and no file named for the test is there; the control `shouldBootWithLibrary` matches 4. ## Acceptance notes - The `uses()` scan is still one regex replace and one regex test per asked name over the cached text, so it costs about 1 ms per call. The whole file now takes about 0.6 to 0.9 s of case time, alone or under load. --- _Generated by [Claude Code](https://claude.ai/code/session_01NqJv2ismsLLfpGXWijRUPq)_ Co-authored-by: Claude <noreply@anthropic.com>
1 parent ea89df3 commit 6927b02

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

‎packages/cli/src/utils/boot-preparation-parity.test.ts‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,24 @@ interface Binding {
190190
from: string;
191191
}
192192

193-
const source = (rel: string): string => readFileSync(resolve(SRC, rel), 'utf8');
193+
/**
194+
* One answer per module, computed on its first ask and shared by every later
195+
* one. The readers below run once per binding and once per row, and `serve.ts`
196+
* is over 7,000 lines: masking it afresh on each ask cost the SERVE_ONLY case
197+
* about 4.7 s against vitest's 5 s default ([#26147]).
198+
*/
199+
function perModule<T>(read: (rel: string) => T): (rel: string) => T {
200+
const memo = new Map<string, T>();
201+
return (rel) => {
202+
if (!memo.has(rel)) memo.set(rel, read(rel));
203+
return memo.get(rel) as T;
204+
};
205+
}
206+
207+
const source = perModule((rel) => readFileSync(resolve(SRC, rel), 'utf8'));
208+
209+
/** `rel` with its comments masked: what `bindings()` and `declares()` read. */
210+
const commentMasked = perModule((rel) => maskComments(source(rel)));
194211

195212
/** Resolve an import specifier written in `rel` to what the test loads. */
196213
function resolveSpecifier(rel: string, spec: string): string {
@@ -202,8 +219,8 @@ function resolveSpecifier(rel: string, spec: string): string {
202219
const relToSrc = (from: string): string => (from.startsWith(SRC) ? from.slice(SRC.length + 1) : from);
203220

204221
/** Every value binding `rel` imports — `import { … } from` and `const { … } = await import(…)`. */
205-
function bindings(rel: string): Binding[] {
206-
const code = maskComments(source(rel));
222+
const bindings = perModule((rel): readonly Binding[] => {
223+
const code = commentMasked(rel);
207224
const out: Binding[] = [];
208225
const add = (clause: string, spec: string): void => {
209226
for (const raw of clause.split(',')) {
@@ -216,27 +233,28 @@ function bindings(rel: string): Binding[] {
216233
for (const m of code.matchAll(/\bimport\s+(?!type\b)\{([^}]*)\}\s*from\s*['"]([^'"]+)['"]/g)) add(m[1], m[2]);
217234
for (const m of code.matchAll(/\bconst\s*\{([^}]*)\}\s*=\s*await\s+import\(\s*['"]([^'"]+)['"]\s*\)/g)) add(m[1], m[2]);
218235
return out;
219-
}
236+
});
220237

221238
/** The import statements `bindings()` reads — what binds a name, never a use of it. */
222239
const IMPORT_STATEMENT = /\bimport\s+(?!type\b)\{[^}]*\}\s*from\s*['"][^'"]*['"]|\bconst\s*\{[^}]*\}\s*=\s*await\s+import\(\s*['"][^'"]*['"]\s*\)/g;
223240

241+
/** `rel`'s code alone: comments and literals masked, the import statements dropped. */
242+
const codeOnly = perModule((rel) => maskCommentsAndLiterals(source(rel)).replace(IMPORT_STATEMENT, ''));
243+
224244
/**
225245
* Does `rel` USE the binding `name` in code — call it, or hand it on (a
226246
* callback such as `plugins.some(isAppPluginLike)`, a static handle such as
227247
* `Serve.providesCapability`'s initializer)? Comments and literals do not count,
228248
* and neither does the import that binds it or the declaration that defines it.
229249
*/
230250
function uses(rel: string, name: string): boolean {
231-
const code = maskCommentsAndLiterals(source(rel))
232-
.replace(IMPORT_STATEMENT, '')
233-
.replace(new RegExp(`\\bfunction\\s+${name}\\b`, 'g'), '');
251+
const code = codeOnly(rel).replace(new RegExp(`\\bfunction\\s+${name}\\b`, 'g'), '');
234252
return new RegExp(`(?<![\\w$.])${name}(?![\\w$])`).test(code);
235253
}
236254

237255
/** Does `rel` declare and export `name` itself? */
238256
const declares = (rel: string, name: string): boolean =>
239-
new RegExp(`\\bexport\\s+(?:async\\s+)?function\\s+${name}\\b`).test(maskComments(source(rel)));
257+
new RegExp(`\\bexport\\s+(?:async\\s+)?function\\s+${name}\\b`).test(commentMasked(rel));
240258

241259
async function load(from: string): Promise<Record<string, unknown>> {
242260
return (await import(from)) as Record<string, unknown>;

0 commit comments

Comments
 (0)