Skip to content

ref(toolkit-core): Share the response cache policy with the MCP - #1688

Merged
BYK merged 2 commits into
mainfrom
feat/shared-cache-policy
Oct 27, 2026
Merged

BYK merged 2 commits into
mainfrom
feat/shared-cache-policy

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Moves the CLI's response cache policy into @sentry/toolkit-core/response-cache so the MCP can use the same rules. This is task 4.1 of the unify-sentry-api-layer proposal (#1669). Codecs, the CacheStore interface, and the memory store (4.2–4.3) come in later PRs.

What moved:

  • classifyUrl and FALLBACK_TTL_MS: the fallback tiers from fix(cli): Stop caching events and traces for 24 hours #1681, unchanged. volatile is 60 seconds, stable is 5 minutes, and no-cache covers autofix and root-cause polling.
  • normalizeUrl and buildCacheKey: identity-scoped keys over the method and the full URL, including the host and sorted query params.
  • computeInvalidationPrefixes: cli/src/lib/cache-keys.ts and its tests were moved as-is. Only the comments that named CLI functions changed.

Key derivation moves to WebCrypto. buildCacheKey now uses crypto.subtle HMAC-SHA256 instead of node:crypto, so it runs in Workers and browsers. That makes it async; the call sites in response-cache.ts were already async. A test checks that the output matches createHmac("sha256", identity) byte for byte, so cache files written by released CLIs still hit. An empty identity now throws instead of producing a key that isn't scoped to anyone. The CLI never passes one, because getIdentityFingerprint() falls back to <anon>.

CLI changes:

  • response-cache.ts imports the policy. Its buildCacheKey wrapper, which defaults to the active identity, is now async, and its tests await it.
  • sentry-client.ts imports computeInvalidationPrefixes from toolkit-core.
  • classifyUrl/normalizeUrl tests import them from toolkit-core directly, so they aren't re-exported only for tests.

One test change outside the cache tests. WebCrypto signs on the libuv threadpool, so parallel requests can finish their cache lookups in either order. The getLogs batching test used to assume the 100-ID batch reached fetch first. It now checks for one request of each size, in any order. Promise.all still returns the batches in order, so behavior is unchanged.

Tests: new tiers.test.ts and keys.test.ts cover the spec scenarios:

  • tiers: organization event, issue event, full trace, polling, and the rule that no tier lasts more than 5 minutes
  • keys: different identities, different hosts, query order, node:crypto parity, and an empty identity

The invalidation tests moved with the module. Two spec scenarios are tested in the CLI instead: token refresh keeping the same identity (getIdentityFingerprint) and invalidation only touching the requester's entries (the disk store).

Checks:

  • pnpm run tsc, pnpm run lint, and pnpm run docs:check pass, and all 431 toolkit-core tests pass.
  • The full CLI unit suite has 6 local failures, all filesystem-permission tests that fail on main too, because the sandbox ignores read-only permissions. None touch the cache.

via BYK.

--

View Junior Session in Sentry

Moves the CLI's cache policy into `@sentry/toolkit-core/response-cache`
so the MCP can use the same rules (task 4.1 of the unify-sentry-api-layer
proposal, #1669):

- `classifyUrl` and `FALLBACK_TTL_MS`: the volatile/stable/no-cache tiers
  for responses without server cache headers.
- `normalizeUrl` and `buildCacheKey`: identity-scoped keys over the method
  and full URL. Key derivation now uses WebCrypto HMAC-SHA256 instead of
  `node:crypto`, so it runs in Workers and browsers. The output is
  byte-identical, so existing CLI cache files still hit, and an empty
  identity is rejected instead of producing an unscoped key.
- `computeInvalidationPrefixes`: moved unchanged from
  `cli/src/lib/cache-keys.ts`, with its tests.

The module has no storage or I/O. The CLI's disk cache imports it, and its
`buildCacheKey` wrapper (which defaults to the active identity) is now
async, as are the tests that call it.

WebCrypto HMAC runs on the libuv threadpool, so parallel requests can
finish their cache lookups in either order. The `getLogs` batching test
no longer assumes which batch reaches fetch first.

Co-Authored-By: BYK <burak.kaya@sentry.io>
@vercel

vercel Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Oct 27, 2026 11:18pm UTC

Request Review

sentry-junior Bot added a commit that referenced this pull request Oct 27, 2026
Co-Authored-By: BYK <burak.kaya@sentry.io>
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Oct 27, 2026
Comment thread packages/toolkit-core/src/response-cache/policy.property.test.ts
Comment thread packages/cli/test/lib/response-cache.test.ts
The tier, key, and URL normalization property tests now live next to the
code in `toolkit-core/src/response-cache/policy.property.test.ts`, with
`fast-check` added as a toolkit-core dev dependency. The CLI keeps only the
tests for its disk store and for its `buildCacheKey` wrapper's default
identity, which comes from the active login. Cases the property tests
already cover are dropped from `tiers.test.ts` and `keys.test.ts`.
@BYK
BYK enabled auto-merge (squash) October 27, 2026 23:23
@BYK
BYK merged commit 8860f39 into main Oct 27, 2026
42 checks passed
@BYK
BYK deleted the feat/shared-cache-policy branch October 27, 2026 23:32

This branch was previously deployed

1 inactive deployment
Preview — f506af4f Deployed Oct 27, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant