agentadapter

package
v0.2.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 9 Imported by: 1 module, 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 agentadapter runs one coding-agent CLI (Grok Build, Claude Code, Antigravity) behind one interface: binary and version, readiness, argv for the implementer and the reviewer, the per-job settings file, the stream decoder, the resume id, and reviewer evidence and denials.

Input is a plain Task and output is events plus the process the caller starts. Persistence, the driver loop, verify and review orchestration stay with the caller (serve today, a worker later).

Import rule (docs/claude-code-runner.md criterion 11): nothing under this package imports internal/store, db, api, jobs, feedback or env, and no code here reads os.Environ or a DEFECT_DRAINER_* variable. The caller resolves configuration and hands it over. boundary_test.go enforces it.

Index

Constants

This section is empty.

Variables

View Source
var ErrAuth = errors.New("agent_auth")

ErrAuth wraps a readiness or run failure caused by the login or key. The job reason is agent_auth.

Functions

func AtLeast

func AtLeast(found, min string) bool

AtLeast compares dotted numeric versions. An unparsable found is false.

func ChildEnv

func ChildEnv(environ, keep, extra []string) []string

ChildEnv is the environment hygiene every child gets (agent, reviewer, verify, toolchain probe). From environ, which the caller read, it drops:

  • every DEFECT_DRAINER_* and DEFECT_CHANNEL_* name, and DEFECTS_ROOT;
  • DD_TEST_ACCOUNT_PASSWORD (a test job sets it again on purpose);
  • every adapter credential name not in keep;
  • the names in extra (the app's env: secret names).

The caller adds its own deliberate variables after this.

func CredentialEnvNames

func CredentialEnvNames() []string

CredentialEnvNames is every adapter credential name, sorted.

func GetEnv

func GetEnv(environ []string, key string) string

GetEnv reads key from an environ slice (last one wins), never os.Getenv.

func HostNote

func HostNote(a Adapter) string

HostNote is a's host marker, "" when a runs here.

func IsDiffOf

func IsDiffOf(cmd string, want map[string]struct{}) bool

IsDiffOf reports whether a shell command is a git diff naming one of want.

func LookBin

func LookBin(override, name string, candidates ...string) string

LookBin returns override when set, else the first existing candidate, else a PATH lookup of name. Adapters call it with the override the caller read.

func ParseVersion

func ParseVersion(out string) string

ParseVersion takes the first x.y.z in a --version output.

func PathSet

func PathSet(paths []string) map[string]struct{}

PathSet is the worktree paths as given and after EvalSymlinks (/var and /private/var on macOS), for matching against a shell command.

Types

type Adapter

type Adapter interface {
	Name() Name
	// Bin is the resolved binary path, "" when not installed.
	Bin() string
	// MinVersion is DD's minimum supported version.
	MinVersion() string
	// VersionArgs is the argv that prints the version.
	VersionArgs() []string
	// Prepare writes the per-task settings or sandbox file. It runs before
	// every spawn, so a file the agent changed is rewritten.
	Prepare(t Task) error
	// Command builds the process for t.
	Command(t Task) (Command, error)
	// NewDecoder returns a fresh stream decoder.
	NewDecoder() Decoder
	// FilterStderr maps one stderr line to what may be logged ("" drops it).
	FilterStderr(line string) string
	// ResumeID is the session to resume: from the CLI's own store (grok) or
	// the id stored from the last run (the others). ok false means the job
	// cannot be resumed.
	ResumeID(handoff, stored string) (id string, ok bool)
	// ResumesByID reports whether later iterations need ResumeID rather than
	// Continue.
	ResumesByID() bool
	// ReviewEvidence reports whether the reviewer's tool events show a
	// completed git diff of one of the worktrees.
	ReviewEvidence(tools [][]byte, worktrees []string) bool
	// Denied reports a permission refusal that must block a reviewer lens.
	Denied(tools [][]byte, end End) (tool string, ok bool)
	// Ready checks the login or key under the task's exact child env.
	Ready(ctx context.Context, t Task) error
	// CredentialEnv are the native credential variable names of this CLI.
	// Every other adapter's child, and every non-agent child, is denied them.
	CredentialEnv() []string
	// LogSource is the job-log source name.
	LogSource() string
	// SupportsSimulator reports whether the adapter can run the workspace
	// sandbox or Simulator writes. Only grok can in v1.
	SupportsSimulator() bool
}

Adapter is one coding-agent CLI.

type AuthError

type AuthError struct{ Reason string }

AuthError is a readiness failure with the CLI's reason.

func (*AuthError) Error

func (e *AuthError) Error() string

func (*AuthError) Unwrap

func (e *AuthError) Unwrap() error

type Command

type Command struct {
	Bin  string
	Args []string
	Dir  string
	Env  []string
}

Command is a process to start: exec.Command(Bin, Args...) with Dir and Env.

type Credential

type Credential struct {
	Mode CredentialMode
	// Key is the API key value in APIKey mode. Never logged, never in argv.
	Key string
	// ConfigDir is the CLI's config home: the logged-in dir in subscription
	// mode, or a per-job dir in API-key mode (Claude). Empty keeps the
	// CLI's default.
	ConfigDir string
}

Credential is what the caller resolved for this host. The adapter never reads a credential itself.

type CredentialMode

type CredentialMode string

CredentialMode is how the adapter's CLI authenticates.

const (
	// Subscription is the CLI's own login, owned by the host owner.
	Subscription CredentialMode = "subscription"
	// APIKey is a key the host owner put in serve's env file. It is passed
	// to this adapter's child only, under the CLI's native variable name.
	APIKey CredentialMode = "api_key"
)

type Decoder

type Decoder interface {
	Decode(line []byte) []Event
}

Decoder turns stdout lines into events. One decoder per process.

type End

type End struct {
	StopReason   string  `json:"stopReason"`
	SessionID    string  `json:"sessionId"`
	NumTurns     int     `json:"num_turns"`
	TotalCostUSD float64 `json:"total_cost_usd"`
	// IsError is the CLI's own failure flag (Claude is_error, agy status
	// ERROR). Grok has none.
	IsError bool `json:"-"`
	// AuthFailed means the CLI could not authenticate: the job reason is
	// agent_auth, never a retry.
	AuthFailed bool `json:"-"`
	// Message is the CLI's error text when IsError.
	Message string `json:"-"`
	// Denials are the tools the CLI's permission layer refused.
	Denials []string `json:"-"`
	// Tokens is the run's total token count where the CLI reports no cost.
	Tokens int `json:"-"`
}

End is the run's last event, normalised across adapters. The JSON names are grok's `end` event, which the jobs package already decodes.

type Event

type Event struct {
	Kind EventKind
	Text string
	Raw  []byte
	End  End
}

Event is one decoded stream line. A line can yield several events.

type EventKind

type EventKind int

EventKind is a decoded stream event.

const (
	// Ignore is anything the job log does not show (thoughts, usage, …).
	Ignore EventKind = iota
	// Text is assistant prose.
	Text
	// Tool is a tool call or result. Raw is what ReviewEvidence and Denied
	// read; the job log never shows it.
	Tool
	// EndEvent is the run's end.
	EndEvent
	// MaxTurns is the positive turn-cap signal. Turn exhaustion also exits
	// non-zero, and this is what tells it apart from a failure.
	MaxTurns
	// Error is an error event; Text holds the message.
	Error
	// Raw is a line that is not an event (an older CLI, a fake bin): it is
	// logged as is.
	Raw
	// Invalid is a `{` line that did not decode.
	Invalid
)

type HostLimiter

type HostLimiter interface {
	HostUnsupported() string
}

HostLimiter is implemented by an adapter that cannot run on some host OS. HostUnsupported returns a short marker ("unsupported-on-darwin") or "".

type Name

type Name string

Name is an adapter name, as DEFECT_DRAINER_AGENT_RUNNER spells it.

const (
	Grok        Name = "grok"
	Claude      Name = "claude"
	Antigravity Name = "antigravity"
)

type Probe

type Probe struct {
	Adapter Name
	State   VersionState
	// Found is the parsed version, "" when missing or unparsable.
	Found string
	Min   string
}

Probe is one adapter's install check.

func ProbeVersion

func ProbeVersion(ctx context.Context, a Adapter, env []string) Probe

ProbeVersion runs `<bin> <VersionArgs>` with env (the caller's child env) and classifies the result. It never fails: a missing or broken CLI is a state, because serve starts without an agent.

func (Probe) Marker

func (p Probe) Marker() string

Marker is the startup-line value: claude@2.1.292, claude@missing, claude@too-old.

type Role

type Role int

Role is which kind of agent process a Task starts.

const (
	// Implementer is the fix or test-run agent: it may edit the handoff and
	// the worktrees, and it resumes across iterations.
	Implementer Role = iota
	// Reviewer is one fresh, read-only review lens. It never resumes.
	Reviewer
)

type Task

type Task struct {
	Role Role
	// Handoff is the cwd: the job's handoff dir, or a review lens dir.
	Handoff string
	// Worktrees are the job's worktree paths the agent may edit.
	Worktrees []string
	// ReadOnly are absolute paths (files or dirs) the agent may read but never
	// edit: a reviewer's view of the job's BRIEF.md, NOTES.md and fix-notes.
	ReadOnly []string
	// Prompt is sent as is. On a resume the caller passes the continue prompt.
	Prompt string
	// MaxTurns is the per-iteration turn cap. Adapters without one ignore it.
	MaxTurns int
	// MaxTurnsArg, when set, is passed to grok's --max-turns verbatim (the
	// env override has always been passed through unparsed).
	MaxTurnsArg string
	// Continue resumes the previous iteration of this job. Grok continues
	// the latest session for the cwd; the others need ResumeID.
	Continue bool
	// ResumeID resumes that exact session. Wins over Continue.
	ResumeID string
	// Sandbox is the app's base sandbox (strict, workspace, read-only) and
	// SandboxProfile a job-scoped grok profile extending it.
	Sandbox        string
	SandboxProfile string
	// Env is the child's base environment, already filtered by the caller
	// (see ChildEnv). The adapter appends its own variables.
	Env []string
	// Credential is this adapter's credential for the host.
	Credential Credential
	// StateDir is a per-job directory outside the handoff that the agent
	// cannot write. Settings files live here.
	StateDir string
	// DenyRead are absolute paths the agent must not read (host secrets,
	// DD's data, other jobs' handoffs, env files, CLI config dirs). The
	// caller builds the list; adapters with read rules enforce it.
	DenyRead []string
	// Timeout bounds one invocation for CLIs that need it (agy).
	Timeout time.Duration
	// AllowCommands are binaries the agent's shell may run, for CLIs that
	// allow-list commands (agy). git is always added.
	AllowCommands []string
	// Grok-only switches the caller read from its env.
	GrokBypassPermissions bool
	GrokTools             string
}

Task is one agent process to start.

type VersionState

type VersionState string

VersionState is what the startup probe found.

const (
	VersionOK      VersionState = "ok"
	VersionMissing VersionState = "missing"
	VersionTooOld  VersionState = "too-old"
)

Directories

Path Synopsis
Package antigravity is the Antigravity CLI (`agy`) adapter.
Package antigravity is the Antigravity CLI (`agy`) adapter.
Package claude is the Claude Code adapter.
Package claude is the Claude Code adapter.
Package grok is the Grok Build adapter.
Package grok is the Grok Build adapter.

Jump to

Keyboard shortcuts

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