@cortexkit/anthropic-auth-core 2.6.0 → 2.7.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,7 +3,7 @@ import { type OwnerOnlyOptions } from './owner-only.ts';
3
3
  declare const RECORD_FORMAT_VERSION = 1;
4
4
  export declare function cacheKeepStoreDirectory(storageId: string, root?: string, host?: 'pi'): string;
5
5
  export declare function legacyCacheKeepStoreDirectory(storageId: string, root?: string): string;
6
- export type SavedCacheKeepTarget = Pick<CacheKeepTarget, 'id' | 'bodyText' | 'url' | 'cacheExpiresAt' | 'nextPrewarmAt' | 'oauthAccountIdentity' | 'isSubagent' | 'ttlMs' | 'lastTouchAt' | 'lastTouchKind'> & {
6
+ export type SavedCacheKeepTarget = Pick<CacheKeepTarget, 'id' | 'bodyText' | 'url' | 'cacheExpiresAt' | 'nextPrewarmAt' | 'oauthAccountIdentity' | 'isSubagent' | 'ttlMs' | 'lastTouchAt' | 'lastTouchKind' | 'policyState'> & {
7
7
  v: typeof RECORD_FORMAT_VERSION;
8
8
  pluginVersion: string;
9
9
  };
@@ -27,6 +27,9 @@ export declare class CacheKeepStore {
27
27
  private readonly options;
28
28
  readonly directory: string;
29
29
  private disabled;
30
+ private memoryOnly;
31
+ private persistenceEnabled;
32
+ private writeWarning;
30
33
  private legacyLoadAttempted;
31
34
  private chain;
32
35
  private readonly pending;
@@ -35,11 +38,20 @@ export declare class CacheKeepStore {
35
38
  private readonly latest;
36
39
  constructor(options: CacheKeepStoreOptions, previous?: CacheKeepStore);
37
40
  private refuse;
41
+ private writeFailed;
38
42
  private directoryReady;
39
43
  private enqueue;
40
44
  load(now: number, accept: (entry: SavedCacheKeepTarget) => Acceptance, claim?: (sessionHash: string) => Promise<(() => Promise<void>) | undefined>): Promise<void>;
41
45
  changed(target: Readonly<CacheKeepTarget>): void;
42
46
  private flush;
47
+ /**
48
+ * Bypass coalescing before a persisted replay can dispatch. An initially
49
+ * unsafe folder (a symlink, foreign owner, or non-private permissions; see
50
+ * directoryReady) disables both writes and restores for this process, so its
51
+ * targets use memory-only bounds. A write failure in an enabled store must
52
+ * pause that warm and retry: an older low-count record could still resume.
53
+ */
54
+ reserveWarm(target: Readonly<CacheKeepTarget>): Promise<boolean>;
43
55
  private writeEntry;
44
56
  removed(id: string): void;
45
57
  clear(): void;
@@ -1,3 +1,4 @@
1
+ import type { CacheWarmStopReason } from "./internal-types/routing-accessor/index.js";
1
2
  import type { AccountStorage } from './accounts.ts';
2
3
  import { type CacheKeepFailure } from './cachekeep-failure.ts';
3
4
  import type { CacheKeepStopReason, CacheKeepTrackedSession } from './cachekeep-registry.ts';
@@ -74,6 +75,22 @@ export declare function buildCacheKeepPrewarmBody(bodyText: string, options?: {
74
75
  ok: false;
75
76
  reason: string;
76
77
  }>;
78
+ export type CacheKeepPolicyState = {
79
+ ownerSessionId: string;
80
+ planned: boolean;
81
+ lastRealRequestAt: number;
82
+ lastWarmAt?: number;
83
+ warmsSinceLastRealRequest: number;
84
+ /** Micro-USD: per-million-token catalogue rate multiplied by prefix tokens. */
85
+ warmCostSoFar: number;
86
+ /** Admitted cost durably reserved for dispatches whose result is not saved yet. */
87
+ pendingWarmCost?: number;
88
+ prefixTokens?: number;
89
+ inputTokens?: number;
90
+ hasWarmed: boolean;
91
+ /** A restored local hours-window record lacks the accepted real-request baseline. */
92
+ needsRealRequest?: boolean;
93
+ };
77
94
  export type CacheKeepTarget = {
78
95
  id: string;
79
96
  url: string;
@@ -88,6 +105,7 @@ export type CacheKeepTarget = {
88
105
  oauthAccountIdentity?: string;
89
106
  accountStoragePath?: string;
90
107
  isSubagent: boolean;
108
+ policyState?: CacheKeepPolicyState;
91
109
  /**
92
110
  * A recovery-round warm for the original (source) model, tracked under a
93
111
  * key that cannot collide with the session's live target. It follows the
@@ -96,6 +114,11 @@ export type CacheKeepTarget = {
96
114
  */
97
115
  sourceWarm?: boolean;
98
116
  } & Pick<CacheKeepTrackedSession, 'ttlMs' | 'lastTouchAt' | 'lastTouchKind'>;
117
+ type AcceptedCacheUsage = {
118
+ cache_read_input_tokens?: number;
119
+ cache_creation_input_tokens?: number;
120
+ input_tokens?: number;
121
+ };
99
122
  /**
100
123
  * Fingerprint of the cacheable prefix of a request body: tools, system
101
124
  * blocks, and messages up to the last cache breakpoint. The billing header
@@ -123,6 +146,7 @@ export type CacheKeepPrewarmAttempt = {
123
146
  export type CacheKeepPrewarmResult = {
124
147
  ok: true;
125
148
  refused?: false;
149
+ ttlMs?: number;
126
150
  usage?: {
127
151
  input_tokens?: number;
128
152
  cache_creation_input_tokens?: number;
@@ -153,13 +177,21 @@ export type CacheKeepPrewarmValidation = {
153
177
  ok: false;
154
178
  reason: string;
155
179
  };
180
+ export declare class CacheKeepWarmRefusal extends Error {
181
+ readonly reason: CacheWarmStopReason;
182
+ constructor(reason: CacheWarmStopReason);
183
+ }
156
184
  export declare class CacheKeepManager {
157
185
  private readonly options;
158
186
  private readonly targets;
187
+ private readonly immediateTargets;
159
188
  private readonly stoppedSessions;
160
189
  private timer;
190
+ private dueTimer;
161
191
  private tickPromise;
162
192
  private nextPrewarmAttemptId;
193
+ private readonly reportedStops;
194
+ private readonly admittedWarmCosts;
163
195
  constructor(options: {
164
196
  loadStorage: () => Promise<AccountStorage | null>;
165
197
  fetchImpl?: typeof fetch;
@@ -167,6 +199,8 @@ export declare class CacheKeepManager {
167
199
  prewarmTimeoutMs?: number;
168
200
  setIntervalImpl?: typeof globalThis.setInterval;
169
201
  clearIntervalImpl?: typeof globalThis.clearInterval;
202
+ setTimeoutImpl?: typeof globalThis.setTimeout;
203
+ clearTimeoutImpl?: typeof globalThis.clearTimeout;
170
204
  prepareHeaders?: (headers: Headers, target: CacheKeepTarget, attempt: CacheKeepPrewarmAttempt) => Promise<Headers | undefined> | Headers | undefined;
171
205
  /** Optionally reauthorize a replayable prewarm after a genuine 401.
172
206
  * Return headers only for a verified newer version of the same account;
@@ -181,6 +215,14 @@ export declare class CacheKeepManager {
181
215
  /** Hosts may mirror targets without delaying a live request on storage I/O. */
182
216
  onTargetChanged?: (target: Readonly<CacheKeepTarget>) => void;
183
217
  onTargetRemoved?: (id: string) => void;
218
+ /** Persistent hosts must commit the reservation before any physical send. */
219
+ reserveWarm?: (target: Readonly<CacheKeepTarget>) => Promise<boolean>;
220
+ /** Persist a known failed dispatch so restore need not charge its cost. */
221
+ settleWarm?: (target: Readonly<CacheKeepTarget>) => Promise<boolean>;
222
+ /** Native hosts authorize asynchronously inside fetchImpl and reserve there. */
223
+ deferWarmReservation?: boolean;
224
+ /** A native host may rewrite the final physical warm after fetchImpl starts. */
225
+ responseTtlMs?: (response: Response) => number | undefined;
184
226
  prepareBody?: (bodyText: string, target: CacheKeepTarget) => string | Promise<string>;
185
227
  /**
186
228
  * Veto a prewarm after its body is built but before anything is
@@ -236,6 +278,22 @@ export declare class CacheKeepManager {
236
278
  */
237
279
  untrack(id: string, reason?: CacheKeepStopReason): boolean;
238
280
  trackedCount(): number;
281
+ isCurrentTarget(target: Readonly<CacheKeepTarget>): boolean;
282
+ /**
283
+ * The tracked policy state keeps the same object identity across warm
284
+ * updates; each newly accepted real request gets a new one.
285
+ */
286
+ trackedPolicyState(id: string): Readonly<CacheKeepPolicyState> | undefined;
287
+ hasPlannedTarget(id: string): boolean;
288
+ updateSubagent(id: string, isSubagent: boolean): void;
289
+ /** An old response cannot price or reset a newer captured request. */
290
+ recordAcceptedUsage(id: string, bodyText: string, usage: AcceptedCacheUsage): void;
291
+ private costs;
292
+ private policyDecision;
293
+ private countDecision;
294
+ /** Called at the physical boundary, after authorization and before fetch. */
295
+ reserveWarmDispatch(target: Readonly<CacheKeepTarget>): Promise<void>;
296
+ private scheduleDueTimer;
239
297
  /**
240
298
  * Drop targets whose cache has already expired, without sending or
241
299
  * publishing anything. Returns the earliest expiry among the targets that
@@ -246,6 +304,7 @@ export declare class CacheKeepManager {
246
304
  private totalBodyBytes;
247
305
  private removeTarget;
248
306
  private retainStopped;
307
+ private reportStop;
249
308
  private evictOldestTarget;
250
309
  private pruneTargets;
251
310
  track(input: {
@@ -260,9 +319,13 @@ export declare class CacheKeepManager {
260
319
  accountStoragePath?: string;
261
320
  isSubagent?: boolean;
262
321
  sourceWarm?: boolean;
322
+ /** Source targets use a synthetic id but belong to a real host session. */
323
+ ownerSessionId?: string;
324
+ acceptedAt?: number;
325
+ acceptedUsage?: AcceptedCacheUsage;
263
326
  ttlMs?: number;
264
327
  /** Restore only the remaining lifetime, never grant a restarted cache a new hour. */
265
- retained?: Pick<CacheKeepTarget, 'cacheExpiresAt' | 'nextPrewarmAt' | 'ttlMs' | 'lastTouchAt' | 'lastTouchKind'>;
328
+ retained?: Pick<CacheKeepTarget, 'cacheExpiresAt' | 'nextPrewarmAt' | 'ttlMs' | 'lastTouchAt' | 'lastTouchKind' | 'policyState'>;
266
329
  }): {
267
330
  tracked: true;
268
331
  } | {
@@ -285,4 +348,6 @@ export declare class CacheKeepManager {
285
348
  private scheduleRetry;
286
349
  private sendPrewarm;
287
350
  private prewarm;
351
+ private acceptWarm;
288
352
  }
353
+ export {};
package/dist/index.d.ts CHANGED
@@ -101,6 +101,7 @@ export * from './request-history.ts';
101
101
  export * from './routing.ts';
102
102
  export * from './server-fallback-usage.ts';
103
103
  export * from './session-cache-activity.ts';
104
+ export * from './session-cache-policy.ts';
104
105
  export type { SseEvent, SseFramer, SseFramerOptions } from './sse.ts';
105
106
  export { createSseFramer, DEFAULT_SSE_MAX_FRAME_BYTES, decodeSseFrame, isSseCandidateFrame, isTerminatedSseFrame, parseSseFrame, SseFrameTooLargeError, } from './sse.ts';
106
107
  export * from './start.ts';