@cortexkit/opencode-antigravity-auth 3.1.7 → 3.1.9
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/cli.js +182 -193
- package/dist/cli.js.map +4 -4
- package/dist/cli.meta.json +83 -54
- package/dist/index.js +6886 -6346
- package/dist/index.js.map +4 -4
- package/dist/index.meta.json +475 -387
- package/dist/server.js +3056 -2446
- package/dist/server.js.map +4 -4
- package/dist/server.meta.json +1 -1
- package/dist/src/common-auth-embedded/claustrum/availability.js +1 -1
- package/dist/src/common-auth-embedded/claustrum/custody.js +1 -1
- package/dist/src/common-auth-embedded/fs/private-files.js +14 -5
- package/dist/src/common-auth-embedded/opencode2/index.d.ts +1 -1
- package/dist/src/common-auth-embedded/opencode2/index.js +1 -1
- package/dist/src/common-auth-embedded/opencode2/install.d.ts +6 -1
- package/dist/src/common-auth-embedded/opencode2/install.js +14 -4
- package/dist/src/common-auth-embedded/opencode2/types.d.ts +10 -7
- package/dist/src/common-auth-embedded/pi-slot/index.d.ts +1 -1
- package/dist/src/common-auth-embedded/pi-slot/index.js +1 -1
- package/dist/src/common-auth-embedded/pi-slot/slot.d.ts +6 -9
- package/dist/src/common-auth-embedded/pi-slot/slot.js +42 -105
- package/dist/src/common-auth-embedded/routing-accessor/index.d.ts +66 -16
- package/dist/src/common-auth-embedded/routing-accessor/index.js +123 -17
- package/dist/src/common-auth-embedded/rpc/index.d.ts +1 -1
- package/dist/src/common-auth-embedded/rpc/index.js +1 -1
- package/dist/src/common-auth-embedded/rpc/port-file.d.ts +4 -0
- package/dist/src/common-auth-embedded/rpc/port-file.js +55 -49
- package/dist/src/common-auth-embedded/rpc/rpc-server.js +3 -2
- package/dist/src/common-auth-embedded/rpc/startup-sweep.js +32 -0
- package/dist/src/common-auth-embedded/source-output.json +77 -68
- package/dist/src/common-auth-embedded/store/mutate.js +34 -13
- package/dist/src/ga/server/index.d.ts +1 -1
- package/dist/src/ga/server/index.d.ts.map +1 -1
- package/dist/src/ga/server/index.js +58 -19
- package/dist/src/ga/server/index.js.map +1 -1
- package/dist/src/plugin/__fixtures__/verified-store.d.ts +1 -1
- package/dist/src/plugin/__fixtures__/verified-store.js +3 -3
- package/dist/src/plugin/auth-loader.d.ts.map +1 -1
- package/dist/src/plugin/auth-loader.js +102 -80
- package/dist/src/plugin/auth-loader.js.map +1 -1
- package/dist/src/plugin/fetch-interceptor.d.ts.map +1 -1
- package/dist/src/plugin/fetch-interceptor.js +27 -13
- package/dist/src/plugin/fetch-interceptor.js.map +1 -1
- package/dist/src/plugin/ga-location-services.d.ts.map +1 -1
- package/dist/src/plugin/ga-location-services.js +21 -11
- package/dist/src/plugin/ga-location-services.js.map +1 -1
- package/dist/src/plugin/index.d.ts.map +1 -1
- package/dist/src/plugin/index.js +13 -5
- package/dist/src/plugin/index.js.map +1 -1
- package/dist/src/plugin/request.d.ts.map +1 -1
- package/dist/src/plugin/request.js +2 -2
- package/dist/src/plugin/request.js.map +1 -1
- package/dist/src/plugin/shared/opencode-retry-hints.d.ts +2 -0
- package/dist/src/plugin/shared/opencode-retry-hints.d.ts.map +1 -0
- package/dist/src/plugin/shared/opencode-retry-hints.js +22 -0
- package/dist/src/plugin/shared/opencode-retry-hints.js.map +1 -0
- package/dist/src/plugin/shared/request-services.d.ts +2 -0
- package/dist/src/plugin/shared/request-services.d.ts.map +1 -1
- package/dist/src/plugin/shared/request-services.js +410 -112
- package/dist/src/plugin/shared/request-services.js.map +1 -1
- package/dist/src/plugin/shared/vault-routing-purpose.d.ts +6 -2
- package/dist/src/plugin/shared/vault-routing-purpose.d.ts.map +1 -1
- package/dist/src/plugin/shared/vault-routing-purpose.js +13 -7
- package/dist/src/plugin/shared/vault-routing-purpose.js.map +1 -1
- package/dist/src/plugin/shared/vault-routing.d.ts +8 -1
- package/dist/src/plugin/shared/vault-routing.d.ts.map +1 -1
- package/dist/src/plugin/shared/vault-routing.js +12 -4
- package/dist/src/plugin/shared/vault-routing.js.map +1 -1
- package/dist/src/plugin/storage.d.ts +1 -0
- package/dist/src/plugin/storage.d.ts.map +1 -1
- package/dist/src/plugin/storage.js +3 -3
- package/dist/src/plugin/storage.js.map +1 -1
- package/dist/src/plugin/vault-custody.d.ts +6 -13
- package/dist/src/plugin/vault-custody.d.ts.map +1 -1
- package/dist/src/plugin/vault-custody.js +19 -16
- package/dist/src/plugin/vault-custody.js.map +1 -1
- package/dist/src/runtime-layout.d.ts.map +1 -1
- package/dist/src/runtime-layout.js +5 -2
- package/dist/src/runtime-layout.js.map +1 -1
- package/dist/src/setup.d.ts.map +1 -1
- package/dist/src/setup.js +2 -7
- package/dist/src/setup.js.map +1 -1
- package/dist/tui-build-map.json +9 -9
- package/dist/tui-ga-build-map.json +1 -1
- package/dist/types/src/common-auth-embedded/opencode2/index.d.ts +1 -1
- package/dist/types/src/common-auth-embedded/opencode2/install.d.ts +6 -1
- package/dist/types/src/common-auth-embedded/opencode2/types.d.ts +10 -7
- package/dist/types/src/common-auth-embedded/routing-accessor/index.d.ts +66 -16
- package/dist/types/src/ga/server/index.d.ts +1 -1
- package/dist/types/src/ga/server/index.d.ts.map +1 -1
- package/dist/types/src/plugin/auth-loader.d.ts.map +1 -1
- package/dist/types/src/plugin/fetch-interceptor.d.ts.map +1 -1
- package/dist/types/src/plugin/ga-location-services.d.ts.map +1 -1
- package/dist/types/src/plugin/index.d.ts.map +1 -1
- package/dist/types/src/plugin/request.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/opencode-retry-hints.d.ts +2 -0
- package/dist/types/src/plugin/shared/opencode-retry-hints.d.ts.map +1 -0
- package/dist/types/src/plugin/shared/request-services.d.ts +2 -0
- package/dist/types/src/plugin/shared/request-services.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/vault-routing-purpose.d.ts +6 -2
- package/dist/types/src/plugin/shared/vault-routing-purpose.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/vault-routing.d.ts +8 -1
- package/dist/types/src/plugin/shared/vault-routing.d.ts.map +1 -1
- package/dist/types/src/plugin/storage.d.ts +1 -0
- package/dist/types/src/plugin/storage.d.ts.map +1 -1
- package/dist/types/src/plugin/vault-custody.d.ts +6 -13
- package/dist/types/src/plugin/vault-custody.d.ts.map +1 -1
- package/dist/types/src/runtime-layout.d.ts.map +1 -1
- package/dist/types/src/setup.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/common-auth-embedded/claustrum/availability.js +1 -1
- package/src/common-auth-embedded/claustrum/custody.js +1 -1
- package/src/common-auth-embedded/fs/private-files.js +14 -5
- package/src/common-auth-embedded/opencode2/index.d.ts +1 -1
- package/src/common-auth-embedded/opencode2/index.js +1 -1
- package/src/common-auth-embedded/opencode2/install.d.ts +6 -1
- package/src/common-auth-embedded/opencode2/install.js +14 -4
- package/src/common-auth-embedded/opencode2/types.d.ts +10 -7
- package/src/common-auth-embedded/pi-slot/index.d.ts +1 -1
- package/src/common-auth-embedded/pi-slot/index.js +1 -1
- package/src/common-auth-embedded/pi-slot/slot.d.ts +6 -9
- package/src/common-auth-embedded/pi-slot/slot.js +42 -105
- package/src/common-auth-embedded/routing-accessor/index.d.ts +66 -16
- package/src/common-auth-embedded/routing-accessor/index.js +123 -17
- package/src/common-auth-embedded/rpc/index.d.ts +1 -1
- package/src/common-auth-embedded/rpc/index.js +1 -1
- package/src/common-auth-embedded/rpc/port-file.d.ts +4 -0
- package/src/common-auth-embedded/rpc/port-file.js +55 -49
- package/src/common-auth-embedded/rpc/rpc-server.js +3 -2
- package/src/common-auth-embedded/rpc/startup-sweep.js +32 -0
- package/src/common-auth-embedded/source-output.json +77 -68
- package/src/common-auth-embedded/store/mutate.js +34 -13
- package/src/tui-compiled/common-auth-embedded/fs/private-files.js +14 -5
- package/src/tui-compiled/common-auth-embedded/rpc/port-file.js +55 -49
- package/src/tui-raw/common-auth-embedded/fs/private-files.js +14 -5
- package/src/tui-raw/common-auth-embedded/rpc/port-file.js +55 -49
|
@@ -2,8 +2,13 @@ import { OpenCode2AuthError } from './errors.js';
|
|
|
2
2
|
import { placeholderSecret } from './integration.js';
|
|
3
3
|
import { watchServerSentEvents } from './sse.js';
|
|
4
4
|
export const DEFAULT_MAX_RECORDS = 512;
|
|
5
|
-
/**
|
|
5
|
+
/** Cache-bound holds allow a one-hour cache lifetime plus maximum session jitter. */
|
|
6
6
|
export const MAX_RETRY_HOLD_MS = 3_621_000;
|
|
7
|
+
/**
|
|
8
|
+
* Terminal quota waits allow a seven-day reset plus margin while staying below
|
|
9
|
+
* the signed 32-bit millisecond timer limit (2^31 - 1 ms).
|
|
10
|
+
*/
|
|
11
|
+
export const MAX_TERMINAL_HOLD_MS = 691_200_000;
|
|
7
12
|
/**
|
|
8
13
|
* The request header `model.request` sets to the attempt it started. The
|
|
9
14
|
* host builds the HTTP request and the WebSocket handshake from the headers
|
|
@@ -136,6 +141,8 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
136
141
|
const logger = options.logger;
|
|
137
142
|
const maxRecords = Math.max(1, options.maxRecords ?? DEFAULT_MAX_RECORDS);
|
|
138
143
|
const hostRetryLimit = options.hostRetryLimit ?? 10;
|
|
144
|
+
// Retry labels start at 2; count the retry being decided as still available.
|
|
145
|
+
const remainingHostRetries = (attempt) => hostRetryLimit + 2 - attempt;
|
|
139
146
|
const forbidden = [
|
|
140
147
|
...(options.hostCredentials ?? [placeholderSecret(providerID)]),
|
|
141
148
|
// An owned request must replace its placeholder even if a caller customizes
|
|
@@ -1002,7 +1009,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1002
1009
|
const canContinue = () => rec.readFailed === true &&
|
|
1003
1010
|
draft.error.type === 'provider.transport' &&
|
|
1004
1011
|
hostDecision.retry &&
|
|
1005
|
-
|
|
1012
|
+
remainingHostRetries(draft.attempt) >= 1 &&
|
|
1006
1013
|
!rec.limit &&
|
|
1007
1014
|
!(rec.status !== undefined && rec.status >= 400) &&
|
|
1008
1015
|
!(draft.error.status !== undefined && draft.error.status >= 400) &&
|
|
@@ -1266,7 +1273,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1266
1273
|
decision = { retry: false };
|
|
1267
1274
|
reason = 'adapter-veto';
|
|
1268
1275
|
}
|
|
1269
|
-
else if (!(
|
|
1276
|
+
else if (!(remainingHostRetries(draft.attempt) >= 1)) {
|
|
1270
1277
|
if (next)
|
|
1271
1278
|
await releaseChoice(next);
|
|
1272
1279
|
next = undefined;
|
|
@@ -1284,7 +1291,10 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1284
1291
|
else if (hold &&
|
|
1285
1292
|
Number.isFinite(hold.delayMs) &&
|
|
1286
1293
|
hold.delayMs >= 0 &&
|
|
1287
|
-
hold.delayMs <=
|
|
1294
|
+
hold.delayMs <=
|
|
1295
|
+
(hold.kind === 'terminal'
|
|
1296
|
+
? MAX_TERMINAL_HOLD_MS
|
|
1297
|
+
: MAX_RETRY_HOLD_MS)) {
|
|
1288
1298
|
// A recognised account limit can be recoverable even when
|
|
1289
1299
|
// the host treats its provider error as final.
|
|
1290
1300
|
decision = {
|
|
@@ -58,7 +58,7 @@ export type HeaderEdits = Readonly<Record<string, string | null>>;
|
|
|
58
58
|
export interface ChooseAccountInput extends RequestScope {
|
|
59
59
|
/** The retry hook's advance choice (which may hold), or a request's own choice. */
|
|
60
60
|
readonly phase: 'retry' | 'request';
|
|
61
|
-
/**
|
|
61
|
+
/** Next-attempt label (first retry is 2); also carried by a consumed hold's wake. */
|
|
62
62
|
readonly hostAttempt?: number;
|
|
63
63
|
/** Revalidate the held account before dispatch, even if the target is not due. */
|
|
64
64
|
readonly wake?: RetryWake;
|
|
@@ -101,14 +101,15 @@ export interface RetryHold {
|
|
|
101
101
|
/**
|
|
102
102
|
* Adapter-computed wait in milliseconds, including per-session jitter.
|
|
103
103
|
* Finite, non-negative values up to MAX_RETRY_HOLD_MS (3_621_000) are
|
|
104
|
-
* accepted
|
|
105
|
-
*
|
|
104
|
+
* accepted, or up to MAX_TERMINAL_HOLD_MS (691_200_000) for kind 'terminal'.
|
|
105
|
+
* Larger or invalid values keep the host's decision and release the choice
|
|
106
|
+
* rather than dispatching before the requested wait has elapsed.
|
|
106
107
|
*/
|
|
107
108
|
readonly delayMs: number;
|
|
108
109
|
/** An absolute wake target; its presence forces a request-phase re-check. */
|
|
109
110
|
readonly target?: number;
|
|
110
111
|
readonly checkpoint?: WaitCheckpoint;
|
|
111
|
-
/**
|
|
112
|
+
/** Terminal quota waits use the longer delay bound and omit the cache bound at wake. */
|
|
112
113
|
readonly kind?: RetryWake['kind'];
|
|
113
114
|
};
|
|
114
115
|
}
|
|
@@ -119,7 +120,7 @@ export interface RetryDisposition {
|
|
|
119
120
|
/** Wait through one host retry, then choose afresh without reserving an account. */
|
|
120
121
|
export interface RetryWaitThenReselect {
|
|
121
122
|
readonly waitThenReselect: {
|
|
122
|
-
/**
|
|
123
|
+
/** Finite, non-negative delay up to MAX_RETRY_HOLD_MS (3_621_000). */
|
|
123
124
|
readonly delayMs: number;
|
|
124
125
|
};
|
|
125
126
|
}
|
|
@@ -518,8 +519,10 @@ export interface InstallOpenCode2AuthOptions {
|
|
|
518
519
|
/** Most attempts kept before the oldest is abandoned and dropped. */
|
|
519
520
|
readonly maxRecords?: number;
|
|
520
521
|
/**
|
|
521
|
-
*
|
|
522
|
-
*
|
|
522
|
+
* Number of host retries, default 10 (next-attempt labels 2 through 11).
|
|
523
|
+
* Remaining retries, including the one being decided, are
|
|
524
|
+
* hostRetryLimit + 2 - hostAttempt. With none available, limit holds and
|
|
525
|
+
* reroutes keep the host's decision and release their choice.
|
|
523
526
|
*/
|
|
524
527
|
readonly hostRetryLimit?: number;
|
|
525
528
|
/**
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** Replace a Pi provider login with a vault placeholder without changing provider or model identifiers. */
|
|
2
2
|
export type { PiSlot, PiSlotErrorCode, PiSlotInspection, PiSlotOptions, } from './slot.js';
|
|
3
3
|
export { openPiSlot, PiSlotError } from './slot.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** Replace a Pi provider login with a vault placeholder without changing provider or model identifiers. */
|
|
2
2
|
export { openPiSlot, PiSlotError } from './slot.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type PiSlotErrorCode = 'invalid-options' | 'invalid-auth' | '
|
|
1
|
+
export type PiSlotErrorCode = 'invalid-options' | 'invalid-auth' | 'unsafe-path' | 'lock-timeout' | 'lock-compromised' | 'io';
|
|
2
2
|
/** Errors deliberately contain neither paths nor credentials nor underlying error messages. */
|
|
3
3
|
export declare class PiSlotError extends Error {
|
|
4
4
|
readonly code: PiSlotErrorCode;
|
|
@@ -10,10 +10,8 @@ export interface PiSlotOptions {
|
|
|
10
10
|
authPath: string;
|
|
11
11
|
provider: string;
|
|
12
12
|
/**
|
|
13
|
-
* Where the
|
|
14
|
-
*
|
|
15
|
-
* stash holds one provider's original login, and a second provider writing
|
|
16
|
-
* to it would overwrite the first one's backup.
|
|
13
|
+
* Where older versions kept the original Pi login; deleted when found.
|
|
14
|
+
* Keep the same path across upgrades so legacy credentials can be removed.
|
|
17
15
|
*/
|
|
18
16
|
stashPath: string;
|
|
19
17
|
/**
|
|
@@ -26,13 +24,12 @@ export interface PiSlotOptions {
|
|
|
26
24
|
lockTimeoutMs?: number;
|
|
27
25
|
}
|
|
28
26
|
export interface PiSlotInspection {
|
|
29
|
-
slot: 'original' | 'placeholder' | '
|
|
30
|
-
stash: 'valid' | 'missing';
|
|
27
|
+
slot: 'original' | 'placeholder' | 'empty';
|
|
31
28
|
}
|
|
32
29
|
export interface PiSlot {
|
|
33
30
|
inspect(): Promise<PiSlotInspection>;
|
|
34
31
|
enterVault(): Promise<void>;
|
|
35
|
-
exitVault(): Promise<'
|
|
32
|
+
exitVault(): Promise<'cleared' | 'nothing-to-do'>;
|
|
36
33
|
}
|
|
37
34
|
/**
|
|
38
35
|
* Production entry point. Pi caches which models are usable; after a
|
|
@@ -44,7 +41,7 @@ export declare function openPiSlot(options: PiSlotOptions): PiSlot;
|
|
|
44
41
|
* Internal test entry point: pause reads, renewals, or renames to simulate races,
|
|
45
42
|
* and observe durable writes/deletion to simulate crashes. Not a public export.
|
|
46
43
|
*/
|
|
47
|
-
export declare function createPiSlot(options: PiSlotOptions, onStep?: (step: '
|
|
44
|
+
export declare function createPiSlot(options: PiSlotOptions, onStep?: (step: 'auth-written' | 'stash-deleted') => void, hooks?: {
|
|
48
45
|
afterReadCheck?: (path: string) => Promise<void>;
|
|
49
46
|
onRenew?: (renew: () => Promise<void>) => void;
|
|
50
47
|
beforeRename?: (path: string) => Promise<void>;
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { createHash } from 'node:crypto';
|
|
2
1
|
import { constants } from 'node:fs';
|
|
3
2
|
import { lstat, mkdir, open, unlink } from 'node:fs/promises';
|
|
4
3
|
import { dirname, resolve } from 'node:path';
|
|
5
4
|
import { setTimeout as sleep } from 'node:timers/promises';
|
|
6
5
|
import { writeJsonAtomic } from '../fs/atomic-write.js';
|
|
7
|
-
import { ensurePrivateDirectory, PrivateFileError, readPrivateBuffer, windowsPrivateFiles, } from '../fs/private-files.js';
|
|
6
|
+
import { ensurePrivateDirectory, PrivateFileError, readPrivateBuffer, verifySafeDirectory, windowsPrivateFiles, } from '../fs/private-files.js';
|
|
8
7
|
import { acquirePiLock } from './lock.js';
|
|
9
8
|
/** Errors deliberately contain neither paths nor credentials nor underlying error messages. */
|
|
10
9
|
export class PiSlotError extends Error {
|
|
@@ -79,31 +78,17 @@ function rootSpans(text) {
|
|
|
79
78
|
function object(value) {
|
|
80
79
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
81
80
|
}
|
|
82
|
-
function canonical(value) {
|
|
83
|
-
if (Array.isArray(value))
|
|
84
|
-
return `[${value.map(canonical).join(',')}]`;
|
|
85
|
-
if (object(value)) {
|
|
86
|
-
return `{${Object.keys(value)
|
|
87
|
-
.sort()
|
|
88
|
-
.map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`)
|
|
89
|
-
.join(',')}}`;
|
|
90
|
-
}
|
|
91
|
-
return JSON.stringify(value);
|
|
92
|
-
}
|
|
93
|
-
function digest(value) {
|
|
94
|
-
return createHash('sha256').update(canonical(value)).digest('hex');
|
|
95
|
-
}
|
|
96
81
|
function code(error) {
|
|
97
82
|
return object(error) ? error.code : undefined;
|
|
98
83
|
}
|
|
99
|
-
async function safeRead(path,
|
|
84
|
+
async function safeRead(path, afterReadCheck) {
|
|
100
85
|
if (windowsPrivateFiles) {
|
|
101
86
|
try {
|
|
102
87
|
await afterReadCheck?.(path);
|
|
103
88
|
const bytes = await readPrivateBuffer(path, true);
|
|
104
89
|
const text = bytes.toString('utf8');
|
|
105
90
|
if (!bytes.equals(Buffer.from(text)))
|
|
106
|
-
throw new PiSlotError(
|
|
91
|
+
throw new PiSlotError('invalid-auth');
|
|
107
92
|
return text;
|
|
108
93
|
}
|
|
109
94
|
catch (error) {
|
|
@@ -116,7 +101,7 @@ async function safeRead(path, privateFile = false, afterReadCheck) {
|
|
|
116
101
|
try {
|
|
117
102
|
file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
118
103
|
const stat = await file.stat();
|
|
119
|
-
if (!stat.isFile()
|
|
104
|
+
if (!stat.isFile()) {
|
|
120
105
|
throw new PiSlotError('unsafe-path');
|
|
121
106
|
}
|
|
122
107
|
await afterReadCheck?.(path);
|
|
@@ -130,7 +115,7 @@ async function safeRead(path, privateFile = false, afterReadCheck) {
|
|
|
130
115
|
const bytes = await file.readFile();
|
|
131
116
|
const text = bytes.toString('utf8');
|
|
132
117
|
if (!bytes.equals(Buffer.from(text))) {
|
|
133
|
-
throw new PiSlotError(
|
|
118
|
+
throw new PiSlotError('invalid-auth');
|
|
134
119
|
}
|
|
135
120
|
return text;
|
|
136
121
|
}
|
|
@@ -169,12 +154,6 @@ function parseAuth(text, provider) {
|
|
|
169
154
|
throw new PiSlotError('invalid-auth');
|
|
170
155
|
}
|
|
171
156
|
}
|
|
172
|
-
function entryText(auth) {
|
|
173
|
-
const value = auth.property?.value;
|
|
174
|
-
return value
|
|
175
|
-
? auth.text.slice(value.offset, value.offset + value.length)
|
|
176
|
-
: null;
|
|
177
|
-
}
|
|
178
157
|
function replaceEntry(auth, provider, next) {
|
|
179
158
|
const { text, root, property } = auth;
|
|
180
159
|
const properties = root.children ?? [];
|
|
@@ -199,7 +178,7 @@ function replaceEntry(auth, provider, next) {
|
|
|
199
178
|
if (next === null)
|
|
200
179
|
return text;
|
|
201
180
|
const last = properties.at(-1);
|
|
202
|
-
// Insert next to the last value
|
|
181
|
+
// Insert next to the last value so removal preserves the surrounding whitespace.
|
|
203
182
|
const offset = last ? last.offset + last.length : root.offset + 1;
|
|
204
183
|
return `${text.slice(0, offset)}${last ? ',' : ''}${JSON.stringify(provider)}:${next}${text.slice(offset)}`;
|
|
205
184
|
}
|
|
@@ -257,34 +236,11 @@ export function createPiSlot(options, onStep, hooks) {
|
|
|
257
236
|
if (authPath === stashPath || stashPath === `${authPath}.lock`) {
|
|
258
237
|
throw new PiSlotError('invalid-options');
|
|
259
238
|
}
|
|
260
|
-
const authIdentity = digest(authPath);
|
|
261
|
-
const placeholderIdentity = digest(placeholderKey);
|
|
262
239
|
const placeholder = { type: 'api_key', key: placeholderKey };
|
|
263
|
-
const isPlaceholder = (entry) =>
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
return undefined;
|
|
268
|
-
try {
|
|
269
|
-
const stash = JSON.parse(text);
|
|
270
|
-
if (stash.version !== 1 ||
|
|
271
|
-
stash.provider !== provider ||
|
|
272
|
-
stash.authIdentity !== authIdentity ||
|
|
273
|
-
stash.placeholderIdentity !== placeholderIdentity ||
|
|
274
|
-
(stash.entry !== null && !object(stash.entry)) ||
|
|
275
|
-
stash.sha256 !== digest(stash.entry) ||
|
|
276
|
-
(stash.entry === null
|
|
277
|
-
? stash.entryJson !== null
|
|
278
|
-
: typeof stash.entryJson !== 'string' ||
|
|
279
|
-
digest(JSON.parse(stash.entryJson)) !== stash.sha256)) {
|
|
280
|
-
throw new Error();
|
|
281
|
-
}
|
|
282
|
-
return stash;
|
|
283
|
-
}
|
|
284
|
-
catch {
|
|
285
|
-
throw new PiSlotError('invalid-stash');
|
|
286
|
-
}
|
|
287
|
-
}
|
|
240
|
+
const isPlaceholder = (entry) => entry !== null &&
|
|
241
|
+
Object.keys(entry).length === 2 &&
|
|
242
|
+
entry.type === placeholder.type &&
|
|
243
|
+
entry.key === placeholder.key;
|
|
288
244
|
async function locked(fn) {
|
|
289
245
|
let release;
|
|
290
246
|
let assertFresh;
|
|
@@ -334,11 +290,27 @@ export function createPiSlot(options, onStep, hooks) {
|
|
|
334
290
|
}
|
|
335
291
|
}
|
|
336
292
|
async function load() {
|
|
337
|
-
|
|
338
|
-
const stash = await loadStash();
|
|
339
|
-
return { auth, stash };
|
|
293
|
+
return parseAuth((await safeRead(authPath, hooks?.afterReadCheck)) ?? '{}', provider);
|
|
340
294
|
}
|
|
341
295
|
async function deleteStash(assertLock) {
|
|
296
|
+
// Legacy credentials are discarded without opening or parsing their contents.
|
|
297
|
+
try {
|
|
298
|
+
const file = await lstat(stashPath);
|
|
299
|
+
if (!file.isFile())
|
|
300
|
+
throw new PiSlotError('unsafe-path');
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
if (code(error) === 'ENOENT')
|
|
304
|
+
return;
|
|
305
|
+
throw error;
|
|
306
|
+
}
|
|
307
|
+
if (windowsPrivateFiles)
|
|
308
|
+
await verifySafeDirectory(dirname(stashPath));
|
|
309
|
+
else {
|
|
310
|
+
const dir = await lstat(dirname(stashPath));
|
|
311
|
+
if (!dir.isDirectory() || (dir.mode & 0o777) !== 0o700)
|
|
312
|
+
throw new PiSlotError('unsafe-path');
|
|
313
|
+
}
|
|
342
314
|
assertLock();
|
|
343
315
|
await unlink(stashPath);
|
|
344
316
|
await syncDir(dirname(stashPath));
|
|
@@ -346,66 +318,31 @@ export function createPiSlot(options, onStep, hooks) {
|
|
|
346
318
|
}
|
|
347
319
|
return {
|
|
348
320
|
inspect: () => locked(async () => {
|
|
349
|
-
const
|
|
321
|
+
const auth = await load();
|
|
350
322
|
const slot = isPlaceholder(auth.entry)
|
|
351
323
|
? 'placeholder'
|
|
352
324
|
: auth.entry === null
|
|
353
325
|
? 'empty'
|
|
354
|
-
:
|
|
355
|
-
|
|
356
|
-
: 'foreign';
|
|
357
|
-
return { slot, stash: stash ? 'valid' : 'missing' };
|
|
326
|
+
: 'original';
|
|
327
|
+
return { slot };
|
|
358
328
|
}),
|
|
359
329
|
enterVault: () => locked(async (assertLock) => {
|
|
360
|
-
const
|
|
361
|
-
if (isPlaceholder(auth.entry)) {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
if (stash && digest(auth.entry) !== stash.sha256)
|
|
367
|
-
throw new PiSlotError('conflict');
|
|
368
|
-
if (!stash) {
|
|
369
|
-
if (windowsPrivateFiles)
|
|
370
|
-
await ensurePrivateDirectory(dirname(stashPath));
|
|
371
|
-
else {
|
|
372
|
-
await mkdir(dirname(stashPath), { recursive: true, mode: 0o700 });
|
|
373
|
-
const dir = await lstat(dirname(stashPath));
|
|
374
|
-
if (!dir.isDirectory() || (dir.mode & 0o777) !== 0o700)
|
|
375
|
-
throw new PiSlotError('unsafe-path');
|
|
376
|
-
}
|
|
377
|
-
const saved = {
|
|
378
|
-
version: 1,
|
|
379
|
-
provider,
|
|
380
|
-
authIdentity,
|
|
381
|
-
placeholderIdentity,
|
|
382
|
-
entry: auth.entry,
|
|
383
|
-
entryJson: entryText(auth),
|
|
384
|
-
sha256: digest(auth.entry),
|
|
385
|
-
};
|
|
386
|
-
await atomicWrite(stashPath, JSON.stringify(saved), assertLock, hooks?.beforeRename);
|
|
387
|
-
onStep?.('stash-written');
|
|
330
|
+
const auth = await load();
|
|
331
|
+
if (!isPlaceholder(auth.entry)) {
|
|
332
|
+
await atomicWrite(authPath, replaceEntry(auth, provider, JSON.stringify(placeholder)), assertLock, hooks?.beforeRename);
|
|
333
|
+
onStep?.('auth-written');
|
|
388
334
|
}
|
|
389
|
-
await
|
|
390
|
-
onStep?.('auth-written');
|
|
335
|
+
await deleteStash(assertLock);
|
|
391
336
|
}),
|
|
392
337
|
exitVault: () => locked(async (assertLock) => {
|
|
393
|
-
const
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
return 'nothing-to-do';
|
|
398
|
-
}
|
|
399
|
-
if (isPlaceholder(auth.entry)) {
|
|
400
|
-
await atomicWrite(authPath, replaceEntry(auth, provider, stash.entryJson), assertLock, hooks?.beforeRename);
|
|
338
|
+
const auth = await load();
|
|
339
|
+
const cleared = isPlaceholder(auth.entry);
|
|
340
|
+
if (cleared) {
|
|
341
|
+
await atomicWrite(authPath, replaceEntry(auth, provider, null), assertLock, hooks?.beforeRename);
|
|
401
342
|
onStep?.('auth-written');
|
|
402
343
|
}
|
|
403
|
-
else if (digest(auth.entry) !== stash.sha256) {
|
|
404
|
-
return 'conflict';
|
|
405
|
-
}
|
|
406
|
-
// Also completes a restore interrupted between the auth rename and stash deletion.
|
|
407
344
|
await deleteStash(assertLock);
|
|
408
|
-
return '
|
|
345
|
+
return cleared ? 'cleared' : 'nothing-to-do';
|
|
409
346
|
}),
|
|
410
347
|
};
|
|
411
348
|
}
|
|
@@ -13,11 +13,31 @@ export interface RoutingCandidate {
|
|
|
13
13
|
accountId: string;
|
|
14
14
|
credentialId: string;
|
|
15
15
|
}
|
|
16
|
-
|
|
16
|
+
type ContinueExhaustion = {
|
|
17
|
+
/** Vault account id, not the plugin's route id. */
|
|
18
|
+
accountId: string;
|
|
19
|
+
credentialId: string;
|
|
20
|
+
/** When the limit or sign-in signal was observed, in Unix milliseconds. */
|
|
21
|
+
observedAtMs: number;
|
|
22
|
+
} & ({
|
|
23
|
+
limitWindow: 'five_hour' | 'seven_day';
|
|
24
|
+
/** Scoped quota group matching `^[a-z0-9._-]{1,64}$`. */
|
|
25
|
+
pool?: string;
|
|
26
|
+
/** Reset time in Unix milliseconds. */
|
|
27
|
+
resetsAtMs?: number;
|
|
28
|
+
/** Non-negative seconds until retry, rounded down. */
|
|
29
|
+
retryAfterS?: number;
|
|
30
|
+
} | {
|
|
31
|
+
limitWindow: 'signed_out';
|
|
32
|
+
pool?: never;
|
|
33
|
+
resetsAtMs?: never;
|
|
34
|
+
retryAfterS?: never;
|
|
35
|
+
});
|
|
36
|
+
export type AskRoutingInput<Candidate extends RoutingCandidate = RoutingCandidate, Refusals extends boolean = false> = {
|
|
17
37
|
sessionId: string;
|
|
18
38
|
provider: string;
|
|
19
39
|
modelId: string;
|
|
20
|
-
/** Only routes
|
|
40
|
+
/** Only routes eligible under the vault and the user's configured rules. */
|
|
21
41
|
candidates: readonly Candidate[];
|
|
22
42
|
/** Defaults to 1500 ms, above routing's normal tail; clamped to 1–2000 ms. */
|
|
23
43
|
deadlineMs?: number;
|
|
@@ -33,7 +53,24 @@ export interface AskRoutingInput<Candidate extends RoutingCandidate = RoutingCan
|
|
|
33
53
|
holdWaitMaxS?: number;
|
|
34
54
|
/** Plugin sticky choice's vault credential id; an eligible-account hint, not a pin. */
|
|
35
55
|
warmCredentialId?: string;
|
|
36
|
-
}
|
|
56
|
+
} & ({
|
|
57
|
+
mode?: never;
|
|
58
|
+
requestId?: never;
|
|
59
|
+
exhausted?: never;
|
|
60
|
+
} | {
|
|
61
|
+
/**
|
|
62
|
+
* Ask routing whether a run should move off its pinned account. Send to
|
|
63
|
+
* the selected account, or wait on it if the answer includes a hold.
|
|
64
|
+
* With `refusals: true`, a `{refused}` answer means keep the run paused
|
|
65
|
+
* on its account until the reset or, for `signed_out`, until the user
|
|
66
|
+
* signs in. Do not ask again while paused.
|
|
67
|
+
*/
|
|
68
|
+
mode: 'continue';
|
|
69
|
+
/** One id per question (e.g. a fresh UUID); reuse it only to retry that same question. */
|
|
70
|
+
requestId: string;
|
|
71
|
+
/** Required on a continue: a proved usage limit or a definite sign-in signal. */
|
|
72
|
+
exhausted: ContinueExhaustion;
|
|
73
|
+
});
|
|
37
74
|
export interface RoutingHold {
|
|
38
75
|
/** Uncapped advice from routing. The plugin must apply its own hold cap. */
|
|
39
76
|
retry_after_ms: number;
|
|
@@ -47,6 +84,7 @@ export type RoutingRefusal = {
|
|
|
47
84
|
refused: {
|
|
48
85
|
code: 'account_limit_exhausted' | 'account_selection_refused' | 'account_selection_final';
|
|
49
86
|
routingCode?: string;
|
|
87
|
+
reason?: string;
|
|
50
88
|
retryAfterSecs?: number;
|
|
51
89
|
resetsAtMs?: number;
|
|
52
90
|
};
|
|
@@ -110,7 +148,21 @@ export type ReportRoutingInput = (RoutingReportIdentity & ({
|
|
|
110
148
|
* as 403 or 429. Must match `^[a-z0-9._-]{1,64}$`.
|
|
111
149
|
*/
|
|
112
150
|
providerCode: string;
|
|
113
|
-
})) | {
|
|
151
|
+
})) | (RoutingReportIdentity & {
|
|
152
|
+
/**
|
|
153
|
+
* Report a 429 or similar response that isn't a proved quota limit. With
|
|
154
|
+
* no provider retry time, use the final attempt's receipt time + 5,000 ms.
|
|
155
|
+
* Routing blocks only new admissions on the account until `retryAtMs`.
|
|
156
|
+
* The session's pin stays: wait until `retryAtMs` and retry the run on
|
|
157
|
+
* the same account. For a proved quota limit, report `exhausted` and ask
|
|
158
|
+
* routing to continue with the exhausted window. Use `credential_refused`
|
|
159
|
+
* for an API key refused for any use.
|
|
160
|
+
*/
|
|
161
|
+
kind: 'transient_backoff';
|
|
162
|
+
/** Absolute retry time in Unix milliseconds. */
|
|
163
|
+
retryAtMs: number;
|
|
164
|
+
providerCode?: string;
|
|
165
|
+
}) | {
|
|
114
166
|
sessionId: string;
|
|
115
167
|
kind: 'turn_notes_supported';
|
|
116
168
|
model: string;
|
|
@@ -175,20 +227,18 @@ export declare function turnNotes(sessionId: string): readonly {
|
|
|
175
227
|
* Ask routing for an eligible vault route. Any failure or unfamiliar answer
|
|
176
228
|
* yields undefined so the plugin can keep its own choice. Opted-in routing
|
|
177
229
|
* refusals instead stop the request. This does not admit routes. Callers never
|
|
178
|
-
* pass an account the vault refused.
|
|
179
|
-
* user
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
* floor, and don't add floors of the plugin's own that duplicate routing's
|
|
185
|
-
* reserves.
|
|
230
|
+
* pass an account the vault refused. Beyond that, a plugin may leave accounts
|
|
231
|
+
* out only under a user-configured eligibility rule, such as a killswitch floor
|
|
232
|
+
* the user turned on (the minimum share of a quota window an account must keep
|
|
233
|
+
* to be offered) or a per-model account list. Plugins ship no floors of
|
|
234
|
+
* their own by default. Routing's pick is final: never reject it under a local
|
|
235
|
+
* floor or choose another account afterwards.
|
|
186
236
|
*/
|
|
187
237
|
export declare function askRouting<Candidate extends RoutingCandidate, Refusals extends boolean = false>(input: AskRoutingInput<Candidate, Refusals>): Promise<RoutingSelection<Candidate> | (Refusals extends true ? RoutingRefusal : never) | undefined>;
|
|
188
238
|
/**
|
|
189
|
-
* Report a vault request, billing result, plugin wait or
|
|
190
|
-
* waiting for routing. Only contract fields are sent,
|
|
191
|
-
* are ignored. Headers must already be limited to
|
|
192
|
-
* caller; values are not filtered or redacted here.
|
|
239
|
+
* Report a vault request, account backoff, billing result, plugin wait or
|
|
240
|
+
* turn-note event without waiting for routing. Only contract fields are sent,
|
|
241
|
+
* and all accessor failures are ignored. Headers must already be limited to
|
|
242
|
+
* rate-limit headers by the caller; values are not filtered or redacted here.
|
|
193
243
|
*/
|
|
194
244
|
export declare function reportRouting(input: ReportRoutingInput): void;
|