Skip to content

refactor(mcp): topos quality sweep + release automation - #18

Merged
jeremy-wayland merged 5 commits into
fix/mcp-housekeeping-and-cache-safetyfrom
refactor/mcp-quality-and-release-automation
Aug 22, 2026
Merged

jeremy-wayland merged 5 commits into
fix/mcp-housekeeping-and-cache-safetyfrom
refactor/mcp-quality-and-release-automation

Conversation

@jeremy-wayland

Copy link
Copy Markdown
Member

Stacked on #17.

Summary

  • Split run_dispatch (complexity 103) into per-kind branch functions; extracted shared scaffolding helpers.
  • Extracted helpers in catalog.py, serialization.py, and tools/dispatch_tools.py to clear topos SIMPLE complexity gates. catalog.py and serialization.py now hit topos IDEAL (all four pillars); tools/dispatch_tools.py hits GOLD.
  • Reset this fork's version to 0.1.0 (own release line, decoupled from upstream's v0.15.0+ tag history) and dropped a dead hatch.version = vcs block that was silently no-op'd by the static version field.
  • Added a tag-triggered (v*.*.*) release workflow that builds the wheel/sdist, verifies the packaged ehrapy[mcp] extra actually speaks MCP over stdio via a new composite action (.github/actions/mcp-handshake-check), and attaches artifacts to a GitHub Release. Replaced the inherited public-PyPI publish step, which would have tried to publish ehrapy to the real PyPI project. Same handshake check now also runs on every push/PR to main.

All four commits are pure structural/infra changes — no runtime behavior changed in the MCP dispatch/serialization code.

Test plan

  • uv run pytest tests/mcp — 50/50 pass after every refactor step
  • uv run python scripts/mcp_eval.py — 44/44 live steps pass
  • uv run ruff check / ruff format --check — clean
  • topos topos_evaluate_project / topos_assess_worktree_change — verified IMPROVEMENT at each step, final rollup: 14/17 files in ehrapy/mcp/ at IDEAL
  • Built the wheel locally, installed ehrapy[mcp] into a clean venv, and ran the handshake check end-to-end against the packaged artifact (list tools, load demo dataset, run preprocessing, read it back, ingest a CSV)
  • Validated all touched workflow/action YAML parses

🤖 Generated with Claude Code

https://claude.ai/code/session_01PC6jVtnaLvJS5mYNzE6XYF

jeremy-wayland and others added 4 commits August 21, 2026 21:01
run_dispatch had grown to a 434-line, complexity-103 function covering all
six dispatch kinds (demo, io-read, get, plot, io-write, edata) in one body.
Split along its own existing numbered comment boundaries into
_dispatch_demo/_dispatch_io_read/_dispatch_get/_dispatch_plot/
_dispatch_io_write/_dispatch_edata, plus small shared helpers for the
suggested-next/path-policy/op-log scaffolding every branch repeated.
run_dispatch is now a ~70-line linear guard-clause dispatcher.

Pure structural move: no behavior change, verified with pytest tests/mcp
(50/50) and topos_assess_worktree_change (IMPROVEMENT; max function
complexity 103 -> 13).
…/serialization/dispatch_tools

Same treatment as the run_dispatch split, applied to the next-worst offenders
from a topos_evaluate_project sweep of ehrapy/mcp/:

- catalog.py: _extract_choices (16) and function_help (15) each did several
  unrelated things inline; split into _literal_choices/_looks_like_choice_param/
  _quoted_choices and _param_info_entry/_first_example_snippet. Now IDEAL
  (all four topos pillars).
- serialization.py: _serialize_result_inner (37) was a 147-line type switch;
  split into one small serializer per result type (_serialize_visual,
  _serialize_ehrdata, _serialize_single_dataframe, ...). _json_safe (18) and
  _serialize_dataframe (16) got the same per-branch treatment. Now IDEAL.
- tools/dispatch_tools.py: get_edata_snapshot (18) had its struct/markdown
  building inlined; split into _edata_snapshot_payload and small formatting
  helpers. Now GOLD (SIMPLE achieved; composable/instability left as-is --
  this module's job is orchestrating five sibling modules, so its fan-out
  isn't fixable without a real architecture change).

Pure structural moves throughout: no behavior change. Verified after every
step with pytest tests/mcp (50/50), scripts/mcp_eval.py (44/44 live steps),
ruff, and topos_assess_worktree_change/topos_evaluate_file.
This fork carries upstream theislab/ehrapy's tag history (v0.14.0, v0.15.0,
...), and the version was already static (version = "0.15.0") despite a
[tool.hatch.version] source = "vcs" block claiming otherwise -- confirmed by
building before/after: the vcs config was dead, hatchling needs
dynamic = ["version"] for it to activate, so it silently no-op'd.

