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

172 lines
4.8 KiB
Go

package builtin
import (
"context"
"log/slog"
"strings"
"testing"
"time"
)
// --- G16: stderr redaction, rate limiting, capture cap ---
func TestRedactSecrets(t *testing.T) {
cases := []struct {
name string
in string
want string
}{
{
name: "api key env pattern",
in: "TAU_API_KEY=sk-abc123testtesttesttest",
want: "TAU_API_KEY=[REDACTED]",
},
{
name: "bearer token, spec's exact example",
in: "Authorization: Bearer tok_abc123",
want: "Authorization: Bearer [REDACTED]",
},
{
name: "bare sk- key",
in: "leaked key: sk-abcdefghijklmnopqrstuvwxyz",
want: "leaked key: [REDACTED]",
},
{
name: "generic secret env var",
in: "DATABASE_PASSWORD=hunter2",
want: "DATABASE_PASSWORD=[REDACTED]",
},
{
name: "generic token env var",
in: "GITHUB_TOKEN=ghp_abcdefghijklmnop",
want: "GITHUB_TOKEN=[REDACTED]",
},
{
name: "no secret present",
in: "starting build in /home/user/project",
want: "starting build in /home/user/project",
},
{
name: "ordinary key=value that is not sensitive",
in: "BUILD_MODE=release",
want: "BUILD_MODE=release",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := redactSecrets(tc.in)
if got != tc.want {
t.Errorf("redactSecrets(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}
func TestDrainChildStderr_RedactsBeforeLogging(t *testing.T) {
var records []slog.Record
orig := slog.Default()
defer slog.SetDefault(orig)
slog.SetDefault(slog.New(captureHandler{records: &records}))
r := strings.NewReader("TAU_API_KEY=sk-abc123testtesttesttest\n")
drainChildStderr(r, "tau#test01")
if len(records) != 1 {
t.Fatalf("expected 1 log record, got %d", len(records))
}
if strings.Contains(records[0].Message, "sk-abc123") {
t.Errorf("secret leaked into log: %q", records[0].Message)
}
if !strings.Contains(records[0].Message, "[REDACTED]") {
t.Errorf("expected redacted message, got %q", records[0].Message)
}
}
func TestDrainChildStderr_CapsTotalCapture(t *testing.T) {
var records []slog.Record
orig := slog.Default()
defer slog.SetDefault(orig)
slog.SetDefault(slog.New(captureHandler{records: &records}))
// Each line is 100 bytes; write enough lines to exceed the 64KiB cap.
// Uses a fake clock that jumps a full rate-limit window forward per
// line, so the rate limiter never engages and only the capture cap is
// under test - isolating it from TestDrainChildStderr_RateLimitsBurstInOneWindow.
line := strings.Repeat("x", 99)
var sb strings.Builder
lineCount := (stderrMaxCaptureBytes / 100) + 50
for range lineCount {
sb.WriteString(line)
sb.WriteByte('\n')
}
fakeNow := time.Now()
clock := func() time.Time {
fakeNow = fakeNow.Add(stderrRateLimitWindow)
return fakeNow
}
drainChildStderrWithClock(strings.NewReader(sb.String()), "tau#test02", clock)
var truncationMarkers int
for _, rec := range records {
if strings.Contains(rec.Message, "capture limit exceeded") {
truncationMarkers++
}
}
if truncationMarkers != 1 {
t.Errorf("expected exactly 1 capture-limit truncation marker, got %d", truncationMarkers)
}
if len(records) >= lineCount {
t.Errorf("expected fewer log records than input lines (cap should stop logging), got %d records for %d lines", len(records), lineCount)
}
}
func TestDrainChildStderr_RateLimitsBurstInOneWindow(t *testing.T) {
var records []slog.Record
orig := slog.Default()
defer slog.SetDefault(orig)
slog.SetDefault(slog.New(captureHandler{records: &records}))
// Well over the 4096*5=20480 byte per-window budget, all within the
// same instant (no real time passes reading from a strings.Reader).
line := strings.Repeat("y", 999)
var sb strings.Builder
for range 50 { // ~50KB total, comfortably over the 20480 budget
sb.WriteString(line)
sb.WriteByte('\n')
}
drainChildStderr(strings.NewReader(sb.String()), "tau#test03")
var rateLimitMarkers int
for _, rec := range records {
if strings.Contains(rec.Message, "rate limit exceeded") {
rateLimitMarkers++
}
}
if rateLimitMarkers != 1 {
t.Errorf("expected exactly 1 rate-limit truncation marker, got %d", rateLimitMarkers)
}
if len(records) >= 50 {
t.Errorf("expected rate limiting to suppress most of the 50 lines, got %d records", len(records))
}
}
// captureHandler is a minimal slog.Handler that records every Record it
// receives, for asserting on log content without depending on slog's text
// formatting.
type captureHandler struct {
records *[]slog.Record
}
func (captureHandler) Enabled(context.Context, slog.Level) bool { return true }
func (h captureHandler) Handle(_ context.Context, r slog.Record) error {
*h.records = append(*h.records, r)
return nil
}
func (h captureHandler) WithAttrs(_ []slog.Attr) slog.Handler { return h }
func (h captureHandler) WithGroup(_ string) slog.Handler { return h }