Documentation
¶
Overview ¶
Package agentrun provides helpers used by the `pyry agent-run` verb (wired in #338B) and the JSONL watcher (#333) to interoperate with claude's on-disk state.
MUST NOT log file contents at any layer. ~/.claude.json may contain tokens or claude-internal state pyry does not own; the helper takes a pass-through view (preserve fields verbatim) and emits a key+verdict on success, not the underlying bytes.
Index ¶
- Constants
- func Drive(ctx context.Context, cfg DriveConfig) error
- func EncodeProjectDir(workdir string) (string, error)
- func MarkWorkdirTrusted(homeDir, workdir string) error
- func ResolveWorkdir(workdir string) (string, error)
- func WriteSettings(workdir string, allowed []string) (string, error)
- type DriveConfig
Constants ¶
const SettingsFilename = ".pyry-agent-run-settings.json"
SettingsFilename is the fixed basename of the per-spawn settings file emitted by WriteSettings inside the caller's workdir. The dispatcher gitignores this filename in its own repo; pyry does not modify any .gitignore here.
Variables ¶
This section is empty.
Functions ¶
func Drive ¶
func Drive(ctx context.Context, cfg DriveConfig) error
Drive spawns claude with the agent-run argv, drives a single user-turn via the PTY, background-drains the PTY output (so claude does not block once its output buffer fills), and waits for the child to exit (typically via ctx cancel → SIGTERM → SIGKILL grace).
Returns nil on a clean exit (or ctx-cancel-driven teardown — operator SIGTERM is success at this verb). *exec.ExitError on non-zero child exit that was NOT triggered by ctx cancellation.
func EncodeProjectDir ¶
EncodeProjectDir returns the dashed directory-name segment claude uses under ~/.claude/projects/ for the given workdir. Chains ResolveWorkdir then maps '/' and '.' to '-' in the resolved absolute path. The result does NOT include the ~/.claude/projects/ prefix or any .jsonl suffix.
func MarkWorkdirTrusted ¶
MarkWorkdirTrusted sets projects[<ResolveWorkdir(workdir)>]. hasTrustDialogAccepted = true in <homeDir>/.claude.json, under a file lock spanning the entire read-modify-write window. Idempotent. Atomic on-disk.
All existing fields in ~/.claude.json are preserved verbatim. Numeric values are preserved using json.Number to avoid float64 precision loss.
func ResolveWorkdir ¶
ResolveWorkdir returns the resolved absolute path of workdir, mirroring how claude resolves a workdir before reading ~/.claude.json's projects map. Resolves symlinks (macOS /var → /private/var). Wraps fs.ErrNotExist when the path does not exist.
func WriteSettings ¶
WriteSettings emits the per-spawn claude settings JSON inside workdir and returns the resolved path.
The shape is exactly {"permissions": {"allow": <allowed>, "defaultMode": "deny"}}. allowed is round-tripped verbatim — caller is responsible for non-emptiness (the pyry agent-run flag parser enforces this at the CLI boundary); a nil or empty slice is accepted and produces "allow": [] cleanly (NOT "allow": null).
The file lives at <workdir>/.pyry-agent-run-settings.json. The dispatcher gitignores this filename in its repo; pyry does not modify any .gitignore.
Written atomically via the project's standard recipe (os.CreateTemp in the same directory → encode → Sync → Close → Rename), at mode 0o600. Overwrite of a prior file is safe; the rename is the commit point.
Stdout-marker contract (sibling #332): runAgentRun prints the returned path behind the literal prefix `settings-file: ` on a line of its own. The dispatcher scrapes that line with `^settings-file: (.+)$`.
Types ¶
type DriveConfig ¶
type DriveConfig struct {
ClaudeBin string // required; claude executable path
WorkDir string // required; child cwd
Args []string // full claude argv (without argv[0])
Logger *slog.Logger // optional; defaults to slog.Default()
// Env is appended to os.Environ() in the spawned child. Tests use this
// to thread TestHelperProcess wiring; production leaves it nil.
Env []string
// Timings exposed for tunability and unit-test ergonomics. Zero values
// fall back to the spike-validated defaults.
TrustDialogDelay time.Duration
PromptDelay time.Duration
// PromptBytes is the user-turn text typed into the PTY after PromptDelay.
// Drive appends a single "\r" after these bytes; callers MUST NOT
// include a trailing CR. Operator-controlled; not sanitised — control
// sequences here flow into claude's TUI parser. Trust boundary: pyry
// trusts its operator-supplied prompt file the same way it trusts
// --workdir.
PromptBytes []byte
}
DriveConfig parameterises Drive. Callers build the full claude argv at the call site (see cmd/pyry/agent_run.go's buildClaudeArgs); Drive owns nothing about flag shape.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package budget enforces the pyry-side --max-turns cap for agent-run by counting assistant JSONL entries and signalling the claude child when the budget is reached.
|
Package budget enforces the pyry-side --max-turns cap for agent-run by counting assistant JSONL entries and signalling the claude child when the budget is reached. |
|
Package jsonl parses claude session JSONL output into structured events, surfacing every line (assistant, user, tool_use, tool_result, system, attachment, or unrecognised) with its verbatim bytes, alongside a deterministic end-of-turn signal on assistant entries.
|
Package jsonl parses claude session JSONL output into structured events, surfacing every line (assistant, user, tool_use, tool_result, system, attachment, or unrecognised) with its verbatim bytes, alongside a deterministic end-of-turn signal on assistant entries. |
|
tail
Package tail watches ~/.claude/projects/<encoded-cwd>/<sid>.jsonl for claude's per-session JSONL output, opens the file as it appears, and feeds appended bytes into a jsonl.Reader until the deterministic end-of-turn signal fires or the caller cancels.
|
Package tail watches ~/.claude/projects/<encoded-cwd>/<sid>.jsonl for claude's per-session JSONL output, opens the file as it appears, and feeds appended bytes into a jsonl.Reader until the deterministic end-of-turn signal fires or the caller cancels. |
|
Package selfcheck implements the boot-time verification that permissions.defaultMode "deny" in the per-spawn settings file still enforces the whitelist when claude is spawned in interactive mode.
|
Package selfcheck implements the boot-time verification that permissions.defaultMode "deny" in the per-spawn settings file still enforces the whitelist when claude is spawned in interactive mode. |
|
Package streamjson re-emits the parsed claude session-JSONL Event stream onto a writer (typically pyry's stdout) and composes a single `type:"result"` trailer line when the run terminates.
|
Package streamjson re-emits the parsed claude session-JSONL Event stream onto a writer (typically pyry's stdout) and composes a single `type:"result"` trailer line when the run terminates. |