Reset to 0.1.0 as Krv-Labs' own MCP-focused release line, decoupled from
upstream's numbering, and dropped the dead vcs block rather than wire it up
properly -- a real tag-derived scheme would fight with upstream's tags on
future merges. Sets up the tag-triggered release workflow in the next commit.
…check

Adds a real release path for consuming this fork's ehrapy[mcp] from other
projects (e.g. Reyni) without waiting on a private package index:

- .github/actions/mcp-handshake-check: composite action that installs
  dist/*.whl with the mcp extra into a clean venv and drives the packaged
  server over stdio (list tools, load a demo dataset, run preprocessing,
  read it back, ingest a CSV). Verifies the shipped artifact, not just the
  dev checkout, which would catch missing package data, a broken
  console-script entry point, or an extras misconfiguration that pytest
  can't see.
- release.yml: was upstream's "on release published, publish to public
  PyPI" flow, which is wrong here since the package name collides with the
  real theislab/ehrapy on PyPI, and this fork has no trusted publisher
  configured for it anyway. Replaced with a "push tag v*.*.*" trigger that
  builds, runs the handshake check, and attaches dist/* to a GitHub Release
  via gh release create. Consume with:
    uv tool run --from "git+https://github.com/Krv-Labs/ehrapy.git@<tag>#egg=ehrapy[mcp]" ehrapy-mcp
- build.yml: runs the same handshake check on every push/PR to main, so a
  packaging regression is caught immediately rather than at the next tag.

Verified locally end-to-end: built the wheel, installed it with the mcp
extra into a throwaway venv, and ran the handshake logic against it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PC6jVtnaLvJS5mYNzE6XYF
@jeremy-wayland
jeremy-wayland changed the base branch from fix/mcp-housekeeping-and-cache-safety to main August 22, 2026 04:12
@jeremy-wayland
jeremy-wayland changed the base branch from main to fix/mcp-housekeeping-and-cache-safety August 22, 2026 04:14
@jeremy-wayland
jeremy-wayland merged commit 7d3ba5f into fix/mcp-housekeeping-and-cache-safety Aug 22, 2026
15 of 17 checks passed
jeremy-wayland added a commit that referenced this pull request Aug 22, 2026
…cache invariant tests (#17)

* fix(mcp): positional row sampling and avoid cache purge on server import

- Use iloc for positional sampling in _sample_rows to handle duplicate index labels without corruption and eliminate O(n^2) scan (fixes #16)
- Make cache purge optional in create_server and move startup purge to main CLI runner so importing server does not purge on-disk cache (fixes #16)
- Add CI invariant test asserting read-only MCP tools do not mutate shared in-memory EHRData instances (fixes #13)

* ci: trigger Check Build now that Actions are enabled for this fork

Empty commit. This fork's Actions were disabled by default (GitHub disables
Actions on forks until manually approved) until just now, so Check Build and
CPU tests have never actually run here -- not on this branch, not anywhere
in the fork's history. This commit's only purpose is to fire the
pull_request:synchronize event and confirm Check Build runs green for the
first time.

* refactor(mcp): topos quality sweep + release automation (#18)

* refactor(mcp): split run_dispatch into per-kind branch functions

run_dispatch had grown to a 434-line, complexity-103 function covering all
six dispatch kinds (demo, io-read, get, plot, io-write, edata) in one body.
Split along its own existing numbered comment boundaries into
_dispatch_demo/_dispatch_io_read/_dispatch_get/_dispatch_plot/
_dispatch_io_write/_dispatch_edata, plus small shared helpers for the
suggested-next/path-policy/op-log scaffolding every branch repeated.
run_dispatch is now a ~70-line linear guard-clause dispatcher.

Pure structural move: no behavior change, verified with pytest tests/mcp
(50/50) and topos_assess_worktree_change (IMPROVEMENT; max function
complexity 103 -> 13).

* refactor(mcp): extract helpers to clear topos SIMPLE gates in catalog/serialization/dispatch_tools

Same treatment as the run_dispatch split, applied to the next-worst offenders
from a topos_evaluate_project sweep of ehrapy/mcp/:

- catalog.py: _extract_choices (16) and function_help (15) each did several
  unrelated things inline; split into _literal_choices/_looks_like_choice_param/
  _quoted_choices and _param_info_entry/_first_example_snippet. Now IDEAL
  (all four topos pillars).
- serialization.py: _serialize_result_inner (37) was a 147-line type switch;
  split into one small serializer per result type (_serialize_visual,
  _serialize_ehrdata, _serialize_single_dataframe, ...). _json_safe (18) and
  _serialize_dataframe (16) got the same per-branch treatment. Now IDEAL.
- tools/dispatch_tools.py: get_edata_snapshot (18) had its struct/markdown
  building inlined; split into _edata_snapshot_payload and small formatting
  helpers. Now GOLD (SIMPLE achieved; composable/instability left as-is --
  this module's job is orchestrating five sibling modules, so its fan-out
  isn't fixable without a real architecture change).

Pure structural moves throughout: no behavior change. Verified after every
step with pytest tests/mcp (50/50), scripts/mcp_eval.py (44/44 live steps),
ruff, and topos_assess_worktree_change/topos_evaluate_file.

* chore(release): start this fork's own version line at 0.1.0

This fork carries upstream theislab/ehrapy's tag history (v0.14.0, v0.15.0,
...), and the version was already static (version = "0.15.0") despite a
[tool.hatch.version] source = "vcs" block claiming otherwise -- confirmed by
building before/after: the vcs config was dead, hatchling needs
dynamic = ["version"] for it to activate, so it silently no-op'd.

Reset to 0.1.0 as Krv-Labs' own MCP-focused release line, decoupled from
upstream's numbering, and dropped the dead vcs block rather than wire it up
properly -- a real tag-derived scheme would fight with upstream's tags on
future merges. Sets up the tag-triggered release workflow in the next commit.

* ci(mcp): tag-triggered GitHub Release + packaged-wheel MCP handshake check

Adds a real release path for consuming this fork's ehrapy[mcp] from other
projects (e.g. Reyni) without waiting on a private package index:

- .github/actions/mcp-handshake-check: composite action that installs
  dist/*.whl with the mcp extra into a clean venv and drives the packaged
  server over stdio (list tools, load a demo dataset, run preprocessing,
  read it back, ingest a CSV). Verifies the shipped artifact, not just the
  dev checkout, which would catch missing package data, a broken
  console-script entry point, or an extras misconfiguration that pytest
  can't see.
- release.yml: was upstream's "on release published, publish to public
  PyPI" flow, which is wrong here since the package name collides with the
  real theislab/ehrapy on PyPI, and this fork has no trusted publisher
  configured for it anyway. Replaced with a "push tag v*.*.*" trigger that
  builds, runs the handshake check, and attaches dist/* to a GitHub Release
  via gh release create. Consume with:
    uv tool run --from "git+https://github.com/Krv-Labs/ehrapy.git@<tag>#egg=ehrapy[mcp]" ehrapy-mcp
- build.yml: runs the same handshake check on every push/PR to main, so a
  packaging regression is caught immediately rather than at the next tag.

Verified locally end-to-end: built the wheel, installed it with the mcp
extra into a throwaway venv, and ran the handshake logic against it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PC6jVtnaLvJS5mYNzE6XYF

* ci: trigger Check Build against main to verify the new MCP handshake step

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>

* ci(gpu): stop GPU tests from firing on pull requests

This fork has no cirun-aws-gpu self-hosted runner provisioned, so the
"Triage: Check if GPU tests are allowed to run" gate could never actually
pass on a PR -- it always showed red, unrelated to whether the PR's own
changes were good. No branch protection currently requires this check, so
it wasn't blocking merges, but it made the PR checks list meaningless noise.

Dropped the pull_request trigger; kept push-to-main (upstream's original
scope) since that doesn't show up in PR checks either way.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PC6jVtnaLvJS5mYNzE6XYF

* docs: mark this repo as Krv-Labs' fork, point Source url at it

Apache 2.0 doesn't require this (LICENSE/attribution were already carried
through correctly -- hatchling bundles LICENSE into every wheel, authors and
the scanpy sub-license are untouched), but it's good practice and easy to
strip if this work goes up as a PR to theislab/ehrapy later:

- README: one-line fork notice above the badges.
- pyproject.toml: urls.Source now points at Krv-Labs/ehrapy instead of
  upstream, so anyone reading a built wheel's metadata finds the actual
  source it came from. Left Home-page/Documentation pointing at upstream,
  since those are genuinely still accurate (this fork has no separate docs
  site).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PC6jVtnaLvJS5mYNzE6XYF

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
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.

1 participant