AIToday
Large Language ModelsAI Safety & AlignmentZenn AI/MLPublished: Oct 15, 2026, 10:00 JST

A PreToolUse safety hook stops AI agents before they break rules

A PreToolUse safety hook stops AI agents before they break rules

3 Key Points

  1. What happened

    The author added a PreToolUse hook to a local Cline and Ollama setup, plus a guard.mjs checker, that cancels tool calls violating three rules and returns the reason to the AI.

  2. Why it matters

    Rules alone are requests an AI can forget mid-task, so this hook forces a stop; the guard sits outside the workspace so the AI cannot edit it.

Not sure about something? Ask the AI

Questions and answers are published on this page.

Summaries like this, in your inbox every morning.

Context & Analysis

The problem this addresses is not malicious AI but helpful AI. Asked to fix a missing package or find a config, an agent may install software system-wide, search the home folder, or call an API to test something — all in good faith, and all capable of breaking a work machine, peeking outside a project, or leaking data.

The author's design splits enforcement into two layers. Rules in a file tell the AI what not to do and how to behave when stopped; the safety hook then inspects the actual tool call before it runs and cancels it if it violates the rules. The hook is written to fail closed: if the checker cannot run or returns nothing, the operation is stopped as a precaution.

The checker sorts tool calls by type — file reads and writes, commands, web access — and matches them against patterns paired with the reason for stopping. That reason is passed back to the AI, which is expected to shift toward asking the user to do the operation instead. Some operations are deliberately allowed, such as pip install inside a project's own virtual environment, to avoid blocking so much that users disable the hook. Web access is restricted to a small self-written MCP server that only issues GET requests, sends no body, strips cookies and auth headers, caps URL and query length, refuses localhost and internal IP addresses, and logs every request. Testing covered 49 decision cases and 11 run cases under the same launch method as Cline, and confirmed fail-closed behavior when guard.mjs was deleted.

FAQ
What exactly does the hook block?
It stops operations that damage the PC environment, change settings like global git or npm config, access outside the workspace even for reading, or send data externally such as curl or git push.
Why not just tell the AI not to do those things?
Rules are only requests, and models can forget them mid-task or decide this time is an exception. Smaller local models follow instructions less reliably, so a hook that runs before every tool call enforces the rule.
How long does one check take?
About 0.3 seconds per decision, according to the author's measurements.

AI news that matters for your work, delivered every morning.

Pick your industry and the AI tools you use, and get news related to your work every day.

Free · 30 seconds with Google · unsubscribe anytimeWhat is AIToday? →

Ask AI

Ask AI anything about this article. The AI reads this article, earlier AIToday articles, and Wikipedia, and cites its sources. Q&As are published on this page for other readers too.

Questions and answers are published on this page.

Related Articles

Next articleWordCraft, a Rust-built open-source Word alternative, hits v0.6.1