Examples and CLI¶
The example library is a registry of runnable stories (fraud review,
clinical triage, autoscaling and about sixty more), each returning its
decision records and a summary. list_examples, run_example and
run_all let you run them from Python instead of parsing terminal
output, and interface_map returns a machine-readable summary of the
public surface. The cli module is the grounded-decision command.
Public here: list_examples, run_example, run_all, interface_map
from grounded_decision.examples.catalog. The command line, with real
output: Command line; what each example teaches:
Example library.
catalog ¶
Registry of every runnable example, and the public interface map.
This is the one place that knows which examples exist. The command line
(uv run grounded-decision examples), the test suite and the
documentation all go through it, so adding an example means adding it
here.
How to use it::
from grounded_decision.examples.catalog import list_examples, run_example
list_examples(track="live") # [{"id", "title", "track"}, ...]
run_example("fraud_review") # prints the human-readable report
run_example("fraud_review", as_json=True)["records"]
Each example's runner is a function run(*, as_json: bool) -> dict that
returns {"id", "title", "records", "summary"}. Product examples expose
it as a module-level run next to ID and TITLE; the step,
adaptive, combo, live, markets and games tracks expose a RUNNERS dict of
id -> (title, runner). See :mod:grounded_decision.examples for what
each track teaches and the recommended reading order.
:func:interface_map returns a machine-readable summary of the public
surface (decide inputs and outputs, the six checks, builders, router
subgoals, compare helpers, integration entry points) and is what
uv run grounded-decision interfaces prints.
INTERFACE_MAP_VERSION
module-attribute
¶
The interface map is versioned with the package.
TRACKS
module-attribute
¶
Every track name, in the order the catalog lists them.
ExampleRunner ¶
Bases: Protocol
runner(*, as_json: bool) -> {"id", "title", "records", "summary"}.
CatalogEntry ¶
Bases: NamedTuple
One registered example.
Attributes:
| Name | Type | Description |
|---|---|---|
title |
str
|
Human heading, e.g. |
runner |
ExampleRunner
|
The function that runs the example. |
track |
str
|
Which track it belongs to (one of :data: |
list_examples ¶
List the registered examples, optionally for one track.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
str | None
|
One of :data: |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
|
Source code in src/grounded_decision/examples/catalog.py
run_example ¶
Run one example by id and return its result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The example id, e.g. |
required |
as_json
|
bool
|
Print one line of JSON instead of the human-readable report. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
|
Raises:
| Type | Description |
|---|---|
SystemExit
|
If |
Source code in src/grounded_decision/examples/catalog.py
run_all ¶
Run every example in catalog order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
as_json
|
bool
|
Print JSON lines instead of human-readable reports (which are separated by a blank line). |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
One result dict per example, in catalog order. |
Source code in src/grounded_decision/examples/catalog.py
interface_map ¶
Describe the public surface of the library as plain data.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A JSON-serialisable dict with the |
dict[str, Any]
|
output, verdicts and the six checks with their catalog |
dict[str, Any]
|
references), the policy |
dict[str, Any]
|
|
dict[str, Any]
|
the |
Source code in src/grounded_decision/examples/catalog.py
cli ¶
Command-line interface: uv run grounded-decision <subcommand>.
Subcommands¶
demo (also the default when no subcommand is given)
Run a short clinical-triage walkthrough: a handful of patients through
one grounded module, printed as readable tick cards. The fastest way to
see what a decision looks like.
examples [ID] [--track TRACK] [--json]
Work with the example library (see :mod:grounded_decision.examples).
* ``examples`` lists every example id with its track and title.
* ``examples --track live`` lists only one track (``product``,
``step``, ``adaptive``, ``combo``, ``live`` or ``markets``).
* ``examples fraud_review`` runs one example and prints its report.
* ``examples fraud_review --json`` prints the same run as one line of
JSON: ``{"id", "title", "records", "summary"}``.
* ``examples --json`` (no id) runs every listed example as JSON lines,
which is handy for piping into ``jq`` or a regression diff.
learn HISTORY.jsonl --max-risk RATE [--confidence C] [--procedure P] [--output FILE]
Learn the check settings from labelled history (see
:mod:grounded_decision.learn). Each line of HISTORY.jsonl is one
past decision: {"evidence": {...}, "proposed_action": "block",
"action_was_correct": true}. Prints what was learned and how far to
trust it, and writes the settings with their provenance to FILE.
Exits with 2 if no setting could be certified.
play --list
Every game (tic-tac-toe, snake, checkers, Chinese checkers), its
agents and its default line-up.
play GAME [--agents A,B] [--view live|history|json|step|none] [--fps N|max] [--delay S]
[--frame-every N] [--seed N] [--size WxH] [--max-steps N] [--until-length N]
[--style auto|ascii|unicode|blocks|braille] [--color auto|always|never]
[--players N] [--coords] [--zoom 1|2|3|auto] [--stall-plies N] [--clear-finished]
[--world MODE] [--board NAME] [--start NAME] [--holes CELLS] [--layout flat|pointy]
Watch agents play (see :mod:grounded_decision.examples.games.play).
play tictactoe with no options is you against the engine, with
exact advice for every square.
play GAME --you [--you-play NAME] [--guarded]
Take a seat yourself; with --guarded the game's gate checks your
moves and overrides the unsafe ones, explaining why.
audit GAME [--agent NAME] [--opponent NAME] [--oracle NAME] [--oracle-depth N]
[--seed N] [--size WxH] [--seat N] [--view history|json] [--no-regimes]
[--regime-seeds N] [--world MODE]
Replay one match and judge every decision of one agent against a
stronger oracle (regret, agreement, the worst decisions with their
boards, the gate's overrides), then print the game's information-regime
table: what not seeing the future costs, and what insuring against the
worst case costs (see :mod:grounded_decision.examples.games.audit).
record GAME [--agents A,B] [--world MODE] [--seed N] [--size WxH] [--max-steps N]
[game rule options] -o FILE.gdr
Play a match of agents to its end and save every frame as a .gdr
replay (see :mod:grounded_decision.session.replay); the options and
their validation are play's.
replay-info FILE.gdr [--verify] [--moves] [--bytes] [--json]
Print a replay's header and check its frames against its content hash
and every move arrow against the position before its move;
--verify also records the header's spec again and checks the frames
are identical; --moves prints every move with its arrows.
serve [--host H] [--port P] [--database FILE] [limits ...]
Run our own service (:mod:grounded_decision.service; needs the
service extra): the session protocol over WebSocket (/ws/) and
HTTP (POST /api/), the reference decision service under
/api/decisions and GET /healthz, with JSON logs on stdout.
Every flag also reads an environment variable
(--max-sessions: GD_SERVICE_MAX_SESSIONS).
new-game NAME [--root DIR] [--force]
Write a new game's skeleton, games/NAME.py (the template game, a
race to N, renamed) and tests/test_games_NAME.py, which runs the
game-module contract on it (see
:mod:grounded_decision.examples.games.scaffold and
docs/contributing/add-a-game.md).
interfaces
Print :func:grounded_decision.examples.catalog.interface_map as
indented JSON: the decide contract, the six checks, the builders,
router and compare helpers, and every example id.
--version
Print the installed package version.
Examples are imported lazily, so --version and --help stay fast.
SUBCOMMAND_PARSERS
module-attribute
¶
The play and audit parsers, kept for game-specific --help.
EXIT_REPLAY_MISMATCH
module-attribute
¶
replay-info: the frames do not match the header's hash or a re-recording.
EXIT_INTERRUPTED
module-attribute
¶
128 + SIGINT, the shell convention for Ctrl-C.
RESTORE_TERMINAL
module-attribute
¶
Reset attributes, show the cursor, leave the alternate screen.
build_parser ¶
The argument parser for every subcommand described in the module docstring.
Source code in src/grounded_decision/cli.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
record_spec ¶
The :class:~grounded_decision.session.SessionSpec that record's options name.
Raises:
| Type | Description |
|---|---|
PlayError
|
Any option the game does not accept ( |
Source code in src/grounded_decision/cli.py
main ¶
Parse argv (default: sys.argv[1:]) and run the subcommand.
Every subcommand runs inside one wrapper, so the terminal behaves:
Ctrl-C prints Interrupted. (no traceback), restores the terminal
and exits 130; a closed pipe (... | head) exits quietly; SIGTERM
runs every cleanup (finally) and exits 143.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
argv
|
list[str] | None
|
Command-line arguments without the program name. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The process exit code: 0 on success, 1 for an unknown subcommand, |
int
|
2 for bad arguments or when |
int
|
setting, 130 after Ctrl-C. |