hookset 1.1.1 → 1.2.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.
- package/claude/codegraph.mjs +90 -0
- package/dist/context.js +24 -3
- package/dist/core.d.ts +22 -0
- package/dist/core.js +40 -0
- package/package.json +1 -1
- package/stations.json +19 -2
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// codegraph — Claude Code SessionStart hook. Canonical copy: burgee packages/hookset/claude/.
|
|
3
|
+
// `hookset init --claude` vendors it next to guard-bash.mjs.
|
|
4
|
+
//
|
|
5
|
+
// A repo whose .mcp.json runs the CodeGraph MCP server answers "where is X, what calls it, how
|
|
6
|
+
// does A reach B" from a prebuilt symbol graph in one codegraph_explore call, where grep takes a
|
|
7
|
+
// search, a read and a re-search. The server never builds its own index, and a cloud session
|
|
8
|
+
// starts from a fresh clone without one, so the tool would answer "not indexed" every time. This
|
|
9
|
+
// starts `codegraph init` (or `sync`, when an index is there) in the background so the session is
|
|
10
|
+
// not held for it; the server picks the index up when it lands.
|
|
11
|
+
//
|
|
12
|
+
// Denying a symbol-shaped grep once was tried and measured on burgee: it added tool calls and did
|
|
13
|
+
// not cut greps, so the hook only builds the index.
|
|
14
|
+
//
|
|
15
|
+
// It does nothing in a repo with no codegraph server in .mcp.json, or with HOOKSET_CODEGRAPH=0.
|
|
16
|
+
// `node codegraph.mjs --self-test` runs the cases at the bottom.
|
|
17
|
+
|
|
18
|
+
import { spawn } from "node:child_process";
|
|
19
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
|
20
|
+
import { tmpdir } from "node:os";
|
|
21
|
+
import { join } from "node:path";
|
|
22
|
+
|
|
23
|
+
/** The npm spec the repo's .mcp.json runs (`@colbymchenry/codegraph@1.6.4`), or null when it runs none. */
|
|
24
|
+
export function serverSpec(dir) {
|
|
25
|
+
try {
|
|
26
|
+
const server = JSON.parse(readFileSync(join(dir, ".mcp.json"), "utf8"))?.mcpServers?.codegraph;
|
|
27
|
+
return (server?.args ?? []).find((a) => typeof a === "string" && /(^|\/)codegraph@[^/]+$/.test(a)) ?? null;
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The npx arguments that bring the index up to date: build it on a fresh clone, catch it up otherwise. */
|
|
34
|
+
export function indexArgs(dir, spec) {
|
|
35
|
+
const indexed = existsSync(join(dir, ".codegraph", "codegraph.db"));
|
|
36
|
+
return ["-y", spec, ...(indexed ? ["sync"] : ["init", "--yes"]), dir];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function startIndex(dir, spec) {
|
|
40
|
+
const env = { ...process.env, CODEGRAPH_TELEMETRY: "0" };
|
|
41
|
+
const windows = process.platform === "win32";
|
|
42
|
+
const child = spawn(windows ? "npx.cmd" : "npx", indexArgs(dir, spec), { cwd: dir, env, detached: true, stdio: "ignore", shell: windows });
|
|
43
|
+
child.on("error", () => {});
|
|
44
|
+
child.unref();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const CASES = {
|
|
48
|
+
specs: {
|
|
49
|
+
'{"mcpServers":{"codegraph":{"args":["-y","@colbymchenry/codegraph@1.6.4","serve","--mcp"]}}}': "@colbymchenry/codegraph@1.6.4",
|
|
50
|
+
'{"mcpServers":{"codegraph":{"command":"codegraph","args":["serve","--mcp"]}}}': null,
|
|
51
|
+
'{"mcpServers":{"other":{"args":["-y","@colbymchenry/codegraph@1.6.4"]}}}': null,
|
|
52
|
+
"not json": null,
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
function selfTest() {
|
|
57
|
+
const bad = [];
|
|
58
|
+
for (const [json, want] of Object.entries(CASES.specs)) {
|
|
59
|
+
const dir = mkdtempSync(join(tmpdir(), "codegraph-"));
|
|
60
|
+
writeFileSync(join(dir, ".mcp.json"), json);
|
|
61
|
+
if (serverSpec(dir) !== want) bad.push(`spec of ${json}: want ${want}, got ${serverSpec(dir)}`);
|
|
62
|
+
}
|
|
63
|
+
const dir = mkdtempSync(join(tmpdir(), "codegraph-"));
|
|
64
|
+
if (indexArgs(dir, "x@1").join(" ") !== `-y x@1 init --yes ${dir}`) bad.push("a fresh clone should init");
|
|
65
|
+
mkdirSync(join(dir, ".codegraph"));
|
|
66
|
+
writeFileSync(join(dir, ".codegraph", "codegraph.db"), "");
|
|
67
|
+
if (indexArgs(dir, "x@1").join(" ") !== `-y x@1 sync ${dir}`) bad.push("an indexed repo should sync");
|
|
68
|
+
if (serverSpec(join(dir, "missing")) !== null) bad.push("no .mcp.json should be null");
|
|
69
|
+
process.stdout.write(bad.length ? `${bad.join("\n")}\n${bad.length} failing\n` : `ok: ${Object.keys(CASES.specs).length + 3} cases\n`);
|
|
70
|
+
process.exit(bad.length ? 1 : 0);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const isMain = Boolean(process.argv[1]) && import.meta.url.endsWith(`/${process.argv[1].split(/[\\/]/).pop()}`);
|
|
74
|
+
if (isMain && process.argv.includes("--self-test")) selfTest();
|
|
75
|
+
else if (isMain) {
|
|
76
|
+
let raw = "";
|
|
77
|
+
process.stdin.on("data", (d) => (raw += d));
|
|
78
|
+
process.stdin.on("end", () => {
|
|
79
|
+
let payload;
|
|
80
|
+
try {
|
|
81
|
+
payload = JSON.parse(raw) ?? {};
|
|
82
|
+
} catch {
|
|
83
|
+
process.exit(0);
|
|
84
|
+
}
|
|
85
|
+
if (process.env.HOOKSET_CODEGRAPH === "0" || payload.hook_event_name !== "SessionStart") process.exit(0);
|
|
86
|
+
const dir = process.env.CLAUDE_PROJECT_DIR || payload.cwd || process.cwd();
|
|
87
|
+
const spec = serverSpec(dir);
|
|
88
|
+
if (spec) startIndex(dir, spec);
|
|
89
|
+
});
|
|
90
|
+
}
|
package/dist/context.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { join, posix } from 'node:path';
|
|
3
|
+
import { loadStations, packageRoot } from './core.js';
|
|
3
4
|
const CHARS_PER_TOKEN = 4;
|
|
4
5
|
const LARGEST_SHOWN = 5;
|
|
5
6
|
const FRONTMATTER = /^---\r?\n([\s\S]*?)\r?\n---/;
|
|
@@ -56,15 +57,34 @@ function entryOf(repo, file) {
|
|
|
56
57
|
}
|
|
57
58
|
return { file, chars, userOnly };
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
+
function servers(repo, pkg) {
|
|
61
|
+
const file = join(repo, '.mcp.json');
|
|
62
|
+
if (!existsSync(file))
|
|
63
|
+
return { entries: [], unmeasured: [] };
|
|
64
|
+
const names = Object.keys(JSON.parse(String(readFileSync(file))).mcpServers ?? {});
|
|
65
|
+
const known = new Map(loadStations(pkg).claude.mcp.map((m) => [m.id, m.contextChars]));
|
|
66
|
+
const entries = [];
|
|
67
|
+
const unmeasured = [];
|
|
68
|
+
for (const name of names) {
|
|
69
|
+
const chars = known.get(name);
|
|
70
|
+
if (chars === undefined)
|
|
71
|
+
unmeasured.push(name);
|
|
72
|
+
else
|
|
73
|
+
entries.push({ file: `.mcp.json#${name}`, chars, userOnly: false });
|
|
74
|
+
}
|
|
75
|
+
return { entries, unmeasured };
|
|
76
|
+
}
|
|
77
|
+
export function measureContext(repo, pkg = packageRoot()) {
|
|
60
78
|
const mem = memory(repo);
|
|
61
|
-
const
|
|
79
|
+
const mcp = servers(repo, pkg);
|
|
80
|
+
const entries = [...listed(repo).map((file) => entryOf(repo, file)), ...mcp.entries];
|
|
62
81
|
const chars = entries.reduce((sum, entry) => sum + entry.chars, mem.chars);
|
|
63
82
|
return {
|
|
64
83
|
tokens: tokensOf(chars),
|
|
65
84
|
memoryFiles: mem.files,
|
|
66
85
|
memoryChars: mem.chars,
|
|
67
86
|
entries,
|
|
87
|
+
unmeasured: mcp.unmeasured,
|
|
68
88
|
};
|
|
69
89
|
}
|
|
70
90
|
function readConfig(repo) {
|
|
@@ -121,6 +141,7 @@ export function contextText(result) {
|
|
|
121
141
|
if (result.updated)
|
|
122
142
|
return `ceiling set to ${result.tokens} tokens`;
|
|
123
143
|
const ceiling = result.ceiling;
|
|
124
|
-
const
|
|
144
|
+
const unmeasured = result.unmeasured.length > 0 ? ` + MCP servers not measured: ${result.unmeasured.join(', ')}` : '';
|
|
145
|
+
const line = `✔ always-loaded context: ${result.tokens} tokens${unmeasured} (ceiling ${ceiling})`;
|
|
125
146
|
return result.tokens < ceiling ? `${line}\n below the ceiling: run \`hookset context --update\` to lock in the cut` : line;
|
|
126
147
|
}
|
package/dist/core.d.ts
CHANGED
|
@@ -28,6 +28,25 @@ export interface ClaudeSkill {
|
|
|
28
28
|
placeholder: string;
|
|
29
29
|
detail: string;
|
|
30
30
|
}
|
|
31
|
+
/** An MCP server every repo runs: written into .mcp.json, its index folder gitignored, its tools allowed. */
|
|
32
|
+
export interface ClaudeMcp {
|
|
33
|
+
id: string;
|
|
34
|
+
detail: string;
|
|
35
|
+
/** The `.mcp.json` entry, written as is under `mcpServers.<id>`. */
|
|
36
|
+
server: {
|
|
37
|
+
type: string;
|
|
38
|
+
command: string;
|
|
39
|
+
args: string[];
|
|
40
|
+
env?: Record<string, string>;
|
|
41
|
+
};
|
|
42
|
+
/** What the server puts in every session (its instructions and tool definitions), in characters. */
|
|
43
|
+
contextChars: number;
|
|
44
|
+
contextSource: string;
|
|
45
|
+
/** The folder the server writes into the repo, which .gitignore must list. */
|
|
46
|
+
ignore: string;
|
|
47
|
+
/** The tool names settings.json allows without a prompt. */
|
|
48
|
+
allow: string[];
|
|
49
|
+
}
|
|
31
50
|
export interface Stations {
|
|
32
51
|
remote: string;
|
|
33
52
|
tagPrefix: string;
|
|
@@ -37,6 +56,7 @@ export interface Stations {
|
|
|
37
56
|
files: string[];
|
|
38
57
|
hooks: ClaudeHook[];
|
|
39
58
|
skills: ClaudeSkill[];
|
|
59
|
+
mcp: ClaudeMcp[];
|
|
40
60
|
};
|
|
41
61
|
}
|
|
42
62
|
export interface Problem {
|
|
@@ -78,6 +98,8 @@ export interface CheckResult {
|
|
|
78
98
|
}
|
|
79
99
|
/** Everything `hookset check` reports for the repo at `repo`, against the presets in `pkg`. */
|
|
80
100
|
export declare function checkRepo(repo: string, pkg: string): CheckResult;
|
|
101
|
+
/** A pack MCP server the repo must run: in .mcp.json as hookset pins it, its index gitignored, enabled in settings.json. */
|
|
102
|
+
export declare function checkMcp(repo: string, mcp: ClaudeMcp): Problem[];
|
|
81
103
|
/** A pack skill the repo must carry and fill in: present, still holding what it requires, no placeholder left. */
|
|
82
104
|
export declare function checkSkill(repo: string, skill: ClaudeSkill): Problem[];
|
|
83
105
|
/** The pack's hooks, each PostToolUse one also on PostToolUseFailure (a failed Bash call lands there). */
|
package/dist/core.js
CHANGED
|
@@ -205,6 +205,26 @@ function checkClaude(repo, pkg, stations) {
|
|
|
205
205
|
}
|
|
206
206
|
for (const skill of stations.claude.skills)
|
|
207
207
|
problems.push(...checkSkill(repo, skill));
|
|
208
|
+
for (const mcp of stations.claude.mcp)
|
|
209
|
+
problems.push(...checkMcp(repo, mcp));
|
|
210
|
+
return problems;
|
|
211
|
+
}
|
|
212
|
+
const readJson = (file, fallback) => (existsSync(file) ? JSON.parse(readText(file)) : fallback);
|
|
213
|
+
export function checkMcp(repo, mcp) {
|
|
214
|
+
const problems = [];
|
|
215
|
+
const server = readJson(join(repo, '.mcp.json'), {}).mcpServers?.[mcp.id];
|
|
216
|
+
if (server === undefined)
|
|
217
|
+
problems.push({ id: `${mcp.id}-mcp-missing`, severity: 'error', message: `.mcp.json does not run the ${mcp.id} server: ${mcp.detail}`, fix: 'run `hookset init --claude`' });
|
|
218
|
+
else if (JSON.stringify(server) !== JSON.stringify(mcp.server))
|
|
219
|
+
problems.push({ id: `${mcp.id}-mcp-drift`, severity: 'error', message: `.mcp.json runs ${mcp.id} differently from the hookset pin (${mcp.server.args.join(' ')})`, fix: 'run `hookset init --claude`; change the pin in burgee packages/hookset/stations.json' });
|
|
220
|
+
const ignore = join(repo, '.gitignore');
|
|
221
|
+
if (!(existsSync(ignore) && readText(ignore).split(/\r?\n/).some((l) => l.trim() === mcp.ignore))) {
|
|
222
|
+
problems.push({ id: `${mcp.id}-not-ignored`, severity: 'error', message: `.gitignore does not list ${mcp.ignore}, the ${mcp.id} index`, fix: `add \`${mcp.ignore}\` to .gitignore (\`hookset init --claude\` does)` });
|
|
223
|
+
}
|
|
224
|
+
const settings = readJson(join(repo, '.claude', 'settings.json'), {});
|
|
225
|
+
if (!(settings.enabledMcpjsonServers ?? []).includes(mcp.id)) {
|
|
226
|
+
problems.push({ id: `${mcp.id}-mcp-disabled`, severity: 'error', message: `.claude/settings.json does not enable the ${mcp.id} server, so every session asks first`, fix: `add "${mcp.id}" to enabledMcpjsonServers (\`hookset init --claude\` does)` });
|
|
227
|
+
}
|
|
208
228
|
return problems;
|
|
209
229
|
}
|
|
210
230
|
export function checkSkill(repo, skill) {
|
|
@@ -269,6 +289,8 @@ export function initRepo(repo, pkg, options = {}) {
|
|
|
269
289
|
if (!existsSync(join(repo, skill.path)))
|
|
270
290
|
write(skill.path, readText(join(pkg, skill.template)));
|
|
271
291
|
write('.claude/settings.json', mergeSettings(repo, stations));
|
|
292
|
+
write('.mcp.json', mergeMcp(repo, stations));
|
|
293
|
+
write('.gitignore', mergeIgnore(repo, stations));
|
|
272
294
|
}
|
|
273
295
|
return { changed: written.length > 0, written };
|
|
274
296
|
}
|
|
@@ -295,6 +317,22 @@ function repin(yaml, ref, remote) {
|
|
|
295
317
|
lines.splice(at, 1, `${line.slice(0, line.indexOf('ref:'))}ref: ${ref}`);
|
|
296
318
|
return lines.join(eol);
|
|
297
319
|
}
|
|
320
|
+
function mergeMcp(repo, stations) {
|
|
321
|
+
const mcp = readJson(join(repo, '.mcp.json'), {});
|
|
322
|
+
mcp.mcpServers = { ...mcp.mcpServers, ...Object.fromEntries(stations.claude.mcp.map((m) => [m.id, m.server])) };
|
|
323
|
+
return `${JSON.stringify(mcp, undefined, 2)}\n`;
|
|
324
|
+
}
|
|
325
|
+
function mergeIgnore(repo, stations) {
|
|
326
|
+
const file = join(repo, '.gitignore');
|
|
327
|
+
const text = existsSync(file) ? readText(file) : '';
|
|
328
|
+
const lines = text.split(/\r?\n/).map((l) => l.trim());
|
|
329
|
+
const missing = stations.claude.mcp.map((m) => m.ignore).filter((i) => !lines.includes(i));
|
|
330
|
+
if (missing.length === 0)
|
|
331
|
+
return text;
|
|
332
|
+
const sep = text === '' || text.endsWith('\n') ? '' : '\n';
|
|
333
|
+
return `${text}${sep}# MCP server indexes, rebuilt per session by .claude/hooks (hookset)\n${missing.join('\n')}\n`;
|
|
334
|
+
}
|
|
335
|
+
const union = (a, b) => [...new Set([...(a ?? []), ...b])];
|
|
298
336
|
function mergeSettings(repo, stations) {
|
|
299
337
|
const file = join(repo, '.claude', 'settings.json');
|
|
300
338
|
const settings = existsSync(file)
|
|
@@ -313,6 +351,8 @@ function mergeSettings(repo, stations) {
|
|
|
313
351
|
groups.push({ matcher: h.matcher, hooks: [{ type: 'command', command: hookCommand(h.file) }] });
|
|
314
352
|
}
|
|
315
353
|
settings.hooks = Object.fromEntries(events);
|
|
354
|
+
settings.enabledMcpjsonServers = union(settings.enabledMcpjsonServers, stations.claude.mcp.map((m) => m.id));
|
|
355
|
+
settings.permissions = { ...settings.permissions, allow: union(settings.permissions?.allow, stations.claude.mcp.flatMap((m) => m.allow)) };
|
|
316
356
|
return `${JSON.stringify(settings, undefined, 2)}\n`;
|
|
317
357
|
}
|
|
318
358
|
function tally(problems) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hookset",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Git hooks presets for AI coding agents: lefthook presets pulled by a pinned tag, a Claude Code hook pack that denies skipped hooks, and `hookset init` and `check`. No dependency outside the burgee family. A husky migration path.",
|
|
6
6
|
"keywords": [
|
package/stations.json
CHANGED
|
@@ -18,13 +18,30 @@
|
|
|
18
18
|
{ "id": "triggerlist", "hook": "pre-push", "mode": "observe", "command": "hookset-triggerlist", "script": "hook:triggerlist", "detail": "protected paths changed only with their trigger" }
|
|
19
19
|
],
|
|
20
20
|
"claude": {
|
|
21
|
-
"files": ["guard-bash.mjs", "stop-after-3.mjs", "verify-before-stop.mjs"],
|
|
21
|
+
"files": ["guard-bash.mjs", "stop-after-3.mjs", "verify-before-stop.mjs", "codegraph.mjs"],
|
|
22
22
|
"hooks": [
|
|
23
23
|
{ "event": "PreToolUse", "matcher": "Bash", "file": "guard-bash.mjs" },
|
|
24
24
|
{ "event": "PreToolUse", "matcher": "Edit|Write|MultiEdit|NotebookEdit", "file": "guard-bash.mjs" },
|
|
25
25
|
{ "event": "PostToolUse", "matcher": "Bash", "file": "stop-after-3.mjs" },
|
|
26
26
|
{ "event": "PostToolUse", "matcher": "Bash", "file": "verify-before-stop.mjs" },
|
|
27
|
-
{ "event": "Stop", "matcher": "", "file": "verify-before-stop.mjs" }
|
|
27
|
+
{ "event": "Stop", "matcher": "", "file": "verify-before-stop.mjs" },
|
|
28
|
+
{ "event": "SessionStart", "matcher": "", "file": "codegraph.mjs" }
|
|
29
|
+
],
|
|
30
|
+
"mcp": [
|
|
31
|
+
{
|
|
32
|
+
"id": "codegraph",
|
|
33
|
+
"detail": "CodeGraph, a prebuilt symbol graph Claude queries instead of grepping; codegraph.mjs builds its index on SessionStart",
|
|
34
|
+
"server": {
|
|
35
|
+
"type": "stdio",
|
|
36
|
+
"command": "npx",
|
|
37
|
+
"args": ["-y", "@colbymchenry/codegraph@1.6.4", "serve", "--mcp", "--no-telemetry"],
|
|
38
|
+
"env": { "CODEGRAPH_TELEMETRY": "0" }
|
|
39
|
+
},
|
|
40
|
+
"contextChars": 8652,
|
|
41
|
+
"contextSource": "codegraph 1.6.4 serve --mcp: initialize instructions (6,718 chars) plus the codegraph_explore tool definition (1,934 chars), measured over stdio on 2026-11-25",
|
|
42
|
+
"ignore": ".codegraph/",
|
|
43
|
+
"allow": ["mcp__codegraph__codegraph_explore"]
|
|
44
|
+
}
|
|
28
45
|
],
|
|
29
46
|
"skills": [
|
|
30
47
|
{ "id": "verify", "path": ".claude/skills/verify/SKILL.md", "template": "claude/skills/verify/SKILL.md", "requires": ["name: verify", "## Feature map"], "placeholder": "TODO(verify)", "detail": "the skill that boots the app, drives the change and checks what it shows, with a feature map" }
|