fix(resolution): gate closure-collection synthesis to Swift/Kotlin and de-quadratic its line accounting (#1235) (#1237)

closureCollectionEdges scanned every method/function node in every
language, but its dispatcher patterns ({ $0( / { it( ) are Swift/Kotlin
trailing-closure syntax — on PHP/JS repos the pass can never emit an
edge, yet .push(/.add( fired its append gate on nearly every function.
Per match it computed the line via src.slice(0, idx).split('\n'), which
is O(source) per match and goes quadratic on match-dense generated
functions (two-byte content roughly doubles it). On a 12,860-file
PHP/JS app that was 20+ minutes of the "Resolving refs" tail — frozen
at 97% — and a #850 watchdog kill; profiled on CRMEB it was 127s of a
166s index for zero edges.

- Skip nodes whose language isn't swift/kotlin before any file I/O.
- makeLineAt(): lazy newline index + binary search, shared with the
  emitter passes' per-file lineOf.
- Yield every 256 regex matches inside the scan's match loops so a
  single pathological function can't starve the watchdog.

CRMEB (ThinkPHP, 2,913 files): 166.8s -> 72.7s, graph byte-identical.
Alamofire: closure-collection edges byte-identical (9 edges, 4 fields).
Drupal core control: graph byte-identical.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-07-09 20:54:45 -05:00
committed by GitHub
co-authored by Claude Fable 5
parent e552dc2d2f
commit e76a355df5
3 changed files with 92 additions and 10 deletions
@@ -106,7 +106,9 @@ describe('closure-collection synthesizer', () => {
);
expect(validatorsEdge).toBeTruthy();
expect(validatorsEdge.source_name).toBe('didCompleteTask');
expect(validatorsEdge.registeredAt).toMatch(/DataRequest\.swift:\d+/);
// Exact wiring-site line, not just shape: pins the binary-search line
// resolver (#1235) to the same answer as the old per-match slice+split.
expect(validatorsEdge.registeredAt).toBe('DataRequest.swift:4');
// The handlers flow: runHandlers → onEvent, via the direct `prop.append`
// form — proves both registrar shapes are covered.
@@ -121,4 +123,37 @@ describe('closure-collection synthesizer', () => {
expect(rows.some((r: any) => r.field === 'names')).toBe(false);
expect(rows.some((r: any) => r.target_name === 'addName')).toBe(false);
});
it('is a no-op on non-Swift/Kotlin code even when the text patterns occur (#1235)', async () => {
// JS that contains every textual trigger the scanner looks for —
// `.forEach`, `.push(`, even a `{ $0(` lookalike inside a string — but
// `{ $0( ` / `{ it( ` element-invocation is Swift/Kotlin trailing-closure
// syntax, so the pass must skip the language entirely (this scan running
// ungated over `.push(`-heavy JS/PHP was the #1235 25-minute index).
fs.writeFileSync(
path.join(dir, 'queue.js'),
`class Queue {
constructor() { this.tasks = []; }
register(fn) { this.tasks.push(fn); }
drain() { this.tasks.forEach(function (t) { t(); }); }
weird() { const s = "tasks.forEach { $0( } tasks.push("; return s; }
}
module.exports = Queue;
`
);
const cg = await CodeGraph.init(dir, { silent: true });
await cg.indexAll();
const db = (cg as any).db.db;
const count = db
.prepare(
`SELECT count(*) c FROM edges
WHERE json_extract(metadata,'$.synthesizedBy') = 'closure-collection'`
)
.get();
cg.close?.();
expect(count.c).toBe(0);
});
});