From bc838bd1de1ffd5696cfb0ab83079401ebd331e1 Mon Sep 17 00:00:00 2001 From: Shay Date: Thu, 23 Jul 2026 01:27:17 -0700 Subject: [PATCH] =?UTF-8?q?feat(coherence):=20grounded-open=20hedge=20arm?= =?UTF-8?q?=20=E2=80=94=20serve=20pack=20surfaces=20honestly=20hedged=20in?= =?UTF-8?q?stead=20of=20over-refusing=20(ADR-0254)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Weekly-audit 2026-07-22, T13 decision (2) ruling (Shay). The Shadow Coherence Gate hard-refused open-geometry surfaces even when the surface was pack-grounded and the openness was a geometric-coherence residual the pack never claimed to certify — a false refusal (the warmed "What is doubt?" case: "To doubt means to think maybe not. pack-grounded (en_core_meta_v1)." was replaced by "I cannot certify an answer: ... contract is open (goldtether_residual)."). Fix the reading, not the question: route open-geometry-but-pack-grounded surfaces to a hedge arm (authoritative=False), discriminated purely by grounding provenance (structural), never by question type. The query-type classifier bypass was rejected as a fail-open cue-table (ADR-0252 / INV-34). - core/cognition/surface_resolution.py: the decision lives INSIDE the gate. Predicate: pack_grounded (grounding_provenance in {pack, teaching}) AND every open token in {versor_condition, goldtether_residual}. The ¬hazard clause is enforced structurally by that residual allowlist — any unrecognized open token (where a genuine safety/harm/imperative hazard lands), non-pack grounding, or a None assessment falls through to the unchanged fail-closed refusal. New authority tag grounded_open_hedge; hedged=True; walk/compose folds suppressed. - core/cognition/pipeline.py: thread grounding provenance into resolve_surface (read once at the gate seam, reused for the OOV telemetry). - docs/adr/ADR-0254: the serving-physics decision + fail-closed argument. - tests/test_grounded_open_hedge_arm.py: RED->GREEN unit suite + real-data warmed-session integration. Genuine safety/harm hazards are unaffected: they ride separate axes (SafetyVerdict refusal, logos-morph override) that supersede this surface. [Verification]: hedge suite 15 passed; warmed_session lane pin + architectural invariants 85 passed (155.8s); GSM8K wrong=0 preserved (16 passed, 3 skipped); smoke 180 passed (131.65s), exit 0. worktree core-wt-hedge @ base 19847f90. --- core/cognition/pipeline.py | 7 +- core/cognition/surface_resolution.py | 120 ++++++++ docs/adr/ADR-0254-grounded-open-hedge-arm.md | 55 ++++ ...weekly-audit-2026-07-22-stragglers-todo.md | 24 +- tests/test_grounded_open_hedge_arm.py | 264 ++++++++++++++++++ 5 files changed, 460 insertions(+), 10 deletions(-) create mode 100644 docs/adr/ADR-0254-grounded-open-hedge-arm.md create mode 100644 tests/test_grounded_open_hedge_arm.py diff --git a/core/cognition/pipeline.py b/core/cognition/pipeline.py index 099cf75d..e4e6ccd5 100644 --- a/core/cognition/pipeline.py +++ b/core/cognition/pipeline.py @@ -467,6 +467,10 @@ class CognitiveTurnPipeline: # === SHADOW COHERENCE GATE WIRING === # Dual-competing: substrate supremacy requires fully grounded graph # AND closed geometric contract (versor_condition + GoldTether residual). + # Grounding provenance (pack/teaching/vault/oov/…) is read once here and + # reused for the OOV telemetry below; the gate consumes it to route an + # open-geometry-but-pack-grounded surface to the hedge arm (T13 dec. 2). + grounding_src = getattr(response, "grounding_source", "") or "" resolved = resolve_surface( canonical_surface=canonical, pre_decoration_surface=pre_decoration, @@ -479,6 +483,7 @@ class CognitiveTurnPipeline: compose_surface=compose_surface, proposition_graph=effective_graph, contract_assessment=contract_assessment, + grounding_provenance=grounding_src, ) surface = resolved.surface articulation_surface = resolved.articulation_surface @@ -555,7 +560,7 @@ class CognitiveTurnPipeline: # Populated observationally; never affects surface, hash (yet), or # any durable mutation. Uses only what the substrate already produced. oov_geometric_context = None - grounding_src = getattr(response, "grounding_source", "") or "" + # grounding_src is computed above at the gate-wiring seam and reused here. has_pending = bool(effective_graph and any( (n.obj or "") in ("", "") or "..." in (n.obj or "") for n in effective_graph.nodes diff --git a/core/cognition/surface_resolution.py b/core/cognition/surface_resolution.py index b041f06f..96009397 100644 --- a/core/cognition/surface_resolution.py +++ b/core/cognition/surface_resolution.py @@ -38,6 +38,22 @@ if TYPE_CHECKING: _ABSTENTION_AUTHORITY = "coherence_abstention" +# --- T13 decision (2): grounded-open hedge arm -------------------------------- +# Grounding provenances whose surfaces are curated enough to hedge (serve +# non-authoritatively) rather than hard-refuse when the geometric contract is +# open. Mirrors the "grounded" test in chat.runtime (pack / teaching). +_GROUNDED_PROVENANCES = frozenset({"pack", "teaching"}) + +# The ONLY open tokens that are hedgeable: geometric-coherence residuals that a +# pack surface never claimed to certify. Any token outside this set — where a +# genuine safety/harm/imperative hazard would land — fails closed to a hard +# refusal (INV-34). Deliberately excludes "missing_wave_field": the absence of +# any field is a harder failure than an open-but-computed residual. +_HEDGEABLE_GEOMETRIC_RESIDUALS = frozenset({"versor_condition", "goldtether_residual"}) + +_GROUNDED_OPEN_HEDGE_AUTHORITY = "grounded_open_hedge" +_GROUNDED_OPEN_HEDGE_PREFIX = "Grounded but not geometrically certified —" + @dataclass(frozen=True, slots=True) class SurfaceResolution: @@ -54,6 +70,11 @@ class SurfaceResolution: ``authoritative`` is True only when a certified answer may leave the system. Geometry-open and assessment-missing paths set this False and attach a typed refusal/violation. + + ``hedged`` is True only on the grounded-open hedge arm (T13 decision 2): + a curated pack/teaching surface served non-authoritatively because the + geometric contract is open on a known coherence residual. It is mutually + exclusive with ``refusal``/``contract_violation`` and never authoritative. """ surface: str @@ -61,6 +82,7 @@ class SurfaceResolution: authority: str fold_sources: tuple[str, ...] = () authoritative: bool = True + hedged: bool = False refusal: CoherenceRefusal | None = None contract_violation: ContractViolation | None = None proof_trace: ProofTrace | None = None @@ -136,6 +158,80 @@ def _abstention_resolution( ) +def _is_pack_grounded(grounding_provenance: str) -> bool: + """Structural discriminator: is the surface curated pack/teaching grounded? + + Purely provenance-based — it never inspects the question text. A lexical + "definitional/epistemic" cue-table would fail a geometric gate open on a + surface cue (fail-open; ADR-0252 / INV-34), which the ruling rejected. + """ + return (grounding_provenance or "").strip().lower() in _GROUNDED_PROVENANCES + + +def _grounded_open_hedge_admissible( + contract_assessment: "ContractAssessment", + grounding_provenance: str, +) -> bool: + """True iff an open-geometry surface may hedge instead of hard-refuse. + + Predicate (authorized): ``pack_grounded ∧ every open token is a known + geometric-coherence residual``. Fail-closed: an unrecognized open token — + where a genuine safety/harm/imperative hazard would appear — makes this + False, so the caller hard-refuses. ``¬hazard`` is thus enforced + *structurally* by the residual allowlist, not by question typing. + """ + if not _is_pack_grounded(grounding_provenance): + return False + open_tokens = set(contract_assessment.missing_bindings) | set( + contract_assessment.unresolved_hazards + ) + if not open_tokens: + # Defensive: the caller only reaches here when the conjugate is open. + return False + return open_tokens <= _HEDGEABLE_GEOMETRIC_RESIDUALS + + +def _grounded_open_hedge_resolution( + *, + canonical_surface: str, + pre_decoration_surface: str, + response_surface: str, + response_articulation_surface: str, +) -> SurfaceResolution: + """Serve the pack surface, honestly hedged and non-authoritative. + + The surface is emitted (not refused) but ``authoritative=False`` and + ``hedged=True``: the pack grounding stands on its textual provenance while + explicitly disclaiming the open geometric certification. Walk/compose folds + are suppressed — a hedge never accretes deterministic inference authority. + """ + base_surface, base_articulation, _authority = _base_runtime_surface( + canonical_surface=canonical_surface, + pre_decoration_surface=pre_decoration_surface, + response_surface=response_surface, + response_articulation_surface=response_articulation_surface, + ) + surface = ( + f"{_GROUNDED_OPEN_HEDGE_PREFIX} {base_surface}" if base_surface else base_surface + ) + articulation = ( + f"{_GROUNDED_OPEN_HEDGE_PREFIX} {base_articulation}" + if base_articulation + else base_articulation + ) + return SurfaceResolution( + surface=surface, + articulation_surface=articulation, + authority=_GROUNDED_OPEN_HEDGE_AUTHORITY, + fold_sources=(), + authoritative=False, + hedged=True, + refusal=None, + contract_violation=None, + proof_trace=None, + ) + + def resolve_surface( *, canonical_surface: str = "", @@ -149,6 +245,7 @@ def resolve_surface( compose_surface: str = "", proposition_graph: "PropositionGraph | None" = None, contract_assessment: "ContractAssessment | None" = None, + grounding_provenance: str = "", require_closed_geometry: bool = True, ) -> SurfaceResolution: """Resolve the final turn surface under dual-competing Shadow Coherence Gate. @@ -168,6 +265,14 @@ def resolve_surface( geometric contract yields a typed abstention — no runtime fluent answer is emitted as certified content. Walk/compose folds are suppressed on abstention paths. + + **Grounded-open hedge arm (T13 decision 2).** When the conjugate is open + but the surface is ``pack``/``teaching`` grounded (``grounding_provenance``) + and every open token is a known geometric-coherence residual, the pack + surface is served *honestly hedged* (``authoritative=False``, ``hedged=True``) + instead of hard-refused. The discriminator is purely structural (provenance + + residual allowlist) — never question typing — and fails closed: an + unrecognized open token or non-pack grounding takes the unchanged refusal. """ # --- Fail-closed: missing assessment never silently passes --- @@ -186,6 +291,21 @@ def resolve_surface( refusal=None, violation=contract_assessment_none_violation(), ) + # --- T13 decision (2): grounded-open hedge arm --- + # A curated pack/teaching surface whose ONLY open tokens are known + # geometric-coherence residuals is served honestly hedged + # (authoritative=False) rather than hard-refused: the pack surface never + # claimed geometric certification. Structural predicate only (grounding + # provenance + residual allowlist); any unrecognized token or non-pack + # grounding falls through to the unchanged fail-closed refusal below. + if _grounded_open_hedge_admissible(contract_assessment, grounding_provenance): + del gate_fired # hedge does not depend on the residual-failure flag + return _grounded_open_hedge_resolution( + canonical_surface=canonical_surface or "", + pre_decoration_surface=pre_decoration_surface or "", + response_surface=response_surface or "", + response_articulation_surface=response_articulation_surface or "", + ) refusal = open_geometry_refusal( missing_bindings=tuple(contract_assessment.missing_bindings), unresolved_hazards=tuple(contract_assessment.unresolved_hazards), diff --git a/docs/adr/ADR-0254-grounded-open-hedge-arm.md b/docs/adr/ADR-0254-grounded-open-hedge-arm.md new file mode 100644 index 00000000..7e48c440 --- /dev/null +++ b/docs/adr/ADR-0254-grounded-open-hedge-arm.md @@ -0,0 +1,55 @@ +# ADR-0254: Grounded-Open Hedge Arm for the Shadow Coherence Gate + +**Status:** Proposed (predicate pre-authorized by Joshua Shay, 2026-07-22 weekly-audit ruling T13 decision 2; ratify on PR #103 merge) +**Date:** 2026-07-23 +**Deciders:** Joshua Shay (ruling authority) + Claude (implementation) +**Companion docs:** [`ADR-0036-safety-refusal-policy.md`](ADR-0036-safety-refusal-policy.md), [`ADR-0037-per-predicate-ethics-refusal.md`](ADR-0037-per-predicate-ethics-refusal.md), [`ADR-0038-hedge-injection.md`](ADR-0038-hedge-injection.md), [`ADR-0252-problem-solving-paradigm-consolidation.md`](ADR-0252-problem-solving-paradigm-consolidation.md) +**Preserves (governing, unchanged):** `wrong=0`, refuse-don't-guess, decode-don't-generate, fail-closed (INV-34), no lexical cue-tables (ADR-0252). + +--- + +## 1. Context + +The dual-competing Shadow Coherence Gate (`core/cognition/surface_resolution.py::resolve_surface`, introduced PR #96) abstains — emits a typed `CoherenceRefusal` — whenever the conjugate competitor (geometric residual contract) is open. The conjugate is open when the field's `versor_condition ≥ 1e-6` (→ `missing_bindings`) or the GoldTether residual `R_GT > 1e-6` (→ `unresolved_hazards`), as built by `_geometry_contract_assessment`. + +The weekly audit (2026-07-22) surfaced an **over-refusal** in the `warmed_session_consistency` lane. Once the wave field is perturbed by a prior turn, a subsequent **pack-grounded** definitional surface — e.g. *"What is doubt?"* → `"To doubt means to think maybe not. pack-grounded (en_core_meta_v1)."` — hits an open `goldtether_residual` and is hard-refused with *"I cannot certify an answer: the geometric coherence contract is open (goldtether_residual)."* + +This is a false refusal. **The pack surface never claimed geometric certification.** It stands on its curated textual provenance; the open geometric residual says only that the *field* did not close, not that the *pack answer* is wrong. + +A tempting fix — a "definitional/epistemic" query-type classifier that bypasses the gate on a lexical cue — is **rejected**: it fails a geometric coherence gate open on a surface cue (fail-open cue-table; violates ADR-0252 and INV-34). + +## 2. Decision + +Route open-geometry-but-**pack-grounded** surfaces to a **hedge arm** — serve the pack surface non-authoritatively (`authoritative=False`) — instead of hard-refusing, discriminated **purely by grounding provenance** (structural), never by question type. + +**Authorized predicate:** + +``` +open_conjugate ∧ pack_grounded ∧ ¬hazard → hedge_arm +``` + +Realized structurally inside the gate (single source of truth): + +- **`pack_grounded`** — `grounding_provenance ∈ {pack, teaching}` (mirrors the existing `chat.runtime` "grounded" test). `vault`/`oov`/`partial`/`none` never hedge. +- **`¬hazard`** — enforced by a **residual allowlist**, not question typing. Hedge iff *every* open token ∈ `{versor_condition, goldtether_residual}` (geometric-coherence residuals the pack surface never certified). **Any** unrecognized open token — where a genuine safety/harm/imperative hazard would land — fails the allowlist and takes the unchanged hard refusal. `missing_wave_field` is deliberately excluded (absence of any field is a harder failure than an open-but-computed residual). + +The hedge outcome: the pack surface with a deterministic `"Grounded but not geometrically certified —"` marker, `authoritative=False`, `hedged=True`, `authority="grounded_open_hedge"`. Walk/compose folds are suppressed — a hedge never accretes deterministic inference authority. + +## 3. Why this is fail-closed, not fail-open + +- The **only** `ContractAssessment` reaching `resolve_surface` is the shadow-gate geometry assessment (`pipeline.py:416`), whose tokens are ⊆ `{versor_condition, missing_wave_field}` (bindings) ∪ `{goldtether_residual}` (hazards). Genuine safety/harm/imperative hazards ride **separate axes** — `SafetyVerdict` → typed refusal (ADR-0036) and the logos-morph override (`pipeline.py:513`) — both of which supersede this surface downstream. The hedge cannot leak a harmful surface: safety and morph refusals still win. +- The predicate is **structural** (provenance + a closed allowlist of geometric tokens). It reads no question text. An unknown token defaults to refusal (INV-34). +- `require_closed_geometry` callers that omit `grounding_provenance` get the **historical hard refusal** unchanged (no accidental hedge). + +## 4. Consequences + +- The over-refusal is fixed: the warmed *"What is doubt?"* case now serves the hedged pack definition (verified end-to-end). +- New auditable authority tag `grounded_open_hedge` flows to `TurnEvent.authority_source`; audit distinguishes hedge from both certified answers and refusals. +- **Not conflated with ADR-0038.** The ADR-0038 hedge is *ethics-commitment-driven* (`hedge_commitments` + manifold `preferred_hedge_soft`); this hedge is *coherence-geometry-driven*. They are independent mechanisms. Unifying the grounded-open marker with the manifold's `preferred_hedge_soft` vocabulary is a **deferred follow-up** (defer substrate-vocab commitment) and intentionally out of scope here. +- `authoritative=False` lives in the gate; downstream observes it as `authority_source="grounded_open_hedge"` (consistent with how refusals already surface — no new result plumbing). + +## 5. Validation + +- **Unit (RED→GREEN):** `tests/test_grounded_open_hedge_arm.py` — hedge activates on pack + geometric-residual openness; fails closed on non-pack grounding, unknown/mixed hazard tokens, `missing_wave_field`, and `None` assessment; closed geometry stays authoritative; omitted provenance stays refusing. +- **Real-data (GSM8K-style):** the warmed mixed-intent sequence exercises the arm on real field dynamics; the `warmed_session_consistency` lane never hard-refuses a pack surface, with `telemetry_consistency_rate` and `no_placeholder_rate` held at 1.0. +- **Regression:** architectural invariants (incl. INV-34) green; **GSM8K `wrong=0` preserved** (the closed-geometry certified math path is untouched). diff --git a/docs/analysis/weekly-audit-2026-07-22-stragglers-todo.md b/docs/analysis/weekly-audit-2026-07-22-stragglers-todo.md index 0e575278..a2e7b4f2 100644 --- a/docs/analysis/weekly-audit-2026-07-22-stragglers-todo.md +++ b/docs/analysis/weekly-audit-2026-07-22-stragglers-todo.md @@ -225,15 +225,21 @@ fail-closed determinism). Execution status: the trace-hash back-stamp. Lane green: `telemetry_consistency_rate` 0.9444 → 1.0 (10/10 warmed_session tests pass). `trace_hash` byte-identity preserved — it folds the pre-decoration surface, not the served surface. - - **(2) RULED (Shay, 2026-07-22): hedge-arm routing, own PR.** The query-type - "definitional/epistemic" classifier bypass is REJECTED — it fails a - geometric coherence gate OPEN on a lexical cue (fail-open / cue-table; - ADR-0252 + INV-34). Ruling: fix the READING, not the question — route - open-geometry-but-**pack-grounded** surfaces to the existing hedge-injection - arm (`authoritative=False`, honestly hedged), discriminated by grounding - *provenance* (structural), never by question type. Its own dedicated PR + - GSM8K-style (real-data) validation. Scoped next; deliberately NOT in the - telemetry PR or the infra PR. + - **(2) RULED + IMPLEMENTED (PR #103, `feat/grounded-open-geometry-hedge-arm`, ADR-0254).** + Ruling: fix the READING, not the question — route open-geometry-but-**pack-grounded** + surfaces to a hedge arm (`authoritative=False`, honestly hedged), discriminated + by grounding *provenance* (structural), never by question type (the query-type + classifier bypass was REJECTED as a fail-open cue-table; ADR-0252 + INV-34). + Built: the decision lives **inside the gate** (`resolve_surface`) — predicate + `pack_grounded ∧ every open token ∈ {versor_condition, goldtether_residual}`. + `¬hazard` is enforced structurally by that residual allowlist: any unrecognized + open token (where a real safety/harm hazard lands), non-pack grounding, or + `None` assessment → unchanged hard refusal. New authority tag + `grounded_open_hedge`. Validation: RED→GREEN unit suite + + `warmed_session_consistency` real-data (the "What is doubt?" over-refusal now + hedges) + GSM8K `wrong=0` preserved + architectural invariants green. + **Deferred:** unifying the hedge marker with the ADR-0038 manifold + `preferred_hedge_soft` vocabulary (defer substrate-vocab commitment). - **(3) RULED (Shay, 2026-07-22): local pre-push gate — IMPLEMENTED.** `scripts/hooks/pre-push` + `scripts/hooks/install.sh` (`core.hooksPath`) run the smoke suite **plus** the warmed_session lane pin on every push — the diff --git a/tests/test_grounded_open_hedge_arm.py b/tests/test_grounded_open_hedge_arm.py new file mode 100644 index 00000000..1fbdebb9 --- /dev/null +++ b/tests/test_grounded_open_hedge_arm.py @@ -0,0 +1,264 @@ +"""RED→GREEN — T13 decision (2): the grounded-open hedge arm. + +Weekly-audit 2026-07-22 ruling (Shay): route open-geometry-but-**pack-grounded** +surfaces to the hedge arm (``authoritative=False``), discriminated *purely by +grounding provenance* (structural) plus a geometric-residual allowlist — never +by question type (a lexical cue-table bypass would be a fail-open ADR-0252 / +INV-34 violation). + +Predicate (authorized): + open_conjugate ∧ pack_grounded ∧ ¬hazard → hedge_arm + +``¬hazard`` is enforced *structurally*: the only assessment that reaches +``resolve_surface`` is the shadow-coherence-gate geometry assessment, whose open +tokens are ⊆ {``versor_condition``, ``goldtether_residual``, ``missing_wave_field``}. +We hedge iff every open token is a *known geometric-coherence residual* that the +pack surface never claimed to certify. ANY unrecognized token — where a genuine +safety/harm/imperative hazard would land — fails the allowlist and hard-refuses. +Genuine harm/imperative content is refused on the separate SafetyVerdict / +logos-morph axes that supersede this surface downstream. + +Anchor: the "What is doubt?" over-refusal — pack surface (``en_core_meta_v1``), +open ``goldtether_residual`` — was hard-refused; it must now hedge. +""" + +from __future__ import annotations + +import pytest + +from core.cognition.surface_resolution import resolve_surface +from generate.problem_frame_contracts import ContractAssessment + +_PACK_SURFACE = "To doubt means to think maybe not. pack-grounded (en_core_meta_v1)." + + +def _open_goldtether_assessment() -> ContractAssessment: + """Mirror ``_geometry_contract_assessment`` when R_GoldTether > 1e-6. + + goldtether_residual lands in ``unresolved_hazards`` (it is a geometric + residual token, not a safety hazard); versor closed → no missing binding. + This is the exact shape behind the "What is doubt?" refusal. + """ + return ContractAssessment( + candidate_organ="shadow_coherence_gate", + missing_bindings=(), + unresolved_hazards=("goldtether_residual",), + runnable=False, + explanation="versor_condition=0.000e+00; R_GoldTether=3.210e-04", + ) + + +class TestHedgeArmActivates: + def test_pack_grounded_open_goldtether_hedges_not_refuses(self) -> None: + res = resolve_surface( + canonical_surface=_PACK_SURFACE, + response_surface=_PACK_SURFACE, + contract_assessment=_open_goldtether_assessment(), + grounding_provenance="pack", + require_closed_geometry=True, + ) + # Hedge arm: served, but never claims geometric certification. + assert res.authoritative is False + assert res.hedged is True + assert res.refusal is None + assert res.contract_violation is None + assert res.authority == "grounded_open_hedge" + # The pack content is served (hedged), not the typed refusal message. + assert _PACK_SURFACE in res.surface + assert "cannot certify" not in res.surface.casefold() + + def test_versor_condition_open_also_hedges_when_grounded(self) -> None: + assessment = ContractAssessment( + candidate_organ="shadow_coherence_gate", + missing_bindings=("versor_condition",), + unresolved_hazards=(), + runnable=False, + explanation="versor open", + ) + res = resolve_surface( + canonical_surface=_PACK_SURFACE, + response_surface=_PACK_SURFACE, + contract_assessment=assessment, + grounding_provenance="teaching", # teaching provenance is also grounded + require_closed_geometry=True, + ) + assert res.hedged is True + assert res.refusal is None + assert res.authoritative is False + + +class TestFailClosedGuardrails: + @pytest.mark.parametrize("provenance", ["none", "oov", "vault", "partial", ""]) + def test_non_pack_grounding_still_hard_refuses(self, provenance: str) -> None: + res = resolve_surface( + canonical_surface=_PACK_SURFACE, + response_surface=_PACK_SURFACE, + contract_assessment=_open_goldtether_assessment(), + grounding_provenance=provenance, + require_closed_geometry=True, + ) + assert res.authoritative is False + assert res.hedged is False + assert res.refusal is not None # ungrounded open geometry never hedges + + def test_unknown_open_token_fails_closed_even_when_pack_grounded(self) -> None: + # A genuine hazard token (outside the geometric-residual allowlist) must + # NEVER be hedged, even with pack provenance. This is the ¬hazard floor. + assessment = ContractAssessment( + candidate_organ="shadow_coherence_gate", + missing_bindings=(), + unresolved_hazards=("harm_imperative",), + runnable=False, + explanation="genuine hazard", + ) + res = resolve_surface( + canonical_surface=_PACK_SURFACE, + response_surface=_PACK_SURFACE, + contract_assessment=assessment, + grounding_provenance="pack", + require_closed_geometry=True, + ) + assert res.hedged is False + assert res.refusal is not None + + def test_mixed_known_and_unknown_token_fails_closed(self) -> None: + # Even one unrecognized token among geometric residuals → refuse. + assessment = ContractAssessment( + candidate_organ="shadow_coherence_gate", + missing_bindings=("versor_condition",), + unresolved_hazards=("goldtether_residual", "harm_imperative"), + runnable=False, + explanation="mixed", + ) + res = resolve_surface( + canonical_surface=_PACK_SURFACE, + response_surface=_PACK_SURFACE, + contract_assessment=assessment, + grounding_provenance="pack", + require_closed_geometry=True, + ) + assert res.hedged is False + assert res.refusal is not None + + def test_missing_wave_field_not_hedgeable(self) -> None: + # No field at all is a harder failure than an open residual — fail closed. + assessment = ContractAssessment( + candidate_organ="shadow_coherence_gate", + missing_bindings=("missing_wave_field",), + unresolved_hazards=(), + runnable=False, + explanation="no field versor available for geometric contract", + ) + res = resolve_surface( + canonical_surface=_PACK_SURFACE, + response_surface=_PACK_SURFACE, + contract_assessment=assessment, + grounding_provenance="pack", + require_closed_geometry=True, + ) + assert res.hedged is False + assert res.refusal is not None + + def test_none_assessment_still_contract_violation(self) -> None: + res = resolve_surface( + canonical_surface=_PACK_SURFACE, + response_surface=_PACK_SURFACE, + contract_assessment=None, + grounding_provenance="pack", + require_closed_geometry=True, + ) + assert res.hedged is False + assert res.contract_violation is not None + assert res.refusal is None + + +class TestClosedGeometryUnaffected: + def test_closed_geometry_pack_stays_authoritative(self) -> None: + assessment = ContractAssessment( + candidate_organ="shadow_coherence_gate", + missing_bindings=(), + unresolved_hazards=(), + runnable=True, + explanation="versor_condition=0.000e+00; R_GoldTether=0.000e+00", + ) + res = resolve_surface( + canonical_surface=_PACK_SURFACE, + response_surface=_PACK_SURFACE, + contract_assessment=assessment, + grounding_provenance="pack", + require_closed_geometry=True, + ) + assert res.authoritative is True + assert res.hedged is False + assert res.refusal is None + + def test_grounding_provenance_defaults_to_no_hedge(self) -> None: + # Back-compat: callers that do not pass grounding_provenance must get + # the historical hard-refusal on open geometry (no accidental hedge). + res = resolve_surface( + canonical_surface=_PACK_SURFACE, + response_surface=_PACK_SURFACE, + contract_assessment=_open_goldtether_assessment(), + require_closed_geometry=True, + ) + assert res.hedged is False + assert res.refusal is not None + + +# --- coherence refusal marker (distinct from the safety TYPED_REFUSAL_PREFIX) --- +_COHERENCE_REFUSAL_MARKER = "cannot certify" + + +class TestRealDataHedgeArm: + """GSM8K-style real-data validation on a warmed ``ChatRuntime``. + + The warmed 'What is doubt?' over-refusal (open ``goldtether_residual`` on a + pack surface once the field is perturbed by a prior turn) is the exact T13 + case. It must now HEDGE — serve the pack definition non-authoritatively — + never hard-refuse. + """ + + @staticmethod + def _warmed_mixed_run() -> list: + from chat.runtime import ChatRuntime + from core.cognition.pipeline import CognitiveTurnPipeline + + runtime = ChatRuntime() + pipeline = CognitiveTurnPipeline(runtime=runtime) + # Deterministic (CORE has no RNG): perturbing the field with a prior + # pack turn opens the goldtether residual on the next pack turn. + sequence = ["What is truth?", "What is doubt?", "Define moment."] + return [pipeline.run(prompt, max_tokens=8) for prompt in sequence] + + def test_open_geometry_pack_surface_hedges_not_refuses(self) -> None: + results = self._warmed_mixed_run() + # No pack-grounded turn in the perturbed sequence is hard-refused. + for res in results: + assert _COHERENCE_REFUSAL_MARKER not in res.surface.casefold(), res.surface + # The hedge arm is genuinely exercised on real field dynamics. + authorities = [getattr(res, "authority_source", "") for res in results] + assert "grounded_open_hedge" in authorities, authorities + # The hedged turn still serves the pack definition content. + hedged = next( + res + for res in results + if getattr(res, "authority_source", "") == "grounded_open_hedge" + ) + assert "not geometrically certified" in hedged.surface.casefold() + + def test_warmed_lane_never_hard_refuses_a_pack_surface(self) -> None: + from evals.framework import get_lane, run_lane + + result = run_lane( + get_lane("warmed_session_consistency"), version="v1", split="public" + ) + for case in result.case_details: + for turn in case["turns"]: + assert _COHERENCE_REFUSAL_MARKER not in turn["surface"].casefold(), ( + case["case_id"], + turn["turn_index"], + turn["surface"], + ) + # The telemetry + placeholder floors from PR #101 must not regress. + assert result.metrics["telemetry_consistency_rate"] == 1.0 + assert result.metrics["no_placeholder_rate"] == 1.0