-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground-sync-manager.js
More file actions
executable file
·478 lines (414 loc) · 13.5 KB
/
background-sync-manager.js
File metadata and controls
executable file
·478 lines (414 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/env node
/**
* StackMemory Background Sync Manager
* Handles all background synchronization tasks:
* - Linear task sync
* - Frame and context backup
* - Cross-session sync
* - Cloud backup (S3/GCS)
* - Redis cache sync
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Load environment variables from .env file (check .env first as per user preference)
dotenv.config({
path: path.join(__dirname, '..', '.env'),
override: true,
silent: true,
});
// Sync intervals (in milliseconds)
const SYNC_INTERVALS = {
linear: 60 * 60 * 1000, // 1 hour - Linear task sync
context: 15 * 60 * 1000, // 15 minutes - Context and frame sync
backup: 4 * 60 * 60 * 1000, // 4 hours - Cloud backup
redis: 5 * 60 * 1000, // 5 minutes - Redis cache sync
crossSession: 10 * 60 * 1000, // 10 minutes - Cross-session sync
};
class BackgroundSyncManager {
constructor() {
this.syncTasks = new Map();
this.stats = {
linear: { count: 0, lastSync: null, errors: 0 },
context: { count: 0, lastSync: null, errors: 0 },
backup: { count: 0, lastSync: null, errors: 0 },
redis: { count: 0, lastSync: null, errors: 0 },
crossSession: { count: 0, lastSync: null, errors: 0 },
};
this.logFile = path.join(
__dirname,
'..',
'.stackmemory',
'sync-manager.log'
);
this.isRunning = false;
}
log(message, level = 'INFO') {
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] [${level}] ${message}\n`;
console.log(logMessage.trim());
try {
fs.appendFileSync(this.logFile, logMessage);
} catch (error) {
console.error('Failed to write to log file:', error.message);
}
}
/**
* Sync Linear tasks (opt-in only)
*/
async syncLinear() {
// Require explicit opt-in via ENABLE_LINEAR_SYNC=true
if (process.env.ENABLE_LINEAR_SYNC !== 'true') {
return; // Silent skip - Linear sync is opt-in
}
if (
!process.env.LINEAR_API_KEY &&
!process.env.STACKMEMORY_LINEAR_API_KEY
) {
this.log('Linear sync skipped - no API key', 'WARN');
return;
}
this.log('Starting Linear sync...');
try {
const { stdout, stderr } = await execAsync(
`node ${path.join(__dirname, 'sync-linear-graphql.js')}`
);
// Parse output for summary
const addedMatch = stdout.match(/Added to local: (\d+)/);
const added = addedMatch ? addedMatch[1] : '0';
this.stats.linear.count++;
this.stats.linear.lastSync = new Date();
this.log(`Linear sync completed - added ${added} tasks`);
} catch (error) {
this.stats.linear.errors++;
this.log(`Linear sync failed: ${error.message}`, 'ERROR');
}
}
/**
* Sync context and frames
*/
async syncContext() {
this.log('Starting context sync...');
try {
const homeDir = process.env.HOME;
const stackMemoryDir = path.join(homeDir, '.stackmemory');
// Get all session directories
const sessionsDir = path.join(stackMemoryDir, 'sessions');
const sharedContextDir = path.join(stackMemoryDir, 'shared-context');
// Count items to sync
let sessionCount = 0;
let contextCount = 0;
if (fs.existsSync(sessionsDir)) {
sessionCount = fs.readdirSync(sessionsDir).length;
}
if (fs.existsSync(sharedContextDir)) {
const projects = fs
.readdirSync(path.join(sharedContextDir, 'projects'))
.filter((f) => f.endsWith('.json'));
contextCount = projects.length;
// Consolidate shared contexts
for (const projectFile of projects) {
const projectPath = path.join(
sharedContextDir,
'projects',
projectFile
);
const data = JSON.parse(fs.readFileSync(projectPath, 'utf8'));
// Remove old/stale entries (older than 30 days)
const thirtyDaysAgo = Date.now() - 30 * 24 * 60 * 60 * 1000;
if (data.contexts) {
data.contexts = data.contexts.filter(
(c) => c.timestamp && c.timestamp > thirtyDaysAgo
);
fs.writeFileSync(projectPath, JSON.stringify(data, null, 2));
}
}
}
this.stats.context.count++;
this.stats.context.lastSync = new Date();
this.log(
`Context sync completed - ${sessionCount} sessions, ${contextCount} shared contexts`
);
} catch (error) {
this.stats.context.errors++;
this.log(`Context sync failed: ${error.message}`, 'ERROR');
}
}
/**
* Backup to cloud storage (S3/GCS)
*/
async syncBackup() {
this.log('Starting cloud backup...');
try {
const homeDir = process.env.HOME;
const stackMemoryDir = path.join(homeDir, '.stackmemory');
const backupDir = path.join(__dirname, '..', 'backups');
// Create backup directory
if (!fs.existsSync(backupDir)) {
fs.mkdirSync(backupDir, { recursive: true });
}
// Create timestamp for backup
const timestamp = new Date()
.toISOString()
.replace(/:/g, '-')
.split('.')[0];
const backupFile = path.join(
backupDir,
`stackmemory-backup-${timestamp}.tar.gz`
);
// Create tar archive of important data
const { stdout, stderr } = await execAsync(
`tar -czf ${backupFile} -C ${homeDir} .stackmemory/sessions .stackmemory/shared-context .stackmemory/projects.db 2>/dev/null || true`
);
const stats = fs.statSync(backupFile);
const sizeMB = (stats.size / (1024 * 1024)).toFixed(2);
// Upload to cloud if configured
if (process.env.AWS_S3_BUCKET) {
await execAsync(
`aws s3 cp ${backupFile} s3://${process.env.AWS_S3_BUCKET}/stackmemory-backups/ --storage-class GLACIER_IR`
);
this.log(`Backup uploaded to S3: ${sizeMB}MB`);
} else if (process.env.GCS_BUCKET) {
await execAsync(
`gsutil cp ${backupFile} gs://${process.env.GCS_BUCKET}/stackmemory-backups/`
);
this.log(`Backup uploaded to GCS: ${sizeMB}MB`);
} else {
this.log(
`Local backup created: ${sizeMB}MB (no cloud storage configured)`
);
}
// Clean up old local backups (keep last 5)
const backups = fs
.readdirSync(backupDir)
.filter((f) => f.startsWith('stackmemory-backup-'))
.sort()
.reverse();
for (let i = 5; i < backups.length; i++) {
fs.unlinkSync(path.join(backupDir, backups[i]));
}
this.stats.backup.count++;
this.stats.backup.lastSync = new Date();
this.log(`Backup completed - ${sizeMB}MB`);
} catch (error) {
this.stats.backup.errors++;
this.log(`Backup failed: ${error.message}`, 'ERROR');
}
}
/**
* Sync with Redis cache
*/
async syncRedis() {
if (!process.env.REDIS_URL) {
return; // Skip if Redis not configured
}
this.log('Starting Redis sync...');
try {
// Import Redis client dynamically
const { default: Redis } = await import('ioredis');
const redis = new Redis(process.env.REDIS_URL);
// Sync recent frames to Redis for fast access
const homeDir = process.env.HOME;
const projectsDb = path.join(homeDir, '.stackmemory', 'projects.db');
if (fs.existsSync(projectsDb)) {
// Get recent frames from SQLite
const { stdout } = await execAsync(
`sqlite3 ${projectsDb} "SELECT frame_id, title, created_at FROM frames WHERE created_at > datetime('now', '-7 days') ORDER BY created_at DESC LIMIT 100"`
);
const frames = stdout.trim().split('\n').filter(Boolean);
// Store in Redis with expiry
for (const frame of frames) {
const [id, title, created] = frame.split('|');
await redis.setex(
`frame:${id}`,
7 * 24 * 60 * 60, // 7 days TTL
JSON.stringify({ id, title, created })
);
}
this.log(`Redis sync completed - ${frames.length} frames cached`);
}
await redis.quit();
this.stats.redis.count++;
this.stats.redis.lastSync = new Date();
} catch (error) {
this.stats.redis.errors++;
this.log(`Redis sync failed: ${error.message}`, 'ERROR');
}
}
/**
* Sync across sessions
*/
async syncCrossSession() {
this.log('Starting cross-session sync...');
try {
const homeDir = process.env.HOME;
const sharedContextDir = path.join(
homeDir,
'.stackmemory',
'shared-context'
);
const projectsDir = path.join(sharedContextDir, 'projects');
if (!fs.existsSync(projectsDir)) {
fs.mkdirSync(projectsDir, { recursive: true });
}
// Get current project
const projectName = path.basename(process.cwd());
const projectFile = path.join(projectsDir, `${projectName}.json`);
let projectData = {
name: projectName,
sessions: [],
lastSync: null,
contexts: [],
};
if (fs.existsSync(projectFile)) {
projectData = JSON.parse(fs.readFileSync(projectFile, 'utf8'));
}
// Update sync timestamp
projectData.lastSync = new Date().toISOString();
// Add current session info
const sessionId = process.env.STACKMEMORY_SESSION_ID || 'unknown';
const existingSession = projectData.sessions.find(
(s) => s.id === sessionId
);
if (existingSession) {
existingSession.lastActive = new Date().toISOString();
} else {
projectData.sessions.push({
id: sessionId,
startTime: new Date().toISOString(),
lastActive: new Date().toISOString(),
});
}
// Keep only recent sessions (last 30 days)
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
projectData.sessions = projectData.sessions.filter(
(s) => new Date(s.lastActive) > thirtyDaysAgo
);
fs.writeFileSync(projectFile, JSON.stringify(projectData, null, 2));
this.stats.crossSession.count++;
this.stats.crossSession.lastSync = new Date();
this.log(
`Cross-session sync completed - ${projectData.sessions.length} active sessions`
);
} catch (error) {
this.stats.crossSession.errors++;
this.log(`Cross-session sync failed: ${error.message}`, 'ERROR');
}
}
/**
* Run all sync tasks
*/
async runSync(taskName) {
switch (taskName) {
case 'linear':
await this.syncLinear();
break;
case 'context':
await this.syncContext();
break;
case 'backup':
await this.syncBackup();
break;
case 'redis':
await this.syncRedis();
break;
case 'crossSession':
await this.syncCrossSession();
break;
}
}
/**
* Start the sync manager
*/
async start() {
if (this.isRunning) {
this.log('Sync manager already running', 'WARN');
return;
}
this.isRunning = true;
this.log('🚀 StackMemory Background Sync Manager starting...');
this.log(`📅 Sync intervals:`);
if (process.env.ENABLE_LINEAR_SYNC === 'true') {
this.log(` Linear: every ${SYNC_INTERVALS.linear / 60000} minutes`);
} else {
this.log(` Linear: disabled (set ENABLE_LINEAR_SYNC=true to enable)`);
}
this.log(` Context: every ${SYNC_INTERVALS.context / 60000} minutes`);
this.log(` Backup: every ${SYNC_INTERVALS.backup / 3600000} hours`);
this.log(` Redis: every ${SYNC_INTERVALS.redis / 60000} minutes`);
this.log(
` Cross-session: every ${SYNC_INTERVALS.crossSession / 60000} minutes`
);
// Run initial sync for all tasks
await this.syncLinear();
await this.syncContext();
await this.syncCrossSession();
// Schedule recurring syncs
this.syncTasks.set(
'linear',
setInterval(() => this.runSync('linear'), SYNC_INTERVALS.linear)
);
this.syncTasks.set(
'context',
setInterval(() => this.runSync('context'), SYNC_INTERVALS.context)
);
this.syncTasks.set(
'backup',
setInterval(() => this.runSync('backup'), SYNC_INTERVALS.backup)
);
this.syncTasks.set(
'redis',
setInterval(() => this.runSync('redis'), SYNC_INTERVALS.redis)
);
this.syncTasks.set(
'crossSession',
setInterval(
() => this.runSync('crossSession'),
SYNC_INTERVALS.crossSession
)
);
this.log('⏰ All sync tasks scheduled');
// Handle graceful shutdown
process.on('SIGINT', () => this.stop());
process.on('SIGTERM', () => this.stop());
}
/**
* Stop the sync manager
*/
stop() {
this.log('🛑 Stopping Background Sync Manager...');
// Clear all intervals
for (const [name, interval] of this.syncTasks) {
clearInterval(interval);
}
this.syncTasks.clear();
// Log final stats
this.log('📊 Final statistics:');
for (const [name, stats] of Object.entries(this.stats)) {
if (stats.count > 0) {
this.log(` ${name}: ${stats.count} syncs, ${stats.errors} errors`);
}
}
this.isRunning = false;
this.log('👋 Sync manager stopped');
process.exit(0);
}
/**
* Get status
*/
getStatus() {
return {
running: this.isRunning,
stats: this.stats,
tasks: Array.from(this.syncTasks.keys()),
};
}
}
// Start the manager
const manager = new BackgroundSyncManager();
manager.start();