xiaozuoassistant 0.1.73 → 0.1.75

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -62,21 +62,43 @@ export class ShortTermMemory {
62
62
  }
63
63
  async loadSessionsFromDisk() {
64
64
  try {
65
- const data = await fs.readJson(INDEX_FILE);
66
- // Log loaded data count for debugging
65
+ // Invalidate cache by reading file directly
66
+ const content = await fs.readFile(INDEX_FILE, 'utf-8');
67
+ const data = JSON.parse(content);
67
68
  console.log(`[ShortTermMemory:${this.instanceId}] Loaded ${Array.isArray(data) ? data.length : 0} sessions from disk.`);
68
- this.sessions.clear();
69
+ // MERGE instead of replace to prevent wiping out pending changes in current process
69
70
  if (Array.isArray(data)) {
70
71
  for (const meta of data) {
71
72
  if (meta && typeof meta.id === 'string') {
72
- this.sessions.set(meta.id, meta);
73
+ // Only update if not exists or if disk version is newer (optional, but for now just trusting disk for existence)
74
+ // But wait, if we have a pending write, our memory version is newer.
75
+ // However, we are reloading because we are MISSING a session.
76
+ // So we should add missing ones.
77
+ if (!this.sessions.has(meta.id)) {
78
+ this.sessions.set(meta.id, meta);
79
+ }
80
+ else {
81
+ // If it exists, should we update?
82
+ // If we update, we might overwrite in-memory changes that haven't been flushed.
83
+ // Safest is to only ADD missing ones.
84
+ // But if another process updated a session, we want that update.
85
+ // Complex conflict resolution needed?
86
+ // For now, let's assume if we are reloading, it's because we need data.
87
+ // Let's just add missing ones.
88
+ // NO, wait. If we don't update existing ones, we miss updates from other processes.
89
+ // But if we overwrite, we lose our own updates.
90
+ // Ideally, we only reload when we *know* we are missing something or on init.
91
+ // Compromise: Update everything. Why? Because `persistIndex` queues writes.
92
+ // If we have a pending write, it will overwrite disk later anyway.
93
+ // So momentarily reverting to disk state is fine, as long as our write queue eventually flushes OUR state.
94
+ this.sessions.set(meta.id, meta);
95
+ }
73
96
  }
74
97
  }
75
98
  }
76
99
  }
77
100
  catch (e) {
78
101
  console.error(`[ShortTermMemory:${this.instanceId}] Failed to load index.json:`, e);
79
- // 此时保持 sessions 为空或之前的状态
80
102
  }
81
103
  }
82
104
  async persistIndex() {
@@ -371,8 +393,24 @@ export class ShortTermMemory {
371
393
  await this.loadSessionsFromDisk();
372
394
  sessionMeta = this.sessions.get(sessionId);
373
395
  }
396
+ // 如果仍然没有,尝试直接从 index.json 读取一次,绕过缓存逻辑
374
397
  if (!sessionMeta) {
375
- console.error(`[ShortTermMemory:${this.instanceId}] Session ${sessionId} not found even after reload.`);
398
+ try {
399
+ const content = await fs.readFile(INDEX_FILE, 'utf-8');
400
+ const data = JSON.parse(content);
401
+ const found = Array.isArray(data) ? data.find((m) => m.id === sessionId) : null;
402
+ if (found) {
403
+ console.log(`[ShortTermMemory:${this.instanceId}] Session ${sessionId} found in fresh disk read, recovering...`);
404
+ this.sessions.set(found.id, found);
405
+ sessionMeta = found;
406
+ }
407
+ }
408
+ catch (e) {
409
+ console.error(`[ShortTermMemory:${this.instanceId}] Emergency disk read failed:`, e);
410
+ }
411
+ }
412
+ if (!sessionMeta) {
413
+ console.error(`[ShortTermMemory:${this.instanceId}] Session ${sessionId} not found even after emergency reload.`);
376
414
  throw new Error(`Session ${sessionId} not found`);
377
415
  }
378
416
  const msg = { ...message };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "xiaozuoassistant",
3
3
  "private": false,
4
4
  "description": "Your personal, locally-hosted AI assistant for office productivity.",
5
- "version": "0.1.73",
5
+ "version": "0.1.75",
6
6
  "author": "mantle.lau",
7
7
  "license": "MIT",
8
8
  "repository": {