# Playbooks & Cache

A playbook is a sequence of human-readable steps that describe the actions
taken during a workflow node's execution. Playbooks bridge the gap between
high-level node descriptions ("Create a ticket") and the low-level browser
interactions that fulfill them.

## What a playbook contains

Each playbook is an ordered list of PlaybookSteps:

- `order`: Step sequence number
- `action`: What to do: `click`, `fill`, `fill_form`, `select`, `navigate`,
  `wait`, `scroll`, `hover`, `upload`, `verify`, `handle_native_dialog`,
  `toggle`, `press_key`, `focus`, `drag`, `resize`, `capture`,
  `extract_table`, `python_execute`, `verify_download`,
  `verify_download_content`, `screenshot`, `agent_loop`, `call`
- `target`: The element or area being acted on
- `params`: Key-value parameters for the action
- `note`: Human-readable description of the step

Each step is one atomic intent executed in one tool call, bounded by stable
page states. Fuse actions coupled through ephemeral UI state — opening a
transient menu and choosing its item, for example — into that call. Split
unrelated intents and every stable intermediate into separate steps.

Example playbook for "Create a ticket":

1. `click`: "New Ticket" button
2. `fill_form`: Title: "QA Test — Billing Bug", Priority: "High"
3. `click`: "Submit" button
4. `verify`: Success toast appears

## How playbooks enable cache replay

When a workflow runs for the first time, the AI agent explores the
application, performs actions, and generates a playbook for each node.
The raw browser actions (clicks, keystrokes, waits) are cached alongside
the playbook steps.

On subsequent runs, the system uses playbook-step-driven replay:

1. Load the playbook steps from the node config
2. For each step, look up the cached browser actions
3. Replay the cached actions directly (no AI agent needed)
4. If a step's cache misses or fails, the AI agent heals that step

This means most runs execute instantly via cache, with the agent only
invoked for steps that have changed or broken.

## Compiled entries

The cache maps playbook steps to cached action indices:

- `cached`: Playbook step has matching cached actions — replayed directly
- `gap`: No cache for this step — agent performs it fresh

A fully cached node replays in seconds. A node with gaps falls back to
agent execution only for the missing steps.

## Verify steps and assertions

A `verify` (or `verify_download`/`verify_download_content`) step names the
inline assertion it proves via `assertionId` — the assertion's UUID from
the node's inlineAssertions, or `'completion'` for the step that closes
out the node's job-to-be-done. The step sits at the position in the flow
where the check actually happened, not appended at the end.

A verify step whose assertionId matches a recorded assertion compiles into
the cache at that position, alongside the other steps. An assertion the
agent didn't place in the flow gets a synthesized trailing verify step
instead.

Agentic assertions (agent-evaluated, not mechanically checkable) never get
a verify step — they can't compile and are judged live during replay.

See `assertions.txt` for how inline assertions are authored, severities, and
when to use agentic evaluation.

## Interaction recipes

Interaction recipes are property-scoped, reusable interaction mechanics.
Calling one executes its transaction atomically and appears in the playbook
as one `call` step, with a human-readable target and the recipe arguments.

## Playbook health

Each cached node tracks playbook health:

- `verified`: Cache is working correctly across recent runs
- `degraded`: Some steps have failures — cache still used but monitored
- `stale`: Cache is no longer reliable — full agent re-execution needed

Step-level failure counts track which specific steps break most often,
guiding targeted cache healing.

## Why playbooks matter for authoring

When you build a workflow, the quality of your node descriptions directly
affects playbook quality. Clear, intent-focused descriptions produce
better playbooks, which produce more reliable cache replay.

- Good: "Create a ticket with title 'QA Test' and priority 'High'"
  → Clean playbook: `fill_form` with clear fields, click submit
- Bad: "Do the thing on the page"
  → Vague playbook: agent guesses at actions, cache breaks easily

The playbook is the semantic contract between your intent and the
execution engine. Write nodes that describe what a human would do,
and the playbook will faithfully capture and replay it.
