Skip to content

Commit 65faaa7

Browse files
committed
Start hooks through an .mjs launcher that enables Node's compile cache
- Each registered hook runs a small .mjs that calls enableCompileCache and imports its .mts, cutting ~30 ms of type stripping per start - The .mts modules install as plain file components; a contract test keeps every node registration on .mjs
1 parent bae9229 commit 65faaa7

7 files changed

Lines changed: 64 additions & 21 deletions

File tree

‎CLAUDE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The consequence that matters most: **nothing here executes from the repo.** Edit
2525

2626
**Run the suite twice when touching state handling** — CI does (`.github/workflows/ci.yml`). The gate's barrier is scoped by a monotonic turn counter rather than wall-clock time.
2727

28-
Node 22.6+ runs `.mts` files directly with `--experimental-strip-types --disable-warning=ExperimentalWarning`. `.mts` keeps the module format independent of the target project's `package.json`. Development dependencies are only for typechecking; hooks import Node built-ins and `hooks/shared.mts`. MCS installs that library as `hooks/memory/shared.mts` beside the entry points. `hooks/memory-loop-activator.sh` stays in shell because it only prints a static reminder on every prompt; its output is covered by the same contract fixtures.
28+
Node 22.6+ runs `.mts` files directly with `--experimental-strip-types --disable-warning=ExperimentalWarning`. `.mts` keeps the module format independent of the target project's `package.json`. Development dependencies are only for typechecking; hooks import Node built-ins and `hooks/shared.mts`. MCS installs that library as `hooks/memory/shared.mts` beside the entry points. Every registered hook is an `.mjs` launcher (`hooks/kb-gate.mjs`, `hooks/sync-memories.mjs`) that calls `module.enableCompileCache()` and then imports its `.mts`, which is installed as a plain file component. Type stripping costs about 30 ms per start whatever the file's size, so a registered `.mts`, however small, pays it every time; the cache keeps the stripped module until its content changes. A contract test asserts every node registration is `.mjs`. `hooks/memory-loop-activator.sh` stays in shell because it only prints a static reminder on every prompt; its output is covered by the same contract fixtures.
2929

3030
Two harness details are load-bearing rather than incidental:
3131

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ memory/
156156
├── techpack.yaml # Manifest — defines all components
157157
├── config/settings.json # Disables built-in auto-memory
158158
├── hooks/
159+
│ ├── sync-memories.mjs # Launcher: enables Node's compile cache, then loads the .mts
159160
│ ├── sync-memories.mts # Memory indexing/reindexing
160161
│ ├── memory-loop-activator.sh # Knowledge extraction reminder
162+
│ ├── kb-gate.mjs # Launcher: enables Node's compile cache, then loads the .mts
161163
│ ├── kb-gate.mts # Keeps KB lookups ahead of delegated discovery
162164
│ └── shared.mts # Shared filesystem and project-path helpers
163165
├── scripts/ # Maintainer checks

