agent

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 23 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 agent holds the model-driven stages of the pipeline: planner, investigators, reviewer and the shared tool loop they run on.

The loop gives a model nothing but an allowlisted, read-only tool registry. Scope, budgets and tool permissions are enforced here in Go. Nothing a model or a target says can change them, because there is no tool whose effect is to change them.

Index

Constants

View Source
const (
	StopNone       = ""
	StopTurns      = "turn limit"
	StopStagnation = "stagnation"
	StopRefusal    = "provider refusal"
	StopTruncated  = "output truncated"
)

Why a conversation stopped before the model chose to.

View Source
const (
	ReviewConfirmed = "confirmed"
	ReviewRefuted   = "refuted"
	ReviewUncertain = "uncertain"
)

Review verdicts.

Variables

This section is empty.

Functions

func ApplyReview

func ApplyReview(f finding.Finding, rev Review) finding.Finding

ApplyReview returns the finding updated with the review's outcome.

  • confirmed: unchanged, apart from a corrected severity.
  • refuted: dismissed, with the reviewer's reason kept.
  • uncertain: kept open, but confidence drops to plausible, because a second reader could not verify it.

func BuildFinding

func BuildFinding(root string, d Draft, origin []string) (finding.Finding, error)

BuildFinding validates a draft against the target and converts it. It returns an error describing every problem, so a rejected draft can be sent back to the model once with specific corrections.

What is checked: the class is known; the file exists inside the target; the line range is inside the file; every quoted line really appears in the file it cites (line numbers are corrected when the quote is found elsewhere in the same file); and the CVSS vector is valid. The score and label are computed here, never taken from the model.

func Correlate

func Correlate(ctx context.Context, r *Runner, parent int64, sealer *envelope.Sealer, fs []finding.Finding) ([]finding.Finding, error)

Correlate asks the model which confirmed findings combine into a larger attack and returns the findings with their kill chain and related ids filled in. Chains that name unknown ids are ignored. A failure here is not fatal: the findings are returned unchanged with the error.

func NewMarker

func NewMarker() (string, error)

NewMarker returns the random token a proof must print to count as a demonstration. It is unguessable so that a proof cannot "succeed" by accident, for example by echoing a file that happens to contain it.

Types

type Box

type Box struct {
	Runtime sandbox.Runtime
	Profile sandbox.Profile
	Budget  *budget.Budget
	Root    string
	// Record is called after every sandbox run so the engine can write the
	// ledger event and the evidence file.
	Record func(purpose string, spec sandbox.Spec, res sandbox.Result) error
}

Box runs things in the sandbox and accounts for them. The engine supplies it; it is the only way the prover and fixer can execute anything.

type Check

type Check func(ctx context.Context, files map[string]string) (problem string, verification string, verified bool, err error)

Check verifies one candidate fix. A non-empty problem rejects the fix and is sent back to the model. With an empty problem the fix is kept, and verified says whether it is proven or only offered.

type Diagnosis

type Diagnosis struct {
	Title, RootCause string
	// File and Line name the faulty code. File is empty when the model named
	// a file that is not in the target.
	File string
	Line int
	// Quotes are the evidence lines that really appear in the files they
	// cite, with corrected line numbers. Quotes that do not match are gone.
	Quotes          []DraftStep
	FixSummary, Why string
	// Files maps a target-relative path to its complete new content.
	Files map[string]string
	// Patch is the unified diff Spackle computed from Files.
	Patch        string
	Verified     bool
	Verification string
	Attempts     int
	// Reason explains why there is no root cause or no accepted fix.
	Reason string
}

Diagnosis is the outcome of looking for a root cause and a fix.

func Diagnose

func Diagnose(ctx context.Context, r *Runner, parent int64, sealer *envelope.Sealer, reg *tools.Registry,
	root string, failure string, wantFix bool, maxAttempts int, check Check) (Diagnosis, error)

Diagnose asks the model for the root cause and a fix, calls check to verify each candidate patch, and feeds a rejected attempt's problem back, up to maxAttempts times. When wantFix is false it asks only for the root cause. The failure text is untrusted and reaches the model only inside an envelope. A fix that check rejected is never returned.

type Draft

type Draft struct {
	Title             string      `json:"title"`
	Class             string      `json:"class"`
	CWE               []string    `json:"cwe"`
	File              string      `json:"file"`
	StartLine         int         `json:"start_line"`
	EndLine           int         `json:"end_line"`
	Symbol            string      `json:"symbol"`
	Vector            string      `json:"cvss_vector"`
	SeverityReasoning string      `json:"severity_reasoning"`
	Description       string      `json:"description"`
	Flow              []DraftStep `json:"flow"`
	Preconditions     []string    `json:"preconditions"`
	AttackSteps       []string    `json:"attack_steps"`
	ChainsInto        []string    `json:"chains_into"`
	Gains             []string    `json:"gains"`
	Exposed           []string    `json:"exposed"`
	FixSummary        string      `json:"fix_summary"`
	FixWhy            string      `json:"fix_why"`
	DefenseInDepth    []string    `json:"defense_in_depth"`
	Confidence        string      `json:"confidence"`
	ConfidenceReason  string      `json:"confidence_reason"`
}

Draft is a finding as a model proposes it. Nothing in a draft is trusted: BuildFinding checks every claim it can against the target before the draft becomes a finding.

type DraftStep

type DraftStep struct {
	Role  string `json:"role"`
	File  string `json:"file"`
	Line  int    `json:"line"`
	Quote string `json:"quote"`
	Note  string `json:"note"`
}

DraftStep is one flow step in a draft.

type EndpointReview

type EndpointReview struct {
	// Confirmed is false when the investigator concluded the observation is
	// not a real flaw; Reason then says why.
	Confirmed bool
	Reason    string
	Finding   finding.Finding
	// Rejected is set when the answer could not be used. The finding is
	// then left as the probe reported it.
	Rejected string
}

EndpointReview is an investigator's assessment of one running-target finding.

func InvestigateEndpoint

func InvestigateEndpoint(ctx context.Context, r *Runner, parent int64, sealer *envelope.Sealer, reg *tools.Registry, f finding.Finding, exchanges string, known func(id string) bool) (EndpointReview, error)

InvestigateEndpoint asks the model to confirm and explain one running-target finding. exchanges are the recorded request and response pairs that produced it, already redacted, as text. known reports whether an exchange id exists, so the model cannot cite evidence that was never recorded.

type FixInput

type FixInput struct {
	Language string
	Image    string
	// PoC is the proof that demonstrated the finding, or nil.
	PoC *PoC
	// TestCmd is the project's test command as an argument vector, or nil.
	TestCmd []string
	// BaselineTestsPass says whether TestCmd passed on the unmodified code.
	// A patch is only held to the suite when the suite was green to begin
	// with: a suite that already fails cannot tell a regression from noise.
	BaselineTestsPass bool
	MaxAttempts       int
}

FixInput is what the fixer needs besides the finding.

type FixResult

type FixResult struct {
	// Files maps a target-relative path to its full new content.
	Files map[string]string
	// Patch is the unified diff Spackle computed from Files.
	Patch string
	Why   string
	// Verified is true only when a proof of concept that used to succeed no
	// longer does, and nothing that was checked got worse.
	Verified bool
	// Verification says, in a sentence, exactly what was and was not checked.
	Verification string
	Attempts     int
	// Reason explains a failure to produce any acceptable patch.
	Reason string
}

FixResult is the outcome of trying to fix a finding.

func Fix

func Fix(ctx context.Context, r *Runner, parent int64, sealer *envelope.Sealer, reg *tools.Registry, box *Box, in FixInput, f finding.Finding) (FixResult, error)

Fix asks the model for a patch and verifies it in the sandbox, up to MaxAttempts times. Each rejected attempt is sent back with the reason. A patch that fails verification is never returned.

type Intent

type Intent = ledger.Intent

Intent is one planned investigation.

func FallbackPlan

func FallbackPlan(m mapper.Map, leads []seed.Lead, limit int) []Intent

FallbackPlan builds intents straight from leads, one per file and class, worst severity first. It is used when the planner fails or returns nothing usable, so a model problem degrades the run instead of ending it.

func Plan

func Plan(ctx context.Context, r *Runner, parent int64, sealer *envelope.Sealer, m mapper.Map, leads []seed.Lead, limit int) ([]Intent, error)

Plan asks the model for a prioritized list of investigations. Whatever the model returns is validated against the real map and leads: intents that name files or leads that do not exist lose those references, and intents left with nothing to look at are dropped.

type Investigation

type Investigation struct {
	Intent Intent
	// Finding is set when the investigator reported a flaw that passed
	// validation.
	Finding *finding.Finding
	// NoFinding is the investigator's reason when it concluded the lead
	// does not hold up.
	NoFinding string
	// Rejected is why the result could not be used: malformed output, a
	// quote that is not in the file, a refusal, a cut-off answer.
	Rejected string
}

Investigation is the result of investigating one intent.

func Investigate

func Investigate(ctx context.Context, r *Runner, parent int64, root string, sealer *envelope.Sealer, reg *tools.Registry, intent Intent, leads []seed.Lead) (Investigation, error)

Investigate runs one investigation. A draft that fails validation is sent back once with the specific problems; if the second answer also fails, the investigation is rejected and recorded, and the run continues.

type Outcome

type Outcome struct {
	Text    string
	Turns   int
	Stopped string
	// Note explains Stopped when there is more to say (a refusal category).
	Note string
}

Outcome is the result of one conversation.

type PoC

type PoC struct {
	Files       map[string]string `json:"files"`
	Command     []string          `json:"command"`
	Marker      string            `json:"marker"`
	Language    string            `json:"language"`
	Image       string            `json:"image"`
	Explanation string            `json:"explanation,omitempty"`
}

PoC is a proof of concept: files added to a copy of the target and the command that runs them inside the sandbox.

func (*PoC) SortedFiles

func (p *PoC) SortedFiles() []string

SortedFiles returns the proof's file names in a stable order.

type ProofResult

type ProofResult struct {
	PoC          *PoC
	Demonstrated bool
	Output       string // redacted, clipped
	Isolation    string
	Reason       string // why it was not demonstrated
	Attempts     int
}

ProofResult is the outcome of trying to demonstrate a finding.

func Prove

func Prove(ctx context.Context, r *Runner, parent int64, sealer *envelope.Sealer, reg *tools.Registry, box *Box, language, image string, f finding.Finding) (ProofResult, error)

Prove asks the model for a proof of concept and runs it in the sandbox. A proof that fails is sent back once with its output. The finding counts as demonstrated only when the run exits 0 and prints the marker.

func Reproduce

func Reproduce(ctx context.Context, r *Runner, parent int64, sealer *envelope.Sealer, reg *tools.Registry,
	box *Box, language, image, issue string) (ProofResult, error)

Reproduce asks the model for a script that reproduces the issue. Same file and command rules as proofs (validatePoC); the script prints the marker only when the buggy behavior is observed. The issue counts as reproduced only when the run exits 0 and prints the marker. A script that fails is sent back once with its output.

type Review

type Review struct {
	Verdict string
	Reason  string
	// Severity is set when the reviewer supplied a valid corrected vector.
	Severity *finding.Severity
}

Review is the reviewer's verdict on one finding.

func ReviewFinding

func ReviewFinding(ctx context.Context, r *Runner, parent int64, sealer *envelope.Sealer, reg *tools.Registry, f finding.Finding) (Review, error)

ReviewFinding runs the adversarial review of one finding in a fresh conversation with its own tool registry.

type Runner

type Runner struct {
	Provider llm.Provider
	Pricer   llm.Pricer
	Budget   *budget.Budget
	Ledger   *ledger.Writer
	// Stage names the pipeline stage in the ledger: planner, investigator,
	// reviewer, prover, fixer or mechanical.
	Stage string
	// MaxTurns caps tool-use turns in one conversation.
	MaxTurns int
	// MaxTokens caps one completion.
	MaxTokens int
}

Runner runs one stage's conversations against a provider.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, parent int64, system, user string, reg *tools.Registry) (Outcome, error)

Run holds a conversation: it sends system and user, executes any tool calls through reg, and returns the model's final text. reg may be nil for a conversation with no tools. A budget error is returned as an error wrapping budget.ErrExhausted, with whatever was produced so far.

Directories

Path Synopsis
Package tools is the allowlisted, read-only tool set an investigator model uses to look at a target.
Package tools is the allowlisted, read-only tool set an investigator model uses to look at a target.

Jump to

Keyboard shortcuts

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