fix(cache): replay unique() params whose paths contain pipes, closing braces or line breaks

MI
3h ago
+130-5

What

Replay cache placeholders now distinguish pipes and closing delimiters inside unique() parameter paths. A name such as x|slug or x|} resolves to its own value alongside a shorter sibling. A line break in a name also round-trips. Encoding lookup in parsePlaceholder uses Object.hasOwn so inherited Object.prototype names like toString are rejected.

Compared with the merge base, steps get a new key when a marked path contains | or }}, or ends in }. In read-write mode those steps record again. Under --strict-cache they miss with no-entry and run live without writing. An isolated internal brace and a line break alone keep their existing keys.

Why

parsePlaceholder splits on the last | to read an optional encoding (uri, form, slug, flat). When a step marked both x and x|slug with unique(), the recording wrote {{param:/x|slug}} for x|slug and replay parsed it back as /x in slug form, typing "delta-updated" instead of "Gamma Updated" and passing silently when the end state still matched. Even without a sibling x, a recording containing the raw placeholder of a key with | failed to expand and fell into invalid-entry.

How it works

flowchart LR
  A["paramPointer('', 'x|slug') -> /x|slug"] --> B["spellPointer -> /x~2slug"]
  B --> C["placeholder -> {{param:/x~2slug}} or {{param:/x~2slug|slug}}"]
  C --> D["parsePlaceholder splits last |, checks Object.hasOwn(ENCODERS)"]
  D --> E["readPointer -> /x|slug"]
  1. spellPointer escapes every | as ~2. It escapes a closing brace as ~3 only before another closing brace or at the end of the pointer. Because paramPointer already escapes literal tildes as ~0, neither spelling collides with a literal key. Percent signs and ordinary internal braces keep their spelling.
  2. The placeholder matcher accepts line terminators inside a path. readPointer restores the pipe and brace escapes after parsePlaceholder splits off the optional encoding. Object.hasOwn(ENCODERS, encoding) rejects inherited names.
  3. templateParams uses the same pointer spelling when deriving the cache key. An older recording with an ambiguous brace misses before any cached action executes.

Escaping % and | as %25 and %7C while keeping keys unchanged would misread pre-fix recordings for keys containing those strings. Escaping every brace would unnecessarily change the existing {{param:/a}b}} spelling. Bumping REPLAY_POLICY_VERSION would invalidate every committed cache.

Issue

None.

Verified

Ran it locally: yes

Closing braces and line breaks

Ran locally with the built CLI, Chromium and a scripted executor. Each case recorded a title, replayed with a different title, and read the app server's saved value. A separate run with caching disabled established the expected write. On the initial PR revision, the trailing brace, adjacent braces and line break each produced the wrong write twice. All five fresh recordings below replayed on the fix with zero executor calls and saved exactly Gamma Updated.

Marked nameStrict replay of a merge-base recordingStrict replay of an initial PR recording
`xslug, beside x`Ran live
`x}, beside x`
x}}tail, beside xRan liveRan live
A pipe and a line feed in one nameRan liveReplayed
a}b, beside aReplayedReplayed

Every live fallback above reported no-entry and saved the expected title. The eight new unit cases failed on the initial PR revision and passed with the fix. They cover all four JavaScript line terminators, repeated braces, raw and slugged values, and a literal ~3 beside an escaped brace.

Pipes, captured at 2346af099

  • Built CLI via our external cache-runner harness (runs packages/e2e/dist/cli/bin.js run with a scripted executor and no model against a local fixture app) on param-pipe-key (params: { 'x|slug': unique(...), x: unique(...) }, record with 'Beta Original' / 'Alpha Original', replay with 'Gamma Updated' / 'Delta Updated'), comparing main (7c1b2b258) and this branch (2346af099, clean tree). Stderr case lines below (paths abbreviated; stdout summary JSON omitted):
$ node cache-runner/run.mjs --repo <base-7c1b2b258> --cases param-pipe-key --save-recordings <base-rec> --out base-pr1.json
param-pipe-key         benign   unsafe         replay=self-finalized: ref=passed

$ node cache-runner/run.mjs --repo <head-2346af099> --cases param-pipe-key --save-recordings <head-rec> --out pr1-fresh.json
param-pipe-key         benign   replayed       replay=self-finalized: ref=passed

$ node cache-runner/run.mjs --repo <head-2346af099> --cases param-pipe-key --frozen <base-rec> --out pr1-frozen.json
param-pipe-key         benign   missed         replay=missed:no-entry ref=passed

$ node cache-runner/run.mjs --repo <base-7c1b2b258> --cases param-pipe-key --frozen <head-rec> --out base-on-pr1.json
param-pipe-key         benign   missed         replay=missed:no-entry ref=passed
scenarioreader: main (7c1b2b258)reader: this PR (2346af099)
param-pipe-key (fresh recording)unsafe (self-finalized, wrote "delta-updated", expected "Gamma Updated")replayed (self-finalized, wrote "Gamma Updated")
param-pipe-key (main recording)unsafe (self-finalized, wrote "delta-updated")missed (no-entry, agent wrote "Gamma Updated")
param-pipe-key (this PR's recording)missed (no-entry, agent wrote "Gamma Updated")replayed (self-finalized, wrote "Gamma Updated")
  • All 4 new tests in packages/e2e/tests/unit/trace-template.test.ts fail on main (7c1b2b258) and pass on 2346af099; 5/5 hand-written mutants on template.ts are killed by AssertionError.
  • pnpm bench:ab not run: across all 70 committed benchmark recordings (31 web, 39 mobile), the only {{param: slot is {{param:/label}}, so no committed entry holds | or changes behavior.

Docs page

No screenshot of the rendered docs/cache.mdx: the change there is one paragraph of prose in the strict-mode section.

Summary by CodeRabbit

  • Bug Fixes
    • Cache replay now handles unique() parameters whose paths contain pipes, closing braces, or line breaks.
    • Some earlier recordings may no longer match these paths and will run live with --strict-cache until refreshed in read-write mode.
  • Documentation
    • Updated cache guidance to explain when earlier recordings run live and how to refresh them.