diff --git a/pkgs/mcpx/internal/daemon/registry.go b/pkgs/mcpx/internal/daemon/registry.go index c840eccbe7..80310b198a 100644 --- a/pkgs/mcpx/internal/daemon/registry.go +++ b/pkgs/mcpx/internal/daemon/registry.go @@ -2331,7 +2331,14 @@ func (r *Registry) callWithLedger(ctx context.Context, p *pool.Pool, server, too var dispatched atomic.Bool callCtx := mcpclient.WithToolCallDispatchHook(ctx, func() { dispatched.Store(true) }) result, err := p.Call(callCtx, rs, tool, args) - if err != nil && dispatched.Load() && !toolReadOnly(p, tool) && r.writeLedger != nil && !mcpclient.IsConclusiveUpstreamError(err) { + // The unknown-outcome classification honors the operator's + // readOnlyTools declaration (#6493) as well as the annotation-based + // classification: a declared read-only tool's post-dispatch failure + // cannot have written, so it must not record an unknown outcome and + // block later writes. declaredReadOnly reads the requested server's + // view -- the same name this call was made under. + readOnly := toolReadOnly(p, tool) || r.declaredReadOnly(server, tool) + if err != nil && dispatched.Load() && !readOnly && r.writeLedger != nil && !mcpclient.IsConclusiveUpstreamError(err) { return nil, r.recordUnknownOutcome(p, server, tool, args, surface) } return result, err diff --git a/pkgs/mcpx/internal/daemon/write_policy_test.go b/pkgs/mcpx/internal/daemon/write_policy_test.go index 5ad78ef32b..1298bcf3bb 100644 --- a/pkgs/mcpx/internal/daemon/write_policy_test.go +++ b/pkgs/mcpx/internal/daemon/write_policy_test.go @@ -16,6 +16,7 @@ import ( "github.com/developing-today/code/pkgs/mcpx/internal/elicit" "github.com/developing-today/code/pkgs/mcpx/internal/mcpclient" "github.com/developing-today/code/pkgs/mcpx/internal/pool" + "github.com/developing-today/code/pkgs/mcpx/internal/testsupport" ) // The write policy gate (#1979), in the destructive_test.go fixture @@ -122,6 +123,77 @@ func TestRegistryPreDispatchFailureDoesNotRecordUnknownOutcome(t *testing.T) { } } +// (#6493) The post-dispatch ledger classification honors the operator's +// readOnlyTools declaration, not just tool annotations. Oracle: after a +// genuinely post-dispatch failure -- the fake server's "slow" tool is +// dispatched, then the call context times out before any response -- +// the ledger must contain no unknown-outcome record for the declared +// read-only tool, and must still contain one for the same tool when it +// is not declared. "slow" carries no annotations, so the annotation +// path alone classifies it as a write in both cases. +func TestCallWithLedgerHonorsDeclaredReadOnlyAfterDispatch(t *testing.T) { + run := func(t *testing.T, declared bool) (error, bool) { + t.Helper() + ledger, err := OpenWriteLedger(t.TempDir()) + if err != nil { + t.Fatal(err) + } + var r *Registry + if declared { + r = writePolicyRegistryWithReadOnly("allow", "slow") + } else { + r = writePolicyRegistry("allow") + } + r.writeLedger = ledger + // Resolve through the real config path, as the pool tests do: + // a hand-built Resolved leaves the folded defaults (instance + // cap, start/call timeouts) at zero and the pool cannot serve. + cfg := &config.Config{MCPServers: map[string]*config.Server{ + "demo": {Name: "demo", Command: testsupport.FakeMCPBinary(t)}, + }} + resolved, err := cfg.Resolve("demo") + if err != nil { + t.Fatalf("resolve: %v", err) + } + scopeKey, _ := resolved.Scope.Key(config.CallContext{}) + p := pool.New(resolved) + t.Cleanup(p.Close) + // Warm the pool with a generous context first: cold start and + // discovery must not eat the timed call's budget, or the failure + // lands in discovery (pre-dispatch) instead of after dispatch. + warmCtx, warmCancel := context.WithTimeout(context.Background(), 90*time.Second) + defer warmCancel() + if _, err := p.Call(warmCtx, config.ResolvedScope{Key: scopeKey}, "echo", map[string]any{"message": "warm"}); err != nil { + t.Fatalf("warm-up call: %v", err) + } + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + _, callErr := r.callWithLedger(ctx, p, "demo", "slow", map[string]any{"ms": 30000}, config.ResolvedScope{Key: scopeKey}, "call") + if callErr == nil { + t.Fatal("slow call unexpectedly succeeded") + } + _, blocked := ledger.Lookup("demo") + return callErr, blocked + } + + callErr, blocked := run(t, true) + var unknown ErrUnknownOutcome + if errors.As(callErr, &unknown) { + t.Fatalf("declared read-only post-dispatch failure = %v, want the raw call error", callErr) + } + if blocked { + t.Fatal("declared read-only tool recorded an unknown outcome after a post-dispatch failure") + } + + callErr, blocked = run(t, false) + if !errors.As(callErr, &unknown) { + t.Fatalf("undeclared post-dispatch failure = %T %v, want ErrUnknownOutcome", callErr, callErr) + } + if !blocked { + t.Fatal("undeclared write tool recorded no unknown outcome after a post-dispatch failure") + } +} + // A catalogue that has expired, or been invalidated by tools/list_changed, // no longer proves a tool is read-only: a tool that has become writable // would run, lose its response, and be exempted from the ledger, leaving a