gitnexus 1.2.7 → 1.2.8
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/README.md +186 -186
- package/dist/cli/ai-context.js +71 -71
- package/dist/cli/analyze.d.ts +1 -1
- package/dist/cli/analyze.js +59 -15
- package/dist/cli/index.js +1 -1
- package/dist/core/augmentation/engine.js +20 -20
- package/dist/core/embeddings/embedder.js +1 -0
- package/dist/core/embeddings/embedding-pipeline.js +26 -26
- package/dist/core/ingestion/cluster-enricher.js +16 -16
- package/dist/core/ingestion/tree-sitter-queries.js +282 -282
- package/dist/core/kuzu/kuzu-adapter.js +9 -9
- package/dist/core/kuzu/schema.js +256 -256
- package/dist/core/search/bm25-index.js +5 -5
- package/dist/core/search/hybrid-search.js +3 -3
- package/dist/core/wiki/graph-queries.js +52 -52
- package/dist/core/wiki/html-viewer.js +192 -192
- package/dist/core/wiki/prompts.js +82 -82
- package/dist/mcp/local/local-backend.js +112 -112
- package/dist/mcp/resources.js +42 -42
- package/dist/mcp/server.js +16 -16
- package/dist/mcp/tools.js +77 -77
- package/hooks/claude/gitnexus-hook.cjs +135 -135
- package/hooks/claude/pre-tool-use.sh +78 -78
- package/hooks/claude/session-start.sh +42 -42
- package/package.json +82 -82
- package/skills/debugging.md +85 -85
- package/skills/exploring.md +75 -75
- package/skills/impact-analysis.md +94 -94
- package/skills/refactoring.md +113 -113
- package/vendor/leiden/index.cjs +355 -355
- package/vendor/leiden/utils.cjs +392 -392
package/dist/mcp/server.js
CHANGED
|
@@ -185,14 +185,14 @@ export async function startMCPServer(backend) {
|
|
|
185
185
|
role: 'user',
|
|
186
186
|
content: {
|
|
187
187
|
type: 'text',
|
|
188
|
-
text: `Analyze the impact of my current code changes before committing.
|
|
189
|
-
|
|
190
|
-
Follow these steps:
|
|
191
|
-
1. Run \`detect_changes(${JSON.stringify({ scope, ...(baseRef ? { base_ref: baseRef } : {}) })})\` to find what changed and affected processes
|
|
192
|
-
2. For each changed symbol in critical processes, run \`context({name: "<symbol>"})\` to see its full reference graph
|
|
193
|
-
3. For any high-risk items (many callers or cross-process), run \`impact({target: "<symbol>", direction: "upstream"})\` for blast radius
|
|
194
|
-
4. Summarize: changes, affected processes, risk level, and recommended actions
|
|
195
|
-
|
|
188
|
+
text: `Analyze the impact of my current code changes before committing.
|
|
189
|
+
|
|
190
|
+
Follow these steps:
|
|
191
|
+
1. Run \`detect_changes(${JSON.stringify({ scope, ...(baseRef ? { base_ref: baseRef } : {}) })})\` to find what changed and affected processes
|
|
192
|
+
2. For each changed symbol in critical processes, run \`context({name: "<symbol>"})\` to see its full reference graph
|
|
193
|
+
3. For any high-risk items (many callers or cross-process), run \`impact({target: "<symbol>", direction: "upstream"})\` for blast radius
|
|
194
|
+
4. Summarize: changes, affected processes, risk level, and recommended actions
|
|
195
|
+
|
|
196
196
|
Present the analysis as a clear risk report.`,
|
|
197
197
|
},
|
|
198
198
|
},
|
|
@@ -207,14 +207,14 @@ Present the analysis as a clear risk report.`,
|
|
|
207
207
|
role: 'user',
|
|
208
208
|
content: {
|
|
209
209
|
type: 'text',
|
|
210
|
-
text: `Generate architecture documentation for this codebase using the knowledge graph.
|
|
211
|
-
|
|
212
|
-
Follow these steps:
|
|
213
|
-
1. READ \`gitnexus://repo/${repo || '{name}'}/context\` for codebase stats
|
|
214
|
-
2. READ \`gitnexus://repo/${repo || '{name}'}/clusters\` to see all functional areas
|
|
215
|
-
3. READ \`gitnexus://repo/${repo || '{name}'}/processes\` to see all execution flows
|
|
216
|
-
4. For the top 5 most important processes, READ \`gitnexus://repo/${repo || '{name}'}/process/{name}\` for step-by-step traces
|
|
217
|
-
5. Generate a mermaid architecture diagram showing the major areas and their connections
|
|
210
|
+
text: `Generate architecture documentation for this codebase using the knowledge graph.
|
|
211
|
+
|
|
212
|
+
Follow these steps:
|
|
213
|
+
1. READ \`gitnexus://repo/${repo || '{name}'}/context\` for codebase stats
|
|
214
|
+
2. READ \`gitnexus://repo/${repo || '{name}'}/clusters\` to see all functional areas
|
|
215
|
+
3. READ \`gitnexus://repo/${repo || '{name}'}/processes\` to see all execution flows
|
|
216
|
+
4. For the top 5 most important processes, READ \`gitnexus://repo/${repo || '{name}'}/process/{name}\` for step-by-step traces
|
|
217
|
+
5. Generate a mermaid architecture diagram showing the major areas and their connections
|
|
218
218
|
6. Write an ARCHITECTURE.md file with: overview, functional areas, key execution flows, and the mermaid diagram`,
|
|
219
219
|
},
|
|
220
220
|
},
|
package/dist/mcp/tools.js
CHANGED
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
export const GITNEXUS_TOOLS = [
|
|
8
8
|
{
|
|
9
9
|
name: 'list_repos',
|
|
10
|
-
description: `List all indexed repositories available to GitNexus.
|
|
11
|
-
|
|
12
|
-
Returns each repo's name, path, indexed date, last commit, and stats.
|
|
13
|
-
|
|
14
|
-
WHEN TO USE: First step when multiple repos are indexed, or to discover available repos.
|
|
15
|
-
AFTER THIS: READ gitnexus://repo/{name}/context for the repo you want to work with.
|
|
16
|
-
|
|
17
|
-
When multiple repos are indexed, you MUST specify the "repo" parameter
|
|
10
|
+
description: `List all indexed repositories available to GitNexus.
|
|
11
|
+
|
|
12
|
+
Returns each repo's name, path, indexed date, last commit, and stats.
|
|
13
|
+
|
|
14
|
+
WHEN TO USE: First step when multiple repos are indexed, or to discover available repos.
|
|
15
|
+
AFTER THIS: READ gitnexus://repo/{name}/context for the repo you want to work with.
|
|
16
|
+
|
|
17
|
+
When multiple repos are indexed, you MUST specify the "repo" parameter
|
|
18
18
|
on other tools (query, context, impact, etc.) to target the correct one.`,
|
|
19
19
|
inputSchema: {
|
|
20
20
|
type: 'object',
|
|
@@ -24,17 +24,17 @@ on other tools (query, context, impact, etc.) to target the correct one.`,
|
|
|
24
24
|
},
|
|
25
25
|
{
|
|
26
26
|
name: 'query',
|
|
27
|
-
description: `Query the code knowledge graph for execution flows related to a concept.
|
|
28
|
-
Returns processes (call chains) ranked by relevance, each with its symbols and file locations.
|
|
29
|
-
|
|
30
|
-
WHEN TO USE: Understanding how code works together. Use this when you need execution flows and relationships, not just file matches. Complements grep/IDE search.
|
|
31
|
-
AFTER THIS: Use context() on a specific symbol for 360-degree view (callers, callees, categorized refs).
|
|
32
|
-
|
|
33
|
-
Returns results grouped by process (execution flow):
|
|
34
|
-
- processes: ranked execution flows with relevance priority
|
|
35
|
-
- process_symbols: all symbols in those flows with file locations
|
|
36
|
-
- definitions: standalone types/interfaces not in any process
|
|
37
|
-
|
|
27
|
+
description: `Query the code knowledge graph for execution flows related to a concept.
|
|
28
|
+
Returns processes (call chains) ranked by relevance, each with its symbols and file locations.
|
|
29
|
+
|
|
30
|
+
WHEN TO USE: Understanding how code works together. Use this when you need execution flows and relationships, not just file matches. Complements grep/IDE search.
|
|
31
|
+
AFTER THIS: Use context() on a specific symbol for 360-degree view (callers, callees, categorized refs).
|
|
32
|
+
|
|
33
|
+
Returns results grouped by process (execution flow):
|
|
34
|
+
- processes: ranked execution flows with relevance priority
|
|
35
|
+
- process_symbols: all symbols in those flows with file locations
|
|
36
|
+
- definitions: standalone types/interfaces not in any process
|
|
37
|
+
|
|
38
38
|
Hybrid ranking: BM25 keyword + semantic vector search, ranked by Reciprocal Rank Fusion.`,
|
|
39
39
|
inputSchema: {
|
|
40
40
|
type: 'object',
|
|
@@ -52,32 +52,32 @@ Hybrid ranking: BM25 keyword + semantic vector search, ranked by Reciprocal Rank
|
|
|
52
52
|
},
|
|
53
53
|
{
|
|
54
54
|
name: 'cypher',
|
|
55
|
-
description: `Execute Cypher query against the code knowledge graph.
|
|
56
|
-
|
|
57
|
-
WHEN TO USE: Complex structural queries that search/explore can't answer. READ gitnexus://repo/{name}/schema first for the full schema.
|
|
58
|
-
AFTER THIS: Use context() on result symbols for deeper context.
|
|
59
|
-
|
|
60
|
-
SCHEMA:
|
|
61
|
-
- Nodes: File, Folder, Function, Class, Interface, Method, CodeElement, Community, Process
|
|
62
|
-
- Multi-language nodes (use backticks): \`Struct\`, \`Enum\`, \`Trait\`, \`Impl\`, etc.
|
|
63
|
-
- All edges via single CodeRelation table with 'type' property
|
|
64
|
-
- Edge types: CONTAINS, DEFINES, CALLS, IMPORTS, EXTENDS, IMPLEMENTS, MEMBER_OF, STEP_IN_PROCESS
|
|
65
|
-
- Edge properties: type (STRING), confidence (DOUBLE), reason (STRING), step (INT32)
|
|
66
|
-
|
|
67
|
-
EXAMPLES:
|
|
68
|
-
• Find callers of a function:
|
|
69
|
-
MATCH (a)-[:CodeRelation {type: 'CALLS'}]->(b:Function {name: "validateUser"}) RETURN a.name, a.filePath
|
|
70
|
-
|
|
71
|
-
• Find community members:
|
|
72
|
-
MATCH (f)-[:CodeRelation {type: 'MEMBER_OF'}]->(c:Community) WHERE c.heuristicLabel = "Auth" RETURN f.name
|
|
73
|
-
|
|
74
|
-
• Trace a process:
|
|
75
|
-
MATCH (s)-[r:CodeRelation {type: 'STEP_IN_PROCESS'}]->(p:Process) WHERE p.heuristicLabel = "UserLogin" RETURN s.name, r.step ORDER BY r.step
|
|
76
|
-
|
|
77
|
-
TIPS:
|
|
78
|
-
- All relationships use single CodeRelation table — filter with {type: 'CALLS'} etc.
|
|
79
|
-
- Community = auto-detected functional area (Leiden algorithm)
|
|
80
|
-
- Process = execution flow trace from entry point to terminal
|
|
55
|
+
description: `Execute Cypher query against the code knowledge graph.
|
|
56
|
+
|
|
57
|
+
WHEN TO USE: Complex structural queries that search/explore can't answer. READ gitnexus://repo/{name}/schema first for the full schema.
|
|
58
|
+
AFTER THIS: Use context() on result symbols for deeper context.
|
|
59
|
+
|
|
60
|
+
SCHEMA:
|
|
61
|
+
- Nodes: File, Folder, Function, Class, Interface, Method, CodeElement, Community, Process
|
|
62
|
+
- Multi-language nodes (use backticks): \`Struct\`, \`Enum\`, \`Trait\`, \`Impl\`, etc.
|
|
63
|
+
- All edges via single CodeRelation table with 'type' property
|
|
64
|
+
- Edge types: CONTAINS, DEFINES, CALLS, IMPORTS, EXTENDS, IMPLEMENTS, MEMBER_OF, STEP_IN_PROCESS
|
|
65
|
+
- Edge properties: type (STRING), confidence (DOUBLE), reason (STRING), step (INT32)
|
|
66
|
+
|
|
67
|
+
EXAMPLES:
|
|
68
|
+
• Find callers of a function:
|
|
69
|
+
MATCH (a)-[:CodeRelation {type: 'CALLS'}]->(b:Function {name: "validateUser"}) RETURN a.name, a.filePath
|
|
70
|
+
|
|
71
|
+
• Find community members:
|
|
72
|
+
MATCH (f)-[:CodeRelation {type: 'MEMBER_OF'}]->(c:Community) WHERE c.heuristicLabel = "Auth" RETURN f.name
|
|
73
|
+
|
|
74
|
+
• Trace a process:
|
|
75
|
+
MATCH (s)-[r:CodeRelation {type: 'STEP_IN_PROCESS'}]->(p:Process) WHERE p.heuristicLabel = "UserLogin" RETURN s.name, r.step ORDER BY r.step
|
|
76
|
+
|
|
77
|
+
TIPS:
|
|
78
|
+
- All relationships use single CodeRelation table — filter with {type: 'CALLS'} etc.
|
|
79
|
+
- Community = auto-detected functional area (Leiden algorithm)
|
|
80
|
+
- Process = execution flow trace from entry point to terminal
|
|
81
81
|
- Use heuristicLabel (not label) for human-readable community/process names`,
|
|
82
82
|
inputSchema: {
|
|
83
83
|
type: 'object',
|
|
@@ -90,12 +90,12 @@ TIPS:
|
|
|
90
90
|
},
|
|
91
91
|
{
|
|
92
92
|
name: 'context',
|
|
93
|
-
description: `360-degree view of a single code symbol.
|
|
94
|
-
Shows categorized incoming/outgoing references (calls, imports, extends, implements), process participation, and file location.
|
|
95
|
-
|
|
96
|
-
WHEN TO USE: After query() to understand a specific symbol in depth. When you need to know all callers, callees, and what execution flows a symbol participates in.
|
|
97
|
-
AFTER THIS: Use impact() if planning changes, or READ gitnexus://repo/{name}/process/{processName} for full execution trace.
|
|
98
|
-
|
|
93
|
+
description: `360-degree view of a single code symbol.
|
|
94
|
+
Shows categorized incoming/outgoing references (calls, imports, extends, implements), process participation, and file location.
|
|
95
|
+
|
|
96
|
+
WHEN TO USE: After query() to understand a specific symbol in depth. When you need to know all callers, callees, and what execution flows a symbol participates in.
|
|
97
|
+
AFTER THIS: Use impact() if planning changes, or READ gitnexus://repo/{name}/process/{processName} for full execution trace.
|
|
98
|
+
|
|
99
99
|
Handles disambiguation: if multiple symbols share the same name, returns candidates for you to pick from. Use uid param for zero-ambiguity lookup from prior results.`,
|
|
100
100
|
inputSchema: {
|
|
101
101
|
type: 'object',
|
|
@@ -111,12 +111,12 @@ Handles disambiguation: if multiple symbols share the same name, returns candida
|
|
|
111
111
|
},
|
|
112
112
|
{
|
|
113
113
|
name: 'detect_changes',
|
|
114
|
-
description: `Analyze uncommitted git changes and find affected execution flows.
|
|
115
|
-
Maps git diff hunks to indexed symbols, then traces which processes are impacted.
|
|
116
|
-
|
|
117
|
-
WHEN TO USE: Before committing — to understand what your changes affect. Pre-commit review, PR preparation.
|
|
118
|
-
AFTER THIS: Review affected processes. Use context() on high-risk symbols. READ gitnexus://repo/{name}/process/{name} for full traces.
|
|
119
|
-
|
|
114
|
+
description: `Analyze uncommitted git changes and find affected execution flows.
|
|
115
|
+
Maps git diff hunks to indexed symbols, then traces which processes are impacted.
|
|
116
|
+
|
|
117
|
+
WHEN TO USE: Before committing — to understand what your changes affect. Pre-commit review, PR preparation.
|
|
118
|
+
AFTER THIS: Review affected processes. Use context() on high-risk symbols. READ gitnexus://repo/{name}/process/{name} for full traces.
|
|
119
|
+
|
|
120
120
|
Returns: changed symbols, affected processes, and a risk summary.`,
|
|
121
121
|
inputSchema: {
|
|
122
122
|
type: 'object',
|
|
@@ -130,14 +130,14 @@ Returns: changed symbols, affected processes, and a risk summary.`,
|
|
|
130
130
|
},
|
|
131
131
|
{
|
|
132
132
|
name: 'rename',
|
|
133
|
-
description: `Multi-file coordinated rename using the knowledge graph + text search.
|
|
134
|
-
Finds all references via graph (high confidence) and regex text search (lower confidence). Preview by default.
|
|
135
|
-
|
|
136
|
-
WHEN TO USE: Renaming a function, class, method, or variable across the codebase. Safer than find-and-replace.
|
|
137
|
-
AFTER THIS: Run detect_changes() to verify no unexpected side effects.
|
|
138
|
-
|
|
139
|
-
Each edit is tagged with confidence:
|
|
140
|
-
- "graph": found via knowledge graph relationships (high confidence, safe to accept)
|
|
133
|
+
description: `Multi-file coordinated rename using the knowledge graph + text search.
|
|
134
|
+
Finds all references via graph (high confidence) and regex text search (lower confidence). Preview by default.
|
|
135
|
+
|
|
136
|
+
WHEN TO USE: Renaming a function, class, method, or variable across the codebase. Safer than find-and-replace.
|
|
137
|
+
AFTER THIS: Run detect_changes() to verify no unexpected side effects.
|
|
138
|
+
|
|
139
|
+
Each edit is tagged with confidence:
|
|
140
|
+
- "graph": found via knowledge graph relationships (high confidence, safe to accept)
|
|
141
141
|
- "text_search": found via regex text search (lower confidence, review carefully)`,
|
|
142
142
|
inputSchema: {
|
|
143
143
|
type: 'object',
|
|
@@ -154,18 +154,18 @@ Each edit is tagged with confidence:
|
|
|
154
154
|
},
|
|
155
155
|
{
|
|
156
156
|
name: 'impact',
|
|
157
|
-
description: `Analyze the blast radius of changing a code symbol.
|
|
158
|
-
Returns all symbols affected by modifying the target, grouped by depth with edge types and confidence.
|
|
159
|
-
|
|
160
|
-
WHEN TO USE: Before making code changes — especially refactoring, renaming, or modifying shared code. Shows what would break.
|
|
161
|
-
AFTER THIS: Review d=1 items (WILL BREAK). READ gitnexus://repo/{name}/processes to check affected execution flows.
|
|
162
|
-
|
|
163
|
-
Depth groups:
|
|
164
|
-
- d=1: WILL BREAK (direct callers/importers)
|
|
165
|
-
- d=2: LIKELY AFFECTED (indirect)
|
|
166
|
-
- d=3: MAY NEED TESTING (transitive)
|
|
167
|
-
|
|
168
|
-
EdgeType: CALLS, IMPORTS, EXTENDS, IMPLEMENTS
|
|
157
|
+
description: `Analyze the blast radius of changing a code symbol.
|
|
158
|
+
Returns all symbols affected by modifying the target, grouped by depth with edge types and confidence.
|
|
159
|
+
|
|
160
|
+
WHEN TO USE: Before making code changes — especially refactoring, renaming, or modifying shared code. Shows what would break.
|
|
161
|
+
AFTER THIS: Review d=1 items (WILL BREAK). READ gitnexus://repo/{name}/processes to check affected execution flows.
|
|
162
|
+
|
|
163
|
+
Depth groups:
|
|
164
|
+
- d=1: WILL BREAK (direct callers/importers)
|
|
165
|
+
- d=2: LIKELY AFFECTED (indirect)
|
|
166
|
+
- d=3: MAY NEED TESTING (transitive)
|
|
167
|
+
|
|
168
|
+
EdgeType: CALLS, IMPORTS, EXTENDS, IMPLEMENTS
|
|
169
169
|
Confidence: 1.0 = certain, <0.8 = fuzzy match`,
|
|
170
170
|
inputSchema: {
|
|
171
171
|
type: 'object',
|
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* GitNexus Claude Code Hook
|
|
4
|
-
*
|
|
5
|
-
* PreToolUse handler — intercepts Grep/Glob/Bash searches
|
|
6
|
-
* and augments with graph context from the GitNexus index.
|
|
7
|
-
*
|
|
8
|
-
* NOTE: SessionStart hooks are broken on Windows (Claude Code bug).
|
|
9
|
-
* Session context is injected via CLAUDE.md / skills instead.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
const fs = require('fs');
|
|
13
|
-
const path = require('path');
|
|
14
|
-
const { execFileSync } = require('child_process');
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Read JSON input from stdin synchronously.
|
|
18
|
-
*/
|
|
19
|
-
function readInput() {
|
|
20
|
-
try {
|
|
21
|
-
const data = fs.readFileSync(0, 'utf-8');
|
|
22
|
-
return JSON.parse(data);
|
|
23
|
-
} catch {
|
|
24
|
-
return {};
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Check if a directory (or ancestor) has a .gitnexus index.
|
|
30
|
-
*/
|
|
31
|
-
function findGitNexusIndex(startDir) {
|
|
32
|
-
let dir = startDir || process.cwd();
|
|
33
|
-
for (let i = 0; i < 5; i++) {
|
|
34
|
-
if (fs.existsSync(path.join(dir, '.gitnexus'))) {
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
const parent = path.dirname(dir);
|
|
38
|
-
if (parent === dir) break;
|
|
39
|
-
dir = parent;
|
|
40
|
-
}
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Extract search pattern from tool input.
|
|
46
|
-
*/
|
|
47
|
-
function extractPattern(toolName, toolInput) {
|
|
48
|
-
if (toolName === 'Grep') {
|
|
49
|
-
return toolInput.pattern || null;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (toolName === 'Glob') {
|
|
53
|
-
const raw = toolInput.pattern || '';
|
|
54
|
-
const match = raw.match(/[*\/]([a-zA-Z][a-zA-Z0-9_-]{2,})/);
|
|
55
|
-
return match ? match[1] : null;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (toolName === 'Bash') {
|
|
59
|
-
const cmd = toolInput.command || '';
|
|
60
|
-
if (!/\brg\b|\bgrep\b/.test(cmd)) return null;
|
|
61
|
-
|
|
62
|
-
const tokens = cmd.split(/\s+/);
|
|
63
|
-
let foundCmd = false;
|
|
64
|
-
let skipNext = false;
|
|
65
|
-
const flagsWithValues = new Set(['-e', '-f', '-m', '-A', '-B', '-C', '-g', '--glob', '-t', '--type', '--include', '--exclude']);
|
|
66
|
-
|
|
67
|
-
for (const token of tokens) {
|
|
68
|
-
if (skipNext) { skipNext = false; continue; }
|
|
69
|
-
if (!foundCmd) {
|
|
70
|
-
if (/\brg$|\bgrep$/.test(token)) foundCmd = true;
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
if (token.startsWith('-')) {
|
|
74
|
-
if (flagsWithValues.has(token)) skipNext = true;
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
const cleaned = token.replace(/['"]/g, '');
|
|
78
|
-
return cleaned.length >= 3 ? cleaned : null;
|
|
79
|
-
}
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function main() {
|
|
87
|
-
try {
|
|
88
|
-
const input = readInput();
|
|
89
|
-
const hookEvent = input.hook_event_name || '';
|
|
90
|
-
|
|
91
|
-
if (hookEvent !== 'PreToolUse') return;
|
|
92
|
-
|
|
93
|
-
const cwd = input.cwd || process.cwd();
|
|
94
|
-
if (!findGitNexusIndex(cwd)) return;
|
|
95
|
-
|
|
96
|
-
const toolName = input.tool_name || '';
|
|
97
|
-
const toolInput = input.tool_input || {};
|
|
98
|
-
|
|
99
|
-
if (toolName !== 'Grep' && toolName !== 'Glob' && toolName !== 'Bash') return;
|
|
100
|
-
|
|
101
|
-
const pattern = extractPattern(toolName, toolInput);
|
|
102
|
-
if (!pattern || pattern.length < 3) return;
|
|
103
|
-
|
|
104
|
-
// Resolve CLI path relative to this hook script (same package)
|
|
105
|
-
// hooks/claude/gitnexus-hook.cjs → dist/cli/index.js
|
|
106
|
-
const cliPath = path.resolve(__dirname, '..', '..', 'dist', 'cli', 'index.js');
|
|
107
|
-
|
|
108
|
-
// augment CLI writes result to stderr (KuzuDB's native module captures
|
|
109
|
-
// stdout fd at OS level, making it unusable in subprocess contexts).
|
|
110
|
-
const { spawnSync } = require('child_process');
|
|
111
|
-
let result = '';
|
|
112
|
-
try {
|
|
113
|
-
const child = spawnSync(
|
|
114
|
-
process.execPath,
|
|
115
|
-
[cliPath, 'augment', pattern],
|
|
116
|
-
{ encoding: 'utf-8', timeout: 8000, cwd, stdio: ['pipe', 'pipe', 'pipe'] }
|
|
117
|
-
);
|
|
118
|
-
result = child.stderr || '';
|
|
119
|
-
} catch { /* graceful failure */ }
|
|
120
|
-
|
|
121
|
-
if (result && result.trim()) {
|
|
122
|
-
console.log(JSON.stringify({
|
|
123
|
-
hookSpecificOutput: {
|
|
124
|
-
hookEventName: 'PreToolUse',
|
|
125
|
-
additionalContext: result.trim()
|
|
126
|
-
}
|
|
127
|
-
}));
|
|
128
|
-
}
|
|
129
|
-
} catch (err) {
|
|
130
|
-
// Graceful failure — log to stderr for debugging
|
|
131
|
-
console.error('GitNexus hook error:', err.message);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
main();
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* GitNexus Claude Code Hook
|
|
4
|
+
*
|
|
5
|
+
* PreToolUse handler — intercepts Grep/Glob/Bash searches
|
|
6
|
+
* and augments with graph context from the GitNexus index.
|
|
7
|
+
*
|
|
8
|
+
* NOTE: SessionStart hooks are broken on Windows (Claude Code bug).
|
|
9
|
+
* Session context is injected via CLAUDE.md / skills instead.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const fs = require('fs');
|
|
13
|
+
const path = require('path');
|
|
14
|
+
const { execFileSync } = require('child_process');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Read JSON input from stdin synchronously.
|
|
18
|
+
*/
|
|
19
|
+
function readInput() {
|
|
20
|
+
try {
|
|
21
|
+
const data = fs.readFileSync(0, 'utf-8');
|
|
22
|
+
return JSON.parse(data);
|
|
23
|
+
} catch {
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Check if a directory (or ancestor) has a .gitnexus index.
|
|
30
|
+
*/
|
|
31
|
+
function findGitNexusIndex(startDir) {
|
|
32
|
+
let dir = startDir || process.cwd();
|
|
33
|
+
for (let i = 0; i < 5; i++) {
|
|
34
|
+
if (fs.existsSync(path.join(dir, '.gitnexus'))) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
const parent = path.dirname(dir);
|
|
38
|
+
if (parent === dir) break;
|
|
39
|
+
dir = parent;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Extract search pattern from tool input.
|
|
46
|
+
*/
|
|
47
|
+
function extractPattern(toolName, toolInput) {
|
|
48
|
+
if (toolName === 'Grep') {
|
|
49
|
+
return toolInput.pattern || null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (toolName === 'Glob') {
|
|
53
|
+
const raw = toolInput.pattern || '';
|
|
54
|
+
const match = raw.match(/[*\/]([a-zA-Z][a-zA-Z0-9_-]{2,})/);
|
|
55
|
+
return match ? match[1] : null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (toolName === 'Bash') {
|
|
59
|
+
const cmd = toolInput.command || '';
|
|
60
|
+
if (!/\brg\b|\bgrep\b/.test(cmd)) return null;
|
|
61
|
+
|
|
62
|
+
const tokens = cmd.split(/\s+/);
|
|
63
|
+
let foundCmd = false;
|
|
64
|
+
let skipNext = false;
|
|
65
|
+
const flagsWithValues = new Set(['-e', '-f', '-m', '-A', '-B', '-C', '-g', '--glob', '-t', '--type', '--include', '--exclude']);
|
|
66
|
+
|
|
67
|
+
for (const token of tokens) {
|
|
68
|
+
if (skipNext) { skipNext = false; continue; }
|
|
69
|
+
if (!foundCmd) {
|
|
70
|
+
if (/\brg$|\bgrep$/.test(token)) foundCmd = true;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (token.startsWith('-')) {
|
|
74
|
+
if (flagsWithValues.has(token)) skipNext = true;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const cleaned = token.replace(/['"]/g, '');
|
|
78
|
+
return cleaned.length >= 3 ? cleaned : null;
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function main() {
|
|
87
|
+
try {
|
|
88
|
+
const input = readInput();
|
|
89
|
+
const hookEvent = input.hook_event_name || '';
|
|
90
|
+
|
|
91
|
+
if (hookEvent !== 'PreToolUse') return;
|
|
92
|
+
|
|
93
|
+
const cwd = input.cwd || process.cwd();
|
|
94
|
+
if (!findGitNexusIndex(cwd)) return;
|
|
95
|
+
|
|
96
|
+
const toolName = input.tool_name || '';
|
|
97
|
+
const toolInput = input.tool_input || {};
|
|
98
|
+
|
|
99
|
+
if (toolName !== 'Grep' && toolName !== 'Glob' && toolName !== 'Bash') return;
|
|
100
|
+
|
|
101
|
+
const pattern = extractPattern(toolName, toolInput);
|
|
102
|
+
if (!pattern || pattern.length < 3) return;
|
|
103
|
+
|
|
104
|
+
// Resolve CLI path relative to this hook script (same package)
|
|
105
|
+
// hooks/claude/gitnexus-hook.cjs → dist/cli/index.js
|
|
106
|
+
const cliPath = path.resolve(__dirname, '..', '..', 'dist', 'cli', 'index.js');
|
|
107
|
+
|
|
108
|
+
// augment CLI writes result to stderr (KuzuDB's native module captures
|
|
109
|
+
// stdout fd at OS level, making it unusable in subprocess contexts).
|
|
110
|
+
const { spawnSync } = require('child_process');
|
|
111
|
+
let result = '';
|
|
112
|
+
try {
|
|
113
|
+
const child = spawnSync(
|
|
114
|
+
process.execPath,
|
|
115
|
+
[cliPath, 'augment', pattern],
|
|
116
|
+
{ encoding: 'utf-8', timeout: 8000, cwd, stdio: ['pipe', 'pipe', 'pipe'] }
|
|
117
|
+
);
|
|
118
|
+
result = child.stderr || '';
|
|
119
|
+
} catch { /* graceful failure */ }
|
|
120
|
+
|
|
121
|
+
if (result && result.trim()) {
|
|
122
|
+
console.log(JSON.stringify({
|
|
123
|
+
hookSpecificOutput: {
|
|
124
|
+
hookEventName: 'PreToolUse',
|
|
125
|
+
additionalContext: result.trim()
|
|
126
|
+
}
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
} catch (err) {
|
|
130
|
+
// Graceful failure — log to stderr for debugging
|
|
131
|
+
console.error('GitNexus hook error:', err.message);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
main();
|