Documentation
¶
Overview ¶
Package selfcheck implements the verification that the per-agent tool-allowlist enforcement contract still refuses tools NOT in permissions.allow when claude is spawned headlessly on the stream-json surface with a per-spawn deny-default settings file.
The full deny-default contract has three coupled halves; all three must hold for the per-agent security boundary to enforce:
argv half — `--permission-mode dontAsk` (production fix #538) is passed on every spawn. Per the CLI reference, an argv permission mode overrides any settings-file defaultMode. Without this, a malformed or absent defaultMode in the settings file silently downgrades to the default "ask" mode. See: https://code.claude.com/docs/en/cli-reference
settings-file half — `permissions.defaultMode: "dontAsk"` is written by internal/agentrun/settings/settings.go on every spawn. This is the belt-and-suspenders pair with the argv half: argv string + JSON field, both deterministic, both saying the same thing.
allow-list half — `permissions.allow: ["Read"]` (canonicalAllow) is the deny-default whitelist this selfcheck verifies against. The probe-tool name (canonicalProbeTool, currently "Write") MUST NOT appear in canonicalAllow; the invariant is pinned by TestProbeToolIsNotInAllowList. A future widening of canonicalAllow to include the probe tool would make PASS structurally unreachable without any compile-time signal.
The read-only-Bash carveout. Per the permission-modes reference, `--permission-mode dontAsk` "auto-denies all tool calls except those matching allow rules and read-only Bash commands". The carveout is scoped to read-only Bash specifically — not "any tool whose effect is read-only". The probe-tool therefore MUST sit off the Bash carveout; "Write" is a distinct tool with no analogous exemption. A prior version of this selfcheck used a Bash echo exhibit and rode the carveout, so PASS/FAIL did not track the deny-default boundary 1:1. Ticket #539 moved the probe to "Write" to close that gap. See: https://code.claude.com/docs/en/permission-modes
What this selfcheck verifies. SelfCheckDenyDefault composes three collaborators — trust.MarkWorkdirTrusted, settings.WriteSettings, and streamrunner.Run — exposed as package-level function variables so tests can mock the entire spawn surface in-process. The CLI wrapper at cmd/pyry/agent_run_selfcheck.go renders the returned Result as PASS / FAIL / inconclusive for operator consumption.
The argv half is verified transitively, and that is the whole design: the selfcheck builds its argv with streamrunner.BuildClaudeArgs, the same function `pyry agent-run` spawns with, so whatever the dispatcher currently passes is what gets checked.
This coupling is the lesson of the package's own history rather than a nicety. The check has followed the runner twice. It began on the stream-json shape verified by the Phase A spike (#329), moved to the terminal runner when the dispatcher cut over (#470/#473), and came back here in #1348 when the terminal runner was deleted. In between it spent weeks attached to a runner nothing spawned, where it would have passed while production ran unrestricted — which is exactly what #1387 later found had happened. A permission check that assembles its own spawn verifies a spawn nobody performs.
Runtime-layer, NOT LLM-layer. This self-check verifies claude's RUNTIME-layer enforcement — the probe sentinel file does NOT appear on disk — NOT the model's LLM-layer output, which may still emit a tool_use block regardless of whether the tool actually executes. Per the permissions reference, "Permission rules are enforced by Claude Code, not by the model": the runtime intercepts between a tool_use block's emission and the tool's execution, converting the would-be permission prompt into a hard deny under "dontAsk". The deny-default boundary therefore lives between the tool_use emission and its tool_result; the detector watches files (the post-enforcement side-effect), not events (the pre-enforcement model output). See: https://code.claude.com/docs/en/permissions
SECURITY: this package MUST NOT log Event.Raw bytes or claude stdout/stderr at any layer. The Result.SentinelPath field is the explicit exception: it is the load-bearing security evidence on FAIL, and MUST remain a path this package constructed — never file contents or captured claude output. The wrapper-error namespaces ("mark workdir trusted", "write settings") MUST NOT substitute workdir realpath or settings tempfile path into their messages — the underlying error already names the failing operation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrSentinelWritten = errors.New("agentrun: self-check: probe sentinel written despite deny-default settings")
ErrSentinelWritten is returned (wrapped) by SelfCheckDenyDefault when the probe sentinel file was on disk after the run. The boundary failed; claude's runtime executed the probe tool despite the deny-default settings.
var ErrTimeout = errors.New("agentrun: self-check: overall timeout")
ErrTimeout is returned when the overall timeout fires before an end-of-turn signal was observed and the sentinel did not appear. Inconclusive — the caller should retry or treat as infrastructure failure. Absence of evidence is not evidence of failure: the deny- default boundary may well have held, but the self-check could not confirm.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
ClaudeBin string // required; claude executable path
WorkDir string // required; existing directory used as the child's cwd
Logger *slog.Logger // optional; defaults to slog.Default()
// OverallTimeout caps the whole self-check, including spawn + watch.
// Zero defaults to defaultSelfCheckTimeout. On timeout, Result reflects
// whatever the watcher observed up to that point; the function returns
// ErrTimeout.
OverallTimeout time.Duration
// Env is appended to os.Environ() in the spawned child via
// streamrunner.Config.Env. Tests use this to thread fake-claude wiring
// through to the test binary; production leaves it nil.
Env []string
}
Config parameterises SelfCheckDenyDefault.
type Result ¶
type Result struct {
// SentinelWritten is true iff the probe sentinel file was on disk
// after the run completed — i.e. claude's runtime executed the probe
// tool despite the deny-default settings.
SentinelWritten bool
// SentinelPath is the sentinel path that appeared on disk. Set only on
// FAIL ("" otherwise). Always a path this package constructed — never
// file contents or captured claude output.
SentinelPath string
// EndOfTurnObserved is true after an end-of-turn assistant event or a
// successful end_turn stream result following an observed assistant.
EndOfTurnObserved bool
// AssistantCount counts assistant Events observed (informational).
AssistantCount int
}
Result captures what the self-check observed. Stable across PASS / FAIL / inconclusive (timeout) outcomes — callers branch on the returned error.
func SelfCheckDenyDefault ¶
SelfCheckDenyDefault composes trust.MarkWorkdirTrusted + settings.WriteSettings + streamrunner.BuildClaudeArgs + streamrunner.Run to drive the exhibit prompt against a headless stream-json claude bound to a per-spawn deny-default settings file (allow ["Read"]), then verifies the probe sentinel did NOT appear on disk — claude's runtime refused to execute the probe tool (canonicalProbeTool).
Returns (Result, nil) on PASS: the sentinel was absent and an completed assistant turn was observed. Returns (Result, ErrSentinelWritten- wrapped) on FAIL: the sentinel file was on disk after the run. Returns (Result, ErrTimeout) on inconclusive. Returns (Result, other) on infrastructure failure (trust, settings, sessionID, spawn, I/O, stat, etc.).