@@ -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' ;
1824import { RepoStorageBackend } from '../storage/index.js' ;
1925import { FileWatcher } from '../sync/watcher/index.js' ;
2026import 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 */
2332const 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 */
191218async 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)
0 commit comments