Files
archie/internal/tools/builtin/pathutil_test.go
T
sam e03cef17c9 feat(tools): lift tau's file and shell tools into the tool registry
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.
2026-07-29 15:46:21 +10:00

132 lines
3.8 KiB
Go

package builtin
import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
// withGoModCache points the module-cache lookup at a temp dir for the duration
// of a test, so these tests never depend on the host's real GOMODCACHE.
func withGoModCache(t *testing.T, dir string) {
t.Helper()
prev := goModCacheDir
goModCacheDir = func() string { return dir }
t.Cleanup(func() { goModCacheDir = prev })
}
func TestReadAllowsGoModCache(t *testing.T) {
cwd := t.TempDir()
cache := t.TempDir()
withGoModCache(t, cache)
dep := filepath.Join(cache, "example.com", "dep@v1.0.0")
if err := os.MkdirAll(dep, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dep, "dep.go"), []byte("package dep\n"), 0o644); err != nil {
t.Fatal(err)
}
res := execRead(t, cwd, `{"path":`+quote(filepath.Join(dep, "dep.go"))+`}`)
if res.IsError {
t.Fatalf("expected read of a module-cache file to succeed, got: %s", res.Content)
}
if !strings.Contains(res.Content, "package dep") {
t.Fatalf("unexpected content: %s", res.Content)
}
}
func TestGrepAllowsGoModCache(t *testing.T) {
cwd := t.TempDir()
cache := t.TempDir()
withGoModCache(t, cache)
if err := os.WriteFile(filepath.Join(cache, "dep.go"), []byte("package dep\nfunc Needle() {}\n"), 0o644); err != nil {
t.Fatal(err)
}
tool := NewGrepTool(cwd, nil)
res, err := tool.Execute(context.Background(), json.RawMessage(
`{"pattern":"Needle","path":`+quote(cache)+`}`), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if res.IsError {
t.Fatalf("expected grep of the module cache to succeed, got: %s", res.Content)
}
if !strings.Contains(res.Content, "Needle") {
t.Fatalf("unexpected content: %s", res.Content)
}
}
// The module cache is a read-only escape hatch. Mutating tools must still be
// confined to the workspace, or the agent could corrupt shared dependencies.
func TestWriteRejectsGoModCache(t *testing.T) {
cwd := t.TempDir()
cache := t.TempDir()
withGoModCache(t, cache)
tool := NewWriteTool(cwd, nil, NewReadTracker())
res, err := tool.Execute(context.Background(), json.RawMessage(
`{"path":`+quote(filepath.Join(cache, "evil.go"))+`,"content":"package evil\n"}`), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !res.IsError || !strings.Contains(res.Content, "escapes working directory") {
t.Fatalf("expected write to the module cache to be rejected, got: %#v", res)
}
}
func TestEditRejectsGoModCache(t *testing.T) {
cwd := t.TempDir()
cache := t.TempDir()
withGoModCache(t, cache)
target := filepath.Join(cache, "dep.go")
if err := os.WriteFile(target, []byte("package dep\n"), 0o644); err != nil {
t.Fatal(err)
}
rt := NewReadTracker()
rt.MarkRead(cwd, target)
tool := NewEditTool(cwd, nil, rt)
res, err := tool.Execute(context.Background(), json.RawMessage(
`{"path":`+quote(target)+`,"edits":[{"old_text":"package dep","new_text":"package hacked"}]}`), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !res.IsError || !strings.Contains(res.Content, "escapes working directory") {
t.Fatalf("expected edit of the module cache to be rejected, got: %#v", res)
}
}
// A path in neither the workspace nor the module cache stays rejected.
func TestReadRejectsUnrelatedAbsolutePath(t *testing.T) {
cwd := t.TempDir()
cache := t.TempDir()
other := t.TempDir()
withGoModCache(t, cache)
target := filepath.Join(other, "secret.txt")
if err := os.WriteFile(target, []byte("secret\n"), 0o644); err != nil {
t.Fatal(err)
}
res := execRead(t, cwd, `{"path":`+quote(target)+`}`)
if !res.IsError || !strings.Contains(res.Content, "escapes working directory") {
t.Fatalf("expected an unrelated absolute path to be rejected, got: %#v", res)
}
}
func quote(s string) string {
b, err := json.Marshal(s)
if err != nil {
panic(err)
}
return string(b)
}