context-mcp-server 1.0.7 → 1.1.0

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.
@@ -1,87 +1,90 @@
1
1
  # Context-MCP — Gemini CLI Usage Guide
2
2
 
3
3
  Persistent memory + codebase knowledge graph.
4
- Every conversation starts with `context.resume`. Every codebase question uses `codegraph_query`. Files only read for bugs/logic.
4
+ `context.resume` starts every session. `codegraph_arch` shows module structure. `codegraph_query` finds specific symbols. Files only for bugs/logic.
5
5
 
6
6
  ---
7
7
 
8
8
  ## 1. Start of Every Conversation (MANDATORY)
9
9
 
10
- Call the `context` MCP tool with `action: "resume"`, `project: "<project-name>"` **before anything else**.
10
+ Call `context` MCP tool: `action:"resume"`, `project:"<project>"` before anything else.
11
11
 
12
- Returns:
13
- - `recentEntries` — decisions, bugs, notes from previous conversations
14
- - `activeDiscussions` — ongoing topics (auto-linked if exactly one active)
15
- - `codegraph` — `{ built: true/false, nodes, edges, communities }`
12
+ Returns: `recentEntries`, `activePlans`, `codegraph`, `stats.totalEntries`.
16
13
 
17
- Then:
18
- - `codegraph.built: true` → use `codegraph_query` before reading any files
19
- - `codegraph.built: false` → call `codegraph_build(path)` first, then proceed
14
+ - `codegraph.built: true` → use graph tools before reading files
15
+ - `codegraph.built: false` → call `codegraph_build(path)` first
16
+ - `stats.totalEntries ≥ 20` → write compaction summary FIRST (see Rule 4)
17
+ - `activePlans` non-empty → read them before starting new work
20
18
 
21
19
  ---
22
20
 
23
- ## 2. When to Auto-Save Context
21
+ ## 2. Save Triggers (MANDATORY)
24
22
 
25
- **After graph build or rebuild** every time `codegraph_build` completes:
26
- ```
27
- context.save type: "architecture" title: "ContextGraph built — <project>"
28
- content: "nodes: X | edges: Y | communities: Z"
29
- ```
23
+ Call `context.save` with `type: "note"` after finishing anything worth keeping:
30
24
 
31
- **User explicitly asks** "save this", "remember this", "note that" → save immediately.
25
+ | Trigger | Required fields |
26
+ |---|---|
27
+ | Task / fix / feature complete | title, why, outcome, files[] |
28
+ | Decision made | title, why, outcome |
29
+ | Discovery / constraint / gotcha | title, content |
30
+ | Config / env / deploy info | title, content |
31
+ | Graph build complete | title, content (nodes/edges count) |
32
+ | User says "save this" | title, content |
33
+ | "compact now" / "compress memory" | `type:"compaction"`, full session summary |
32
34
 
33
- **During plan / implementation / discussion / research** save only when genuinely valuable:
35
+ **Do NOT save:** routine reads, search results, explanations of existing code.
36
+
37
+ ---
34
38
 
35
- | What happened | Type |
36
- |--------------|------|
37
- | Approach / library / pattern decided | `decision` |
38
- | Bug found (root cause known) or fixed | `bug` |
39
- | System structure understood | `architecture` |
40
- | Gotcha, constraint, non-obvious behavior | `note` |
41
- | Config / env var / secret key discovered | `config` |
42
- | External API or service integration learned | `note` |
43
- | Performance insight (why something is slow/fast) | `note` |
44
- | How to run tests / test pattern discovered | `note` |
45
- | Deploy / release step discovered | `note` |
46
- | Milestone / feature / task completed | `note` |
39
+ ## 3. Plans (MANDATORY for multi-file work)
47
40
 
48
- Do NOT save: routine reads, search results, temporary debugging dead-ends.
49
- Feature spans sessions → `discussion.save` then `discussion.update`.
50
- Need past info → `search` before asking. Always pass `project`.
41
+ **Create a plan when:** editing 2+ files, multi-step implementation, refactor, multi-file bug fix.
42
+
43
+ 1. Call `plan.save` with name, content, project before starting
44
+ 2. Call `plan.update status:"done"` when complete — deletes the plan
45
+
46
+ Check `activePlans` on resume — don't create duplicates.
51
47
 
52
48
  ---
53
49
 
54
- ## 3. ContextGraph Pipeline
50
+ ## 4. Auto-Summary at ≥ 20 Entries (MANDATORY)
55
51
 
56
- > The knowledge graph is also called **ContextGraph**. The MCP tools use the `codegraph_*` prefix — both names refer to the same thing.
52
+ When `totalEntries 20`, call `context.save` BEFORE the user's task:
57
53
 
58
- ### Step 1 — Build (once, fast, local)
59
54
  ```
60
- codegraph_build(path) → AST graph: functions, classes, imports, edges
55
+ type: "compaction" title: "Session summary <YYYY-MM-DD>"
56
+ content: "<what was built, decided, broke, current state>"
57
+ project: "<project>"
61
58
  ```
62
59
 
63
- ### Step 2 — Query (free, instant)
64
- ```
65
- codegraph_query(path, question) → fetch any details about the codebase
66
- codegraph_explain(path, node) → single node: type, file, connections
67
- codegraph_path(path, from, to) → shortest path
68
- codegraph_nodes(path, type) → list nodes by type
69
- codegraph_report(path) → full graph analysis
70
- ```
60
+ ---
61
+
62
+ ## 5. Search Before Asking
63
+
64
+ Call `search` before asking user to re-explain past work.
71
65
 
72
66
  ---
73
67
 
74
- ## 4. Graph vs File
68
+ ## 6. ContextGraph Tools
69
+
70
+ ```
71
+ codegraph_build(path) → build AST graph (run once)
72
+ codegraph_arch(path, limit?) → module map: files, exports, imports
73
+ codegraph_query(path, question?, node?) → find symbol or answer structural question
74
+ codegraph_nodes(path, type) → list all nodes of a type
75
+ codegraph_report(path) → structural analysis
76
+ ```
75
77
 
76
- **Graph** use for any question about what exists: finding functions, classes, files, dependencies, callers, imports, paths between concepts.
77
- **File** — bugs, logic, tracing unexpected behavior.
78
+ Use `codegraph_arch` first. Never read files for structure questions.
78
79
 
79
80
  ---
80
81
 
81
- ## 5. Rules
82
+ ## 7. Rules
82
83
 
83
- 1. **`context.resume` first** — before any tool or response
84
- 2. **Always pass `project`** — never save to global unless truly cross-project
85
- 3. **`search` before asking**if user references past work, find it first
86
- 4. **`codegraph_query` before reading files**graph is faster and cheaper
87
- 5. **Read files for bugs/logic**graph is structure only, not behavior
84
+ 1. `context.resume` first — before any tool or response
85
+ 2. Always pass `project`
86
+ 3. Save on task complete `why` + `outcome` + `files`
87
+ 4. Compaction at 20 entries before starting task
88
+ 5. Plan before multi-file work`status:"done"` deletes it
89
+ 6. Search before asking about past work
90
+ 7. Graph tools before files
@@ -4,7 +4,7 @@ Both `project` and `rootPath` are required: `project` names the memory bucket, `
4
4
 
5
5
  This loads:
6
6
  - Recent decisions, bugs, and notes from past sessions
7
- - Active discussions
7
+ - Active plans
8
8
  - ContextGraph status (built or not)
9
9
 
10
10
  If `codegraph.built` is false in the response, immediately call `codegraph_build` on the project path before proceeding.
@@ -2,8 +2,11 @@ Call the `context` MCP tool with `action: "save"` to store a note for the curren
2
2
 
3
3
  Parse `$ARGUMENTS` to determine:
4
4
  - `project`: infer from current working directory name if not specified
5
- - `title`: first sentence or phrase from the argument
5
+ - `title`: first sentence or phrase (up to 120 chars — be specific)
6
+ - `why`: reason it mattered — what problem it solved or constraint it revealed
7
+ - `outcome`: result — what changed, what was verified, what shipped, which files were affected
8
+ - `files`: list of files changed (required for task/bug types)
6
9
  - `content`: full argument text
7
- - `type`: auto-detect — if mentions a bug/fix → `"bug"`, decision/chose/decided → `"decision"`, structure/architecture → `"architecture"`, otherwise `"note"`
10
+ - `type`: auto-detect — bug/fix/error → `"bug"`, task/done/complete/shipped/implemented → `"task"`, decision/chose/decided/approach → `"decision"`, config/env/secret/deploy → `"config"`, otherwise `"note"`
8
11
 
9
- Confirm to the user what was saved (title, type, project).
12
+ Confirm to the user: title, type, why, outcome, project.
@@ -13,7 +13,7 @@ Every conversation starts with `context.resume`. Every structural question uses
13
13
 
14
14
  Call `context` tool, `action: "resume"`, `project: "<project-name>"` before anything else.
15
15
 
16
- Returns: `recentEntries`, `activeDiscussions`, `codegraph { built, nodes, edges }`.
16
+ Returns: `recentEntries`, `activePlans`, `codegraph { built, nodes, edges }`.
17
17
 
18
18
  - `codegraph.built: true` → use `codegraph_query` before reading files
19
19
  - `codegraph.built: false` → run `codegraph_build(path)` first
@@ -24,8 +24,8 @@ Returns: `recentEntries`, `activeDiscussions`, `codegraph { built, nodes, edges
24
24
  |-----------|--------|
25
25
  | Decision made | `context.save` type: `"decision"` |
26
26
  | Bug found/fixed | `context.save` type: `"bug"` |
27
- | Architecture understood | `context.save` type: `"architecture"` |
28
- | Multi-session feature | `discussion.create` |
27
+ | Discovery / structure understood | `context.save` type: `"note"` |
28
+ | Making any plan | `plan.save` + `planDir` |
29
29
 
30
30
  ## 3. CodeGraph
31
31
 
@@ -12,7 +12,17 @@ description: >
12
12
  # Context-MCP
13
13
 
14
14
  Persistent memory + codebase knowledge graph across every conversation.
15
- `context.resume` starts every session. `codegraph_query` answers every structure question. Files only for bugs/logic.
15
+ `context.resume` starts every session. `codegraph_arch` shows module structure. `codegraph_query` finds specific symbols. Files only for bugs/logic.
16
+
17
+ ---
18
+
19
+ ## Subcommands
20
+
21
+ When invoked with an argument, act immediately:
22
+
23
+ **`/context-mcp resume`** — call `context` tool with `action:"resume"`, infer `project` from cwd, pass `rootPath`. Report what was returned.
24
+
25
+ **`/context-mcp save`** — call `context` tool with `action:"save"` to save the most recent thing worth keeping from this conversation. Use `type:"note"`, fill in `title`, `why`, `outcome`, `files` from context. Confirm what was saved.
16
26
 
17
27
  ---
18
28
 
@@ -23,92 +33,109 @@ Call `context` tool **before any tool or response** with:
23
33
  - `project: "<basename of git repo root dir>"` — infer from `cwd` if not stated
24
34
  - `rootPath: "<absolute path to git repo root>"` — required for sandbox + graph lookup
25
35
 
26
- Both fields are required: `project` names the memory bucket, `rootPath` enables exact graph matching and file sandboxing.
27
-
28
36
  Returns:
29
- - `recentEntries` — decisions, bugs, notes from past sessions
30
- - `activeDiscussions` — ongoing topics (auto-linked if exactly one active)
37
+ - `recentEntries` — last 15 entries; newest 5 have full content, rest have 200-char preview
38
+ - `activePlans` — in-progress plans; read them before starting any new work
31
39
  - `codegraph` — `{ built: true/false, nodes, edges, communities }`
40
+ - `stats.totalEntries` — if ≥ 20, write a compaction summary before proceeding (see Rule 4)
32
41
 
33
42
  Then:
34
- - `codegraph.built: true` → use `codegraph_query` before reading any files
35
- - `codegraph.built: false` → call `codegraph_build(path)` first, then proceed
43
+ - `codegraph.built: true` → use `codegraph_arch` for structure overview, `codegraph_query` for specific lookups
44
+ - `codegraph.built: false` → call `codegraph_build(path)` first
36
45
 
37
46
  ---
38
47
 
39
- ## When to Auto-Save Context
48
+ ## When to Save Context (MANDATORY TRIGGERS)
40
49
 
41
- ### Always save no user prompt needed
50
+ Call `context.save` with `type: "note"` after finishing anything worth keeping:
42
51
 
43
- **1. After graph build or rebuild**
44
- Every time `codegraph_build` completes successfully, immediately call:
45
52
  ```
46
- context.save project: "<project>" type: "architecture" title: "ContextGraph built — <project>"
47
- content: "nodes: X | edges: Y | communities: Z | built: <timestamp>"
53
+ context.save
54
+ project: "<project>"
55
+ title: "<what was done — up to 120 chars>"
56
+ why: "<why it mattered>"
57
+ outcome: "<what the result was>"
58
+ files: ["src/file.js", ...]
48
59
  ```
49
60
 
50
- **2. When user explicitly asks**
51
- Any phrase like "save this", "remember this", "note that", "log this" → `context.save` immediately with whatever was just discussed.
52
-
53
- **3. During plan / implementation / discussion / research save when something valuable happens**
54
- Only save if the moment is genuinely worth keeping across sessions:
55
- - A decision was made and agreed on (approach, library, pattern, architecture)
56
- - A bug was found with its root cause identified, or fixed
57
- - An important discovery (gotcha, constraint, non-obvious behavior, env requirement)
58
- - A significant milestone reached (feature complete, refactor done, plan finalized)
59
- - Something that would save future-you from re-learning it
60
-
61
- Do NOT save for: routine file reads, search results, explanations of existing code, temporary debugging steps that led nowhere.
62
-
63
- | What happened | Type |
64
- |--------------|------|
65
- | Approach / library / pattern decided | `decision` |
66
- | Bug found (root cause known) or fixed | `bug` |
67
- | System structure understood | `architecture` |
68
- | Gotcha, constraint, non-obvious behavior | `note` |
69
- | Config / env var / secret key discovered | `config` |
70
- | External API or service integration learned | `note` |
71
- | Performance insight (why something is slow/fast) | `note` |
72
- | How to run tests / test pattern discovered | `note` |
73
- | Deploy / release step discovered | `note` |
74
- | Milestone / feature / task completed | `note` |
75
-
76
- Always pass `project`. Feature spans multiple sessions → `discussion.create` or `discussion.update`. Need past info → `search` before asking user. Auto-compact fires at >20 entries.
61
+ **Save immediately after:**
62
+ - Task / fix / feature complete
63
+ - Decision made (architecture, library, approach)
64
+ - Discoverynon-obvious behavior, constraint, gotcha
65
+ - Config / env / deploy info
66
+ - Graph build complete include nodes/edges count in content
67
+ - User says "save this" / "remember this"
68
+
69
+ **Manual compaction** "compact now", "compress memory", "clean up context":
70
+ Save a full session summary as `type: "compaction"`. Server removes old entries using it.
71
+
72
+ **Do NOT save:** routine reads, search results, explanations of existing code, dead-end debugging.
77
73
 
78
74
  ---
79
75
 
80
- ## ContextGraph Pipeline
76
+ ## Plans (MANDATORY for multi-file work)
81
77
 
82
- > Also called **CodeGraph**. MCP tools use the `codegraph_*` prefix both names mean the same thing.
78
+ **Create a plan when:** editing 2+ files, multi-step implementation, refactor, multi-file bug fix.
83
79
 
84
- ### Build (once per project, ~seconds, local)
85
- ```
86
- codegraph_build(path)
87
- ```
88
- Parses codebase into AST graph via tree-sitter. Extracts functions, classes, imports, call edges for 16+ languages. Build files get a single metadata node. Saved to `~/.context-mcp/graphs.json`.
80
+ **Skip plan for:** single-file edits, questions, simple config tweaks.
81
+
82
+ 1. `plan.save` with name, content, project, planDir — before starting work
83
+ 2. Work through plan
84
+ 3. `plan.update status:"done"` when complete deletes the plan
85
+
86
+ On `resume`, check `activePlans` — do not duplicate in-progress work.
87
+
88
+ ---
89
+
90
+ ## Auto-Summary Rule (MANDATORY)
91
+
92
+ When `resume` returns `stats.totalEntries ≥ 20`, call `context.save` **before the user's task**:
89
93
 
90
- ### Query (free, instant, forever)
91
94
  ```
92
- codegraph_query(path, question?, node?) structural question OR single-node lookup (or both in one call)
93
- codegraph_path(path, from, to) → shortest path between two concepts
94
- codegraph_nodes(path, type) → list all nodes of a type
95
- codegraph_report(path) → god nodes, clusters, surprises
95
+ type: "compaction" title: "Session summary <date>"
96
+ content: "<what was built, decided, broke, current state>"
97
+ project: "<project>"
96
98
  ```
97
99
 
98
100
  ---
99
101
 
100
- ## Graph vs File
102
+ ## Search Before Asking
103
+
104
+ If user references past work → `search` first. Never ask user to re-explain saved information.
105
+
106
+ ---
107
+
108
+ ## ContextGraph Pipeline
101
109
 
102
- **Graph** what exists: finding functions, classes, files, dependencies, callers, imports, paths between concepts.
110
+ ### Build (once per project)
111
+ ```
112
+ codegraph_build(path) → AST graph: functions, classes, imports, edges
113
+ ```
114
+
115
+ ### Query tools
116
+ ```
117
+ codegraph_arch(path) → module map: every file, exports, imports
118
+ codegraph_query(path, question?, node?) → find symbol or answer structural question
119
+ codegraph_nodes(path, type) → list all nodes of a type
120
+ codegraph_report(path) → god nodes, clusters, structural analysis
121
+ ```
103
122
 
104
- **File** bugs, logic inside a specific function, tracing unexpected behavior.
123
+ | Question | Tool |
124
+ |---|---|
125
+ | Architecture overview / what files exist | `codegraph_arch` |
126
+ | Where is function/class X defined? | `codegraph_query node:"X"` |
127
+ | What does module Y import? | `codegraph_query question:"..."` |
128
+ | List all classes/functions | `codegraph_nodes type:"class"` |
129
+ | Most connected / central files | `codegraph_report` |
105
130
 
106
131
  ---
107
132
 
108
133
  ## Rules
109
134
 
110
- 1. **`context.resume` first** — before any tool or response
111
- 2. **Always pass `project`** — never save to global unless truly cross-project
112
- 3. **`search` before asking**if user references past work, find it first
113
- 4. **`codegraph_query` before reading files**graph is faster and cheaper
114
- 5. **Read files for bugs/logic only**graph is structure, not behavior
135
+ 1. `context.resume` first — before any tool or response
136
+ 2. Always pass `project`
137
+ 3. Save on task complete `why` + `outcome` + `files`
138
+ 4. Compaction at 20 entries before starting task
139
+ 5. Plan for multi-file work`status:"done"` deletes it
140
+ 6. Search before asking about past work
141
+ 7. Graph tools before files
@@ -1,35 +1,84 @@
1
1
  # Context-MCP — Windsurf Usage Guide
2
2
 
3
- Persistent memory + codebase knowledge graph for Windsurf.
4
- Every conversation starts with `context.resume`. Every structural question uses `codegraph_query`. Files only read for bugs/logic.
3
+ Persistent memory + codebase knowledge graph.
4
+ `context.resume` starts every session. `codegraph_arch` shows module structure. `codegraph_query` finds specific symbols. Files only for bugs/logic.
5
+
6
+ ---
5
7
 
6
8
  ## 1. Start of Every Conversation (MANDATORY)
7
9
 
8
- Call `context` tool, `action: "resume"`, `project: "<project-name>"` before anything else.
10
+ Call `context` tool: `action:"resume"`, `project:"<name>"` before anything else.
9
11
 
10
- Returns: `recentEntries`, `activeDiscussions`, `codegraph { built, nodes, edges }`.
12
+ Returns: `recentEntries`, `activePlans`, `codegraph`, `stats.totalEntries`.
11
13
 
12
- - `codegraph.built: true` → use `codegraph_query` before reading files
13
14
  - `codegraph.built: false` → run `codegraph_build(path)` first
15
+ - `stats.totalEntries ≥ 20` → write compaction summary first (see Rule 4)
16
+ - `activePlans` non-empty → read before starting new work
17
+
18
+ ---
19
+
20
+ ## 2. Save Triggers (MANDATORY)
21
+
22
+ Call `context.save` with `type: "note"` after finishing anything worth keeping:
23
+
24
+ | Trigger | Required fields |
25
+ |---|---|
26
+ | Task / fix / feature complete | title, why, outcome, files[] |
27
+ | Decision made | title, why, outcome |
28
+ | Discovery / constraint / gotcha | title, content |
29
+ | Config / env / deploy info | title, content |
30
+ | Graph build complete | title, content (nodes/edges count) |
31
+ | User says "save this" | title, content |
32
+ | "compact now" / "compress memory" | `type:"compaction"`, full session summary |
33
+
34
+ **Do NOT save:** routine reads, search results, explanations of existing code.
35
+
36
+ ---
37
+
38
+ ## 3. Plans
39
+
40
+ Call `plan.save` before any work touching 2+ files or multiple steps.
41
+ Call `plan.update status:"done"` when complete — deletes the plan.
42
+ Check `activePlans` on resume — don't duplicate.
43
+
44
+ ---
45
+
46
+ ## 4. Auto-Summary at ≥ 20 Entries (MANDATORY)
47
+
48
+ When `totalEntries ≥ 20`, call `context.save` BEFORE the user's task:
49
+
50
+ ```
51
+ type: "compaction" title: "Session summary — <date>"
52
+ content: "<what was built, decided, broke, current state>"
53
+ project: "<project>"
54
+ ```
55
+
56
+ ---
57
+
58
+ ## 5. Search Before Asking
59
+
60
+ Call `search` before asking user to re-explain past work.
61
+
62
+ ---
14
63
 
15
- ## 2. Save Context
64
+ ## 6. ContextGraph Tools
16
65
 
17
- | Situation | Action |
18
- |-----------|--------|
19
- | Decision made | `context.save` type: `"decision"` |
20
- | Bug found/fixed | `context.save` type: `"bug"` |
21
- | Architecture understood | `context.save` type: `"architecture"` |
22
- | Multi-session feature | `discussion.create` |
66
+ ```
67
+ codegraph_arch(path) → module map (files, exports, imports)
68
+ codegraph_query(path, ...) → find specific function/class/file
69
+ codegraph_nodes(path, type) → list all nodes of a type
70
+ codegraph_report(path) → structural analysis
71
+ ```
23
72
 
24
- ## 3. CodeGraph
73
+ Use `codegraph_arch` first. Never read files for structure questions.
25
74
 
26
- Build once: `codegraph_build(path)` — then query forever.
27
- Use `codegraph_query` for structural questions. Read files for bugs/logic.
75
+ ---
28
76
 
29
- ## Rules
77
+ ## 7. Rules
30
78
 
31
79
  1. `context.resume` first — every conversation
32
- 2. Always pass `project`
33
- 3. `search` before asking the user about past work
34
- 4. `codegraph_query` before reading files
35
- 5. Files only for bugs and logic
80
+ 2. Save on task complete — `why` + `outcome` + `files`
81
+ 3. Compaction at 20 entries before starting task
82
+ 4. Plan for 2+ file changes — `status:"done"` deletes it
83
+ 5. Search before asking about past work
84
+ 6. Graph tools before files
@@ -6,6 +6,7 @@
6
6
  import { spawnSync } from 'node:child_process';
7
7
  import { dirname, join } from 'node:path';
8
8
  import { fileURLToPath } from 'node:url';
9
+ import { saveGraph, saveContext, updateContext, getContext, flushToDisk } from '../db.js';
9
10
 
10
11
  const __dirname = dirname(fileURLToPath(import.meta.url));
11
12
  const REPO_ROOT = join(__dirname, '..', '..');
@@ -82,16 +83,18 @@ export const definitions = [
82
83
  },
83
84
  },
