Commit Graph

54 Commits

Author SHA1 Message Date
John Dvorak 2bdda12030 fix: getTopologyValue off-by-one bug in clause-based topology evaluators
getTopologyValue used subject ID as direct array index (arr[subjectId] ?? 0),
but solver subject IDs are 1-indexed while topology arrays are 0-indexed parallel
arrays.  This produced wrong results for any subjectId != position+1.

Fixed by using world.subjects.ids.indexOf(subjectId) to map subject ID to array
position, matching the predicate evaluator's getTopologyValueBySubject pattern.

Affected callers: evaluateAttachedToScrollContainer, evaluateInStackingContext.
The predicate-based evaluators in predicates.ts were already correct.
2026-05-22 16:40:01 -07:00
John Dvorak 8f823d959b feat: CI verification pipeline and structural conformance gates
- Add scripts/check-structural.js: counts production as any, empty catch,
  and ?? 0 patterns; fails CI if counts exceed committed baseline
- Add scripts/.structural-baseline.json: committed baseline (101 as any,
  17 empty catch, 62 ?? 0); use --fix-baseline to ratchet down
- Add scripts/ci-verify.sh: ordered pipeline (build → typecheck → lint →
  structural → unit tests → E2E) with pass/fail summary
- Add npm scripts: test:structural, test:structural:fix, ci:verify
- Add cache-staleness conformance test: verifies WORLD_CACHE_SCHEMA_VERSION
  is prefixed to all cache keys for auto-invalidation on schema changes
- Cache test suite grows from 141 → 142 passes
2026-05-22 16:33:34 -07:00
John Dvorak 3b7be0aaf0 refactor: missing-fact discipline in proof witness generation
- Remove defensive ?? 0 fallbacks in buildFailedPredicate that could
  silently report false geometry in proof witnesses
- Require all 4 rect fields present before building subjectRect/referenceRect
  (was only checking subjectLeft)
- Add explicit undefined guards for observed gap, measured values,
  delta, and aspect ratio — return undefined (no failedPredicate)
  when critical metrics are missing instead of fabricating 0 defaults
- Generic synthesizeGenericFailedPredicate checks value presence
  instead of defaulting metrics[keys] to 0
- Option defaults (minGap, maxGap, tolerance, min, max bounds)
  retain ?? 0/?? Infinity as correct neutral values for user parameters
2026-05-22 16:17:18 -07:00
John Dvorak ce04b2b3de refactor: surface extraction cleanup failures as visible diagnostics
- Add IMH_EXTRACTION_CLEANUP_FAILED and IMH_EXTRACTION_RESIDUAL_ATTRIBUTES
  diagnostic codes with severity warning and extraction-error category
- Check for residual data-imhotep-runtime-id attributes before extraction
  (both fast-geometry and CDP paths), emit diagnostic if prior run leaked them
- Surface cleanup failures (attribute removal, CDP session detach) as
  returned diagnostics instead of silent console.warn
- Wrap CDP sessionManager.detach() in try/catch to prevent finally-block
  unhandled throws on closed/navigated pages
- Count injected vs cleaned runtime-id attributes; report mismatch as
  IMH_EXTRACTION_CLEANUP_FAILED with metrics
- Move errors array declaration before try block in fast-geometry path
  so finally can append cleanup diagnostics
2026-05-22 16:06:21 -07:00
John Dvorak 066ef9f677 refactor: remove global registry fallbacks — factory pattern for test isolation
predicates.ts: Add populateDefaultPredicates(registry) accepting any
  PredicateRegistry. Add createDefaultPredicateRegistry() factory.
  registerDefaultPredicates() now delegates to populateDefaultPredicates
  on the global (backward compatible).

logic-engine.ts: Replace globalPredicateRegistry fallback with
  createDefaultPredicateRegistry() factory. Each evaluateLogic() call
  creates a fresh self-populated registry unless one is explicitly
  injected. No shared mutable state between evaluations.

engine.ts: Same pattern for clauses — add populateDefaultClauses(registry),
  createDefaultClauseRegistry() factory. registerDefaultClauses() now
  delegates to populateDefaultClauses on the global. evaluate() replaces
  globalClauseRegistry fallback with createDefaultClauseRegistry().

registry.ts: @deprecated tag on registerClause with migration note.

Both global registries remain for backward compatibility via the
  deprecated registerDefault*() functions, but the evaluation engines
  no longer depend on them. Every evaluation gets its own registry by
  default, so custom predicates registered by one test cannot leak
  into another. Tests using explicit registry injection are unaffected.

662 tests pass (315 DSL + 141 core + 149 solver + 57 E2E).
2026-05-22 15:44:44 -07:00
John Dvorak 6c2471052f refactor: runtime isolation — deprecate global singletons, add counter resets
config.ts: @deprecated on configure/getConfig/project/getProjectConfig.
  Current code has zero production callers — dead code path. Functions
  remain for backward compatibility but signal migration to scoped config.

context.ts: @deprecated on setDefaultContext. Documents that consumers
  should call resetDefaultContext() after use in tests to prevent
  context-seed leakage.

pipeline.ts: Export resetTraceCounter() for test isolation of _traceId
  counter variable.

extraction.ts: Export resetPageCacheNamespaceCounter() for test
  isolation of pageCacheNamespaceCounter.

Combined with prior fix (defaultPredicatesRegistered→sentinel check
+ compatibilityWarningEmitted→warnedUis WeakSet), 5 of 7 correctness-
  affecting global mutable state items are now tied down. Remaining:
  globalPredicateRegistry/globalClauseRegistry (already @deprecated
  with explicit injection path via LogicEngineOptions/EvalOptions).

605 SDK + 57 E2E = 662 tests pass.
2026-05-22 15:35:20 -07:00
John Dvorak 6a98d8ce9e refactor: compiler pipeline hardening — parser diagnostics + exhaustive switches + golden equivalence
grammar.ts:
  - Replace 2 parseAssertion console.warn + return-null patterns with
    this.errorWithCode() throws, producing structured ParseError
    diagnostics instead of silently skipping invalid clauses
  - Maintain parseBlock's return-null contract for unmatched tokens
    (caller loops over tokens expecting null for non-block starters)

compiler.ts:
  - buildOptionsFromAssertion: convert 3 standalone ifs to if/else if/else
    with never-exhausted throw on unknown assertion type
  - compileToFormula: replace silent return-null with throw on unknown
    assertion type

validator.ts:
  - validateAssertion: replace silent return {valid:true} for unknown
    assertion types with throw

fol-compiler.ts:
  - compileSingleAssertion: replace silent return-null with throw

Golden equivalence tests (fol-equivalence-golden.test.ts):
  7 new deterministic tests covering gaps identified in REFACTOR item 5:
    - Size assertions: atLeast, atMost, between (fluent vs dense)
    - Compound assertions: .and chain, .or chain
    - Options: gap, tolerance in jnd
  Documents known discrepancy: fluent size.* prefix vs dense canonical name
  (fol-compiler normalizes at FormulaNode level, canonical path does not)

Existing property-based equivalence tests cover spatial, quantifier,
and frame equivalence. Topology predicates have no fluent API surface
(dense-DSL-only), so equivalence must be verified at evaluation level
(already covered by hard E2E topology tests).

662 tests pass (315 DSL + 141 core + 149 solver + 57 E2E).
2026-05-22 15:23:29 -07:00
John Dvorak 8ac69254d4 feat: fact planning derived from predicate specs with fulfillment tracking
predicate-specs.ts:
  - Add FactCategory enum (geometry, topology, styles, fragments,
    domAncestry, clipping, scroll, visibility, transforms, text)
  - Add FactPlan interface with predicateFacts provenance map and
    per-category fulfillment tracking (fulfilled/failed/approximated/skipped)
  - Add requiredFactToCategory() — centralized mapping from required-fact
    strings to FactCategory values, replacing scattered inline checks
  - Add planRequiredFacts() — builds a complete FactPlan from collected
    predicate facts + AST structural analysis (CssLengthMetrics, domAncestry)
  - Add createEmptyFactPlan() factory

extraction.ts:
  - computeRequiredFacts returns FactPlan (was inline record), uses
    planRequiredFacts from spec infrastructure
  - extractWorld/extractWorldFastGeometry/extractWorldCdp accept FactPlan
    instead of inline requiredFacts record
  - After successful extraction, populate FactPlan.fulfillment with
    per-category 'fulfilled' status, providing a structured audit trail
    of which facts were requested and obtained

Adding a new predicate with new required facts now requires only a
spec entry — the fact planner, extraction engine, and fulfillment
tracker all derive behavior from the spec table automatically.

658 tests pass.
2026-05-22 15:02:20 -07:00
John Dvorak e78ffe3419 refactor: remove world smuggling — type styles, fragments, topologySubjectIds
registry.ts: Add optional styles and fragments table types to the
  solver's GeometryWorld. Previously these were attached via
  (world as any).styles / .fragments, bypassing the type system.

extraction.ts:
  - remapTopologyIds: accept topologySubjectIds as explicit parameter
    instead of reading (world as any)._topologySubjectIds and deleting
    it post-remap. Eliminates 3 smuggling accesses (read, set, delete)
  - All worldAny.styles / worldAny.fragments / worldAny.strings /
    worldAny.transforms / worldAny.matrices assignments now use
    typed world.xxx access. worldAny variable removed entirely.
  - attachMeasuredChWidths: use world.styles directly.
  - (result.world as any).env mutation replaced with typed
    result.world.env assignment.
  - (sizeWorld as any).styles replaced with typed access.

