Hooks

Hooks run at six points of the loop: shell commands from hooks in config, and plugin handlers registered with oi.on(event, fn). Plugin hooks run first, then shell hooks in config order.

Event When Answer
session_start the first turn of a session in this process context
user_prompt before the model sees a message block, context
before_tool before the permission check deny, input (a rewritten input)
after_tool after a tool ran, before the model sees its result content (replace), append
stop an agent is about to end its turn continue (feedback; the agent keeps working, at most 8 times a turn)
notification an approval waits; a turn ended ignored
{ "hooks": {
    "after_tool": [{ "matcher": "write|edit", "command": "zig fmt \"$OI_FILE\"" }],
    "stop":       [{ "command": "zig build test >/dev/null 2>&1 || { echo 'the tests fail: fix them' >&2; exit 2; }", "timeout_ms": 300000 }],
    "notification": [{ "command": "notify-send oi \"$OI_MESSAGE\"" }] } }
  • Input. A shell hook gets the event as JSON on stdin, plus OI_HOOK_EVENT, OI_SESSION_ID, OI_TOOL_NAME, OI_FILE and OI_MESSAGE in its environment. matcher is a regex that must match the whole tool name.
  • Exit status.
    • 0 succeeds. A JSON object on stdout is the hook's answer; for session_start and user_prompt, plain text on stdout becomes context.
    • 2 speaks with stderr: it denies the tool, blocks the prompt, appends to the tool result (after_tool), or sends the agent back to work (stop).
    • Anything else is reported and ignored.
  • Plugins. A plugin hook returns the same fields.
  • Sandbox and trust. Shell hooks run in the OS sandbox like bash; stdin is written while the output drains, and timeout_ms (default 60 s) kills a hook that hangs. Hooks from your own files (~/.config/oi/config.json, projects.json) run as configured. Hooks from a repository's shared .oi/config.json run only after you approve them ("always" saves a hook(<command>) rule), or when one of your own hook(...) rules or an auto mode you chose (flag, your config, Shift+Tab) allows them. The repository's own rules and permission_mode never approve its hooks.