refactor(tools): delete unreachable dispatch/gating engine

DispatchSequential, DispatchConcurrent, ClassifyEntry and their
supporting types (dispatch.go) have zero non-test callers; the live
tool path is agentexec/toolset.go's BuildToolSetFrom. Removes the
dead engine and its tests, and updates the PRD status table that
referenced it (tools-core-1).
This commit is contained in:
sam committed 2026-09-15 10:19:39 +10:00
1 parent 72d9125bba
commit 766c09d477
4 files changed
+1 -1081

No files matched your search

+1 -1
View File
@@ -20,7 +20,7 @@ Every claim in this document is mapped to what exists in the tree today.
| Subagent used in production | **Implemented, one consumer** | `internal/app/agentworker/review.go:112` — the adversarial reviewer. That is the only non-test use of `Subagent` in the repo |
| A **delegate/subagent tool** on the chat surface | **Aspirational — zero code** | No `subagent`/`delegate` tool name is registered anywhere |
| **Fan-out orchestration** (N missions, one collator) | **Aspirational — zero code** | `agentloop`'s package doc states it runs *one* mission: "orchestration across missions lives with the caller". No caller does this yet |
| Parallel-safe tool execution | **Implemented, no production caller** | `internal/tools/dispatch.go` `ExecutionClass` (`ExecParallelSafe` auto-derived from `ClassIdempotent`; `ClassRead` does not exist). `DispatchSequential`/`DispatchConcurrent`/`ClassifyEntry` have **zero non-test callers**; live concurrency is ai-sdk's `GenerateOptions.MaxParallelToolCalls`, which archie-core never sets |
| Parallel-safe tool execution | **Aspirational — zero code** | The unreachable `internal/tools/dispatch.go` dispatch/gating engine (`ExecutionClass`, `DispatchSequential`/`DispatchConcurrent`/`ClassifyEntry`) was deleted as dead code (tools-core-1); live concurrency is ai-sdk's `GenerateOptions.MaxParallelToolCalls`, which archie-core never sets |
| Per-result caps + spill-to-disk | **Implemented** | `internal/agentexec/toolset.go` `ToolLimits{MaxResultChars,SpillDir}`, applied in `toolExecute` via `tools.CapPayload` (`limit.go:44`); no **aggregate** turn cap by design (`toolset.go:62`). `config.ToolPolicy.SpillDir`; default cap 50k chars, spill dir deliberately not defaulted |
| Tool guardrails (repeat-failure / no-progress) | **Implemented, not on the call path** | `internal/tools/guardrail.go` engine exists. Its only non-test caller is `dispatchOne` — which is itself unreachable — so **the chat path never consults it**, and the agent path only records per *stage* (`domain/workflow/agent.go:199`) |
| Structured capture tools (model reports into a typed store, not prose) | **Implemented** | `internal/app/agentworker/review.go:247` `record_finding` / `record_checked` — `core.NewTypedTool` appending to a caller-owned slice. This is the pattern a research source ledger should reuse |
-31
View File
@@ -114,37 +114,6 @@ func TestToolClassificationString(t *testing.T) {
}
}
func TestClassifyEntry(t *testing.T) {
t.Run("idempotent tools are parallel-safe", func(t *testing.T) {
e := ToolEntry{
Name: "reader",
Handler: noopHandler,
Classification: ClassIdempotent,
}
if got := ClassifyEntry(e); got != ExecParallelSafe {
t.Errorf("expected parallel-safe, got %v", got)
}
})
t.Run("mutating tools are never-parallel", func(t *testing.T) {
e := ToolEntry{
Name: "writer",
Handler: noopHandler,
Classification: ClassMutating,
}
if got := ClassifyEntry(e); got != ExecNeverParallel {
t.Errorf("expected never-parallel, got %v", got)
}
})
t.Run("unclassified tools default to never-parallel", func(t *testing.T) {
e := ToolEntry{Name: "unknown", Handler: noopHandler}
if got := ClassifyEntry(e); got != ExecNeverParallel {
t.Errorf("expected never-parallel, got %v", got)
}
})
}
func TestClassificationJSONRoundTrip(t *testing.T) {
e := ToolEntry{
Name: "classified",
-404
View File
@@ -1,404 +0,0 @@
package tools
import (
"context"
"fmt"
"sync"
"time"
)
// ExecutionClass defines how a tool interacts with the dispatch gating engine.
type ExecutionClass int
const (
// ExecNeverParallel marks tools that must run alone -- typically state
// mutators where concurrent execution could cause corruption.
ExecNeverParallel ExecutionClass = iota
// ExecParallelSafe marks tools that can run concurrently with any
// other tool. Read-only tools (idempotent classification) are
// automatically promoted to this class.
ExecParallelSafe
// ExecPathScoped marks tools that can run in parallel with tools
// targeting different paths/scopes, but must serialize with tools
// targeting the same path.
ExecPathScoped
)
// String returns a human-readable representation of the execution class.
func (c ExecutionClass) String() string {
switch c {
case ExecNeverParallel:
return "never-parallel"
case ExecParallelSafe:
return "parallel-safe"
case ExecPathScoped:
return "path-scoped"
default:
return "unknown"
}
}
// ClassifyEntry derives the [ExecutionClass] from a [ToolEntry]'s
// classification flags. Idempotent tools are parallel-safe. Mutating
// tools are never-parallel unless explicitly marked otherwise.
// Unclassified tools default to never-parallel (conservative).
func ClassifyEntry(e ToolEntry) ExecutionClass {
if e.Classification.IsIdempotent() {
return ExecParallelSafe
}
if e.Classification.IsMutating() {
return ExecNeverParallel
}
// Conservative default: treat unclassified tools as never-parallel.
return ExecNeverParallel
}
// ---------------------------------------------------------------------------
// Dispatch types
// ---------------------------------------------------------------------------
// DispatchResult holds the outcome of a single tool invocation.
type DispatchResult struct {
// ToolName is the name of the tool that was invoked.
ToolName string
// Output is the value returned by the tool's Handler. nil when
// IsError is true.
Output any
// Preview is a truncated version of the output suitable for
// inclusion in the conversation context. It is at most 500 chars.
Preview string
// Error is the error returned by the handler or dispatch layer.
Error error
// Duration is the wall-clock time spent executing the tool
// (handler execution only; does not include spill time).
Duration time.Duration
}
// IsError reports whether the result represents a failed invocation.
func (r DispatchResult) IsError() bool { return r.Error != nil }
// previewChars is the maximum length of a result preview.
const previewChars = 500
// truncatePreview returns a short summary of the output suitable for
// the conversation context.
func truncatePreview(output any) string {
if output == nil {
return ""
}
s := fmt.Sprint(output)
if len(s) <= previewChars {
return s
}
return s[:previewChars] + "... [truncated]"
}
// ---------------------------------------------------------------------------
// Tool invocation
// ---------------------------------------------------------------------------
// ToolCall represents a single pending tool invocation.
type ToolCall struct {
// Entry is the tool to invoke. Handler must be non-nil.
Entry ToolEntry
// Input is the deserialized JSON input to pass to the handler.
Input map[string]any
// PathScope is an optional key used by ExecPathScoped gating.
// Tools with the same non-empty PathScope are serialized.
PathScope string
}
// invokeTool executes a single tool and returns a [DispatchResult].
//
// Tools that require human approval are refused. The dispatch path has no
// ApprovalRequester — it is used by the agent execution loop, which runs
// without a human in the loop. The chat path gates approval through
// BuildToolSetFrom → toolExecute, which has the approver wired.
func invokeTool(ctx context.Context, call ToolCall) DispatchResult {
start := time.Now()
result := DispatchResult{ToolName: call.Entry.Name}
if call.Entry.Classification.IsApprovalRequired() {
result.Error = fmt.Errorf("tool %s: requires human approval and cannot be invoked through the agent dispatch path", call.Entry.Name)
return result
}
output, err := call.Entry.Handler(ctx, call.Input)
result.Duration = time.Since(start)
if err != nil {
result.Error = err
return result
}
result.Output = output
result.Preview = truncatePreview(output)
return result
}
// ---------------------------------------------------------------------------
// 17.5 -- Sequential dispatch
// ---------------------------------------------------------------------------
// DispatchSequential invokes tools one at a time, capturing per-result
// previews and classifying errors. Each result includes a truncated
// preview for inclusion in the conversation context.
//
// spillDir controls displacement of individual oversized results. Aggregate
// output volume never stops dispatch.
//
// If guardrail is non-nil, failures are recorded and the dispatch may
// abort early on a hard-stop decision.
func DispatchSequential(
ctx context.Context,
calls []ToolCall,
spillDir string,
guardrail *GuardrailEngine,
) []DispatchResult {
results := make([]DispatchResult, 0, len(calls))
for _, call := range calls {
// Check for hard-stop before dispatching.
if guardrail != nil && guardrail.HardStopped() {
results = append(results, DispatchResult{
ToolName: call.Entry.Name,
Error: fmt.Errorf("guardrail hard-stop active"),
})
break
}
result := dispatchOne(ctx, call, spillDir, guardrail)
results = append(results, result)
// Abort on hard-stop.
if guardrail != nil && guardrail.HardStopped() {
break
}
}
return results
}
// ---------------------------------------------------------------------------
// 17.6 -- Concurrent dispatch
// ---------------------------------------------------------------------------
// gatedGroup holds a set of calls that must run sequentially within the
// group but can run concurrently with other groups.
type gatedGroup struct {
calls []ToolCall
class ExecutionClass
scope string // non-empty only for ExecPathScoped
}
// DispatchConcurrent invokes tools concurrently where safe, applying
// execution gating:
//
// - ExecNeverParallel: runs alone, blocking all other execution.
// - ExecParallelSafe: runs concurrently with any other tool.
// - ExecPathScoped: same-path tools serialize, different paths run
// in parallel.
//
// Results are returned in the same order as calls.
func DispatchConcurrent(
ctx context.Context,
calls []ToolCall,
spillDir string,
guardrail *GuardrailEngine,
) []DispatchResult {
if len(calls) == 0 {
return nil
}
// Classify each call and build gating groups.
groups := buildGatingGroups(calls)
// Execute groups: never-parallel groups block all concurrency.
// Others run in parallel within their group, sequentially between groups.
results := make([]DispatchResult, len(calls))
resultIdx := 0
for _, group := range groups {
if group.class == ExecNeverParallel || len(group.calls) == 1 {
// Run sequentially.
for _, call := range group.calls {
results[resultIdx] = dispatchOne(ctx, call, spillDir, guardrail)
resultIdx++
}
} else {
// Run in parallel within group.
res := dispatchParallel(ctx, group.calls, spillDir, guardrail)
for _, r := range res {
results[resultIdx] = r
resultIdx++
}
}
}
return results
}
// buildGatingGroups partitions calls into groups based on their execution
// class. Never-parallel calls get isolated groups. Path-scoped calls share
// a group if they have the same path; otherwise they get separate groups.
// Parallel-safe calls all share one group.
//
// Groups are tracked by index (not pointer) to avoid the value-copy bug
// where appending to a map-stored pointer was invisible to the slice copy.
func buildGatingGroups(calls []ToolCall) []gatedGroup {
if len(calls) == 0 {
return nil
}
var groups []gatedGroup
// Track path-scoped groups by scope key to index in the groups slice.
pathScopedIdx := make(map[string]int)
for _, call := range calls {
class := ClassifyEntry(call.Entry)
switch class {
case ExecNeverParallel:
// Isolate: one group per never-parallel call.
groups = append(groups, gatedGroup{
calls: []ToolCall{call},
class: ExecNeverParallel,
})
case ExecPathScoped:
scope := call.PathScope
if scope == "" {
// No scope specified -- treat as never-parallel.
groups = append(groups, gatedGroup{
calls: []ToolCall{call},
class: ExecNeverParallel,
})
continue
}
if idx, ok := pathScopedIdx[scope]; ok {
// Update existing group in-place through the index.
groups[idx].calls = append(groups[idx].calls, call)
} else {
idx := len(groups)
groups = append(groups, gatedGroup{
calls: []ToolCall{call},
class: ExecPathScoped,
scope: scope,
})
pathScopedIdx[scope] = idx
}
case ExecParallelSafe:
// Batch all parallel-safe calls into one group.
if idx, ok := firstParallelSafeIdx(groups); ok {
groups[idx].calls = append(groups[idx].calls, call)
} else {
groups = append(groups, gatedGroup{
calls: []ToolCall{call},
class: ExecParallelSafe,
})
}
}
}
return groups
}
// firstParallelSafeIdx returns the index of the first parallel-safe group
// in the slice. Used by buildGatingGroups to batch parallel-safe calls.
func firstParallelSafeIdx(groups []gatedGroup) (int, bool) {
for i := range groups {
if groups[i].class == ExecParallelSafe {
return i, true
}
}
return 0, false
}
// dispatchOne runs a single call with guardrail and per-tool limit checks.
//
// Callers that do not own the batch cannot use this: it caps
// DispatchResult.Preview, whereas a caller handing the result straight to a
// model has to cap the payload it actually sends. [CapPayload] is the shared
// rule between the two; this sequence is specific to batch dispatch.
func dispatchOne(
ctx context.Context,
call ToolCall,
spillDir string,
guardrail *GuardrailEngine,
) DispatchResult {
if guardrail != nil && guardrail.HardStopped() {
return DispatchResult{
ToolName: call.Entry.Name,
Error: fmt.Errorf("guardrail hard-stop active"),
}
}
result := invokeTool(ctx, call)
// Enforce per-tool result size limit (17.4).
enforcePerToolLimit(&result, call.Entry, spillDir)
// Track guardrail.
if guardrail != nil {
if result.IsError() {
_ = guardrail.RecordFailure(call.Entry.Name, result.Error)
} else {
_ = guardrail.RecordSuccess(call.Entry.Name)
}
}
return result
}
// enforcePerToolLimit replaces the preview when the result exceeds the tool's
// MaxResultSizeChars, spilling to disk when a directory is configured and
// truncating inline otherwise. [CapPayload] owns that rule; this only decides
// whether it applies.
//
// Returns whether the result was capped.
func enforcePerToolLimit(result *DispatchResult, entry ToolEntry, spillDir string) bool {
if result.IsError() || entry.MaxResultSizeChars <= 0 || result.Output == nil {
return false
}
fullOutput := fmt.Sprint(result.Output)
capped := CapPayload(entry.Name, fullOutput, entry.MaxResultSizeChars, spillDir)
if capped == fullOutput {
return false
}
result.Preview = capped
return true
}
// dispatchParallel runs multiple calls concurrently and returns results
// in the same order.
func dispatchParallel(
ctx context.Context,
calls []ToolCall,
spillDir string,
guardrail *GuardrailEngine,
) []DispatchResult {
results := make([]DispatchResult, len(calls))
var wg sync.WaitGroup
wg.Add(len(calls))
for i, call := range calls {
go func(idx int, c ToolCall) {
defer wg.Done()
results[idx] = dispatchOne(ctx, c, spillDir, guardrail)
}(i, call)
}
wg.Wait()
return results
}
-645
View File
@@ -1,645 +0,0 @@
package tools
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
)
// maxTruncationNotice bounds the explanatory notice CapPayload appends when it
// truncates inline. Tests assert the capped payload plus this allowance, so a
// notice that grew without bound would fail rather than quietly eat context.
const maxTruncationNotice = 128
func TestExecutionClassString(t *testing.T) {
tests := []struct {
c ExecutionClass
want string
}{
{ExecNeverParallel, "never-parallel"},
{ExecParallelSafe, "parallel-safe"},
{ExecPathScoped, "path-scoped"},
{ExecutionClass(99), "unknown"},
}
for _, tt := range tests {
if got := tt.c.String(); got != tt.want {
t.Errorf("String() = %q, want %q", got, tt.want)
}
}
}
func TestTruncatePreview(t *testing.T) {
t.Run("nil output", func(t *testing.T) {
if got := truncatePreview(nil); got != "" {
t.Errorf("expected empty string, got %q", got)
}
})
t.Run("short output passthrough", func(t *testing.T) {
s := "short result"
if got := truncatePreview(s); got != s {
t.Errorf("expected %q, got %q", s, got)
}
})
t.Run("long output truncated", func(t *testing.T) {
long := strings.Repeat("x", previewChars+100)
got := truncatePreview(long)
if len(got) > previewChars+len("... [truncated]") {
t.Errorf("preview too long: %d chars", len(got))
}
if !strings.HasSuffix(got, "... [truncated]") {
t.Error("truncated preview should end with '... [truncated]'")
}
})
}
func TestDispatchResultIsError(t *testing.T) {
r := DispatchResult{}
if r.IsError() {
t.Error("zero result should not be an error")
}
r.Error = errors.New("fail")
if !r.IsError() {
t.Error("result with error should be an error")
}
}
func TestInvokeTool(t *testing.T) {
e := ToolEntry{
Name: "echo",
Handler: echoHandler,
}
result := invokeTool(context.Background(), ToolCall{
Entry: e,
Input: map[string]any{"msg": "hello"},
})
if result.IsError() {
t.Fatalf("unexpected error: %v", result.Error)
}
if result.ToolName != "echo" {
t.Errorf("ToolName = %q, want 'echo'", result.ToolName)
}
if result.Duration <= 0 {
t.Error("duration should be non-zero")
}
if result.Preview == "" {
t.Error("preview should not be empty")
}
}
func TestInvokeToolError(t *testing.T) {
e := ToolEntry{
Name: "failing",
Handler: func(_ context.Context, _ map[string]any) (any, error) {
return nil, errors.New("simulated failure")
},
}
result := invokeTool(context.Background(), ToolCall{Entry: e})
if !result.IsError() {
t.Error("expected error")
}
if result.Preview != "" {
t.Errorf("preview should be empty on error, got %q", result.Preview)
}
}
// --- Sequential dispatch ---
func TestDispatchSequentialEmpty(t *testing.T) {
results := DispatchSequential(context.Background(), nil, "", nil)
if len(results) != 0 {
t.Errorf("expected 0 results, got %d", len(results))
}
}
func TestDispatchSequentialBasic(t *testing.T) {
e := ToolEntry{
Name: "echo",
Handler: echoHandler,
}
calls := []ToolCall{
{Entry: e, Input: map[string]any{"msg": "first"}},
{Entry: e, Input: map[string]any{"msg": "second"}},
}
results := DispatchSequential(context.Background(), calls, "", nil)
if len(results) != 2 {
t.Fatalf("expected 2 results, got %d", len(results))
}
for i, r := range results {
if r.IsError() {
t.Errorf("result %d error: %v", i, r.Error)
}
if r.Preview == "" {
t.Errorf("result %d has empty preview", i)
}
}
}
func TestDispatchSequentialOutputVolumeDoesNotStop(t *testing.T) {
e := ToolEntry{
Name: "big-echo",
Handler: echoHandler,
}
calls := []ToolCall{
{Entry: e, Input: map[string]any{"msg": strings.Repeat("x", 100)}},
{Entry: e, Input: map[string]any{"msg": strings.Repeat("y", 100)}},
}
spillDir := "" // Aggregate output volume never stops dispatch.
results := DispatchSequential(context.Background(), calls, spillDir, nil)
if len(results) != 2 {
t.Fatalf("expected 2 results, got %d", len(results))
}
for i, result := range results {
if result.IsError() {
t.Errorf("result %d failed after accumulated output: %v", i, result.Error)
}
}
}
func TestDispatchSequentialGuardrailHardStop(t *testing.T) {
// First failure warns; second warn hard-stops.
config := ToolCallGuardrailConfig{
ExactFailureWarnAfter: 1,
HardStopAfterWarnRepeat: 2,
}
g := NewGuardrailEngine(config)
e := ToolEntry{
Name: "failing",
Handler: func(_ context.Context, _ map[string]any) (any, error) {
return nil, errors.New("boom")
},
}
calls := []ToolCall{
{Entry: e}, {Entry: e}, {Entry: e},
}
results := DispatchSequential(context.Background(), calls, "", g)
// First call: warn, second call: hard-stop → abort.
// Should stop before completing all 3.
if len(results) < 2 {
t.Fatalf("expected at least 2 results, got %d", len(results))
}
}
// --- Concurrent dispatch ---
func TestDispatchConcurrentEmpty(t *testing.T) {
results := DispatchConcurrent(context.Background(), nil, "", nil)
if len(results) != 0 {
t.Errorf("expected 0 results, got %d", len(results))
}
}
func TestDispatchConcurrentParallel(t *testing.T) {
var order []string
var counter atomic.Int32
makeTool := func(name string) ToolEntry {
return ToolEntry{
Name: name,
Handler: noopHandler,
Classification: ClassIdempotent, // parallel-safe
}
}
calls := []ToolCall{
{Entry: makeTool("a")},
{Entry: makeTool("b")},
{Entry: makeTool("c")},
{Entry: makeTool("d")},
}
results := DispatchConcurrent(context.Background(), calls, "", nil)
if len(results) != 4 {
t.Fatalf("expected 4 results, got %d", len(results))
}
for _, r := range results {
if r.IsError() {
t.Errorf("unexpected error for %s: %v", r.ToolName, r.Error)
}
}
// Sanity: all tools ran.
if counter.Load() < 0 {
t.Error("counter should not be negative")
}
_ = order
}
func TestDispatchConcurrentNeverParallelIsolated(t *testing.T) {
// Mutating tools should run sequentially (never-parallel).
var running atomic.Int32
var maxConcurrent atomic.Int32
makeMutating := func(name string) ToolEntry {
return ToolEntry{
Name: name,
Classification: ClassMutating,
Handler: func(_ context.Context, _ map[string]any) (any, error) {
n := running.Add(1)
if n > maxConcurrent.Load() {
maxConcurrent.Store(n)
}
time.Sleep(10 * time.Millisecond)
running.Add(-1)
return nil, nil
},
}
}
calls := []ToolCall{
{Entry: makeMutating("m1")},
{Entry: makeMutating("m2")},
}
results := DispatchConcurrent(context.Background(), calls, "", nil)
if len(results) != 2 {
t.Fatalf("expected 2 results, got %d", len(results))
}
// Mutating tools are never-parallel → each in its own group → serial.
// Maximum concurrent should be 1.
if maxConcurrent.Load() > 1 {
t.Errorf("mutating tools should be serial; max concurrent was %d", maxConcurrent.Load())
}
}
func TestDispatchConcurrentPathScoped(t *testing.T) {
// Path-scoped tools with the same path serialize; different paths parallelize.
var running atomic.Int32
var maxConcurrent atomic.Int32
makePathScoped := func(name, _ string) ToolEntry {
return ToolEntry{
Name: name,
Classification: ClassIdempotent, // idempotent but we override via PathScope
Handler: func(_ context.Context, _ map[string]any) (any, error) {
n := running.Add(1)
if n > maxConcurrent.Load() {
maxConcurrent.Store(n)
}
time.Sleep(10 * time.Millisecond)
running.Add(-1)
return nil, nil
},
}
}
calls := []ToolCall{
{Entry: makePathScoped("a", "path-1")},
{Entry: makePathScoped("b", "path-2")}, // Different path → can run concurrently with "a".
}
results := DispatchConcurrent(context.Background(), calls, "", nil)
if len(results) != 2 {
t.Fatalf("expected 2 results, got %d", len(results))
}
// Both are idempotent, so they're parallel-safe (ExecParallelSafe), not path-scoped.
// ClassifyEntry maps idempotent → ExecParallelSafe regardless of PathScope.
// PathScope only matters when the tool is classified as ExecPathScoped,
// which currently requires explicit classification that doesn't exist yet.
// So they run in parallel.
if maxConcurrent.Load() < 1 {
t.Error("tools should have run")
}
}
// --- buildGatingGroups ---
func TestBuildGatingGroupsEmpty(t *testing.T) {
groups := buildGatingGroups(nil)
if len(groups) != 0 {
t.Errorf("expected 0 groups, got %d", len(groups))
}
}
func TestBuildGatingGroupsIsolatesNeverParallel(t *testing.T) {
e := ToolEntry{
Name: "mutator",
Handler: noopHandler,
Classification: ClassMutating,
}
calls := []ToolCall{
{Entry: e},
{Entry: e},
{Entry: e},
}
groups := buildGatingGroups(calls)
// Each should be in its own group.
if len(groups) != 3 {
t.Errorf("expected 3 isolated groups, got %d", len(groups))
}
for _, g := range groups {
if g.class != ExecNeverParallel {
t.Errorf("expected never-parallel group, got %v", g.class)
}
if len(g.calls) != 1 {
t.Errorf("expected 1 call per group, got %d", len(g.calls))
}
}
}
func TestBuildGatingGroupsBatchesParallelSafe(t *testing.T) {
e := ToolEntry{
Name: "reader",
Handler: noopHandler,
Classification: ClassIdempotent,
}
calls := []ToolCall{
{Entry: e}, {Entry: e}, {Entry: e},
}
groups := buildGatingGroups(calls)
// All parallel-safe → one group.
if len(groups) != 1 {
t.Errorf("expected 1 group, got %d", len(groups))
}
if groups[0].class != ExecParallelSafe {
t.Errorf("expected parallel-safe group, got %v", groups[0].class)
}
if len(groups[0].calls) != 3 {
t.Errorf("expected 3 calls in group, got %d", len(groups[0].calls))
}
}
func TestDispatchSequentialErrorCapture(t *testing.T) {
e := ToolEntry{
Name: "failing",
Handler: func(_ context.Context, _ map[string]any) (any, error) {
return nil, fmt.Errorf("something went wrong")
},
}
results := DispatchSequential(context.Background(), []ToolCall{{Entry: e}}, "", nil)
if len(results) != 1 {
t.Fatalf("expected 1 result, got %d", len(results))
}
if !results[0].IsError() {
t.Error("expected error result")
}
if results[0].Error.Error() != "something went wrong" {
t.Errorf("error = %v, want 'something went wrong'", results[0].Error)
}
}
func TestDispatchPerToolResultSizeLimit(t *testing.T) {
// 17.4 -- Per-tool max_result_size_chars enforcement.
dir := t.TempDir()
spillDir := dir
e := ToolEntry{
Name: "big-output",
Handler: echoHandler,
MaxResultSizeChars: 50,
}
results := DispatchSequential(context.Background(), []ToolCall{
{Entry: e, Input: map[string]any{"msg": strings.Repeat("x", 200)}},
}, spillDir, nil)
if len(results) != 1 {
t.Fatalf("expected 1 result, got %d", len(results))
}
if !results[0].IsError() && !strings.Contains(results[0].Preview, "spilled") &&
!strings.Contains(results[0].Preview, "truncated") {
t.Errorf("expected spill or truncation marker, got: %s", results[0].Preview)
}
}
func TestDispatchPerToolLimitNoSpillDir(t *testing.T) {
e := ToolEntry{
Name: "big-output",
Handler: echoHandler,
MaxResultSizeChars: 50,
}
results := DispatchSequential(context.Background(), []ToolCall{
{Entry: e, Input: map[string]any{"msg": strings.Repeat("x", 200)}},
}, "", nil)
if len(results) != 1 {
t.Fatalf("expected 1 result, got %d", len(results))
}
if !strings.Contains(results[0].Preview, "truncated") {
t.Errorf("expected truncation marker, got: %s", results[0].Preview)
}
}
// --- Regression tests for adversarial review findings ---
func TestBuildGatingGroupsSameScopePathScoped(t *testing.T) {
// Regression for: value-copy bug in buildGatingGroups silently dropped
// path-scoped calls with the same scope.
//
// Two calls with the same PathScope must end up in the same gatedGroup.
// Before fix: the second call was silently dropped because a value copy
// of the group was stored in the slice, while the map-stored pointer
// was appended to but invisible to the slice.
makePathScoped := func(name, _ string) ToolEntry {
return ToolEntry{
Name: name,
Handler: noopHandler,
Classification: ClassMutating, // ClassifyEntry → ExecNeverParallel, but we
}
}
// We need a classification that routes to ExecPathScoped. Since
// ClassifyEntry doesn't produce ExecPathScoped from any existing flag,
// test buildGatingGroups directly with a manually-constructed ToolCall.
calls := []ToolCall{
{Entry: makePathScoped("a", "/tmp"), PathScope: "/tmp"},
{Entry: makePathScoped("b", "/tmp"), PathScope: "/tmp"},
{Entry: makePathScoped("c", "/var"), PathScope: "/var"},
}
// Override ClassifyEntry for this test: force ExecPathScoped.
// We do this by testing buildGatingGroups with calls that have
// entries we know will route correctly. Since no existing flag
// routes to ExecPathScoped, we test the group-building logic
// indirectly through a helper that uses the same index-based
// pattern as the fix.
// Direct test: verify buildGatingGroups with ExecNeverParallel
// (mutating tools route here) — each call gets its own group.
groups := buildGatingGroups(calls)
neverParallelCount := 0
for _, g := range groups {
if g.class == ExecNeverParallel {
neverParallelCount += len(g.calls)
}
}
if neverParallelCount != 3 {
t.Errorf("expected 3 never-parallel calls, got %d", neverParallelCount)
}
}
func TestBuildGatingGroupsIndexTracking(t *testing.T) {
// Regression for: index-based tracking ensures same-scope calls
// are grouped together (not silently dropped via value-copy bug).
//
// We test the firstParallelSafeIdx helper directly.
groups := []gatedGroup{
{class: ExecNeverParallel, calls: []ToolCall{{}}},
{class: ExecParallelSafe, calls: []ToolCall{{}}},
{class: ExecNeverParallel, calls: []ToolCall{{}}},
}
idx, ok := firstParallelSafeIdx(groups)
if !ok {
t.Fatal("expected to find a parallel-safe group")
}
if idx != 1 {
t.Errorf("expected index 1, got %d", idx)
}
// No parallel-safe group.
groups2 := []gatedGroup{
{class: ExecNeverParallel, calls: []ToolCall{{}}},
}
_, ok = firstParallelSafeIdx(groups2)
if ok {
t.Error("expected false when no parallel-safe group exists")
}
}
func TestDispatchPerToolLimitSpillFailureFallback(t *testing.T) {
// Regression: when per-tool MaxResultSizeChars is set but the configured
// spill directory is unwritable, inline truncation
// must still be applied.
//
// Before fix: the "spill failed" path had no fallback; the per-tool
// limit was silently bypassed, leaving the full 500-char preview.
// Create a regular file and use a sub-path as the spill directory.
// Even root cannot write a file when a path component is a regular
// file, making this test reliable regardless of privileges.
f, err := os.CreateTemp(t.TempDir(), "archie-test-spill-blocker")
if err != nil {
t.Fatal(err)
}
f.Close()
defer os.Remove(f.Name())
spillDir := filepath.Join(f.Name(), "sub")
e := ToolEntry{
Name: "big-output",
Handler: echoHandler,
MaxResultSizeChars: 30,
}
results := DispatchSequential(context.Background(), []ToolCall{
{Entry: e, Input: map[string]any{"msg": strings.Repeat("x", 500)}},
}, spillDir, nil)
if len(results) != 1 {
t.Fatalf("expected 1 result, got %d", len(results))
}
// Should be truncated to MaxResultSizeChars even though spill failed.
if !strings.Contains(results[0].Preview, "truncated") {
t.Errorf("expected inline truncation fallback, got: %s", results[0].Preview)
}
// The payload is cut to the limit; the notice explaining the cut is the
// only thing allowed past it.
maxPreview := e.MaxResultSizeChars + maxTruncationNotice
if len(results[0].Preview) > maxPreview {
t.Errorf("preview too long (%d chars), expected at most %d", len(results[0].Preview), maxPreview)
}
}
func TestDispatchSequentialOutputVolumeNeverStopsCalls(t *testing.T) {
e := ToolEntry{
Name: "echo",
Handler: echoHandler,
}
spillDir := ""
calls := []ToolCall{
{Entry: e, Input: map[string]any{"msg": "short"}},
{Entry: e, Input: map[string]any{"msg": "also-short"}},
{Entry: e, Input: map[string]any{"msg": "never-runs"}},
}
results := DispatchSequential(context.Background(), calls, spillDir, nil)
if len(results) != len(calls) {
t.Errorf("output accumulation stopped dispatch: got %d results, want %d", len(results), len(calls))
}
for i, result := range results {
if result.Error != nil {
t.Errorf("result %d failed after accumulated output: %v", i, result.Error)
}
}
}
func TestDispatchPerToolLimitWithoutSpillTruncates(t *testing.T) {
// A per-tool limit without a spill directory must truncate explicitly.
e := ToolEntry{
Name: "verbose",
Handler: echoHandler,
MaxResultSizeChars: 40,
}
results := DispatchSequential(context.Background(), []ToolCall{
{Entry: e, Input: map[string]any{"msg": strings.Repeat("y", 300)}},
}, "", nil)
if len(results) != 1 {
t.Fatalf("expected 1 result, got %d", len(results))
}
if !strings.Contains(results[0].Preview, "truncated") {
t.Errorf("expected truncation without spill directory, got: %s", results[0].Preview)
}
}
func TestDispatchPerToolLimitWithinLimit(t *testing.T) {
// Sanity: output within per-tool limit passes through unchanged.
e := ToolEntry{
Name: "concise",
Handler: echoHandler,
MaxResultSizeChars: 500,
}
results := DispatchSequential(context.Background(), []ToolCall{
{Entry: e, Input: map[string]any{"msg": "short"}},
}, "", nil)
if len(results) != 1 {
t.Fatalf("expected 1 result, got %d", len(results))
}
if strings.Contains(results[0].Preview, "truncated") || strings.Contains(results[0].Preview, "spilled") {
t.Errorf("expected clean pass-through, got: %s", results[0].Preview)
}
}