refactor(storage): collapse duplicated session-write authority gate to one function - #3505
Conversation
…o one function Problem: ArchiveStore._write_parsed_precedence_result (archive_tiers/revision_governance.py) and the daemon batch-ingest path's _write_session (pipeline/services/ingest_batch/_core.py) each hand-carried their own copy of the "does revision authority refuse this write" check (accepted raw_revision_heads lookup + raw_session_memberships ambiguous- decision lookup). polylogue-c737 is the concrete symptom: PR #3397 fixed the ambiguous-membership refusal in one copy, then PR #3398 had to separately re-derive and re-apply the identical fix to the other -- "the signature of duplicated semantics rather than a missing check" per polylogue-aggz Invariant 2. Solution: extracted the two checks into one function, revision_authority_refuses_write, in storage/sqlite/archive_tiers/ingest_precedence.py (the module that already owns the sibling precedence primitives shared across these same two write paths, e.g. should_skip_stale_replace/browser_capture_precedence). Both _write_parsed_precedence_result and _write_session now call it and no longer carry their own copy of the governed-head/ambiguous-membership logic -- the duplicated blocks are deleted, not left as a bypassable alternate. Verification: python -m devtools test tests/unit/storage/test_revision_replay.py \ tests/unit/pipeline/test_ingest_batch.py \ polylogue/storage/sqlite/archive_tiers/ingest_precedence.py \ polylogue/storage/sqlite/archive_tiers/revision_governance.py \ polylogue/pipeline/services/ingest_batch/_core.py \ tests/unit/storage/test_archive_tiers_archive.py -> 127 passed Anti-vacuity: temporarily replaced each call site's `if revision_authority_refuses_write(...): ...` with `if False: ...` in turn. test_precedence_write_refuses_a_raw_recorded_ambiguous (revision_governance path) and test_write_session_refuses_a_raw_recorded_ambiguous_membership (_core.py path) both failed as expected, then passed again once reverted -- each production write path's own regression test depends on this function actually running. Ref polylogue-aggz Co-Authored-By: Claude <noreply@anthropic.com>
|
Warning Review limit reached
Next review available in: 56 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…th follow-up Co-Authored-By: Claude <noreply@anthropic.com>
Summary
Consolidates a duplicated revision-authority refusal check that lived
independently in two production session-write paths into one function,
revision_authority_refuses_write(storage/sqlite/archive_tiers/ingest_precedence.py).Problem
ArchiveStore._write_parsed_precedence_result(archive_tiers/revision_governance.py,the real implementation behind
write_parsed_for_retained_raw/write_raw_and_parsed,used by the one-shot importer and the revision-authority-aware live batch path)
and
_write_session(pipeline/services/ingest_batch/_core.py, the daemon'sdefault batch-ingest write path for most non-drive origins) each hand-carried
their own copy of two checks: "has this session_id already been claimed by an
accepted
raw_revision_headscohort winner" and "is this raw's ownraw_session_membershipsdecision recordedambiguous".polylogue-c737 is the concrete symptom this duplication produced: the live
archive had 28
aistudio-drivecohorts genuinely recordedambiguous(correctlyrefused a winner) whose sessions were nonetheless materialized in
index.dbwith 641 attachments stuck
unfetched. PR #3397 fixed the ambiguous-membershiprefusal in
_write_parsed_precedence_result; PR #3398 then had to independentlyre-derive and hand-apply the identical fix to
_write_sessionbecause it was aseparate copy that had never been patched — "the signature of duplicated
semantics rather than a missing check" (polylogue-aggz Invariant 2).
I read both write functions in full (
revision_governance.py~2900 lines,ingest_batch/_core.py~1970 lines) before touching anything, and traced realcallers rather than assuming duplication from file/line coordinates alone. Note:
the coordinator's initial lead (
_write_parsed_precedence_result/write_parsedduplicated between
revision_governance.pyandarchive.py) turned out to be afalse positive —
archive.py's copies are genuine one-line delegating methodsto the sole implementation in
revision_governance.py, exactly as that module'sown docstring documents (an already-completed extraction, not a live
duplication). The real duplication was the
_core.py/revision_governance.pypair named in polylogue-c737's own closure notes as a "known sibling hole."
Solution
revision_authority_refuses_write(conn, source_conn, *, session_id, raw_id, provider_session_id)toingest_precedence.py— the module thatalready owns the sibling precedence primitives shared by these same two
write paths (
should_skip_stale_replace,browser_capture_precedence,session_has_parser_ingest_flag,stored_message_count), so this is acontinuation of an established consolidation pattern (see that module's own
should_skip_stale_replacedocstring, itself a prior 3-copy→1 consolidationfor polylogue-t83e), not a new one-off shim.
_write_parsed_precedence_resultand_write_sessionnow call this onefunction instead of carrying their own inline SQL + comment block. The two
duplicated blocks (~59 lines and ~65 lines including their explanatory
comments) are deleted, not left as bypassable dead alternates, per this
repo's surgical-renewal rule.
session_id/raw_id/provider_session_idare required keyword argumentswith no defaults — a caller cannot invoke the gate without supplying
identity, and (per the anti-vacuity check below) cannot silently skip
calling it without a production regression test failing.
Acceptance criteria (Invariant 2 only — this lane's scope per coordinator reshape)
authority": partially satisfied. The specific duplicated-semantics bug
shape polylogue-c737/fix(storage): refuse a precedence write for a raw recorded ambiguous membership #3397/fix(storage): scope the ambiguous-membership refusal to the membership written #3398 exhibited (independently hand-maintained
copies of the revision-authority refusal check) is now structurally
impossible — there is exactly one implementation of that refusal decision.
This PR does not merge the two write paths themselves into one function;
_write_parsed_precedence_resultand_write_sessionremain separateentry points serving genuinely different callers (one-shot importer /
revision-authority-aware live batch vs. the daemon's default batch-ingest
path), each still carrying its own freshness/browser-capture-precedence
logic downstream of the shared gate. A full single-function merge of the two
write paths is a larger, riskier change spanning ~4900 lines across two
files and many call sites; I judged landing this smaller, verified slice
safer than forcing that merge in one lane, per this lane's explicit
"acceptable to land a smaller slice" guidance.
bypassed": satisfied for this slice — both inline duplicate-check blocks
(with their explanatory comments) are deleted from
revision_governance.pyand
ingest_batch/_core.py.Anti-vacuity
Production callers exercised:
ArchiveStore.write_parsed_for_retained_raw(→
_write_parsed_precedence_result) is reached from the one-shot importer(
pipeline/services/archive_ingest.py) and the revision-authority-aware livebatch path;
_write_sessionis reached from the daemon's default batch-ingestpath (
_write_session_entry→_process_ingest_batch_sync, the daemon'sprimary write path for most non-drive origins).
Mutation proof (temporarily replacing each call site's
if revision_authority_refuses_write(...): ...withif False: ..., one at atime, then reverting):
revision_governance.pymutation →tests/unit/storage/test_revision_replay.py::test_precedence_write_refuses_a_raw_recorded_ambiguousfails (assert (1,) == (0,), i.e. the ambiguous raw's session gets written).ingest_batch/_core.pymutation →tests/unit/pipeline/test_ingest_batch.py::test_write_session_refuses_a_raw_recorded_ambiguous_membershipfails (assert True is False).Both pass again once reverted (see Verification below for the clean run).
Follow-ups (not in scope here)
_write_parsed_precedence_resultand_write_sessioninto one literal chokepoint (rather than one sharedauthority-gate they both call) remains open, if the full Invariant 2 shape
is wanted. I have not filed a new bead for this per this lane's bd-write
restrictions (worktree
.beads/issues.jsonlreimport hazard) — reporting ithere for the coordinator to file.
ArchiveStore.write_parsed/SessionRepository.save_parsed_sessionis athird code path that writes directly to
sessionsviawrite_parsed_session_to_archivewith no revision-authorityconsultation at all (no raw_id, no governed-head check, no ambiguous-
membership check). Traced its only production caller
(
repository_writes.py::save_parsed_session) and that method itself has noreal production caller — only a docstring example and test infrastructure
reference it. Currently dead in the real ingest pipeline, but it is a public
method reachable from
SessionRepository/ArchiveStore, so it remains astructurally-possible fourth way to write a session bypassing authority if
anything ever calls it for real. Worth a follow-up bead to either delete it
or route it through the same gate.
Verification
Ref polylogue-aggz