Skip to content

fix(ai-memory): bound claude-mem sync runs and drop per-row table scans - #290

Merged
entelecheia merged 1 commit into
mainfrom
fix/289-bound-claude-mem-sync
Oct 18, 2026
Merged

entelecheia merged 1 commit into
mainfrom
fix/289-bound-claude-mem-sync

Conversation

@entelecheia

Copy link
Copy Markdown
Member

Fixes #289.

Root cause

SyncDB.Import checked each incoming row's natural key with WHERE created_at = ? AND COALESCE(title,'') = ?. created_at has no index, so every lookup scanned all of observations, and the attribution repair ran up to five more full-scan UPDATEs per dedupe hit. With 219k observations and a 50.8k-row 7-day overlap, that is hundreds of thousands of full scans inside an IMMEDIATE transaction: the run spun at ~100% CPU for hours, held the write lock (the peer's and the worker's writes got SQLITE_BUSY), and ignored SIGTERM because no DB call took the context.

Changes

  • Import loads the receiver's (created_at, key) → rowids once per table (from the bundle's oldest created_at), dedupes in memory, repairs by rowid, and records rows it inserts so duplicates inside one bundle still dedupe.
  • Context threaded through every SyncDB call; dot ai memory sync --timeout (default 10m) bounds client and --serve runs. Deadline or SIGTERM interrupts the statement; the deferred rollback leaves no partial import. The error says "sync aborted after the 10m0s deadline" or "sync canceled".
  • RunMemorySync logs each phase's start and end with row counts; errors are prefixed with the phase (pull: local import: ...). Import logs each table.
  • LaunchAgent wrapper: watchdog sends SIGTERM at 15 min and SIGKILL 30 s later; golden updated.
  • Docs: docs/commands/dot_ai_memory_sync.md (generated), docs/COMMANDS-GUIDE.md.

Verification

  • Real-scale: copy of the live 2.8 GB store (.backup), 8-day export (50,827 obs, 2,274 sums, 324 sessions) re-imported: new code 1.4 s; v2.70.46 killed unfinished after 60 s. --timeout 300ms aborted with no row change and integrity_check ok.
  • New tests: held write lock → pull: local import + SQLITE_BUSY within the busy timeout; stalled phase ends at the deadline naming pull: peer export; cancel mid-import rolls back; in-bundle duplicates dedupe.
  • Watchdog script checked with a TERM-ignoring fake binary (killed at limit + grace, rc 137) and a normal one (exit code passed through).
  • go test ./... -race (2 workers): all pass except TestJSONGoldens, which fails only because the Orca session exports CLAUDE_CONFIG_DIR; it passes with that unset (environment leak, unrelated). golangci-lint --new-from-rev main: 0 issues (two errcheck hits in untouched claudemem.go predate this branch).

After release

The push-direction import runs on the peer, so both Macs need the new dot. The plist is rewritten only by dot ai memory install --peer <target>.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Tc37VsosXPm6MTjU6ej3c9

The import looked up each incoming row's natural key with a query on the
unindexed created_at column, and the attribution repair did the same with
up to five UPDATEs per row. On a 220k-row store, a 50k-row overlap window
meant hundreds of thousands of full table scans under the write lock: the
scheduled run spun for hours, blocked the peer's imports with SQLITE_BUSY
and kept dot admit deferring heavy work.

- Load the receiver's natural keys once per import and repair by rowid.
  The same 50.8k-row bundle on a copy of the real store now imports in
  1.4 s (the previous code did not finish in 60 s).
- Thread the context through every DB call and add --timeout (default
  10m), so a deadline or SIGTERM interrupts the statement and the open
  import rolls back.
- Log each phase's start and end with row counts; errors name the phase.
- The LaunchAgent wrapper sends SIGTERM after 15 min and SIGKILL 30 s
  later as a backstop.

Fixes #289

Claude-Session: https://claude.ai/code/session_01Tc37VsosXPm6MTjU6ej3c9
Copilot AI balanced review requested due to automatic review settings October 18, 2026 20:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T21:00:30.884913Z 7113991 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@entelecheia
entelecheia merged commit 9e0bc1c into main Oct 18, 2026
13 checks passed
@entelecheia
entelecheia deleted the fix/289-bound-claude-mem-sync branch October 18, 2026 21:08
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.

fix(ai memory): bound claude-mem sync runs; a local-phase hang blocked replication and dot admit for 5 h

2 participants