bench

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 17 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 bench runs Spackle over the labelled corpus in bench/corpus and scores what it reports against each fixture's ground truth. The matching rule and the metrics are documented in bench/README.md; this package is the only producer of the files in bench/results.

Index

Constants

View Source
const (
	ModeDeterministic = "deterministic"
	ModeAgentic       = "agentic"
)

Run modes, as recorded in a results file.

View Source
const Schema = "spackle.bench/v1"

Schema identifies the results file format.

Variables

This section is empty.

Functions

func FileName

func FileName(r Results) string

FileName returns "<date>-<model>.json" with the model sanitized to [a-z0-9._-] (other characters become "-", runs collapsed), "none" for deterministic.

func Summary

func Summary(r Results) string

Summary renders the short end-of-run report for the terminal.

func Table

func Table(rs []Results) string

Table renders the README table (GitHub markdown) for one or more results, newest first: columns Date | Version | Model | Mode | Precision | Recall | Verified-fix rate | Cost per finding. Cost per finding shows "n/a" for deterministic runs and "unknown" when the cost is not known.

func Write

func Write(dir string, r Results, force bool) (string, error)

Write writes results as indented JSON with a trailing newline into dir, creating it. It refuses to overwrite an existing file unless force is true.

Types

type CorpusInfo

type CorpusInfo struct {
	Fixtures     int            `json:"fixtures"`
	Clean        int            `json:"clean"`
	Vulnerable   int            `json:"vulnerable"`
	TruthEntries int            `json:"truth_entries"`
	ByLanguage   map[string]int `json:"by_language"`
}

CorpusInfo describes the corpus a run was measured against, so results from different corpus revisions are not compared as if they were the same.

type FixtureScore

type FixtureScore struct {
	Fixture        string     `json:"fixture"`
	Language       string     `json:"language"`
	Difficulty     string     `json:"difficulty"`
	Clean          bool       `json:"clean"`
	TruePositives  int        `json:"true_positives"`
	FalsePositives int        `json:"false_positives"`
	FalseNegatives int        `json:"false_negatives"`
	Matched        []Match    `json:"matched"`
	Missed         []Vuln     `json:"missed"`
	Spurious       []Reported `json:"spurious"`
	VerifiedFixes  int        `json:"verified_fixes"`
	// Error is set when the scan of this fixture failed.
	Error string `json:"error,omitempty"`
}

FixtureScore is the outcome for one fixture.

func Score

func Score(t Truth, reported []Reported) FixtureScore

Score compares reported findings with a fixture's truth. A reported finding matches a truth entry when the class is equal, the file is equal (forward slashes, case-sensitive) and the line is within 3 lines. Each truth entry and each reported finding is matched at most once; when several candidates exist prefer the smallest line distance, then the lower finding id. Dismissed findings are ignored entirely (not TP, not FP). In a clean fixture every non-dismissed reported finding is a false positive.

type Match

type Match struct {
	Class        string `json:"class"`
	File         string `json:"file"`
	Line         int    `json:"line"`
	FindingID    string `json:"finding_id"`
	ReportedLine int    `json:"reported_line"`
}

Match pairs one truth entry with the reported finding that matched it.

type Options

type Options struct {
	CorpusDir string
	Config    config.Config
	Version   string
	Now       func() time.Time
	Progress  io.Writer
	// Scan runs one fixture; nil means engine.Run. Tests inject a fake.
	Scan func(ctx context.Context, o engine.Options) (engine.Result, error)
}

Options configure a benchmark run.

type Reported

type Reported struct {
	ID         string `json:"id"`
	Class      string `json:"class"`
	File       string `json:"file"`
	Line       int    `json:"line"`
	Confidence string `json:"confidence"`
	Status     string `json:"status"`
	// Fixed is true when the finding carries a verified fix.
	Fixed bool `json:"fixed"`
}

Reported is a finding reduced to what scoring needs.

func FromFinding

func FromFinding(f finding.Finding) Reported

