From c5cb1afad7e4e3e814e04e0df7e6cf52b7d1be40 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Fri, 20 Nov 2026 18:11:16 +0100 Subject: [PATCH] fix(agent): keep the record_fact harness opt-out through tool wrappers record_fact opts out of the harness schema check (norma Spec.RawInput) and validates its own input. The harness finds the opt-out with a type assertion on the tool it is handed, and that tool is a wrapper: catalog tools are metered (meteredTool), and guardPanic wraps every tool last (guardedTool). Both embed CoreTool, which hides AcceptsRawInput, so the opt-out was lost and a malformed call was answered by the SDK ("invalid tool input: facts: expected array") instead of by the handler. guardedTool and meteredTool now forward AcceptsRawInput, as overriddenTool already does. Test: TestFix410AcceptsRawInputSurvivesWrappers covers each wrapper and record_fact through wireTools, AugmentTools and harness.Query. --- agent/assembly.go | 8 ++ server/record_fact_rawinput_test.go | 119 ++++++++++++++++++++++++++++ server/tool_usage.go | 8 ++ 3 files changed, 135 insertions(+) create mode 100644 server/record_fact_rawinput_test.go diff --git a/agent/assembly.go b/agent/assembly.go index c0885d9..4a075e7 100644 --- a/agent/assembly.go +++ b/agent/assembly.go @@ -101,6 +101,14 @@ func guardPanic(t actool.CoreTool) actool.CoreTool { return &guardedTool{CoreToo type guardedTool struct{ actool.CoreTool } +// AcceptsRawInput forwards the wrapped tool's opt-out of the harness schema +// check, as overriddenTool does: this wrapper is the one the harness sees, and +// the embedded interface alone would hide the opt-out from its type assertion. +func (g *guardedTool) AcceptsRawInput() bool { + r, ok := g.CoreTool.(interface{ AcceptsRawInput() bool }) + return ok && r.AcceptsRawInput() +} + func (g *guardedTool) Call(ctx context.Context, in json.RawMessage, tc *actool.ToolContext) (res actool.Result, err error) { defer func() { if r := recover(); r != nil { diff --git a/server/record_fact_rawinput_test.go b/server/record_fact_rawinput_test.go new file mode 100644 index 0000000..92a643a --- /dev/null +++ b/server/record_fact_rawinput_test.go @@ -0,0 +1,119 @@ +package server + +import ( + "context" + "strings" + "testing" + + "github.com/Autumn-27/norma/harness" + "github.com/Autumn-27/norma/llm" + actool "github.com/Autumn-27/norma/tool" + + "github.com/Autumn-27/artex/agent" + "github.com/Autumn-27/artex/db" +) + +// acceptsRawInput is the assertion norma's harness makes before its schema +// check: only a tool that reports AcceptsRawInput skips it. +func acceptsRawInput(t actool.CoreTool) bool { + r, ok := t.(interface{ AcceptsRawInput() bool }) + return ok && r.AcceptsRawInput() +} + +// record_fact opts out of the harness schema check and validates its own +// input. Every wrapper between the handler and the harness has to forward +// that opt-out: an embedded CoreTool alone hides it from the harness's type +// assertion, and the stricter published schema then rejects the calls the +// handler exists to answer. +func TestFix410AcceptsRawInputSurvivesWrappers(t *testing.T) { + oldAugment, oldResolve := agent.ToolAugment(), agent.ToolResolve() + t.Cleanup(func() { + agent.SetToolAugment(oldAugment) + agent.SetToolResolve(oldResolve) + }) + ctx := context.Background() + probe := func(raw bool) actool.CoreTool { + return actool.Build(actool.Spec{Name: "fix410_probe", Schema: map[string]any{"type": "object"}, RawInput: raw}) + } + + // AugmentTools without hooks applies guardPanic and nothing else. + t.Run("guardedTool", func(t *testing.T) { + agent.SetToolAugment(nil) + agent.SetToolResolve(nil) + for _, raw := range []bool{true, false} { + out, _, cleanup := agent.AugmentTools(ctx, "worker", []actool.CoreTool{probe(raw)}) + cleanup() + if len(out) != 1 { + t.Fatalf("AugmentTools returned %d tools, want 1", len(out)) + } + if got := acceptsRawInput(out[0]); got != raw { + t.Errorf("%T: AcceptsRawInput = %v, wrapped tool reports %v", out[0], got, raw) + } + } + }) + + t.Run("meteredTool", func(t *testing.T) { + for _, raw := range []bool{true, false} { + wrapped := meterTool(probe(raw), &recordingToolUsage{}, "fix410_probe", "worker", agent.RunInfo{}) + if got := acceptsRawInput(wrapped); got != raw { + t.Errorf("%T: AcceptsRawInput = %v, wrapped tool reports %v", wrapped, got, raw) + } + } + }) + + // The chain a worker's record_fact goes through on the server: the catalog + // decorator and the usage meter (ToolResolve, wireTools), then guardPanic + // (AugmentTools), then the harness itself. + t.Run("server chain", func(t *testing.T) { + dsn, _, err := db.DSN() + if err != nil { + db.SkipPGUnavailable(t, err) + } + pg, err := db.Open(dsn) + if err != nil { + db.SkipPGUnavailable(t, err) + } + defer pg.Close() + agent.SetToolAugment(nil) + wireTools(pg, nil) + task, err := pg.CreateTaskWithOptions("fix410", "record_fact through the wrapper chain", db.TaskCreateOptions{}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = pg.DeleteTask(task.ID) }) + ts := agent.NewToolSet(pg.Exploration(task.ExplorationID), "worker") + tools, _, cleanup := agent.AugmentTools(ctx, "worker", ts.WorkerTools()) + defer cleanup() + recordFact := names(tools)["record_fact"] + if recordFact == nil { + t.Fatal("worker has no record_fact") + } + if !acceptsRawInput(recordFact) { + t.Errorf("record_fact lost the harness opt-out behind %T", recordFact) + } + + // The first call is malformed (facts is not an array): the harness + // schema check answers it with its generic error unless the tool opted + // out. The second is the legacy single fact, which the published + // schema does not describe and the handler still accepts. + p := &toolScript{results: make(chan string, 4), calls: []scriptedCall{ + {"record_fact", `{"facts":{"summary":"not an array"}}`}, + {"record_fact", `{"summary":"legacy single fact"}`}, + }} + for _, err := range harness.Query(ctx, harness.QueryInput{ + Provider: p, + Tools: actool.NewRegistry(recordFact), + Messages: []llm.Message{llm.UserText("record the facts")}, + }, harness.QueryDeps{}) { + if err != nil { + t.Fatal(err) + } + } + if refusal := p.next(t); !strings.Contains(refusal, "invalid record_fact input: facts must be an array") { + t.Errorf("malformed call: want the handler's own refusal, not the SDK schema error, got: %s", refusal) + } + if legacy := p.next(t); strings.HasPrefix(legacy, "[error]") || ts.Writes().Facts != 1 { + t.Errorf("legacy single fact not written (facts=%d): %s", ts.Writes().Facts, legacy) + } + }) +} diff --git a/server/tool_usage.go b/server/tool_usage.go index 5f9e7c8..5e69cfb 100644 --- a/server/tool_usage.go +++ b/server/tool_usage.go @@ -33,6 +33,14 @@ func meterTool(t actool.CoreTool, recorder toolUsageRecorder, toolKey, agentKey return &meteredTool{CoreTool: t, recorder: recorder, toolKey: toolKey, agentKey: agentKey, ri: ri} } +// AcceptsRawInput forwards the resolved tool's opt-out of the harness schema +// check; the embedded interface alone would hide it from the harness's type +// assertion. +func (m *meteredTool) AcceptsRawInput() bool { + r, ok := m.CoreTool.(interface{ AcceptsRawInput() bool }) + return ok && r.AcceptsRawInput() +} + func (m *meteredTool) Call(ctx context.Context, input json.RawMessage, tc *actool.ToolContext) (actool.Result, error) { if err := m.recorder.InsertToolUsage(&db.ToolUsage{ ToolKey: m.toolKey,