hooks

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package hooks provides shared utilities for the Claude Code hook commands implemented in cmd/mememory: stdin payload parsing, lock-file management for the forced-recall mechanism, and the ~/.claude/settings.json patcher that the `mememory install-hooks` command uses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanStaleLocks

func CleanStaleLocks(maxAge time.Duration) (int, error)

CleanStaleLocks removes lock files older than maxAge from os.TempDir(). Used at SessionStart to garbage-collect locks left by crashed Claude Code sessions. Returns the number of files removed.

func CreateLock

func CreateLock(sessionID string) error

CreateLock writes an empty file at LockPath(sessionID). Existing files are truncated — repeat calls within the same session are safe. Returns nil for empty session ids (silent no-op, matches LockPath's behaviour).

func LockExists

func LockExists(sessionID string) bool

LockExists reports whether the lock file for a session id is currently on disk. False for empty session ids.

func LockPath

func LockPath(sessionID string) string

LockPath returns the full path to the lock file for a given session id. Sessions without an id (empty string) get an empty path — callers should treat that as "no lock to manage".

func PatchClaudeSettings

func PatchClaudeSettings(path string, install bool) error

PatchClaudeSettings ensures (install=true) or removes (install=false) the four mememory hooks in a Claude Code settings.json file. Other settings — language, theme, custom hooks the user added — are preserved untouched.

Idempotent: install leaves an existing mememory hook entry alone (so users who tweaked the command — e.g., added --url — keep their customisation). Uninstall removes ALL entries whose command starts with "mememory <word>" for our managed words, regardless of customisation.

A backup at <path>.mememory-backup-<timestamp> is written before the file is modified. Missing source file is not an error — install creates a new settings.json from scratch in that case.

func RemoveLock

func RemoveLock(sessionID string) error

RemoveLock deletes the lock file for a session id. Missing files are not errors — that's the normal case after the first recall has cleared the lock.

Types

type HookInput

type HookInput struct {
	SessionID     string `json:"session_id"`
	HookEventName string `json:"hook_event_name"`
	ToolName      string `json:"tool_name"`
	ToolUseID     string `json:"tool_use_id"`
	Cwd           string `json:"cwd"`
	Source        string `json:"source"`
}

HookInput is the subset of fields we read from a hook stdin JSON payload. Claude Code sends different schemas for SessionStart / UserPromptSubmit / PreToolUse / PostToolUse — but the fields we care about have stable names across events, so a single tolerant struct handles all of them.

Fields that don't apply to a given event remain zero-valued — that's the caller's signal that they weren't sent.

func ReadHookInput

func ReadHookInput(r io.Reader) (HookInput, error)

ReadHookInput parses a hook stdin payload from r. Empty input or unknown fields are not errors — we get zero-valued fields the caller can branch on.

func ReadHookInputFromStdin

func ReadHookInputFromStdin() (HookInput, error)

ReadHookInputFromStdin is a convenience wrapper that reads from os.Stdin only when stdin is piped. When the binary is run from a TTY (manual invocation), it returns an empty HookInput without blocking on Read.

Jump to

Keyboard shortcuts

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