Skip to content

fix: serialize per-server tool-approval writes so baselines and approvals are complete (UX-02) - #1571

Merged
Dumbris merged 6 commits into
mainfrom
fix/qa070-ux02-approval-lock-core
Oct 11, 2026
Merged

Dumbris merged 6 commits into
mainfrom
fix/qa070-ux02-approval-lock-core

Conversation

@Dumbris

@Dumbris Dumbris commented Oct 11, 2026

Copy link
Copy Markdown
Member

Impact

UX-02 (QA 0.70.0, P1). After a trusted baseline or an approval, some tools stayed pending. Users had finished review but could not call the tools they had picked. The approval response still said status: approved and gave no sign that the decision was incomplete.

Cause

Tool-approval records for one server have several writers, and none of them was mutually exclusive with the others. The writers are discovery passes (checkToolApprovals, reached from the reactive connect, boot and post-reload sweeps, HandleUpstreamServerChange and the approval reindex), operator approve/block/toggle, server-approval baseline promotion (QuarantineServer(false) → approveBaselineToolsForServer) and the scanner's baseline+blocks commit. Each one reads a record, decides, then writes the whole record back. A pass holding a stale copy could therefore overwrite a fresher approval with pending. A pass could also decide isBaselinePass from a stale snapshot and file already-approved tools as new pending tools. Once a server has a baseline, no later pass promotes stranded records, so they persisted across reload and restart. Full diagnosis is in docs/qa/0.70.0-ux-2026-10-10/remediation/diagnosis-wf_b479d8a8.json.

On current main, 1f5acb6 (UX-12) added a global captureSnapMu that already serializes discovery passes with each other, so the cold-start repro no longer strands tools live. Operator writes, baseline promotion and the scanner block write still race a pass, and the deterministic tests below reproduce that.

Change (minimal scope)

  1. Per-server tool-approval lock (internal/runtime/tool_approval_lock.go, a sync.Map of server → *sync.Mutex):
    • Discovery pass: held across the whole checkToolApprovals read-modify-write, including the gate/quarantine read and the isBaselinePass decision.
    • Operator writes: ApproveTools, ApproveAllTools, approveBaselineToolsForServer, BlockTools, BlockAllTools and the tool enable toggles take it.
    • Baseline promotion: goes through approveToolsLocked, which re-reads each record and never promotes one that is now changed.
    • Quarantine flip and storage read: the QuarantineServer storage flip takes the lock. A pass also reads the storage quarantine flag and fails closed if it cannot read it, so a server being quarantined is never baseline-approved.
    • Scanner and deletes: the scanner's SaveIntegrityBaselineWithBlocks runs under the lock through an optional ToolApprovalLocker capability, and removed-tool deletes take it too.
    • What the lock never covers: it is never held across LoadConfiguredServers, HandleUpstreamServerChange, SaveConfiguration or UnquarantineServer. SSE events and the reindex goroutine run after it is released.
  2. Applied counts, added alongside the existing fields. tools/approve and security/approve read the records back and add approved_count, blocked_count, still_pending, still_changed and held_tools. tools/approve also adds not_approved, and its approved now counts requested tools that actually read approved. If the read-back fails, the response sets outcome_unavailable: true instead of reporting counts it cannot know. status keeps its meaning. mcpproxy review approve and mcpproxy security approve print the counts and warn on stderr when anything is still held, so JSON/YAML stdout stays parseable.

Not included on purpose: binding approvals to reviewed definitions, capture-order inventories, Web UI state changes and macOS tray changes (see follow-ups). No new dependencies, no change to any built-in tool schema and no storage format change. The diff is about 1.1k lines, roughly 470 of them tests.

Compatibility

All response fields are additive, and status: "approved" is unchanged. One existing field changes value: tools/approve's approved used to echo the request length and now counts what applied. CLI table lines gain a counts suffix.

