@cortexkit/common-auth 0.49.2 → 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,
@@ -1,5 +1,9 @@
1
1
  import { basename, dirname, join, resolve } from 'node:path';
2
2
  const maxSymlinkHops = 40;
3
+ // Same shape as Node's realpath ELOOP, so callers that branch on `code` keep working.
4
+ function symlinkLoop(file) {
5
+ return Object.assign(new Error(`ELOOP: too many symbolic links encountered, realpath '${file}'`), { code: 'ELOOP', syscall: 'realpath', path: file });
6
+ }
3
7
  /**
4
8
  * Canonical path of a file that other processes may replace by write-and-rename.
5
9
  *
@@ -26,7 +30,7 @@ export async function canonicalFilePath(file, fs) {
26
30
  if (!isSymlink)
27
31
  return target;
28
32
  if (hops >= maxSymlinkHops)
29
- throw new Error('Too many file symlink hops');
33
+ throw symlinkLoop(file);
30
34
  target = resolve(dirname(target), await fs.readlink(target));
31
35
  }
32
36
  }
@@ -47,7 +51,7 @@ export function canonicalFilePathSync(file, fs) {
47
51
  if (!isSymlink)
48
52
  return target;
49
53
  if (hops >= maxSymlinkHops)
50
- throw new Error('Too many file symlink hops');
54
+ throw symlinkLoop(file);
51
55
  target = resolve(dirname(target), fs.readlinkSync(target));
52
56
  }
53
57
  }
@@ -92,7 +92,7 @@ export const confirmedAffinity = immutable({
92
92
  export const earliestPoolEngine = immutable({
93
93
  evidence: 'typed',
94
94
  markers: hosts(ANTIGRAVITY_FAILURE_LADDER),
95
- readingClearsCoveredScopes: 'off',
95
+ readingClearsCoveredScopes: 'on',
96
96
  successClearsCoveredScopes: 'off',
97
97
  unknown: hosts(reselect),
98
98
  shortLimit: {
@@ -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',
@@ -19,4 +19,4 @@ export type { PullReason } from './runtime.js';
19
19
  export type { ApiKeyCredential, CredentialStampStatus, OAuthCredential, PoolCredential, PoolRow, ProviderStateCodec, ProviderStateDrop, ProviderStateReplacement, QuotaCodec, RotateCredential, StoredCredential, } from './schema.js';
20
20
  export { fingerprintOf, LEGACY_STORE_VERSION, POOL_KEY, POOL_ROWS_KEY, POOL_SCHEMA_VERSION, PROVIDER_STATE_KEY, REFRESH_STAMP_TOLERANCE_MS, rowLockKey, } from './schema.js';
21
21
  export type { PoolSettings, SettingsMutator, SettingsRead, UpdateSettingsOptions, UpdateSettingsResult, } from './settings.js';
22
- export { POOL_OWNED_KEYS } from './settings.js';
22
+ export { POOL_OWNED_KEYS, readPoolSettingsAt } from './settings.js';
@@ -4,4 +4,4 @@ export { openPoolStore } from './pool.js';
4
4
  export { DECLINE_TRANSITION } from './provider-state.js';
5
5
  export { POOL_LOCK_DEFAULTS } from './refresh-lock.js';
6
6
  export { fingerprintOf, LEGACY_STORE_VERSION, POOL_KEY, POOL_ROWS_KEY, POOL_SCHEMA_VERSION, PROVIDER_STATE_KEY, REFRESH_STAMP_TOLERANCE_MS, rowLockKey, } from './schema.js';
7
- export { POOL_OWNED_KEYS } from './settings.js';
7
+ export { POOL_OWNED_KEYS, readPoolSettingsAt } from './settings.js';
@@ -52,6 +52,14 @@ export type UpdateSettingsResult = {
52
52
  };
53
53
  /** Reads only the config's settings, without taking a lock or writing anything. */
54
54
  export declare function readPoolSettings(rt: StoreRuntime): Promise<SettingsRead>;
55
+ /**
56
+ * Reads the settings of the pool config at `configPath`, exactly as a store's
57
+ * `readSettings()` does, without opening a store. It reads only that file: no
58
+ * state file, lock, write or migration. A plugin that has already resolved
59
+ * which config file to trust (for example, after a location check) reads it
60
+ * here rather than opening a second store on it.
61
+ */
62
+ export declare function readPoolSettingsAt(configPath: string): Promise<SettingsRead>;
55
63
  /**
56
64
  * One locked read-modify-write of the plugin's settings in the config file
57
65
  * the pool lives in. The pool must be ready (a pending migration or a load
@@ -63,9 +63,19 @@ function settingsRefusal(message) {
63
63
  }
64
64
  const settingsCache = new FileReadCache();
65
65
  /** Reads only the config's settings, without taking a lock or writing anything. */
66
- export async function readPoolSettings(rt) {
67
- const read = async ([configPath = rt.ctx.configPath,] = []) => {
68
- const result = await readPoolConfig(configPath);
66
+ export function readPoolSettings(rt) {
67
+ return readPoolSettingsAt(rt.ctx.configPath);
68
+ }
69
+ /**
70
+ * Reads the settings of the pool config at `configPath`, exactly as a store's
71
+ * `readSettings()` does, without opening a store. It reads only that file: no
72
+ * state file, lock, write or migration. A plugin that has already resolved
73
+ * which config file to trust (for example, after a location check) reads it
74
+ * here rather than opening a second store on it.
75
+ */
76
+ export async function readPoolSettingsAt(configPath) {
77
+ const read = async ([path = configPath,] = []) => {
78
+ const result = await readPoolConfig(path);
69
79
  if (result.status === 'error')
70
80
  return { status: 'error', file: 'config', reason: result.reason };
71
81
  return { status: result.status, settings: settingsOf(result.config) };
@@ -73,7 +83,7 @@ export async function readPoolSettings(rt) {
73
83
  // Native Windows security checks cannot be replaced by a stat fingerprint.
74
84
  const result = windowsPrivateFiles
75
85
  ? await read()
76
- : await settingsCache.read([rt.ctx.configPath], read);
86
+ : await settingsCache.read([configPath], read);
77
87
  return structuredClone(result);
78
88
  }
79
89
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/common-auth",
3
- "version": "0.49.2",
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": {