Skip to content

feat: opt-in process hooks at the tool boundary (PreToolUse/PostToolUse, default OFF) - #113

Merged
ajianaz merged 1 commit into
developfrom
feat/tool-hooks
Sep 22, 2026
Merged

ajianaz merged 1 commit into
developfrom
feat/tool-hooks

Conversation

@ajianaz

@ajianaz ajianaz commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

What (description of the change)

Adds opt-in process hooks at the tool boundary (pattern reference: ZCode's hook surface, Apache-2.0 — pattern only, simplified to a deny-only v0 contract):

  • --on-pretool <cmd> / --on-posttool <cmd> (global, repeatable, default OFF — no flags = zero behavioral or performance change).
  • Contract: the hook process receives ONE JSON object on stdin ({"event":"pretool"|"posttool","tool":...,"input":...,"ok":...}); exit code 2 = DENY (pre-hooks only: the call settles as a durable pre-hook denial error and the loop replans — same replan shape as an approval denial); any other non-zero exit / timeout / spawn failure is a logged NON-BLOCKING hook failure. argv execution only, 5s default timeout, 32 KiB stdout cap — built on the existing run_with_timeout_stdin discipline.
  • Scope: fires for Write/Destructive tools only; ReadOnly tools stay zero-overhead (cora-caught MAJOR fixed: the post-hook snapshot was originally outside the ReadOnly guard).
  • Wiring: hooks live on the ToolRegistry (shell-tools-gated module tole_core::hooks); the turn loop consults them after the approval gate (pre) and around execute (post, observe-only). The resume/replay path keeps its approval-freshness semantics unchanged.

Deferred on purpose: Stop-hook continue:true (extra model steps), workspace hook trust/manifest digests, hook output → model context injection.

Why (the problem you're solving)

Closes #110. Tole's approval policy is compile-time: anything an owner wants gated beyond the built-in risk tiers (org rules, SIEM logging, project-specific prohibitions) required a code change. A tiny process-hook surface buys policy injection at the same trust boundary as the existing Approver trait — default OFF, deny-only, auditable through the durable log.

Testing (how you verified this works)

  • 6 hook unit tests: exit 0 noop, exit 2 deny (with/without stdout reason), non-2 exit = hook failure, timeout = hook failure, stdin payload integrity (event/tool/input/ok reach the hook). Script-file hooks per the documented v0 contract.
  • Full workspace suites green; --no-default-features (mobile) profile builds and tests green (hooks module is shell-tools-gated like the subprocess helper it uses); clippy clean.
  • cora review loop: first pass caught a real MAJOR (post-hooks firing for ReadOnly tools, contradicting the documented contract) — fixed and re-reviewed to "No issues found".
  • Live E2E (glm-5.1, release binary): with --on-pretool guard.sh (exit 2) + --allow run_command, an echo mission settles with a durable {"error":"pre-hook denial","detail":"run_command: denied by pretool hook: echo-command-forbidden-by-guard"} and the command never executes; the identical control run without hooks completes (CHAIN-OK).

@github-actions

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

Cora-caught MAJOR: the post-hook snapshot sat outside the ReadOnly
guard, so with hooks wired every read_file call spawned the post-hook
processes — contradicting the documented zero-overhead contract for
ReadOnly tools. Gate the snapshot on risk and keep both notify calls
on the snapshot.
@ajianaz
ajianaz merged commit 116461c into develop Sep 22, 2026
16 checks passed
@ajianaz
ajianaz deleted the feat/tool-hooks branch September 22, 2026 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: opt-in process hooks at the tool boundary (PreToolUse/PostToolUse, default OFF)

1 participant