Caching
Reference for Cofactor's test cache. How playbooks and cached actions replay, cache-only runs, every cofactor workflow cache command and commit option, the playbook file fields and limits, and the assertion and capture shapes.
Cofactor records how each step of an E2E test worked, and replays that record on later runs. A replay runs your application again with fresh data and checks the same assertions. It does not reuse an earlier result. For the plain-language version, see Self-healing. To build a cache by hand, see Author repeatable cached tests.
What a cache holds
| Term | Meaning |
|---|---|
| Playbook | The ordered, readable steps for one node of a test. |
| Cached action | The executable form of a step: an interaction, its inputs, captured variables and checks. For browser steps it is a self-contained Playwright snippet run against the signed-in page. |
| Cache row | What Cofactor stores per node: the playbook and its cached actions. |
| Gap | A step that cannot replay yet. It is listed with a reason. |
Macros and variables stay dynamic. A cache keeps the $var and macro references, so each run uses its own values. Assertions stay part of the test, and a replay that skips one is not a working cache.
In a normal run Cofactor replays a cache that has proven itself. Otherwise an agent follows the playbook, repairs a step, or does the step fresh and records a new candidate. Editing a test or changing the application can make a cache unusable.
MCP steps cache their tool calls the same way. Cofactor records those itself, and you cannot author them.
Cache-only runs
cofactor workflow run <workflow id> --cache-only [--environment <name|id>]
| Behavior | Detail |
|---|---|
| Waits | The command waits for the run. It exits with code 1 when the run fails, times out or is canceled, and with code 2 when the result is inconclusive. |
| No agent for cached steps | A step that has no usable cache fails the run. The failure names the first step that could not replay. |
| Real changes | It is a real run. It writes and deletes in the application the same way the test does. |
| Still agent-driven | Login steps and steps set to always use the agent can still use an agent. |
| Safety check | If the created run does not record cacheOnly, the command cancels it and exits with an error. |
A green status does not prove every step replayed. Read the run's replay and assertion results.
Commands
| Command | What it does |
|---|---|
cofactor workflow cache status <workflow id> [--json] | Shows each node's cache. In the JSON, effectivePlaybooks[].activeCache is the cache in use. nodes[] can also list a newer row that is not usable. |
cofactor workflow cache commit <workflow id> --node <node id> --playbook <file> | Validates a playbook file and writes it to one node. |
cofactor workflow cache clear <workflow id> <mode> | Marks caches unusable, or deletes them. See below. |
cofactor workflow cache pin <workflow id> | Pins the cache. It is read-only at run time and recached offline. unpin reverses it. |
cofactor workflow run <workflow id> --cache-only | Runs from cache only. |
cofactor workflow debug cache <run id> [--misses-only] | Lists cache hits and misses for a run, with the reason for each miss. |
cofactor workflow debug overview <run id> | Prints a run summary, including the cache hit rate. |
cofactor workflow debug materialize <run id> [--out <dir>] | Writes the run's evidence to a folder. |
cofactor workflow guide playbook | Prints the playbook authoring guide. |
cofactor workflow guide assertions | Prints the assertion guide. |
cache clear takes exactly one mode:
| Mode | Effect |
|---|---|
--node <node id> | Marks one node's cache unusable. |
--below-version <n> | Marks caches with an older compiler version unusable. |
--mode all | Marks every cache row unusable. The rows stay. |
--mode hard --yes | Deletes the cache rows and strips recorded playbook steps from the node configs. This is the same as Reset flow in the app. |
--stale-only also exists for old rows and rarely matches anything now.
Commit options
| Option | Meaning |
|---|---|
| (none) | Validates and writes the playbook. The result says whether it installed your playbook, patched the row, or kept the one already there. |
--dry-run | Runs the validators and commit guards and writes nothing. It cannot be combined with --force or --verified. |
--force --expected-cache-id <id|none> --yes | Replaces the cache even when the normal rule would keep the existing one. Use the id you just read from cache status, or none when no cache exists for that node. Validation still applies. |
--verified --verified-from <run id> --verified-from <run id> | Imports content you attest two clean cache-only runs already proved. It needs --force, --expected-cache-id and --yes, and two different run ids. It does not run any proof. |
--unverified-from-trace --yes | Writes an unverified candidate from eligible trace evidence. The content must have no gaps and no agent actions. It cannot be combined with --dry-run, --force, --verified or --expected-cache-id. It was formerly --cache-only-boundary. |
--json | Prints the raw result. |
--playbook was formerly --recipe, which still works. workflow promote --force is a different option. It adds a test to suite runs and does not replace a cache.
A successful commit or dry run is not proof that the playbook replays. A cache-only run is.
Playbook file
A JSON object for one node. The server recomputes the node hash and ignores any hash you send.
| Field | Shape |
|---|---|
sourceRunId | UUID of a run of this same test that supplies the evidence. For a normal commit, the node must have succeeded in that run. |
playbookSteps | 1 to 200 readable steps, order numbered from 1. Each has an action and a target. A verify step carries the configured assertionId. |
plannedActions | Up to 200 executable actions. Each has stepOrder, toolName, actionType and an args object. |
plannedActions[].code | For run_code, a self-contained Playwright snippet of at most 16,000 characters. |
plannedActions[].selectors | Optional selector strategy for non-code actions. A run_code action puts its locators in code. |
plannedActions[].elementDescription | Optional text, at most 2,000 characters. |
plannedActions[].assertion | For an assert action, the assertion object below. |
gaps | Up to 200 entries of { "stepOrder": number, "reason": string } for steps that cannot replay yet. Use [] for a fully cached node. The reason is 1 to 2,000 characters. |
carriedActions | Optional, up to 200 entries of { "stepOrder": number, "action": object }, copied from an existing proven cache. |
variableValues | Optional map of variable names to recorded string values. |
Example action for a Save click:
{
"stepOrder": 1,
"toolName": "browser_run_code",
"actionType": "run_code",
"args": {},
"code": "await page.getByRole('button', { name: 'Save', exact: true }).click();"
}
Locator rules for cached code:
- Use semantic locators, such as a role and an accessible name.
- Cofactor refuses positional picks such as
.first(),.last()and.nth()that choose an element to act on. Identify each target by its role, label or text. - It always refuses ephemeral element references, fixed table indexes and selectors built from volatile values.
Assertion object
A cached assertion is an action with actionType: "assert", args: {} and an assertion object.
| Field | Meaning |
|---|---|
type | One of text_present, text_absent, element_visible, element_hidden, evaluate, playwright, toast_present, download_present, download_content. |
condition | Required, not empty. The text, selector or code for the type. |
expected | Optional value to compare with. |
severity | Optional. |
inlineAssertionId | The id of the configured assertion this one fulfills. |
label | Optional. |
For playwright and evaluate, the condition returns a boolean. A condition that ends in a bare textContent(), innerText(), inputValue() or count() call with no expected is rejected, because the result alone does not say pass or fail. Add expected to compare the value, or return a comparison.
{
"type": "playwright",
"condition": "return await page.getByRole('status').innerText() === 'Saved';",
"inlineAssertionId": "<configured assertion id>",
"severity": "fail"
}
Capture a value
A step can save a value for later steps.
- Declare the output name in the node's
declaredOutputs. - Give the readable step
"action": "capture"and"params": { "saveAs": "<name>" }. - Give its
run_codeaction"args": { "saveAs": "<name>" }, and return a string. UseJSON.stringifyto return several values. - Captured strings are limited to 2,048 characters.
- Later snippets and assertions read the value as
vars.<name>.
What the server checks on commit
| Check | Covers |
|---|---|
| Shape | Field types, supported actions, code length, assertion objects and option combinations. |
| Test fidelity | Required macros and variable references stay present. Assertion ids are known and not claimed twice. |
| Replay admission | Actions must be executable under the cache contract. Strict checks cover positional locators and unstable anchors. |
| Write guards | The source run belongs to the test and your organization. A forced write names the cache it replaces. A verified import names two proof runs. |
Validators check the file. They do not check that a locator finds anything on the live page. Only a run does.
Related
- Author repeatable cached tests walks the CLI loop.
- Self-healing explains replay, repair and test health.
- Assertions covers the checks a replay keeps.
- Runs covers run results and evidence.