Files
archie/internal/tools/provider/builtin/provider_test.go
T
sam 2c805ef15d feat(tools): wire the workspace codesearch index into grep
internal/tools/builtin.RegisterBuiltins has always accepted a GrepIndex and
nothing ever passed one, so every grep ran the direct walk no matter what
[indexing] was set to. internal/indexing was fully implemented and tested
with zero production callers, and internal/config decoded IndexingConfig into
nothing. Two halves of one defect.

The composition root now builds an indexing.Manager and hands it to the
workspace tool provider, which forwards it to RegisterBuiltins. deadcode
confirms the result rather than the commit message asserting it: unreachable
functions drop from 88 to 73, and all 15 internal/indexing entries are gone.

Failure degrades rather than propagates. A manager that cannot build logs a
warning and grep keeps its direct walk, which is exactly what it did before
this was wired. An accelerator must never stop the daemon starting.

The context is detached with WithoutCancel rather than replaced with
Background: the index build's lifetime is the process, while the gateway
path's boot context is cancelled once startup returns, so inheriting
cancellation would kill the build mid-flight. Background would have satisfied
the linter too, but would silently drop the caller's context values, so
registerTools now takes the context its callers already had.

internal/agentexec's RegisterBuiltins call is deliberately left alone. It
registers builtins only to harvest their names for a plugin tool-name
collision check and never executes them, so there is no grep there to
accelerate.
2026-09-18 13:40:45 +10:00

308 lines
9.1 KiB
Go

package builtin_test
import (
"context"
"os"
"path/filepath"
"strings"
"sync"
"testing"
provider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
)
func startedProvider(t *testing.T, workspace string) *provider.Provider {
t.Helper()
p := provider.New(workspace, false)
if err := p.Start(context.Background()); err != nil {
t.Fatalf("Start: %v", err)
}
t.Cleanup(func() { _ = p.Stop(context.Background()) })
return p
}
func TestDiscoverExposesWorkspaceTools(t *testing.T) {
t.Parallel()
entries, err := startedProvider(t, t.TempDir()).Discover(context.Background())
if err != nil {
t.Fatalf("Discover: %v", err)
}
got := make(map[string]bool, len(entries))
for _, e := range entries {
got[e.Name] = true
if e.Handler == nil {
t.Errorf("tool %q has no handler, so the model could call something that does nothing", e.Name)
}
if e.Description == "" {
t.Errorf("tool %q has no description", e.Name)
}
}
for _, want := range []string{"read", "write", "edit", "shell", "grep", "find"} {
if !got[want] {
t.Errorf("tool %q missing from discovery; got %v", want, got)
}
}
}
// TestHandlerRoundTripsThroughTauExecutor covers the seam this package
// exists for: archie hands the handler decoded arguments, tau's executor
// wants raw JSON, and the result comes back as content.
func TestHandlerRoundTripsThroughTauExecutor(t *testing.T) {
t.Parallel()
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "hello.txt"), []byte("g'day\n"), 0o600); err != nil {
t.Fatal(err)
}
entries, err := startedProvider(t, dir).Discover(context.Background())
if err != nil {
t.Fatalf("Discover: %v", err)
}
var read func(context.Context, map[string]any) (any, error)
for _, e := range entries {
if e.Name == "read" {
read = e.Handler
}
}
if read == nil {
t.Fatal("no read tool discovered")
}
out, err := read(context.Background(), map[string]any{"path": "hello.txt"})
if err != nil {
t.Fatalf("read handler: %v", err)
}
if text, ok := out.(string); !ok || !strings.Contains(text, "g'day") {
t.Errorf("read returned %#v, want content containing the file text", out)
}
}
// TestHandlerSurfacesToolFailureAsError guards the second half of the seam:
// tau reports a tool-level failure in Result.IsError rather than as an
// error, and swallowing that would show the model a successful call.
func TestHandlerSurfacesToolFailureAsError(t *testing.T) {
t.Parallel()
entries, err := startedProvider(t, t.TempDir()).Discover(context.Background())
if err != nil {
t.Fatalf("Discover: %v", err)
}
for _, e := range entries {
if e.Name != "read" {
continue
}
if _, err := e.Handler(context.Background(), map[string]any{"path": "absent.txt"}); err == nil {
t.Error("reading a missing file reported success")
}
return
}
t.Fatal("no read tool discovered")
}
// TestShellHandlerEnforcesHardlineRules checks the rules are actually
// wired into the executing handler. The rules are tested in
// internal/tools/command; what matters here is that a call cannot reach
// tau's executor without passing through them.
func TestShellHandlerEnforcesHardlineRules(t *testing.T) {
t.Parallel()
shell := shellHandler(t, t.TempDir())
if _, err := shell(context.Background(), map[string]any{"command": "rm -rf /"}); err == nil {
t.Error("the shell tool accepted a recursive delete of /")
} else if !strings.Contains(err.Error(), "refused") {
t.Errorf("error = %v, want a refusal naming the rule", err)
}
}
// TestShellHandlerAllowsOrdinaryCommands guards the other direction: the
// screen must not stand between the model and normal work.
func TestShellHandlerAllowsOrdinaryCommands(t *testing.T) {
t.Parallel()
dir := t.TempDir()
out, err := shellHandler(t, dir)(context.Background(), map[string]any{
"command": `echo "rm -rf / is only mentioned here"`,
})
if err != nil {
t.Fatalf("an ordinary echo was refused: %v", err)
}
if text, ok := out.(string); !ok || !strings.Contains(text, "only mentioned here") {
t.Errorf("shell returned %#v, want the echoed text", out)
}
}
func shellHandler(t *testing.T, workspace string) func(context.Context, map[string]any) (any, error) {
t.Helper()
entries, err := startedProvider(t, workspace).Discover(context.Background())
if err != nil {
t.Fatalf("Discover: %v", err)
}
for _, e := range entries {
if e.Name == "shell" {
return e.Handler
}
}
t.Fatal("no shell tool discovered")
return nil
}
// TestUnconfiguredWorkspaceIsRejected keeps the tools from silently rooting
// at the daemon's own working directory, which in the container is /.
func TestUnconfiguredWorkspaceIsRejected(t *testing.T) {
t.Parallel()
if err := provider.New("", false).Start(context.Background()); err == nil {
t.Error("a provider with no workspace started successfully")
}
}
// TestUnrestrictedFilesystemReachesOutsideWorkspace covers the operator
// posture end to end, through a real read of a real file.
//
// A deployment whose agent partitions disks, mounts them and migrates data is
// working across the filesystem by definition. Confined, every one of those
// touches has to go through shell -- which was never confined anyway -- so the
// jail only ever decided which tool did the work, not what the agent reached.
//
// Not parallel: confinement is process-wide, so these two subtests must not
// run beside tests that assume the default.
func TestUnrestrictedFilesystemReachesOutsideWorkspace(t *testing.T) {
// A file deliberately outside the workspace, as /etc or a fresh mount
// would be.
outside := filepath.Join(t.TempDir(), "fstab")
if err := os.WriteFile(outside, []byte("UUID=1234 /mnt/newdisk ext4 defaults 0 2\n"), 0o600); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
unrestricted bool
wantErr bool
}{
{name: "confined refuses", unrestricted: false, wantErr: true},
{name: "unrestricted reads", unrestricted: true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
p := provider.New(t.TempDir(), tc.unrestricted)
if err := p.Start(context.Background()); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = p.Stop(context.Background()) })
entries, err := p.Discover(context.Background())
if err != nil {
t.Fatal(err)
}
var read func(context.Context, map[string]any) (any, error)
for _, e := range entries {
if e.Name == "read" {
read = e.Handler
}
}
if read == nil {
t.Fatal("no read tool discovered")
}
out, err := read(context.Background(), map[string]any{"path": outside})
if tc.wantErr {
if err == nil {
t.Fatal("read reached outside the workspace while confined")
}
return
}
if err != nil {
t.Fatalf("read was refused with confinement disabled: %v", err)
}
if text, ok := out.(string); !ok || !strings.Contains(text, "/mnt/newdisk") {
t.Errorf("read returned %#v, want the file contents", out)
}
})
}
// Leave the process as we found it for any test that runs after.
t.Cleanup(func() {
p := provider.New(t.TempDir(), false)
_ = p.Start(context.Background())
_ = p.Stop(context.Background())
})
}
// stubGrepIndex records whether the grep tool consulted a supplied workspace
// index, and serves a fixed candidate set.
type stubGrepIndex struct {
mu sync.Mutex
calls int
pattern string
files []string
}
func (s *stubGrepIndex) Candidates(_ context.Context, pattern string, _, _ bool) ([]string, bool) {
s.mu.Lock()
defer s.mu.Unlock()
s.calls++
s.pattern = pattern
return s.files, true
}
func (s *stubGrepIndex) observed() (int, string) {
s.mu.Lock()
defer s.mu.Unlock()
return s.calls, s.pattern
}
// TestGrepConsultsSuppliedWorkspaceIndex covers the wiring this provider is
// the only production owner of. internal/tools/builtin.RegisterBuiltins has
// always accepted a GrepIndex, and nothing ever passed one, so every grep ran
// the direct walk regardless of configuration. A provider that accepts an
// index and then drops it on the floor is the exact failure worth a test:
// it looks wired from the outside and changes nothing.
func TestGrepConsultsSuppliedWorkspaceIndex(t *testing.T) {
t.Parallel()
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "hit.go"), []byte("package p\n\nfunc Target() {}\n"), 0o600); err != nil {
t.Fatal(err)
}
idx := &stubGrepIndex{files: []string{filepath.Join(dir, "hit.go")}}
p := provider.New(dir, false, idx)
if err := p.Start(context.Background()); err != nil {
t.Fatalf("Start: %v", err)
}
t.Cleanup(func() { _ = p.Stop(context.Background()) })
entries, err := p.Discover(context.Background())
if err != nil {
t.Fatalf("Discover: %v", err)
}
var grep func(context.Context, map[string]any) (any, error)
for _, e := range entries {
if e.Name == "grep" {
grep = e.Handler
}
}
if grep == nil {
t.Fatal("no grep tool discovered")
}
if _, err := grep(context.Background(), map[string]any{"pattern": "Target"}); err != nil {
t.Fatalf("grep handler: %v", err)
}
calls, pattern := idx.observed()
if calls == 0 {
t.Fatal("grep never consulted the supplied workspace index, so the index is decoded, constructed and ignored")
}
if pattern != "Target" {
t.Errorf("index asked for pattern %q, want the caller's pattern %q", pattern, "Target")
}
}