Metadata-Version: 2.4
Name: trugs-runner
Version: 0.1.1
Summary: The TRUGS runner offers and records actions against a fixed graph.
License: Apache-2.0
Project-URL: Homepage, https://trugs.ai
Project-URL: Source, https://github.com/TRUGS-LLC/TRUGS-RUNNER
Project-URL: Issues, https://github.com/TRUGS-LLC/TRUGS-RUNNER/issues
Project-URL: Specification, https://github.com/TRUGS-LLC/TRUGS
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: jsonschema<5,>=4.19
Requires-Dist: trugs-tools<3.0.0,>=2.2.0
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: ruff==0.16.5; extra == "dev"
Requires-Dist: trugs-tools==2.2.0; extra == "dev"
Dynamic: license-file

# the TRUGS runner records and checks work against a fixed graph.

[![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/LICENSE)
[![check](https://github.com/TRUGS-LLC/TRUGS-RUNNER/actions/workflows/check.yml/badge.svg)](https://github.com/TRUGS-LLC/TRUGS-RUNNER/actions/workflows/check.yml)
[![run](https://github.com/TRUGS-LLC/TRUGS-RUNNER/actions/workflows/run.yml/badge.svg)](https://github.com/TRUGS-LLC/TRUGS-RUNNER/actions/workflows/run.yml)

An agent can fail by leaving the declared path or by taking a permitted step that is wrong. A rail is a TRUG that a person orders in advance, declaring the steps, permissions and evidence needed to move on. The TRUGS runner validates and captures the rail once at the start and never lets a run change it. Gates admit evidence, not claims. A deterministic oracle decides completion by checking conditions written down in advance; it never judges the quality of the work.

## Install

You need Python 3.11 or later. The project's own checks run on Linux.

To use the runner as a library, install the published package:

```bash
python3 -m pip install trugs-runner
```

The package installs two Python modules: `executable_trug`, the engine and its command line, which you run as
`python3 -m executable_trug`, and `trugs_runner`, the library for building rails in Python. It also installs
`jsonschema` and `trugs-tools`.

The walkthrough below uses the examples in this repository, which are not part of the package. To follow it, clone
the repository and install it into a new virtual environment:

```bash
git clone https://github.com/TRUGS-LLC/TRUGS-RUNNER.git
cd TRUGS-RUNNER
python3 -m venv .venv
. .venv/bin/activate
python3 -m pip install -e .
python3 -m executable_trug --help
```

The last command prints the command's help, beginning `usage: executable-trug`.

## Run it

```sh
export PYTHONPATH=src
G=examples/letter-classification/letter-classification.trug.json
W="$PWD/examples/letter-classification/workers/deterministic_worker.py"

python3 -m executable_trug pin $G                      # record what each program is
python3 examples/letter-classification/driver.py --worker-command "python3 $W" \
  --run-dir /tmp/run --word Strawberry --head-file letter-head.json
```

The worker command uses an absolute path because capabilities run from the
captured snapshot directory. External workers are explicit driver adapter resources, separate
from the declared definition files.

It classifies each character of the word as R or not-R, one agent call per character,
and exits `0`.

A run that stops part way — the driver killed, the machine restarted — retains its
committed record. The head file is saved only when the driver returns, so after an
interruption it may be absent or stale. First stop and wait for the old driver and
**all capability/worker processes and their descendants**; killing the driver alone
does not establish that they stopped. After a restart, confirm none survived or
were restarted. Keep other drivers and external writers stopped during recovery.
`--processes-stopped` declares your confirmation; it does not stop processes for you.

Recover the current head by verifying the record without executing an action,
then continue with that exact external head:

```sh
python3 examples/letter-classification/driver.py --run-dir /tmp/run --recover-head --processes-stopped --head-file letter-head.json
python3 examples/letter-classification/driver.py --worker-command "python3 $W" --run-dir /tmp/run --expected-head letter-head.json --head-file letter-head.json --processes-stopped
```

Recovery requires `--head-file` and `--processes-stopped`, and cannot be combined
with `--expected-head` or person input. Invalid record/content is refused. Continue
still refuses a stale head; it never refreshes the supplied head automatically.

An action whose observation is committed is routed without invoking it again.
An unsafe action with a committed intent but no accepted observation reports
`invocation_unresolved` (exit `2`), preserving its effect and state rather than
repeating it. Recover the newly recorded halt head using the first command again;
continuation at that head returns the same halt (exit `21`). An operator must
separately choose retry or abandon through the public continuation surface; this
driver never chooses a recovery resolution automatically.

## The part that matters

Run it again with a worker that always answers `record_r`:

```sh
python3 examples/letter-classification/driver.py --worker-command "python3 $W --always-record-r" \
  --run-dir /tmp/wrong --word Strawberry
```

Same number of steps. Every receipt written. Every gate passed. Exit `21` — the
oracle caught it.

That split is the design. The per-step gate asks only *did the required thing
happen?*, which is cheap enough to run every step. Whether the conditions the task
stated now hold is a separate question, answered once at the end by something exact.

Note what the oracle is never asked. It does not judge whether the work was *good* —
correctness is a judgment about intent, and anything that forms judgments is an opinion
rather than an instrument. It checks *completion*: conditions written down in advance.
And it only ever checks conditions it can decide **without running what the agent
produced**, because an artifact that has to be run to be judged authors every input to
the verdict. This oracle reads receipts; it executes nothing the agent wrote. See
[CONCEPTS.md](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/CONCEPTS.md).

## Scope

This software does not include self-modifying TRUGS: graphs that change their own structure while they run.

## Honest limits

The TRUGS runner offers and records; it does not confine.

- **It is not a security boundary, and it does not contain the worker.** A worker
  runs as an ordinary process under the host's identity, launched by a capability. It
  is kept from the oracle, the receipts and the result programs only by being given no
  path to them — not by isolation. A worker able to look can find them, and anything
  it does before stating its choice has happened whether or not the rail then refuses
  the choice. A deployment that runs an untrusted worker must contain it itself — in a
  VM, for example; a locked-down system is a different layer from this one. The rail
  is a step up from instructions a model may ignore, not a sandbox.
- **It measures no model.** The bundled worker decides by comparison, not judgment.
  Nothing measured against it says anything about an AI model.
- **The graph never rewrites itself.** It is fixed for the life of a run. See
  [Scope](#scope).
- **The run directory is trusted.** `record.read_record()` with its defaults,
  `rail.read_state()` and `cli.inspect()` verify the complete canonical entry chain
  and referenced content; inspection reports content loss separately. A retained
  earlier head supplied to complete verification anchors the verified prefix at
  that sequence. `read_record(verify_payloads=False)` verifies the chain only.
  Ordinary reads during start and continue resolve the position, input, state,
  pending links and gate operands needed for the current operation. They check
  format support and the supplied latest head, validate the current protocol,
  state and gates, and validate new entries before publication. They do not rehash
  earlier entries or content and report `chain_integrity: null`. On this path a
  retained head establishes agreement with the stored latest-head tuple, without
  establishing prefix integrity. Someone able to replace all bytes and the caller's
  retained head can still rewrite the history consistently. The chain does not
  authenticate who acted. See [Record §5](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/SPEC/SPEC-261003-run-record.md#5-covering-digest-sdk-verification-and-limits).
- **The actor is declared, not authenticated.** Every invocation is checked against
  the root's `actor`; the rail cannot tell which process is calling it. Modal edges
  naming any other actor — the worker's `SHALL_NOT` edges included — are not checked
  at run time, because no other actor makes requests of the rail.
- **Definitions are captured at start.** Start validates and captures the rail.
  Before each step, the engine compares the captured files with their recorded
  fingerprints and stops with a report if they differ. Installed edits affect new runs. Snapshot
  storage is trusted local storage, not a confinement mechanism for workers.
- **A refusal never selects a route.** A permission refusal preserves the position;
  other ordinary execution refusals halt the run for a person. Installed edits
  cannot change the definitions used by that run.
- **Stops retain their reason, cursor and unresolved intent.** When recovery is
  offered, continue accepts an explicit retry or abandon choice with the retained
  head and process confirmation.
- **Authoring run stops at a person pause.** It carries returned heads through
  start/continue and reports the pause for an explicit person submission.
- **Model content has three admitted paths at this release.** It proceeds only
  to a person inside the declared discussion form, by an exact whole-result check
  against independently bound input, or to `unverified`.
- **The pattern rails keep that boundary.** The router and bounded tool loop use
  program workers; the evaluator-optimizer uses a person to review model drafts.
  They do not show a model evaluating a model, gateway tool calling, child rails
  or fan-out.
- **Compliance uses the pinned checker.** `make check` includes `trug compliance`
  against the stored baseline; use the [pinned checking environment](#verify) to
  compare results.

## Pattern rails

See the [examples index](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/examples/README.md) for all four examples and four pattern rails.

- [Approve and record](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/patterns/approve-and-record/) shows a person's exact acceptance governing a recording effect, with a custom node beside library nodes.
- [Router](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/patterns/router/) checks a program worker's proposed route against captured choices before invoking its declared tool.
- [Bounded tool loop](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/patterns/bounded-tool-loop/) alternates a program worker and a tool up to a declared return bound, then asks a person.
- [Evaluator-optimizer](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/patterns/evaluator-optimizer/) binds a person's recorded feedback into the next model draft and reaches a summary and person pause at the round limit.

## Reference

See [COMMANDS.md](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/COMMANDS.md) for authoring, gateway, record and maintenance operations.
The command blocks below are kept here as executable reference examples.

### Authoring and execution reference

Run from an empty working directory with the engine installed, or with an absolute
`PYTHONPATH` pointing to this checkout's `src`; all commands in this block exit 0.

<!-- PR13 commands -->
```bash
python3 -m executable_trug create draft
python3 -m executable_trug pin draft/rail.json
python3 -m executable_trug check draft/rail.json
python3 -m executable_trug test draft/rail.json --samples draft/samples.json > tests.json
python3 -m executable_trug run draft/rail.json --input-file draft/input.json --run-dir auto-run --snapshot-store "$PWD/snapshots" > pause.json
python3 -m executable_trug start draft/rail.json --input-file draft/input.json --run-dir run --snapshot-store "$PWD/snapshots" > start.json
python3 -c 'import json; json.dump(json.load(open("start.json"))["head"], open("head.json", "w"))'
python3 -m executable_trug continue --run-dir run --expected-head head.json > work.json
python3 -c 'import json; json.dump(json.load(open("work.json"))["head"], open("head.json", "w"))'
python3 -m executable_trug continue --run-dir run --expected-head head.json > person.json
python3 -c 'import json; json.dump(json.load(open("person.json"))["head"], open("head.json", "w"))'
python3 -m executable_trug continue --run-dir run --expected-head head.json --person-input-file draft/accept.json > terminal.json
```

### Model gateway reference

Set `GATEWAY_CONFIG` to the gateway configuration's absolute path; start this in a
separate terminal and wait for its `ready` event.

<!-- PR7 gateway daemon -->
```bash
python3 -m executable_trug.gateway --config "$GATEWAY_CONFIG"
```

For a model rail, set `GRAPH`, `INPUT` and `SAMPLES` to its graph, JSON input and
samples, `SNAPSHOTS` to its snapshot store, and `GATEWAY_SOCKET` to the configured
control socket. These are alternative operations: the first creates a fresh run;
the last uses an existing `run` and its complete retained head in `head.json`.

<!-- PR7 operator commands -->
```bash
python3 -m executable_trug run "$GRAPH" --input-file "$INPUT" --run-dir gateway-run --snapshot-store "$SNAPSHOTS" --gateway-socket "$GATEWAY_SOCKET" > gateway-result.json
python3 -m executable_trug test "$GRAPH" --samples "$SAMPLES" --gateway-socket "$GATEWAY_SOCKET" > gateway-tests.json
python3 -m executable_trug continue --run-dir run --expected-head head.json --gateway-socket "$GATEWAY_SOCKET" > gateway-step.json
```

### Gateway checker reference

The offline checker reads retained cases; replace the uppercase placeholders with
your case and output paths, using new output and log paths.

<!-- PR8 gateway checker offline -->
```bash
python3 scripts/check-model-gateway.py verify OUTPUT/ordinary --expectations OUTPUT/ordinary/expectations.json
python3 scripts/check-model-gateway.py replay OUTPUT
python3 scripts/check-model-gateway.py capture CASE
python3 scripts/check-model-gateway.py bundle OUTPUT > OFFLINE.log
python3 scripts/check-model-gateway.py extract --log OFFLINE.log --output EXTRACTED
```

Live checks require an explicit `live` command and a separately started operator
gateway; replace the socket, model and output placeholders with your configuration.
These calls use a provider and are separate from offline verification.

<!-- PR8 gateway checker live -->
```bash
python3 scripts/check-model-gateway.py live --provider sol --gateway-socket /operator/runtime/control.sock --model MODEL --output OUTPUT
python3 scripts/check-model-gateway.py live --provider local-vllm --gateway-socket /operator/runtime/control.sock --model MODEL --output OUTPUT
```

### Local maintenance reference

These commands inspect the authoring walkthrough's paused `auto-run` and erase its
content after explicit process and external-writer quiescence confirmations.
The confirmation arguments declare that the processes have stopped; they do not
stop them.

<!-- PR18 maintenance commands -->
```bash
python3 -c 'import json; from executable_trug.maintenance import inspect_record; json.dump(inspect_record("auto-run"), open("inspection.json", "w"))'
python3 -c 'import json; from executable_trug.maintenance import erase; head = json.load(open("pause.json"))["head"]; json.dump(erase("auto-run", head, processes_stopped=True, external_writers_stopped=True), open("erased.json", "w"))'
```

## What just happened

The graph is [`letter-classification.trug.json`](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/examples/letter-classification/letter-classification.trug.json).
For each character it ran this cycle:

1. a capability read the next character
2. a worker was asked for one choice — `record_r` or `record_not_r`
3. the runtime ran the program that choice named, which wrote a receipt
4. a gate confirmed the receipt existed
5. the index advanced, and the cycle repeated

When the word ran out, a **deterministic oracle** checked every receipt against the
rule the task stated, and decided whether the run had met it.

A separately configured worker can be supplied with `--worker-command`.
The worker needs no tools and no file access. It answers with one line.

## Make your own

To compose library and custom nodes on one Python page, see [Author a rail](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/src/trugs_runner/README.md).

See [`examples/letter-classification/README.md`](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/examples/letter-classification/README.md)
for the file layout and the steps. In short: author a graph using TRUGS nouns and
prepositions, give every node its complete list of outcomes, name in the root's
`actor` property the actor that drives the run and give it a `MAY` edge to each
capability it may have invoked, install the programs the graph names, `pin` them, and
write an oracle.

If you cannot write a deterministic oracle for a step, the specifics have not been
stated precisely enough yet — a fact about the specification, not about the step. If you
can only write one by running what the agent produced, see
[CONCEPTS.md](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/CONCEPTS.md) before you rely on its verdict.

## Verify

The pinned container checks use the locked image and dependency files in
`tests/container/`. Preparation fetches prerequisites; build and checks run offline.
Run the pinned checks below:

```sh
scripts/test-container prepare --python 3.11
scripts/test-container build --python 3.11
scripts/test-container run --python 3.11 -- make check
scripts/test-container prepare --python 3.14
scripts/test-container build --python 3.14
scripts/test-container run --python 3.14 -- make check
```

Inside the prepared environment, `make check` runs format, lint, tests, graph
validation and compliance with the same pinned tools as CI. The examples use that
environment with `PYTHONPATH=src`.

## Choosing from a second TRUG

An executable TRUG can name another TRUG as pinned data and gate a worker's choice
against it: a `DATA` node with a `path` and `digest`, and a `SUBJECT_TO` edge carrying
the state key it constrains and what an admissible node is — the property it must
carry (`requires`) and, optionally, its node type. A value proposed for that key must be
the id of such a node in the data TRUG, or the rail refuses it, commits nothing of it,
and halts the run. Changing the data TRUG makes the graph inadmissible until
re-pinned.
[`examples/faq-answer/`](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/examples/faq-answer/) shows one such choice — a question
answered from an FAQ TRUG. A gate fails closed: admission refuses a malformed gate, or any other edge to
`DATA`, rather than ignoring it.

## What the rail refuses, and what it does not control

The rail constrains what **the rail** does with an actor's request and a worker's
choice. It does not constrain what a worker **program** does while it runs.

Refused **before anything is invoked** — nothing runs:

- an invocation the graph does not permit the programme's actor — a `SHALL_NOT` edge,
  or no `SHALL` or `MAY` edge at all. The programme position stays unchanged
  while the refusal is recorded;
- an invocation the step budget leaves no room for. The run halts.

Refused **after the capability ran, before an observation** — a result that fails
the step protocol or state admission, such as an undeclared state key. No state
patch or observation from that result is committed, and the cursor does not move.
The intent and refusal evidence remain, and the run halts at that node.

A **route-gate halt** happens after the observation and admitted state have been
committed. If the gate cannot select an admitted route, the run keeps that
observation and state and records a halt without a transition or cursor move.
The capability, and any worker it ran, have already acted in either case; effects
outside the rail are not undone. See [Actions §12](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/SPEC/SPEC-261005-actions-gates-and-step-protocol.md).

Execution refusals report a reason and retained head; recorded refusals append to
`record/entries/`. Parser and keyword errors report before starting work.
State changes refused before an observation and refused cursor moves stay
uncommitted; referenced diagnostics and refusal evidence remain available in
private `content/objects/`.

Each invocation is two-phase: an intent naming the node, capability and attempt is
committed before the capability runs, and the observation after it. If a run stops
between the two, whether the capability acted is unknown, so the rail never invokes it
again: `continue` reports the unresolved invocation for a person. Safe-to-repeat actions
use the retained request; retry and abandon are explicit continuation choices.

Admission also refuses a capability or data path that is absolute or resolves outside
the install root, through `..` or a symbolic link, and a graph in which one actor holds
conflicting modals (`SHALL_NOT` and `MAY`, say) toward the same capability.

## More

A TRUG is defined by the [TRUGS specification](https://github.com/TRUGS-LLC/TRUGS);
start with [GETTING_STARTED.md](https://github.com/TRUGS-LLC/TRUGS/blob/main/GETTING_STARTED.md).
The validator and language come from [trugs-tools](https://github.com/TRUGS-LLC/TRUGS-TOOLS).
The [design record](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/SPEC/README.md) names the eleven specifications.

[CONCEPTS.md](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/CONCEPTS.md) — what makes a graph an executable TRUG, the failure modes
it addresses, and why the oracle is deterministic.

[`examples/faq-answer/`](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/examples/faq-answer/) — one executable TRUG choosing an
answer from a pinned FAQ TRUG, with the rail refusing a choice the FAQ does not hold.

[`examples/development-orchestration/`](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/examples/development-orchestration/) — seven
independently runnable development-role TRUGs composed into one provider-neutral,
fixture-only lifecycle that pauses for a human decision.

## License

Apache 2.0. Copyright 2026 TRUGS LLC — see
[LICENSE](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/LICENSE) and [NOTICE](https://github.com/TRUGS-LLC/TRUGS-RUNNER/blob/main/NOTICE).
