molecule-core/scripts/wheel_smoke.py
Hongming Wang 169e284d57 feat(workspace-runtime): expose universal MCP server to runtime=external operators
Ship the baseline universal MCP path that any external runtime (Claude
Code, hermes, codex, anything that speaks MCP stdio) can use, before
optimizing per-runtime channels. Today the workspace MCP server only
spins up inside the container; external operators have no way to call
the 8 platform tools (delegate_task, list_peers, send_message_to_user,
commit_memory, etc.) from outside.

Three additive changes:

1. **`platform_auth.get_token()` env-var fallback** — adds
   `MOLECULE_WORKSPACE_TOKEN` as a fallback when no
   `${CONFIGS_DIR}/.auth_token` file exists. File-first preserves
   in-container behavior unchanged. External operators (no /configs
   volume) now have a way to supply the token without faking the
   filesystem layout.

2. **`molecule-mcp` console script** — adds a new entry point in the
   published `molecule-ai-workspace-runtime` PyPI wheel. Operators run
   `pip install molecule-ai-workspace-runtime`, set 3 env vars
   (WORKSPACE_ID, PLATFORM_URL, MOLECULE_WORKSPACE_TOKEN), and register
   the binary in their agent's MCP config. `mcp_cli.main` is a thin
   validator wrapper — it checks env BEFORE importing the heavy
   `a2a_mcp_server` module so a misconfigured first-run gets a friendly
   3-line error instead of a 20-line module-level RuntimeError
   traceback.

3. **Wheel smoke gate** — extends `scripts/wheel_smoke.py` to assert
   `cli_main` and `mcp_cli.main` are importable. Same regression class
   as the 0.1.16 main_sync incident: a silent rename or unrewritten
   import here would break every external operator on the next wheel
   publish (memory: feedback_runtime_publish_pipeline_gates.md).

Test coverage:
- `tests/test_platform_auth.py` — 8 new tests for the env-var fallback:
  file-priority, env-fallback, whitespace handling, cache, header
  construction, empty-env-as-unset.
- `tests/test_mcp_cli.py` — 8 new tests for the validator: each
  required var separately, file-or-env satisfies token requirement,
  whitespace-only env treated as missing, help mentions canvas Tokens
  tab.
- Full `workspace/tests/` suite green: 1346 passed, 1 skipped.
- Local end-to-end: built wheel, installed in venv, ran `molecule-mcp`
  with no env → friendly error; with env → MCP server starts.

Why now / why this shape: user redirect was "support the baseline
first so all runtimes can use, then optimize". A claude-only MCP
channel leaves hermes/codex/third-party operators broken on
runtime=external. This PR ships the runtime-agnostic baseline; per-
runtime polish (claude-channel push delivery, hermes-native
bindings) is a follow-up PR. PR #2412 fixed the partner bug where
canvas Restart silently revoked the operator's token — the two
together unblock the external-runtime story end-to-end.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 15:20:19 -07:00

157 lines
6.3 KiB
Python

