fix(cache): tell long-id steps apart under --strict-cache with an identity digest

Merged
SA
8d ago
+243-20
  • Under --strict-cache, a step whose key misses is checked for a recording of the same step under another key (packages/core/src/cache/rekeyed.ts).
  • That check compares provenance as an entry stores it, and recordedProvenance cuts testId, targetId, and agent to 300 characters (packages/core/src/cache/recorder.ts).
  • Two ids that differ only after character 299 cut to the same string, so a never-recorded test matched another test's recording and failed with a false REPLAY_STALE.
  • Now recordedProvenance also writes an optional identityDigest (SHA-256 of the whole redacted test id, target id, and agent name) when bound would make one of them ambiguous. That is when it cuts the name, or when the whole name already ends like a cut (… at 299 or 300 characters, which a createAgent cache key can). Short-id entries are unchanged and keep the same cache key.
  • readProvenance validates the digest like paramsDigest, and stepKey keys by it. A long-id step whose key really changed still fails with REPLAY_STALE.
  • An entry that cut a name before the digest existed never matches, so that step runs live. completeProvenance writes the digest into such an entry once a confirmed replay proves which step it belongs to.
  • readTraceEntry drops unknown fields, so older runners ignore the new one. There is no TRACE_SCHEMA_VERSION bump, the same as when paramsDigest, callIndex, and agent were added.
  • Rebased onto main after #1737 moved the cache into @e2e-dev/core, so the changeset names both e2e and @e2e-dev/core. Main is merged in at 66d07ca4, and the ja and zh cache tables get the new row.
  • completeProvenance rewrites a kept entry only when its stored provenance lacks the occurrence fields or holds another identity digest, so a confirmed long-name entry that already has the step's digest keeps its bytes.
const ambiguous = [testId, targetId, agent ?? ''].some((text) => boundIsAmbiguous(text, MAX_TRACE_DESCRIPTOR_CHARS));
// ...
...(ambiguous ? { identityDigest: sha256Hex(JSON.stringify([testId, targetId, agent ?? null])) } : {}),

Issue

Fixes #1809

Verified

Ran it locally: yes

Built CLI at e6a69cfa against a scratch web project (later commits change only comments, docs, and tests): two tests under a 342-character describe title, each running agent.act('increment the counter') with a scripted model. Run 1 records the first test only (e2e run --grep 'first variant'). Run 2 is CI=1 e2e run --strict-cache on both.

main (25bde7a7)this branch (e6a69cfa)
Tests 1 failed | 1 passed (2)
Cache 1 replayed · 1 missed
REPLAY_STALE: the recording of this step no longer replays: the store holds it under another cache key (…json), exit 2
Tests 2 passed (2)
Cache 1 replayed · 1 missed
second test ran live (2 model calls), exit 0
  • The recorded entry keeps a 300-character testId plus identityDigest. Main and this branch write the same key file in the same project (3b488c9e….json).
  • After changing the agent's context, --strict-cache on the first test fails with REPLAY_STALE ... since the agent's context (its context, or the test's agentContext) changed after it was recorded. A long-id step whose key really changed is still caught.
  • A short-id entry carries no identityDigest.
  • The same project with the describe title checkout flow passes 2 of 2 on main, so the id length is the trigger.
  • Mutations each fail the new tests:
    • the digest left out of stepKey, > changed to >=, only testId checked, the … case dropped, >= maxChars - 1 changed to >= maxChars
    • the digest over unredacted names, the length measured on unredacted names, the target or agent name not redacted
    • the digest dropped on read, the typeof check on a read digest dropped, the paramsDigest check dropped
    • no backfill, a backfill on every replay (recordedFor.identityDigest === undefined, which kdy1 tried), a backfill that never replaces or drops a stored digest
  • The no-rewrite test counts the store's writes, since a rewrite in the same millisecond keeps the same bytes.
  • pnpm check passes. e2e unit tests: 3974 passed, 2 skipped, on the branch with main merged in.

🤖 Generated with Claude Code