Verification

  • Tests first: with the lock neutered, TestCheckToolApprovals_OverlappingBaselinePasses_NoStrandedPending, TestApproveTools_DuringDiscoveryPass_ApprovalNotLost, TestQuarantineServer_FlipWaitsForInFlightPass and TestWithToolApprovalLock_WaitsForInFlightPass fail. With the lock they pass, also under -race -count=3.
  • Race tests: go test -race -tags server -timeout 20m -skip "E2E|Binary|MCPProtocol|TestInfoEndpoint|TestGracefulShutdownNoPanic|TestSocketInfoEndpoint" ./internal/server/... ./internal/httpapi/... ./internal/runtime/... ./internal/security/scanner/... ./cmd/mcpproxy/ passes.
  • Lint: golangci-lint run --config .github/.golangci.yml ./... and the same with --build-tags server report no issues in changed files.
  • Live runs: scratch instances built with make build from the final head:
    • Fresh trusted 320-tool baseline: 10/10 runs gave 320 approved, 0 pending.
    • Big profile, mcpproxy review approve large-review --yes --force --all --except <15 delete_*>: 6/6 runs gave 165 approved and enabled, 15 approved and disabled, 0 pending, immediately and after 60s.
    • Min profile, mcpproxy review approve large-review --yes: 165 + 15 blocked. The state held after 120s, 3× discover-tools, a rescan, a hot reload, disable/enable and a restart. call_tool_read executes the read tools, and retrieve_tools returns them.
    • Changed and new tools from a fixture variant come back as changed and pending and are refused. After an explicit POST /quarantine, nothing is exposed.
    • Web Review screen default approve (Playwright), 3/3 runs: 165 + 15 blocked, 0 pending, immediately and after 120s.
  • Independent review: OpenCode github-copilot/gpt-6.1-sol, read-only. Rounds 1–4 found reporting gaps, all fixed. Round 5 returned VERDICT: PASS at the final head.
  • Evidence: docs/qa/0.70.0-ux-2026-10-10/remediation/evidence/ux02c/EVIDENCE.md, kept in the main checkout and not part of this diff.

Supersedes #1555

Known follow-ups (out of scope, filed)

🤖 Generated with Claude Code

…X-02)

Discovery passes and operator approvals wrote the same tool-approval records
without per-server mutual exclusion. A pass holding a stale copy of a record
wrote it back over a fresher approval (or a baseline decision taken from a
stale snapshot filed already-approved tools as pending), and the stranded
pending records then survived reload and restart.

- per-server mutex (sync.Map server -> *sync.Mutex) covers checkToolApprovals
  end to end: gate read, prior-record snapshot, isBaselinePass and all writes
- ApproveTools / ApproveAllTools / server-approval baseline promotion,
  BlockTools / BlockAllTools, tool enable toggles, removed-tool deletes and the
  QuarantineServer storage flip take the same lock
- baseline promotion re-reads each record under the lock (approveToolsLocked)
  and never promotes a record that turned "changed"
- passes also read the storage quarantine flag and fail closed when it cannot
  be read, so a server quarantined mid-flight is never auto-baselined
- the scanner's baseline+blocks commit runs under the lock through an optional
  ToolApprovalLocker capability
- SSE events and the reindex goroutine run after the lock is released; the
  lock is never held across LoadConfiguredServers, HandleUpstreamServerChange
  or UnquarantineServer
…-02)

tools/approve and security/approve read the approval records back after the
write and add approved_count, blocked_count, still_pending, still_changed and
held_tools. tools/approve's approved now counts requested tools that actually
read approved and lists the rest in not_approved. status stays "approved".
review approve and security approve print the counts and warn on stderr when
any tool still needs review.
…unts on tools/approve table output (UX-02 review)
@cloudflare-workers-and-pages

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: fix/qa070-ux02-approval-lock-core

Available Artifacts

  • archive-darwin-amd64 (31 MB)
  • archive-darwin-arm64 (28 MB)
  • archive-linux-amd64 (19 MB)
  • archive-linux-arm64 (17 MB)
  • archive-windows-amd64 (31 MB)
  • archive-windows-arm64 (27 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (27 MB)
  • installer-dmg-darwin-arm64 (24 MB)
  • smart-mcp-proxymcpproxy-goHZCSKU.dockerbuild (0 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 38114556864 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@codecov-commenter

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants