memhook 0.2.0 → 0.3.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.
@@ -0,0 +1,283 @@
1
+ /**
2
+ * `memhook init` / `memhook uninstall` orchestration.
3
+ *
4
+ * These are INTERACTIVE, user-invoked commands — NOT the hook entrypoint. They
5
+ * are allowed to prompt, own the TTY, print to stdout, and exit non-zero on
6
+ * user error (docs/SPECIFICATION.md §9: "memhook run is the only command that
7
+ * must obey the fail-soft contract"). The one hard rule: they must never
8
+ * corrupt `~/.claude/settings.json`. So:
9
+ * - the merge itself is pure + unit-tested (src/install.ts),
10
+ * - an unparseable settings file aborts rather than being overwritten,
11
+ * - every write is preceded by a timestamped backup,
12
+ * - `--dry-run` prints the plan and writes nothing.
13
+ *
14
+ * All file I/O lives here; `install.ts` stays pure.
15
+ */
16
+ import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
17
+ import { homedir } from "node:os";
18
+ import { dirname, join } from "node:path";
19
+ import { createInterface } from "node:readline/promises";
20
+ import { stringify as yamlStringify } from "yaml";
21
+ import { addHooks, removeHooks } from "./install.js";
22
+ import { buildCatalog } from "./catalog.js";
23
+ import { loadConfig } from "./config.js";
24
+ import { makeAnsi } from "./ansi.js";
25
+ const PROVIDERS = ["anthropic", "openai", "ollama"];
26
+ const DEFAULT_KEY_ENV = {
27
+ anthropic: "ANTHROPIC_API_KEY",
28
+ openai: "OPENAI_API_KEY",
29
+ ollama: undefined,
30
+ };
31
+ /** A backup path next to `path`, stamped so successive runs never collide. */
32
+ export function backupPath(path, stamp) {
33
+ return `${path}.bak-${stamp}`;
34
+ }
35
+ function stampNow() {
36
+ return new Date()
37
+ .toISOString()
38
+ .replace(/[:.]/g, "-")
39
+ .replace(/-(\d{3})Z$/, "Z");
40
+ }
41
+ function defaultSettingsPath() {
42
+ return join(homedir(), ".claude", "settings.json");
43
+ }
44
+ function configYamlPath() {
45
+ return join(homedir(), ".config", "memhook", "config.yaml");
46
+ }
47
+ /**
48
+ * Build the minimal YAML config object for the chosen provider — only keys that
49
+ * differ from the built-in defaults are emitted, so the file stays small and
50
+ * the anthropic-default install writes no config at all.
51
+ */
52
+ export function buildConfigObject(opts) {
53
+ const provider = {};
54
+ if (opts.provider !== "anthropic")
55
+ provider["type"] = opts.provider;
56
+ if (opts.model)
57
+ provider["model"] = opts.model;
58
+ if (opts.apiKeyEnv && opts.apiKeyEnv !== DEFAULT_KEY_ENV[opts.provider]) {
59
+ provider["apiKeyEnv"] = opts.apiKeyEnv;
60
+ }
61
+ return Object.keys(provider).length > 0 ? { provider } : null;
62
+ }
63
+ function makeIo(env) {
64
+ const ansi = makeAnsi({ isTTY: Boolean(process.stdout.isTTY), env });
65
+ return { out: (s) => process.stdout.write(s + "\n"), ansi };
66
+ }
67
+ /**
68
+ * Read + JSON-parse settings; returns `{}` for a missing/empty file, throws for
69
+ * invalid JSON. Reads-then-handles-ENOENT rather than checking existence first,
70
+ * which avoids a check-then-use race (CodeQL js/file-system-race).
71
+ */
72
+ function readSettings(path) {
73
+ let text;
74
+ try {
75
+ text = readFileSync(path, "utf8");
76
+ }
77
+ catch (err) {
78
+ if (err.code === "ENOENT")
79
+ return {};
80
+ throw err;
81
+ }
82
+ if (text.trim() === "")
83
+ return {};
84
+ return JSON.parse(text);
85
+ }
86
+ function writeJson(path, value) {
87
+ mkdirSync(dirname(path), { recursive: true });
88
+ writeFileSync(path, JSON.stringify(value, null, 2) + "\n", "utf8");
89
+ }
90
+ /**
91
+ * Copy `path` to its timestamped backup; returns false when there was nothing
92
+ * to back up (file absent). Copy-then-handle-ENOENT — not an `existsSync`
93
+ * pre-check — avoids a check-then-use race and is robust if the file vanishes.
94
+ */
95
+ function backupFile(path, stamp) {
96
+ try {
97
+ copyFileSync(path, backupPath(path, stamp));
98
+ return true;
99
+ }
100
+ catch (err) {
101
+ if (err.code === "ENOENT")
102
+ return false;
103
+ throw err;
104
+ }
105
+ }
106
+ // ── memhook init ──────────────────────────────────────────────────────────
107
+ export async function runInit(opts, env = process.env) {
108
+ const io = makeIo(env);
109
+ const { ansi } = io;
110
+ const settingsPath = opts.settingsPath ?? defaultSettingsPath();
111
+ const interactive = !opts.yes && Boolean(process.stdin.isTTY) && !opts.dryRun;
112
+ io.out(ansi.bold("memhook init") + ansi.dim(" — wire memhook into Claude Code\n"));
113
+ // 1. Provider / key / model — flags win, then prompts, then defaults.
114
+ let provider = opts.provider ?? "anthropic";
115
+ const model = opts.model;
116
+ let apiKeyEnv = opts.apiKeyEnv;
117
+ if (interactive) {
118
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
119
+ try {
120
+ const p = (await rl.question(`Provider ${ansi.dim("[anthropic]")} / openai / ollama: `))
121
+ .trim()
122
+ .toLowerCase();
123
+ if (p && PROVIDERS.includes(p))
124
+ provider = p;
125
+ if (provider !== "ollama") {
126
+ const defKey = DEFAULT_KEY_ENV[provider];
127
+ const k = (await rl.question(`API key env var ${ansi.dim(`[${defKey}]`)}: `)).trim();
128
+ apiKeyEnv = k || apiKeyEnv || defKey;
129
+ }
130
+ }
131
+ finally {
132
+ rl.close();
133
+ }
134
+ }
135
+ else {
136
+ apiKeyEnv = apiKeyEnv ?? DEFAULT_KEY_ENV[provider];
137
+ }
138
+ // 2. Compute the settings.json merge (pure).
139
+ let existing;
140
+ try {
141
+ existing = readSettings(settingsPath);
142
+ }
143
+ catch {
144
+ io.out(ansi.red("✗ ") +
145
+ `${settingsPath} is not valid JSON. Refusing to overwrite it.\n` +
146
+ ansi.dim(" Fix or move the file, then re-run `memhook init`."));
147
+ return 1;
148
+ }
149
+ const merge = addHooks(existing, opts.bin);
150
+ const configObj = buildConfigObject({ provider, model, apiKeyEnv });
151
+ // 3. Plan summary.
152
+ io.out(ansi.bold("\nPlan"));
153
+ if (merge.added.length > 0) {
154
+ io.out(` ${ansi.green("+")} hook ${merge.added.join(" + ")} → ${settingsPath}`);
155
+ io.out(` ${ansi.dim(`backup → ${backupPath(settingsPath, "<timestamp>")}`)}`);
156
+ }
157
+ for (const ev of merge.alreadyPresent) {
158
+ io.out(` ${ansi.dim("·")} hook ${ev} already wired ${ansi.dim("(skip)")}`);
159
+ }
160
+ if (configObj) {
161
+ io.out(` ${ansi.green("+")} config → ${configYamlPath()} ${ansi.dim(`(provider: ${provider})`)}`);
162
+ }
163
+ else {
164
+ io.out(` ${ansi.dim("·")} provider anthropic (default) ${ansi.dim("— no config file needed")}`);
165
+ }
166
+ if (!opts.noCatalog)
167
+ io.out(` ${ansi.green("+")} build catalog`);
168
+ // 4. API-key heads-up (never blocks; just warns).
169
+ if (provider !== "ollama" && apiKeyEnv && !env[apiKeyEnv]) {
170
+ io.out(`\n ${ansi.yellow("!")} ${apiKeyEnv} is not set in this shell — ` +
171
+ ansi.dim(`export it before memhook can route (the hook fails soft until then).`));
172
+ }
173
+ if (opts.dryRun) {
174
+ io.out(ansi.dim("\n(dry run — nothing written)"));
175
+ return 0;
176
+ }
177
+ if (interactive) {
178
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
179
+ try {
180
+ const go = (await rl.question(`\n${ansi.bold("Proceed?")} ${ansi.dim("[Y/n]")} `))
181
+ .trim()
182
+ .toLowerCase();
183
+ if (go === "n" || go === "no") {
184
+ io.out(ansi.dim("Aborted. Nothing written."));
185
+ return 0;
186
+ }
187
+ }
188
+ finally {
189
+ rl.close();
190
+ }
191
+ }
192
+ // 5. Write (settings first, with backup).
193
+ const stamp = stampNow();
194
+ if (merge.added.length > 0) {
195
+ backupFile(settingsPath, stamp);
196
+ writeJson(settingsPath, merge.settings);
197
+ io.out(`${ansi.green("✓")} wired ${merge.added.join(" + ")} into ${settingsPath}`);
198
+ }
199
+ if (configObj) {
200
+ const cfgPath = configYamlPath();
201
+ backupFile(cfgPath, stamp);
202
+ mkdirSync(dirname(cfgPath), { recursive: true });
203
+ writeFileSync(cfgPath, yamlStringify(configObj), "utf8");
204
+ io.out(`${ansi.green("✓")} wrote ${cfgPath}`);
205
+ }
206
+ // 6. Bootstrap memory dirs so build-catalog + the router have somewhere to look.
207
+ for (const d of [join(homedir(), ".claude", "rules"), join(homedir(), ".claude", "projects")]) {
208
+ mkdirSync(d, { recursive: true });
209
+ }
210
+ // 7. Seed the catalog.
211
+ if (!opts.noCatalog) {
212
+ try {
213
+ const config = loadConfig(env);
214
+ const res = buildCatalog({ cwd: process.cwd(), outputPath: config.catalog.path });
215
+ io.out(`${ansi.green("✓")} catalog ${config.catalog.path} ${ansi.dim(`(${res.lines}L)`)}`);
216
+ }
217
+ catch {
218
+ io.out(ansi.yellow("! ") + "catalog build skipped (run `memhook build-catalog` later)");
219
+ }
220
+ }
221
+ io.out(`\n${ansi.green("Done.")} Restart Claude Code, then watch it live with ` +
222
+ ansi.bold("memhook tail") +
223
+ ".");
224
+ return 0;
225
+ }
226
+ // ── memhook uninstall ───────────────────────────────────────────────────────
227
+ export async function runUninstall(opts, env = process.env) {
228
+ const io = makeIo(env);
229
+ const { ansi } = io;
230
+ const settingsPath = opts.settingsPath ?? defaultSettingsPath();
231
+ const interactive = !opts.yes && Boolean(process.stdin.isTTY) && !opts.dryRun;
232
+ io.out(ansi.bold("memhook uninstall") + ansi.dim(" — remove memhook hooks\n"));
233
+ let existing;
234
+ try {
235
+ existing = readSettings(settingsPath);
236
+ }
237
+ catch {
238
+ io.out(ansi.red("✗ ") + `${settingsPath} is not valid JSON. Refusing to touch it.`);
239
+ return 1;
240
+ }
241
+ const result = removeHooks(existing);
242
+ if (result.removed === 0) {
243
+ io.out(ansi.dim("No memhook hooks found. Nothing to do."));
244
+ return 0;
245
+ }
246
+ io.out(ansi.bold("Plan"));
247
+ io.out(` ${ansi.red("-")} ${result.removed} memhook hook(s) from ${result.removedEvents.join(" + ")}`);
248
+ io.out(` ${ansi.dim(`backup → ${backupPath(settingsPath, "<timestamp>")}`)}`);
249
+ if (opts.purge)
250
+ io.out(` ${ansi.red("-")} purge cache + log`);
251
+ if (opts.dryRun) {
252
+ io.out(ansi.dim("\n(dry run — nothing written)"));
253
+ return 0;
254
+ }
255
+ if (interactive) {
256
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
257
+ try {
258
+ const go = (await rl.question(`\n${ansi.bold("Proceed?")} ${ansi.dim("[y/N]")} `))
259
+ .trim()
260
+ .toLowerCase();
261
+ if (go !== "y" && go !== "yes") {
262
+ io.out(ansi.dim("Aborted. Nothing written."));
263
+ return 0;
264
+ }
265
+ }
266
+ finally {
267
+ rl.close();
268
+ }
269
+ }
270
+ const stamp = stampNow();
271
+ backupFile(settingsPath, stamp);
272
+ writeJson(settingsPath, result.settings);
273
+ io.out(`${ansi.green("✓")} removed ${result.removed} hook(s) from ${settingsPath}`);
274
+ if (opts.purge) {
275
+ const config = loadConfig(env);
276
+ for (const target of [config.cache.dir, config.logging.jsonlPath]) {
277
+ io.out(ansi.dim(` (left in place: ${target} — remove manually if desired)`));
278
+ }
279
+ }
280
+ io.out(`\n${ansi.green("Done.")} Restart Claude Code to drop the hooks.`);
281
+ return 0;
282
+ }
283
+ //# sourceMappingURL=init.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.js","sourceRoot":"","sources":["../../src/init.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC/E,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,SAAS,IAAI,aAAa,EAAE,MAAM,MAAM,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAiB,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAqB,MAAM,aAAa,CAAC;AAC5D,OAAO,EA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E,CAAC;gBAC/B,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC,CAAC;gBAC9C,OAAO,CAAC,CAAC;YACX,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,EAAE,CAAC,KAAK,EAAE,CAAC;QACb,CAAC;IACH,CAAC;IAED,MAAM,KAAK,GAAG,QAAQ,EAAE,CAAC;IACzB,UAAU,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;IAChC,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC;IACzC,EAAE,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,YAAY,MAAM,CAAC,OAAO,iBAAiB,YAAY,EAAE,CAAC,CAAC;IAEpF,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,MAAM,MAAM,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;QAC/B,KAAK,MAAM,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;YAClE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,qBAAqB,MAAM,gCAAgC,CAAC,CAAC,CAAC;QAChF,CAAC;IACH,CAAC;IAED,EAAE,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,yCAAyC,CAAC,CAAC;IAC1E,OAAO,CAAC,CAAC;AACX,CAAC"}
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Pure settings.json hook wiring — the dangerous-but-testable core of
3
+ * `memhook init` / `memhook uninstall`.
4
+ *
5
+ * Everything here is a pure data transform: it takes the parsed contents of
6
+ * `~/.claude/settings.json` and returns a NEW object with memhook's hooks
7
+ * added or removed. There is NO file I/O in this module — the orchestration
8
+ * layer (`src/init.ts`) handles reading, backing up, and writing. Keeping the
9
+ * merge pure means the idempotency / non-clobbering guarantees are unit-tested
10
+ * without touching anyone's real config (the one file we must never corrupt).
11
+ *
12
+ * The Claude Code hook shape (sourced from the README + docs/SPECIFICATION.md
13
+ * §10) is:
14
+ *
15
+ * {
16
+ * "hooks": {
17
+ * "UserPromptSubmit": [ { "hooks": [ { "type": "command", "command": "memhook run" } ] } ],
18
+ * "SessionStart": [ { "hooks": [ { "type": "command", "command": "memhook build-catalog" } ] } ]
19
+ * }
20
+ * }
21
+ *
22
+ * Each event maps to an array of matcher-groups; each group has a `hooks` array
23
+ * of `{ type, command }`. memhook contributes one group per event and leaves
24
+ * every other key — and every other user hook — untouched.
25
+ */
26
+ /** The two events memhook wires, paired with the subcommand each runs. */
27
+ export declare const MEMHOOK_HOOKS: readonly [{
28
+ readonly event: "UserPromptSubmit";
29
+ readonly subcommand: "run";
30
+ }, {
31
+ readonly event: "SessionStart";
32
+ readonly subcommand: "build-catalog";
33
+ }];
34
+ export type HookEvent = (typeof MEMHOOK_HOOKS)[number]["event"];
35
+ export interface HookCommand {
36
+ type: "command";
37
+ command: string;
38
+ [key: string]: unknown;
39
+ }
40
+ export interface HookGroup {
41
+ hooks?: HookCommand[];
42
+ [key: string]: unknown;
43
+ }
44
+ export interface Settings {
45
+ hooks?: Record<string, HookGroup[]>;
46
+ [key: string]: unknown;
47
+ }
48
+ export interface AddResult {
49
+ settings: Settings;
50
+ /** Events where a memhook hook was newly added. */
51
+ added: HookEvent[];
52
+ /** Events where a memhook hook was already present (idempotent no-op). */
53
+ alreadyPresent: HookEvent[];
54
+ }
55
+ export interface RemoveResult {
56
+ settings: Settings;
57
+ /** Number of individual memhook hook commands removed. */
58
+ removed: number;
59
+ /** Event names a memhook hook was removed from. */
60
+ removedEvents: string[];
61
+ }
62
+ /**
63
+ * If `command` invokes the memhook binary, return its subcommand
64
+ * (e.g. "run", "build-catalog"), else null. Matches the README form
65
+ * (`memhook run`), an absolute-path form (`/usr/local/bin/memhook run`), and a
66
+ * node form (`node dist/bin/memhook.js run`). Detection is binary-name based
67
+ * (not the configured invocation), so `uninstall` cleans up a hook regardless
68
+ * of how it was originally written.
69
+ */
70
+ export declare function memhookSubcommand(command: unknown): string | null;
71
+ /**
72
+ * Return a deep clone with memhook's hooks added. Idempotent: an event that
73
+ * already has a memhook hook for its subcommand is left untouched. Every other
74
+ * key and every other user hook is preserved exactly.
75
+ *
76
+ * @param input parsed settings.json (non-object input is treated as `{}`)
77
+ * @param bin the command used to invoke memhook (default `"memhook"`)
78
+ */
79
+ export declare function addHooks(input: unknown, bin?: string): AddResult;
80
+ /**
81
+ * Return a deep clone with every memhook hook removed. Scans ALL hook events
82
+ * (not just the two memhook registers) so a hook moved by hand is still cleaned
83
+ * up. Empties that result — a group whose `hooks` becomes empty, an event whose
84
+ * group list becomes empty — are pruned so no dangling shells are left behind.
85
+ */
86
+ export declare function removeHooks(input: unknown): RemoveResult;
87
+ //# sourceMappingURL=install.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"install.d.ts","sourceRoot":"","sources":["../../src/install.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,0EAA0E;AAC1E,eAAO,MAAM,aAAa;;;;;;EAGhB,CAAC;AAEX,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC;AAEhE,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,SAAS,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,CAAC,EAAE,WAAW,EAAE,CAAC;IACtB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,QAAQ;IACvB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;IACpC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,SAAS;IACxB,QAAQ,EAAE,QAAQ,CAAC;IACnB,mDAAmD;IACnD,KAAK,EAAE,SAAS,EAAE,CAAC;IACnB,0EAA0E;IAC1E,cAAc,EAAE,SAAS,EAAE,CAAC;CAC7B;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,QAAQ,CAAC;IACnB,0DAA0D;IAC1D,OAAO,EAAE,MAAM,CAAC;IAChB,mDAAmD;IACnD,aAAa,EAAE,MAAM,EAAE,CAAC;CACzB;AAMD;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAIjE;AAQD;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,SAAY,GAAG,SAAS,CAmBnE;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY,CAqCxD"}
@@ -0,0 +1,124 @@
1
+ /**
2
+ * Pure settings.json hook wiring — the dangerous-but-testable core of
3
+ * `memhook init` / `memhook uninstall`.
4
+ *
5
+ * Everything here is a pure data transform: it takes the parsed contents of
6
+ * `~/.claude/settings.json` and returns a NEW object with memhook's hooks
7
+ * added or removed. There is NO file I/O in this module — the orchestration
8
+ * layer (`src/init.ts`) handles reading, backing up, and writing. Keeping the
9
+ * merge pure means the idempotency / non-clobbering guarantees are unit-tested
10
+ * without touching anyone's real config (the one file we must never corrupt).
11
+ *
12
+ * The Claude Code hook shape (sourced from the README + docs/SPECIFICATION.md
13
+ * §10) is:
14
+ *
15
+ * {
16
+ * "hooks": {
17
+ * "UserPromptSubmit": [ { "hooks": [ { "type": "command", "command": "memhook run" } ] } ],
18
+ * "SessionStart": [ { "hooks": [ { "type": "command", "command": "memhook build-catalog" } ] } ]
19
+ * }
20
+ * }
21
+ *
22
+ * Each event maps to an array of matcher-groups; each group has a `hooks` array
23
+ * of `{ type, command }`. memhook contributes one group per event and leaves
24
+ * every other key — and every other user hook — untouched.
25
+ */
26
+ /** The two events memhook wires, paired with the subcommand each runs. */
27
+ export const MEMHOOK_HOOKS = [
28
+ { event: "UserPromptSubmit", subcommand: "run" },
29
+ { event: "SessionStart", subcommand: "build-catalog" },
30
+ ];
31
+ function isPlainObject(v) {
32
+ return typeof v === "object" && v !== null && !Array.isArray(v);
33
+ }
34
+ /**
35
+ * If `command` invokes the memhook binary, return its subcommand
36
+ * (e.g. "run", "build-catalog"), else null. Matches the README form
37
+ * (`memhook run`), an absolute-path form (`/usr/local/bin/memhook run`), and a
38
+ * node form (`node dist/bin/memhook.js run`). Detection is binary-name based
39
+ * (not the configured invocation), so `uninstall` cleans up a hook regardless
40
+ * of how it was originally written.
41
+ */
42
+ export function memhookSubcommand(command) {
43
+ if (typeof command !== "string")
44
+ return null;
45
+ const m = command.match(/(?:^|[\s/\\])memhook(?:\.[cm]?js)?["']?\s+([a-z][a-z-]*)/i);
46
+ return m?.[1] ? m[1].toLowerCase() : null;
47
+ }
48
+ /** True if `group` contains a memhook hook for `subcommand`. */
49
+ function groupHasMemhook(group, subcommand) {
50
+ if (!Array.isArray(group.hooks))
51
+ return false;
52
+ return group.hooks.some((h) => memhookSubcommand(h?.command) === subcommand);
53
+ }
54
+ /**
55
+ * Return a deep clone with memhook's hooks added. Idempotent: an event that
56
+ * already has a memhook hook for its subcommand is left untouched. Every other
57
+ * key and every other user hook is preserved exactly.
58
+ *
59
+ * @param input parsed settings.json (non-object input is treated as `{}`)
60
+ * @param bin the command used to invoke memhook (default `"memhook"`)
61
+ */
62
+ export function addHooks(input, bin = "memhook") {
63
+ const settings = isPlainObject(input) ? structuredClone(input) : {};
64
+ if (!isPlainObject(settings.hooks))
65
+ settings.hooks = {};
66
+ const hooks = settings.hooks;
67
+ const added = [];
68
+ const alreadyPresent = [];
69
+ for (const { event, subcommand } of MEMHOOK_HOOKS) {
70
+ const list = Array.isArray(hooks[event]) ? hooks[event] : (hooks[event] = []);
71
+ if (list.some((g) => isPlainObject(g) && groupHasMemhook(g, subcommand))) {
72
+ alreadyPresent.push(event);
73
+ continue;
74
+ }
75
+ list.push({ hooks: [{ type: "command", command: `${bin} ${subcommand}` }] });
76
+ added.push(event);
77
+ }
78
+ return { settings, added, alreadyPresent };
79
+ }
80
+ /**
81
+ * Return a deep clone with every memhook hook removed. Scans ALL hook events
82
+ * (not just the two memhook registers) so a hook moved by hand is still cleaned
83
+ * up. Empties that result — a group whose `hooks` becomes empty, an event whose
84
+ * group list becomes empty — are pruned so no dangling shells are left behind.
85
+ */
86
+ export function removeHooks(input) {
87
+ const settings = isPlainObject(input) ? structuredClone(input) : {};
88
+ let removed = 0;
89
+ const removedEvents = [];
90
+ if (!isPlainObject(settings.hooks))
91
+ return { settings, removed, removedEvents };
92
+ const hooks = settings.hooks;
93
+ for (const event of Object.keys(hooks)) {
94
+ const list = hooks[event];
95
+ if (!Array.isArray(list))
96
+ continue;
97
+ let removedHere = 0;
98
+ const kept = [];
99
+ for (const group of list) {
100
+ if (!isPlainObject(group) || !Array.isArray(group.hooks)) {
101
+ kept.push(group);
102
+ continue;
103
+ }
104
+ const before = group.hooks.length;
105
+ group.hooks = group.hooks.filter((h) => memhookSubcommand(h?.command) === null);
106
+ removedHere += before - group.hooks.length;
107
+ // Drop a group only if WE emptied it (a group that was already empty, or
108
+ // empty for other reasons, is preserved untouched).
109
+ if (group.hooks.length > 0 || before === 0)
110
+ kept.push(group);
111
+ }
112
+ if (removedHere > 0) {
113
+ removed += removedHere;
114
+ removedEvents.push(event);
115
+ if (kept.length > 0)
116
+ hooks[event] = kept;
117
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
118
+ else
119
+ delete hooks[event];
120
+ }
121
+ }
122
+ return { settings, removed, removedEvents };
123
+ }
124
+ //# sourceMappingURL=install.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"install.js","sourceRoot":"","sources":["../../src/install.ts"],"names":[],"mappings":"AAAA;;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,IAAI,CAAC,KAAK,CAAC,CAAC;YAC1B,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;gBAAE,KAAK,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;YACzC,gEAAgE;;gBAC3D,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;IAED,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,aAAa,EAAE,CAAC;AAC9C,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/router.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAkBH,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,UAAU;IACzB,kBAAkB,EAAE;QAClB,aAAa,EAAE,kBAAkB,CAAC;QAClC,iBAAiB,EAAE,MAAM,CAAC;KAC3B,CAAC;CACH;AAuBD,wBAAsB,KAAK,CACzB,SAAS,EAAE,MAAM,EACjB,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,OAAO,CAAC,UAAU,CAAC,CA0JrB"}
1
+ {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/router.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAoBH,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,UAAU;IACzB,kBAAkB,EAAE;QAClB,aAAa,EAAE,kBAAkB,CAAC;QAClC,iBAAiB,EAAE,MAAM,CAAC;KAC3B,CAAC;CACH;AAuBD,wBAAsB,KAAK,CACzB,SAAS,EAAE,MAAM,EACjB,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,OAAO,CAAC,UAAU,CAAC,CAyKrB"}
@@ -18,7 +18,7 @@
18
18
  * Fail-soft: every error path falls back to empty additionalContext.
19
19
  * Never blocks Claude Code.
20
20
  */
21
- import { existsSync, readFileSync, statSync, appendFileSync, mkdirSync, readdirSync, } from "node:fs";
21
+ import { existsSync, readFileSync, openSync, fstatSync, closeSync, appendFileSync, mkdirSync, readdirSync, } from "node:fs";
22
22
  import { join, dirname, basename } from "node:path";
23
23
  import { LocalCache } from "./cache.js";
24
24
  import { loadConfig } from "./config.js";
@@ -52,7 +52,26 @@ export async function route(stdinJson, env = process.env) {
52
52
  logEntry(config, baseLog(input.prompt, "pre_filter_skip"));
53
53
  return EMPTY;
54
54
  }
55
- if (!existsSync(config.catalog.path)) {
55
+ // Read the catalog through a single fd: `fstat` gives the cache-key mtime and
56
+ // we read the content from the same handle. Using one open fd — instead of
57
+ // `existsSync`/`statSync` then `readFileSync` on the path — closes a
58
+ // check-then-use window (CodeQL js/file-system-race). A missing or unreadable
59
+ // catalog falls through to `no_catalog` (fail-soft). The content is only used
60
+ // on a cache miss, but reading it here (a small index file) keeps the catalog
61
+ // access to one race-free handle.
62
+ let catalogContent;
63
+ let catalogMtimeMs;
64
+ try {
65
+ const fd = openSync(config.catalog.path, "r");
66
+ try {
67
+ catalogMtimeMs = fstatSync(fd).mtimeMs;
68
+ catalogContent = readFileSync(fd, "utf8");
69
+ }
70
+ finally {
71
+ closeSync(fd);
72
+ }
73
+ }
74
+ catch {
56
75
  logEntry(config, baseLog(input.prompt, "no_catalog"));
57
76
  return EMPTY;
58
77
  }
@@ -63,12 +82,11 @@ export async function route(stdinJson, env = process.env) {
63
82
  logEntry(config, baseLog(input.prompt, "no_api_key"));
64
83
  return EMPTY;
65
84
  }
66
- const catalogStat = statSync(config.catalog.path);
67
85
  const cache = new LocalCache(config.cache.dir, config.cache.ttlMin, config.cache.evictionDays);
68
86
  const cacheKey = config.cache.enabled
69
87
  ? cache.key({
70
88
  prompt: input.prompt,
71
- catalogMtimeMs: catalogStat.mtimeMs,
89
+ catalogMtimeMs,
72
90
  cwd,
73
91
  scriptVersion: config.scriptVersion,
74
92
  provider: `${config.provider.type}:${config.provider.model}`,
@@ -93,7 +111,6 @@ export async function route(stdinJson, env = process.env) {
93
111
  }
94
112
  }
95
113
  if (!fromCache) {
96
- const catalogContent = readFileSync(config.catalog.path, "utf8");
97
114
  const systemPrompt = buildSystemPrompt(catalogContent);
98
115
  // Provider construction can throw on bad config; the constructor `throw`s
99
116
  // are reachable from the hook, so they MUST be caught here (fail-soft).
@@ -252,10 +269,16 @@ function readSelected(basenames, cwd, config) {
252
269
  seen.push(name);
253
270
  for (const dir of dirs) {
254
271
  const file = join(dir, name);
255
- if (!existsSync(file))
272
+ // Read directly and treat a read failure as "not in this dir" — no
273
+ // existsSync precheck, so no check-then-use race (CodeQL js/file-system-race).
274
+ let content;
275
+ try {
276
+ content = readFileSync(file, "utf8");
277
+ }
278
+ catch {
256
279
  continue;
280
+ }
257
281
  // Cap-A1 projection check — pre-injection, allow ≥1 file always.
258
- const content = readFileSync(file, "utf8");
259
282
  const projected = additional.length + content.length + 64;
260
283
  if (injected > 0 && projected > config.selection.maxAdditionalChars) {
261
284
  return { additional, injected, allBasenames: seen };
@@ -329,6 +352,10 @@ function logEntry(config, entry) {
329
352
  additional_size_chars: entry.additionalSizeChars,
330
353
  additional_size_tokens_est: entry.additionalSizeTokensEst,
331
354
  status: entry.status,
355
+ // Additive field (v0.3) — the model that handled this turn. Read by
356
+ // `memhook tail`. The frozen log schema permits adding fields; never
357
+ // rename/remove existing ones (docs/SPECIFICATION.md §14).
358
+ model: config.provider.model,
332
359
  });
333
360
  appendFileSync(config.logging.jsonlPath, line + "\n", "utf8");
334
361
  }