From 80b074250bb1ad05a36dbc6bd8abad41b4ad7be2 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Sun, 18 Oct 2026 07:03:30 -0500 Subject: [PATCH] mcpx: name the resolved key in the degraded-scope log line (#280) Mirrors dezren39/nix PR #396. warnDegraded logged 'degraded to per-call' for every degraded scope, but repo/worktree degrade to cwd: and parent-session can degrade to session:. Pass the resolved key from keyFor into warnDegraded, print it, and include it in the once-only dedup key. Adds registry_degraded_test.go ported from nix (ref #280). --- pkgs/mcpx/internal/daemon/registry.go | 15 +++-- .../internal/daemon/registry_degraded_test.go | 62 +++++++++++++++++++ 2 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 pkgs/mcpx/internal/daemon/registry_degraded_test.go diff --git a/pkgs/mcpx/internal/daemon/registry.go b/pkgs/mcpx/internal/daemon/registry.go index b050941beb..0b225a64e2 100644 --- a/pkgs/mcpx/internal/daemon/registry.go +++ b/pkgs/mcpx/internal/daemon/registry.go @@ -1030,7 +1030,7 @@ func (r *Registry) keyFor(p *pool.Pool, cc config.CallContext) string { scope := p.Config().Scope key, degraded := scope.Key(cc) if degraded != "" { - r.warnDegraded(p.Name(), scope, degraded) + r.warnDegraded(p.Name(), scope, key, degraded) } if cc.CallID != "" { r.sessMu.Lock() @@ -1049,14 +1049,17 @@ func (r *Registry) keyFor(p *pool.Pool, cc config.CallContext) string { } // warnDegraded reports a scope that could not be resolved as configured, once -// per server and reason. Silent degradation to per-call isolation would look -// like a performance problem rather than a configuration one. -func (r *Registry) warnDegraded(server string, scope config.Scope, reason string) { - k := server + "\x00" + string(scope) + "\x00" + reason +// per server, resolved key and reason. Silent degradation would look like a +// performance problem rather than a configuration one, so the line names the +// key the scope actually degraded to: only some scopes fall back to per-call +// isolation, while repo/worktree/cwd degrade to a directory shared by every +// caller in it and parent-session can degrade to the session key. +func (r *Registry) warnDegraded(server string, scope config.Scope, key, reason string) { + k := server + "\x00" + string(scope) + "\x00" + key + "\x00" + reason if _, seen := r.degraded.LoadOrStore(k, true); seen { return } - r.logf("server %q: scope %q degraded to per-call: %s", server, scope, reason) + r.logf("server %q: scope %q degraded to %s: %s", server, scope, key, reason) } // ReleaseCaller stops instances created for a caller whose scope made them diff --git a/pkgs/mcpx/internal/daemon/registry_degraded_test.go b/pkgs/mcpx/internal/daemon/registry_degraded_test.go new file mode 100644 index 0000000000..47df2d5415 --- /dev/null +++ b/pkgs/mcpx/internal/daemon/registry_degraded_test.go @@ -0,0 +1,62 @@ +package daemon + +import ( + "fmt" + "strings" + "testing" + + "github.com/dezren39/mcpx/internal/config" +) + +// TestWarnDegradedNamesResolvedKey is the regression test for #280: the +// degraded-scope log line used to claim every scope "degraded to per-call", +// even when Scope.Key fell back to a directory (shared by every caller in +// it) or to the session key. The line must name the resolved key. +func TestWarnDegradedNamesResolvedKey(t *testing.T) { + cases := []struct { + name string + scope config.Scope + key string + reason string + }{ + {"worktree to cwd", config.ScopeWorktree, "cwd:/work/bare.git", "no worktree: core.bare is true"}, + {"repo to cwd", config.ScopeRepo, "cwd:/work/plain", "not a git repository"}, + {"parent-session to session", config.ScopeParentSession, "session:s1", "no parent session id; using the session id"}, + {"session to per-call", config.ScopeSession, "call:c1", "no session id; set MCPX_SESSION_ID or pass --session"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var lines []string + r := &Registry{logf: func(format string, args ...any) { + lines = append(lines, fmt.Sprintf(format, args...)) + }} + r.warnDegraded("bytree", tc.scope, tc.key, tc.reason) + if len(lines) != 1 { + t.Fatalf("logged %d lines, want 1: %v", len(lines), lines) + } + want := fmt.Sprintf("server %q: scope %q degraded to %s: %s", "bytree", tc.scope, tc.key, tc.reason) + if lines[0] != want { + t.Fatalf("log line = %q, want %q", lines[0], want) + } + if strings.Contains(lines[0], "per-call") && !strings.HasPrefix(tc.key, "call:") { + t.Fatalf("log line claims per-call for a shared key: %q", lines[0]) + } + }) + } +} + +// TestWarnDegradedDedupesPerResolvedKey: the same degradation warns once, +// but the same reason resolving to a different key is a different +// degradation and must warn again — the two directories are not shared. +func TestWarnDegradedDedupesPerResolvedKey(t *testing.T) { + var lines []string + r := &Registry{logf: func(format string, args ...any) { + lines = append(lines, fmt.Sprintf(format, args...)) + }} + r.warnDegraded("bytree", config.ScopeWorktree, "cwd:/work/a", "no worktree") + r.warnDegraded("bytree", config.ScopeWorktree, "cwd:/work/a", "no worktree") + r.warnDegraded("bytree", config.ScopeWorktree, "cwd:/work/b", "no worktree") + if len(lines) != 2 { + t.Fatalf("logged %d lines, want 2 (one per distinct key): %v", len(lines), lines) + } +}