Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions dokimi/changelog.d/20261205_120000_napetrov_cli-json-paid-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--
Copyright (C) 2026 Intel Corporation
SPDX-License-Identifier: Apache-2.0
-->

### Added

- **`--json` on five more paid leaves: `skill novelty analyze`, `catalog routing`, `skill sensitivity`,
`eval questions analyze` and `eval questions review`.** Each prints one `cli_output.v1` document: the
estimate under `--dry-run` (the two `eval questions` leaves also refuse under `--max-usd` before the first
call: `budget_stopped`, exit 3), the saved result on
success, and, after a stop, the result it kept (`budget_stopped` at exit 3, `partial` at exit 1). Where a
run keeps no result (`eval questions analyze` writes its report only at the end, so a cap reached mid-run
leaves none) the stop is a `failed` document that carries the cost. An input error names the leaf's
`--help` as its `next` step. The other leaves keep their human output. Part of #94.

### Fixed

- **`tools preference`: a refused `--out` or `--items-out` path after the model calls exits 2 with its
`next` step.** The document had an empty `next`; it now names the leaf's `--help`.
5 changes: 5 additions & 0 deletions dokimi/docs/reference/cli-interface.json
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@
{"key": "--help/-h", "action": "_HelpAction", "choices": null, "const": null, "default": "==SUPPRESS==", "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--include-disabled", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--include-invalid", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--json", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--keep-copies-under", "action": "_StoreAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": null},
{"key": "--max-calls", "action": "_StoreAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": "dokimi.cli_types.positive_int#96184e0d3f3b"},
{"key": "--max-output-tokens", "action": "_StoreAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": "dokimi.cli_types.positive_int#96184e0d3f3b"},
Expand Down Expand Up @@ -1035,6 +1036,7 @@
{"key": "--concurrency", "action": "_StoreAction", "choices": null, "const": null, "default": 5, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": "int"},
{"key": "--dry-run", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--help/-h", "action": "_HelpAction", "choices": null, "const": null, "default": "==SUPPRESS==", "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--json", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--max-usd", "action": "_StoreAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": "dokimi.cli_types.positive_usd#3f7e5c8902a3"},
{"key": "--model", "action": "ProviderModelAction", "choices": null, "const": null, "default": "gpt-6.1-sol", "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": null},
{"key": "--out", "action": "_StoreAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": null},
Expand Down Expand Up @@ -1092,6 +1094,7 @@
{"key": "--concurrency", "action": "_StoreAction", "choices": null, "const": null, "default": 6, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": "dokimi.cli_types.positive_int#96184e0d3f3b"},
{"key": "--dry-run", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--help/-h", "action": "_HelpAction", "choices": null, "const": null, "default": "==SUPPRESS==", "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--json", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--limit", "action": "_StoreAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": "dokimi.cli_types.positive_int#96184e0d3f3b"},
{"key": "--max-usd", "action": "_StoreAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": "dokimi.cli_types.positive_usd#3f7e5c8902a3"},
{"key": "--model", "action": "ProviderModelAction", "choices": null, "const": null, "default": "gpt-6.1-sol", "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": null},
Expand Down Expand Up @@ -1946,6 +1949,7 @@
{"key": "--format", "action": "_StoreAction", "choices": ["'html'", "'md'"], "const": null, "default": "html", "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": null},
{"key": "--help/-h", "action": "_HelpAction", "choices": null, "const": null, "default": "==SUPPRESS==", "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--include-resources", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--json", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--judge", "action": "_AppendAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": null},
{"key": "--judge-aggregate", "action": "_StoreAction", "choices": ["'majority'", "'median'", "'unanimous'"], "const": null, "default": "median", "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": null},
{"key": "--judge-model", "action": "_StoreAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": null},
Expand Down Expand Up @@ -2133,6 +2137,7 @@
{"key": "--help/-h", "action": "_HelpAction", "choices": null, "const": null, "default": "==SUPPRESS==", "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--include-disabled", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--include-invalid", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--json", "action": "_StoreTrueAction", "choices": null, "const": true, "default": false, "group_key": null, "group_required": null, "hidden": false, "is_flag": true, "kind": "option", "nargs": 0, "position": null, "required": false, "type": null},
{"key": "--keep-copies-under", "action": "_StoreAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": null},
{"key": "--max-calls", "action": "_StoreAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": "dokimi.cli_types.positive_int#96184e0d3f3b"},
{"key": "--max-output-tokens", "action": "_StoreAction", "choices": null, "const": null, "default": null, "group_key": null, "group_required": null, "hidden": false, "is_flag": false, "kind": "option", "nargs": null, "position": null, "required": false, "type": "dokimi.cli_types.positive_int#96184e0d3f3b"},
Expand Down
5 changes: 4 additions & 1 deletion dokimi/dokimi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,9 @@
if code in (2, 5):
doc = cli_output.error_document(kind, "input" if code == 2 else "infra", "see stderr",
run_id=cli_output.current_run_id(), reason=reason)
help_argv = getattr(args, "_leaf_help_argv", None)
if code == 2 and help_argv and getattr(args, "input_help_next", False): # a refused path exits 2 (#94 F2)

Check warning on line 456 in dokimi/dokimi/cli.py

View workflow job for this annotation

GitHub Actions / Mutation of changed lines

mutant inconclusive: a candidate test file for it did not run

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix before merge (blocking): An exit 2 from a --json leaf with no document of its own gets no help step if the condition at cli.py:456 is negated, and the tests still pass.

Suggestion: Add a test that makes a --json leaf raise SystemExit(2) and asserts that next holds the --help step.

I tested this patch: it fixes the problem, and the tests still pass.

Patch
--- a/dokimi/tests/test_cli_json_novelty.py
+++ b/dokimi/tests/test_cli_json_novelty.py
@@ -70,6 +70,19 @@
     assert step["spends"] is False and step["est_usd"] == 0
 
 
+def test_a_json_leaf_that_exits_2_without_a_document_names_its_help(tmp_path, monkeypatch, capsys):
+    monkeypatch.chdir(tmp_path)
+
+    def refused(_args, _state):
+        raise SystemExit(2)
+
+    monkeypatch.setattr(novelty_cmd, "_novelty_analyze", refused)
+    code, doc = _cli(monkeypatch, capsys, "skill", "novelty", "analyze", "--artifact", _skill(tmp_path), "--json")
+
+    assert code == 2 and doc["status"] == "failed" and doc["error"]["code"] == "input"
+    assert doc["next"][0]["argv"][-1] == "--help"
+
+
 def test_a_run_the_cap_stopped_is_budget_stopped_and_keeps_its_cost(tmp_path, capsys):
     state = {"payload": {"cost": {"usd": 0.02, "llm_calls": 3}, "status": "partial"}, "partial": True,
              "out": tmp_path / "novelty.json"}
Details

If a --json leaf exits 2 through a plain SystemExit(2), _document_on_exit at cli.py:456 is what should give its document the --help step. If I negate that condition, the step goes missing, exit 5 gets one, and the tests still pass. The help-step tests I read take the InputError route, not this one. I'd expect one test that makes a --json leaf raise SystemExit(2) and asserts that next holds the --help step.

doc["next"] = [cli_output.free_step(help_argv, "Show the accepted syntax of this command.")]
else:
doc = cli_output.envelope(kind, run_id=cli_output.current_run_id(), status="failed", reason=reason)
cli_output.emit(doc)
Expand Down Expand Up @@ -482,7 +485,7 @@
except Exception as exc: # noqa: BLE001 - printed with its traceback, exit 5
# A crash is an infra failure, exit 5 for every command (§2.6): it never reads as a failed verdict.
traceback.print_exc()
if _emits_cli_output(args): # a --json leaf still prints its one document on a crash
if _emits_cli_output(args) and not cli_output.printed(): # a leaf that printed its document keeps it
_exit_with_document(args, "infra", f"{type(exc).__name__}: {exc}")
sys.exit(EXIT_INFRA)
except SystemExit as exc: # every exit code of a --json leaf carries its document (§2.7)
Expand Down
49 changes: 49 additions & 0 deletions dokimi/dokimi/cli_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,55 @@
limits=[{"code": limit_code, "text": refusal}])


def limit_code_of(data: Any) -> str:
"""The ``limits`` code of a budget stop: ``max_calls`` when the run's partial block says --max-calls stopped it."""
block = data.get("partial") if isinstance(data, dict) else None
reason = block.get("reason") if isinstance(block, dict) else None
return "max_calls" if reason == "call_cap" else "max_usd"


def stop_document(kind: str, data: Any, stop: SystemExit, *, limit_code: str = "max_usd",
reason: str | None = None, **fields: Any) -> dict[str, Any]:
"""The document of a run that stopped with ``stop`` (#94 F2, X1).

Exit 3 is a budget stop: status ``budget_stopped``, the message under ``limits``. Any other exit is ``partial``
when the run kept a result (``data`` is not None) and ``failed`` when it kept none. ``reason`` overrides the
message, which otherwise is the stop's own text; an int-coded ``SystemExit(3)`` has no text of its own. The
caller prints this, then re-raises ``stop`` to keep its exit code.
"""
from dokimi.exit_codes import EXIT_INCONCLUSIVE

text = str(stop)
if not text or text == str(stop.code):
text = f"the run stopped (exit {stop.code})"
message = reason or text
budget = stop.code == EXIT_INCONCLUSIVE
limits = [{"code": limit_code, "text": message}] if budget else []
status = "budget_stopped" if budget else ("partial" if data is not None else "failed")
return envelope(kind, run_id=current_run_id(), status=status, reason=message, data=data, limits=limits, **fields)


def write_failure_document(kind: str, exc: BaseException, cost: dict[str, Any], *, help_argv: list[str] | None = None,
subjects: list[dict[str, Any]] | None = None,
artifacts: dict[str, Any] | None = None) -> dict[str, Any]:
"""The document of a run that made its paid calls and then failed to write its output (#94 F2): the cost of the
calls is kept. An input refusal is exit 2 and names the leaf's help step; any other failure is ``failed``. The
caller prints this, then re-raises ``exc`` (cli.py then sees a printed document and keeps the exit code)."""
from dokimi.files import InputError

reason = f"the run made its calls, then it failed: {str(exc) or type(exc).__name__}"

Check warning on line 137 in dokimi/dokimi/cli_output.py

View workflow job for this annotation

GitHub Actions / Mutation of changed lines

mutant inconclusive: a candidate test file for it did not run
if isinstance(exc, InputError):
doc = error_document(kind, "input", str(exc) or type(exc).__name__, run_id=current_run_id(), reason=reason,
cost=cost)
if help_argv:
doc["next"] = [free_step(help_argv, "Show the accepted syntax of this command.")]
return doc
# the exit is 5 (infra) and the error says so, as the crash document does
return envelope(kind, run_id=current_run_id(), status="failed", reason=reason, subjects=subjects,
artifacts=artifacts, cost=cost,
error=error_object("infra", str(exc) or type(exc).__name__))

Check warning on line 147 in dokimi/dokimi/cli_output.py

View workflow job for this annotation

GitHub Actions / Mutation of changed lines

mutant inconclusive: a candidate test file for it did not run


def free_step(argv: list[str], why: str) -> dict[str, Any]:
"""One ``next`` entry for a command that costs nothing to run: ``spends`` false, ``est_usd`` 0 (#941)."""
return {"argv": argv, "why": why, "spends": False, "est_usd": 0, "kind": "sequence"}
Expand Down
Loading
Loading