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 ¶
const ( ModeDeterministic = "deterministic" ModeAgentic = "agentic" )
Run modes, as recorded in a results file.
const Schema = "spackle.bench/v1"
Schema identifies the results file format.
Variables ¶
This section is empty.
Functions ¶
func FileName ¶
FileName returns "<date>-<model>.json" with the model sanitized to [a-z0-9._-] (other characters become "-", runs collapsed), "none" for deterministic.
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 ¶
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 ¶
LoadResults reads every *.json in dir (sorted newest first). A missing dir returns no results.
func Run ¶
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 ¶
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.