monomind 2.1.0 → 2.1.3

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 (49) hide show
  1. package/README.md +74 -92
  2. package/package.json +3 -3
  3. package/packages/@monomind/cli/.claude/helpers/handlers/capture-handler.cjs +24 -10
  4. package/packages/@monomind/cli/.claude/helpers/handlers/gates-handler.cjs +2 -2
  5. package/packages/@monomind/cli/.claude/helpers/handlers/route-handler.cjs +29 -0
  6. package/packages/@monomind/cli/.claude/helpers/handlers/session-handler.cjs +35 -23
  7. package/packages/@monomind/cli/.claude/helpers/hook-handler.cjs +21 -0
  8. package/packages/@monomind/cli/.claude/helpers/utils/monograph.cjs +73 -2
  9. package/packages/@monomind/cli/README.md +74 -92
  10. package/packages/@monomind/cli/dist/src/browser/workflow/store.d.ts +2 -2
  11. package/packages/@monomind/cli/dist/src/commands/browse.d.ts +1 -1
  12. package/packages/@monomind/cli/dist/src/commands/doctor-env-checks.js +5 -0
  13. package/packages/@monomind/cli/dist/src/commands/org.js +46 -29
  14. package/packages/@monomind/cli/dist/src/commands/platforms.d.ts +1 -1
  15. package/packages/@monomind/cli/dist/src/init/executor.js +30 -6
  16. package/packages/@monomind/cli/dist/src/mcp-tools/auto-install.d.ts +8 -8
  17. package/packages/@monomind/cli/dist/src/mcp-tools/coherence/types.d.ts +31 -31
  18. package/packages/@monomind/cli/dist/src/mcp-tools/hive-mind-tools.js +5 -1
  19. package/packages/@monomind/cli/dist/src/mcp-tools/monograph-tools.js +28 -3
  20. package/packages/@monomind/cli/dist/src/mcp-tools/quality/coverage-analysis/prioritize-gaps.d.ts +36 -36
  21. package/packages/@monomind/cli/dist/src/mcp-tools/quality/security-compliance/detect-secrets.d.ts +4 -4
  22. package/packages/@monomind/cli/dist/src/memory/ewc-consolidation.d.ts +12 -0
  23. package/packages/@monomind/cli/dist/src/memory/sona-optimizer.d.ts +12 -0
  24. package/packages/@monomind/cli/dist/src/orgrt/broker.d.ts +3 -1
  25. package/packages/@monomind/cli/dist/src/orgrt/broker.js +8 -3
  26. package/packages/@monomind/cli/dist/src/orgrt/bus.d.ts +5 -1
  27. package/packages/@monomind/cli/dist/src/orgrt/bus.js +5 -1
  28. package/packages/@monomind/cli/dist/src/orgrt/daemon.d.ts +17 -1
  29. package/packages/@monomind/cli/dist/src/orgrt/daemon.js +115 -21
  30. package/packages/@monomind/cli/dist/src/orgrt/forwarder.js +41 -8
  31. package/packages/@monomind/cli/dist/src/orgrt/inbox.d.ts +11 -0
  32. package/packages/@monomind/cli/dist/src/orgrt/inbox.js +56 -0
  33. package/packages/@monomind/cli/dist/src/orgrt/policy.d.ts +5 -1
  34. package/packages/@monomind/cli/dist/src/orgrt/policy.js +58 -5
  35. package/packages/@monomind/cli/dist/src/orgrt/server.d.ts +3 -2
  36. package/packages/@monomind/cli/dist/src/orgrt/server.js +7 -40
  37. package/packages/@monomind/cli/dist/src/orgrt/session.d.ts +10 -1
  38. package/packages/@monomind/cli/dist/src/orgrt/session.js +24 -1
  39. package/packages/@monomind/cli/dist/src/orgrt/test-loop.js +8 -8
  40. package/packages/@monomind/cli/dist/src/orgrt/types.d.ts +52 -52
  41. package/packages/@monomind/cli/dist/src/output.d.ts +29 -29
  42. package/packages/@monomind/cli/dist/src/output.js +10 -2
  43. package/packages/@monomind/cli/dist/src/types.d.ts +2 -2
  44. package/packages/@monomind/cli/dist/src/ui/dashboard.html +203 -9
  45. package/packages/@monomind/cli/dist/src/ui/orgs-files.js +192 -0
  46. package/packages/@monomind/cli/dist/src/ui/orgs.html +21 -52
  47. package/packages/@monomind/cli/dist/src/ui/server.mjs +42 -6
  48. package/packages/@monomind/cli/package.json +12 -16
  49. package/packages/@monomind/cli/dist/src/orgrt/live.html +0 -56
package/README.md CHANGED
@@ -5,8 +5,8 @@
5
5
  <h1 align="center">Monomind</h1>
6
6
 
7
7
  <p align="center">
8
- <strong>Hire an AI team. Set a goal. Walk away.</strong><br/>
9
- Autonomous Claude Code orchestration with persistent memory, self-coordinating agent orgs, and a codebase knowledge graph.
8
+ <strong>An open-source MCP server that extends Claude Code with a codebase knowledge graph, persistent memory, and multi-agent coordination.</strong><br/>
9
+ MIT licensed &middot; Fully local &middot; No data leaves your machine
10
10
  </p>