‎biome.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"files": {
44
"includes": [
55
"hooks/**/*.mts",
6+
"hooks/**/*.mjs",
67
"scripts/**/*.mts",
78
"tests/**/*.mts",
89
"tests/fixtures/*.json",

‎hooks/kb-gate.mjs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env -S node --experimental-strip-types --disable-warning=ExperimentalWarning
2+
import { enableCompileCache } from "node:module";
3+
4+
// Stripping types costs about 30 ms on every start, and only plain JS runs before the cache is on, so
5+
// this launcher stays .mjs and the cache keeps the stripped module until it changes.
6+
enableCompileCache();
7+
await import("./kb-gate.mts");

‎hooks/sync-memories.mjs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env -S node --experimental-strip-types --disable-warning=ExperimentalWarning
2+
import { enableCompileCache } from "node:module";
3+
4+
// Stripping types costs about 30 ms on every start, and only plain JS runs before the cache is on, so
5+
// this launcher stays .mjs and the cache keeps the stripped module until it changes.
6+
enableCompileCache();
7+
await import("./sync-memories.mts");

‎techpack.yaml‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ components:
218218
source: hooks/shared.mts
219219
destination: shared.mts
220220

221+
# Registered hooks run a small .mjs launcher that turns on Node's compile cache before
222+
# importing these, so the TypeScript is stripped once instead of on every start.
223+
- id: hook-sync-memories-module
224+
displayName: Sync memories module
225+
description: Indexing logic loaded by the sync memories launcher
226+
hook:
227+
source: hooks/sync-memories.mts
228+
destination: sync-memories.mts
229+
230+
- id: hook-kb-gate-module
231+
displayName: KB gate module
232+
description: KB gate logic loaded by the KB gate launchers
233+
hook:
234+
source: hooks/kb-gate.mts
235+
destination: kb-gate.mts
236+
221237
- id: hook-continuous-learning
222238
displayName: Memory loop activator
223239
description: Reminds to evaluate learnings on each prompt
@@ -235,8 +251,8 @@ components:
235251
hookTimeout: 120
236252
hookStatusMessage: "Indexing memories..."
237253
hook:
238-
source: hooks/sync-memories.mts
239-
destination: sync-memories.mts
254+
source: hooks/sync-memories.mjs
255+
destination: sync-memories.mjs
240256

241257
- id: hook-reindex-memories
242258
displayName: Reindex memories hook
@@ -247,8 +263,8 @@ components:
247263
hookTimeout: 120
248264
hookStatusMessage: "Reindexing memories..."
249265
hook:
250-
source: hooks/sync-memories.mts
251-
destination: sync-memories.mts
266+
source: hooks/sync-memories.mjs
267+
destination: sync-memories.mjs
252268

253269
# ── KB-first delegation ─────────────────────────────────────────────────
254270
# Several registrations of one dispatcher script, which branches on
@@ -267,8 +283,8 @@ components:
267283
hookInterpreter: node --experimental-strip-types --disable-warning=ExperimentalWarning
268284
hookEvent: UserPromptSubmit
269285
hook:
270-
source: hooks/kb-gate.mts
271-
destination: kb-gate.mts
286+
source: hooks/kb-gate.mjs
287+
destination: kb-gate.mjs
272288
# Checked because losing this one corrupts the gate rather than disabling it:
273289
# with no turn stamp the counter reads 0 forever, so every search ever made in
274290
# the session counts as fresh and the barrier silently widens from per-turn to
@@ -286,8 +302,8 @@ components:
286302
hookEvent: PostToolUse
287303
hookMatcher: "mcp__memory-loop__query|Skill"
288304
hook:
289-
source: hooks/kb-gate.mts
290-
destination: kb-gate.mts
305+
source: hooks/kb-gate.mjs
306+
destination: kb-gate.mjs
291307
# Same reasoning: without this, no search or skill is ever recorded, so the
292308
# gate is pure friction until the budget releases it.
293309
doctorChecks:
@@ -305,8 +321,8 @@ components:
305321
# builds. Matching only `Task` silently matches nothing.
306322
hookMatcher: "Agent|Task|Write|Edit|NotebookEdit|Bash"
307323
hook:
308-
source: hooks/kb-gate.mts
309-
destination: kb-gate.mts
324+
source: hooks/kb-gate.mjs
325+
destination: kb-gate.mjs
310326
doctorChecks:
311327
- type: hookEventExists
312328
name: "PreToolUse hook registered"
@@ -320,8 +336,8 @@ components:
320336
hookEvent: SessionStart
321337
hookMatcher: "compact"
322338
hook:
323-
source: hooks/kb-gate.mts
324-
destination: kb-gate.mts
339+
source: hooks/kb-gate.mjs
340+
destination: kb-gate.mjs
325341

326342
- id: hook-kb-gate-retrieval
327343
displayName: KB gate — record memory retrievals
@@ -332,17 +348,17 @@ components:
332348
# Every Read starts this hook, and it never needs to block, so it stays off the turn's critical path.
333349
hookAsync: true
334350
hook:
335-
source: hooks/kb-gate.mts
336-
destination: kb-usage.mts
351+
source: hooks/kb-gate.mjs
352+
destination: kb-usage.mjs
337353

338354
- id: hook-kb-subagent-brief
339355
displayName: Sub-agent KB brief
340356
description: Tells discovery sub-agents that the KB exists, so they search it instead of sweeping files
341357
hookInterpreter: node --experimental-strip-types --disable-warning=ExperimentalWarning
342358
hookEvent: SubagentStart
343359
hook:
344-
source: hooks/kb-gate.mts
345-
destination: kb-gate.mts
360+
source: hooks/kb-gate.mjs
361+
destination: kb-gate.mjs
346362
doctorChecks:
347363
- type: hookEventExists
348364
name: "SubagentStart hook registered"

‎tests/contracts.test.mts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,24 @@ for (const type of ["commonjs", "module"]) {
4040
assert.match(manifest, /hook:\n {6}source: hooks\/shared.mts\n {6}destination: shared.mts/);
4141
const installed = `${f.project}/.claude/hooks/memory`;
4242
mkdirSync(installed, { recursive: true });
43-
cpSync(`${root}/hooks/shared.mts`, `${installed}/shared.mts`);
43+
for (const module of ["shared", "kb-gate", "sync-memories"]) {
44+
assert.match(manifest, new RegExp(`hook:\\n {6}source: hooks/${module}.mts\\n {6}destination: ${module}.mts`));
45+
writeFileSync(
46+
`${installed}/${module}.mts`,
47+
readFileSync(`${root}/hooks/${module}.mts`, "utf8").replace("__KB_GATE_MODE__", "enforce"),
48+
);
49+
}
4450
for (const component of hooks) {
4551
const source = field(component, "source");
4652
const destination = field(component, "destination");
4753
const event = field(component, "hookEvent");
4854
if (destination === "memory-loop-activator.sh") assert.doesNotMatch(component, /hookInterpreter:/);
49-
else assert.match(component, /hookInterpreter: node --experimental-strip-types --disable-warning=ExperimentalWarning/);
50-
writeFileSync(`${installed}/${destination}`, readFileSync(`${root}/${source}`, "utf8").replace("__KB_GATE_MODE__", "enforce"));
55+
else {
56+
assert.match(component, /hookInterpreter: node --experimental-strip-types --disable-warning=ExperimentalWarning/);
57+
// A registered .mts would strip its types on every start, before any launcher could enable the cache.
58+
assert.match(destination, /\.mjs$/);
59+
}
60+
cpSync(`${root}/${source}`, `${installed}/${destination}`);
5161
const output = f.run(
5262
`${installed}/${destination}`,
5363
{
@@ -62,7 +72,7 @@ for (const type of ["commonjs", "module"]) {
6272
if (event === "SubagentStart") assert.equal(JSON.parse(output).hookSpecificOutput.hookEventName, event);
6373
}
6474
assert.match(manifest, /hookMatcher: "Agent\|Task\|Write\|Edit\|NotebookEdit\|Bash"/);
65-
assert.match(manifest, /hookEvent: SessionStart\n {4}hookMatcher: "compact"\n {4}hook:\n {6}source: hooks\/kb-gate.mts/);
75+
assert.match(manifest, /hookEvent: SessionStart\n {4}hookMatcher: "compact"\n {4}hook:\n {6}source: hooks\/kb-gate.mjs/);
6676
assert.match(manifest, /hookMatcher: "mcp__memory-loop__query\|Skill"/);
6777
assert.match(manifest, /hookMatcher: "mcp__memory-loop__get\|mcp__memory-loop__multi_get\|Read"/);
6878
assert.equal((manifest.match(/hookAsync: true/g) ?? []).length, 3);

0 commit comments

Comments
 (0)