predicates.ts: Replace all 3 (world as any).styles inline type-assert
  reads (getSubjectFontSizePx, getRootFontSizePx, getSubjectChWidthPx)
  with direct world.styles?.xxx access. No runtime behavior change.

Zero (world as any) casts remain in extraction.ts or predicates.ts.
658 tests pass.
2026-05-22 14:38:37 -07:00
John Dvorak 45b5575e53 fix: prevent silent passing on missing topology data + visible cleanup failures
predicates.ts (missing-fact discipline):
  - getTopologyValueBySubject: return -1 sentinel instead of ?? 0
    when topology data is missing/unset. NaN and negative values
    also treated as missing. Previously returned 0 which was
    indistinguishable from root subject ID.
  - clippedByPredicate: return 'indeterminate' when clippingRoot < 0
  - inStackingContextPredicate: return 'indeterminate' when sc < 0
    or refSC < 0 (both subject and reference). Previously treated
    missing data as false — a silent wrong answer.
  - attachedToScrollContainerPredicate: return 'indeterminate' when
    scrollContainer < 0
  - escapeClippingChainOfPredicate: return 'indeterminate' when
    clippingRoot < 0

extraction.ts (cleanup visibility):
  - Promote fast-geometry and CDP cleanup failures from console.debug
    (invisible during test execution) to console.warn. Contaminated
    pages are now diagnosable without debug-log inspection.

658 tests pass.
2026-05-22 13:55:11 -07:00
John Dvorak 1a8f308f71 refactor: remove global mutable state — registry guard, compat warning, stats
predicates.ts: Replace defaultPredicatesRegistered boolean guard with
  sentinel predicate check inside the registry. registerDefaultPredicates()
  is now always safe to call — no module-scope flag that can drift out
  of sync with the actual registry state. clearPredicateRegistry() no
  longer needs to manually reset a flag.

extraction.ts:
  - Replace compatibilityWarningEmitted process-singleton boolean with
    WeakSet<ImhotepUi> (warnedUis). Each ImhotepUi instance now gets its
    own compatibility warning, fixing the bug where two pages would share
    a single warning gate.
  - Export resetExtractionPathStats() for test isolation of fast-path
    and CDP fallback counters.

598 SDK + 3 conformance + 57 E2E = 658 tests pass.
2026-05-22 13:52:09 -07:00
John Dvorak 7f40224f0f fix: propagate compound state through toAst() and canonical lowering
ast.ts: Add optional compoundOperator and compoundParts fields to
  RelationAssertion, allowing compound fluent assertions to carry
  their .and / .or structure through the AST layer.

fluent.ts: toAst() now emits compoundOperator/compoundParts when
  isCompound, closing the gap where compound state was silently
  dropped in canonical extraction, preset building, and validation.

lower-to-canonical.ts: Handle RelationAssertion with compound metadata
  by lowering each part as a separate grouped clause (same groupId,
  same compoundOperator), matching DSL parser compound behavior.

validator.ts: Validate options on each compound part, closing the
  false-positive validation gap where only the final relation was checked.

3 lossy code paths (extractCanonicalFromAssertion, presets.toPresetResult,
validateRelation) now preserve compound structure. Primary FOL evaluation
path was already correct (fol-compiler reads compoundParts directly).

658 tests pass.
2026-05-22 13:46:54 -07:00
John Dvorak a49d6008a4 fix: add WORLD_CACHE_SCHEMA_VERSION to cache key for automatic invalidation
Cache keys now include a schema version prefix so that world-schema
changes automatically invalidate stale cached extraction results.
Previously two incompatible schema versions would share the same
cache key if URL/selectors/env matched, silently returning stale data.

WORLD_CACHE_SCHEMA_VERSION exported publicly so consumers can
increment it when making schema-incompatible changes to extraction.

658 tests pass.
2026-05-22 13:35:12 -07:00
John Dvorak 1bc0c8e6df refactor: replace smuggled compound properties with typed FluentRelation API
fluent.ts: Add public getters isCompound, compoundOperator, compoundParts
  to FluentRelation. Add optional CompoundState to constructor. Add
  CompoundPart/CompoundState interfaces. Replace 4 (this as any)._xxx
  property smuggles in FluentCompoundBuilder._addPart() with properly
  typed constructor initialization via CompoundState.

fol-compiler.ts: Replace 3 (relation as any)._compoundParts /
  ._compoundOperator / ._isCompound duck-type reads with direct
  relation.compoundParts / relation.compoundOperator / relation.isCompound.

extraction.ts: Replace (rel as any)._compoundParts read in
  getSelectorsFromAssertion with typed compoundRel.isCompound /
  compoundRel.compoundParts access.

Eliminates 7 (as any) casts across 3 production files. Zero remaining
_compoundParts/_compoundOperator/_isCompound/_originalFirstRelation
smuggled references in any production source file.

