claude-code-cache-fix 1.6.0 → 1.6.2-debug.1

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 +26 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-code-cache-fix",
3
- "version": "1.6.0",
3
+ "version": "1.6.2-debug.1",
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
@@ -162,10 +162,19 @@ function sortSkillsBlock(text) {
162
162
  const match = text.match(
163
163
  /^([\s\S]*?\n\n)(- [\s\S]+?)(\n<\/system-reminder>\s*)$/
164
164
  );
165
- if (!match) return text;
165
+ if (!match) {
166
+ debugLog("SKILLS SORT: regex did NOT match — block passed through unsorted",
167
+ `(length=${text.length}, starts=${JSON.stringify(text.slice(0, 80))})`);
168
+ return text;
169
+ }
166
170
  const [, header, entriesText, footer] = match;
167
171
  const entries = entriesText.split(/\n(?=- )/);
172
+ const preSort = entries.map(e => (e.match(/^- ([^:]+)/) || [])[1] || "?");
168
173
  entries.sort();
174
+ const postSort = entries.map(e => (e.match(/^- ([^:]+)/) || [])[1] || "?");
175
+ const orderChanged = preSort.some((name, i) => name !== postSort[i]);
176
+ debugLog(`SKILLS SORT: ${entries.length} entries, order ${orderChanged ? "CHANGED" : "unchanged"}`,
177
+ `footer=${JSON.stringify(footer)}`);
169
178
  return header + entries.join("\n") + footer;
170
179
  }
171
180
 
@@ -962,12 +971,18 @@ globalThis.fetch = async function (url, options) {
962
971
  }
963
972
 
964
973
  // Clone response to extract TTL tier and usage telemetry from SSE stream.
965
- // Pass the model from the request so we can log a complete usage record.
974
+ // Pass the model and quota headers so we can log a complete usage record.
966
975
  try {
967
976
  let reqModel = "unknown";
968
977
  try { reqModel = JSON.parse(options?.body)?.model || "unknown"; } catch {}
978
+ const quotaHeaders = {
979
+ q5h: parseFloat(response.headers.get("anthropic-ratelimit-unified-5h-utilization") || "0"),
980
+ q7d: parseFloat(response.headers.get("anthropic-ratelimit-unified-7d-utilization") || "0"),
981
+ status: response.headers.get("anthropic-ratelimit-unified-status") || null,
982
+ overage: response.headers.get("anthropic-ratelimit-unified-overage-status") || null,
983
+ };
969
984
  const clone = response.clone();
970
- drainTTLFromClone(clone, reqModel).catch(() => {});
985
+ drainTTLFromClone(clone, reqModel, quotaHeaders).catch(() => {});
971
986
  } catch {
972
987
  // clone() failure is non-fatal
973
988
  }
@@ -988,7 +1003,7 @@ globalThis.fetch = async function (url, options) {
988
1003
  * Writes TTL tier to ~/.claude/quota-status.json (merges with existing data)
989
1004
  * and logs to debug log.
990
1005
  */
991
- async function drainTTLFromClone(clone, model) {
1006
+ async function drainTTLFromClone(clone, model, quotaHeaders) {
992
1007
  if (!clone.body) return;
993
1008
 
994
1009
  const reader = clone.body.getReader();
@@ -1082,8 +1097,11 @@ async function drainTTLFromClone(clone, model) {
1082
1097
  if (startUsage) {
1083
1098
  try {
1084
1099
  const cc = startUsage.cache_creation || {};
1100
+ const now = new Date();
1101
+ const utcHour = now.getUTCHours();
1102
+ const utcDay = now.getUTCDay();
1085
1103
  const record = {
1086
- timestamp: new Date().toISOString(),
1104
+ timestamp: now.toISOString(),
1087
1105
  model: model || "unknown",
1088
1106
  input_tokens: startUsage.input_tokens ?? 0,
1089
1107
  output_tokens: deltaUsage?.output_tokens ?? 0,
@@ -1092,6 +1110,9 @@ async function drainTTLFromClone(clone, model) {
1092
1110
  ephemeral_1h_input_tokens: cc.ephemeral_1h_input_tokens ?? 0,
1093
1111
  ephemeral_5m_input_tokens: cc.ephemeral_5m_input_tokens ?? 0,
1094
1112
  ttl_tier: ttlTier,
1113
+ q5h_pct: quotaHeaders ? Math.round(quotaHeaders.q5h * 100) : null,
1114
+ q7d_pct: quotaHeaders ? Math.round(quotaHeaders.q7d * 100) : null,
1115
+ peak_hour: utcDay >= 1 && utcDay <= 5 && utcHour >= 13 && utcHour < 19,
1095
1116
  };
1096
1117
  appendFileSync(USAGE_JSONL, JSON.stringify(record) + "\n");
1097
1118
  } catch {