Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Hooks

How a pack hook runs, from the tool's command to the hook's result. This guide is for pack authors and extension authors. Everything in it is api = 1. What api = 1 promises, and what a 1.x release may add, is on the stability page.

A pack declares a hook once, in tool-neutral terms. The core builds one command line for it, the extension makes the tool run that command, and a runtime the core ships calls the hook's Lua module. The tool-specific parts, turning the tool's payload into a neutral event and the hook's result back into the tool's output, belong to the extension's translate.lua.

Written for extension authors in extensions.md. The core's side of planning is in plan-and-apply.md.

1. Layout

Every tool that has a lua hook gets three files under <root>, the directory its extension names:

<root>/runtime.lua                 written by the core (below)
<root>/translate.lua               written by the extension (required)
<root>/<pack>/hooks/enforce.lua    a root file, written by the core
  • runtime.lua is written by the core, the same bytes for every tool (crates/agentstacks/assets/runtime.lua).
  • translate.lua is written by the extension, one per tool.
  • <root>/<pack>/hooks/... are the pack's own files, written by the core as root files.

airsl's require root is the script's directory. A hook run of {{root core}}/hooks/enforce.lua therefore becomes the module core.hooks.enforce.

2. Rules for a pack hook

A hook in pack.toml (or in native/<tool>/hooks.toml) is checked when the pack loads.

  • grants.policy = "pure" is refused in api = 1. confined is the only allowed value:
    • hooks[1].grants.policy: "pure" is not supported in api 1 (airsl has no require under pure, airsstack/airsl#12)
    • hooks[1].grants.policy: "foo" (expected confined)
  • Each segment of the run path (after the {{root <pack>}} form, with .lua removed from the last one) must be require-able: letters, digits, _ and - only. This also applies to native/<tool>/hooks.toml.
    • hooks[1].run: "{{root core}}/hooks/my.hook.lua" segment "my.hook" may use only letters, digits, _ and -

The run-segment refusal quotes run as written and reports only the first bad segment. It applies only to a run that ends in .lua, because other values are already refused by the .lua rule.

3. Command line

The core builds this argv for each lua hook:

airsl run --fail-open --policy confined
  [--allow-read <p>]… [--allow-write <p>]… [--allow-env <N>]… [--allow-exec <b>]…
  <root>/runtime.lua <module> <may_block: 0|1> <kind: neutral|native> [args…]
  • The core builds it with project-relative paths. A pack hook's grants map one to one onto the --allow-* flags.
  • The extension's duty: make the tool run this command with the project directory as its working directory. Example: claude extension, cd "$CLAUDE_PROJECT_DIR" && ….
  • A project-relative grant then resolves against that working directory: --allow-read .tool/agentstacks/core lets the hook read .tool/agentstacks/core/data.txt. Without the grant, a read is denied: fs.read denied: … is outside the granted read roots: none are granted.
  • --fail-open is fixed. A missing module exits 0 silently. Without it, airsl prints module `core.hooks.missing` not found under … and exits 1.
  • Elements may hold a {{var}} value the extension supplied (an expression such as $TMPDIR). Turning the argv into the tool's command string, quoting included, is the extension's job.

4. runtime.lua

The core writes <root>/runtime.lua when a pack of the tool has any lua hook, neutral or native, whether or not the extension later skips it. This is its full text, byte for byte:

-- agentstacks hook runtime, api 1.
-- usage: airsl run ... runtime.lua <hook-module> <may_block: 0|1> <kind: neutral|native> [args...]
local translate = require("translate")
local name, may_block, kind = arg[1], arg[2] == "1", arg[3]
local args = {}
for i = 4, #arg do
  args[#args + 1] = arg[i]
end
local payload = airsstack.hook.payload()
local hook

-- Runs the hook on one event; a block from a hook that is not may_block is dropped with a line.
local function run(event)
  hook = hook or require(name)
  event.args = args
  local result = hook(event)
  if type(result) == "table" and result.block ~= nil and not may_block then
    airsstack.stdio.error("agentstacks: hook " .. name .. " tried to block but is not declared may_block; reason: "
      .. tostring(result.block) .. "\n")
    result.block = nil
  end
  return result
end

local result
if kind == "native" then
  result = run({ payload = payload })
else
  local event = translate.event(payload)
  if event == nil then
    return
  end
  if event.event ~= nil then
    result = run(event)
  else
    -- A list of events: the hook runs once per event, in order, until one blocks.
    local contexts = {}
    for _, each in ipairs(event) do
      local one = run(each)
      if type(one) == "table" then
        if one.context ~= nil then
          contexts[#contexts + 1] = tostring(one.context)
        end
        if one.block ~= nil then
          result = { block = one.block }
          break
        end
      end
    end
    if #contexts > 0 then
      result = result or {}
      result.context = table.concat(contexts, "\n")
    end
  end
end
local out = translate.result(result, payload)
if out ~= nil then
  airsstack.stdio.write(out)
end

What it does for each run:

RunOutput
context{"context":"event=tool.before session=s1 args=--manifest,x.json read=true:hello\n …"}
block from a hook that isn't may_blockstdout empty; stderr agentstacks: hook core.hooks.blk tried to block but is not declared may_block; reason: edits to tool.before are not allowed
same hook with may_block 1{"decision":"block","reason":"edits to tool.before are not allowed"}
native{"context":"native payload tool_name=Edit event=nil"}
context and block from a hook that isn't may_blockstdout {"context":"keep this context"}; the block line on stderr
translator returns nil (an event it does not map)no output, exit 0

The table above is an example: claude extension (its translate.lua, with tool_name=Edit as the tool's own payload field). A real tool's translator decides the actual output text.

M.event may return a list of events instead of one. The rules:

CaseResult
M.event returns a table with event setone event; the result is what the hook returned, except that a block from a hook that is not may_block is dropped with the stderr line
M.event returns a list (a table with no event key)the hook runs once per element, in order; each gets args
a list, may_block 1, an element blocksthe loop stops; the result is { block = <that block> }, plus context = <contexts so far, joined with "\n"> when any element returned one (the blocking element's context included)
a list, may_block 0, an element blocksthe stderr line as above, the block dropped, the loop goes on
a list where no hook returns context or block, where every block was dropped and no context came back, or an empty listM.result gets nil
native kindone event { payload = <raw>, args = … }
a context that is not a stringin a list it is joined after tostring; a single event passes it through unchanged

The hook module is required only when a hook runs, so a translator returning nil never loads it.

airsstack.stdio (error, write) and airsstack.hook.payload exist under confined.

5. Neutral protocol

Event (built by the translator):

FieldWhen
eventalways: one of the hook events of the pack format (session.start, session.end, prompt.submit, tool.before, tool.after, agent.stop)
session_id, cwdalways
agentalways; nil = the main agent
agent_idoptional; the tool's id for the agent instance, nil when the tool has none
argsalways (added by the runtime)
sourcesession.start: startup, resume, clear or compact
promptprompt.submit
tool (read, edit or write), pathtool.before, tool.after
last_messageagent.stop

There is no raw payload on a neutral event. A native hook gets { payload = <raw>, args = … }.

Result (returned by the hook): nil, { context = "<text>" }, { block = "<reason>" }, or both.

Native hooks reach the extension in the same hook shape as a pack hook: kind, event, match as the list the pack format reader holds, may_block, and command (a lua hook) or text (a message hook).

6. Translator contract

<root>/translate.lua is one file per tool, written by the extension. It returns a module with two functions:

  • M.event(payload) returns the neutral event, a list of neutral events, or nil for an event that is not for this runtime (the hook is then not run). A list makes the runtime run the hook once per event (section 4).
  • M.result(result or nil, payload) returns the string to print, or nil. payload is the raw payload for neutral and native hooks alike. The runtime passes it because the payload can be read only once.

The file is required when runtime.lua is written and at least one lua hook is covered by a write. Without it the plan is refused: claude/adapter.lua: lua hooks need <root>/translate.lua (write it), with <root> filled in.

7. may_block

A hook declares may_block to say it may stop the tool. A block from a hook that is not declared may_block is dropped with one line on stderr, and any context it returned is kept. The behaviour table in section 4 shows both outcomes.

8. Filtering and message hooks

Filtering by match and agents is the extension's job, in the tool's own config. The runtime does not filter.

Message hooks (kind = "message") never reach the runtime. The extension renders them natively.

9. Shims for other hook languages

When a tool demands another hook language, the extension writes a thin shim. Example: OpenCode hooks are JavaScript plugins (.js), so its shim is JavaScript. The shim only runs the command line of section 3 and forwards the payload and the result. It is an ordinary write, shown in the plan like any other file: the core gives it nothing new.

10. airsl on the machine

Hook commands run on the airsl binary found on PATH. When a locked pack declares requires.airsl, the core adds a require_binary for airsl with the range the packs need, and records the installed version in the lock's [airsl] table (the pin). After install, doctor is the check that airsl is still there and still the pinned version. The tables and messages are in plan-and-apply.md and plan-and-apply.md.