pi-cache-optimizer 2.9.2 → 2.10.0
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 +18 -0
- package/README.zh-CN.md +18 -0
- package/index.ts +528 -99
- package/package.json +1 -1
- package/src/command-completion.ts +3 -2
- package/src/command.ts +2 -2
- package/src/config.ts +47 -3
- package/src/stats-archive.ts +301 -79
- package/src/stats-fs.ts +54 -0
- package/src/stats-refresh.ts +474 -0
- package/src/stats-store.ts +134 -21
package/package.json
CHANGED
|
@@ -18,9 +18,9 @@ export const CACHE_OPTIMIZER_COMMANDS = [
|
|
|
18
18
|
"rollback",
|
|
19
19
|
] as const;
|
|
20
20
|
|
|
21
|
-
export const CACHE_OPTIMIZER_CONFIG_ARGUMENTS = ["footer-mode", "prompt-rewrite", "virtual-rewrite", "skill-compression", "openai-cache-key", "tool-order", "zero-price-warming", "reset"] as const;
|
|
21
|
+
export const CACHE_OPTIMIZER_CONFIG_ARGUMENTS = ["footer-mode", "prompt-rewrite", "virtual-rewrite", "skill-compression", "openai-cache-key", "tool-order", "zero-price-warming", "stats-incremental", "reset"] as const;
|
|
22
22
|
|
|
23
|
-
export const CACHE_OPTIMIZER_FEATURE_COMMANDS = ["prompt-rewrite", "virtual-rewrite", "skill-compression", "openai-cache-key", "tool-order", "zero-price-warming"] as const;
|
|
23
|
+
export const CACHE_OPTIMIZER_FEATURE_COMMANDS = ["prompt-rewrite", "virtual-rewrite", "skill-compression", "openai-cache-key", "tool-order", "zero-price-warming", "stats-incremental"] as const;
|
|
24
24
|
|
|
25
25
|
export const CACHE_OPTIMIZER_FEATURE_VALUES = ["on", "off"] as const;
|
|
26
26
|
|
|
@@ -33,6 +33,7 @@ export const FEATURE_COMMAND_MAP: Record<string, PersistedCacheOptimizerFeature>
|
|
|
33
33
|
"openai-cache-key": "openAICacheKey",
|
|
34
34
|
"tool-order": "toolOrder",
|
|
35
35
|
"zero-price-warming": "zeroPriceWarming",
|
|
36
|
+
"stats-incremental": "statsIncremental",
|
|
36
37
|
};
|
|
37
38
|
|
|
38
39
|
export const CACHE_OPTIMIZER_STATS_ARGUMENTS = ["all", "contributors"] as const;
|
package/src/command.ts
CHANGED
|
@@ -173,7 +173,7 @@ export function createCacheOptimizerCommandHandler(runtime: CommandRuntime) {
|
|
|
173
173
|
const resolved = resolveFooterStatsMode(persistedFooterStatsMode);
|
|
174
174
|
cmdCtx.ui.notify(
|
|
175
175
|
`Usage: /cache-optimizer config footer-mode total|session|process\n` +
|
|
176
|
-
` /cache-optimizer config prompt-rewrite|virtual-rewrite|skill-compression|openai-cache-key|tool-order|zero-price-warming on|off\n` +
|
|
176
|
+
` /cache-optimizer config prompt-rewrite|virtual-rewrite|skill-compression|openai-cache-key|tool-order|zero-price-warming|stats-incremental on|off\n` +
|
|
177
177
|
` /cache-optimizer config reset\n` +
|
|
178
178
|
`Current footer mode: ${resolved.mode} (${resolved.source})`,
|
|
179
179
|
"info",
|
|
@@ -1007,7 +1007,7 @@ export function createCacheOptimizerCommandHandler(runtime: CommandRuntime) {
|
|
|
1007
1007
|
diagnosis.push(" stats all — Show detailed totals for every model across all local sessions");
|
|
1008
1008
|
diagnosis.push(" stats contributors — Show per-session contributors for the active model");
|
|
1009
1009
|
diagnosis.push(" compat — Show compat suggestion with edit location");
|
|
1010
|
-
diagnosis.push(" config prompt-rewrite|virtual-rewrite|skill-compression|openai-cache-key|tool-order|zero-price-warming on|off — Persist feature settings");
|
|
1010
|
+
diagnosis.push(" config prompt-rewrite|virtual-rewrite|skill-compression|openai-cache-key|tool-order|zero-price-warming|stats-incremental on|off — Persist feature settings");
|
|
1011
1011
|
diagnosis.push(" config footer-mode total|session|process — Persist the footer stats mode");
|
|
1012
1012
|
diagnosis.push(" config reset — Remove persistent feature overrides");
|
|
1013
1013
|
diagnosis.push(" fix — Auto-fix compat issues (writes models.json or extension config, requires UI)");
|
package/src/config.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { STATE_DIR } from "./paths.ts";
|
|
|
4
4
|
import { PI_CACHE_RETENTION_ENV, STARTUP_CACHE_RETENTION_ENV, requestLongCacheRetention, restoreCacheRetentionEnv } from "./retention.ts";
|
|
5
5
|
import { type Stats, readFileSync } from "node:fs";
|
|
6
6
|
import { lstat, mkdir, readFile } from "node:fs/promises";
|
|
7
|
-
import { dirname, join } from "node:path";
|
|
7
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
export const CONFIG_FILE_PATH = join(STATE_DIR, "pi-cache-optimizer-config.json");
|
|
@@ -19,6 +19,19 @@ export const NO_ZERO_PRICE_WARMING_ENV = "PI_CACHE_OPTIMIZER_NO_ZERO_PRICE_WARMI
|
|
|
19
19
|
|
|
20
20
|
export const FOOTER_MODE_ENV = "PI_CACHE_OPTIMIZER_FOOTER_MODE";
|
|
21
21
|
|
|
22
|
+
/** Kill switch for the incremental footer refresh (2.10): 0/false/off/no restores the 2.9.3 full scan per refresh. */
|
|
23
|
+
export const STATS_INCREMENTAL_ENV = "PI_CACHE_OPTIMIZER_STATS_INCREMENTAL";
|
|
24
|
+
|
|
25
|
+
/** Opt-in check (1/true/on/yes) that the footer's shard cache never misses a change the file identity hides (see TieredShardIndex.verifyCachedContents). */
|
|
26
|
+
export const STATS_VERIFY_ENV = "PI_CACHE_OPTIMIZER_STATS_VERIFY";
|
|
27
|
+
|
|
28
|
+
export function isStatsVerifyEnabled(env: MutableEnv = process.env): boolean {
|
|
29
|
+
return isEnabledEnv(env[STATS_VERIFY_ENV]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Opt-in footer refresh timing log: 1/true/on/yes writes JSON lines to STATS_TIMING_LOG_PATH; an absolute path writes there. */
|
|
33
|
+
export const STATS_TIMING_ENV = "PI_CACHE_OPTIMIZER_STATS_TIMING";
|
|
34
|
+
|
|
22
35
|
export type FooterStatsMode = "session" | "total" | "process";
|
|
23
36
|
|
|
24
37
|
export type FooterStatsModeSource = "config" | "env" | "default";
|
|
@@ -28,7 +41,7 @@ export type PersistedCacheOptimizerConfigV1 = {
|
|
|
28
41
|
footerMode?: FooterStatsMode;
|
|
29
42
|
};
|
|
30
43
|
|
|
31
|
-
export type PersistedCacheOptimizerFeature = "promptRewrite" | "virtualRewrite" | "skillCompression" | "openAICacheKey" | "toolOrder" | "zeroPriceWarming";
|
|
44
|
+
export type PersistedCacheOptimizerFeature = "promptRewrite" | "virtualRewrite" | "skillCompression" | "openAICacheKey" | "toolOrder" | "zeroPriceWarming" | "statsIncremental";
|
|
32
45
|
|
|
33
46
|
export type PersistedCacheOptimizerConfigV2 = {
|
|
34
47
|
version: 2;
|
|
@@ -71,6 +84,7 @@ export function featureEnabled(
|
|
|
71
84
|
if (feature === "promptRewrite" || feature === "skillCompression" || feature === "zeroPriceWarming") {
|
|
72
85
|
return !isEnabledEnv(env[envName]);
|
|
73
86
|
}
|
|
87
|
+
if (feature === "statsIncremental") return !isDisabledEnv(env[envName]);
|
|
74
88
|
return isEnabledEnv(env[envName]) || defaultValue;
|
|
75
89
|
}
|
|
76
90
|
|
|
@@ -79,6 +93,35 @@ export function isZeroPriceWarmingEnabled(): boolean {
|
|
|
79
93
|
return runtimeOptimizerEnabled && featureEnabled("zeroPriceWarming", NO_ZERO_PRICE_WARMING_ENV, true);
|
|
80
94
|
}
|
|
81
95
|
|
|
96
|
+
/**
|
|
97
|
+
* Incremental footer refresh (tiered shard index). Unlike the optimizer
|
|
98
|
+
* features it does not follow /cache-optimizer disable: it changes how stats
|
|
99
|
+
* are read, not what is sent to providers.
|
|
100
|
+
*
|
|
101
|
+
* Kill switch: `/cache-optimizer config stats-incremental off` (persisted,
|
|
102
|
+
* wins over the environment like every feature setting) or
|
|
103
|
+
* PI_CACHE_OPTIMIZER_STATS_INCREMENTAL=0. Note: versions before 2.10 reject a
|
|
104
|
+
* config file with a feature key they do not know, so while this key is
|
|
105
|
+
* persisted an older Pi sharing the agent directory falls back to its
|
|
106
|
+
* defaults; the README says so.
|
|
107
|
+
*/
|
|
108
|
+
export function isStatsIncrementalEnabled(
|
|
109
|
+
env: MutableEnv = process.env,
|
|
110
|
+
config: PersistedCacheOptimizerConfigV3 = persistedCacheOptimizerConfig,
|
|
111
|
+
): boolean {
|
|
112
|
+
return featureEnabled("statsIncremental", STATS_INCREMENTAL_ENV, true, env, config);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export const STATS_TIMING_LOG_PATH = join(STATE_DIR, "pi-cache-optimizer-stats-timing.jsonl");
|
|
116
|
+
|
|
117
|
+
/** Where the opt-in refresh timing log goes, or undefined when it is off. */
|
|
118
|
+
export function statsTimingLogPath(env: MutableEnv = process.env): string | undefined {
|
|
119
|
+
const value = env[STATS_TIMING_ENV]?.trim();
|
|
120
|
+
if (!value || isDisabledEnv(value)) return undefined;
|
|
121
|
+
if (isEnabledEnv(value)) return STATS_TIMING_LOG_PATH;
|
|
122
|
+
return isAbsolute(value) ? value : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
82
125
|
export function isToolOrderEnabled(env: MutableEnv = process.env): boolean {
|
|
83
126
|
return runtimeOptimizerEnabled && featureEnabled("toolOrder", TOOL_ORDER_ENV, false);
|
|
84
127
|
}
|
|
@@ -101,7 +144,7 @@ export function parsePersistedCacheOptimizerConfig(value: unknown): PersistedCac
|
|
|
101
144
|
const rawFeatures = record.version === 3 ? record.features : undefined;
|
|
102
145
|
if (rawFeatures !== undefined && !asRecord(rawFeatures)) return undefined;
|
|
103
146
|
const featuresRecord = asRecord(rawFeatures);
|
|
104
|
-
const featureNames = new Set<PersistedCacheOptimizerFeature>(["promptRewrite", "virtualRewrite", "skillCompression", "openAICacheKey", "toolOrder", "zeroPriceWarming"]);
|
|
147
|
+
const featureNames = new Set<PersistedCacheOptimizerFeature>(["promptRewrite", "virtualRewrite", "skillCompression", "openAICacheKey", "toolOrder", "zeroPriceWarming", "statsIncremental"]);
|
|
105
148
|
if (featuresRecord && Object.keys(featuresRecord).some((key) => !featureNames.has(key as PersistedCacheOptimizerFeature) || typeof featuresRecord[key] !== "boolean")) return undefined;
|
|
106
149
|
|
|
107
150
|
const rawPromptCacheKey = record.promptCacheKey;
|
|
@@ -362,6 +405,7 @@ export function formatPersistentFeatureConfig(): string {
|
|
|
362
405
|
["OpenAI cache key", "openAICacheKey", NO_OPENAI_CACHE_KEY_ENV, true],
|
|
363
406
|
["Tool ordering", "toolOrder", TOOL_ORDER_ENV, false],
|
|
364
407
|
["Zero-price cache warming", "zeroPriceWarming", NO_ZERO_PRICE_WARMING_ENV, true],
|
|
408
|
+
["Incremental footer refresh", "statsIncremental", STATS_INCREMENTAL_ENV, true],
|
|
365
409
|
];
|
|
366
410
|
for (const [label, feature, envName, defaultValue] of entries) {
|
|
367
411
|
const hasEnv = process.env[envName] !== undefined || (feature === "openAICacheKey" && process.env[OPENAI_CACHE_KEY_ENV] !== undefined);
|