vouchington-tooling 0.59.0 → 0.61.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/README.md +108 -0
- package/dist/cli/commands/hook.d.mts +2 -0
- package/dist/cli/commands/hook.mjs +19 -0
- package/dist/cli/commands/sandbox-command-audit.d.mts +7 -0
- package/dist/cli/commands/sandbox-command-audit.mjs +27 -0
- package/dist/cli/index.mjs +8 -0
- package/dist/cli/parse-forwarded-commands.d.mts +9 -0
- package/dist/cli/parse-forwarded-commands.mjs +6 -0
- package/dist/cli/parse.d.mts +9 -0
- package/dist/cli/usage-text.d.mts +1 -0
- package/dist/cli/usage-text.mjs +179 -0
- package/dist/cli/usage.d.mts +2 -1
- package/dist/cli/usage.mjs +2 -156
- package/dist/command-policy/engine.d.mts +7 -0
- package/dist/command-policy/engine.mjs +58 -0
- package/dist/command-policy/git-patterns.d.mts +20 -0
- package/dist/command-policy/git-patterns.mjs +82 -0
- package/dist/command-policy/git-process.d.mts +4 -0
- package/dist/command-policy/git-process.mjs +54 -0
- package/dist/command-policy/github-checkout-owners.d.mts +22 -0
- package/dist/command-policy/github-checkout-owners.mjs +122 -0
- package/dist/command-policy/github-command-context.d.mts +6 -0
- package/dist/command-policy/github-command-context.mjs +19 -0
- package/dist/command-policy/github-command-cwd.d.mts +8 -0
- package/dist/command-policy/github-command-cwd.mjs +130 -0
- package/dist/command-policy/github-command-position.d.mts +7 -0
- package/dist/command-policy/github-command-position.mjs +12 -0
- package/dist/command-policy/github-configured-base.d.mts +25 -0
- package/dist/command-policy/github-configured-base.mjs +59 -0
- package/dist/command-policy/github-invocation.d.mts +9 -0
- package/dist/command-policy/github-invocation.mjs +80 -0
- package/dist/command-policy/github-merge-authority.d.mts +21 -0
- package/dist/command-policy/github-merge-authority.mjs +86 -0
- package/dist/command-policy/github-option-flags.d.mts +3 -0
- package/dist/command-policy/github-option-flags.mjs +67 -0
- package/dist/command-policy/github-options.d.mts +12 -0
- package/dist/command-policy/github-options.mjs +73 -0
- package/dist/command-policy/github-stack-workflow.d.mts +9 -0
- package/dist/command-policy/github-stack-workflow.mjs +94 -0
- package/dist/command-policy/github-workflow.d.mts +7 -0
- package/dist/command-policy/github-workflow.mjs +50 -0
- package/dist/command-policy/hook/args.d.mts +11 -0
- package/dist/command-policy/hook/args.mjs +49 -0
- package/dist/command-policy/hook/harness.d.mts +26 -0
- package/dist/command-policy/hook/harness.mjs +119 -0
- package/dist/command-policy/hook/policy-module.d.mts +7 -0
- package/dist/command-policy/hook/policy-module.mjs +48 -0
- package/dist/command-policy/hook/post-tool-use.d.mts +9 -0
- package/dist/command-policy/hook/post-tool-use.mjs +51 -0
- package/dist/command-policy/hook/pre-tool-use.d.mts +21 -0
- package/dist/command-policy/hook/pre-tool-use.mjs +45 -0
- package/dist/command-policy/hook/run.d.mts +17 -0
- package/dist/command-policy/hook/run.mjs +69 -0
- package/dist/command-policy/hook/types.d.mts +33 -0
- package/dist/command-policy/hook/types.mjs +5 -0
- package/dist/command-policy/index.d.mts +29 -0
- package/dist/command-policy/index.mjs +23 -0
- package/dist/command-policy/path-references.d.mts +14 -0
- package/dist/command-policy/path-references.mjs +68 -0
- package/dist/command-policy/path-rules.d.mts +11 -0
- package/dist/command-policy/path-rules.mjs +37 -0
- package/dist/command-policy/payload.d.mts +8 -0
- package/dist/command-policy/payload.mjs +54 -0
- package/dist/command-policy/types.d.mts +77 -0
- package/dist/command-policy/types.mjs +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +1 -0
- package/dist/sandbox-command-audit/args.d.mts +20 -0
- package/dist/sandbox-command-audit/args.mjs +64 -0
- package/dist/sandbox-command-audit/classify-prefixes.d.mts +23 -0
- package/dist/sandbox-command-audit/classify-prefixes.mjs +50 -0
- package/dist/sandbox-command-audit/classify.d.mts +46 -0
- package/dist/sandbox-command-audit/classify.mjs +74 -0
- package/dist/sandbox-command-audit/claude-extract.d.mts +23 -0
- package/dist/sandbox-command-audit/claude-extract.mjs +76 -0
- package/dist/sandbox-command-audit/claude-tool-uses.d.mts +15 -0
- package/dist/sandbox-command-audit/claude-tool-uses.mjs +53 -0
- package/dist/sandbox-command-audit/codex-extract.d.mts +21 -0
- package/dist/sandbox-command-audit/codex-extract.mjs +77 -0
- package/dist/sandbox-command-audit/codex-scan.d.mts +11 -0
- package/dist/sandbox-command-audit/codex-scan.mjs +98 -0
- package/dist/sandbox-command-audit/codex-spool.d.mts +14 -0
- package/dist/sandbox-command-audit/codex-spool.mjs +46 -0
- package/dist/sandbox-command-audit/discover-files.d.mts +44 -0
- package/dist/sandbox-command-audit/discover-files.mjs +85 -0
- package/dist/sandbox-command-audit/failure-text.d.mts +7 -0
- package/dist/sandbox-command-audit/failure-text.mjs +34 -0
- package/dist/sandbox-command-audit/fixtures.test-helpers.d.mts +34 -0
- package/dist/sandbox-command-audit/fixtures.test-helpers.mjs +41 -0
- package/dist/sandbox-command-audit/index.d.mts +26 -0
- package/dist/sandbox-command-audit/index.mjs +15 -0
- package/dist/sandbox-command-audit/json-record.d.mts +6 -0
- package/dist/sandbox-command-audit/json-record.mjs +16 -0
- package/dist/sandbox-command-audit/limits.d.mts +30 -0
- package/dist/sandbox-command-audit/limits.mjs +66 -0
- package/dist/sandbox-command-audit/repo-scope.d.mts +25 -0
- package/dist/sandbox-command-audit/repo-scope.mjs +79 -0
- package/dist/sandbox-command-audit/report-raw.d.mts +16 -0
- package/dist/sandbox-command-audit/report-raw.mjs +55 -0
- package/dist/sandbox-command-audit/report.d.mts +10 -0
- package/dist/sandbox-command-audit/report.mjs +61 -0
- package/dist/sandbox-command-audit/run.d.mts +36 -0
- package/dist/sandbox-command-audit/run.mjs +70 -0
- package/dist/sandbox-command-audit/scan.d.mts +10 -0
- package/dist/sandbox-command-audit/scan.mjs +79 -0
- package/dist/sandbox-command-audit/transcript-cwd.d.mts +14 -0
- package/dist/sandbox-command-audit/transcript-cwd.mjs +153 -0
- package/dist/sandbox-command-audit/types.d.mts +59 -0
- package/dist/sandbox-command-audit/types.mjs +23 -0
- package/dist/tmux/cli-shared.d.mts +18 -0
- package/dist/tmux/cli-shared.mjs +8 -0
- package/dist/tmux/cli.d.mts +3 -0
- package/dist/tmux/cli.mjs +34 -0
- package/dist/tmux/config.d.mts +24 -0
- package/dist/tmux/config.mjs +91 -0
- package/dist/tmux/fake-tmux.test-helpers.d.mts +46 -0
- package/dist/tmux/fake-tmux.test-helpers.mjs +105 -0
- package/dist/tmux/hook-payload.d.mts +8 -0
- package/dist/tmux/hook-payload.mjs +39 -0
- package/dist/tmux/index.d.mts +9 -0
- package/dist/tmux/index.mjs +7 -0
- package/dist/tmux/launcher.d.mts +11 -0
- package/dist/tmux/launcher.mjs +127 -0
- package/dist/tmux/reminder-post-tool.d.mts +21 -0
- package/dist/tmux/reminder-post-tool.mjs +88 -0
- package/dist/tmux/runner.d.mts +11 -0
- package/dist/tmux/runner.mjs +26 -0
- package/dist/tmux/session-name.d.mts +5 -0
- package/dist/tmux/session-name.mjs +10 -0
- package/dist/tmux/shell-quote.d.mts +9 -0
- package/dist/tmux/shell-quote.mjs +16 -0
- package/dist/tmux/up.d.mts +2 -0
- package/dist/tmux/up.mjs +73 -0
- package/dist/tmux/windows.d.mts +18 -0
- package/dist/tmux/windows.mjs +50 -0
- package/package.json +16 -1
- package/scripts/__pycache__/run_bounded_process.cpython-312.pyc +0 -0
- package/scripts/tmux/tmux-agent-reminder.sh +68 -0
- package/scripts/tmux/tmux-name.sh +34 -0
- package/scripts/tmux/tmux-read-title.sh +13 -0
- package/scripts/tmux/tmux-target.sh +130 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { findStackInitBaseBlock, withGuidance, } from "./github-configured-base.mjs";
|
|
2
|
+
import { hasNamedFlag, hasNumericPrSelector } from "./github-option-flags.mjs";
|
|
3
|
+
const STACK_AGENT_ACTIONS = new Set([
|
|
4
|
+
'add',
|
|
5
|
+
'bottom',
|
|
6
|
+
'checkout',
|
|
7
|
+
'delete',
|
|
8
|
+
'down',
|
|
9
|
+
'init',
|
|
10
|
+
'link',
|
|
11
|
+
'merge',
|
|
12
|
+
'push',
|
|
13
|
+
'rebase',
|
|
14
|
+
'submit',
|
|
15
|
+
'sync',
|
|
16
|
+
'top',
|
|
17
|
+
'unstack',
|
|
18
|
+
'up',
|
|
19
|
+
'view',
|
|
20
|
+
]);
|
|
21
|
+
const HELP_FLAGS = new Set(['--help', '-h']);
|
|
22
|
+
/**
|
|
23
|
+
* `cwd` is the invocation's working directory, or undefined when it could not be determined (an
|
|
24
|
+
* unresolved `cd` target). The init HEAD-ancestry guard fails open in that case rather than
|
|
25
|
+
* substitute the session cwd.
|
|
26
|
+
*/
|
|
27
|
+
export function findGitHubStackWorkflowBlock(invocation, cwd, policy) {
|
|
28
|
+
const stack = stackInvocation(invocation);
|
|
29
|
+
if (stack === null || isStackHelp(stack)) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const block = (reason) => ({
|
|
33
|
+
reason: withGuidance(reason, policy.guidance),
|
|
34
|
+
});
|
|
35
|
+
const { action, optionTokens } = stack;
|
|
36
|
+
const draftFirst = `New PRs must be opened as draft first.`;
|
|
37
|
+
const followUp = policy.draftFollowUp === '' ? '' : `, then ${policy.draftFollowUp}`;
|
|
38
|
+
if (!STACK_AGENT_ACTIONS.has(action)) {
|
|
39
|
+
return block('gh stack allowlist is closed. Use only the non-interactive commands. Interactive TUIs (modify, switch) and trunk are banned.');
|
|
40
|
+
}
|
|
41
|
+
if (action === 'init') {
|
|
42
|
+
return findStackInitBaseBlock(optionTokens, cwd, policy);
|
|
43
|
+
}
|
|
44
|
+
if (action === 'checkout') {
|
|
45
|
+
return isStackCheckoutTarget(optionTokens)
|
|
46
|
+
? null
|
|
47
|
+
: block('gh stack checkout takes exactly one stack number or PR number: `gh stack checkout <stack-number>`. The bare form is an interactive picker, and a branch-name target resolves only against stacks this worktree already tracks.');
|
|
48
|
+
}
|
|
49
|
+
if (action === 'submit') {
|
|
50
|
+
if (hasNamedFlag(optionTokens, 'open')) {
|
|
51
|
+
return block(`${draftFirst} Use \`gh stack submit --auto\`${followUp}.`);
|
|
52
|
+
}
|
|
53
|
+
return hasNamedFlag(optionTokens, 'auto')
|
|
54
|
+
? null
|
|
55
|
+
: block('gh stack submit is an interactive TUI. Use `gh stack submit --auto` so new stacked PRs open as drafts.');
|
|
56
|
+
}
|
|
57
|
+
if (action === 'link' && hasNamedFlag(optionTokens, 'open')) {
|
|
58
|
+
return block(`${draftFirst} Use \`gh stack link\` without --open${followUp}.`);
|
|
59
|
+
}
|
|
60
|
+
// Merge authority itself belongs to findAutomationMergeBlock and findInteractiveMergeConfirm.
|
|
61
|
+
if (action === 'merge' && !hasNumericPrSelector(optionTokens)) {
|
|
62
|
+
return block('gh stack merge requires a PR-number selector. Bare merge is a TUI and lands the whole stack. Use `gh stack merge --squash <pr>`.');
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
// Exactly one positive stack or PR number. The bare form opens a picker, and gh-stack resolves a
|
|
67
|
+
// branch name only against stacks this worktree already tracks.
|
|
68
|
+
function isStackCheckoutTarget(optionTokens) {
|
|
69
|
+
return optionTokens.length === 1 && /^[1-9]\d*$/.test(optionTokens[0]);
|
|
70
|
+
}
|
|
71
|
+
// gh-stack is a cobra CLI. Its root command only prints help, `help [command]` only prints help,
|
|
72
|
+
// and `--help` or `-h` prints a command's help and exits before the command runs. Only the bare
|
|
73
|
+
// forms count, since a `--` or any other argument beside the flag can change what runs.
|
|
74
|
+
function isStackHelp({ action, optionTokens }) {
|
|
75
|
+
if (HELP_FLAGS.has(action) || action === 'help') {
|
|
76
|
+
return (optionTokens.length === 0 ||
|
|
77
|
+
(action === 'help' && optionTokens.length === 1 && STACK_AGENT_ACTIONS.has(optionTokens[0])));
|
|
78
|
+
}
|
|
79
|
+
return (STACK_AGENT_ACTIONS.has(action) && optionTokens.length === 1 && HELP_FLAGS.has(optionTokens[0]));
|
|
80
|
+
}
|
|
81
|
+
const EXTENSION_AREAS = new Set(['extension', 'extensions', 'ext']);
|
|
82
|
+
function stackInvocation(invocation) {
|
|
83
|
+
if (invocation.area === 'stack') {
|
|
84
|
+
return invocation;
|
|
85
|
+
}
|
|
86
|
+
const action = invocation.optionTokens[1];
|
|
87
|
+
if (EXTENSION_AREAS.has(invocation.area) &&
|
|
88
|
+
invocation.action === 'exec' &&
|
|
89
|
+
invocation.optionTokens[0] === 'stack' &&
|
|
90
|
+
action !== undefined) {
|
|
91
|
+
return { ...invocation, action, area: 'stack', optionTokens: invocation.optionTokens.slice(2) };
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { BlockDecision, StackPolicy } from './types.mts';
|
|
2
|
+
export type GitHubWorkflowPolicy = {
|
|
3
|
+
automationContext: boolean;
|
|
4
|
+
mergeGuidance?: string | undefined;
|
|
5
|
+
stack?: StackPolicy | undefined;
|
|
6
|
+
};
|
|
7
|
+
export declare function findGitHubWorkflowBlock(command: string, cwd: string, policy: GitHubWorkflowPolicy): BlockDecision | null;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { tokenizeShellWordsDetailed } from "../shell-parse/index.mjs";
|
|
2
|
+
import { commandsToInspectForGitHubPolicy } from "./github-command-context.mjs";
|
|
3
|
+
import { commandCwd } from "./github-command-cwd.mjs";
|
|
4
|
+
import { commandPrefixAt } from "./github-command-position.mjs";
|
|
5
|
+
import { defaultGitProbe } from "./git-process.mjs";
|
|
6
|
+
import { parseGhOrGhStackInvocation } from "./github-invocation.mjs";
|
|
7
|
+
import { findAutomationMergeBlock } from "./github-merge-authority.mjs";
|
|
8
|
+
import { findGitHubStackWorkflowBlock } from "./github-stack-workflow.mjs";
|
|
9
|
+
function resolveStackPolicy(stack) {
|
|
10
|
+
return {
|
|
11
|
+
baseBranch: stack.baseBranch ?? 'main',
|
|
12
|
+
remote: stack.remote ?? 'origin',
|
|
13
|
+
guidance: stack.guidance ?? '',
|
|
14
|
+
draftFollowUp: stack.draftFollowUp ?? '',
|
|
15
|
+
git: stack.git ?? defaultGitProbe,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function findGitHubWorkflowBlock(command, cwd, policy) {
|
|
19
|
+
const stackBlock = policy.stack === undefined
|
|
20
|
+
? null
|
|
21
|
+
: findStackBlock(command, cwd, resolveStackPolicy(policy.stack));
|
|
22
|
+
// The specific reasons win; any other merge in automation gets the coarse block.
|
|
23
|
+
return (stackBlock ??
|
|
24
|
+
findAutomationMergeBlock(command, policy.automationContext, policy.mergeGuidance ?? ''));
|
|
25
|
+
}
|
|
26
|
+
function findStackBlock(command, cwd, stack) {
|
|
27
|
+
for (const commandToInspect of commandsToInspectForGitHubPolicy(command)) {
|
|
28
|
+
const tokens = tokenizeShellWordsDetailed(commandToInspect, { splitRedirections: true }).map((token) => token.value);
|
|
29
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
30
|
+
if (!mayRunGh(tokens[index]) || commandPrefixAt(tokens, index) === null) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const invocation = parseGhOrGhStackInvocation(tokens, index);
|
|
34
|
+
if (invocation === null) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const block = findGitHubStackWorkflowBlock(invocation, commandCwd(tokens, index, cwd), stack);
|
|
38
|
+
if (block !== null) {
|
|
39
|
+
return block;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
// Every policy above reads a gh or gh-stack command. Skipping other words before commandPrefixAt
|
|
46
|
+
// keeps a long command from rescanning its segment at every word.
|
|
47
|
+
function mayRunGh(word) {
|
|
48
|
+
const name = word.slice(word.lastIndexOf('/') + 1);
|
|
49
|
+
return name === 'gh' || name === 'gh-stack';
|
|
50
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type HookHarness } from './types.mts';
|
|
2
|
+
export type HookArgs = {
|
|
3
|
+
event: 'pre-tool-use' | 'post-tool-use';
|
|
4
|
+
harness: HookHarness;
|
|
5
|
+
policy: string;
|
|
6
|
+
root?: string;
|
|
7
|
+
/** Environment variable names that mark an unattended run when set (see isAutomationContext). */
|
|
8
|
+
automationEnv: string[];
|
|
9
|
+
};
|
|
10
|
+
/** Parses `hook` arguments; throws a usage error naming the first problem. */
|
|
11
|
+
export declare function parseHookArgs(args: readonly string[]): HookArgs;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { HOOK_HARNESSES } from "./types.mjs";
|
|
2
|
+
const HOOK_USAGE = 'usage: vouchington hook pre-tool-use|post-tool-use --harness claude|codex|cursor|grok --policy <module> [--root <dir>] [--automation-env NAME]...';
|
|
3
|
+
const VALUE_FLAGS = new Set(['--harness', '--policy', '--root', '--automation-env']);
|
|
4
|
+
/** Parses `hook` arguments; throws a usage error naming the first problem. */
|
|
5
|
+
export function parseHookArgs(args) {
|
|
6
|
+
const [event, ...rest] = args;
|
|
7
|
+
if (event !== 'pre-tool-use' && event !== 'post-tool-use') {
|
|
8
|
+
throw new Error(`${HOOK_USAGE}\nunknown or missing hook event: ${event ?? '(none)'}`);
|
|
9
|
+
}
|
|
10
|
+
const flags = parseFlags(rest);
|
|
11
|
+
const harness = flags.harness;
|
|
12
|
+
if (!HOOK_HARNESSES.includes(harness)) {
|
|
13
|
+
throw new Error(`${HOOK_USAGE}\n--harness must be one of ${HOOK_HARNESSES.join(', ')}`);
|
|
14
|
+
}
|
|
15
|
+
if (flags.policy === undefined || flags.policy === '') {
|
|
16
|
+
throw new Error(`${HOOK_USAGE}\n--policy <module> is required`);
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
event,
|
|
20
|
+
harness: harness,
|
|
21
|
+
policy: flags.policy,
|
|
22
|
+
automationEnv: flags.automationEnv,
|
|
23
|
+
...(flags.root === undefined ? {} : { root: flags.root }),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function parseFlags(args) {
|
|
27
|
+
const flags = { automationEnv: [] };
|
|
28
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
29
|
+
const arg = args[index];
|
|
30
|
+
const [name, inline] = arg.startsWith('--') ? splitFlag(arg) : [arg, undefined];
|
|
31
|
+
if (!VALUE_FLAGS.has(name)) {
|
|
32
|
+
throw new Error(`${HOOK_USAGE}\nunknown argument: ${arg}`);
|
|
33
|
+
}
|
|
34
|
+
const value = inline ?? args[(index += 1)];
|
|
35
|
+
// A flag-looking token is the next flag, not this flag's value (`--policy --root x`).
|
|
36
|
+
if (value === undefined || (inline === undefined && value.startsWith('--'))) {
|
|
37
|
+
throw new Error(`${HOOK_USAGE}\n${name} needs a value`);
|
|
38
|
+
}
|
|
39
|
+
if (name === '--automation-env')
|
|
40
|
+
flags.automationEnv.push(value);
|
|
41
|
+
else
|
|
42
|
+
flags[name.slice(2)] = value;
|
|
43
|
+
}
|
|
44
|
+
return flags;
|
|
45
|
+
}
|
|
46
|
+
function splitFlag(arg) {
|
|
47
|
+
const equals = arg.indexOf('=');
|
|
48
|
+
return equals === -1 ? [arg, undefined] : [arg.slice(0, equals), arg.slice(equals + 1)];
|
|
49
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { HookPayload } from '../types.mts';
|
|
2
|
+
import type { HookHarness } from './types.mts';
|
|
3
|
+
/**
|
|
4
|
+
* True when the process runs unattended, where merge authority is never delegated: Claude's
|
|
5
|
+
* `claude -p` (`CLAUDE_CODE_SESSION_ATTENDED=0`) or a CI marker. `extraMarkers` adds
|
|
6
|
+
* caller-specific variable names.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isAutomationContext(env: NodeJS.ProcessEnv, extraMarkers?: readonly string[]): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Parses hook stdin into a payload. Blank, invalid, or non-object input is an empty payload unless
|
|
11
|
+
* `strict`, which throws so a blocking hook fails closed instead of reading a missing or truncated
|
|
12
|
+
* payload as "nothing to judge".
|
|
13
|
+
*/
|
|
14
|
+
export declare function parseHookPayload(stdin: string, strict?: boolean): HookPayload;
|
|
15
|
+
export declare function normalizeCursorShellPayload(payload: HookPayload): HookPayload;
|
|
16
|
+
/**
|
|
17
|
+
* The harness actually calling. Grok and Cursor reuse another harness's hook configuration, so
|
|
18
|
+
* their own env and payload markers win over the declared `--harness`. Cursor resolves before
|
|
19
|
+
* `claude` is trusted because it inherits Claude's attended env when launched from a Claude session.
|
|
20
|
+
*/
|
|
21
|
+
export declare function resolveHarness(declared: HookHarness, env: NodeJS.ProcessEnv, payload: HookPayload): HookHarness;
|
|
22
|
+
/**
|
|
23
|
+
* True only when Claude Code marks the session attended (CLAUDE_CODE_SESSION_ATTENDED=1; 0 for
|
|
24
|
+
* `claude -p`). A missing or changed variable fails safe: no silent merge allow.
|
|
25
|
+
*/
|
|
26
|
+
export declare function isAttendedClaudeSession(env: NodeJS.ProcessEnv): boolean;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { isRecord } from "../payload.mjs";
|
|
2
|
+
/** Environment variables that mark an automated launch. Set to anything but empty, `0`, or `false`. */
|
|
3
|
+
const AUTOMATION_MARKERS = [
|
|
4
|
+
'CI',
|
|
5
|
+
'GITHUB_ACTIONS',
|
|
6
|
+
'GITLAB_CI',
|
|
7
|
+
'BUILDKITE',
|
|
8
|
+
'CIRCLECI',
|
|
9
|
+
'JENKINS_URL',
|
|
10
|
+
'TF_BUILD',
|
|
11
|
+
'CODEBUILD_BUILD_ID',
|
|
12
|
+
];
|
|
13
|
+
/**
|
|
14
|
+
* True when the process runs unattended, where merge authority is never delegated: Claude's
|
|
15
|
+
* `claude -p` (`CLAUDE_CODE_SESSION_ATTENDED=0`) or a CI marker. `extraMarkers` adds
|
|
16
|
+
* caller-specific variable names.
|
|
17
|
+
*/
|
|
18
|
+
export function isAutomationContext(env, extraMarkers = []) {
|
|
19
|
+
if (env.CLAUDE_CODE_SESSION_ATTENDED === '0')
|
|
20
|
+
return true;
|
|
21
|
+
return [...AUTOMATION_MARKERS, ...extraMarkers].some((name) => {
|
|
22
|
+
const value = env[name]?.trim().toLowerCase();
|
|
23
|
+
return value !== undefined && value !== '' && value !== '0' && value !== 'false';
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Parses hook stdin into a payload. Blank, invalid, or non-object input is an empty payload unless
|
|
28
|
+
* `strict`, which throws so a blocking hook fails closed instead of reading a missing or truncated
|
|
29
|
+
* payload as "nothing to judge".
|
|
30
|
+
*/
|
|
31
|
+
export function parseHookPayload(stdin, strict = false) {
|
|
32
|
+
if (stdin.trim() === '') {
|
|
33
|
+
if (strict)
|
|
34
|
+
throw new Error('hook payload is empty');
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
let parsed;
|
|
38
|
+
try {
|
|
39
|
+
parsed = JSON.parse(stdin);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (strict)
|
|
43
|
+
throw new Error(`hook payload is not valid JSON: ${error.message}`);
|
|
44
|
+
return {};
|
|
45
|
+
}
|
|
46
|
+
if (isRecord(parsed) && !Array.isArray(parsed))
|
|
47
|
+
return normalizeCursorShellPayload(parsed);
|
|
48
|
+
if (strict)
|
|
49
|
+
throw new Error('hook payload is not a JSON object');
|
|
50
|
+
return {};
|
|
51
|
+
}
|
|
52
|
+
// Cursor runs the Claude-compat hooks but names its shell tool `Shell` and reports the result as
|
|
53
|
+
// `tool_output`, a JSON string `{exitCode, output}`. Normalizing both to Claude's `Bash` +
|
|
54
|
+
// `tool_response` shape means policy reads one payload dialect. An existing `tool_response` is
|
|
55
|
+
// never overwritten.
|
|
56
|
+
export function normalizeCursorShellPayload(payload) {
|
|
57
|
+
if (payload.tool_name !== 'Shell')
|
|
58
|
+
return payload;
|
|
59
|
+
const result = cursorShellResult(payload.tool_output);
|
|
60
|
+
return {
|
|
61
|
+
...payload,
|
|
62
|
+
tool_name: 'Bash',
|
|
63
|
+
...(result === undefined || payload.tool_response !== undefined
|
|
64
|
+
? {}
|
|
65
|
+
: { tool_response: result }),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function cursorShellResult(raw) {
|
|
69
|
+
let parsed = raw;
|
|
70
|
+
if (typeof raw === 'string') {
|
|
71
|
+
try {
|
|
72
|
+
parsed = JSON.parse(raw);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (!isRecord(parsed) || typeof parsed.exitCode !== 'number')
|
|
79
|
+
return undefined;
|
|
80
|
+
return {
|
|
81
|
+
exit_code: parsed.exitCode,
|
|
82
|
+
stdout: firstString(parsed.stdout, parsed.output),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// GROK_AGENT is deliberately not a signal: it is a shell-level marker that could be exported in a
|
|
86
|
+
// Claude or Codex session.
|
|
87
|
+
function isGrokHookProcess(env) {
|
|
88
|
+
return Boolean(env.GROK_SESSION_ID || env.GROK_HOOK_EVENT);
|
|
89
|
+
}
|
|
90
|
+
// Cursor passes `cursor_version` in every payload and sets CURSOR_VERSION/CURSOR_PROJECT_DIR in the
|
|
91
|
+
// hook env. It runs the Claude-compat config, so the markers only override `claude`, never `codex`.
|
|
92
|
+
function isCursorHookProcess(declared, env, payload) {
|
|
93
|
+
return (declared === 'claude' &&
|
|
94
|
+
(typeof payload.cursor_version === 'string' ||
|
|
95
|
+
Boolean(env.CURSOR_VERSION || env.CURSOR_PROJECT_DIR)));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The harness actually calling. Grok and Cursor reuse another harness's hook configuration, so
|
|
99
|
+
* their own env and payload markers win over the declared `--harness`. Cursor resolves before
|
|
100
|
+
* `claude` is trusted because it inherits Claude's attended env when launched from a Claude session.
|
|
101
|
+
*/
|
|
102
|
+
export function resolveHarness(declared, env, payload) {
|
|
103
|
+
if (isGrokHookProcess(env))
|
|
104
|
+
return 'grok';
|
|
105
|
+
if (isCursorHookProcess(declared, env, payload))
|
|
106
|
+
return 'cursor';
|
|
107
|
+
return declared;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* True only when Claude Code marks the session attended (CLAUDE_CODE_SESSION_ATTENDED=1; 0 for
|
|
111
|
+
* `claude -p`). A missing or changed variable fails safe: no silent merge allow.
|
|
112
|
+
*/
|
|
113
|
+
export function isAttendedClaudeSession(env) {
|
|
114
|
+
return env.CLAUDE_CODE_SESSION_ATTENDED === '1';
|
|
115
|
+
}
|
|
116
|
+
// Cursor documents `stdout`; `output` is kept as a fallback for older payloads.
|
|
117
|
+
function firstString(...values) {
|
|
118
|
+
return values.find((value) => typeof value === 'string') ?? '';
|
|
119
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { HookContext, HookPolicy } from './types.mts';
|
|
2
|
+
/**
|
|
3
|
+
* Imports a `--policy` module and resolves its policy. The module's default export (or a named
|
|
4
|
+
* `policy` export) is a `HookPolicy` object or a function of the hook context that returns one.
|
|
5
|
+
* A relative path resolves against the repository root.
|
|
6
|
+
*/
|
|
7
|
+
export declare function loadHookPolicy(specifier: string, context: HookContext): Promise<HookPolicy>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { resolve } from 'node:path';
|
|
10
|
+
import { pathToFileURL } from 'node:url';
|
|
11
|
+
import { isRecord } from "../payload.mjs";
|
|
12
|
+
/**
|
|
13
|
+
* Imports a `--policy` module and resolves its policy. The module's default export (or a named
|
|
14
|
+
* `policy` export) is a `HookPolicy` object or a function of the hook context that returns one.
|
|
15
|
+
* A relative path resolves against the repository root.
|
|
16
|
+
*/
|
|
17
|
+
export async function loadHookPolicy(specifier, context) {
|
|
18
|
+
const url = pathToFileURL(resolve(context.root, specifier)).href;
|
|
19
|
+
const loaded = (await import(__rewriteRelativeImportExtension(url)));
|
|
20
|
+
const exported = selectExport(loaded);
|
|
21
|
+
const policy = typeof exported === 'function' ? await exported(context) : exported;
|
|
22
|
+
if (!isRecord(policy) || Array.isArray(policy)) {
|
|
23
|
+
throw new Error(`policy module ${specifier} must default-export a policy object or a function returning one`);
|
|
24
|
+
}
|
|
25
|
+
return validated(policy, specifier);
|
|
26
|
+
}
|
|
27
|
+
const FIELD_SHAPES = {
|
|
28
|
+
command: (value) => isRecord(value) && !Array.isArray(value),
|
|
29
|
+
checkers: Array.isArray,
|
|
30
|
+
reminder: (value) => typeof value === 'function',
|
|
31
|
+
onPayload: (value) => typeof value === 'function',
|
|
32
|
+
};
|
|
33
|
+
// A misspelled shape would silently disable the policy, so a present field must have its type.
|
|
34
|
+
function validated(policy, specifier) {
|
|
35
|
+
for (const [field, isValid] of Object.entries(FIELD_SHAPES)) {
|
|
36
|
+
if (policy[field] !== undefined && !isValid(policy[field])) {
|
|
37
|
+
throw new Error(`policy module ${specifier} has an invalid \`${field}\``);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return policy;
|
|
41
|
+
}
|
|
42
|
+
// A CommonJS module's `default` is its whole `module.exports`, so `exports.policy = {...}` shows up as
|
|
43
|
+
// `default: { policy }` beside the named `policy`; the named export is the policy then.
|
|
44
|
+
function selectExport(loaded) {
|
|
45
|
+
const wrapper = loaded.default;
|
|
46
|
+
const wrapsNamed = isRecord(wrapper) && loaded.policy !== undefined && wrapper.policy === loaded.policy;
|
|
47
|
+
return wrapsNamed ? loaded.policy : (wrapper ?? loaded.policy);
|
|
48
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HookContext, HookPolicy, PostToolUseWarning } from './types.mts';
|
|
2
|
+
/** Runs every matching checker. A checker that throws becomes one error warning, never a crash. */
|
|
3
|
+
export declare function checkFile(filePath: string, root: string, checkers?: HookPolicy['checkers']): PostToolUseWarning[];
|
|
4
|
+
/**
|
|
5
|
+
* The stdout of a post-tool-use hook: one `additionalContext` JSON object carrying the checkers'
|
|
6
|
+
* `[level] message` lines and the reminder, or an empty string when there is nothing to say. The
|
|
7
|
+
* hook is advisory and never blocks, so failures here are swallowed.
|
|
8
|
+
*/
|
|
9
|
+
export declare function postToolUseOutput(context: HookContext, policy: HookPolicy): Promise<string>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { hookFilePath, hookToolInput } from "../payload.mjs";
|
|
2
|
+
import { patchResultPaths } from "../path-references.mjs";
|
|
3
|
+
function failure(error) {
|
|
4
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
5
|
+
return { level: 'error', message: `Unexpected error in post-tool-use checks: ${message}` };
|
|
6
|
+
}
|
|
7
|
+
/** Runs every matching checker. A checker that throws becomes one error warning, never a crash. */
|
|
8
|
+
export function checkFile(filePath, root, checkers = []) {
|
|
9
|
+
return checkers.flatMap((checker) => {
|
|
10
|
+
try {
|
|
11
|
+
return checker.matches(filePath, root) ? checker.check(filePath, root) : [];
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
return [failure(error)];
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
// Edit/Write report `file_path`; Codex's apply_patch carries every touched file in its patch headers,
|
|
19
|
+
// and only files that exist afterwards are checked.
|
|
20
|
+
function editedPaths(context) {
|
|
21
|
+
const command = hookToolInput(context.payload)?.command;
|
|
22
|
+
if (typeof command === 'string' && command.startsWith('*** Begin Patch')) {
|
|
23
|
+
return patchResultPaths(command);
|
|
24
|
+
}
|
|
25
|
+
const filePath = hookFilePath(context.payload);
|
|
26
|
+
return filePath === '' ? [] : [filePath];
|
|
27
|
+
}
|
|
28
|
+
async function reminderOf(policy, context) {
|
|
29
|
+
try {
|
|
30
|
+
return (await policy.reminder?.(context)) || null;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The stdout of a post-tool-use hook: one `additionalContext` JSON object carrying the checkers'
|
|
38
|
+
* `[level] message` lines and the reminder, or an empty string when there is nothing to say. The
|
|
39
|
+
* hook is advisory and never blocks, so failures here are swallowed.
|
|
40
|
+
*/
|
|
41
|
+
export async function postToolUseOutput(context, policy) {
|
|
42
|
+
const warnings = editedPaths(context).flatMap((path) => checkFile(path, context.root, policy.checkers));
|
|
43
|
+
const lines = warnings.map(({ level, message }) => `[${level}] ${message}`);
|
|
44
|
+
const reminder = await reminderOf(policy, context);
|
|
45
|
+
const text = [...lines, ...(reminder === null ? [] : [reminder])].join('\n');
|
|
46
|
+
if (text === '')
|
|
47
|
+
return '';
|
|
48
|
+
return `${JSON.stringify({
|
|
49
|
+
hookSpecificOutput: { additionalContext: text, hookEventName: 'PostToolUse' },
|
|
50
|
+
})}\n`;
|
|
51
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { CommandPolicy, HookPayload } from '../types.mts';
|
|
2
|
+
import type { HookHarness } from './types.mts';
|
|
3
|
+
/**
|
|
4
|
+
* What a pre-tool-use hook writes and exits with. Exit 2 is the block all four harnesses honor.
|
|
5
|
+
* Claude, Codex, and Grok surface the stderr reason; Cursor surfaces the stdout JSON. Exit 0 with
|
|
6
|
+
* empty stdio is no opinion.
|
|
7
|
+
*/
|
|
8
|
+
export type PreToolUseResult = {
|
|
9
|
+
exitCode: 0 | 2;
|
|
10
|
+
stderr: string;
|
|
11
|
+
stdout: string;
|
|
12
|
+
};
|
|
13
|
+
export type PreToolUseOptions = {
|
|
14
|
+
harness?: HookHarness | undefined;
|
|
15
|
+
/** True only when the harness marked the session attended (see isAttendedClaudeSession). */
|
|
16
|
+
attended?: boolean | undefined;
|
|
17
|
+
/** See EvaluateOptions.automationContext. */
|
|
18
|
+
automationContext?: boolean | undefined;
|
|
19
|
+
cwd?: string | undefined;
|
|
20
|
+
};
|
|
21
|
+
export declare function preToolUseOutput(payload: HookPayload, policy: CommandPolicy | undefined, options?: PreToolUseOptions): PreToolUseResult;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { evaluateCommandPolicy } from "../engine.mjs";
|
|
2
|
+
export function preToolUseOutput(payload, policy, options = {}) {
|
|
3
|
+
// A policy module without a command table has no opinion, merge authority included.
|
|
4
|
+
if (policy === undefined) {
|
|
5
|
+
return { exitCode: 0, stderr: '', stdout: noOpinion(options) };
|
|
6
|
+
}
|
|
7
|
+
const decision = evaluateCommandPolicy(payload, policy, {
|
|
8
|
+
...(options.automationContext === undefined
|
|
9
|
+
? {}
|
|
10
|
+
: { automationContext: options.automationContext }),
|
|
11
|
+
...(options.cwd === undefined ? {} : { cwd: options.cwd }),
|
|
12
|
+
});
|
|
13
|
+
if (decision === null) {
|
|
14
|
+
return { exitCode: 0, stderr: '', stdout: noOpinion(options) };
|
|
15
|
+
}
|
|
16
|
+
if (decision.disposition === 'confirm') {
|
|
17
|
+
return { exitCode: 0, stderr: '', stdout: renderConfirm(policy, options) };
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
exitCode: 2,
|
|
21
|
+
stderr: `${decision.reason}\n`,
|
|
22
|
+
stdout: JSON.stringify({ decision: 'deny', reason: decision.reason }),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
// Cursor treats a hook that prints nothing as failed (and blocks the call under `failClosed`), so
|
|
26
|
+
// "no opinion" there is an empty JSON object. Every other harness reads empty stdio as no opinion.
|
|
27
|
+
function noOpinion(options) {
|
|
28
|
+
return options.harness === 'cursor' ? '{}' : '';
|
|
29
|
+
}
|
|
30
|
+
// Only an attended Claude session gets a silent allow: the human already made the merge decision by
|
|
31
|
+
// asking for it, so a second tool-level click is redundant. Codex, Cursor, Grok, and an unattended
|
|
32
|
+
// Claude session (`claude -p`) get no opinion, so the harness's own prompt or classifier decides.
|
|
33
|
+
function renderConfirm(policy, options) {
|
|
34
|
+
if (options.harness !== 'claude' || options.attended !== true) {
|
|
35
|
+
return noOpinion(options);
|
|
36
|
+
}
|
|
37
|
+
const guidance = policy.mergeGuidance === undefined ? '' : ` ${policy.mergeGuidance}`;
|
|
38
|
+
return JSON.stringify({
|
|
39
|
+
hookSpecificOutput: {
|
|
40
|
+
hookEventName: 'PreToolUse',
|
|
41
|
+
permissionDecision: 'allow',
|
|
42
|
+
permissionDecisionReason: `Interactive merge: proceeding on the human decision already made in this session.${guidance}`,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type HookIo = {
|
|
2
|
+
readStdin: () => Promise<string>;
|
|
3
|
+
stdout: {
|
|
4
|
+
write(text: string): unknown;
|
|
5
|
+
};
|
|
6
|
+
stderr: {
|
|
7
|
+
write(text: string): unknown;
|
|
8
|
+
};
|
|
9
|
+
env: NodeJS.ProcessEnv;
|
|
10
|
+
cwd: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* `vouchington hook <event> --harness <h> --policy <module>`. Pre-tool-use fails closed (exit 2) when
|
|
14
|
+
* its policy cannot load, because the gates it enforces are a security boundary; post-tool-use is
|
|
15
|
+
* advisory and always exits 0. Bad arguments exit 2 before anything is read.
|
|
16
|
+
*/
|
|
17
|
+
export declare function runHook(args: readonly string[], io: HookIo): Promise<number>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import { hookCwd } from "../payload.mjs";
|
|
3
|
+
import { parseHookArgs } from "./args.mjs";
|
|
4
|
+
import { isAttendedClaudeSession, isAutomationContext, parseHookPayload, resolveHarness, } from "./harness.mjs";
|
|
5
|
+
import { loadHookPolicy } from "./policy-module.mjs";
|
|
6
|
+
import { postToolUseOutput } from "./post-tool-use.mjs";
|
|
7
|
+
import { preToolUseOutput } from "./pre-tool-use.mjs";
|
|
8
|
+
/**
|
|
9
|
+
* `vouchington hook <event> --harness <h> --policy <module>`. Pre-tool-use fails closed (exit 2) when
|
|
10
|
+
* its policy cannot load, because the gates it enforces are a security boundary; post-tool-use is
|
|
11
|
+
* advisory and always exits 0. Bad arguments exit 2 before anything is read.
|
|
12
|
+
*/
|
|
13
|
+
export async function runHook(args, io) {
|
|
14
|
+
let parsed;
|
|
15
|
+
try {
|
|
16
|
+
parsed = parseHookArgs(args);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
io.stderr.write(`${error.message}\n`);
|
|
20
|
+
// Post-tool-use is advisory: an argument typo must not reject an already-completed tool call.
|
|
21
|
+
return args[0] === 'post-tool-use' ? 0 : 2;
|
|
22
|
+
}
|
|
23
|
+
const blocking = parsed.event === 'pre-tool-use';
|
|
24
|
+
try {
|
|
25
|
+
const payload = parseHookPayload(await io.readStdin(), blocking);
|
|
26
|
+
const context = {
|
|
27
|
+
harness: resolveHarness(parsed.harness, io.env, payload),
|
|
28
|
+
payload,
|
|
29
|
+
env: io.env,
|
|
30
|
+
root: resolve(io.cwd, parsed.root ?? '.'),
|
|
31
|
+
};
|
|
32
|
+
const policy = await loadHookPolicy(parsed.policy, context);
|
|
33
|
+
await runOnPayload(policy, context, io);
|
|
34
|
+
if (blocking) {
|
|
35
|
+
const result = preToolUseOutput(payload, policy.command, {
|
|
36
|
+
harness: context.harness,
|
|
37
|
+
attended: isAttendedClaudeSession(io.env),
|
|
38
|
+
automationContext: isAutomationContext(io.env, parsed.automationEnv),
|
|
39
|
+
// The configured root, not the hook process's directory, when the payload names none.
|
|
40
|
+
cwd: hookCwd(payload, context.root),
|
|
41
|
+
});
|
|
42
|
+
if (result.stdout !== '')
|
|
43
|
+
io.stdout.write(result.stdout);
|
|
44
|
+
if (result.stderr !== '')
|
|
45
|
+
io.stderr.write(result.stderr);
|
|
46
|
+
return result.exitCode;
|
|
47
|
+
}
|
|
48
|
+
const output = await postToolUseOutput(context, policy);
|
|
49
|
+
if (output !== '')
|
|
50
|
+
io.stdout.write(output);
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
55
|
+
io.stderr.write(`vouchington hook ${parsed.event}: ${message}\n`);
|
|
56
|
+
return blocking ? 2 : 0;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// A failing side effect (for example persisting a session id) is reported, never allowed to
|
|
60
|
+
// suppress the command policy or the post-tool-use checks.
|
|
61
|
+
async function runOnPayload(policy, context, io) {
|
|
62
|
+
try {
|
|
63
|
+
await policy.onPayload?.(context);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
67
|
+
io.stderr.write(`vouchington hook: onPayload failed: ${message}\n`);
|
|
68
|
+
}
|
|
69
|
+
}
|