84
85
  {
85
- name: 'codegraph_path',
86
- description: 'Find the shortest relationship path between two concepts in the graph.',
86
+ name: 'codegraph_arch',
87
+ description:
88
+ 'Return a module map of the project — every file with its exported functions/classes and its imports. ' +
89
+ 'Use this to understand project structure without reading any files. ' +
90
+ 'Call after codegraph_build. Much faster than reading each file individually.',
87
91
  inputSchema: {
88
92
  type: 'object',
89
93
  properties: {
90
- path: { type: 'string' },
91
- from: { type: 'string' },
92
- to: { type: 'string' },
94
+ path: { type: 'string', description: 'Project root' },
95
+ limit: { type: 'integer', description: 'Max files in output (default 100)' },
93
96
  },
94
- required: ['path', 'from', 'to'],
97
+ required: ['path'],
95
98
  },
96
99
  },
97
100
  ];
@@ -103,46 +106,46 @@ export function handle(name, args, state) {
103
106
 
104
107
  // Persist graph metadata + save/update a context entry as a visible build record
105
108
  if (name === 'codegraph_build' && result.success) {
106
- import('../db.js').then(({ saveGraph, saveContext, updateContext, getContext }) => {
107
- saveGraph({
108
- path: args.path,
109
- nodes: result.nodes,
110
- edges: result.edges,
111
- communities: result.communities,
112
- cached: result.cached,
113
- changed: result.changed,
114
- time_ms: result.time_ms,
115
- summary: result.summary || '',
116
- });
109
+ saveGraph({
110
+ path: args.path,
111
+ nodes: result.nodes,
112
+ edges: result.edges,
113
+ communities: result.communities,
114
+ cached: result.cached,
115
+ changed: result.changed,
116
+ time_ms: result.time_ms,
117
+ summary: result.summary || '',
118
+ });
119
+ flushToDisk(); // write graph.json to disk immediately so ctx list sees it
117
120
 
118
- const inferredProject = args.path
119
- ? args.path.replace(/\\/g, '/').replace(/\/$/, '').split('/').pop()
120
- : null;
121
- const project = state?.sessionProject || inferredProject || null;
122
- const title = `CodeGraph — ${args.path}`;
123
- const content = [
124
- `nodes: ${result.nodes} | edges: ${result.edges} | communities: ${result.communities}`,
125
- `cached: ${result.cached} | changed: ${result.changed} | time: ${result.time_ms}ms`,
126
- result.summary || '',
127
- ].filter(Boolean).join('\n');
121
+ const inferredProject = args.path
122
+ ? args.path.replace(/\\/g, '/').replace(/\/$/, '').split('/').pop()
123
+ : null;
124
+ const project = state?.sessionProject || inferredProject || null;
125
+ const title = `ContextGraph built — ${args.path}`;
126
+ const content = [
127
+ `nodes: ${result.nodes} | edges: ${result.edges} | communities: ${result.communities}`,
128
+ `cached: ${result.cached} | changed: ${result.changed} | time: ${result.time_ms}ms`,
129
+ result.summary || '',
130
+ ].filter(Boolean).join('\n');
128
131
 
129
- const existing = getContext({ project, tags: ['codegraph'], limit: 100 })
130
- .find(e => e.title === title);
132
+ // Search all projects same path always produces same title regardless of session
133
+ const existing = getContext({ tags: ['codegraph'], limit: 100 })
134
+ .find(e => e.title === title);
131
135
 
132
- if (existing) {
133
- updateContext({ id: existing.id, content, status: 'active' });
134
- } else {
135
- saveContext({
136
- project,
137
- sessionId: state?.sessionId || null,
138
- title,
139
- content,
140
- type: 'architecture',
141
- source: 'auto',
142
- tags: ['codegraph', 'graph-build'],
143
- });
144
- }
145
- }).catch(() => {});
136
+ if (existing) {
137
+ updateContext({ id: existing.id, content, status: 'active' });
138
+ } else {
139
+ saveContext({
140
+ project,
141
+ sessionId: state?.sessionId || null,
142
+ title,
143
+ content,
144
+ type: 'note',
145
+ source: 'auto',
146
+ tags: ['codegraph', 'graph-build'],
147
+ });
148
+ }
146
149
  }
147
150
 
148
151
  return result;