Skip to content

fix(mcpbroker): idle-session broker workspace refresh + concurrency fixes - #1530

Merged
jhrozek merged 11 commits into
mainfrom
impl/idle-session-broker-workspace-refresh
Sep 14, 2026
Merged

jhrozek merged 11 commits into
mainfrom
impl/idle-session-broker-workspace-refresh

Conversation

@jhrozek

@jhrozek jhrozek commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements idle-session MCP broker workspace refresh (ADR 0335): an owner of a broker-backed mecak8s session can run /tools-connect after any completed conversation turn to destructively replace the session's ToolHive-owned broker tool bundle, and to recover broker capability after a broker-process restart. This PR also fixes two concurrency bugs found during a follow-up panel review and adds the acceptance plan's five named test proofs.

  • engine/session/workspace_enrollment.go: relaxes the workspace-enrollment lifecycle from pre-prompt-only to idle-without-conflicting-control.
  • internal/adapter/mcpbroker/workspace_enrollment.go: adds ResetWorkspaceEnrollment, the narrow root-internal operation that withdraws the completed catalogue while preserving the logical broker session/grant custody.
  • internal/adapter/server/workspace_enrollment.go: destructive-replacement sequencing (reset → withdraw engine → begin → discover → build engine → complete → persist), each step fail-closed.
  • cmd/mecatui/*: mecatui//tools-connect and /mcp expose the same recovery action whenever the session is stably idle, instead of fresh-session-only.
  • Fixed a real lock-order inversion: ResetWorkspaceEnrollment took logical.mu before a.mu, the reverse of every other method holding both — a concurrent lazy-authorization catalogue refresh racing an explicit refresh could deadlock the attachment. Reordered to match the package convention.
  • Fixed a real serialization gap: RetryWorkspaceEnrollment composed two independently-locking public methods, releasing runEntryMu between cancel and begin — a narrow window for a prompt to slip in between them. Collapsed into one lock acquisition.
  • Added the acceptance plan's named Scenario 1/2 tests and invariants, including fault injection at each destructive-replacement boundary (broker reset, engine build, attachment commit, snapshot persistence) and a simulated broker-process-loss-and-recovery test over a shared session store.

Development stage

  • Implementation — based on an approved, merged Plan / Interface PR

Contract linkage

Interface conformance

Matches the approved contract: no gRPC/protobuf schema change, no exported engine/ API change. The two fixes in this PR (lock ordering inside ResetWorkspaceEnrollment; collapsing RetryWorkspaceEnrollment to a single runEntryMu acquisition via new unexported connectWorkspaceServicesLocked/cancelWorkspaceEnrollmentLocked helpers) are internal-only and change no public surface.

Issue relationship

Closes #1463

Type of change

  • Bug fix
  • New feature

Test plan

Baseline checks

  • Linting (task lint) — 0 issues across all modules
  • Offline test suite (task test)
  • Offline demo (go run ./cmd/mecademo)
  • Markdown changed: docs generation/link checks (task docs)
  • User docs/user-facing behavior changed: site build (task site:build)
  • Guarded engine API affected: compatibility check (task api:check)
  • Final implementation review: /panel-review — ran, found the two concurrency bugs above (now fixed) and the missing named acceptance tests (now added); no unwaived blocker remains.

Changes

File Change
internal/adapter/mcpbroker/workspace_enrollment.go Fix lock-order inversion in ResetWorkspaceEnrollment; single-pass route filter
internal/adapter/server/workspace_enrollment.go Fix RetryWorkspaceEnrollment serialization gap via *Locked helpers
internal/adapter/server/session_mutation_inventory.go Update audit doc rows for the renamed locked helpers
internal/adapter/mcpbroker/workspace_enrollment_test.go New: real-attachment reset behavior test + lock-order regression test
internal/adapter/server/mcp_broker_test.go New brokerEngineResultWithStore helper (engines that drive a real persisted turn)
internal/adapter/server/workspace_enrollment_test.go New: TestIdleSessionBrokerRefresh_Scenario1_ExistingConversationControls, _Scenario1_DestructiveCatalogueReplacement, _Scenario2_RestartAndExplicitRecovery, TestInvariant_idle_session_broker_refresh_serialization, _preserves_toolhive_custody, plus the two retry regression tests

User-facing change

An owner can run /tools-connect on an established (non-empty-conversation) broker-backed session to replace its connected tools, and can recover broker capability with the same command after a broker-process restart, without needing to start a fresh session. Starting a refresh is destructive: broker tools become unavailable until the replacement completes.

Special notes for reviewers

  • The two concurrency fixes were caught by an internal /panel-review pass across Spec/Standards/Test-adequacy/Domain axes, each independently verified to reproduce against the pre-fix code before being fixed (a hanging lock-order repro, and an AST-based structural check).
  • task test here means go test scoped to the touched packages plus a full task test run kicked off locally before opening this PR; CI is the authoritative full run.

jhrozek and others added 11 commits September 14, 2026 22:42
ResetWorkspaceEnrollment took logical.mu before a.mu, the reverse of every
other method that holds both (Commit/Abort/beginOperation/
freezeAuthenticatedCatalogue/RefreshGrantedAuthorizationCatalogue) — a
concurrent lazy-authorization catalogue refresh racing an explicit
/tools-connect refresh could deadlock the attachment. Reordered to match the
package convention and folded its two-pass route filter into one pass.

RetryWorkspaceEnrollment composed two independently-locking public methods
(cancel, then ConnectWorkspaceServices), releasing runEntryMu between them —
a real, if narrow, window for a prompt to start between cancellation and the
replacement begin. Collapsed into a single lock acquisition delegating to new
connectWorkspaceServicesLocked/cancelWorkspaceEnrollmentLocked helpers.

Both fixes ship with regression tests verified to fail against the pre-fix
code (a hanging lock-order reproduction, and an AST-based structural check
that RetryWorkspaceEnrollment acquires runEntryMu exactly once).

Also adds the acceptance plan's named Scenario 1/2 tests
(TestIdleSessionBrokerRefresh_Scenario1_ExistingConversationControls,
_Scenario1_DestructiveCatalogueReplacement, _Scenario2_RestartAndExplicitRecovery,
TestInvariant_idle_session_broker_refresh_serialization, and
_preserves_toolhive_custody) proving AC1.1-AC1.4 and AC2.1-AC2.4 end to end,
including fault injection at each destructive-replacement boundary (broker
reset, engine build, attachment commit, snapshot persistence) and simulated
broker-process-loss recovery over a shared session store.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@jhrozek
jhrozek force-pushed the impl/idle-session-broker-workspace-refresh branch from d57a299 to 4796d08 Compare September 14, 2026 20:43
@jhrozek
jhrozek merged commit 396211d into main Sep 14, 2026
36 checks passed
@jhrozek
jhrozek deleted the impl/idle-session-broker-workspace-refresh branch September 14, 2026 21:01
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Sep 14, 2026
2 tasks
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.

Allow /tools-connect mid-conversation

1 participant