Documentation
¶
Overview ¶
Package hookd receives Claude Code hook payloads from the caprock-hook shim on POST /v1/hook, normalizes them into events, and hands them to the recorder. Unknown events and fields are logged and ignored, never fatal.
Index ¶
Constants ¶
const MaxBody = 4 << 20
MaxBody bounds a hook payload. tool_response can be large; 4 MiB is generous.
const RecordTimeout = 30 * time.Second
RecordTimeout bounds one hook event's write. Far longer than the shim waits on purpose: the shim's second is about not delaying Claude Code, and has nothing to do with how long the event may take to reach the database.
Variables ¶
var ErrNoSession = errors.New("hook payload without session_id")
ErrNoSession marks payloads without a session_id — nothing to attach them to.
var ErrUnknownEvent = errors.New("unknown hook event")
ErrUnknownEvent marks hook_event_name values hookd does not consume.
Functions ¶
func EffortLevel ¶ added in v0.167.0
EffortLevel is level when it is one of the reasoning efforts Claude Code names in a hook payload's `effort.level` — "low", "medium", "high", "xhigh", "max" (its /effort levels; "medium" and "high" are the ones observed on a real machine) — and "" for anything else. Only a known word is stored and shown: the field is Claude Code's, and a value it starts sending tomorrow is not ours to print until it is named here.
Types ¶
type Handler ¶
type Handler struct {
Token string
Recorder Recorder
Log *slog.Logger
Now func() time.Time
// OnAccepted is called after a successful record (loop detector hook-in).
OnAccepted func(res rollup.Result)
// Decide answers Stop events (Phase 2). nil ⇒ empty 204 reply.
Decide func(ctx context.Context, p Payload) []byte
// Handoff answers SessionStart: what the last session in this repository
// left behind, returned to the one now opening. nil or empty ⇒ 204, and
// the session starts exactly as it does today.
Handoff func(ctx context.Context, p Payload) []byte
// Observe sees every payload that names a session, PermissionRequest
// included, before it is recorded. It may write to the database — a new
// permission prompt is stored before the hook is answered, so a crash
// cannot lose it (ADR-035) — and must not otherwise block.
Observe func(p Payload)
// AfterMain sees every main-thread event once it is recorded, with
// continued set on a Stop that the Decide reply turned back into a
// running turn. The message queue (ADR-053) reads a turn's end from it.
// It must not block: the shim is waiting for this answer.
AfterMain func(ev *event.Event, continued bool)
// Annotate may add to a main-thread event before it is recorded: the
// message queue marks a prompt that came from Telegram (ADR-081). It
// must not block.
Annotate func(ev *event.Event)
}
Handler serves POST /v1/hook.
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the contract: bearer-token gated, 204 on success, 401 on bad token, 400 on unparsable body, 200 with a JSON body only for Stop decisions (Phase 2). Unknown events return 204 (ignored, logged).
type Payload ¶
type Payload struct {
SessionID string `json:"session_id"`
PromptID string `json:"prompt_id"`
TranscriptPath string `json:"transcript_path"`
Cwd string `json:"cwd"`
HookEventName string `json:"hook_event_name"`
PermissionMode string `json:"permission_mode"`
Effort json.RawMessage `json:"effort"` // {level}; read by effortOf
AgentID string `json:"agent_id"`
AgentType string `json:"agent_type"`
ToolName string `json:"tool_name"`
ToolInput json.RawMessage `json:"tool_input"`
ToolUseID string `json:"tool_use_id"`
Prompt string `json:"prompt"`
StopReason string `json:"stop_reason"`
Source string `json:"source"` // SessionStart
Model string `json:"model"` // SessionStart (optional)
Trigger string `json:"trigger"` // PreCompact
Reason string `json:"reason"` // SessionEnd
Error string `json:"error"` // StopFailure / PostToolUseFailure
// PermissionSuggestions is what PermissionRequest's "don't ask again"
// option would add, verbatim.
PermissionSuggestions json.RawMessage `json:"permission_suggestions"`
}
Payload is the subset of hook JSON that hookd reads. The body is stored as the event payload with only its tool_input and tool_response bounded (capBody), so nothing here limits what the UI can show.