Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150import type { PreToolUseInput, PreToolUseOutput,} from '@monochromatic-dev/claude-code-plugin-hook-type/ts';import { BUN_TEST_BAN_REASON, invokesBunTest,} from '@monochromatic-dev/agent-harness-shared-shell-command-analyzer/ts';import type { ReadonlyDeep, } from 'type-fest';
/** Output returned by the guardrail handler. Either a typed deny response or the empty pass-through `{}`. Every {@link PreToolUseOutput} field is optional, so `{}` is itself a valid {@link PreToolUseOutput}; no separate empty type is needed. */type GuardrailOutput = PreToolUseOutput;
/** Deny response for misleading `bun test` invocations. */const BUN_TEST_DENY_OUTPUT: GuardrailOutput = { hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', permissionDecisionReason: BUN_TEST_BAN_REASON, },};
/** Build deny response for Agent resume polling. @param resume - Agent resume identifier @returns deny response explaining automatic background notifications @example ```ts agentResumeDenyOutput('agent-id'); ``` */function agentResumeDenyOutput(resume: string,): GuardrailOutput { return { hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', permissionDecisionReason: [ `Blocked: Agent resume call (agent ID: ${resume}).`, 'Background agents notify automatically on completion.', 'Do not poll or resume running agents; wait for the notification.', "If you need the result now, use TaskOutput to check the agent's status.", ] .join(' ',), }, };}
/** Guard for Agent and Bash tool calls. @param event - parsed {@link PreToolUseInput} event from Claude Code @returns deny response with reason, or `{}` to allow tool call @example ```ts guardrailHandler({ tool_name: 'Bash', tool_input: { command: 'mise run //pkg:test' }, ... }); ``` */function guardrailHandler(event: ReadonlyDeep<PreToolUseInput>,): GuardrailOutput { if (event.tool_name === 'Bash') { /** Bash command string extracted defensively; `undefined` when field is absent. */ const command = 'command' in event.tool_input ? event.tool_input .command : undefined;
if (((typeof command) === 'string') && invokesBunTest(command,)) return BUN_TEST_DENY_OUTPUT;
return {}; }
if (event.tool_name !== 'Agent') return {};
/** Agent's `resume` field; presence triggers no-polling deny path. */ const resume = 'resume' in event.tool_input ? event.tool_input .resume : undefined;
if ((typeof resume) === 'string') return agentResumeDenyOutput(resume,);
return {};}
/** Parses raw stdin as a {@link PreToolUseInput}. Input is trusted; it comes from Claude Code's hook dispatch system. @param raw - JSON payload from Claude Code stdin @returns parsed PreToolUse event @example ```ts const event = guardrailParser(await text(process.stdin)); ``` */function guardrailParser(raw: string,): PreToolUseInput { // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- trusted JSON contract from Claude Code hook system return JSON.parse(raw,) as PreToolUseInput;}
/** Serializes guardrail output for stdout. No trailing newline; matches Claude Code's wire convention. @param output - {@link GuardrailOutput} handler result to serialize @returns JSON string for stdout @mutates output - `JSON.stringify` may invoke `toJSON`, getters, or proxy traps. @example ```ts process.stdout.write(guardrailWriter({})); ``` */function guardrailWriter(output: GuardrailOutput,): string { return JSON.stringify(output,);}
export type { GuardrailOutput, };
export { guardrailHandler, guardrailParser, guardrailWriter,};