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

99 lines
3.0 KiB
Go

package builtin
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
)
func TestWriteTool_PopulatesDiffDetails_NewFile(t *testing.T) {
tmp := t.TempDir()
path := filepath.Join(tmp, "f.txt")
tool := NewWriteTool(tmp, NewMutationQueue(), nil)
params := `{"path": "f.txt", "content": "hello\n"}`
res, err := tool.Execute(context.Background(), json.RawMessage(params), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if res.IsError {
t.Fatalf("unexpected tool error: %s", res.Content)
}
details, ok := res.Details.(DiffDetails)
if !ok {
t.Fatalf("expected Details to be a DiffDetails, got %T", res.Details)
}
if details.OldContent != "" {
t.Fatalf("expected empty OldContent for a new file, got %q", details.OldContent)
}
if details.NewContent != "hello\n" {
t.Fatalf("NewContent mismatch: got %q", details.NewContent)
}
if details.Path != path {
t.Fatalf("Path mismatch: got %q want %q", details.Path, path)
}
}
func TestWriteTool_PopulatesDiffDetails_Overwrite(t *testing.T) {
tmp := t.TempDir()
path := filepath.Join(tmp, "f.txt")
original := "old content\n"
if err := os.WriteFile(path, []byte(original), 0o644); err != nil {
t.Fatal(err)
}
tool := NewWriteTool(tmp, NewMutationQueue(), nil)
params := `{"path": "f.txt", "content": "new content\n", "overwrite": true}`
res, err := tool.Execute(context.Background(), json.RawMessage(params), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if res.IsError {
t.Fatalf("unexpected tool error: %s", res.Content)
}
details, ok := res.Details.(DiffDetails)
if !ok {
t.Fatalf("expected Details to be a DiffDetails, got %T", res.Details)
}
if details.OldContent != original {
t.Fatalf("OldContent mismatch: got %q want %q", details.OldContent, original)
}
if details.NewContent != "new content\n" {
t.Fatalf("NewContent mismatch: got %q", details.NewContent)
}
}
// TestWriteTool_RespectsParentContextDeadline is the regression test for
// tau-6wa: the executor used to create a timeout-derived context and then
// discard it, passing the original (unbounded) ctx to every downstream I/O
// call. With that bug, an already-expired parent context would have no
// effect on the write. Here the parent's deadline has already passed when
// Execute is called, so the executor must surface a timeout error instead
// of proceeding to write the file.
func TestWriteTool_RespectsParentContextDeadline(t *testing.T) {
tmp := t.TempDir()
path := filepath.Join(tmp, "f.txt")
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second))
defer cancel()
tool := NewWriteTool(tmp, NewMutationQueue(), nil)
params := `{"path": "f.txt", "content": "hello\n"}`
res, err := tool.Execute(ctx, json.RawMessage(params), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !res.IsError {
t.Fatalf("expected a timeout error result for an already-expired context, got success: %+v", res)
}
if _, statErr := os.Stat(path); statErr == nil {
t.Fatal("expected no file to be written when the context had already expired")
}
}