hooks

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatList

func FormatList(config Config, diagnostics []Diagnostic) string

func IsValidEvent

func IsValidEvent(event Event) bool

IsValidEvent reports whether event is one Zero recognizes.

func WriteConfig

func WriteConfig(path string, config Config) error

Types

type AppendCompletedInput

type AppendCompletedInput struct {
	HookID     string
	Event      Event
	Matcher    string
	ToolCallID string
	Status     AuditStatus
	Results    []AuditResult
	DurationMs int
}

type AppendStartedInput

type AppendStartedInput struct {
	HookID     string
	Event      Event
	Matcher    string
	ToolCallID string
	Commands   []AuditCommand
}

type AuditCommand

type AuditCommand = Command

type AuditEvent

type AuditEvent struct {
	Sequence   int            `json:"sequence"`
	CreatedAt  string         `json:"createdAt"`
	Type       string         `json:"type"`
	HookID     string         `json:"hookId"`
	Event      Event          `json:"event"`
	Matcher    string         `json:"matcher,omitempty"`
	ToolCallID string         `json:"toolCallId,omitempty"`
	Commands   []AuditCommand `json:"commands,omitempty"`
	Status     AuditStatus    `json:"status,omitempty"`
	Results    []AuditResult  `json:"results,omitempty"`
	DurationMs int            `json:"durationMs,omitempty"`
}

type AuditResult

type AuditResult struct {
	ExitCode int    `json:"exitCode"`
	Stdout   string `json:"stdout,omitempty"`
	Stderr   string `json:"stderr,omitempty"`
}

type AuditStatus

type AuditStatus string
const (
	AuditCompleted AuditStatus = "completed"
	AuditError     AuditStatus = "error"
	AuditBlocked   AuditStatus = "blocked"
)

type AuditStore

type AuditStore struct {
	// contains filtered or unexported fields
}

func NewAuditStore

func NewAuditStore(options AuditStoreOptions) (*AuditStore, error)

func (*AuditStore) AppendCompleted

func (store *AuditStore) AppendCompleted(input AppendCompletedInput) (AuditEvent, error)

func (*AuditStore) AppendStarted

func (store *AuditStore) AppendStarted(input AppendStartedInput) (AuditEvent, error)

func (*AuditStore) ReadEvents

func (store *AuditStore) ReadEvents() ([]AuditEvent, error)

ReadEvents deliberately does not acquire store.mu. append holds store.mu while writing a single O_APPEND JSONL record, and lock-free readers may miss or skip an in-progress append while still avoiding deadlocks with append's read step.

type AuditStoreOptions

type AuditStoreOptions struct {
	AuditPath string
	Now       func() time.Time
}

type Command

type Command struct {
	Command string   `json:"command"`
	Args    []string `json:"args"`
}

type Config

type Config struct {
	Enabled bool         `json:"enabled"`
	Hooks   []Definition `json:"hooks"`
}

type ConfigSource

type ConfigSource string
const (
	SourceUser    ConfigSource = "user"
	SourceProject ConfigSource = "project"
)

type ConfigStore

type ConfigStore struct {
	// contains filtered or unexported fields
}

func NewConfigStore

func NewConfigStore(options StoreOptions) (*ConfigStore, error)

func (*ConfigStore) List

func (store *ConfigStore) List() (Config, error)

func (*ConfigStore) Remove

func (store *ConfigStore) Remove(hookID string) (bool, error)

func (*ConfigStore) SetEnabled

func (store *ConfigStore) SetEnabled(hookID string, enabled bool) (bool, error)

func (*ConfigStore) Upsert

func (store *ConfigStore) Upsert(hook Definition) (Definition, error)

type Definition

type Definition struct {
	ID          string   `json:"id"`
	Name        string   `json:"name,omitempty"`
	Description string   `json:"description,omitempty"`
	Event       Event    `json:"event"`
	Matcher     string   `json:"matcher,omitempty"`
	Command     string   `json:"command"`
	Args        []string `json:"args"`
	Enabled     bool     `json:"enabled"`
}

func Select

func Select(config Config, input SelectInput) []Definition

type Diagnostic

