Distill the evidence ledger to twelve records through one compaction path
44 records to 12: supersession chains collapsed to their survivors, per-commit receipts folded into the decision they served, standing pre-rebuild decisions merged into one record map.yaml already owns. Every removed record is archived verbatim. `compact-evidence` is now the one durable deletion path; the status tool reads folded scalars, and the compaction helper imports the map readers instead of carrying copies. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
e211075969
commit
18f2eb96a2
8 changed files with 139 additions and 118 deletions
|
|
@ -8,6 +8,8 @@ from dataclasses import dataclass
|
|||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from ghostlight_state import extract_active_subgoals, extract_map_field
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
STATE_DIR = ROOT / "state"
|
||||
|
|
@ -45,31 +47,6 @@ def run_git(args: list[str]) -> str:
|
|||
return completed.stdout.strip()
|
||||
|
||||
|
||||
def extract_map_field(name: str) -> str | None:
|
||||
prefix = f" {name}:"
|
||||
for line in read_text(MAP_PATH).splitlines():
|
||||
if line.startswith(prefix):
|
||||
return line.split(":", 1)[1].strip()
|
||||
return None
|
||||
|
||||
|
||||
def extract_active_subgoals() -> list[str]:
|
||||
lines = read_text(MAP_PATH).splitlines()
|
||||
results: list[str] = []
|
||||
in_section = False
|
||||
for line in lines:
|
||||
if not in_section:
|
||||
if line.strip() == "active_subgoals:":
|
||||
in_section = True
|
||||
continue
|
||||
if line.startswith(" - "):
|
||||
results.append(line[4:].strip())
|
||||
continue
|
||||
if line and not line.startswith(" "):
|
||||
break
|
||||
return results
|
||||
|
||||
|
||||
def current_scratch_subgoal() -> str | None:
|
||||
lines = read_text(SCRATCH_PATH).splitlines()
|
||||
for index, line in enumerate(lines):
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ from ghostlight_state_store import (
|
|||
append_evidence,
|
||||
load_branches,
|
||||
migrate_legacy_evidence,
|
||||
replace_evidence,
|
||||
save_branches,
|
||||
)
|
||||
|
||||
|
|
@ -35,10 +36,22 @@ def utc_stamp() -> str:
|
|||
|
||||
|
||||
def extract_map_field(name: str) -> str | None:
|
||||
"""Read one ` name:` field under `current_status`, plain or block scalar."""
|
||||
prefix = f" {name}:"
|
||||
for line in read_text(MAP_PATH).splitlines():
|
||||
if line.startswith(prefix):
|
||||
return line.split(":", 1)[1].strip()
|
||||
lines = read_text(MAP_PATH).splitlines()
|
||||
for index, line in enumerate(lines):
|
||||
if not line.startswith(prefix):
|
||||
continue
|
||||
value = line.split(":", 1)[1].strip()
|
||||
if value not in {">", "|", ">-", "|-"}:
|
||||
return value
|
||||
block: list[str] = []
|
||||
for continuation in lines[index + 1 :]:
|
||||
if continuation.strip() and not continuation.startswith(" "):
|
||||
break
|
||||
block.append(continuation.strip())
|
||||
joiner = "\n" if value.startswith("|") else " "
|
||||
return joiner.join(part for part in block if part).strip() or None
|
||||
return None
|
||||
|
||||
|
||||
|
|
@ -112,6 +125,23 @@ def cmd_add_evidence(args: argparse.Namespace) -> int:
|
|||
return 0
|
||||
|
||||
|
||||
def cmd_compact_evidence(args: argparse.Namespace) -> int:
|
||||
records: list[dict[str, Any]] = []
|
||||
for number, line in enumerate(read_text(Path(args.replacement)).splitlines(), 1):
|
||||
if not line.strip():
|
||||
continue
|
||||
record = json.loads(line)
|
||||
missing = {"ts", "type", "status", "note"} - set(record)
|
||||
if not isinstance(record, dict) or missing:
|
||||
raise SystemExit(f"replacement line {number} lacks {sorted(missing)}")
|
||||
records.append(record)
|
||||
if not records:
|
||||
raise SystemExit("refusing to compact the ledger to nothing")
|
||||
archived, kept = replace_evidence(records)
|
||||
print(f"Archived {archived} record(s) to state/evidence.archive.jsonl; ledger now holds {kept}.")
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_migrate_evidence(_: argparse.Namespace) -> int:
|
||||
imported = migrate_legacy_evidence()
|
||||
print(f"Imported {imported} legacy evidence record(s) into CultCache.")
|
||||
|
|
@ -169,6 +199,13 @@ def build_parser() -> argparse.ArgumentParser:
|
|||
evidence_parser.add_argument("--branch")
|
||||
evidence_parser.set_defaults(func=cmd_add_evidence)
|
||||
|
||||
compact_parser = subparsers.add_parser(
|
||||
"compact-evidence",
|
||||
help="Replace the evidence document with a distilled JSONL file; every current record is archived first.",
|
||||
)
|
||||
compact_parser.add_argument("--replacement", required=True)
|
||||
compact_parser.set_defaults(func=cmd_compact_evidence)
|
||||
|
||||
migrate_evidence_parser = subparsers.add_parser(
|
||||
"migrate-evidence",
|
||||
help="Import legacy JSONL evidence missing from the CultCache authority.",
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ VENDOR_SRC = ROOT / "vendor" / "cultcache-py" / "src"
|
|||
CACHE_PATH = STATE_DIR / "ghostlight-state.cultcache.jsonl"
|
||||
BRANCHES_PATH = STATE_DIR / "branches.json"
|
||||
EVIDENCE_PATH = STATE_DIR / "evidence.jsonl"
|
||||
ARCHIVE_PATH = STATE_DIR / "evidence.archive.jsonl"
|
||||
|
||||
if str(VENDOR_SRC) not in sys.path:
|
||||
sys.path.insert(0, str(VENDOR_SRC))
|
||||
|
|
@ -100,6 +101,28 @@ def append_evidence(record: dict[str, Any]) -> None:
|
|||
_export_evidence(cache)
|
||||
|
||||
|
||||
def replace_evidence(records: list[dict[str, Any]]) -> tuple[int, int]:
|
||||
"""Replace the whole evidence document with distilled records.
|
||||
|
||||
Every record currently in the store is appended verbatim to
|
||||
``state/evidence.archive.jsonl`` before deletion, so the archive stays
|
||||
the complete history and the export carries only what still steers.
|
||||
Returns (archived, kept).
|
||||
"""
|
||||
cache = open_state_cache()
|
||||
snapshot = cache.snapshot().get(EVIDENCE_DOC.type, {})
|
||||
archived = sorted(snapshot.values(), key=lambda record: record.get("ts", ""))
|
||||
with ARCHIVE_PATH.open("a", encoding="utf-8") as archive:
|
||||
for record in archived:
|
||||
archive.write(json.dumps(record, ensure_ascii=True) + "\n")
|
||||
for key in list(snapshot):
|
||||
cache.delete(EVIDENCE_DOC, key)
|
||||
for index, record in enumerate(records):
|
||||
cache.put(EVIDENCE_DOC, _evidence_key(record, index), record)
|
||||
_export_evidence(cache)
|
||||
return len(archived), len(records)
|
||||
|
||||
|
||||
def migrate_legacy_evidence() -> int:
|
||||
"""Import legacy JSONL records that predate the CultCache authority cut."""
|
||||
cache = open_state_cache()
|
||||
|
|
|
|||
Loading…
Reference in a new issue