brain-memory 0.1.0-beta.3 → 0.1.0-beta.31

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.
Files changed (66) hide show
  1. package/CHANGELOG.md +339 -0
  2. package/README.md +400 -94
  3. package/SECURITY.md +61 -0
  4. package/assets/brand/README.md +28 -0
  5. package/assets/brand/icon-dark.svg +19 -0
  6. package/assets/brand/icon-light.svg +20 -0
  7. package/assets/brand/linkedin_cover.png +0 -0
  8. package/assets/brand/linkedin_cover_light.png +0 -0
  9. package/assets/brand/mark-dark.svg +19 -0
  10. package/assets/brand/mark-light.svg +20 -0
  11. package/assets/brand/png/icon-dark-1024.png +0 -0
  12. package/assets/brand/png/icon-dark-128.png +0 -0
  13. package/assets/brand/png/icon-dark-256.png +0 -0
  14. package/assets/brand/png/icon-dark-512.png +0 -0
  15. package/assets/brand/png/icon-dark-64.png +0 -0
  16. package/assets/brand/png/icon-light-1024.png +0 -0
  17. package/assets/brand/png/icon-light-128.png +0 -0
  18. package/assets/brand/png/icon-light-256.png +0 -0
  19. package/assets/brand/png/icon-light-512.png +0 -0
  20. package/assets/brand/png/icon-light-64.png +0 -0
  21. package/assets/brand/png/mark-dark-1024.png +0 -0
  22. package/assets/brand/png/mark-light-1024.png +0 -0
  23. package/assets/icon.png +0 -0
  24. package/assets/icon.svg +19 -0
  25. package/bin/brain.js +111 -0
  26. package/bin/cloud-sync.js +150 -0
  27. package/bin/forget.js +122 -0
  28. package/bin/install.js +229 -254
  29. package/bin/memorize.js +458 -0
  30. package/bin/pin.js +44 -0
  31. package/bin/recall.js +259 -0
  32. package/bin/reinforce.js +187 -0
  33. package/bin/session-start.js +298 -0
  34. package/bin/skill.js +67 -0
  35. package/bin/unpin.js +38 -0
  36. package/commands/brain/forget.md +245 -15
  37. package/commands/brain/memorize.md +76 -144
  38. package/commands/brain/pin.md +79 -0
  39. package/commands/brain/remember.md +48 -105
  40. package/commands/brain/skills.md +60 -0
  41. package/commands/brain/sleep.md +69 -28
  42. package/commands/brain/status.md +24 -10
  43. package/commands/brain/sync.md +386 -0
  44. package/package.json +22 -8
  45. package/prompts/antigravity.md +180 -0
  46. package/prompts/claude.md +124 -20
  47. package/prompts/openai.md +124 -20
  48. package/prompts/opencode.md +179 -0
  49. package/src/cloud-sync.js +534 -0
  50. package/src/crypto.js +130 -0
  51. package/src/export-import.js +200 -0
  52. package/src/git-sync.js +444 -0
  53. package/src/index-manager.js +219 -18
  54. package/src/installer.js +444 -0
  55. package/src/pinning.js +124 -0
  56. package/src/scorer.js +45 -11
  57. package/src/skills.js +208 -0
  58. package/src/tfidf.js +520 -0
  59. package/commands/brain/consolidate.md +0 -153
  60. package/commands/brain/explore.md +0 -79
  61. package/commands/brain/init.md +0 -157
  62. package/commands/brain/review.md +0 -130
  63. package/commands/brain/sunshine.md +0 -337
  64. package/hooks/session-end.md +0 -48
  65. package/hooks/session-start.md +0 -48
  66. package/prompts/gemini.md +0 -76
package/SECURITY.md ADDED
@@ -0,0 +1,61 @@
1
+ # Security Policy
2
+
3
+ ## Reporting Vulnerabilities
4
+
5
+ If you discover a security vulnerability, please report it privately via email to **onur@omelas.tech**. Do not open a public GitHub issue for security vulnerabilities.
6
+
7
+ You should receive a response within 48 hours. Please include:
8
+ - Description of the vulnerability
9
+ - Steps to reproduce
10
+ - Potential impact
11
+
12
+ ## Scope
13
+
14
+ Brain Memory handles sensitive data in several areas:
15
+
16
+ - **AES-256-GCM encryption** — Optional encryption for Git-synced and exported memory files using a user-provided passphrase (PBKDF2-SHA512, 100K iterations)
17
+ - **File system access** — Reads and writes to the `.brain/` directory tree
18
+ - **Git operations** — When sync is configured, pushes/pulls to a user-specified Git remote using the system `git` binary and the user's existing Git/SSH authentication
19
+
20
+ ## Hosted service: Brain Cloud & the connector
21
+
22
+ Brain Memory is **local-first** — by default your memories are plain files on your
23
+ own disk and never leave it. The hosted **Brain Cloud** sync hub and the **Claude
24
+ connector** (a remote MCP server) are entirely optional. When you do use them, this
25
+ is how your data is protected:
26
+
27
+ - **Encrypted at rest.** Brains stored in Brain Cloud are encrypted on disk with
28
+ **AES-256-GCM** using a **per-user key** (HKDF-derived from a server master key).
29
+ A stolen disk, backup, or snapshot yields no readable memories.
30
+ - **No plaintext on the connector.** The connector keeps each user's working copy
31
+ in **RAM only** (a tmpfs, wiped on restart) — it is never written to the
32
+ connector's disk.
33
+ - **Encrypted in transit.** All traffic to `api.brainmemory.ai` and the connector
34
+ is HTTPS/TLS.
35
+ - **Strict tenant isolation.** Every cloud request is authorized against the
36
+ authenticated user; one account can never read or overwrite another's brain.
37
+ - **Revocable sessions.** CLI sessions use **rotating refresh tokens with
38
+ automatic reuse detection** — a replayed (stolen) token revokes the whole
39
+ session family. You can log out a device, or **all** devices, at any time.
40
+ - **Verified-identity sign-in.** Connector and dashboard login is Google OAuth via
41
+ Firebase; account-linking requires a **verified** email.
42
+ - **Least privilege.** The connector runs as an unprivileged, sandboxed service
43
+ (systemd hardening: no new privileges, read-only filesystem, private tmp).
44
+
45
+ Because the cloud runs deterministic recall server-side, this is **server-side**
46
+ encryption at rest, not end-to-end encryption: the service necessarily processes
47
+ your memories in memory. If you require that the server never sees plaintext, keep
48
+ your brain **local-only** (the default) or use Git/export sync with a passphrase
49
+ (below) instead of Brain Cloud.
50
+
51
+ ## Supported Versions
52
+
53
+ | Version | Supported |
54
+ |---------|-----------|
55
+ | 0.1.x (beta) | Yes |
56
+
57
+ ## Design Principles
58
+
59
+ - **No runtime dependencies** — Reduces supply chain attack surface
60
+ - **Local-first** — Sensitive data stays on disk by default; sync is opt-in
61
+ - **No stored credentials** — Git sync relies on the user's existing SSH keys or Git credential helpers; Brain Memory never stores auth tokens
@@ -0,0 +1,28 @@
1
+ # Brain Memory — brand icon exports
2
+
3
+ The firing-neuron "activation" mark in the technical-white identity's brand cyan
4
+ (`#46C6FF`). Regenerate everything from the canonical geometry with
5
+ `website/scripts/gen-icon.mjs` (favicons/app icon) — these files mirror that mark
6
+ for use on external surfaces.
7
+
8
+ ## Which file to use
9
+
10
+ | You're placing it on… | Use |
11
+ |----------------------------------|---------------------------------------|
12
+ | A **dark** UI / near-black tile | `icon-dark.svg` · `png/icon-dark-*.png` |
13
+ | A **light** UI / paper surface | `icon-light.svg` · `png/icon-light-*.png` |
14
+ | A surface that already has a bg (no tile) | `mark-dark.svg` (light/white mark) · `mark-light.svg` (deep-cyan mark) |
15
+
16
+ - **Tiles** (`icon-*`) include the rounded-square background and are self-contained.
17
+ - **Marks** (`mark-*`) are transparent — the neuron only.
18
+ - **Dark** keeps the white-hot core; **light** deepens the cyans + uses a dark-cyan
19
+ core so the mark stays legible on paper.
20
+
21
+ PNG sizes: 1024 / 512 / 256 / 128 / 64.
22
+
23
+ ## Wide images
24
+
25
+ `linkedin_cover.png` / `linkedin_cover_light.png` — 6336×1584 (4× the 1584×396
26
+ LinkedIn banner): the app tile centered on a flat canvas, no text (dark =
27
+ `#252525`, light = paper). Regenerate with
28
+ `node website/scripts/gen-linkedin-cover.mjs` (writes `website/public/`).
@@ -0,0 +1,19 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" role="img" aria-label="Brain Memory"><defs><radialGradient id="core" cx="50%" cy="50%" r="50%"><stop offset="0%" stop-color="#8FDDFF" stop-opacity="0.95"/><stop offset="100%" stop-color="#46C6FF" stop-opacity="0"/></radialGradient></defs><rect width="32" height="32" rx="7" ry="7" fill="#07090A"/>
2
+ <circle cx="16" cy="16" r="12" fill="url(#core)" opacity="0.7"/>
3
+ <g stroke="#2E9FD6" stroke-width="1.25" stroke-linecap="round">
4
+ <line x1="16" y1="16" x2="8" y2="8" stroke-opacity="0.4"/>
5
+ <line x1="16" y1="16" x2="25" y2="8" stroke-opacity="0.82"/>
6
+ <line x1="16" y1="16" x2="27" y2="17" stroke-opacity="0.5"/>
7
+ <line x1="16" y1="16" x2="24" y2="25" stroke-opacity="0.75"/>
8
+ <line x1="16" y1="16" x2="8" y2="23" stroke-opacity="0.4"/>
9
+ <line x1="16" y1="16" x2="6" y2="16" stroke-opacity="0.35"/>
10
+ </g>
11
+ <circle cx="8" cy="8" r="1.6" fill="#1C7FB0"/>
12
+ <circle cx="27" cy="17" r="1.4" fill="#4FB8E6"/>
13
+ <circle cx="24" cy="25" r="1.9" fill="#2E9FD6"/>
14
+ <circle cx="8" cy="23" r="1.6" fill="#2389BE"/>
15
+ <circle cx="6" cy="16" r="1.3" fill="#1B6E97"/>
16
+ <circle cx="25" cy="8" r="2.4" fill="#6FD3FF"/>
17
+ <circle cx="21" cy="11.5" r="1.3" fill="#CFEEFF"/>
18
+ <circle cx="16" cy="16" r="3.7" fill="#DFF4FF"/>
19
+ <circle cx="16" cy="16" r="1.9" fill="#FFFFFF"/></svg>
@@ -0,0 +1,20 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" role="img" aria-label="Brain Memory"><defs><radialGradient id="core" cx="50%" cy="50%" r="50%"><stop offset="0%" stop-color="#46C6FF" stop-opacity="0.95"/><stop offset="100%" stop-color="#46C6FF" stop-opacity="0"/></radialGradient></defs><rect width="32" height="32" rx="7" ry="7" fill="#F4F2EC"/>
2
+ <circle cx="16" cy="16" r="12" fill="url(#core)" opacity="0.5"/>
3
+ <g stroke="#1C7FB0" stroke-width="1.25" stroke-linecap="round">
4
+ <line x1="16" y1="16" x2="8" y2="8" stroke-opacity="0.4"/>
5
+ <line x1="16" y1="16" x2="25" y2="8" stroke-opacity="0.85"/>
6
+ <line x1="16" y1="16" x2="27" y2="17" stroke-opacity="0.55"/>
7
+ <line x1="16" y1="16" x2="24" y2="25" stroke-opacity="0.8"/>
8
+ <line x1="16" y1="16" x2="8" y2="23" stroke-opacity="0.4"/>
9
+ <line x1="16" y1="16" x2="6" y2="16" stroke-opacity="0.35"/>
10
+ </g>
11
+ <circle cx="8" cy="8" r="1.6" fill="#1C7FB0"/>
12
+ <circle cx="27" cy="17" r="1.4" fill="#2E9FD6"/>
13
+ <circle cx="24" cy="25" r="1.9" fill="#1C7FB0"/>
14
+ <circle cx="8" cy="23" r="1.6" fill="#2389BE"/>
15
+ <circle cx="6" cy="16" r="1.3" fill="#1B6E97"/>
16
+ <circle cx="25" cy="8" r="2.4" fill="#2E9FD6"/>
17
+ <circle cx="21" cy="11.5" r="1.3" fill="#5BC8FF"/>
18
+ <circle cx="16" cy="16" r="3.7" fill="#1C7FB0"/>
19
+ <circle cx="16" cy="16" r="1.9" fill="#0B4D6E"/>
20
+ <circle cx="16" cy="16" r="0.8" fill="#5BC8FF"/></svg>
Binary file
@@ -0,0 +1,19 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" role="img" aria-label="Brain Memory"><defs><radialGradient id="core" cx="50%" cy="50%" r="50%"><stop offset="0%" stop-color="#8FDDFF" stop-opacity="0.95"/><stop offset="100%" stop-color="#46C6FF" stop-opacity="0"/></radialGradient></defs>
2
+ <circle cx="16" cy="16" r="12" fill="url(#core)" opacity="0.7"/>
3
+ <g stroke="#2E9FD6" stroke-width="1.25" stroke-linecap="round">
4
+ <line x1="16" y1="16" x2="8" y2="8" stroke-opacity="0.4"/>
5
+ <line x1="16" y1="16" x2="25" y2="8" stroke-opacity="0.82"/>
6
+ <line x1="16" y1="16" x2="27" y2="17" stroke-opacity="0.5"/>
7
+ <line x1="16" y1="16" x2="24" y2="25" stroke-opacity="0.75"/>
8
+ <line x1="16" y1="16" x2="8" y2="23" stroke-opacity="0.4"/>
9
+ <line x1="16" y1="16" x2="6" y2="16" stroke-opacity="0.35"/>
10
+ </g>
11
+ <circle cx="8" cy="8" r="1.6" fill="#1C7FB0"/>
12
+ <circle cx="27" cy="17" r="1.4" fill="#4FB8E6"/>
13
+ <circle cx="24" cy="25" r="1.9" fill="#2E9FD6"/>
14
+ <circle cx="8" cy="23" r="1.6" fill="#2389BE"/>
15
+ <circle cx="6" cy="16" r="1.3" fill="#1B6E97"/>
16
+ <circle cx="25" cy="8" r="2.4" fill="#6FD3FF"/>
17
+ <circle cx="21" cy="11.5" r="1.3" fill="#CFEEFF"/>
18
+ <circle cx="16" cy="16" r="3.7" fill="#DFF4FF"/>
19
+ <circle cx="16" cy="16" r="1.9" fill="#FFFFFF"/></svg>
@@ -0,0 +1,20 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" role="img" aria-label="Brain Memory"><defs><radialGradient id="core" cx="50%" cy="50%" r="50%"><stop offset="0%" stop-color="#46C6FF" stop-opacity="0.95"/><stop offset="100%" stop-color="#46C6FF" stop-opacity="0"/></radialGradient></defs>
2
+ <circle cx="16" cy="16" r="12" fill="url(#core)" opacity="0.5"/>
3
+ <g stroke="#1C7FB0" stroke-width="1.25" stroke-linecap="round">
4
+ <line x1="16" y1="16" x2="8" y2="8" stroke-opacity="0.4"/>
5
+ <line x1="16" y1="16" x2="25" y2="8" stroke-opacity="0.85"/>
6
+ <line x1="16" y1="16" x2="27" y2="17" stroke-opacity="0.55"/>
7
+ <line x1="16" y1="16" x2="24" y2="25" stroke-opacity="0.8"/>
8
+ <line x1="16" y1="16" x2="8" y2="23" stroke-opacity="0.4"/>
9
+ <line x1="16" y1="16" x2="6" y2="16" stroke-opacity="0.35"/>
10
+ </g>
11
+ <circle cx="8" cy="8" r="1.6" fill="#1C7FB0"/>
12
+ <circle cx="27" cy="17" r="1.4" fill="#2E9FD6"/>
13
+ <circle cx="24" cy="25" r="1.9" fill="#1C7FB0"/>
14
+ <circle cx="8" cy="23" r="1.6" fill="#2389BE"/>
15
+ <circle cx="6" cy="16" r="1.3" fill="#1B6E97"/>
16
+ <circle cx="25" cy="8" r="2.4" fill="#2E9FD6"/>
17
+ <circle cx="21" cy="11.5" r="1.3" fill="#5BC8FF"/>
18
+ <circle cx="16" cy="16" r="3.7" fill="#1C7FB0"/>
19
+ <circle cx="16" cy="16" r="1.9" fill="#0B4D6E"/>
20
+ <circle cx="16" cy="16" r="0.8" fill="#5BC8FF"/></svg>
Binary file
@@ -0,0 +1,19 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="1024" height="1024" role="img" aria-label="Brain Memory"><defs><radialGradient id="core" cx="50%" cy="50%" r="50%"><stop offset="0%" stop-color="#8FDDFF" stop-opacity="0.95"/><stop offset="100%" stop-color="#46C6FF" stop-opacity="0"/></radialGradient></defs><rect x="0" y="0" width="32" height="32" rx="7" ry="7" fill="#07090A"/>
2
+ <circle cx="16" cy="16" r="12" fill="url(#core)" opacity="0.7"/>
3
+ <g stroke="#2E9FD6" stroke-width="1.25" stroke-linecap="round">
4
+ <line x1="16" y1="16" x2="8" y2="8" stroke-opacity="0.4"/>
5
+ <line x1="16" y1="16" x2="25" y2="8" stroke-opacity="0.82"/>
6
+ <line x1="16" y1="16" x2="27" y2="17" stroke-opacity="0.5"/>
7
+ <line x1="16" y1="16" x2="24" y2="25" stroke-opacity="0.75"/>
8
+ <line x1="16" y1="16" x2="8" y2="23" stroke-opacity="0.4"/>
9
+ <line x1="16" y1="16" x2="6" y2="16" stroke-opacity="0.35"/>
10
+ </g>
11
+ <circle cx="8" cy="8" r="1.6" fill="#1C7FB0"/>
12
+ <circle cx="27" cy="17" r="1.4" fill="#4FB8E6"/>
13
+ <circle cx="24" cy="25" r="1.9" fill="#2E9FD6"/>
14
+ <circle cx="8" cy="23" r="1.6" fill="#2389BE"/>
15
+ <circle cx="6" cy="16" r="1.3" fill="#1B6E97"/>
16
+ <circle cx="25" cy="8" r="2.4" fill="#6FD3FF"/>
17
+ <circle cx="21" cy="11.5" r="1.3" fill="#CFEEFF"/>
18
+ <circle cx="16" cy="16" r="3.7" fill="#DFF4FF"/>
19
+ <circle cx="16" cy="16" r="1.9" fill="#FFFFFF"/></svg>
package/bin/brain.js ADDED
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * brain — unified CLI dispatcher for the Brain Memory system.
5
+ *
6
+ * Routes subcommands to their implementations, replacing the former per-binary
7
+ * surface (brain-recall, brain-reinforce, brain-memorize, brain-cloud). Each
8
+ * delegate module still runs on load and remains invokable as `node bin/<file>`,
9
+ * so direct invocation and the existing test suite are unaffected.
10
+ *
11
+ * Usage:
12
+ * brain Interactive installer (also: brain install)
13
+ * brain install|update|uninstall [flags]
14
+ * brain recall "<query>" [--project P] [--task T] [--top N] [--context] [--reindex]
15
+ * brain memorize [--sync] Store memories from a JSON payload on stdin
16
+ * brain reinforce <id> [<id>...] Spaced reinforcement + Hebbian co-retrieval
17
+ * brain cloud <login|logout|push|pull|status>
18
+ * brain --help | brain --version
19
+ */
20
+
21
+ const path = require('path');
22
+
23
+ const VERSION = require('../package.json').version;
24
+
25
+ // Subcommands whose modules read process.argv.slice(2) directly. We strip the
26
+ // leading subcommand token, then require the module (which executes on load).
27
+ const DELEGATED = {
28
+ recall: 'recall.js',
29
+ reinforce: 'reinforce.js',
30
+ memorize: 'memorize.js',
31
+ cloud: 'cloud-sync.js',
32
+ 'session-start': 'session-start.js',
33
+ pin: 'pin.js',
34
+ unpin: 'unpin.js',
35
+ skill: 'skill.js',
36
+ };
37
+
38
+ // install.js finds its positional subcommand from process.argv itself, so it is
39
+ // required without stripping anything. Bare `brain` also routes here, preserving
40
+ // `npx brain-memory` via npm's single-bin fallback.
41
+ const INSTALLER = new Set(['install', 'update', 'uninstall']);
42
+
43
+ const HELP = `◉ brain — Brain Memory CLI
44
+
45
+ Usage: brain <command> [options]
46
+
47
+ Memory
48
+ recall "<query>" [--project P] [--task T] [--top N]
49
+ Deterministic recall (TF-IDF + decay + spreading)
50
+ recall --context Session-start recall from project context
51
+ recall --reindex Rebuild the search index
52
+ memorize [--sync] Store memories from a JSON payload on stdin
53
+ reinforce <id> [<id>...] Spaced reinforcement + Hebbian co-retrieval
54
+ pin <id> [--scope global|project:<name>] [--priority N]
55
+ Pin a memory into the always-present tier
56
+ unpin <id> Remove a memory from the always-present tier
57
+ skill <list|show|use|add|remove|export>
58
+ Manage procedural skills (progressive disclosure)
59
+ session-start [--project P] [--task T] [--top N]
60
+ Budget-bounded startup payload (agent-invoked)
61
+
62
+ Sync
63
+ cloud <login|logout|push|pull|status>
64
+
65
+ Setup
66
+ install Interactive installer (default when no command)
67
+ update Update an existing installation
68
+ uninstall [--delete-data] Remove the installation
69
+
70
+ Other
71
+ --help, -h Show this help
72
+ --version, -v Show version
73
+ `;
74
+
75
+ function main() {
76
+ const sub = process.argv[2];
77
+
78
+ if (sub === '--version' || sub === '-v') {
79
+ console.log(VERSION);
80
+ return;
81
+ }
82
+
83
+ if (sub === '--help' || sub === '-h' || sub === 'help') {
84
+ console.log(HELP);
85
+ return;
86
+ }
87
+
88
+ // Bare `brain` or an installer subcommand → run the installer.
89
+ if (sub === undefined || INSTALLER.has(sub)) {
90
+ require(path.join(__dirname, 'install.js'));
91
+ return;
92
+ }
93
+
94
+ const file = DELEGATED[sub];
95
+ if (file) {
96
+ process.argv.splice(2, 1); // drop the subcommand token
97
+ const mod = require(path.join(__dirname, file));
98
+ // Modules guarded with `require.main === module` (e.g. session-start) export
99
+ // a main() that we must invoke; legacy modules already ran on require.
100
+ if (mod && typeof mod.main === 'function') {
101
+ mod.main(process.argv.slice(2));
102
+ }
103
+ return;
104
+ }
105
+
106
+ console.error(`Unknown command: ${sub}\n`);
107
+ console.error(HELP);
108
+ process.exit(1);
109
+ }
110
+
111
+ main();
@@ -0,0 +1,150 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * brain cloud — CLI for Brain Cloud sync
5
+ *
6
+ * Usage:
7
+ * brain cloud login [--api-url URL] Authenticate via device code flow
8
+ * brain cloud logout Clear stored credentials
9
+ * brain cloud push Upload ~/.brain/ to the cloud
10
+ * brain cloud pull Download from the cloud to ~/.brain/
11
+ * brain cloud status Show connection and sync status
12
+ */
13
+
14
+ const cloud = require('../src/cloud-sync');
15
+ const { getBrainDir } = require('../src/index-manager');
16
+
17
+ // Resolved via getBrainDir so Cloud sync targets the same brain as the rest of
18
+ // the CLI, including a $BRAIN_DIR override (synced folder, git working copy, …).
19
+ const BRAIN_DIR = getBrainDir();
20
+
21
+ async function main() {
22
+ const args = process.argv.slice(2);
23
+ const command = args[0];
24
+
25
+ if (!command || command === '--help' || command === '-h') {
26
+ console.log(`
27
+ brain cloud — Brain Cloud sync CLI
28
+
29
+ Commands:
30
+ login [--api-url URL] Authenticate via device code flow
31
+ logout Clear stored credentials
32
+ push Upload ~/.brain/ to the cloud
33
+ pull Download from the cloud to ~/.brain/
34
+ status Show connection and sync status
35
+ `.trim());
36
+ process.exit(0);
37
+ }
38
+
39
+ switch (command) {
40
+ case 'login':
41
+ await cmdLogin(args);
42
+ break;
43
+ case 'logout':
44
+ cmdLogout();
45
+ break;
46
+ case 'push':
47
+ await cmdPush();
48
+ break;
49
+ case 'pull':
50
+ await cmdPull();
51
+ break;
52
+ case 'status':
53
+ await cmdStatus();
54
+ break;
55
+ default:
56
+ console.error(`Unknown command: ${command}`);
57
+ process.exit(1);
58
+ }
59
+ }
60
+
61
+ async function cmdLogin(args) {
62
+ let apiUrl;
63
+ const urlIdx = args.indexOf('--api-url');
64
+ if (urlIdx !== -1 && args[urlIdx + 1]) {
65
+ apiUrl = args[urlIdx + 1];
66
+ }
67
+
68
+ console.log('Requesting device code...');
69
+ const loginInfo = await cloud.login(BRAIN_DIR, apiUrl);
70
+
71
+ console.log();
72
+ console.log(' Open this URL in your browser to log in:');
73
+ console.log();
74
+ console.log(` ${loginInfo.verify_url}`);
75
+ console.log();
76
+ console.log(` Your code: ${loginInfo.user_code}`);
77
+ console.log();
78
+ console.log(` Waiting for approval (expires in ${Math.floor(loginInfo.expires_in / 60)} min)...`);
79
+
80
+ await loginInfo.waitForApproval();
81
+
82
+ const config = cloud.readConfig(BRAIN_DIR);
83
+ console.log();
84
+ console.log(`✓ Logged in as ${config.user_email || 'unknown'}`);
85
+ if (config.brain_id) {
86
+ console.log(` Brain ID: ${config.brain_id}`);
87
+ }
88
+ console.log(` API: ${config.api_url}`);
89
+ }
90
+
91
+ function cmdLogout() {
92
+ cloud.logout(BRAIN_DIR);
93
+ console.log('✓ Logged out. Credentials cleared.');
94
+ }
95
+
96
+ async function cmdPush() {
97
+ console.log('Packing ~/.brain/ ...');
98
+ const result = await cloud.push(BRAIN_DIR);
99
+ console.log();
100
+ console.log('✓ Push complete!');
101
+ console.log(` Size: ${formatBytes(result.size_bytes)}`);
102
+ console.log(` Files: ${result.file_count}`);
103
+ console.log(` Checksum: ${result.checksum}`);
104
+ }
105
+
106
+ async function cmdPull() {
107
+ console.log('Downloading from cloud...');
108
+ const result = await cloud.pull(BRAIN_DIR);
109
+ console.log();
110
+ console.log('✓ Pull complete!');
111
+ console.log(` Size: ${formatBytes(result.size_bytes)}`);
112
+ if (result.checksum) {
113
+ console.log(` Checksum: ${result.checksum}`);
114
+ }
115
+ }
116
+
117
+ async function cmdStatus() {
118
+ const s = await cloud.status(BRAIN_DIR);
119
+
120
+ if (!s.connected) {
121
+ console.log('☁️ Not connected to Brain Cloud');
122
+ console.log(' Run: brain cloud login');
123
+ return;
124
+ }
125
+
126
+ console.log('☁️ Brain Cloud Status');
127
+ console.log('─────────────────────────');
128
+ console.log(` API: ${s.api_url}`);
129
+ console.log(` User: ${s.user_email || 'unknown'}`);
130
+ console.log(` Brain ID: ${s.brain_id || 'none'}`);
131
+ if (s.brain_name) console.log(` Brain Name: ${s.brain_name}`);
132
+ if (s.brain_size !== null) console.log(` Cloud Size: ${formatBytes(s.brain_size)}`);
133
+ if (s.brain_files !== null) console.log(` Cloud Files: ${s.brain_files}`);
134
+ console.log(` Last Push: ${s.last_push || 'never'}`);
135
+ console.log(` Last Pull: ${s.last_pull || 'never'}`);
136
+ console.log(` Last Synced: ${s.last_synced || 'never'}`);
137
+ console.log(` Connected: ${s.connected_at || 'unknown'}`);
138
+ }
139
+
140
+ function formatBytes(bytes) {
141
+ if (bytes === 0 || bytes === null || bytes === undefined) return '0 B';
142
+ const units = ['B', 'KB', 'MB', 'GB'];
143
+ const i = Math.floor(Math.log(bytes) / Math.log(1024));
144
+ return `${(bytes / Math.pow(1024, i)).toFixed(i === 0 ? 0 : 1)} ${units[i]}`;
145
+ }
146
+
147
+ main().catch((err) => {
148
+ console.error(`Error: ${err.message}`);
149
+ process.exit(1);
150
+ });
package/bin/forget.js ADDED
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * brain forget <id> — archive a memory (recoverable). Deterministic helper used
5
+ * by the connector's `brain_forget` tool. Honors BRAIN_DIR.
6
+ *
7
+ * Archive (default): move the memory file into `_archived/`, record it in
8
+ * `_archived/index.json`, and remove it from the live index, associations,
9
+ * review queue, and search index — so it stops surfacing in recall but stays
10
+ * recoverable. Deep / forensic erasure remains the agent-driven `/brain:forget
11
+ * --deep` path; this primitive only archives.
12
+ *
13
+ * Usage:
14
+ * BRAIN_DIR=/path/to/.brain node bin/forget.js mem_20260101_abc123
15
+ *
16
+ * Output: JSON describing the result.
17
+ */
18
+
19
+ const fs = require('fs');
20
+ const path = require('path');
21
+
22
+ const {
23
+ getBrainDir,
24
+ readIndex, writeIndex, removeMemory,
25
+ readAssociations, writeAssociations, removeEdgesForMemory,
26
+ readReviewQueue, writeReviewQueue, removeFromReviewQueue,
27
+ readArchiveIndex, writeArchiveIndex,
28
+ } = require('../src/index-manager');
29
+ // Search-index helpers take (brainDir) — the same signature memorize/recall use.
30
+ // (index-manager exports same-named helpers with a (projectRoot) signature that
31
+ // re-appends `.brain`, which would silently no-op the search-index update here.)
32
+ const { removeDocument, readSearchIndex, writeSearchIndex } = require('../src/tfidf');
33
+
34
+ function main(argv) {
35
+ const args = argv || process.argv.slice(2);
36
+ const id = args.find((a) => a && !a.startsWith('--'));
37
+ const force = args.includes('--force');
38
+ if (!id) {
39
+ console.error(JSON.stringify({ error: 'Usage: brain forget <id> [--force]' }));
40
+ process.exit(1);
41
+ }
42
+
43
+ const brainDir = getBrainDir();
44
+ const index = readIndex();
45
+ if (!index) {
46
+ console.error(JSON.stringify({ error: 'Brain not initialized.' }));
47
+ process.exit(1);
48
+ }
49
+ const entry = index.memories[id];
50
+ if (!entry) {
51
+ console.error(JSON.stringify({ error: `Memory not found: ${id}` }));
52
+ process.exit(1);
53
+ }
54
+
55
+ // CoALA salience protection: high-salience memories are "never auto-pruned"
56
+ // (the documented guarantee). Enforce it deterministically here — the only
57
+ // archival primitive — instead of relying on the agent to honor it. --force
58
+ // overrides for a deliberate, explicit removal.
59
+ const SALIENCE_FLOOR = 0.7;
60
+ if (!force && typeof entry.salience === 'number' && entry.salience >= SALIENCE_FLOOR) {
61
+ console.error(JSON.stringify({
62
+ error: `Refusing to archive high-salience memory ${id} (salience ${entry.salience} >= ${SALIENCE_FLOOR}). Pass --force to override.`,
63
+ protected: true,
64
+ }));
65
+ process.exit(1);
66
+ }
67
+
68
+ const now = new Date().toISOString();
69
+
70
+ // 1. Move the memory file into _archived/ (recoverable).
71
+ let archivedPath = null;
72
+ if (entry.path) {
73
+ const src = path.join(brainDir, entry.path);
74
+ const dest = path.join(brainDir, '_archived', entry.path);
75
+ if (fs.existsSync(src)) {
76
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
77
+ fs.renameSync(src, dest);
78
+ archivedPath = path.join('_archived', entry.path);
79
+ }
80
+ }
81
+
82
+ // 2. Record in the archive index.
83
+ const arch = readArchiveIndex() || { version: 1, archived_count: 0, memories: {} };
84
+ arch.memories[id] = {
85
+ path: entry.path, archived_path: archivedPath, title: entry.title, type: entry.type,
86
+ cognitive_type: entry.cognitive_type, strength: entry.strength, salience: entry.salience,
87
+ confidence: entry.confidence, tags: entry.tags || [], archived_date: now, archived_reason: 'forget',
88
+ };
89
+ arch.archived_count = Object.keys(arch.memories).length;
90
+ writeArchiveIndex(arch);
91
+
92
+ // 3. Remove from the live index.
93
+ removeMemory(index, id);
94
+ writeIndex(index);
95
+
96
+ // 4. Remove association edges.
97
+ const assoc = readAssociations() || { version: 1, edges: {} };
98
+ removeEdgesForMemory(assoc, id);
99
+ writeAssociations(assoc);
100
+
101
+ // 5. Remove from the review queue.
102
+ const queue = readReviewQueue();
103
+ if (queue) {
104
+ removeFromReviewQueue(queue, id);
105
+ writeReviewQueue(queue);
106
+ }
107
+
108
+ // 6. Remove from the search index (so it stops being recalled).
109
+ const searchIndex = readSearchIndex(brainDir);
110
+ if (searchIndex) {
111
+ removeDocument(searchIndex, id);
112
+ writeSearchIndex(brainDir, searchIndex);
113
+ }
114
+
115
+ console.log(JSON.stringify({ archived: true, id, title: entry.title, memory_count: index.memory_count }));
116
+ }
117
+
118
+ if (require.main === module) {
119
+ main(process.argv.slice(2));
120
+ }
121
+
122
+ module.exports = { main };