type Diagnostic struct {
	Kind      DiagnosticKind `json:"kind"`
	Message   string         `json:"message"`
	Source    ConfigSource   `json:"source,omitempty"`
	Path      string         `json:"path,omitempty"`
	HookID    string         `json:"hookId,omitempty"`
	FieldPath string         `json:"fieldPath,omitempty"`
}

type DiagnosticKind

type DiagnosticKind string
const (
	DiagnosticIO        DiagnosticKind = "io"
	DiagnosticJSON      DiagnosticKind = "json"
	DiagnosticSchema    DiagnosticKind = "schema"
	DiagnosticDuplicate DiagnosticKind = "duplicate"
)

type DispatchInput

type DispatchInput struct {
	Event      Event
	ToolName   string // for beforeTool/afterTool matching
	ToolCallID string
	// Payload is serialized to JSON and written to each hook command's stdin so a
	// hook can inspect the tool call, its result, or session context.
	Payload any
}

DispatchInput describes one lifecycle point at which hooks may run.

type DispatchOutcome

type DispatchOutcome struct {
	Ran       int    // hooks that executed
	Blocked   bool   // a blocking-event hook exited non-zero, vetoing the action
	BlockedBy string // ID of the hook that blocked (empty unless Blocked)
	Reason    string // the blocking hook's stderr/stdout, for surfacing to the model
	// Messages collects the output (stdout, else stderr) of each hook that
	// produced any, in run order. afterTool validators use this to feed results
	// (e.g. a formatter diff or vet warning) back to the model on the tool result.
	Messages []string
}

DispatchOutcome reports what happened for one Dispatch call.

type Dispatcher

type Dispatcher struct {
	// contains filtered or unexported fields
}

Dispatcher selects and runs the hooks configured for a lifecycle event, recording each run to the audit store. A beforeTool hook that exits non-zero blocks the tool; hooks for other events are advisory (failures are recorded but do not interrupt the run).

func NewDispatcher

func NewDispatcher(options DispatcherOptions) *Dispatcher

NewDispatcher builds a Dispatcher. A zero/empty config yields a dispatcher that runs nothing, so callers can always construct one unconditionally.

func (*Dispatcher) Dispatch

func (dispatcher *Dispatcher) Dispatch(ctx context.Context, input DispatchInput) DispatchOutcome

Dispatch runs every enabled hook configured for the input event (and matcher, for tool events). It returns once all hooks have run, or early if a blocking hook vetoes the action.

type DispatcherOptions

type DispatcherOptions struct {
	Config  Config
	Audit   *AuditStore   // optional; when set, every run is recorded
	Cwd     string        // working directory for hook commands
	Env     []string      // extra environment entries appended to the process env
	Timeout time.Duration // per-command timeout (defaults to defaultHookTimeout)
	// contains filtered or unexported fields
}

DispatcherOptions configures a Dispatcher.

type Event

type Event string
const (
	EventBeforeTool      Event = "beforeTool"
	EventAfterTool       Event = "afterTool"
	EventSessionStart    Event = "sessionStart"
	EventSessionEnd      Event = "sessionEnd"
	EventSpecialistStart Event = "specialistStart"
	EventSpecialistStop  Event = "specialistStop"
)

func KnownEvents

func KnownEvents() []Event

KnownEvents returns the hook events Zero recognizes, in dispatch order.

type LoadOptions

type LoadOptions struct {
	Cwd               string
	Env               map[string]string
	UserConfigPath    string
	ProjectConfigPath string
}

type LoadResult

type LoadResult struct {
	Config      Config       `json:"config"`
	Diagnostics []Diagnostic `json:"diagnostics"`
	Paths       Paths        `json:"paths"`
}

func LoadConfig

func LoadConfig(options LoadOptions) (LoadResult, error)

type Paths

type Paths struct {
	UserConfigPath    string `json:"userConfigPath"`
	ProjectConfigPath string `json:"projectConfigPath"`
	AuditPath         string `json:"auditPath"`
}

func ResolvePaths

func ResolvePaths(options ResolvePathOptions) (Paths, error)

type ResolvePathOptions

type ResolvePathOptions struct {
	Cwd string
	Env map[string]string
}

type SelectInput

type SelectInput struct {
	Event    Event
	ToolName string
}

type StoreOptions

type StoreOptions struct {
	ConfigPath string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL