The chat agent had two tools -- memory_edit and skill_activate -- so it
could not read a file, search a tree or run a command. ai-sdk's runtime
already drives a tool loop for it (MaxSteps 8, toolExecute wired as the
executor), so the loop was never the missing piece: the registry was empty
of anything useful.
Lifted from tau internal/agent/tools at commit f5289ea, per the recorded
copy-and-mutate strategy: every file carries a provenance header naming its
tau source path, the commit, and the mutations made, so refreshing from
upstream is a diff rather than archaeology.
internal/tools/builtin/ the lifted tools, ~3.1k lines
internal/tools/provider/builtin/ the adapter to tools.ToolEntry
Six tools: read, write, edit, shell, grep, find. Tau's own tests came with
them and pass unmodified, which is the evidence that the lift is faithful.
Deliberately not lifted:
- agent.go (1214 lines): subagent spawning, and the only real coupling in
the package -- 10 tau packages including chat, sessions, store and
registry. Nothing the chat agent needs to do work.
- skill.go: depends on tau's skills package; archie has internal/skill and
already exposes skill_activate.
- docs.go: serves tau's own embedded documentation via tau/docs.
Not lifting the coordinator either. It is tau's agent loop (6.4k lines over
ten packages) and archie already has one in the ai-sdk runtime; porting it
would leave two loops competing. Its cancellation and tool events are worth
having, but they are a few hundred lines built against the existing path
rather than six thousand lifted onto a second one.
The adapter bridges two shape differences: archie hands handlers decoded
arguments where tau's executor takes raw JSON, and tau reports tool-level
failure in Result.IsError rather than as an error -- surfaced as a real
error so the runtime and the guardrail engine both see it. Tools are
classified idempotent or mutating for guardrails and budgets; that is
categorisation, not a jail.
chat.workspace roots the tools and defaults to empty, which registers none
of them. They read, write and execute, so the directory has to be a
deliberate choice rather than whatever the daemon started in -- in the
container that is /.
Verified: go build, go vet, gofmt clean; full suite passes.
149 lines
4.0 KiB
Go
149 lines
4.0 KiB
Go
package builtin
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestFindFallback(t *testing.T) {
|
|
// Create a temporary directory tree.
|
|
tmp := t.TempDir()
|
|
createTestFile(t, tmp, "foo.go", "package main")
|
|
createTestFile(t, tmp, "bar.go", "package bar")
|
|
createTestFile(t, tmp, "README.md", "# readme")
|
|
createTestFile(t, tmp, "subdir/baz.go", "package baz")
|
|
createTestFile(t, tmp, "subdir/nested/deep.txt", "deep")
|
|
createTestFile(t, tmp, "vendor/mod.go", "package mod")
|
|
os.MkdirAll(filepath.Join(tmp, "hiddendir"), 0o755)
|
|
createTestFile(t, tmp, "hiddendir/visible.txt", "text")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
params FindParams
|
|
want []string
|
|
wantEmpty bool
|
|
}{
|
|
{
|
|
name: "glob pattern *.go (matches all depths)",
|
|
params: FindParams{Path: tmp, Pattern: "*.go", Type: "file"},
|
|
want: []string{"bar.go", "foo.go", "subdir/baz.go", "vendor/mod.go"},
|
|
},
|
|
{
|
|
name: "match directories (nested shown)",
|
|
params: FindParams{Path: tmp, Type: "directory"},
|
|
want: []string{"hiddendir", "subdir", "subdir/nested", "vendor"},
|
|
},
|
|
{
|
|
name: "max depth 1",
|
|
params: FindParams{Path: tmp, Type: "file", MaxDepth: 1},
|
|
want: []string{"bar.go", "foo.go", "README.md"},
|
|
},
|
|
{
|
|
name: "max depth 0 unlimited",
|
|
params: FindParams{Path: tmp, Type: "file", MaxDepth: 0},
|
|
want: []string{
|
|
"bar.go", "foo.go", "README.md",
|
|
"hiddendir/visible.txt", "subdir/baz.go",
|
|
"subdir/nested/deep.txt", "vendor/mod.go",
|
|
},
|
|
},
|
|
{
|
|
name: "exclude vendor",
|
|
params: FindParams{Path: tmp, Type: "file", Exclude: "vendor"},
|
|
want: []string{
|
|
"bar.go", "foo.go", "README.md",
|
|
"hiddendir/visible.txt", "subdir/baz.go",
|
|
"subdir/nested/deep.txt",
|
|
},
|
|
},
|
|
{
|
|
name: "exclude by glob",
|
|
params: FindParams{Path: tmp, Type: "file", Exclude: "*.md"},
|
|
want: []string{
|
|
"bar.go", "foo.go",
|
|
"hiddendir/visible.txt", "subdir/baz.go",
|
|
"subdir/nested/deep.txt", "vendor/mod.go",
|
|
},
|
|
},
|
|
{
|
|
name: "no matches",
|
|
params: FindParams{Path: tmp, Pattern: "nonexistent*", Type: "file"},
|
|
wantEmpty: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
res, err := runFindGoFallback(ctx, tmp, tmp, tc.params)
|
|
if err != nil {
|
|
t.Fatalf("runFindGoFallback returned error: %v", err)
|
|
}
|
|
if tc.wantEmpty {
|
|
if res.Content != "no matches found" {
|
|
t.Fatalf("expected empty, got:\n%s", res.Content)
|
|
}
|
|
return
|
|
}
|
|
gotLines := strings.Split(strings.TrimSpace(res.Content), "\n")
|
|
if len(gotLines) == 1 && gotLines[0] == "" {
|
|
gotLines = []string{}
|
|
}
|
|
sort.Strings(gotLines)
|
|
sort.Strings(tc.want)
|
|
if len(gotLines) != len(tc.want) {
|
|
t.Fatalf("got %d results, want %d\ngot: %v\nwant: %v", len(gotLines), len(tc.want), gotLines, tc.want)
|
|
}
|
|
for i := range tc.want {
|
|
if gotLines[i] != tc.want[i] {
|
|
t.Fatalf("result mismatch at %d: got %q, want %q", i, gotLines[i], tc.want[i])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFindFallback_NonExistentPath(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
res, _ := runFindGoFallback(ctx, "/nonexistent/path", "/nonexistent/path", FindParams{})
|
|
if !res.IsError {
|
|
t.Fatal("expected error for non-existent path")
|
|
}
|
|
}
|
|
|
|
func TestFindFallback_NotADirectory(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
f := filepath.Join(tmp, "file.txt")
|
|
if err := os.WriteFile(f, []byte("hello"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
res, _ := runFindGoFallback(ctx, tmp, f, FindParams{})
|
|
if !res.IsError {
|
|
t.Fatal("expected error when path is a file")
|
|
}
|
|
}
|
|
|
|
func createTestFile(t *testing.T, base, relPath, content string) {
|
|
t.Helper()
|
|
full := filepath.Join(base, relPath)
|
|
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(full, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|