@cortexkit/common-auth 0.21.1 → 0.22.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/auth-menu/login.d.ts +6 -10
- package/dist/auth-menu/login.js +14 -11
- package/dist/claustrum/consumer.d.ts +5 -1
- package/dist/claustrum/consumer.js +40 -1
- package/dist/claustrum/index.d.ts +1 -1
- package/dist/claustrum/insula.d.ts +23 -0
- package/dist/claustrum/insula.js +64 -2
- package/package.json +1 -1
|
@@ -26,28 +26,24 @@ export interface MenuLogin {
|
|
|
26
26
|
signal?: AbortSignal;
|
|
27
27
|
}): Promise<LoginFlow>;
|
|
28
28
|
/**
|
|
29
|
-
* Opens the URL in a browser; false or a throw means
|
|
30
|
-
*
|
|
29
|
+
* Opens the URL in a browser; false or a throw means its opener could not be
|
|
30
|
+
* started. Defaults to `openBrowserForMenu`.
|
|
31
31
|
*/
|
|
32
32
|
openBrowser?(url: string): boolean | undefined | Promise<boolean | undefined>;
|
|
33
33
|
}
|
|
34
|
-
type BrowserExec = (file: string, args: string[], options: {
|
|
35
|
-
stdio: 'ignore';
|
|
36
|
-
timeout: number;
|
|
37
|
-
}) => unknown;
|
|
38
34
|
type BrowserSpawn = (file: string, args: string[], options: {
|
|
39
35
|
stdio: 'ignore';
|
|
40
|
-
windowsHide
|
|
36
|
+
windowsHide?: true;
|
|
41
37
|
}) => {
|
|
42
38
|
pid?: number;
|
|
43
39
|
unref(): void;
|
|
44
40
|
on(event: 'error', listener: () => void): unknown;
|
|
45
41
|
};
|
|
46
|
-
/** Opens a URL with the platform's opener; false when
|
|
47
|
-
export declare function openBrowserForMenu(url: string, platform?: NodeJS.Platform,
|
|
42
|
+
/** Opens a URL with the platform's opener; false when it could not start. */
|
|
43
|
+
export declare function openBrowserForMenu(url: string, platform?: NodeJS.Platform, spawn?: BrowserSpawn): boolean;
|
|
48
44
|
/**
|
|
49
45
|
* Runs a login for the menu: a browser login first and, when no browser can
|
|
50
|
-
* be
|
|
46
|
+
* be started, a device-code login instead, so a headless machine can still
|
|
51
47
|
* add an account. The URL is always printed, so an operator can open it by
|
|
52
48
|
* hand when the opener reports success but nothing appears.
|
|
53
49
|
*/
|
package/dist/auth-menu/login.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
/** Opens a URL with the platform's opener; false when
|
|
3
|
-
export function openBrowserForMenu(url, platform = process.platform,
|
|
1
|
+
import { spawn as defaultSpawn } from 'node:child_process';
|
|
2
|
+
/** Opens a URL with the platform's opener; false when it could not start. */
|
|
3
|
+
export function openBrowserForMenu(url, platform = process.platform, spawn = defaultSpawn) {
|
|
4
4
|
try {
|
|
5
5
|
if (platform === 'win32') {
|
|
6
6
|
// Start-Process opens the URL with its associated app. Base64 UTF-16LE
|
|
@@ -28,15 +28,18 @@ export function openBrowserForMenu(url, platform = process.platform, execFileSyn
|
|
|
28
28
|
// Node leaves pid undefined when the process could not be started.
|
|
29
29
|
return child.pid !== undefined;
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
31
|
+
const child = spawn(platform === 'darwin' ? 'open' : 'xdg-open', [url], {
|
|
32
|
+
stdio: 'ignore',
|
|
33
|
+
});
|
|
34
|
+
// A failed launch reports asynchronously; without a listener it would
|
|
35
|
+
// become an uncaught exception after this function has returned.
|
|
36
|
+
child.on('error', () => { });
|
|
37
|
+
child.unref();
|
|
38
|
+
// The URL handler's process may outlive the caller; a pid means launch began.
|
|
39
|
+
return child.pid !== undefined;
|
|
38
40
|
}
|
|
39
41
|
catch {
|
|
42
|
+
// Browser launch is best effort; the printed URL remains the fallback.
|
|
40
43
|
return false;
|
|
41
44
|
}
|
|
42
45
|
}
|
|
@@ -53,7 +56,7 @@ function printFlow(context, flow) {
|
|
|
53
56
|
}
|
|
54
57
|
/**
|
|
55
58
|
* Runs a login for the menu: a browser login first and, when no browser can
|
|
56
|
-
* be
|
|
59
|
+
* be started, a device-code login instead, so a headless machine can still
|
|
57
60
|
* add an account. The URL is always printed, so an operator can open it by
|
|
58
61
|
* hand when the opener reports success but nothing appears.
|
|
59
62
|
*/
|
|
@@ -3,7 +3,7 @@ import type { QuotaObservation } from '../quota/index.js';
|
|
|
3
3
|
import type { RoutingRow } from '../routing/index.js';
|
|
4
4
|
import { type ClaustrumFamily, type ClaustrumScopedAttempt, type ClaustrumScopedClient, type IdentityParser, type KeptReceiptState, type VaultGrantReach } from './custody.js';
|
|
5
5
|
import { type ClaustrumLogger } from './errors.js';
|
|
6
|
-
import { type InsulaQuotaOptions } from './insula.js';
|
|
6
|
+
import { type InsulaQuotaOptions, type InsulaQuotaResult } from './insula.js';
|
|
7
7
|
import { type AccountMapper, type QuotaReceipt, type VaultRosterFile } from './roster.js';
|
|
8
8
|
import { type WatchMode } from './watch.js';
|
|
9
9
|
export interface ClaustrumConsumerOptions<Q = QuotaObservation> {
|
|
@@ -134,6 +134,10 @@ export declare class ClaustrumConsumer<Q = QuotaObservation> {
|
|
|
134
134
|
* A route without a known provider account cannot join an insula entry.
|
|
135
135
|
*/
|
|
136
136
|
cachedInsulaQuotaForRoute(routeId: string): Promise<Q | undefined>;
|
|
137
|
+
/** Cache-only background lookup, preserving the reason a route has no reading. */
|
|
138
|
+
lookupInsulaQuotaForRoute(routeId: string): Promise<InsulaQuotaResult<Q>>;
|
|
139
|
+
/** Skip the fallback poll while insula is cold or its upstream fetch failed. */
|
|
140
|
+
pollInsulaQuotaForRoute(routeId: string, fallback: () => Promise<Q | undefined>): Promise<Q | undefined>;
|
|
137
141
|
/**
|
|
138
142
|
* Authorize one physical send on a vault route. The roster file is re-read
|
|
139
143
|
* first, so a decline committed by another process applies before the next
|
|
@@ -3,7 +3,7 @@ import { combineAbortSignals } from '../util/any-signal.js';
|
|
|
3
3
|
import { ClaustrumScopedCustody, decideScopedRetryAfter401, } from './custody.js';
|
|
4
4
|
import { readClaustrumEnrollmentToken } from './enrollment.js';
|
|
5
5
|
import { ClaustrumConsumerError } from './errors.js';
|
|
6
|
-
import { acquireInsulaQuotaSource } from './insula.js';
|
|
6
|
+
import { acquireInsulaQuotaSource, } from './insula.js';
|
|
7
7
|
import { acceptVaultRoute, declineVaultRoute, readVaultRoster, recordVaultAccountQuota, recordVaultQuota, refreshVaultRoster, vaultRoutingRows, } from './roster.js';
|
|
8
8
|
import { SAFETY_POLL_MS } from './watch.js';
|
|
9
9
|
/**
|
|
@@ -32,6 +32,7 @@ export class ClaustrumConsumer {
|
|
|
32
32
|
#insula;
|
|
33
33
|
#vaultWatch;
|
|
34
34
|
#releaseInsulaWatch;
|
|
35
|
+
#insulaSkips = new Map();
|
|
35
36
|
/** When this instance last completed a discovery, by `now`. */
|
|
36
37
|
#discoveredAt;
|
|
37
38
|
constructor(options) {
|
|
@@ -356,6 +357,44 @@ export class ClaustrumConsumer {
|
|
|
356
357
|
return undefined;
|
|
357
358
|
}
|
|
358
359
|
}
|
|
360
|
+
/** Cache-only background lookup, preserving the reason a route has no reading. */
|
|
361
|
+
async lookupInsulaQuotaForRoute(routeId) {
|
|
362
|
+
const options = this.#options.insula;
|
|
363
|
+
if (!options || this.#shutdown.signal.aborted || !(await this.#active()))
|
|
364
|
+
return { miss: 'unreachable' };
|
|
365
|
+
try {
|
|
366
|
+
await readClaustrumEnrollmentToken(this.#options.tokenPath);
|
|
367
|
+
const roster = await readVaultRoster(this.#options.rosterPath);
|
|
368
|
+
const account = roster?.rows.find((row) => row.routeId === routeId)?.accountIdentity;
|
|
369
|
+
if (!account || !this.#insula)
|
|
370
|
+
return { miss: 'unreachable' };
|
|
371
|
+
const result = this.#insula.source.lookup(options.provider, account, (this.#options.now ?? Date.now)());
|
|
372
|
+
const reading = result.reading &&
|
|
373
|
+
options.normalize(result.reading.entry, result.reading.context);
|
|
374
|
+
if (result.miss)
|
|
375
|
+
return { miss: result.miss, ...(reading !== undefined && { reading }) };
|
|
376
|
+
// Refusing an echo or unsupported window leaves the plugin's poll in charge.
|
|
377
|
+
return reading === undefined ? { miss: 'unreachable' } : { reading };
|
|
378
|
+
}
|
|
379
|
+
catch {
|
|
380
|
+
return { miss: 'unreachable' };
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
/** Skip the fallback poll while insula is cold or its upstream fetch failed. */
|
|
384
|
+
async pollInsulaQuotaForRoute(routeId, fallback) {
|
|
385
|
+
const result = await this.lookupInsulaQuotaForRoute(routeId);
|
|
386
|
+
if (result.miss === 'cold' || result.miss === 'upstream-failed') {
|
|
387
|
+
if (this.#insulaSkips.get(routeId) !== result.miss) {
|
|
388
|
+
this.#insulaSkips.set(routeId, result.miss);
|
|
389
|
+
this.#logger.warn('skipped insula provider poll', {
|
|
390
|
+
reason: result.miss,
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
return undefined;
|
|
394
|
+
}
|
|
395
|
+
this.#insulaSkips.delete(routeId);
|
|
396
|
+
return result.miss ? fallback() : result.reading;
|
|
397
|
+
}
|
|
359
398
|
#normalizeInsulaQuota(accountIdentity) {
|
|
360
399
|
if (!this.#options.insula || !accountIdentity)
|
|
361
400
|
return undefined;
|
|
@@ -7,7 +7,7 @@ export { type AccountIdentitySource, type ClaustrumFamily, type ClaustrumRosterW
|
|
|
7
7
|
export { type ClaustrumEnrollmentClient, type ClaustrumEnrollmentConnection, type ClaustrumEnrollmentDisconnectResult, ClaustrumEnrollmentManager, type ClaustrumEnrollmentPaths, type ClaustrumEnrollmentResetResult, type ClaustrumEnrollmentStatus, classifyEnrollmentError, connectClaustrumEnrollmentClient, disconnectClaustrumEnrollment, type EnrollmentDisposition, enrollmentAuthority, enrollmentName, getClaustrumEnrollmentPaths, hostEnrollmentPaths, RETRYABLE_ENROLLMENT_CODES, readClaustrumEnrollmentStatus, readClaustrumEnrollmentToken, resetClaustrumEnrollmentState, TERMINAL_ENROLLMENT_CODES, } from './enrollment.js';
|
|
8
8
|
export { ClaustrumConsumerError, type ClaustrumConsumerFailureKind, type ClaustrumLogger, } from './errors.js';
|
|
9
9
|
export { assertHostSlotMatchesMode, assertNotCustodyPlaceholder, CUSTODY_PLACEHOLDER_PREFIX, classifyHostSlot, custodyPlaceholder, custodyPlaceholderKey, type HostSlotContent, isCustodyPlaceholder, isCustodyPlaceholderValue, } from './host-slot.js';
|
|
10
|
-
export { acquireInsulaQuotaSource, type InsulaQuotaContext, type InsulaQuotaOptions, type InsulaQuotaSource, type InsulaUsageEntry, type InsulaUsageTransport, type InsulaUsageWatch, type InsulaUsageWindow, type InsulaWatchOptions, } from './insula.js';
|
|
10
|
+
export { acquireInsulaQuotaSource, type InsulaQuotaContext, type InsulaQuotaLookup, type InsulaQuotaMiss, type InsulaQuotaOptions, type InsulaQuotaReading, type InsulaQuotaResult, type InsulaQuotaSource, type InsulaUsageEntry, type InsulaUsageTransport, type InsulaUsageWatch, type InsulaUsageWindow, type InsulaWatchOptions, } from './insula.js';
|
|
11
11
|
export { acceptAccount, type DeclinedAccount, declineAccount, isDeclined, } from './interlock.js';
|
|
12
12
|
export { type AccountMapper, acceptVaultRoute, DEFAULT_ROUTE_PREFIX, declineVaultRoute, type MutateVaultRosterOptions, mutateVaultRoster, type ProjectionOptions, projectVaultRoster, type QuotaReceipt, readVaultRoster, recordVaultAccountQuota, recordVaultQuota, refreshVaultRoster, resolveVaultPrimary, type VaultPrimary, type VaultPrimaryBinding, type VaultPrimaryUnavailableReason, type VaultRosterFile, type VaultRosterLockContext, type VaultRosterRow, vaultRoutingRows, } from './roster.js';
|
|
13
13
|
export { type ChangeNotice, ChangeNoticeReader, RESUBSCRIBE_BACKOFF_MS, SAFETY_POLL_MS, ScopedRosterWatch, type ScopedRosterWatchOptions, type ScopedWatchEnd, type ScopedWatchHandle, type ScopedWatchNoticeShape, type ScopedWatchSource, WATCH_RETRY_MS, type WatchMode, } from './watch.js';
|
|
@@ -34,6 +34,27 @@ export interface InsulaQuotaContext {
|
|
|
34
34
|
fetchedAt: number;
|
|
35
35
|
source?: string;
|
|
36
36
|
}
|
|
37
|
+
export type InsulaQuotaMiss = 'cold' | 'stale' | 'absent' | 'upstream-failed' | 'credential' | 'unreachable';
|
|
38
|
+
export interface InsulaQuotaReading {
|
|
39
|
+
entry: InsulaUsageEntry;
|
|
40
|
+
context: InsulaQuotaContext;
|
|
41
|
+
}
|
|
42
|
+
/** A miss may retain a dated reading; it is not a current quota observation. */
|
|
43
|
+
export type InsulaQuotaLookup = {
|
|
44
|
+
reading: InsulaQuotaReading;
|
|
45
|
+
miss?: never;
|
|
46
|
+
} | {
|
|
47
|
+
miss: InsulaQuotaMiss;
|
|
48
|
+
reading?: InsulaQuotaReading;
|
|
49
|
+
};
|
|
50
|
+
/** The normalized equivalent of a source lookup, for a plugin's quota type. */
|
|
51
|
+
export type InsulaQuotaResult<Q> = {
|
|
52
|
+
reading: Q;
|
|
53
|
+
miss?: never;
|
|
54
|
+
} | {
|
|
55
|
+
miss: InsulaQuotaMiss;
|
|
56
|
+
reading?: Q;
|
|
57
|
+
};
|
|
37
58
|
export interface InsulaQuotaOptions<Q> {
|
|
38
59
|
provider: 'codex' | 'claude' | 'antigravity';
|
|
39
60
|
/**
|
|
@@ -110,6 +131,8 @@ export declare class InsulaQuotaSource {
|
|
|
110
131
|
entry: InsulaUsageEntry;
|
|
111
132
|
context: InsulaQuotaContext;
|
|
112
133
|
} | undefined;
|
|
134
|
+
/** Cache-only background lookup; current() retains its original miss behavior. */
|
|
135
|
+
lookup(provider: string, accountId: string, now: number): InsulaQuotaLookup;
|
|
113
136
|
}
|
|
114
137
|
export declare function acquireInsulaQuotaSource(transport: InsulaUsageTransport, tokenPath: string): {
|
|
115
138
|
source: InsulaQuotaSource;
|
package/dist/claustrum/insula.js
CHANGED
|
@@ -128,6 +128,9 @@ export function insulaUsageTransport(options) {
|
|
|
128
128
|
}
|
|
129
129
|
const MINUTE = 60_000;
|
|
130
130
|
const MAX_AGE = 4 * MINUTE;
|
|
131
|
+
// consumer-contract.md:331 refetches a slot after 60 s. Two such intervals
|
|
132
|
+
// bound our willingness to wait, not a claim that insula completed its sweep.
|
|
133
|
+
const COLD_WAIT = 2 * MINUTE;
|
|
131
134
|
const sources = new Map();
|
|
132
135
|
// Keep only the minute budget after the last owner closes, so project churn
|
|
133
136
|
// cannot reset the bound. Expired budgets are pruned on the next tick.
|
|
@@ -170,6 +173,9 @@ export class InsulaQuotaSource {
|
|
|
170
173
|
transport;
|
|
171
174
|
key;
|
|
172
175
|
#entries = [];
|
|
176
|
+
#available = false;
|
|
177
|
+
#completeProviders = new Set();
|
|
178
|
+
#coldSince = new Map();
|
|
173
179
|
/** Every usage.get, polled or watch-driven, shares this one slot. */
|
|
174
180
|
#reads = new CoalescedRead(async () => {
|
|
175
181
|
try {
|
|
@@ -178,13 +184,25 @@ export class InsulaQuotaSource {
|
|
|
178
184
|
!Array.isArray(reply.result) ||
|
|
179
185
|
reply.error !== undefined)
|
|
180
186
|
throw new Error('unavailable insula usage');
|
|
181
|
-
// Each response is a snapshot, not a delta.
|
|
182
|
-
// neither pending nor the entry count
|
|
187
|
+
// Each response is a snapshot, not a delta. Only completeProviders
|
|
188
|
+
// asserts completeness; neither pending nor the entry count does.
|
|
183
189
|
this.#entries = reply.result.filter((entry) => record(entry) && typeof entry.provider === 'string');
|
|
190
|
+
this.#available =
|
|
191
|
+
this.#entries.length === reply.result.length &&
|
|
192
|
+
(reply.completeProviders === undefined ||
|
|
193
|
+
(Array.isArray(reply.completeProviders) &&
|
|
194
|
+
reply.completeProviders.every((value) => typeof value === 'string')));
|
|
195
|
+
this.#completeProviders = new Set(this.#available && Array.isArray(reply.completeProviders)
|
|
196
|
+
? reply.completeProviders
|
|
197
|
+
: []);
|
|
198
|
+
for (const provider of this.#completeProviders)
|
|
199
|
+
this.#coldSince.delete(provider);
|
|
184
200
|
}
|
|
185
201
|
catch {
|
|
186
202
|
// Refusal is absence, even if the previous reading was still young.
|
|
187
203
|
this.#entries = [];
|
|
204
|
+
this.#available = false;
|
|
205
|
+
this.#completeProviders.clear();
|
|
188
206
|
}
|
|
189
207
|
}, () => { });
|
|
190
208
|
#reader = new ChangeNoticeReader();
|
|
@@ -391,6 +409,50 @@ export class InsulaQuotaSource {
|
|
|
391
409
|
}
|
|
392
410
|
return undefined;
|
|
393
411
|
}
|
|
412
|
+
/** Cache-only background lookup; current() retains its original miss behavior. */
|
|
413
|
+
lookup(provider, accountId, now) {
|
|
414
|
+
if (!this.#available)
|
|
415
|
+
return { miss: 'unreachable' };
|
|
416
|
+
const entry = this.#entries.find((value) => value.provider === provider &&
|
|
417
|
+
typeof value.account === 'string' &&
|
|
418
|
+
value.account.trim() === accountId);
|
|
419
|
+
// An unlabelled error describes the provider's credential path, not an
|
|
420
|
+
// unrelated labelled account. Never borrow another account's verdict.
|
|
421
|
+
const errorClass = (entry ??
|
|
422
|
+
this.#entries.find((value) => value.provider === provider &&
|
|
423
|
+
value.account === undefined &&
|
|
424
|
+
value.errorClass !== undefined))?.errorClass;
|
|
425
|
+
if (errorClass === 'upstream_failed')
|
|
426
|
+
return { miss: 'upstream-failed' };
|
|
427
|
+
if (errorClass === 'credential_absent' ||
|
|
428
|
+
errorClass === 'credential_unusable' ||
|
|
429
|
+
errorClass === 'credential_rejected')
|
|
430
|
+
return { miss: 'credential' };
|
|
431
|
+
if (errorClass !== undefined)
|
|
432
|
+
return { miss: 'unreachable' };
|
|
433
|
+
if (entry) {
|
|
434
|
+
const fetchedAt = typeof entry.fetchedAt === 'string'
|
|
435
|
+
? Date.parse(entry.fetchedAt)
|
|
436
|
+
: Number.NaN;
|
|
437
|
+
if (!record(entry.usage) || !Number.isFinite(fetchedAt))
|
|
438
|
+
return { miss: 'unreachable' };
|
|
439
|
+
const reading = {
|
|
440
|
+
entry,
|
|
441
|
+
context: {
|
|
442
|
+
fetchedAt,
|
|
443
|
+
...(typeof entry.source === 'string' && { source: entry.source }),
|
|
444
|
+
},
|
|
445
|
+
};
|
|
446
|
+
return now >= fetchedAt + MAX_AGE || entry.stale !== undefined
|
|
447
|
+
? { miss: 'stale', reading }
|
|
448
|
+
: { reading };
|
|
449
|
+
}
|
|
450
|
+
if (this.#completeProviders.has(provider))
|
|
451
|
+
return { miss: 'absent' };
|
|
452
|
+
const since = this.#coldSince.get(provider) ?? now;
|
|
453
|
+
this.#coldSince.set(provider, since);
|
|
454
|
+
return { miss: now - since >= COLD_WAIT ? 'unreachable' : 'cold' };
|
|
455
|
+
}
|
|
394
456
|
}
|
|
395
457
|
export function acquireInsulaQuotaSource(transport, tokenPath) {
|
|
396
458
|
const key = JSON.stringify([
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cortexkit/common-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.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": {
|