pi-cache-optimizer 2.10.7 → 2.10.9
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/CHANGELOG.md +13 -0
- package/index.ts +55 -13
- package/package.json +1 -1
- package/src/routing.ts +9 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,19 @@ last version Pi recorded for this package under "Extension Updates". Pi records
|
|
|
5
5
|
it sees this file, so notes start with the update after that.
|
|
6
6
|
Older history is in the [git log](https://github.com/jiangge/pi-cache-optimizer/commits/master).
|
|
7
7
|
|
|
8
|
+
## [2.10.9] - 2026-11-26
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- Memory: the extension no longer keeps an ended session in memory (its extension instance and its last session context). Before, Pi kept every session ended by `/new`, `/resume` or `/fork` until `/reload`, and SDK hosts running several sessions kept every finished one. Thanks @o7w7o (#33).
|
|
13
|
+
- When one extension instance serves a second session after a shutdown, it publishes its prompt-cache hints for other extensions again.
|
|
14
|
+
|
|
15
|
+
## [2.10.8] - 2026-11-22
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
|
|
19
|
+
- Native virtual models: when several credentialed providers share the routed model id and only some set `compat.promptCacheKeyMode: "disabled"`, the extension no longer adds a `prompt_cache_key` depending on catalog order; such requests are left unchanged. Providers whose mode is `"enabled"`, `"auto"` or unset are still treated alike. Thanks @qsgy-edge (#32).
|
|
20
|
+
|
|
8
21
|
## [2.10.7] - 2026-11-21
|
|
9
22
|
|
|
10
23
|
### Fixed
|
package/index.ts
CHANGED
|
@@ -112,8 +112,15 @@ function notifyCacheCompatIfNeeded(
|
|
|
112
112
|
// this module. Tests await it after session_start; runtime code uses the
|
|
113
113
|
// per-instance promise instead.
|
|
114
114
|
let latestStatsRestoreForTests: Promise<void> = Promise.resolve();
|
|
115
|
-
/**
|
|
116
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Per extension instance in this module: resolves once no footer refresh (or
|
|
117
|
+
* its publish) is in flight. Held weakly: Pi can call one loaded module's
|
|
118
|
+
* factory again (its factory cache, SDK hosts), and a strong reference here
|
|
119
|
+
* would keep every instance (and its last ctx) alive for the module's
|
|
120
|
+
* lifetime, including one that never saw session_shutdown. The instance's
|
|
121
|
+
* own handlers keep its drain function alive while the instance is.
|
|
122
|
+
*/
|
|
123
|
+
const statsRefreshSettledForTests = new Set<WeakRef<() => Promise<void>>>();
|
|
117
124
|
|
|
118
125
|
export const __internals_for_tests = {
|
|
119
126
|
parseGlobalPiSettingsText: (text: string): unknown => parseGlobalPiSettingsText(text),
|
|
@@ -125,7 +132,11 @@ export const __internals_for_tests = {
|
|
|
125
132
|
* asserting on what such a refresh publishes.
|
|
126
133
|
*/
|
|
127
134
|
statsRefreshSettled: async (): Promise<void> => {
|
|
128
|
-
for (const
|
|
135
|
+
for (const ref of Array.from(statsRefreshSettledForTests)) {
|
|
136
|
+
const settled = ref.deref();
|
|
137
|
+
if (settled) await settled();
|
|
138
|
+
else statsRefreshSettledForTests.delete(ref);
|
|
139
|
+
}
|
|
129
140
|
},
|
|
130
141
|
setStatsRefreshTestHooks,
|
|
131
142
|
setStatsReadFsForTests,
|
|
@@ -626,7 +637,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
626
637
|
}
|
|
627
638
|
}
|
|
628
639
|
|
|
629
|
-
const
|
|
640
|
+
const cacheHintsService = markOptimizerOwnedCacheHintsService({
|
|
630
641
|
version: 1,
|
|
631
642
|
getHints(input: PiCacheHintsInput): PiCacheHintsOutput | undefined {
|
|
632
643
|
if (!runtimeOptimizerEnabled || !featureEnabled("promptRewrite", NO_PROMPT_REWRITE_ENV, true)) return undefined;
|
|
@@ -645,7 +656,12 @@ export default function (pi: ExtensionAPI) {
|
|
|
645
656
|
cacheRetention: hint.cacheRetention,
|
|
646
657
|
};
|
|
647
658
|
},
|
|
648
|
-
})
|
|
659
|
+
});
|
|
660
|
+
/** Undefined once session_shutdown took the service out; the next session_start puts it back. */
|
|
661
|
+
let uninstallCacheHintsService: (() => void) | undefined = installCacheHints();
|
|
662
|
+
function installCacheHints(): () => void {
|
|
663
|
+
return installCacheHintsService(cacheHintsService, { discardPrevious: isOptimizerOwnedCacheHintsService });
|
|
664
|
+
}
|
|
649
665
|
|
|
650
666
|
/**
|
|
651
667
|
* Build a session-scoped stats key from the current session hash + model key.
|
|
@@ -1202,7 +1218,12 @@ export default function (pi: ExtensionAPI) {
|
|
|
1202
1218
|
if (backgroundStatsTasks.size === 0 && refreshScheduler.idle && !shardIndex.fullScanInFlight) return;
|
|
1203
1219
|
}
|
|
1204
1220
|
}
|
|
1205
|
-
|
|
1221
|
+
// drainStatsRefresh stays strongly reachable while this instance is: the
|
|
1222
|
+
// session_shutdown handler calls it, so it lives in the factory's shared
|
|
1223
|
+
// closure context. Keep it referenced from a handler if that call moves.
|
|
1224
|
+
const drainStatsRefreshRef = new WeakRef(drainStatsRefresh);
|
|
1225
|
+
for (const ref of statsRefreshSettledForTests) if (ref.deref() === undefined) statsRefreshSettledForTests.delete(ref);
|
|
1226
|
+
statsRefreshSettledForTests.add(drainStatsRefreshRef);
|
|
1206
1227
|
|
|
1207
1228
|
function buildCurrentStatsShard(state: "active" | "closed" = "active"): PersistedStatsShardV7 {
|
|
1208
1229
|
const now = Date.now();
|
|
@@ -1698,6 +1719,10 @@ export default function (pi: ExtensionAPI) {
|
|
|
1698
1719
|
promptCacheRetention400Models.has(key),
|
|
1699
1720
|
anthropicTtlOrderErrorModels.has(key),
|
|
1700
1721
|
isPromptCacheKeyOmittedForModel(model),
|
|
1722
|
+
// The decision, not the raw compat.promptCacheKeyMode: "enabled",
|
|
1723
|
+
// "auto" and an unset mode all inject, so only "disabled" may split
|
|
1724
|
+
// otherwise-equal candidates (PR #32).
|
|
1725
|
+
shouldInjectOpenAIPromptCacheKeyForModel(model),
|
|
1701
1726
|
]);
|
|
1702
1727
|
}
|
|
1703
1728
|
|
|
@@ -1713,9 +1738,18 @@ export default function (pi: ExtensionAPI) {
|
|
|
1713
1738
|
// The same extension instance lives on across session switches
|
|
1714
1739
|
// (shutdown, then start): reopen what the shutdown closed.
|
|
1715
1740
|
refreshScheduler.open();
|
|
1741
|
+
statsRefreshSettledForTests.add(drainStatsRefreshRef);
|
|
1716
1742
|
latestStatsCtx = ctx;
|
|
1743
|
+
uninstallCacheHintsService ??= installCacheHints();
|
|
1717
1744
|
const tui = ctx.mode === "tui";
|
|
1718
|
-
if (sessionStartsHandled++ > 0)
|
|
1745
|
+
if (sessionStartsHandled++ > 0) {
|
|
1746
|
+
// The previous session's per-request state: its shutdown normally
|
|
1747
|
+
// clears it, but a start without one, or racing one, would inherit it.
|
|
1748
|
+
latestCacheHint = undefined;
|
|
1749
|
+
providerRequestStates.length = 0;
|
|
1750
|
+
delete getProtocolGlobal().__piCacheOptimizerCacheKey__;
|
|
1751
|
+
retireShardForSessionStart();
|
|
1752
|
+
}
|
|
1719
1753
|
resetStatsForSessionStart(event.reason, ctx);
|
|
1720
1754
|
const previousRestore = statsRestorePromise;
|
|
1721
1755
|
statsRestorePromise = previousRestore
|
|
@@ -1774,6 +1808,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
1774
1808
|
});
|
|
1775
1809
|
|
|
1776
1810
|
pi.on("session_shutdown", async (_event, ctx) => {
|
|
1811
|
+
// A session_start on this instance while the shutdown awaited owns the
|
|
1812
|
+
// state from then on: the cleanup below must not undo it.
|
|
1813
|
+
const shutdownGeneration = statsRestoreGeneration;
|
|
1777
1814
|
try {
|
|
1778
1815
|
// Close the gate first (no new background refresh, watcher event or
|
|
1779
1816
|
// scan-triggered publish), then let what is in flight finish, then
|
|
@@ -1788,12 +1825,17 @@ export default function (pi: ExtensionAPI) {
|
|
|
1788
1825
|
await retryUnpersistedRetiredShards();
|
|
1789
1826
|
}
|
|
1790
1827
|
} finally {
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1828
|
+
if (shutdownGeneration === statsRestoreGeneration) {
|
|
1829
|
+
closeStatsRefreshGate();
|
|
1830
|
+
statsRefreshSettledForTests.delete(drainStatsRefreshRef);
|
|
1831
|
+
latestStatsCtx = undefined;
|
|
1832
|
+
latestCacheHint = undefined;
|
|
1833
|
+
providerRequestStates.length = 0;
|
|
1834
|
+
delete getProtocolGlobal().__piCacheOptimizerCacheKey__;
|
|
1835
|
+
uninstallCacheHintsService?.();
|
|
1836
|
+
uninstallCacheHintsService = undefined;
|
|
1837
|
+
restoreCacheRetentionEnv(STARTUP_CACHE_RETENTION_ENV);
|
|
1838
|
+
}
|
|
1797
1839
|
}
|
|
1798
1840
|
});
|
|
1799
1841
|
|
package/package.json
CHANGED
package/src/routing.ts
CHANGED
|
@@ -502,12 +502,18 @@ export function installCacheHintsService(
|
|
|
502
502
|
options: { discardPrevious?: (value: unknown) => boolean } = {},
|
|
503
503
|
): () => void {
|
|
504
504
|
const globals = getProtocolGlobal();
|
|
505
|
-
|
|
505
|
+
// Never hold a previous service that would be discarded anyway: it is
|
|
506
|
+
// another optimizer instance, and holding it would pin that instance (and,
|
|
507
|
+
// transitively, every one before it) for this instance's lifetime.
|
|
508
|
+
let previous: unknown = globals[PI_CACHE_HINTS_SYMBOL];
|
|
509
|
+
if (previous !== undefined && options.discardPrevious?.(previous)) previous = undefined;
|
|
506
510
|
globals[PI_CACHE_HINTS_SYMBOL] = service;
|
|
507
511
|
return () => {
|
|
512
|
+
const restore = previous;
|
|
513
|
+
previous = undefined;
|
|
508
514
|
if (globals[PI_CACHE_HINTS_SYMBOL] !== service) return;
|
|
509
|
-
if (
|
|
510
|
-
globals[PI_CACHE_HINTS_SYMBOL] =
|
|
515
|
+
if (restore !== undefined) {
|
|
516
|
+
globals[PI_CACHE_HINTS_SYMBOL] = restore;
|
|
511
517
|
} else {
|
|
512
518
|
delete globals[PI_CACHE_HINTS_SYMBOL];
|
|
513
519
|
}
|