feat(learning): S9 — D7 læringssløyfe: verdict-inbox, fail-closed promoteringsgate, artefakt-sourced persona
- inbox.py (§4.2+§5): VerdictDocument med verbatim-id-regel; write_verdict
authoring-primitiv (deterministisk JSON); load_inbox tolerant (skip, aldri
raise; sortert på filnavn); merge_inbox_into_store first-write-wins,
idempotent, skriver aldri (rolle-splitt §3 steg 7)
- promotion.py (§6): promote fail-closed mot {approved,
approved_with_adjustment}; eksplisitt påkrevd timestamp; minimal frontmatter
(rationale → description, aldri strukturerte læringsfelt); path-safe token
med content-hash-fallback; idempotent index-lenking med fast nøytral label
- persona.py (§4.3): load_persona_example fail-fast (run-path-vokabular,
marker ⊆ rationale); drop_persona_verdict artefakt-sourced ved kalltid mot
delt shared/-artefakt
- experience.py (kirurgisk): seeding leser verdict_id VERBATIM fra frontmatter
— re-minting ville kollidert distinkte promoterte kandidater
- 43 nye load-bearing tester (step7/step8/persona), 164/164 uten API-nøkkel;
to-runs-bevis med fersk store + tom-inbox-kontroll; fire detach-bevis kjørt
røde og revertert grønne
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QdSfQdND84oeq2mbjueLTS
This commit is contained in:
parent
9a4caeb419
commit
22bfc80dda
7 changed files with 947 additions and 2 deletions
103
src/portfolio_optimiser_claude/promotion.py
Normal file
103
src/portfolio_optimiser_claude/promotion.py
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
"""The promotion gate (method-spec §3 Step 8, §6) — an opt-in PUBLIC primitive.
|
||||
|
||||
``promote`` lifts one APPROVED verdict from the raw output layer into the OKF
|
||||
context layer as a navigable ``type: verdict`` concept file. It is NEVER wired
|
||||
into the run itself: the system reads context; the gate/persona promotes.
|
||||
Fail-closed — only human/persona-approved knowledge enters the wiki, never raw
|
||||
agent output (self-contamination). The index link label is FIXED and neutral:
|
||||
the index body flows verbatim into the rendered read-context, so a descriptive
|
||||
label would leak the learning signal around the gated fold.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from portfolio_optimiser_claude.inbox import VerdictDocument
|
||||
|
||||
# §6: the gate's accepted set — approved_with_adjustment exists ONLY here and
|
||||
# in bundle-seed frontmatter, never on the run path (§4).
|
||||
_ACCEPTED_DECISIONS = frozenset({"approved", "approved_with_adjustment"})
|
||||
_PROMOTED_PREFIX = "promoted-verdict-"
|
||||
# §6: FIXED neutral label — carries NO verdict signal.
|
||||
_NEUTRAL_LABEL = "Promotert ekspert-dom"
|
||||
_TOKEN_UNSAFE = re.compile(r"[^A-Za-z0-9._-]")
|
||||
_INDEX_FILENAME = "index.md"
|
||||
|
||||
|
||||
class PromotionError(ValueError):
|
||||
"""A verdict was refused at the gate (§6) — nothing written, nothing linked."""
|
||||
|
||||
|
||||
def _filename_token(verdict_id: str) -> str:
|
||||
# Path-safe, fail-closed against escaping names: sanitise to the safe
|
||||
# alphabet; a degenerate token falls back to a content hash.
|
||||
token = _TOKEN_UNSAFE.sub("", verdict_id)
|
||||
if not token.strip("."):
|
||||
token = hashlib.sha256(verdict_id.encode("utf-8")).hexdigest()[:16]
|
||||
return token
|
||||
|
||||
|
||||
def _frontmatter_text(value: str) -> str:
|
||||
# The frontmatter parser is line-oriented — prose must stay on one line.
|
||||
return " ".join(value.split())
|
||||
|
||||
|
||||
def promote(
|
||||
verdict: VerdictDocument,
|
||||
bundle_dir: Path,
|
||||
*,
|
||||
approved_by: str,
|
||||
experiment: str,
|
||||
timestamp: str,
|
||||
) -> Path:
|
||||
"""Promote one approved verdict into the bundle — fail-closed, idempotent link.
|
||||
|
||||
``timestamp`` is an explicit REQUIRED argument (no wall-clock default), so
|
||||
promotion is deterministic and reproducible. The promoted file is minimal:
|
||||
the rationale becomes ``description`` (the learning signal as prose) — the
|
||||
structured learning fields of hand-authored seeds are never reproduced.
|
||||
Ids key on candidate features (§4.2), so re-approving the same candidate
|
||||
overwrites the same file (last-write-wins): one curated verdict file per
|
||||
distinct candidate, not one per verdict event.
|
||||
"""
|
||||
if verdict.decision not in _ACCEPTED_DECISIONS:
|
||||
raise PromotionError(
|
||||
f"promotion refused: decision {verdict.decision!r} is not in the "
|
||||
f"gate's accepted set {sorted(_ACCEPTED_DECISIONS)} (§6 fail-closed)"
|
||||
)
|
||||
filename = f"{_PROMOTED_PREFIX}{_filename_token(verdict.id)}.md"
|
||||
path = (bundle_dir / filename).resolve()
|
||||
if path.parent != bundle_dir.resolve():
|
||||
raise PromotionError(f"promotion refused: {filename!r} escapes the bundle")
|
||||
path.write_text(
|
||||
"---\n"
|
||||
"type: verdict\n"
|
||||
f"title: {_NEUTRAL_LABEL}\n"
|
||||
f"decision: {verdict.decision}\n"
|
||||
f"description: {_frontmatter_text(verdict.rationale)}\n"
|
||||
f"verdict_id: {verdict.id}\n"
|
||||
f"provenance: approved_by={approved_by}; experiment={experiment}; "
|
||||
f"timestamp={timestamp}\n"
|
||||
"tags: [verdict, promoted]\n"
|
||||
"---\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
_link_from_index(bundle_dir, filename)
|
||||
return path
|
||||
|
||||
|
||||
def _link_from_index(bundle_dir: Path, filename: str) -> None:
|
||||
# §6: navigation follows only index cross-links — an unlinked file is
|
||||
# unreachable. Idempotent: re-promotion never double-links. (reference
|
||||
# limitation: the read-modify-write is not atomic — single-process MVP.)
|
||||
index_path = bundle_dir / _INDEX_FILENAME
|
||||
index_text = index_path.read_text(encoding="utf-8")
|
||||
if f"({filename})" in index_text:
|
||||
return
|
||||
link_line = f"- [{_NEUTRAL_LABEL}]({filename})\n"
|
||||
if not index_text.endswith("\n"):
|
||||
index_text += "\n"
|
||||
index_path.write_text(index_text + link_line, encoding="utf-8")
|
||||
Loading…
Add table
Add a link
Reference in a new issue