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.
This commit is contained in:
1 parent
a533810546
commit
7b69a4df1f
5 files changed
+133
-7
No files matched your search
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user