Documentation
¶
Overview ¶
Package agent runs the step loop: query the model, execute tool calls, repeat.
Index ¶
- Constants
- func AdditionalDirsInfo(dirs []tools.ExtraDir, readOnly bool) []trajectory.AdditionalDir
- func ApplyPatchToolSignature(readOnly, plan, images, applyPatch bool) string
- func CommandEnvInfo(c *tools.CommandEnv) *trajectory.CommandEnvInfo
- func CommandToolsInfo(e *tools.Executor) []trajectory.CommandToolInfo
- func FileAccessInfo(f *tools.FileAccess) *trajectory.FileAccessInfo
- func ImagesToolSignature(readOnly, plan, images bool) string
- func LSPToolSignature(readOnly, plan, images, applyPatch, delegate, webFetch, lsp bool) string
- func PermissionsInfo(p *permissions.Policy) *trajectory.PermissionsInfo
- func PlanToolSignature(readOnly, plan bool) string
- func ScopedAgentsMDDelivered(t *trajectory.Trajectory) []string
- func SubmitToolSpec(s *outschema.Schema) model.ToolSpec
- func SystemPrompt(cfg config.AgentConfig, root string, skipAgentsMD bool, readOnly ...bool) string
- func ToolSignature(readOnly ...bool) string
- func ValidateResume(t *trajectory.Trajectory, cfg config.AgentConfig, root string) error
- func WebFetchToolSignature(readOnly, plan, images, applyPatch, delegate, webFetch bool) string
- type Agent
- type Snapshotter
- type Subagents
- type Workers
Constants ¶
const ( StatusSubmitted = "Submitted" StatusCompleted = "Completed" // model stopped calling tools without the submit marker StatusStepLimit = "StepLimitExceeded" StatusCostLimit = "CostLimitExceeded" StatusTimeLimit = "TimeLimitExceeded" StatusInterrupted = "Interrupted" StatusError = "Error" // StatusInvalidOutput ends an --output-schema run that produced no valid // submit_result within MaxInvalidOutputs consecutive attempts. StatusInvalidOutput = "InvalidOutput" // StatusFormatError: too many consecutive steps of malformed tool calls. StatusFormatError = "RepeatedFormatError" // StatusVerificationFailed: stop hooks were still rejecting the result // when the max_stop_continuations cap was reached. StatusVerificationFailed = "VerificationFailed" // StatusOutputTruncated: too many consecutive responses were cut off at // the output-token limit. StatusOutputTruncated = "OutputTruncated" // StatusNeedsInput: the model asked the user a question with ask_user; // a resume with the answer continues the run. StatusNeedsInput = trajectory.StatusNeedsInput )
const MaxInvalidOutputs = 3
MaxInvalidOutputs is how many consecutive invalid result attempts (an invalid or mixed submit_result call, or a turn with no tool calls) end an --output-schema run as InvalidOutput.
const SubmitMarker = "COMPLETE_TASK_AND_SUBMIT_FINAL_OUTPUT"
const SubmitToolName = "submit_result"
SubmitToolName is the built-in tool an --output-schema run ends with. It contains no "__", so it cannot collide with MCP tools (<alias>__<name>).
Variables ¶
This section is empty.
Functions ¶
func AdditionalDirsInfo ¶ added in v0.5.0
func AdditionalDirsInfo(dirs []tools.ExtraDir, readOnly bool) []trajectory.AdditionalDir
AdditionalDirsInfo records dirs, with their effective modes, for trajectories and run.started; nil when there are none.
func ApplyPatchToolSignature ¶ added in v0.5.0
ApplyPatchToolSignature is ImagesToolSignature with apply_patch enabled or not. Without apply_patch it equals ImagesToolSignature.
func CommandEnvInfo ¶ added in v0.5.0
func CommandEnvInfo(c *tools.CommandEnv) *trajectory.CommandEnvInfo
CommandEnvInfo summarizes c by name for trajectories and run.started; nil when no policy is active.
func CommandToolsInfo ¶ added in v0.5.0
func CommandToolsInfo(e *tools.Executor) []trajectory.CommandToolInfo
CommandToolsInfo records the command tools e offers for trajectories; nil when it offers none.
func FileAccessInfo ¶ added in v0.5.0
func FileAccessInfo(f *tools.FileAccess) *trajectory.FileAccessInfo
FileAccessInfo records f's rules for trajectories and run.started; nil when f is nil.
func ImagesToolSignature ¶ added in v0.5.0
ImagesToolSignature is ToolSignature with update_plan and view_image enabled or not. Without images it equals PlanToolSignature.
func LSPToolSignature ¶ added in v0.5.0
LSPToolSignature is the signature with every opt-in tool flag, lsp last.
func PermissionsInfo ¶ added in v0.8.0
func PermissionsInfo(p *permissions.Policy) *trajectory.PermissionsInfo
PermissionsInfo records p's policy for trajectories and run.started; nil when p is nil.
func PlanToolSignature ¶ added in v0.5.0
PlanToolSignature is ToolSignature with update_plan enabled or not.
func ScopedAgentsMDDelivered ¶ added in v0.5.0
func ScopedAgentsMDDelivered(t *trajectory.Trajectory) []string
ScopedAgentsMDDelivered returns the scoped AGENTS.md paths of checkpoint t whose delivering results are still in context, for run.started; nil when there are none.
func SubmitToolSpec ¶ added in v0.5.0
SubmitToolSpec returns the submit_result tool for s.
func SystemPrompt ¶ added in v0.4.0
SystemPrompt returns the effective system prompt a run rooted at root would use, given cfg and whether AGENTS.md is skipped. It is exported so callers can validate a checkpoint against the current configuration.
func ToolSignature ¶ added in v0.4.0
ToolSignature fingerprints the definitions plus an execution contract version. Bump the prefix when tool semantics change without a spec change. The plan tool is opt-in: without it the signature is unchanged, so checkpoints from earlier builds still resume.
func ValidateResume ¶ added in v0.4.0
func ValidateResume(t *trajectory.Trajectory, cfg config.AgentConfig, root string) error
ValidateResume runs before provider setup, using only the explicit execution root and current config. Checkpoint paths and credentials are never executed or loaded from metadata.
func WebFetchToolSignature ¶ added in v0.5.0
WebFetchToolSignature is the signature with every opt-in tool flag, web_fetch last.
Types ¶
type Agent ¶
type Agent struct {
Model model.Model
ModelName string
// FallbackModels is the fallback chain recorded in the trajectory and
// run.started. Model must then be a *model.Fallback over ModelName
// followed by these specs; empty means no fallback.
FallbackModels []string
// ReasoningEffort is recorded in new trajectories; the Model sends it.
ReasoningEffort string
// OpenAIAPI is the openai provider's resolved transport, recorded in
// trajectories and run.started; the Model uses it.
OpenAIAPI string
// Profile is the config profile selected with --profile, recorded in
// the trajectory and run.started; "" when none.
Profile string
// ProviderAPI and ProviderBaseURL describe a config-defined provider,
// recorded in trajectories and run.started; empty for built-ins.
ProviderAPI string
ProviderBaseURL string
Tools *tools.Executor
// RedactSecrets describes Tools.Redactor by name for the trajectory
// and run.started; nil when redact_secrets is off.
RedactSecrets *trajectory.RedactSecretsInfo
Config config.AgentConfig
Out io.Writer // progress log; nil discards
Err io.Writer // warnings; nil discards
Verbose bool
// Streamed means the model already wrote its text to Out as it arrived.
Streamed bool
// SkipAgentsMD stops Run from appending the nearest AGENTS.md at or above
// <root> to the system prompt.
SkipAgentsMD bool
// ScopedAgentsMD delivers AGENTS.md files in directories below <root>
// with the results of the built-in file tools that touch paths there
// (--scoped-agents-md). SkipAgentsMD turns it off.
ScopedAgentsMD bool
// SkillsEnabled records that skills discovery was on for this run; the
// discovered skills are Tools.Skills.
SkillsEnabled bool
// NoCompact turns off context compaction: the full history is always
// sent, and a context-overflow error ends the run.
NoCompact bool
// Hooks run around tool calls the executor permits, and stop hooks
// before a successful finish; nil runs none.
Hooks *hooks.Runner
// Policy is the compiled tool permission policy; nil enforces nothing.
// It is applied between the executor's Permits check and the hooks, so a
// denial reuses the error-result path and hooks never override it.
Policy *permissions.Policy
// PermissionApprover answers a policy "ask" through the channel the run
// has (a terminal prompt, ACP session/request_permission, ...). nil
// resolves asks with the policy's non-interactive decision.
PermissionApprover func(ctx context.Context, req permissions.Request) permissions.Answer
// OnStep is called after every step, e.g. to save the trajectory. A
// non-nil error stops the run.
OnStep func(*trajectory.Trajectory) error
// OnEvent receives machine-readable progress events when configured.
OnEvent func(string, map[string]any)
// Resume, when non-nil, continues that trajectory instead of starting a
// fresh run: its messages, accounting totals, and steps are the starting
// state, and a continuation notice is appended before the first request.
Resume *trajectory.Trajectory
ResumeSource string // audit-only input path for resume provenance
// FollowUp, with Resume, continues a finished (Submitted or Completed)
// checkpoint with this new user instruction, or answers a NeedsInput
// checkpoint's question. It must be empty for an unfinished checkpoint
// and set for the others.
FollowUp string
// Continue marks Resume as the trajectory this process's previous Run
// returned (an ACP session's earlier turn), not a loaded checkpoint:
// the checks against changes between invocations are skipped, and
// FollowUp continues it whatever status that turn ended with.
Continue bool
// TaskImages (--image) are attached to the task message of a fresh run,
// or to the follow-up message with FollowUp. The message text gets one
// label line per image.
TaskImages []model.Image
// OutputSchema, when non-nil, adds the submit_result tool and makes the
// run succeed only with a result that validates against it.
OutputSchema *outschema.Schema
// Snapshots, when non-nil, captures the workspace: a baseline before
// the first request, before every tool batch that can change files, and
// at the end of the run (--snapshots).
Snapshots Snapshotter
// Worktree, when non-nil, records the --worktree checkout a fresh run
// works in; it is copied into the new trajectory.
Worktree *trajectory.WorktreeInfo
// Review, when non-nil, records the change set a fresh review run was
// given; it is copied into the new trajectory.
Review *trajectory.ReviewInfo
// Subagents runs the opt-in delegate tool's children; Tools.Delegate
// offers the tool. nil leaves delegate calls failing with an error.
Subagents *Subagents
// Workers runs the opt-in worker tool's children; Tools.Workers
// offers the tool. nil leaves worker calls failing with an error.
Workers *Workers
// contains filtered or unexported fields
}
func (*Agent) Run ¶
func (a *Agent) Run(ctx context.Context, task string) *trajectory.Trajectory
Run executes task until submission, a limit, or an error. It always returns a trajectory describing how the run ended.
type Snapshotter ¶ added in v0.5.0
type Snapshotter interface {
Take(kind string, step int) (trajectory.Snapshot, error)
}
Snapshotter captures the working directory.
type Subagents ¶ added in v0.5.0
type Subagents struct {
// Model and ModelName are the children's model; the caller builds it
// (by default the parent's model spec, without streaming).
Model model.Model
ModelName string
// MaxSteps caps each child's model calls; 0 means
// config.DefaultSubagentMaxSteps.
MaxSteps int
// Streamed reports whether Model writes text to the terminal itself.
Streamed bool
}
Subagents configures the children the opt-in delegate tool runs.
type Workers ¶ added in v0.8.0
type Workers struct {
// Model and ModelName are the workers' model (by default the parent's
// model spec, without streaming).
Model model.Model
ModelName string
// MaxSteps caps each worker's model calls; 0 means
// config.DefaultWorkerMaxSteps.
MaxSteps int
// Tools are the configured worker tools; nil means
// config.DefaultWorkerTools. A worker gets only those the parent has.
Tools []string
// RunName prefixes worker branch and worktree names
// (tinyagent/<RunName>-w<n>); "" generates one per process.
RunName string
}
Workers configures the children the opt-in worker tool runs.