fix(tools): report a cancelled find walk as an error, not empty results

The WalkDir callback swallows every per-entry error and returns nil,
so the only error that can escape is ctx.Err() from the cancellation
select. The old guard compared that error against itself and never
fired, so a timeout or /stop fell through to whatever partial matches
had accumulated, or "no matches found" when it had none -- telling the
model a search found nothing when it never finished.

Refs archie-core-ts9
This commit is contained in:
sam committed 2026-08-05 11:52:19 +10:00
1 parent 58f3d40683
commit 393eb18ec5
2 files changed
+29 -2

No files matched your search

+4 -2
View File
@@ -13,7 +13,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
@@ -239,9 +238,12 @@ func runFindGoFallback(ctx context.Context, cwd, searchPath string, p FindParams
return nil
})
if err != nil && !errors.Is(err, ctx.Err()) {
if err != nil {
return Result{Content: fmt.Sprintf("find error: %v", err), IsError: true}, nil
}
if ctxErr := ctx.Err(); ctxErr != nil {
return Result{Content: fmt.Sprintf("find error: search did not complete: %v", ctxErr), IsError: true}, nil
}
if len(matches) == 0 {
return Result{Content: "no matches found"}, nil
+25
View File
@@ -136,6 +136,31 @@ func TestFindFallback_NotADirectory(t *testing.T) {
}
}
// A cancelled or timed-out walk must be reported as an error, not silently
// returned as "no matches found" (or as whatever partial results happened to
// accumulate before cancellation). The WalkDir callback swallows every
// per-entry error and returns nil, so the only error that can ever reach the
// caller is ctx.Err() from the cancellation select -- the old guard compared
// that against itself and never fired.
func TestFindFallback_CancelledWalkReportsError(t *testing.T) {
tmp := t.TempDir()
createTestFile(t, tmp, "foo.go", "package main")
ctx, cancel := context.WithCancel(context.Background())
cancel()
res, err := runFindGoFallback(ctx, tmp, tmp, FindParams{})
if err != nil {
t.Fatalf("runFindGoFallback returned an error: %v", err)
}
if !res.IsError {
t.Fatalf("expected an error result for a cancelled walk, got: %q", res.Content)
}
if res.Content == "no matches found" {
t.Fatal("cancelled walk reported as 'no matches found': the search never finished")
}
}
func createTestFile(t *testing.T, base, relPath, content string) {
t.Helper()
full := filepath.Join(base, relPath)