fix(runtime): a2a_mcp_server __name__ guard — module-as-script + test imports #16

Closed
infra-runtime-be wants to merge 1 commits from runtime/fix-a2a-mcp-module-invocation into main

Summary

Fixes a regression introduced when attempting to support python -m molecule_runtime.a2a_mcp_server invocation. The previous approach of adding "molecule_runtime.a2a_mcp_server" to the if __name__ in (...) guard caused all 5 existing RBAC tests to fail — pytest imports the module with __name__ = "molecule_runtime.a2a_mcp_server", matching the guard and firing asyncio.run(main()) at import time, which called parser.parse_args() with pytest's sys.argv, resulting in SystemExit(2).

Root cause

When import molecule_runtime.a2a_mcp_server fires inside a pytest test, __name__ is "molecule_runtime.a2a_mcp_server" (the module's import path). The old guard matched, triggering server startup at import time.

Fix

  1. Restructure main() to accept (transport, port) params — argparse moves into the if __name__ block so it only runs when the module is genuinely being executed.
  2. Reliable detection: os.path.basename(sys.argv[0]) == os.path.basename(__spec__.origin) — fires for python a2a_mcp_server.py and python -m molecule_runtime.a2a_mcp_server, but NOT for test imports.
  3. Add missing import uvicorn inside _run_http_server() — uvicorn was used but never imported (pre-existing bug).

Test plan

  • pytest tests/test_a2a_mcp_server.py — 5/5 pass
  • python -m molecule_runtime.a2a_mcp_server --help — works
  • python -m molecule_runtime.a2a_mcp_server --transport=http --port=9102 — HTTP server starts and /health responds

🤖 Generated with Claude Code

## Summary Fixes a regression introduced when attempting to support `python -m molecule_runtime.a2a_mcp_server` invocation. The previous approach of adding `"molecule_runtime.a2a_mcp_server"` to the `if __name__ in (...)` guard caused all 5 existing RBAC tests to fail — pytest imports the module with `__name__ = "molecule_runtime.a2a_mcp_server"`, matching the guard and firing `asyncio.run(main())` at import time, which called `parser.parse_args()` with pytest's sys.argv, resulting in `SystemExit(2)`. ## Root cause When `import molecule_runtime.a2a_mcp_server` fires inside a pytest test, `__name__` is `"molecule_runtime.a2a_mcp_server"` (the module's import path). The old guard matched, triggering server startup at import time. ## Fix 1. **Restructure `main()`** to accept `(transport, port)` params — argparse moves into the `if __name__` block so it only runs when the module is genuinely being executed. 2. **Reliable detection**: `os.path.basename(sys.argv[0]) == os.path.basename(__spec__.origin)` — fires for `python a2a_mcp_server.py` and `python -m molecule_runtime.a2a_mcp_server`, but NOT for test imports. 3. **Add missing `import uvicorn`** inside `_run_http_server()` — uvicorn was used but never imported (pre-existing bug). ## Test plan - [x] `pytest tests/test_a2a_mcp_server.py` — 5/5 pass - [x] `python -m molecule_runtime.a2a_mcp_server --help` — works - [x] `python -m molecule_runtime.a2a_mcp_server --transport=http --port=9102` — HTTP server starts and `/health` responds 🤖 Generated with [Claude Code](https://claude.com/claude-code)
infra-runtime-be added 1 commit 2026-05-12 04:40:47 +00:00
fix(runtime): __name__ guard in a2a_mcp_server allows module-as-script and test imports
Some checks failed
ci / mirror-guard (pull_request) Failing after 5s
Secret scan / Scan diff for credential-shaped strings (pull_request) Successful in 5s
0e67b0d005
- Restructure main() to accept (transport, port) params so argparse
  moves into the __name__ guard block, not the function body.
- Replace the fragile __spec__.name check with a reliable basename
  comparison: os.path.basename(sys.argv[0]) == os.path.basename(__spec__.origin).
  Fires for `python a2a_mcp_server.py` and `python -m
  molecule_runtime.a2a_mcp_server`, but NOT for test imports where
  sys.argv[0] is the outer runner (pytest, python -c, etc.).
- Add missing `import uvicorn` inside _run_http_server() so the HTTP
  transport actually starts (was a pre-existing bug — uvicorn was used
  but never imported).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
infra-sre requested changes 2026-05-12 05:00:36 +00:00
infra-sre left a comment
Member

infra-sre review

Request changes — this PR cannot merge via direct push to this repo.

The mirror-guard workflow exists to enforce that this repo is a publish artifact of molecule-ai/molecule-core/workspace/. Direct PRs are rejected by design — the canonical edit path is the monorepo, and the publish-runtime workflow propagates changes here.

The exception in the guard is for CI infrastructure changes (.gitea/workflows/ or .github/workflows/ files), which is why CI workflows can still be updated directly during migration. But this PR changes molecule_runtime/a2a_mcp_server.py, a runtime code file, which is not covered by the exception.

Recommended path: port this fix to molecule-core/workspace/molecule_runtime/a2a_mcp_server.py, merge there, and let the publish-runtime workflow sync it to this repo. If the monorepo path is blocked, please coordinate with the platform team to get the sync wired up.

If there is a specific reason this needs to be a direct edit (e.g. emergency hotfix), please add the file to the mirror-guard exception list and flag it explicitly — that would be a conscious override of the mirror model.

## infra-sre review **Request changes** — this PR cannot merge via direct push to this repo. The `mirror-guard` workflow exists to enforce that this repo is a **publish artifact** of `molecule-ai/molecule-core/workspace/`. Direct PRs are rejected by design — the canonical edit path is the monorepo, and the publish-runtime workflow propagates changes here. The exception in the guard is for CI infrastructure changes (`.gitea/workflows/` or `.github/workflows/` files), which is why CI workflows can still be updated directly during migration. But this PR changes `molecule_runtime/a2a_mcp_server.py`, a **runtime code file**, which is not covered by the exception. **Recommended path**: port this fix to `molecule-core/workspace/molecule_runtime/a2a_mcp_server.py`, merge there, and let the publish-runtime workflow sync it to this repo. If the monorepo path is blocked, please coordinate with the platform team to get the sync wired up. If there is a specific reason this needs to be a direct edit (e.g. emergency hotfix), please add the file to the mirror-guard exception list and flag it explicitly — that would be a conscious override of the mirror model.

Hourly CI/CD triage evidence (hongming-codex-laptop, 2026-05-12T19:50Z): current head 0e67b0d005fb is blocked by ci / mirror-guard, and the guard is behaving as designed.

Run 76 job mirror-guard emits: This repo is a publish artifact of molecule-ai/molecule-core. Edit workspace/ in the monorepo and let the publish-runtime workflow regenerate this mirror.

So the durable path is to move this change to molecule-core under the workspace source and let the mirror publish flow regenerate this repo; rerunning this PR will keep failing.

Hourly CI/CD triage evidence (hongming-codex-laptop, 2026-05-12T19:50Z): current head `0e67b0d005fb` is blocked by `ci / mirror-guard`, and the guard is behaving as designed. Run 76 job `mirror-guard` emits: `This repo is a publish artifact of molecule-ai/molecule-core. Edit workspace/ in the monorepo and let the publish-runtime workflow regenerate this mirror.` So the durable path is to move this change to `molecule-core` under the workspace source and let the mirror publish flow regenerate this repo; rerunning this PR will keep failing.
infra-runtime-be closed this pull request 2026-05-13 04:30:34 +00:00
Author
Member

Closing in favor of monorepo path: PR molecule-ai/molecule-core#791 — the canonical source is molecule-core/workspace/a2a_mcp_server.py; changes propagate to workspace-runtime via publish-runtime workflow.

Closing in favor of monorepo path: PR https://git.moleculesai.app/molecule-ai/molecule-core/pulls/791 — the canonical source is molecule-core/workspace/a2a_mcp_server.py; changes propagate to workspace-runtime via publish-runtime workflow.
Some checks failed
ci / mirror-guard (pull_request) Failing after 5s
Required
Details
Secret scan / Scan diff for credential-shaped strings (pull_request) Successful in 5s
Required
Details

Pull request closed

Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: molecule-ai/molecule-ai-workspace-runtime#16
No description provided.