pi-cache-optimizer 2.0.1 → 2.0.2

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.
@@ -49,6 +49,29 @@ const OPENAI_PROMPT_CACHE_KEY_PREFIX = "pi-dsco-";
49
49
  const NO_AUTO_CONFIG_ENV = "PI_CACHE_OPTIMIZER_NO_AUTO_CONFIG";
50
50
  const DEEPSEEK_API_KEY_ENV = "DEEPSEEK_API_KEY";
51
51
 
52
+ // Minimum trimmed length for a candidate to qualify as a stable-prefix "part".
53
+ //
54
+ // `optimizeSystemPrompt` removes each accepted candidate from the dynamic
55
+ // remainder via `rest.replace(part, "")`. Short or character-class candidates
56
+ // (think: `S`, `- u`, `- (`, `- }`) match the FIRST occurrence of those bytes
57
+ // anywhere in `rest`, ripping unrelated text out of the prompt and yielding a
58
+ // non-deterministic dynamic remainder per request. Both behaviors poison the
59
+ // provider's prompt-prefix cache.
60
+ //
61
+ // The threshold also caps the upstream string-vs-array regression we saw with
62
+ // trellis 0.5.16 / 0.6.0-beta.17 (subagent tool registration passing
63
+ // `promptGuidelines: "<long string>"` instead of `["<long string>"]`, which
64
+ // pi then iterates char-by-char). Even if a similar bug recurs upstream, this
65
+ // extension will not lift its single-character byproducts into the stable
66
+ // prefix candidate list.
67
+ //
68
+ // 8 chars is comfortably above all single-bullet (`- X` = 3 chars) and
69
+ // short-token noise while leaving every legitimate guideline / tool snippet /
70
+ // context-file payload above the bar. If a real future guideline is shorter
71
+ // than 8 chars, the cost is that it is not lifted into the stable prefix; the
72
+ // dynamic-remainder path still includes it untouched.
73
+ const MIN_STABLE_CANDIDATE_LENGTH = 8;
74
+
52
75
  const ASSISTANT_MESSAGE_MODEL_TOKEN_KEYS = ["model", "name"];
53
76
  const OPENAI_REASONING_MODEL_PATTERN = /(^|[/\s:_-])o[1345]($|[-_.:/\s])/;
54
77
 
@@ -187,9 +210,11 @@ function optimizeSystemPrompt(
187
210
  let rest = original;
188
211
 
189
212
  // Stable layer: content likely to be identical across sessions/turns.
213
+ // Short / single-char candidates are dropped: see MIN_STABLE_CANDIDATE_LENGTH.
190
214
  for (const candidate of buildStableCandidates(opts)) {
191
215
  const part = candidate.trim();
192
- if (!part || seen.has(part) || !rest.includes(part)) continue;
216
+ if (!part || part.length < MIN_STABLE_CANDIDATE_LENGTH) continue;
217
+ if (seen.has(part) || !rest.includes(part)) continue;
193
218
 
194
219
  stableParts.push(part);
195
220
  seen.add(part);
@@ -1005,6 +1030,15 @@ function emitDeepseekApiKeyHintIfNeeded(
1005
1030
  );
1006
1031
  }
1007
1032
 
1033
+ // Internal helpers exported only so the task verification script
1034
+ // (.trellis/tasks/.../verify.ts) can exercise them. They are not part of the
1035
+ // extension's public API; pi only invokes the default export below.
1036
+ export const __internals_for_tests = {
1037
+ buildStableCandidates,
1038
+ optimizeSystemPrompt,
1039
+ MIN_STABLE_CANDIDATE_LENGTH,
1040
+ };
1041
+
1008
1042
  export default function (pi: ExtensionAPI) {
1009
1043
  const warnedModels = new Set<string>();
1010
1044
  let cacheStatsByProvider: Partial<Record<CacheProviderId, CacheStats>> = emptyAllCacheStats();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-cache-optimizer",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Pi extension that improves provider-side KV/prompt cache hit rates (DeepSeek, OpenAI, Claude, Gemini) by reordering the system prompt, requesting long retention, and showing footer cache stats. Renamed from pi-deepseek-cache-optimizer.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -16,10 +16,12 @@
16
16
  "author": "freescheme",
17
17
  "license": "MIT",
18
18
  "files": [
19
- "extension.ts"
19
+ "index.ts"
20
20
  ],
21
21
  "pi": {
22
- "extensions": ["./extension.ts"],
22
+ "extensions": [
23
+ "./index.ts"
24
+ ],
23
25
  "image": "https://img.shields.io/badge/Pi-Cache%20Optimizer-4A90D9"
24
26
  },
25
27
  "peerDependencies": {
@@ -27,6 +29,6 @@
27
29
  },
28
30
  "repository": {
29
31
  "type": "git",
30
- "url": "git+https://github.com/jiangge/pi-deepseek-cache-optimizer.git"
32
+ "url": "git+https://github.com/jiangge/pi-cache-optimizer.git"
31
33
  }
32
34
  }