harness

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 7 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 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

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

func Available

func Available(h Harness) (path string, ok bool)

Available finds the first of the harness's runner binaries on PATH.

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 NewClaude

func NewClaude() *Claude

NewClaude returns the builtin Claude Code harness.

func (Claude) CLI

func (b Claude) CLI() []string

func (Claude) EnvKeys

func (b Claude) EnvKeys() []string

func (Claude) ID

func (b Claude) ID() string

func (Claude) Name

func (b Claude) Name() string

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

func (c *Claude) RuntimeError(stdout []byte, exitCode int, timedOut bool) string

RuntimeError classifies claude's output; see streamRuntimeError.

func (*Claude) ScanUsage

func (c *Claude) ScanUsage(line []byte) (int, bool)

ScanUsage reads the output-token count off one live stream line; see scanStreamUsage.

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 NewFake

func NewFake() *Fake

NewFake returns the builtin fake harness.

func (Fake) CLI

func (b Fake) CLI() []string

func (Fake) EnvKeys

func (b Fake) EnvKeys() []string

func (Fake) ID

func (b Fake) ID() string

func (Fake) Name

func (b Fake) Name() string

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

func (f *Fake) RuntimeError(stdout []byte, exitCode int, timedOut bool) string

RuntimeError classifies the fixture exactly as Claude classifies live output.

func (*Fake) ScanUsage

func (f *Fake) ScanUsage(line []byte) (int, bool)

ScanUsage scans fixture lines exactly as Claude scans live ones.

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.

func All

func All() []Harness

All returns the builtin harness set.

func ByID

func ByID(id string) (Harness, bool)

ByID returns the builtin harness with the given id, if any.

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.

Jump to

Keyboard shortcuts

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