Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Create `~/.config/opencode/opencode-synced.jsonc`:
"includeModelFavorites": true,
"includeOpencodeSkills": true,
"includeAgentsDir": true,
"mode": "sync",
"extraSecretPaths": [],
"extraConfigPaths": [],
}
Expand All @@ -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/`
Expand Down
6 changes: 6 additions & 0 deletions src/sync/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand Down
9 changes: 9 additions & 0 deletions src/sync/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,6 +67,7 @@ export interface SyncConfig {
includeModelFavorites?: boolean;
includeOpencodeSkills?: boolean;
includeAgentsDir?: boolean;
mode?: SyncMode;
secretsBackend?: SecretsBackendConfig;
extraSecretPaths?: string[];
extraConfigPaths?: string[];
Expand All @@ -75,6 +82,7 @@ export interface NormalizedSyncConfig extends SyncConfig {
includeModelFavorites: boolean;
includeOpencodeSkills: boolean;
includeAgentsDir: boolean;
mode: SyncMode;
secretsBackend?: SecretsBackendConfig;
extraSecretPaths: string[];
extraConfigPaths: string[];
Expand Down Expand Up @@ -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 : [],
Expand Down
54 changes: 54 additions & 0 deletions src/sync/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> =>
(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();
Expand Down
24 changes: 20 additions & 4 deletions src/sync/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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, {
Expand Down
Loading