core/algebra
Shay 3e2710faee
fix(ingest): close issue #300 — normalize_to_versor margin at the gate (#312)
The bug: ingest.gate.inject raised RuntimeError("Injection produced
non-versor field") on a class of ordinary English token combinations
(declarative-with-quantity + transfer phrase + "How many" question).
Both observed condition values (1.02e-06, 2.12e-06) cleared
unitize_versor's `bad_residue` heuristic but landed just above the
gate's 1e-6 downstream check, crashing the engine on textbook word
problems like:

  "Tom has 5 apples. He gives 2 to Sarah. How many does Tom have?"

Root cause: normalize_to_versor accepted the unitized candidate
without checking that it strictly satisfied the gate's
versor_condition < _RUNTIME_CLOSURE_TOLERANCE (1e-6) contract.
unitize_versor's internal tolerance is permissive for construction-
time inputs; the gate's downstream tolerance is stricter.  When the
two diverged on certain token mixes, the candidate slipped through
and the gate's assert fired.

Fix: mirror the strict-closure pattern from _runtime_closed /
_close_applied_versor.  If unitize_versor succeeds but the result
still fails the public versor_condition < _RUNTIME_CLOSURE_TOLERANCE
contract, project through the deterministic construction map
(_seed_to_rotor) instead of returning the drifted candidate.

Per CLAUDE.md: threshold stays at 1e-6 (Non-Negotiable Field
Invariant).  Construction boundary is where drift is repaired.
The fix lives at the SINGLE allowed normalization site
(ingest/gate.py's only entry point into the algebra) without
loosening any invariant.

Tests added (11):
- versor_condition strictly satisfied on a range of seeded random
  inputs (property test)
- 20-iteration synthetic-marginal probe exercises the construction-
  fallback path
- The three issue-#300 bisected crash repros run end-to-end through
  `core chat` and complete without raising the RuntimeError
- Threshold constant pinned (failing the test if anyone lowers
  _RUNTIME_CLOSURE_TOLERANCE)

Validation:
- All 11 new tests pass
- 37 existing versor / ingest tests pass (test_versor_closure +
  test_versor_*_rust_parity + test_core_ingest + test_unknown_token_ingest)
- Three pre-existing main failures (architectural_invariants
  INV02 / INV21 / INV24) are unchanged by this PR — verified by
  running them against origin/main directly before and after the
  fix
- The three crashing prompts now produce clean grounded surfaces
  through `core chat`

Closes issue #300.
2026-05-26 16:39:49 -07:00
..
__init__.py
backend.py perf(rust): zero-copy FFI for diffusion_step + parity-aligned bench gate 2026-05-21 08:51:15 -07:00
cga.py
cl41.py
holonomy.py
rotor.py fix(drift): proper rotor-manifold scaling; restore respond contract 2026-05-16 11:44:45 -07:00
versor.py fix(ingest): close issue #300 — normalize_to_versor margin at the gate (#312) 2026-05-26 16:39:49 -07:00