Skip to content

core(cache): ActCache key derivation isn't URL-normalized, causing silent cache misses #2189

Description

@yawbtng

what's happening

ActCache.buildActCacheKey (packages/core/lib/v3/cache/ActCache.ts:184-195) hashes the page URL verbatim from safeGetPageUrl (packages/core/lib/v3/cache/utils.ts:10-16), which is just page.url(). There's no normalization step.

As a result, URLs that are semantically equivalent — same origin, same path, same set of query parameters — produce different cache keys whenever the parameters appear in a different order. The cache silently misses.

why it matters

Real-world URLs reorder query parameters all the time. Examples:

First visit Second visit Same page? Cache hits today?
?id=42&utm_source=email ?utm_source=email&id=42 yes no
?ref=home&page=2 ?page=2&ref=home yes no

Anything that runs through an email/SMS/ad funnel hits this, and most e-commerce / SaaS marketing URLs do. Every reordering invalidates the cache, which means an LLM call, which means cost + latency, which is the exact thing ENABLE_CACHING is meant to avoid.

proposed fix (conservative)

Sort query parameters before hashing. Strict superset of current cache hits — no current hit can become a miss, and previously-missed reorderings now hit. Nothing else changes: the stored entry.url would still be the raw URL (for debugging), only the key derivation gets normalized. No opinions on tracking-param stripping (utm_*, fbclid, etc.) and no fragment handling in this pass — both can be follow-ups if desired since they involve semantic choices.

Suggested code shape:

// in cache/utils.ts
export function normalizeUrlForCacheKey(rawUrl: string): string {
  if (!rawUrl) return rawUrl;
  try {
    const url = new URL(rawUrl);
    url.searchParams.sort();
    return url.toString();
  } catch {
    return rawUrl;
  }
}

Then ActCache.prepareContext passes normalizeUrlForCacheKey(pageUrl) into buildActCacheKey instead of the raw URL. Tests cover: same URL with reordered params, URL without params, malformed URL fallback.

scope

  • Files: packages/core/lib/v3/cache/ActCache.ts, packages/core/lib/v3/cache/utils.ts, new test under packages/core/tests/unit/.
  • One-time effect on existing on-disk caches: keys for URLs that have query params will change. Old entries become unreachable but don't break anything — they just sit until purged. Could ship behind a no-op in a separate PR if that's a concern.

Happy to open the PR right after this — wanted the issue on the books first as a reference.

Activity

  1. added a commit that references this issue on Jun 10, 2026
    cc07727
  2. added a commit that references this issue on Jun 10, 2026
    8d7d414
  3. added a commit that references this issue on Aug 4, 2026
    cc5235e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions