Skip to content

Bug: SNAPSHOT_INTERVAL config is read and logged but no setInterval timer triggers mem::snapshot-create #1006

Closed

Description

@yingliang-zhang

Summary

SNAPSHOT_ENABLED=true and SNAPSHOT_INTERVAL=3600 are correctly loaded via loadSnapshotConfig() and displayed in the boot log:

Git snapshots: ~/.agentmemory/snapshots (every 3600s)

However, no periodic timer ever calls mem::snapshot-create. The snapshot function is registered (registerSnapshotFunction), but unlike auto-forget, lesson-decay, insight-decay, consolidation, and recent-searches-sweep — which all have setInterval blocks in the startup section (~line 20545 of src-MOg2zKJs.mjs) — there is no corresponding setInterval for snapshot creation.

The result: snapshots are only created when manually triggered via the MCP memory_snapshot_create tool or POST /agentmemory/snapshot/create. If the daemon crashes between manual snapshots, all data since the last snapshot is lost.

Root cause (source investigation)

In src/index.ts (compiled: dist/src-MOg2zKJs.mjs), around line 20464:

const snapshotConfig = loadSnapshotConfig();
if (snapshotConfig.enabled) {
    registerSnapshotFunction(sdk, kv, snapshotConfig.dir);
    bootLog(`Git snapshots: ${snapshotConfig.dir} (every ${snapshotConfig.interval}s)`);
}

The snapshotConfig.interval value is read and printed, but never passed to a setInterval. Compare with the other periodic tasks nearby (~line 20545+):

// auto-forget — HAS setInterval ✓
if (process.env.AUTO_FORGET_ENABLED !== "false") {
    setInterval(async () => { await sdk.trigger({function_id: "mem::auto-forget", ...}); }, autoForgetIntervalMs).unref();
}

// lesson-decay — HAS setInterval ✓
if (process.env.LESSON_DECAY_ENABLED !== "false") {
    setInterval(async () => { ... }, 864e5).unref();
}

// consolidation — HAS setInterval ✓
if (isConsolidationEnabled()) {
    setInterval(async () => { ... }, consolidationIntervalMs).unref();
}

// snapshot — NO setInterval ✗
// (only registerSnapshotFunction + bootLog, no timer)

Suggested fix

Add a setInterval for periodic snapshot creation, mirroring the pattern used by the other periodic tasks:

if (snapshotConfig.enabled) {
    registerSnapshotFunction(sdk, kv, snapshotConfig.dir);
    const snapshotIntervalMs = snapshotConfig.interval * 1000;
    setInterval(async () => {
        try {
            await sdk.trigger({
                function_id: "mem::snapshot-create",
                payload: { message: "periodic snapshot" }
            });
        } catch {}
    }, snapshotIntervalMs).unref();
    bootLog(`Git snapshots: ${snapshotConfig.dir} (every ${snapshotConfig.interval}s)`);
}

Environment

  • agentmemory v0.9.27 (npm global)
  • iii engine v0.11.2 (native)
  • macOS (also affects all platforms — this is a code-level bug)

Context

This compounds with the data-loss-on-restart issues (#843, #913, #950). Even with the stop-order fix from #849 merged, a crash or kill -9 bypasses the shutdown handler entirely. Periodic snapshots are the safety net — but the net has a hole in it.

I have a local workaround (cron job calling POST /agentmemory/snapshot/create every 2h + auto-restore from latest snapshot in start.sh), which confirms the snapshot create/restore APIs work correctly. Happy to submit a PR for the setInterval fix if the maintainers agree this is the right approach.

Workaround

External cron job or start.sh script that periodically calls:

curl -X POST http://localhost:3111/agentmemory/snapshot/create \
  -H "Content-Type: application/json" \
  -d "{\"message\":\"periodic snapshot\"}"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions