claude-brain 0.29.2 → 0.30.1

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.29.2
1
+ 0.30.1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-brain",
3
- "version": "0.29.2",
3
+ "version": "0.30.1",
4
4
  "description": "Local development assistant bridging Obsidian vaults with Claude Code via MCP",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -9,9 +9,11 @@
9
9
  },
10
10
  "files": [
11
11
  "src/**/*.ts",
12
+ "src/intelligence/hf-manifest.json",
12
13
  "src/hooks/claude-code-mastery.md",
13
14
  "scripts/postinstall.mjs",
14
15
  "packs/",
16
+ "skills/",
15
17
  "assets/",
16
18
  "package.json",
17
19
  "tsconfig.json",
@@ -0,0 +1,148 @@
1
+ ---
2
+ name: persistent-memory
3
+ description: Persistent memory management for Claude sessions using the brain MCP tool. Use when starting a work session, making architectural decisions, learning from mistakes, ending a session, or when user says "remember this", "what do I know about", "recall", "brain", or "session summary". Also use when searching code symbols, files, or dependencies with search_code.
4
+ license: MIT
5
+ metadata:
6
+ author: claude-brain
7
+ version: 1.0.0
8
+ mcp-server: claude-brain
9
+ category: productivity
10
+ tags: [memory, persistence, decisions, context, code-intelligence]
11
+ ---
12
+
13
+ # Persistent Memory
14
+
15
+ Manage persistent memory across Claude sessions using the `brain` and `search_code` MCP tools. Memory persists between conversations — decisions, lessons, and context survive session boundaries.
16
+
17
+ ## Critical Rules
18
+
19
+ - The `brain` tool auto-classifies intent. Most of the time, just pass a natural language message — don't force an action unless you have a reason.
20
+ - Do NOT store file paths, "read file X" events, or progress like "ran tests" — these are too granular and captured automatically by hooks.
21
+ - DO store the WHY — reasoning behind decisions, user preferences, lessons learned from debugging.
22
+ - Use the `project` parameter to scope memories to the right project. If omitted, it's auto-detected from the message.
23
+
24
+ ## Session Workflow
25
+
26
+ ### Step 1: Session Start — Recall Context
27
+
28
+ At the beginning of significant work, recall what you know:
29
+
30
+ ```
31
+ brain("What do I know about this project?")
32
+ ```
33
+
34
+ This returns past decisions, preferences, patterns, and lessons. Use it to avoid re-asking questions the user already answered in previous sessions.
35
+
36
+ Expected output: A summary of stored memories with relevance scores.
37
+
38
+ ### Step 2: During Work — Store Decisions and Lessons
39
+
40
+ Store when you encounter something worth remembering:
41
+
42
+ **Decisions:**
43
+ ```
44
+ brain("Decided to use JWT over sessions because the app is stateless")
45
+ ```
46
+
47
+ **Mistakes and fixes:**
48
+ ```
49
+ brain("The bug was caused by missing CORS credentials — fixed with credentials: include")
50
+ ```
51
+
52
+ **User preferences:**
53
+ ```
54
+ brain("User prefers explicit error messages over generic 500s")
55
+ ```
56
+
57
+ **Architecture changes:**
58
+ ```
59
+ brain("Changed database from MySQL to Postgres for better JSON support")
60
+ ```
61
+
62
+ ### Step 3: Session End — Summarize
63
+
64
+ Before finishing significant work, store a 2-3 sentence summary:
65
+
66
+ ```
67
+ brain("Session summary: Built auth flow for expense tracker. Chose JWT for stateless architecture. Hit CORS issue on /api/login, fixed with credentials: include.")
68
+ ```
69
+
70
+ ## Using search_code
71
+
72
+ The `search_code` tool searches indexed code — faster than grep for large projects.
73
+
74
+ **Search symbols (functions, classes, types):**
75
+ ```
76
+ search_code({ query: "handleAuth", project: "my-app" })
77
+ ```
78
+
79
+ **Search files by name:**
80
+ ```
81
+ search_code({ query: "config", project: "my-app", type: "files" })
82
+ ```
83
+
84
+ **Show dependencies of a file:**
85
+ ```
86
+ search_code({ query: "deps", project: "my-app", type: "dependencies", file_path: "src/auth.ts" })
87
+ ```
88
+
89
+ If search returns no results, the project may need indexing. Run `claude-brain reindex` first.
90
+
91
+ ## Advanced Patterns
92
+
93
+ ### Updating Past Memories
94
+ When a previous decision changes:
95
+ ```
96
+ brain({ message: "Changed my mind — use Postgres instead of MySQL", action: "update" })
97
+ ```
98
+
99
+ ### Getting Full Details
100
+ Search returns compact summaries. For the full stored content:
101
+ ```
102
+ brain("details 42")
103
+ ```
104
+ where 42 is the memory ID from a previous search result.
105
+
106
+ ### Deleting Outdated Memories
107
+ ```
108
+ brain({ message: "delete memory about MySQL choice", action: "delete" })
109
+ ```
110
+
111
+ ### Scoping to Projects
112
+ Always pass the project name for multi-project work:
113
+ ```
114
+ brain({ message: "Chose Tailwind for styling", project: "expense-tracker" })
115
+ ```
116
+
117
+ ## What to Store vs. Skip
118
+
119
+ ### Store These
120
+ - Architectural decisions and their reasoning
121
+ - User preferences for tools, style, workflow
122
+ - Solutions to hard debugging problems
123
+ - Patterns confirmed across multiple sessions
124
+ - Key file paths and project structure insights
125
+
126
+ ### Skip These
127
+ - File read/write events (captured by hooks)
128
+ - "Ran tests" / "built successfully" (too granular)
129
+ - Anything already in CLAUDE.md or the codebase
130
+ - Speculative conclusions from reading a single file
131
+
132
+ ## Troubleshooting
133
+
134
+ ### brain tool returns "no relevant memories"
135
+ **Cause:** No memories stored yet, or query doesn't match stored content.
136
+ **Solution:** Try broader phrasing. Use `brain("list all memories for project X")` to see what's stored.
137
+
138
+ ### search_code returns empty results
139
+ **Cause:** Project not indexed.
140
+ **Solution:** Run `claude-brain reindex` in terminal, then retry.
141
+
142
+ ### Memories scoped to wrong project
143
+ **Cause:** Project name not specified or auto-detected incorrectly.
144
+ **Solution:** Always pass `project` parameter explicitly for multi-project work.
145
+
146
+ ### brain tool seems slow
147
+ **Cause:** Large memory database or complex semantic search.
148
+ **Solution:** Use more specific queries. Scope to a project to narrow search space.
@@ -0,0 +1,90 @@
1
+ # Tool Reference
2
+
3
+ Complete parameter reference for the brain and search_code MCP tools.
4
+
5
+ ## brain
6
+
7
+ Your persistent memory. Tell it decisions, ask it questions, or update/delete past notes.
8
+
9
+ ### Parameters
10
+
11
+ | Parameter | Type | Required | Description |
12
+ |-----------|------|----------|-------------|
13
+ | `message` | string | Yes | What you are doing, decided, learned, or need. Natural language. |
14
+ | `project` | string | No | Project name to scope memories (e.g., "my-app"). Auto-detected if omitted. |
15
+ | `action` | enum | No | Force action: `auto`, `store`, `recall`, `update`, `delete`. Default: `auto`. |
16
+
17
+ ### Intent Classification
18
+
19
+ When `action` is `auto` (default), the tool classifies your message into one of these intents:
20
+
21
+ - **session_start** — "What do I know about X?" / beginning of session
22
+ - **decision_made** — "Decided to use X because Y"
23
+ - **store_this** — "Remember that..." / storing information
24
+ - **pattern_found** — "I noticed a pattern where..."
25
+ - **mistake_learned** — "The bug was X, fixed by Y"
26
+ - **question** — "What was the reason for X?"
27
+ - **context_needed** — Requesting stored context
28
+ - **update_memory** — Changing a previous memory
29
+ - **delete_memory** — Removing a memory
30
+ - **detail_request** — "details {ID}" for full content
31
+ - **list_all** — "list all memories" / "show everything"
32
+ - **timeline** — "show history" / "what happened when"
33
+ - **comparison** — "compare X vs Y decisions"
34
+ - **exploration** — Open-ended exploration of stored knowledge
35
+ - **progress_update** — "Completed X, next steps Y"
36
+
37
+ ### Response Format
38
+
39
+ ```json
40
+ {
41
+ "action": "stored | retrieved | updated | deleted | none",
42
+ "summary": "Brief description of what happened",
43
+ "content": "Full response with details",
44
+ "relevantItems": 5
45
+ }
46
+ ```
47
+
48
+ ## search_code
49
+
50
+ Search indexed code for symbols, files, or dependencies. Faster than grep for indexed projects.
51
+
52
+ ### Parameters
53
+
54
+ | Parameter | Type | Required | Default | Description |
55
+ |-----------|------|----------|---------|-------------|
56
+ | `query` | string | Yes | — | Symbol name, file name, or search term |
57
+ | `project` | string | Yes | — | Project name (usually directory name) |
58
+ | `type` | enum | No | `symbols` | `symbols`, `files`, or `dependencies` |
59
+ | `file_path` | string | No* | — | Required when type is `dependencies` |
60
+ | `limit` | number | No | 20 | Max results (1-100) |
61
+
62
+ ### Search Types
63
+
64
+ **symbols** (default): Search functions, classes, types, interfaces by name.
65
+ ```
66
+ search_code({ query: "UserService", project: "my-app" })
67
+ ```
68
+
69
+ **files**: Search for files by name pattern.
70
+ ```
71
+ search_code({ query: "middleware", project: "my-app", type: "files" })
72
+ ```
73
+
74
+ **dependencies**: Show imports and imported-by for a specific file.
75
+ ```
76
+ search_code({ query: "deps", project: "my-app", type: "dependencies", file_path: "src/auth/service.ts" })
77
+ ```
78
+
79
+ ### Supported File Types
80
+
81
+ `.ts .tsx .js .jsx .mjs .cjs .py .go .rs .vue .html .css .json .yaml .yml`
82
+
83
+ ### Indexing
84
+
85
+ If search returns no results, the project needs indexing:
86
+ ```bash
87
+ claude-brain reindex
88
+ ```
89
+
90
+ This parses the codebase using tree-sitter and stores symbols in SQLite for fast lookup.
@@ -16,10 +16,13 @@ import { join } from 'node:path'
16
16
  import { homedir } from 'node:os'
17
17
  import { parseArgs } from 'citty'
18
18
  import { renderLogo, theme, heading, dimText, successText, warningText, errorText, box, summaryPanel } from '@/cli/ui/index.js'
19
+ import { progressBar } from '@/cli/ui/components.js'
19
20
  import { getHomePaths, getClaudeBrainHome } from '@/config/home'
20
21
  import { getTrainingStats, type TrainingTask } from '@/training/data-store'
21
22
  import type { ModelManifest, ModelManifestEntry, ModelTask } from '@/intelligence/model-manager'
22
23
  import { shouldRetrain, retrainTask, retrainAll, type RetrainConfig } from '@/training/retrain-pipeline'
24
+ import { downloadFromHuggingFace, type HfManifest } from '@/intelligence/hf-downloader'
25
+ import hfManifestData from '@/intelligence/hf-manifest.json'
23
26
 
24
27
  const ALL_TASKS: ModelTask[] = ['intent', 'entity', 'query', 'knowledge', 'compress', 'pattern']
25
28
 
@@ -38,7 +41,7 @@ export async function runModels() {
38
41
  subcommand: { type: 'positional', required: false, description: 'Subcommand: list, status, download, enable, disable, benchmark, stats, retrain' },
39
42
  taskArg: { type: 'positional', required: false, description: 'Task name or "all" (for enable/disable/benchmark/retrain)' },
40
43
  task: { type: 'string', description: 'Target task (for download --task)' },
41
- source: { type: 'string', description: 'Source: local (default) or release' },
44
+ source: { type: 'string', description: 'Source: local (default) or hf (Hugging Face Hub)' },
42
45
  force: { type: 'boolean', description: 'Force retrain even if checks say not needed' },
43
46
  })
