Skip to content

feat(firestore): open historical_scan_memo_scope at the sync-task attempt boundary - #22048

Merged
Git-on-my-level merged 1 commit into
mainfrom
lane/firestore-reads-b3
Oct 17, 2026
Merged

Git-on-my-level merged 1 commit into
mainfrom
lane/firestore-reads-b3

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Firestore reads loop, batch 3 slice B3S0 (scope opener). Batch 2 (#22019) shipped three default-off levers;
historical_scan_memo is registered in FIRESTORE_READ_LEVERS at 5% on prod but inert because the memo
scope historical_scan_memo_scope(uid) had no production opener. This PR opens the scope at the sync-task
attempt boundary so the lever starts saving reads when ramped:

  • backend/routers/sync.py::_run_sync_job_body: wrap the awaited pipeline call in
    with historical_scan_memo_scope(uid): (sync.py:2278), inside the acquired per-job run-lock, after
    admission/download checks, for both fresh and backfill lanes. Parsing, lock acquisition, early exits
    (locked/terminal/expired), retry handling and lock release stay outside the scope. Exactly one scope per
    executed task attempt; each Cloud Tasks retry is a fresh delivery/context, so no memo state crosses attempts.
  • people_scan_dedupe deliberately stays inert: its only shared callers (database/users.py:989 via
    get_people — speaker-learning authority path — and utils/llm/known_spellings.py:147) do not qualify for a
    task-wide opt-in under the S6 review constraints (external people writers lack complete invalidation). No
    config change in this PR; the flag can stay at 5% until a qualifying call site is audited.
  • ledger_purge_coalesce needs no opener (gated internally at the purge orchestrators) — verified unchanged.

Executor propagation: run_blocking uses copy_context() + ctx.run, so executor leaves see the memo object,
never reset its token, and observe closed after scope exit (cancelled-leaf containment is tested). No
ContextVar reset runs in a foreign context (desktop-backend incident rule respected). The inline fallback spawn
(sync.py:1635) is intentionally NOT wrapped: a scope around a fire-and-forget spawn would close before the
pipeline runs; inline-dispatch jobs keep current read counts (efficacy-only, correctness unaffected).

Product invariants affected

  • INV-MEM-1 — no new tier/collection; the scope only bounds the lifetime of the batch-2 memo inside one task
    attempt. Default off (lever at 0/unset = byte-for-byte current behavior; the CM bare-yields).
  • INV-MEM-3 — universal memory authority unchanged: memo hits only remove identical re-reads
    (same uid/cursor/limit) inside one attempt; every write in database/memories.py fences the memo; budget
    accounting stays truthful.
  • INV-MEM-4 — read-count behavior only, behind the default-off lever; no promotion/authority/mutation change.
  • INV-TASK-2 — no task creation/write behavior touched.
  • INV-DATA-1 — no data-plane/ownership change; no new runtime_env keys (historical_scan_memo already in
    KNOWN_LEVERS and already declared in prod FIRESTORE_READ_LEVERS at 5).

Test plan

  • New suite backend/tests/unit/test_fsr_scope_opener.py (15 tests): read-count proof (2 identical scans →
    1 physical read, both lanes, real 5%-cohort hashing), fence-through-scope (mid-attempt write re-reads then
    re-memoizes), off-path equality (unset with memo-allocation guard / outside_cohort / no_opener all
    (2,2)), scope hygiene (no opener on locked/terminal/expired exits; close on success/TimeoutError/
    CancelledError with entries cleared and ContextVar None), executor propagation without ambient leakage,
    retry freshness (attempt-2 reads post-write value).
  • Builder run: 406 tests green across opener + sync_cloud_tasks (131) + sync_v2 (182) + sync_dead_letters (43)
    • historical_scan_memo (35) via the BACKEND_UNIT_TEST_FILE_LIST runner at nice 19 (lever-on and env-unset
      passes).
  • Independent review: zcode glm-5.3 verdict SAFE, no fixes required — confirmed off-path unchanged, one
    scope per attempt inside the run-lock, closed on all exits, no cross-attempt memo survival, zero unfenced
    memories-write paths (sweep found 0; entities.py:159 KG reassignment has no production caller), and
    reviewer independently re-ran 50 tests green. Reviewer notes inline-fallback spawn intentionally unwrapped
    (correctness-preserving) — do not "fix" by wrapping the spawn site later.

Line-count exceptions

Line-Count-Exception: backend/routers/sync.py | 2882 -> 2887 | open historical_scan_memo_scope around the sync-task pipeline attempt (lever historical_scan_memo)

Failure-Class: none

Rollout

  1. This PR changes no config: historical_scan_memo is already declared at 5% in prod
    FIRESTORE_READ_LEVERS; on deploy the scope opener activates the existing 5% cohort (safety soak).
  2. Loop readout (readout.py absolute caller reads vs the pinned post-B2 baseline) decides the later
    100% config PR; guard services backend-sync/backend-sync-backfill stay under verify.py hourly gates
    (backfill exemption expires 2026-10-18T01:00Z and is re-checked before any ramp).

View guided diff Turn on auto-fix

…empt boundary

Batch 3 slice B3S0: the batch-2 historical_scan_memo lever (5% on prod) was inert
because no production code opened the memo scope. Open one scope per executed
sync/backfill task attempt in _run_sync_job_body, inside the acquired run-lock,
covering both lanes. Retry/lock/admission paths stay outside; executor leaves
inherit the memo via copy_context without resetting it; every memories write
still fences. people_scan_dedupe stays inert (no qualifying call site under the
S6 constraints). 15 new tests; 406 targeted tests green; zcode glm-5.3 review
SAFE with zero required fixes.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T17:21:45.709240Z 214d1fd PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Git-on-my-level
Git-on-my-level merged commit 5bb18b1 into main Oct 17, 2026
62 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/firestore-reads-b3 branch October 17, 2026 17:31
Git-on-my-level added a commit that referenced this pull request Oct 20, 2026
…and 7 ticks) (#22629)

* config(firestore): re-enable six exonerated levers at 5% (guards in band 7 ticks)

usage_month_cache, singleton_loops, queue_age_sample_budget, backfill_task_reads,
historical_scan_memo, ledger_purge_coalesce: 0 -> 5. backend-sync/-backfill 5xx
in band 11Z-17Z 2026-10-19 with all sync levers at 0 (drain finished); singleton
lease rework (#22142) and queue-age code live on served image 4f43b2d; scope
opener live (#22048). people_scan_dedupe stays 0 (no qualifying call site).
set_levers.py replace-mode regeneration (16 sites, 6 files) incl. contract tests.

* chore(firestore): regenerate flag registry after main merge (13-key literal, six levers at 5)
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