EN·ES·DE·PT·FR
⌘K

Hooks and policies

Hooks are how behaviour is added to the loop. The kernel stays small; policy, memory, logging and limits are all hooks.

Hook has default no-op methods; implement the ones you need: on_run_start, edit_transcript, before_model, after_model, before_tool, after_tool, on_stop, on_run_end, among others.

A hook that blocks tool calls named in a deny list:

rust
use aidaemon_sdk::prelude::*;
use aidaemon_sdk::{ToolCall, ToolDecision, ToolInfo};
use async_trait::async_trait;

struct DenyTools(Vec<String>);

#[async_trait]
impl Hook for DenyTools {
    async fn before_tool(
        &self,
        _ctx: &HookContext<'_>,
        call: &ToolCall,
        _tool: &ToolInfo,
    ) -> ToolDecision {
        if self.0.contains(&call.name) {
            // The reason goes back to the model as the tool result.
            ToolDecision::Deny { reason: format!("`{}` is not allowed here", call.name) }
        } else {
            ToolDecision::Allow
        }
    }
}

// preset("helper").hook(DenyTools(vec!["delete_file".into()]))

Deny, rewrite, interrupt

ToolDecision also has Rewrite { arguments } (change the arguments) and Interrupt { .. } (pause the run, for example to ask a person). An interrupted run returns RunStatus::Interrupted; continue it, in this or another process, with agent.resume(outcome.state, Resolution::Approve) or Resolution::Deny { reason }.

rust
let mut outcome = agent.run("cli:me", "Clean up the build folder").await?;
while let RunStatus::Interrupted { interruption } = &outcome.status {
    println!("approve {}?", interruption.call.name);
    outcome = agent.resume(outcome.state, Resolution::Approve).await?;
}

The state is serde-serializable, so it can sit in a database until someone answers. After a decline, the kernel refuses any call that would do the same thing again in that run. The approvals and resume_after_crash examples show the full round trips.

Ready-made policies

Hooks live in aidaemon_sdk::policies (feature policies). Every decision comes from typed state (annotations, access manifests, receipts, message origins) or structural parsing, never from matching words in model prose.

PolicyWhat it does
ApprovalPolicy + ApprovalModepauses a run for the owner before destructive (or all write) calls; ApprovalRules and ApprovalGrants remember decisions
ScopeGuardkeeps calls inside the directories you allow, from each call's access manifest
EgressGuardblocks outbound arguments that contain secret shapes or literal secrets
Redactorremoves secrets from tool output
UntrustedOutputmarks tool output as data, not instructions
SameToolStreakGuardstops a run that keeps polling the same tool without progress
ContextWindowkeeps requests inside a token budget by eliding old tool results in coarse steps, so the prompt prefix stays stable
ConversationCompactionsummarizes older turns
ProjectInstructions, NestedInstructionsload AGENTS.md files into the request
MemoryInjectionputs recalled memory into every model request (see Memory)
TracingSink, OtelSink (otel)event stream to logs or OpenTelemetry
Order matters
Hooks run in registration order. Register request injectors (ProjectInstructions, MemoryInjection) before ContextWindow, and Redactor before UntrustedOutput. Before tools run the order is pinned: authority and scope, then egress, then untrusted handling, then approvals last, so the owner is only asked about calls everything else allows. The preset wires its defaults in this order.

A check on what the agent actually did, rather than what it said, is a few lines of on_stop reading outcome.receipts(); there is no separate completion-check policy.