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 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 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 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 copies caprock-hook from beside this executable into the data dir (if present and different). When no sibling shim exists, `caprock hook` is used as the fallback command (ShimCommand).
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 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 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.