feat(adr-0249): P4 turn-program compiler + chained-relaxation executor #69

Merged
core-labs merged 1 commit from feat/adr-0249-p4-turn-program into main 2026-07-18 20:00:03 +00:00
Owner

The composition frontier. P1–P3 gave the corridor single-relation arithmetic and deduction; this makes it solve multi-step problems by chaining certified relaxation turns — the actual "wall is COMPOSITION" lesson getting its mechanism.

What it does

evals/turn_program.pycompile_turn_program(graph) turns a MathProblemGraph into an ordered TurnProgram (a seed + affine steps); execute_turn_program(program) runs it as a chain of relaxation turns and returns the decoded answer plus a certified turn ledger. The accumulator flows turn-to-turn as a field state and is decoded only once, at the end (anti-hollow, spike §2/§4.1) — the substrate performs every step and composition depth lives in the certified chain, not in matrix size.

Verified end-to-end: ((5+3)*2)-4 = 12, (10/2+7)*3 = 36, (100-40)/4 = 15, 4*3+5 = 17 — every turn ground_state_certified.

Ring-2 correction (verified in-tree)

The spike said "chain turns through the Ring-2 residual protocol." Grounding against the code, run_residual_protocol (core/ports/residual_protocol.py:222) is zero-bound / non-mutating — its stage-5 recertification re-witnesses the subject and raises recertification_witness_changed if it moved. Arithmetic turns mutate, so they cannot be routed through the protocol as the transforming subject; it certifies a fixed state's admissibility, not a state transition. Handled honestly, without forcing a hollow integration:

  • Per-turn certificate = the relaxation's own RelaxationCertificate (carries certificate_id, converged, psi_digest binding it to the exact state).
  • Tamper-evident sequence = a purpose-fit TurnRecord chain that reuses the Ring-2 chain-integrity pattern (content-addressed, GENESIS_DIGEST-linked, verify_turn_chain mirroring verify_replay_chain). Records carry certificate ids + step provenance, never a decoded value. Tampering any non-terminal record breaks the successor link; the terminal record's integrity comes from deterministic re-execution (the same property verify_replay_chain has).

