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:
+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