Skip to content

feat: add optional periodic config sync (autoSync) - #107

Open
Rahulsharma0810 wants to merge 1 commit into
iHildy:mainfrom
Rahulsharma0810:feat/auto-sync
Open

Rahulsharma0810 wants to merge 1 commit into
iHildy:mainfrom
Rahulsharma0810:feat/auto-sync

Conversation

@Rahulsharma0810

Copy link
Copy Markdown

Closes #98

What

Optional periodic config sync, off by default:

{ "autoSync": { "enabled": true, "intervalSec": 300, "push": true, "pull": true } }
  • intervalSec defaults to 300 and is clamped to a minimum of 30.
  • A timer (unref'd, cleared on dispose, re-evaluated against the config file on every tick, and skipped while another sync holds the lock) runs one pass:
    • fetch;
    • stage local config into the sync repo with the existing syncLocalToRepo;
    • remote ahead and no local edits: fast-forward and apply, with the usual "restart opencode" notice;
    • local edits and the remote is not ahead: commit and push;
    • local edits and the remote also changed: discard the staged copy, leave local files alone, and show one notice asking for a manual pull or push.

Why not just reuse the startup flow

runStartup pulls and applies the remote as soon as it is ahead, without checking for unpushed local edits. Running that every few minutes would silently overwrite edits made since the last sync, so the periodic pass checks for local changes first and stops on a conflict. I did not change startup behaviour in this PR.

Notes

  • opencode-synced.jsonc is itself a synced file, so enabling autoSync on one machine enables it on the others at their next pull. This is documented in the README and exercised by the test.
  • getAheadBehind in repo.ts is now exported so the cycle can reuse it.
  • The Turso syncIntervalSec setting is unchanged and still only applies to session sync.
  • Not included: a file watcher. A polling interval was the simplest option; a debounced watcher could be added later on top of the same cycle function.

Tests

  • config.test.ts: normalization (disabled by default, minimum interval, invalid values, push/pull defaults).
  • service.test.ts (auto sync), using two machines against a local bare remote and driving runAutoSyncCycle directly:
    • idle, push of a local edit, pull of a remote edit, idle again;
    • both sides edited: result is conflict, the local edit is kept, the sync clone is left clean, the remote is untouched, and the notice is shown once across two passes;
    • push: false stages nothing and leaves the clone clean; pull: false skips when the remote is ahead;
    • timer scheduling: no timer when disabled, one setInterval with the configured period when enabled, cleared on dispose.
  • bun run check, bun run test (210 passing) and bun run build pass.

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.

Config sync runs only at startup: add optional periodic / change-triggered sync

1 participant