diff --git a/README.md b/README.md index d371aee..97a02b6 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ Create `~/.config/opencode/opencode-synced.jsonc`: "includeModelFavorites": true, "includeOpencodeSkills": true, "includeAgentsDir": true, + "mode": "sync", "extraSecretPaths": [], "extraConfigPaths": [], } @@ -155,6 +156,19 @@ Create `~/.config/opencode/opencode-synced.jsonc`: Review it before syncing, keep the sync repository private when needed, or set `"includeAgentsDir": false` to opt out. +### Pull-only mode + +Set `"mode": "pull-only"` on machines that should only receive config, such as a headless +server, a shared machine or an appliance. In this mode: + +- startup and `/sync-pull` still fetch and apply remote changes; +- startup never stages or pushes this machine's config, and `push` and `secrets-push` return a + "skipped" message instead of committing; +- `status` shows `Mode: pull-only`. + +The default is `"mode": "sync"`. A read-only deploy key on the remote is a good complement, since it +makes an accidental push impossible at the Git level as well. + Disable default directory sync by setting: - `"includeOpencodeSkills": false` to skip `~/.config/opencode/skills/` - `"includeAgentsDir": false` to skip `~/.agents/` diff --git a/src/sync/config.test.ts b/src/sync/config.test.ts index 7e4ff1c..8e473ff 100644 --- a/src/sync/config.test.ts +++ b/src/sync/config.test.ts @@ -152,6 +152,12 @@ describe('normalizeSyncConfig', () => { expect(normalized.includeAgentsDir).toBe(false); }); + it('defaults mode to sync and accepts pull-only', () => { + expect(normalizeSyncConfig({}).mode).toBe('sync'); + expect(normalizeSyncConfig({ mode: 'pull-only' }).mode).toBe('pull-only'); + expect(normalizeSyncConfig({ mode: 'bogus' as never }).mode).toBe('sync'); + }); + it('defaults extra path lists when omitted', () => { const normalized = normalizeSyncConfig({ includeSecrets: true }); expect(normalized.extraSecretPaths).toEqual([]); diff --git a/src/sync/config.ts b/src/sync/config.ts index d22e1cc..3cba72b 100644 --- a/src/sync/config.ts +++ b/src/sync/config.ts @@ -26,6 +26,12 @@ export interface SecretsBackendConfig { export type SessionBackendType = 'git' | 'turso'; +/** + * - `sync` (default): pull remote changes and push local changes. + * - `pull-only`: only apply remote changes; never commit or push this machine's config. + */ +export type SyncMode = 'sync' | 'pull-only'; + export interface TursoSessionBackendSettings { database?: string; url?: string; @@ -61,6 +67,7 @@ export interface SyncConfig { includeModelFavorites?: boolean; includeOpencodeSkills?: boolean; includeAgentsDir?: boolean; + mode?: SyncMode; secretsBackend?: SecretsBackendConfig; extraSecretPaths?: string[]; extraConfigPaths?: string[]; @@ -75,6 +82,7 @@ export interface NormalizedSyncConfig extends SyncConfig { includeModelFavorites: boolean; includeOpencodeSkills: boolean; includeAgentsDir: boolean; + mode: SyncMode; secretsBackend?: SecretsBackendConfig; extraSecretPaths: string[]; extraConfigPaths: string[]; @@ -188,6 +196,7 @@ export function normalizeSyncConfig(config: SyncConfig): NormalizedSyncConfig { includeModelFavorites, includeOpencodeSkills, includeAgentsDir, + mode: config.mode === 'pull-only' ? 'pull-only' : 'sync', secretsBackend: normalizeSecretsBackend(config.secretsBackend), extraSecretPaths: Array.isArray(config.extraSecretPaths) ? config.extraSecretPaths : [], extraConfigPaths: Array.isArray(config.extraConfigPaths) ? config.extraConfigPaths : [], diff --git a/src/sync/service.test.ts b/src/sync/service.test.ts index 5d12cb9..27fc7ec 100644 --- a/src/sync/service.test.ts +++ b/src/sync/service.test.ts @@ -294,6 +294,60 @@ describe('explicit Git remote service flow', () => { }); }, 30_000); + it('never pushes from a pull-only machine but still pulls', async () => { + await withIsolatedEnvironment(async (root) => { + const testShell = createTestShell(); + const remotePath = path.join(root, 'sync-remote.git'); + await testShell`git init --bare ${remotePath}`.quiet(); + const remoteCommitCount = async (): Promise => + (await testShell`git --git-dir ${remotePath} rev-list --count main`.text()).trim(); + + useIsolatedHome(path.join(root, 'machine-a')); + const machineALocations = resolveSyncLocations(); + await fs.mkdir(machineALocations.configRoot, { recursive: true }); + await fs.writeFile( + path.join(machineALocations.configRoot, 'opencode.json'), + '{"theme":"machine-a"}\n', + 'utf8' + ); + const machineAService = createSyncService({ client: createClient(), $: testShell }); + await machineAService.init({ repo: remotePath, branch: 'main' }); + const commitsAfterInit = await remoteCommitCount(); + + useIsolatedHome(path.join(root, 'machine-b')); + const machineBLocations = resolveSyncLocations(); + const machineBService = createSyncService({ client: createClient(), $: testShell }); + await machineBService.link({ repo: remotePath, branch: 'main' }); + const linkedConfig = await loadSyncConfig(machineBLocations); + if (!linkedConfig) throw new Error('Expected machine B sync config.'); + await writeSyncConfig(machineBLocations, { ...linkedConfig, mode: 'pull-only' }); + await expect(machineBService.status()).resolves.toContain('Mode: pull-only'); + + await fs.writeFile( + path.join(machineBLocations.configRoot, 'opencode.json'), + '{"theme":"machine-b-local"}\n', + 'utf8' + ); + await expect(machineBService.push()).resolves.toContain('pull-only mode'); + await machineBService.startupSync(); + expect(await remoteCommitCount()).toBe(commitsAfterInit); + + useIsolatedHome(path.join(root, 'machine-a')); + await fs.writeFile( + path.join(machineALocations.configRoot, 'opencode.json'), + '{"theme":"machine-a-v2"}\n', + 'utf8' + ); + await machineAService.push(); + + useIsolatedHome(path.join(root, 'machine-b')); + await machineBService.pull(); + await expect( + fs.readFile(path.join(machineBLocations.configRoot, 'opencode.json'), 'utf8') + ).resolves.toContain('machine-a-v2'); + }); + }, 30_000); + it('fails closed with recovery instructions for an oversized unpushed session commit', async () => { await withIsolatedEnvironment(async (root) => { const testShell = createTestShell(); diff --git a/src/sync/service.ts b/src/sync/service.ts index ba27d7a..a7b64ce 100644 --- a/src/sync/service.ts +++ b/src/sync/service.ts @@ -726,6 +726,7 @@ export function createSyncService(ctx: SyncServiceContext): SyncService { const statusLines = [ `Repo: ${repoIdentifier}`, `Branch: ${branch}`, + `Mode: ${config.mode}`, `Secrets: ${includeSecrets}`, `Secrets backend: ${secretsBackend}`, `MCP secrets: ${includeMcpSecrets}`, @@ -1043,6 +1044,9 @@ export function createSyncService(ctx: SyncServiceContext): SyncService { push: () => runExclusive(async () => { const config = await getConfigOrThrow(locations); + if (config.mode === 'pull-only') { + return PULL_ONLY_MESSAGE; + } const repoRoot = resolveRepoRoot(config, locations); await ensureRepoCloned(ctx.$, config, repoRoot); await ensureSensitiveSyncPolicy(ctx, locations, config); @@ -1135,15 +1139,19 @@ export function createSyncService(ctx: SyncServiceContext): SyncService { }) ), secretsPush: () => - runExclusive(() => - runSecretsCommand(async (backend) => { + runExclusive(async () => { + const config = await loadSyncConfig(locations); + if (config?.mode === 'pull-only') { + return PULL_ONLY_MESSAGE; + } + return await runSecretsCommand(async (backend) => { const result = await pushSecretsWithBackend(backend); if (result === 'skipped') { return 'Secrets unchanged; skipping 1Password push.'; } return 'Pushed secrets to 1Password.'; - }) - ), + }); + }), secretsStatus: () => runExclusive(() => runSecretsCommand(async (backend) => { @@ -1419,6 +1427,9 @@ export function createSyncService(ctx: SyncServiceContext): SyncService { }; } +const PULL_ONLY_MESSAGE = + 'Skipped: opencode-synced is in pull-only mode (set "mode": "sync" in opencode-synced.jsonc to push).'; + function assertValidSecretsBackend(config: NormalizedSyncConfig): void { const resolution = resolveSecretsBackendConfig(config); if (resolution.state === 'invalid') { @@ -1489,6 +1500,11 @@ async function runStartup( return; } + if (config.mode === 'pull-only') { + log.debug('Pull-only mode, not pushing local changes'); + return; + } + const overrides = await loadOverrides(locations); const plan = buildSyncPlan(config, locations, repoRoot); await syncLocalToRepo(plan, overrides, {