feat(mcp): gate Connected-but-unattached MCP namespaces (Closes #708)
The prior #708 slice added assess_connected_namespace_attachment() as a pure
decision function with no call site: nothing invoked it, so a session whose
role namespaces were Connected at the host but absent from the active session
tool surface still passed every mutation gate. Detection existed on paper only.
This makes it load-bearing.
Decision layer (mcp_namespace_health.py)
- assess_connected_namespace_attachment() gains secret-free telemetry
(connected/attached/required/missing counts, discovery cache hit and age,
reconnect_required, auto_attach_attempted, auto_recovered, error_type) and
reports reconnect_required, auto_recovered and startup_ordering_race.
- Startup ordering: a namespace whose connect completed after the session tool
snapshot cannot be in that snapshot, so parallel multi-role startup is
identified as its own race with the affected namespaces listed.
- attachment_gate_from_session() is a fail-closed gate keyed by
ATTACHMENT_GATED_TASKS. An unassessed namespace does not gate, matching #543
semantics, so this never fabricates a block.
- SANCTIONED_ATTACH_RECOVERY_TOOL names gitea_request_mcp_reconnect (#678) as
the only recovery.
Server wiring (gitea_mcp_server.py)
- New tool gitea_assess_mcp_namespace_attachment classifies the condition and
records a per-namespace verdict in _LIVE_NAMESPACE_ATTACHMENT.
- gitea_submit_pr_review and gitea_merge_pr now consult
_namespace_attachment_gate() alongside the existing #543 health gate, so both
fail closed while a required namespace is unattached.
- Watchdog check-in emits status only, never namespace contents.
The typed condition mcp_connected_namespaces_missing stays distinct from config
drift (#672), transport-closed (#584) and resolver EOF (#685). Recovery never
routes through direct imports, CLI or raw API mutation, profile hopping,
session-state overrides, or process kills.
Docs
- docs/mcp-namespace-health.md documents the tool arguments, startup ordering,
the fail-closed gate, and the telemetry contract.
- skills/llm-project-workflow/SKILL.md states Connected is not attached, and
that preflight proof is live tool visibility plus gitea_whoami on the role
namespace rather than host status alone.
- docs/mcp-tool-inventory.md lists the new tool.
- docs/remote-mcp/threat-model-anchors.json and threat-model.md: 21 #956 anchors
restamped for the line shift these additions caused in gitea_mcp_server.py.
Every anchor was re-derived from its recorded expect substring; none guessed.
Tests
- tests/test_issue_708_attachment_wiring.py (19 cases): typed detection, proof
mapping, reconnect-only next action, auto-attach success and failure,
reconnect rediscovery, multi-role startup ordering, telemetry including a
no-secret-leak assertion, fail-closed gate per role, unassessed and unmapped
tasks not gating, partial attachment gating only the affected role, and no
healthy verdict without attachment proof.
Verification
- tests/test_issue_708_attachment_wiring.py + test_issue_708_mcp_namespace_attachment.py: 24 passed
- namespace/session/runtime/review sweep: 427 passed, 12 subtests
- full suite head: 31 failed, 5885 passed, 6 skipped, 1047 subtests
- full suite base 17ba1ff035: 30 failed, 5862 passed, 6 skipped, 1047 subtests
- failing identifier sets match, plus tests/test_mirror_refs.py DryRunBanner,
which fails on the unmodified base in isolation and passes here: flaky, not a
regression from this branch.
- Gate proven by execution, not inspection: registering the assessment blocks
merge_pr and review_pr, and attaching the namespaces clears the block.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This commit is contained in:
@@ -6716,6 +6716,45 @@ def _live_namespace_health_gate(task: str) -> list[str]:
|
||||
)
|
||||
|
||||
|
||||
# Session-scoped MCP namespace *attachment* assessments (#708).
|
||||
# Distinct from _LIVE_NAMESPACE_HEALTH: a namespace can probe healthy while never
|
||||
# being attached to the active session tool surface ("Connected" != "tools available").
|
||||
_LIVE_NAMESPACE_ATTACHMENT: dict[str, dict] = {}
|
||||
|
||||
|
||||
def _record_live_namespace_attachment(assessment: dict | None) -> None:
|
||||
"""Store a per-namespace attachment verdict for mutation gates (#708)."""
|
||||
if not isinstance(assessment, dict):
|
||||
return
|
||||
proof = assessment.get("proof_of_connected_vs_attached")
|
||||
if not isinstance(proof, dict):
|
||||
return
|
||||
missing = set(assessment.get("missing_namespaces") or [])
|
||||
error_type = assessment.get("error_type")
|
||||
for ns, state in proof.items():
|
||||
ns_name = str(ns or "").strip()
|
||||
if not ns_name or not isinstance(state, dict):
|
||||
continue
|
||||
attached = bool(state.get("attached"))
|
||||
_LIVE_NAMESPACE_ATTACHMENT[ns_name] = {
|
||||
"namespace": ns_name,
|
||||
"connected": bool(state.get("connected")),
|
||||
"attached": attached,
|
||||
"attachment_healthy": attached and ns_name not in missing,
|
||||
"error_type": None if attached else error_type,
|
||||
"discovery_status": assessment.get("discovery_status"),
|
||||
"reconnect_required": bool(assessment.get("reconnect_required")),
|
||||
"auto_recovered": bool(assessment.get("auto_recovered")),
|
||||
}
|
||||
|
||||
|
||||
def _namespace_attachment_gate(task: str) -> list[str]:
|
||||
"""Fail closed on a recorded Connected-but-unattached namespace (#708)."""
|
||||
return mcp_namespace_health.attachment_gate_from_session(
|
||||
task, _LIVE_NAMESPACE_ATTACHMENT
|
||||
)
|
||||
|
||||
|
||||
def _decision_lock_binding(lock: dict | None = None) -> dict:
|
||||
"""Resolve key fields for durable decision-lock storage."""
|
||||
profile = get_profile()
|
||||
@@ -7596,6 +7635,10 @@ def _evaluate_pr_review_submission(
|
||||
if ns_gate:
|
||||
reasons.extend(ns_gate)
|
||||
return result
|
||||
attach_gate = _namespace_attachment_gate("review_pr")
|
||||
if attach_gate:
|
||||
reasons.extend(attach_gate)
|
||||
return result
|
||||
|
||||
if action not in _REVIEW_ACTIONS:
|
||||
reasons.append(
|
||||
@@ -11154,6 +11197,13 @@ def gitea_merge_pr(
|
||||
reasons.extend(ns_gate)
|
||||
return result
|
||||
|
||||
# Gate 0c — the merger namespace must be attached to the active session (#708).
|
||||
# Connected at the host layer is not proof the tools are in this session.
|
||||
attach_gate = _namespace_attachment_gate("merge_pr")
|
||||
if attach_gate:
|
||||
reasons.extend(attach_gate)
|
||||
return result
|
||||
|
||||
# Gate 1 — valid merge method (no API call on a bad method).
|
||||
if do not in _MERGE_METHODS:
|
||||
reasons.append(
|
||||
@@ -19419,6 +19469,76 @@ def gitea_assess_mcp_namespace_health(
|
||||
return result
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def gitea_assess_mcp_namespace_attachment(
|
||||
connected_servers: list[str] | None = None,
|
||||
attached_session_namespaces: list[str] | None = None,
|
||||
required_namespaces: list[str] | None = None,
|
||||
discovery_cache_age_seconds: float | None = None,
|
||||
discovery_cache_hit: bool | None = None,
|
||||
auto_attach_attempted: bool = False,
|
||||
auto_attach_succeeded: bool = False,
|
||||
session_tool_snapshot_at: float | None = None,
|
||||
namespace_connected_at: dict | None = None,
|
||||
) -> dict:
|
||||
"""Detect Connected-but-namespaces-not-attached MCP sessions (#708).
|
||||
|
||||
MCP servers can report **Connected** at the CLI/host inventory layer while the
|
||||
active LLM session exposes none of their tool namespaces. That is a *session
|
||||
attachment* failure and is reported here as its own typed condition,
|
||||
``mcp_connected_namespaces_missing`` — deliberately distinct from config drift
|
||||
(#672), transport-closed (#584), and resolver EOF (#685).
|
||||
|
||||
Connected is not proof that tools are available. The required preflight proof is
|
||||
live tool visibility plus ``gitea_whoami`` on the role namespace, never host
|
||||
Connected status alone.
|
||||
|
||||
The verdict is recorded in the session so ``gitea_submit_pr_review`` and
|
||||
``gitea_merge_pr`` fail closed while a required namespace is unattached. The only
|
||||
sanctioned recovery is the client attach/reconnect path followed by full preflight;
|
||||
direct imports, CLI/API mutation, profile hopping, session-state overrides, and
|
||||
process kills are forbidden and never suggested.
|
||||
|
||||
Args:
|
||||
connected_servers: Namespaces the host/CLI reports as Connected.
|
||||
attached_session_namespaces: Namespaces actually exposed in the active
|
||||
session tool surface.
|
||||
required_namespaces: Namespaces required for this workflow; defaults to the
|
||||
canonical Gitea role namespaces.
|
||||
discovery_cache_age_seconds: Age of the client tool-discovery cache entry.
|
||||
discovery_cache_hit: Whether the tool list came from that cache.
|
||||
auto_attach_attempted: Whether the runtime tried to auto-attach namespaces.
|
||||
auto_attach_succeeded: Whether that auto-attach succeeded.
|
||||
session_tool_snapshot_at: Epoch seconds the session tool snapshot was taken.
|
||||
namespace_connected_at: Per-namespace epoch seconds that connect completed,
|
||||
used to identify multi-role startup ordering races.
|
||||
|
||||
Returns:
|
||||
dict with ``discovery_status``, ``missing_namespaces``,
|
||||
``proof_of_connected_vs_attached``, ``error_type``, ``reconnect_required``,
|
||||
``auto_recovered``, ``startup_ordering_race``, a secret-free ``telemetry``
|
||||
block, and a reconnect-only ``exact_next_action``.
|
||||
"""
|
||||
result = mcp_namespace_health.assess_connected_namespace_attachment(
|
||||
connected_servers=connected_servers,
|
||||
attached_session_namespaces=attached_session_namespaces,
|
||||
required_namespaces=required_namespaces,
|
||||
discovery_cache_age_seconds=discovery_cache_age_seconds,
|
||||
discovery_cache_hit=discovery_cache_hit,
|
||||
auto_attach_attempted=auto_attach_attempted,
|
||||
auto_attach_succeeded=auto_attach_succeeded,
|
||||
session_tool_snapshot_at=session_tool_snapshot_at,
|
||||
namespace_connected_at=namespace_connected_at,
|
||||
)
|
||||
_record_live_namespace_attachment(result)
|
||||
# #606-style watchdog check-in (best-effort, fail open). Status only, no names.
|
||||
sentry_observability.monitor_checkin(
|
||||
"namespace_attachment",
|
||||
"ok" if result.get("attachment_healthy") else "error",
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def gitea_activate_profile(
|
||||
profile_name: str,
|
||||
|
||||
Reference in New Issue
Block a user