diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json new file mode 100644 index 00000000..2a77670c --- /dev/null +++ b/.claude-plugin/marketplace.json @@ -0,0 +1,19 @@ +{ + "name": "model-serving-minefield", + "owner": { + "name": "Blackwellboy", + "url": "https://github.com/Blackwellboy" + }, + "metadata": { + "description": "Diagnose local LLM serving traps (vLLM, llama.cpp, Ollama, SGLang, MLX) from a symptom, your config files, or a live endpoint." + }, + "plugins": [ + { + "name": "minefield", + "source": "./", + "description": "Model Serving Minefield: the trap-registry skill plus the read-only minefield MCP server (symptom search, file scan, Doctor report interpretation). Runs the server with uvx, so nothing to install.", + "category": "debugging", + "keywords": ["llm-serving", "vllm", "llama.cpp", "ollama", "sglang", "diagnostics", "mcp"] + } + ] +} diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.json new file mode 100644 index 00000000..c9b7da16 --- /dev/null +++ b/.claude-plugin/plugin.json @@ -0,0 +1,19 @@ +{ + "name": "minefield", + "description": "Model Serving Minefield: the trap-registry skill plus the read-only minefield MCP server for diagnosing local LLM serving failures.", + "version": "0.2.2", + "author": { + "name": "Blackwellboy", + "url": "https://github.com/Blackwellboy" + }, + "homepage": "https://github.com/Blackwellboy/model-serving-minefield", + "repository": "https://github.com/Blackwellboy/model-serving-minefield", + "license": "MIT", + "keywords": ["llm-serving", "vllm", "llama.cpp", "ollama", "sglang", "diagnostics", "mcp"], + "mcpServers": { + "minefield": { + "command": "uvx", + "args": ["--from", "model-serving-minefield>=0.2.2,<0.3", "minefield-mcp"] + } + } +} diff --git a/.gitignore b/.gitignore index fb89a539..40f54f3a 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ hosts.yml *credentials*.json *cookies*.txt *cookies*.json +*.egg-info/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 98514aea..caf6c333 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,18 @@ CLI/MCP surface or the docs adds an entry at the top of this file, under a pull request that changes those paths without touching this file; label it `no-changelog` for a change nobody would look for here (typo, CI-only). +## 2026-12-04 - one command for anything, honest exit codes, `uvx msmf` + +- `minefield `: an endpoint URL (`http://...` or `host:port`) and/or files that exist and look like paths now route to `minefield diagnose` in lite mode (at most 5 live requests), with any remaining words as the symptom. Plain words still go to `guide`, and a bare word is never taken as a path, so `minefield logs are empty` is unchanged. In a terminal the routed command is printed to stderr. +- `diagnose` exits **3** when an endpoint was supplied but could not be reached, and the text view now says "Could not reach URL: nothing was tested live" with what to check. It used to exit 0 with "No canonical trap rose to the candidate list", which reads like a clean run. `--fail-on-problem` (opt-in, for CI) exits 1 when a live probe reports a PROBLEM; file and symptom leads never change the exit code. +- `diagnose` reads the endpoint key from `MINEFIELD_API_KEY` when `--api-key` is not given, so it stays out of shell history and `ps`. `OPENAI_API_KEY` is deliberately not read: that would send a cloud key to whatever local URL was typed. +- `minefield --version` prints the package version and trap count (it used to be parsed as `guide --version` and fail). +- The no-match hint suggests `minefield http://HOST:PORT/v1` (5 requests) instead of `quick` (the full Doctor). +- Alias package `msmf` 0.1.1 adds an `msmf` executable, so `uvx msmf ...` and `pipx run msmf ...` work. Needs publishing to take effect. +- [MCP setup](docs/MCP_SETUP.md) opens with a zero-install `uvx --from model-serving-minefield minefield-mcp` config that works in any MCP client. +- The repository is now a Claude Code plugin marketplace (`.claude-plugin/marketplace.json` and `plugin.json`): `claude plugin install minefield@model-serving-minefield` installs the existing skill plus the MCP server run through `uvx`. Checked with `claude plugin validate .` and a real install into an empty config, where the server reported Connected. +- Registry count and Doctor coverage are unchanged. + ## 2026-11-22 - secret scan on new commits, ignore rules for weights and secrets - CI gains a `secret-scan` job: gitleaks (checksum-pinned binary, values redacted) scans only the commits in each push or pull request. `.gitleaks.toml` allowlists known fake-credential fixtures. diff --git a/README.md b/README.md index 0c406e77..92998e9a 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,12 @@ minefield streaming shows blank replies --stack vllm (From a clone instead: `python -m pip install .`) +**One command for anything.** Give it a URL, files, a symptom, or all three: +`minefield http://HOST:PORT/v1` runs a live check of at most 5 requests, +`minefield ./start.sh ./logs/` scans files, and words are a symptom lookup. With +nothing installed: `uvx msmf http://HOST:PORT/v1`. See the +[CLI reference](docs/CLI_REFERENCE.md#one-command). + Not sure where to start? Run `minefield` on its own for a numbered menu of everything it can do. Each option says what it touches (nothing, files you name, or a set number of requests to your endpoint) before it runs. @@ -119,6 +125,10 @@ minefield quick --base-url http://HOST:PORT/v1 --json doctor.json plugin and type `/minefield` for the menu; the skill and MCP server also work. See [Hermes setup](docs/HERMES_SETUP.md). +**Claude Code user:** `/plugin marketplace add Blackwellboy/model-serving-minefield`, +then `/plugin install minefield@model-serving-minefield`. You get the skill and +the MCP server (run through `uvx`, nothing else to install). + **MCP user:** run `minefield-mcp` over stdio. It exposes search, report-interpretation, reproduction-plan, explicit-file inspection and a `list_menu` tool (the same numbered menu as the CLI); @@ -424,7 +434,7 @@ against 70 of this registry's 168 entries in under a minute: ```bash curl -sO https://raw.githubusercontent.com/Blackwellboy/model-serving-minefield/main/doctor/minefield_doctor.py -python3 minefield_doctor.py --base-url http://localhost:8000/v1 +python3 minefield_doctor.py --base-url http://HOST:PORT/v1 ``` Read-only and bounded: GET probes plus at most 12 small temperature-0 diff --git a/dist/MANIFEST.txt b/dist/MANIFEST.txt index f6f6dd34..95d11461 100644 --- a/dist/MANIFEST.txt +++ b/dist/MANIFEST.txt @@ -6,14 +6,14 @@ dist/MINEFIELD_AGENT_BUNDLE.md 709206 0e595e0c1f91dbf5e4fbd5ce7f6eb7987ee4564c39 dist/MINEFIELD_AGENT_BUNDLE_LITE.md 156835 479b8d1c35f13b7442f91f51acec9d1c096ead2f999722a378091a0766e6096d dist/MINEFIELD_REGISTRY.json 820767 f3c1ff8b3d0d773a8667af1c23ef4bdc357ca153a0a1987da29743946e4b4013 dist/MINEFIELD_REGISTRY.min.json 718403 713947fe9c2f9a8b8a12be7e4ccdd23866e87b34663529c48f838a02c8afb111 -docs/CLI_REFERENCE.md 8803 9d3e62fbec78a92b7744bb3824015e8943d8e316c43f169de5558f77de6f1db5 +docs/CLI_REFERENCE.md 10564 a8e160435f2c6e519cd7fadd3cc1c01b79e7e2dfb9b546b0b7121cc2bff33164 docs/DIAGNOSIS_CONTRACT.md 3240 b08dafb0891190efeae60d67f92c93ff23c03f0b95a29c6d37f5b661c14ec622 docs/DIAGNOSTIC_COVERAGE.md 1586 7845ecf742e55cf13e0092fec7ba4a1cc997acdc5ca458048771c4cf24cc3086 docs/GITHUB_ACTION_SCAN.md 5744 e188b0b2e6f5b3875cc90aee1369c81fd2d136b5028232ac6c157cc41f7e8a5c docs/HERMES_SETUP.md 3085 3672ac7d84c0913d3adabf1a54ddb1e174da0a8dbc5e339e522c1bccf87d3079 docs/LIBRARY_API.md 5419 211dc4034ecbb16e5a4ec11947a23b414dac7ad154c8f65f299cb3a1b1e90c56 docs/LIVE_MODEL_QUALIFICATION.md 8508 a6ce0d86b0d5fb5ca8cf06449d1ee1e10b2afab38d89d290747e0bea5498299a -docs/MCP_SETUP.md 1997 3298738be9cb90e9f2115f7d8d9feb641d7e32cd805b0c15b5001d59a9a6e177 +docs/MCP_SETUP.md 3277 ba42be23da52e41e3ff60e317ef84f492d063ecfd0de51afc68f6b4ac15dd9ad docs/PRIVACY_AND_SAFETY.md 912 e464c950efea90364eb1e0efe0ff9ebc7bc43aee128d582447371d0d56435825 docs/PRODUCT_ARCHITECTURE.md 2372 a06a93801fc63c3b7310b31210e9be4a788a36a369d1cced75f86514c09a22f9 docs/PRODUCT_BASELINE_20260730.md 3603 6a2059518021beae13ec0ef2aaf781b297a00261f94afb2d1869796bce62c8a2 @@ -30,7 +30,7 @@ minefield/__init__.py 713 e3ba29028534134b43175244e02985622db2e69319a50d2e810dbf minefield/__main__.py 48 935a1c1166b0c1ea35a82256345000bf2c73ded718d77773bc27a71ecce28f7d minefield/api.py 22140 bf2857b416d9b34e5c3d049048c643c915a1c756f6f6006a07e9cfd1a47958f1 minefield/blind_review.py 4241 6faf73dca9601b3acf0129489bcf12c0b74af28d0bf05774723feca7c0870f77 -minefield/cli.py 24005 25bf9d312b8e5bc3fc461ea2d2dfdec8ccb210a0215b4fe6f1c40cb74d856808 +minefield/cli.py 27956 681973d992b1f2408990034fd6f84646a3cb44e89d533b0ae72cc8fed0ea4320 minefield/context_lookup.py 3042 79ce2b28920ce334d825a0d941700454536b3af397bade9c86fc5940ed18c41b minefield/coverage.py 5887 5c390e719aad6254b1958a640a32e2ac50ba91587d6a3cf288f4f8db1dbebb1f minefield/data/UNVERIFIED_LEADS.json 71896 7458344b8d12b0424d9ac06210969aff591b147f54478313cbde7c0ed1ff955e @@ -52,7 +52,7 @@ minefield/probe_options.py 2597 011ad8a1994b41b58a668e06d4c7436f95126c2c05e649ac minefield/promotion_receipt.py 4382 8ac3f106755ad8cbcce1685dc290fa058db481aceb5890f3bfb79daa5a797d44 minefield/redaction.py 5487 4d9b98facb54a20a7b29611e19bf4d851890c12a5f64f6a1999c2566f086ef9f minefield/registry.py 12154 71cb91ee90f698e8513c928f13f398ecf2389cb897f54e55fe3314ab25ba5f16 -minefield/render.py 12820 178cb4d567f94b69df0468ec9d8393f544bcbc54f133a59df6c6105240778b18 +minefield/render.py 13214 f810167857a935e5ebf28606647f197b0c4dae3e10686039434718ea568a8a96 minefield/results_inspector.py 17621 7bc02c411e40bd60ca8bb1b8ed1b48a75d0a8d18b2746023ddc3f052fa23b362 minefield/safe_template.py 7329 9f69a574d6b7ec850ce1b4a1987b8a3afdb80912783af9e132be4df54b572d62 minefield/scan.py 19569 f90a4c73fc735beebfc1b67c434d53b7897f01dcc9cf0ed61577d8b7ced567a7 diff --git a/dist/SHA256SUMS b/dist/SHA256SUMS index 621f45b6..9d0d4678 100644 --- a/dist/SHA256SUMS +++ b/dist/SHA256SUMS @@ -1,19 +1,19 @@ 116e76f24d684efeff14c25c50e737917224597915829f25c1cfddb3138031a1 AGENT_START_HERE.md 3ff80219826911232226f354a3e4de1a4f6486eedf6fb88b49fb77c05f611a90 CORE.md -ac430a54ea5acbd012e891098a18a262701f6346a29fff27f325b4d8e4f47a0c MANIFEST.txt +bcc571f7d07f8cc247db02438ade6bbe821d5f2538961c92378c6b820084d98b MANIFEST.txt 5dbe60c30e78d106825bc8102d785625d11153445a1d8a0639a6668e25ea11f9 MINEFIELD_AGENT_BUNDLE.json 0e595e0c1f91dbf5e4fbd5ce7f6eb7987ee4564c393fec26e2ce54f65ee359ab MINEFIELD_AGENT_BUNDLE.md 479b8d1c35f13b7442f91f51acec9d1c096ead2f999722a378091a0766e6096d MINEFIELD_AGENT_BUNDLE_LITE.md f3c1ff8b3d0d773a8667af1c23ef4bdc357ca153a0a1987da29743946e4b4013 MINEFIELD_REGISTRY.json 713947fe9c2f9a8b8a12be7e4ccdd23866e87b34663529c48f838a02c8afb111 MINEFIELD_REGISTRY.min.json -9d3e62fbec78a92b7744bb3824015e8943d8e316c43f169de5558f77de6f1db5 docs/CLI_REFERENCE.md +a8e160435f2c6e519cd7fadd3cc1c01b79e7e2dfb9b546b0b7121cc2bff33164 docs/CLI_REFERENCE.md b08dafb0891190efeae60d67f92c93ff23c03f0b95a29c6d37f5b661c14ec622 docs/DIAGNOSIS_CONTRACT.md 7845ecf742e55cf13e0092fec7ba4a1cc997acdc5ca458048771c4cf24cc3086 docs/DIAGNOSTIC_COVERAGE.md e188b0b2e6f5b3875cc90aee1369c81fd2d136b5028232ac6c157cc41f7e8a5c docs/GITHUB_ACTION_SCAN.md 3672ac7d84c0913d3adabf1a54ddb1e174da0a8dbc5e339e522c1bccf87d3079 docs/HERMES_SETUP.md 211dc4034ecbb16e5a4ec11947a23b414dac7ad154c8f65f299cb3a1b1e90c56 docs/LIBRARY_API.md a6ce0d86b0d5fb5ca8cf06449d1ee1e10b2afab38d89d290747e0bea5498299a docs/LIVE_MODEL_QUALIFICATION.md -3298738be9cb90e9f2115f7d8d9feb641d7e32cd805b0c15b5001d59a9a6e177 docs/MCP_SETUP.md +ba42be23da52e41e3ff60e317ef84f492d063ecfd0de51afc68f6b4ac15dd9ad docs/MCP_SETUP.md e464c950efea90364eb1e0efe0ff9ebc7bc43aee128d582447371d0d56435825 docs/PRIVACY_AND_SAFETY.md a06a93801fc63c3b7310b31210e9be4a788a36a369d1cced75f86514c09a22f9 docs/PRODUCT_ARCHITECTURE.md 6a2059518021beae13ec0ef2aaf781b297a00261f94afb2d1869796bce62c8a2 docs/PRODUCT_BASELINE_20260730.md @@ -30,7 +30,7 @@ e3ba29028534134b43175244e02985622db2e69319a50d2e810dbf154b807dad minefield/__in 935a1c1166b0c1ea35a82256345000bf2c73ded718d77773bc27a71ecce28f7d minefield/__main__.py bf2857b416d9b34e5c3d049048c643c915a1c756f6f6006a07e9cfd1a47958f1 minefield/api.py 6faf73dca9601b3acf0129489bcf12c0b74af28d0bf05774723feca7c0870f77 minefield/blind_review.py -25bf9d312b8e5bc3fc461ea2d2dfdec8ccb210a0215b4fe6f1c40cb74d856808 minefield/cli.py +681973d992b1f2408990034fd6f84646a3cb44e89d533b0ae72cc8fed0ea4320 minefield/cli.py 79ce2b28920ce334d825a0d941700454536b3af397bade9c86fc5940ed18c41b minefield/context_lookup.py 5c390e719aad6254b1958a640a32e2ac50ba91587d6a3cf288f4f8db1dbebb1f minefield/coverage.py 7458344b8d12b0424d9ac06210969aff591b147f54478313cbde7c0ed1ff955e minefield/data/UNVERIFIED_LEADS.json @@ -52,7 +52,7 @@ c573be23c18571f4f6f11f72be220c75a925b33efd23fec96bb94288e9b5c6e7 minefield/mode 8ac3f106755ad8cbcce1685dc290fa058db481aceb5890f3bfb79daa5a797d44 minefield/promotion_receipt.py 4d9b98facb54a20a7b29611e19bf4d851890c12a5f64f6a1999c2566f086ef9f minefield/redaction.py 71cb91ee90f698e8513c928f13f398ecf2389cb897f54e55fe3314ab25ba5f16 minefield/registry.py -178cb4d567f94b69df0468ec9d8393f544bcbc54f133a59df6c6105240778b18 minefield/render.py +f810167857a935e5ebf28606647f197b0c4dae3e10686039434718ea568a8a96 minefield/render.py 7bc02c411e40bd60ca8bb1b8ed1b48a75d0a8d18b2746023ddc3f052fa23b362 minefield/results_inspector.py 9f69a574d6b7ec850ce1b4a1987b8a3afdb80912783af9e132be4df54b572d62 minefield/safe_template.py f90a4c73fc735beebfc1b67c434d53b7897f01dcc9cf0ed61577d8b7ced567a7 minefield/scan.py diff --git a/dist/model-serving-minefield-agent-pack.zip b/dist/model-serving-minefield-agent-pack.zip index 53138798..9b1e4bfa 100644 Binary files a/dist/model-serving-minefield-agent-pack.zip and b/dist/model-serving-minefield-agent-pack.zip differ diff --git a/docs/CLI_REFERENCE.md b/docs/CLI_REFERENCE.md index e9143dd3..61d1ed3f 100644 --- a/docs/CLI_REFERENCE.md +++ b/docs/CLI_REFERENCE.md @@ -1,7 +1,39 @@ # CLI reference +## One command + +Give `minefield` whatever you have. It works out what each argument is: + +```bash +minefield http://HOST:PORT/v1 # live check, at most 5 requests +minefield HOST:PORT # same; http:// is added +minefield ./start.sh docker-compose.yml # offline scan of those files +minefield streaming replies are blank # symptom lookup +minefield HOST:PORT ./start.sh tool calls come back as text # all three at once +uvx msmf http://HOST:PORT/v1 # the same, with nothing installed +``` + +A URL (`http://`, `https://`, or `host:port`) becomes `--base-url`. An argument +that exists on disk **and** looks like a path (contains `/` or `.`) is scanned: +write `./logs`, not `logs`, so `minefield logs are empty` stays a symptom. Any +URL or path routes to `minefield diagnose` in lite mode (the full command is +printed to stderr in a terminal); plain words go to `minefield guide`. + +Exit codes for `diagnose` (and so for the one-command form): `0` it ran, read +the result; `3` an endpoint was given but could not be reached, so nothing was +tested live; `2` bad input or no named file could be read; `1` a live probe +reported a PROBLEM, **only** with `--fail-on-problem` (for CI). File and symptom +matches are leads and never change the exit code. The API key is read from +`MINEFIELD_API_KEY` when `--api-key` is not given, so it stays out of shell +history and the process list. `minefield --version` prints the package version +and the number of traps it ships. + +## Every command + ```text -minefield quick --base-url URL [doctor options] +minefield [URL] [PATH...] [SYMPTOM...] (one command; see above) +minefield --version +minefield quick --base-url URL [doctor options] (full Doctor, dozens of requests) minefield inspect-config FILE... --allowed-root ROOT [--allowed-root ROOT...] minefield inspect-logs FILE... --allowed-root ROOT [--allowed-root ROOT...] minefield SYMPTOM... (shorthand for `minefield guide`) @@ -10,7 +42,8 @@ minefield guide SYMPTOM... [--stack STACK] [--model MODEL] [--version VERSION] [--log EXCERPT] [--limit N] [--text | --json] minefield diagnose [SYMPTOM...] [--files PATH...] [--base-url URL] [--stack STACK] [--model MODEL] [--version VERSION] - [--mode lite|doctor] [--max-requests N] [--probe ID] [--text | --json] + [--mode lite|doctor] [--max-requests N] [--probe ID] + [--api-key KEY | env MINEFIELD_API_KEY] [--fail-on-problem] [--text | --json] minefield bundle [--config FILE] [--log FILE] [--doctor-report FILE] [--output ZIP] [--no-write] minefield coverage [--json] diff --git a/docs/MCP_SETUP.md b/docs/MCP_SETUP.md index ce9013aa..bbd293e9 100644 --- a/docs/MCP_SETUP.md +++ b/docs/MCP_SETUP.md @@ -1,6 +1,30 @@ # MCP setup -Install the Python package first: +## Any MCP client, nothing to install + +If the client can run `uvx` (from [uv](https://docs.astral.sh/uv/)), point it at +the published package and skip the install step: + +```json +{ + "mcpServers": { + "minefield": { + "command": "uvx", + "args": ["--from", "model-serving-minefield", "minefield-mcp"] + } + } +} +``` + +That JSON shape works in Claude Desktop, Cursor, Windsurf, Cline and most other +clients. One-liners: `claude mcp add minefield -- uvx --from model-serving-minefield minefield-mcp`, +`codex mcp add minefield -- uvx --from model-serving-minefield minefield-mcp`, +`hermes mcp add minefield --command uvx --args --from model-serving-minefield minefield-mcp` +(check `hermes mcp add --help` on your version for the argument syntax). + +## Installed package + +Or install the Python package first: ```bash python -m pip install . @@ -60,7 +84,20 @@ Place this in `.cursor/mcp.json` for project scope: ## Claude Code and Claude Desktop -Claude Code: +Claude Code, as a plugin (the skill and the MCP server together; the server runs +through `uvx`): + +```bash +claude plugin marketplace add Blackwellboy/model-serving-minefield +claude plugin install minefield@model-serving-minefield +``` + +The plugin is this repository itself (`.claude-plugin/`), so an install copies +the whole tree, about 36 MB, and the agent can read every trap file offline. +Bump `version` in `.claude-plugin/plugin.json` with each release so installed +copies update. + +Claude Code, MCP server only: ```bash claude mcp add minefield -- minefield-mcp diff --git a/minefield/cli.py b/minefield/cli.py index 36919662..e2cc41c8 100644 --- a/minefield/cli.py +++ b/minefield/cli.py @@ -4,6 +4,8 @@ import argparse import json +import os +import re import sys from pathlib import Path from typing import Any @@ -30,6 +32,83 @@ ) +# Exit codes shared with hermes-minefield: 0 ran, 1 a live PROBLEM (only with +# --fail-on-problem), 2 bad input, 3 nothing could be tested (UNKNOWN). +EXIT_PROBLEM = 1 +EXIT_UNTESTED = 3 + +API_KEY_ENV = "MINEFIELD_API_KEY" # pragma: allowlist secret (an env var name, not a key) + +_URL_RE = re.compile(r"^https?://\S+$", re.IGNORECASE) +# HOST:PORT or HOST:PORT/v1 with no scheme; HOST is a name, IPv4 or [IPv6] +_HOSTPORT_RE = re.compile(r"^(localhost|\[[0-9a-f:]+\]|[0-9]{1,3}(\.[0-9]{1,3}){3}|[a-z0-9][a-z0-9.-]*):[0-9]{1,5}(/\S*)?$", + re.IGNORECASE) +# diagnose flags that take no value; every other flag consumes the next token. +_DIAGNOSE_BOOL_FLAGS = {"--json", "--text", "--fail-on-problem"} + + +def _as_url(token: str) -> str | None: + if _URL_RE.match(token): + return token + if _HOSTPORT_RE.match(token): + return "http://" + token + return None + + +def _as_path(token: str) -> bool: + """A token is a file to scan only if it exists AND looks like a path. + + `minefield logs are empty` must stay a symptom even when ./logs exists, so a + bare word never becomes a path: write ./logs, start.sh or a/b to scan it. + """ + looks_like_path = "/" in token or "." in token or token.startswith("~") + return looks_like_path and os.path.exists(os.path.expanduser(token)) + + +def route_anything(argv: list[str]) -> list[str] | None: + """`minefield `: route a URL and/or files (plus optional words) to diagnose. + + Returns diagnose argv, or None when nothing in argv is a URL or an existing + path, so the caller keeps treating it as a plain-words symptom. + """ + urls: list[str] = [] + paths: list[str] = [] + words: list[str] = [] + flags: list[str] = [] + i = 0 + while i < len(argv): + token = argv[i] + if token.startswith("-") and len(token) > 1: + flags.append(token) + if "=" not in token and token not in _DIAGNOSE_BOOL_FLAGS and i + 1 < len(argv): + flags.append(argv[i + 1]) + i += 1 + elif (url := _as_url(token)) is not None: + urls.append(url) + elif _as_path(token): + paths.append(token) + else: + words.append(token) + i += 1 + if not urls and not paths: + return None + if len(urls) > 1: + raise SystemExit("minefield: give at most one endpoint URL, got: " + ", ".join(urls)) + routed = ["diagnose", *words] + if paths: + routed += ["--files", *paths] + if urls: + routed += ["--base-url", urls[0]] + return routed + flags + + +def _version_text() -> str: + from . import __version__ + + registry = load_registry() + return f"minefield {__version__} (registry: {len(registry.get('entries') or [])} traps)" + + def _emit(value: Any, as_json: bool = True) -> None: if as_json: print(json.dumps(value, indent=2, sort_keys=True)) @@ -88,7 +167,8 @@ def parser() -> argparse.ArgumentParser: diag.add_argument("symptom", nargs="*", help="what you observe, in plain words") diag.add_argument("--files", nargs="+", default=[], help="configs, model folders, logs, or results to scan") diag.add_argument("--base-url", help="optional OpenAI-compatible endpoint for bounded live probes") - diag.add_argument("--api-key", help="optional endpoint API key; never included in output") + diag.add_argument("--api-key", help=f"optional endpoint API key; never included in output. " + f"Prefer the {API_KEY_ENV} environment variable, which stays out of shell history and ps") diag.add_argument("--stack") diag.add_argument("--model") diag.add_argument("--version") @@ -105,6 +185,9 @@ def parser() -> argparse.ArgumentParser: help="lite uses a small live budget; doctor runs the full endpoint catalogue") diag.add_argument("--max-requests", type=int, help="hard live request budget; lite defaults to 5, doctor defaults to uncapped") + diag.add_argument("--fail-on-problem", action="store_true", + help="exit 1 when a live probe reports a PROBLEM (for CI); " + "an unreachable endpoint always exits 3") diag_fmt = diag.add_mutually_exclusive_group() diag_fmt.add_argument("--json", action="store_true") diag_fmt.add_argument("--text", action="store_true") @@ -202,6 +285,9 @@ def main(argv: list[str] | None = None) -> int: argv = list(sys.argv[1:] if argv is None else argv) if not argv: argv = ["menu"] + if argv[0] in {"--version", "-V", "version"}: + print(_version_text()) + return 0 if argv[0] == "quick": return run_doctor(argv[1:]) if argv[0] == "menu": @@ -215,9 +301,17 @@ def main(argv: list[str] | None = None) -> int: print(menu.render("markdown" if margs.markdown else "text")) return 0 return menu.interactive(lambda chosen: main(list(chosen))) - # `minefield empty content when streaming` is shorthand for `minefield guide ...`. + # `minefield `: an endpoint URL and/or files go to diagnose (lite, + # at most 5 requests); plain words are shorthand for `minefield guide ...`. if argv and argv[0] not in {"-h", "--help"} and argv[0] not in _command_names(): - argv = ["guide", *argv] + routed = route_anything(argv) + if routed is not None: + if sys.stderr.isatty(): + shown = ["minefield", *routed] + print("running: " + " ".join(shown), file=sys.stderr) + argv = routed + else: + argv = ["guide", *argv] args = parser().parse_args(argv) registry = load_registry() if args.command == "inspect-config": @@ -283,7 +377,7 @@ def main(argv: list[str] | None = None) -> int: " ".join(part for part in symptom_parts if part).strip(), paths=args.files, base_url=args.base_url, - api_key=args.api_key, + api_key=args.api_key or os.environ.get(API_KEY_ENV) or None, stack=args.stack, model=args.model, version=args.version, @@ -308,6 +402,12 @@ def main(argv: list[str] | None = None) -> int: _emit(report) if args.files and not (report.get("file_scan") or {}).get("accepted_paths"): return 2 + target = report.get("detected_target") + if args.base_url and isinstance(target, dict) and target.get("reachable") is False: + # Nothing was tested live: never let that read as a clean run. + return EXIT_UNTESTED + if args.fail_on_problem and ((report.get("live_doctor") or {}).get("problems")): + return EXIT_PROBLEM elif args.command == "doctor-plan": # Do not detect an endpoint or read user infrastructure: this command # deliberately never invokes a network path, even when --hf-repo is set. diff --git a/minefield/render.py b/minefield/render.py index 70b7ca8b..c527a0f7 100644 --- a/minefield/render.py +++ b/minefield/render.py @@ -113,7 +113,7 @@ def wrap(label: str, text: str) -> list[str]: "Try:", " - describe what you observe, e.g. \"empty content when streaming\" or \"decode slows at long context\"", " - add the stack: --stack vllm (or llama.cpp, ollama, sglang, mlx)", - " - check a live endpoint: minefield quick --base-url http://HOST:PORT/v1", + " - check a live endpoint: minefield http://HOST:PORT/v1 (at most 5 requests)", ] else: out.append(style.dim("Ranked by how closely your description matches. A match is a lead to check, not a diagnosis.")) @@ -245,6 +245,11 @@ def render_unified_diagnosis( if target.get("build"): details.append(str(target["build"])) out.append("Target: " + " · ".join(details) + f" · {status}") + if not target.get("reachable"): + out.append(style.bold( + f"Could not reach {target.get('base_url') or 'the endpoint'}: nothing was tested live.")) + out.append(" Is the server running? Check the host and port, that the URL ends in /v1, and") + out.append(" that /v1/models answers (curl it). An API key goes in MINEFIELD_API_KEY.") scan_report = report.get("file_scan") if scan_report is not None: diff --git a/packages/msmf/README.md b/packages/msmf/README.md index 4490302e..b3309fb3 100644 --- a/packages/msmf/README.md +++ b/packages/msmf/README.md @@ -3,6 +3,7 @@ msmf This is a tiny alias package that depends on `model-serving-minefield` and exposes the same console scripts: +- `msmf` → `minefield.cli:main` (so `uvx msmf ...` and `pipx run msmf ...` work with no install step) - `minefield` → `minefield.cli:main` - `minefield-mcp` → `minefield.mcp_server:main` diff --git a/packages/msmf/pyproject.toml b/packages/msmf/pyproject.toml index cb18b8eb..b8229ff8 100644 --- a/packages/msmf/pyproject.toml +++ b/packages/msmf/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "msmf" -version = "0.1.0" +version = "0.1.1" description = "Thin alias that depends on model-serving-minefield and exposes the same CLIs" readme = "README.md" requires-python = ">=3.10" @@ -30,6 +30,9 @@ Issues = "https://github.com/Blackwellboy/model-serving-minefield/issues" Changelog = "https://github.com/Blackwellboy/model-serving-minefield/blob/main/CHANGELOG.md" [project.scripts] +# `uvx msmf ` / `pipx run msmf ` run the tool with no install step: +# both look for an executable named like the package. +msmf = "minefield.cli:main" minefield = "minefield.cli:main" minefield-mcp = "minefield.mcp_server:main" diff --git a/tests/test_one_command.py b/tests/test_one_command.py new file mode 100644 index 00000000..28a7f7b4 --- /dev/null +++ b/tests/test_one_command.py @@ -0,0 +1,105 @@ +"""`minefield `: a URL and/or files route to diagnose, words stay a symptom.""" + +from __future__ import annotations + +import os +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +from minefield.cli import route_anything + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "doctor" / "tests")) + +from fixture_server import FixtureLane # noqa: E402 + + +def _cli(*args: str, cwd: Path = ROOT, env_extra: dict | None = None) -> subprocess.CompletedProcess: + env = {k: v for k, v in os.environ.items() if k != "MINEFIELD_API_KEY"} + env.update({"PYTHONPATH": str(ROOT), **(env_extra or {})}) + return subprocess.run( + [sys.executable, "-m", "minefield", *args], + capture_output=True, text=True, env=env, cwd=cwd, check=False, timeout=120, + ) + + +class RouteTests(unittest.TestCase): + def test_plain_words_stay_a_symptom(self): + self.assertIsNone(route_anything(["streaming", "shows", "blank", "replies"])) + + def test_bare_word_that_is_a_directory_is_still_a_word(self): + with tempfile.TemporaryDirectory() as tmp: + os.makedirs(os.path.join(tmp, "logs")) + old = os.getcwd() + os.chdir(tmp) + try: + self.assertIsNone(route_anything(["logs", "are", "empty"])) + self.assertEqual(route_anything(["./logs"]), ["diagnose", "--files", "./logs"]) + finally: + os.chdir(old) + + def test_url_files_words_and_flags(self): + with tempfile.TemporaryDirectory() as tmp: + start = os.path.join(tmp, "start.sh") + Path(start).write_text("vllm serve x\n") + routed = route_anything( + ["localhost:8000", start, "tool", "calls", "--stack", "vllm", "--json"]) + self.assertEqual(routed, [ + "diagnose", "tool", "calls", "--files", start, + "--base-url", "http://localhost:8000", "--stack", "vllm", "--json", + ]) + + def test_schemes_and_hosts(self): + for token, url in ( + ("http://gpu:8000/v1", "http://gpu:8000/v1"), + ("https://example.test/v1", "https://example.test/v1"), + ("127.0.0.1:30000/v1", "http://127.0.0.1:30000/v1"), + ("localhost:9", "http://localhost:9"), + ): + self.assertEqual(route_anything([token])[-1], url, token) + + def test_two_urls_is_an_error(self): + with self.assertRaises(SystemExit): + route_anything(["localhost:1", "localhost:2"]) + + +class OneCommandCliTests(unittest.TestCase): + def test_version(self): + proc = _cli("--version") + self.assertEqual(proc.returncode, 0) + self.assertRegex(proc.stdout, r"^minefield \d+\.\d+\.\d+ \(registry: \d+ traps\)") + + def test_unreachable_endpoint_is_untested_not_clean(self): + proc = _cli("localhost:9", "--text") + self.assertEqual(proc.returncode, 3, proc.stdout + proc.stderr) + self.assertIn("Could not reach http://localhost:9/v1", proc.stdout) + self.assertIn("nothing was tested live", proc.stdout) + + def test_url_runs_lite_and_fail_on_problem_exits_1(self): + with FixtureLane(stream_channel="reasoning") as url: + proc = _cli(url, "--text") + self.assertEqual(proc.returncode, 0, proc.stderr) + self.assertIn("Live: 5 request(s)", proc.stdout) + self.assertIn("Trap 23", proc.stdout) + strict = _cli(url, "--json", "--fail-on-problem") + self.assertEqual(strict.returncode, 1, strict.stderr) + + def test_clean_lane_with_fail_on_problem_exits_0(self): + # The fixture's defaults carry two defects (77, 57); turn both off. + with FixtureLane(validates_top_level=True, string_false_truthy=False) as url: + proc = _cli(url, "--json", "--fail-on-problem") + self.assertEqual(proc.returncode, 0, proc.stderr) + + def test_api_key_from_environment_is_sent_and_never_printed(self): + secret = "sk-SENTINEL-minefield-env-key" # pragma: allowlist secret (fake fixture key) + with FixtureLane() as url: + proc = _cli(url, "--json", env_extra={"MINEFIELD_API_KEY": secret}) + self.assertEqual(proc.returncode, 0, proc.stderr) + self.assertNotIn(secret, proc.stdout + proc.stderr) + + +if __name__ == "__main__": + unittest.main()