@cortexkit/common-auth 0.33.0 → 0.35.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/claustrum/consumer.d.ts +12 -0
- package/dist/claustrum/consumer.js +36 -0
- package/dist/claustrum/custody.d.ts +16 -4
- package/dist/claustrum/custody.js +74 -9
- package/dist/claustrum/hub.d.ts +3 -2
- package/dist/claustrum/hub.js +8 -0
- package/dist/claustrum/index.d.ts +1 -0
- package/dist/claustrum/roster.d.ts +2 -0
- package/dist/claustrum/roster.js +2 -0
- package/dist/claustrum/usage.d.ts +41 -0
- package/dist/claustrum/usage.js +145 -0
- package/dist/commands/builtins.d.ts +5 -0
- package/dist/commands/builtins.js +38 -0
- package/dist/fs/private-files.d.ts +5 -0
- package/dist/fs/private-files.js +7 -0
- package/dist/fs/refresh-file-lock.js +142 -47
- package/dist/fs/with-lock.js +2 -5
- package/dist/limits/activity.d.ts +26 -0
- package/dist/limits/activity.js +52 -0
- package/dist/limits/axes.d.ts +69 -0
- package/dist/limits/axes.js +7 -0
- package/dist/limits/bridge.d.ts +4 -0
- package/dist/limits/bridge.js +10 -0
- package/dist/limits/decide.d.ts +146 -0
- package/dist/limits/decide.js +425 -0
- package/dist/limits/duration.d.ts +8 -0
- package/dist/limits/duration.js +20 -0
- package/dist/limits/envelope-loss.d.ts +29 -0
- package/dist/limits/envelope-loss.js +150 -0
- package/dist/limits/families.d.ts +59 -0
- package/dist/limits/families.js +149 -0
- package/dist/limits/header-classifier.d.ts +9 -0
- package/dist/limits/header-classifier.js +121 -0
- package/dist/limits/hold.d.ts +13 -0
- package/dist/limits/hold.js +25 -0
- package/dist/limits/identity.d.ts +14 -0
- package/dist/limits/identity.js +21 -0
- package/dist/limits/index.d.ts +45 -0
- package/dist/limits/index.js +26 -0
- package/dist/limits/jitter.d.ts +2 -0
- package/dist/limits/jitter.js +9 -0
- package/dist/limits/labels.d.ts +3 -0
- package/dist/limits/labels.js +11 -0
- package/dist/limits/marker.d.ts +69 -0
- package/dist/limits/marker.js +107 -0
- package/dist/limits/merge-laws.d.ts +15 -0
- package/dist/limits/merge-laws.js +31 -0
- package/dist/limits/namespace.d.ts +92 -0
- package/dist/limits/namespace.js +191 -0
- package/dist/limits/persistence.d.ts +33 -0
- package/dist/limits/persistence.js +9 -0
- package/dist/limits/pi-wait.d.ts +16 -0
- package/dist/limits/pi-wait.js +51 -0
- package/dist/limits/pool-adapter.d.ts +16 -0
- package/dist/limits/pool-adapter.js +162 -0
- package/dist/limits/presets.d.ts +11 -0
- package/dist/limits/presets.js +240 -0
- package/dist/limits/registry.d.ts +61 -0
- package/dist/limits/registry.js +478 -0
- package/dist/limits/request-history.d.ts +79 -0
- package/dist/limits/request-history.js +120 -0
- package/dist/limits/reset.d.ts +21 -0
- package/dist/limits/reset.js +44 -0
- package/dist/limits/routing-hold.d.ts +16 -0
- package/dist/limits/routing-hold.js +23 -0
- package/dist/limits/scope.d.ts +12 -0
- package/dist/limits/scope.js +10 -0
- package/dist/limits/signal.d.ts +41 -0
- package/dist/limits/signal.js +1 -0
- package/dist/limits/terminal.d.ts +52 -0
- package/dist/limits/terminal.js +133 -0
- package/dist/limits/unconfirmed-retain-counter.d.ts +13 -0
- package/dist/limits/unconfirmed-retain-counter.js +41 -0
- package/dist/limits/vault-adapter.d.ts +16 -0
- package/dist/limits/vault-adapter.js +123 -0
- package/dist/limits/vault.d.ts +1 -0
- package/dist/limits/vault.js +1 -0
- package/dist/limits/wake.d.ts +21 -0
- package/dist/limits/wake.js +47 -0
- package/dist/limits/wording.d.ts +25 -0
- package/dist/limits/wording.js +114 -0
- package/dist/opencode2/index.d.ts +1 -1
- package/dist/opencode2/install.d.ts +2 -2
- package/dist/opencode2/install.js +161 -18
- package/dist/opencode2/types.d.ts +95 -9
- package/dist/routing/admission.d.ts +7 -2
- package/dist/routing/admission.js +17 -7
- package/dist/routing-accessor/handle.d.ts +1 -0
- package/dist/routing-accessor/handle.js +10 -0
- package/dist/routing-accessor/index.d.ts +13 -3
- package/dist/routing-accessor/index.js +11 -10
- package/dist/rpc/port-file.d.ts +12 -3
- package/dist/rpc/port-file.js +46 -4
- package/dist/store/attribution.js +3 -2
- package/dist/store/errors.d.ts +1 -1
- package/dist/store/hooks.d.ts +5 -1
- package/dist/store/hooks.js +20 -5
- package/dist/store/index.d.ts +1 -0
- package/dist/store/limits-namespace.d.ts +7 -0
- package/dist/store/limits-namespace.js +34 -0
- package/dist/store/pool.d.ts +10 -1
- package/dist/store/pool.js +2 -0
- package/dist/store/provider-state.js +5 -5
- package/dist/store/refresh.js +1 -1
- package/dist/store/schema.js +9 -4
- package/dist/store/settings.d.ts +2 -0
- package/dist/store/settings.js +11 -4
- package/package.json +10 -2
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// OpenCode 1 session/retry.ts, also recorded in src/redaction/index.ts. Body
|
|
2
|
+
// matches can trigger replay even when the error itself is non-retryable.
|
|
3
|
+
const openCode1Patterns = [
|
|
4
|
+
/429|500|502|503|504|524/i,
|
|
5
|
+
/rate increased too quickly|rate limit|rate-limit|rate_limit|too many requests/i,
|
|
6
|
+
/overloaded|service unavailable|service_unavailable|service-unavailable|internal error|internal_error|internal server error|server error|server_error|server-error|provider returned error|provider_returned_error|provider-returned-error/i,
|
|
7
|
+
/terminated|fetch failed|failed to fetch|network[-_\s]error|upstream connect|connection error|connection refused|connection lost|socket connection was closed|socket hang up|reset before headers|getaddrinfo|enotfound|eai_again|econnrefused|econnreset|etimedout/i,
|
|
8
|
+
/^timeout$|\b(?:request|response|connection|network|stream|read) (?:timeout|timed out|time out)\b/i,
|
|
9
|
+
/try your request again|retry your request|resource exhausted|resource_exhausted/i,
|
|
10
|
+
/\btry again (?:later|in\b)|\b(?:currently|temporarily) at capacity\b/i,
|
|
11
|
+
];
|
|
12
|
+
// @earendil-works/pi-ai@1.4.0, dist/utils/retry.js:26-106,
|
|
13
|
+
// RETRYABLE_PROVIDER_ERROR_PATTERN. Pin this independently of the dev peer.
|
|
14
|
+
const pi140Pattern = new RegExp([
|
|
15
|
+
'overloaded',
|
|
16
|
+
'server_busy',
|
|
17
|
+
'servers are currently busy',
|
|
18
|
+
'currently experiencing high demand',
|
|
19
|
+
'model is at capacity',
|
|
20
|
+
'rate.?limit',
|
|
21
|
+
'too many requests',
|
|
22
|
+
'(?<![a-z0-9])(?:429|500|502|503|504|520|524)(?![a-z0-9])',
|
|
23
|
+
'service.?(?:is.?)?(?:temporarily.?)?unavailable',
|
|
24
|
+
'server.?error',
|
|
25
|
+
'internal.?error',
|
|
26
|
+
'provider.?returned.?(?:an.?)?error',
|
|
27
|
+
'exceeded request buffer limit while retrying upstream',
|
|
28
|
+
'unable to forward upstream',
|
|
29
|
+
'upstream.?request.?failed',
|
|
30
|
+
'upstream_stream_read_error',
|
|
31
|
+
'stream was interrupted',
|
|
32
|
+
'http/?2 stream failed',
|
|
33
|
+
'network.?error',
|
|
34
|
+
'connection.?error',
|
|
35
|
+
'connection.?refused',
|
|
36
|
+
'connection.?lost',
|
|
37
|
+
'other side closed',
|
|
38
|
+
'fetch failed',
|
|
39
|
+
'request to \\S+ failed, reason:',
|
|
40
|
+
'getaddrinfo',
|
|
41
|
+
'ENOTFOUND',
|
|
42
|
+
'EAI_AGAIN',
|
|
43
|
+
'upstream.?connect',
|
|
44
|
+
'reset before headers',
|
|
45
|
+
'socket hang up',
|
|
46
|
+
'socket connection was closed',
|
|
47
|
+
'timed? out',
|
|
48
|
+
'timeout',
|
|
49
|
+
'terminated',
|
|
50
|
+
'websocket.?closed',
|
|
51
|
+
'websocket.?error',
|
|
52
|
+
'ended without',
|
|
53
|
+
'stream ended before message_stop',
|
|
54
|
+
'stream ended before a terminal response event',
|
|
55
|
+
'http2 request did not get a response',
|
|
56
|
+
'stream disconnected before completion',
|
|
57
|
+
'pending stream has been canceled',
|
|
58
|
+
'retry delay',
|
|
59
|
+
'you can retry your request',
|
|
60
|
+
'try your request again',
|
|
61
|
+
'please retry your request',
|
|
62
|
+
'ResourceExhausted',
|
|
63
|
+
'subscription_sharing_usage_unavailable',
|
|
64
|
+
'subscription_sharing_user_unavailable',
|
|
65
|
+
].join('|'), 'i');
|
|
66
|
+
export function hasUnsafeWording(text) {
|
|
67
|
+
return (openCode1Patterns.some((pattern) => pattern.test(text)) ||
|
|
68
|
+
pi140Pattern.test(text));
|
|
69
|
+
}
|
|
70
|
+
export class UnsafeWordingError extends Error {
|
|
71
|
+
messageKey;
|
|
72
|
+
code = 'unsafe-wording';
|
|
73
|
+
constructor(messageKey) {
|
|
74
|
+
super(`Unsafe refusal template: ${messageKey}`);
|
|
75
|
+
this.messageKey = messageKey;
|
|
76
|
+
this.name = 'UnsafeWordingError';
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** Registration rejects unsafe fixed text; actual substitutions are checked again. */
|
|
80
|
+
export function registerLimitMessages(options) {
|
|
81
|
+
const refusals = new Map(Object.entries(options.refusals ?? {}));
|
|
82
|
+
const retryable = new Map(Object.entries(options.retryable ?? {}));
|
|
83
|
+
for (const [key, template] of refusals) {
|
|
84
|
+
for (const reset of [
|
|
85
|
+
undefined,
|
|
86
|
+
'45s',
|
|
87
|
+
'2m',
|
|
88
|
+
'1h 30m',
|
|
89
|
+
'99d 23h 59m',
|
|
90
|
+
'over 99 days',
|
|
91
|
+
]) {
|
|
92
|
+
for (const labels of [[], ['this account']]) {
|
|
93
|
+
const parameters = Object.freeze({
|
|
94
|
+
labels: Object.freeze(labels),
|
|
95
|
+
reset,
|
|
96
|
+
});
|
|
97
|
+
let rendered;
|
|
98
|
+
try {
|
|
99
|
+
rendered = template(parameters);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
throw new UnsafeWordingError(key);
|
|
103
|
+
}
|
|
104
|
+
if (typeof rendered !== 'string' || hasUnsafeWording(rendered)) {
|
|
105
|
+
throw new UnsafeWordingError(key);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return Object.freeze({
|
|
111
|
+
formatReset: options.formatReset,
|
|
112
|
+
render: (key, parameters, nonRetryable) => (nonRetryable ? refusals : retryable).get(key)?.(parameters),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
@@ -5,4 +5,4 @@ export type { FormAnswer, LoginContext, PoolAuthorization, PoolLoginMethod, Regi
|
|
|
5
5
|
export { isPlaceholderCredential, PLACEHOLDER_LIFETIME_MS, PLACEHOLDER_METADATA_KEY, PLACEHOLDER_PREFIX, placeholderCredential, placeholderSecret, registerOpenCode2AuthMethods, VAULT_ACTIVATION_INSTRUCTIONS, } from './integration.js';
|
|
6
6
|
export type { ServerSentEvent } from './sse.js';
|
|
7
7
|
export { watchServerSentEvents } from './sse.js';
|
|
8
|
-
export type { AccountHeadersResult, AccountRequest, Attempt, AttemptEndReason, AttemptOutcome, ChooseAccountInput, ChooseAccountResult, EventVerdict, HeaderEdits, HostError, InstallOpenCode2AuthOptions, LimitSignal, OpenCode2AuthAdapter, OpenCode2AuthEventName, OpenCode2AuthEvents, OpenCode2AuthInstallation, OpenCode2AuthLogger, OpenCode2HookContext, RequestKind, RequestScope, ResponseAccount, RetryHold, RetryReason, SameAccountRetryInput, SameAccountRetryResult, SelectingHook, Transport, } from './types.js';
|
|
8
|
+
export type { AccountHeadersResult, AccountRequest, Attempt, AttemptEndReason, AttemptOutcome, ChooseAccountInput, ChooseAccountResult, EventVerdict, HeaderEdits, HostError, InstallOpenCode2AuthOptions, LimitSignal, OpenCode2AuthAdapter, OpenCode2AuthEventName, OpenCode2AuthEvents, OpenCode2AuthInstallation, OpenCode2AuthLogger, OpenCode2HookContext, RequestKind, RequestScope, ResponseAccount, RetryDisposition, RetryHold, RetryReason, RetryWaitThenReselect, RetryWake, SameAccountRetryInput, SameAccountRetryResult, SelectingHook, Transport, } from './types.js';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ChooseAccountResult, HeaderEdits, InstallOpenCode2AuthOptions, OpenCode2AuthAdapter, OpenCode2AuthInstallation, OpenCode2HookContext } from './types.js';
|
|
2
2
|
export declare const DEFAULT_MAX_RECORDS = 512;
|
|
3
|
-
/**
|
|
4
|
-
export declare const MAX_RETRY_HOLD_MS
|
|
3
|
+
/** Allow a one-hour cache lifetime plus the maximum session jitter. */
|
|
4
|
+
export declare const MAX_RETRY_HOLD_MS = 3621000;
|
|
5
5
|
/**
|
|
6
6
|
* The request header `model.request` sets to the attempt it started. The
|
|
7
7
|
* host builds the HTTP request and the WebSocket handshake from the headers
|
|
@@ -2,8 +2,8 @@ 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
|
-
/**
|
|
6
|
-
export const MAX_RETRY_HOLD_MS =
|
|
5
|
+
/** Allow a one-hour cache lifetime plus the maximum session jitter. */
|
|
6
|
+
export const MAX_RETRY_HOLD_MS = 3_621_000;
|
|
7
7
|
/**
|
|
8
8
|
* The request header `model.request` sets to the attempt it started. The
|
|
9
9
|
* host builds the HTTP request and the WebSocket handshake from the headers
|
|
@@ -134,6 +134,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
134
134
|
ownership.credential(headers) === placeholderSecret(providerID);
|
|
135
135
|
const logger = options.logger;
|
|
136
136
|
const maxRecords = Math.max(1, options.maxRecords ?? DEFAULT_MAX_RECORDS);
|
|
137
|
+
const hostRetryLimit = options.hostRetryLimit ?? 10;
|
|
137
138
|
const forbidden = [
|
|
138
139
|
...(options.hostCredentials ?? [placeholderSecret(providerID)]),
|
|
139
140
|
// An owned request must replace its placeholder even if a caller customizes
|
|
@@ -203,6 +204,21 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
203
204
|
catch { }
|
|
204
205
|
};
|
|
205
206
|
const describe = (error) => error instanceof Error ? error.message : String(error);
|
|
207
|
+
const retireRequest = (request) => request.excluded.clear();
|
|
208
|
+
const exclusions = (request) => request.excluded.size === 0 ? {} : { excluded: [...request.excluded] };
|
|
209
|
+
const rejectsExcluded = (accountId, input, request) => {
|
|
210
|
+
if (!options.enforceRequestExclusion ||
|
|
211
|
+
accountId === undefined ||
|
|
212
|
+
!request.excluded.has(accountId))
|
|
213
|
+
return false;
|
|
214
|
+
warn('opencode2 auth ignored a chooser result for a request-excluded account', {
|
|
215
|
+
sessionID: input.sessionID,
|
|
216
|
+
kind: input.kind,
|
|
217
|
+
phase: input.phase,
|
|
218
|
+
accountId,
|
|
219
|
+
});
|
|
220
|
+
return true;
|
|
221
|
+
};
|
|
206
222
|
// Adapter choices may enqueue work by session. Serialize their consumption
|
|
207
223
|
// and release so a new choice cannot overtake cleanup, even after forgetting.
|
|
208
224
|
const choiceWork = new Map();
|
|
@@ -296,6 +312,11 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
296
312
|
};
|
|
297
313
|
if (error)
|
|
298
314
|
rec.endError = error;
|
|
315
|
+
// An error status can end before retry runs, and a limit may have arrived
|
|
316
|
+
// on an earlier event. Neither is a successful logical request.
|
|
317
|
+
if (error?.reason === 'cancelled' ||
|
|
318
|
+
(!error && !rec.limit && (rec.status === undefined || rec.status < 400)))
|
|
319
|
+
retireRequest(rec.request);
|
|
299
320
|
rec.ended = deliverEnd(rec.attempt, outcome);
|
|
300
321
|
};
|
|
301
322
|
const abandon = (rec, message) => finish(rec, { reason: 'abandoned', message });
|
|
@@ -399,10 +420,28 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
399
420
|
};
|
|
400
421
|
const select = async (scope, hook, assertActive, transport) => {
|
|
401
422
|
const key = keyOf(scope.sessionID, scope.kind);
|
|
402
|
-
const { input, accountId, sameAccountRetry } = await afterChoiceWork(scope.sessionID, async () => {
|
|
423
|
+
const { input, accountId, sameAccountRetry, request } = await afterChoiceWork(scope.sessionID, async () => {
|
|
403
424
|
assertActive();
|
|
404
425
|
const retried = pendingRetries.get(key);
|
|
405
426
|
pendingRetries.delete(key);
|
|
427
|
+
const others = attemptsOf(key);
|
|
428
|
+
if (retried &&
|
|
429
|
+
others.some((rec) => rec.id !== retried.attemptId && rec.attempt && !rec.ended))
|
|
430
|
+
warn('opencode2 auth bound a pending retry to an ambiguous concurrent request; keeping its exclusions', {
|
|
431
|
+
sessionID: scope.sessionID,
|
|
432
|
+
kind: scope.kind,
|
|
433
|
+
});
|
|
434
|
+
if (!retried) {
|
|
435
|
+
// A failed attempt that the host never retried must not seed the next
|
|
436
|
+
// logical request. Live attempts keep their own independent tokens.
|
|
437
|
+
const live = new Set(others
|
|
438
|
+
.filter((rec) => rec.attempt && !rec.ended)
|
|
439
|
+
.map((rec) => rec.request));
|
|
440
|
+
for (const rec of others)
|
|
441
|
+
if (!live.has(rec.request))
|
|
442
|
+
retireRequest(rec.request);
|
|
443
|
+
}
|
|
444
|
+
const request = retried?.request ?? { excluded: new Set() };
|
|
406
445
|
const previousAccountId = retried?.accountId ?? newestOf(key)?.accountId;
|
|
407
446
|
supersede(key);
|
|
408
447
|
const next = retried?.next;
|
|
@@ -413,13 +452,18 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
413
452
|
const matches = next !== undefined &&
|
|
414
453
|
next.input.modelID === scope.modelID &&
|
|
415
454
|
next.input.agent === scope.agent;
|
|
455
|
+
const wake = retried?.wake;
|
|
416
456
|
const input = {
|
|
417
457
|
...scope,
|
|
418
458
|
phase: 'request',
|
|
459
|
+
...exclusions(request),
|
|
419
460
|
...(previousAccountId === undefined ? {} : { previousAccountId }),
|
|
420
461
|
...(retried?.rerouteFrom ? { rerouteFrom: retried.rerouteFrom } : {}),
|
|
462
|
+
...(wake === undefined
|
|
463
|
+
? {}
|
|
464
|
+
: { wake, hostAttempt: next?.input.hostAttempt }),
|
|
421
465
|
};
|
|
422
|
-
if (next && !matches) {
|
|
466
|
+
if (next && (!matches || wake !== undefined)) {
|
|
423
467
|
await inChoiceOrder(scope.sessionID, () => releaseChoice(next));
|
|
424
468
|
assertActive();
|
|
425
469
|
}
|
|
@@ -429,11 +473,20 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
429
473
|
}
|
|
430
474
|
const choice = sameRoute
|
|
431
475
|
? sameAccount.accountId
|
|
432
|
-
: matches
|
|
476
|
+
: matches && wake === undefined
|
|
433
477
|
? next.accountId
|
|
434
478
|
: await adapter.chooseAccount(input);
|
|
435
|
-
|
|
436
|
-
|
|
479
|
+
let accountId = typeof choice === 'string' ? choice : undefined;
|
|
480
|
+
if (rejectsExcluded(accountId, input, request)) {
|
|
481
|
+
const denied = accountId;
|
|
482
|
+
if (sameRoute && sameAccount)
|
|
483
|
+
await inChoiceOrder(scope.sessionID, () => releaseSameAccount(sameAccount));
|
|
484
|
+
else if (denied !== undefined)
|
|
485
|
+
await inChoiceOrder(scope.sessionID, () => releaseChoice(matches && next ? next : { accountId: denied, input }));
|
|
486
|
+
accountId = undefined;
|
|
487
|
+
assertActive();
|
|
488
|
+
}
|
|
489
|
+
return { input, accountId, sameAccountRetry: sameRoute, request };
|
|
437
490
|
});
|
|
438
491
|
assertActive();
|
|
439
492
|
let headers = {};
|
|
@@ -454,6 +507,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
454
507
|
id,
|
|
455
508
|
key,
|
|
456
509
|
scope,
|
|
510
|
+
request,
|
|
457
511
|
accountId,
|
|
458
512
|
headers,
|
|
459
513
|
attempt: accountId === undefined
|
|
@@ -577,6 +631,8 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
577
631
|
noteQuota(rec, transport, verdict.quota);
|
|
578
632
|
if (verdict.outputStarted)
|
|
579
633
|
rec.outputStarted = true;
|
|
634
|
+
if (verdict.hostRetry === 'veto')
|
|
635
|
+
rec.hostRetry = 'veto';
|
|
580
636
|
if (verdict.limit)
|
|
581
637
|
noteLimit(rec, verdict.limit, transport);
|
|
582
638
|
if (verdict.error !== undefined)
|
|
@@ -747,13 +803,23 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
747
803
|
});
|
|
748
804
|
}
|
|
749
805
|
if (adapter.rewriteResponse) {
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
806
|
+
const rewritten = await adapter.rewriteResponse({
|
|
807
|
+
...account,
|
|
808
|
+
request: draft.request,
|
|
809
|
+
response,
|
|
810
|
+
attempt,
|
|
811
|
+
});
|
|
812
|
+
response = rewritten ?? response;
|
|
813
|
+
}
|
|
814
|
+
if (adapter.responseRetry) {
|
|
815
|
+
const disposition = await adapter.responseRetry({
|
|
816
|
+
...account,
|
|
817
|
+
request: draft.request,
|
|
818
|
+
response,
|
|
819
|
+
attempt,
|
|
820
|
+
});
|
|
821
|
+
if (disposition === 'veto')
|
|
822
|
+
rec.hostRetry = 'veto';
|
|
757
823
|
}
|
|
758
824
|
if (response !== original)
|
|
759
825
|
draft.response = response;
|
|
@@ -865,6 +931,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
865
931
|
return;
|
|
866
932
|
}
|
|
867
933
|
const rec = picked;
|
|
934
|
+
const vetoed = () => rec.hostRetry === 'veto';
|
|
868
935
|
await withSelection(rec.scope, async (assertActive) => {
|
|
869
936
|
const checkRetryActive = assertActive;
|
|
870
937
|
const retrySameAccount = adapter.retrySameAccount?.bind(adapter);
|
|
@@ -872,6 +939,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
872
939
|
if (retrySameAccount &&
|
|
873
940
|
handle &&
|
|
874
941
|
!rec.outputStarted &&
|
|
942
|
+
(!vetoed() || mixed(draft.sessionID)) &&
|
|
875
943
|
!rec.sameAccountRetry) {
|
|
876
944
|
if (rec.ended)
|
|
877
945
|
await rec.ended;
|
|
@@ -884,6 +952,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
884
952
|
rec.accountId === accountId &&
|
|
885
953
|
pickForRetry(draft.sessionID) === rec &&
|
|
886
954
|
!rec.outputStarted &&
|
|
955
|
+
(!vetoed() || mixed(draft.sessionID)) &&
|
|
887
956
|
(!ownership ||
|
|
888
957
|
latestTransportOwned.get(draft.sessionID) === true);
|
|
889
958
|
if (!active() || rec.sameAccountRetry)
|
|
@@ -940,6 +1009,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
940
1009
|
// Pin only this retry's route: choosing afresh could turn a token
|
|
941
1010
|
// refresh or path recovery into an account switch in a mixed session.
|
|
942
1011
|
pendingRetries.set(rec.key, {
|
|
1012
|
+
request: rec.request,
|
|
943
1013
|
attemptId: rec.id,
|
|
944
1014
|
sessionID: rec.scope.sessionID,
|
|
945
1015
|
order: ++retryOrder,
|
|
@@ -1006,6 +1076,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1006
1076
|
let reason;
|
|
1007
1077
|
let rerouteFrom;
|
|
1008
1078
|
let next;
|
|
1079
|
+
let wake;
|
|
1009
1080
|
if (rec.accountId === undefined || !attempt) {
|
|
1010
1081
|
decision = { retry: false };
|
|
1011
1082
|
reason = 'no-account';
|
|
@@ -1016,6 +1087,12 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1016
1087
|
decision = { retry: false };
|
|
1017
1088
|
reason = 'output-started';
|
|
1018
1089
|
}
|
|
1090
|
+
else if (vetoed()) {
|
|
1091
|
+
// Refusals formatted by the adapter carry policy on the attributed attempt,
|
|
1092
|
+
// not in message wording or a session-wide flag.
|
|
1093
|
+
decision = { retry: false };
|
|
1094
|
+
reason = 'adapter-veto';
|
|
1095
|
+
}
|
|
1019
1096
|
else {
|
|
1020
1097
|
if (rec.limit) {
|
|
1021
1098
|
const refused = {
|
|
@@ -1032,6 +1109,8 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1032
1109
|
const input = {
|
|
1033
1110
|
...rec.scope,
|
|
1034
1111
|
phase: 'retry',
|
|
1112
|
+
...exclusions(rec.request),
|
|
1113
|
+
hostAttempt: draft.attempt,
|
|
1035
1114
|
previousAccountId: rec.accountId,
|
|
1036
1115
|
rerouteFrom: refused,
|
|
1037
1116
|
};
|
|
@@ -1046,7 +1125,25 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1046
1125
|
error: describe(error),
|
|
1047
1126
|
});
|
|
1048
1127
|
}
|
|
1049
|
-
const
|
|
1128
|
+
const chosenAccount = typeof choice === 'string'
|
|
1129
|
+
? choice
|
|
1130
|
+
: typeof choice === 'object' && 'hold' in choice
|
|
1131
|
+
? rec.accountId
|
|
1132
|
+
: undefined;
|
|
1133
|
+
if (rejectsExcluded(chosenAccount, input, rec.request)) {
|
|
1134
|
+
if (chosenAccount !== undefined)
|
|
1135
|
+
await releaseChoice({ accountId: chosenAccount, input });
|
|
1136
|
+
choice = undefined;
|
|
1137
|
+
}
|
|
1138
|
+
const hold = typeof choice === 'object' && 'hold' in choice
|
|
1139
|
+
? choice.hold
|
|
1140
|
+
: undefined;
|
|
1141
|
+
const wait = typeof choice === 'object' && 'waitThenReselect' in choice
|
|
1142
|
+
? choice.waitThenReselect
|
|
1143
|
+
: undefined;
|
|
1144
|
+
const disposition = typeof choice === 'object' && 'hostRetry' in choice
|
|
1145
|
+
? choice.hostRetry
|
|
1146
|
+
: undefined;
|
|
1050
1147
|
const accountId = typeof choice === 'string'
|
|
1051
1148
|
? choice
|
|
1052
1149
|
: hold
|
|
@@ -1073,21 +1170,61 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1073
1170
|
reason: 'host-decides',
|
|
1074
1171
|
};
|
|
1075
1172
|
}
|
|
1076
|
-
if (
|
|
1173
|
+
if (vetoed() || disposition === 'veto') {
|
|
1174
|
+
if (next)
|
|
1175
|
+
await releaseChoice(next);
|
|
1176
|
+
next = undefined;
|
|
1177
|
+
checkRetryActive();
|
|
1178
|
+
if (mixed(draft.sessionID))
|
|
1179
|
+
return {
|
|
1180
|
+
decision: hostDecision,
|
|
1181
|
+
reason: 'host-decides',
|
|
1182
|
+
};
|
|
1183
|
+
rec.hostRetry = 'veto';
|
|
1184
|
+
decision = { retry: false };
|
|
1185
|
+
reason = 'adapter-veto';
|
|
1186
|
+
}
|
|
1187
|
+
else if (!(hostRetryLimit - draft.attempt >= 1)) {
|
|
1188
|
+
if (next)
|
|
1189
|
+
await releaseChoice(next);
|
|
1190
|
+
next = undefined;
|
|
1191
|
+
reason = 'host-decides-last-account';
|
|
1192
|
+
}
|
|
1193
|
+
else if (wait &&
|
|
1194
|
+
Number.isFinite(wait.delayMs) &&
|
|
1195
|
+
wait.delayMs >= 0 &&
|
|
1196
|
+
wait.delayMs <= MAX_RETRY_HOLD_MS) {
|
|
1197
|
+
// Only the failed attempt's context is kept. No next choice
|
|
1198
|
+
// exists to dispatch or release when the host wakes or cancels.
|
|
1199
|
+
decision = { retry: true, delay: Math.ceil(wait.delayMs) };
|
|
1200
|
+
reason = 'wait-then-reselect';
|
|
1201
|
+
}
|
|
1202
|
+
else if (hold &&
|
|
1077
1203
|
Number.isFinite(hold.delayMs) &&
|
|
1078
|
-
hold.delayMs >= 0
|
|
1204
|
+
hold.delayMs >= 0 &&
|
|
1205
|
+
hold.delayMs <= MAX_RETRY_HOLD_MS) {
|
|
1079
1206
|
// A recognised account limit can be recoverable even when
|
|
1080
1207
|
// the host treats its provider error as final.
|
|
1081
1208
|
decision = {
|
|
1082
1209
|
retry: true,
|
|
1083
|
-
delay: Math.
|
|
1210
|
+
delay: Math.ceil(hold.delayMs),
|
|
1084
1211
|
};
|
|
1212
|
+
if (hold.target !== undefined) {
|
|
1213
|
+
wake = {
|
|
1214
|
+
target: hold.target,
|
|
1215
|
+
...(hold.checkpoint === undefined
|
|
1216
|
+
? {}
|
|
1217
|
+
: { checkpoint: hold.checkpoint }),
|
|
1218
|
+
...(hold.kind === undefined ? {} : { kind: hold.kind }),
|
|
1219
|
+
};
|
|
1220
|
+
}
|
|
1085
1221
|
reason = 'hold';
|
|
1086
1222
|
}
|
|
1087
1223
|
else if (!hold &&
|
|
1088
1224
|
accountId !== undefined &&
|
|
1089
1225
|
accountId !== rec.accountId) {
|
|
1090
1226
|
rec.rerouted = true;
|
|
1227
|
+
rec.request.excluded.add(rec.accountId);
|
|
1091
1228
|
rerouteFrom = refused;
|
|
1092
1229
|
// The next attempt takes this choice rather than asking
|
|
1093
1230
|
// again, so an account limited between here and the host's
|
|
@@ -1121,12 +1258,14 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1121
1258
|
rec.accountId !== undefined &&
|
|
1122
1259
|
attempts.has(rec.id)) {
|
|
1123
1260
|
pendingRetries.set(rec.key, {
|
|
1261
|
+
request: rec.request,
|
|
1124
1262
|
attemptId: rec.id,
|
|
1125
1263
|
sessionID: rec.scope.sessionID,
|
|
1126
1264
|
order: ++retryOrder,
|
|
1127
1265
|
accountId: rec.accountId,
|
|
1128
1266
|
...(rerouteFrom === undefined ? {} : { rerouteFrom }),
|
|
1129
1267
|
...(next === undefined ? {} : { next }),
|
|
1268
|
+
...(wake === undefined ? {} : { wake }),
|
|
1130
1269
|
});
|
|
1131
1270
|
}
|
|
1132
1271
|
else if (next) {
|
|
@@ -1134,6 +1273,8 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1134
1273
|
// have been overridden by a reroute to another account.
|
|
1135
1274
|
await releaseChoice(next);
|
|
1136
1275
|
}
|
|
1276
|
+
if (!decision.retry)
|
|
1277
|
+
retireRequest(rec.request);
|
|
1137
1278
|
return { decision, reason };
|
|
1138
1279
|
});
|
|
1139
1280
|
checkRetryActive();
|
|
@@ -1166,6 +1307,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1166
1307
|
if (rec.scope.sessionID !== sessionID)
|
|
1167
1308
|
continue;
|
|
1168
1309
|
abandon(rec, 'its session was forgotten');
|
|
1310
|
+
retireRequest(rec.request);
|
|
1169
1311
|
attempts.delete(rec.id);
|
|
1170
1312
|
}
|
|
1171
1313
|
for (const [key, pending] of pendingRetries) {
|
|
@@ -1192,6 +1334,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1192
1334
|
if (pending.sessionID === sessionID &&
|
|
1193
1335
|
pending.order < idleOrder) {
|
|
1194
1336
|
pendingRetries.delete(key);
|
|
1337
|
+
retireRequest(pending.request);
|
|
1195
1338
|
await releaseRetry(pending);
|
|
1196
1339
|
}
|
|
1197
1340
|
}
|