claude-code-cache-fix 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/preload.mjs +20 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-code-cache-fix",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "Fixes prompt cache regression in Claude Code that causes up to 20x cost increase on resumed sessions",
5
5
  "type": "module",
6
6
  "exports": "./preload.mjs",
package/preload.mjs CHANGED
@@ -942,10 +942,30 @@ function updateCacheControlStickyState(body, priorState) {
942
942
  capped = capped.slice(capped.length - CACHE_CONTROL_STICKY_MAX_POSITIONS);
943
943
  }
944
944
 
945
+ // Count existing cache_control markers across the entire body (system +
946
+ // messages) so sticky never pushes the total past Anthropic's hard limit
947
+ // of 4. CC may use 2 or 3 of those slots itself depending on version.
948
+ const ANTHROPIC_MARKER_LIMIT = 4;
949
+ let existingMarkers = 0;
950
+ if (Array.isArray(body.system)) {
951
+ for (const b of body.system) {
952
+ if (b && typeof b === "object" && b.cache_control) existingMarkers++;
953
+ }
954
+ }
955
+ for (const msg of body.messages) {
956
+ if (!msg || !Array.isArray(msg.content)) continue;
957
+ for (const b of msg.content) {
958
+ if (b && typeof b === "object" && b.cache_control) existingMarkers++;
959
+ }
960
+ }
961
+ const stickyBudget = Math.max(0, ANTHROPIC_MARKER_LIMIT - existingMarkers);
962
+
945
963
  // Compute mutations: for each tracked hash present in this body, if the
946
964
  // message doesn't already have any marker, add one at its last block.
965
+ // Stop once the sticky budget is exhausted.
947
966
  const mutations = [];
948
967
  for (const pos of capped) {
968
+ if (mutations.length >= stickyBudget) break;
949
969
  const msgIdx = hashToMsgIdx.get(pos.msg_hash);
950
970
  if (msgIdx === undefined) continue;
951
971
  const msg = body.messages[msgIdx];