hookloom 1.0.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-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/README.md +12 -0
- package/dist/{check-DiQUS1CB.mjs → check-CRZ6Rgdo.mjs} +3 -2
- package/dist/check-ySa9Ce1V.mjs +1 -0
- package/dist/cli.mjs +204 -22
- package/dist/command-line-OrJYMqHx.mjs +62 -0
- package/dist/dispatcher.mjs +34 -80
- package/dist/events-CHsq0Cl4.mjs +132 -0
- package/dist/examples/blocked-fetch.mjs +24 -0
- package/dist/examples/browser-close.mjs +106 -0
- package/dist/examples/dash-guard.mjs +907 -0
- package/dist/examples/default-branch-health.mjs +214 -0
- package/dist/examples/github-rate-limit.mjs +31 -0
- package/dist/examples/issue-comment-guard.mjs +157 -0
- package/dist/examples/private-reference-guard.mjs +69 -0
- package/dist/examples/repository-metadata.mjs +44 -0
- package/dist/files-CBn-xAo2.mjs +50 -0
- package/dist/hook-DIS0wk_5.mjs +51 -0
- package/dist/lock-8b6urUtv.mjs +22 -0
- package/dist/permission-rule-BVXyOtut.mjs +123 -0
- package/dist/private-references-CUZqASjK.mjs +254 -0
- package/dist/prompt-text-A_jEvJei.mjs +29 -0
- package/dist/schema-Dl9EJYyt.mjs +112 -0
- package/dist/{schema-DR7G3z1u.mjs → schema-EYih4WFo.mjs} +1 -1
- package/dist/{schema-78xAlSN_.mjs → schemas-B4KRDIno.mjs} +1 -239
- package/dist/{permission-rule-DXm-Jvfg.mjs → shell-4WSaabze.mjs} +12 -123
- package/dist/state-Db3zojdV.mjs +51 -0
- package/package.json +1 -1
- package/dist/check-C4-tOQXQ.mjs +0 -1
- package/dist/command-line-ByzwzoIt.mjs +0 -109
package/README.md
CHANGED
|
@@ -84,6 +84,18 @@ These stay native:
|
|
|
84
84
|
- `asyncRewake` hooks, and model switch hooks with a matcher;
|
|
85
85
|
- project, local and managed settings, and hooks written inline in Codex's `config.toml`.
|
|
86
86
|
|
|
87
|
+
## Examples
|
|
88
|
+
|
|
89
|
+
Some hooks ship with hookloom. None runs until you add it:
|
|
90
|
+
|
|
91
|
+
```sh
|
|
92
|
+
npx hookloom examples
|
|
93
|
+
npx hookloom examples add private-reference-guard default-branch-health
|
|
94
|
+
npx hookloom examples remove default-branch-health
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`add` writes ordinary registry entries, which you can edit. They name the installed package as `${HOOKLOOM_ROOT}` and the Node running the dispatcher as `${HOOKLOOM_NODE}`, which hookloom fills in when it runs a command, so they survive updates; your own hooks may use both names too.
|
|
98
|
+
|
|
87
99
|
## Checking an installation
|
|
88
100
|
|
|
89
101
|
```sh
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { r as permissionRuleProblem } from "./permission-rule-
|
|
2
|
-
import {
|
|
1
|
+
import { r as permissionRuleProblem } from "./permission-rule-BVXyOtut.mjs";
|
|
2
|
+
import { r as harnesses, t as eventContract } from "./events-CHsq0Cl4.mjs";
|
|
3
|
+
import { t as handlersOf } from "./schema-Dl9EJYyt.mjs";
|
|
3
4
|
// src/registry/check.ts
|
|
4
5
|
/**
|
|
5
6
|
* The rules a registry must meet for a harness to be able to run it, beyond its shape.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as checkRegistry } from "./check-CRZ6Rgdo.mjs";
|
package/dist/cli.mjs
CHANGED
|
@@ -2,34 +2,131 @@
|
|
|
2
2
|
import { isDeepStrictEqual, parseArgs } from "node:util";
|
|
3
3
|
import { accessSync, constants, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
4
4
|
import { delimiter, dirname, extname, isAbsolute, join, resolve } from "node:path";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { r as harnesses, t as eventContract } from "./events-CHsq0Cl4.mjs";
|
|
6
|
+
import { n as isRecord } from "./shell-4WSaabze.mjs";
|
|
7
|
+
import { a as registryPath, i as configurationDirectory, r as claudeDirectory, t as atomicWrite } from "./files-CBn-xAo2.mjs";
|
|
7
8
|
import { createHash } from "node:crypto";
|
|
8
|
-
import {
|
|
9
|
-
import { t as
|
|
9
|
+
import { t as checkRegistry } from "./check-CRZ6Rgdo.mjs";
|
|
10
|
+
import { c as number, d as strictObject, f as string, l as object, m as unknown, n as array, r as boolean, s as looseObject, t as _enum, u as record$1 } from "./schemas-B4KRDIno.mjs";
|
|
11
|
+
import { n as parseRegistry } from "./schema-Dl9EJYyt.mjs";
|
|
12
|
+
import { n as withLock } from "./lock-8b6urUtv.mjs";
|
|
10
13
|
import { execFile, spawn } from "node:child_process";
|
|
11
14
|
import { createInterface } from "node:readline";
|
|
12
15
|
import { homedir, tmpdir } from "node:os";
|
|
13
16
|
import { once } from "node:events";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
17
|
+
import { n as invocation, r as version, t as claudeUsesPowerShell } from "./command-line-OrJYMqHx.mjs";
|
|
18
|
+
// src/examples/catalogue.ts
|
|
19
|
+
const BOTH = ["claude", "codex"];
|
|
20
|
+
const LOCAL_SECONDS = 10;
|
|
21
|
+
const NETWORK_SECONDS = 90;
|
|
22
|
+
const examples = [
|
|
23
|
+
{
|
|
24
|
+
name: "blocked-fetch",
|
|
25
|
+
summary: "After a page fetch is refused or blocked, suggest trying a browser.",
|
|
26
|
+
needs: [],
|
|
27
|
+
registrations: [{
|
|
28
|
+
event: "PostToolUse",
|
|
29
|
+
harnesses: ["claude"],
|
|
30
|
+
matcher: "WebFetch|Agent",
|
|
31
|
+
timeout: LOCAL_SECONDS
|
|
32
|
+
}, {
|
|
33
|
+
event: "PostToolUse",
|
|
34
|
+
harnesses: BOTH,
|
|
35
|
+
matcher: "Bash",
|
|
36
|
+
commandPattern: "\\b(?:curl|wget|http|https)\\b",
|
|
37
|
+
timeout: LOCAL_SECONDS
|
|
38
|
+
}]
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: "github-rate-limit",
|
|
42
|
+
summary: "After gh is refused for a rate limit, point at the REST route.",
|
|
43
|
+
needs: [],
|
|
44
|
+
registrations: [{
|
|
45
|
+
event: "PostToolUse",
|
|
46
|
+
harnesses: BOTH,
|
|
47
|
+
matcher: "Bash",
|
|
48
|
+
commandPattern: "\\bgh\\b",
|
|
49
|
+
timeout: LOCAL_SECONDS
|
|
50
|
+
}]
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: "repository-metadata",
|
|
54
|
+
summary: "After a repository is created, forked, renamed or moved, ask for its description and topics.",
|
|
55
|
+
needs: [],
|
|
56
|
+
registrations: [{
|
|
57
|
+
event: "PostToolUse",
|
|
58
|
+
harnesses: BOTH,
|
|
59
|
+
matcher: "Bash",
|
|
60
|
+
commandPattern: "\\bgh\\s+(?:repo\\s+(?:create|fork|rename)|api\\b.*/transfer)\\b",
|
|
61
|
+
timeout: LOCAL_SECONDS
|
|
62
|
+
}]
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "browser-close",
|
|
66
|
+
summary: "At the end of a turn that left the Playwright browser open, ask for it to be closed.",
|
|
67
|
+
needs: [],
|
|
68
|
+
registrations: [{
|
|
69
|
+
event: "Stop",
|
|
70
|
+
harnesses: ["claude"],
|
|
71
|
+
timeout: LOCAL_SECONDS
|
|
72
|
+
}]
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: "dash-guard",
|
|
76
|
+
summary: "Refuse a change or published message that adds an em-dash, or a spaced double hyphen used as one.",
|
|
77
|
+
needs: [],
|
|
78
|
+
registrations: [{
|
|
79
|
+
event: "PreToolUse",
|
|
80
|
+
harnesses: ["claude"],
|
|
81
|
+
matcher: "Edit|Write|NotebookEdit|Bash",
|
|
82
|
+
timeout: LOCAL_SECONDS
|
|
83
|
+
}, {
|
|
84
|
+
event: "PreToolUse",
|
|
85
|
+
harnesses: ["codex"],
|
|
86
|
+
matcher: "Bash|apply_patch",
|
|
87
|
+
timeout: LOCAL_SECONDS
|
|
88
|
+
}]
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: "private-reference-guard",
|
|
92
|
+
summary: "Refuse to name a private GitHub repository in a public one.",
|
|
93
|
+
needs: ["git", "gh"],
|
|
94
|
+
registrations: [{
|
|
95
|
+
event: "PreToolUse",
|
|
96
|
+
harnesses: ["claude"],
|
|
97
|
+
matcher: "Edit|Write|NotebookEdit",
|
|
98
|
+
timeout: NETWORK_SECONDS
|
|
99
|
+
}, {
|
|
100
|
+
event: "PreToolUse",
|
|
101
|
+
harnesses: BOTH,
|
|
102
|
+
matcher: "Bash",
|
|
103
|
+
commandPattern: "\\b(?:gh|git)\\b",
|
|
104
|
+
timeout: NETWORK_SECONDS
|
|
105
|
+
}]
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
name: "issue-comment-guard",
|
|
109
|
+
summary: "Refuse a comment on your own issue that nobody else has joined: edit its body instead.",
|
|
110
|
+
needs: ["gh"],
|
|
111
|
+
registrations: [{
|
|
112
|
+
event: "PreToolUse",
|
|
113
|
+
harnesses: BOTH,
|
|
114
|
+
matcher: "Bash",
|
|
115
|
+
commandPattern: "\\bgh\\b",
|
|
116
|
+
timeout: NETWORK_SECONDS
|
|
117
|
+
}]
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
name: "default-branch-health",
|
|
121
|
+
summary: "On a prompt, warn when the default branch of the repository in use is red.",
|
|
122
|
+
needs: ["git", "gh"],
|
|
123
|
+
registrations: [{
|
|
124
|
+
event: "UserPromptSubmit",
|
|
125
|
+
harnesses: BOTH,
|
|
126
|
+
timeout: 15
|
|
127
|
+
}]
|
|
31
128
|
}
|
|
32
|
-
|
|
129
|
+
];
|
|
33
130
|
// src/migrate/plan.ts
|
|
34
131
|
/**
|
|
35
132
|
* Decides which native hooks the dispatcher can run exactly as the harness does, and which must stay where they are.
|
|
@@ -323,6 +420,67 @@ function rollback(locations, requested) {
|
|
|
323
420
|
};
|
|
324
421
|
});
|
|
325
422
|
}
|
|
423
|
+
// src/examples/install.ts
|
|
424
|
+
/**
|
|
425
|
+
* Adds an example hook to a registry, or takes it out. The groups written name the installed package through a placeholder, so they keep working across updates.
|
|
426
|
+
*/
|
|
427
|
+
function command(name, harness, windows) {
|
|
428
|
+
const script = `"\${HOOKLOOM_ROOT}/dist/examples/${name}.mjs" ${harness}`;
|
|
429
|
+
return windows ? `node ${script} ; exit $LASTEXITCODE` : `"\${HOOKLOOM_NODE}" ${script}`;
|
|
430
|
+
}
|
|
431
|
+
function groupsFor(name, registration, windows) {
|
|
432
|
+
const { harnesses, matcher, commandPattern, timeout } = registration;
|
|
433
|
+
return harnesses.map((harness) => ({
|
|
434
|
+
harnesses: [harness],
|
|
435
|
+
...matcher === void 0 ? {} : { matcher },
|
|
436
|
+
hooks: [{
|
|
437
|
+
type: "command",
|
|
438
|
+
command: command(name, harness, windows),
|
|
439
|
+
timeout,
|
|
440
|
+
...commandPattern === void 0 ? {} : { when: {
|
|
441
|
+
field: "command",
|
|
442
|
+
pattern: commandPattern
|
|
443
|
+
} }
|
|
444
|
+
}]
|
|
445
|
+
}));
|
|
446
|
+
}
|
|
447
|
+
function runs(group, name) {
|
|
448
|
+
const hooks = isRecord(group) ? group["hooks"] : void 0;
|
|
449
|
+
return Array.isArray(hooks) && hooks.some((hook) => isRecord(hook) && typeof hook["command"] === "string" && hook["command"].includes(`/dist/examples/${name}.mjs"`));
|
|
450
|
+
}
|
|
451
|
+
function without(events, name) {
|
|
452
|
+
return Object.fromEntries(Object.entries(events).flatMap(([event, groups]) => {
|
|
453
|
+
if (!Array.isArray(groups)) return [[event, groups]];
|
|
454
|
+
const kept = groups.filter((group) => !runs(group, name));
|
|
455
|
+
return kept.length === 0 ? [] : [[event, kept]];
|
|
456
|
+
}));
|
|
457
|
+
}
|
|
458
|
+
function listed(value) {
|
|
459
|
+
return Array.isArray(value) ? value : [];
|
|
460
|
+
}
|
|
461
|
+
function rewrite(path, change) {
|
|
462
|
+
const content = existsSync(path) ? readFileSync(path, "utf8") : emptyRegistry;
|
|
463
|
+
const registry = JSON.parse(content);
|
|
464
|
+
if (!isRecord(registry)) throw new TypeError(`${path} does not hold a JSON object`);
|
|
465
|
+
const hooks = registry["hooks"];
|
|
466
|
+
const next = isRecord(hooks) ? {
|
|
467
|
+
...registry,
|
|
468
|
+
hooks: change(hooks)
|
|
469
|
+
} : change(registry);
|
|
470
|
+
const problems = checkRegistry(parseRegistry(next));
|
|
471
|
+
if (problems.length > 0) throw new Error(`the registry would not run: ${problems.map(({ event, message }) => `${event}: ${message}`).join("; ")}`);
|
|
472
|
+
atomicWrite(path, `${JSON.stringify(next, null, 2)}\n`);
|
|
473
|
+
}
|
|
474
|
+
/** Writes an example's groups into the registry, replacing any it wrote before. */
|
|
475
|
+
function addExample(path, example, windows) {
|
|
476
|
+
rewrite(path, (events) => example.registrations.reduce((accumulated, registration) => ({
|
|
477
|
+
...accumulated,
|
|
478
|
+
[registration.event]: [...listed(accumulated[registration.event]), ...groupsFor(example.name, registration, windows)]
|
|
479
|
+
}), without(events, example.name)));
|
|
480
|
+
}
|
|
481
|
+
function removeExample(path, name) {
|
|
482
|
+
rewrite(path, (events) => without(events, name));
|
|
483
|
+
}
|
|
326
484
|
// src/cli/codex.ts
|
|
327
485
|
/**
|
|
328
486
|
* Asks Codex which hooks it would run, through the protocol its app server speaks on standard input and output.
|
|
@@ -780,6 +938,27 @@ async function importHooks(io, options) {
|
|
|
780
938
|
io.say(name === void 0 ? "There was nothing to move." : `Migrated. Undo with: hookloom rollback ${name}`);
|
|
781
939
|
return 0;
|
|
782
940
|
}
|
|
941
|
+
/** Lists the example hooks, or adds or removes the named ones. */
|
|
942
|
+
function manageExamples(io, action, names) {
|
|
943
|
+
if (action === void 0 || action === "list") {
|
|
944
|
+
for (const example of examples) io.say(`${example.name}: ${example.summary}${example.needs.length === 0 ? "" : ` Needs ${example.needs.join(" and ")}.`}`);
|
|
945
|
+
return 0;
|
|
946
|
+
}
|
|
947
|
+
if (action !== "add" && action !== "remove") throw new Error(`unknown examples action ${action}; use list, add or remove`);
|
|
948
|
+
if (names.length === 0) throw new Error(`name the examples to ${action}; hookloom examples lists them`);
|
|
949
|
+
const chosenExamples = names.map((name) => {
|
|
950
|
+
const example = examples.find((candidate) => candidate.name === name);
|
|
951
|
+
if (example === void 0) throw new Error(`there is no example called ${name}; hookloom examples lists them`);
|
|
952
|
+
return example;
|
|
953
|
+
});
|
|
954
|
+
const registry = registryPath(io.env);
|
|
955
|
+
for (const example of chosenExamples) {
|
|
956
|
+
if (action === "add") addExample(registry, example, process.platform === "win32");
|
|
957
|
+
else removeExample(registry, example.name);
|
|
958
|
+
io.say(`${action === "add" ? "added" : "removed"} ${example.name} ${action === "add" ? "to" : "from"} ${registry}`);
|
|
959
|
+
}
|
|
960
|
+
return 0;
|
|
961
|
+
}
|
|
783
962
|
function rollbackImport(io, name) {
|
|
784
963
|
const result = rollback(locations(io.env), name);
|
|
785
964
|
for (const { path, how } of result.files) io.say(`${path}: ${how === "merged" ? "kept later changes and undid the migration" : how}`);
|
|
@@ -808,6 +987,8 @@ Usage:
|
|
|
808
987
|
Move your own hooks into the registry. Without --apply, only report.
|
|
809
988
|
hookloom rollback [<migration>]
|
|
810
989
|
Undo a migration, by default the latest.
|
|
990
|
+
hookloom examples [list | add <name>... | remove <name>...]
|
|
991
|
+
Add or remove the example hooks that ship with hookloom. None runs until added.
|
|
811
992
|
|
|
812
993
|
The registry is read from HOOKLOOM_REGISTRY, or hookloom/registry.json in your configuration directory.`;
|
|
813
994
|
function selectedHarnesses(values) {
|
|
@@ -874,6 +1055,7 @@ async function main(argv, io) {
|
|
|
874
1055
|
acknowledgeCodexReview: values["acknowledge-codex-review"]
|
|
875
1056
|
});
|
|
876
1057
|
if (command === "rollback") return rollbackImport(io, argument);
|
|
1058
|
+
if (command === "examples") return manageExamples(io, argument, positionals.slice(2));
|
|
877
1059
|
throw new Error(`unknown command ${command}; hookloom --help lists the commands`);
|
|
878
1060
|
}
|
|
879
1061
|
const io = {
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { delimiter, join } from "node:path";
|
|
3
|
+
// src/version.ts
|
|
4
|
+
const version = "1.2.0";
|
|
5
|
+
// src/dispatch/command-line.ts
|
|
6
|
+
/**
|
|
7
|
+
* The program and arguments with which each harness runs a command hook on each platform.
|
|
8
|
+
*/
|
|
9
|
+
const GIT_BASH_INSTALLS = ["C:\\Program Files\\Git\\bin\\bash.exe", "C:\\Program Files (x86)\\Git\\bin\\bash.exe"];
|
|
10
|
+
function gitBash(platform) {
|
|
11
|
+
return platform.env["CLAUDE_CODE_GIT_BASH_PATH"] ?? GIT_BASH_INSTALLS.find((path) => existsSync(path));
|
|
12
|
+
}
|
|
13
|
+
/** Whether Claude Code would run a hook command through PowerShell: on Windows without Git Bash. */
|
|
14
|
+
function claudeUsesPowerShell(platform) {
|
|
15
|
+
return platform.windows && gitBash(platform) === void 0;
|
|
16
|
+
}
|
|
17
|
+
function onPath(platform, program) {
|
|
18
|
+
return (platform.env["PATH"] ?? platform.env["Path"] ?? "").split(delimiter).some((directory) => directory !== "" && existsSync(join(directory, program)));
|
|
19
|
+
}
|
|
20
|
+
function powershell(platform, command) {
|
|
21
|
+
return {
|
|
22
|
+
program: onPath(platform, "pwsh.exe") ? "pwsh.exe" : "powershell.exe",
|
|
23
|
+
args: [
|
|
24
|
+
"-NoProfile",
|
|
25
|
+
"-Command",
|
|
26
|
+
command
|
|
27
|
+
]
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Claude Code runs a command with `sh -c`, or on Windows with Git Bash and otherwise PowerShell. Codex runs it with the session's shell and no login: the user's shell, or PowerShell on Windows (`build_hooks_config` in `codex-rs/core/src/session/mod.rs`).
|
|
32
|
+
* @throws Error when the handler asks for PowerShell outside Windows.
|
|
33
|
+
*/
|
|
34
|
+
function invocation(harness, handler, platform) {
|
|
35
|
+
if (handler.args !== void 0) return {
|
|
36
|
+
program: handler.command,
|
|
37
|
+
args: handler.args
|
|
38
|
+
};
|
|
39
|
+
const command = platform.windows ? handler.commandWindows ?? handler.command : handler.command;
|
|
40
|
+
if (handler.shell === "powershell") {
|
|
41
|
+
if (!platform.windows) throw new Error("PowerShell command hooks require Windows");
|
|
42
|
+
return powershell(platform, command);
|
|
43
|
+
}
|
|
44
|
+
if (harness === "codex") return platform.windows ? powershell(platform, command) : {
|
|
45
|
+
program: platform.env["SHELL"] ?? "/bin/sh",
|
|
46
|
+
args: ["-c", command]
|
|
47
|
+
};
|
|
48
|
+
if (!platform.windows) return {
|
|
49
|
+
program: handler.shell === "bash" ? "bash" : "/bin/sh",
|
|
50
|
+
args: ["-c", command]
|
|
51
|
+
};
|
|
52
|
+
const bash = gitBash(platform);
|
|
53
|
+
if (handler.shell === "bash") return {
|
|
54
|
+
program: bash ?? "bash",
|
|
55
|
+
args: ["-c", command]
|
|
56
|
+
};
|
|
57
|
+
return bash === void 0 ? powershell(platform, command) : {
|
|
58
|
+
program: bash,
|
|
59
|
+
args: ["-c", command]
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export { invocation as n, version as r, claudeUsesPowerShell as t };
|
package/dist/dispatcher.mjs
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import { text } from "node:stream/consumers";
|
|
2
|
-
import {
|
|
2
|
+
import { r as harnesses, t as eventContract } from "./events-CHsq0Cl4.mjs";
|
|
3
3
|
import { closeSync, existsSync, fstatSync, globSync, openSync, readFileSync, readSync, readdirSync, rmSync, statSync } from "node:fs";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { dirname, join, resolve } from "node:path";
|
|
6
|
-
import { a as
|
|
6
|
+
import { a as registryPath, n as cacheDirectory, r as claudeDirectory, t as atomicWrite } from "./files-CBn-xAo2.mjs";
|
|
7
7
|
import { createHash } from "node:crypto";
|
|
8
|
-
import { a as
|
|
8
|
+
import { a as stringField, i as parsePayload, n as isRecord, o as toolInput, r as namedField } from "./shell-4WSaabze.mjs";
|
|
9
|
+
import { n as invocation, r as version } from "./command-line-OrJYMqHx.mjs";
|
|
10
|
+
import { a as sessionKey, i as removeOlderThan, n as readRecord, o as writeRecord, r as records, t as digest } from "./state-Db3zojdV.mjs";
|
|
9
11
|
import { spawn } from "node:child_process";
|
|
12
|
+
import { t as withoutHarnessMessages } from "./prompt-text-A_jEvJei.mjs";
|
|
13
|
+
import { t as handlersOf } from "./schema-Dl9EJYyt.mjs";
|
|
14
|
+
import { n as permissionRuleMatches, t as parsePermissionRule } from "./permission-rule-BVXyOtut.mjs";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
10
16
|
// src/registry/load.ts
|
|
11
17
|
/**
|
|
12
18
|
* Reads the registry for a dispatch. Every tool call dispatches an event, so a document is validated once per content and version and the outcome remembered.
|
|
@@ -14,7 +20,7 @@ import { spawn } from "node:child_process";
|
|
|
14
20
|
const MEMO_PREFIX = "registry-";
|
|
15
21
|
var RegistryError = class extends Error {};
|
|
16
22
|
async function validate(text, path) {
|
|
17
|
-
const [{ parseRegistry }, { checkRegistry }] = await Promise.all([import("./schema-
|
|
23
|
+
const [{ parseRegistry }, { checkRegistry }] = await Promise.all([import("./schema-EYih4WFo.mjs"), import("./check-ySa9Ce1V.mjs")]);
|
|
18
24
|
const registry = parseRegistry(JSON.parse(text));
|
|
19
25
|
const problems = checkRegistry(registry);
|
|
20
26
|
if (problems.length > 0) throw new RegistryError(`${path} cannot run:\n${problems.map((problem) => `${problem.harness} ${problem.event}: ${problem.message}`).join("\n")}`);
|
|
@@ -53,51 +59,6 @@ const silent = {
|
|
|
53
59
|
failed: false,
|
|
54
60
|
timedOut: false
|
|
55
61
|
};
|
|
56
|
-
// src/dispatch/state.ts
|
|
57
|
-
/**
|
|
58
|
-
* Small JSON records the dispatcher keeps between events. Losing one only means a handler runs or speaks once more, so an unreadable record counts as absent.
|
|
59
|
-
*/
|
|
60
|
-
function digest(identity) {
|
|
61
|
-
return createHash("sha256").update(JSON.stringify(identity)).digest("hex");
|
|
62
|
-
}
|
|
63
|
-
function readRecord(path) {
|
|
64
|
-
try {
|
|
65
|
-
const value = JSON.parse(readFileSync(path, "utf8"));
|
|
66
|
-
return isRecord(value) ? value : void 0;
|
|
67
|
-
} catch {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
/** Records state if it can: a write that fails is dropped, never turned into a hook failure. */
|
|
72
|
-
function writeRecord(path, record) {
|
|
73
|
-
try {
|
|
74
|
-
atomicWrite(path, JSON.stringify(record));
|
|
75
|
-
} catch {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
/** The names of the JSON files in a directory, none when it does not exist. */
|
|
80
|
-
function records(directory) {
|
|
81
|
-
try {
|
|
82
|
-
return readdirSync(directory).filter((name) => name.endsWith(".json")).map((name) => join(directory, name));
|
|
83
|
-
} catch {
|
|
84
|
-
return [];
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
function removeOlderThan(directory, milliseconds) {
|
|
88
|
-
const cutoff = Date.now() - milliseconds;
|
|
89
|
-
for (const path of records(directory)) {
|
|
90
|
-
const status = statSync(path, { throwIfNoEntry: false });
|
|
91
|
-
if (status !== void 0 && status.mtimeMs < cutoff) rmSync(path, { force: true });
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
/** Identifies the session, and the subagent within it, that an input belongs to; `undefined` when the harness gave no session id. */
|
|
95
|
-
function sessionKey(payload) {
|
|
96
|
-
const session = stringField(payload, "session_id");
|
|
97
|
-
if (session === void 0 || session === "") return void 0;
|
|
98
|
-
const agent = stringField(payload, "agent_id");
|
|
99
|
-
return agent === void 0 || agent === "" ? session : `${session}/${agent}`;
|
|
100
|
-
}
|
|
101
62
|
// src/dispatch/cache.ts
|
|
102
63
|
/**
|
|
103
64
|
* Reuses a handler's successful result for the window its `cache` field names, so it runs at most once per window and key without tracking that itself.
|
|
@@ -110,7 +71,7 @@ function selfAndAncestors(directory) {
|
|
|
110
71
|
}
|
|
111
72
|
function fingerprint(patterns, payload) {
|
|
112
73
|
const cwd = resolve(namedField(payload, "cwd") || ".");
|
|
113
|
-
|
|
74
|
+
const seen = patterns.flatMap((pattern) => {
|
|
114
75
|
if (pattern.startsWith(ANCESTORS)) return selfAndAncestors(cwd).map((directory) => join(directory, pattern.slice(8)));
|
|
115
76
|
return [pattern === "~" || pattern.startsWith("~/") ? join(homedir(), pattern.slice(1)) : pattern];
|
|
116
77
|
}).flatMap((pattern) => globSync(pattern.replaceAll("\\", "/")).toSorted().flatMap((path) => {
|
|
@@ -120,7 +81,8 @@ function fingerprint(patterns, payload) {
|
|
|
120
81
|
status.size,
|
|
121
82
|
status.mtimeMs
|
|
122
83
|
]] : [];
|
|
123
|
-
}))
|
|
84
|
+
}));
|
|
85
|
+
return digest(seen);
|
|
124
86
|
}
|
|
125
87
|
function entryPath(handler, cache, context) {
|
|
126
88
|
const identity = [
|
|
@@ -499,34 +461,6 @@ async function runCommand(handler, execution) {
|
|
|
499
461
|
async function execute(handler, execution) {
|
|
500
462
|
return handler.type === "http" ? post(handler, execution) : runCommand(handler, execution);
|
|
501
463
|
}
|
|
502
|
-
// src/dispatch/prompt-text.ts
|
|
503
|
-
/**
|
|
504
|
-
* Separates what a person wrote in a prompt from what the harness put there: other sessions' messages, background-task notices and the harness's own wrappers arrive through the same field as typing.
|
|
505
|
-
*/
|
|
506
|
-
const MESSAGE_BLOCK = /<(?<tag>agent-message|teammate-message|cross-session-message|task-notification)(?=[\s/>])[^>\n]*>(?:[^\n]*?<\/\k<tag>>|[\s\S]*?^[ \t]*<\/\k<tag>>)/gmu;
|
|
507
|
-
const NOTICE_BLOCK = /<(?<tag>system-reminder|codex_internal_context)(?=[\s/>])[^>]*>[\s\S]*?<\/\k<tag>>|<artifact-content-authored-by-others\s*\/>/gu;
|
|
508
|
-
const CROSS_SESSION_NOTICE = /^\[Cross-session idle notice\](?:[\s\S]*?This is an automated notice from that session's harness[^\n]*|[^\n]*)|^\[Cross-session delivery notice\][^\n]*$/gmu;
|
|
509
|
-
const HARNESS_LINES = new RegExp([
|
|
510
|
-
"^Another Claude session sent a message(?: while you were working)?:[ \\t]*$",
|
|
511
|
-
"^This came from another Claude session\\b[^\\n]*\\bnot typed by your user\\b[^\\n]*$",
|
|
512
|
-
"^That \"[^\\n]*?\" is an agent working inside this same session\\b[^\\n]*$",
|
|
513
|
-
"^\\[SYSTEM NOTIFICATION - NOT USER INPUT\\][ \\t]*(?:\\n(?:This is an automated background-task event|Do NOT interpret this as|No human input has been received)[^\\n]*)*",
|
|
514
|
-
"^Goal check-in: «(?:[\\s\\S]*?^Check on their progress[^\\n]*|[^\\n]*)",
|
|
515
|
-
"^\\[Earlier usage-limit notes no longer apply\\.[^\\n]*$",
|
|
516
|
-
"^\\[Request interrupted by user[^\\n]*$",
|
|
517
|
-
"^This session is being continued from another machine\\. Application state may have changed\\.[^\\n]*$"
|
|
518
|
-
].join("|"), "gmu");
|
|
519
|
-
const HARNESS_PROMPT = /^\s*(?:Stop hook feedback:|Base directory for this skill:|This session is being continued from a previous conversation|The summarized conversation included Artifact content)/u;
|
|
520
|
-
/** The prompt without other sessions' messages and the harness's notices, keeping the user's commands and attachments; empty when nothing of the user's is left. */
|
|
521
|
-
function withoutHarnessMessages(prompt) {
|
|
522
|
-
const stripped = [
|
|
523
|
-
MESSAGE_BLOCK,
|
|
524
|
-
NOTICE_BLOCK,
|
|
525
|
-
CROSS_SESSION_NOTICE,
|
|
526
|
-
HARNESS_LINES
|
|
527
|
-
].reduce((text, pattern) => text.replaceAll(pattern, " "), prompt);
|
|
528
|
-
return HARNESS_PROMPT.test(stripped) ? "" : stripped.trim();
|
|
529
|
-
}
|
|
530
464
|
// src/dispatch/repeat.ts
|
|
531
465
|
/**
|
|
532
466
|
* Keeps hooks from saying the same thing again: a handler's `repeat` window, and a Stop block that repeats with no work done since.
|
|
@@ -744,6 +678,26 @@ function selectHandlers(registry, occurrence) {
|
|
|
744
678
|
return handlersOf(group).flatMap((handler) => selectHandler(context, group, handler, matched) ?? []);
|
|
745
679
|
});
|
|
746
680
|
}
|
|
681
|
+
// src/package-root.ts
|
|
682
|
+
/** The directory of this package, whether run from its sources or its bundles, which both sit one level inside it. */
|
|
683
|
+
const packageRoot = fileURLToPath(new URL("..", import.meta.url));
|
|
684
|
+
// src/dispatch/placeholders.ts
|
|
685
|
+
/**
|
|
686
|
+
* Names a registry may write in a command to reach this installation, replaced before any shell reads the command so one spelling works in all of them: the directory of the installed package, which changes with every version, and the Node running the dispatcher.
|
|
687
|
+
*/
|
|
688
|
+
function replaced(text) {
|
|
689
|
+
return text.replaceAll("${HOOKLOOM_ROOT}", packageRoot.replaceAll("\\", "/").replace(/\/$/u, "")).replaceAll("${HOOKLOOM_NODE}", process.execPath.replaceAll("\\", "/"));
|
|
690
|
+
}
|
|
691
|
+
function withPlaceholders(handler) {
|
|
692
|
+
if (handler.type !== "command") return handler;
|
|
693
|
+
const { command, commandWindows, args } = handler;
|
|
694
|
+
return {
|
|
695
|
+
...handler,
|
|
696
|
+
command: replaced(command),
|
|
697
|
+
...commandWindows === void 0 ? {} : { commandWindows: replaced(commandWindows) },
|
|
698
|
+
...args === void 0 ? {} : { args: args.map(replaced) }
|
|
699
|
+
};
|
|
700
|
+
}
|
|
747
701
|
// src/dispatch/run.ts
|
|
748
702
|
/**
|
|
749
703
|
* Handles one occurrence of one event: selects the registry's handlers, runs them together and answers the harness.
|
|
@@ -771,7 +725,7 @@ async function runOne(occurrence, selection, raw, payload) {
|
|
|
771
725
|
const result = await cached(handler, {
|
|
772
726
|
...state,
|
|
773
727
|
directory: cache
|
|
774
|
-
}, async () => execute(handler, {
|
|
728
|
+
}, async () => execute(withPlaceholders(handler), {
|
|
775
729
|
harness,
|
|
776
730
|
input,
|
|
777
731
|
env,
|