selfcheck

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package selfcheck implements the boot-time verification that the per-agent tool-allowlist enforcement contract still refuses tools NOT in permissions.allow when claude is spawned as an interactive-TUI process under a PTY 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:

  1. 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

  2. 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.

  3. 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 four collaborators — trust.MarkWorkdirTrusted, settings.WriteSettings, sessions.NewID, and ptyrunner.Run — exposed as package-level function variables so tests can mock the entire spawn surface in-process. The Phase A spike (#329) verified empirically the streamrunner shape; the post-#470 production cutover moved the dispatcher to ptyrunner, and the post-#473 rewrite moved the selfcheck along with it so it verifies the ACTUAL production path rather than the fallback. The post-#538 argv addition (`--permission-mode dontAsk` in ptyrunner.buildArgs) is verified transitively: the selfcheck spawns claude via the same ptyrunner.Run the dispatcher uses, so the argv half is whatever ptyrunner.buildArgs currently emits. The CLI wrapper at cmd/pyry/agent_run_selfcheck.go renders the returned Result as PASS / FAIL / inconclusive for operator + CI consumption.

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", "mint session id") MUST NOT substitute workdir realpath, settings tempfile path, or session id into their messages — the underlying error already names the failing operation.

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
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
	// ptyrunner.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 iff a deterministic end-of-turn assistant
	// event was observed (stop_reason "end_turn" with non-empty text).
	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

func SelfCheckDenyDefault(ctx context.Context, cfg Config) (Result, error)

SelfCheckDenyDefault composes trust.MarkWorkdirTrusted + settings.WriteSettings + sessions.NewID + ptyrunner.Run to drive the exhibit prompt against an interactive-TUI 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 end-of-turn assistant event fired. 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.).

Jump to

Keyboard shortcuts

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