@cortexkit/common-auth 0.50.0 → 0.51.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.
@@ -3,6 +3,12 @@ export type CacheKeepDecisionInput = {
3
3
  /** All times and the cache lifetime are in milliseconds. */
4
4
  now: number;
5
5
  ttlMs: number;
6
+ /**
7
+ * How long before expiry the warm falls due, so a slow or failed send still
8
+ * has time for retries before the cache lapses. Capped at half the lifetime.
9
+ * Default 30 s, which leaves room for a single send.
10
+ */
11
+ leadMs?: number;
6
12
  lastRealRequestAt: number;
7
13
  lastWarmAt?: number;
8
14
  /** The caller resets the count and costs on every real request. */
@@ -23,7 +23,7 @@ function withinHours(hours, at) {
23
23
  * report a stop for a session that never warmed.
24
24
  */
25
25
  export function decideCacheKeep(input) {
26
- const { mode, now, ttlMs, lastRealRequestAt, lastWarmAt, warmsSinceLastRealRequest, warmCostSoFar, nextWarmCost, rewriteCost, } = input;
26
+ const { mode, now, ttlMs, leadMs = 30_000, lastRealRequestAt, lastWarmAt, warmsSinceLastRealRequest, warmCostSoFar, nextWarmCost, rewriteCost, } = input;
27
27
  if (mode === 'off')
28
28
  return { action: 'stop', reason: 'unsupported' };
29
29
  if (!Number.isFinite(ttlMs) || ttlMs <= 0) {
@@ -40,11 +40,13 @@ export function decideCacheKeep(input) {
40
40
  return { action: 'stop', reason: 'cost-cap' };
41
41
  const last = Math.max(lastRealRequestAt, lastWarmAt ?? lastRealRequestAt);
42
42
  const expiresAt = last + ttlMs;
43
- const due = expiresAt - Math.min(30_000, ttlMs / 2);
43
+ const lead = Math.min(Number.isFinite(leadMs) && leadMs > 0 ? leadMs : 30_000, ttlMs / 2);
44
+ const due = expiresAt - lead;
44
45
  const at = Math.max(now, due);
45
- // Replaying inside the expiry margin risks paying for a cold cache rewrite.
46
+ // Replaying inside the expiry margin risks paying for a cold cache rewrite,
47
+ // so a warm that can no longer start in time stops as a missed deadline.
46
48
  if (at >= expiresAt - Math.min(5_000, ttlMs / 10)) {
47
- return { action: 'stop', reason: 'cost-cap' };
49
+ return { action: 'stop', reason: 'deadline-missed' };
48
50
  }
49
51
  if (mode === 'hours' &&
50
52
  (!withinHours(input.hours, due) || !withinHours(input.hours, at)))
@@ -103,6 +103,13 @@ export interface CacheKeepManagerOptions<M> {
103
103
  ttlMs?: number;
104
104
  /** Warm this long before the cache expires. Default one tick plus 15 s. */
105
105
  leadMs?: number;
106
+ /**
107
+ * How long before expiry a planned warm (a session with a cache plan) falls
108
+ * due, capped at half the cache lifetime. Default 30 s, which leaves room
109
+ * for only one send. Set it longer, for example 5 minutes, when a slow send
110
+ * needs room for retries before the cache lapses.
111
+ */
112
+ planLeadMs?: number;
106
113
  /** Idle bound for main sessions, from their last real request. Default 1 h. */
107
114
  maxIdleWarmMs?: number;
108
115
  /** Idle bound for subagent sessions. Default 30 minutes. */
@@ -199,6 +206,7 @@ export declare class CacheKeepManager<M = undefined> {
199
206
  private readonly log;
200
207
  private readonly ttlMs;
201
208
  private readonly leadMs;
209
+ private readonly planLeadMs;
202
210
  private readonly maxIdleWarmMs;
203
211
  private readonly maxSubagentIdleMs;
204
212
  private readonly tickIntervalMs;
@@ -48,6 +48,7 @@ export class CacheKeepManager {
48
48
  log;
49
49
  ttlMs;
50
50
  leadMs;
51
+ planLeadMs;
51
52
  maxIdleWarmMs;
52
53
  maxSubagentIdleMs;
53
54
  tickIntervalMs;
@@ -75,6 +76,7 @@ export class CacheKeepManager {
75
76
  // One tick of slack so a target that becomes due between ticks is still
76
77
  // warmed before its cache expires.
77
78
  this.leadMs = positive(options.leadMs, this.tickIntervalMs + 15_000);
79
+ this.planLeadMs = options.planLeadMs;
78
80
  this.maxIdleWarmMs = positive(options.maxIdleWarmMs, DEFAULT_MAX_IDLE_WARM_MS);
79
81
  this.maxSubagentIdleMs = positive(options.maxSubagentIdleMs, DEFAULT_MAX_SUBAGENT_IDLE_MS);
80
82
  this.maxTargets = positive(options.maxTargets, DEFAULT_MAX_TARGETS);
@@ -337,7 +339,7 @@ export class CacheKeepManager {
337
339
  cacheExpiresAt: target.cacheExpiresAt,
338
340
  failures: target.failures,
339
341
  });
340
- this.drop(sessionKey, target.plan ? 'cost-cap' : 'expired');
342
+ this.drop(sessionKey, target.plan ? 'deadline-missed' : 'expired');
341
343
  return true;
342
344
  }
343
345
  isCurrent(sessionKey, target) {
@@ -404,6 +406,9 @@ export class CacheKeepManager {
404
406
  : { mode: plan.mode }),
405
407
  now: this.now(),
406
408
  ttlMs: target.ttlMs,
409
+ ...(this.planLeadMs !== undefined
410
+ ? { leadMs: this.planLeadMs }
411
+ : {}),
407
412
  lastRealRequestAt: target.lastRealRequestAt,
408
413
  lastWarmAt: target.lastWarmedAt,
409
414
  warmsSinceLastRealRequest: target.warmCount,
@@ -14,14 +14,14 @@ export interface SessionToolWait {
14
14
  /** Epoch milliseconds when the oldest running plan tool started. */
15
15
  since: number;
16
16
  }
17
- export declare const CACHE_WARM_STOP_REASONS: readonly ['cost-cap', 'count-cap', 'outside-hours', 'session-ended', 'unsupported'];
17
+ export declare const CACHE_WARM_STOP_REASONS: readonly ['cost-cap', 'count-cap', 'deadline-missed', 'outside-hours', 'session-ended', 'unsupported'];
18
18
  /** Billing rejects unknown stop reasons; omit them rather than guessing. */
19
19
  export declare function isCacheWarmStopReason(value: unknown): value is (typeof CACHE_WARM_STOP_REASONS)[number];
20
20
  /**
21
21
  * The stop reasons the llm-runner contract defines today; any other string is
22
22
  * accepted too, for reasons that contract adds later.
23
23
  */
24
- export type CacheWarmStopReason = 'cost-cap' | 'count-cap' | 'outside-hours' | 'session-ended' | 'unsupported' | (string & Record<never, never>);
24
+ export type CacheWarmStopReason = 'cost-cap' | 'count-cap' | 'deadline-missed' | 'outside-hours' | 'session-ended' | 'unsupported' | (string & Record<never, never>);
25
25
  export interface CacheTouchInput {
26
26
  /** Integer epoch milliseconds when the provider answered. */
27
27
  at: number;
@@ -2,6 +2,7 @@ import { routingHandle } from './handle.js';
2
2
  export const CACHE_WARM_STOP_REASONS = [
3
3
  'cost-cap',
4
4
  'count-cap',
5
+ 'deadline-missed',
5
6
  'outside-hours',
6
7
  'session-ended',
7
8
  'unsupported',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/common-auth",
3
- "version": "0.50.0",
3
+ "version": "0.51.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": {