rungate 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -217,7 +217,33 @@ export interface ClaimSite {
217
217
  form: ClaimForm | null;
218
218
  }
219
219
 
220
+ /**
221
+ * A source with no id in it cannot produce a site, so it is not scanned (#1223).
222
+ *
223
+ * Every `add()` below sits inside the `matchAll(scIdMentionPattern())` loop,
224
+ * so a source the pattern does not match returns `[]` — this is an EXACT
225
+ * early return, not a heuristic, and it uses the same pattern the loop does
226
+ * so the two cannot disagree about what an id is.
227
+ *
228
+ * The three full-source scans above the loop are the whole cost of this
229
+ * function and they are paid before anything looks for an id. MEASURED over
230
+ * `test/` on 2026-10-22: 567 files / 9.27 MiB, scan 698ms CPU, of which
231
+ * `stripJsComments` 193ms, `stripJsComments({blankLiterals})` 184ms and
232
+ * `commentMask` 352ms. **Half the tree has no SC id at all** — 286 files of
233
+ * 567 carry one — so the probe costs 1ms over the whole tree and removes
234
+ * 333ms: **48% cheaper per scan, with identical output.**
235
+ *
236
+ * `test/sc-scan-cost.test.ts` holds it to the identical-output half over
237
+ * every file in the tree, against a reference that does not short-circuit.
238
+ * Making the probe narrower than the loop's pattern turns that red.
239
+ *
240
+ * Deliberately NOT done here, and measured rather than argued: making
241
+ * `commentMask` lazy — computing it only when an id turns out to be in a
242
+ * comment. 254 of those 286 files have one, so it would fire 11% of the time
243
+ * and bought 405ms → 373ms (8%). Not worth a second code path.
244
+ */
220
245
  export function coverageClaimSites(source: string): ClaimSite[] {
246
+ if (!scIdMentionPattern().test(source)) return [];
221
247
  const noComments = stripJsComments(source);
222
248
  const codeOnly = stripJsComments(source, { blankLiterals: true });
223
249
  const mask = commentMask(source);
@@ -331,9 +357,19 @@ export function findTestFilesForSCs(
331
357
  const dir = testDir || TEST_DIR;
332
358
  const out = new Map<string, ScCredit[]>();
333
359
  if (!existsSync(dir)) return out;
334
- // Tree-wide, and taken before any file is read: whether a criterion has a
335
- // running test anywhere is not a question a single file can answer (#810).
336
- const intended = new Set(intendedOnly(dir).keys());
360
+ // ONE parse of the tree, two readings of it (#1223). `intendedOnly(dir)`
361
+ // walked and parsed every file, and the credit loop below then walked and
362
+ // parsed every file again — the same deterministic scan, twice per call,
363
+ // and this function is called 17 times against the real tree across the
364
+ // suite. The scan is 97% parse and 3% read (23ms of 753ms over 567 files),
365
+ // so what has to be shared is the SITES; re-reading a source below for
366
+ // `isQualifiedBy` costs nothing.
367
+ //
368
+ // Tree-wide, and settled before any credit is awarded: whether a criterion
369
+ // has a running test anywhere is not a question a single file can answer
370
+ // (#810).
371
+ const scan = scanTestTree(dir);
372
+ const intended = new Set(intendedFrom(scan).keys());
337
373
 
338
374
  const wanted = new Map<string, string[]>();
339
375
  for (const sc of scs) {
@@ -342,10 +378,11 @@ export function findTestFilesForSCs(
342
378
  wanted.set(sc.id, specs);
343
379
  }
344
380
 
345
- for (const file of walkTestFiles(dir)) {
381
+ for (const { file, sites } of scan) {
382
+ if (sites.length === 0) continue;
346
383
  const source = readFileSync(join(dir, file), "utf-8");
347
384
  const credits: ScCredit[] = [];
348
- for (const { id, at, kind, form } of coverageClaimSites(source)) {
385
+ for (const { id, at, kind, form } of sites) {
349
386
  // #360. A comment earns a credit only in a structural declaring form.
350
387
  // The position was never the question — a comment is equally where this
351
388
  // project declares coverage and where it explains itself, and a bare id
@@ -417,12 +454,49 @@ export function findTestFilesForSCs(
417
454
  * `measured` is what gives that case something to see.
418
455
  */
419
456
  export function intendedOnly(testDir: string): Map<string, string[]> {
457
+ if (!existsSync(testDir)) return new Map();
458
+ return intendedFrom(scanTestTree(testDir));
459
+ }
460
+
461
+ /** One file of the test tree, with its claim sites already parsed. */
462
+ export interface ScannedTestFile {
463
+ /** Path relative to the scanned directory, as `walkTestFiles` reports it. */
464
+ file: string;
465
+ sites: ClaimSite[];
466
+ }
467
+
468
+ /**
469
+ * The test tree, walked once and parsed once (#1223).
470
+ *
471
+ * The one place a tree-wide claim scan happens, so a caller that wants two
472
+ * readings of it pays for one. Both readings this file needs — the intended
473
+ * set and the credit map — were separately re-deriving it, which doubled the
474
+ * cost of every `findTestFilesForSCs` call. See that function.
475
+ *
476
+ * Not memoised, deliberately. A cache would have to decide when the tree has
477
+ * changed, and the callers that pass a scratch directory write into it
478
+ * between calls — that is the fail-open shape in
479
+ * `.claude/rules/checks-must-be-able-to-fail.md`, where a stale reading and a
480
+ * correct one are the same green. Sharing a reading is the CALLER's decision,
481
+ * because only the caller knows its inputs have not moved;
482
+ * `test/sc-id-namespace.test.ts` does exactly that for the two cases that ask
483
+ * the identical whole-registry question.
484
+ */
485
+ export function scanTestTree(testDir: string): ScannedTestFile[] {
486
+ if (!existsSync(testDir)) return [];
487
+ return walkTestFiles(testDir).map(file => ({
488
+ file,
489
+ sites: coverageClaimSites(readFileSync(join(testDir, file), "utf-8")),
490
+ }));
491
+ }
492
+
493
+ /** `intendedOnly`'s reading, over a scan somebody else already paid for. */
494
+ export function intendedFrom(scan: readonly ScannedTestFile[]): Map<string, string[]> {
420
495
  const out = new Map<string, string[]>();
421
- if (!existsSync(testDir)) return out;
422
496
  const measured = new Set<string>();
423
497
  const intended = new Map<string, string[]>();
424
- for (const file of walkTestFiles(testDir)) {
425
- for (const site of coverageClaimSites(readFileSync(join(testDir, file), "utf-8"))) {
498
+ for (const { file, sites } of scan) {
499
+ for (const site of sites) {
426
500
  if (site.kind === "unrun-title") {
427
501
  const files = intended.get(site.id) ?? [];
428
502
  if (!files.includes(file)) files.push(file);