Skip to content

fix: wire SNAPSHOT_INTERVAL to a periodic timer that triggers mem::snapshot-create#1010

Open
yingliang-zhang wants to merge 2 commits into
rohitg00:mainfrom
yingliang-zhang:fix/1006-snapshot-interval-timer
Open

fix: wire SNAPSHOT_INTERVAL to a periodic timer that triggers mem::snapshot-create#1010
yingliang-zhang wants to merge 2 commits into
rohitg00:mainfrom
yingliang-zhang:fix/1006-snapshot-interval-timer

Conversation

@yingliang-zhang

@yingliang-zhang yingliang-zhang commented Jul 4, 2026

Copy link
Copy Markdown

Summary

The config value (default 3600s) was read and logged at boot — Git snapshots: ... (every 3600s) — but no setInterval timer was ever created. Periodic snapshots never fired automatically. Snapshots only happened when manually triggered via the API/MCP endpoint or the memory_snapshot_create MCP tool.

This means that in a long-running daemon, if no one manually triggers a snapshot, data could be lost on crash/restart with only the last manual snapshot available for recovery.

Fix

Add a setInterval that triggers mem::snapshot-create at the configured interval, following the exact same pattern as the existing auto-forget, lesson-decay, consolidation, and recent-searches-sweep timers:

const snapshotTimer = setInterval(async () => {
    try {
        await sdk.trigger({ function_id: "mem::snapshot-create", payload: {} });
    } catch {}
}, snapshotIntervalMs);
snapshotTimer.unref();

A guard (snapshotIntervalMs > 0) prevents an infinite loop if someone explicitly sets SNAPSHOT_INTERVAL=0.

How to verify

  1. Set SNAPSHOT_ENABLED=true and SNAPSHOT_INTERVAL=10 in .env
  2. Start the daemon: agentmemory
  3. Wait 15 seconds
  4. Run agentmemory mcp and trigger memory_snapshot_list
  5. Verify a periodic snapshot commit appears with a timestamp ~10s after boot
  6. Check daemon.err.log for Snapshot created info entries

Test results

  • npm run build — clean
  • npx vitest run test/snapshot.test.ts — 5/5 passed
  • Full suite: 1411/1415 passed (4 pre-existing failures in embedding-provider.test.ts unrelated to this change, confirmed by running tests on unmodified main)

Fixes #1006

Summary by CodeRabbit

  • New Features
    • Snapshotting can now run automatically on the configured interval when enabled.
    • The snapshot trigger fires periodically in the background, without keeping the process alive.
    • Snapshot interval timing is derived from the configured seconds value, with zero disabling periodic execution.
    • Errors during scheduled snapshot ticks are suppressed to avoid impacting normal operation.
    • Startup logging now reflects whether periodic snapshot execution is active.

…apshot-create

The SNAPSHOT_INTERVAL config value was read and logged at boot but no
setInterval timer was ever created, so periodic snapshots never fired.
Snapshots only happened when manually triggered via the API/MCP.

Add a setInterval that triggers mem::snapshot-create at the configured
interval, following the same pattern as auto-forget, lesson-decay, and
consolidation timers (try/catch + unref).

Fixes rohitg00#1006

Signed-off-by: yingliang-zhang <zhangyingliang@outlook.com>
@vercel

vercel Bot commented Jul 4, 2026

Copy link
Copy Markdown

@yingliang-zhang is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 001d1ae5-6c3e-4f97-ab7d-4ed45dd12fa6

📥 Commits

Reviewing files that changed from the base of the PR and between 1470ddc and 4b11197.

📒 Files selected for processing (1)
  • src/index.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/index.ts

📝 Walkthrough

Walkthrough

When snapshotting is enabled, the worker now derives a millisecond interval from the configured snapshot interval, starts a periodic timer that triggers mem::snapshot-create, and updates the boot log to reflect whether timer-based snapshotting is active.

Changes

Snapshot scheduling

Layer / File(s) Summary
Add periodic snapshot trigger timer
src/index.ts
Computes snapshotIntervalMs, starts a repeating setInterval when the interval is positive, triggers sdk.trigger for mem::snapshot-create on each tick, suppresses errors, unref()s the timer, and updates the boot log to distinguish active timer mode from manual-only mode.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Worker as src/index.ts
  participant Timer as setInterval
  participant SDK as sdk.trigger
  participant SnapshotFn as mem::snapshot-create

  Worker->>Timer: start interval (snapshotIntervalMs)
  loop every tick
    Timer->>SDK: trigger(function_id="mem::snapshot-create")
    SDK->>SnapshotFn: execute snapshot creation
    SDK-->>Timer: errors suppressed via try/catch
  end
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: wiring SNAPSHOT_INTERVAL to periodic snapshot creation.
Linked Issues check ✅ Passed The change adds a periodic setInterval for mem::snapshot-create, with unref and a zero-interval guard, matching issue #1006.
Out of Scope Changes check ✅ Passed The PR stays confined to snapshot startup logic in src/index.ts and doesn't introduce unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/index.ts (1)

359-361: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Boot log doesn't reflect whether the periodic timer actually started.

This bootLog always fires when snapshotConfig.enabled, even if snapshotIntervalMs was 0 and the guard at Line 351 skipped creating the timer. Operators would see "Git snapshots: ... (every 0s)" implying periodic snapshots are running, when in fact only manual/API-triggered snapshots work — the exact gap this PR is meant to close.

📝 Proposed fix to clarify boot log
-    bootLog(
-      `Git snapshots: ${snapshotConfig.dir} (every ${snapshotConfig.interval}s)`,
-    );
+    bootLog(
+      snapshotIntervalMs > 0
+        ? `Git snapshots: ${snapshotConfig.dir} (every ${snapshotConfig.interval}s)`
+        : `Git snapshots: ${snapshotConfig.dir} (periodic timer disabled, SNAPSHOT_INTERVAL=0 — manual/API trigger only)`,
+    );
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/index.ts` around lines 359 - 361, The boot log in the snapshot startup
path is misleading because it always reports periodic Git snapshots even when
the timer was not created. Update the logic around the snapshot initialization
in src/index.ts so the `bootLog` tied to the snapshot setup only runs when
`snapshotIntervalMs` is actually greater than zero and the interval timer is
started, and use the existing snapshot-related symbols like `snapshotConfig` and
`snapshotIntervalMs` to keep the message aligned with real behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/index.ts`:
- Around line 359-361: The boot log in the snapshot startup path is misleading
because it always reports periodic Git snapshots even when the timer was not
created. Update the logic around the snapshot initialization in src/index.ts so
the `bootLog` tied to the snapshot setup only runs when `snapshotIntervalMs` is
actually greater than zero and the interval timer is started, and use the
existing snapshot-related symbols like `snapshotConfig` and `snapshotIntervalMs`
to keep the message aligned with real behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e9c1354b-6df0-4347-a598-53b4256c0abe

📥 Commits

Reviewing files that changed from the base of the PR and between 93ae9bc and 1470ddc.

📒 Files selected for processing (1)
  • src/index.ts

…timer

Address CodeRabbit nitpick: the boot log always reported 'every Ns'
even when the timer guard (snapshotIntervalMs > 0) skipped creating
it, making SNAPSHOT_INTERVAL=0 misleadingly look like periodic
snapshots were running.

Signed-off-by: yingliang-zhang <zhangyingliang@outlook.com>
@yingliang-zhang

Copy link
Copy Markdown
Author

Addressed the CodeRabbit nitpick in commit 4b11197 — the boot log now shows periodic timer disabled, SNAPSHOT_INTERVAL=0 — manual trigger only when the guard skips the timer, instead of the misleading every 0s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant