Why: tavily_search/web_search existed as tools but were never registered on the default execution path (parity-matrix #5 leftover), and the kernel had no URL fetch capability at all. Evidence: go vet ./... clean; go test ./... all ok. Fetch cases cover private/loopback/link-local rejection (127/8, 10/8, 192.168/16, 169.254 metadata endpoint, ::1), file:// scheme rejection, DNS resolution landing on private space, per-hop redirect validation rejecting a redirect to loopback, allowed-host policy, HTML stripping (script/style removed, entities decoded), content-type gating, stub end-to-end fetch, and absence without FetchConfig (IP normalization fix included: IPv4-mapped ::ffff:0:0/96 must not swallow all IPv4). Adapter tests assert Options.Search registers both search tools and passes queries to the provider, and that neither registers when unconfigured. Impact: native.Options gains Search and Fetch; both tool families are opt-in by the composition root (fail-closed), closing parity-matrix #5 wiring. Refs: docs/parity-matrix.md #5; product settings tools.tavily.* injection point.
166 lines
5.4 KiB
Go
166 lines
5.4 KiB
Go
package native
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
osexec "os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"agent-native/exec"
|
|
"agent-native/runtime/llm"
|
|
"agent-native/runtime/tools"
|
|
)
|
|
|
|
func TestBackendExecToolsWiredWithOptionsAndRequestEnv(t *testing.T) {
|
|
bash, err := osexec.LookPath("bash")
|
|
if err != nil {
|
|
t.Fatalf("bash not available: %v", err)
|
|
}
|
|
dir := t.TempDir()
|
|
b := New(Options{
|
|
Model: "fake",
|
|
Stream: script(
|
|
llm.Message{Role: llm.RoleAssistant, StopReason: llm.StopToolCalls, ToolCalls: []llm.ToolCall{{
|
|
ID: "c1", Name: "run_command",
|
|
Arguments: `{"command":"echo role=$ROLE_FLAG step=$STEP_FLAG > exec_out.txt"}`,
|
|
}}},
|
|
llm.Message{Role: llm.RoleAssistant, StopReason: llm.StopToolCalls, ToolCalls: []llm.ToolCall{{
|
|
ID: "c2", Name: "write_file",
|
|
Arguments: `{"path":"output.md","content":"done"}`,
|
|
}}},
|
|
llm.Message{Role: llm.RoleAssistant, Content: "ok", StopReason: llm.StopEnd},
|
|
),
|
|
Exec: &tools.ExecConfig{Bash: bash, Env: []string{"PATH=/usr/bin:/bin", "ROLE_FLAG=backend"}},
|
|
})
|
|
res, err := b.Execute(context.Background(), exec.Request{
|
|
WorkDir: dir, Prompt: "run", Targets: []string{"output.md"},
|
|
Env: map[string]string{"STEP_FLAG": "request"},
|
|
})
|
|
if err != nil || !res.Success {
|
|
t.Fatalf("execute: err=%v res=%+v logs=%s", err, res, res.Logs)
|
|
}
|
|
data, err := os.ReadFile(filepath.Join(dir, "exec_out.txt"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(data), "role=backend") || !strings.Contains(string(data), "step=request") {
|
|
t.Fatalf("env merge wrong: %q", data)
|
|
}
|
|
}
|
|
|
|
func TestBackendExecToolsAbsentWithoutOptions(t *testing.T) {
|
|
stream, c := captureStream()
|
|
b := New(Options{Model: "fake", Stream: stream})
|
|
res, err := b.Execute(context.Background(), exec.Request{
|
|
WorkDir: t.TempDir(), Prompt: "x", Targets: []string{"output.md"},
|
|
ToolProfile: exec.ToolProfileReasoning,
|
|
})
|
|
if err != nil || !res.Success {
|
|
t.Fatalf("execute: err=%v res=%+v", err, res)
|
|
}
|
|
// reasoning 档本无工具;改用 workspace 档断言 decls 不含执行工具。
|
|
stream2, c2 := captureStream()
|
|
b2 := New(Options{Model: "fake", Stream: stream2})
|
|
_, _ = b2.Execute(context.Background(), exec.Request{
|
|
WorkDir: t.TempDir(), Prompt: "x", Targets: []string{"output.md"},
|
|
ToolProfile: exec.ToolProfileWorkspace,
|
|
})
|
|
for _, names := range c2.toolDecls {
|
|
for _, n := range names {
|
|
if n == "run_command" || n == "run_python" {
|
|
t.Fatalf("exec tools must not register without Options.Exec, got %v", names)
|
|
}
|
|
}
|
|
}
|
|
_ = c
|
|
}
|
|
|
|
func TestBackendAllowlistCanDisableExecTools(t *testing.T) {
|
|
bash, _ := osexec.LookPath("bash")
|
|
stream, c := captureStream()
|
|
b := New(Options{
|
|
Model: "fake", Stream: stream,
|
|
Exec: &tools.ExecConfig{Bash: bash, Env: []string{"PATH=/usr/bin:/bin"}},
|
|
})
|
|
res, err := b.Execute(context.Background(), exec.Request{
|
|
WorkDir: t.TempDir(), Prompt: "x", Targets: []string{"output.md"},
|
|
ToolAllowlist: []string{"read_file", "write_file", "edit_file", "list_dir", "grep", "glob", "multi_edit"},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.Success {
|
|
t.Fatalf("write-only allowlist without write? (unexpected) res=%+v", res)
|
|
}
|
|
for _, names := range c.toolDecls {
|
|
for _, n := range names {
|
|
if n == "run_command" {
|
|
t.Fatalf("allowlist must be able to strip run_command, got %v", names)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// fakeSearchProvider 与 tools 包测试同构的最小搜索替身。
|
|
type fakeSearchProvider struct{ query string }
|
|
|
|
func (f *fakeSearchProvider) Search(_ context.Context, req tools.SearchRequest) (tools.SearchResponse, error) {
|
|
f.query = req.Query
|
|
return tools.SearchResponse{Answer: "ans", Results: []tools.SearchResult{{Title: "T", Content: "C", Score: 0.9}}}, nil
|
|
}
|
|
|
|
func TestBackendSearchToolsWiredViaOptions(t *testing.T) {
|
|
provider := &fakeSearchProvider{}
|
|
stream, c := captureStream(
|
|
llm.Message{Role: llm.RoleAssistant, StopReason: llm.StopToolCalls, ToolCalls: []llm.ToolCall{{
|
|
ID: "c1", Name: "web_search", Arguments: `{"query":"golang generics"}`,
|
|
}}},
|
|
llm.Message{Role: llm.RoleAssistant, StopReason: llm.StopToolCalls, ToolCalls: []llm.ToolCall{{
|
|
ID: "c2", Name: "write_file",
|
|
Arguments: `{"path":"output.md","content":"done"}`,
|
|
}}},
|
|
llm.Message{Role: llm.RoleAssistant, Content: "ok", StopReason: llm.StopEnd},
|
|
)
|
|
b := New(Options{Model: "fake", Stream: stream, Search: provider})
|
|
dir := t.TempDir()
|
|
res, err := b.Execute(context.Background(), exec.Request{
|
|
WorkDir: dir, Prompt: "search", Targets: []string{"output.md"},
|
|
})
|
|
if err != nil || !res.Success {
|
|
t.Fatalf("execute: err=%v res=%+v logs=%s", err, res, res.Logs)
|
|
}
|
|
if provider.query != "golang generics" {
|
|
t.Fatalf("provider should receive the query, got %q", provider.query)
|
|
}
|
|
// 搜索工具确实出现在 decls 里。
|
|
found := false
|
|
for _, names := range c.toolDecls {
|
|
for _, n := range names {
|
|
if n == "web_search" || n == "tavily_search" {
|
|
found = true
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("search tools must be declared when Options.Search is set")
|
|
}
|
|
}
|
|
|
|
func TestBackendSearchToolsAbsentWithoutOptions(t *testing.T) {
|
|
stream, c := captureStream()
|
|
b := New(Options{Model: "fake", Stream: stream})
|
|
_, _ = b.Execute(context.Background(), exec.Request{
|
|
WorkDir: t.TempDir(), Prompt: "x", Targets: []string{"output.md"},
|
|
ToolProfile: exec.ToolProfileWorkspace,
|
|
})
|
|
for _, names := range c.toolDecls {
|
|
for _, n := range names {
|
|
if n == "web_search" || n == "tavily_search" {
|
|
t.Fatalf("search tools must not register without Options.Search, got %v", names)
|
|
}
|
|
}
|
|
}
|
|
}
|