mesh-issues: check every new issue for a duplicate; claude-code: one judgement with no tools (hq ADR 0278)
mesh/merge-gate pass: builds claude-code, mesh-issues → ace, g14, novox, shanks; no bus step; every machine composes with the change as it did without (4 o…
mesh/repo-check pass: its merge-check.sh passed, each part in its toolchain: merge-check.sh (go), merge-check-typescript.sh (typescript)
mesh/delivery superseded: a newer head of the same pull request

The operator asked for duplicate issues to be consolidated as they are opened.
The tracker consumes its own opened event and asks the agent on its node, through
claude-code's new claude_code_judge, whether an older issue not resolved asks the
same question; it closes only the newer issue of a certain duplicate, keeping its
report on the older one, and fails loudly when it cannot judge. groom runs the
same judgement over the backlog as a dry run, applied on confirm.
This commit is contained in:
jochen
2026-10-10 17:11:54 +02:00
parent fb77be42ca
commit f58840b8a6
13 changed files with 1750 additions and 16 deletions
@@ -0,0 +1,246 @@
package main
// One judgement: a question answered by the node's agent with no tools (novox/hq ADR 0278 §5).
//
// A module that needs a judgement only a language model can make — the issue tracker's duplicate check — asks
// this module, which holds the agent and the licence the node is bound to, rather than taking the token itself
// (ADR 0206). The agent runs in print mode as the account agents run as on this node (ADR 0266), in an empty
// directory, with its built-in tools off, in safe mode (no tool servers, plugins, hooks or instruction files)
// and keeping no session: the session can do nothing but answer, so a question that carries text written to
// steer it can at most steer the answer. The answer is shaped by the caller's JSON schema.
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"time"
stdio "git.novox.be/novox/mesh-sdk/go"
)
const (
// JudgeBound is how long one judgement may take, waiting for another included: under the bus's 30 seconds
// a tool call is answered in, so the caller hears this module's refusal rather than a timeout.
JudgeBound = 25 * time.Second
// MostQuestion is the largest question taken.
MostQuestion = 64 << 10
// MostSchema is the largest answer schema taken.
MostSchema = 16 << 10
// JudgeProgram is the agent's program as the module's package installs it.
JudgeProgram = "/usr/bin/claude"
)
// JudgeModels are the model aliases a judgement may name; the first is the default.
var JudgeModels = []string{"sonnet", "haiku", "opus"}
// JudgeAsk is one question.
type JudgeAsk struct {
Question string
Schema map[string]any
Model string
}
// Judged is one answer.
type Judged struct {
Answer json.RawMessage `json:"answer"`
Model string `json:"model"`
Node string `json:"node"`
Seconds float64 `json:"seconds"`
}
// Judge runs judgements on this node, one at a time.
type Judge struct {
P Paths
// Licensed says whether the agent's account holds a licence now.
Licensed func() bool
// Exec runs the command line with the question on its input and answers what it printed.
Exec func(ctx context.Context, argv []string, stdin []byte) ([]byte, error)
Now func() time.Time
// Bound is how long one judgement may take; JudgeBound unless a test says less.
Bound time.Duration
turn chan struct{}
}
// NewJudge is the judge of this node.
func NewJudge(p Paths) *Judge {
return &Judge{P: p, Licensed: func() bool { return holdsAnyLicence(p) }, Exec: execJudgement, Now: time.Now,
turn: make(chan struct{}, 1)}
}
// Argv is the command line of one judgement: the agent in print mode with no tools, in safe mode, keeping no
// session, as the account agents run as here — through sudo when that is not the account this runs as.
func (j *Judge) Argv(schema, model string) []string {
argv := []string{JudgeProgram, "-p", "--output-format", "json", "--json-schema", schema, "--model", model,
"--tools", "", "--safe-mode", "--no-session-persistence"}
if j.P.Confined() {
argv = append([]string{"sudo", "-n", "-H", "-u", j.P.Agent, "--"}, argv...)
}
return argv
}
// Judge answers one question, or refuses in words.
func (j *Judge) Judge(a JudgeAsk) (*Judged, error) {
question := strings.TrimSpace(a.Question)
if question == "" {
return nil, errors.New("claude_code_judge needs a question")
}
if len(question) > MostQuestion {
return nil, fmt.Errorf("the question is %d bytes; a judgement takes at most %d (64 KiB)", len(question), MostQuestion)
}
if a.Schema == nil {
return nil, errors.New("claude_code_judge needs schema: the JSON schema of the answer, an object")
}
if t, _ := a.Schema["type"].(string); t != "object" {
return nil, errors.New("the answer's schema is an object's: its type is \"object\"")
}
schema, err := json.Marshal(a.Schema)
if err != nil || len(schema) > MostSchema {
return nil, fmt.Errorf("the answer's schema is not JSON of at most %d bytes", MostSchema)
}
model := strings.TrimSpace(a.Model)
if model == "" {
model = JudgeModels[0]
}
known := false
for _, m := range JudgeModels {
known = known || m == model
}
if !known {
return nil, fmt.Errorf("model %q is not one of %s", model, strings.Join(JudgeModels, ", "))
}
if !j.Licensed() {
return nil, fmt.Errorf("the agent on %s holds no licence, so nothing can judge here: the licence manager's "+
"bindings say which node holds which", j.P.Node)
}
start := j.Now()
bound := j.Bound
if bound <= 0 {
bound = JudgeBound
}
ctx, cancel := context.WithTimeout(context.Background(), bound)
defer cancel()
select {
case j.turn <- struct{}{}:
defer func() { <-j.turn }()
case <-ctx.Done():
return nil, fmt.Errorf("another judgement held %s's agent for all of %s; ask again", j.P.Node, bound)
}
out, err := j.Exec(ctx, j.Argv(string(schema), model), []byte(question))
if ctx.Err() != nil {
return nil, fmt.Errorf("the agent on %s did not answer within %s; the judgement was ended", j.P.Node, bound)
}
if err != nil {
return nil, fmt.Errorf("the agent on %s could not judge: %v", j.P.Node, err)
}
answer, err := answerOf(out)
if err != nil {
return nil, fmt.Errorf("the agent on %s answered, and not in the shape asked: %v", j.P.Node, err)
}
return &Judged{Answer: answer, Model: model, Node: j.P.Node, Seconds: j.Now().Sub(start).Round(100 * time.Millisecond).Seconds()}, nil
}
// answerOf reads print mode's result: the structured answer, or why there is none.
func answerOf(out []byte) (json.RawMessage, error) {
var r struct {
IsError bool `json:"is_error"`
Subtype string `json:"subtype"`
Result string `json:"result"`
Structured json.RawMessage `json:"structured_output"`
}
if err := json.Unmarshal(bytes.TrimSpace(out), &r); err != nil {
return nil, fmt.Errorf("its output is not print mode's result (%v)", err)
}
if r.IsError {
why := r.Result
if len(why) > 300 {
why = why[:300]
}
return nil, fmt.Errorf("it ended in an error (%s): %s", r.Subtype, why)
}
if len(r.Structured) == 0 || string(r.Structured) == "null" {
return nil, errors.New("it gave no structured answer")
}
var obj map[string]any
if err := json.Unmarshal(r.Structured, &obj); err != nil {
return nil, errors.New("its answer is not an object")
}
return r.Structured, nil
}
// execJudgement runs one judgement in an empty directory, ended when the context is.
func execJudgement(ctx context.Context, argv []string, stdin []byte) ([]byte, error) {
dir, err := os.MkdirTemp("", "claude-code-judge-")
if err != nil {
return nil, err
}
defer os.RemoveAll(dir)
// The agent's account must be able to stand in it.
_ = os.Chmod(dir, 0o755)
cmd := exec.CommandContext(ctx, argv[0], argv[1:]...)
cmd.Dir = dir
cmd.Stdin = bytes.NewReader(stdin)
cmd.WaitDelay = 2 * time.Second
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
why := strings.TrimSpace(stderr.String())
if len(why) > 300 {
why = why[:300]
}
if why == "" {
// Print mode says an error as its result on stdout.
if a, aerr := answerOf(out); aerr != nil && a == nil && len(out) > 0 {
why = aerr.Error()
}
}
return nil, fmt.Errorf("%v: %s", err, why)
}
return out, nil
}
// judgeTool is the tool.
func judgeTool(j *Judge) stdio.Tool {
return stdio.Tool{Name: "claude_code_judge",
Description: "One judgement by this node's agent, for a module that needs a question only a language model " +
"can answer (novox/hq ADR 0278): give the question in plain words and the JSON schema of the answer (an " +
"object); optionally the model (" + strings.Join(JudgeModels, ", ") + "; default " + JudgeModels[0] + "). The agent " +
"answers as the account agents run as here, under the licence this node is bound to, with no tools, " +
"no tool servers, no instructions and no session kept — it can only answer — within 25 seconds or " +
"not at all. One at a time on a node. Refused: an empty or larger than 64 KiB question, a schema that " +
"is not an object, an unknown model, a node holding no licence. The question is never logged.",
Input: map[string]any{
"question": str("the question, with everything the answer needs"),
"schema": map[string]any{"type": "object", "description": "the JSON schema the answer follows"},
"model": map[string]any{"type": "string", "enum": JudgeModels, "description": "the model (default " + JudgeModels[0] + ")"},
},
Run: func(a map[string]any) (any, error) {
for k := range a {
if k != "question" && k != "schema" && k != "model" {
return nil, fmt.Errorf("claude_code_judge takes question, schema and model; not %s", k)
}
}
var ask JudgeAsk
var ok bool
if ask.Question, ok = a["question"].(string); !ok {
return nil, errors.New("question is text")
}
if v, given := a["schema"]; given {
if ask.Schema, ok = v.(map[string]any); !ok {
return nil, errors.New("schema is a JSON schema: an object")
}
}
if v, given := a["model"]; given && v != nil {
if ask.Model, ok = v.(string); !ok {
return nil, errors.New("model is text")
}
}
return j.Judge(ask)
}}
}
@@ -0,0 +1,168 @@
package main
import (
"context"
"encoding/json"
"errors"
"slices"
"strings"
"testing"
"time"
)
func testJudge(confined bool, licensed bool, exec func(ctx context.Context, argv []string, stdin []byte) ([]byte, error)) *Judge {
p := Paths{Node: "control", Operator: "op", Agent: "op"}
if confined {
p.Agent = "nox"
}
return &Judge{P: p, Licensed: func() bool { return licensed }, Exec: exec, Now: time.Now, turn: make(chan struct{}, 1)}
}
var verdictSchema = map[string]any{"type": "object", "properties": map[string]any{"verdict": map[string]any{"type": "string"}}}
// The session can do nothing but answer: built-in tools off, safe mode, no session kept, as the agent's account.
func TestAJudgementRunsTheAgentWithNoToolsAsItsAccount(t *testing.T) {
var got []string
var input string
j := testJudge(true, true, func(_ context.Context, argv []string, stdin []byte) ([]byte, error) {
got, input = argv, string(stdin)
return []byte(`{"is_error":false,"subtype":"success","structured_output":{"verdict":"none"}}`), nil
})
out, err := j.Judge(JudgeAsk{Question: "is 1 the same as 2?", Schema: verdictSchema})
if err != nil {
t.Fatal(err)
}
if string(out.Answer) != `{"verdict":"none"}` || out.Model != "sonnet" || out.Node != "control" {
t.Fatalf("%+v", out)
}
if input != "is 1 the same as 2?" {
t.Fatalf("the question goes on the input, never the command line: %q", input)
}
line := strings.Join(got, " ")
if !strings.HasPrefix(line, "sudo -n -H -u nox -- "+JudgeProgram+" -p ") {
t.Fatalf("not run as the agent's account: %s", line)
}
i := slices.Index(got, "--tools")
if i < 0 || got[i+1] != "" {
t.Fatalf("built-in tools are not off: %q", got)
}
for _, want := range []string{"--safe-mode", "--no-session-persistence", "--json-schema"} {
if !slices.Contains(got, want) {
t.Errorf("%s missing: %q", want, got)
}
}
for _, a := range got {
if strings.Contains(a, "is 1 the same") {
t.Fatal("the question is on the command line")
}
}
// Where agents run as the operator's account, no sudo.
j = testJudge(false, true, func(_ context.Context, argv []string, _ []byte) ([]byte, error) {
got = argv
return []byte(`{"structured_output":{"verdict":"none"}}`), nil
})
if _, err := j.Judge(JudgeAsk{Question: "q", Schema: verdictSchema, Model: "haiku"}); err != nil {
t.Fatal(err)
}
if got[0] != JudgeProgram || !slices.Contains(got, "haiku") {
t.Fatalf("%q", got)
}
}
func TestAJudgementRefusesWhatItCannotAnswer(t *testing.T) {
ran := false
ok := func(context.Context, []string, []byte) ([]byte, error) {
ran = true
return []byte(`{"structured_output":{}}`), nil
}
cases := map[string]struct {
j *Judge
ask JudgeAsk
say string
}{
"empty": {testJudge(true, true, ok), JudgeAsk{Question: " ", Schema: verdictSchema}, "needs a question"},
"too large": {testJudge(true, true, ok), JudgeAsk{Question: strings.Repeat("x", MostQuestion+1), Schema: verdictSchema}, "64 KiB"},
"no schema": {testJudge(true, true, ok), JudgeAsk{Question: "q"}, "needs schema"},
"not object": {testJudge(true, true, ok), JudgeAsk{Question: "q", Schema: map[string]any{"type": "string"}}, "object"},
"model": {testJudge(true, true, ok), JudgeAsk{Question: "q", Schema: verdictSchema, Model: "gpt"}, "not one of"},
"no licence": {testJudge(true, false, ok), JudgeAsk{Question: "q", Schema: verdictSchema}, "holds no licence"},
}
for name, c := range cases {
_, err := c.j.Judge(c.ask)
if err == nil || !strings.Contains(err.Error(), c.say) {
t.Errorf("%s: %v", name, err)
}
}
if ran {
t.Fatal("a refused judgement ran the agent")
}
}
func TestAJudgementThatFailsSaysWhy(t *testing.T) {
answers := map[string][]byte{
"an error": []byte(`{"is_error":true,"subtype":"error_during_execution","result":"rate limited"}`),
"no structure": []byte(`{"is_error":false,"result":"it is a duplicate"}`),
"not an object": []byte(`{"structured_output":[1,2]}`),
"not json": []byte(`Invalid API key`),
}
for name, out := range answers {
j := testJudge(true, true, func(context.Context, []string, []byte) ([]byte, error) { return out, nil })
if _, err := j.Judge(JudgeAsk{Question: "q", Schema: verdictSchema}); err == nil || !strings.Contains(err.Error(), "not in the shape") {
t.Errorf("%s: %v", name, err)
}
}
j := testJudge(true, true, func(context.Context, []string, []byte) ([]byte, error) {
return nil, errors.New("exit status 1: sudo: a password is required")
})
if _, err := j.Judge(JudgeAsk{Question: "q", Schema: verdictSchema}); err == nil || !strings.Contains(err.Error(), "could not judge") {
t.Errorf("%v", err)
}
}
// One at a time on a node, and never past the bound: a judgement still waiting at the bound is refused.
func TestAJudgementWaitsItsTurnWithinTheBound(t *testing.T) {
j := testJudge(true, true, func(ctx context.Context, _ []string, _ []byte) ([]byte, error) {
<-ctx.Done()
return nil, ctx.Err()
})
j.Bound = 300 * time.Millisecond
j.turn <- struct{}{} // another judgement holds the agent
start := time.Now()
_, err := j.Judge(JudgeAsk{Question: "q", Schema: verdictSchema})
if err == nil || !strings.Contains(err.Error(), "another judgement") {
t.Fatalf("%v", err)
}
if time.Since(start) > 2*time.Second {
t.Fatal("waited past the bound")
}
}
func TestAJudgementPastTheBoundIsEnded(t *testing.T) {
j := testJudge(true, true, func(ctx context.Context, _ []string, _ []byte) ([]byte, error) {
<-ctx.Done()
return nil, ctx.Err()
})
j.Bound = 200 * time.Millisecond
if _, err := j.Judge(JudgeAsk{Question: "q", Schema: verdictSchema}); err == nil || !strings.Contains(err.Error(), "did not answer within") {
t.Fatalf("%v", err)
}
}
func TestTheToolTakesOnlyItsArguments(t *testing.T) {
j := testJudge(true, true, func(context.Context, []string, []byte) ([]byte, error) {
return []byte(`{"structured_output":{"verdict":"none"}}`), nil
})
tool := judgeTool(j)
if _, err := tool.Run(map[string]any{"question": "q", "schema": verdictSchema, "by": "x"}); err == nil {
t.Fatal("an unknown argument was taken")
}
out, err := tool.Run(map[string]any{"question": "q", "schema": verdictSchema})
if err != nil {
t.Fatal(err)
}
raw, _ := json.Marshal(out)
if !strings.Contains(string(raw), `"answer":{"verdict":"none"}`) {
t.Fatalf("%s", raw)
}
}
@@ -355,6 +355,7 @@ func tools(p Paths, servers ServerState, view *ServerView, config ConfigState, c
Description: "Read-only: everything registered through this module that a Claude Code session runs or obeys — the tool servers, the hooks, the settings and the skills, subagents, commands, output styles and instructions — by key, with who registered it, when, and whether it is still rendered (novox/hq issue 340), beside what the operator set for this node at the controller's terminal (managed_settings, mcp_servers). `by` is the node whose tool runner served the call, never the caller.",
Run: func(map[string]any) (any, error) { return Registrations(p, servers, config) }})
out = append(out, proposalTools(p)...)
out = append(out, judgeTool(NewJudge(p)))
return append(out, configTools(p, config, configView)...)
}