pi-cache-optimizer 2.4.4 → 2.4.5

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.
package/README.md CHANGED
@@ -62,7 +62,7 @@ This extension is pure Node.js — no shell exec, no native bindings, no platfor
62
62
  | Windows | Works through the bash shell Pi requires on Windows (Git Bash, Cygwin, MSYS2, or WSL). See Pi's [Windows setup](https://github.com/earendil-works/pi-coding-agent/blob/main/docs/windows.md). |
63
63
  | Termux / Android | Works inside Pi's Termux setup. |
64
64
 
65
- State files under `~/.pi/agent/` are resolved via Node's `os.homedir()`, so on Windows the path automatically expands to `C:\Users\<you>\.pi\agent\...`. All shell snippets in this README are bash, matching the shell Pi runs in on every supported platform; no PowerShell or `cmd.exe` translation is needed when commands are executed inside (or for) Pi.
65
+ State files under `~/.pi/agent/` are resolved via Node's `os.homedir()`, so on Windows the path automatically expands to `C:\Users\<you>\.pi\agent\...`. The extension's compat warnings, `/cache-optimizer doctor`, and `/cache-optimizer compat` show the platform-appropriate path automatically (`~/.pi/agent/models.json` on Linux/macOS, `%USERPROFILE%\.pi\agent\models.json` on Windows). All shell snippets in this README are bash, matching the shell Pi runs in on every supported platform; no PowerShell or `cmd.exe` translation is needed when commands are executed inside (or for) Pi.
66
66
 
67
67
  ## Quickstart
68
68
 
@@ -126,6 +126,64 @@ rm -f ~/.pi/agent/deepseek-cache-optimizer-stats.json
126
126
 
127
127
 
128
128
 
129
+ ## Adding an OpenAI-compatible proxy channel
130
+
131
+ When adding a third-party OpenAI-compatible proxy provider (e.g. `otokapi`, `cafecode`,
132
+ OpenRouter, etc.) to `~/.pi/agent/models.json`, the `compat` flags for cache optimization
133
+ are NOT required for the model to work — but they dramatically improve cache durability.
134
+
135
+ ### Minimal provider config template
136
+
137
+ ```jsonc
138
+ {
139
+ "providers": {
140
+ "your-provider-id": {
141
+ "api": "openai-completions", // or "openai-responses"
142
+ "baseUrl": "https://your-proxy.example.com/v1",
143
+ "apiKey": "your-api-key",
144
+ "models": {
145
+ "gpt-5.5": {
146
+ "id": "gpt-5.5",
147
+ "name": "GPT 5.5",
148
+ "contextWindowTokens": 128000,
149
+ "maxOutputTokens": 8192,
150
+ "thinking": {
151
+ // Use the thinking modes your proxy actually supports.
152
+ // Pi maps --thinking <level> to tokens via thinkingLevelMap.
153
+ // The template below keeps each level distinct — DO NOT
154
+ // map everything to "xhigh". Your proxy may not support
155
+ // all levels; remove unsupported ones or test each.
156
+ "thinkingLevelMap": {
157
+ "off": null,
158
+ "minimal": "minimal",
159
+ "low": "low",
160
+ "medium": "medium",
161
+ "high": "high",
162
+ "xhigh": "xhigh"
163
+ }
164
+ },
165
+ "compat": {
166
+ "supportsLongCacheRetention": true,
167
+ "sendSessionAffinityHeaders": true
168
+ }
169
+ }
170
+ }
171
+ }
172
+ }
173
+ }
174
+ ```
175
+
176
+ Key points:
177
+
178
+ - `thinkingLevelMap` keeps distinct levels. If your proxy does not support a particular
179
+ level (e.g. `minimal`), remove that entry or set to `null`. Do **not** collapse all
180
+ levels to `"xhigh"` — that defeats user control over reasoning effort.
181
+ - `compat` flags help Pi request longer cache retention and send session-affinity
182
+ headers for proxy-side cache locality. Only enable them if your proxy supports them.
183
+ - The extension detects model families by `id`/`name` strings, not by provider id,
184
+ base URL, or API type. Use recognizable model ids (e.g. `gpt-5.5`, `kimi-k2.5`) for
185
+ correct stats adapter selection.
186
+
129
187
  ## Footer cache stats
130
188
 
131
189
  The Pi footer displays stats for the **active model family** only, for example:
@@ -166,10 +224,18 @@ Reset behavior:
166
224
 
167
225
  For direct DeepSeek or DeepSeek-like OpenAI-compatible proxies, configure the provider or model `compat` like this:
