{"modulePath":"github.com/memcode-ai/memcode","version":"v0.56.16","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package hooks\n\nPackage hooks runs user-defined shell commands at agent lifecycle points — the extensibility seam for policy and automation the prompt can't provide (deterministic guards, notifications, context injection). Configuration is plain JSON the user owns: ~/.memcode/hooks.json (user-wide) merged with \\\u003croot\u003e/.memcode/hooks.json (project; runs after user hooks).\n\nEvents and semantics (deliberately Claude-Code-compatible so existing hook scripts port over):\n\n\tsession_start   payload {event, session_id, root}; combined stdout is\n\t                injected into the system prompt as standing context.\n\tpre_tool_use    payload {event, tool, input, session_id, root}. Exit 2\n\t                BLOCKS the tool call and feeds stderr to the model as the\n\t                reason. Any other non-zero exit is a non-blocking warning.\n\tpost_tool_use   payload adds {result, is_error}; exit codes are advisory.\n\tsession_end     payload {event, session_id, root}; fire-and-forget.\n\nThe hook command runs through the platform shell with the payload as JSON on stdin, cwd = project root, and MEMCODE\\_HOOK\\_EVENT / MEMCODE\\_TOOL\\_NAME / MEMCODE\\_SESSION\\_ID / MEMCODE\\_PROJECT\\_DIR in the environment. Default timeout 60s per hook (config \"timeout\" in seconds overrides).\n\n## Constants\n\n```go\nconst (\n\tSessionStart = \"session_start\"\n\tPreToolUse   = \"pre_tool_use\"\n\tPostToolUse  = \"post_tool_use\"\n\tSessionEnd   = \"session_end\"\n)\n```\nEvents.\n\n## Types\n\n```go\ntype Hook struct {\n\t// Matcher is a regexp matched against the TOOL NAME for pre/post_tool_use\n\t// (full match; empty = every tool). Ignored for session events.\n\tMatcher string `json:\"matcher,omitempty\"`\n\tCommand string `json:\"command\"`\n\tTimeout int    `json:\"timeout,omitempty\"` // seconds; 0 = default\n\t// contains filtered or unexported fields\n}\n```\nHook is one configured command.\n\n```go\ntype Result struct {\n\tBlock   bool   // pre_tool_use exit 2\n\tMessage string // block reason (stderr) or warning text\n\tStdout  string // captured stdout (used by session_start context injection)\n}\n```\nResult is one hook execution's outcome.\n\n```go\ntype Set struct {\n\t// contains filtered or unexported fields\n}\n```\nSet is the merged, compiled hook configuration for one session.\n\n```go\nfunc Load(root string) *Set\n```\nLoad reads and merges the user-wide then project hooks files. Missing files are fine (empty set); malformed files or matchers become Warnings, never errors — a broken hooks.json must not take the agent down.\n\n```go\nfunc (s *Set) Empty() bool\n```\nEmpty reports whether no hooks are configured (the fast path).\n\n```go\nfunc (s *Set) Run(ctx context.Context, event, toolName string, payload any) []Result\n```\nRun executes every hook for event whose matcher accepts toolName, in config order, and returns their results. payload is marshalled once onto stdin.\n\n```go\nfunc (s *Set) SetSessionID(id string)\n```\nSetSessionID stamps the CURRENT session id onto the set so hook commands see it as MEMCODE\\_SESSION\\_ID. Callers re-stamp on use (the id changes across /resume and /fork while the loaded set is cached for the Session's lifetime).\n\n```go\nfunc (s *Set) Warnings() []string\n```\nWarnings are non-fatal load problems (bad JSON, bad matchers) for surfacing once per session.\n\n","path":"github.com/memcode-ai/memcode/internal/hooks","name":"hooks","synopsis":"Package hooks runs user-defined shell commands at agent lifecycle points — the extensibility seam for policy and automation the prompt can't provide (deterministic guards, notifications, context injection).","isRedistributable":true}
