fix: CDP extraction concurrency + topology ID remapping + predicate completeness

CDP protocol fixes:
- session.ts: getDocumentRootNodeId() now pushes {depth:-1} full DOM tree
  (was shallow default, causing querySelectorAll to find zero nodes)
- extraction.ts: serialize resolveSelector calls (was concurrent Promise.all,
  CDP DOM.querySelectorAll is not safe for concurrent calls on same session)

Topology ID mismatch fixes (critical — backendNodeId vs solver subject ID):
- extraction.ts: add remapTopologyIds() — builds Map<backendNodeId,solverId>
  from world.subjects.domNodeId/ids, remaps all 6 topology arrays +
  clipping.subjectId + scroll.containerId
- topology.ts: createsStackingContext() now checks contain:paint|layout
  (both create stacking contexts per CSS spec, was missing)
- topology.ts: evaluateInStackingContext() handles unary (no reference)

Predicate completeness:
- predicates.ts: 3 new evaluators — intersects, touches, hasGap (indices 31-33)
- predicates.ts: escapeClippingChainOf replaced indeterminate stub with
  deterministic 2-ary evaluator (overflow + clip chain + ancestry checks)
- predicates.ts: attachedToScrollContainer arity 1→2, checks reference match
- predicates.ts: 7 aliases + 4 alignment aliases promoted to BUILTIN_PREDICATES

DSL grammar fixes:
- grammar.ts: isKeywordThatCanBePredicate() extended with all missing keywords
- lower-to-canonical.ts: add TopologyAssertion lowering (was silently dropped)
- compiler.ts: add intersects/touches/hasGap relation codes
- validator.ts: add option sets for new predicates
- lexer.ts: add intersects/touches/hasGap token kinds

Safety + infrastructure:
- joins.ts: MAX_CARTESIAN_TUPLES=100,000 guard with descriptive error
- extraction.ts: computeRequiredFacts() registers defaults + broadens topology gate
- package.json: add missing imhotep-state dependency to imhotep-playwright

Tests:
- 9 new predicate tests (intersects, touches, hasGap, escapeClippingChainOf)
- Convert stale separatedFrom/contains GAP tests to working assertions
- Add computeRequiredFacts topology gating tests (public.test.ts)
This commit is contained in:
John Dvorak
2026-05-22 10:20:06 -07:00
parent 654becabc6
commit 1bc92e1f7d
15 changed files with 458 additions and 47 deletions
+9
View File
@@ -33,7 +33,10 @@ const SPATIAL_RELATIONS = new Set([
'inside',
'contains',
'overlaps',
'intersects',
'touches',
'separatedFrom',
'hasGap',
// Spatial aliases
'beside',
'nextTo',
@@ -73,7 +76,10 @@ const VALID_OPTIONS: Record<string, Set<string>> = {
inside: new Set(['tolerance', 'minGap', 'space']),
contains: new Set(['tolerance', 'space']),
overlaps: new Set(['tolerance', 'space']),
intersects: new Set(['tolerance', 'space']),
touches: new Set(['tolerance', 'inStackingContext', 'space']),
separatedFrom: new Set(['minGap', 'maxGap', 'tolerance', 'space']),
hasGap: new Set(['minGap', 'maxGap', 'tolerance', 'space']),
// Spatial alias options
beside: new Set(['minGap', 'maxGap', 'tolerance', 'inStackingContext', 'space']),
nextTo: new Set(['minGap', 'maxGap', 'tolerance', 'inStackingContext', 'space']),
@@ -101,7 +107,10 @@ const QUANTIFIER_COMPATIBLE = new Set([
'inside',
'contains',
'overlaps',
'intersects',
'touches',
'separatedFrom',
'hasGap',
'beside',
'nextTo',
'adjacent',