feat: audit session transcripts for numeric claims with no tool evidence (#12)
Adds a deterministic check for the failure mode where a figure is derived once and then restated without being re-checked. - internal/claims extracts number+unit assertions from assistant prose and matches them against tool output, unit-aware (matching a bare number makes "200MB" count as backed by any output containing 200). - Classification: sourced (printed this turn), stale (printed in an earlier turn, restated now), unsupported (never printed by a tool). - `knox claims lint [--session ID] [--json]` reads opencode.db. The evidence window is the turn, not the message: opencode emits one message per step, so a tool call and the prose citing it land in consecutive messages. No model calls, so it costs nothing to run.
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
// Package claims implements the sourced-claim check.
|
||||
//
|
||||
// The rule it enforces is narrow on purpose: a number asserted in assistant
|
||||
// prose must appear in the output of a tool call in the same message. If it
|
||||
// does not, the number was either remembered from an earlier turn or derived
|
||||
// without being shown — the shape of error that produced "6,884 lines" from a
|
||||
// diff that had been run against the wrong ref, and then repeated five times
|
||||
// without re-checking.
|
||||
//
|
||||
// It is fully deterministic: no model calls, no tokens. That is what makes it
|
||||
// affordable to run on every message.
|
||||
package claims
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Finding is one numeric assertion and whether this message backed it.
|
||||
type Finding struct {
|
||||
Assertion string // as written, e.g. "6,884 lines"
|
||||
Value int64 // normalized numeric value, e.g. 6884
|
||||
Unit string // e.g. "lines"
|
||||
Sourced bool
|
||||
}
|
||||
|
||||
// claimRe matches a number followed by a unit we care about. The unit list is
|
||||
// deliberately small: every entry is something a tool can be expected to have
|
||||
// printed, so "unsourced" is a fair verdict rather than a style opinion.
|
||||
var claimRe = regexp.MustCompile(
|
||||
`(?i)\b(\d[\d,_.]*)\s*(lines?|commits?|files?|rows?|bytes?|observations?|entries|threads?|peers?|nodes?|ms|%|[KMGT]i?B)\b`)
|
||||
|
||||
// Extract returns the numeric assertions in a piece of prose.
|
||||
func Extract(text string) []Finding {
|
||||
matches := claimRe.FindAllStringSubmatch(text, -1)
|
||||
if len(matches) == 0 {
|
||||
return nil
|
||||
}
|
||||
findings := make([]Finding, 0, len(matches))
|
||||
for _, m := range matches {
|
||||
value, ok := parseNumber(m[1])
|
||||
if !ok || value < 10 {
|
||||
// Single digits and small counts collide with everything a tool
|
||||
// prints; flagging them would train the reader to ignore output.
|
||||
continue
|
||||
}
|
||||
findings = append(findings, Finding{
|
||||
Assertion: strings.TrimSpace(m[0]),
|
||||
Value: value,
|
||||
Unit: strings.ToLower(m[2]),
|
||||
})
|
||||
}
|
||||
return findings
|
||||
}
|
||||
|
||||
// claimKey identifies a measurement: the number and its unit together. Matching
|
||||
// the number alone is far too loose — "200MB" would be "backed" by any output
|
||||
// containing 200 — so the unit has to agree too.
|
||||
type claimKey struct {
|
||||
value int64
|
||||
unit string
|
||||
}
|
||||
|
||||
// normUnit folds plurals so "1 line" and "3 lines" share a unit, without
|
||||
// mangling unit symbols.
|
||||
func normUnit(u string) string {
|
||||
u = strings.ToLower(strings.TrimSpace(u))
|
||||
switch u {
|
||||
case "%", "ms", "kb", "kib", "mb", "mib", "gb", "gib", "tb", "tib":
|
||||
return u
|
||||
}
|
||||
return strings.TrimSuffix(u, "s")
|
||||
}
|
||||
|
||||
// Mark records whether each finding is backed by one of the tool outputs.
|
||||
func Mark(findings []Finding, toolOutputs []string) []Finding {
|
||||
if len(findings) == 0 {
|
||||
return findings
|
||||
}
|
||||
seen := make(map[claimKey]struct{})
|
||||
for _, out := range toolOutputs {
|
||||
for _, c := range Extract(out) {
|
||||
seen[claimKey{c.Value, normUnit(c.Unit)}] = struct{}{}
|
||||
}
|
||||
}
|
||||
for i := range findings {
|
||||
_, findings[i].Sourced = seen[claimKey{findings[i].Value, normUnit(findings[i].Unit)}]
|
||||
}
|
||||
return findings
|
||||
}
|
||||
|
||||
// parseNumber converts "6,884" / "6_884" / "6884.0" to 6884. Decimals are
|
||||
// truncated to their integer part: prose usually rounds anyway, and the point
|
||||
// is to find the magnitude a tool printed.
|
||||
func parseNumber(s string) (int64, bool) {
|
||||
s = strings.TrimSpace(s)
|
||||
s = strings.NewReplacer(",", "", "_", "").Replace(s)
|
||||
if i := strings.IndexByte(s, '.'); i >= 0 {
|
||||
s = s[:i]
|
||||
}
|
||||
if s == "" {
|
||||
return 0, false
|
||||
}
|
||||
v, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return v, true
|
||||
}
|
||||
|
||||
// Unsourced filters to the findings that no tool call backed.
|
||||
func Unsourced(findings []Finding) []Finding {
|
||||
var out []Finding
|
||||
for _, f := range findings {
|
||||
if !f.Sourced {
|
||||
out = append(out, f)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user