Skip to content

[Medium] Memory caches never invalidated on external file changes #83

Description

@ether-btc

Severity: Medium

Location: src/memory/search.ts:85-89 and src/memory/knowledge.ts:41-45

Description:
ensureIndex() and loadKnowledge() both use module-level caches (dirty flag, cache variable) that persist for the lifetime of the Node.js process. These caches are never invalidated if the JSON files are modified externally (e.g., by another process, a backup restore, or manual editing).

For a long-running agent that may run for weeks, stale cached data can accumulate without the agent realizing it.

Suggested Fix:
Add file-watcher based cache invalidation using fs.watch or fs.watchFile, or at minimum check file mtime against cached mtime when loading:

let cacheMtime: number | null = null;

export function loadKnowledge(): KnowledgeEntry[] {
  const p = getKnowledgePath();
  if (!fs.existsSync(p)) return [];
  const stat = fs.statSync(p);
  if (cacheMtime !== stat.mtimeMs) {
    cache = null;
    cacheMtime = stat.mtimeMs;
  }
  if (cache) return cache;
  // ...
}

Apply the same pattern to feedback.ts and chat.ts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions