hooks

package
v0.8.0 Latest Latest
Warning

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

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

Documentation

Overview

Package hooks manages Claude Code settings.json files and the PromptVM hook tracker sidecar (.promptvm-hooks.json). It provides read/write helpers used by the hooks CLI commands.

The core types and functions are split across:

  • settings.go: Settings, Scope, ReadSettings, MergeHook, RemoveHook, Write, Checksum
  • tracker.go: Tracker, TrackedHook, LoadTracker, TrackerFilePath

Index

Constants

View Source
const CaptureHookCommand = "promptvm sync run"

CaptureHookCommand is the command every capture hook event invokes. The command is identical across events and platforms; `sync run` reads the event name from stdin and self-detaches, so non-blocking behavior does not depend on Claude Code's `async` field or a `setsid` binary (HOOK-3 / DX-6).

View Source
const CaptureHookSlug = "promptvm-context-sync"

CaptureHookSlug is the tracker/_slug identifier for the context-sync capture hook. It scopes MergeHook/RemoveHook so re-running `sync init` is idempotent and never collides with marketplace-installed hooks.

Variables

This section is empty.

Functions

func BuildCaptureFragment added in v0.6.0

func BuildCaptureFragment(events []string) map[string]interface{}

BuildCaptureFragment builds a Claude Code settings.json "hooks" fragment for the given events. Each event gets a single command-hook matcher group tagged with CaptureHookSlug so it is trackable and removable. The fragment is shaped for the settings "hooks" key (HOOK-7), not PromptVM's internal "events" key.

SessionStart is reconcile-only and stdout-silent (HOOK-4); it carries no matcher so it fires on all sources (startup|resume|clear|compact).

func Checksum

func Checksum(events map[string]interface{}) string

Checksum computes sha256 of the JSON-encoded event entries for a hook. Keys are sorted for deterministic output.

func SettingsFilePath

func SettingsFilePath(scope Scope) (string, error)

SettingsFilePath returns the path to the Claude Code settings.json for the given scope, anchoring project/local scopes at the current working directory.

func SettingsFilePathAt added in v0.7.0

func SettingsFilePathAt(scope Scope, root string) (string, error)

SettingsFilePathAt returns the settings.json path for the given scope, anchoring project/local scopes at root. When root is "" it falls back to the current working directory. Callers that know the git repo root (e.g. `sync init`) MUST pass it so the settings file lands next to the manifest at the repo root rather than wherever the command happened to be invoked from.

func TrackerFilePath

func TrackerFilePath(scope Scope) (string, error)

TrackerFilePath returns path to the sidecar file for the given scope.

Types

type Scope

type Scope string

Scope determines which settings file to target.

const (
	ScopeProject Scope = "project"
	ScopeUser    Scope = "user"
	// ScopeLocal targets .claude/settings.local.json — the per-machine,
	// gitignored project settings tier Claude Code reads with highest
	// precedence. Added for context-sync (DX-2).
	ScopeLocal Scope = "local"
)

type Settings

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

Settings represents a Claude Code settings.json file. We use map[string]interface{} to preserve unknown keys.

func ReadSettings

func ReadSettings(path string) (*Settings, error)

ReadSettings reads and parses the settings file. Returns empty settings if file doesn't exist.

func (*Settings) CaptureEventsInstalled added in v0.6.0

func (s *Settings) CaptureEventsInstalled() []string

CaptureEventsInstalled returns the capture-hook event names currently present in the settings, identified by the CaptureHookSlug tag.

func (*Settings) Hooks

func (s *Settings) Hooks() map[string]interface{}

Hooks returns the hooks map from settings, or empty map if none.

func (*Settings) MergeHook

func (s *Settings) MergeHook(fragment map[string]interface{}, slug string, force bool)

MergeHook adds a hook's event entries to the settings. For each event:

  • If event doesn't exist, create it with the matchers.
  • If event exists, append matchers (dedup by checksum).
  • If force, replace any existing entries from the same slug first.

The fragment is a map of event names to arrays of matcher objects. Each matcher object may contain a "_slug" field used for identification.

func (*Settings) RemoveHook

func (s *Settings) RemoveHook(tracker *Tracker, slug string) bool

RemoveHook removes all entries associated with a slug (using the tracker to identify them). It removes matcher entries whose "_slug" field matches the given slug and cleans up empty arrays. Returns true if any entries were removed.

func (*Settings) Write

func (s *Settings) Write(path string) error

Write atomically writes the settings file (tmp + rename pattern).

type TrackedHook

type TrackedHook struct {
	Slug        string   `json:"slug"`
	Version     int      `json:"version"`
	SourceURL   string   `json:"source_url,omitempty"`
	InstalledAt string   `json:"installed_at"`
	Events      []string `json:"events"`
	Checksum    string   `json:"checksum"`
}

TrackedHook represents a managed hook entry.

type Tracker

type Tracker struct {
	Hooks []TrackedHook `json:"hooks"`
	// contains filtered or unexported fields
}

Tracker manages the sidecar file.

func LoadTracker

func LoadTracker(scope Scope) (*Tracker, error)

LoadTracker reads the sidecar file. Returns empty tracker if not found.

func LoadTrackerFromPath

func LoadTrackerFromPath(path string) (*Tracker, error)

LoadTrackerFromPath reads a tracker from a specific file path.

func (*Tracker) Add

func (t *Tracker) Add(hook TrackedHook)

Add adds or updates a tracked hook entry. If a hook with the same slug already exists, it is replaced.

func (*Tracker) EventsForSlug

func (t *Tracker) EventsForSlug(slug string) []string

EventsForSlug returns the event keys associated with a slug.

func (*Tracker) Get

func (t *Tracker) Get(slug string) *TrackedHook

Get returns a tracked hook by slug, or nil.

func (*Tracker) Remove

func (t *Tracker) Remove(slug string) bool

Remove removes a tracked hook by slug. Returns true if found.

func (*Tracker) Save

func (t *Tracker) Save() error

Save atomically writes the tracker file.

Jump to

Keyboard shortcuts

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