|
| 1 | +# Flows |
| 2 | + |
| 3 | +## Directory layout |
| 4 | + |
| 5 | +- `macros/` - reusable helpers for common setup/navigation actions that stop in a navigable state for further interactive work. |
| 6 | +- `tests/` - critical-scenario scripts for QA/perf verification that assert explicit outcomes (for example Sentry spans) and then stop. |
| 7 | + |
| 8 | +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`, `@param`) via `# @`-prefixed comment headers, while flow type is derived from location (`flows/macros/` or `flows/tests/`). |
| 9 | + |
| 10 | +## Agent decision loop (interactive) |
| 11 | + |
| 12 | +Before manually navigating, use this human-in-the-loop loop: |
| 13 | + |
| 14 | +1. `agent-device snapshot -i` - see current state. |
| 15 | +2. `grep -H '^# @' .claude/skills/agent-device/flows/macros/*.ad` - interactive catalog. |
| 16 | +3. For each candidate flow, run `agent-device is exists "<selector>"` per `@pre`. Keep flows where every `@pre` passes. |
| 17 | +4. Rank survivors by goal closeness and present top macro candidates to the user with a short "why this flow" note: |
| 18 | + - Prefer flows whose `@post` selectors literally match destination language from the user request (same `text`, `label`, or selector phrase). |
| 19 | +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.** |
| 20 | + - Only propose flows from `flows/macros/` in interactive usage. |
| 21 | +6. Scan selected flow `# @param` headers. Ask the user for any missing parameter values, then build explicit CLI args (`-e KEY=VALUE`) for replay. |
| 22 | +7. `agent-device replay <path> -e KEY=VALUE ...`. |
| 23 | +8. 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. |
| 24 | + |
| 25 | +## QA workflow (separate) |
| 26 | + |
| 27 | +`flows/tests/` is reserved for dedicated QA automation and should not be offered to users as part of the interactive helper loop above. Run these flows with the dedicated test runner: |
| 28 | + |
| 29 | +```bash |
| 30 | +agent-device test .claude/skills/agent-device/flows/tests/<name>.ad -e KEY=VALUE ... |
| 31 | +``` |
| 32 | + |
| 33 | +## Metadata header spec |
| 34 | + |
| 35 | +Each flow starts with `# @key value` comment lines. The `.ad` parser treats `#` lines as no-ops, so headers cost nothing at replay time. |
| 36 | + |
| 37 | +| Field | Cardinality | Value | |
| 38 | +| -------- | ----------- | ------------------------------------------------------------------------------------------------ | |
| 39 | +| `@desc` | 1 | One-line human summary. | |
| 40 | +| `@pre` | 1..N | Selector that must resolve in the current snapshot. Multiple lines are ANDed. | |
| 41 | +| `@post` | 0..N | Selector expected after replay. Multiple lines are ANDed. Used for chaining + success. | |
| 42 | +| `@tag` | 0..N | Free-form category (`auth`, `onboarding`, ...) or scoped (`sentry-<spanName>`). | |
| 43 | +| `@param` | 0..N | Runtime input contract: `@param KEY description.` Use with `${KEY}` in flow body. | |
| 44 | + |
| 45 | +Selector syntax matches the body: `id="..."`, `role="..." label="..."`, `text="..."`, `||` for fallbacks. |
| 46 | + |
| 47 | +## Parametrization (`agent-device` v0.13.0+) |
| 48 | + |
| 49 | +Declare runtime inputs via metadata (`@param`) and reference them in the body with `${VAR}` interpolation. Values are supplied by caller arguments (`-e`) or shell imports (`AD_VAR_*`) - never by in-file `env` directives. |
| 50 | + |
| 51 | +| Construct | Where | Purpose | |
| 52 | +| ------------------ | ------------------------ | -------------------------------------------------------------------------------- | |
| 53 | +| `# @param KEY ...` | Metadata header comments | Declares expected input and documents meaning for the agent/user handoff. | |
| 54 | +| `${KEY}` | Body | Interpolation point. Resolves at replay time. | |
| 55 | +| `${KEY:-fallback}` | Body | Use `fallback` if `KEY` is unset. | |
| 56 | +| `\${KEY}` | Body | Literal `${KEY}` (escape). | |
| 57 | + |
| 58 | +Resolution precedence (high to low): CLI `-e KEY=VALUE` (repeatable) > shell `AD_VAR_KEY=...` (auto-imported as `KEY`) > built-ins (`AD_PLATFORM`, `AD_SESSION`, `AD_FILENAME`, `AD_DEVICE`, `AD_ARTIFACTS`). Unresolved `${X}` errors with `file:line`. |
| 59 | + |
| 60 | +Override at runtime without editing the file: |
| 61 | + |
| 62 | +```bash |
| 63 | +agent-device replay <flow>.ad -e EMAIL=other@example.com |
| 64 | +``` |
| 65 | + |
| 66 | +## Authoring rules |
| 67 | + |
| 68 | +- **No `open`, no `close`, no `context` header.** Caller owns lifecycle. |
| 69 | +- **No fixed `wait` calls.** `fill`/`press` resolve selectors with retry. Only add `wait <selector>` for real post-action blocks. |
| 70 | +- **Durable selectors.** Prefer `id=...` first, then `role=... label=...`, with `||` fallbacks. Avoid `@eN` refs. |
| 71 | +- **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. |
| 72 | +- **Choose directory intentionally.** Put reusable setup/navigation steps in `flows/macros/`; put outcome verification scenarios in `flows/tests/`. |
| 73 | +- **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"). |
| 74 | +- **Peers share `@pre` and differ on `@post`.** One flow per narrow outcome is better than a mega-flow with conditional branches. |
| 75 | +- **Use `@param` for substituted values.** If a literal is interpolated into the body, declare `# @param KEY description.` and reference it as `${KEY}`. |
| 76 | +- **Do not use `env` directives in repo flows.** Runtime values must come from `-e KEY=VALUE` (preferred) or `AD_VAR_KEY=...`. |
| 77 | +- **Use inline defaults sparingly.** Optional tuning values can use `${KEY:-fallback}` in the body; required values should have no fallback and must be provided by caller input. |
| 78 | + |
| 79 | +## Recording a new flow |
| 80 | + |
| 81 | +1. Drive the target screen manually. |
| 82 | +2. Start a session with `--save-script`: |
| 83 | + ```bash |
| 84 | + agent-device open <app> --save-script .claude/skills/agent-device/flows/<kind>/<name>.ad |
| 85 | + ``` |
| 86 | +3. Perform the steps. |
| 87 | +4. `agent-device close` - flushes the `.ad`. |
| 88 | +5. Edit the generated file: |
| 89 | + - Delete the `context` line, leading `open ... --relaunch`, trailing `close`, and eyeballing `wait`s. |
| 90 | + - Move file to `flows/macros/` or `flows/tests/`, then add `@desc`, `@pre`, optional `@post`, optional `@tag`, and any needed `@param` headers. |
| 91 | +6. Verify: pre-check from a matching state, replay, post-check. |
| 92 | + |
| 93 | +## Maintenance |
| 94 | + |
| 95 | +Heal selector drift in place: |
| 96 | + |
| 97 | +```bash |
| 98 | +agent-device replay -u .claude/skills/agent-device/flows/<kind>/<name>.ad |
| 99 | +``` |
| 100 | + |
| 101 | +Re-verify `@pre`/`@post` still hold, then commit. Note: `replay -u` can rewrite interpolated lines to concrete selectors/values; review diffs and restore `${KEY}` placeholders where needed. Keep runtime inputs in `@param` + `-e`/`AD_VAR_*`; do not reintroduce in-file `env` directives. |
0 commit comments