FromFinding reduces a finding to what scoring needs. A fix counts as verified only when the finding is marked fixed and its verification record says so; a patch that was written but not verified does not count.

type Results

type Results struct {
	Schema       string            `json:"schema"`
	Date         string            `json:"date"` // YYYY-MM-DD (UTC)
	Version      string            `json:"spackle_version"`
	Commit       string            `json:"commit,omitempty"`
	Mode         string            `json:"mode"` // deterministic|agentic
	Provider     string            `json:"provider,omitempty"`
	Model        string            `json:"model"` // "none" for deterministic
	Corpus       CorpusInfo        `json:"corpus"`
	Totals       Totals            `json:"totals"`
	ByLanguage   map[string]Totals `json:"by_language"`
	ByDifficulty map[string]Totals `json:"by_difficulty"`
	Fixtures     []FixtureScore    `json:"fixtures"`
	Notes        []string          `json:"notes,omitempty"`
}

Results is the file written to bench/results/<date>-<model>.json.

func LoadResults

func LoadResults(dir string) ([]Results, error)

LoadResults reads every *.json in dir (sorted newest first). A missing dir returns no results.

func Run

func Run(ctx context.Context, opts Options) (Results, error)

Run scans every fixture in the corpus and scores the findings.

A fixture whose scan fails is recorded with its Error and its truth entries count as false negatives. The run stops early, returning what was done with a Note, when the context is cancelled, when a scan is refused for a reason that would repeat for every fixture, or when the cumulative model spend reaches Config.Budget.USD. Callers can tell a stopped run from a complete one by comparing len(Fixtures) with Corpus.Fixtures.

type Totals

type Totals struct {
	TruePositives  int `json:"true_positives"`
	FalsePositives int `json:"false_positives"`
	FalseNegatives int `json:"false_negatives"`
	// Precision, Recall and F1 are 0 when the denominator is 0, and are
	// rounded to 4 decimals.
	Precision     float64 `json:"precision"`
	Recall        float64 `json:"recall"`
	F1            float64 `json:"f1"`
	VerifiedFixes int     `json:"verified_fixes"`
	// VerifiedFixRate is verified fixes / true positives.
	VerifiedFixRate float64 `json:"verified_fix_rate"`
	CostUSD         float64 `json:"cost_usd"`
	CostKnown       bool    `json:"cost_known"`
	InputTokens     int     `json:"input_tokens"`
	OutputTokens    int     `json:"output_tokens"`
	TokensEstimated bool    `json:"tokens_estimated"`
	// CostPerFinding is CostUSD / true positives; 0 when either is 0.
	CostPerFinding float64 `json:"cost_per_finding"`
	// DecoysFlagged counts clean fixtures with at least one false positive.
	DecoysFlagged int `json:"decoys_flagged"`
	CleanFixtures int `json:"clean_fixtures"`
	// FailedFixtures counts fixtures whose scan failed. They are left out
	// of every figure above: a scan that never ran found nothing, which
	// must not read as a decoy left alone or as a missed flaw.
	FailedFixtures int `json:"failed_fixtures,omitempty"`
}

Totals aggregates a set of fixture scores.

type Truth

type Truth struct {
	Fixture         string `json:"fixture"`
	Language        string `json:"language"`
	Difficulty      string `json:"difficulty"`
	Clean           bool   `json:"clean"`
	Description     string `json:"description"`
	Vulnerabilities []Vuln `json:"vulnerabilities"`
}

Truth is one fixture's ground truth (bench/corpus/<fixture>/truth.json).

func LoadCorpus

func LoadCorpus(dir string) ([]Truth, error)

LoadCorpus reads every fixture directory under dir (sorted by name). A directory without SPACKLE_FIXTURE, README.md or a parseable truth.json is an error naming the fixture.

type Vuln

type Vuln struct {
	Class string `json:"class"`
	CWE   string `json:"cwe"`
	File  string `json:"file"`
	Line  int    `json:"line"`
	Sink  string `json:"sink"`
	Note  string `json:"note"`
}

Vuln is one planted vulnerability.

Jump to

Keyboard shortcuts

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