#!/usr/bin/env python3
"""Smoke-test an installed molecule-ai-workspace-runtime wheel.
Runs the same invariant assertions in two workflows:
* publish-runtime.yml — after building dist/*.whl, before PyPI upload
* runtime-prbuild-compat.yml — after building the PR's wheel, before merge
Splitting the smoke across two inline heredocs let PR-time and publish-time
drift apart. After 2026-04 we kept hitting publish-time failures for
regressions a PR-time check could have caught. One script, both gates.
Failure here intentionally exits non-zero so the workflow's `run:` step fails.
Each block prints a single ✓ line on success so the GH summary log stays
readable; assertion errors propagate with their own message.
Run directly: `python scripts/wheel_smoke.py` after `pip install <wheel>`.
"""
import os
import sys
def smoke_imports_and_invariants() -> None:
"""Module imports + stable contract assertions.
Importing main_sync by name is the strongest pre-PyPI gate we have for
import-rewrite mistakes (the 0.1.16 incident, where main.py loaded but
main_sync was missing because the build script dropped a re-export).
"""
from molecule_runtime.main import main_sync # noqa: F401
from molecule_runtime import a2a_client, a2a_tools # noqa: F401
from molecule_runtime.builtin_tools import memory # noqa: F401
from molecule_runtime.adapters import get_adapter, BaseAdapter, AdapterConfig
# cli_main + mcp_cli.main are the molecule-mcp console-script entry
# points — the external-runtime universal MCP path. Same regression
# class as the 0.1.16 main_sync incident: a silent rename or missed
# rewrite here would break every external operator's MCP install on
# the next wheel publish. Pin both names because pyproject points
# at mcp_cli.main, which then imports a2a_mcp_server.cli_main.
from molecule_runtime.a2a_mcp_server import cli_main # noqa: F401
from molecule_runtime.mcp_cli import main as mcp_cli_main # noqa: F401
assert callable(cli_main), "a2a_mcp_server.cli_main must be callable"
assert callable(mcp_cli_main), "mcp_cli.main must be callable"
assert a2a_client._A2A_ERROR_PREFIX, "a2a_client missing error sentinel"
assert callable(get_adapter), "adapters.get_adapter must be callable"
assert hasattr(BaseAdapter, "name"), "BaseAdapter interface broken"
assert hasattr(AdapterConfig, "__init__"), "AdapterConfig dataclass missing"
print("✓ module imports + invariants OK")
def smoke_agent_card_call_shape() -> None:
"""Construct AgentCard with the EXACT kwargs main.py uses.
Pure imports don't catch field-shape regressions in upstream SDKs that
only surface at construction time. Two bugs of this exact class shipped
since the a2a-sdk 1.0 migration:
- state_transition_history=True (#2179)
- supported_protocols=[...] (the protobuf field is supported_interfaces;
every workspace boot crashed with `ValueError: Protocol message
AgentCard has no "supported_protocols" field`)
main.py and this block MUST stay in lockstep — adding a kwarg there
without mirroring it here is the regression vector.
"""
from a2a.types import AgentCard, AgentCapabilities, AgentSkill, AgentInterface
AgentCard(
name="smoke-agent",
description="wheel-smoke: AgentCard call-shape",
version="0.0.0-smoke",
supported_interfaces=[
AgentInterface(protocol_binding="https://a2a.g/v1", url="http://localhost:8080"),
],
capabilities=AgentCapabilities(
streaming=True,
push_notifications=False,
),
skills=[
AgentSkill(
id="smoke-skill",
name="Smoke",
description="no-op",
tags=["smoke"],
examples=["noop"],
),
],
default_input_modes=["text/plain", "application/json"],
default_output_modes=["text/plain", "application/json"],
)
print("✓ AgentCard call-shape smoke passed")
def smoke_well_known_path_alignment() -> None:
"""The SDK's published constant must match the path it actually mounts.
main.py polls AGENT_CARD_WELL_KNOWN_PATH to detect server readiness. If
the constant and create_agent_card_routes() drift, every workspace's
initial_prompt silently drops (probe 404s, falls through to "skipping").
This was the #2193 incident class.
"""
from a2a.types import AgentCard
from a2a.utils.constants import AGENT_CARD_WELL_KNOWN_PATH
from a2a.server.routes import create_agent_card_routes
mounted_paths = [
getattr(r, "path", None)
for r in create_agent_card_routes(
AgentCard(
name="wk-smoke",
description="well-known mount alignment",
version="0.0.0-smoke",
)
)
]
assert AGENT_CARD_WELL_KNOWN_PATH in mounted_paths, (
f"AGENT_CARD_WELL_KNOWN_PATH ({AGENT_CARD_WELL_KNOWN_PATH!r}) is NOT among "
f"paths mounted by create_agent_card_routes ({mounted_paths!r}). The SDK "
"constant and its own route factory have drifted — workspace probes will "
"404 forever, silently dropping every workspace initial_prompt."
)
print(f"✓ well-known mount alignment OK ({AGENT_CARD_WELL_KNOWN_PATH})")
def smoke_message_helper() -> None:
"""new_text_message is the v1.x rename of new_agent_text_message.
main.py and a2a_executor.py call new_text_message in hot paths; if the
import breaks, every reply errors with ImportError before the message
even leaves the workspace. Importing here catches a future v2.x rename
at publish time.
"""
from a2a.helpers import new_text_message
msg = new_text_message("smoke")
assert msg is not None, "new_text_message returned None"
print("✓ message helper import + call OK")
def main() -> int:
# main.py validates WORKSPACE_ID at module-import time via platform_auth.
# Set placeholders so the smoke doesn't trip on the env-var guard.
os.environ.setdefault("WORKSPACE_ID", "00000000-0000-0000-0000-000000000000")
os.environ.setdefault("PLATFORM_URL", "http://localhost:8080")
smoke_imports_and_invariants()
smoke_agent_card_call_shape()
smoke_well_known_path_alignment()
smoke_message_helper()
print("✓ wheel smoke passed")
return 0
if __name__ == "__main__":
sys.exit(main())