Skip to content

Commit dcaf4c5

Browse files
committed
Fix: Write pulled data to disk in initial and interval sync
The sync was updating local state after pull but not writing the actual pulled data to disk. This caused subsequent syncs to see stale local data and push it, overwriting remote.
1 parent 3b9f51b commit dcaf4c5

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

src/plugin/plugin.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@ import {
1414
updateConfig,
1515
initializeEngine,
1616
} from './state-manager.js';
17-
import { getTokenSource, loadLocalData, saveLocalState } from '../data/index.js';
17+
import {
18+
getTokenSource,
19+
loadLocalData,
20+
saveLocalState,
21+
writeLocalData,
22+
deleteTombstonedItems,
23+
} from '../data/index.js';
1824
import { RepoStorageBackend } from '../storage/index.js';
1925
import { FileWatcher } from '../sync/watcher/index.js';
2026
import type { PluginState } from './types.js';
27+
import type { CategoryData } from '../sync/operations/types.js';
28+
import type { SyncResult } from '../types/sync.js';
29+
import { syncLog } from '../sync/engine/logger.js';
2130

2231
/** Default repo name for sync storage */
2332
const DEFAULT_REPO_NAME = '.opencode-sync';
@@ -187,6 +196,24 @@ async function ensureStorageExists(pathConfig: PathConfig): Promise<void> {
187196
}
188197
}
189198

199+
/** Write pulled data to disk after a successful pull/merge */
200+
async function writePulledData(pathConfig: PathConfig, result: SyncResult): Promise<void> {
201+
if (result.action !== 'pulled' && result.action !== 'merged') {
202+
return;
203+
}
204+
205+
if (result.pulledData) {
206+
const data = result.pulledData as CategoryData[];
207+
syncLog(`[WRITE] Writing ${String(data.length)} categories to disk`);
208+
await writeLocalData(pathConfig, data);
209+
syncLog(`[WRITE] Finished writing to disk`);
210+
}
211+
212+
if (result.tombstonedItems) {
213+
await deleteTombstonedItems(pathConfig, result.tombstonedItems);
214+
}
215+
}
216+
190217
/** Persist engine's local state to disk after successful sync */
191218
async function persistLocalState(pathConfig: PathConfig): Promise<void> {
192219
const state = getPluginState();
@@ -227,6 +254,8 @@ function performInitialSync(pathConfig: PathConfig): void {
227254
const dur = Date.now() - syncStart;
228255

229256
if (result.success && result.action !== 'error') {
257+
// Write pulled data to disk BEFORE persisting state
258+
await writePulledData(pathConfig, result);
230259
await persistLocalState(pathConfig);
231260
log(`Initial sync complete in ${String(dur)}ms: ${result.message}`);
232261
} else {
@@ -301,8 +330,9 @@ function startIntervalSync(pathConfig: PathConfig): void {
301330
try {
302331
const { categories } = await loadLocalData(pathConfig, config.sync);
303332
const result = await engine.sync(categories);
304-
// Persist state after successful sync
333+
// Write pulled data and persist state after successful sync
305334
if (result.success) {
335+
await writePulledData(pathConfig, result);
306336
await persistLocalState(pathConfig);
307337
}
308338
// Log interval sync results (both success and no-change)

src/plugin/sync-handler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
deleteTombstonedItems,
1515
} from '../data/index.js';
1616
import { getPluginState } from './state-manager.js';
17+
import { syncLog } from '../sync/engine/logger.js';
1718

1819
type LogLevel = 'error' | 'info' | 'debug' | 'warn';
1920

@@ -62,14 +63,20 @@ async function handleSyncSuccess(
6263

6364
// Write pulled data to local filesystem
6465
if (result.action === 'pulled' || result.action === 'merged') {
66+
syncLog(
67+
`[WRITE] Action: ${result.action}, pulledData: ${result.pulledData ? 'present' : 'missing'}`
68+
);
6569
if (result.pulledData) {
6670
const data = result.pulledData as CategoryData[];
67-
console.warn(`[SYNC-DEBUG] Writing ${String(data.length)} categories to disk`);
71+
syncLog(`[WRITE] Writing ${String(data.length)} categories to disk`);
6872
await writeLocalData(pathConfig, data);
73+
syncLog(`[WRITE] Finished writing to disk`);
6974
}
7075
if (result.tombstonedItems) {
7176
await deleteTombstonedItems(pathConfig, result.tombstonedItems);
7277
}
78+
} else {
79+
syncLog(`[WRITE] Skipping write - action: ${result.action}`);
7380
}
7481

7582
if (newState) {

0 commit comments

Comments
 (0)