hookloom 1.0.0 → 1.1.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-D6B_qnUy.mjs → check-CRZ6Rgdo.mjs} +3 -2
- package/dist/check-ySa9Ce1V.mjs +1 -0
- package/dist/cli.mjs +213 -41
- package/dist/command-line-BjI01lOI.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/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-Bvs-uwjo.mjs → schema-EYih4WFo.mjs} +1 -1
- package/dist/{schema-ChV-yJDT.mjs → schemas-B4KRDIno.mjs} +9 -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-nhb0tW8i.mjs +0 -1
- package/dist/command-line-BBETAyPT.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,115 @@
|
|
|
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-BjI01lOI.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: "private-reference-guard",
|
|
76
|
+
summary: "Refuse to name a private GitHub repository in a public one.",
|
|
77
|
+
needs: ["git", "gh"],
|
|
78
|
+
registrations: [{
|
|
79
|
+
event: "PreToolUse",
|
|
80
|
+
harnesses: ["claude"],
|
|
81
|
+
matcher: "Edit|Write|NotebookEdit",
|
|
82
|
+
timeout: NETWORK_SECONDS
|
|
83
|
+
}, {
|
|
84
|
+
event: "PreToolUse",
|
|
85
|
+
harnesses: BOTH,
|
|
86
|
+
matcher: "Bash",
|
|
87
|
+
commandPattern: "\\b(?:gh|git)\\b",
|
|
88
|
+
timeout: NETWORK_SECONDS
|
|
89
|
+
}]
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: "issue-comment-guard",
|
|
93
|
+
summary: "Refuse a comment on your own issue that nobody else has joined: edit its body instead.",
|
|
94
|
+
needs: ["gh"],
|
|
95
|
+
registrations: [{
|
|
96
|
+
event: "PreToolUse",
|
|
97
|
+
harnesses: BOTH,
|
|
98
|
+
matcher: "Bash",
|
|
99
|
+
commandPattern: "\\bgh\\b",
|
|
100
|
+
timeout: NETWORK_SECONDS
|
|
101
|
+
}]
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: "default-branch-health",
|
|
105
|
+
summary: "On a prompt, warn when the default branch of the repository in use is red.",
|
|
106
|
+
needs: ["git", "gh"],
|
|
107
|
+
registrations: [{
|
|
108
|
+
event: "UserPromptSubmit",
|
|
109
|
+
harnesses: BOTH,
|
|
110
|
+
timeout: 15
|
|
111
|
+
}]
|
|
31
112
|
}
|
|
32
|
-
|
|
113
|
+
];
|
|
33
114
|
// src/migrate/plan.ts
|
|
34
115
|
/**
|
|
35
116
|
* Decides which native hooks the dispatcher can run exactly as the harness does, and which must stay where they are.
|
|
@@ -323,6 +404,67 @@ function rollback(locations, requested) {
|
|
|
323
404
|
};
|
|
324
405
|
});
|
|
325
406
|
}
|
|
407
|
+
// src/examples/install.ts
|
|
408
|
+
/**
|
|
409
|
+
* 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.
|
|
410
|
+
*/
|
|
411
|
+
function command(name, harness, windows) {
|
|
412
|
+
const script = `"\${HOOKLOOM_ROOT}/dist/examples/${name}.mjs" ${harness}`;
|
|
413
|
+
return windows ? `node ${script} ; exit $LASTEXITCODE` : `"\${HOOKLOOM_NODE}" ${script}`;
|
|
414
|
+
}
|
|
415
|
+
function groupsFor(name, registration, windows) {
|
|
416
|
+
const { harnesses, matcher, commandPattern, timeout } = registration;
|
|
417
|
+
return harnesses.map((harness) => ({
|
|
418
|
+
harnesses: [harness],
|
|
419
|
+
...matcher === void 0 ? {} : { matcher },
|
|
420
|
+
hooks: [{
|
|
421
|
+
type: "command",
|
|
422
|
+
command: command(name, harness, windows),
|
|
423
|
+
timeout,
|
|
424
|
+
...commandPattern === void 0 ? {} : { when: {
|
|
425
|
+
field: "command",
|
|
426
|
+
pattern: commandPattern
|
|
427
|
+
} }
|
|
428
|
+
}]
|
|
429
|
+
}));
|
|
430
|
+
}
|
|
431
|
+
function runs(group, name) {
|
|
432
|
+
const hooks = isRecord(group) ? group["hooks"] : void 0;
|
|
433
|
+
return Array.isArray(hooks) && hooks.some((hook) => isRecord(hook) && typeof hook["command"] === "string" && hook["command"].includes(`/dist/examples/${name}.mjs"`));
|
|
434
|
+
}
|
|
435
|
+
function without(events, name) {
|
|
436
|
+
return Object.fromEntries(Object.entries(events).flatMap(([event, groups]) => {
|
|
437
|
+
if (!Array.isArray(groups)) return [[event, groups]];
|
|
438
|
+
const kept = groups.filter((group) => !runs(group, name));
|
|
439
|
+
return kept.length === 0 ? [] : [[event, kept]];
|
|
440
|
+
}));
|
|
441
|
+
}
|
|
442
|
+
function listed(value) {
|
|
443
|
+
return Array.isArray(value) ? value : [];
|
|
444
|
+
}
|
|
445
|
+
function rewrite(path, change) {
|
|
446
|
+
const content = existsSync(path) ? readFileSync(path, "utf8") : emptyRegistry;
|
|
447
|
+
const registry = JSON.parse(content);
|
|
448
|
+
if (!isRecord(registry)) throw new TypeError(`${path} does not hold a JSON object`);
|
|
449
|
+
const hooks = registry["hooks"];
|
|
450
|
+
const next = isRecord(hooks) ? {
|
|
451
|
+
...registry,
|
|
452
|
+
hooks: change(hooks)
|
|
453
|
+
} : change(registry);
|
|
454
|
+
const problems = checkRegistry(parseRegistry(next));
|
|
455
|
+
if (problems.length > 0) throw new Error(`the registry would not run: ${problems.map(({ event, message }) => `${event}: ${message}`).join("; ")}`);
|
|
456
|
+
atomicWrite(path, `${JSON.stringify(next, null, 2)}\n`);
|
|
457
|
+
}
|
|
458
|
+
/** Writes an example's groups into the registry, replacing any it wrote before. */
|
|
459
|
+
function addExample(path, example, windows) {
|
|
460
|
+
rewrite(path, (events) => example.registrations.reduce((accumulated, registration) => ({
|
|
461
|
+
...accumulated,
|
|
462
|
+
[registration.event]: [...listed(accumulated[registration.event]), ...groupsFor(example.name, registration, windows)]
|
|
463
|
+
}), without(events, example.name)));
|
|
464
|
+
}
|
|
465
|
+
function removeExample(path, name) {
|
|
466
|
+
rewrite(path, (events) => without(events, name));
|
|
467
|
+
}
|
|
326
468
|
// src/cli/codex.ts
|
|
327
469
|
/**
|
|
328
470
|
* Asks Codex which hooks it would run, through the protocol its app server speaks on standard input and output.
|
|
@@ -471,17 +613,17 @@ function nativeHookFiles(env) {
|
|
|
471
613
|
codex: join(codexDirectory(env), "hooks.json")
|
|
472
614
|
};
|
|
473
615
|
}
|
|
474
|
-
const claudeList = array(
|
|
475
|
-
|
|
616
|
+
const claudeList = array(looseObject({ id: string() }));
|
|
617
|
+
const claudeEntry = object({
|
|
476
618
|
version: string(),
|
|
477
619
|
installPath: string(),
|
|
478
620
|
enabled: boolean()
|
|
479
|
-
})
|
|
480
|
-
const codexList = object({ installed: array(
|
|
481
|
-
|
|
621
|
+
});
|
|
622
|
+
const codexList = object({ installed: array(looseObject({ pluginId: string() })) });
|
|
623
|
+
const codexEntry = object({
|
|
482
624
|
version: string(),
|
|
483
625
|
enabled: boolean()
|
|
484
|
-
})
|
|
626
|
+
});
|
|
485
627
|
const INSTALL = {
|
|
486
628
|
claude: (source) => [[
|
|
487
629
|
"plugin",
|
|
@@ -509,6 +651,24 @@ function installCommands(harness, source) {
|
|
|
509
651
|
return INSTALL[harness](source);
|
|
510
652
|
}
|
|
511
653
|
/**
|
|
654
|
+
* The enabled hookloom plugin in a harness's `plugin list --json` output, or `undefined` when it has none.
|
|
655
|
+
* @throws when the listing, or the plugin's own entry, cannot be read.
|
|
656
|
+
*/
|
|
657
|
+
function listedPlugin(harness, listing, env) {
|
|
658
|
+
if (harness === "claude") {
|
|
659
|
+
const entry = claudeList.parse(listing).filter(({ id }) => id === PLUGIN_ID).map((found) => claudeEntry.parse(found)).find(({ enabled }) => enabled);
|
|
660
|
+
return entry === void 0 ? void 0 : {
|
|
661
|
+
version: entry.version,
|
|
662
|
+
directory: entry.installPath
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
const entry = codexList.parse(listing).installed.filter(({ pluginId }) => pluginId === PLUGIN_ID).map((found) => codexEntry.parse(found)).find(({ enabled }) => enabled);
|
|
666
|
+
return entry === void 0 ? void 0 : {
|
|
667
|
+
version: entry.version,
|
|
668
|
+
directory: join(codexDirectory(env), "plugins", "cache", "hookloom", "hookloom", entry.version)
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
512
672
|
* The enabled hookloom plugin of a harness, or `undefined` when it has none.
|
|
513
673
|
* @throws when the harness is not on the PATH or cannot list its plugins.
|
|
514
674
|
*/
|
|
@@ -521,19 +681,7 @@ async function installedPlugin(harness, env) {
|
|
|
521
681
|
"--json"
|
|
522
682
|
], env);
|
|
523
683
|
if (listed.code !== 0) throw new Error(`${harness} could not list its plugins: ${listed.stderr.trim()}`);
|
|
524
|
-
|
|
525
|
-
if (harness === "claude") {
|
|
526
|
-
const entry = claudeList.parse(document).find((plugin) => plugin.id === "hookloom@hookloom" && plugin.enabled);
|
|
527
|
-
return entry === void 0 ? void 0 : {
|
|
528
|
-
version: entry.version,
|
|
529
|
-
directory: entry.installPath
|
|
530
|
-
};
|
|
531
|
-
}
|
|
532
|
-
const entry = codexList.parse(document).installed.find((plugin) => plugin.pluginId === "hookloom@hookloom" && plugin.enabled);
|
|
533
|
-
return entry === void 0 ? void 0 : {
|
|
534
|
-
version: entry.version,
|
|
535
|
-
directory: join(codexDirectory(env), "plugins", "cache", "hookloom", "hookloom", entry.version)
|
|
536
|
-
};
|
|
684
|
+
return listedPlugin(harness, JSON.parse(listed.stdout), env);
|
|
537
685
|
}
|
|
538
686
|
// src/cli/in-order.ts
|
|
539
687
|
/** Applies an asynchronous step to each item in turn, each starting after the one before has finished, and resolves with their results. */
|
|
@@ -774,6 +922,27 @@ async function importHooks(io, options) {
|
|
|
774
922
|
io.say(name === void 0 ? "There was nothing to move." : `Migrated. Undo with: hookloom rollback ${name}`);
|
|
775
923
|
return 0;
|
|
776
924
|
}
|
|
925
|
+
/** Lists the example hooks, or adds or removes the named ones. */
|
|
926
|
+
function manageExamples(io, action, names) {
|
|
927
|
+
if (action === void 0 || action === "list") {
|
|
928
|
+
for (const example of examples) io.say(`${example.name}: ${example.summary}${example.needs.length === 0 ? "" : ` Needs ${example.needs.join(" and ")}.`}`);
|
|
929
|
+
return 0;
|
|
930
|
+
}
|
|
931
|
+
if (action !== "add" && action !== "remove") throw new Error(`unknown examples action ${action}; use list, add or remove`);
|
|
932
|
+
if (names.length === 0) throw new Error(`name the examples to ${action}; hookloom examples lists them`);
|
|
933
|
+
const chosenExamples = names.map((name) => {
|
|
934
|
+
const example = examples.find((candidate) => candidate.name === name);
|
|
935
|
+
if (example === void 0) throw new Error(`there is no example called ${name}; hookloom examples lists them`);
|
|
936
|
+
return example;
|
|
937
|
+
});
|
|
938
|
+
const registry = registryPath(io.env);
|
|
939
|
+
for (const example of chosenExamples) {
|
|
940
|
+
if (action === "add") addExample(registry, example, process.platform === "win32");
|
|
941
|
+
else removeExample(registry, example.name);
|
|
942
|
+
io.say(`${action === "add" ? "added" : "removed"} ${example.name} ${action === "add" ? "to" : "from"} ${registry}`);
|
|
943
|
+
}
|
|
944
|
+
return 0;
|
|
945
|
+
}
|
|
777
946
|
function rollbackImport(io, name) {
|
|
778
947
|
const result = rollback(locations(io.env), name);
|
|
779
948
|
for (const { path, how } of result.files) io.say(`${path}: ${how === "merged" ? "kept later changes and undid the migration" : how}`);
|
|
@@ -802,6 +971,8 @@ Usage:
|
|
|
802
971
|
Move your own hooks into the registry. Without --apply, only report.
|
|
803
972
|
hookloom rollback [<migration>]
|
|
804
973
|
Undo a migration, by default the latest.
|
|
974
|
+
hookloom examples [list | add <name>... | remove <name>...]
|
|
975
|
+
Add or remove the example hooks that ship with hookloom. None runs until added.
|
|
805
976
|
|
|
806
977
|
The registry is read from HOOKLOOM_REGISTRY, or hookloom/registry.json in your configuration directory.`;
|
|
807
978
|
function selectedHarnesses(values) {
|
|
@@ -868,6 +1039,7 @@ async function main(argv, io) {
|
|
|
868
1039
|
acknowledgeCodexReview: values["acknowledge-codex-review"]
|
|
869
1040
|
});
|
|
870
1041
|
if (command === "rollback") return rollbackImport(io, argument);
|
|
1042
|
+
if (command === "examples") return manageExamples(io, argument, positionals.slice(2));
|
|
871
1043
|
throw new Error(`unknown command ${command}; hookloom --help lists the commands`);
|
|
872
1044
|
}
|
|
873
1045
|
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.1.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-BjI01lOI.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,
|