You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 3c6009c
Browse filesBrowse the repository at this point in the historyBrowse files
chore(python): per-file coverage gate in CI and local:gate
Add `npm run coverage:py` (scripts/coverage-py.mjs): for each Python
server, a locked sync, then `uv run --frozen pytest --cov
--cov-report=term-missing --cov-report=json`, then a per-file check of
coverage.json (scripts/lib/py-coverage.mjs) that fails any file below 90%
on lines or on branches. coverage.py's fail_under is global, hence the
script. Discovery, server selection and the carry-on-after-a-failure shape
are shared with validate-py.mjs.
Wire it in as a parallel `coverage` matrix job in python.yml and as a
stage of local:gate:stages after validate:py, and document it in
docs/quality-gate.md, AGENTS.md (the Python half of the coverage rule),
CONTRIBUTING.md and the pre-push-gate and testing skills.
Signed-off-by: cliffhall <cliff@futurescale.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: .claude/skills/pre-push-gate/SKILL.md
+37-3Lines changed: 37 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -75,7 +75,8 @@ unquoted `#` or `:` in a `description`.
75
75
`verify:skills:cli` is the authoritative validator. It fetches the pinned
76
76
Claude Code CLI with `npx` when the installed one is a different version, so it
77
77
**fails offline**. (`validate:py`'s `sync` step is the other stage that can:
78
-
`uv sync --locked` downloads when a server's environment is missing or stale.)
78
+
`uv sync --locked` downloads when a server's environment is missing or stale.
79
+
`coverage:py` opens with the same sync, a no-op once `validate:py` has run.)
79
80
80
81
### `verify:typecheck-coverage`
81
82
@@ -171,6 +172,38 @@ the last `[validate:py] <server>: <step>` line above it. Re-run one server with
171
172
172
173
`uv: command not found` means `uv` is not installed. Never substitute `pip`.
173
174
175
+
### `coverage:py`
176
+
177
+
The Python per-file coverage gate: every file each server measures must reach
178
+
**90% on lines and 90% on branches**. The summary names the server, the step,
179
+
and for a shortfall each file and dimension below 90:
180
+
181
+
```
182
+
FAIL time — per-file check (2 below 90%)
183
+
src/time/src/mcp_server_time/server.py: lines 84.2% (75/89) is below 90%
184
+
src/time/src/mcp_server_time/server.py: branches 78.5% (11/14) is below 90%
185
+
```
186
+
187
+
Re-run one server with `npm run coverage:py -- <server>`. The per-file table
188
+
above the summary gives both figures for every file, and coverage.py's own
189
+
report above that (`term-missing`) lists the uncovered lines and partial
190
+
branches (`120->124` is the jump from line 120 to 124 never taken).
191
+
192
+
| Step | Fix |
193
+
| --- | --- |
194
+
|`sync`| As for `validate:py`: run `uv lock` in that server and commit the lockfile |
195
+
|`pytest --cov`| A test failed, so no verdict was reached; fix it as for `validate:py`'s `pytest`. `unrecognized arguments: --cov` means `pytest-cov` is missing from the server's dev dependencies |
196
+
|`per-file check`, "below 90%" | Write tests for the lines and branches the report lists. Drive the server in-process through a `ClientSession` where you can (`/testing`) |
197
+
|`per-file check`, "was not written" or "measured no files" | The run produced no usable `coverage.json`: check the server's `[tool.coverage.run]` (`branch = true`, `source` naming its package) |
198
+
|`per-file check`, "no covered_branches/num_branches" |`branch = true` is missing from `[tool.coverage.run]`|
199
+
200
+
**Never lower the gate**, and never drop a file from `source` to clear it.
201
+
Code that genuinely cannot run (a guard the types already rule out, a branch
202
+
the SDK's input validation makes unreachable) is marked at the source with
203
+
`# pragma: no cover # <reason>`; every pragma carries its reason
204
+
(`AGENTS.md`**Always test new or modified code**). A pragma on a line that is
205
+
merely awkward to test is not justified; write the test.
206
+
174
207
### `smoke`
175
208
176
209
`smoke — FAIL <server> over <transport>` followed by the reason and the tail
@@ -221,8 +254,9 @@ The details of the lease are in
221
254
222
255
## Not part of the gate
223
256
224
-
`npm run skills:eval` (real model calls), `npm run coverage` (a report; no
225
-
threshold is enforced yet) and `npm run pack:verify` (installs each package's
257
+
`npm run skills:eval` (real model calls), `npm run coverage` (the TypeScript
258
+
servers' report; no threshold is enforced on them yet; the Python servers' gate
259
+
is the `coverage:py` stage above) and `npm run pack:verify` (installs each package's
226
260
publish artifact and boots it; needs the network, and belongs to the release
227
261
flow) are separate commands. Run the eval suite when you
|`fetch`, `git`, `time`|`src/<server>/tests/`|`test_<subject>.py`| pytest, with `pytest-asyncio` (`asyncio_mode = "auto"`) |
31
31
| Root tooling |`scripts/`, beside the script |`<name>.test.mjs`|`node --test`|
32
32
33
33
Each vitest config includes only `**/__tests__/**/*.test.ts`, so a test file
@@ -43,10 +43,9 @@ helpers go in `__tests__/` without the `.test` suffix, as
43
43
|`filesystem`| Unit tests of `lib.ts` and the path helpers, plus an SDK `Client` over `StdioClientTransport` that spawns the built `dist/index.js`|
44
44
|`sequentialthinking`| Unit tests of `lib.ts`, plus an SDK `Client` over stdio against `dist/index.js`|
45
45
|`memory`| Direct calls on the exported `KnowledgeGraphManager` and `register…` functions, with a mocked server |
46
-
|`fetch`, `git`, `time`|Direct calls on the functions in `server.py`, with `unittest.mock`|
46
+
|`fetch`, `git`, `time`|A `ClientSession` linked in-process to the real `serve()` over in-memory streams (below), plus direct calls on the pure functions in `server.py`|
47
47
48
-
No test in the repo uses an in-memory transport yet, and no Python test opens
49
-
a `ClientSession`.
48
+
No TypeScript test uses an in-memory transport yet.
50
49
51
50
## Choosing a harness for a new test
52
51
@@ -168,44 +167,71 @@ the wire. The `filesystem` stdio tests and `memory`'s dist-layout test have no
168
167
such guard and **fail**. A skipped test in this repo usually means "not
169
168
built".
170
169
171
-
### Python: `ClientSession` over stdio
170
+
### Python: `ClientSession` over in-memory streams, in-process
172
171
173
-
Each Python server constructs its `Server` inside `serve()`, so it cannot be
174
-
handed to an in-memory session. A protocol-level test spawns the module and
175
-
talks to it through the SDK's stdio client:
172
+
Each Python server builds its `Server` inside `serve()` and serves it over the
173
+
`stdio_server()` it imports into `server.py`. A test replaces that one name
174
+
with a context manager that yields the server side of a pair of in-memory
175
+
streams, runs the real `serve()` in a task group, and opens a `ClientSession`
176
+
on the client side. No process, no build, and the SDK's input validation and
177
+
error mapping run exactly as they do for a real client:
176
178
177
179
```python
178
-
import sys
180
+
from contextlib import asynccontextmanager
181
+
from typing import Any, AsyncIterator
179
182
180
183
import anyio
181
-
from mcp import ClientSession, StdioServerParameters
result =await session.call_tool("get_current_time", {"timezone": "UTC"})
212
+
assert result.isError isFalse
213
+
```
214
+
215
+
-**Patch the name `server.py` looks up** (`mcp_server_<name>.server.stdio_server`),
216
+
not `mcp.server.stdio.stdio_server`: `server.py` imported the function, so
217
+
the module attribute is what `serve()` calls.
218
+
-**Cancel the task group after the session closes.**`serve()` runs until its
219
+
streams end, so without the cancel the test never returns.
220
+
-**Assert on the wire shape where you can**, for example
221
+
`result.model_dump(by_alias=True, mode="json")`, rather than on Python
222
+
attribute names: the SDK v2 port (#4851) renames them, and a wire-level
223
+
assertion survives it.
224
+
-**No network in a test.**`fetch` replaces `httpx.AsyncClient` with one on an
225
+
`httpx.MockTransport`; `git` works against a temporary repository its
226
+
fixture creates and closes before removing.
227
+
228
+
`pytest-asyncio` with `asyncio_mode = "auto"` is a dev dependency of all three,
229
+
so an `async def test_…` runs as it stands.
230
+
231
+
A subprocess test over stdio (`StdioServerParameters(command=sys.executable,
232
+
args=["-m", "mcp_server_<name>"])` with `mcp.client.stdio.stdio_client`) is
233
+
kept to a thin smoke of the entry point. coverage.py does not measure a child
234
+
process, so code reached only that way reads as uncovered.
209
235
210
236
## Running them
211
237
@@ -216,8 +242,10 @@ than adding a dependency for one test.
216
242
| Every TypeScript server |`npm test` at the root |
217
243
| One TypeScript server's whole chain |`npm run validate -w src/<server>` (format check, lint, typecheck, build, test) |
218
244
| One Python server |`uv run pytest` in `src/<server>`|
219
-
| One Python test |`uv run pytest <path to the test file>::<name>` in `src/<server>` (for example `tests/test_server.py::test_git_checkout_existing_branch`, or `test/time_server_test.py::…` in `time`) |
245
+
| One Python test |`uv run pytest <path to the test file>::<name>` in `src/<server>` (for example `tests/test_server.py::test_git_checkout_existing_branch`) |
220
246
| One Python server's whole chain |`npm run validate:py -- <server>`|
247
+
| One Python server's coverage gate |`npm run coverage:py -- <server>` at the root |
248
+
| Every Python server's coverage gate |`npm run coverage:py` at the root |
221
249
| Root tooling |`npm run test:scripts`|
222
250
223
251
`npm run typecheck -w src/<server>` is the only step that typechecks the test
@@ -235,12 +263,30 @@ npm run coverage -w src/<server> # one server
235
263
npm run coverage # all four
236
264
```
237
265
238
-
Today the report is **informational**. No vitest config sets a threshold, so
239
-
`coverage` fails only when a test fails, and neither `validate` nor CI runs it.
240
-
The Python servers have no coverage tooling at all: `pytest-cov` is not a dev
241
-
dependency of any of them. Read the report to find what a change left
266
+
For the TypeScript servers the report is **informational** today. No vitest
267
+
config sets a threshold, so `coverage` fails only when a test fails, and
268
+
neither `validate` nor CI runs it. Read the report to find what a change left
242
269
untested; do not describe it as a gate.
243
270
271
+
**The Python servers' coverage is a gate.**`npm run coverage:py -- <server>`
272
+
runs, in `src/<server>`:
273
+
274
+
```sh
275
+
uv run --frozen pytest --cov --cov-report=term-missing --cov-report=json
276
+
```
277
+
278
+
which prints coverage.py's report (the missing lines and partial branches)
279
+
and writes `coverage.json` (ignored by git). `scripts/lib/py-coverage.mjs` then
280
+
checks **every file** in it: at least 90% of its lines and 90% of its branches,
281
+
each file on its own. What is measured is the server's `[tool.coverage.run]`
282
+
(`branch = true`, `source` set to its package). `local:gate` and CI's
283
+
**Coverage \<server\>** job both run it. It is kept out of the plain
284
+
`uv run pytest` loop, which stays fast and uninstrumented.
285
+
286
+
A line or branch that genuinely cannot run is marked
287
+
`# pragma: no cover # <reason>`, with the reason on the same line; never
288
+
lower the gate. Diagnosing a red run is `/pre-push-gate`.
289
+
244
290
⚠️ **A spawned server is invisible to the report.** v8 coverage measures the
245
291
vitest process, not a child it starts, so code reached only through a stdio
246
292
test reads as uncovered: `sequentialthinking`'s `index.ts` reports 0% while its
0 commit comments