Documentation
¶
Overview ¶
Package harness abstracts the coding-agent CLIs patchy drives. A harness builds the argv for one prompted run (PromptSpec), the runner package executes it, and the harness parses the CLI's stream-json stdout back into an AgentResult — harnesses never touch os/exec themselves, so every caller can be tested against captured output.
Two harnesses register: Claude (the claude CLI, patchy's real agent) and Fake (replays a fixture file through cat, for tests and the agent-runner's --fake mode).
Index ¶
- Constants
- func Available(h Harness) (path string, ok bool)
- type AgentResult
- type Claude
- func (b Claude) CLI() []string
- func (b Claude) EnvKeys() []string
- func (b Claude) ID() string
- func (b Claude) Name() string
- func (c *Claude) ParseResult(stdout []byte) (AgentResult, bool)
- func (c *Claude) PromptSpec(ws string, req PromptRequest) runner.CommandSpec
- func (c *Claude) RuntimeError(stdout []byte, exitCode int, timedOut bool) string
- func (c *Claude) ScanUsage(line []byte) (int, bool)
- type Fake
- func (b Fake) CLI() []string
- func (b Fake) EnvKeys() []string
- func (b Fake) ID() string
- func (b Fake) Name() string
- func (f *Fake) ParseResult(stdout []byte) (AgentResult, bool)
- func (f *Fake) PromptSpec(ws string, req PromptRequest) runner.CommandSpec
- func (f *Fake) RuntimeError(stdout []byte, exitCode int, timedOut bool) string
- func (f *Fake) ScanUsage(line []byte) (int, bool)
- type Harness
- type PromptRequest
- type Usage
- type UsageScanner
Constants ¶
const FakeFixtureEnv = "PATCHY_FAKE_FIXTURE"
FakeFixtureEnv names the environment variable pointing at the stream-json fixture file the Fake harness replays.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AgentResult ¶
type AgentResult struct {
FinalText string
SessionID string // the session id the CLI reports on its result event
NumTurns int
Usage *Usage // includes CostUSD from total_cost_usd; nil when unreported
IsError bool
Subtype string
Errors []string
}
AgentResult is the parsed terminal result of one agent run.
type Claude ¶
type Claude struct {
// contains filtered or unexported fields
}
Claude drives the `claude` CLI (Claude Code).
func (*Claude) ParseResult ¶
func (c *Claude) ParseResult(stdout []byte) (AgentResult, bool)
ParseResult reads the terminal result event from claude's stream-json output; see parseStreamResult.
func (*Claude) PromptSpec ¶
func (c *Claude) PromptSpec(ws string, req PromptRequest) runner.CommandSpec
PromptSpec builds the headless claude invocation for one prompted run. stream-json with --verbose emits one JSON event per line, which is what ParseResult and ScanUsage parse. Optional request fields append their flag only when set, in a stable order.
func (*Claude) RuntimeError ¶
RuntimeError classifies claude's output; see streamRuntimeError.
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake is a stand-in harness for tests and the agent-runner's --fake mode: it replays a captured stream-json fixture through cat instead of invoking an agent, and parses it with the same stream-json helpers as Claude, so everything downstream of PromptSpec is exercised for real. Unix-only (it shells out to /bin/cat's argv convention); the real deployments it fakes run in Linux pods anyway.
func (*Fake) ParseResult ¶
func (f *Fake) ParseResult(stdout []byte) (AgentResult, bool)
ParseResult parses the fixture exactly as Claude parses live output.
func (*Fake) PromptSpec ¶
func (f *Fake) PromptSpec(ws string, req PromptRequest) runner.CommandSpec
PromptSpec ignores the request's prompt and flags and replays the fixture named by FakeFixtureEnv; ws and Env still land on the spec so runner-side behavior (workspace, environment) stays faithful.
func (*Fake) RuntimeError ¶
RuntimeError classifies the fixture exactly as Claude classifies live output.
type Harness ¶
type Harness interface {
ID() string // registry key, e.g. "claude"
Name() string // human name, e.g. "Claude Code"
CLI() []string // runner binary candidates, in preference order
EnvKeys() []string // credential env vars, in preference order
// PromptSpec builds the headless command for one prompted run in workspace ws.
PromptSpec(ws string, req PromptRequest) runner.CommandSpec
// ParseResult extracts the terminal result from the CLI's full stdout.
// ok is false when the output carried no terminal result event (plain
// text, crash mid-stream); the AgentResult then carries the raw stdout as
// FinalText and nothing else.
ParseResult(stdout []byte) (res AgentResult, ok bool)
// RuntimeError returns a short reason when the agent run produced no
// usable output (auth blocked, crash, empty/error envelope), or "" when
// the output is usable. A benign non-zero exit (e.g. max-turns) that
// still produced a result returns "" — it is a partial answer, not an
// error.
RuntimeError(stdout []byte, exitCode int, timedOut bool) string
}
Harness is the required surface every agent CLI implements. A harness drives a model — it does not own one; the model a run targets is supplied as a harness-specific CLI id on the PromptRequest.
type PromptRequest ¶
type PromptRequest struct {
Prompt string
Model string // harness-specific model id
SystemPromptAppend string // appended to the CLI's system prompt when set
SessionID string // pre-assigned UUID → claude --session-id
MaxTurns int // agent-turn ceiling; 0 leaves the CLI default
AllowedTools []string // tool grammar entries, space-joined for claude
DisallowedTools []string
AddDirs []string // extra directories the agent may access
Env []string // extras appended to os.Environ() by the runner
}
PromptRequest describes one prompted agent run, harness-agnostically; the harness maps it onto its CLI's flags.
type Usage ¶
type Usage struct {
InputTokens *int // fresh (uncached) input only
CacheReadTokens *int
CacheCreationTokens *int
OutputTokens *int
CostUSD *float64
}
Usage is the harness-reported consumption of one agent session. Fields are nil where the CLI does not report them. InputTokens is the fresh (uncached) input only; cache reads and writes are reported separately so a multi-turn session's cheap cache traffic does not inflate the headline input figure.
type UsageScanner ¶
type UsageScanner interface {
// ScanUsage returns the output-token count reported by one stream line
// (claude: assistant events' message.usage.output_tokens), ok=false
// otherwise.
ScanUsage(line []byte) (outputTokens int, ok bool)
}
UsageScanner is the optional capability of reading token usage off the live output stream; it powers the caller's output-token budget kill switch.