把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到 third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或 改写 tag 都不再影响本仓库构建。 - 已验证 GOPROXY=off 下可完整编译并打出发行包 - go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希) - 升级方法与授权注意事项见 third_party/README.md
132 lines
4.5 KiB
Go
132 lines
4.5 KiB
Go
package harness
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/norma/llm"
|
|
"github.com/Autumn-27/norma/permission"
|
|
"github.com/Autumn-27/norma/tool"
|
|
)
|
|
|
|
// A tool's InputSchema does two unrelated jobs: it tells the model what to send,
|
|
// and it decides what gets through. Most tools want both. A tool that repairs
|
|
// malformed arguments itself wants only the first — the gate would reject the
|
|
// very calls it exists to repair, because a truncated or fence-wrapped call is
|
|
// not valid JSON.
|
|
//
|
|
// These tests cover both sides of the opt-out.
|
|
|
|
// recordingSpec builds a tool that remembers the bytes it was handed.
|
|
func recordingSpec(name string, raw bool, got *json.RawMessage) tool.CoreTool {
|
|
return tool.Build(tool.Spec{
|
|
Name: name,
|
|
Schema: map[string]any{"type": "object", "required": []any{"content"}},
|
|
Permissions: func(context.Context, json.RawMessage, permission.Context) permission.Decision {
|
|
return permission.Decision{Behavior: permission.Allow}
|
|
},
|
|
RawInput: raw,
|
|
Run: func(_ context.Context, in json.RawMessage, _ *tool.ToolContext) (tool.Result, error) {
|
|
*got = in
|
|
return tool.Result{Content: []llm.ContentBlock{llm.TextBlock("ran")}}, nil
|
|
},
|
|
})
|
|
}
|
|
|
|
func execWith(t *testing.T, ct tool.CoreTool, input string) llm.ContentBlock {
|
|
t.Helper()
|
|
l := &loop{ctx: context.Background(), in: QueryInput{
|
|
Tools: tool.NewRegistry(ct),
|
|
PermissionMode: permission.ModeBypass,
|
|
}}
|
|
res, _ := l.execOne(llm.ContentBlock{
|
|
Type: llm.BlockToolUse, ID: "toolu_1", Name: ct.Name(), Input: json.RawMessage(input),
|
|
}, nil)
|
|
return res
|
|
}
|
|
|
|
// The default: invalid JSON is answered with a validation error and Run is never
|
|
// reached. This is the behaviour every other tool relies on.
|
|
func TestSchemaGateStillRejectsForOrdinaryTools(t *testing.T) {
|
|
var got json.RawMessage
|
|
res := execWith(t, recordingSpec("Ordinary", false, &got), `{"content":[{"a`)
|
|
|
|
if !res.IsError {
|
|
t.Fatal("malformed input produced a success result for a tool that did not opt out")
|
|
}
|
|
if !strings.Contains(res.Content[0].Text, "invalid tool input") {
|
|
t.Fatalf("result = %q, want the schema validation error", res.Content[0].Text)
|
|
}
|
|
if got != nil {
|
|
t.Fatalf("Run was reached with %q despite failing validation", got)
|
|
}
|
|
}
|
|
|
|
// The opt-out: the same bytes reach Run untouched, so the tool can repair them.
|
|
func TestRawInputToolReceivesMalformedArgumentsVerbatim(t *testing.T) {
|
|
cases := map[string]string{
|
|
"truncated mid-object": `{"content":[{"startId":"m00001","end`,
|
|
"fenced in markdown": "```json\n{\"content\":[]}\n```",
|
|
"trailing comma": `{"content":[{"a":1,},],}`,
|
|
"not an object": `"just a string"`,
|
|
"missing required": `{"topic":"t"}`,
|
|
"empty": ``,
|
|
}
|
|
for name, input := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
var got json.RawMessage
|
|
res := execWith(t, recordingSpec("Lenient", true, &got), input)
|
|
|
|
if res.IsError {
|
|
t.Fatalf("the harness rejected input the tool opted in to handle: %q", res.Content[0].Text)
|
|
}
|
|
if string(got) != input {
|
|
t.Fatalf("Run received %q, want the bytes the model sent, %q", got, input)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Opting out of validation must not opt out of anything else. Permission is the
|
|
// one that matters: a tool that skips the schema check still answers to the same
|
|
// permission decision.
|
|
func TestRawInputToolStillHonoursPermissions(t *testing.T) {
|
|
var got json.RawMessage
|
|
denied := tool.Build(tool.Spec{
|
|
Name: "Denied",
|
|
Schema: map[string]any{"type": "object"},
|
|
RawInput: true,
|
|
Permissions: func(context.Context, json.RawMessage, permission.Context) permission.Decision {
|
|
return permission.Decision{Behavior: permission.Deny, Message: "nope"}
|
|
},
|
|
Run: func(_ context.Context, in json.RawMessage, _ *tool.ToolContext) (tool.Result, error) {
|
|
got = in
|
|
return tool.Result{}, nil
|
|
},
|
|
})
|
|
res := execWith(t, denied, `{"garbage`)
|
|
|
|
if !res.IsError {
|
|
t.Fatal("a denied raw-input tool produced a success result")
|
|
}
|
|
if got != nil {
|
|
t.Fatal("a denied raw-input tool was executed anyway")
|
|
}
|
|
}
|
|
|
|
// A tool that does not implement the optional interface at all — the shape every
|
|
// host-supplied CoreTool has — must keep being validated.
|
|
type bareTool struct{ tool.CoreTool }
|
|
|
|
func TestToolsWithoutTheOptionalInterfaceAreValidated(t *testing.T) {
|
|
var got json.RawMessage
|
|
res := execWith(t, bareTool{recordingSpec("Bare", true, &got)}, `{"content":[{"a`)
|
|
|
|
if !res.IsError {
|
|
t.Fatal("a CoreTool that does not implement AcceptsRawInput skipped validation; " +
|
|
"the assertion is matching something it should not")
|
|
}
|
|
}
|