package cmd import ( "encoding/json" "fmt" "os" "strings" "github.com/david/knox/internal/claims" "github.com/david/knox/internal/db" "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(kdb *db.KnoxDB) *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") // check is the live contract used by the opencode plugin: it takes the // assembled turn from stdin and reports verdicts, so all the classification // logic stays in one tested place instead of being reimplemented in TS. checkCmd := &cobra.Command{ Use: "check", Short: "Classify claims from a JSON turn on stdin (plugin interface)", Long: `Read {"prose": "...", "outputs": [...], "prior_prose": "..."} on stdin and report each numeric claim as sourced, stale or unsupported. "outputs" are tool results from the current turn. "prior_prose" is everything the assistant asserted in earlier turns of the same session: a figure repeated from there without fresh evidence is what "stale" means.`, RunE: func(c *cobra.Command, args []string) error { var in struct { Prose string `json:"prose"` Outputs []string `json:"outputs"` PriorProse string `json:"prior_prose"` } if err := json.NewDecoder(os.Stdin).Decode(&in); err != nil { return fmt.Errorf("read stdin: %w", err) } scored := claims.Classify(claims.Extract(in.Prose), in.Outputs, in.PriorProse) out := struct { Findings []map[string]any `json:"findings"` }{} for _, s := range scored { out.Findings = append(out.Findings, map[string]any{ "assertion": s.Assertion, "value": s.Value, "unit": s.Unit, "status": s.Status.String(), }) } enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") return enc.Encode(out) }, } // record persists a check's verdicts into the ledger so repetition is // tracked across turns and sessions, not just within one transcript. recordCmd := &cobra.Command{ Use: "record", Short: "Persist check verdicts from stdin JSON into the claim ledger", RunE: func(c *cobra.Command, args []string) error { var in struct { Findings []struct { Assertion string `json:"assertion"` Value int64 `json:"value"` Unit string `json:"unit"` Status string `json:"status"` } `json:"findings"` } if err := json.NewDecoder(os.Stdin).Decode(&in); err != nil { return fmt.Errorf("read stdin: %w", err) } for _, f := range in.Findings { if err := kdb.RecordClaim(f.Assertion, f.Unit, f.Value, f.Status); err != nil { return err } } fmt.Printf("recorded %d claim(s)\n", len(in.Findings)) return nil }, } // due is the digest the plugin injects: claims that need re-checking. var dueLimit int var dueJSON bool dueCmd := &cobra.Command{ Use: "due", Short: "Claims needing re-verification (restated without evidence, or past interval)", RunE: func(c *cobra.Command, args []string) error { due, err := kdb.DueClaims(dueLimit) if err != nil { return err } if dueJSON { enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") return enc.Encode(due) } if len(due) == 0 { fmt.Println("no claims due") return nil } for _, c := range due { fmt.Printf("%s restated=%d verified=%d due=%s\n", c.Assertion, c.ReuseCount, c.VerifyCount, c.DueAt) } return nil }, } dueCmd.Flags().IntVar(&dueLimit, "limit", 5, "Max claims to list") dueCmd.Flags().BoolVar(&dueJSON, "json", false, "Machine-readable output") cmd.AddCommand(lintCmd, checkCmd, recordCmd, dueCmd) return cmd }