@cortexkit/common-auth 0.45.0 → 0.47.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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
2
|
import { link, lstat, mkdir, open, readdir, readFile, rename, rm, stat, unlink, } from 'node:fs/promises';
|
|
3
3
|
import { basename, dirname, join } from 'node:path';
|
|
4
|
+
import { createLogger } from '../logger/index.js';
|
|
4
5
|
import { DisappearedLockRecordError, isWindowsAccessDeniedCreate, isWindowsAccessDeniedRead, readLockRecord, } from './lock-record.js';
|
|
5
6
|
import { createPrivateBytes, createPrivateDirectoryExclusive, ensurePrivateDirectory, isWindowsSharingViolation, PrivateFileError, privateOperation, readPrivateText, verifyPrivate, windowsPrivateFiles, writePrivateAtomic, } from './private-files.js';
|
|
6
7
|
import { LockOwnershipError, lockPathFor } from './with-lock.js';
|
|
@@ -1191,17 +1192,17 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
1191
1192
|
removalConfirmed = true;
|
|
1192
1193
|
}, deadline);
|
|
1193
1194
|
changingRecordError = undefined;
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
});
|
|
1199
|
-
releaseConfirmed = removalConfirmed;
|
|
1195
|
+
// Acquiring the marker does not confirm removal: stale-marker
|
|
1196
|
+
// recovery can take it away after the lease owner was checked.
|
|
1197
|
+
if (markerAcquired && removalConfirmed) {
|
|
1198
|
+
releaseConfirmed = true;
|
|
1200
1199
|
return;
|
|
1201
1200
|
}
|
|
1202
|
-
if (
|
|
1203
|
-
|
|
1204
|
-
|
|
1201
|
+
if (!markerAcquired) {
|
|
1202
|
+
if (signal?.aborted)
|
|
1203
|
+
return;
|
|
1204
|
+
await recoverStaleEvictionMarker(deadline);
|
|
1205
|
+
}
|
|
1205
1206
|
}
|
|
1206
1207
|
catch (error) {
|
|
1207
1208
|
const changingRecord = windowsPrivateFiles &&
|
|
@@ -1242,6 +1243,9 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
1242
1243
|
new PrivateFileError(lockPath, 'release-not-confirmed', {
|
|
1243
1244
|
code: 'ETIMEDOUT',
|
|
1244
1245
|
}));
|
|
1246
|
+
createLogger('fs').warn('File lock release deadline expired', {
|
|
1247
|
+
lockPath,
|
|
1248
|
+
});
|
|
1245
1249
|
},
|
|
1246
1250
|
};
|
|
1247
1251
|
}
|
|
@@ -12,14 +12,16 @@ export interface RoutingCandidate {
|
|
|
12
12
|
accountId: string;
|
|
13
13
|
credentialId: string;
|
|
14
14
|
}
|
|
15
|
-
export interface AskRoutingInput<Candidate extends RoutingCandidate = RoutingCandidate> {
|
|
15
|
+
export interface AskRoutingInput<Candidate extends RoutingCandidate = RoutingCandidate, Refusals extends boolean = false> {
|
|
16
16
|
sessionId: string;
|
|
17
17
|
provider: string;
|
|
18
18
|
modelId: string;
|
|
19
19
|
/** Only routes already eligible under the plugin's vault and killswitch rules. */
|
|
20
20
|
candidates: readonly Candidate[];
|
|
21
|
-
/** Defaults to
|
|
21
|
+
/** Defaults to 1500 ms, above routing's normal tail; clamped to 1–2000 ms. */
|
|
22
22
|
deadlineMs?: number;
|
|
23
|
+
/** Opt in to refusals, which stop the request instead of using a local choice. */
|
|
24
|
+
refusals?: Refusals;
|
|
23
25
|
/**
|
|
24
26
|
* Longest same-account hold, in seconds, that routing may advise. The
|
|
25
27
|
* holdWaitMaxS helper from `./limits` caps it by the setting and the current
|
|
@@ -38,6 +40,14 @@ export interface RoutingSelection<Candidate extends RoutingCandidate = RoutingCa
|
|
|
38
40
|
candidate: Candidate;
|
|
39
41
|
hold?: RoutingHold;
|
|
40
42
|
}
|
|
43
|
+
export type RoutingRefusal = {
|
|
44
|
+
refused: {
|
|
45
|
+
code: 'account_limit_exhausted' | 'account_selection_refused' | 'account_selection_final';
|
|
46
|
+
routingCode?: string;
|
|
47
|
+
retryAfterSecs?: number;
|
|
48
|
+
resetsAtMs?: number;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
41
51
|
interface RoutingReportIdentity {
|
|
42
52
|
sessionId: string;
|
|
43
53
|
accountId: string;
|
|
@@ -77,6 +87,23 @@ export type ReportRoutingInput = (RoutingReportIdentity & ({
|
|
|
77
87
|
pool?: string;
|
|
78
88
|
/** Optional reset time in epoch milliseconds. */
|
|
79
89
|
resetsAt?: number;
|
|
90
|
+
} | {
|
|
91
|
+
/**
|
|
92
|
+
* A provider's typed refusal that condemns the API key until it is
|
|
93
|
+
* topped up, such as an exhausted prepaid balance. Never use for
|
|
94
|
+
* 401, 403 or short rate limits.
|
|
95
|
+
*/
|
|
96
|
+
outcome: 'credential_refused';
|
|
97
|
+
credentialId: string;
|
|
98
|
+
/**
|
|
99
|
+
* What the provider actually sent: its own code from the structured
|
|
100
|
+
* error (OpenRouter's `openrouter_credits`). When the body carries
|
|
101
|
+
* no code, `http_<status>`, but only for a status the provider
|
|
102
|
+
* documents with a single balance meaning (DeepSeek's `http_402`).
|
|
103
|
+
* Never message text, and never a status with several meanings such
|
|
104
|
+
* as 403 or 429. Must match `^[a-z0-9._-]{1,64}$`.
|
|
105
|
+
*/
|
|
106
|
+
providerCode: string;
|
|
80
107
|
})) | {
|
|
81
108
|
sessionId: string;
|
|
82
109
|
kind: 'turn_notes_supported';
|
|
@@ -128,10 +155,11 @@ export declare function turnNotes(sessionId: string): readonly {
|
|
|
128
155
|
}[];
|
|
129
156
|
/**
|
|
130
157
|
* Ask routing for an eligible vault route. Any failure or unfamiliar answer
|
|
131
|
-
* yields undefined so the plugin can keep its own choice.
|
|
132
|
-
*
|
|
158
|
+
* yields undefined so the plugin can keep its own choice. Opted-in routing
|
|
159
|
+
* refusals instead stop the request. This does not admit routes: callers must
|
|
160
|
+
* exclude vault refusals and killswitch-blocked accounts.
|
|
133
161
|
*/
|
|
134
|
-
export declare function askRouting<Candidate extends RoutingCandidate>(input: AskRoutingInput<Candidate>): Promise<RoutingSelection<Candidate> | undefined>;
|
|
162
|
+
export declare function askRouting<Candidate extends RoutingCandidate, Refusals extends boolean = false>(input: AskRoutingInput<Candidate, Refusals>): Promise<RoutingSelection<Candidate> | (Refusals extends true ? RoutingRefusal : never) | undefined>;
|
|
135
163
|
/**
|
|
136
164
|
* Report a vault request, billing result or turn-note event without waiting for
|
|
137
165
|
* routing. Only contract fields are sent, and all accessor failures are ignored.
|
|
@@ -136,11 +136,35 @@ function selectionFor(answer, candidates) {
|
|
|
136
136
|
}
|
|
137
137
|
return { candidate };
|
|
138
138
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
139
|
+
function refusalFor(value) {
|
|
140
|
+
if (!isObject(value))
|
|
141
|
+
return undefined;
|
|
142
|
+
const code = value.code;
|
|
143
|
+
if (code !== 'account_limit_exhausted' &&
|
|
144
|
+
code !== 'account_selection_refused' &&
|
|
145
|
+
code !== 'account_selection_final') {
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
const refused = { code };
|
|
149
|
+
const routingCode = value.routing_code;
|
|
150
|
+
if (typeof routingCode === 'string' &&
|
|
151
|
+
/^[a-z0-9._-]{1,64}$/.test(routingCode)) {
|
|
152
|
+
refused.routingCode = routingCode;
|
|
153
|
+
}
|
|
154
|
+
const retryAfterSecs = value.retry_after_secs;
|
|
155
|
+
if (typeof retryAfterSecs === 'number' &&
|
|
156
|
+
Number.isFinite(retryAfterSecs) &&
|
|
157
|
+
retryAfterSecs >= 0) {
|
|
158
|
+
refused.retryAfterSecs = retryAfterSecs;
|
|
159
|
+
}
|
|
160
|
+
const resetsAtMs = value.resets_at_ms;
|
|
161
|
+
if (typeof resetsAtMs === 'number' &&
|
|
162
|
+
Number.isSafeInteger(resetsAtMs) &&
|
|
163
|
+
resetsAtMs > 0) {
|
|
164
|
+
refused.resetsAtMs = resetsAtMs;
|
|
165
|
+
}
|
|
166
|
+
return { refused };
|
|
167
|
+
}
|
|
144
168
|
export async function askRouting(input) {
|
|
145
169
|
let timer;
|
|
146
170
|
try {
|
|
@@ -150,9 +174,9 @@ export async function askRouting(input) {
|
|
|
150
174
|
const accountFor = handle.accountFor;
|
|
151
175
|
if (typeof accountFor !== 'function')
|
|
152
176
|
return undefined;
|
|
153
|
-
const requested = input.deadlineMs ??
|
|
177
|
+
const requested = input.deadlineMs ?? 1500;
|
|
154
178
|
const deadlineMs = Number.isNaN(requested)
|
|
155
|
-
?
|
|
179
|
+
? 1500
|
|
156
180
|
: Math.min(2000, Math.max(1, requested));
|
|
157
181
|
const expired = new Promise((resolve) => {
|
|
158
182
|
timer = setTimeout(() => resolve(undefined), deadlineMs);
|
|
@@ -166,10 +190,15 @@ export async function askRouting(input) {
|
|
|
166
190
|
if (typeof cap === 'number' && Number.isFinite(cap)) {
|
|
167
191
|
request.hold_wait_max_s = Math.max(0, Math.floor(cap));
|
|
168
192
|
}
|
|
193
|
+
if (input.refusals === true)
|
|
194
|
+
request.refusals = true;
|
|
169
195
|
const pending = accountFor.call(handle, request, { deadlineMs });
|
|
170
196
|
if (!isObject(pending) || typeof pending.then !== 'function')
|
|
171
197
|
return undefined;
|
|
172
198
|
const answer = await Promise.race([pending, expired]);
|
|
199
|
+
if (input.refusals === true && isObject(answer) && 'refused' in answer) {
|
|
200
|
+
return refusalFor(answer.refused);
|
|
201
|
+
}
|
|
173
202
|
return selectionFor(answer, input.candidates);
|
|
174
203
|
}
|
|
175
204
|
catch {
|
|
@@ -334,6 +363,19 @@ export function reportRouting(input) {
|
|
|
334
363
|
};
|
|
335
364
|
break;
|
|
336
365
|
}
|
|
366
|
+
case 'credential_refused': {
|
|
367
|
+
const { credentialId, providerCode } = input;
|
|
368
|
+
if (!isNonEmptyString(credentialId) ||
|
|
369
|
+
typeof providerCode !== 'string' ||
|
|
370
|
+
!/^[a-z0-9._-]{1,64}$/.test(providerCode))
|
|
371
|
+
return;
|
|
372
|
+
payload = {
|
|
373
|
+
...identity,
|
|
374
|
+
outcome: 'credential_refused',
|
|
375
|
+
provider_code: providerCode,
|
|
376
|
+
};
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
337
379
|
default:
|
|
338
380
|
return;
|
|
339
381
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cortexkit/common-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.47.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": {
|