Documentation
¶
Overview ¶
Package hooks installs and removes the caprock-hook shim in Claude Code's user-level settings.json, non-destructively. See .ai/03-contracts.md § Hook shim.
Index ¶
- Constants
- Variables
- func AcceptBypass(settingsPath string) (backup string, err error)
- func BypassAccepted(settingsPath string) (bool, error)
- func DefaultSettingsPath() (string, error)
- func EnsureShim(dataDir string) error
- func Install(settingsPath, shimPath string) (backup string, err error)
- func InstallStatusline(settingsPath, cmdPath string) (backup string, err error)
- func KeepTranscripts(settingsPath string, days int) (changed bool, err error)
- func ListBackups(settingsPath string) []string
- func MarshalIndent(v any) ([]byte, error)
- func ParseOrdered(data []byte) (any, error)
- func RepairStatusline(settingsPath, cmdPath string) (repaired bool, err error)
- func Restore(settingsPath, backupPath string) error
- func SetStatuslineOn(settingsPath, cmdPath string, on bool) (changed bool, err error)
- func ShellCommand(p string) string
- func ShimCommand(dataDir string) string
- func StatuslineInstalled(settingsPath, cmdPath string) (ours bool, present bool, err error)
- func Uninstall(settingsPath, shimPath string) (removed bool, err error)
- func UninstallStatusline(settingsPath, cmdPath string) (removed bool, err error)
- type Object
- type Repaired
- type Retention
- type Status
Constants ¶
const BypassKey = "skipDangerousModePermissionPrompt"
BypassKey is the user setting Claude Code writes when someone answers "Yes, I accept" to its one-time bypass warning ("WARNING: Claude Code running in Bypass Permissions mode"). Until it is true, the first session started with --dangerously-skip-permissions opens on that warning, with "No, exit" selected — so Enter ends the session (ADR-042).
const DefaultRetentionDays = 30
DefaultRetentionDays is Claude Code's period when the key is unset.
const KeepRetentionDays = 36500
KeepRetentionDays is what "Keep them from now on" writes: a hundred years, which is Claude Code never deleting a transcript by age.
const RetentionKey = "cleanupPeriodDays"
RetentionKey is the user setting that says how many days Claude Code keeps a conversation's transcript under ~/.claude/projects. At startup it deletes every transcript older than that; unset, the period is DefaultRetentionDays. Caprock can only import what is still on disk, so a new user's history starts about that many days back.
const ShimTimeoutSeconds = 5
ShimTimeoutSeconds is the per-hook timeout written into settings.json. The shim itself exits within ~1s; 5s is headroom for a slow disk, and it bounds the Phase 2 Stop request-response (5s) too.
Variables ¶
var Events = []string{"SessionStart", "UserPromptSubmit", "PreToolUse", "PostToolUse", "Stop", "SubagentStop", "SessionEnd", "PreCompact", "StopFailure", "PermissionRequest"}
Events the shim registers for (Phase 0). Order is the order written.
Functions ¶
func AcceptBypass ¶ added in v0.79.0
AcceptBypass records the user's acceptance the way Claude Code's own dialog does — the same key, true, in user settings — after Caprock showed the same warning and the user accepted it there. The file is backed up once first, and every other key is kept in place and order.
func BypassAccepted ¶ added in v0.79.0
BypassAccepted reports whether the user has accepted Claude Code's bypass warning in their user settings. A missing file is "no", not an error.
func DefaultSettingsPath ¶
DefaultSettingsPath is ~/.claude/settings.json.
func EnsureShim ¶ added in v0.70.1
EnsureShim brings `<data dir>/caprock-hook` up to date with this binary, the path every settings.json Caprock wrote runs on each hook event.
- A caprock-hook beside this executable (Homebrew, the release archives, `make build`) is copied in whenever the bytes differ.
- With none beside it (the desktop app ships caprock alone), an existing data-dir shim is replaced by this binary itself, which runs as the shim when started under that name (cmd/caprock). Without this, a shim a Homebrew install put there months earlier kept feeding hooks to every later daemon, because nothing ever replaced it (a user's 0.31.2 shim under 0.147.0). With no data-dir shim either, nothing is written and `<self> hook` is the registered command (ShimCommand).
Every write goes to a temporary file in the data dir and is renamed over the shim, so Claude Code, which may run it at any moment, sees the old file or the new one and never a missing or half-written one (rule 3).
func Install ¶
Install merges our entries into settings.json (creating the file if absent), backing the original up once before the first write. Returns the backup path ("" when nothing was written or no original existed).
func InstallStatusline ¶ added in v0.5.0
InstallStatusline sets settings.json's statusLine to our command, backing the file up once. It refuses to clobber a statusLine the user set to something else — callers should check StatuslineInstalled and only offer to install when nothing (or our own) is present. Returns the backup path (empty if no change).
func KeepTranscripts ¶ added in v0.157.0
KeepTranscripts writes cleanupPeriodDays = days into user settings, the one key changed and every other byte of the file left as it was: the value is replaced where it stands, or the key is added first in the object, indented like the key after it. The user asked for it on the Lifetime screen, after it said what the setting does.
The file is backed up once first, as for the hooks entry, and written atomically. An unparsable file is refused, never overwritten: Claude Code would refuse it too, and the user's own edits are in it. Asking again for the period already set changes nothing and writes nothing.
func ListBackups ¶ added in v0.18.0
ListBackups returns the Caprock backups of settings.json, oldest first.
func MarshalIndent ¶
MarshalIndent renders an ordered value with 2-space indentation, matching how Claude Code writes settings.json.
func ParseOrdered ¶
ParseOrdered decodes JSON preserving object key order.
func RepairStatusline ¶ added in v0.53.2
RepairStatusline rewrites our own statusLine entry when the stored command would not actually run, and reports whether it changed anything.
The one form this exists for is the quotes-around-everything command an older version wrote (`"…/caprock statusline"`), which the shell resolves as a single filename that does not exist — so the status line printed nothing, silently, for as long as the entry survived. Recognising it is not enough: install treats anything of ours as an idempotent no-op, so without this the entry would be identified and then left exactly as broken as it was found.
It is deliberately narrow. A statusLine that is not ours is never touched, and neither is one of ours that already runs — this repairs a known-broken spelling, it does not normalise everyone's command to today's preferred one.
func Restore ¶ added in v0.18.0
Restore copies a backup back over settings.json. It takes the backup path explicitly rather than picking one, because choosing which snapshot a user wants is a decision only they can make. Before overwriting it snapshots the current file, so a restore is itself undoable.
func SetStatuslineOn ¶ added in v0.112.2
SetStatuslineOn applies Settings → Status line to one settings.json: on (Full or Short) registers our command when no statusLine is set, off (Off) removes ours. Both go through the same ownership checks as install and uninstall — a statusLine the user set to something else is never replaced and never removed — and both are no-ops when there is nothing to do. Reports whether the file changed.
func ShellCommand ¶ added in v0.27.4
func ShimCommand ¶ added in v0.70.1
ShimCommand returns the command to register in settings.json: the shim binary in the data dir when it exists, else `<self> hook`.
func StatuslineInstalled ¶ added in v0.5.0
StatuslineInstalled reports whether settings.json already has a statusLine command that points at our binary (`… statusline`). A different statusLine command (the user's own) is left alone and reported as not-ours.
func Uninstall ¶
Uninstall removes only entries whose command points at our shim, and drops empty containers it leaves behind. Other hooks are untouched. Returns whether anything was removed.
func UninstallStatusline ¶ added in v0.5.0
UninstallStatusline removes the statusLine only if it is ours, leaving a user-set statusLine untouched. Returns whether anything was removed.
Types ¶
type Object ¶
type Object struct {
Keys []string
Values map[string]any // values are: *Object, []any, string, float64, bool, nil
}
Object is a JSON object with stable key order.
func (*Object) MarshalJSON ¶
MarshalJSON writes keys in insertion order.
type Repaired ¶ added in v0.149.0
type Repaired struct {
// Hooks counts our hook commands pointed at the current shim (or
// removed, where the event already had the current one).
Hooks int
// Statusline is whether our statusLine was pointed at this binary.
Statusline bool
}
Repaired says what RepairPaths rewrote in one settings.json.
func RepairPaths ¶ added in v0.149.0
RepairPaths points Caprock's own entries in settings.json at the binaries this install runs, and reports what it changed. shimCmd is what an install registers (ShimCommand); statuslineCmd is `<ShellCommand(self)> statusline`, or "" to leave the statusLine alone.
An entry is rewritten only when it is ours (the same recognition install and uninstall use) and the program it runs is missing or is a different file from the one this install would register: a Homebrew caprock that was uninstalled, or one months older than the daemon reading its hooks. A command of anyone else's is never touched, and neither is ours when it already runs this install's binary, whatever its spelling or flags. A statusLine's flags (`--width 100`) are kept.
The file is backed up once and written atomically, like every other write here. It is the daemon's self-repair at start: nobody can be expected to notice that a status line or a hook has been running someone else's binary.
type Retention ¶ added in v0.157.0
type Retention struct {
// Days is the period Claude Code applies: the setting, or
// DefaultRetentionDays when it is unset.
Days int `json:"days"`
// Set reports whether the settings name a period at all.
Set bool `json:"set"`
}
Retention is what the user settings say about it.
func ReadRetention ¶ added in v0.157.0
ReadRetention reads cleanupPeriodDays from user settings. A missing file is the default period, not an error; an unparsable one is an error, the same as for every other read of this file. A value that is not a number is taken as unset, which is how Claude Code's settings check treats it.
type Status ¶
type Status struct {
SettingsPath string `json:"settings_path"`
ShimPath string `json:"shim_path"`
Installed []string `json:"installed"` // events with our entry present
Missing []string `json:"missing"` // events from Events without our entry
ShimExists bool `json:"shim_exists"`
}
Status describes what is registered.
func InstallFor ¶ added in v0.70.1
InstallFor is `caprock hooks install` as one call, for the CLI and the dashboard's Install button alike: put the shim in the data dir, merge our entries into the settings file at settingsPath (backed up first), and report what is registered now. The two used to be separate code in cmd/caprock; one path means the button cannot install something the command would not.
func StatusFor ¶ added in v0.70.1
StatusFor is what is registered, checked against the command an install would write — the one question /v1/status, `caprock status` and the Install button must all answer the same way.
The daemon used to check against the data-dir shim path alone. When no caprock-hook sat beside the executable, install registered the fallback `"<exe>" hook`, which the check recognised only if the executable was named caprock; under any other name (a renamed or preview build) the button said "installed" and the next status said all nine events were missing.