diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index a0749444c..78f95a167 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -44,6 +44,7 @@ import ( "github.com/samcharles93/archie-core/internal/forge" forgewebhook "github.com/samcharles93/archie-core/internal/forge/webhook" "github.com/samcharles93/archie-core/internal/gateway" + "github.com/samcharles93/archie-core/internal/indexing" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/overlay" infraembedding "github.com/samcharles93/archie-core/internal/infrastructure/embedding" @@ -64,6 +65,7 @@ import ( "github.com/samcharles93/archie-core/internal/skill" "github.com/samcharles93/archie-core/internal/storage" "github.com/samcharles93/archie-core/internal/tools" + toolsbuiltin "github.com/samcharles93/archie-core/internal/tools/builtin" "github.com/samcharles93/archie-core/internal/tools/minimax" toolprovider "github.com/samcharles93/archie-core/internal/tools/provider" builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin" @@ -1190,9 +1192,40 @@ func (b *boot) setupGuardrails() { ) } +// workspaceIndex builds the optional codesearch index that narrows grep's +// candidate files. It returns a slice so a failure is simply "no index" +// rather than a typed-nil reaching the provider. +// +// Every failure degrades instead of propagating: grep falls back to its +// direct walk, which is what it did before this was wired at all. An +// accelerator that can refuse to build must never stop the daemon starting. +// +// The context is detached from cancellation but keeps its values, because +// NewManager starts an asynchronous build whose lifetime is the process +// while the gateway path's boot context is cancelled once startup returns. +// Inheriting cancellation would kill the build mid-flight and leave grep +// permanently unindexed with nothing to show why; using Background instead +// would silently drop any values the caller's context carries. +func (b *boot) workspaceIndex(ctx context.Context, workspace string) []toolsbuiltin.GrepIndex { + cfg, log := b.cfg, b.log + idxCfg := cfg.Indexing.WithDefaults(cfg.WorkDir) + manager, err := indexing.NewManager( + context.WithoutCancel(ctx), + indexing.Config{IndexDir: idxCfg.IndexDir, DBPath: idxCfg.DBPath, Log: log}, + workspace, + ) + if err != nil { + log.Warn("workspace index unavailable; grep will search unindexed", + "workspace", workspace, "index_dir", idxCfg.IndexDir, "error", err) + return nil + } + log.Info("workspace index enabled", "workspace", workspace, "index_dir", idxCfg.IndexDir) + return []toolsbuiltin.GrepIndex{manager} +} + // registerTools registers the tool providers with a lifecycle: memory, // workspace file/shell tools and optional MCP servers. -func (b *boot) registerTools() error { +func (b *boot) registerTools(ctx context.Context) error { cfg, log := b.cfg, b.log b.providerRegistry = toolprovider.NewRegistry(b.toolReg) if err := b.providerRegistry.Register(memorytoolprovider.New(b.memManager)); err != nil { @@ -1204,7 +1237,8 @@ func (b *boot) registerTools() error { // deliberate choice rather than a default. if workspace := cfg.Chat.Workspace; workspace != "" { unrestricted := cfg.Chat.UnrestrictedFilesystem - if err := b.providerRegistry.Register(builtintoolprovider.New(workspace, unrestricted)); err != nil { + index := b.workspaceIndex(ctx, workspace) + if err := b.providerRegistry.Register(builtintoolprovider.New(workspace, unrestricted, index...)); err != nil { log.Error("workspace tool provider registration failed", "err", err) return err } diff --git a/internal/app/archied/gateway.go b/internal/app/archied/gateway.go index e21c5aea3..dc9398a04 100644 --- a/internal/app/archied/gateway.go +++ b/internal/app/archied/gateway.go @@ -115,7 +115,7 @@ func (b *boot) startGatewayRuntime(ctx context.Context, actor gateway.ChatTaskAc if err := b.setupMemory(); err != nil { //nolint:contextcheck // setupMemory owns its lifecycle contexts, matching the daemon's setupMemoryAll return nil, err } - if err := b.registerTools(); err != nil { + if err := b.registerTools(ctx); err != nil { return nil, err } b.registerStandaloneTools() diff --git a/internal/app/archied/main.go b/internal/app/archied/main.go index 168b780df..18b4ec0c9 100644 --- a/internal/app/archied/main.go +++ b/internal/app/archied/main.go @@ -435,7 +435,7 @@ func Run() int { //nolint:cyclop // the composition root's setup sequence is del } b.setupGuardrails() - if err := b.registerTools(); err != nil { + if err := b.registerTools(ctx); err != nil { return 1 } b.registerStandaloneTools() diff --git a/internal/tools/provider/builtin/provider.go b/internal/tools/provider/builtin/provider.go index f80e2f4bb..2717037ec 100644 --- a/internal/tools/provider/builtin/provider.go +++ b/internal/tools/provider/builtin/provider.go @@ -32,6 +32,11 @@ type Provider struct { // config.ChatConfig.UnrestrictedFilesystem for when that is wanted. unrestricted bool + // workspaceIndex narrows grep's candidate files. Optional: nil leaves + // grep on its direct walk, which is the behaviour when no index is + // configured or its build has not finished. + workspaceIndex toolsbuiltin.GrepIndex + // registry holds the constructed tools. Built once in Start so // Discover cannot partially fail. registry *toolsbuiltin.Registry @@ -43,8 +48,15 @@ type Provider struct { // // unrestricted lets the file tools reach absolute paths outside workspace; // relative paths still resolve against it either way. -func New(workspace string, unrestricted bool) *Provider { - return &Provider{workspace: workspace, unrestricted: unrestricted} +// A workspace index is optional and variadic to match +// toolsbuiltin.RegisterBuiltins, which has always taken one the same way. +// Only the first is used. +func New(workspace string, unrestricted bool, indexes ...toolsbuiltin.GrepIndex) *Provider { + p := &Provider{workspace: workspace, unrestricted: unrestricted} + if len(indexes) > 0 { + p.workspaceIndex = indexes[0] + } + return p } // Manifest declares the adapter's tool capability. @@ -72,7 +84,14 @@ func (p *Provider) Start(ctx context.Context) error { toolsbuiltin.SetPathConfinement(!p.unrestricted) registry := toolsbuiltin.NewRegistry() - if err := toolsbuiltin.RegisterBuiltins(registry, p.workspace); err != nil { + // Forwarded only when present. Passing a nil interface would still work + // today (grep nil-checks it), but an explicit branch keeps a typed-nil + // index from reaching a call on a nil receiver. + var indexes []toolsbuiltin.GrepIndex + if p.workspaceIndex != nil { + indexes = append(indexes, p.workspaceIndex) + } + if err := toolsbuiltin.RegisterBuiltins(registry, p.workspace, indexes...); err != nil { return fmt.Errorf("workspace tool provider: register builtins: %w", err) } p.registry = registry diff --git a/internal/tools/provider/builtin/provider_test.go b/internal/tools/provider/builtin/provider_test.go index 3b64a0dce..08b997ed4 100644 --- a/internal/tools/provider/builtin/provider_test.go +++ b/internal/tools/provider/builtin/provider_test.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "strings" + "sync" "testing" provider "github.com/samcharles93/archie-core/internal/tools/provider/builtin" @@ -232,3 +233,75 @@ func TestUnrestrictedFilesystemReachesOutsideWorkspace(t *testing.T) { _ = 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") + } +}