hook
import "github.com/barevoidco/zelk/core/hook"Package hook defines typed, in-process hooks: Go functions zelk runs at fixed points of the loop, the main way a host customizes behaviour without forking zelk.
Gate hooks (PreToolUse, UserPromptSubmit, Stop and the others that can change what happens) run as durable effects: each invocation is logged before it runs, its decision is recorded, and replay or resume use the recorded decision without running the hook again. A gate that fails, panics or times out fails closed. Observe-only hooks, and gate hooks registered with WithAsync, run in the background, can’t change anything and are best effort: never re-run after a crash.
Results of several hooks of one event combine deterministically, by registration order: the most restrictive decision wins (deny > defer > ask > allow), at most one hook may rewrite an input or replace an output, and extra context is concatenated.
Each event constructor requires its permitted output type. Hosts register definitions; the root owner invokes and joins them through internal runtime methods. Match preserves separate wildcard patterns, including literal pipes.
Constants
Section titled “Constants”const ( DecideDeny = event.HookDeny DecideDefer = event.HookDefer DecideAsk = event.HookAsk DecideAllow = event.HookAllow)The PreToolUse decisions, from most restrictive to least.
const EventCwdChanged = runtime.EventCwdChangedEventCwdChanged names the CwdChanged hook point.
const EventInstructionsLoaded = runtime.EventInstructionsLoadedEventInstructionsLoaded names the InstructionsLoaded hook point.
const EventInterrupt = runtime.EventInterruptEventInterrupt names the Interrupt hook point.
const EventNotification = runtime.EventNotificationEventNotification names the Notification hook point.
const EventPermissionDenied = runtime.EventPermissionDeniedEventPermissionDenied names the PermissionDenied hook point.
const EventPermissionRequest = runtime.EventPermissionRequestEventPermissionRequest names the PermissionRequest hook point.
const EventPostCompact = runtime.EventPostCompactEventPostCompact names the PostCompact hook point.
const EventPostModelSwitch = runtime.EventPostModelSwitchEventPostModelSwitch names the PostModelSwitch hook point.
const EventPostToolBatch = runtime.EventPostToolBatchEventPostToolBatch names the PostToolBatch hook point.
const EventPostToolUse = runtime.EventPostToolUseEventPostToolUse names the PostToolUse hook point.
const EventPostToolUseFailure = runtime.EventPostToolUseFailureEventPostToolUseFailure names the PostToolUseFailure hook point.
const EventPreCompact = runtime.EventPreCompactEventPreCompact names the PreCompact hook point.
const EventPreModelSwitch = runtime.EventPreModelSwitchEventPreModelSwitch names the PreModelSwitch hook point.
const EventPreToolUse = runtime.EventPreToolUseEventPreToolUse names the PreToolUse hook point.
const EventSessionEnd = runtime.EventSessionEndEventSessionEnd names the SessionEnd hook point.
const EventSessionStart = runtime.EventSessionStartEventSessionStart names the SessionStart hook point.
const EventSkillLoaded = runtime.EventSkillLoadedEventSkillLoaded names the SkillLoaded hook point.
const EventStop = runtime.EventStopEventStop names the Stop hook point.
const EventSubagentStart = runtime.EventSubagentStartEventSubagentStart names the SubagentStart hook point.
const EventSubagentStop = runtime.EventSubagentStopEventSubagentStop names the SubagentStop hook point.
const EventUserPromptSubmit = runtime.EventUserPromptSubmitEventUserPromptSubmit names the UserPromptSubmit hook point.
Variables
Section titled “Variables”var ErrInvalid = runtime.ErrInvalidErrInvalid reports a nil callback or invalid event registration.
var ErrUnsupported = runtime.ErrUnsupportedErrUnsupported reports an invalid decision or unsupported runtime output.
type Base = runtime.BaseBase describes common invocation identity.
CompactInput
Section titled “CompactInput”type CompactInput = runtime.CompactInputCompactInput describes an upcoming compaction.
ContextOutput
Section titled “ContextOutput”type ContextOutput struct { Context string // Message is for the user, never the model: a warning the host shows // where the hook ran. Message string}ContextOutput adds model context at a non-blocking lifecycle point. A SubagentStart hook can’t return a Message: it runs while the child is configured, where nothing records it.
ContextOutput.WithContext
Section titled “ContextOutput.WithContext”func (o ContextOutput) WithContext(text string) ContextOutputWithContext adds model context at this lifecycle point.
Decision
Section titled “Decision”type Decision = event.HookDecisionDecision is a PreToolUse decision; the zero value leaves policy unchanged.
DeniedOutput
Section titled “DeniedOutput”type DeniedOutput struct { Note string // Message is for the user, never the model: a warning the host shows // where the hook ran. Message string}DeniedOutput adds a model note about a denied action.
DeniedOutput.WithNote
Section titled “DeniedOutput.WithNote”func (o DeniedOutput) WithNote(note string) DeniedOutputWithNote adds a denial note.
type Event = runtime.EventEvent names a hook point.
GateOutput
Section titled “GateOutput”type GateOutput struct { Block bool Reason string // Message is for the user, never the model: a warning the host shows // where the hook ran. Message string}GateOutput blocks a stop, compaction or model switch, or leaves it unchanged.
func Block(reason string) GateOutputBlock prevents a stop, compaction or model switch with the given reason.
type Hook runtime.HookHook is an immutable event callback definition registered with zelk.WithHooks. Its zero value is invalid; hosts do not invoke registered hooks directly.
Observe
Section titled “Observe”func Observe(e Event, fn func(context.Context, ObservedInput) error, opts ...Option) HookObserve registers a best-effort callback for an observation-only event.
PermissionDenied
Section titled “PermissionDenied”func PermissionDenied(m Matcher, fn func(context.Context, PermissionInput) (DeniedOutput, error), opts ...Option) HookPermissionDenied registers fn at the PermissionDenied event with its permitted output shape.
PermissionRequest
Section titled “PermissionRequest”func PermissionRequest(m Matcher, fn func(context.Context, PermissionInput) (PermissionOutput, error), opts ...Option) HookPermissionRequest registers fn at the PermissionRequest event with its permitted output shape.
PostModelSwitch
Section titled “PostModelSwitch”func PostModelSwitch(fn func(context.Context, SwitchInput) (ContextOutput, error), opts ...Option) HookPostModelSwitch registers fn at the PostModelSwitch event with its permitted output shape.
PostToolBatch
Section titled “PostToolBatch”func PostToolBatch(fn func(context.Context, Base) (ContextOutput, error), opts ...Option) HookPostToolBatch registers fn at the PostToolBatch event with its permitted output shape.
PostToolUse
Section titled “PostToolUse”func PostToolUse(m Matcher, fn func(context.Context, ResultInput) (ToolResultOutput, error), opts ...Option) HookPostToolUse registers fn at the PostToolUse event with its permitted output shape.
PostToolUseFailure
Section titled “PostToolUseFailure”func PostToolUseFailure(m Matcher, fn func(context.Context, ResultInput) (ToolResultOutput, error), opts ...Option) HookPostToolUseFailure registers fn at the PostToolUseFailure event with its permitted output shape.
PreCompact
Section titled “PreCompact”func PreCompact(fn func(context.Context, CompactInput) (GateOutput, error), opts ...Option) HookPreCompact registers fn at the PreCompact event with its permitted output shape.
PreModelSwitch
Section titled “PreModelSwitch”func PreModelSwitch(fn func(context.Context, SwitchInput) (GateOutput, error), opts ...Option) HookPreModelSwitch registers fn at the PreModelSwitch event with its permitted output shape.
PreToolUse
Section titled “PreToolUse”func PreToolUse(m Matcher, fn func(context.Context, PreToolUseInput) (PreToolUseOutput, error), opts ...Option) HookPreToolUse registers fn at the PreToolUse event with its permitted output shape.
Example
Register the definition with zelk.WithHooks; the execution owner invokes it.
decide := func(_ context.Context, in hook.PreToolUseInput) (hook.PreToolUseOutput, error) { if strings.Contains(in.Command(), "terraform apply") { return hook.Deny("apply runs only in CI"), nil } return hook.PreToolUseOutput{}, nil}definition := hook.PreToolUse(hook.Match("Bash"), decide, hook.WithName("no-apply"))result, err := decide(context.Background(), hook.PreToolUseInput{Input: []byte(`{"command":"terraform apply"}`)})if err != nil { panic(err)}fmt.Println(definition.Name(), result.Decision, result.Reason)// Output: no-apply deny apply runs only in CIOutput:
no-apply deny apply runs only in CISessionStart
Section titled “SessionStart”func SessionStart(m Matcher, fn func(context.Context, SessionInput) (ContextOutput, error), opts ...Option) HookSessionStart registers fn at the SessionStart event with its permitted output shape.
func Stop(fn func(context.Context, StopInput) (GateOutput, error), opts ...Option) HookStop registers fn at the Stop event with its permitted output shape.
Example
check := func(_ context.Context, in hook.StopInput) (hook.GateOutput, error) { if !strings.Contains(in.Text, "tests pass") { return hook.Block("Run the tests and fix any failure before you finish."), nil } return hook.GateOutput{}, nil}definition := hook.Stop(check)if err := definition.Validate(); err != nil { panic(err)}result, err := check(context.Background(), hook.StopInput{Text: "Done."})if err != nil { panic(err)}fmt.Println(result.Block)// Output: trueOutput:
trueSubagentStart
Section titled “SubagentStart”func SubagentStart(m Matcher, fn func(context.Context, SubagentInput) (ContextOutput, error), opts ...Option) HookSubagentStart registers fn at the SubagentStart event with its permitted output shape.
SubagentStop
Section titled “SubagentStop”func SubagentStop(m Matcher, fn func(context.Context, StopInput) (GateOutput, error), opts ...Option) HookSubagentStop registers fn at the SubagentStop event with its permitted output shape.
UserPromptSubmit
Section titled “UserPromptSubmit”func UserPromptSubmit(fn func(context.Context, PromptInput) (PromptOutput, error), opts ...Option) HookUserPromptSubmit registers fn at the UserPromptSubmit event with its permitted output shape.
Hook.Event
Section titled “Hook.Event”func (h Hook) Event() EventEvent returns the callback’s event point.
Hook.Matches
Section titled “Hook.Matches”func (h Hook) Matches(key string) boolMatches reports whether the callback selects key.
Hook.Name
Section titled “Hook.Name”func (h Hook) Name() stringName returns the registered name, or an empty string before automatic naming.
Hook.Validate
Section titled “Hook.Validate”func (h Hook) Validate() errorValidate reports invalid registration without invoking the callback.
Matcher
Section titled “Matcher”type Matcher = runtime.MatcherMatcher selects invocations using separate wildcard patterns.
func Match(patterns ...string) MatcherMatch selects any of patterns. With no patterns it selects every invocation. A vertical bar is a literal character, not an alternative separator.
ObservedInput
Section titled “ObservedInput”type ObservedInput = runtime.ObservedInputObservedInput describes an observation notification.
Option
Section titled “Option”type Option = runtime.OptionOption configures hook registration.
WithAsync
Section titled “WithAsync”func WithAsync() OptionWithAsync runs best-effort observation without allowing a gate decision.
WithChildren
Section titled “WithChildren”func WithChildren() OptionWithChildren applies the hook inside child sessions too.
WithName
Section titled “WithName”func WithName(name string) OptionWithName supplies a stable identity used to resume recorded gate invocations.
WithTimeout
Section titled “WithTimeout”func WithTimeout(d time.Duration) OptionWithTimeout bounds one invocation; a timed-out gate fails closed.
PermissionDecision
Section titled “PermissionDecision”type PermissionDecision stringPermissionDecision is an approval hook’s allow-or-deny answer.
const PermissionAllow PermissionDecision = "allow"PermissionAllow approves an action at the human approval gate.
const PermissionDeny PermissionDecision = "deny"PermissionDeny refuses an action at the human approval gate.
PermissionInput
Section titled “PermissionInput”type PermissionInput = runtime.PermissionInputPermissionInput describes a call needing human input or denied by policy.
PermissionOutput
Section titled “PermissionOutput”type PermissionOutput struct { Decision PermissionDecision Reason string Suggestion string // Message is for the user, never the model: a warning the host shows // where the hook ran. Message string}PermissionOutput approves or denies a request, optionally granting an offered rule.
AllowSession
Section titled “AllowSession”func AllowSession(id string) PermissionOutputAllowSession approves a permission request and grants its offered rule by ID.
PreToolUseInput
Section titled “PreToolUseInput”type PreToolUseInput = runtime.PreToolUseInputPreToolUseInput describes a tool call before policy evaluation.
PreToolUseOutput
Section titled “PreToolUseOutput”type PreToolUseOutput struct { Decision Decision Reason string Input json.RawMessage Context string // Message is for the user, never the model: a warning the host shows // where the hook ran. Message string}PreToolUseOutput can decide, rewrite a tool input or add model context.
func Allow() PreToolUseOutputAllow permits a proposed action subject to ordinary policy ceilings.
func Ask(reason string) PreToolUseOutputAsk requires a human for the proposed action.
func Defer(reason string) PreToolUseOutputDefer suspends until a human answers the proposed action.
func Deny(reason string) PreToolUseOutputDeny refuses a proposed action with a model-visible reason.
Rewrite
Section titled “Rewrite”func Rewrite(input json.RawMessage) PreToolUseOutputRewrite substitutes input that is evaluated through policy again.
PreToolUseOutput.WithContext
Section titled “PreToolUseOutput.WithContext”func (o PreToolUseOutput) WithContext(text string) PreToolUseOutputWithContext adds model context to this tool decision.
PreToolUseOutput.WithInput
Section titled “PreToolUseOutput.WithInput”func (o PreToolUseOutput) WithInput(input json.RawMessage) PreToolUseOutputWithInput rewrites the tool input; the replacement is evaluated by policy.
PromptInput
Section titled “PromptInput”type PromptInput = runtime.PromptInputPromptInput describes a submitted user prompt.
PromptOutput
Section titled “PromptOutput”type PromptOutput struct { Block bool Reason string Context string // Message is for the user, never the model: a warning the host shows // where the hook ran. Message string}PromptOutput blocks a user prompt or adds context before it is submitted.
PromptOutput.WithContext
Section titled “PromptOutput.WithContext”func (o PromptOutput) WithContext(text string) PromptOutputWithContext adds context to a user prompt decision.
ResultInput
Section titled “ResultInput”type ResultInput = runtime.ResultInputResultInput describes a completed tool call.
SessionInput
Section titled “SessionInput”type SessionInput = runtime.SessionInputSessionInput describes a session startup or resume.
StopInput
Section titled “StopInput”type StopInput = runtime.StopInputStopInput describes a candidate run ending.
SubagentInput
Section titled “SubagentInput”type SubagentInput = runtime.SubagentInputSubagentInput describes a child session starting.
SwitchInput
Section titled “SwitchInput”type SwitchInput = runtime.SwitchInputSwitchInput describes a proposed model switch.
ToolInput
Section titled “ToolInput”type ToolInput = runtime.ToolInputToolInput describes a proposed tool call.
ToolResultOutput
Section titled “ToolResultOutput”type ToolResultOutput struct { Output string Replace bool Context string Note string // Message is for the user, never the model: a warning the host shows // where the hook ran. Message string}ToolResultOutput can replace a model-visible result and add context or a note.
Replace
Section titled “Replace”func Replace(output string) ToolResultOutputReplace substitutes a tool result’s model-visible text.
ToolResultOutput.WithContext
Section titled “ToolResultOutput.WithContext”func (o ToolResultOutput) WithContext(text string) ToolResultOutputWithContext adds model context after a tool result.
ToolResultOutput.WithNote
Section titled “ToolResultOutput.WithNote”func (o ToolResultOutput) WithNote(text string) ToolResultOutputWithNote adds a classifier note after a tool result.