598 SDK + 3 conformance + 57 E2E = 658 tests pass.
2026-05-22 13:31:34 -07:00
John Dvorak 9df295b915 refactor: eliminate remaining hardcoded predicate-name dispatch
Extraction.ts (3 fixes):
  - Replace 2 'inStackingContext' string checks with isVariableArityPredicate()
  - Replace 7-name diagnostic formatting if/else with spec-driven
    getPredicateSpec() checks (isDirectional → gap message,
    isSize → threshold hint, else generic)

Grammar.ts: Replace 8 hardcoded parser routing checks
  (atLeast/atMost/aspectRatio/between/clippedBy/attachedToScrollContainer/
  escapeClippingChainOf/inStackingContext) with SIZE_PREDICATE_NAMES and
  TOPOLOGY_PREDICATE_NAMES Sets derived from spec table.

Pipeline.ts: Replace 15-entry CODE_TO_CLAUSE_KIND map with runtime
  generation from PREDICATE_SPECS. Prefix derived from spec.isSize
  ('size.*') / validOptions.includes('axis') ('alignment.*') /
  else ('relation.*'). Manual override for aspectRatio code 15.

Proofs.ts: Replace 11-case switch(kind) with 5 spec-driven if/else
  branches categorized by validOptions presence (hasGap→directional,
  hasAxis→alignment) + 2 specific name checks (inside overflow,
  aspectRatio ratio). 11 predicate names → 0 hardcoded.

Lexer.ts: Export KEYWORDS map for conformance testing.

Conformance tests:
  - Solver: every BUILTIN_PREDICATES entry matches its PREDICATE_SPECS
    counterpart; every spec name (incl. aliases) has a registered
    evaluator with matching descriptor (2 tests)
  - DSL: every predicate name from collectAllPredicateNames() appears
    in the lexer KEYWORDS table (1 test)

598 SDK + 3 conformance + 57 E2E = 658 tests pass.
2026-05-22 13:15:35 -07:00
John Dvorak 283ab1b39f refactor: convert grammar predicate detection to spec table
Replace 21-item hardcoded isKeywordThatCanBePredicate array with
isPredicateName() from PredicateSpec (+ 'size' special case for
fluent API FOL bodies).

Replace 24-item parseRelation relationKinds array with
collectSpatialPredicateNames() from spec. Error message now also
auto-derives from the spec.

595 SDK + 57 E2E tests pass.
2026-05-22 12:51:20 -07:00
John Dvorak 2e27693278 refactor: convert DSL compiler relationCodes and unary check to spec table
Replace 19-entry relationCodes string-to-integer map with
getPredicateRelationCode() from PredicateSpec. Keep only
aspectRatioBetween (15) as local DSL-keyword override.

Replace string-based unary detection ('atLeast'||'atMost') in
compileSimpleAssertionToFormula with isUnaryPredicate() from spec.

595 SDK + 57 E2E tests pass.
2026-05-22 12:48:39 -07:00
John Dvorak 6f82849b4f refactor: convert validator to use predicate spec table
Replace 5 static Sets/maps (SPATIAL_RELATIONS, SIZE_RELATIONS,
VALID_OPTIONS, QUANTIFIER_COMPATIBLE, UNARY_RELATIONS) with
derivation from the unified PredicateSpec table:
  - collectSpatialPredicateNames / collectSizePredicateNames
    for category sets
  - getPredicateValidOptions for option validation
  - collectQuantifierCompatiblePredicateNames for quantifier checks
  - isUnaryPredicate for unary detection

Local override map retained for fluent API dotted size variants
('size.atLeast' etc.) and 'aspectRatioBetween' DSL keyword, which
are input conventions, not distinct predicates.

595 SDK + 57 E2E tests pass.
2026-05-22 12:46:41 -07:00
John Dvorak c0357b152f refactor: introduce unified PredicateSpec table, convert extraction consumers
Imhotep-core: add predicate-specs.ts with 34 PredicateSpec entries as
  the single source of truth for predicate metadata (name, arity,
  aliases, requiredFacts, validOptions, diagnosticCode, relationCode,
  decompose rules, category flags). Lookup helpers derive all
  per-predicate information from the static table.

Extraction.ts (3 consumers converted):
  - computeRequiredFacts: replace getRequiredFactsForPredicate (global
    registry) with getPredicateRequiredFacts (static spec table).
    Removes registerDefaultPredicates() dependency from fact planning.
  - compileCanonicalClauseToFormula: replace 4 string-branch patterns
    ('between'/'separatedFrom'/'atLeast'/'aspectRatio'/'inStackingContext')
    with spec-driven getPredicateDecomposition() and isUnaryPredicate().
    Same behavior, zero string dispatch in predicate selection.
  - mapFolDiagnostic: replace PREDICATE_TO_DIAGNOSTIC_CODE (13-entry
    Record) with getPredicateDiagnosticCode() from spec table.