168
226
 
169
- ```json
227
+ The `compat` block goes inside your provider object in `~/.pi/agent/models.json`, at
228
+ the same level as `baseUrl`, `api`, `apiKey`, and `models`:
229
+
230
+ ```jsonc
170
231
  {
171
232
  "providers": {
172
233
  "deepseek": {
234
+ "api": "openai-completions",
235
+ "baseUrl": "https://api.deepseek.com/v1",
236
+ "apiKey": "sk-...",
237
+ "models": { /* ... */ },
238
+ // 👇 compat goes here, NOT inside models
173
239
  "compat": {
174
240
  "thinkingFormat": "deepseek",
175
241
  "supportsLongCacheRetention": true,
@@ -191,6 +257,63 @@ For Claude/Anthropic models behind an OpenAI-compatible endpoint, the extension
191
257
 
192
258
  > Reminder: only enable session-affinity headers or cache-control compat when your endpoint or proxy supports them.
193
259
 
260
+ ## Diagnostic command
261
+
262
+ The extension registers a Pi command `/cache-optimizer` for interactive diagnosis.
263
+
264
+ ```
265
+ /cache-optimizer — interactive menu (or text help when no UI)
266
+ /cache-optimizer doctor — show provider, model, API, base URL, compat status
267
+ /cache-optimizer compat — show compat suggestion with edit instructions
268
+ ```
269
+
270
+ When run without arguments, `/cache-optimizer` shows an interactive selection menu
271
+ (Doctor / Compat / Cancel) when the Pi UI supports it (`ctx.ui.select`). In
272
+ non-interactive terminals, it falls back to text help with current model compat
273
+ status.
274
+
275
+ ### `/cache-optimizer doctor`
276
+
277
+ Displays the active model's provider, model id, name, API type, base URL, current
278
+ `compat` flags, and any missing cache/session-affinity flags. If flags are missing,
279
+ it also shows a copyable JSON snippet and the exact edit location.
280
+
281
+ When all compat flags are present and applicable (third-party `openai-completions`
282
+ proxy), the output shows `✅ Compat fully configured.` For models where the
283
+ compat check does not apply (official OpenAI, non-`openai-completions` APIs,
284
+ custom transports), it shows `ℹ️ Compat check not applicable for this model.`:
285
+
286
+ ```text
287
+ Provider: otokapi
288
+ Model: gpt-5.5
289
+ API: openai-completions
290
+ Base URL: https://otokapi.example.com/v1
291
+ Compat: {}
292
+ ⚠️ Missing compat flags: supportsLongCacheRetention, sendSessionAffinityHeaders
293
+ Edit ~/.pi/agent/models.json -> providers["otokapi"] -> compat (same level as baseUrl/api/apiKey/models):
294
+ {
295
+ "supportsLongCacheRetention": true,
296
+ "sendSessionAffinityHeaders": true
297
+ }
298
+ ```
299
+
300
+ ### `/cache-optimizer compat`
301
+
302
+ Shows only the compat suggestion for the active model, including file path,
303
+ provider path, and copyable JSON snippet. When no flags are missing, it shows
304
+ `✅ Compat fully configured.` if the model is an applicable third-party proxy,
305
+ or `ℹ️ Compat check not applicable for this model.` otherwise.
306
+
307
+ ### Security
308
+
309
+ The command reads only metadata exposed by Pi through `ctx.model`:
310
+ provider, id, name, api, baseUrl, compat. It does NOT read or expose:
311
+ - API keys or environment secrets
312
+ - Request/response payloads
313
+ - Prompts or model outputs
314
+ - HTTP headers
315
+ - Raw `~/.pi/agent/models.json` content
316
+
194
317
  ## How it works
195
318
 
196
319
  Provider caches are usually based on exact or near-exact prefix matching. Pi's system prompt contains stable content that is likely shared across sessions (tools, skills, guidelines) and dynamic content that changes frequently (git status, task context).
package/README.zh-CN.md CHANGED
@@ -65,7 +65,7 @@ Generic OpenAI-compatible 代理**不会**仅因为使用 OpenAI 形状 API 或
65
65
  | Windows | 通过 Pi 在 Windows 下要求的 bash shell 运行(Git Bash、Cygwin、MSYS2 或 WSL)。详见 Pi 的 [Windows setup](https://github.com/earendil-works/pi-coding-agent/blob/main/docs/windows.md)。 |
66
66
  | Termux / Android | 在 Pi 的 Termux 环境中可用。 |
67
67
 
68
- 状态文件 `~/.pi/agent/` 通过 Node 的 `os.homedir()` 解析,所以在 Windows 上会自动展开为 `C:\Users\<你>\.pi\agent\...`。本文档中所有 shell 命令均使用 bash 语法,与 Pi 在每个受支持平台下运行的 shell 一致;只要在 Pi 内(或为 Pi 而执行)运行,就**不需要**改写为 PowerShell 或 `cmd.exe` 形式。
68
+ 状态文件 `~/.pi/agent/` 通过 Node 的 `os.homedir()` 解析,所以在 Windows 上会自动展开为 `C:\Users\<你>\.pi\agent\...`。扩展的 compat 提醒、`/cache-optimizer doctor` 和 `/cache-optimizer compat` 会自动显示适合当前平台的路径(Linux/macOS 上显示 `~/.pi/agent/models.json`,Windows 上显示 `%USERPROFILE%\.pi\agent\models.json`)。本文档中所有 shell 命令均使用 bash 语法,与 Pi 在每个受支持平台下运行的 shell 一致;只要在 Pi 内(或为 Pi 而执行)运行,就**不需要**改写为 PowerShell 或 `cmd.exe` 形式。
69
69
 
70
70
  ## 快速开始
71
71
 
@@ -129,6 +129,56 @@ rm -f ~/.pi/agent/deepseek-cache-optimizer-stats.json
129
129
 
130
130
 
131
131
 
132
+ ## 添加 OpenAI-compatible 代理渠道
133
+
134
+ 当在 `~/.pi/agent/models.json` 中添加第三方 OpenAI-compatible 代理 provider(例如 `otokapi`、`cafecode`、OpenRouter 等)时,缓存优化的 `compat` 标志对模型正常使用不是必需的,但它们能显著提高缓存持久性。
135
+
136
+ ### 最小 provider 配置模板
137
+
138
+ ```jsonc
139
+ {
140
+ "providers": {
141
+ "your-provider-id": {
142
+ "api": "openai-completions", // 或 "openai-responses"
143
+ "baseUrl": "https://your-proxy.example.com/v1",
144
+ "apiKey": "your-api-key",
145
+ "models": {
146
+ "gpt-5.5": {
147
+ "id": "gpt-5.5",
148
+ "name": "GPT 5.5",
149
+ "contextWindowTokens": 128000,
150
+ "maxOutputTokens": 8192,
151
+ "thinking": {
152
+ // 使用你的代理实际支持的 thinking 级别。
153
+ // Pi 通过 thinkingLevelMap 将 --thinking <level> 映射为 token。
154
+ // 下面模板保持各级别独立 —— 不要全部映射为 "xhigh"。
155
+ // 你的代理可能不支持所有级别;移除不支持的或逐个测试。
156
+ "thinkingLevelMap": {
157
+ "off": null,
158
+ "minimal": "minimal",
159
+ "low": "low",
160
+ "medium": "medium",
161
+ "high": "high",
162
+ "xhigh": "xhigh"
163
+ }
164
+ },
165
+ "compat": {
166
+ "supportsLongCacheRetention": true,
167
+ "sendSessionAffinityHeaders": true
168
+ }
169
+ }
170
+ }
171
+ }
172
+ }
173
+ }
174
+ ```
175
+
176
+ 关键点:
177
+
178
+ - `thinkingLevelMap` 保持不同的 level 独立。如果你的代理不支持某个级别(例如 `minimal`),请移除该条目或设为 `null`。**不要**将所有级别都映射为 `"xhigh"` —— 那会破坏用户对推理努力度的控制。
179
+ - `compat` 标志帮助 Pi 请求更长的缓存保留时间,并通过发送 session-affinity headers 实现代理侧缓存本地性。仅在代理支持时才启用。
180
+ - 扩展通过模型 `id`/`name` 字符串来检测模型家族,而不是通过 provider id、base URL 或 API 类型。请使用易识别的模型 id(例如 `gpt-5.5`、`kimi-k2.5`),以便正确匹配统计 adapter。
181
+
132
182
  ## 底部缓存统计
133
183
 
134
184
  Pi footer 只显示**当前活跃模型 family** 的统计,例如:
@@ -167,12 +217,19 @@ Gemini cache 1/2 · 0.18M/0.50M tok (36%)
167
217
 
168
218
  ## 建议的 compat 配置
169
219
 
170
- 对直连 DeepSeek 或 DeepSeek-like OpenAI-compatible 代理,建议在对应 provider 或 model 的 `compat` 中配置:
220
+ 对直连 DeepSeek 或 DeepSeek-like OpenAI-compatible 代理,建议在对应 provider 或 model 的 `compat` 中配置。
221
+
222
+ `compat` 块应该放在 `~/.pi/agent/models.json` 中 provider 对象内部,与 `baseUrl`、`api`、`apiKey`、`models` 同级:
171
223
 
172
- ```json
224
+ ```jsonc
173
225
  {
174
226
  "providers": {
175
227
  "deepseek": {
228
+ "api": "openai-completions",
229
+ "baseUrl": "https://api.deepseek.com/v1",
230
+ "apiKey": "sk-...",
231
+ "models": { /* ... */ },
232
+ // 👇 compat 在此位置,而不是在 models 内部
176
233
  "compat": {
177
234
  "thinkingFormat": "deepseek",
178
235
  "supportsLongCacheRetention": true,
@@ -194,6 +251,51 @@ Gemini cache 1/2 · 0.18M/0.50M tok (36%)
194
251
 
195
252
  > 提醒:只有在 endpoint 或代理明确支持时,才建议启用 session-affinity headers 或 cache-control compat。
196
253
 
254
+ ## 诊断命令
255
+
256
+ 扩展注册了 Pi 命令 `/cache-optimizer` 用于交互式诊断。
257
+
258
+ ```
259
+ /cache-optimizer — 交互菜单(无 UI 时显示文字帮助)
260
+ /cache-optimizer doctor — 显示 provider、model、API、base URL、compat 状态
261
+ /cache-optimizer compat — 显示 compat 建议和编辑说明
262
+ ```
263
+
264
+ 不带参数时,当 Pi UI 支持时(`ctx.ui.select` 可用),`/cache-optimizer` 会显示交互选择菜单(Doctor / Compat / Cancel)。在非交互终端中,会回退到文字帮助和当前模型 compat 状态。
265
+
266
+ ### `/cache-optimizer doctor`
267
+
268
+ 显示当前模型的 provider、model id、名称、API 类型、base URL、当前 `compat` 标志以及缺少的缓存/session-affinity 标志。如果缺少标志,还会显示可复制的 JSON 片段和精确编辑位置。
269
+
270
+ 如果所有 compat 标志都已配置且适用(第三方 `openai-completions` 代理),输出显示 `✅ Compat fully configured.`。对于不适用 compat 检查的模型(官方 OpenAI、非 `openai-completions` API、custom transport),显示 `ℹ️ Compat check not applicable for this model.`:
271
+
272
+ ```text
273
+ Provider: otokapi
274
+ Model: gpt-5.5
275
+ API: openai-completions
276
+ Base URL: https://otokapi.example.com/v1
277
+ Compat: {}
278
+ ⚠️ Missing compat flags: supportsLongCacheRetention, sendSessionAffinityHeaders
279
+ Edit ~/.pi/agent/models.json -> providers["otokapi"] -> compat (same level as baseUrl/api/apiKey/models):
280
+ {
281
+ "supportsLongCacheRetention": true,
282
+ "sendSessionAffinityHeaders": true
283
+ }
284
+ ```
285
+
286
+ ### `/cache-optimizer compat`
287
+
288
+ 仅显示当前模型的 compat 建议,包括文件路径、provider 路径和可复制 JSON 片段。当没有缺失标志时,如果模型是适用的第三方代理则显示 `✅ Compat fully configured.`,否则显示 `ℹ️ Compat check not applicable for this model.`。
289
+
290
+ ### 安全说明
291
+
292
+ 命令只读取 Pi 通过 `ctx.model` 暴露的元数据:provider、id、name、api、baseUrl、compat。它**不会**读取或暴露:
293
+ - API key 或环境密钥
294
+ - 请求/响应 payload
295
+ - Prompt 或模型输出
296
+ - HTTP headers
297
+ - `~/.pi/agent/models.json` 的原始内容
298
+
197
299
  ## 原理
198
300
 
199
301
  Provider 缓存通常依赖精确或近似精确的前缀匹配。Pi 的 system prompt 包含跨会话稳定的内容(工具定义、技能、规范),也包含每次变化的动态内容(git status、当前任务)。
package/index.ts CHANGED
@@ -48,6 +48,16 @@ const NO_PROMPT_REWRITE_ENV = "PI_CACHE_OPTIMIZER_NO_PROMPT_REWRITE";
48
48
  // persisted metrics.
49
49
  let promptTruncationDetected = false;
50
50
 
51
+ // Timestamp (ms) of the most recent integrity truncation event.
52
+ // Used by /cache-optimizer doctor to surface recovery guidance.
53
+ // Reset to 0 on reload.
54
+ let lastPromptIntegrityWarningAt = 0;
55
+
56
+ /** Getter for lastPromptIntegrityWarningAt (exported for tests via __internals_for_tests). */
57
+ function getLastPromptIntegrityWarningAt(): number {
58
+ return lastPromptIntegrityWarningAt;
59
+ }
60
+
51
61
  // Minimum count of skills before compression is worth applying.
52
62
  // Below this, pi's verbose XML block is small enough that the overhead of
53
63
  // an additional one-line index isn't worth the loss of per-skill
@@ -548,6 +558,26 @@ function getCompat(model: PiModel | undefined): CacheCompat {
548
558
  return (model?.compat ?? {}) as CacheCompat;
549
559
  }
550
560
 
561
+ /**
562
+ * Return a platform-friendly display path for `~/.pi/agent/models.json`.
563
+ *
564
+ * On Windows (platform starts with "win") the path is shown as
565
+ * `%USERPROFILE%\.pi\agent\models.json` to match Windows conventions.
566
+ * On all other platforms (Linux, macOS, etc.) it is shown as
567
+ * `~/.pi/agent/models.json` (the Unix-style tilde shorthand).
568
+ *
569
+ * This is a DISPLAY helper only. Actual path resolution is done by Pi
570
+ * (via Node `os.homedir()` + path.join), and this string is never used
571
+ * for I/O — only for warning/doctor/README text so that users on any
572
+ * platform see a copyable path they recognize.
573
+ */
574
+ function getModelsJsonDisplayPath(platform: string = process.platform): string {
575
+ if (platform.startsWith("win")) {
576
+ return `%USERPROFILE%\\.pi\\agent\\models.json`;
577
+ }
578
+ return "~/.pi/agent/models.json";
579
+ }
580
+
551
581
  function isEnabledEnv(value: string | undefined): boolean {
552
582
  if (!value) return false;
553
583
  const normalized = value.trim().toLowerCase();
@@ -994,9 +1024,15 @@ function buildOpenAIProxyCompatWarningText(key: string, missing: string[]): stri
994
1024
  suggestion[flag] = true;
995
1025
  }
996
1026
 
1027
+ // Extract provider id from the model key (e.g. "otokapi/gpt-5.5" -> "otokapi").
1028
+ // If no slash is found, fall back to the key itself.
1029
+ const slashIdx = key.indexOf("/");
1030
+ const providerLabel = slashIdx > 0 ? key.slice(0, slashIdx) : key;
1031
+
1032
+ const modelsJsonPath = getModelsJsonDisplayPath();
997
1033
  const lines: string[] = [
998
1034
  `💡 pi-cache-optimizer: ${key} is a third-party GPT/OpenAI-compatible proxy but merged compat lacks ${missing.join(" and ")}.`,
999
- `Add under the model's compat in ~/.pi/agent/models.json (only if the endpoint supports them):`,
1035
+ `Edit ${modelsJsonPath} -> providers["${providerLabel}"] -> compat (at the same level as baseUrl/api/apiKey/models):`,
1000
1036
  ``,
1001
1037
  JSON.stringify(suggestion, null, 2),
1002
1038
  ``,
@@ -1050,9 +1086,12 @@ const CACHE_PROVIDER_ADAPTERS: CacheProviderAdapter[] = [
1050
1086
  if (missing.length === 0) return undefined;
1051
1087
 
1052
1088
  const key = modelKey(model);
1089
+ const slashIdx = key.indexOf("/");
1090
+ const providerLabel = slashIdx > 0 ? key.slice(0, slashIdx) : key;
1091
+ const modelsJsonPath = getModelsJsonDisplayPath();
1053
1092
  return (
1054
1093
  `💡 pi-cache-optimizer: ${key} is DeepSeek-like but merged compat lacks ${missing.join(" and ")}. ` +
1055
- "Proxies may reduce or hide cache hits; add these compat flags in ~/.pi/agent/models.json when the endpoint supports them."
1094
+ `Proxies may reduce or hide cache hits. Edit ${modelsJsonPath} -> providers["${providerLabel}"] -> compat (at the same level as baseUrl/api/apiKey/models).`
1056
1095
  );
1057
1096
  },
1058
1097
  },
@@ -1519,6 +1558,76 @@ async function writePersistedCacheStats(state: CacheStatsState): Promise<void> {
1519
1558
 
1520
1559
 
1521
1560
 
1561
+ function isCompatCheckApplicable(model: PiModel): boolean {
1562
+ return lower(model.api) === "openai-completions" && !isOfficialOpenAIBaseUrl(model);
1563
+ }
1564
+
1565
+ function buildDoctorDiagnosis(model: PiModel): string {
1566
+ const lines: string[] = [];
1567
+ lines.push(`Provider: ${model.provider}`);
1568
+ lines.push(`Model: ${model.id}`);
1569
+ if (model.name && model.name !== model.id) lines.push(`Name: ${model.name}`);
1570
+ lines.push(`API: ${model.api}`);
1571
+ lines.push(`Base URL: ${model.baseUrl || "(default)"}`);
1572
+
1573
+ const compat = getCompat(model);
1574
+ lines.push(`Compat: ${JSON.stringify(compat)}`);
1575
+
1576
+ const missing = describeMissingOpenAICompatibleProxyCompat(model);
1577
+ if (missing.length > 0) {
1578
+ lines.push(`⚠️ Missing compat flags: ${missing.join(", ")}`);
1579
+ const key = modelKey(model);
1580
+ const slashIdx = key.indexOf("/");
1581
+ const providerLabel = slashIdx > 0 ? key.slice(0, slashIdx) : key;
1582
+ const suggestion = Object.fromEntries(missing.map((f) => [f, true]));
1583
+ const modelsJsonPath = getModelsJsonDisplayPath();
1584
+ lines.push(`Edit ${modelsJsonPath} -> providers["${providerLabel}"] -> compat (same level as baseUrl/api/apiKey/models):`);
1585
+ lines.push(JSON.stringify(suggestion, null, 2));
1586
+ } else if (isCompatCheckApplicable(model)) {
1587
+ lines.push("✅ Compat fully configured.");
1588
+ } else {
1589
+ lines.push("ℹ️ Compat check not applicable for this model.");
1590
+ }
1591
+
1592
+ // ── Integrity diagnostics ──
1593
+ if (lastPromptIntegrityWarningAt > 0) {
1594
+ const ago = Date.now() - lastPromptIntegrityWarningAt;
1595
+ const mins = Math.floor(ago / 60000);
1596
+ if (mins < 5) {
1597
+ lines.push("");
1598
+ lines.push("⚠️ Recent prompt integrity issue detected:");
1599
+ lines.push(` Last detected ${mins > 0 ? `${mins} min` : `${Math.floor(ago / 1000)}s`} ago. The prompt reorder was`);
1600
+ lines.push(` skipped on that turn to preserve structural markers.`);
1601
+ lines.push(` Common causes: extension system prompt format change, substring collision.`);
1602
+ lines.push(` Steps:`);
1603
+ lines.push(` 1. Run /reload to reset (may clear transient issues).`);
1604
+ lines.push(` 2. Set PI_CACHE_OPTIMIZER_NO_PROMPT_REWRITE=1 & /reload to disable reorder.`);
1605
+ lines.push(` 3. If persistent, file an issue with this doctor output.`);
1606
+ }
1607
+ }
1608
+
1609
+ return lines.join("\n");
1610
+ }
1611
+
1612
+ function buildCompatDiagnosis(model: PiModel): string | undefined {
1613
+ const missing = describeMissingOpenAICompatibleProxyCompat(model);
1614
+ if (missing.length === 0) return undefined;
1615
+
1616
+ const key = modelKey(model);
1617
+ const slashIdx = key.indexOf("/");
1618
+ const providerLabel = slashIdx > 0 ? key.slice(0, slashIdx) : key;
1619
+ const suggestion = Object.fromEntries(missing.map((f) => [f, true]));
1620
+ const modelsJsonPath = getModelsJsonDisplayPath();
1621
+ return (
1622
+ `Active model: ${key}\n` +
1623
+ `Missing: ${missing.join(", ")}\n\n` +
1624
+ `Edit ${modelsJsonPath} -> providers["${providerLabel}"] -> compat` +
1625
+ ` (at the same level as baseUrl/api/apiKey/models) and add:\n` +
1626
+ `${JSON.stringify(suggestion, null, 2)}\n\n` +
1627
+ `Only enable if your endpoint supports them.`
1628
+ );
1629
+ }
1630
+
1522
1631
  // Internal helpers exported only so the task verification script
1523
1632
  // (.trellis/tasks/.../verify.ts) can exercise them. They are not part of the
1524
1633
  // extension's public API; pi only invokes the default export below.
@@ -1576,6 +1685,14 @@ export const __internals_for_tests = {
1576
1685
  getAssistantMessageModelTokenValues,
1577
1686
  getCompat,
1578
1687
  modelKey,
1688
+ // Platform-friendly path helper
1689
+ getModelsJsonDisplayPath,
1690
+ // Integrity diagnostics
1691
+ getLastPromptIntegrityWarningAt,
1692
+ // Diagnostic command helpers
1693
+ isCompatCheckApplicable,
1694
+ buildDoctorDiagnosis,
1695
+ buildCompatDiagnosis,
1579
1696
  // Cache stats helpers (module-level, usable from verify script)
1580
1697
  addUsageToCacheStats,
1581
1698
  formatCacheStats,
@@ -1592,8 +1709,10 @@ export default function (pi: ExtensionAPI) {
1592
1709
  let lastStatusText: string | undefined;
1593
1710
  let persistenceWarningShown = false;
1594
1711
  let persistTimer: ReturnType<typeof setTimeout> | null = null;
1712
+ let integrityNotificationShown = false;
1595
1713
  const PERSIST_DEBOUNCE_MS = 2000;
1596
1714
 
1715
+
1597
1716
  function getCacheStatsState(): CacheStatsState {
1598
1717
  return { statsByModel: cacheStatsByModel, legacyFamily: cacheStatsLegacyFamily };
1599
1718
  }
@@ -1698,6 +1817,9 @@ export default function (pi: ExtensionAPI) {
1698
1817
  cacheStatsByModel = {};
1699
1818
  cacheStatsLegacyFamily = emptyAllCacheStats();
1700
1819
  lastStatusText = undefined;
1820
+ // Reset integrity diagnostics on reload
1821
+ lastPromptIntegrityWarningAt = 0;
1822
+ integrityNotificationShown = false;
1701
1823
  await flushPersistCacheStats(ctx);
1702
1824
  return;
1703
1825
  }
@@ -1737,6 +1859,35 @@ export default function (pi: ExtensionAPI) {
1737
1859
  if (promptTruncationDetected && statusText !== undefined) {
1738
1860
  statusText = statusText + " ⚠️ integrity";
1739
1861
  promptTruncationDetected = false;
1862
+ lastPromptIntegrityWarningAt = Date.now();
1863
+
1864
+ // One-time notification with recovery steps (per session).
1865
+ if (!integrityNotificationShown) {
1866
+ integrityNotificationShown = true;
1867
+ ctx.ui.notify(
1868
+ `⚠️ ${LOG_PREFIX}: A prompt structural marker was lost during reorder on this turn. ` +
1869
+ `The original prompt was used instead to preserve integrity.\n\n` +
1870
+ `Recovery steps:\n` +
1871
+ `1. Run /reload to reset (may clear transient issues).\n` +
1872
+ `2. Set PI_CACHE_OPTIMIZER_NO_PROMPT_REWRITE=1 and /reload to disable reorder.\n` +
1873
+ `3. If persistent, run /cache-optimizer doctor and file an issue (no API keys/prompts).`,
1874
+ "warning",
1875
+ );
1876
+ }
1877
+ }
1878
+
1879
+ // ⚠️ compat footer marker: if the active model is a non-official
1880
+ // openai-completions model with missing supportsLongCacheRetention
1881
+ // or sendSessionAffinityHeaders, append the marker to indicate that
1882
+ // compat configuration is incomplete. Re-evaluated on every status
1883
+ // update so the marker persists through stats changes and day
1884
+ // rollovers. Redundant setStatus calls are blocked by the
1885
+ // `lastStatusText` early return above.
1886
+ if (statusText !== undefined && model) {
1887
+ const compatMissing = describeMissingOpenAICompatibleProxyCompat(model);
1888
+ if (compatMissing.length > 0) {
1889
+ statusText = statusText + " ⚠️ compat";
1890
+ }
1740
1891
  }
1741
1892
 
1742
1893
  if (statusText === lastStatusText) return;
@@ -1868,4 +2019,99 @@ export default function (pi: ExtensionAPI) {
1868
2019
  schedulePersistCacheStats(ctx);
1869
2020
  await publishStatus(ctx);
1870
2021
  });
2022
+
2023
+ // ────────────────────────────────────────────────────────────────
2024
+ // Register /cache-optimizer command
2025
+ // Subcommands:
2026
+ // doctor — show current model/provider/api/baseUrl/compat status
2027
+ // compat — show compat suggestion with file path
2028
+ // (no args) — show help summary + current diagnosis
2029
+ // ────────────────────────────────────────────────────────────────
2030
+ pi.registerCommand("cache-optimizer", {
2031
+ description: "Diagnose Pi cache configuration",
2032
+ handler: async (args: string, cmdCtx) => {
2033
+ const model = cmdCtx.model;
2034
+ const subcommand = args.trim().toLowerCase().split(/\s+/)[0] || "help";
2035
+
2036
+ if (subcommand === "doctor") {
2037
+ if (!model) {
2038
+ cmdCtx.ui.notify("No active model selected. Select a model first with /model or pi --model.", "warning");
2039
+ return;
2040
+ }
2041
+ cmdCtx.ui.notify(buildDoctorDiagnosis(model), "info");
2042
+ } else if (subcommand === "compat") {
2043
+ if (!model) {
2044
+ cmdCtx.ui.notify("No active model selected. Select a model first with /model or pi --model.", "warning");
2045
+ return;
2046
+ }
2047
+ const compatResult = buildCompatDiagnosis(model);
2048
+ if (compatResult) {
2049
+ cmdCtx.ui.notify(compatResult, "warning");
2050
+ } else {
2051
+ cmdCtx.ui.notify(
2052
+ isCompatCheckApplicable(model)
2053
+ ? "✅ Compat fully configured."
2054
+ : "ℹ️ Compat check not applicable for this model.",
2055
+ "info",
2056
+ );
2057
+ }
2058
+ } else {
2059
+ // Try interactive selection menu when UI supports it
2060
+ if (cmdCtx.hasUI) {
2061
+ const menuOptions = [
2062
+ "🩺 Doctor — Show current model cache configuration",
2063
+ "⚙️ Compat — Show compat suggestion with edit instructions",
2064
+ "❌ Cancel",
2065
+ ];
2066
+ const choice = await cmdCtx.ui.select("Cache Optimizer", menuOptions);
2067
+ if (choice === menuOptions[0]) {
2068
+ if (!model) {
2069
+ cmdCtx.ui.notify("No active model selected. Select a model first with /model or pi --model.", "warning");
2070
+ } else {
2071
+ cmdCtx.ui.notify(buildDoctorDiagnosis(model), "info");
2072
+ }
2073
+ } else if (choice === menuOptions[1]) {
2074
+ if (!model) {
2075
+ cmdCtx.ui.notify("No active model selected. Select a model first with /model or pi --model.", "warning");
2076
+ } else {
2077
+ const compatResult = buildCompatDiagnosis(model);
2078
+ if (compatResult) {
2079
+ cmdCtx.ui.notify(compatResult, "warning");
2080
+ } else {
2081
+ cmdCtx.ui.notify(
2082
+ isCompatCheckApplicable(model)
2083
+ ? "✅ Compat fully configured."
2084
+ : "ℹ️ Compat check not applicable for this model.",
2085
+ "info",
2086
+ );
2087
+ }
2088
+ }
2089
+ }
2090
+ // choice === "cancel" or undefined → no action
2091
+ return;
2092
+ }
2093
+
2094
+ // Fallback: text help when no interactive UI
2095
+ const diagnosis: string[] = [];
2096
+ diagnosis.push("📋 /cache-optimizer commands:");
2097
+ diagnosis.push(" doctor — Show current model/provider/api/baseUrl/compat status");
2098
+ diagnosis.push(" compat — Show compat suggestion with edit location");
2099
+ diagnosis.push("");
2100
+ if (model) {
2101
+ const missing = describeMissingOpenAICompatibleProxyCompat(model);
2102
+ if (missing.length > 0) {
2103
+ diagnosis.push(`⚠️ Active model "${modelKey(model)}" missing compat: ${missing.join(", ")}`);
2104
+ diagnosis.push('Run "/cache-optimizer compat" for edit instructions.');
2105
+ } else if (isCompatCheckApplicable(model)) {
2106
+ diagnosis.push(`✅ Active model "${modelKey(model)}": compat fully configured.`);
2107
+ } else {
2108
+ diagnosis.push(`ℹ️ Active model "${modelKey(model)}": compat check not applicable.`);
2109
+ }
2110
+ } else {
2111
+ diagnosis.push("No active model selected.");
2112
+ }
2113
+ cmdCtx.ui.notify(diagnosis.join("\n"), "info");
2114
+ }
2115
+ },
2116
+ });
1871
2117
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-cache-optimizer",
3
- "version": "2.4.4",
3
+ "version": "2.4.5",
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",