4a3e671aa1
feat: live guardrail for restated figures (opencode plugin) (#15) Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
144 lines
4.3 KiB
Go
144 lines
4.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"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")
|
|
|
|
// 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)
|
|
},
|
|
}
|
|
|
|
cmd.AddCommand(lintCmd, checkCmd)
|
|
return cmd
|
|
}
|