44
47
 
@@ -256,7 +259,7 @@ function loadManifest(): ModelManifest | null {
256
259
 
257
260
  // ─── download ─────────────────────────────────────────────────────
258
261
 
259
- function downloadModels(taskFilter: string, source: string) {
262
+ async function downloadModels(taskFilter: string, source: string) {
260
263
  console.log()
261
264
  console.log(renderLogo())
262
265
  console.log()
@@ -280,17 +283,9 @@ function downloadModels(taskFilter: string, source: string) {
280
283
  console.log(successText(`Created models directory: ${paths.models}`))
281
284
  }
282
285
 
283
- // Release source not yet implemented
284
- if (source === 'release') {
285
- console.log(
286
- box(
287
- 'Downloading from release artifacts is not yet available.\n' +
288
- 'Use --source local to install from ~/slm-training/models/ instead.',
289
- 'Coming Soon'
290
- )
291
- )
292
- console.log()
293
- return
286
+ // Hugging Face Hub source
287
+ if (source === 'hf' || source === 'release') {
288
+ return downloadFromHF(tasks, paths.models)
294
289
  }
295
290
 
296
291
  // Local source — copy from ~/slm-training/models/
@@ -409,6 +404,109 @@ function downloadModels(taskFilter: string, source: string) {
409
404
  console.log()
410
405
  }
411
406
 
407
+ // ─── download from HF ─────────────────────────────────────────────
408
+
409
+ const isTTY = process.stdout.isTTY === true
410
+
411
+ async function downloadFromHF(tasks: ModelTask[], modelsDir: string) {
412
+ const manifest = hfManifestData as HfManifest
413
+
414
+ // Compute total download size
415
+ let totalSize = 0
416
+ for (const task of tasks) {
417
+ const entry = manifest.models[task]
418
+ if (entry) totalSize += entry.size
419
+ }
420
+
421
+ console.log(` ${dimText('Source:')} Hugging Face Hub (${manifest.hfRepo})`)
422
+ console.log(` ${dimText('Target:')} ${modelsDir}`)
423
+ console.log(` ${dimText('Models:')} ${tasks.join(', ')}`)
424
+ console.log(` ${dimText('Total:')} ~${formatBytes(totalSize)}`)
425
+ console.log()
426
+
427
+ const results = await downloadFromHuggingFace(manifest, {
428
+ destDir: modelsDir,
429
+ tasks,
430
+ onProgress(task, downloaded, total) {
431
+ if (isTTY && total > 0) {
432
+ const pct = (downloaded / total) * 100
433
+ const bar = progressBar(pct, 25)
434
+ process.stdout.write(`\r ${task.padEnd(12)} ${bar} ${formatBytes(downloaded)} / ${formatBytes(total)}`)
435
+ }
436
+ },
437
+ onComplete(task, bytes) {
438
+ if (isTTY) process.stdout.write('\r' + ' '.repeat(80) + '\r')
439
+ console.log(` ${successText(task.padEnd(12))} ${formatBytes(bytes)} ${dimText('SHA256 verified')}`)
440
+ },
441
+ onError(task, error) {
442
+ if (isTTY) process.stdout.write('\r' + ' '.repeat(80) + '\r')
443
+ console.log(` ${errorText(task.padEnd(12))} ${error}`)
444
+ },
445
+ })
446
+
447
+ console.log()
448
+
449
+ const succeeded = results.filter(r => r.success)
450
+ if (succeeded.length === 0) {
451
+ console.log(warningText('No models were downloaded.'))
452
+ console.log()
453
+ return
454
+ }
455
+
456
+ // Build manifest from downloaded results + existing
457
+ const manifestPath = join(modelsDir, 'manifest.json')
458
+ const manifestModels: Partial<Record<ModelTask, ModelManifestEntry>> = {}
459
+
460
+ // Preserve existing manifest entries
461
+ if (existsSync(manifestPath)) {
462
+ try {
463
+ const existing: ModelManifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
464
+ if (existing.models) Object.assign(manifestModels, existing.models)
465
+ } catch { /* overwrite corrupt */ }
466
+ }
467
+
468
+ const installedTasks: ModelTask[] = []
469
+ let totalBytes = 0
470
+
471
+ for (const result of succeeded) {
472
+ const task = result.task as ModelTask
473
+ const entry = manifest.models[task]
474
+ if (!entry) continue
475
+
476
+ manifestModels[task] = {
477
+ version: entry.version,
478
+ file: entry.file,
479
+ sha256: entry.sha256,
480
+ params: entry.params,
481
+ accuracy: entry.accuracy ?? undefined,
482
+ labels: entry.labels,
483
+ maxSeqLen: entry.maxSeqLen,
484
+ }
485
+ installedTasks.push(task)
486
+ totalBytes += result.bytes
487
+ }
488
+
489
+ // Write manifest
490
+ const localManifest: ModelManifest = { models: manifestModels }
491
+ writeFileSync(manifestPath, JSON.stringify(localManifest, null, 2))
492
+
493
+ // Auto-enable models in config
494
+ const config = loadConfigFile()
495
+ if (!config.slm) config.slm = {}
496
+ config.slm.enabled = true
497
+ if (!config.slm.tasks) config.slm.tasks = {}
498
+ for (const task of installedTasks) {
499
+ config.slm.tasks[task] = 'model'
500
+ }
501
+ saveConfigFile(config)
502
+ updateConfigYml(installedTasks, 'model')
503
+
504
+ console.log(successText(`Downloaded ${succeeded.length} model${succeeded.length !== 1 ? 's' : ''} (${formatBytes(totalBytes)})`))
505
+ console.log(successText(`Auto-enabled ${installedTasks.join(', ')} in config`))
506
+ console.log(dimText(`Manifest written to ${manifestPath}`))
507
+ console.log()
508
+ }
509
+
412
510
  // ─── enable ───────────────────────────────────────────────────────
413
511
 
414
512
  function enableTask(taskArg: string) {
@@ -783,7 +881,7 @@ function printModelsHelp() {
783
881
  const subcommands = [
784
882
  ['list', 'Show installed models and their status'],
785
883
  ['status', 'Show inference routing and ONNX runtime status'],
786
- ['download', 'Download pre-trained models (--task <task>|all)'],
884
+ ['download', 'Download models (--source local|hf, --task <task>|all)'],
787
885
  ['enable <task|all>', 'Enable model inference for task(s)'],
788
886
  ['disable <task|all>', 'Disable model inference for task(s)'],
789
887
  ['benchmark <task>', 'Run accuracy benchmark on test data'],
@@ -805,6 +903,9 @@ function printModelsHelp() {
805
903
  console.log(theme.bold('Examples:'))
806
904
  console.log(` ${dimText('claude-brain models list')}`)
807
905
  console.log(` ${dimText('claude-brain models status')}`)
906
+ console.log(` ${dimText('claude-brain models download --source hf')}`)
907
+ console.log(` ${dimText('claude-brain models download --source hf --task intent')}`)
908
+ console.log(` ${dimText('claude-brain models download --source local')}`)
808
909
  console.log(` ${dimText('claude-brain models enable all')}`)
809
910
  console.log(` ${dimText('claude-brain models enable intent')}`)
810
911
  console.log(` ${dimText('claude-brain models disable pattern')}`)
@@ -47,14 +47,21 @@ export async function runServe() {
47
47
  return runAsDaemon(httpOnly, pidManager)
48
48
  }
49
49
 
50
- /** Check if the daemon at the given port is responsive and initialized */
50
+ /** Check if the daemon at the given port is responsive, initialized, and can serve MCP tools */
51
51
  async function isDaemonHealthy(port: number): Promise<boolean> {
52
52
  try {
53
- const res = await fetch(`http://localhost:${port}/api/health`, {
53
+ const healthRes = await fetch(`http://localhost:${port}/api/health`, {
54
54
  signal: AbortSignal.timeout(2000),
55
55
  })
56
- const json = await res.json() as any
57
- return json.success === true && json.initialized === true
56
+ const healthJson = await healthRes.json() as any
57
+ if (healthJson.success !== true || healthJson.initialized !== true) return false
58
+
59
+ // Verify MCP proxy endpoints actually work (old versions return 404)
60
+ const toolsRes = await fetch(`http://localhost:${port}/api/mcp/list-tools`, {
61
+ signal: AbortSignal.timeout(2000),
62
+ })
63
+ const toolsJson = await toolsRes.json() as any
64
+ return toolsJson.success === true && Array.isArray(toolsJson.data?.tools)
58
65
  } catch {
59
66
  return false
60
67
  }
@@ -93,7 +100,9 @@ async function runAsProxy(daemonPort: number) {
93
100
 
94
101
  process.on('SIGTERM', () => stopProxy('SIGTERM'))
95
102
  process.on('SIGINT', () => stopProxy('SIGINT'))
96
- process.on('SIGHUP', () => stopProxy('SIGHUP'))
103
+ if (process.platform !== 'win32') {
104
+ process.on('SIGHUP', () => stopProxy('SIGHUP'))
105
+ }
97
106
 
98
107
  await proxy.start()
99
108
  mainLogger.info('MCP proxy ready — forwarding to daemon')
@@ -382,15 +391,43 @@ async function runAsDaemon(httpOnly: boolean, pidManager: ServerPidManager) {
382
391
  } catch (error) {
383
392
  mainLogger.debug({ error }, 'No hook queue to drain')
384
393
  }
385
- } catch (error) {
386
- mainLogger.error({ error }, 'Failed to start HTTP API server')
394
+ } catch (error: any) {
395
+ // EADDRINUSE: kill the stale process and retry once
396
+ if (error?.code === 'EADDRINUSE' || String(error).includes('EADDRINUSE')) {
397
+ mainLogger.warn({ port: config.port }, 'Port in use — killing stale process and retrying')
398
+ try {
399
+ const { killProcessOnPort } = await import('@/utils/kill-port')
400
+ const killed = killProcessOnPort(config.port)
401
+ if (killed.length > 0) {
402
+ mainLogger.info({ killed }, 'Killed stale process(es) on port')
403
+ }
404
+ await new Promise(r => setTimeout(r, 1000))
405
+ await httpServer.start()
406
+ mainLogger.info({ port: config.port }, 'HTTP API server started (after recovery)')
407
+
408
+ // Drain hook queue on retry success too
409
+ try {
410
+ const { drainQueue } = await import('@/hooks/queue')
411
+ const drained = await drainQueue(config.port)
412
+ if (drained > 0) {
413
+ mainLogger.info({ drained }, 'Drained hook queue')
414
+ }
415
+ } catch {}
416
+ } catch (retryError) {
417
+ mainLogger.error({ error: retryError }, 'Failed to start HTTP API server after recovery — MCP stdio still works')
418
+ }
419
+ } else {
420
+ mainLogger.error({ error }, 'Failed to start HTTP API server')
421
+ }
387
422
  }
388
423
  }, 2000)
389
424
 
390
425
  // ── Signal handlers ──────────────────────────────────────
391
426
  process.on('SIGTERM', () => shutdown('SIGTERM'))
392
427
  process.on('SIGINT', () => shutdown('SIGINT'))
393
- process.on('SIGHUP', () => shutdown('SIGHUP'))
428
+ if (process.platform !== 'win32') {
429
+ process.on('SIGHUP', () => shutdown('SIGHUP'))
430
+ }
394
431
 
395
432
  if (httpOnly) {
396
433
  // HTTP-only daemon mode: no MCP stdio. Use idle watchdog instead of infinite keepAlive.
@@ -362,6 +362,8 @@ export const ConfigSchema = z.object({
362
362
  enabled: z.boolean().default(false),
363
363
  /** Directory containing ONNX model files */
364
364
  modelsDir: z.string().default('~/.claude-brain/models'),
365
+ /** Hugging Face repo for downloading pre-trained models */
366
+ hfRepo: z.string().default('demgun101/claude-brain-models'),
365
367
  /** Minimum model confidence to use model prediction (below → regex fallback) */
366
368
  confidenceThreshold: z.number().min(0).max(1).default(0.7),
367
369
  /** Per-task mode: 'model' uses model only, 'regex' uses regex only, 'both' runs both and logs comparison */
@@ -0,0 +1,222 @@
1
+ /**
2
+ * Hugging Face Hub Downloader — streams ONNX models from HF with SHA256 verification.
3
+ * Atomic writes via temp file + rename. Inline retry with exponential backoff.
4
+ */
5
+
6
+ import { createHash } from 'node:crypto'
7
+ import { existsSync, mkdirSync, renameSync, unlinkSync } from 'node:fs'
8
+ import { join } from 'node:path'
9
+ import type { ModelTask } from '@/intelligence/model-manager'
10
+
11
+ export interface HfManifestEntry {
12
+ file: string
13
+ metaFile: string
14
+ sha256: string
15
+ metaSha256: string
16
+ size: number
17
+ version: string
18
+ params: string
19
+ accuracy: number | null
20
+ labels: string[]
21
+ maxSeqLen: number
22
+ }
23
+
24
+ export interface HfManifest {
25
+ hfRepo: string
26
+ hfBranch: string
27
+ models: Record<string, HfManifestEntry>
28
+ }
29
+
30
+ export interface DownloadOptions {
31
+ /** Target directory to write models into */
32
+ destDir: string
33
+ /** Tasks to download (subset of manifest keys) */
34
+ tasks: ModelTask[]
35
+ /** HF repo override (default from manifest) */
36
+ hfRepo?: string
37
+ /** HF branch override (default from manifest) */
38
+ hfBranch?: string
39
+ /** Progress callback: task name, bytes downloaded so far, total bytes */
40
+ onProgress?: (task: string, downloaded: number, total: number) => void
41
+ /** Called when a task completes */
42
+ onComplete?: (task: string, bytes: number) => void
43
+ /** Called on error */
44
+ onError?: (task: string, error: string) => void
45
+ }
46
+
47
+ export interface DownloadResult {
48
+ task: string
49
+ success: boolean
50
+ bytes: number
51
+ error?: string
52
+ }
53
+
54
+ const MAX_RETRIES = 3
55
+ const RETRY_DELAYS = [2000, 4000, 8000]
56
+ const DOWNLOAD_TIMEOUT_MS = 300_000 // 5 minutes per file
57
+
58
+ /**
59
+ * Build HF resolve URL for a file.
60
+ * Pattern: https://huggingface.co/{repo}/resolve/{branch}/{filename}
61
+ */
62
+ function hfUrl(repo: string, branch: string, filename: string): string {
63
+ return `https://huggingface.co/${repo}/resolve/${branch}/${filename}`
64
+ }
65
+
66
+ /**
67
+ * Stream-download a single file with SHA256 verification and atomic write.
68
+ * Returns the number of bytes written.
69
+ */
70
+ async function downloadFile(
71
+ url: string,
72
+ destPath: string,
73
+ expectedSha256: string,
74
+ expectedSize: number,
75
+ onProgress?: (downloaded: number, total: number) => void,
76
+ ): Promise<number> {
77
+ const tempPath = `${destPath}.download`
78
+
79
+ // Clean up any leftover temp file
80
+ if (existsSync(tempPath)) {
81
+ unlinkSync(tempPath)
82
+ }
83
+
84
+ const response = await fetch(url, {
85
+ signal: AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS),
86
+ })
87
+
88
+ if (!response.ok) {
89
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`)
90
+ }
91
+
92
+ if (!response.body) {
93
+ throw new Error('Response body is null')
94
+ }
95
+
96
+ const contentLength = parseInt(response.headers.get('content-length') || '0', 10) || expectedSize
97
+ const hash = createHash('sha256')
98
+ const writer = Bun.file(tempPath).writer()
99
+ let downloaded = 0
100
+
101
+ const reader = response.body.getReader()
102
+
103
+ try {
104
+ while (true) {
105
+ const { done, value } = await reader.read()
106
+ if (done) break
107
+
108
+ writer.write(value)
109
+ hash.update(value)
110
+ downloaded += value.byteLength
111
+
112
+ if (onProgress) {
113
+ onProgress(downloaded, contentLength)
114
+ }
115
+ }
116
+ } finally {
117
+ await writer.end()
118
+ }
119
+
120
+ // Verify SHA256
121
+ const actualSha256 = hash.digest('hex')
122
+ if (actualSha256 !== expectedSha256) {
123
+ // Clean up bad file
124
+ if (existsSync(tempPath)) unlinkSync(tempPath)
125
+ throw new Error(
126
+ `SHA256 mismatch: expected ${expectedSha256.slice(0, 12)}..., got ${actualSha256.slice(0, 12)}...`
127
+ )
128
+ }
129
+
130
+ // Atomic rename
131
+ renameSync(tempPath, destPath)
132
+ return downloaded
133
+ }
134
+
135
+ /**
136
+ * Download a single file with retry logic.
137
+ */
138
+ async function downloadWithRetry(
139
+ url: string,
140
+ destPath: string,
141
+ expectedSha256: string,
142
+ expectedSize: number,
143
+ onProgress?: (downloaded: number, total: number) => void,
144
+ ): Promise<number> {
145
+ let lastError: Error | null = null
146
+
147
+ for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
148
+ try {
149
+ return await downloadFile(url, destPath, expectedSha256, expectedSize, onProgress)
150
+ } catch (err) {
151
+ lastError = err instanceof Error ? err : new Error(String(err))
152
+
153
+ // Clean up temp file on failure
154
+ const tempPath = `${destPath}.download`
155
+ if (existsSync(tempPath)) {
156
+ try { unlinkSync(tempPath) } catch { /* ignore */ }
157
+ }
158
+
159
+ if (attempt < MAX_RETRIES) {
160
+ await new Promise(resolve => setTimeout(resolve, RETRY_DELAYS[attempt]))
161
+ }
162
+ }
163
+ }
164
+
165
+ throw lastError!
166
+ }
167
+
168
+ /**
169
+ * Main entry point: download models from Hugging Face Hub.
170
+ */
171
+ export async function downloadFromHuggingFace(
172
+ manifest: HfManifest,
173
+ options: DownloadOptions,
174
+ ): Promise<DownloadResult[]> {
175
+ const repo = options.hfRepo || manifest.hfRepo
176
+ const branch = options.hfBranch || manifest.hfBranch
177
+ const results: DownloadResult[] = []
178
+
179
+ // Ensure dest directory exists
180
+ if (!existsSync(options.destDir)) {
181
+ mkdirSync(options.destDir, { recursive: true })
182
+ }
183
+
184
+ for (const task of options.tasks) {
185
+ const entry = manifest.models[task]
186
+ if (!entry) {
187
+ const err = `No manifest entry for task "${task}"`
188
+ options.onError?.(task, err)
189
+ results.push({ task, success: false, bytes: 0, error: err })
190
+ continue
191
+ }
192
+
193
+ try {
194
+ // Download ONNX model
195
+ const modelUrl = hfUrl(repo, branch, entry.file)
196
+ const modelDest = join(options.destDir, entry.file)
197
+
198
+ const bytes = await downloadWithRetry(
199
+ modelUrl,
200
+ modelDest,
201
+ entry.sha256,
202
+ entry.size,
203
+ (downloaded, total) => options.onProgress?.(task, downloaded, total),
204
+ )
205
+
206
+ // Download metadata JSON
207
+ const metaUrl = hfUrl(repo, branch, entry.metaFile)
208
+ const metaDest = join(options.destDir, entry.metaFile)
209
+
210
+ await downloadWithRetry(metaUrl, metaDest, entry.metaSha256, 0)
211
+
212
+ options.onComplete?.(task, bytes)
213
+ results.push({ task, success: true, bytes })
214
+ } catch (err) {
215
+ const msg = err instanceof Error ? err.message : String(err)
216
+ options.onError?.(task, msg)
217
+ results.push({ task, success: false, bytes: 0, error: msg })
218
+ }
219
+ }
220
+
221
+ return results
222
+ }
@@ -0,0 +1,78 @@
1
+ {
2
+ "hfRepo": "demgun101/claude-brain-models",
3
+ "hfBranch": "main",
4
+ "models": {
5
+ "intent": {
6
+ "file": "intent.onnx",
7
+ "metaFile": "intent.json",
8
+ "sha256": "f276ec091712f53ceeccfdca70d468d0b5aa6da7ee5c4bd7867a7464a9732dd2",
9
+ "metaSha256": "d5702fd45e5685187e74810da75e41be9d12d9fd82b7ccd7244a9f0d33076c65",
10
+ "size": 120073283,
11
+ "version": "0.1.0",
12
+ "params": "nanoGPT-intent",
13
+ "accuracy": 0.9904,
14
+ "labels": ["session_start", "context_needed", "decision_made", "store_this", "pattern_found", "mistake_learned", "progress_update", "question", "comparison", "exploration", "list_all", "update_memory", "delete_memory", "detail_request", "timeline", "no_action"],
15
+ "maxSeqLen": 128
16
+ },
17
+ "entity": {
18
+ "file": "entity.onnx",
19
+ "metaFile": "entity.json",
20
+ "sha256": "d15706b78112e8dda3caa7e054237106b67200f90a1e6e0e4336526468dea8b5",
21
+ "metaSha256": "1de0636bc3bd9de23c0ce767a148d6becad7b6add6a0a717449a7ac185296d55",
22
+ "size": 120058476,
23
+ "version": "0.1.0",
24
+ "params": "nanoGPT-entity",
25
+ "accuracy": 0.9795,
26
+ "labels": ["O", "B-TECH", "I-TECH", "B-PROJECT", "I-PROJECT", "B-CONCEPT", "I-CONCEPT"],
27
+ "maxSeqLen": 128
28
+ },
29
+ "query": {
30
+ "file": "query.onnx",
31
+ "metaFile": "query.json",
32
+ "sha256": "802446105ad873b4a172dc875c07e245882f48691f72ae0abb0bda5934fa084a",
33
+ "metaSha256": "32393875ff58a87da83480673868e4ac14c3fd1a59e16836d7c1f2fa02a7f25e",
34
+ "size": 120057880,
35
+ "version": "0.1.0",
36
+ "params": "nanoGPT-query",
37
+ "accuracy": 0.985,
38
+ "labels": ["factual", "procedural", "comparative", "temporal", "exploratory", "decision"],
39
+ "maxSeqLen": 128
40
+ },
41
+ "knowledge": {
42
+ "file": "knowledge.onnx",
43
+ "metaFile": "knowledge.json",
44
+ "sha256": "a786eefc5ef5c6f2fc132c6de7f0972891057f5a993c9c2d9264207d36165035",
45
+ "metaSha256": "6aa77194cbd8c10a2451958ec5a3e43328df97ee8aea46abf53e3e432f3f3c4d",
46
+ "size": 120056340,
47
+ "version": "0.1.0",
48
+ "params": "nanoGPT-knowledge",
49
+ "accuracy": 0.998,
50
+ "labels": ["fact", "preference", "constraint", "goal", "definition"],
51
+ "maxSeqLen": 128
52
+ },
53
+ "compress": {
54
+ "file": "compress.onnx",
55
+ "metaFile": "compress.json",
56
+ "sha256": "2d950a0e0a2cdc5dc90b7c44803c5fb81c76d824bd208c64a7104c5845e1c237",
57
+ "metaSha256": "e91500c665ec47083bb6b3ff8c83d529f263792d83f7f169401267ceb1e8d031",
58
+ "size": 357902441,
59
+ "version": "0.1.0",
60
+ "params": "nanoGPT-compress",
61
+ "accuracy": null,
62
+ "labels": [],
63
+ "maxSeqLen": 256
64
+ },
65
+ "pattern": {
66
+ "file": "pattern.onnx",
67
+ "metaFile": "pattern.json",
68
+ "sha256": "3b44371eaef11fb8ccc4c1636d6cfbdcdf62ae8bc6ffc808ff0ff45b24824fde",
69
+ "metaSha256": "1126f3fa9a115b4d26063e006e7d33b9e82b7740194ac86b5dae9f25d5dfd1a2",
70
+ "size": 254526620,
71
+ "version": "0.1.0",
72
+ "params": "nanoGPT-pattern",
73
+ "accuracy": 0.8667,
74
+ "labels": ["solution", "anti-pattern", "best-practice", "common-issue"],
75
+ "maxSeqLen": 128
76
+ }
77
+ }
78
+ }
@@ -8,6 +8,7 @@ import { existsSync, readFileSync, writeFileSync, mkdirSync, unlinkSync } from '
8
8
  import { join, dirname, resolve } from 'node:path'
9
9
  import { homedir, platform } from 'node:os'
10
10
  import { fileURLToPath } from 'node:url'
11
+ import { killProcessOnPort } from '@/utils/kill-port'
11
12
 
12
13
  const __filename = fileURLToPath(import.meta.url)
13
14
  const __dirname = dirname(__filename)
@@ -223,34 +224,8 @@ export class AutoUpdater {
223
224
  // No matching processes — that's fine
224
225
  }
225
226
 
226
- // Kill by port 3000
227
- try {
228
- if (isWindows) {
229
- const result = execSync(`netstat -ano | findstr :3000 | findstr LISTENING`, {
230
- encoding: 'utf-8', stdio: 'pipe', timeout: 5000,
231
- })
232
- const pids = new Set(
233
- result.split('\n')
234
- .map(line => line.trim().split(/\s+/).pop())
235
- .filter(p => p && Number(p) !== myPid)
236
- )
237
- for (const pid of pids) {
238
- try { execSync(`taskkill /F /PID ${pid}`, { stdio: 'pipe', timeout: 5000 }) } catch {}
239
- }
240
- } else {
241
- const raw = execSync(`lsof -ti :3000`, {
242
- encoding: 'utf-8', stdio: 'pipe', timeout: 5000,
243
- }).trim()
244
- if (raw) {
245
- const pids = raw.split('\n').filter(p => p && Number(p) !== myPid)
246
- for (const pid of pids) {
247
- try { process.kill(Number(pid), 'SIGKILL') } catch {}
248
- }
249
- }
250
- }
251
- } catch {
252
- // No process on port — that's fine
253
- }
227
+ // Kill by port 3000 using shared utility
228
+ killProcessOnPort(3000, myPid)
254
229
 
255
230
  // Clean up stale PID files
256
231
  const pidPath = join(this.dataDir, 'server.pid')
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  import { existsSync, readFileSync, writeFileSync, unlinkSync } from 'node:fs'
8
+ import { execSync } from 'node:child_process'
8
9
  import { join } from 'node:path'
9
10
  import { getHomePaths } from '@/config/home'
10
11
 
@@ -65,10 +66,32 @@ export class ServerPidManager {
65
66
  }
66
67
 
67
68
  // Signal 0 tests if process exists without killing it
68
- process.kill(pid, 0)
69
+ try {
70
+ process.kill(pid, 0)
71
+ } catch (signalError: any) {
72
+ // On Windows, process.kill(pid, 0) can throw unexpected errors
73
+ // Fall back to tasklist to verify the PID exists
74
+ if (process.platform === 'win32' && signalError?.code !== 'ESRCH') {
75
+ try {
76
+ const result = execSync(`tasklist /FI "PID eq ${pid}" /NH`, {
77
+ encoding: 'utf-8', stdio: 'pipe', timeout: 3000,
78
+ })
79
+ if (!result.includes(String(pid))) {
80
+ this.cleanup()
81
+ return null
82
+ }
83
+ } catch {
84
+ this.cleanup()
85
+ return null
86
+ }
87
+ } else {
88
+ this.cleanup()
89
+ return null
90
+ }
91
+ }
69
92
  return { pid, port }
70
93
  } catch {
71
- // Process not running or invalid file, clean up stale PID file
94
+ // Invalid file format, clean up stale PID file
72
95
  this.cleanup()
73
96
  return null
74
97
  }
@@ -97,7 +120,7 @@ export class ServerPidManager {
97
120
  }
98
121
  }
99
122
 
100
- /** Register cleanup handlers on SIGINT, SIGTERM, SIGHUP, and process exit. */
123
+ /** Register cleanup handlers on SIGINT, SIGTERM, SIGHUP (non-Windows), and process exit. */
101
124
  registerCleanupHandlers(): void {
102
125
  const doCleanup = () => {
103
126
  this.cleanup()
@@ -106,6 +129,8 @@ export class ServerPidManager {
106
129
  process.on('exit', doCleanup)
107
130
  process.on('SIGINT', doCleanup)
108
131
  process.on('SIGTERM', doCleanup)
109
- process.on('SIGHUP', doCleanup)
132
+ if (process.platform !== 'win32') {
133
+ process.on('SIGHUP', doCleanup)
134
+ }
110
135
  }
111
136
  }
@@ -52,3 +52,7 @@ export {
52
52
  getPhase12Instance,
53
53
  resetPhase12Cache
54
54
  } from './phase12-helper'
55
+
56
+ export {
57
+ killProcessOnPort
58
+ } from './kill-port'
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Cross-platform utility to kill the process holding a specific port.
3
+ * Used by serve.ts (EADDRINUSE recovery) and auto-updater.ts (ghost cleanup).
4
+ */
5
+
6
+ import { execSync } from 'node:child_process'
7
+
8
+ const isWindows = process.platform === 'win32'
9
+
10
+ /**
11
+ * Kill the process listening on the given port.
12
+ * Skips the current process (myPid) to avoid self-termination.
13
+ * Returns the PIDs that were killed, or an empty array if none found.
14
+ */
15
+ export function killProcessOnPort(port: number, myPid: number = process.pid): number[] {
16
+ const killed: number[] = []
17
+
18
+ try {
19
+ if (isWindows) {
20
+ const result = execSync(`netstat -ano | findstr :${port} | findstr LISTENING`, {
21
+ encoding: 'utf-8', stdio: 'pipe', timeout: 5000,
22
+ })
23
+ const pids = new Set(
24
+ result.split('\n')
25
+ .map(line => line.trim().split(/\s+/).pop())
26
+ .filter((p): p is string => !!p && Number(p) !== myPid && !isNaN(Number(p)))
27
+ )
28
+ for (const pid of pids) {
29
+ try {
30
+ execSync(`taskkill /F /PID ${pid}`, { stdio: 'pipe', timeout: 5000 })
31
+ killed.push(Number(pid))
32
+ } catch {}
33
+ }
34
+ } else {
35
+ const raw = execSync(`lsof -ti :${port}`, {
36
+ encoding: 'utf-8', stdio: 'pipe', timeout: 5000,
37
+ }).trim()
38
+ if (raw) {
39
+ const pids = raw.split('\n').filter(p => p && Number(p) !== myPid)
40
+ for (const pid of pids) {
41
+ try {
42
+ process.kill(Number(pid), 'SIGKILL')
43
+ killed.push(Number(pid))
44
+ } catch {}
45
+ }
46
+ }
47
+ }
48
+ } catch {
49
+ // No process on port — that's fine
50
+ }
51
+
52
+ return killed
53
+ }