# From a rule to an agent workflow

AgentRun combines code, typed judgments, and agent sessions in one inspectable workflow. Start with the smallest kind of work that answers the question, then connect those pieces with explicit branches and limits.

This primer uses the same fictional vendor and supplied documents as the [vendor-review example](vendor-review.md). Its decisions are scripted. The interpreter executes the actual code, judgments, branches, schemas, and review limits; no model is called.

```sh
npm ci
npm run build
node examples/primer.mjs
```

Use `node examples/primer.mjs --json` to inspect every lesson's inputs, observed outputs, requests, and events. The implementation is [examples/primer.mjs](../examples/primer.mjs).

## 1. Use code for a rule you already know

Does a citation identify a source in the supplied pack?

```js
(state) => ({
  knownCitation: state.sources.some((source) => source.id === state.sourceId),
});
```

The primer executes this function as a `code` node. `P1` returns `true`; `missing-citation` returns `false`. No agent or judge adapter is supplied to this workflow, so the check uses zero inference calls.

Membership is a mechanical fact. It does not establish that a quotation supports a claim, that the document is accurate, or that a recommendation is justified. Keep those questions distinct.

## 2. Use System One for a focused judgment

Are the research findings supported, conflicting, or missing evidence? A `judge` node asks that question with a typed answer:

```json
{
  "node": "judge",
  "label": "evidence-classifier",
  "state": {
    "requiredTopics": "{requiredTopics}",
    "evidence": "{evidence}"
  },
  "out": "Classification",
  "as": "classification"
}
```

`Classification` defines the answer choices and the whole rubric. Every required topic must have cited evidence. Missing topics take priority; disagreement between covered topics means conflict. The output is one of `supported`, `conflicting`, or `missing`, with the raw answers retained beside it.

System One is the interface for small typed judgments. A host-supplied adapter answers those questions; Jev is one supported implementation. This primer's adapter returns deterministic fixture answers. Its values of zero and one are not measured confidence or a quality claim about a live model.

### Test that subsystem without running the whole team

The primer lifts the exact classifier node and its complete schema into a small, independently runnable workflow. It supplies frozen research findings and checks three host-owned expected answers:

| Supplied findings                   | Expected answer | Observed fixture answer |
| ----------------------------------- | --------------- | ----------------------- |
| Complete, consistent pilot evidence | `supported`     | `supported`             |
| Conflicting deletion policies       | `conflicting`   | `conflicting`           |
| No deletion policy                  | `missing`       | `missing`               |

Each case runs one judge request and no researchers or writer. That makes a changed rubric or judge adapter testable without rerunning the upstream work.

The primer also injects a deliberate regression: an adapter returns `supported` when the deletion policy is missing. The answer still matches the schema, so the interpreter completes. The independent expectation records **expected `missing`, actual `supported`, check failed**. A valid shape does not establish a correct judgment.

This failed check is an intentional lesson output, not a hidden runtime failure. The tests assert that the check fails. The primer CLI exits successfully when it has demonstrated that behavior.

## 3. Give an agent a job that needs several steps

The product researcher gathers cited findings for integrations, EU residency, and the pilot's scope. It receives an explicit projection of the request, sources, and topics, plus an output schema. Its result contains three findings with source IDs and exact quotations.

An `agent` node delegates a session to the host. The host decides how to run that session, which tools it may use, and its resource budget. The node describes the job and the information it receives. The scripted session here returns a fixed research result; a live adapter could search or inspect documents before submitting the same structured output.

## 4. Connect the pieces with control flow

The [complete vendor workflow](vendor-review.md) composes those pieces:

- A router sends a narrow integration question to one fact checker, or starts a full review.
- The full review runs product and security researchers in parallel, then joins their findings.
- The classifier marks the evidence. A code gate creates a review item only for a conflict, so a senior reviewer runs only when needed.
- Missing or unresolved evidence ends in a human handoff. Sufficient evidence goes to a writer whose submission must pass verification.

These choices change which sessions the host has to run. The primer counts actual adapter calls from all four retained scenarios:

| Scenario             | Agent sessions | Judge requests | Of those, submission reviews |
| -------------------- | -------------: | -------------: | ---------------------------: |
| Full pilot           |              3 |              3 |                            1 |
| Conflicting evidence |              4 |              4 |                            2 |
| Quick question       |              2 |              3 |                            1 |
| Missing evidence     |              2 |              2 |                            0 |

A quick question skips both researchers and uses a fact checker plus a writer. A full, consistent review uses two researchers plus a writer. A conflict adds a senior reviewer and a second submission review. Missing evidence stops before a writer is started.

These are counts of host adapter calls, not dollar savings or latency benchmarks. One agent session may use multiple model turns and tools. Actual spending depends on the host's implementation, models, token use, and pricing. Explicit branches make the work inspectable and let you avoid starting sessions that an input does not need.

## 5. Put execution limits in the workflow

The writer's `verify` clause permits at most two submission reviews. In the conflict fixture, draft one has an invalid citation and is rejected; the corrected second draft passes. The real interpreter emits both review events. The adapter receives review feedback and can repair the draft within its session. Exhausting the bound fails the workflow.

The missing-evidence case instead returns `status: "escalated"` with this reason:

> Request a documented deletion policy from the vendor before recommending a pilot.

No writer session starts. The result retains the source evidence, so a person can investigate the gap. The workflow does not replace missing evidence with a plausible conclusion.

A local test suite checks the code results, isolated classifier, caught regression, observed resource counts, repair bound, and escalation:

```sh
node --test examples/primer.test.mjs
```

For the complete executable workflow and host adapter interfaces, continue to the [vendor-review walkthrough](vendor-review.md). For every primitive's contract, see the [DSL guide](guide.md).