595 SDK + 57 hard E2E tests pass.
2026-05-22 12:44:57 -07:00
John Dvorak aa69ddc52f fix: replace as any casts in AST walkers with typed guards + add options to PredicateCall
logic-ast.ts: add optional options?: Record<string, unknown> to
  PredicateCall interface. Previously any predicate needing options
  (e.g., space, dimension, tolerance) smuggled them via (node as any)
  .options, bypassing the type system entirely.

extraction.ts: replace all 18 (node as any).body/.left/.right etc.
  casts with proper type guard narrowing (isForAllFormula, isExistsFormula,
  isAndFormula, isOrFormula, isNotFormula, isImpliesFormula, isPredicateCall).
  Affected functions: collectPredicates, formulaNeedsCssLengthMetrics,
  usesLayoutSpace, computeRequiredFacts & nestDomAncestry, getSelectorsFromFormula.

595 SDK + 57 E2E tests pass.
2026-05-22 12:18:22 -07:00
John Dvorak 07ed316135 fix: NaN guard in normalizeOptionValue + warn on silent parser drops
compiler.ts: normalizeOptionValue now rejects NaN numbers and empty
  strings. Previously typeof NaN === 'number' passed through and
  propagated into option values, causing predicate comparisons like
  value >= NaN to produce silent wrong results.

grammar.ts: parseAssertion now emits console.warn when silently
  skipping unexpected tokens or failed clauses. Previously these
  returns-null were invisible to developers.

454 solver+DSL + 57 E2E tests pass.
2026-05-22 12:08:39 -07:00
John Dvorak 9b691b2c7c fix: close out remaining audit findings — type safety, equivalence, deprecations
lower-to-canonical.ts: clauseEquivalent now compares compoundOperator
  and compoundGroupId. Previously, compound assertions with different
  operators (.and vs .or) were considered equivalent.

fol-compiler.ts: adaptGrammarFormulaToLogicAst validates node.kind
  against known formula kinds (forall/exists/and/or/not/implies/predicate)
  before passing through as FormulaNode. Previously any object with a
  'kind' property was blindly cast.

predicates.ts / registry.ts: @deprecated tags on globalPredicateRegistry
  and globalClauseRegistry. Both are still functional but consumers should
  transition to explicit injection via LogicEngineOptions / EvaluationOptions.

454 solver+DSL tests pass, zero regressions.
2026-05-22 12:05:43 -07:00
John Dvorak e17e4d6c20 fix: remove silent error suppression — cache failures, box index mutation, cleanup catches
geometry-cache.ts: replace 5 empty catch blocks with console.warn
- statSync failure, rmSync failure (x2), readCachedWorld failure,
  readCachedExtractionResult failure were all silently swallowed.
  Now emit context-bearing warnings so stale/corrupt caches are visible.

predicates.ts: replace __boxIndex as any mutation with WeakMap
- getBorderRect used (world as any).__boxIndex to cache a subject-to-
  box-index map on the world object. Replaced with module-level WeakMap
  that auto-collects when the world is GC'd. Eliminates 2 as any casts.

extraction.ts: serialize materializeSemanticSelector + debug cleanup
- 3 Promise.all sites over page.evaluate changed to sequential for..of
  to eliminate DOM modification race conditions.
- 2 .catch(()=>{}) cleanup blocks now use console.debug so failed
  cleanup is traceable when debugging.
- resolveViewport catch now emits console.warn on zero-viewport fallback.

648 SDK + 57 E2E tests pass.
2026-05-22 12:00:20 -07:00
John Dvorak a424d29ccc fix: remove design-debt shims — falsy ID bug, selector normalization, concurrency, exception swallowing
pipeline.ts: || undefined → ?? undefined (9 occurrences)
- || converts valid subject ID 0 to undefined because 0 is falsy in JS.
  This broke clause witnesses and topology references for the first subject.

domain-index.ts: remove .toLowerCase() on CSS selectors
- CSS selectors are case-sensitive (IDs, class names, attribute values).
  Lowercasing on lookup but not on storage (selectorIndex) meant case-
  sensitive selectors never matched — returning empty arrays silently.

canonical.ts: add warning when visualBoxes falls back to layout boxes
- visualBoxes ?? boxes silently substituted layout coordinates for visual
  space, producing incorrect results for transform-dependent assertions.
  Now emits console.warn so silent data corruption is visible.

extraction.ts: serialize materializeSemanticSelector calls (3 sites)
- Changed Promise.all over page.evaluate() to sequential for..of. While
  Playwright serializes CDP calls internally, concurrent DOM-modifying
  evaluate() calls create undefined execution order. Sequential resolution
  eliminates theoretical race conditions for semantic selector injection.

engine.ts: include stack trace in evaluator exception diagnostics
- Catch-all converted ALL exceptions (including TypeError from programming
  bugs) to IMH_EVALUATOR_EXCEPTION with just err.message. Now includes
  stack trace and logs to console.warn for visibility. Distinguishes
  TypeError (programming bug) from other evaluation errors.

648 SDK tests + 57 E2E hard tests pass, zero regressions.
2026-05-22 11:55:58 -07:00
John Dvorak 0a73063c76 fix: topology array reordering by subject order + unary inStackingContext
Topology array order mismatch (critical bug):
- CDP browser script iterates elements in document order (querySelectorAll),
  but solver accesses topology arrays by selector-resolution order.
- Fix: add subjectIds array to TopologyRecord tracking the backendNodeId
  at each document-order position; remapTopologyIds now reorders all 6
  topology arrays before remapping backendNodeIds to solver IDs.
- Fallback: when subjectIds is missing (cached pre-fix data), falls back
  to simple remap without reordering.

Unary inStackingContext compilation:
- compileCanonicalClauseToFormula treated inStackingContext as always binary,
  creating referenceBinding with undefined selector for unary assertions.
- Fix: add (inStackingContext && !clause.reference) to unaryPredicate/isUnary.

Hard test fixture (fixtures package):
- 29-element geometric fixture with 10 scenarios
- 57 assertions (54 spatial + 3 topology) all pass end-to-end
2026-05-22 11:38:28 -07:00
John Dvorak 1ac30c6e18 fix: support between predicate in FOL dense DSL
- grammar.ts: add 'between' to isKeywordThatCanBePredicate() so the parser
  recognizes it as a valid predicate keyword in forall/exists formula bodies
- compiler.ts: add special case in compileDenseFOLToFormula for
  between(, min, max, dimension?) that extracts numeric args into
  options ({min, max, dimension}) instead of dropping them in the generic
  arg loop. The existing betweenPredicate evaluator already handles these.
- fol-dense-combinations.test.ts: replace GAP test with two verified-working
  tests for between and between with dimension
2026-05-22 10:48:26 -07:00
John Dvorak 1bc92e1f7d 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)
2026-05-22 10:20:06 -07:00
John Dvorak 654becabc6 chore: remove dead makeNotImplementedPredicate helper
All 27 BUILTIN_PREDICATES now have real evaluators. The
makeNotImplementedPredicate factory and its IMH_FEATURE_NOT_YET_IMPLEMENTED
path were the last remaining NYI scaffolding — no caller existed for it.
IMH_FEATURE_NOT_YET_IMPLEMENTED diagnostic code kept in the taxonomy
as a future fallback, but no evaluator produces it.
2026-05-21 20:15:40 -07:00
John Dvorak b4ae8e9134 fix: children($var) domain compilation and resolver support
- Add children branch in convertDomain() alongside descendants —
  children() now correctly maps to parentVar: '$parent'
  instead of falling through to the default branch which reversed
  the parentVar/selector mapping.
- SelectorDomainResolver now handles parentVar domains with no
  CSS selector filter (children domain collects all registered
  subject IDs, then filters by parent via ancestor index).
- All 1072 tests pass across 14 packages.
2026-05-21 20:13:57 -07:00
John Dvorak 35c5dbd153 fix: add missing predicate evaluators and BUILTIN_PREDICATES entries
- CRITICAL: escapeClippingChainOf now has an evaluator (returns indeterminate
  with clear diagnostic — full implementation requires fragment-level bounds
  analysis). Previously parsed but silently produced IMH_EVALUATOR_MISSING.
- attachedToScrollContainer added to BUILTIN_PREDICATES with evaluator
  (checks world.topology.scrollContainerOf). Was only in legacy engine.
- aspectRatio added to BUILTIN_PREDICATES with evaluator (compares
  width/height ratio against min/max bounds). Was only in legacy engine.
- Fix all BUILTIN_PREDICATES index references shifted by new entries
  (beside 17→20, nextTo 18→21, adjacent 19→22, touching 20→23,
  near 21→24, under 22→25, within 23→26, separatedFrom 16→18,
  inStackingContext 15→17).
- Register attachedToScrollContainer, escapeClippingChainOf, aspectRatio
  in registerDefaultPredicates.
- Zero NYI/not-implemented predicates remain in the registry.
2026-05-21 20:10:17 -07:00
John Dvorak 2eff60814d fix: force CDP extraction when formula uses variable-bound domains
- computeRequiredFacts now returns domAncestry flag by scanning formula
  bindings for parentVar references (descendants/children domains)
- extractWorld fast-path gate now requires domAncestry === false — formulas
  with parentVar domains will always use CDP extraction, which provides
  the DOM parentNodeId data needed for ancestor index construction
- Prevents silent indeterminate results when descendants(, sel) is
  used on the fast path (which lacks DOM ancestry data)
- Cache key updated to include domAncestry flag ('a') so cached fast vs
  CDP results for the same selectors don't collide
2026-05-21 18:59:09 -07:00
John Dvorak 7c61cb51ee fix: propagate formula metrics into FOL diagnostic mapping
- mapFolDiagnostic now accepts optional metrics parameter
- adaptFOLResultToImhotepResult matches solver diagnostics to
  formula results by clauseId/formulaId and passes formula-level
  metrics (gap, dimensions, stacking context, overflow, etc.)
  into the diagnostics output
- Previously mapFolDiagnostic always returned metrics: {} and
  sourceRef: {}, making compound/quantified failures undiagnosable
  for users investigating contracts
2026-05-21 17:17:06 -07:00
John Dvorak 16dcf42762 fix: propagate operand metrics through boolean connectives
- evaluateAnd: carry left metrics on left-fail short-circuit,
  carry right metrics on right fail/indeterminate
- evaluateOr: carry left metrics on left-pass short-circuit,
  carry right metrics on right pass, merge both on double-fail
- evaluateNot: carry operand metrics when operand passes
  (so not fails with diagnostic context), carry on indeterminate
- evaluateImplies: carry consequent metrics on fail/indeterminate

This ensures compound formula failures preserve measured geometry
context (gap, dimensions, direction) rather than presenting empty
diagnostics to users investigating layout contract violations.
2026-05-21 17:14:00 -07:00
John Dvorak 5830d5861e fix: resolve inStackingContext arity/semantics mismatch
- Change BUILTIN_PREDICATES[15] arity from 1 to 2 to match binary usage
  in the FOL compiler and topology engine
- Update evaluator to handle both unary (does element have stacking context)
  and binary (do both elements share the same stacking context) cases
- Binary comparison: both elements must have a valid stacking context
  AND their stackingContextOf values must match
- Aligns predicate semantics with the legacy engine in solver/topology.ts
  and the topology query layer's inStackingContext(graph, subj, ref?) API
2026-05-21 17:10:38 -07:00
John Dvorak 19559b658b feat: implement variable-bound FOL domain resolution for descendants/children
- Extend DomainResolver.resolve() signature to accept optional BindingEnv
  so that parentVar domains can be resolved with runtime variable bindings
- Pass BindingEnv through evaluateForAll/evaluateExists to resolver calls
- Add buildAncestorIndex() to precompute DOM ancestor sets from CDP data
- SelectorDomainResolver now filters descendant domains by the bound parent
  when domain.parentVar is present and ancestor index is available
- Return undefined for parentVar domains when no ancestor index or env
  (prevents silent fallback to global domain resolution)
- Update all test DomainResolver mocks for new resolve interface
- Add 10 unit tests covering ancestor index construction, backward compat,
  descendant filtering, exclusion of non-descendants, empty descendants,
  missing parentVar/env, and no-ancestor-index safety
2026-05-21 17:05:35 -07:00
John Dvorak b7ac0e8f31 chore: remove 2 unnecessary as any casts (renderer kind, window properties)
- renderer.kind is typed on RendererDescriptor, no cast needed
- buildGeometryWorld returns proper GeometryWorld type (solver variant
  differs from core variant, as any required for that cross-package cast)
2026-05-21 15:14:19 -07:00
John Dvorak 4d5456ac4a chore: classify barrel exports with @public/@internal JSDoc annotations
- Mark core API as @public: runtime, fixtures, fluent entry points,
  semantic subjects, renderers, presets
- Mark internals as @internal: pool classes, page wrapper, environment
  utilities, target resolution
- Reorganize barrel into clear @public / @internal sections
2026-05-21 15:05:35 -07:00
John Dvorak de12e93cf8 chore: fill CSS contain test gaps + unskip clippedBy e2e test
- Add 4 unit tests in predicates.test.ts for new contain metrics:
  inside hasClippedOverflow (with/without clipping)
  clippedBy clipKind (contain:paint=1, overflow=2)
- Unskip and implement clippedBy e2e test with overflow:hidden container
  in e2e-edge.test.ts (was stale skipped with 'not yet implemented')
2026-05-21 14:44:26 -07:00
John Dvorak 4ff56d61c2 feat: wire directional alignment predicates into dense DSL grammar
- Add leftAlignedWith/rightAlignedWith/topAlignedWith/bottomAlignedWith
  to lexer TokenKind union and keyword map
- Add to grammar.ts consumeRelation() recognized relation kinds
- Dense DSL users can now write: '.a' leftAlignedWith '.b', etc.
  (previously returned parser error 'Expected relation')
2026-05-21 14:39:01 -07:00
John Dvorak 96bcce1ddb feat: implement directional alignment predicate evaluators (no more NYI)
- Replace makeNotImplementedPredicate stubs for leftAlignedWith,
  rightAlignedWith, topAlignedWith, bottomAlignedWith with real
  evaluators delegating to alignedWithPredicate with axis option
- Update logic-engine.test.ts to expect real evaluation results
  instead of IMH_FEATURE_NOT_YET_IMPLEMENTED
2026-05-21 14:36:02 -07:00
John Dvorak 8dae898304 feat: annotate inside/contains overflow metrics with clipping awareness
- insidePredicate: add hasClippedOverflow=1 when reference element
  clips its content (contain:paint or overflow:hidden) and the
  subject overflows beyond the reference bounds
- containsPredicate: same, checking the subject (container) clips
- Reads world.clipping table to determine if the container clips
- Safe when clipping data is absent (unit test fixtures)
2026-05-21 14:11:47 -07:00
John Dvorak d23d2a431e feat: extract CSS contain from browser in Playwright adapter
- Add contain: cs.contain to browser-side style extraction
- Parse contain string into bitfield matching CDP extractor schema
- Add inline-size to CDP styles.ts contain parser for consistency
2026-05-21 14:05:55 -07:00
John Dvorak afb985cdf3 chore: add contain to CLIP_KIND_MAP (forward compat for Playwright pipeline) 2026-05-21 13:54:34 -07:00
John Dvorak c4a3d304ef feat: solver consumes CSS contain:paint for clippedBy diagnostics
- Add Contain=5 to geometry world ClipKind enum (avoids collision with
  topology engine's CONTAIN=3 vs Mask=3)
- Fix CDP isClippingElement() to detect contain:paint (style.contain)
- CDP topology builder sets clipKind=5 for contain:paint, 1 for overflow
- Enrich clippedByPredicate with clipKind metric:
  1=contain:paint, 2=overflow (hidden/scroll/auto)
- Reads world.clipping.clipKind of the reference's clip node
2026-05-21 13:52:28 -07:00
John Dvorak a75c3be9e0 feat: parse CSS contain values in geometry normalization
- Add CONTAIN_MAP with bitfield flags matching CDP extractor schema
  (layout=1, paint=2, size=4, style=8, strict=15, content=7, inline-size=16)
- Parse space-separated contain keywords in normalizeStyles()
- Replace containFlags[i]=0 TODO with actual value lookup
- Unknown contain keywords map to 0 gracefully
2026-05-21 13:20:57 -07:00
John Dvorak a222a9fa8a chore: e2e tests for compound fluent assertions + clean barrel exports
- Add 2 e2e FOL solver tests for fluent .and/.or through checkAll()
  using a mock page with distinct geometry positions (P0-1 regression)
- Remove 4 individual adapter factories from barrel index:
  createReactAdapter, createVueAdapter, createStorybookAdapter,
  createCustomAdapter (use react()/vue()/storybook()/custom() instead)
- Reduce barrel from 101 to 89 lines
2026-05-21 13:07:16 -07:00
John Dvorak 771ddaea4e fix: compound .and/.or chaining silently ignored in fluent API
The proxy in buildExpectation() returned FluentRelation raw (not
proxied), so .and/.or chaining happened outside the assertion store.
The second relation was never stored — checkAll() only evaluated
the first relation (false positive).

Add ensureAndOrProxied() to override .and/.or on FluentRelation
instances so compound builders flow through the assertion store
proxy. Preserves instanceof FluentRelation checks (no JS Proxy on
the instance, just property overrides).

3 regression tests cover .and, .or, and triple-chaining.
2026-05-21 12:31:35 -07:00
John Dvorak 70f528fbab chore: polish for FOL contract delivery (CHANGELOG, CLI guardrails, metadata)
- Update CHANGELOG.md for 1.1.0 (date, refactoring, repository fixes)
- Add overwrite guardrails to imhotep-cli init — skips existing files
- Add bugs/homepage/keywords metadata to 5 public packages
- Mark imhotep-bench and imhotep-fixtures as private packages
- Add selector field to SourceReference interface (core types)
- Remove 7  casts from check-all.ts (folAst.position, cardinality results)
- Generate package-lock.json for reproducible installs
2026-05-21 11:58:31 -07:00
John Dvorak 4ceb411028 refactor: extract check-all.ts from public.ts (checkAll closure)
Extracted the 629-line checkAll closure from imhotep() into a standalone
makeCheckAll() factory in check-all.ts. Handles temporal dead zone by
passing getUi() getter instead of ui value.

public.ts reduced from 3568 lines to 916 lines (-74.3%). Now contains
only: imports/re-exports, normalizeRuntime, the imhotep() factory
function, property-run internals, and entry points.
2026-05-21 11:26:26 -07:00
John Dvorak cec9597090 refactor: extract extraction.ts from public.ts (1935 lines)
Moved the extraction pipeline, formula analysis, selector resolution glue,
CDP extraction, canonical compilation, contract building, cardinality
evaluation, FOL diagnostic mapping, compatibility reporting, and all
module-level extraction state into a dedicated extraction.ts module.

public.ts reduced from 3568 to 1533 lines (-57%). The remaining file
contains only the imhotep() entry point, property-run internals, and
component/story/fixture entry points, plus re-exports for backward
compatibility.
2026-05-21 11:04:48 -07:00