The index built google/codesearch over the chat workspace on every daemon start, for the process lifetime, through a child-process helper and a SQLite metadata DB -- and grep consulted it only for searches confined to the workspace root, which the analysed sessions showed was a small fraction of searches. The compute was continuous; the benefit was not. Removes internal/indexing, the workspace-codesearch helper subcommand, the [indexing] config section, and the GrepIndex accelerator seam from the grep tool, which returns to its direct walk. google/codesearch leaves go.mod with it.
260 lines
7.8 KiB
Go
260 lines
7.8 KiB
Go
package builtin_test
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"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())
|
|
})
|
|
}
|
|
|
|
// TestWorkspaceToolsCarryTheirOwnIcon pins that each builtin declares an icon
|
|
// and that the conversion carries it. A chat surface renders what the tool
|
|
// registered, so an icon missing here is an icon missing on screen.
|
|
func TestWorkspaceToolsCarryTheirOwnIcon(t *testing.T) {
|
|
p := startedProvider(t, t.TempDir())
|
|
entries, err := p.Discover(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("Tools: %v", err)
|
|
}
|
|
if len(entries) == 0 {
|
|
t.Fatal("no workspace tools registered")
|
|
}
|
|
seen := make(map[string]bool)
|
|
for _, entry := range entries {
|
|
if entry.Emoji == "" {
|
|
t.Errorf("tool %q registered no icon", entry.Name)
|
|
continue
|
|
}
|
|
seen[entry.Emoji] = true
|
|
}
|
|
if len(seen) < 2 {
|
|
t.Errorf("icons = %v, want tools to be distinguishable by icon", seen)
|
|
}
|
|
}
|