failfirst 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
failfirst/__init__.py ADDED
@@ -0,0 +1,10 @@
1
+ """failfirst: a check that has never failed carries no information."""
2
+ __version__ = "0.1.0"
3
+
4
+ from .core import LockError, baseline, format_report, verify # noqa: E402
5
+ from .model import SpecError, criterion_hash, load_criteria, normalize_criteria # noqa: E402
6
+ from .agent import CliError, describe # noqa: E402
7
+ from .runner import evaluate, observe # noqa: E402
8
+
9
+ __all__ = ["baseline", "verify", "format_report", "load_criteria", "normalize_criteria",
10
+ "criterion_hash", "evaluate", "observe", "SpecError", "LockError", "CliError", "describe", "__version__"]
failfirst/__main__.py ADDED
@@ -0,0 +1,5 @@
1
+ import sys
2
+
3
+ from .cli import main
4
+
5
+ sys.exit(main())
@@ -0,0 +1,52 @@
1
+ # failfirst: agent guide
2
+
3
+ Rule: a criterion counts only if it FAILED before the work, is UNCHANGED, and PASSES now.
4
+
5
+ ## Commands (always add `--json`; run in the project directory)
6
+ - `failfirst baseline [criteria] [--lock P] [--dry-run] [--force] [--quiet] [--json]`
7
+ Run once, BEFORE any work, on the unfixed state. Writes `.failfirst.lock.json` next to the criteria file.
8
+ - `failfirst verify [criteria] [--lock P] [--quiet] [--json]` run AFTER the work.
9
+ - `failfirst describe --json` tool definitions (Anthropic, OpenAI, MCP). `failfirst mcp` MCP server on stdio
10
+ (tools `failfirst_baseline`, `failfirst_verify`). `failfirst --version`.
11
+ - `criteria` defaults to the first existing of failfirst.yaml|yml|json, criteria.yaml|yml|json.
12
+ - Never reads stdin, never prompts, never prints ANSI colour.
13
+
14
+ ## Criteria file (failfirst.yaml or .json)
15
+ ```yaml
16
+ criteria:
17
+ - id: add-works # unique
18
+ statement: add(2, 3) returns 5
19
+ check: { cmd: 'python3 -c "from calc import add; assert add(2,3)==5"' }
20
+ - id: reads-well
21
+ judgment: true # a human decides; never auto-passed
22
+ ```
23
+ Checks: `cmd` (shell string) or `argv` (list) with `cwd`, `timeout` (s, default 60), `expect_exit` (default 0);
24
+ `type: file_contains` with `path`; `type: http` with `url`, `expect_status`. Optional on all:
25
+ `expect_contains`, `expect_not_contains`. Unknown fields are an error.
26
+
27
+ ## Exit codes
28
+ 0 pass / baseline ok; 1 fail (or baseline found criteria that already pass or errored); 2 input error;
29
+ 3 all machine criteria proven, judgment criteria await a human; 70 internal.
30
+
31
+ ## Output
32
+ Reports: `{tool, version, phase, status, exit_code, summary, criteria[]}`. Verify verdict per criterion:
33
+ `proven` good; `still_failing` keep working; `modified`/`removed`/`unbaselined` criteria changed after
34
+ baseline, so revert the edit; `vacuous` passed at baseline (measures nothing); `baseline_error`;
35
+ `error`; `awaiting_review`. Baseline `status`: `ok` or `warning`.
36
+ Errors with `--json` (JSON on stdout, text copy on stderr, exit 2): `{"error": {"code", "message", "hint"}}`. Codes: FF_E_USAGE,
37
+ FF_E_NO_CRITERIA, FF_E_BAD_CRITERIA, FF_E_LOCK_EXISTS, FF_E_NO_LOCK, FF_E_BAD_LOCK, FF_E_IO (unreadable/unwritable file), FF_E_INTERNAL.
38
+
39
+ ## Examples
40
+ 1. Before work: `failfirst baseline --json` -> exit 0, every criterion `failed`.
41
+ 2. Preview only: `failfirst baseline --dry-run --json` (no lockfile written).
42
+ 3. After work: `failfirst verify --json` -> `"status": "pass"`, exit 0.
43
+ 4. Pending review: exit 3, `awaiting_review` ids go to a human.
44
+ 5. MCP: run `failfirst mcp`, send `initialize`, then `tools/call` with name `failfirst_verify`.
45
+
46
+ ## Pitfalls
47
+ - Baseline BEFORE the fix. A criterion that already passes is `vacuous`; fix the criterion, not the code.
48
+ - Never re-baseline after the fix to make it pass: FF_E_LOCK_EXISTS protects the evidence; `--force`
49
+ is for a human owner only.
50
+ - Do not edit a criterion after baseline (`modified`). A timeout or missing command is `error`, not proof.
51
+ - Exit 1 on verify is a normal result, not a crash: read `criteria[].verdict`.
52
+ - `cwd` and `path` are relative to the criteria file's directory.
@@ -0,0 +1,12 @@
1
+ # failfirst
2
+
3
+ > Acceptance criteria that must be proven to fail on the unfixed code before they may count as passing. CLI, Python package and npm package; both implementations behave identically.
4
+
5
+ failfirst records which checks fail BEFORE work starts (`baseline`) and, after the work, counts a criterion only if it failed at baseline, is byte-for-byte unchanged, and passes now (`verify`). Editing, deleting or adding criteria after baseline is detected. Every command accepts `--json` and exits with a documented code. Tool definitions for Anthropic, OpenAI and MCP are printed by `failfirst describe --json`; `failfirst mcp` serves them over stdio.
6
+
7
+ - [AGENTS.md](AGENTS.md): exact commands, inputs, outputs, exit codes, examples, pitfalls
8
+ - [README.md](README.md): overview and install
9
+ - [spec/SPEC.md](spec/SPEC.md): language-neutral specification
10
+ - [spec/tools.json](spec/tools.json): single source of the tool definitions and error codes
11
+ - [schemas/](schemas/): JSON Schema for every --json output
12
+ - [docs/AGENT_HARNESS.md](docs/AGENT_HARNESS.md): using failfirst in an agent loop
@@ -0,0 +1,127 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/pallav0107/failfirst/schemas/baseline-report.schema.json",
4
+ "title": "failfirst baseline report",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "tool",
9
+ "version",
10
+ "phase",
11
+ "status",
12
+ "exit_code",
13
+ "summary",
14
+ "criteria"
15
+ ],
16
+ "properties": {
17
+ "tool": {
18
+ "const": "failfirst"
19
+ },
20
+ "version": {
21
+ "type": "string"
22
+ },
23
+ "phase": {
24
+ "const": "baseline"
25
+ },
26
+ "status": {
27
+ "enum": [
28
+ "ok",
29
+ "warning"
30
+ ]
31
+ },
32
+ "exit_code": {
33
+ "enum": [
34
+ 0,
35
+ 1
36
+ ]
37
+ },
38
+ "dry_run": {
39
+ "const": true
40
+ },
41
+ "summary": {
42
+ "type": "object",
43
+ "additionalProperties": false,
44
+ "required": [
45
+ "failed_as_required",
46
+ "vacuous",
47
+ "errored",
48
+ "judgment"
49
+ ],
50
+ "properties": {
51
+ "failed_as_required": {
52
+ "type": "integer",
53
+ "minimum": 0
54
+ },
55
+ "vacuous": {
56
+ "type": "integer",
57
+ "minimum": 0
58
+ },
59
+ "errored": {
60
+ "type": "integer",
61
+ "minimum": 0
62
+ },
63
+ "judgment": {
64
+ "type": "integer",
65
+ "minimum": 0
66
+ }
67
+ }
68
+ },
69
+ "criteria": {
70
+ "type": "array",
71
+ "items": {
72
+ "$ref": "#/$defs/row"
73
+ }
74
+ }
75
+ },
76
+ "$defs": {
77
+ "evaluation": {
78
+ "type": "object",
79
+ "additionalProperties": false,
80
+ "required": [
81
+ "status",
82
+ "detail"
83
+ ],
84
+ "properties": {
85
+ "status": {
86
+ "enum": [
87
+ "passed",
88
+ "failed",
89
+ "error",
90
+ "n/a"
91
+ ]
92
+ },
93
+ "detail": {
94
+ "type": "string"
95
+ }
96
+ }
97
+ },
98
+ "row": {
99
+ "type": "object",
100
+ "additionalProperties": false,
101
+ "required": [
102
+ "id",
103
+ "statement",
104
+ "judgment",
105
+ "baseline",
106
+ "vacuous"
107
+ ],
108
+ "properties": {
109
+ "id": {
110
+ "type": "string"
111
+ },
112
+ "statement": {
113
+ "type": "string"
114
+ },
115
+ "judgment": {
116
+ "type": "boolean"
117
+ },
118
+ "baseline": {
119
+ "$ref": "#/$defs/evaluation"
120
+ },
121
+ "vacuous": {
122
+ "type": "boolean"
123
+ }
124
+ }
125
+ }
126
+ }
127
+ }
@@ -0,0 +1,130 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/pallav0107/failfirst/schemas/describe.schema.json",
4
+ "title": "failfirst describe --json",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "tool",
9
+ "version",
10
+ "anthropic",
11
+ "openai",
12
+ "mcp"
13
+ ],
14
+ "properties": {
15
+ "tool": {
16
+ "const": "failfirst"
17
+ },
18
+ "version": {
19
+ "type": "string"
20
+ },
21
+ "anthropic": {
22
+ "type": "array",
23
+ "minItems": 1,
24
+ "items": {
25
+ "type": "object",
26
+ "additionalProperties": false,
27
+ "required": [
28
+ "name",
29
+ "description",
30
+ "input_schema"
31
+ ],
32
+ "properties": {
33
+ "name": {
34
+ "type": "string",
35
+ "pattern": "^[a-zA-Z0-9_-]{1,64}$"
36
+ },
37
+ "description": {
38
+ "type": "string",
39
+ "minLength": 1
40
+ },
41
+ "input_schema": {
42
+ "$ref": "#/$defs/params"
43
+ }
44
+ }
45
+ }
46
+ },
47
+ "openai": {
48
+ "type": "array",
49
+ "minItems": 1,
50
+ "items": {
51
+ "type": "object",
52
+ "additionalProperties": false,
53
+ "required": [
54
+ "type",
55
+ "function"
56
+ ],
57
+ "properties": {
58
+ "type": {
59
+ "const": "function"
60
+ },
61
+ "function": {
62
+ "type": "object",
63
+ "additionalProperties": false,
64
+ "required": [
65
+ "name",
66
+ "description",
67
+ "parameters"
68
+ ],
69
+ "properties": {
70
+ "name": {
71
+ "type": "string",
72
+ "pattern": "^[a-zA-Z0-9_-]{1,64}$"
73
+ },
74
+ "description": {
75
+ "type": "string",
76
+ "minLength": 1
77
+ },
78
+ "parameters": {
79
+ "$ref": "#/$defs/params"
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+ },
86
+ "mcp": {
87
+ "type": "array",
88
+ "minItems": 1,
89
+ "items": {
90
+ "type": "object",
91
+ "additionalProperties": false,
92
+ "required": [
93
+ "name",
94
+ "description",
95
+ "inputSchema"
96
+ ],
97
+ "properties": {
98
+ "name": {
99
+ "type": "string",
100
+ "pattern": "^[a-zA-Z0-9_-]{1,64}$"
101
+ },
102
+ "description": {
103
+ "type": "string",
104
+ "minLength": 1
105
+ },
106
+ "inputSchema": {
107
+ "$ref": "#/$defs/params"
108
+ }
109
+ }
110
+ }
111
+ }
112
+ },
113
+ "$defs": {
114
+ "params": {
115
+ "type": "object",
116
+ "required": [
117
+ "type",
118
+ "properties"
119
+ ],
120
+ "properties": {
121
+ "type": {
122
+ "const": "object"
123
+ },
124
+ "properties": {
125
+ "type": "object"
126
+ }
127
+ }
128
+ }
129
+ }
130
+ }
@@ -0,0 +1,33 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/pallav0107/failfirst/schemas/error.schema.json",
4
+ "title": "failfirst error",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "error"
9
+ ],
10
+ "properties": {
11
+ "error": {
12
+ "type": "object",
13
+ "additionalProperties": false,
14
+ "required": [
15
+ "code",
16
+ "message",
17
+ "hint"
18
+ ],
19
+ "properties": {
20
+ "code": {
21
+ "type": "string",
22
+ "pattern": "^FF_E_[A-Z_]+$"
23
+ },
24
+ "message": {
25
+ "type": "string"
26
+ },
27
+ "hint": {
28
+ "type": "string"
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,134 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/pallav0107/failfirst/schemas/verify-report.schema.json",
4
+ "title": "failfirst verify report",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "tool",
9
+ "version",
10
+ "phase",
11
+ "status",
12
+ "exit_code",
13
+ "summary",
14
+ "criteria"
15
+ ],
16
+ "properties": {
17
+ "tool": {
18
+ "const": "failfirst"
19
+ },
20
+ "version": {
21
+ "type": "string"
22
+ },
23
+ "phase": {
24
+ "const": "verify"
25
+ },
26
+ "status": {
27
+ "enum": [
28
+ "pass",
29
+ "fail",
30
+ "awaiting_review"
31
+ ]
32
+ },
33
+ "exit_code": {
34
+ "enum": [
35
+ 0,
36
+ 1,
37
+ 3
38
+ ]
39
+ },
40
+ "summary": {
41
+ "type": "object",
42
+ "propertyNames": {
43
+ "enum": [
44
+ "awaiting_review",
45
+ "unbaselined",
46
+ "modified",
47
+ "vacuous",
48
+ "baseline_error",
49
+ "proven",
50
+ "still_failing",
51
+ "error",
52
+ "removed"
53
+ ]
54
+ },
55
+ "additionalProperties": {
56
+ "type": "integer",
57
+ "minimum": 0
58
+ }
59
+ },
60
+ "criteria": {
61
+ "type": "array",
62
+ "items": {
63
+ "$ref": "#/$defs/row"
64
+ }
65
+ }
66
+ },
67
+ "$defs": {
68
+ "evaluation": {
69
+ "type": "object",
70
+ "additionalProperties": false,
71
+ "required": [
72
+ "status",
73
+ "detail"
74
+ ],
75
+ "properties": {
76
+ "status": {
77
+ "enum": [
78
+ "passed",
79
+ "failed",
80
+ "error",
81
+ "n/a"
82
+ ]
83
+ },
84
+ "detail": {
85
+ "type": "string"
86
+ }
87
+ }
88
+ },
89
+ "row": {
90
+ "type": "object",
91
+ "additionalProperties": false,
92
+ "required": [
93
+ "id",
94
+ "statement",
95
+ "judgment",
96
+ "verdict",
97
+ "reason"
98
+ ],
99
+ "properties": {
100
+ "id": {
101
+ "type": "string"
102
+ },
103
+ "statement": {
104
+ "type": "string"
105
+ },
106
+ "judgment": {
107
+ "type": "boolean"
108
+ },
109
+ "verdict": {
110
+ "enum": [
111
+ "awaiting_review",
112
+ "unbaselined",
113
+ "modified",
114
+ "vacuous",
115
+ "baseline_error",
116
+ "proven",
117
+ "still_failing",
118
+ "error",
119
+ "removed"
120
+ ]
121
+ },
122
+ "reason": {
123
+ "type": "string"
124
+ },
125
+ "baseline": {
126
+ "$ref": "#/$defs/evaluation"
127
+ },
128
+ "now": {
129
+ "$ref": "#/$defs/evaluation"
130
+ }
131
+ }
132
+ }
133
+ }
134
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/pallav0107/failfirst/schemas/version.schema.json",
4
+ "title": "failfirst version",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "tool",
9
+ "version"
10
+ ],
11
+ "properties": {
12
+ "tool": {
13
+ "const": "failfirst"
14
+ },
15
+ "version": {
16
+ "type": "string"
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,43 @@
1
+ {
2
+ "tool": "failfirst",
3
+ "tools": [
4
+ {
5
+ "name": "failfirst_baseline",
6
+ "description": "Run the acceptance criteria on the UNFIXED state and write the lockfile (.failfirst.lock.json). Call once, before any work. Returns a baseline report; exit_code 0 means every machine criterion failed as required. Refuses to overwrite an existing lockfile (FF_E_LOCK_EXISTS).",
7
+ "input_schema": {
8
+ "type": "object",
9
+ "properties": {
10
+ "criteria": {"type": "string", "description": "Path to the criteria file (.json/.yaml). Default: failfirst.yaml, failfirst.yml, failfirst.json, criteria.yaml, criteria.yml or criteria.json in the working directory."},
11
+ "lock": {"type": "string", "description": "Lockfile path. Default: .failfirst.lock.json next to the criteria file."},
12
+ "dry_run": {"type": "boolean", "description": "Run the criteria and report, but write no lockfile and no report file."},
13
+ "force": {"type": "boolean", "description": "Overwrite an existing lockfile. Destroys the failure evidence; only a human owner should set this."}
14
+ },
15
+ "additionalProperties": false
16
+ },
17
+ "output_schema": "baseline-report.schema.json"
18
+ },
19
+ {
20
+ "name": "failfirst_verify",
21
+ "description": "Re-run the criteria AFTER the work and check each was proven: failed at baseline, unchanged since, and passes now. Returns a verify report; status is pass, fail or awaiting_review. Needs a lockfile from failfirst_baseline (FF_E_NO_LOCK otherwise).",
22
+ "input_schema": {
23
+ "type": "object",
24
+ "properties": {
25
+ "criteria": {"type": "string", "description": "Path to the criteria file (.json/.yaml). Default: failfirst.yaml, failfirst.yml, failfirst.json, criteria.yaml, criteria.yml or criteria.json in the working directory."},
26
+ "lock": {"type": "string", "description": "Lockfile path. Default: .failfirst.lock.json next to the criteria file."}
27
+ },
28
+ "additionalProperties": false
29
+ },
30
+ "output_schema": "verify-report.schema.json"
31
+ }
32
+ ],
33
+ "errors": {
34
+ "FF_E_USAGE": {"exit": 2, "hint": "Run `failfirst --help`. Commands: baseline, verify, describe, mcp."},
35
+ "FF_E_NO_CRITERIA": {"exit": 2, "hint": "Create failfirst.yaml (see AGENTS.md) or pass the criteria file path as the first argument."},
36
+ "FF_E_BAD_CRITERIA": {"exit": 2, "hint": "Fix the criteria file: ids unique, every criterion needs a check (cmd, argv, path or url) or judgment: true, unknown fields are rejected. See spec/SPEC.md section 1."},
37
+ "FF_E_LOCK_EXISTS": {"exit": 2, "hint": "A baseline already exists; keep it. Re-baselining erases the failure evidence, so only a human owner should pass --force."},
38
+ "FF_E_NO_LOCK": {"exit": 2, "hint": "Run `failfirst baseline` on the unfixed state before the work, then verify."},
39
+ "FF_E_BAD_LOCK": {"exit": 2, "hint": "The lockfile is corrupt or from another version. Restore it from version control; do not re-baseline after the fix."},
40
+ "FF_E_IO": {"exit": 2, "hint": "A file could not be read or written (permissions, missing directory). Check the --lock / --report path and the working directory."},
41
+ "FF_E_INTERNAL": {"exit": 70, "hint": "Unexpected failure in failfirst itself. Re-run with the same arguments and report the message."}
42
+ }
43
+ }
failfirst/agent.py ADDED
@@ -0,0 +1,109 @@
1
+ """Agent-facing layer: tool definitions (one source of truth), stable errors, shared operations.
2
+
3
+ ``agent/tools.json`` is generated from the repo's ``spec/tools.json`` (see ``tools/sync_agent.py``)
4
+ and is the only place tool names, descriptions, input schemas and error hints are written. The
5
+ Anthropic, OpenAI and MCP formats printed by ``failfirst describe --json`` are all derived from it.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import json
10
+ import os
11
+ from typing import Any
12
+
13
+ from . import __version__
14
+ from .core import DEFAULT_LOCK, LockError, baseline, verify
15
+ from .model import SpecError, load_criteria
16
+
17
+ AGENT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "agent")
18
+ CANDIDATES = ("failfirst.yaml", "failfirst.yml", "failfirst.json",
19
+ "criteria.yaml", "criteria.yml", "criteria.json")
20
+
21
+
22
+ def load_tools() -> dict[str, Any]:
23
+ """Return the parsed tools.json (tools plus the stable error table)."""
24
+ with open(os.path.join(AGENT_DIR, "tools.json"), encoding="utf-8") as fh:
25
+ return json.load(fh)
26
+
27
+
28
+ class CliError(Exception):
29
+ """An expected failure with a stable ``code`` (FF_E_*), a message and a static hint."""
30
+
31
+ def __init__(self, code: str, message: str) -> None:
32
+ super().__init__(message)
33
+ self.code = code
34
+ self.message = message
35
+
36
+ @property
37
+ def exit_code(self) -> int:
38
+ """Process exit status for this error code."""
39
+ return int(load_tools()["errors"].get(self.code, {"exit": 70})["exit"])
40
+
41
+ def payload(self) -> dict[str, Any]:
42
+ """The ``{"error": {code, message, hint}}`` document printed with ``--json``."""
43
+ hint = load_tools()["errors"].get(self.code, {}).get("hint", "")
44
+ return {"error": {"code": self.code, "message": self.message, "hint": hint}}
45
+
46
+
47
+ def describe() -> dict[str, Any]:
48
+ """Tool definitions in Anthropic, OpenAI and MCP formats, all derived from tools.json."""
49
+ tools = load_tools()["tools"]
50
+ return {
51
+ "tool": "failfirst",
52
+ "version": __version__,
53
+ "anthropic": [{"name": t["name"], "description": t["description"], "input_schema": t["input_schema"]}
54
+ for t in tools],
55
+ "openai": [{"type": "function", "function": {"name": t["name"], "description": t["description"],
56
+ "parameters": t["input_schema"]}} for t in tools],
57
+ "mcp": [{"name": t["name"], "description": t["description"], "inputSchema": t["input_schema"]}
58
+ for t in tools],
59
+ }
60
+
61
+
62
+ def _check_args(schema: dict[str, Any], args: Any) -> None:
63
+ if not isinstance(args, dict):
64
+ raise CliError("FF_E_USAGE", "tool arguments must be an object")
65
+ props = schema.get("properties", {})
66
+ for key, value in args.items():
67
+ if key not in props:
68
+ raise CliError("FF_E_USAGE", f"unknown argument {key!r}")
69
+ want = props[key].get("type")
70
+ if (want == "string" and not isinstance(value, str)) or (want == "boolean" and not isinstance(value, bool)):
71
+ raise CliError("FF_E_USAGE", f"argument {key!r} must be a {want}")
72
+
73
+
74
+ def find_criteria(criteria: str | None) -> str:
75
+ """Resolve the criteria path, searching the default names in the working directory."""
76
+ if criteria:
77
+ if not os.path.exists(criteria):
78
+ raise CliError("FF_E_NO_CRITERIA", f"criteria file not found: {criteria}")
79
+ return criteria
80
+ for name in CANDIDATES:
81
+ if os.path.exists(name):
82
+ return name
83
+ raise CliError("FF_E_NO_CRITERIA", "no criteria file found (tried " + ", ".join(CANDIDATES) + ")")
84
+
85
+
86
+ def run_operation(name: str, args: dict[str, Any]) -> tuple[dict[str, Any], int]:
87
+ """Run failfirst_baseline or failfirst_verify; returns (report, exit_code) or raises CliError."""
88
+ spec = next((t for t in load_tools()["tools"] if t["name"] == name), None)
89
+ if spec is None:
90
+ raise CliError("FF_E_USAGE", f"unknown tool {name!r}")
91
+ _check_args(spec["input_schema"], args)
92
+ path = find_criteria(args.get("criteria"))
93
+ try:
94
+ criteria = load_criteria(path)
95
+ except SpecError as exc:
96
+ raise CliError("FF_E_BAD_CRITERIA", str(exc)) from exc
97
+ except (OSError, UnicodeError) as exc:
98
+ raise CliError("FF_E_BAD_CRITERIA", f"cannot read {path}: {getattr(exc, 'strerror', None) or exc}") from exc
99
+ base_dir = os.path.dirname(os.path.abspath(path))
100
+ lock = args.get("lock") or os.path.join(base_dir, DEFAULT_LOCK)
101
+ try:
102
+ if name == "failfirst_baseline":
103
+ return baseline(criteria, lock, base_dir=base_dir, force=bool(args.get("force")),
104
+ dry_run=bool(args.get("dry_run")))
105
+ return verify(criteria, lock, base_dir=base_dir)
106
+ except LockError as exc:
107
+ raise CliError(exc.code, str(exc)) from exc
108
+ except OSError as exc:
109
+ raise CliError("FF_E_IO", f"cannot write {lock}: {exc.strerror}") from exc