brain-memory 0.6.1 → 0.6.2

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.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,38 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/), and this
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.6.2] - 2026-11-16
10
+
11
+ Writes are safe to repeat, and two memories can no longer share a file.
12
+
13
+ ### Fixed
14
+ - **A second memory with the same path no longer overwrites the first.** Two
15
+ memories with the same title derive the same file path; the second write
16
+ replaced the first one's file while both stayed in the index, so the older
17
+ memory's content was lost and its id pointed at the newer text. The second
18
+ memory now gets its own file (`<name>-<id suffix>.md`). Most exposed through
19
+ the connector, which derives the path from the title.
20
+
21
+ ### Added
22
+ - **Replayed writes are recognised.** A `memorize` that repeats one from the
23
+ last ten minutes — same title, type, origin, path and content — returns the
24
+ memory already stored, marked `duplicate: true`, instead of storing it again.
25
+ This covers a client retrying after a timeout and a host executing an approved
26
+ tool call twice. `brain forget` on a memory that is already archived now
27
+ reports `already_archived` instead of failing. The connector says "Already
28
+ stored" / "Already archived" for these.
29
+ - **Connector: the Memory Inspector follows the MCP Apps specification.** With
30
+ `CONNECTOR_ENABLE_UI=1`, `brain_verify` links a view (`ui://brain/inspector`,
31
+ `text/html;profile=mcp-app`) that shows the review queue: each memory held
32
+ for verification, its origin and why it was held. The view is read-only —
33
+ approving and rejecting stay in the conversation. On for the hosted connector
34
+ (mcp.brainmemory.ai); off by default when self-hosting.
35
+ - **Docs: importing brain memories into Codex** via the `claude-memory` export.
36
+
37
+ ### Changed
38
+ - **Connector deploy installs from the lock file** (`npm ci`), so a deploy
39
+ cannot pick up a dependency version that was not tested.
40
+
9
41
  ## [0.6.1] - 2026-11-10
10
42
 
11
43
  First npm release of the 0.6.0 work: 0.6.0 was tagged but never published, so
