Repository navigation
Description
What this is
Parent/executor issue for the backend listen→conversation lifecycle hardening. It binds together #9516 (converge conversation lifecycle onto a single mutation owner) and #9351 (durable recording sessions for listen lifecycle), adds the meta-machinery neither child specifies (failure-class evidence, ordering fuzzer, anti-regrowth guards, checks-manifest registration), and defines the closure criteria for all of it.
An agent should be able to execute from this issue alone: read the two children for their locked designs, follow the phases here, and open one PR whose body contains Closes #9516, Closes #9351, and Closes this issue.
This parent supersedes #9516's "Phase 1 lands as its own PR" sequencing (deliberate: one reviewable PR was chosen over two landings). The intent of that sequencing is preserved as a hard commit-ordering rule: characterization/contract tests are the first commit(s), refactor commits come after, and a reviewer must be able to check out the test commit alone and watch it pass against unmodified production code.
Why: the recurring failure classes
Churn since 2026-04-01: routers/transcribe.py 71 commits, utils/conversations/process_conversation.py 67, routers/sync.py 60, routers/pusher.py 30. The fixes cluster; every work item below maps to one of these classes — if you are tempted to cut an item, check which class it leaves open.
| Class | Example fix commits | What allowed it |
|---|---|---|
| A. Plural mutation authority | claim_conversation_status() CAS scar (database/conversations.py:878), ten modules writing conversation docs via a generic dict API (full census in #9516) |
No single owner of lifecycle verbs; any caller can set any field |
| B. Finalization races on disconnect/restart | 8d55fc0cfd (guard disconnect finalization races), 25227df431 (desktop recording finalization reconciliation), 2d67863cad (make listen finalization durable) |
Finalization decisions interleave client disconnect, STT callbacks, and background finalizers with no stated ordering contract |
| C. Identity/binding drift | 3367796271 (bind recordings by client identity), #9349 (stale global pointer attaching recordings to wrong conversation) |
Conversation attachment inferred from user-global pointers/timestamps/WS ordering instead of an explicit durable mapping |
| D. Stream teardown edge cases | c69a7484c4 (flush multi-channel tail audio before pusher close), d4252a6738 (retain failed audio, surface live STT errors) |
Teardown paths hand-ordered per call site |
Class A is #9516's core. Classes B/C are #9351's core. Class D is closed as a byproduct of the ordering fuzzer below.
Rules of engagement (binding for the whole PR)
- A test that would not have caught one of the fix commits above is decoration. Name the commit or class each new test would have caught.
- Characterization before movement. The Phase 1 tests assert current behavior including current quirks — they are the lock, not the spec. If a quirk is found that must change, that change is its own later commit with its own justification, never silently folded into the refactor.
- Guards must be class-closing. Hand-enumerated interleavings are tripwires; properties that hold under any interleaving are guards. Prefer guards.
- Follow the repo's backend rules exactly: import purity, no in-function imports, executors via
run_blocking(never bareasyncio.to_thread),asyncio.wait(FIRST_COMPLETED)supervisor patterns in WS handlers,record_fallbackon any provider/mode/fail-open branch,sanitize()/sanitize_pii()in logs. SeeAGENTS.mdandbackend/docs/test_isolation.md. - Hermetic or out of CI. Every new CI test uses fake Firestore/Redis seams per
backend/docs/test_isolation.md— no live services, no sleeps, no ordering dependence.
Execution phases (one PR, commits strictly in this order)
Phase 1 — characterization & contract lock (first commit(s))
Execute #9516's Phase 1 as specified there, plus:
- Register the conversation lifecycle workflow in
backend/testing/workflow_contracts.json(mechanism exists; flow is unregistered). - Ordering fuzzer (the meta-addition): extract the finalization decision logic reachable from
routers/transcribe.py/utils/conversations/process_conversation.pyinto a pure function (inputs: session/conversation state + one event; output: decision), then property-test it over randomized legal event sequences — disconnect-before-final, final-after-disconnect, duplicate finals, concurrent finalize-vs-reprocess, restart mid-finalization, merge during processing. Seeded PRNG (OMI_FUZZ_SEED), fixed corpus in CI, failing sequences checked in as named regression tests. Invariants: exactly one terminal lifecycle outcome per conversation, no side-effect fanout executes twice for the same idempotency key, no transition out of a terminal state. This is Introduce durable recording sessions for listen lifecycle #9351's "state-machine/property tests" requirement made concrete and shared with refactor(backend): converge conversation lifecycle onto a single mutation owner #9516's state machine. - Gate: full
backend/test.shgreen, new tests pass against unmodified production code.
Phase 2 — single mutation owner (#9516)
Execute #9516's locked target architecture: one lifecycle service owning status transitions, processing admission (single claim point, idempotent by conversation id), finalization/merge/reprocess orchestration; typed transitions failing closed; migrate all ten writer modules; delete every alternative lifecycle-write path (content-field writes stay plain). Keep the ownership matrix from #9516 as the acceptance table.
- Anti-regrowth guard (meta-addition): a small static checker (registered in
.github/checks-manifest.yaml, both lanes) that fails when a module outside the lifecycle service writes lifecycle fields (status, claim fields) throughdatabase/conversations.py. Label it a static tripwire; include a self-test fixture proving it fails on a pasted-back direct write. Follow the conventions of existing.github/scripts/check_*_ratchet.pycheckers. - Gate: zero legacy-path use demonstrated via the Phase 1 counters/traces before any deletion commit.
Phase 3 — durable recording sessions (#9351)
Execute #9351's design requirements as written: immutable recording_session_id idempotency key, persisted session→conversation mapping with lifecycle state and generation metadata, versioned lifecycle-event envelope with monotonic sequence, idempotent finalization, feature-flagged rollout with dual-write/shadow validation, retire the user-global in-progress pointer only after mismatch metrics demonstrate safe cutover. Route the new session state machine's transitions through the Phase 2 lifecycle service — do not create a second mutation owner.
- The rollout flag / shadow-validation scaffolding gets
LIFECYCLE:headers per AGENTS.md (one-time files needDELETE-AFTER:). - Fallback/shadow-mismatch branches call
record_fallback(component from the closed enum, e.g.sync_dispatch/other) — no new one-off counters.
Phase gates
If Phase 1 characterization reveals a behavior change that requires redesign beyond the two children's locked architectures, stop and surface it on this issue before proceeding. Phase 3 starts only when Phase 2's guard shows zero legacy writers.
Verification (cite evidence in the PR body)
backend/test-preflight.sh, then fullbackend/test.shafter each phase.make preflight— new manifest checks present and green.- Fuzzer seed corpus + run count/runtime recorded in the PR body.
- Update
docs/doc/developer/backend/listen_pusher_pipeline.mdxin the same PR (this changes conversation lifecycle — mandatory per AGENTS.md). - PR body includes: the failure-class table above as recurrence evidence, root cause + durable guard per class, the refactor(backend): converge conversation lifecycle onto a single mutation owner #9516 ownership matrix as shipped, per-child closure checklist (below), and how any non-hermetic validation (shadow-mode metrics) was run.
Closure criteria
- refactor(backend): converge conversation lifecycle onto a single mutation owner #9516 closes when: lifecycle verbs have exactly one owner, typed transitions fail closed, all ten writers migrated, legacy paths deleted, contract suite + workflow registration in CI, anti-regrowth checker live.
- Introduce durable recording sessions for listen lifecycle #9351 closes when: recording sessions are durable server resources with idempotent finalization, property tests cover its listed scenarios, rollout flag + shadow validation shipped, metrics for mapping conflicts/stale-event discards/legacy-path usage emitting.
- This issue closes when: both children close via the single PR and the meta-machinery (fuzzer, guards, manifest entries, doc update) is verified in CI.
Explicitly out of scope
- Conversation sync: add durable mutations, change feed, and tombstones #9355 (sync change feed / tombstones) — related but needs its own rollback plan; do not fold in.
- Any STT-provider work (Parakeet/Deepgram issues), diarization, translation.
- App/desktop client changes beyond what Introduce durable recording sessions for listen lifecycle #9351's envelope versioning strictly requires (compat path stays until cutover).
Related
Parent of: #9516, #9351. Sibling playbook: #9515 (macOS convergence), the PTT stability plan (desktop). Phase 1 precedent: #9349.
Activity
test(backend): fuzz finalization ordering decisions
feat(listen): persist durable recording sessions
backend: make listen conversation lifecycle durable (#9700)
fix(backend): restart an expired Firestore transaction instead of kil…
fix(backend): restart an expired Firestore transaction instead of kil…
fix(backend): restart an expired Firestore transaction instead of kil…