Files
archie/internal/tools/builtin/read_outline_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

141 lines
4.7 KiB
Go

package builtin
import (
"fmt"
"strings"
"testing"
)
// bigGoFile builds a Go file well past the outline threshold, with each
// function body padded so bodies dominate the byte count.
func bigGoFile(t *testing.T, dir, name string, funcs int) {
t.Helper()
var b strings.Builder
b.WriteString("package big\n\nimport \"fmt\"\n\n")
for i := range funcs {
fmt.Fprintf(&b, "// Doc for Fn%d.\nfunc Fn%d() {\n", i, i)
for j := range 20 {
fmt.Fprintf(&b, "\tfmt.Println(\"body %d line %d\")\n", i, j)
}
b.WriteString("}\n\n")
}
writeReadTestFile(t, dir, name, b.String())
}
func TestRead_LargeGoFileReturnsOutline(t *testing.T) {
tmp := t.TempDir()
bigGoFile(t, tmp, "big.go", 60)
res := execRead(t, tmp, `{"path":"big.go"}`)
if res.IsError {
t.Fatalf("unexpected tool error: %s", res.Content)
}
if !strings.Contains(res.Content, "func Fn0()") || !strings.Contains(res.Content, "func Fn59()") {
t.Fatalf("outline should span the whole file, got:\n%s", truncateForMsg(res.Content))
}
if strings.Contains(res.Content, "body 0 line 0") {
t.Fatalf("outline must not include function bodies, got:\n%s", truncateForMsg(res.Content))
}
if !strings.Contains(res.Content, "offset") {
t.Fatalf("outline must tell the model how to read a range, got:\n%s", truncateForMsg(res.Content))
}
}
// Line numbers are the whole point: without them the outline is not actionable.
func TestRead_OutlineCarriesLineNumbers(t *testing.T) {
tmp := t.TempDir()
bigGoFile(t, tmp, "big.go", 60)
res := execRead(t, tmp, `{"path":"big.go"}`)
// Each Fn is preceded by a doc comment, so blocks are 24 lines apart
// starting at line 6. Assert the mapping is real rather than just present.
for _, want := range []string{"6: func Fn0()", "30: func Fn1()"} {
if !strings.Contains(res.Content, want) {
t.Fatalf("expected outline entry %q, got:\n%s", want, truncateForMsg(res.Content))
}
}
}
func TestRead_ExplicitRangeBypassesOutline(t *testing.T) {
tmp := t.TempDir()
bigGoFile(t, tmp, "big.go", 60)
res := execRead(t, tmp, `{"path":"big.go","offset":6,"limit":3}`)
if !strings.Contains(res.Content, "body 0 line 0") {
t.Fatalf("an explicit range must return raw content, got:\n%s", truncateForMsg(res.Content))
}
}
func TestRead_FullBypassesOutline(t *testing.T) {
tmp := t.TempDir()
bigGoFile(t, tmp, "big.go", 60)
res := execRead(t, tmp, `{"path":"big.go","full":true}`)
if !strings.Contains(res.Content, "body 0 line 0") {
t.Fatalf("full:true must return raw content, got:\n%s", truncateForMsg(res.Content))
}
}
// Files at or below the normal read budget must behave exactly as before.
func TestRead_SmallGoFileIsUnaffected(t *testing.T) {
tmp := t.TempDir()
bigGoFile(t, tmp, "small.go", 3)
res := execRead(t, tmp, `{"path":"small.go"}`)
if !strings.Contains(res.Content, "body 0 line 0") {
t.Fatalf("a small file must return raw content, got:\n%s", truncateForMsg(res.Content))
}
}
// An unrecognised language has no reliable declaration pattern, so outlining
// would risk returning an arbitrary subset of lines as if it were structure.
func TestRead_LargeUnknownFileTypeIsUnaffected(t *testing.T) {
tmp := t.TempDir()
var b strings.Builder
for i := range 2000 {
fmt.Fprintf(&b, "arbitrary line %d\n", i)
}
writeReadTestFile(t, tmp, "data.unknownext", b.String())
res := execRead(t, tmp, `{"path":"data.unknownext"}`)
if !strings.Contains(res.Content, "arbitrary line 0") {
t.Fatalf("an unknown file type must fall back to raw content, got:\n%s", truncateForMsg(res.Content))
}
}
// A large source file with no recognisable declarations (generated tables,
// embedded data) must not collapse to an empty outline.
func TestRead_LargeGoFileWithoutDeclarationsIsUnaffected(t *testing.T) {
tmp := t.TempDir()
var b strings.Builder
b.WriteString("package gen\n")
for i := range 2000 {
fmt.Fprintf(&b, "\t\"key%d\": %d,\n", i, i)
}
writeReadTestFile(t, tmp, "table.go", b.String())
res := execRead(t, tmp, `{"path":"table.go"}`)
if !strings.Contains(res.Content, `"key0"`) {
t.Fatalf("a file with no declarations must fall back to raw content, got:\n%s", truncateForMsg(res.Content))
}
}
func truncateForMsg(s string) string {
if len(s) > 600 {
return s[:600] + "..."
}
return s
}
// An explicit limit that happens to equal the default is still explicit, and
// must be honoured as a content request rather than read as "unspecified".
func TestRead_ExplicitDefaultSizedLimitBypassesOutline(t *testing.T) {
tmp := t.TempDir()
bigGoFile(t, tmp, "big.go", 60)
res := execRead(t, tmp, fmt.Sprintf(`{"path":"big.go","limit":%d}`, DefaultReadLines))
if !strings.Contains(res.Content, "body 0 line 0") {
t.Fatalf("an explicit limit must return raw content, got:\n%s", truncateForMsg(res.Content))
}
}