Skip to content

refactor(sync): collapse the two read-only job readers into get_sync_job - #19437

Merged
aryanorastar merged 12 commits into
BasedHardware:mainfrom
aryanorastar:fix/sync-single-read-only-reader
Oct 5, 2026
Merged

aryanorastar merged 12 commits into
BasedHardware:mainfrom
aryanorastar:fix/sync-single-read-only-reader

Conversation

@aryanorastar

@aryanorastar aryanorastar commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator

#19379 has merged, so this is no longer stacked: the diff against main is now just this change — 6 files, +22/−30.

Summary

#19379 makes get_sync_job read-only. Once it does, main's get_raw_sync_job — added by #19362 for the per-UID sequencer — is the same function: both read the job key, return None on a missing, corrupt or non-dict blob, and write nothing.

Checked structurally rather than by eye: the two bodies are AST-identical modulo the local variable name (raw vs job).

Two copies of one read-only primitive is exactly how read-side mutation creeps back into one of them — the bug #19379 fixes started as a reader that wrote. So this keeps a single reader.

What changed

  • backend/database/sync_jobs.py — remove get_raw_sync_job and _read_raw_sync_job.
  • backend/routers/sync.py, backend/utils/sync/uid_sequencer.py — point the five callers at get_sync_job, and drop the now-duplicate import name.
  • Tests that patched the removed name now patch get_sync_job.

No compatibility alias, per the root AGENTS.md. No behaviour change.

The one fixture that needed thought

test_sync_task_persistence_fence_terminalizes_backfill_and_releases_its_slot is why this was deferred out of #19379's merge rather than done there.

It runs a backfill task with no sequencer_epoch, which first reads its own job to decide whether the UID sequencer should adopt it. The harness stubs the whole database.sync_jobs module with a bare MagicMock(), so before this change that read returned an auto-mock whose status never equalled 'queued', and the migration branch was skipped by accident. After consolidation the read consumes from the test's explicit get_sync_job sequence, sees the real queued job, and migrates — so it returned sequenced_legacy_job instead of superseded.

Rather than feed it a sentinel that reproduces the accident, the test now states its scenario: the legacy direct path, with the sequencer disabled, no owner and nothing pending — the three conditions the migration gate actually reads. The migration read gets its own entry ahead of the processor's three.

Mutation-checked: flipping uid_sequencer.enabled to True makes the test fail, so the pinned gate is load-bearing rather than decorative.

Verification

check result
every test_sync_* suite 952 passed, 0 failed
test_sync_cloud_tasks 101 passed
test_sync_backfill_uid_sequencer (#19362) 13 passed
test_sync_status_read_ownership (#19379) 12 passed
bash backend/scripts/typecheck.sh 0 errors
references to get_raw_sync_job / _read_raw_sync_job 0

Failure class

Failure-Class: FC-split-mutation-authority

Inherited from #19379, whose fix: commit is in this branch's history. This change is a refactor: and adds no new class: collapsing two read paths into one is the guard against that same class recurring.

Product invariants affected

none

Review in cubic

aryanorastar and others added 3 commits September 27, 2026 14:39
Reproduce five live-reader failures, including completed-result clobbering. Keep lookups read-only and let the existing run-lease finalizer own recovery. The real-route regression matrix preserves legacy, active, inline, standby and abandoned-task semantics.

Verified: 379 focused backend tests; 0 pyright errors; local Uvicorn + curl with fake Redis and outbound network blocked; 32 repository preflight checks.

Refs BasedHardware#19373

Failure-Class: FC-split-mutation-authority
…audit

# Conflicts:
#	backend/database/sync_jobs.py
Stacked on BasedHardware#19379. Once that change makes get_sync_job read-only, main's
get_raw_sync_job (added by BasedHardware#19362 for the per-UID sequencer) is the same
function: both read the job key, return None on a missing, corrupt or non-dict
blob, and write nothing. Checked structurally rather than by eye -- the two
bodies are AST-identical modulo the local variable name. Two copies of one
read-only primitive is exactly how read-side mutation creeps back into one of
them, so this keeps a single reader.

- database/sync_jobs.py: remove get_raw_sync_job and _read_raw_sync_job.
- routers/sync.py, utils/sync/uid_sequencer.py: point the five callers at
  get_sync_job and drop the now-duplicate import name.
- Tests that patched the removed name now patch get_sync_job.

One fixture needed a deliberate change rather than a rename.
test_sync_task_persistence_fence_terminalizes_backfill_and_releases_its_slot
runs a backfill task with no sequencer epoch, which first reads its own job to
decide whether the UID sequencer should adopt it. Its harness stubs the whole
database.sync_jobs module with a bare MagicMock, so before this change that read
returned an auto-mock whose status never equalled 'queued' and the migration
branch was skipped by accident. After consolidation the read consumes from the
test's explicit get_sync_job sequence, sees the real 'queued' job, and migrates.

Rather than feed it a sentinel, the test now states its scenario: the legacy
direct path with the sequencer disabled, no owner and nothing pending, and the
migration read gets its own entry ahead of the processor's three. Flipping the
sequencer on makes the test fail, so the gate is load-bearing rather than
decorative.

Verified: every test_sync_* suite, 952 passed and 0 failed; typecheck 0 errors.
No behaviour change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aryanorastar
aryanorastar marked this pull request as ready for review September 27, 2026 14:53
@aryanorastar

Copy link
Copy Markdown
Collaborator Author

#19379 merged, so this is no longer stacked. Merged current main on d9fff25262 — clean, no conflicts — and the diff against main is now just this change: 6 files, +22/−30. PR body updated to drop the stacked banner.

Re-checked that the consolidation is still complete, since main moved after I wrote it:

check result
references to get_raw_sync_job / _read_raw_sync_job anywhere 0 — no new callers landed
writes inside get_sync_job 0 — still a pure read
every test_sync_* suite 952 passed, 0 failed
bash backend/scripts/typecheck.sh 0 errors

@Git-on-my-level

Copy link
Copy Markdown
Member

Reviewed this against the base — the consolidation checks out.

Equivalence verified. On current main, get_sync_job is already read-only (post-#19379) and its body is identical to the removed _read_raw_sync_job modulo the local variable name: same Redis key read, same None on missing/corrupt/non-dict blob, no writes on either path. So the five call-site swaps are behavior-preserving:

  • backend/routers/sync.py — the four read sites (legacy-registration observed_job check, legacy adoption legacy_job, prior_job attempt recovery, and the post-response nonterminal-ack read) all treat the job as read-only here.
  • backend/utils/sync/uid_sequencer.py — reconcile_uid's terminal/None branching is unchanged by the rename.

The test updates are mostly mechanical patch-target renames, with one exception worth calling out as a improvement: in test_sync_cloud_tasks.py, test_sync_task_persistence_fence_terminalizes_backfill_and_releases_its_slot previously skipped the sequencer-adoption branch only because the harness's bare MagicMock returned an auto-mock whose status never matched. The rewrite pins the decision explicitly (sequencer disabled, no owner, nothing pending) and feeds the extra queued-job read deliberately. That converts an accidental pass into a stated one.

On the red unit checks: every failing test on this head (test_meeting_treatment.py ×8, test_chat_first_proactive_engine.py ×2, test_developer_from_segments_idempotency.py ×3) reproduces identically on the latest main CI runs — pre-existing breakage in the desktop meeting-treatment area, unrelated to sync readers. This PR's own suites (test_sync_cloud_tasks.py, test_sync_backfill_uid_sequencer.py, test_sync_dead_letters.py) and the Sync Cloud Tasks / Hermetic E2E / Listen Pusher gauntlets all pass on this head.

One reader instead of two is the right shape — the duplicated read-only primitive was exactly where read-side mutation could creep back in. Thanks for the careful PR description; the deferred-out-of-#19379 sequencing reads well in hindsight. Leaving the merge call to a maintainer while main's shards are red.


AI-generated maintainer feedback posted on David's behalf.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added backend Backend Task (python) positive-signal Automation verified a genuine fix/quality contribution labels Sep 27, 2026
@aryanorastar

Copy link
Copy Markdown
Collaborator Author

Merged current main (030954b), no conflicts.

  • Picks up main's fix for the meeting-treatment duration tests from fix(conversations): use transcript span for meeting-treatment duration #19391, which were the only failures in the earlier red Backend unit shards.
  • No new get_raw_sync_job / _read_raw_sync_job callers came in with main's sync changes. test_sync_cloud_tasks.py (101), test_sync_backfill_uid_sequencer.py (13), and test_sync_dead_letters.py (43) pass on the merged tree.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor consolidating two structurally-identical read-only job readers into one (get_sync_job); reduces the read/write-split-authority bug class from #19379. Not a bug fix itself, so approve-only per confidence gate (refactor/consolidation, not a defect fix) — leaving for Nik to land.

@Git-on-my-level

Copy link
Copy Markdown
Member

Follow-up verification on the rebased head (030954bd), since the earlier equivalence check predated the main merge:

  • Re-confirmed on base main that get_sync_job (backend/database/sync_jobs.py) is a pure read whose body matches the removed _read_raw_sync_job/get_raw_sync_job pair exactly modulo the local variable name: same {JOB_KEY_PREFIX} read, None on missing/corrupt/non-dict blob, no writes on either path. So the five call-site swaps — four read sites in backend/routers/sync.py (terminal-status observed_job check, legacy-adoption legacy_job, prior_job attempt recovery, post-response nonterminal-ack read) and reconcile_uid's terminal-cleanup read in backend/utils/sync/uid_sequencer.py — are behavior-preserving.
  • Repo-wide search finds no remaining references to the removed names outside the six files in this PR, so the consolidation is complete.
  • The one non-mechanical test change is handled well: test_sync_task_persistence_fence_terminalizes_backfill_and_releases_its_slot used to pass by accident (the stubbed module made the legacy-adoption read return an auto-mock whose status never equalled 'queued', silently skipping migration). Rather than reproducing that accident, the fixture now pins the decision off explicitly (sequencer disabled, no owner, nothing pending) and prepends one queued-job read for the adoption check — a real improvement in what the test states. The changes in test_sync_backfill_uid_sequencer.py, test_sync_cloud_tasks.py (remaining two sites), and test_sync_dead_letters.py are mechanical renames of the patched symbol.
  • Checks green on this head, including the Backend Hermetic Merge Gate and Sync Cloud Tasks Stack Gauntlet.

No behavior change found; this closes the read/write split-authority gap from #19379. Maintainer approval is already on this head — nothing further needed from my side.

Feedback generated by automated review on behalf of the maintainers.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

…only-reader

# Conflicts:
#	backend/database/sync_jobs.py
#	backend/utils/sync/uid_sequencer.py

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pure refactor (collapses two AST-identical read-only job readers into get_sync_job); approve-only per confidence gate, CI green.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The duplicate reader and several callers remain despite the PR’s stated purpose.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Attempts to consolidate sync job reads under get_sync_job, but the current head leaves the duplicate reader and callers intact.

Changes:

  • Migrates router reads and test mocks to get_sync_job.
  • Makes the legacy-path test fixture explicit.
  • Does not complete the repository-wide consolidation.
File Description
backend/​routers/​sync.py Migrates four reader call sites.
backend/​tests/​unit/​test_sync_cloud_tasks.py Updates mocks and legacy-path setup.
backend/​tests/​unit/​test_sync_dead_letters.py Updates the patched reader.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread backend/routers/sync.py Resolved

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this cleanup — the goal (one read-only job reader, no duplicate primitive) is exactly right, and the analysis in the description is solid. However, the merge of origin/main in bc7719a silently reverted the core of the change, and I'd like that restored before merge.

What happened: branch commit 6015ed2 delivered the full consolidation (6 files, +22/−30 — remove get_raw_sync_job/_read_raw_sync_job from backend/database/sync_jobs.py, migrate backend/utils/sync/uid_sequencer.py and test_sync_backfill_uid_sequencer.py). The merge then hit conflicts in exactly those two files and both were resolved toward main's side:

  • backend/database/sync_jobs.py still defines _read_raw_sync_job and get_raw_sync_job.
  • backend/utils/sync/uid_sequencer.py (lines 14/157) still imports and calls get_raw_sync_job.
  • main has since grown another caller — backend/scripts/recover_dev_failed_sync_backfill.py (#19647) — which was never migrated even in 6015ed2.

So at this head the diff is only 3 files (+18/−9), and the "two readers" problem this PR exists to eliminate is still fully present. The PR body's file list no longer matches the actual diff.

What survived is verified correct: all four production read sites in backend/routers/sync.py (observed_job in sync_local_files_v2, legacy_job, prior_job, and the post-body terminal check in run_sync_job) now call get_sync_job; I checked structurally that get_sync_job and _read_raw_sync_job on main have identical executable bodies modulo the local variable name, so this is a pure rename at runtime. The test updates follow correctly — test_sync_dead_letters.py's _run_job_harness patch tracks the rename, and the fence test's sequencer-off pinning with the duplicated first get_sync_job read is a sound way to keep the legacy direct path covered. One nit while you're in there: test_sync_cloud_tasks.py:3191/3205 still sets/asserts module.get_raw_sync_job, which is vacuous now that the router no longer imports it.

Ask:

  1. Re-apply the deletion from 6015ed2 on top of current main (cherry-pick or redo — either is fine).
  2. Also migrate the new recover_dev_failed_sync_backfill.py caller so no get_raw_sync_job references remain.
  3. Restore the test_sync_backfill_uid_sequencer.py patches from 6015ed2.
  4. Update the PR body's changed-file list to the final state.

Noting that this head already has a maintainer approval — flagging for changes anyway because the approved diff no longer contains the PR's stated change, which looks like an accidental conflict resolution rather than a decision. Once the consolidation is back, this should re-approve cleanly.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level removed the positive-signal Automation verified a genuine fix/quality contribution label Sep 29, 2026

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor consolidating duplicate read-only sync job readers — approve-only per confidence gate (not a defect fix). Verified diff: get_raw_sync_job fully removed from sync.py and all callers/tests repointed to get_sync_job; the copilot bot's 'duplicate remains' claim does not match the actual diff.

@aryanorastar

Copy link
Copy Markdown
Collaborator Author

Review follow-up: the consolidation is now complete. _read_raw_sync_job / get_raw_sync_job are removed; the UID sequencer, recovery script, and their tests all use get_sync_job. The focused 172-test backend set and the full local pre-push gate passed. GitHub’s shard fallback is still running on the current head.

@Git-on-my-level
Git-on-my-level dismissed their stale review October 2, 2026 00:09

Resolved on head e855d62: the consolidation silently reverted by the bc7719a merge is restored — get_raw_sync_job/_read_raw_sync_job removed from backend/database/sync_jobs.py, get_sync_job verified as a pure read, all call sites migrated, checks green.

@Git-on-my-level

Copy link
Copy Markdown
Member

The re-push restores exactly what the earlier changes-requested review asked for: the consolidation that the merge of origin/main in bc7719a had silently reverted. Verified on the current head — get_raw_sync_job and _read_raw_sync_job are gone from backend/database/sync_jobs.py, and get_sync_job (now the single reader) is a pure read: r.get, JSON parse, None on missing/corrupt/non-dict, no writes and no lease acquisition, structurally identical to the removed duplicate.

The call-site migrations in backend/routers/sync.py (the unregistered-terminal recheck in sync_local_files_v2, the legacy backfill adoption read in run_sync_job, and the prior_job attempt read), backend/utils/sync/uid_sequencer.py reconcile_uid, and all three reads in backend/scripts/recover_dev_failed_sync_backfill.py are behavior-preserving renames.

The test updates are mechanical monkeypatch retargets (test_recover_dev_failed_sync_backfill.py, test_sync_backfill_uid_sequencer.py, test_sync_dead_letters.py) plus one real fixture repair in test_sync_cloud_tasks.py: test_sync_task_persistence_fence_terminalizes_backfill_and_releases_its_slot used to skip the sequencer-adoption branch only because a bare MagicMock returned a non-queued auto-mock; it now pins that decision off explicitly and pre-pends the extra queued read to the side_effect sequence, so the legacy path is covered deliberately rather than by accident — a genuine test-quality improvement.

No remaining concerns from my side; it's ready to merge from a code-correctness standpoint.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-affirming: pure refactor collapsing two read-only job readers into get_sync_job. Not a bug fix -> approve-only.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor(sync): collapses duplicate read-only job readers — refactor not bug fix; approve-only despite CLEAN state and good tests

@aryanorastar
aryanorastar merged commit 3abb458 into BasedHardware:main Oct 5, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants