029c5078ba
feat: audit session transcripts for numeric claims with no tool evidence (#13) Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
122 lines
3.7 KiB
Go
122 lines
3.7 KiB
Go
// 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
|
|
}
|