package/bin/forget.js CHANGED
@@ -58,7 +58,21 @@ function archiveMemory(brainDir, id, opts = {}) {
58
58
  const index = readIndex();
59
59
  if (!index) return { error: 'Brain not initialized.' };
60
60
  const entry = index.memories[id];
61
- if (!entry) return { error: `Memory not found: ${id}` };
61
+ if (!entry) {
62
+ // Archiving is repeatable: a second call for a memory already in the
63
+ // archive (a retried or replayed tool call) reports the state it asked for.
64
+ const archivedEntry = (readArchiveIndex() || { memories: {} }).memories[id];
65
+ if (archivedEntry) {
66
+ return {
67
+ archived: true,
68
+ already_archived: true,
69
+ id,
70
+ title: archivedEntry.title,
71
+ memory_count: index.memory_count,
72
+ };
73
+ }
74
+ return { error: `Memory not found: ${id}` };
75
+ }
62
76
 
63
77
  if (!force && typeof entry.salience === 'number' && entry.salience >= SALIENCE_FLOOR) {
64
78
  return {
package/bin/memorize.js CHANGED
@@ -437,6 +437,52 @@ function trySync() {
437
437
 
438
438
  // --- Main ---
439
439
 
440
+ // --- Replay protection -------------------------------------------------------
441
+ //
442
+ // A tool call can be executed twice: a client retries after a timeout, or a host
443
+ // replays an approved write. Without a guard the second run stores the same
444
+ // memory again under a new id. A write is treated as a replay — and answered
445
+ // with the memory already stored — when title, type, origin, path and content
446
+ // all match a memory created inside this window. Outside it, storing the same
447
+ // text again is taken to be deliberate.
448
+ const REPLAY_WINDOW_MS = 10 * 60 * 1000;
449
+
450
+ function bodyHash(content) {
451
+ return contentHash(String(content).trim());
452
+ }
453
+
454
+ /** The id of a just-stored memory this write repeats, or null. */
455
+ function findReplay(index, mem, origin, hash, now) {
456
+ const nowMs = Date.parse(now);
457
+ for (const [id, entry] of Object.entries(index.memories || {})) {
458
+ if (entry.body_hash !== hash) continue;
459
+ if (entry.title !== mem.title || entry.type !== mem.type || entry.origin !== origin) continue;
460
+ // The first write may have been moved off an occupied path (uniquePath).
461
+ if (entry.path !== mem.path && entry.path !== suffixedPath(mem.path, id)) continue;
462
+ const age = nowMs - Date.parse(entry.created);
463
+ if (age >= 0 && age <= REPLAY_WINDOW_MS) return id;
464
+ }
465
+ return null;
466
+ }
467
+
468
+ /** `notes/x.md` → `notes/x-<id suffix>.md`. */
469
+ function suffixedPath(relPath, id) {
470
+ const ext = path.posix.extname(relPath);
471
+ return `${relPath.slice(0, relPath.length - ext.length)}-${id.split('_').pop()}${ext}`;
472
+ }
473
+
474
+ /**
475
+ * A path no other memory occupies. Two memories with the same title derive the
476
+ * same path; writing the second over the first would destroy the first one's
477
+ * content while its index entry lived on, pointing at the wrong file.
478
+ */
479
+ function uniquePath(brainDir, index, relPath, id) {
480
+ const taken = (p) =>
481
+ fs.existsSync(path.join(brainDir, p)) ||
482
+ Object.values(index.memories || {}).some((e) => e.path === p);
483
+ return taken(relPath) ? suffixedPath(relPath, id) : relPath;
484
+ }
485
+
440
486
  async function main() {
441
487
  const args = parseArgs(process.argv.slice(2));
442
488
 
@@ -590,10 +636,42 @@ async function main() {
590
636
  const pinDropped = Boolean(mem.pinned && quarantine.quarantined);
591
637
  if (pinDropped) mem.pinned = false;
592
638
 
639
+ // Replay protection: an identical write that just landed is answered with
640
+ // the memory already stored, not stored again.
641
+ const hash = bodyHash(mem.content);
642
+ const replayId = findReplay(index, mem, origin, hash, now);
643
+ if (replayId) {
644
+ const prior = index.memories[replayId];
645
+ results.push({
646
+ id: replayId,
647
+ title: prior.title,
648
+ path: prior.path,
649
+ type: prior.type,
650
+ cognitive_type: prior.cognitive_type,
651
+ strength: prior.strength,
652
+ decay_rate: prior.decay_rate,
653
+ origin: prior.origin,
654
+ salience: prior.salience,
655
+ confidence: prior.confidence,
656
+ tags: prior.tags || [],
657
+ edges_created: 0,
658
+ duplicate: true,
659
+ ...(prior.quarantined
660
+ ? { quarantine_pending: true, quarantine_reasons: prior.quarantine_reasons || [] }
661
+ : {}),
662
+ });
663
+ continue;
664
+ }
665
+
593
666
  // Generate ID
594
667
  const id = generateId();
595
668
  newIds.push(id);
596
669
 
670
+ // Validated before it is made unique, so a traversal attempt is refused
671
+ // rather than renamed.
672
+ validateBrainPath(path.join(brainDir, mem.path), brainDir);
673
+ mem.path = uniquePath(brainDir, index, mem.path, id);
674
+
597
675
  // Compute strength/decay
598
676
  const { fileContent, strength, decay_rate } = buildMemoryFileContent(
599
677
  mem, id, now, origin, policy.policy.decay_multiplier, quarantine
@@ -641,6 +719,8 @@ async function main() {
641
719
  // never came through a write path — the one poisoning route every other
642
720
  // defense here is blind to. See src/integrity.js.
643
721
  indexEntry.content_hash = contentHash(fileContent);
722
+ // The content alone, so a repeat of this write can be recognised.
723
+ indexEntry.body_hash = hash;
644
724
  addMemory(index, id, indexEntry);
645
725
 
646
726
  // CoALA Phase 1: register a born-pinned memory in the pinned manifest
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brain-memory",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "A brain-inspired hierarchical memory system for AI coding agents. Deep nested file structure that mimics human memory with time-decay and strength-based recall.",
5
5
  "keywords": [
6
6
  "claude-code",