Tier-1 scope (ruling #1), fail-closed

Single-accumulator chains over add / subtract / multiply / divide with constant Quantity operands and positive scale. Everything else — multi-entity, transfer, rate, comparison, fraction, partition, non-positive scale, unit mismatch — is refused with a typed TurnProgramError and is the recorded composition frontier, never a silent drop. Off-serving (A-04): bridges the generate-side graph to the corridor; never imported by chat/runtime.py.

Tests

tests/test_adr_0249_turn_program.py15/15 green. Five multi-step arithmetic chains, answer-unit propagation, chain verification + tamper detection, determinism, anti-hollow record shape, pure-compile check, and four refusal classes (multi-entity, transfer, non-positive scale, unit mismatch).

Next

P5 integrates this into the generalized-lift instrument against a real GSM8K holdout slice (honest-NULL, recorded Tier-1 coverage); then ADR-0249 lands with acceptance evidence. No self-Accept.

[Verification]: uv run python -m pytest tests/test_adr_0249_turn_program.py -q → 15 passed; uv run core test --suite smoke -q → 176 passed.

The composition frontier. P1–P3 gave the corridor single-relation arithmetic and deduction; this makes it solve **multi-step** problems by chaining certified relaxation turns — the actual "wall is COMPOSITION" lesson getting its mechanism. ## What it does `evals/turn_program.py` — `compile_turn_program(graph)` turns a `MathProblemGraph` into an ordered `TurnProgram` (a seed + affine steps); `execute_turn_program(program)` runs it as a chain of relaxation turns and returns the decoded answer plus a certified turn ledger. The accumulator flows turn-to-turn **as a field state and is decoded only once, at the end** (anti-hollow, spike §2/§4.1) — the substrate performs every step and composition depth lives in the certified chain, not in matrix size. Verified end-to-end: `((5+3)*2)-4 = 12`, `(10/2+7)*3 = 36`, `(100-40)/4 = 15`, `4*3+5 = 17` — every turn `ground_state_certified`. ## Ring-2 correction (verified in-tree) The spike said "chain turns through the Ring-2 residual protocol." Grounding against the code, `run_residual_protocol` (`core/ports/residual_protocol.py:222`) is **zero-bound / non-mutating** — its stage-5 recertification re-witnesses the subject and raises `recertification_witness_changed` if it moved. Arithmetic turns *mutate*, so they cannot be routed through the protocol as the transforming subject; it certifies a *fixed* state's admissibility, not a state *transition*. Handled honestly, without forcing a hollow integration: - **Per-turn certificate** = the relaxation's own `RelaxationCertificate` (carries `certificate_id`, `converged`, `psi_digest` binding it to the exact state). - **Tamper-evident sequence** = a purpose-fit `TurnRecord` chain that reuses the Ring-2 chain-integrity *pattern* (content-addressed, `GENESIS_DIGEST`-linked, `verify_turn_chain` mirroring `verify_replay_chain`). Records carry certificate ids + step provenance, never a decoded value. Tampering any non-terminal record breaks the successor link; the terminal record's integrity comes from deterministic re-execution (the same property `verify_replay_chain` has). ## Tier-1 scope (ruling #1), fail-closed Single-accumulator chains over add / subtract / multiply / divide with constant `Quantity` operands and positive scale. Everything else — multi-entity, transfer, rate, comparison, fraction, partition, non-positive scale, unit mismatch — is refused with a typed `TurnProgramError` and is the **recorded composition frontier**, never a silent drop. Off-serving (A-04): bridges the generate-side graph to the corridor; never imported by `chat/runtime.py`. ## Tests `tests/test_adr_0249_turn_program.py` — **15/15 green.** Five multi-step arithmetic chains, answer-unit propagation, chain verification + tamper detection, determinism, anti-hollow record shape, pure-compile check, and four refusal classes (multi-entity, transfer, non-positive scale, unit mismatch). ## Next P5 integrates this into the generalized-lift instrument against a real GSM8K holdout slice (honest-NULL, recorded Tier-1 coverage); then ADR-0249 lands with acceptance evidence. No self-Accept. [Verification]: `uv run python -m pytest tests/test_adr_0249_turn_program.py -q` → 15 passed; `uv run core test --suite smoke -q` → 176 passed.
core-labs added 1 commit 2026-07-18 19:58:39 +00:00
The composition frontier: multi-step arithmetic compiled from a MathProblemGraph
into an ordered turn program (one affine relation-well per step) and executed as
a chain of certified relaxation turns. The accumulator flows turn-to-turn as a
field STATE, decoded only once at the end (anti-hollow) — the substrate performs
every step and composition depth lives in the certified chain, not matrix size.
Verified: ((5+3)*2)-4=12, (10/2+7)*3=36, (100-40)/4=15, all turns
ground_state_certified.

Ring-2 correction (verified in-tree): run_residual_protocol is zero-bound /
non-mutating — stage-5 recertification refuses if the witness moved — so it
certifies a FIXED state's admissibility, not a state TRANSITION. Arithmetic turns
mutate. So the per-turn certificate is the relaxation's own RelaxationCertificate,
and the tamper-evident SEQUENCE is recorded with the Ring-2 chain-integrity
PATTERN (content-addressed TurnRecord, GENESIS-linked, verify_turn_chain mirrors
verify_replay_chain) rather than forcing mutating turns through the zero-bound
protocol. Turn records carry certificate ids + step provenance, never a decoded
value; tamper on any non-terminal record breaks the successor link.

Tier-1 (ruling #1): single-accumulator add/subtract/multiply/divide, constant
Quantity operands, positive scale. Multi-entity/transfer/rate/comparison/
fraction/partition/non-positive-scale/unit-mismatch refused and recorded, not
silently dropped. Off-serving (A-04).

15/15 pins green.

[Verification]: uv run python -m pytest tests/test_adr_0249_turn_program.py -q
core-labs merged commit 805752db72 into main 2026-07-18 20:00:03 +00:00
core-labs deleted branch feat/adr-0249-p4-turn-program 2026-07-18 20:00:05 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: core-labs/core#69
No description provided.