Repository navigation
perf(sync): checkpoint and mark a ledger segment in one transaction - #24253
Conversation
Each successful sync segment ran two transactions on the same content ledger document (checkpoint, then processed marker): ~0.5M writes and ~0.5M reads/day each on 2026-11-01. Combine them into one commit with the same owner fence and the same raise points. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
2 issues found across 6 files
Confidence score: 4/5
test_sync_ledger_checkpoint_mark.pycould pass even if the checkpoint write replaces the ledger and drops its owner and processed-marker fields. Assert the recorded(data, merge)values.test_sync_v2.pycould pass even if the transactional helper is bypassed, because the legacy mock raises the same exception. Make the test distinguish those paths.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="backend/tests/unit/test_sync_v2.py">
<violation number="1" location="backend/tests/unit/test_sync_v2.py:2723">
P2: This setup leaves the regression test unable to prove that the transactional helper is used: the legacy mock raises the same exception, so bypassing `checkpoint_and_mark_sync_segment` still produces `upstream_error`. Make the legacy mock non-raising or assert it is unused, and assert `checkpoint_and_mark_sync_segment` was called once.</violation>
</file>
<file name="backend/tests/unit/test_sync_ledger_checkpoint_mark.py">
<violation number="1" location="backend/tests/unit/test_sync_ledger_checkpoint_mark.py:48">
P2: This assertion ignores whether the already-processed checkpoint uses `merge=True`, so a replacing write that wipes the ledger owner and processed-marker fields would pass the test. Assert the recorded `(data, merge)` tuple, not only `data`.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
| stubs['pipeline'].compute_sync_segment_id = MagicMock(return_value='segment-1') | ||
| stubs['pipeline'].process_segment = MagicMock(return_value=True) | ||
| stubs['pipeline'].checkpoint_sync_content_partial_result.side_effect = RuntimeError('ledger unavailable') | ||
| stubs['pipeline'].checkpoint_and_mark_sync_segment = MagicMock( |
There was a problem hiding this comment.
P2: This setup leaves the regression test unable to prove that the transactional helper is used: the legacy mock raises the same exception, so bypassing checkpoint_and_mark_sync_segment still produces upstream_error. Make the legacy mock non-raising or assert it is unused, and assert checkpoint_and_mark_sync_segment was called once.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At backend/tests/unit/test_sync_v2.py, line 2723:
<comment>This setup leaves the regression test unable to prove that the transactional helper is used: the legacy mock raises the same exception, so bypassing `checkpoint_and_mark_sync_segment` still produces `upstream_error`. Make the legacy mock non-raising or assert it is unused, and assert `checkpoint_and_mark_sync_segment` was called once.</comment>
<file context>
@@ -2720,6 +2720,9 @@ async def test_success_metric_waits_for_durable_segment_checkpoint(self, fenced_
stubs['pipeline'].compute_sync_segment_id = MagicMock(return_value='segment-1')
stubs['pipeline'].process_segment = MagicMock(return_value=True)
stubs['pipeline'].checkpoint_sync_content_partial_result.side_effect = RuntimeError('ledger unavailable')
+ stubs['pipeline'].checkpoint_and_mark_sync_segment = MagicMock(
+ side_effect=RuntimeError('ledger unavailable')
+ )
</file context>
| def test_already_processed_segment_still_checkpoints_like_the_separate_calls(): | ||
| outcome, sets = _run({**OWNED, 'processed_segment_ids': ['seg-2']}) | ||
| assert outcome == sync_ledger.SEGMENT_ALREADY_PROCESSED | ||
| assert [data for data, _ in sets] == [{'partial_result': {'conversation_ids': ['c']}, 'updated_at': NOW}] |
There was a problem hiding this comment.
P2: This assertion ignores whether the already-processed checkpoint uses merge=True, so a replacing write that wipes the ledger owner and processed-marker fields would pass the test. Assert the recorded (data, merge) tuple, not only data.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At backend/tests/unit/test_sync_ledger_checkpoint_mark.py, line 48:
<comment>This assertion ignores whether the already-processed checkpoint uses `merge=True`, so a replacing write that wipes the ledger owner and processed-marker fields would pass the test. Assert the recorded `(data, merge)` tuple, not only `data`.</comment>
<file context>
@@ -0,0 +1,58 @@
+def test_already_processed_segment_still_checkpoints_like_the_separate_calls():
+ outcome, sets = _run({**OWNED, 'processed_segment_ids': ['seg-2']})
+ assert outcome == sync_ledger.SEGMENT_ALREADY_PROCESSED
+ assert [data for data, _ in sets] == [{'partial_result': {'conversation_ids': ['c']}, 'updated_at': NOW}]
+
+
</file context>
Summary
Firestore write cut (lead ruling 268f54: "take every efficiency gain").
Every successful sync segment with a content ledger ran two transactions on the same
users/*/sync_content_ledger/{content}document:checkpoint_sync_content_partial_result, thenadd_processed_sync_segment_id. Each did one read and one write. The write probe (#24218) measured both at about 0.49-0.50M writes/day on backend-sync plus backfill on Sunday 2026-11-01, so this cut saves:database.sync_ledger.checkpoint_and_mark_sync_segmentnow does both in one transaction (one read, one write). The pipeline uses it whenever the segment has an ID; segments without one keep the checkpoint-only call.Outcomes and failure points are unchanged:
SyncJobRunLeaseLostwhere the old checkpoint raised (inside the segment lock, before the job marker).add_processed_sync_segment_id.The ledger's processed marker now commits atomically with its result contribution, instead of after it by ordering. The cumulative partial is still written under
segment_lock, so concurrent segments cannot regress it.Crash windows: a retry skips a segment if either the job marker or the ledger marker is set (
pipeline.py~3311).Product invariants affected
utils/sync/pipeline.py,database/sync_ledger.py): same document, fields, owner fence and ordering guarantees. The checkpoint and marker become one atomic commit.Verification
All runs used
backend/test.shunderbin/omi-heavy+nice -n 19, at the unit layer, unless noted:tests/unit/test_sync_ledger_checkpoint_mark.py(6): owned new segment → one write carrying both fields; already processed → checkpoint only; four foreign-owner shapes → no write.test_sync_v2(175),test_sync_silent_failure(65),test_sync_two_lane(47),test_sync_geolocation_enrichment(6),test_sync_cap_coordinator(41),test_wal_audio_coverage_pipeline(10): pass. Tests that mocked the two calls now mock the combined call where a segment ID exists.backend/testing/e2e/run.sh -k sync): 11 passed.Rollback
Revert. No data migration: the document shape is identical.
Failure-Class: none
Line-Count-Exception: backend/utils/sync/pipeline.py | 3937 -> 3959 | one combined ledger call beside the existing checkpoint path, with its raise points kept where the separate calls raised; no new pipeline logic
🤖 Generated with Claude Code