@cortexkit/common-auth 0.41.1 → 0.42.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/dist/cachekeep/index.d.ts +1 -1
- package/dist/cachekeep/manager.d.ts +38 -4
- package/dist/cachekeep/manager.js +213 -33
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type { CacheKeepDecision, CacheKeepDecisionInput } from './decision.js';
|
|
2
2
|
export { decideCacheKeep } from './decision.js';
|
|
3
|
-
export type { CacheKeepAdapter, CacheKeepBackoff, CacheKeepLogger, CacheKeepManagerOptions, CacheKeepProfile, CacheKeepSendInput, CacheKeepStatus, CacheKeepTargetStatus, CacheKeepTargetView, CacheKeepTrackInput, CacheKeepTrackResult, } from './manager.js';
|
|
3
|
+
export type { CacheKeepAdapter, CacheKeepBackoff, CacheKeepLogger, CacheKeepManagerOptions, CacheKeepPlan, CacheKeepProfile, CacheKeepSendInput, CacheKeepStatus, CacheKeepTargetStatus, CacheKeepTargetView, CacheKeepTrackInput, CacheKeepTrackResult, } from './manager.js';
|
|
4
4
|
export { CacheKeepManager } from './manager.js';
|
|
5
5
|
export type { CacheKeepWindow } from './window.js';
|
|
6
6
|
export { isWithinCacheKeepWindow, normalizeCacheKeepWindow, } from './window.js';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { SessionCacheHours, SessionKeepWarmMode } from '../routing-accessor/cache-policy.js';
|
|
1
2
|
import { type CacheKeepWindow } from './window.js';
|
|
2
3
|
export interface CacheKeepLogger {
|
|
3
4
|
debug(message: string, data?: unknown): void;
|
|
@@ -12,9 +13,9 @@ export interface CacheKeepLogger {
|
|
|
12
13
|
export interface CacheKeepProfile {
|
|
13
14
|
/** Provider cache lifetime for this request; defaults to the manager's `ttlMs`. */
|
|
14
15
|
ttlMs?: number;
|
|
15
|
-
/**
|
|
16
|
+
/** Legacy/unpriced-auto warm cap; unlimited when absent. */
|
|
16
17
|
maxWarms?: number;
|
|
17
|
-
/**
|
|
18
|
+
/** Legacy/unpriced-auto idle bound replacing the main or subagent default. */
|
|
18
19
|
maxIdleMs?: number;
|
|
19
20
|
}
|
|
20
21
|
/** What the plugin sees of a tracked target. */
|
|
@@ -34,12 +35,32 @@ export interface CacheKeepSendInput<M> {
|
|
|
34
35
|
/** Aborted on the warm timeout and when the manager stops. */
|
|
35
36
|
signal: AbortSignal;
|
|
36
37
|
}
|
|
38
|
+
/** A fresh, plugin-validated session policy with optional provider prices. */
|
|
39
|
+
export interface CacheKeepPlan {
|
|
40
|
+
mode: SessionKeepWarmMode;
|
|
41
|
+
hours?: SessionCacheHours;
|
|
42
|
+
costs?: {
|
|
43
|
+
nextWarm: number;
|
|
44
|
+
rewrite: number;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
37
47
|
/**
|
|
38
48
|
* The provider half of keep-warm. The manager decides when and what to warm;
|
|
39
49
|
* the plugin turns a captured request into a replay, resolves the account's
|
|
40
50
|
* credential, sends it and reads usage back.
|
|
41
51
|
*/
|
|
42
52
|
export interface CacheKeepAdapter<M> {
|
|
53
|
+
/** Read a fresh policy; undefined preserves legacy only if never planned. */
|
|
54
|
+
plan?(sessionKey: string): CacheKeepPlan | undefined;
|
|
55
|
+
/** Successful warm reports never block scheduling. */
|
|
56
|
+
onWarmed?(sessionKey: string, input: {
|
|
57
|
+
at: number;
|
|
58
|
+
}): void | Promise<void>;
|
|
59
|
+
/** Report retirement only after a target has successfully warmed. */
|
|
60
|
+
onWarmStopped?(sessionKey: string, input: {
|
|
61
|
+
at: number;
|
|
62
|
+
reason: string;
|
|
63
|
+
}): void | Promise<void>;
|
|
43
64
|
/** Build the replay body from the captured one. A throw backs the target off. */
|
|
44
65
|
buildBody(target: CacheKeepTargetView<M>): string | Promise<string>;
|
|
45
66
|
/**
|
|
@@ -107,7 +128,7 @@ export type CacheKeepTrackResult = {
|
|
|
107
128
|
tracked: true;
|
|
108
129
|
} | {
|
|
109
130
|
tracked: false;
|
|
110
|
-
reason: 'body-exceeds-max-bytes' | 'outside-window';
|
|
131
|
+
reason: 'body-exceeds-max-bytes' | 'outside-window' | 'unsupported' | 'session-ended';
|
|
111
132
|
};
|
|
112
133
|
export type CacheKeepTrackInput<M> = {
|
|
113
134
|
sessionKey: string;
|
|
@@ -122,6 +143,7 @@ export type CacheKeepTrackInput<M> = {
|
|
|
122
143
|
});
|
|
123
144
|
export interface CacheKeepTargetStatus {
|
|
124
145
|
sessionKey: string;
|
|
146
|
+
mode?: SessionKeepWarmMode;
|
|
125
147
|
accountId: string | undefined;
|
|
126
148
|
isSubagent: boolean;
|
|
127
149
|
cacheExpiresAt: number;
|
|
@@ -151,7 +173,7 @@ export interface CacheKeepStatus {
|
|
|
151
173
|
* Keeps idle prompt caches warm: one target per session holding the latest
|
|
152
174
|
* request body, replayed just before the provider's cache would expire.
|
|
153
175
|
*
|
|
154
|
-
*
|
|
176
|
+
* Without a session plan, bounds act in this order: a target retires once its last confirmed
|
|
155
177
|
* cache lifetime ends (`cacheExpiresAt`, set by its capture or its last
|
|
156
178
|
* successful warm), whatever else holds, because replaying a history whose
|
|
157
179
|
* cache has expired rebuilds a cold cache instead of keeping a warm one
|
|
@@ -165,6 +187,9 @@ export interface CacheKeepStatus {
|
|
|
165
187
|
* (expiry minus the warm timeout and 5 seconds). Only default-policy retries
|
|
166
188
|
* observe that deadline: first attempts and explicit backoff policies retain
|
|
167
189
|
* their existing lead-time scheduling, bounded by the cache lifetime.
|
|
190
|
+
* Session plans use the shared decision rule and one-shot due timers instead;
|
|
191
|
+
* only unpriced auto keeps the legacy idle and profile warm bounds. Lifetime,
|
|
192
|
+
* target-count, byte and LRU bounds apply to every target.
|
|
168
193
|
*/
|
|
169
194
|
export declare class CacheKeepManager<M = undefined> {
|
|
170
195
|
private readonly targets;
|
|
@@ -189,6 +214,7 @@ export declare class CacheKeepManager<M = undefined> {
|
|
|
189
214
|
private totalBytes;
|
|
190
215
|
private tickPromise;
|
|
191
216
|
private disposed;
|
|
217
|
+
private planWarningLogged;
|
|
192
218
|
private abortController;
|
|
193
219
|
constructor(options: CacheKeepManagerOptions<M>);
|
|
194
220
|
/**
|
|
@@ -219,6 +245,13 @@ export declare class CacheKeepManager<M = undefined> {
|
|
|
219
245
|
private retireIfExpired;
|
|
220
246
|
private isCurrent;
|
|
221
247
|
private view;
|
|
248
|
+
private readPlan;
|
|
249
|
+
private refreshPlan;
|
|
250
|
+
private usesLegacyBounds;
|
|
251
|
+
private planDecision;
|
|
252
|
+
private clearPlanTimer;
|
|
253
|
+
private schedulePlan;
|
|
254
|
+
private report;
|
|
222
255
|
private fail;
|
|
223
256
|
private lastRetryStart;
|
|
224
257
|
private canRetry;
|
|
@@ -227,6 +260,7 @@ export declare class CacheKeepManager<M = undefined> {
|
|
|
227
260
|
private warm;
|
|
228
261
|
private idleDefault;
|
|
229
262
|
private pruneStale;
|
|
263
|
+
private retireIfIdle;
|
|
230
264
|
private leastRecentlyTouched;
|
|
231
265
|
private drop;
|
|
232
266
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { combineAbortSignals } from '../util/any-signal.js';
|
|
2
|
+
import { decideCacheKeep } from './decision.js';
|
|
2
3
|
import { classifyFailure } from './failure.js';
|
|
3
4
|
import { isWithinCacheKeepWindow } from './window.js';
|
|
4
5
|
const DEFAULT_TTL_MS = 5 * 60_000;
|
|
@@ -21,7 +22,7 @@ function positive(value, fallback) {
|
|
|
21
22
|
* Keeps idle prompt caches warm: one target per session holding the latest
|
|
22
23
|
* request body, replayed just before the provider's cache would expire.
|
|
23
24
|
*
|
|
24
|
-
*
|
|
25
|
+
* Without a session plan, bounds act in this order: a target retires once its last confirmed
|
|
25
26
|
* cache lifetime ends (`cacheExpiresAt`, set by its capture or its last
|
|
26
27
|
* successful warm), whatever else holds, because replaying a history whose
|
|
27
28
|
* cache has expired rebuilds a cold cache instead of keeping a warm one
|
|
@@ -35,6 +36,9 @@ function positive(value, fallback) {
|
|
|
35
36
|
* (expiry minus the warm timeout and 5 seconds). Only default-policy retries
|
|
36
37
|
* observe that deadline: first attempts and explicit backoff policies retain
|
|
37
38
|
* their existing lead-time scheduling, bounded by the cache lifetime.
|
|
39
|
+
* Session plans use the shared decision rule and one-shot due timers instead;
|
|
40
|
+
* only unpriced auto keeps the legacy idle and profile warm bounds. Lifetime,
|
|
41
|
+
* target-count, byte and LRU bounds apply to every target.
|
|
38
42
|
*/
|
|
39
43
|
export class CacheKeepManager {
|
|
40
44
|
targets = new Map();
|
|
@@ -59,6 +63,7 @@ export class CacheKeepManager {
|
|
|
59
63
|
totalBytes = 0;
|
|
60
64
|
tickPromise = null;
|
|
61
65
|
disposed = false;
|
|
66
|
+
planWarningLogged = false;
|
|
62
67
|
abortController = new AbortController();
|
|
63
68
|
constructor(options) {
|
|
64
69
|
this.adapter = options.adapter;
|
|
@@ -88,7 +93,17 @@ export class CacheKeepManager {
|
|
|
88
93
|
*/
|
|
89
94
|
track(input) {
|
|
90
95
|
const { sessionKey, bodyText } = input;
|
|
91
|
-
this.
|
|
96
|
+
const previous = this.targets.get(sessionKey);
|
|
97
|
+
const plan = this.readPlan(sessionKey);
|
|
98
|
+
if (!plan && previous?.plan) {
|
|
99
|
+
this.drop(sessionKey, 'session-ended');
|
|
100
|
+
return { tracked: false, reason: 'session-ended' };
|
|
101
|
+
}
|
|
102
|
+
if (plan?.mode === 'off') {
|
|
103
|
+
this.drop(sessionKey, 'unsupported');
|
|
104
|
+
return { tracked: false, reason: 'unsupported' };
|
|
105
|
+
}
|
|
106
|
+
this.pruneStale(plan ? sessionKey : undefined);
|
|
92
107
|
const bodyBytes = Buffer.byteLength(bodyText, 'utf8');
|
|
93
108
|
if (bodyBytes > this.maxBytes) {
|
|
94
109
|
this.log?.debug('cachekeep track skipped (body exceeds maxBytes)', {
|
|
@@ -99,7 +114,9 @@ export class CacheKeepManager {
|
|
|
99
114
|
return { tracked: false, reason: 'body-exceeds-max-bytes' };
|
|
100
115
|
}
|
|
101
116
|
const window = this.getWindow?.();
|
|
102
|
-
if (
|
|
117
|
+
if (!plan &&
|
|
118
|
+
window &&
|
|
119
|
+
!isWithinCacheKeepWindow(window, new Date(this.now()))) {
|
|
103
120
|
this.log?.debug('cachekeep track skipped (outside window)', {
|
|
104
121
|
sessionKey,
|
|
105
122
|
startHour: window.startHour,
|
|
@@ -109,7 +126,8 @@ export class CacheKeepManager {
|
|
|
109
126
|
}
|
|
110
127
|
// Delete before re-adding so a retracked session is also the most recent
|
|
111
128
|
// in insertion order.
|
|
112
|
-
this.
|
|
129
|
+
const hasWarmed = this.targets.get(sessionKey)?.hasWarmed ?? false;
|
|
130
|
+
this.drop(sessionKey, null);
|
|
113
131
|
while (this.targets.size >= this.maxTargets ||
|
|
114
132
|
(this.totalBytes + bodyBytes > this.maxBytes && this.targets.size > 0)) {
|
|
115
133
|
const evictKey = this.leastRecentlyTouched();
|
|
@@ -118,7 +136,7 @@ export class CacheKeepManager {
|
|
|
118
136
|
this.log?.debug('cachekeep evicted least recently used target', {
|
|
119
137
|
sessionKey: evictKey,
|
|
120
138
|
});
|
|
121
|
-
this.drop(evictKey);
|
|
139
|
+
this.drop(evictKey, 'evicted');
|
|
122
140
|
}
|
|
123
141
|
const isSubagent = input.isSubagent === true;
|
|
124
142
|
const accountId = input.accountId || undefined;
|
|
@@ -130,7 +148,10 @@ export class CacheKeepManager {
|
|
|
130
148
|
});
|
|
131
149
|
const ttlMs = positive(profile?.ttlMs, this.ttlMs);
|
|
132
150
|
const now = this.now();
|
|
133
|
-
|
|
151
|
+
const target = {
|
|
152
|
+
plan,
|
|
153
|
+
warmCostSoFar: 0,
|
|
154
|
+
hasWarmed,
|
|
134
155
|
bodyText,
|
|
135
156
|
bodyBytes,
|
|
136
157
|
accountId,
|
|
@@ -147,10 +168,13 @@ export class CacheKeepManager {
|
|
|
147
168
|
lastRealRequestAt: now,
|
|
148
169
|
failures: 0,
|
|
149
170
|
warmCount: 0,
|
|
150
|
-
}
|
|
171
|
+
};
|
|
172
|
+
this.targets.set(sessionKey, target);
|
|
151
173
|
this.totalBytes += bodyBytes;
|
|
152
174
|
this.log?.debug('cachekeep captured target', { sessionKey, accountId });
|
|
153
175
|
this.start();
|
|
176
|
+
if (plan)
|
|
177
|
+
this.planDecision(sessionKey, target);
|
|
154
178
|
return { tracked: true };
|
|
155
179
|
}
|
|
156
180
|
/** Forget a session, e.g. when the host deletes it. */
|
|
@@ -188,9 +212,8 @@ export class CacheKeepManager {
|
|
|
188
212
|
this.timer = null;
|
|
189
213
|
}
|
|
190
214
|
this.startedAt = null;
|
|
191
|
-
for (const
|
|
192
|
-
this.
|
|
193
|
-
this.targets.clear();
|
|
215
|
+
for (const sessionKey of this.targets.keys())
|
|
216
|
+
this.drop(sessionKey, 'stopped');
|
|
194
217
|
this.totalBytes = 0;
|
|
195
218
|
this.log?.debug('cachekeep stopped');
|
|
196
219
|
}
|
|
@@ -211,6 +234,7 @@ export class CacheKeepManager {
|
|
|
211
234
|
...(window ? { window } : {}),
|
|
212
235
|
targets: [...this.targets].map(([sessionKey, target]) => ({
|
|
213
236
|
sessionKey,
|
|
237
|
+
...(target.plan ? { mode: target.plan.mode } : {}),
|
|
214
238
|
accountId: target.accountId,
|
|
215
239
|
isSubagent: target.isSubagent,
|
|
216
240
|
cacheExpiresAt: target.cacheExpiresAt,
|
|
@@ -249,20 +273,33 @@ export class CacheKeepManager {
|
|
|
249
273
|
// their cache lifetime ends, so one still alive when the window reopens
|
|
250
274
|
// is warmed again.
|
|
251
275
|
const window = this.getWindow?.();
|
|
252
|
-
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
276
|
+
const outsideWindow = window && !isWithinCacheKeepWindow(window, new Date(this.now()));
|
|
255
277
|
for (const [sessionKey, target] of [...this.targets]) {
|
|
256
278
|
if (this.disposed)
|
|
257
279
|
return;
|
|
258
280
|
if (this.targets.get(sessionKey) !== target)
|
|
259
281
|
continue;
|
|
282
|
+
if (!this.refreshPlan(sessionKey, target))
|
|
283
|
+
continue;
|
|
260
284
|
const now = this.now();
|
|
285
|
+
if (target.plan) {
|
|
286
|
+
const decision = this.planDecision(sessionKey, target);
|
|
287
|
+
if (decision.action !== 'warm')
|
|
288
|
+
continue;
|
|
289
|
+
if (target.backoffUntil !== undefined && now < target.backoffUntil) {
|
|
290
|
+
this.schedulePlan(sessionKey, target, target.backoffUntil);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
if (outsideWindow)
|
|
296
|
+
continue;
|
|
297
|
+
if (target.cacheExpiresAt > now + this.leadMs)
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
261
300
|
if (target.backoffUntil !== undefined && now < target.backoffUntil) {
|
|
262
301
|
continue;
|
|
263
302
|
}
|
|
264
|
-
if (target.cacheExpiresAt > now + this.leadMs)
|
|
265
|
-
continue;
|
|
266
303
|
if (!this.canRetry(target))
|
|
267
304
|
continue;
|
|
268
305
|
await this.warm(sessionKey, target);
|
|
@@ -282,7 +319,7 @@ export class CacheKeepManager {
|
|
|
282
319
|
cacheExpiresAt: target.cacheExpiresAt,
|
|
283
320
|
failures: target.failures,
|
|
284
321
|
});
|
|
285
|
-
this.drop(sessionKey);
|
|
322
|
+
this.drop(sessionKey, target.plan ? 'cost-cap' : 'expired');
|
|
286
323
|
return true;
|
|
287
324
|
}
|
|
288
325
|
isCurrent(sessionKey, target) {
|
|
@@ -299,6 +336,114 @@ export class CacheKeepManager {
|
|
|
299
336
|
ttlMs: target.ttlMs,
|
|
300
337
|
};
|
|
301
338
|
}
|
|
339
|
+
readPlan(sessionKey) {
|
|
340
|
+
try {
|
|
341
|
+
return this.adapter.plan?.(sessionKey);
|
|
342
|
+
}
|
|
343
|
+
catch {
|
|
344
|
+
if (!this.planWarningLogged) {
|
|
345
|
+
this.planWarningLogged = true;
|
|
346
|
+
this.log?.warn('cachekeep plan unavailable');
|
|
347
|
+
}
|
|
348
|
+
return undefined;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
refreshPlan(sessionKey, target) {
|
|
352
|
+
const plan = this.readPlan(sessionKey);
|
|
353
|
+
if (!plan && target.plan) {
|
|
354
|
+
this.drop(sessionKey, 'session-ended');
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
target.plan = plan;
|
|
358
|
+
if (plan?.mode === 'off') {
|
|
359
|
+
this.drop(sessionKey, 'unsupported');
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
usesLegacyBounds(target) {
|
|
365
|
+
return !target.plan || (target.plan.mode === 'auto' && !target.plan.costs);
|
|
366
|
+
}
|
|
367
|
+
planDecision(sessionKey, target) {
|
|
368
|
+
const plan = target.plan;
|
|
369
|
+
if (!plan)
|
|
370
|
+
return { action: 'stop', reason: 'session-ended' };
|
|
371
|
+
if (this.usesLegacyBounds(target)) {
|
|
372
|
+
if (this.retireIfIdle(sessionKey, target, this.getSustain?.() === true)) {
|
|
373
|
+
return { action: 'stop', reason: 'idle' };
|
|
374
|
+
}
|
|
375
|
+
if (target.maxWarms !== undefined &&
|
|
376
|
+
target.warmCount >= target.maxWarms) {
|
|
377
|
+
this.drop(sessionKey, 'count-cap');
|
|
378
|
+
return { action: 'stop', reason: 'count-cap' };
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
const decision = plan.mode === 'hours' && !plan.hours
|
|
382
|
+
? { action: 'stop', reason: 'unsupported' }
|
|
383
|
+
: decideCacheKeep({
|
|
384
|
+
...(plan.mode === 'hours'
|
|
385
|
+
? { mode: plan.mode, hours: plan.hours }
|
|
386
|
+
: { mode: plan.mode }),
|
|
387
|
+
now: this.now(),
|
|
388
|
+
ttlMs: target.ttlMs,
|
|
389
|
+
lastRealRequestAt: target.lastRealRequestAt,
|
|
390
|
+
lastWarmAt: target.lastWarmedAt,
|
|
391
|
+
warmsSinceLastRealRequest: target.warmCount,
|
|
392
|
+
...(plan.costs
|
|
393
|
+
? {
|
|
394
|
+
warmCostSoFar: target.warmCostSoFar,
|
|
395
|
+
nextWarmCost: plan.costs.nextWarm,
|
|
396
|
+
rewriteCost: plan.costs.rewrite,
|
|
397
|
+
}
|
|
398
|
+
: {}),
|
|
399
|
+
});
|
|
400
|
+
if (decision.action === 'stop')
|
|
401
|
+
this.drop(sessionKey, decision.reason);
|
|
402
|
+
else if (decision.action === 'wait') {
|
|
403
|
+
this.schedulePlan(sessionKey, target, Math.max(decision.until, target.backoffUntil ?? 0));
|
|
404
|
+
}
|
|
405
|
+
else
|
|
406
|
+
this.clearPlanTimer(target);
|
|
407
|
+
return decision;
|
|
408
|
+
}
|
|
409
|
+
clearPlanTimer(target) {
|
|
410
|
+
if (target.planTimer === undefined)
|
|
411
|
+
return;
|
|
412
|
+
this.clearIntervalImpl(target.planTimer);
|
|
413
|
+
target.planTimer = undefined;
|
|
414
|
+
target.planDueAt = undefined;
|
|
415
|
+
}
|
|
416
|
+
schedulePlan(sessionKey, target, until) {
|
|
417
|
+
if (target.planTimer !== undefined && target.planDueAt === until)
|
|
418
|
+
return;
|
|
419
|
+
this.clearPlanTimer(target);
|
|
420
|
+
target.planDueAt = until;
|
|
421
|
+
// Clear the injected interval before dispatch to make it a one-shot, just
|
|
422
|
+
// like retries. Waiting for an in-flight tick prevents a lost wake-up.
|
|
423
|
+
target.planTimer = this.setIntervalImpl(() => {
|
|
424
|
+
this.clearPlanTimer(target);
|
|
425
|
+
return (async () => {
|
|
426
|
+
await this.tickPromise;
|
|
427
|
+
if (this.isCurrent(sessionKey, target))
|
|
428
|
+
await this.tick();
|
|
429
|
+
})().catch((error) => {
|
|
430
|
+
this.log?.warn('cachekeep tick failed', classifyFailure(error, 'send'));
|
|
431
|
+
});
|
|
432
|
+
}, Math.max(0, until - this.now()));
|
|
433
|
+
if (typeof target.planTimer === 'object' && 'unref' in target.planTimer) {
|
|
434
|
+
target.planTimer.unref();
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
report(callback) {
|
|
438
|
+
try {
|
|
439
|
+
void Promise.resolve(callback()).catch(() => {
|
|
440
|
+
this.log?.debug('cachekeep report failed');
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
catch {
|
|
444
|
+
this.log?.debug('cachekeep report failed');
|
|
445
|
+
}
|
|
446
|
+
}
|
|
302
447
|
fail(sessionKey, target, message, failure, transient = false, status) {
|
|
303
448
|
if (!this.isCurrent(sessionKey, target))
|
|
304
449
|
return;
|
|
@@ -317,6 +462,9 @@ export class CacheKeepManager {
|
|
|
317
462
|
this.clearRetry(target);
|
|
318
463
|
if (fastRetry)
|
|
319
464
|
this.scheduleRetry(sessionKey, target, delay);
|
|
465
|
+
else if (target.plan) {
|
|
466
|
+
this.schedulePlan(sessionKey, target, target.backoffUntil);
|
|
467
|
+
}
|
|
320
468
|
this.log?.warn(message, {
|
|
321
469
|
sessionKey,
|
|
322
470
|
accountId: target.accountId,
|
|
@@ -383,7 +531,7 @@ export class CacheKeepManager {
|
|
|
383
531
|
accountId: target.accountId,
|
|
384
532
|
activeAccountId: active,
|
|
385
533
|
});
|
|
386
|
-
this.drop(sessionKey);
|
|
534
|
+
this.drop(sessionKey, 'account-changed');
|
|
387
535
|
return;
|
|
388
536
|
}
|
|
389
537
|
}
|
|
@@ -401,6 +549,13 @@ export class CacheKeepManager {
|
|
|
401
549
|
return;
|
|
402
550
|
if (!this.canRetry(target))
|
|
403
551
|
return;
|
|
552
|
+
if (!this.refreshPlan(sessionKey, target))
|
|
553
|
+
return;
|
|
554
|
+
if (target.plan &&
|
|
555
|
+
this.planDecision(sessionKey, target).action !== 'warm') {
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
const nextWarmCost = target.plan?.costs?.nextWarm ?? 0;
|
|
404
559
|
const combined = combineAbortSignals([
|
|
405
560
|
AbortSignal.timeout(this.warmTimeoutMs),
|
|
406
561
|
this.abortController.signal,
|
|
@@ -450,45 +605,66 @@ export class CacheKeepManager {
|
|
|
450
605
|
this.clearRetry(target);
|
|
451
606
|
target.failures = 0;
|
|
452
607
|
target.warmCount += 1;
|
|
608
|
+
target.hasWarmed = true;
|
|
609
|
+
target.warmCostSoFar += nextWarmCost;
|
|
610
|
+
if (this.adapter.onWarmed) {
|
|
611
|
+
this.report(() => this.adapter.onWarmed?.(sessionKey, { at: now }));
|
|
612
|
+
}
|
|
453
613
|
this.log?.debug('cachekeep fired', {
|
|
454
614
|
sessionKey,
|
|
455
615
|
accountId: target.accountId,
|
|
456
616
|
warmCount: target.warmCount,
|
|
457
617
|
...usage,
|
|
458
618
|
});
|
|
459
|
-
if (
|
|
619
|
+
if (this.usesLegacyBounds(target) &&
|
|
620
|
+
target.maxWarms !== undefined &&
|
|
621
|
+
target.warmCount >= target.maxWarms) {
|
|
460
622
|
this.log?.debug('cachekeep retired target (warm cap reached)', {
|
|
461
623
|
sessionKey,
|
|
462
624
|
warmCount: target.warmCount,
|
|
463
625
|
});
|
|
464
|
-
this.drop(sessionKey);
|
|
626
|
+
this.drop(sessionKey, 'count-cap');
|
|
627
|
+
}
|
|
628
|
+
if (target.plan && this.isCurrent(sessionKey, target)) {
|
|
629
|
+
if (this.refreshPlan(sessionKey, target))
|
|
630
|
+
this.planDecision(sessionKey, target);
|
|
465
631
|
}
|
|
466
632
|
}
|
|
467
633
|
idleDefault(isSubagent) {
|
|
468
634
|
return isSubagent ? this.maxSubagentIdleMs : this.maxIdleWarmMs;
|
|
469
635
|
}
|
|
470
|
-
pruneStale() {
|
|
636
|
+
pruneStale(skipSessionKey) {
|
|
471
637
|
const now = this.now();
|
|
472
638
|
const sustain = this.getSustain?.() === true;
|
|
473
639
|
for (const [sessionKey, target] of [...this.targets]) {
|
|
640
|
+
if (sessionKey === skipSessionKey)
|
|
641
|
+
continue;
|
|
474
642
|
if (this.retireIfExpired(sessionKey, target))
|
|
475
643
|
continue;
|
|
476
|
-
|
|
477
|
-
// window, count, byte and warm caps still apply to them.
|
|
478
|
-
if (sustain && !target.isSubagent)
|
|
644
|
+
if (!this.refreshPlan(sessionKey, target))
|
|
479
645
|
continue;
|
|
480
|
-
|
|
481
|
-
if (target.lastRealRequestAt >= now - maxIdleMs)
|
|
646
|
+
if (!this.usesLegacyBounds(target))
|
|
482
647
|
continue;
|
|
483
|
-
this.
|
|
484
|
-
sessionKey,
|
|
485
|
-
accountId: target.accountId,
|
|
486
|
-
lastRealRequestAt: target.lastRealRequestAt,
|
|
487
|
-
maxIdleMs,
|
|
488
|
-
});
|
|
489
|
-
this.drop(sessionKey);
|
|
648
|
+
this.retireIfIdle(sessionKey, target, sustain, now);
|
|
490
649
|
}
|
|
491
650
|
}
|
|
651
|
+
retireIfIdle(sessionKey, target, sustain, now = this.now()) {
|
|
652
|
+
// Sustain exempts main sessions from the idle cap only; the lifetime,
|
|
653
|
+
// window, count, byte and warm caps still apply to them.
|
|
654
|
+
if (sustain && !target.isSubagent)
|
|
655
|
+
return false;
|
|
656
|
+
const maxIdleMs = target.maxIdleMs ?? this.idleDefault(target.isSubagent);
|
|
657
|
+
if (target.lastRealRequestAt >= now - maxIdleMs)
|
|
658
|
+
return false;
|
|
659
|
+
this.log?.debug('cachekeep pruned idle target', {
|
|
660
|
+
sessionKey,
|
|
661
|
+
accountId: target.accountId,
|
|
662
|
+
lastRealRequestAt: target.lastRealRequestAt,
|
|
663
|
+
maxIdleMs,
|
|
664
|
+
});
|
|
665
|
+
this.drop(sessionKey, 'idle');
|
|
666
|
+
return true;
|
|
667
|
+
}
|
|
492
668
|
leastRecentlyTouched() {
|
|
493
669
|
let evictKey;
|
|
494
670
|
let evictTouchedAt = Number.POSITIVE_INFINITY;
|
|
@@ -503,13 +679,17 @@ export class CacheKeepManager {
|
|
|
503
679
|
}
|
|
504
680
|
return evictKey;
|
|
505
681
|
}
|
|
506
|
-
drop(sessionKey) {
|
|
682
|
+
drop(sessionKey, reason = 'session-ended') {
|
|
507
683
|
const target = this.targets.get(sessionKey);
|
|
508
684
|
if (!target)
|
|
509
685
|
return false;
|
|
510
686
|
this.clearRetry(target);
|
|
687
|
+
this.clearPlanTimer(target);
|
|
511
688
|
this.totalBytes -= target.bodyBytes;
|
|
512
689
|
this.targets.delete(sessionKey);
|
|
690
|
+
if (reason !== null && target.hasWarmed && this.adapter.onWarmStopped) {
|
|
691
|
+
this.report(() => this.adapter.onWarmStopped?.(sessionKey, { at: this.now(), reason }));
|
|
692
|
+
}
|
|
513
693
|
return true;
|
|
514
694
|
}
|
|
515
695
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cortexkit/common-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.42.0",
|
|
4
4
|
"description": "Shared code for the CortexKit auth plugins: account pool, quota and routing, commands and auth menu, OpenCode 2 hooks, Claustrum custody, and plumbing (loopback RPC, file locks, logger, sidebar state, TUI preferences and build).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|