M58 (TASK-115-FREE): LLM-Safe JSON Envelope, Structured Error Codes & Compliance Control Matrix¶
1. Purpose & Architectural Decisions¶
- Problem: many LLM bash harnesses treat non-zero exit codes (such as exit
1when security findings exist) as command execution failures and retry 2–3 times, duplicating scans and confusing the caller. - Decision (P0.3): upgrade
--format jsononverity scanandverity testto emit a deterministic LLM-safe top-level envelope, standardize structured error codes withnext_actionhints, and upgrade--compliancefrom finding tags to a full Control Coverage Matrix. - Deterministic truth invariant: for identical findings,
status,exit_code, andcommand_succeededare derived only from the finding set and exit code — never from wall-clock time, ordering, or retry count.command_succeeded == Truewhenever the command executed and produced a report (exit0or1), andFalseonly on exit2. - Free/Pro boundary: Free owns the envelope, the
ERR_*taxonomy, and the single-repo control coverage matrix. Pro owns cross-repo control rollups and signed compliance audit bundles.
2. Target Files & Technical Deliverables¶
| File | Deliverable |
|---|---|
src/verity/cli.py |
Emit the top-level envelope for verity scan/verity test --format json; map operational failures to ERR_* + next_action; keep stdout JSON-only. |
src/verity/discovery/auditor.py |
Produce per-control coverage buckets (tested_passed, tested_failed, not_applicable_static) for --compliance. |
src/verity/reporting/markdown.py |
Render the control coverage matrix in Markdown output. |
tests/unit/cli/test_json_envelope_and_compliance_matrix.py |
New ATDD suite (below). |
Envelope fields (top level, both commands): "schema_version": "1.0", "status": "clean" | "findings_detected" | "execution_error", "exit_code": int, "command_succeeded": bool.
Structured error codes (with a next_action hint string): ERR_MISSING_CREDENTIALS, ERR_MODEL_NOT_FOUND, ERR_RATE_LIMITED, ERR_MCP_SERVER_TIMEOUT, ERR_MCP_SCHEMA_DRIFT, ERR_CONFIG_PARSE.
Control Coverage Matrix: per OWASP MCP Top 10 / LLM Top 10 control ID, a status of tested_passed, tested_failed, or not_applicable_static — replacing finding-only tagging.
3. ATDD Traceability Matrix¶
| AC ID | Acceptance Criterion | Paired Benign / Negative Control | Verification Tier | Test Target |
|---|---|---|---|---|
AC-58.1 |
verity scan --format json sets schema_version == "1.0", status == "findings_detected", exit_code == 1, command_succeeded == True when findings are detected. |
Clean target sets status == "clean", exit_code == 0, command_succeeded == True. |
unit |
tests/unit/cli/test_json_envelope_and_compliance_matrix.py::test_json_envelope_exit_semantics |
AC-58.2 |
Operational failures (exit 2) emit status == "execution_error", exit_code == 2, command_succeeded == False, plus the matching ERR_* code and non-empty next_action. |
A successfully executed scan with findings never reports execution_error (no false retry trigger). |
unit |
tests/unit/cli/test_json_envelope_and_compliance_matrix.py::test_structured_error_codes_and_next_action |
AC-58.3 |
verity scan --compliance --format json emits the OWASP control coverage matrix with explicit tested_passed, tested_failed, and not_applicable_static buckets. |
Findings-only tagging without a coverage matrix fails the assertion. | unit |
tests/unit/cli/test_json_envelope_and_compliance_matrix.py::test_compliance_control_coverage_matrix |
AC-58.4 |
Lint and the full unit suite pass. | Clean workspace yields zero lint errors. | ruff |
uv run ruff check src tests scripts && uv run pytest tests/ |
4. Verification Commands¶
uv run pytest tests/unit/cli/test_json_envelope_and_compliance_matrix.py
uv run pytest tests/
uv run ruff check src tests scripts
5. Execution Checklist¶
- [ ]
AC-58.1verified green - [ ]
AC-58.2verified green - [ ]
AC-58.3verified green - [ ]
AC-58.4verified green - [ ] Board #5 item
PVTI_lAHOD-xmQM4Br351zhOMDCMsynced & status updated