Skip to content

feat(pipeline): fail-open in-flight ledger with the C2 read and coverage contract - #25047

Merged
Git-on-my-level merged 9 commits into
BasedHardware:mainfrom
Git-on-my-level:lane/pipeline-c1
Nov 10, 2026
Merged

Git-on-my-level merged 9 commits into
BasedHardware:mainfrom
Git-on-my-level:lane/pipeline-c1

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

C1a-core: storage-neutral in-flight ledger contract (dormant, no live hooks)

Failure-Class: FC-state-deadline-without-reader-exit

Line-Count-Exception: backend/database/firestore_index_registry.py | 2342 -> 2343 | one collection-group single-field index for the C2 ledger window read helper.

Size (measured, not estimated)

git diff --numstat 720f078b33dde3872f5156fb87a961ffec2300c7...HEAD outside
backend/tests/**:

4   0  .github/scripts/dead_code/backend.allowlist.json
7   0  backend/charts/monitoring/metric-inventory.prod.json
5   0  backend/config/refactor_modes.py
1   0  backend/database/firestore_index_registry.py
462 0  backend/database/pipeline_ledger.py
2   0  backend/docs/feature-flag-registry.md
21  0  backend/utils/observability/reasons.py
1   0  config/feature-flags.yaml
23  0  firestore.indexes.json

526 changed non-test lines (526 additions, 0 deletions), 26 over the
500-line cap. The C1 fix batch itself is 77 changed non-test lines (56
additions, 21 deletions) in backend/database/pipeline_ledger.py, under the
200-line fix cap. The 26-line overage is the fix batch on top of the 491-line
split; the alternative was to leave the pre-review findings open. Total
including tests: 1,073 additions / 2 deletions.

The one .github change is the dead-code allowlist entry the ratchet itself
prescribes for a dormant module (same mechanism as
backend/utils/batch_pipeline/vendor_health.py). It is a data file, not a
workflow. C1a2 removes the entry when the first caller lands.

Base and head

  • Base: 720f078b33dde3872f5156fb87a961ffec2300c7 (origin/main at branch
    creation; still the merge base).
  • Head: d84481d96c321e0c4c62ea4cfc90662beb88fd5e (push 3/3).

This is push 3/3 for #25047. Push 2/2 (d9efc6ce) was the mechanical content
split that brought the dormant core under the cap. Push 3/3 is the C1 fix
batch for the three 28a41fa pre-reviews. The live seal/notes/upload-failure
callers are still not in this PR; they remain in
evidence/c1a2-live-transition-hooks.patch (C1a2).

C1 fix batch — finding by finding

The three pre-reviews were written against 28a41fa, which still carried the
live callers. The split moved those callers to the preserved C1a2 patch, so
the P1 caller findings are fixed there, not in this dormant core head. The
core findings are fixed in this head.

# Finding Resolution
1 P1 post-commit writer recreates ledger data after deletion C1a2 patch. advance() accepts source_ref/owner_ref; the ledger transaction reads both and refuses to create a row when the source is missing/deleted or the owner document is gone. Fail-open for the already-committed source transition. Emulator test test_delayed_ledger_write_does_not_recreate_after_source_delete pauses before advance(), deletes the source, and asserts no row.
2 P1 callers never supply revision/reopen C1a2 patch. pipeline_ledger.source_revision() derives an ordered revision from the source snapshot's update_time; seal and notes pass it with reopen=True, so a proof repair can close a failed:upload_failed recording and a stale failure hook loses. Emulator test test_late_event_from_an_older_revision_is_refused.
3 P2 TTL type This head. expires_at is a UTC datetime (Firestore timestamp), not a numeric epoch. test_expires_at_is_a_utc_timestamp_not_an_epoch asserts the serialized type.
4 P2 upload-failure terminal has no firing clock C1a2 patch. mark_record_upload_failed persists record_upload_failed_at; the seal hook passes it as end_at (falling back to the observation time), never last speech or the ledger-write time.
5 P2 first durable summary skipped when the acceptance clock is unknown C1a2 patch. The noted predicate accepts reason in {'summary', 'accept_unknown'} while still excluding discarded and local_only.
6 P2 census helper has no owner scope This head. iter_window()/census_window() require uid and query users/{uid}/pipeline_ledger; no cross-owner read. Emulator test test_census_window_never_reads_another_owner.
7 P2 raw identifiers in rows This head. capture_id/conversation_id are no longer stored in the row body; the document path is the documented key. The census projects bounded fields only.
8 P2 partial results before truncation failure This head. The window is materialized before the first yield, so a truncated window raises LedgerWindowTruncated without handing the caller a prefix. test_iter_window_yields_no_prefix_before_truncation.
9 P2 arbitrary failed:<text> bypasses validation This head. plan_advance() normalizes any failed: stage through failed_stage(), so only the closed failure vocabulary is stored; unknown text collapses to failed:other. test_arbitrary_failed_reason_collapses_to_the_closed_enum.
10 P2 repeated partial seals inflate admissions C1a2 patch. Admission is counted only when the seal actually closes the recording, not on a partial-seal repair.
11 P2 ledger emulator suite never runs in CI Follow-up, not fixed here. Adding the path to backend-emulator-proofs.yml alone would not run the suite: the workflow invokes backend/scripts/run-emulator-proofs.sh, which does not list test_pipeline_ledger_firestore.py. The one-line trigger addition is therefore insufficient; the follow-up is to add the suite to that script (and then the trigger path). No workflow change is made in this PR.

What C1a-core is

A storage-neutral ledger contract plus its declared inventory. It has no live
transition hooks
: nothing in the product calls advance(). It is dormant
observation/schema infrastructure, and this PR does not claim otherwise.

  • backend/database/pipeline_ledger.py (new, 462 lines): stage table with a
    deadline and exit per nonterminal stage; plan_advance() pure transition
    rules; advance() in its own Firestore transaction that reads the document
    before writing it (CAS); iter_window() / census_window() bounded,
    owner-scoped read helper; content-free document shape; counters/gauges/histogram.
  • backend/utils/observability/reasons.py: closed PipelineLedgerOutcome and
    PipelineLedgerFailureReason enums and the PIPELINE_LEDGER reason family.
  • backend/config/refactor_modes.py + config/feature-flags.yaml +
    backend/docs/feature-flag-registry.md: PIPELINE_LEDGER via default_on.
  • backend/charts/monitoring/metric-inventory.prod.json: the seven declared
    metric names.
  • firestore.indexes.json + backend/database/firestore_index_registry.py:
    the pipeline_ledger.admitted_at collection-group index retained for the
    future global aggregate; the per-owner read helper uses the automatic
    single-field index.

Stages: capturing, sealed, transcribed, noted, done; terminals
no_speech, noise, failed:<reason> with a closed reason vocabulary.
advance() is idempotent for the same stage+revision, refuses backward
transitions, loses a stale revision, and re-opens a terminal only with an
explicit higher revision. Terminal rows carry expires_at clamped to
[26h, 7d] (PIPELINE_LEDGER_TERMINAL_TTL_SECONDS, default 93600).

What core proves

  • The pure transition contract: table invariants, forward/backward rank,
    duplicate, stale revision, late-append and retry re-open, terminal finality,
    max-16 history, bounded reason/end type, TTL clamp, schema fields and
    content-free shape, owner_key truncated hash, closed failed: vocabulary.
  • advance() is fail-open on a store error, silent when the flag is off, and
    writes the planned document with a read-before-write CAS in its own
    transaction. A stale advance cannot overwrite a newer stage (emulator).
  • The read helper is bounded, owner-scoped, read-only, rejects an inverted
    window, projects bounded fields, and fails closed with
    LedgerWindowTruncated before yielding a prefix.
  • Metrics are content-free (no uid, capture id, transcript, title or reason
    text).
  • The declared index and metric inventory match the code.

What core does not prove

  • No real transition is instrumented. Seal, first durable notes, upload
    failure, admission, transcript landed, episode composed and the remaining
    terminal exits are not wired. There is no production coverage claim; the
    99.5% target is measured only after those transitions land.
  • No atomicity with a real write. There is no real write in this PR. The
    contract is that a ledger write is a separate transaction after the real
    commit, so it is not atomic with it; a crash between them loses that row and
    the independent admission counter makes the loss visible. That statement is
    inherited from the reviewed design and is not exercised here.
  • No portability/erasure wiring. The collection is never populated in
    core, so there is nothing to export or erase. The export inventory and
    recursive account wipe move to C1a2 with the first live write, so no window
    exists where rows are written without erasure.
  • The P1 caller fixes are in the C1a2 patch, not this head. They are not
    claimed as proven by this PR's tests.

Default, rollback, metric honesty

  • PIPELINE_LEDGER defaults on, but with no call sites it writes nothing.
    The default is inert in this PR.
  • Rollback is a no-op: setting PIPELINE_LEDGER=off changes nothing because
    nothing calls advance(). No data deletion is needed.
  • All seven metrics are registered and declared, but in production every series
    is flat: admit_total is never incremented, rows never moves,
    stage_seconds never observes, and write_total / write_failures_total /
    transition_total / unknown_end_type_total only move when a test calls
    advance() directly. Do not read these as coverage.

C1a2 allocation (preserved, not rewritten)

Moved to evidence/c1a2-live-transition-hooks.patch in the run directory
(data/run-2026-11-08-pipeline-refactor/evidence/), with
evidence/c1a2-handoff.md. The patch now applies to this head
(d84481d9) and carries the P1 caller fixes plus findings 4, 5 and 10.

File Non-test add/del
backend/database/record_audio.py (seal + failed:upload_failed, revision, firing clock, admission) 49 / 2
backend/database/conversations.py (first-summary noted, revision/reopen) 51 / 2
backend/database/pipeline_ledger.py (end_type_for_trigger, source/owner fence, source_revision) 62 / 5
backend/utils/observability/reasons.py (DELETED outcome) 1 / 0
backend/services/users/data_export.py (portability) 13 / 0
backend/database/user_data_deletion.py (wipe inventory) 1 / 1
backend/docs/listen_pusher_pipeline/changes/2026-11-10-pipeline-in-flight-ledger.md 64 / 0
.github/scripts/dead_code/backend.allowlist.json 0 / 4
C1a2 non-test total 241 / 14 (255 changed)

C1a2 also carries the moved tests: seal, summary persistence, durable outbox
transaction count, conversation transaction expiry, export inventory, the two
test_pipeline_ledger.py cases, and the emulator hook cases including the
deletion-interleaving and revision-ordering tests. C1a2 lands as a separately
reviewed PR after C1a-core merges, and it removes the
backend/database/pipeline_ledger.py dead-code allowlist entry.

Tests (this head)

Run with the worktree venv and GCP credential env vars unset, through
bin/omi-heavy:

  • tests/unit/test_pipeline_ledger.py (31 passed): table invariants,
    legal/illegal/duplicate/stale/terminal transitions, late-append and retry
    re-open, max-16 history, bounded reason/end type, TTL datetime, closed
    failed: vocabulary, schema/redaction, firing-clock separation, fail-open,
    disabled switch, owner-scoped read helper, truncation-before-prefix,
    content-free metrics.
  • tests/integration/test_pipeline_ledger_firestore.py (4 passed, loopback
    emulator): stale advance cannot overwrite a newer stage, disabled ledger
    writes nothing, census window reads committed rows, census never reads
    another owner.
  • Inventory/flag/reason/index suites (355 passed): test_firestore_field_indexes.py,
    test_reason_label_enums.py, test_config_refactor_w6.py,
    test_monitoring_telemetry_contract.py, test_flag_budget.py,
    test_firestore_outside_query_contract.py.
  • Export/privacy and touched-transition suites (270 passed):
    test_data_export.py, test_data_export_streaming.py,
    test_users_export_streaming.py, test_batch_pipeline_privacy.py,
    test_delete_account_purge_storage.py, test_record_audio_seal.py,
    test_summary_persistence.py, test_audio_durable_outbox.py,
    test_conversation_persistence_transaction_expiry.py.
  • C1a2 moved suites (281 passed) and C1a2 emulator (11 passed) were run in a
    temporary worktree at this head with the updated patch applied; the patch
    applies cleanly to d84481d9.
  • Full pre-push gate: passed (bounded local evidence; CI remains the full test
    authority).

Product invariants affected

  • INV-MEM-3

Scope split

  • C1a2 (preserved patch): seal, first durable notes, upload failure,
    end-type mapping, portability export, recursive erasure, change note, and
    the P1 caller fixes.
  • C1b: admission (utils/batch_pipeline/live.py) and transcript landed
    (database/batch_pipeline_projection.py), blocked by the fix(batch,sync): explicit backlog state machine with owners, deadlines and attempt budgets #24893 ownership
    fence; episode composed; remaining terminal exits; true admission
    denominator.
  • C2: reads only through iter_window() / census_window().

No admission live.py/projection fence, episode composition, Jev/E1, C2
endpoint, prod data/config, UI, .github workflow, credentials, model names,
deploy or merge in this PR.

Add a storage-neutral stage contract and fail-open store helper under
database/pipeline_ledger.py. One document per capture records stage,
entered_at, deadline_at, attempt, reason, end_type, end_at, the last 16
transitions, and a terminal TTL. Every nonterminal stage has a deadline
and an exit; advance() is idempotent and refuses backward transitions.

Instrument the seal transaction (database/record_audio.py) and the first
durable notes transaction (database/conversations.py). The ledger read and
plan happen before the real transaction, so the real transaction stays
write-only; a ledger error returns None and the real transition proceeds
unchanged. PIPELINE_LEDGER defaults on and off stops all ledger writes.

Admission and transcript landed are deferred to C1b because BasedHardware#24893 still
owns those files.
…contract

Rewrite the ledger around a post-commit CAS transaction so a ledger failure
can never fail the real transition, and a concurrent writer cannot overwrite a
newer stage. Add the C2 census fields (schema version, identity, owner key,
admission/created/updated clocks, revision, terminal, speech basis, Home and
episode clocks), stage+revision idempotency with explicit late-append and retry
re-open, a closed reason vocabulary, and a bounded read-only iter_window /
census_window helper.

Instrument seal, first durable notes and the upload-failure terminal. Add the
independent admission counter, write outcome/failure counters, transition and
stage gauges, and the unknown-end-type counter. Include the ledger collection
in the portability export inventory and the recursive account wipe.

Admission and transcript landed remain C1b because BasedHardware#24893 owns those files.
…roup index

Add the iter_window/census_window driver entries and the pipeline_ledger
admitted_at collection-group single-field index so the Firestore query-shape
guard can serve the C2 read helper.
@Git-on-my-level Git-on-my-level changed the title feat(pipeline): write-only in-flight capture ledger at seal and notes feat(pipeline): fail-open in-flight ledger with the C2 read and coverage contract Nov 10, 2026
…counts

Route the ledger outcome and failure labels through reason_label with new
closed enums, and update the export-section and transaction-count assertions
that the new ledger write and export section change.
Move the seal/notes/upload-failure hooks, their tests, the change note,
the portability/erasure wiring and the trigger->end-type mapping into the
preserved C1a2 patch. C1a-core keeps the storage-neutral ledger contract,
CAS/idempotency/revision semantics, bounded read helper, counters, flag,
index and metric inventory. Core has no live transition writes.
- expires_at is a UTC timestamp for the Firestore TTL policy
- census reads are owner-scoped, projected, and fail closed before any prefix
- failed:<text> collapses to the closed failure vocabulary
- stage labels are bounded; raw capture/conversation ids leave the row body
@Git-on-my-level
Git-on-my-level merged commit 623b84f into BasedHardware:main Nov 10, 2026
67 of 68 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant