hook

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextKey added in v0.1.4

func ContextKey(inp *protocol.HookInput) string

ContextKey returns the state-store key for one hook invocation.

It is deliberately NOT the session id. Claude Code reports a subagent's tool calls under the PARENT's session_id, but a subagent has its own context that is thrown away when it finishes — nothing it reads ever reaches the parent. Keying the file cache on session_id alone therefore lets a subagent's read deny the parent's, handing back "no re-read needed" for content the parent never saw. That is the one failure this cache must not have: a deny cannot be downgraded to a safe passthrough, because there is no content in hand.

Splitting the key keeps each context honest, and still lets a subagent dedup against its own earlier reads.

func Dispatch

func Dispatch(store hookcore.StateStore, r io.Reader, w io.Writer) error

Dispatch is the single PostToolUse entry point. It decodes the event once and routes by tool name: Read and Write keep their file-specific handlers; every other tool (Bash, Glob, Grep, mcp__*, anything new) flows through the generic pipeline so it gets dedup / delta / noise-strip / squeeze / structural summaries for free — no per-tool hardcoding. store is the pipeline's session and cache backend (the on-disk cache for the CLI, an in-memory store for a future daemon).

func DispatchBytes

func DispatchBytes(store hookcore.StateStore, req []byte, w io.Writer) error

DispatchBytes is Dispatch for callers that already hold the whole request in memory (the daemon, via a pooled read buffer): it decodes with a single json.Unmarshal instead of wrapping the bytes in a json.Decoder, avoiding that decoder's extra buffering/streaming layer on the hottest path. Pool safety is unchanged — json.Unmarshal, like the Decoder, copies every string out of req, so nothing routeInput retains aliases the pooled bytes.

func HandleBash

func HandleBash(r io.Reader, w io.Writer) error

HandleBash is retained for backward compatibility. PostToolUse routing now goes through Dispatch, which handles Bash (and every non-Read/Write tool) via the generic pipeline.

func HandleGlob

func HandleGlob(r io.Reader, w io.Writer) error

HandleGlob is retained for backward compatibility; Glob is handled by the generic pipeline via Dispatch (buildGlobTree is its tool-specific step).

func HandleGrep

func HandleGrep(r io.Reader, w io.Writer) error

HandleGrep is retained for backward compatibility; Grep is handled by the generic pipeline via Dispatch (buildGrepSummary is its tool-specific step).

func HandlePostCompact

func HandlePostCompact(r io.Reader, w io.Writer) error

HandlePostCompact runs after compaction completes. It injects a manifest of previously-read files into the fresh context, so Claude knows what's been read without re-reading.

func HandlePreCompact

func HandlePreCompact(r io.Reader, w io.Writer) error

HandlePreCompact runs before Claude Code performs context compaction. It records CompactedAt in the session state so subsequent reads know to serve full content (since compaction erased the context).

func HandlePreToolUse

func HandlePreToolUse(r io.Reader, w io.Writer) error

HandlePreToolUse intercepts Read tool calls before execution. If the file's mtime matches the cached mtime and the file was seen after the last compaction, it denies the read with an §unchanged§ marker — the file is not read at all, saving I/O and output tokens.

func HandleRead

func HandleRead(r io.Reader, w io.Writer) error

HandleRead processes a PostToolUse hook call for the Read tool. It reads the hook JSON from r, applies delta/unchanged logic, and writes the hook output JSON to w.

func HandleSessionStart

func HandleSessionStart(r io.Reader, w io.Writer) error

HandleSessionStart runs at session start (after compaction or fresh session). If there is an existing state file from a previous compacted session, it injects the file manifest. Otherwise it's a no-op.

func HandleWrite

func HandleWrite(r io.Reader, w io.Writer) error

HandleWrite compresses Write/Edit/MultiEdit tool responses. Claude just wrote this content — no need to see the full file (Edit's echo even embeds the entire pre-edit originalFile). Also caches the written file for delta tracking on the next Read.

Types

This section is empty.

Jump to

Keyboard shortcuts

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