Skip to content

Sync with the server at least once per run - #143

Closed
Ketan-K17 wants to merge 1 commit into
jonhadfield:mainfrom
Ketan-K17:always-sync-reads
Closed

Ketan-K17 wants to merge 1 commit into
jonhadfield:mainfrom
Ketan-K17:always-sync-reads

Conversation

@Ketan-K17

Copy link
Copy Markdown
Contributor

Fixes #138

Summary

cache.Sync skips the API call when nothing changed locally and the last sync was under 5 minutes ago, so a new sn run could miss changes made in other clients. The first sync in each process now always calls the API. Later syncs in the same run can still skip it, so the savings from gosn-v2#92 within a run are kept.

 SyncQuiet(si)                         # was: sync(si), unexported
   cache.Sync(SyncInput{
     Session, Close,
+    AlwaysSync: si.AlwaysSync || !syncedWithServer,
   })
+  syncedWithServer = true (on success)

The 20 direct cache.Sync calls in cmd/sncli and internal/sncli/tasks.go now go through SyncQuiet (no spinner, same as before). The old sync helper also copied only Session and Close, so it dropped AlwaysSync.

Evidence

Two cache directories, one standing in for another client:

A: sn get notes --title x          -> 3
B: sn add note --title x-4         (other client)
A: sn get notes --title x          -> before: 3   after: 4

go test ./... passes.

Merge Danger

Door: two-way

Blast Radius: API calls

Each run makes at most one extra sync request, and only when it would have skipped. Runs within 5 minutes of each other no longer reuse the cached state.

cache.Sync skips the API call when nothing changed locally and the last
sync was under five minutes ago, so a new run could miss changes made in
other clients. Force the first sync in each process with AlwaysSync, and
route sn-cli's direct cache.Sync calls through the same helper. Later
syncs in a run can still skip the call. The Sync wrapper also no longer
drops AlwaysSync when it copies SyncInput.

Fixes jonhadfield#138
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@jonhadfield

Copy link
Copy Markdown
Owner

Landed on main as ea96bfb.

@jonhadfield jonhadfield closed this Nov 2, 2026
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.

Reads miss changes from other clients for up to 5 minutes

2 participants