|
| 1 | +# Flows |
| 2 | + |
| 3 | +Composable `.ad` snippets - bounded units of work. A flow may span one or multiple screens as long as it represents a coherent, reusable action with clear start (`@pre`) and completion (`@post`) checkpoints. Each flow advertises machine-matchable metadata (`@pre`, `@post`, `@tag`) via `# @`-prefixed comment headers, so an agent can pick the right one from a snapshot. |
| 4 | + |
| 5 | +## Agent decision loop |
| 6 | + |
| 7 | +Before manually navigating, use this human-in-the-loop loop: |
| 8 | + |
| 9 | +1. `agent-device snapshot -i` - see current state. |
| 10 | +2. `grep -H '^# @' .claude/skills/agent-device/flows/*.ad` - full catalog in one read. |
| 11 | +3. For each candidate flow, run `agent-device is exists "<selector>"` per `@pre`. Keep flows where every `@pre` passes. |
| 12 | +4. Rank survivors by goal closeness (`@post` overlap with the requested destination when present) and present top candidates to the user with a short "why this flow" note. |
| 13 | +5. Wait for user selection before replaying. **Auto-run is allowed only when there is exactly one survivor and it is an unambiguous match for an explicit user request.** |
| 14 | +6. `agent-device replay <path>`. |
| 15 | +7. If the flow declares `@post`, verify each `@post` with `is exists`. On success, re-enter the loop only if the user's stated goal is not complete; otherwise stop and report completion. On failure, propose peer flow/manual fallback options and ask before continuing. If no `@post` is declared (utility flow), rely on explicit user confirmation or the next snapshot before continuing. |
| 16 | + |
| 17 | +## Metadata header spec |
| 18 | + |
| 19 | +Each flow starts with `# @key value` comment lines. The `.ad` parser treats `#` lines as no-ops, so headers cost nothing at replay time. |
| 20 | + |
| 21 | +| Field | Cardinality | Value | |
| 22 | +| -------- | ----------- | ------------------------------------------------------------------------------------------------ | |
| 23 | +| `@desc` | 1 | One-line human summary. | |
| 24 | +| `@pre` | 1..N | Selector that must resolve in the current snapshot. Multiple lines are ANDed. | |
| 25 | +| `@post` | 0..N | Selector expected after replay. Multiple lines are ANDed. Used for chaining + success. | |
| 26 | +| `@tag` | 0..N | Free-form category (`auth`, `onboarding`, ...) or scoped (`sentry-<spanName>`). | |
| 27 | + |
| 28 | +Selector syntax matches the body: `id="..."`, `role="..." label="..."`, `text="..."`, `||` for fallbacks. |
| 29 | + |
| 30 | +## Parametrization (`agent-device` v0.13.0+) |
| 31 | + |
| 32 | +Lift body literals to named variables via `env` + `${VAR}` interpolation so values can be overridden at runtime without editing the file. |
| 33 | + |
| 34 | +| Construct | Where | Purpose | |
| 35 | +| ------------------ | -------------------- | -------------------------------------------------------------------------------- | |
| 36 | +| `env KEY=VALUE` | Header (after `# @`) | File-level default. Quote values with spaces or `||` chains: `env KEY="a || b"`. | |
| 37 | +| `${KEY}` | Body | Interpolation point. Resolves at replay time. | |
| 38 | +| `${KEY:-fallback}` | Body | Use `fallback` if `KEY` is unset. | |
| 39 | +| `\${KEY}` | Body | Literal `${KEY}` (escape). | |
| 40 | + |
| 41 | +Resolution precedence (high to low): CLI `-e KEY=VALUE` (repeatable) > shell `AD_KEY=...` (auto-imported, prefix stripped) > file `env` > built-ins (`AD_PLATFORM`, `AD_SESSION`, `AD_FILENAME`, `AD_DEVICE`, `AD_ARTIFACTS`). Unresolved `${X}` errors with `file:line`. |
| 42 | + |
| 43 | +Override at runtime without editing the file: |
| 44 | + |
| 45 | +```bash |
| 46 | +agent-device replay <flow>.ad -e EMAIL=other@example.com |
| 47 | +``` |
| 48 | + |
| 49 | +## Authoring rules |
| 50 | + |
| 51 | +- **No `open`, no `close`, no `context` header.** Caller owns lifecycle. |
| 52 | +- **No fixed `wait` calls.** `fill`/`press` resolve selectors with retry. Only add `wait <selector>` for real post-action blocks. |
| 53 | +- **Durable selectors.** Prefer `id=...` first, then `role=... label=...`, with `||` fallbacks. Avoid `@eN` refs. |
| 54 | +- **Every flow declares `@desc` and `@pre`.** Add `@post` for outcome-bearing flows; utility flows (for example `go-back`) may omit it. Add `@tag` when applicable. |
| 55 | +- **Keep scope coherent, not artificially tiny.** Flows can span multiple screens when that sequence is the reusable intent (for example "create and submit manual expense"). |
| 56 | +- **Peers share `@pre` and differ on `@post`.** One flow per narrow outcome is better than a mega-flow with conditional branches. |
| 57 | +- **Use `env` for substituted values.** If a literal is interpolated into the body, declare a matching `env` default and reference it as `${VAR}`. |
| 58 | + |
| 59 | +## Recording a new flow |
| 60 | + |
| 61 | +1. Drive the target screen manually. |
| 62 | +2. Start a session with `--save-script`: |
| 63 | + ```bash |
| 64 | + agent-device open <app> --save-script .claude/skills/agent-device/flows/<name>.ad |
| 65 | + ``` |
| 66 | +3. Perform the steps. |
| 67 | +4. `agent-device close` - flushes the `.ad`. |
| 68 | +5. Edit the generated file: |
| 69 | + - Delete the `context` line, leading `open ... --relaunch`, trailing `close`, and eyeballing `wait`s. |
| 70 | + - Add `@desc`, `@pre`, optional `@post`, and `@tag` headers. |
| 71 | +6. Verify: pre-check from a matching state, replay, post-check. |
| 72 | + |
| 73 | +## Maintenance |
| 74 | + |
| 75 | +Heal selector drift in place: |
| 76 | + |
| 77 | +```bash |
| 78 | +agent-device replay -u .claude/skills/agent-device/flows/<name>.ad |
| 79 | +``` |
| 80 | + |
| 81 | +Re-verify `@pre`/`@post` still hold, then commit. Note: `replay -u` is rejected when the script declares `env` directives (rewrite would drop them); strip the `env` block manually before healing, then re-add it. |
0 commit comments