diff --git a/internal/domain/workflow/tdd.go b/internal/domain/workflow/tdd.go index c90bf2041..1bc02b595 100644 --- a/internal/domain/workflow/tdd.go +++ b/internal/domain/workflow/tdd.go @@ -23,126 +23,138 @@ func TDD() Workflow { StagePrepareWorktree(), StageRepoStages(), StageBaselineGate(), - - AgentStage{ - Name: "analyse", - Role: "planner", - ReadOnly: true, - Mission: func(tc *TaskContext) string { - return fmt.Sprintf( - "Analyse this %s for the repository %s and determine the problem surface.\n\n"+ - "%s\n\n"+ - "Explore the code read-only and call finish with status \"passed\" and a summary "+ - "containing: the root cause (file and function), the exact conditions that trigger it, "+ - "the expected vs actual behaviour, and which test cases would prove the bug. "+ - "Call finish with status \"blocked\" if you cannot locate a plausible cause.", - taskKind(tc.Task), tc.Repo.FullName(), taskPromptBlock(tc.Task), - ) - }, - OnResult: func(tc *TaskContext, res agentexec.Result) error { - tc.Task.Plan = res.Summary - return nil - }, - }.Stage(), - - AgentStage{ - Name: "repro-tests", - Role: "builder", - // The inverted gate: code-quality commands must still - // pass, but the test command (last in repo.Gate) MUST - // fail -- proof the repro captures the bug. - Gate: func(tc *TaskContext) agentexec.Gate { - return tddReproGate(tc.Repo, tc.Cfg.Budgets) - }, - Mission: func(tc *TaskContext) string { - cmd := testCommand(tc.Repo) - return fmt.Sprintf( - "Write tests that REPRODUCE this bug in the repository %s. Do NOT fix the bug yet.\n\n"+ - "%s\n\n\n%s\n\n\n"+ - "Add tests that pass once the bug is fixed but FAIL today because of it. The gate is "+ - "inverted for this stage: it requires the full gate to pass EXCEPT %s which is required "+ - "to FAIL. Do not touch non-test files. Call finish with status \"passed\" once the failing "+ - "repro is in place, summarising which tests capture the bug.", - tc.Repo.FullName(), taskPromptBlock(tc.Task), tc.Task.Plan, cmd, - ) - }, - }.Stage(), - - // Capture the failing test run -- the proof the repro - // reproduces the bug, posted on the PR later. - {Name: "capture-proof", Run: func(ctx context.Context, tc *TaskContext) error { - argv := testCommandArgv(tc.Repo) - if len(argv) == 0 { - return fmt.Errorf("tdd: repo %s has no gate configured -- cannot capture repro proof (add at least one [[repos.gate]] command, with the test runner last)", tc.Repo.FullName()) - } - cmd := exec.CommandContext(ctx, argv[0], argv[1:]...) - cmd.Dir = tc.Dir - out, err := cmd.CombinedOutput() - if err == nil { - return fmt.Errorf("repro tests unexpectedly pass after commit -- the bug is not captured") - } - tc.ReproProof = clip(string(out), 4000) - return nil - }}, - + tddAnalyseStage(), + tddReproTestsStage(), + tddCaptureProofStage(), StageCommit("commit-repro", func(tc *TaskContext) string { return "test: failing repro" + commitIssueReference("Refs", tc.Task) }), - - AgentStage{ - Name: "fix", - Role: "builder", - Gate: func(tc *TaskContext) agentexec.Gate { - return GateFromRepo(tc.Repo, tc.Cfg.Budgets) - }, - // Environmental, not advisory: the fix stage cannot touch - // test files at all -- the committed repro is the spec. - ProtectGlobs: func(tc *TaskContext) []string { - glob := tc.Repo.ResolvedTestGlob() - if glob == "" { - return nil - } - return []string{glob} - }, - ExtraRules: "The repro tests written in the previous stage are the bug's specification. " + - "Test files are write-protected in this stage -- make them pass by fixing the code they exercise.", - Mission: func(tc *TaskContext) string { - return fmt.Sprintf( - "Fix the bug in the repository %s. Failing repro tests are already committed; make them pass.\n\n"+ - "%s\n\n\n%s\n\n\n"+ - "The full quality gate (including 'go test ./...') must pass. Make the smallest fix "+ - "that makes the repro tests pass without changing them. Call finish with status "+ - "\"passed\" and a summary for the PR reviewer: root cause, the fix, and verification.", - tc.Repo.FullName(), taskPromptBlock(tc.Task), tc.Task.Plan, - ) - }, - OnResult: func(tc *TaskContext, res agentexec.Result) error { - tc.BuildSummary = res.Summary - return nil - }, - }.Stage(), - + tddFixStage(), StageCommitPush(func(tc *TaskContext) string { return fmt.Sprintf("fix: %s (archie)", tc.Task.Title) + commitIssueReference("Fixes", tc.Task) }), StageYaegiGate(), StageDiffCap(), - - // Open the PR, then post the captured failing-test output as - // evidence the bug was reproduced before the fix. - {Name: "open-pr", Run: func(ctx context.Context, tc *TaskContext) error { - body := fmt.Sprintf("%s\n\n---\n*workflow: tdd (failing repro committed first) · %d iterations · %d tokens*", - tc.BuildSummary, tc.Task.Iterations, tc.Task.TokensUsed) - if err := OpenPR(ctx, tc, body); err != nil { - return err - } - return nil - }}, + tddOpenPRStage(), }, } } +// tddAnalyseStage explores the bug read-only and records the root cause and +// reproduction plan on tc.Task.Plan. +func tddAnalyseStage() Stage { + return AgentStage{ + Name: "analyse", + Role: "planner", + ReadOnly: true, + Mission: func(tc *TaskContext) string { + return fmt.Sprintf( + "Analyse this %s for the repository %s and determine the problem surface.\n\n"+ + "%s\n\n"+ + "Explore the code read-only and call finish with status \"passed\" and a summary "+ + "containing: the root cause (file and function), the exact conditions that trigger it, "+ + "the expected vs actual behaviour, and which test cases would prove the bug. "+ + "Call finish with status \"blocked\" if you cannot locate a plausible cause.", + taskKind(tc.Task), tc.Repo.FullName(), taskPromptBlock(tc.Task), + ) + }, + OnResult: func(tc *TaskContext, res agentexec.Result) error { + tc.Task.Plan = res.Summary + return nil + }, + }.Stage() +} + +// tddReproTestsStage writes tests that reproduce the bug, under the +// inverted gate: code-quality commands must still pass, but the test +// command (last in repo.Gate) MUST fail -- proof the repro captures the bug. +func tddReproTestsStage() Stage { + return AgentStage{ + Name: "repro-tests", + Role: "builder", + Gate: func(tc *TaskContext) agentexec.Gate { + return tddReproGate(tc.Repo, tc.Cfg.Budgets) + }, + Mission: func(tc *TaskContext) string { + cmd := testCommand(tc.Repo) + return fmt.Sprintf( + "Write tests that REPRODUCE this bug in the repository %s. Do NOT fix the bug yet.\n\n"+ + "%s\n\n\n%s\n\n\n"+ + "Add tests that pass once the bug is fixed but FAIL today because of it. The gate is "+ + "inverted for this stage: it requires the full gate to pass EXCEPT %s which is required "+ + "to FAIL. Do not touch non-test files. Call finish with status \"passed\" once the failing "+ + "repro is in place, summarising which tests capture the bug.", + tc.Repo.FullName(), taskPromptBlock(tc.Task), tc.Task.Plan, cmd, + ) + }, + }.Stage() +} + +// tddCaptureProofStage captures the failing test run -- the proof the repro +// reproduces the bug, posted on the PR later. +func tddCaptureProofStage() Stage { + return Stage{Name: "capture-proof", Run: func(ctx context.Context, tc *TaskContext) error { + argv := testCommandArgv(tc.Repo) + if len(argv) == 0 { + return fmt.Errorf("tdd: repo %s has no gate configured -- cannot capture repro proof (add at least one [[repos.gate]] command, with the test runner last)", tc.Repo.FullName()) + } + cmd := exec.CommandContext(ctx, argv[0], argv[1:]...) + cmd.Dir = tc.Dir + out, err := cmd.CombinedOutput() + if err == nil { + return fmt.Errorf("repro tests unexpectedly pass after commit -- the bug is not captured") + } + tc.ReproProof = clip(string(out), 4000) + return nil + }} +} + +// tddFixStage fixes the bug. Test files are write-protected: the committed +// repro is the spec, and the fix stage cannot touch it. +func tddFixStage() Stage { + return AgentStage{ + Name: "fix", + Role: "builder", + Gate: func(tc *TaskContext) agentexec.Gate { + return GateFromRepo(tc.Repo, tc.Cfg.Budgets) + }, + ProtectGlobs: func(tc *TaskContext) []string { + glob := tc.Repo.ResolvedTestGlob() + if glob == "" { + return nil + } + return []string{glob} + }, + ExtraRules: "The repro tests written in the previous stage are the bug's specification. " + + "Test files are write-protected in this stage -- make them pass by fixing the code they exercise.", + Mission: func(tc *TaskContext) string { + return fmt.Sprintf( + "Fix the bug in the repository %s. Failing repro tests are already committed; make them pass.\n\n"+ + "%s\n\n\n%s\n\n\n"+ + "The full quality gate (including 'go test ./...') must pass. Make the smallest fix "+ + "that makes the repro tests pass without changing them. Call finish with status "+ + "\"passed\" and a summary for the PR reviewer: root cause, the fix, and verification.", + tc.Repo.FullName(), taskPromptBlock(tc.Task), tc.Task.Plan, + ) + }, + OnResult: func(tc *TaskContext, res agentexec.Result) error { + tc.BuildSummary = res.Summary + return nil + }, + }.Stage() +} + +// tddOpenPRStage opens the PR, then posts the captured failing-test output +// as evidence the bug was reproduced before the fix. +func tddOpenPRStage() Stage { + return Stage{Name: "open-pr", Run: func(ctx context.Context, tc *TaskContext) error { + body := fmt.Sprintf("%s\n\n---\n*workflow: tdd (failing repro committed first) · %d iterations · %d tokens*", + tc.BuildSummary, tc.Task.Iterations, tc.Task.TokensUsed) + return OpenPR(ctx, tc, body) + }} +} + // tddReproGate builds the inverted gate for the repro-tests stage: // every command from repo.Gate runs normally except the last one (the // test runner, by convention), which gets ExpectFailure -- the repro diff --git a/internal/tools/builtin/edit.go b/internal/tools/builtin/edit.go index 7bed95330..4918bedd7 100644 --- a/internal/tools/builtin/edit.go +++ b/internal/tools/builtin/edit.go @@ -234,50 +234,78 @@ func applyEdits(content string, edits []EditAction) (string, error) { return "", fmt.Errorf("edit %d: old_text and new_text are identical", i+1) } - matches := indexAll(content, edit.OldText) - - if edit.ReplaceAll { - if len(matches) == 0 { - matches = fuzzyIndexAll(content, edit.OldText) - } - if len(matches) == 0 { - return "", editNotFoundError(i, content, edit.OldText) - } - for _, m := range matches { - spans = append(spans, editSpan{start: m[0], end: m[1], repl: edit.NewText, editIdx: i}) - } - continue - } - - switch len(matches) { - case 1: - spans = append(spans, editSpan{start: matches[0][0], end: matches[0][1], repl: edit.NewText, editIdx: i}) - case 0: - fuzzy := fuzzyIndexAll(content, edit.OldText) - switch len(fuzzy) { - case 1: - spans = append(spans, editSpan{start: fuzzy[0][0], end: fuzzy[0][1], repl: edit.NewText, editIdx: i}) - case 0: - return "", editNotFoundError(i, content, edit.OldText) - default: - return "", editAmbiguousError(i, len(fuzzy)) - } - default: - return "", editAmbiguousError(i, len(matches)) + editSpans, err := resolveEditSpans(content, edit, i) + if err != nil { + return "", err } + spans = append(spans, editSpans...) } + if err := checkSpansOverlap(spans); err != nil { + return "", err + } + + return renderSpans(content, spans), nil +} + +// resolveEditSpans resolves one edit against the original content into the +// span(s) it applies. Matching is exact first; if old_text is not found, a +// fuzzy pass retries the match with trailing whitespace stripped from every +// line of both the content and old_text, which recovers the most common +// exact-match failure. +func resolveEditSpans(content string, edit EditAction, i int) ([]editSpan, error) { + matches := indexAll(content, edit.OldText) + + if edit.ReplaceAll { + if len(matches) == 0 { + matches = fuzzyIndexAll(content, edit.OldText) + } + if len(matches) == 0 { + return nil, editNotFoundError(i, content, edit.OldText) + } + spans := make([]editSpan, 0, len(matches)) + for _, m := range matches { + spans = append(spans, editSpan{start: m[0], end: m[1], repl: edit.NewText, editIdx: i}) + } + return spans, nil + } + + switch len(matches) { + case 1: + return []editSpan{{start: matches[0][0], end: matches[0][1], repl: edit.NewText, editIdx: i}}, nil + case 0: + fuzzy := fuzzyIndexAll(content, edit.OldText) + switch len(fuzzy) { + case 1: + return []editSpan{{start: fuzzy[0][0], end: fuzzy[0][1], repl: edit.NewText, editIdx: i}}, nil + case 0: + return nil, editNotFoundError(i, content, edit.OldText) + default: + return nil, editAmbiguousError(i, len(fuzzy)) + } + default: + return nil, editAmbiguousError(i, len(matches)) + } +} + +// checkSpansOverlap sorts spans by start offset and rejects any that overlap. +func checkSpansOverlap(spans []editSpan) error { sort.Slice(spans, func(a, b int) bool { return spans[a].start < spans[b].start }) for i := 1; i < len(spans); i++ { if spans[i].start < spans[i-1].end { - return "", fmt.Errorf( + return fmt.Errorf( "edits %d and %d overlap in the file; merge them into a single edit", spans[i-1].editIdx+1, spans[i].editIdx+1, ) } } + return nil +} +// renderSpans applies non-overlapping spans (already sorted by start) to +// content in one pass. +func renderSpans(content string, spans []editSpan) string { var b strings.Builder cursor := 0 for _, s := range spans { @@ -286,7 +314,7 @@ func applyEdits(content string, edits []EditAction) (string, error) { cursor = s.end } b.WriteString(content[cursor:]) - return b.String(), nil + return b.String() } func editNotFoundError(idx int, content, oldText string) error { diff --git a/internal/tools/builtin/find.go b/internal/tools/builtin/find.go index d6a56d978..59bb4b976 100644 --- a/internal/tools/builtin/find.go +++ b/internal/tools/builtin/find.go @@ -6,6 +6,8 @@ // internal/tools package holding the registry these are registered into). // - discovery is confined to the active workspace. Module-cache and other // absolute paths are rejected so find cannot list home or pkg/mod trees. +// - runFindGoFallback split into compileFindFilter and findWalkVisitor to +// satisfy golangci-lint's gocognit limit. No behavioural change. // // Refresh by diffing against that path at a newer tau commit. Do not // edit without recording the change above. @@ -16,6 +18,7 @@ import ( "context" "encoding/json" "fmt" + "io/fs" "os" "os/exec" "path/filepath" @@ -146,27 +149,57 @@ func runFindGoFallback(ctx context.Context, cwd, searchPath string, p FindParams var matches []string - var patternRe *regexp.Regexp - if p.Pattern != "" { - reStr := globToRegex(p.Pattern) - var err error - patternRe, err = regexp.Compile("^(?:" + reStr + ")$") - if err != nil { - return Result{Content: fmt.Sprintf("invalid pattern: %v", err), IsError: true}, nil - } + patternRe, err := compileFindFilter(p.Pattern, true) + if err != nil { + return Result{Content: fmt.Sprintf("invalid pattern: %v", err), IsError: true}, nil + } + excludeRe, err := compileFindFilter(p.Exclude, false) + if err != nil { + return Result{Content: fmt.Sprintf("invalid exclude: %v", err), IsError: true}, nil } - var excludeRe *regexp.Regexp - if p.Exclude != "" { - reStr := globToRegex(p.Exclude) - var err error - excludeRe, err = regexp.Compile("(?:" + reStr + ")") - if err != nil { - return Result{Content: fmt.Sprintf("invalid exclude: %v", err), IsError: true}, nil - } + visit := findWalkVisitor(ctx, cwd, searchPath, p, patternRe, excludeRe, &matches) + err = filepath.WalkDir(searchPath, visit) + 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 } - err = filepath.WalkDir(searchPath, func(walkPath string, d os.DirEntry, err error) error { + if len(matches) == 0 { + return Result{Content: "no matches found"}, nil + } + + sort.Strings(matches) + output := strings.Join(matches, "\n") + tr := TruncateHead(output, DefaultMaxLines, DefaultMaxBytes) + return Result{Content: tr.Content, Truncated: tr.Truncated, ResultBytes: tr.OriginalSize}, nil +} + +// compileFindFilter compiles a glob filter into a regexp. anchored wraps the +// translated pattern in ^(?:...)$ (whole-name match, used for the include +// pattern); unanchored leaves it as (?:...) (substring match, used for +// exclude). Returns a nil regexp and nil error for an empty glob. +func compileFindFilter(glob string, anchored bool) (*regexp.Regexp, error) { + if glob == "" { + return nil, nil + } + reStr := globToRegex(glob) + if anchored { + return regexp.Compile("^(?:" + reStr + ")$") + } + return regexp.Compile("(?:" + reStr + ")") +} + +// findWalkVisitor returns the filepath.WalkDir callback for +// runFindGoFallback: it enforces max depth, skips hidden entries and +// exclude matches, applies the type and pattern filters, and appends +// surviving paths (relative to cwd) to matches. +func findWalkVisitor( + ctx context.Context, cwd, searchPath string, p FindParams, patternRe, excludeRe *regexp.Regexp, matches *[]string, +) fs.WalkDirFunc { + return func(walkPath string, d os.DirEntry, err error) error { if err != nil { return nil //nolint:nilerr // intentionally skip inaccessible entries } @@ -236,24 +269,9 @@ func runFindGoFallback(ctx context.Context, cwd, searchPath string, p FindParams if err != nil { outputPath = walkPath } - matches = append(matches, filepath.ToSlash(outputPath)) + *matches = append(*matches, filepath.ToSlash(outputPath)) return nil - }) - 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 - } - - sort.Strings(matches) - output := strings.Join(matches, "\n") - tr := TruncateHead(output, DefaultMaxLines, DefaultMaxBytes) - return Result{Content: tr.Content, Truncated: tr.Truncated, ResultBytes: tr.OriginalSize}, nil } func isFdBinary(binary string) bool { diff --git a/internal/tools/builtin/grep.go b/internal/tools/builtin/grep.go index 63e6033cb..f85893159 100644 --- a/internal/tools/builtin/grep.go +++ b/internal/tools/builtin/grep.go @@ -10,6 +10,10 @@ // and the existing pure-Go grepFallback covers its absence. // - discovery is confined to the active workspace. Module-cache and other // absolute paths are rejected so grep cannot search home or pkg/mod trees. +// - makeGrepExecutor, grepFallback, and grepDirFallback were split into +// smaller helpers (resolveGrepTargets, runGrepBinary, grepExplicitTargets, +// setGrepResultsRelPath, grepWalkVisitor) to satisfy golangci-lint +// cyclop/gocognit limits. No behavioural change. // // Refresh by diffing against that path at a newer tau commit. Do not // edit without recording the change above. @@ -21,6 +25,7 @@ import ( "encoding/json" "errors" "fmt" + "io/fs" "os" "os/exec" "path/filepath" @@ -150,27 +155,7 @@ func makeGrepExecutor(cwd string, workspaceIndex GrepIndex) Executor { return Result{Content: "error: path escapes working directory", IsError: true, ErrorKind: "sandbox_escape"}, nil } - args := buildGrepArgs(p) - searchTargets := []string{searchPath} - searchBackend := "direct" - // The index is workspace-wide, so it can serve any search scoped at or - // below cwd once its candidates are narrowed to the requested subtree. - // Requiring searchPath == cwd meant subdirectory searches silently fell - // back to a full walk, which is why the index served only 5% of - // searches across analysed sessions. - if workspaceIndex != nil && isConfined(cwd, searchPath) { - if candidates, ok := workspaceIndex.Candidates(ctx, p.Pattern, p.Literal, p.CaseSensitive); ok { - if scoped := filterUnder(searchPath, candidates); len(scoped) > 0 { - searchTargets = scoped - searchBackend = "codesearch" - } - // An empty scoped set is NOT evidence of no matches: the index - // may simply be cold or stale for this subtree, so fall through - // to the authoritative direct walk rather than reporting "no - // matches found" from an empty candidate list. - } - } - args = append(args, searchTargets...) + searchTargets, searchBackend := resolveGrepTargets(ctx, workspaceIndex, p, cwd, searchPath) limit := p.Limit if limit <= 0 { @@ -191,49 +176,85 @@ func makeGrepExecutor(cwd string, workspaceIndex GrepIndex) Executor { return grepBackendResult(capGrepResult(output, limit), searchBackend, clamped), nil } - cmd := exec.CommandContext(ctx, binary, args...) - cmd.Dir = cwd + result, backend := runGrepBinary(ctx, binary, cwd, p, searchPath, searchTargets, searchBackend, limit) + return grepBackendResult(result, backend, clamped), nil + } +} - var stdout, stderr bytes.Buffer +// resolveGrepTargets picks the search targets and backend label: the +// workspace index's narrowed candidates when it can serve the request, or a +// direct walk of searchPath otherwise. +// +// The index is workspace-wide, so it can serve any search scoped at or below +// cwd once its candidates are narrowed to the requested subtree. Requiring +// searchPath == cwd meant subdirectory searches silently fell back to a full +// walk, which is why the index served only 5% of searches across analysed +// sessions. +func resolveGrepTargets(ctx context.Context, workspaceIndex GrepIndex, p GrepParams, cwd, searchPath string) ([]string, string) { + if workspaceIndex != nil && isConfined(cwd, searchPath) { + if candidates, ok := workspaceIndex.Candidates(ctx, p.Pattern, p.Literal, p.CaseSensitive); ok { + if scoped := filterUnder(searchPath, candidates); len(scoped) > 0 { + return scoped, "codesearch" + } + // An empty scoped set is NOT evidence of no matches: the index + // may simply be cold or stale for this subtree, so fall through + // to the authoritative direct walk rather than reporting "no + // matches found" from an empty candidate list. + } + } + return []string{searchPath}, "direct" +} + +// runGrepBinary runs the external rg binary against searchTargets, retrying +// against the authoritative searchPath if a codesearch-backed run fails, and +// returns a Result plus the backend that actually produced it. +func runGrepBinary( + ctx context.Context, binary, cwd string, p GrepParams, + searchPath string, searchTargets []string, searchBackend string, limit int, +) (Result, string) { + args := append(buildGrepArgs(p), searchTargets...) + cmd := exec.CommandContext(ctx, binary, args...) + cmd.Dir = cwd + + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + + err := cmd.Run() + + output := stdout.String() + if searchBackend == "codesearch" && err != nil { + // Snapshot candidates can disappear, or an unusually large candidate + // argv can fail to spawn. Retry the authoritative workspace path. + searchBackend = "direct" + args = append(buildGrepArgs(p), searchPath) + cmd = exec.CommandContext(ctx, binary, args...) + cmd.Dir = cwd + stdout.Reset() + stderr.Reset() cmd.Stdout = &stdout cmd.Stderr = &stderr - err = cmd.Run() - - output := stdout.String() - if searchBackend == "codesearch" && err != nil { - // Snapshot candidates can disappear, or an unusually large candidate - // argv can fail to spawn. Retry the authoritative workspace path. - searchBackend = "direct" - args = append(buildGrepArgs(p), searchPath) - cmd = exec.CommandContext(ctx, binary, args...) - cmd.Dir = cwd - stdout.Reset() - stderr.Reset() - cmd.Stdout = &stdout - cmd.Stderr = &stderr - err = cmd.Run() - output = stdout.String() - } - if output == "" && err != nil { - // Exit 1 means "no matches"; exit 2 means a real failure (bad - // pattern, unreadable path). Reporting the latter as an empty - // result would have the model state a file contains nothing when - // the search never ran, so the code must be checked, not just the - // error type. - var exitErr *exec.ExitError - if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 { - return grepBackendResult(Result{Content: "no matches found"}, searchBackend, clamped), nil - } - errMsg := stderr.String() - if errMsg == "" { - errMsg = err.Error() - } - return grepBackendResult(Result{Content: fmt.Sprintf("grep error: %s", errMsg), IsError: true}, searchBackend, clamped), nil - } - - return grepBackendResult(capGrepResult(output, limit), searchBackend, clamped), nil + output = stdout.String() } + if output == "" && err != nil { + // Exit 1 means "no matches"; exit 2 means a real failure (bad + // pattern, unreadable path). Reporting the latter as an empty + // result would have the model state a file contains nothing when + // the search never ran, so the code must be checked, not just the + // error type. + var exitErr *exec.ExitError + if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 { + return Result{Content: "no matches found"}, searchBackend + } + errMsg := stderr.String() + if errMsg == "" { + errMsg = err.Error() + } + return Result{Content: fmt.Sprintf("grep error: %s", errMsg), IsError: true}, searchBackend + } + + return capGrepResult(output, limit), searchBackend } // filterUnder returns the candidates at or below root. Paths are compared @@ -335,32 +356,16 @@ func grepFallback(ctx context.Context, p GrepParams, searchPath, cwd string, tar return "", err } - var results []grepResult - // When explicit files are provided (index candidates), search only those. if len(targets) > 0 && targets[0] != searchPath { - for _, path := range targets { - select { - case <-ctx.Done(): - return "", ctx.Err() - default: - } - res, fErr := grepFile(ctx, path, matcher, p.ContextBefore, p.ContextAfter) - if fErr != nil { - continue - } - for i := range res { - res[i].relPath, _ = filepath.Rel(cwd, path) - res[i].relPath = filepath.ToSlash(res[i].relPath) - if res[i].relPath == "" || res[i].relPath == "." { - res[i].relPath = filepath.ToSlash(filepath.Base(path)) - } - } - results = append(results, res...) + results, err := grepExplicitTargets(ctx, targets, cwd, matcher, p) + if err != nil { + return "", err } return formatGrepResults(results), nil } + var results []grepResult info, err := os.Stat(searchPath) if err != nil { return "", err @@ -371,12 +376,7 @@ func grepFallback(ctx context.Context, p GrepParams, searchPath, cwd string, tar if err != nil { return "", err } - for i := range res { - res[i].relPath, _ = filepath.Rel(cwd, searchPath) - if res[i].relPath == "" || res[i].relPath == "." { - res[i].relPath = filepath.Base(searchPath) - } - } + setGrepResultsRelPath(res, cwd, searchPath) results = append(results, res...) } else if err := grepDirFallback(ctx, searchPath, cwd, p, matcher, &results); err != nil { return "", err @@ -385,11 +385,58 @@ func grepFallback(ctx context.Context, p GrepParams, searchPath, cwd string, tar return formatGrepResults(results), nil } +// grepExplicitTargets searches only the given files (index candidates), +// skipping any that fail to read. +func grepExplicitTargets(ctx context.Context, targets []string, cwd string, matcher func(string) bool, p GrepParams) ([]grepResult, error) { + var results []grepResult + for _, path := range targets { + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } + res, fErr := grepFile(ctx, path, matcher, p.ContextBefore, p.ContextAfter) + if fErr != nil { + continue + } + setGrepResultsRelPath(res, cwd, path) + results = append(results, res...) + } + return results, nil +} + +// setGrepResultsRelPath fills in each result's relPath relative to cwd, +// falling back to the file's base name when the relative path collapses to +// "" or ".". +func setGrepResultsRelPath(res []grepResult, cwd, path string) { + for i := range res { + res[i].relPath, _ = filepath.Rel(cwd, path) + res[i].relPath = filepath.ToSlash(res[i].relPath) + if res[i].relPath == "" || res[i].relPath == "." { + res[i].relPath = filepath.ToSlash(filepath.Base(path)) + } + } +} + // grepDirFallback walks searchPath and appends every readable, non-hidden file // matching the include filter to results. It skips hidden directories and // unreadable files without failing the whole search. func grepDirFallback(ctx context.Context, searchPath, cwd string, p GrepParams, matcher func(string) bool, results *[]grepResult) error { - err := filepath.WalkDir(searchPath, func(walkPath string, d os.DirEntry, err error) error { + visit := grepWalkVisitor(ctx, searchPath, cwd, p, matcher, results) + err := filepath.WalkDir(searchPath, visit) + if err != nil && !errors.Is(err, ctx.Err()) { + return err + } + return nil +} + +// grepWalkVisitor returns the filepath.WalkDir callback for grepDirFallback: +// it skips hidden directories/files, applies the include filter, greps each +// remaining file, and appends its (relPath-normalised) matches to results. +func grepWalkVisitor( + ctx context.Context, searchPath, cwd string, p GrepParams, matcher func(string) bool, results *[]grepResult, +) fs.WalkDirFunc { + return func(walkPath string, d os.DirEntry, err error) error { if err != nil { return nil //nolint:nilerr // intentionally skip inaccessible entries } @@ -424,20 +471,10 @@ func grepDirFallback(ctx context.Context, searchPath, cwd string, p GrepParams, if err != nil { return nil //nolint:nilerr // intentionally skip files we can't read } - for i := range res { - res[i].relPath, _ = filepath.Rel(cwd, walkPath) - res[i].relPath = filepath.ToSlash(res[i].relPath) - if res[i].relPath == "" || res[i].relPath == "." { - res[i].relPath = filepath.ToSlash(filepath.Base(walkPath)) - } - } + setGrepResultsRelPath(res, cwd, walkPath) *results = append(*results, res...) return nil - }) - if err != nil && !errors.Is(err, ctx.Err()) { - return err } - return nil } // buildMatcher returns a line matcher based on grep parameters. diff --git a/internal/tools/builtin/read.go b/internal/tools/builtin/read.go index 1cd2b27a2..94f51788d 100644 --- a/internal/tools/builtin/read.go +++ b/internal/tools/builtin/read.go @@ -4,6 +4,9 @@ // Mutations from upstream: // - package renamed tools -> builtin (archie-core already has an // internal/tools package holding the registry these are registered into). +// - serveReadContent split into readOffsetLimitWindow and +// readTrailingNotice to satisfy golangci-lint's cyclop limit. No +// behavioural change. // // Refresh by diffing against that path at a newer tau commit. Do not // edit without recording the change above. @@ -339,21 +342,11 @@ func serveReadContent(cwd, path string, p readParams, info os.FileInfo, state *r } } - // Apply offset (1-based). - startLine := 1 - if p.Offset > 0 { - startLine = p.Offset - } + startLine, endLine := readOffsetLimitWindow(p, totalLines) if startLine > totalLines { return Result{Content: fmt.Sprintf("offset %d exceeds file length (%d lines)", startLine, totalLines), IsError: true} } - // Apply limit. - endLine := totalLines - if p.Limit > 0 && startLine+p.Limit-1 < endLine { - endLine = startLine + p.Limit - 1 - } - // Skip lines this session has already been shown from an unchanged // file. Repeated reads were 23.6% of all tool result tokens across // analysed sessions - one file was read 58 times in a single session. @@ -382,28 +375,53 @@ func serveReadContent(cwd, path string, p readParams, info os.FileInfo, state *r ) + output } + output += readTrailingNotice(p, tr, lines, startLine, endLine, totalLines) + + return Result{Content: output, Truncated: tr.Truncated || endLine < totalLines, ResultBytes: tr.OriginalSize} +} + +// readOffsetLimitWindow resolves the requested 1-based [startLine, endLine] +// window from the offset/limit parameters and the file's total line count. +func readOffsetLimitWindow(p readParams, totalLines int) (startLine, endLine int) { + startLine = 1 + if p.Offset > 0 { + startLine = p.Offset + } + + endLine = totalLines + if p.Limit > 0 && startLine+p.Limit-1 < endLine { + endLine = startLine + p.Limit - 1 + } + return startLine, endLine +} + +// readTrailingNotice returns the suffix appended to the served content: a +// shell-fallback hint when the first requested line alone exceeded the byte +// limit, a continuation hint when the byte limit truncated mid-range, or a +// continuation hint when a user-specified limit stopped before EOF. Returns +// "" when none apply. +func readTrailingNotice(p readParams, tr TruncationResult, lines []string, startLine, endLine, totalLines int) string { switch { case tr.Truncated && tr.OutputLines == 0: // The first requested line alone exceeds the byte limit. Point the // model at a shell fallback that can slice within the line. lineSize := FormatSize(len(lines[startLine-1])) - output = fmt.Sprintf( + return fmt.Sprintf( "[line %d is %s, exceeding the %s output limit. Use shell: sed -n '%dp' %s | head -c %d]", startLine, lineSize, FormatSize(DefaultMaxBytes), startLine, p.Path, DefaultMaxBytes, ) case tr.Truncated: shownEnd := startLine + tr.OutputLines - 1 - output += fmt.Sprintf( + return fmt.Sprintf( "\n\n[showing lines %d-%d of %d. Use offset=%d to continue.]", startLine, shownEnd, totalLines, shownEnd+1, ) case endLine < totalLines: // A user-specified limit stopped early, but the file has more content. - output += fmt.Sprintf( + return fmt.Sprintf( "\n\n[%d more lines in file. Use offset=%d to continue.]", totalLines-endLine, endLine+1, ) } - - return Result{Content: output, Truncated: tr.Truncated || endLine < totalLines, ResultBytes: tr.OriginalSize} + return "" } diff --git a/internal/tools/builtin/shell.go b/internal/tools/builtin/shell.go index bc35002cb..522b1d42f 100644 --- a/internal/tools/builtin/shell.go +++ b/internal/tools/builtin/shell.go @@ -11,6 +11,9 @@ // cancels the turn context, so without this the command it was aimed // at would keep going. tau applies the same treatment to spawned // agents in agent.go; this extends it to the shell tool. +// - makeShellExecutor split into buildShellCmd, formatShellOutput, and +// shellErrorResult to satisfy golangci-lint's gocognit limit. No +// behavioural change. // // Refresh by diffing against that path at a newer tau commit. Do not // edit without recording the change above. @@ -121,53 +124,10 @@ func makeShellExecutor(cwd string, mq *MutationQueue) Executor { ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() - shell, args := shellCommand(p.Command) - cmd := exec.CommandContext(ctx, shell, args...) - if cwd != "" { - cmd.Dir = cwd - } - - // Run the command as its own process group and, on cancellation, - // signal the group rather than the shell alone. - // - // exec.CommandContext's default kills only the process it started. - // A shell's real work is almost always a child of that shell, so - // the default leaves the actual command running: cancelling - // `go test ./...` reaps the shell and lets the test binary carry - // on, and anything backgrounded with & or nohup is never touched. - // /stop cancels this context, so that gap is the difference - // between stopping a command and only appearing to. - setProcessGroup(cmd) - cmd.Cancel = func() error { return signalProcessGroup(cmd, syscall.SIGKILL) } - // Stop waiting on inherited pipes shortly after the kill. A - // grandchild holding stdout open would otherwise keep Wait - // blocked long after the group was signalled. - cmd.WaitDelay = shellWaitDelay - - var stdout, stderr bytes.Buffer - bw := &bridgeWriter{bridge: bridge} - cmd.Stdout = io.MultiWriter(&stdout, bw) - cmd.Stderr = io.MultiWriter(&stderr, bw) - + cmd, stdout, stderr := buildShellCmd(ctx, cwd, p.Command, bridge) err := cmd.Run() - var b strings.Builder - if stdout.Len() > 0 { - b.WriteString(stdout.String()) - } - if stderr.Len() > 0 { - if b.Len() > 0 { - b.WriteString("\n") - } - b.WriteString("[stderr]\n") - b.WriteString(stderr.String()) - } - - output := b.String() - if output == "" { - output = "(no output)" - } - + output := formatShellOutput(stdout, stderr) // Collapse before truncation, so a long passing test run is reduced to // its summaries rather than tail-truncated into a partial log. output, _ = collapseGoTestOutput(p.Command, output, err == nil) @@ -183,31 +143,83 @@ func makeShellExecutor(cwd string, mq *MutationQueue) Executor { } if err != nil { - // Check context cancellation first - a process may exit with - // a non-zero code after the deadline, and we want to report - // timeout rather than a misleading exit code. - if ctx.Err() != nil { - return Result{ - Content: fmt.Sprintf("[timeout after: %s]\n%s", timeout, content), IsError: true, - ErrorKind: "timeout", Truncated: tr.Truncated, ResultBytes: len(output), - }, nil - } - - exitErr := &exec.ExitError{} - if errors.As(err, &exitErr) { - return Result{ - Content: fmt.Sprintf("[exit code: %d]\n%s", exitErr.ExitCode(), content), IsError: true, - ErrorKind: "command_exit", Truncated: tr.Truncated, ResultBytes: len(output), - }, nil - } - - return Result{Content: fmt.Sprintf("error executing command: %v", err), IsError: true}, nil + return shellErrorResult(ctx, err, timeout, content, tr, output), nil } return Result{Content: content, Truncated: tr.Truncated, ResultBytes: len(output)}, nil } } +// buildShellCmd constructs the shell command to run: its own process group, +// killed as a group on cancellation (see the package-level comment for why), +// with stdout/stderr tee'd to both an in-memory buffer and the UI bridge. +func buildShellCmd(ctx context.Context, cwd, command string, bridge UIBridge) (cmd *exec.Cmd, stdout, stderr *bytes.Buffer) { + shell, args := shellCommand(command) + cmd = exec.CommandContext(ctx, shell, args...) + if cwd != "" { + cmd.Dir = cwd + } + + setProcessGroup(cmd) + cmd.Cancel = func() error { return signalProcessGroup(cmd, syscall.SIGKILL) } + // Stop waiting on inherited pipes shortly after the kill. A + // grandchild holding stdout open would otherwise keep Wait + // blocked long after the group was signalled. + cmd.WaitDelay = shellWaitDelay + + stdout, stderr = &bytes.Buffer{}, &bytes.Buffer{} + bw := &bridgeWriter{bridge: bridge} + cmd.Stdout = io.MultiWriter(stdout, bw) + cmd.Stderr = io.MultiWriter(stderr, bw) + return cmd, stdout, stderr +} + +// formatShellOutput joins stdout and stderr, labelling stderr when both are +// present, and substitutes a placeholder when the command produced nothing. +func formatShellOutput(stdout, stderr *bytes.Buffer) string { + var b strings.Builder + if stdout.Len() > 0 { + b.WriteString(stdout.String()) + } + if stderr.Len() > 0 { + if b.Len() > 0 { + b.WriteString("\n") + } + b.WriteString("[stderr]\n") + b.WriteString(stderr.String()) + } + + output := b.String() + if output == "" { + output = "(no output)" + } + return output +} + +// shellErrorResult classifies a command's run error into a timeout, a +// nonzero exit, or a raw execution failure, and builds the matching Result. +func shellErrorResult(ctx context.Context, err error, timeout time.Duration, content string, tr TruncationResult, output string) Result { + // Check context cancellation first - a process may exit with + // a non-zero code after the deadline, and we want to report + // timeout rather than a misleading exit code. + if ctx.Err() != nil { + return Result{ + Content: fmt.Sprintf("[timeout after: %s]\n%s", timeout, content), IsError: true, + ErrorKind: "timeout", Truncated: tr.Truncated, ResultBytes: len(output), + } + } + + exitErr := &exec.ExitError{} + if errors.As(err, &exitErr) { + return Result{ + Content: fmt.Sprintf("[exit code: %d]\n%s", exitErr.ExitCode(), content), IsError: true, + ErrorKind: "command_exit", Truncated: tr.Truncated, ResultBytes: len(output), + } + } + + return Result{Content: fmt.Sprintf("error executing command: %v", err), IsError: true} +} + // goTestSummaryRe matches cmd/go's per-package result lines: "ok", "FAIL" and // "?" (no test files). Note it must NOT match "---", which prefixes per-test // result lines ("--- PASS: TestX") - exactly the noise being collapsed. A diff --git a/internal/webui/api_chat.go b/internal/webui/api_chat.go index a9e595a4c..060f3a6c8 100644 --- a/internal/webui/api_chat.go +++ b/internal/webui/api_chat.go @@ -418,50 +418,11 @@ func (s *Server) handleChatStream(w http.ResponseWriter, r *http.Request) { }, }) } else { - type turnResult struct { - reply string - err error + var cancelled bool + reply, err, cancelled = s.runQueuedChatTurn(r, chat, sessionID, msg, showToolCalls, writeChatEvent) + if cancelled { + return } - // One channel carries both text and tool events, because the - // order between them is the point: a tool line that overtakes - // the sentence it interrupted describes a turn that never - // happened. - events := make(chan chatStreamEvent, 32) - result := make(chan turnResult, 1) - chat.Turns.Submit(r.Context(), sessionID, func(turnCtx context.Context) { - turnReply, turnErr := chat.Router.RouteStream(turnCtx, msg, chatStreamSink{ - showToolCalls: showToolCalls, - write: func(event chatStreamEvent) { - select { - case events <- event: - case <-r.Context().Done(): - } - }, - }) - result <- turnResult{reply: turnReply, err: turnErr} - }) - for { - select { - case event := <-events: - writeChatEvent(event, sessionID) - case turn := <-result: - for { - select { - case event := <-events: - writeChatEvent(event, sessionID) - default: - goto drained - } - } - drained: - reply, err = turn.reply, turn.err - goto turnDone - case <-r.Context().Done(): - chat.Turns.Stop(sessionID) - return - } - } - turnDone: } if err != nil { writeChatEvent(chatStreamEvent{Type: "error", Text: err.Error()}, sessionID) @@ -473,6 +434,55 @@ func (s *Server) handleChatStream(w http.ResponseWriter, r *http.Request) { writeChatEvent(chatStreamEvent{Type: "done", Text: reply}, sessionID) } +// runQueuedChatTurn submits msg to the session's turn queue and streams its +// text/tool events as they arrive, in order, via writeChatEvent. One channel +// carries both text and tool events, because the order between them is the +// point: a tool line that overtakes the sentence it interrupted describes a +// turn that never happened. Returns cancelled=true when the request context +// was cancelled before the turn finished, in which case the caller must not +// write any further response. +func (s *Server) runQueuedChatTurn( + r *http.Request, chat *ChatService, sessionID string, msg gateway.Message, showToolCalls bool, + writeChatEvent func(chatStreamEvent, string), +) (reply string, err error, cancelled bool) { + type turnResult struct { + reply string + err error + } + events := make(chan chatStreamEvent, 32) + result := make(chan turnResult, 1) + chat.Turns.Submit(r.Context(), sessionID, func(turnCtx context.Context) { + turnReply, turnErr := chat.Router.RouteStream(turnCtx, msg, chatStreamSink{ + showToolCalls: showToolCalls, + write: func(event chatStreamEvent) { + select { + case events <- event: + case <-r.Context().Done(): + } + }, + }) + result <- turnResult{reply: turnReply, err: turnErr} + }) + for { + select { + case event := <-events: + writeChatEvent(event, sessionID) + case turn := <-result: + for { + select { + case event := <-events: + writeChatEvent(event, sessionID) + default: + return turn.reply, turn.err, false + } + } + case <-r.Context().Done(): + chat.Turns.Stop(sessionID) + return "", nil, true + } + } +} + func (s *Server) handleChatCancel(w http.ResponseWriter, r *http.Request) { chat, ok := s.chatReady(w) if !ok {