Help us shape the future of Go! Take the 2026 Go Developer Survey

hook

package
v0.69.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package hook is part of the GoFastr harness.

See docs/harness-architecture.md for the architecture this package implements.

Package hook implements lifecycle hooks: shell commands the harness runs at well-known event points. Per § Hook timeouts:

  • Per-event default timeouts (5/30/30/60/5/5).
  • SIGTERM at deadline, SIGKILL at deadline+5s.
  • stdout/stderr capped at 64KB.
  • Non-zero exit emits HookError; deadline emits HookTimeout.

Hooks are SHA-256 hashed (rule 13). Project-local hooks (`<repo>/.gofastr/harness/hooks/`) are off unless --allow-project-hooks is set.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultTimeout

func DefaultTimeout(e Event) time.Duration

DefaultTimeout returns the per-event default timeout. Profiles may override per-hook via timeout_ms in TOML.

Types

type Event

type Event string

Event names; constants mirror the doc.

const (
	EventSessionStart     Event = "SessionStart"
	EventUserPromptSubmit Event = "UserPromptSubmit"
	EventPreToolUse       Event = "PreToolUse"
	EventPostToolUse      Event = "PostToolUse"
	EventCompact          Event = "Compact"
	EventStop             Event = "Stop"
)

type Hook

type Hook struct {
	Event   Event
	Command string        // shell command, run via /bin/sh -c
	Timeout time.Duration // 0 = use DefaultTimeout(Event)
	SHA256  string        // populated by the TOFU loader

	// Source tells the runner where the hook came from for the
	// --allow-project-hooks gate. "user" = ~/.config/...; "project"
	// = <repo>/.gofastr/...; "builtin" = ships with the binary.
	Source string
}

Hook is one configured hook entry.

type Result

type Result struct {
	Event    Event
	Command  string
	ExitCode int
	TimedOut bool
	Output   string // combined stdout+stderr, capped at 64KB
	Duration time.Duration
}

Result is the outcome of running one hook.

func (Result) Summary

func (r Result) Summary() string

Summary returns a short human-readable description for the cost meter / TUI.

func (Result) TrimOutput

func (r Result) TrimOutput(n int) string

TrimOutput returns the first n bytes of Output, ellipsizing if cut.

type Runner

type Runner struct {
	AllowProjectHooks bool
	// contains filtered or unexported fields
}

Runner runs hooks per event. Concurrency-safe.

func New

func New() *Runner

New returns an empty Runner.

func (*Runner) HooksFor

func (r *Runner) HooksFor(e Event) []Hook

HooksFor returns the registered hooks for an event in registration order.

func (*Runner) Register

func (r *Runner) Register(h Hook) error

Register adds a hook. Returns an error if the hook's Source is "project" and AllowProjectHooks is false, the hook is silently skipped (no error returned to keep callers simple).

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, e Event, env []string) []Result

Run executes every hook for the given event sequentially. Returns a slice of results. The caller is expected to publish HookTimeout / HookError events to the bus based on each Result.

Jump to

Keyboard shortcuts

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