feat: live guardrail for restated figures (opencode plugin) (#15)
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>
This commit was merged in pull request #15.
This commit is contained in:
@@ -119,3 +119,39 @@ func Unsourced(findings []Finding) []Finding {
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Scored is a finding with the verdict for a specific evidence window.
|
||||
type Scored struct {
|
||||
Finding
|
||||
Status Status
|
||||
}
|
||||
|
||||
// Classify tags findings against this turn's tool output and the claims this
|
||||
// session already made in earlier turns. It is the single place that decides
|
||||
// sourced/stale/unsupported, so the CLI and the live plugin cannot disagree.
|
||||
//
|
||||
// Staleness is measured against *earlier prose*, not earlier tool output, and
|
||||
// that distinction matters: a tool printed "6884 deletions" while the prose
|
||||
// said "6,884 lines", so matching tool output by number+unit called the real
|
||||
// incident "unsupported" — silent. Comparing prose to prose catches the actual
|
||||
// failure, which is restating a figure the session already asserted.
|
||||
func Classify(findings []Finding, turnOutputs []string, priorProse string) []Scored {
|
||||
prior := make(map[claimKey]struct{})
|
||||
for _, c := range Extract(priorProse) {
|
||||
prior[claimKey{c.Value, normUnit(c.Unit)}] = struct{}{}
|
||||
}
|
||||
|
||||
out := make([]Scored, 0, len(findings))
|
||||
for _, orig := range findings {
|
||||
f := Mark([]Finding{orig}, turnOutputs)[0]
|
||||
status := Sourced
|
||||
if !f.Sourced {
|
||||
status = Unsupported
|
||||
if _, repeated := prior[claimKey{f.Value, normUnit(f.Unit)}]; repeated {
|
||||
status = Stale
|
||||
}
|
||||
}
|
||||
out = append(out, Scored{Finding: f, Status: status})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -205,10 +205,10 @@ type LintRun struct {
|
||||
// quoted from an earlier turn count as unsourced.
|
||||
func Lint(s *Session) []LintRun {
|
||||
var out []LintRun
|
||||
var turnOutputs, earlierOutputs []string
|
||||
var turnOutputs []string
|
||||
var priorProse strings.Builder
|
||||
for _, m := range s.Messages {
|
||||
if m.Role == "user" {
|
||||
earlierOutputs = append(earlierOutputs, turnOutputs...)
|
||||
turnOutputs = nil
|
||||
continue
|
||||
}
|
||||
@@ -217,16 +217,11 @@ func Lint(s *Session) []LintRun {
|
||||
continue
|
||||
}
|
||||
prose := strings.Join(m.Text, "\n")
|
||||
for _, f := range Mark(Extract(prose), turnOutputs) {
|
||||
run := LintRun{MessageID: m.ID, Seq: m.Seq, Finding: f, Status: Sourced}
|
||||
if !f.Sourced {
|
||||
run.Status = Unsupported
|
||||
if len(Mark([]Finding{f}, earlierOutputs)) == 1 && Mark([]Finding{f}, earlierOutputs)[0].Sourced {
|
||||
run.Status = Stale
|
||||
}
|
||||
}
|
||||
out = append(out, run)
|
||||
for _, sc := range Classify(Extract(prose), turnOutputs, priorProse.String()) {
|
||||
out = append(out, LintRun{MessageID: m.ID, Seq: m.Seq, Finding: sc.Finding, Status: sc.Status})
|
||||
}
|
||||
priorProse.WriteString(prose)
|
||||
priorProse.WriteString("\n")
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
+41
-1
@@ -3,6 +3,7 @@ package cmd
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/david/knox/internal/claims"
|
||||
@@ -98,6 +99,45 @@ exactly what this flags: repetition should lower confidence, not raise it.`,
|
||||
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)
|
||||
// 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user