diff --git a/changelog.d/fixed-admin-mcp-detail-less-write-ops.md b/changelog.d/fixed-admin-mcp-detail-less-write-ops.md new file mode 100644 index 0000000000..6692c8b688 --- /dev/null +++ b/changelog.d/fixed-admin-mcp-detail-less-write-ops.md @@ -0,0 +1 @@ +- Admin MCP: `budget.reset`, `backup.create`, `circuit_breaker.engage` and `circuit_breaker.release` can be previewed and confirmed again. Since the unpersistable-preview check landed, `write_preview` refused all four with "contains a confirmation value that cannot be stored; update that field outside admin MCP", so the fleet circuit breaker, a budget-window reset and a backup could not be run through MCP on either transport. These operations take no arguments and report no details, and the pending-confirmation scrub turned their empty (nil) details into `{}`, which the check read as a masked value. The scrub now leaves nil maps and lists as they are; credential-bearing values are refused exactly as before. diff --git a/src/pkg/adminmcp/adminmcp.go b/src/pkg/adminmcp/adminmcp.go index 38cb05a6a1..cb4532efa8 100644 --- a/src/pkg/adminmcp/adminmcp.go +++ b/src/pkg/adminmcp/adminmcp.go @@ -607,6 +607,13 @@ func scrubPendingValue(v any, key string) any { out.Body = scrubPendingValue(out.Body, "body") return out case map[string]any: + if x == nil { + // Nil stays nil. A preview with no details (budget.reset, + // backup.create, the circuit breaker) would otherwise come back + // as {} and read as a changed value to + // pendingValueNeedsFreshConfirm, which refuses the preview. + return x + } out := make(map[string]any, len(x)) for k, val := range x { if credentialBearingKey(k) && !maskExempt(k, val) { @@ -617,12 +624,18 @@ func scrubPendingValue(v any, key string) any { } return out case []any: + if x == nil { + return x + } out := make([]any, len(x)) for i, val := range x { out[i] = scrubPendingValue(val, key) } return out case []string: + if x == nil { + return x + } out := make([]string, len(x)) for i, val := range x { out[i] = scrubOutboundString(val) diff --git a/src/pkg/adminmcp/write_contract_test.go b/src/pkg/adminmcp/write_contract_test.go index 373ac6f905..f16964081b 100644 --- a/src/pkg/adminmcp/write_contract_test.go +++ b/src/pkg/adminmcp/write_contract_test.go @@ -154,3 +154,72 @@ func TestWritePreviewPersistsOnlyExecutableRequest(t *testing.T) { } } } + +// A write op whose preview carries no details was refused at write_preview +// with "contains a confirmation value that cannot be stored": the pending +// scrub turned its nil details into {} and the unpersistable check read that +// as a changed value. These four ops take no arguments and report no details. +func TestDetailLessWriteOpsPreviewAndConfirm(t *testing.T) { + for _, tc := range []struct{ op, method, path string }{ + {WriteOpBudgetReset, http.MethodPost, "/api/config/governor/budget/reset"}, + {WriteOpBackupCreate, http.MethodPost, "/api/backup"}, + {WriteOpCircuitBreakerEngage, http.MethodPost, "/api/breaker/engage"}, + {WriteOpCircuitBreakerRelease, http.MethodPost, "/api/breaker/release"}, + } { + t.Run(tc.op, func(t *testing.T) { + provider := &writeProvider{} + h := NewHandler(provider, WithWritesEnabled(true), WithPendingStore(NewFilePendingStore(filepath.Join(t.TempDir(), "pending.json"))), WithHiveID("hive-a")) + previewed, err := h.PreviewWrite(context.Background(), map[string]any{"operation": tc.op, "args": map[string]any{}}) + if err != nil { + t.Fatalf("write_preview refused %s: %v", tc.op, err) + } + id, _ := previewed.(map[string]any)["confirmation_id"].(string) + if _, err := h.ConfirmWrite(context.Background(), map[string]any{"confirmation_id": id}); err != nil { + t.Fatalf("write_confirm refused %s: %v", tc.op, err) + } + if len(provider.requests) != 1 || provider.requests[0].Method != tc.method || provider.requests[0].Path != tc.path { + t.Fatalf("executed %+v, want %s %s", provider.requests, tc.method, tc.path) + } + }) + } +} + +// Every registered op that takes no arguments must preview with none, so a +// new argument-less op cannot ship refused the same way. +func TestArgumentLessWriteOpsPreviewWithNoArgs(t *testing.T) { + seen := 0 + for _, op := range DefaultWriteRegistry().Ops() { + props, _ := op.InputSchema()["properties"].(map[string]any) + if len(props) != 0 { + continue + } + seen++ + h := NewHandler(&writeProvider{}, WithWritesEnabled(true), WithHiveID("hive-a")) + if _, err := h.PreviewWrite(context.Background(), map[string]any{"operation": op.Name(), "args": map[string]any{}}); err != nil { + t.Errorf("write_preview refused argument-less op %s: %v", op.Name(), err) + } + } + if seen == 0 { + t.Fatal("no argument-less write op found; the test no longer covers anything") + } +} + +// The unpersistable check compares a value with its scrubbed copy, so the +// scrub must not turn nil into an empty map or list. +func TestPendingScrubKeepsNilValues(t *testing.T) { + for name, v := range map[string]any{ + "nil details": map[string]any(nil), + "nested nil map": map[string]any{"config": map[string]any(nil)}, + "nil list": map[string]any{"items": []any(nil)}, + "nil string list": map[string]any{"names": []string(nil)}, + "request, no body": WriteRequest{Method: http.MethodPost, Path: "/api/backup"}, + } { + if pendingValueNeedsFreshConfirm(v) { + t.Errorf("%s: scrubbing changed a value that holds nothing to mask", name) + } + } + // A real credential-bearing value is still caught. + if !pendingValueNeedsFreshConfirm(map[string]any{"api_key": "abc"}) { + t.Error("a credential-bearing key no longer needs a fresh confirmation") + } +}