core/chat/narrative_surface.py
Shay 6207b5fd0e feat(register): R1–R4 register pack subsystem — deterministic surface variation
Introduces the presentation axis as a fourth pack class (sibling to identity /
safety / ethics), orthogonal to the truth path. Same input + same packs +
same register ⇒ bit-for-bit reproducible surface; varying any of the three ⇒
genuinely different output. No stochastic sampling.

ADR-0068 (R1): RegisterPack frozen dataclass, loader, ratify script, seam test.
  - default_neutral_v1 ratified as null register.

ADR-0069 (R2): realizer register parameter threaded through 9 composer entry
  points; RuntimeConfig.register_pack_id; three byte-identity invariants
  (A: None ≡ pre-R2 unregistered; B: None ≡ default_neutral_v1; C: trace_hash
  invariant under register). Amended to default-with-lint after 167-call-site
  scout: composers default to UNREGISTERED, AST lint enforces explicit
  register= at runtime call sites.

ADR-0070 (R3): terse_v1 register, first non-neutral pack. realizer_overrides
  schema with known-keys allow-list (disclosure_domain_count ∈ {1,2,3}).
  build_pack_surface_candidate reads override with fail-soft clamp. New
  invariant register_invariant_grounding asserts grounding_source +
  trace_hash byte-identical across {None, neutral, terse}.

ADR-0071 (R4): seeded surface variation via convivial_v1.
  chat/register_variation.py applies SHA-256-seeded marker selection from
  bounded discourse-marker buckets. ChatResponse.pre_decoration_surface routes
  truth-path surface to core/cognition/pipeline.py so trace_hash stays
  invariant under register (the load-bearing architectural fix — initially
  invariant C failed under convivial because decoration was leaking into
  trace_hash via response.surface). Empty-string marker entries now
  legitimate ("no marker this turn" is a valid seed pick). realizer_overrides
  schema widened with per_intent nested block (validated against IntentTag
  whitelist; wired but not exercised by convivial). Two new invariants:
  seeded_variation_replay_equivalence (fresh runtimes → byte-identical) and
  seeded_variation_turn_distinct (same prompt across turns → ≥2 distinct
  surfaces).

ADR-0072 (R5, draft): telemetry + operator surface — TurnEvent gains
  register_id and register_variant_id, core chat --register flag, core demo
  register-tour. Status: Proposed; not yet implemented.

Three ratified register packs ship: default_neutral_v1 (null), terse_v1
(disclosure_domain_count=1), convivial_v1 (3 openings × 3 closings).

Verification:
  - 84 register tests pass + 1 documented skip
  - Curated lanes green: smoke 67, cognition 120+1s, teaching 17, packs 6,
    runtime 19, algebra 132
  - Cognition eval byte-identical to pre-register baseline:
    public 100/100/91.7/100, holdout 100/100/83.3/100
  - Full lane: 2608 passed, 4 skipped, 1 failed (pre-existing
    test_cli_demo.py "Combined Demo" → "Run Every Demo" rename, unrelated)

Truth-path isolation: chat/register_variation.py is realizer-side; the seam
test (tests/test_register_pack_seam.py) refuses imports of packs.register
from intent classification, propagation, vault recall, trace hashing, and
algebra.
2026-05-19 16:52:36 -07:00

184 lines
6.9 KiB
Python

"""chat/narrative_surface.py — Phase 3.3: NARRATIVE intent composer.
When a prompt classifies as NARRATIVE — "Tell me about X", "Describe
X", "What can you say about X" — the composer walks every reviewed
chain rooted on X across every registered teaching corpus and emits
a multi-clause surface that surfaces *everything* the system has
reviewed about X.
Sibling to:
- :func:`chat.teaching_grounding.teaching_grounded_surface` —
surfaces ONE chain rooted on X for a specific intent.
- :func:`chat.teaching_grounding.teaching_grounded_surface_composed`
— extends one chain with a follow-up (depth-1 chain-of-chains).
- :func:`chat.pack_grounding.pack_grounded_surface` — surfaces X's
pack semantic_domains.
Whereas those composers pick one chain or one extension, NARRATIVE
aggregates *every distinct (predicate, object) clause* rooted on X
across both cause and verification intents. Surface format:
"{X} — narrative-grounded ({corpus_ids}): {dX1}; {dX2}.
{X} {conn1} {O1} ({dO1}); {X} {conn2} {O2} ({dO2}); ...
No session evidence yet."
Design constraints (matching ADR-0052..0065 doctrine):
- **No content synthesis.** Every visible non-template token is
either the lemma X, a verbatim pack ``semantic_domains`` atom, a
reviewed chain object lemma, or a fixed connective from
``humanize_predicate``.
- **Deterministic ordering.** Clauses sort by (intent_name,
connective, object) so identical corpus state always produces
the identical surface.
- **Dedup by (connective, object).** When cause and verification
carry the same predicate + object, only one clause is emitted —
the dual-tag is implicit in the chain provenance and adding both
reads as noise to the user.
- **Pack-internal.** Chains are loaded from the cross-corpus
aggregator (:func:`_all_chains_index`); each chain's object
domains are read from its bound pack via
:func:`_pack_for_corpus`.
- **Bounded clause count.** Default ``max_clauses=4`` to keep the
surface readable. Operators can raise the cap for analytic
workloads.
Returns ``None`` when no chain references X as subject — caller
falls through to the pack-grounded surface (DEFINITION-like
narrative) or to the OOV invitation if X is also not pack-resident.
"""
from __future__ import annotations
from chat.cross_pack_grounding import cross_pack_chains_for_subject
from chat.pack_resolver import _pack_lexicon_for, resolve_lemma
from packs.register.loader import RegisterPack, UNREGISTERED
from chat.teaching_grounding import (
_all_chains_index,
_pack_for_corpus,
)
from generate.semantic_templates import humanize_predicate
def _object_domains_for_chain(chain) -> tuple[str, ...]:
"""Return the object lemma's semantic domains for *chain*.
Handles both in-pack ``TeachingChain`` (residency via its bound
corpus pack) and cross-pack ``CrossPackChain`` (residency in
its declared ``object_pack_id``).
"""
object_pack_id = getattr(chain, "object_pack_id", None)
if object_pack_id:
return _pack_lexicon_for(object_pack_id).get(chain.object, ())
return _pack_for_corpus(chain.corpus_id).get(chain.object, ())
def _subject_domains_for_chain(chain) -> tuple[str, ...]:
"""Same as :func:`_object_domains_for_chain` but for the subject."""
subject_pack_id = getattr(chain, "subject_pack_id", None)
if subject_pack_id:
return _pack_lexicon_for(subject_pack_id).get(chain.subject, ())
return _pack_for_corpus(chain.corpus_id).get(chain.subject, ())
def narrative_grounded_surface(
subject_lemma: str,
*,
max_clauses: int = 4,
register: RegisterPack = UNREGISTERED,
) -> str | None:
"""Return a deterministic NARRATIVE-tier surface, or ``None``.
Aggregates every reviewed chain whose subject equals *subject_lemma*
across all registered teaching corpora. Dedups by (connective,
object). Sorts clauses lexicographically for replay stability.
``max_clauses`` caps the emitted clause count. Default 4 reads
smoothly; operators can raise for analytic workloads.
Returns ``None`` when no chain references *subject_lemma* — the
caller routes through pack-grounded DEFINITION (or OOV if the
lemma is unknown).
"""
if not subject_lemma or not isinstance(subject_lemma, str):
return None
key = subject_lemma.strip().lower()
if not key:
return None
if max_clauses < 1:
return None
index = _all_chains_index()
matching: list = [
chain for (s, _), chain in index.items() if s == key
]
# ADR-0067 — merge cross-pack chains rooted on the same subject.
# In-pack chains take precedence on (intent, connective, object)
# collision (first-occurrence-wins in dedup loop below).
matching.extend(cross_pack_chains_for_subject(key))
if not matching:
return None
# Dedup by (connective, object) — verification and cause carrying
# the same predicate produce one clause, not two. Stable sort
# by (intent, connective, object) so replay produces byte-identical
# output.
seen: set[tuple[str, str]] = set()
deduped: list = []
for chain in sorted(
matching, key=lambda c: (c.intent, c.connective, c.object),
):
sig = (chain.connective, chain.object)
if sig in seen:
continue
seen.add(sig)
deduped.append(chain)
if len(deduped) >= max_clauses:
break
# Subject domains: take from the first chain's bound pack so the
# narrative header is sourced from the lemma's own pack — even
# when the matching chains span multiple corpora.
first = deduped[0]
subject_domains = _subject_domains_for_chain(first)
if not subject_domains:
# Fall back to cross-pack resolver — subject may live in a
# different pack than its chains' corpus binding (defensive).
resolved = resolve_lemma(first.subject)
if resolved is None:
return None
subject_domains = resolved[1]
head_subject = "; ".join(
subject_domains[: max(1, first.domains_subject_k)]
)
# Collect involved corpora for the tag.
corpora = tuple(sorted({c.corpus_id for c in deduped}))
corpora_tag = corpora[0] if len(corpora) == 1 else " + ".join(corpora)
# Emit one clause per deduped chain.
clauses: list[str] = []
for chain in deduped:
obj_domains = _object_domains_for_chain(chain)
if not obj_domains:
continue
obj_head = "; ".join(
obj_domains[: max(1, chain.domains_object_k)]
)
connective = humanize_predicate(chain.connective)
clauses.append(
f"{chain.subject} {connective} {chain.object} ({obj_head})"
)
if not clauses:
return None
return (
f"{first.subject} — narrative-grounded ({corpora_tag}): "
f"{head_subject}. {'; '.join(clauses)}. "
f"No session evidence yet."
)
__all__ = ["narrative_grounded_surface"]