@cortexkit/pi-anthropic-auth 2.2.8 → 2.4.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/native.d.ts CHANGED
@@ -28,6 +28,7 @@ export interface PiVaultPollOptions {
28
28
  }
29
29
  export declare class PiNativeRuntime {
30
30
  private readonly paths;
31
+ private readonly holderRegistration;
31
32
  private readonly initialization;
32
33
  private readonly env;
33
34
  private vaultWatch;
package/dist/native.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { resolve } from 'node:path';
2
- import { acquireNativeAccountRuntime, createNativeUsagePublisher, getAccountStatePath, getHostClaustrumEnrollmentPaths, isKillswitchEnabled, killswitchPassesPolicy, logger, nativeAccountCanServe, nativeMigrationAuthorityPhase, nativeRuntimeOptionIdentity, quotaSnapshotHasStandardWindows, quotaSnapshotPassesModelScope, quotaSnapshotPassesPolicy, readNativeAccountsForRequest, readNativeMigrationJournal, resolveClaustrumConnectionPath, resolveLocalRoutingDestination, resolveNativePoolPaths, } from '@cortexkit/anthropic-auth-core';
2
+ import { acquireNativeAccountRuntime, createNativeUsagePublisher, getAccountStatePath, getHostClaustrumEnrollmentPaths, isKillswitchEnabled, killswitchPassesPolicy, logger, nativeAccountCanServe, nativeMigrationAuthorityPhase, nativeRuntimeOptionIdentity, quotaSnapshotHasStandardWindows, quotaSnapshotPassesModelScope, quotaSnapshotPassesPolicy, readNativeAccountsForRequest, readNativeMigrationJournal, registerNativePoolHolder, resolveClaustrumConnectionPath, resolveLocalRoutingDestination, resolveNativePoolPaths, } from '@cortexkit/anthropic-auth-core';
3
3
  import { getPiAccountStoragePath, getPiClaustrumConnectionOptions, getPiConfigDir, requirePiNativeHostAuth, } from './paths.js';
4
4
  export function piAttemptAccessToken(attempt) {
5
5
  return attempt.source === 'vault'
@@ -30,6 +30,18 @@ function requestPolicy(routeId, modelId) {
30
30
  }
31
31
  const runtimes = new Map();
32
32
  const RUNTIME_LIMIT = 16;
33
+ const closingRuntimes = new Map();
34
+ function closeRuntime(entry) {
35
+ const closing = entry.runtime.close();
36
+ const pending = closingRuntimes.get(entry.storagePath) ?? new Set();
37
+ closingRuntimes.set(entry.storagePath, pending);
38
+ pending.add(closing);
39
+ void closing.then(() => {
40
+ pending.delete(closing);
41
+ if (!pending.size)
42
+ closingRuntimes.delete(entry.storagePath);
43
+ }, () => { });
44
+ }
33
45
  function runtimeCacheKey(storagePath) {
34
46
  const enrollment = getHostClaustrumEnrollmentPaths('pi');
35
47
  return JSON.stringify([
@@ -60,6 +72,7 @@ export function isNativeMigrationRequired(error) {
60
72
  export const PI_VAULT_POLL_INTERVAL_MS = 2000;
61
73
  export class PiNativeRuntime {
62
74
  paths;
75
+ holderRegistration;
63
76
  initialization;
64
77
  env;
65
78
  vaultWatch;
@@ -82,7 +95,19 @@ export class PiNativeRuntime {
82
95
  const poll = options.vaultPoll ?? { intervalMs: PI_VAULT_POLL_INTERVAL_MS };
83
96
  const identity = getPiClaustrumConnectionOptions(storagePath).identity;
84
97
  this.paths = resolveNativePoolPaths(storagePath);
85
- this.initialization = this.paths.then((paths) => {
98
+ // Record that this process uses these files before any pool I/O, so a
99
+ // later version never moves them while it runs. A refusal (the files
100
+ // were moved, or an override points into a managed folder) fails
101
+ // initialization, which every request then reports.
102
+ this.holderRegistration = this.paths.then((paths) => registerNativePoolHolder({
103
+ paths,
104
+ role: 'pi',
105
+ kind: 'pi',
106
+ onLockEvent: options.local?.onLockEvent,
107
+ }));
108
+ void this.holderRegistration.catch(() => { });
109
+ this.initialization = this.holderRegistration.then(async () => {
110
+ const paths = await this.paths;
86
111
  const runtime = acquireNativeAccountRuntime({
87
112
  paths,
88
113
  host: 'pi',
@@ -278,6 +303,7 @@ export class PiNativeRuntime {
278
303
  close() {
279
304
  this.closed = true;
280
305
  this.vaultWatch?.close();
306
+ void this.holderRegistration.then((registration) => registration.close(), () => { });
281
307
  this.closing ??= (async () => {
282
308
  await this.usagePublications.drain();
283
309
  await this.drainRoutingPublications();
@@ -316,19 +342,22 @@ export function getPiNativeRuntime(storagePath = getPiAccountStoragePath(), opti
316
342
  const oldest = runtimes.keys().next().value;
317
343
  if (oldest === undefined)
318
344
  break;
319
- void runtimes.get(oldest)?.runtime.close();
345
+ const evicted = runtimes.get(oldest);
346
+ if (evicted)
347
+ closeRuntime(evicted);
320
348
  runtimes.delete(oldest);
321
349
  }
322
350
  runtimes.set(key, { contextKey, storagePath: resolve(storagePath), runtime });
323
351
  return runtime;
324
352
  }
325
353
  export function closePiNativeRuntime(storagePath = getPiAccountStoragePath()) {
326
- const closing = [];
327
354
  for (const [key, entry] of runtimes) {
328
355
  if (entry.storagePath !== resolve(storagePath))
329
356
  continue;
330
357
  runtimes.delete(key);
331
- closing.push(entry.runtime.close());
358
+ closeRuntime(entry);
332
359
  }
333
- return Promise.all(closing).then(() => { });
360
+ // Eviction removes cache entries before their quota drain finishes. Shutdown
361
+ // must also join those closes, not only the runtimes still in the cache.
362
+ return Promise.all(closingRuntimes.get(resolve(storagePath)) ?? []).then(() => { });
334
363
  }
package/dist/stream.js CHANGED
@@ -114,6 +114,7 @@ async function getPiStickyRouter(storagePath) {
114
114
  path,
115
115
  vaultSeed,
116
116
  onLockEvent: runtime.onLockEvent,
117
+ relocationGuard: runtime.relocationGuard,
117
118
  });
118
119
  setBoundedService(stickyRouters, path, router);
119
120
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/pi-anthropic-auth",
3
- "version": "2.2.8",
3
+ "version": "2.4.0",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,7 +36,7 @@
36
36
  "prepublishOnly": "bun run build"
37
37
  },
38
38
  "dependencies": {
39
- "@cortexkit/anthropic-auth-core": "2.2.8"
39
+ "@cortexkit/anthropic-auth-core": "2.4.0"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@earendil-works/pi-ai": ">=0.86.1 <2",