ab9066486a
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.
104 lines
2.8 KiB
Go
104 lines
2.8 KiB
Go
package cmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/david/knox/internal/claims"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// NewClaimsCmd exposes the sourced-claim check: numeric assertions in assistant
|
|
// prose that no tool call in the same message backed.
|
|
func NewClaimsCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "claims",
|
|
Short: "Audit numeric claims in session transcripts for missing evidence",
|
|
}
|
|
|
|
var (
|
|
sessionID string
|
|
dbPath string
|
|
jsonOut bool
|
|
)
|
|
lintCmd := &cobra.Command{
|
|
Use: "lint",
|
|
Short: "Report numeric assertions that no tool result in the same message supports",
|
|
Long: `Report numeric assertions that no tool result in the same message supports.
|
|
|
|
A claim is "sourced" when a tool call in the same assistant message printed the
|
|
same number. Restating a figure from an earlier turn without re-checking it is
|
|
exactly what this flags: repetition should lower confidence, not raise it.`,
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
db, err := claims.Open(dbPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
|
|
if sessionID == "" {
|
|
sessionID, err = claims.LatestSession(db)
|
|
if err != nil {
|
|
return fmt.Errorf("no sessions found: %w", err)
|
|
}
|
|
}
|
|
s, err := claims.LoadSession(db, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
runs := claims.Lint(s)
|
|
|
|
var stale, unsupported, sourced int
|
|
var flagged []claims.LintRun
|
|
for _, r := range runs {
|
|
switch r.Status {
|
|
case claims.Stale:
|
|
stale++
|
|
flagged = append(flagged, r)
|
|
case claims.Unsupported:
|
|
unsupported++
|
|
flagged = append(flagged, r)
|
|
default:
|
|
sourced++
|
|
}
|
|
}
|
|
|
|
if jsonOut {
|
|
b, _ := json.MarshalIndent(map[string]any{
|
|
"session": s.ID,
|
|
"title": s.Title,
|
|
"claims": len(runs),
|
|
"sourced": sourced,
|
|
"stale": stale,
|
|
"unsupported": unsupported,
|
|
"findings": flagged,
|
|
}, "", " ")
|
|
fmt.Println(string(b))
|
|
return nil
|
|
}
|
|
|
|
fmt.Printf("session %s (%s)\n", s.ID, strings.TrimSpace(s.Title))
|
|
fmt.Printf("claims: %d checked — %d sourced, %d stale, %d unsupported\n",
|
|
len(runs), sourced, stale, unsupported)
|
|
for _, r := range flagged {
|
|
mark := "✗"
|
|
if r.Status == claims.Sourced {
|
|
mark = "✓"
|
|
}
|
|
fmt.Printf(" %s %-11s msg %-4d %s\n", mark, r.Status, r.Seq, r.Finding.Assertion)
|
|
}
|
|
if len(flagged) == 0 {
|
|
fmt.Println("every numeric claim was backed by a tool result in its own turn")
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
lintCmd.Flags().StringVar(&sessionID, "session", "", "Session id (default: most recently updated)")
|
|
lintCmd.Flags().StringVar(&dbPath, "db", "", "opencode database path (default: ~/.local/share/opencode/opencode.db)")
|
|
lintCmd.Flags().BoolVar(&jsonOut, "json", false, "Machine-readable output")
|
|
|
|
cmd.AddCommand(lintCmd)
|
|
return cmd
|
|
}
|