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
11 changes: 11 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,
"pullMode": "replace",
"extraSecretPaths": [],
"extraConfigPaths": [],
}
Expand All @@ -155,6 +156,16 @@ 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 mode

`pullMode` controls how `/sync-pull` (and the startup pull) applies synced **directories**:

- `"replace"` (default): each synced directory is replaced by the repo copy, so files that exist
only on this machine inside that directory (for example a local-only agent, command or plugin)
are deleted.
- `"merge"`: repo files are copied over local ones and files that exist only locally are left in
place. Files deleted in the repo are not deleted locally either.

Disable default directory sync by setting:
- `"includeOpencodeSkills": false` to skip `~/.config/opencode/skills/`
- `"includeAgentsDir": false` to skip `~/.agents/`
Expand Down
40 changes: 40 additions & 0 deletions src/sync/apply.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,3 +902,43 @@ describe('MCP secret scrub round trip', () => {
});
});
});

describe('syncRepoToLocal pull mode for directories', () => {
async function seedDirectories(root: string): Promise<{ plan: SyncPlan; localDir: string }> {
const repoRoot = path.join(root, 'repo');
const homeDir = path.join(root, 'home');
const repoDir = path.join(repoRoot, 'config', 'agents');
const localDir = path.join(homeDir, '.config', 'opencode', 'agents');
await fs.mkdir(repoDir, { recursive: true });
await fs.mkdir(localDir, { recursive: true });
await fs.writeFile(path.join(repoDir, 'shared.md'), 'from-repo', 'utf8');
await fs.writeFile(path.join(localDir, 'shared.md'), 'stale-local', 'utf8');
await fs.writeFile(path.join(localDir, 'local-only.md'), 'keep-me', 'utf8');
const plan = createPlan(repoRoot, homeDir, [
{ localPath: localDir, repoPath: repoDir, type: 'dir', isSecret: false, isConfigFile: false },
]);
return { plan, localDir };
}

it('replaces the directory by default, removing local-only files', async () => {
await withTempDir(async (root) => {
const { plan, localDir } = await seedDirectories(root);

await syncRepoToLocal(plan, null);

expect(await fs.readFile(path.join(localDir, 'shared.md'), 'utf8')).toBe('from-repo');
await expect(fs.access(path.join(localDir, 'local-only.md'))).rejects.toThrow();
});
});

it('keeps local-only files in merge mode while updating shared files', async () => {
await withTempDir(async (root) => {
const { plan, localDir } = await seedDirectories(root);

await syncRepoToLocal(plan, null, { pullMode: 'merge' });

expect(await fs.readFile(path.join(localDir, 'shared.md'), 'utf8')).toBe('from-repo');
expect(await fs.readFile(path.join(localDir, 'local-only.md'), 'utf8')).toBe('keep-me');
});
});
});
21 changes: 13 additions & 8 deletions src/sync/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
chmodIfExists,
deepMerge,
hasOwn,
type PullMode,
parseJsonc,
pathExists,
stripOverrides,
Expand Down Expand Up @@ -51,18 +52,19 @@ interface ExtraPathManifest {
export async function syncRepoToLocal(
plan: SyncPlan,
overrides: Record<string, unknown> | null,
options: { chunkOptions?: ChunkOptions } = {}
options: { chunkOptions?: ChunkOptions; pullMode?: PullMode } = {}
): Promise<void> {
const merge = options.pullMode === 'merge';
for (const item of plan.items) {
if (item.chunkLargeFiles) {
await copyChunkableItemFromRepo(item, plan.repoRoot, options.chunkOptions);
continue;
}
await copyItem(item.repoPath, item.localPath, item.type);
await copyItem(item.repoPath, item.localPath, item.type, false, merge);
}

await applyExtraPaths(plan, plan.extraConfigs);
await applyExtraPaths(plan, plan.extraSecrets);
await applyExtraPaths(plan, plan.extraConfigs, merge);
await applyExtraPaths(plan, plan.extraSecrets, merge);

if (overrides && Object.keys(overrides).length > 0) {
await applyOverridesToLocalConfig(plan, overrides);
Expand Down Expand Up @@ -424,7 +426,8 @@ async function copyItem(
sourcePath: string,
destinationPath: string,
type: SyncItem['type'],
removeWhenMissing = false
removeWhenMissing = false,
merge = false
): Promise<void> {
if (
type === 'file' &&
Expand All @@ -447,7 +450,9 @@ async function copyItem(
return;
}

await removePath(destinationPath);
if (!merge) {
await removePath(destinationPath);
}
await copyDirRecursive(sourcePath, destinationPath);
}

Expand Down Expand Up @@ -584,7 +589,7 @@ async function removePath(targetPath: string): Promise<void> {
await fs.rm(targetPath, { recursive: true, force: true });
}

async function applyExtraPaths(plan: SyncPlan, extra: ExtraPathPlan): Promise<void> {
async function applyExtraPaths(plan: SyncPlan, extra: ExtraPathPlan, merge = false): Promise<void> {
const allowlist = extra.allowlist;
if (allowlist.length === 0) return;

Expand All @@ -606,7 +611,7 @@ async function applyExtraPaths(plan: SyncPlan, extra: ExtraPathPlan): Promise<vo

if (!(await pathExists(repoPath))) continue;

await copyItem(repoPath, localPath, entryType);
await copyItem(repoPath, localPath, entryType, false, merge);
await applyExtraPathModes(localPath, entry);
}
}
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 pullMode to replace and accepts merge', () => {
expect(normalizeSyncConfig({}).pullMode).toBe('replace');
expect(normalizeSyncConfig({ pullMode: 'merge' }).pullMode).toBe('merge');
expect(normalizeSyncConfig({ pullMode: 'bogus' as never }).pullMode).toBe('replace');
});

it('defaults extra path lists when omitted', () => {
const normalized = normalizeSyncConfig({ includeSecrets: true });
expect(normalized.extraSecretPaths).toEqual([]);
Expand Down
11 changes: 11 additions & 0 deletions src/sync/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export interface SecretsBackendConfig {

export type SessionBackendType = 'git' | 'turso';

/**
* How `pull` applies synced directories to the local machine.
* - `replace` (default): each synced directory is replaced by the repo copy, so local-only files
* inside it are deleted.
* - `merge`: repo files are copied over local ones and local-only files are left untouched.
*/
export type PullMode = 'replace' | 'merge';

export interface TursoSessionBackendSettings {
database?: string;
url?: string;
Expand Down Expand Up @@ -61,6 +69,7 @@ export interface SyncConfig {
includeModelFavorites?: boolean;
includeOpencodeSkills?: boolean;
includeAgentsDir?: boolean;
pullMode?: PullMode;
secretsBackend?: SecretsBackendConfig;
extraSecretPaths?: string[];
extraConfigPaths?: string[];
Expand All @@ -75,6 +84,7 @@ export interface NormalizedSyncConfig extends SyncConfig {
includeModelFavorites: boolean;
includeOpencodeSkills: boolean;
includeAgentsDir: boolean;
pullMode: PullMode;
secretsBackend?: SecretsBackendConfig;
extraSecretPaths: string[];
extraConfigPaths: string[];
Expand Down Expand Up @@ -188,6 +198,7 @@ export function normalizeSyncConfig(config: SyncConfig): NormalizedSyncConfig {
includeModelFavorites,
includeOpencodeSkills,
includeAgentsDir,
pullMode: config.pullMode === 'merge' ? 'merge' : 'replace',
secretsBackend: normalizeSecretsBackend(config.secretsBackend),
extraSecretPaths: Array.isArray(config.extraSecretPaths) ? config.extraSecretPaths : [],
extraConfigPaths: Array.isArray(config.extraConfigPaths) ? config.extraConfigPaths : [],
Expand Down
6 changes: 3 additions & 3 deletions src/sync/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ export function createSyncService(ctx: SyncServiceContext): SyncService {

const overrides = await loadOverrides(locations);
const plan = buildSyncPlan(config, locations, repoRoot);
await syncRepoToLocal(plan, overrides);
await syncRepoToLocal(plan, overrides, { pullMode: config.pullMode });

await updateState(locations, {
lastPull: new Date().toISOString(),
Expand Down Expand Up @@ -1020,7 +1020,7 @@ export function createSyncService(ctx: SyncServiceContext): SyncService {

const overrides = await loadOverrides(locations);
const plan = buildSyncPlan(config, locations, repoRoot);
await syncRepoToLocal(plan, overrides);
await syncRepoToLocal(plan, overrides, { pullMode: config.pullMode });
await runSecretsPullIfConfigured(config);

await updateState(locations, {
Expand Down Expand Up @@ -1479,7 +1479,7 @@ async function runStartup(
log.info('Pulled remote changes', { branch });
const overrides = await loadOverrides(locations);
const plan = buildSyncPlan(config, locations, repoRoot);
await syncRepoToLocal(plan, overrides);
await syncRepoToLocal(plan, overrides, { pullMode: config.pullMode });
await options.runSecretsPullIfConfigured(config);
await updateState(locations, {
lastPull: new Date().toISOString(),
Expand Down
Loading