11
11
 
12
12
  <p align="center">
@@ -30,126 +30,105 @@
30
30
 
31
31
  ## What is Monomind?
32
32
 
33
- Claude Code is already powerful. Monomind makes it **run itself**.
33
+ Monomind is an **open-source CLI and MCP server** that plugs into Claude Code via the standard [Model Context Protocol](https://modelcontextprotocol.io/). It adds capabilities that Claude Code doesn't ship with out of the box:
34
34
 
35
- Install once. Wire it into Claude Code. Then instead of prompting Claude to do individual tasks, you tell Monomind what outcome you want and it assembles a team, coordinates the work, and delivers.
35
+ - **Codebase knowledge graph** tree-sitter parses your code into a SQLite-backed graph of files, functions, classes, and their relationships. Query imports, callers, and blast radius before making changes.
36
+ - **Persistent memory** — a JSON pattern store with episodic recall that survives across sessions. Agents and orgs share context without re-prompting.
37
+ - **Multi-agent coordination** — in-session, spawn ad-hoc agent teams via Claude Code's Task tool; for persistent background work, `monomind org run` starts a real SDK-backed daemon with policy-gated role agents and a live dashboard.
38
+ - **Reusable slash commands** — 30+ development workflows (build, review, debug, TDD, architecture) available as `/mastermind:*` commands inside Claude Code.
36
39
 
37
40
  ```bash
38
- # Assemble an AI content team and let it run
39
- /mastermind:createorg content-team "publish 3 SEO-optimized posts per week"
40
- /mastermind:runorg --org content-team
41
-
42
- # Or run the autonomous code improvement loop
43
- /mastermind:autodev --tillend --focus security
41
+ npm install -g monomind # MIT licensed, 0 external API calls
42
+ cd your-project && monomind init
43
+ claude mcp add monomind npx monomind mcp start
44
44
  ```
45
45
 
46
- That's it. Come back later.
46
+ ### Trust & Security
47
+
48
+ | Concern | Answer |
49
+ |---|---|
50
+ | **License** | [MIT](LICENSE) — use it however you want |
51
+ | **Data privacy** | Everything runs locally. No telemetry, no phone-home, no external API calls from Monomind itself. Your code stays on your machine. |
52
+ | **Dependencies** | Standard npm packages (tree-sitter, sql.js, zod). No native binaries. No post-install scripts that download code. |
53
+ | **Permissions** | Registers as an MCP server — Claude Code controls what tools are available and prompts you before executing anything sensitive. |
54
+ | **Source** | Fully open. Read every line at [github.com/monoes/monomind](https://github.com/monoes/monomind). |
55
+ | **Maintenance** | Active development, regular releases on npm. |
47
56
 
48
57
  ---
49
58
 
50
59
  ## 🏢 Autonomous Organizations
51
60
 
52
- > **This is the headline feature.** Build a persistent AI organization — roles, hierarchy, shared task board and start it as a background daemon that runs without you.
61
+ > **This is the headline feature.** `monomind org run` starts a persistent, SDK-backed daemon that runs an autonomous agent organization — roles, hierarchy, policy-gated tool access, a live dashboard until you stop it.
53
62
 
54
63
  ### The idea
55
64
 
56
- Every business function needs a team. Monomind lets you design that team in one command, then run it forever. The org persists across sessions. It checkpoints, recovers from failures, and coordinates its agents through a shared task board all automatically.
65
+ Every business function needs a team. Define the org once as a JSON file — goal, roles, who reports to whom, per-role tool/file/budget policy — then run it as a real background daemon backed by the Claude Agent SDK. It persists across sessions, serves a live WebSocket dashboard, and can discover and message other Monomind orgs running on the same machine.
57
66
 
58
67
  ```mermaid
59
68
  flowchart TD
60
69
  U(["You"])
61
- CO["/mastermind:createorg\nDefine goal + roles"]
62
- RO["/mastermind:runorg\nStart daemon"]
63
- BOSS["Boss Agent\ncoordinator"]
64
- W["Writer\nContent Creator"]
70
+ DEF["org.json\nGoal + roles + policy"]
71
+ RUN["monomind org run\nStart SDK daemon"]
72
+ BOSS["Boss Agent\nAgent SDK session"]
73
+ W["Writer"]
65
74
  S["SEO Specialist"]
66
75
  R["Reviewer"]
67
- M["Growth Marketer"]
68
- BOARD[("Shared\nTask Board")]
69
- MEM[("Persistent\nMemory")]
76
+ DASH[("Live Dashboard\n:4243")]
77
+ XORG[("Other orgs\ncross-process")]
70
78
 
71
- U --> CO --> RO --> BOSS
79
+ U --> DEF --> RUN --> BOSS
72
80
  BOSS -->|spawns| W
73
81
  BOSS -->|spawns| S
74
82
  BOSS -->|spawns| R
75
- BOSS -->|spawns| M
76
- BOSS <-->|claims + reports| BOARD
77
- W <-->|stores output| MEM
78
- S <-->|reads context| MEM
83
+ RUN <-->|serves| DASH
84
+ RUN <-.->|--cross-process| XORG
79
85
 
80
86
  style BOSS fill:#00D2AA22,stroke:#00D2AA
81
- style BOARD fill:#F59E0B22,stroke:#F59E0B
82
- style MEM fill:#8B5CF622,stroke:#8B5CF6
83
- ```
84
-
85
- ### Two commands to a running org
86
-
87
- **Step 1 — Design it:**
88
-
87
+ style DASH fill:#F59E0B22,stroke:#F59E0B
88
+ style XORG fill:#8B5CF622,stroke:#8B5CF6
89
89
  ```
90
- / mastermind:createorg content-team
91
- "Build and publish 3 blog posts per week on AI dev tools"
92
-
93
- Deriving roles from goal...
94
-
95
- ╔ ORG: content-team
96
- ║ TOPOLOGY: star (5 roles)
97
90
 
98
- boss → coordinator
99
- writer → Content Creator
100
- reviewer → reviewer
101
- marketer → Growth Hacker
102
- seo → SEO Specialist
91
+ ### Run one
103
92
 
104
- Type "go" to save, or describe changes.
105
- go
93
+ ```bash
94
+ # .monomind/orgs/<name>.json defines the org: goal, roles, policy.
95
+ # See .monomind/orgs/sample-team.json in a fresh `monomind init` for a working example.
106
96
 
107
- Saved .monomind/orgs/content-team.json
108
- → Run: /mastermind:runorg --org content-team
109
- ```
97
+ monomind org run content-team --task "Build and publish 3 blog posts per week"
110
98
 
111
- **Step 2 Start it:**
99
+ # Live dashboard: http://localhost:4243
100
+ # ✓ Boss agent (Claude Agent SDK session) spawns, reads the org goal,
101
+ # assigns work to role agents, coordinates until the task completes
102
+ # or you stop it.
112
103
 
113
- ```bash
114
- /mastermind:runorg --org content-team
115
-
116
- # Boss agent spawns in background
117
- # Coordinates all roles via shared task board
118
- # Checkpoints every 30 minutes
119
- # Loops until you stop it
104
+ monomind org status content-team # runtime state
105
+ monomind org stop content-team # request a graceful stop
106
+ monomind org list # every org + status
120
107
  ```
121
108
 
122
109
  ### What runs under the hood
123
110
 
124
111
  | What | How |
125
112
  |---|---|
126
- | **Boss agent** | Coordinator type, no supervisor owns the goal |
127
- | **Role agents** | Spawned on demand, specialized by task type |
128
- | **Task board** | Todo Doing Done, shared across all agents |
129
- | **Memory** | All output stored in an org-scoped memory namespace |
130
- | **Checkpoint** | State saved every 30 min survives crashes and restarts |
131
- | **Governance** | `auto` (free), `board` (approve sensitive), `strict` (approve all external actions) |
132
-
133
- ### Topology is auto-derived
134
-
135
- ```mermaid
136
- graph LR
137
- A["1-3 roles"] -->|mesh| B["All-to-all\ndirect comms"]
138
- C["4-6 roles"] -->|star| D["Boss to workers\nfan-out"]
139
- E["7+ roles"] -->|hierarchical| F["Boss to leads to workers\nmiddle management"]
140
- ```
113
+ | **OrgDaemon** | Hosts one or more orgs in a single process; real Claude Agent SDK sessions per role, not simulated |
114
+ | **PolicyEngine** | Per-role gates on tool access, file read/write scope, web access, token budget — enforced, with a full audit trail |
115
+ | **Live dashboard** | WebSocket-served at `:4243` by default (`--port` to change, `--serve=false` to disable) |
116
+ | **Cross-process comms** | `--cross-process` (default on) lets orgs on different `monomind` processes/projects discover and message each other |
117
+ | **Scheduling** | `monomind org serve` hosts orgs whose definition has a `schedule` field, running them on interval |
141
118
 
142
119
  ### Org management commands
143
120
 
144
121
  ```bash
145
- /mastermind:createorg <name> "<goal>" # design org from a goal
146
- /mastermind:runorg --org <name> # start as background daemon
147
- /mastermind:orgs # list all orgs + status
148
- /mastermind:orgstatus --org <name> # detailed status for one org
149
- /mastermind:stoporg --org <name> # stop a running org
150
- /mastermind:approve # review pending approval requests
122
+ monomind org run <name> [--task "..."] [--port N] [--cross-process] # start a daemon
123
+ monomind org stop <name> # request a running org to stop
124
+ monomind org status [name] # runtime state for one or all orgs
125
+ monomind org list # list every org + status
126
+ monomind org serve [--port N] # host-only mode, runs scheduled orgs
127
+ monomind org delete <name> # remove an org
151
128
  ```
152
129
 
130
+ > **Note:** the older `/mastermind:createorg` + `/mastermind:runorg` prompt-orchestrated flow is deprecated — it has no delivery guarantees or ground-truth event stream. It still runs for orgs not yet migrated, but new orgs should use `monomind org run` directly against a hand-authored `.monomind/orgs/<name>.json`.
131
+
153
132
  ---
154
133
 
155
134
  ## ⚡ The Autonomous Build Loop
@@ -206,11 +185,11 @@ claude mcp add monomind npx monomind mcp start
206
185
  monomind doctor --fix
207
186
  ```
208
187
 
209
- Open Claude Code. You now have 80+ slash commands available:
188
+ Open Claude Code. You now have 60+ slash commands available:
210
189
 
211
190
  ```bash
212
191
  /mastermind:autodev --tillend # start autonomous code loop
213
- /mastermind:createorg my-team # create your first AI org
192
+ monomind org run my-team # run your first AI org (see .monomind/orgs/sample-team.json)
214
193
  /mastermind:help # show all commands
215
194
  ```
216
195
 
@@ -262,7 +241,7 @@ Before touching any file, Monomind queries **Monograph** — a SQLite-backed kno
262
241
 
263
242
  ## 🎣 Hooks & Workers
264
243
 
265
- Monomind wires 22 hook events into Claude Code. Every edit, task, command, and session fires hooks that log patterns, route agents, and train the intelligence system.
244
+ Monomind wires 29 hook subcommands into Claude Code across edit, task, command, and session lifecycle events logging patterns, routing agents, and feeding the intelligence system.
266
245
 
267
246
  ```mermaid
268
247
  flowchart LR
@@ -296,7 +275,7 @@ const result = await fence.detect(userInput);
296
275
 
297
276
  ---
298
277
 
299
- ## 📋 80+ Slash Commands
278
+ ## 📋 60+ Slash Commands
300
279
 
301
280
  Everything runs from inside Claude Code via slash commands. Here's the highlight reel:
302
281
 
@@ -315,9 +294,9 @@ Everything runs from inside Claude Code via slash commands. Here's the highlight
315
294
  ### Organizations
316
295
  | Command | What it does |
317
296
  |---|---|
318
- | `/mastermind:createorg` | Design an autonomous agent org |
319
- | `/mastermind:runorg` | Start it as a background daemon |
320
- | `/mastermind:orgs` | List all orgs + status |
297
+ | `monomind org run <name>` | Start an org as a real SDK-backed daemon |
298
+ | `monomind org status` / `list` | Runtime state for one or all orgs |
299
+ | `monomind org stop <name>` | Request a graceful stop |
321
300
  | `/mastermind:approve` | Action pending approval requests |
322
301
 
323
302
  ### Business Domains
@@ -329,7 +308,7 @@ Everything runs from inside Claude Code via slash commands. Here's the highlight
329
308
  | `/mastermind:finance` | Budgets, invoicing, modeling |
330
309
  | `/mastermind:ops` | Operations and workflow automation |
331
310
 
332
- **[→ Full reference (80+ commands)](https://monoes.github.io/monomind/#slash)**
311
+ **[→ Full reference (60+ commands)](https://monoes.github.io/monomind/#slash)**
333
312
 
334
313
  ---
335
314
 
@@ -351,24 +330,27 @@ graph TD
351
330
  CC["Claude Code"]
352
331
  MCP["MCP Server\nmonomind mcp start"]
353
332
  D["Background Workers\n(@monomind/hooks, in-process)"]
333
+ ORG["OrgDaemon\nmonomind org run\nreal SDK sessions"]
354
334
 
355
- CC <-->|"MCP tools: monograph, memory, swarm"| MCP
335
+ CC <-->|"MCP tools: monograph, memory"| MCP
356
336
  MCP <--> D
357
337
 
358
338
  D --> ADB[("Memory store\npatterns + episodes")]
359
339
  D --> MG[("Monograph\ncode graph")]
360
- D --> HK["Hooks\n22 event types"]
361
- D --> SW["Swarm\n4 topologies\n3 consensus strategies"]
340
+ D --> HK["Hooks\n29 subcommands"]
362
341
 
363
- CC -->|"Task tool - spawns agents"| AG["Agent Swarm\narchitect, coder\ntester, reviewer\nsecurity, perf"]
342
+ CC -->|"Task tool - spawns agents"| AG["In-session agents\narchitect, coder\ntester, reviewer\nsecurity, perf"]
364
343
  AG <-->|reads and writes| ADB
344
+ ORG -->|"spawns, policy-gated"| RA["Role agents"]
345
+ ORG <-->|reads and writes| ADB
365
346
 
366
347
  style CC fill:#00D2AA22,stroke:#00D2AA
367
348
  style AG fill:#8B5CF622,stroke:#8B5CF6
368
349
  style ADB fill:#F59E0B22,stroke:#F59E0B
350
+ style ORG fill:#F59E0B22,stroke:#F59E0B
369
351
  ```
370
352
 
371
- **Claude Code handles all execution.** MCP tools only coordinate. Your data never leaves your machine.
353
+ **Claude Code's Task tool drives in-session multi-agent work; `monomind org run` drives persistent background orgs.** Your data never leaves your machine.
372
354
 
373
355
  ---
374
356
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "monomind",
3
- "version": "2.1.0",
4
- "description": "Monomind - Power toolkit for Claude Code. Autonomous agent orgs, codebase knowledge graph, keyword-routed specialist prompts, and 80+ slash commands. One init, then walk away.",
3
+ "version": "2.1.3",
4
+ "description": "Open-source CLI extension for Claude Code. Adds an MCP server with a codebase knowledge graph, persistent memory, multi-agent coordination, and reusable slash commands. MIT licensed, runs locally, no data leaves your machine.",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
7
  "bin": {
@@ -140,7 +140,7 @@
140
140
  "devDependencies": {
141
141
  "@types/node": "^20.0.0",
142
142
  "tsx": "^4.21.0",
143
- "typescript": "^5.0.0",
143
+ "typescript": "^7.0.2",
144
144
  "vite": ">=8.0.16",
145
145
  "vitest": "^4.1.4"
146
146
  },
@@ -41,6 +41,18 @@ function subagentKey(hookInput) {
41
41
  return crypto.createHash('md5').update(String(raw)).digest('hex').slice(0, 16);
42
42
  }
43
43
 
44
+ // Determine subagent success from its result summary — errors/failures/
45
+ // exceptions indicate failure. Shared by the Monograph `success` column and
46
+ // the intelligence.feedback() call below, so both use one definition.
47
+ function deriveSubagentSuccess(summary) {
48
+ if (!summary) return true;
49
+ var sumLower = summary.toLowerCase();
50
+ if (sumLower.includes('error') || sumLower.includes('failed') || sumLower.includes('exception') || sumLower.includes('fatal')) {
51
+ return false;
52
+ }
53
+ return true;
54
+ }
55
+
44
56
  // Delete snap-*.json files older than SNAP_MAX_AGE_MS — orphaned when a
45
57
  // SubagentStop never arrived (crash, forced-stop) or was matched to a
46
58
  // different snapshot before keyed matching existed.
@@ -345,6 +357,17 @@ async function handleSubagentStop(hookInput) {
345
357
  + (session ? ' · sess=' + session : '')
346
358
  + (org ? ' · ' + org + '/' + runId : ''));
347
359
 
360
+ // Feed the real per-subagent success/failure signal into the same
361
+ // intelligence-outcomes.jsonl that session-handler.cjs's SessionEnd
362
+ // heuristic reads (via outcomesSignal) to veto a commit-based "success".
363
+ // Without this, that veto path never fires — SubagentStop is the only
364
+ // remaining event that carries a genuine failure signal, since post-task
365
+ // (TeammateIdle/TaskCompleted) is dead code (those aren't valid Claude
366
+ // Code hook events and are stripped from settings.json on init).
367
+ try {
368
+ require('../intelligence.cjs').feedback(deriveSubagentSuccess(summary));
369
+ } catch (e) { /* non-fatal — feedback recording must never block subagent-stop */ }
370
+
348
371
  if (!org && !session) {
349
372
  // No active org or session — log to general capture file only
350
373
  // P2-26: rotate — was a pure appendFileSync with no cap, unlike
@@ -486,16 +509,7 @@ async function handleSubagentStop(hookInput) {
486
509
  tokens_in: totalTin || 0,
487
510
  tokens_out: totalTout || 0,
488
511
  cost_usd: costUsd || 0,
489
- success: (function() {
490
- // Determine success from result summary — errors/failures/exceptions indicate failure
491
- if (summary) {
492
- var sumLower = summary.toLowerCase();
493
- if (sumLower.includes('error') || sumLower.includes('failed') || sumLower.includes('exception') || sumLower.includes('fatal')) {
494
- return 0;
495
- }
496
- }
497
- return 1;
498
- })(),
512
+ success: deriveSubagentSuccess(summary) ? 1 : 0,
499
513
  duration_ms: snap.ts ? (Date.now() - snap.ts) : 0,
500
514
  timestamp: Date.now(),
501
515
  });
@@ -6,7 +6,7 @@
6
6
  * @monomind/guidance package it mirrored has been removed.
7
7
  *
8
8
  * Gates enforced at runtime:
9
- * pre-bash → destructive-ops (require-confirmation block)
9
+ * pre-bash → destructive-ops (hard block; no confirm-and-proceed path exists)
10
10
  * pre-write → secrets (block)
11
11
  */
12
12
 
@@ -60,7 +60,7 @@ function checkDestructive(command) {
60
60
  return {
61
61
  triggered: true,
62
62
  matched: match[0],
63
- reason: `Destructive operation detected: "${match[0]}". Confirm this is intentional and document a rollback plan before proceeding.`,
63
+ reason: `Destructive operation blocked: "${match[0]}". This hook always blocks (Claude Code's PreToolUse protocol has no confirm-and-proceed path) — there is no way to run this exact command after confirming. If it's genuinely intended, use a non-destructive equivalent instead (e.g. move the target aside, or scope the operation more narrowly).`,
64
64
  };
65
65
  }
66
66
  }
@@ -299,6 +299,35 @@ module.exports = {
299
299
  console.log('[CODEBASE] Graph index ' + mapData.graphStaleness.commitsBehind + ' commits behind HEAD — run monograph build');
300
300
  }
301
301
  }
302
+ // Graph gate connectivity nudge — the pre-search/pre-bash gate
303
+ // (utils/monograph.cjs _graphGateShouldBlock) hard-blocks the first
304
+ // Grep/Glob/bash-grep-or-find call each session until a real
305
+ // monograph_query/monograph_suggest call fires. If that block was
306
+ // never followed by a real graph call, the monomind MCP server is
307
+ // most likely not connected this session (config present but
308
+ // unapproved/not started) — surface it once so the user can fix the
309
+ // actual cause instead of the gate silently degrading to a no-op.
310
+ var graphGateFile = path.join(CWD, '.monomind', 'graph-gate-state.json');
311
+ var mcpWarnFile = path.join(CWD, '.monomind', 'mcp-not-connected-warned.json');
312
+ if (fs.existsSync(graphGateFile) && fs.statSync(graphGateFile).size < 4096) {
313
+ var gateState = JSON.parse(fs.readFileSync(graphGateFile, 'utf-8'));
314
+ var gateSessId = String((hCtx.hookInput && (hCtx.hookInput.sessionId || hCtx.hookInput.session_id)) || '');
315
+ if (gateState && gateState.sessionId === gateSessId && gateState.blockedOnce && !gateState.queried) {
316
+ var alreadyWarnedMcp = false;
317
+ if (fs.existsSync(mcpWarnFile)) {
318
+ try {
319
+ var mcpWarnData = JSON.parse(fs.readFileSync(mcpWarnFile, 'utf-8'));
320
+ if (mcpWarnData && mcpWarnData.sessionId === gateSessId) alreadyWarnedMcp = true;
321
+ } catch (e) { /* corrupt — warn again to be safe */ }
322
+ }
323
+ if (!alreadyWarnedMcp) {
324
+ console.log('[MCP] The graph gate blocked a search but no monograph_query/monograph_suggest call followed — the monomind MCP server is likely not connected this session. Run `claude mcp add monomind -- npx monomind@latest mcp start` (then restart), or approve the .mcp.json trust prompt if one is pending.');
325
+ try {
326
+ fs.writeFileSync(mcpWarnFile, JSON.stringify({ sessionId: gateSessId, warnedAt: new Date().toISOString() }));
327
+ } catch (e) { /* non-fatal */ }
328
+ }
329
+ }
330
+ }
302
331
  // Deep dive findings (god nodes, high-degree files from background analysis)
303
332
  var deepdiveFile = path.join(metricsDir, 'deepdive.json');
304
333
  if (fs.existsSync(deepdiveFile) && fs.statSync(deepdiveFile).size < 32768) {
@@ -41,34 +41,46 @@ module.exports = {
41
41
  if (fs.existsSync(lastRoutePath) && (function() { try { return fs.statSync(lastRoutePath).size <= MAX_ROUTE; } catch(_) { return false; } }())) {
42
42
  var lastRoute = JSON.parse(fs.readFileSync(lastRoutePath, 'utf-8'));
43
43
 
44
- // Derive sessionSuccess from git commits (strongest signal: user committed = success)
44
+ // Derive sessionSuccess. A commit alone is not proof of success — a
45
+ // session can commit a partial fix and then hit repeated failures —
46
+ // so recent intelligence-outcomes are checked FIRST and can veto a
47
+ // commit-based "true" when they show a majority of failures.
45
48
  try {
46
49
  var execSync = require('child_process').execSync;
47
- var recentCommits = execSync('git log --oneline --since="30 minutes ago" 2>/dev/null || true', { cwd: CWD, timeout: 3000, encoding: 'utf-8' }).trim();
48
- if (recentCommits.length > 0) {
49
- sessionSuccess = true;
50
+
51
+ var outcomesSignal = null; // majority success/failure from intelligence-outcomes, if any
52
+ var outcomesPath = path.join(CWD, '.monomind', 'data', 'intelligence-outcomes.jsonl');
53
+ var MAX_OUTCOMES = 512 * 1024;
54
+ if (fs.existsSync(outcomesPath) && (function() { try { return fs.statSync(outcomesPath).size <= MAX_OUTCOMES; } catch(_) { return false; } }())) {
55
+ var windowMs = 30 * 60 * 1000;
56
+ var cutoff = Date.now() - windowMs;
57
+ var outcomeLines = fs.readFileSync(outcomesPath, 'utf-8').trim().split('\n').filter(Boolean);
58
+ var recent = outcomeLines.map(function(l) {
59
+ try { return JSON.parse(l); } catch { return null; }
60
+ }).filter(function(e) { return e && e.ts && e.ts >= cutoff; });
61
+ if (recent.length > 0) {
62
+ var failures = recent.filter(function(e) { return e.success === false; }).length;
63
+ outcomesSignal = failures / recent.length < 0.5;
64
+ }
65
+ }
66
+
67
+ if (outcomesSignal === false) {
68
+ // Recent tool/command outcomes were majority failures — this
69
+ // overrides a commit, since committing doesn't undo those failures.
70
+ sessionSuccess = false;
50
71
  } else {
51
- // No commits check if files were modified (work in progress, not failure)
52
- var gitStatus = execSync('git diff --name-only 2>/dev/null || true', { cwd: CWD, timeout: 3000, encoding: 'utf-8' }).trim();
53
- // Modified files = probably still working; no changes at all = exploration or failure
54
- if (gitStatus.length === 0) {
55
- // Fall back to intelligence-outcomes as a weak signal
56
- var outcomesPath = path.join(CWD, '.monomind', 'data', 'intelligence-outcomes.jsonl');
57
- var MAX_OUTCOMES = 512 * 1024;
58
- if (fs.existsSync(outcomesPath) && (function() { try { return fs.statSync(outcomesPath).size <= MAX_OUTCOMES; } catch(_) { return false; } }())) {
59
- var windowMs = 30 * 60 * 1000;
60
- var cutoff = Date.now() - windowMs;
61
- var outcomeLines = fs.readFileSync(outcomesPath, 'utf-8').trim().split('\n').filter(Boolean);
62
- var recent = outcomeLines.map(function(l) {
63
- try { return JSON.parse(l); } catch { return null; }
64
- }).filter(function(e) { return e && e.ts && e.ts >= cutoff; });
65
- if (recent.length > 0) {
66
- var failures = recent.filter(function(e) { return e.success === false; }).length;
67
- sessionSuccess = failures / recent.length < 0.5;
68
- }
72
+ var recentCommits = execSync('git log --oneline --since="30 minutes ago" 2>/dev/null || true', { cwd: CWD, timeout: 3000, encoding: 'utf-8' }).trim();
73
+ if (recentCommits.length > 0) {
74
+ sessionSuccess = true;
75
+ } else {
76
+ // No commits check if files were modified (work in progress, not failure)
77
+ var gitStatus = execSync('git diff --name-only 2>/dev/null || true', { cwd: CWD, timeout: 3000, encoding: 'utf-8' }).trim();
78
+ // Modified files = probably still working; no changes at all = exploration or failure
79
+ if (gitStatus.length === 0 && outcomesSignal !== null) {
80
+ sessionSuccess = outcomesSignal;
69
81
  }
82
+ // else: modified files exist, or no evidence either way (leave null)
70
83
  }
71
- // else: modified files exist — work in progress, not evidence either way (leave null)
72
84
  }
73
85
  } catch (e) { /* non-critical — leave null (no evidence) */ }
74
86
 
@@ -55,6 +55,7 @@ const {
55
55
  getMonographSuggestions, getMonographNeighbors,
56
56
  _recordGraphTelemetry, _injectCompactGraphMap,
57
57
  _findAffectedTests, _maybeRebuildMonograph,
58
+ _graphGateShouldBlock, _graphGateMarkQueried,
58
59
  } = monograph;
59
60
 
60
61
  const {
@@ -409,6 +410,15 @@ const handlers = {
409
410
  var isGrep = /\b(?:grep|rg|ag)\b/.test(cmd);
410
411
  var isFind = /\b(?:find|fd)\b/.test(cmd) && !isGrep;
411
412
  if (isGrep || isFind) {
413
+ var sessIdGate = String((hCtx.hookInput && (hCtx.hookInput.sessionId || hCtx.hookInput.session_id)) || '');
414
+ if (_graphGateShouldBlock(sessIdGate)) {
415
+ process.stderr.write(JSON.stringify({
416
+ decision: 'block',
417
+ reason: '[graph-gate] Call mcp__monomind__monograph_query or monograph_suggest before grep/rg/find for code exploration (CLAUDE.md). This blocks once per session — after your first monograph call (or this one warning), Bash grep/find work normally for the rest of the session.',
418
+ }) + '\n');
419
+ process.exitCode = 2;
420
+ return;
421
+ }
412
422
  var graphAssisted = false;
413
423
  if (_isGraphFresh()) {
414
424
  try {
@@ -659,6 +669,15 @@ const handlers = {
659
669
 
660
670
  'pre-search': () => {
661
671
  var tool = hCtx.toolName || '';
672
+ var sessIdGate = String((hCtx.hookInput && (hCtx.hookInput.sessionId || hCtx.hookInput.session_id)) || '');
673
+ if (_graphGateShouldBlock(sessIdGate)) {
674
+ process.stderr.write(JSON.stringify({
675
+ decision: 'block',
676
+ reason: '[graph-gate] Call mcp__monomind__monograph_query or monograph_suggest before ' + (tool || 'Grep/Glob') + ' for code exploration (CLAUDE.md). This blocks once per session — after your first monograph call (or this one warning), search tools work normally for the rest of the session.',
677
+ }) + '\n');
678
+ process.exitCode = 2;
679
+ return;
680
+ }
662
681
  var graphResolved = false;
663
682
  try {
664
683
  var grepPattern = (typeof toolInput === 'object' && toolInput !== null)
@@ -817,6 +836,8 @@ const handlers = {
817
836
  'post-graph-tool': () => {
818
837
  // Record monograph MCP tool calls as graph wins
819
838
  _recordGraphTelemetry('monograph_call');
839
+ var sessIdGate = String((hCtx.hookInput && (hCtx.hookInput.sessionId || hCtx.hookInput.session_id)) || '');
840
+ _graphGateMarkQueried(sessIdGate);
820
841
  },
821
842
 
822
843
  'graph-status': () => {
@@ -9,6 +9,25 @@ const { _getRecentEdits } = require('./telemetry.cjs');
9
9
 
10
10
  const CWD = process.env.CLAUDE_PROJECT_DIR || process.cwd();
11
11
 
12
+ // @monoes/monograph is "type":"module" with an exports map that has no
13
+ // "require" condition — a bare `require('@monoes/monograph')` (or
14
+ // require() of its package directory) always throws "No exports main
15
+ // defined", regardless of whether the package is actually installed.
16
+ // require()-ing the package's *resolved entry file* directly bypasses the
17
+ // exports-map restriction and works via Node's require(esm) support (this
18
+ // codebase targets Node 20+, which has it). Reads the entry path from the
19
+ // package's own package.json instead of hardcoding "dist/src/index.js" so
20
+ // this survives a future monograph dist-layout change.
21
+ function _resolvePkgEntryFile(pkgDir) {
22
+ try {
23
+ var pkg = JSON.parse(fs.readFileSync(path.join(pkgDir, 'package.json'), 'utf-8'));
24
+ var entry = (pkg.exports && pkg.exports['.'] && (pkg.exports['.'].import || pkg.exports['.'].default)) || pkg.main;
25
+ if (!entry) return null;
26
+ var full = path.join(pkgDir, entry);
27
+ return fs.existsSync(full) ? full : null;
28
+ } catch (e) { return null; }
29
+ }
30
+
12
31
  function _requireMonograph() {
13
32
  var candidates = [
14
33
  path.join(CWD, 'node_modules/.pnpm/node_modules/@monoes/monograph'),
@@ -16,9 +35,21 @@ function _requireMonograph() {
16
35
  path.join(CWD, 'node_modules/@monoes/monograph'),
17
36
  ];
18
37
  for (var i = 0; i < candidates.length; i++) {
19
- try { if (fs.existsSync(candidates[i])) return require(candidates[i]); } catch(e) {}
38
+ var entry = fs.existsSync(candidates[i]) ? _resolvePkgEntryFile(candidates[i]) : null;
39
+ if (entry) { try { return require(entry); } catch (e) {} }
40
+ }
41
+ // Ancestor-directory search — the equivalent of bare `require('@monoes/
42
+ // monograph')`'s own node_modules walk, needed since that call form can
43
+ // never succeed against this package's exports map.
44
+ var dir = CWD;
45
+ for (;;) {
46
+ var pkgDir = path.join(dir, 'node_modules', '@monoes', 'monograph');
47
+ var entry = fs.existsSync(pkgDir) ? _resolvePkgEntryFile(pkgDir) : null;
48
+ if (entry) { try { return require(entry); } catch (e) {} break; }
49
+ var parent = path.dirname(dir);
50
+ if (parent === dir) break;
51
+ dir = parent;
20
52
  }
21
- try { return require('@monoes/monograph'); } catch(e) {}
22
53
  return null;
23
54
  }
24
55
 
@@ -257,6 +288,44 @@ function _recordGraphTelemetry(event) {
257
288
  } catch (e) { /* non-fatal */ }
258
289
  }
259
290
 
291
+ // ─── Graph gate ─────────────────────────────────────────────────────────────
292
+ // The pre-search/pre-bash heuristic assist above silently resolves Grep/Bash
293
+ // patterns against the graph and counts that as a "graph win" even when the
294
+ // agent never actually called monograph_query — so the graph-usage % can look
295
+ // healthy while zero real monograph_call events ever fire. This gate forces
296
+ // at least one real monograph_query/monograph_suggest call per session before
297
+ // Grep/Glob/bash grep|find are allowed, by hard-blocking (exitCode 2) the
298
+ // first such call each session. Capped at ONE block per session (never a
299
+ // second) so a subagent with no monograph MCP tool access can't deadlock.
300
+ function _graphGateStateFile() {
301
+ return path.join(CWD, '.monomind', 'graph-gate-state.json');
302
+ }
303
+
304
+ function _graphGateMarkQueried(sessionId) {
305
+ if (!sessionId) return;
306
+ try {
307
+ fs.mkdirSync(path.join(CWD, '.monomind'), { recursive: true });
308
+ fs.writeFileSync(_graphGateStateFile(), JSON.stringify({ sessionId: sessionId, queried: true }));
309
+ } catch (e) { /* non-fatal */ }
310
+ }
311
+
312
+ function _graphGateShouldBlock(sessionId) {
313
+ if (!sessionId || !_isGraphFresh()) return false;
314
+ var f = _graphGateStateFile();
315
+ var d = {};
316
+ try { d = JSON.parse(fs.readFileSync(f, 'utf-8')); } catch (e) {}
317
+ if (typeof d !== 'object' || d === null || d.sessionId !== sessionId) {
318
+ d = { sessionId: sessionId, queried: false, blockedOnce: false };
319
+ }
320
+ if (d.queried || d.blockedOnce) return false;
321
+ d.blockedOnce = true;
322
+ try {
323
+ fs.mkdirSync(path.join(CWD, '.monomind'), { recursive: true });
324
+ fs.writeFileSync(f, JSON.stringify(d));
325
+ } catch (e) { return false; }
326
+ return true;
327
+ }
328
+
260
329
  function _injectCompactGraphMap() {
261
330
  try {
262
331
  var db = _openMonographDb();
@@ -476,5 +545,7 @@ module.exports = {
476
545
  _injectCompactGraphMap,
477
546
  _findAffectedTests,
478
547
  _maybeRebuildMonograph,
548
+ _graphGateShouldBlock,
549
+ _graphGateMarkQueried,
479
550
  injectGodNodesContext,
480
551
  };