Skip to content

fix(kb): last_sync_at only moves forward, so an earlier-finishing sync cannot widen the next window (#20758) - #20801

Merged
mrveiss merged 7 commits into
mainfrom
issue-20758-monotonic-last-sync
Nov 4, 2026
Merged

mrveiss merged 7 commits into
mainfrom
issue-20758-monotonic-last-sync

Conversation

@mrveiss

@mrveiss mrveiss commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Closes #20758

Thinking Path

Since #20556, every sync path (scheduler, connector create, Sync button) holds the per-connector lease, so two runs overlap only after a lease is lost. In that window, the run that finishes first could still set an older last_sync_at over a newer one. The next incremental sync would then use the older value as since and re-read more than it needs to. This makes the time monotonic at both places it is written:

  • Inside the WATCH transaction, a stored time later than this run's is kept. The call still reports "recorded", because the connector exists and a later sync is on record.
  • The live instance.config.last_sync_at is assigned only when it is unset or older.
  • All three times go through one _aware() before comparing. A naive time is read as UTC, an ISO string is parsed with parse_utc_iso (Z or offset), and an unreadable stored value is logged and replaced.

What Changed

File Change
autobot-backend/api/_connector_sync_result.py _aware, _is_later; _write_last_sync takes a datetime and keeps a later stored time; record_last_sync compares before setting the live value
autobot-backend/api/connector_sync_result_19714_test.py Existing tests pass a datetime; 4 new tests

Verification

api/connector_sync_result_19714_test.py: 15 passed. New tests:

  • test_an_earlier_finishing_run_keeps_the_later_stored_time
  • test_a_later_run_still_moves_the_stored_time_forward (the control, with a Z-suffixed stored time)
  • test_an_unreadable_stored_time_is_replaced
  • test_out_of_order_completion_leaves_the_later_time_everywhere: the newer run finishes first, then the older one. Both the stored and the live time stay at the newer run's.

Mutation check: with _is_later forced to True (the old unconditional write), the earlier-finishing test and the out-of-order test fail (2 failed, 13 passed).

Model Used

Claude Opus 5.5

@mrveiss mrveiss added bug Something isn't working priority: low backend phase:M1.8 Knowledge base: upload, web sources and chat retrieval work end to end labels Nov 4, 2026
@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 58 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between 474c4b8 and d4c5a27.

Files selected for processing: 2
  • autobot-backend/api/_connector_sync_result.py
  • autobot-backend/api/connector_sync_result_19714_test.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Notice: 29 open PRs — past the runaway threshold (25)

This is not a request to defer this PR. There is no cap on open PRs (#19397) -- nothing blocks opening or pushing a PR.

This notice only means the count is high enough to be worth a glance for a runaway — something opening PRs in a loop, or a merge pipeline that has stalled so nothing is draining.

Currently open:

If the queue is draining normally, ignore this. Otherwise:

  1. Check whether CI is dispatching at all — see the ci-dispatch-watchdog status on these PRs
  2. Approved PRs land through the merge train the coordinator sequences (merge commits, never squash) -- check the open chore(merge-train) PR is moving, not each PR on its own
  3. Look for a loop opening near-identical PRs

Warn-only runaway detector — .github/workflows/pr-queue-gate.yml. It never blocks a merge.

@mrveiss

mrveiss commented Nov 4, 2026

Copy link
Copy Markdown
Owner Author

Code review on PR #20801 (head 01bc846): APPROVE

Checked against the three ACs of #20758. All are met; no CRITICAL, HIGH or MEDIUM finding.

  • Comparison: _aware makes naive datetimes UTC, and parse_utc_iso handles Z, offset and naive strings. None or an unreadable stored value counts as "unset", so a first sync writes. The test uses a naive _EARLIER and a Z-suffixed stored string.
  • Atomicity: The compare runs in Python, but inside the existing WATCH/MULTI on the key. A concurrent writer trips WatchError and the loop re-reads and re-compares, so two writers cannot regress it. No SQL is involved: the store is a Redis JSON record.
  • Skipped timestamp: last_sync_at is the only field this module writes, so there is nothing to keep consistent. Status, counts and history are written elsewhere (knowledge_connectors.py around line 367) and still update. A skipped timestamp returns True, so history is still recorded.
  • Single writer: _write_last_sync and record_last_sync are defined only in _connector_sync_result.py; the only caller is knowledge_connectors.py.
  • Function size: all functions are 30 lines or fewer.
  • Tests: The out-of-order test and the earlier-finishing test both depend on _is_later. I did not run them, so I can't confirm the PR's "2 failed" mutation result. From the code it is plausible.
Severity file:line Issue Fix
LOW autobot-backend/api/_connector_sync_result.py:86-88 When Redis already holds a later time, _write_last_sync returns True without saying so. If this instance's live last_sync_at is unset or older, as with a config loaded before the other run finished, the status route shows the older time while Redis holds the newer one. The out-of-order test uses one shared instance, so it does not cover this. Optional follow-up: have _write_last_sync return the effective time and assign the live value from that.
LOW autobot-backend/api/_connector_sync_result.py:85 The compared field is completed_at (finish time), not started_at. A run that started earlier but finished later still wins, and the later since can skip changes made during that run. This matches the AC and the pre-existing semantics, so I'm noting it, not requesting a change. None for this PR.
LOW autobot-backend/api/connector_sync_result_19714_test.py No test for an equal-timestamp tie (> means no rewrite) or a live value given as an ISO string. Optional.

…s holds, so a run behind it cannot set an older one (#20758)
@mrveiss

mrveiss commented Nov 4, 2026

Copy link
Copy Markdown
Owner Author

Code review on PR #20801 (head a202384): APPROVE

Delta review of a202384 against 01bc846 (one commit). LOW 1 is resolved:

  • _write_last_sync returns the time Redis holds afterwards: when, or a stored time that is later or equal. A tie is never rewritten.
  • record_last_sync sets the live last_sync_at from that value, and only forward, so the status route can no longer show an older time than Redis.
  • Two new tests use a separate store and instance (stored later, so live equals stored) and a tie (no set, record byte-equal). Each fails under its matching mutation.
    No findings.

@mrveiss
mrveiss merged commit 2901ebd into main Nov 4, 2026
3 of 4 checks passed
@mrveiss
mrveiss deleted the issue-20758-monotonic-last-sync branch November 4, 2026 17:21
mrveiss added a commit that referenced this pull request Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend bug Something isn't working phase:M1.8 Knowledge base: upload, web sources and chat retrieval work end to end priority: low

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(kb): overlapping connector syncs can move last_sync_at backward

1 participant