feat(ledger): the ledger can say "these look wrong" without acting on it (#4208)
CI & Build / Python lint (push) Failing after 3s
CI & Build / Plugin hooks (push) Successful in 11s
CI & Build / TypeScript typecheck (push) Successful in 57s
CI & Build / integration (push) Successful in 1m6s
CI & Build / Python tests (push) Successful in 1m49s
CI & Build / Build & push image (push) Skipped

THE HALF THAT WAS MISSING. #4204 put a floor under what the write-path hook
may assert. A floor only guards new writes; every row already stored stands
(lesson #4202). Measured after that fix shipped: Portal carried 32 rows under
one canon and 3 under another, all stamped on scores of 0.69-0.77 — below the
0.80 floor, so none of them could be written today, and all of them were still
there. Scribe's own ledger carries 334 under #2860.

`stamps_to_review` reports two things and changes nothing:

  weak       — rows the hook stamped on a resemblance below the current floor,
               each with its score, signature and derived form.
  incoherent — canons whose own judged rows do not agree on a form. A canon
               claims some shapes are the same sort of thing; when its members
               are a class, three getters and a dozen tests, that claim has
               stopped being true and every base-rate reading built on it is
               reading noise. `canon_form` already made such a canon fall
               silent — nothing made it VISIBLE.

IT DELIBERATELY CANNOT FIX ANYTHING, and that is the design, not an omission.
The first version of this commit was an automatic sweep that reset rows by
score. That is the original defect pointed the other way: what harmed the
ledger was not one wrong score, it was a machine recording permanent
classifications unattended. Un-recording them unattended is the same act with
a wider blast radius. An agent reads the evidence, judges, and records the
judgment under its own name through `classify_shapes`.

`test_the_service_carries_no_machinery_for_bulk_withdrawal` asserts that
structurally, so the next person to reach for an auto-retire has the argument
again on purpose rather than in a diff nobody reads.

A JUDGMENT IS NEVER LISTED AS WEAK, whatever its age. This is the measured
correction to an assumption I nearly shipped: of Scribe's 334 rows under
#2860, 302 are in `services/` — the canon's own home — and the ones sampled
there are `classified_by="audit"` with no score at all. The legitimate bulk
of that canon was never scored; it was judged by an agent in batch. Listing
those as weak would invite an agent to withdraw the only real judgments in the
ledger. An agent's decision is a different KIND of evidence, not a worse one.

THE SCORE NOW HAS A PARSER. It lived only inside a prose sentence, so nothing
could ask how strong the evidence for a row was without re-deriving it — which
is how 32 rows sat unexamined for nineteen days. Format and reader are one
constant apart (`_RESEMBLE_REASON` / `stamp_score`), with a round-trip test and
a test pinned to reason strings taken verbatim from the two poisoned ledgers.

`live_rows_for` is `live_rows` behind the project read gate, for callers that
arrive from outside rather than from a job that already knows who is asking.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01821k5B3Ysecp9fNYs92Kuy
This commit is contained in:
2026-09-20 22:28:12 -04:00
co-authored by Claude Opus 5
parent d5b46ffc45
commit 400253d039
4 changed files with 373 additions and 3 deletions
+134 -2
View File
@@ -439,6 +439,16 @@ async def mark_canonicals(
await session.commit()
async def live_rows_for(user_id: int, project_id: int) -> list[CodeShape]:
"""`live_rows` behind the project read gate — for callers that arrive from
outside (an MCP tool, a route) rather than from a job that already
established who is asking. Empty for a project the caller cannot read,
never a partial answer."""
if not await access.can_read_project(user_id, project_id):
return []
return await live_rows(project_id)
async def live_rows(project_id: int) -> list[CodeShape]:
"""Every un-vanished ledger row for a project — the accounting readout's
input, across ALL its repos (a repo unreachable this refresh still counts;
@@ -1137,6 +1147,34 @@ async def recent_pulls(user_id: int, *, window: timedelta = PULL_WINDOW) -> dict
return {}
# THE HOOK'S EVIDENCE, WRITTEN ONCE AND READ BACK BY ONE PARSER.
#
# The stamp's score used to exist only inside a prose sentence, which meant
# nothing could ask "how strong was the evidence for this row?" without
# re-deriving it. That is how 32 rows on one project and 334 on another sat
# unexamined: the number was printed, never stored, never queried. Format and
# parser live beside each other so a change to one that breaks the other is a
# visible edit rather than a silent drift (rule 33's reasoning, one module
# down).
_REFERENCE_REASON = "hook: pulled #{sid}; payload references `{symbol}`"
_RESEMBLE_REASON = "hook: pulled #{sid}; payload resembles it ({score:.2f})"
_RESEMBLE_READ = re.compile(
r"^hook: pulled #(\d+); payload resembles it \(([0-9]*\.?[0-9]+)\)$"
)
def stamp_score(reason: str | None) -> float | None:
"""The resemblance a hook stamp recorded, or None if it was not one.
None means "this row was not written on a similarity score" — a by-name
reference, an agent judgment, an audit — NOT "the score was zero". Every
caller treats the two differently, because a row a person judged is not
weak evidence, it is a different kind of evidence entirely.
"""
m = _RESEMBLE_READ.match((reason or "").strip())
return float(m.group(2)) if m else None
def _stamp_allowed(rank: int, mine: str, canon: str) -> bool:
"""May this evidence assert that a shape of form ``mine`` IS ``canon``?
@@ -1208,9 +1246,11 @@ async def stamp_write_path_instances(
symbol = fields.get("symbol") or ""
kind = snippet_kind(symbol, fields.get("language") or "")
if references_symbol(code, symbol, kind):
rank, why = 2, f"hook: pulled #{sid}; payload references `{_norm_symbol(symbol)}`"
rank, why = 2, _REFERENCE_REASON.format(
sid=sid, symbol=_norm_symbol(symbol)
)
elif resembles.get(sid, 0.0) >= _RESEMBLE_MIN:
rank, why = 1, f"hook: pulled #{sid}; payload resembles it ({resembles[sid]:.2f})"
rank, why = 1, _RESEMBLE_REASON.format(sid=sid, score=resembles[sid])
else:
# A SCORE BELOW THE FLOOR IS NOT WEAK EVIDENCE, IT IS NONE. This
# branch used to be `elif sid in resembles`, which took any score
@@ -2199,6 +2239,98 @@ async def flag_divergence(project_id: int, *, since: datetime | None) -> int:
return flagged
# How many of a canon's rows a review listing shows before "…" — enough to
# judge from, not the whole table.
_REVIEW_ROWS_SHOWN = 12
def stamps_to_review(rows: Iterable[CodeShape], *, top: int = 10) -> dict:
"""Judged rows whose EVIDENCE no longer meets the bar the ledger now
holds, and canons whose own rows no longer agree what they are.
THIS CHANGES NOTHING, AND THAT IS THE POINT. Every row here was written
unattended by the write-path hook on a similarity score, and the damage
that caused was not the score being wrong once — it was a machine
asserting a permanent classification with nobody reading it. A sweep that
silently un-asserted them would be the identical mistake with a larger
blast radius and a cleaner conscience. So this reports; an agent reads the
evidence, decides, and records the decision through `classify_shapes`
under its own name. The ledger should be able to say "these look wrong"
without being able to act on it alone.
Two lists, because they are different questions:
`weak` — rows stamped on a resemblance below `_RESEMBLE_MIN`. The floor
arrived after they did (#4204), and a guard at the point of classification
does not undo the classifications already stored (lesson #4202). Rows a
person or an audit judged are never listed, however old: a human judgment
is not weak evidence, it is a different kind of evidence.
`incoherent` — canons whose judged rows do not agree on a form. A canon is
a claim that some set of shapes are the same sort of thing; when its own
members are a class, three getters and a dozen tests, that claim has
stopped being true, and every base-rate reading built on it is reading
noise. `canon_form` already makes such a canon fall silent — this is what
makes it VISIBLE, which is the half that was missing.
"""
live = [r for r in rows if r.vanished_at is None]
weak = []
for r in live:
if r.status not in _NEEDS_TARGET or r.classified_by != "hook":
continue
score = stamp_score(r.reason)
if score is None or score >= _RESEMBLE_MIN:
continue
weak.append({
"path": r.path, "symbol": r.symbol, "kind": r.kind,
"status": r.status, "snippet_id": r.snippet_id,
"score": score, "signature": r.signature or "",
"form": shape_form(r.signature or "", r.kind),
"classified_at": r.classified_at.isoformat() if r.classified_at else None,
})
weak.sort(key=lambda d: (d["score"], d["path"], d["symbol"]))
by_canon: dict[int, list[CodeShape]] = {}
for r in live:
if r.status in _NEEDS_TARGET and r.snippet_id:
by_canon.setdefault(int(r.snippet_id), []).append(r)
incoherent = []
for sid, members in by_canon.items():
forms: dict[str, int] = {}
for r in members:
f = shape_form(r.signature or "", r.kind)
if f:
forms[f] = forms.get(f, 0) + 1
readable = sum(forms.values())
if readable < _DENSITY_MIN_JUDGED:
continue # too few to say anything either way
if canon_form(members, sid):
continue # a form holds the majority: coherent
incoherent.append({
"snippet_id": sid,
"judged": len(members),
"forms": dict(sorted(forms.items(), key=lambda kv: -kv[1])),
"weak_rows": sum(
1 for r in members
if r.classified_by == "hook"
and (stamp_score(r.reason) or 1.0) < _RESEMBLE_MIN
),
"sample": [
{"path": r.path, "symbol": r.symbol,
"signature": r.signature or "", "by": r.classified_by}
for r in members[:_REVIEW_ROWS_SHOWN]
],
})
incoherent.sort(key=lambda d: (-d["weak_rows"], -d["judged"]))
return {
"weak_count": len(weak),
"weak": weak[:top],
"incoherent_count": len(incoherent),
"incoherent": incoherent[:top],
"floor": _RESEMBLE_MIN,
}
def divergence_summary(rows: Iterable[CodeShape], *, top: int = 10) -> dict:
"""Readout view: flagged shapes (newest first) and the recheck count."""
flagged = [r for r in rows if r.diverges_from is not None and r.status in _MECHANICAL_TODO]