@cortexkit/opencode-antigravity-auth 3.0.29 → 3.0.31
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 +783 -254
- package/dist/cli.js.map +4 -4
- package/dist/cli.meta.json +145 -77
- package/dist/index.js +842 -297
- package/dist/index.js.map +4 -4
- package/dist/index.meta.json +177 -85
- package/dist/server.js +842 -300
- package/dist/server.js.map +4 -4
- package/dist/server.meta.json +1 -1
- package/dist/src/cli.d.ts +1 -0
- package/dist/src/cli.d.ts.map +1 -1
- package/dist/src/cli.js +22 -2
- package/dist/src/cli.js.map +1 -1
- package/dist/src/common-auth-embedded/claustrum/consumer.d.ts +12 -0
- package/dist/src/common-auth-embedded/claustrum/consumer.js +36 -0
- package/dist/src/common-auth-embedded/claustrum/custody.d.ts +16 -4
- package/dist/src/common-auth-embedded/claustrum/custody.js +74 -9
- package/dist/src/common-auth-embedded/claustrum/hub.d.ts +3 -2
- package/dist/src/common-auth-embedded/claustrum/hub.js +8 -0
- package/dist/src/common-auth-embedded/claustrum/index.d.ts +1 -0
- package/dist/src/common-auth-embedded/claustrum/roster.d.ts +4 -0
- package/dist/src/common-auth-embedded/claustrum/roster.js +34 -8
- package/dist/src/common-auth-embedded/claustrum/usage.d.ts +41 -0
- package/dist/src/common-auth-embedded/claustrum/usage.js +145 -0
- package/dist/src/common-auth-embedded/commands/builtins.d.ts +5 -0
- package/dist/src/common-auth-embedded/commands/builtins.js +38 -0
- package/dist/src/common-auth-embedded/fs/file-read-cache.js +10 -5
- package/dist/src/common-auth-embedded/fs/private-files.d.ts +5 -0
- package/dist/src/common-auth-embedded/fs/private-files.js +7 -0
- package/dist/src/common-auth-embedded/fs/refresh-file-lock.js +142 -47
- package/dist/src/common-auth-embedded/fs/with-lock.js +2 -5
- package/dist/src/common-auth-embedded/limits/activity.d.ts +26 -0
- package/dist/src/common-auth-embedded/limits/axes.d.ts +69 -0
- package/dist/src/common-auth-embedded/limits/decide.d.ts +146 -0
- package/dist/src/common-auth-embedded/limits/duration.d.ts +8 -0
- package/dist/src/common-auth-embedded/limits/families.d.ts +59 -0
- package/dist/src/common-auth-embedded/limits/marker.d.ts +69 -0
- package/dist/src/common-auth-embedded/limits/request-history.d.ts +79 -0
- package/dist/src/common-auth-embedded/limits/scope.d.ts +12 -0
- package/dist/src/common-auth-embedded/limits/signal.d.ts +41 -0
- package/dist/src/common-auth-embedded/limits/terminal.d.ts +52 -0
- package/dist/src/common-auth-embedded/limits/wake.d.ts +21 -0
- package/dist/src/common-auth-embedded/limits/wording.d.ts +25 -0
- package/dist/src/common-auth-embedded/opencode2/index.d.ts +1 -1
- package/dist/src/common-auth-embedded/opencode2/install.d.ts +4 -3
- package/dist/src/common-auth-embedded/opencode2/install.js +219 -27
- package/dist/src/common-auth-embedded/opencode2/types.d.ts +113 -11
- package/dist/src/common-auth-embedded/routing/admission.d.ts +7 -2
- package/dist/src/common-auth-embedded/routing/admission.js +17 -7
- package/dist/src/common-auth-embedded/routing-accessor/handle.js +10 -0
- package/dist/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
- package/dist/src/common-auth-embedded/routing-accessor/index.js +11 -10
- package/dist/src/common-auth-embedded/rpc/port-file.d.ts +12 -3
- package/dist/src/common-auth-embedded/rpc/port-file.js +46 -4
- package/dist/src/common-auth-embedded/setup/enroll.js +7 -1
- package/dist/src/common-auth-embedded/source-output.json +260 -124
- package/dist/src/common-auth-embedded/store/attribution.js +3 -2
- package/dist/src/common-auth-embedded/store/errors.d.ts +1 -1
- package/dist/src/common-auth-embedded/store/hooks.d.ts +5 -1
- package/dist/src/common-auth-embedded/store/hooks.js +20 -5
- package/dist/src/common-auth-embedded/store/index.d.ts +1 -0
- package/dist/src/common-auth-embedded/store/limits-namespace.d.ts +13 -0
- package/dist/src/common-auth-embedded/store/limits-namespace.js +46 -0
- package/dist/src/common-auth-embedded/store/pool.d.ts +10 -1
- package/dist/src/common-auth-embedded/store/pool.js +2 -0
- package/dist/src/common-auth-embedded/store/provider-state.js +5 -5
- package/dist/src/common-auth-embedded/store/refresh.js +1 -1
- package/dist/src/common-auth-embedded/store/schema.js +9 -4
- package/dist/src/common-auth-embedded/store/settings.d.ts +2 -0
- package/dist/src/common-auth-embedded/store/settings.js +11 -4
- package/dist/src/plugin/__fixtures__/release-model-ids.d.ts.map +1 -1
- package/dist/src/plugin/__fixtures__/release-model-ids.js +0 -1
- package/dist/src/plugin/__fixtures__/release-model-ids.js.map +1 -1
- package/dist/src/plugin/__fixtures__/verified-store.js +2 -2
- package/dist/src/plugin/account-access.js +1 -1
- package/dist/src/plugin/request.d.ts.map +1 -1
- package/dist/src/plugin/request.js +3 -2
- package/dist/src/plugin/request.js.map +1 -1
- package/dist/src/plugin/shared/request-services.d.ts +1 -1
- package/dist/src/plugin/shared/request-services.js +1 -1
- package/dist/src/runtime-layout.d.ts +9 -2
- package/dist/src/runtime-layout.d.ts.map +1 -1
- package/dist/src/runtime-layout.js +8 -8
- package/dist/src/runtime-layout.js.map +1 -1
- package/dist/src/setup.d.ts.map +1 -1
- package/dist/src/setup.js +29 -40
- 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/cli.d.ts +1 -0
- package/dist/types/src/cli.d.ts.map +1 -1
- package/dist/types/src/common-auth-embedded/claustrum/consumer.d.ts +12 -0
- package/dist/types/src/common-auth-embedded/claustrum/custody.d.ts +16 -4
- package/dist/types/src/common-auth-embedded/claustrum/hub.d.ts +3 -2
- package/dist/types/src/common-auth-embedded/claustrum/index.d.ts +1 -0
- package/dist/types/src/common-auth-embedded/claustrum/roster.d.ts +4 -0
- package/dist/types/src/common-auth-embedded/claustrum/usage.d.ts +41 -0
- package/dist/types/src/common-auth-embedded/commands/builtins.d.ts +5 -0
- package/dist/types/src/common-auth-embedded/limits/activity.d.ts +26 -0
- package/dist/types/src/common-auth-embedded/limits/axes.d.ts +69 -0
- package/dist/types/src/common-auth-embedded/limits/decide.d.ts +146 -0
- package/dist/types/src/common-auth-embedded/limits/duration.d.ts +8 -0
- package/dist/types/src/common-auth-embedded/limits/families.d.ts +59 -0
- package/dist/types/src/common-auth-embedded/limits/marker.d.ts +69 -0
- package/dist/types/src/common-auth-embedded/limits/request-history.d.ts +79 -0
- package/dist/types/src/common-auth-embedded/limits/scope.d.ts +12 -0
- package/dist/types/src/common-auth-embedded/limits/signal.d.ts +41 -0
- package/dist/types/src/common-auth-embedded/limits/terminal.d.ts +52 -0
- package/dist/types/src/common-auth-embedded/limits/wake.d.ts +21 -0
- package/dist/types/src/common-auth-embedded/limits/wording.d.ts +25 -0
- package/dist/types/src/common-auth-embedded/opencode2/index.d.ts +1 -1
- package/dist/types/src/common-auth-embedded/opencode2/install.d.ts +4 -3
- package/dist/types/src/common-auth-embedded/opencode2/types.d.ts +113 -11
- package/dist/types/src/common-auth-embedded/routing/admission.d.ts +7 -2
- package/dist/types/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
- package/dist/types/src/common-auth-embedded/store/errors.d.ts +1 -1
- package/dist/types/src/common-auth-embedded/store/hooks.d.ts +5 -1
- package/dist/types/src/common-auth-embedded/store/index.d.ts +1 -0
- package/dist/types/src/common-auth-embedded/store/limits-namespace.d.ts +13 -0
- package/dist/types/src/common-auth-embedded/store/pool.d.ts +10 -1
- package/dist/types/src/common-auth-embedded/store/settings.d.ts +2 -0
- package/dist/types/src/plugin/request.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/request-services.d.ts +1 -1
- package/dist/types/src/runtime-layout.d.ts +9 -2
- package/dist/types/src/runtime-layout.d.ts.map +1 -1
- package/dist/types/src/setup.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/common-auth-embedded/claustrum/consumer.d.ts +12 -0
- package/src/common-auth-embedded/claustrum/consumer.js +36 -0
- package/src/common-auth-embedded/claustrum/custody.d.ts +16 -4
- package/src/common-auth-embedded/claustrum/custody.js +74 -9
- package/src/common-auth-embedded/claustrum/hub.d.ts +3 -2
- package/src/common-auth-embedded/claustrum/hub.js +8 -0
- package/src/common-auth-embedded/claustrum/index.d.ts +1 -0
- package/src/common-auth-embedded/claustrum/roster.d.ts +4 -0
- package/src/common-auth-embedded/claustrum/roster.js +34 -8
- package/src/common-auth-embedded/claustrum/usage.d.ts +41 -0
- package/src/common-auth-embedded/claustrum/usage.js +145 -0
- package/src/common-auth-embedded/commands/builtins.d.ts +5 -0
- package/src/common-auth-embedded/commands/builtins.js +38 -0
- package/src/common-auth-embedded/fs/file-read-cache.js +10 -5
- package/src/common-auth-embedded/fs/private-files.d.ts +5 -0
- package/src/common-auth-embedded/fs/private-files.js +7 -0
- package/src/common-auth-embedded/fs/refresh-file-lock.js +142 -47
- package/src/common-auth-embedded/fs/with-lock.js +2 -5
- package/src/common-auth-embedded/limits/activity.d.ts +26 -0
- package/src/common-auth-embedded/limits/axes.d.ts +69 -0
- package/src/common-auth-embedded/limits/decide.d.ts +146 -0
- package/src/common-auth-embedded/limits/duration.d.ts +8 -0
- package/src/common-auth-embedded/limits/families.d.ts +59 -0
- package/src/common-auth-embedded/limits/marker.d.ts +69 -0
- package/src/common-auth-embedded/limits/request-history.d.ts +79 -0
- package/src/common-auth-embedded/limits/scope.d.ts +12 -0
- package/src/common-auth-embedded/limits/signal.d.ts +41 -0
- package/src/common-auth-embedded/limits/terminal.d.ts +52 -0
- package/src/common-auth-embedded/limits/wake.d.ts +21 -0
- package/src/common-auth-embedded/limits/wording.d.ts +25 -0
- package/src/common-auth-embedded/opencode2/index.d.ts +1 -1
- package/src/common-auth-embedded/opencode2/install.d.ts +4 -3
- package/src/common-auth-embedded/opencode2/install.js +219 -27
- package/src/common-auth-embedded/opencode2/types.d.ts +113 -11
- package/src/common-auth-embedded/routing/admission.d.ts +7 -2
- package/src/common-auth-embedded/routing/admission.js +17 -7
- package/src/common-auth-embedded/routing-accessor/handle.js +10 -0
- package/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
- package/src/common-auth-embedded/routing-accessor/index.js +11 -10
- package/src/common-auth-embedded/rpc/port-file.d.ts +12 -3
- package/src/common-auth-embedded/rpc/port-file.js +46 -4
- package/src/common-auth-embedded/setup/enroll.js +7 -1
- package/src/common-auth-embedded/source-output.json +260 -124
- package/src/common-auth-embedded/store/attribution.js +3 -2
- package/src/common-auth-embedded/store/errors.d.ts +1 -1
- package/src/common-auth-embedded/store/hooks.d.ts +5 -1
- package/src/common-auth-embedded/store/hooks.js +20 -5
- package/src/common-auth-embedded/store/index.d.ts +1 -0
- package/src/common-auth-embedded/store/limits-namespace.d.ts +13 -0
- package/src/common-auth-embedded/store/limits-namespace.js +46 -0
- package/src/common-auth-embedded/store/pool.d.ts +10 -1
- package/src/common-auth-embedded/store/pool.js +2 -0
- package/src/common-auth-embedded/store/provider-state.js +5 -5
- package/src/common-auth-embedded/store/refresh.js +1 -1
- package/src/common-auth-embedded/store/schema.js +9 -4
- package/src/common-auth-embedded/store/settings.d.ts +2 -0
- package/src/common-auth-embedded/store/settings.js +11 -4
- package/src/tui-compiled/common-auth-embedded/fs/private-files.js +7 -0
- package/src/tui-compiled/common-auth-embedded/rpc/port-file.js +46 -4
- package/src/tui-raw/common-auth-embedded/fs/private-files.js +7 -0
- package/src/tui-raw/common-auth-embedded/rpc/port-file.js +46 -4
|
@@ -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
|
|
@@ -101,7 +101,8 @@ function applyHeaderEditsTo(target, edits) {
|
|
|
101
101
|
* - `retry` asks the host to retry at once when an account was refused
|
|
102
102
|
* before any output and `chooseAccount` names another account for the
|
|
103
103
|
* next attempt, keeps the host's decision when the refused account was the
|
|
104
|
-
* last one that can serve
|
|
104
|
+
* last one that can serve. After output, only a witnessed HTTP read failure
|
|
105
|
+
* with adapter-approved same-account continuation can keep the host's retry.
|
|
105
106
|
*
|
|
106
107
|
* Attribution. A send belongs to the attempt named by the mark
|
|
107
108
|
* `model.request` left on it, so several attempts of one session and kind
|
|
@@ -134,6 +135,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
134
135
|
ownership.credential(headers) === placeholderSecret(providerID);
|
|
135
136
|
const logger = options.logger;
|
|
136
137
|
const maxRecords = Math.max(1, options.maxRecords ?? DEFAULT_MAX_RECORDS);
|
|
138
|
+
const hostRetryLimit = options.hostRetryLimit ?? 10;
|
|
137
139
|
const forbidden = [
|
|
138
140
|
...(options.hostCredentials ?? [placeholderSecret(providerID)]),
|
|
139
141
|
// An owned request must replace its placeholder even if a caller customizes
|
|
@@ -203,6 +205,21 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
203
205
|
catch { }
|
|
204
206
|
};
|
|
205
207
|
const describe = (error) => error instanceof Error ? error.message : String(error);
|
|
208
|
+
const retireRequest = (request) => request.excluded.clear();
|
|
209
|
+
const exclusions = (request) => request.excluded.size === 0 ? {} : { excluded: [...request.excluded] };
|
|
210
|
+
const rejectsExcluded = (accountId, input, request) => {
|
|
211
|
+
if (!options.enforceRequestExclusion ||
|
|
212
|
+
accountId === undefined ||
|
|
213
|
+
!request.excluded.has(accountId))
|
|
214
|
+
return false;
|
|
215
|
+
warn('opencode2 auth ignored a chooser result for a request-excluded account', {
|
|
216
|
+
sessionID: input.sessionID,
|
|
217
|
+
kind: input.kind,
|
|
218
|
+
phase: input.phase,
|
|
219
|
+
accountId,
|
|
220
|
+
});
|
|
221
|
+
return true;
|
|
222
|
+
};
|
|
206
223
|
// Adapter choices may enqueue work by session. Serialize their consumption
|
|
207
224
|
// and release so a new choice cannot overtake cleanup, even after forgetting.
|
|
208
225
|
const choiceWork = new Map();
|
|
@@ -296,6 +313,11 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
296
313
|
};
|
|
297
314
|
if (error)
|
|
298
315
|
rec.endError = error;
|
|
316
|
+
// An error status can end before retry runs, and a limit may have arrived
|
|
317
|
+
// on an earlier event. Neither is a successful logical request.
|
|
318
|
+
if (error?.reason === 'cancelled' ||
|
|
319
|
+
(!error && !rec.limit && (rec.status === undefined || rec.status < 400)))
|
|
320
|
+
retireRequest(rec.request);
|
|
299
321
|
rec.ended = deliverEnd(rec.attempt, outcome);
|
|
300
322
|
};
|
|
301
323
|
const abandon = (rec, message) => finish(rec, { reason: 'abandoned', message });
|
|
@@ -399,10 +421,28 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
399
421
|
};
|
|
400
422
|
const select = async (scope, hook, assertActive, transport) => {
|
|
401
423
|
const key = keyOf(scope.sessionID, scope.kind);
|
|
402
|
-
const { input, accountId, sameAccountRetry } = await afterChoiceWork(scope.sessionID, async () => {
|
|
424
|
+
const { input, accountId, sameAccountRetry, request } = await afterChoiceWork(scope.sessionID, async () => {
|
|
403
425
|
assertActive();
|
|
404
426
|
const retried = pendingRetries.get(key);
|
|
405
427
|
pendingRetries.delete(key);
|
|
428
|
+
const others = attemptsOf(key);
|
|
429
|
+
if (retried &&
|
|
430
|
+
others.some((rec) => rec.id !== retried.attemptId && rec.attempt && !rec.ended))
|
|
431
|
+
warn('opencode2 auth bound a pending retry to an ambiguous concurrent request; keeping its exclusions', {
|
|
432
|
+
sessionID: scope.sessionID,
|
|
433
|
+
kind: scope.kind,
|
|
434
|
+
});
|
|
435
|
+
if (!retried) {
|
|
436
|
+
// A failed attempt that the host never retried must not seed the next
|
|
437
|
+
// logical request. Live attempts keep their own independent tokens.
|
|
438
|
+
const live = new Set(others
|
|
439
|
+
.filter((rec) => rec.attempt && !rec.ended)
|
|
440
|
+
.map((rec) => rec.request));
|
|
441
|
+
for (const rec of others)
|
|
442
|
+
if (!live.has(rec.request))
|
|
443
|
+
retireRequest(rec.request);
|
|
444
|
+
}
|
|
445
|
+
const request = retried?.request ?? { excluded: new Set() };
|
|
406
446
|
const previousAccountId = retried?.accountId ?? newestOf(key)?.accountId;
|
|
407
447
|
supersede(key);
|
|
408
448
|
const next = retried?.next;
|
|
@@ -413,13 +453,18 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
413
453
|
const matches = next !== undefined &&
|
|
414
454
|
next.input.modelID === scope.modelID &&
|
|
415
455
|
next.input.agent === scope.agent;
|
|
456
|
+
const wake = retried?.wake;
|
|
416
457
|
const input = {
|
|
417
458
|
...scope,
|
|
418
459
|
phase: 'request',
|
|
460
|
+
...exclusions(request),
|
|
419
461
|
...(previousAccountId === undefined ? {} : { previousAccountId }),
|
|
420
462
|
...(retried?.rerouteFrom ? { rerouteFrom: retried.rerouteFrom } : {}),
|
|
463
|
+
...(wake === undefined
|
|
464
|
+
? {}
|
|
465
|
+
: { wake, hostAttempt: next?.input.hostAttempt }),
|
|
421
466
|
};
|
|
422
|
-
if (next && !matches) {
|
|
467
|
+
if (next && (!matches || wake !== undefined)) {
|
|
423
468
|
await inChoiceOrder(scope.sessionID, () => releaseChoice(next));
|
|
424
469
|
assertActive();
|
|
425
470
|
}
|
|
@@ -427,13 +472,28 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
427
472
|
await inChoiceOrder(scope.sessionID, () => releaseSameAccount(sameAccount));
|
|
428
473
|
assertActive();
|
|
429
474
|
}
|
|
475
|
+
if (sameAccount?.continuation &&
|
|
476
|
+
(!sameRoute || mixed(scope.sessionID))) {
|
|
477
|
+
if (sameRoute)
|
|
478
|
+
await inChoiceOrder(scope.sessionID, () => releaseSameAccount(sameAccount));
|
|
479
|
+
throw noAccount(scope);
|
|
480
|
+
}
|
|
430
481
|
const choice = sameRoute
|
|
431
482
|
? sameAccount.accountId
|
|
432
|
-
: matches
|
|
483
|
+
: matches && wake === undefined
|
|
433
484
|
? next.accountId
|
|
434
485
|
: await adapter.chooseAccount(input);
|
|
435
|
-
|
|
436
|
-
|
|
486
|
+
let accountId = typeof choice === 'string' ? choice : undefined;
|
|
487
|
+
if (rejectsExcluded(accountId, input, request)) {
|
|
488
|
+
const denied = accountId;
|
|
489
|
+
if (sameRoute && sameAccount)
|
|
490
|
+
await inChoiceOrder(scope.sessionID, () => releaseSameAccount(sameAccount));
|
|
491
|
+
else if (denied !== undefined)
|
|
492
|
+
await inChoiceOrder(scope.sessionID, () => releaseChoice(matches && next ? next : { accountId: denied, input }));
|
|
493
|
+
accountId = undefined;
|
|
494
|
+
assertActive();
|
|
495
|
+
}
|
|
496
|
+
return { input, accountId, sameAccountRetry: sameRoute, request };
|
|
437
497
|
});
|
|
438
498
|
assertActive();
|
|
439
499
|
let headers = {};
|
|
@@ -454,6 +514,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
454
514
|
id,
|
|
455
515
|
key,
|
|
456
516
|
scope,
|
|
517
|
+
request,
|
|
457
518
|
accountId,
|
|
458
519
|
headers,
|
|
459
520
|
attempt: accountId === undefined
|
|
@@ -577,6 +638,8 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
577
638
|
noteQuota(rec, transport, verdict.quota);
|
|
578
639
|
if (verdict.outputStarted)
|
|
579
640
|
rec.outputStarted = true;
|
|
641
|
+
if (verdict.hostRetry === 'veto')
|
|
642
|
+
rec.hostRetry = 'veto';
|
|
580
643
|
if (verdict.limit)
|
|
581
644
|
noteLimit(rec, verdict.limit, transport);
|
|
582
645
|
if (verdict.error !== undefined)
|
|
@@ -740,20 +803,34 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
740
803
|
error: describe(error),
|
|
741
804
|
})));
|
|
742
805
|
}
|
|
743
|
-
response = new Response(trackBody(body, (error) =>
|
|
806
|
+
response = new Response(trackBody(body, (error) => {
|
|
807
|
+
if (error?.reason === 'failed')
|
|
808
|
+
rec.httpReadFailed = true;
|
|
809
|
+
finish(rec, error);
|
|
810
|
+
}), {
|
|
744
811
|
status: original.status,
|
|
745
812
|
statusText: original.statusText,
|
|
746
813
|
headers: original.headers,
|
|
747
814
|
});
|
|
748
815
|
}
|
|
749
816
|
if (adapter.rewriteResponse) {
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
817
|
+
const rewritten = await adapter.rewriteResponse({
|
|
818
|
+
...account,
|
|
819
|
+
request: draft.request,
|
|
820
|
+
response,
|
|
821
|
+
attempt,
|
|
822
|
+
});
|
|
823
|
+
response = rewritten ?? response;
|
|
824
|
+
}
|
|
825
|
+
if (adapter.responseRetry) {
|
|
826
|
+
const disposition = await adapter.responseRetry({
|
|
827
|
+
...account,
|
|
828
|
+
request: draft.request,
|
|
829
|
+
response,
|
|
830
|
+
attempt,
|
|
831
|
+
});
|
|
832
|
+
if (disposition === 'veto')
|
|
833
|
+
rec.hostRetry = 'veto';
|
|
757
834
|
}
|
|
758
835
|
if (response !== original)
|
|
759
836
|
draft.response = response;
|
|
@@ -865,13 +942,44 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
865
942
|
return;
|
|
866
943
|
}
|
|
867
944
|
const rec = picked;
|
|
945
|
+
const vetoed = () => rec.hostRetry === 'veto';
|
|
868
946
|
await withSelection(rec.scope, async (assertActive) => {
|
|
869
947
|
const checkRetryActive = assertActive;
|
|
870
948
|
const retrySameAccount = adapter.retrySameAccount?.bind(adapter);
|
|
871
949
|
const handle = rec.attempt;
|
|
950
|
+
const afterOutput = rec.outputStarted;
|
|
951
|
+
if (afterOutput &&
|
|
952
|
+
rec.httpReadFailed &&
|
|
953
|
+
retrySameAccount &&
|
|
954
|
+
handle &&
|
|
955
|
+
!mixed(draft.sessionID)) {
|
|
956
|
+
if (!rec.limit) {
|
|
957
|
+
const signal = adapter.limitFromError?.(draft.error, handle);
|
|
958
|
+
if (signal)
|
|
959
|
+
noteLimit(rec, signal, 'error');
|
|
960
|
+
}
|
|
961
|
+
if (rec.ended)
|
|
962
|
+
await rec.ended;
|
|
963
|
+
if (rec.limit)
|
|
964
|
+
await rec.limit.delivered;
|
|
965
|
+
checkRetryActive();
|
|
966
|
+
}
|
|
967
|
+
// The host drops read-operation metadata from its retry error. Only
|
|
968
|
+
// our attributed body reader can prove a read failed; wording cannot.
|
|
969
|
+
const canContinue = () => rec.httpReadFailed === true &&
|
|
970
|
+
draft.error.type === 'provider.transport' &&
|
|
971
|
+
hostDecision.retry &&
|
|
972
|
+
hostRetryLimit - draft.attempt >= 1 &&
|
|
973
|
+
!rec.limit &&
|
|
974
|
+
!(rec.status !== undefined && rec.status >= 400) &&
|
|
975
|
+
!(draft.error.status !== undefined && draft.error.status >= 400) &&
|
|
976
|
+
!handle?.reboundFrom &&
|
|
977
|
+
!vetoed() &&
|
|
978
|
+
!mixed(draft.sessionID);
|
|
872
979
|
if (retrySameAccount &&
|
|
873
980
|
handle &&
|
|
874
|
-
!
|
|
981
|
+
(!afterOutput || canContinue()) &&
|
|
982
|
+
(!vetoed() || mixed(draft.sessionID)) &&
|
|
875
983
|
!rec.sameAccountRetry) {
|
|
876
984
|
if (rec.ended)
|
|
877
985
|
await rec.ended;
|
|
@@ -883,7 +991,9 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
883
991
|
const active = () => attempts.has(rec.id) &&
|
|
884
992
|
rec.accountId === accountId &&
|
|
885
993
|
pickForRetry(draft.sessionID) === rec &&
|
|
886
|
-
|
|
994
|
+
rec.outputStarted === afterOutput &&
|
|
995
|
+
(!afterOutput || canContinue()) &&
|
|
996
|
+
(!vetoed() || mixed(draft.sessionID)) &&
|
|
887
997
|
(!ownership ||
|
|
888
998
|
latestTransportOwned.get(draft.sessionID) === true);
|
|
889
999
|
if (!active() || rec.sameAccountRetry)
|
|
@@ -895,6 +1005,9 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
895
1005
|
error: draft.error,
|
|
896
1006
|
attempt: draft.attempt,
|
|
897
1007
|
...(rec.limit ? { limit: rec.limit.signal } : {}),
|
|
1008
|
+
...(afterOutput
|
|
1009
|
+
? { continuation: 'interrupted-read' }
|
|
1010
|
+
: {}),
|
|
898
1011
|
};
|
|
899
1012
|
let result;
|
|
900
1013
|
try {
|
|
@@ -910,7 +1023,11 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
910
1023
|
checkRetryActive();
|
|
911
1024
|
return false;
|
|
912
1025
|
}
|
|
913
|
-
const sameAccount = {
|
|
1026
|
+
const sameAccount = {
|
|
1027
|
+
accountId: result.accountId,
|
|
1028
|
+
input,
|
|
1029
|
+
...(afterOutput ? { continuation: true } : {}),
|
|
1030
|
+
};
|
|
914
1031
|
try {
|
|
915
1032
|
checkRetryActive();
|
|
916
1033
|
}
|
|
@@ -940,6 +1057,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
940
1057
|
// Pin only this retry's route: choosing afresh could turn a token
|
|
941
1058
|
// refresh or path recovery into an account switch in a mixed session.
|
|
942
1059
|
pendingRetries.set(rec.key, {
|
|
1060
|
+
request: rec.request,
|
|
943
1061
|
attemptId: rec.id,
|
|
944
1062
|
sessionID: rec.scope.sessionID,
|
|
945
1063
|
order: ++retryOrder,
|
|
@@ -947,7 +1065,9 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
947
1065
|
sameAccount,
|
|
948
1066
|
});
|
|
949
1067
|
rec.judged = true;
|
|
950
|
-
draft.decision =
|
|
1068
|
+
draft.decision = afterOutput
|
|
1069
|
+
? hostDecision
|
|
1070
|
+
: { retry: true, delay: 0 };
|
|
951
1071
|
return true;
|
|
952
1072
|
});
|
|
953
1073
|
checkRetryActive();
|
|
@@ -957,7 +1077,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
957
1077
|
accountId: rec.accountId,
|
|
958
1078
|
kind: rec.scope.kind,
|
|
959
1079
|
attempt: draft.attempt,
|
|
960
|
-
reason: 'same-account',
|
|
1080
|
+
reason: afterOutput ? 'continue-same-account' : 'same-account',
|
|
961
1081
|
hostDecision,
|
|
962
1082
|
decision: draft.decision,
|
|
963
1083
|
handle: rec.attempt,
|
|
@@ -1006,16 +1126,22 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1006
1126
|
let reason;
|
|
1007
1127
|
let rerouteFrom;
|
|
1008
1128
|
let next;
|
|
1129
|
+
let wake;
|
|
1009
1130
|
if (rec.accountId === undefined || !attempt) {
|
|
1010
1131
|
decision = { retry: false };
|
|
1011
1132
|
reason = 'no-account';
|
|
1012
1133
|
}
|
|
1013
1134
|
else if (rec.outputStarted) {
|
|
1014
|
-
//
|
|
1015
|
-
// send it again.
|
|
1135
|
+
// Only the guarded same-account path above may continue partial output.
|
|
1016
1136
|
decision = { retry: false };
|
|
1017
1137
|
reason = 'output-started';
|
|
1018
1138
|
}
|
|
1139
|
+
else if (vetoed()) {
|
|
1140
|
+
// Refusals formatted by the adapter carry policy on the attributed attempt,
|
|
1141
|
+
// not in message wording or a session-wide flag.
|
|
1142
|
+
decision = { retry: false };
|
|
1143
|
+
reason = 'adapter-veto';
|
|
1144
|
+
}
|
|
1019
1145
|
else {
|
|
1020
1146
|
if (rec.limit) {
|
|
1021
1147
|
const refused = {
|
|
@@ -1032,6 +1158,8 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1032
1158
|
const input = {
|
|
1033
1159
|
...rec.scope,
|
|
1034
1160
|
phase: 'retry',
|
|
1161
|
+
...exclusions(rec.request),
|
|
1162
|
+
hostAttempt: draft.attempt,
|
|
1035
1163
|
previousAccountId: rec.accountId,
|
|
1036
1164
|
rerouteFrom: refused,
|
|
1037
1165
|
};
|
|
@@ -1046,7 +1174,25 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1046
1174
|
error: describe(error),
|
|
1047
1175
|
});
|
|
1048
1176
|
}
|
|
1049
|
-
const
|
|
1177
|
+
const chosenAccount = typeof choice === 'string'
|
|
1178
|
+
? choice
|
|
1179
|
+
: typeof choice === 'object' && 'hold' in choice
|
|
1180
|
+
? rec.accountId
|
|
1181
|
+
: undefined;
|
|
1182
|
+
if (rejectsExcluded(chosenAccount, input, rec.request)) {
|
|
1183
|
+
if (chosenAccount !== undefined)
|
|
1184
|
+
await releaseChoice({ accountId: chosenAccount, input });
|
|
1185
|
+
choice = undefined;
|
|
1186
|
+
}
|
|
1187
|
+
const hold = typeof choice === 'object' && 'hold' in choice
|
|
1188
|
+
? choice.hold
|
|
1189
|
+
: undefined;
|
|
1190
|
+
const wait = typeof choice === 'object' && 'waitThenReselect' in choice
|
|
1191
|
+
? choice.waitThenReselect
|
|
1192
|
+
: undefined;
|
|
1193
|
+
const disposition = typeof choice === 'object' && 'hostRetry' in choice
|
|
1194
|
+
? choice.hostRetry
|
|
1195
|
+
: undefined;
|
|
1050
1196
|
const accountId = typeof choice === 'string'
|
|
1051
1197
|
? choice
|
|
1052
1198
|
: hold
|
|
@@ -1073,21 +1219,61 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1073
1219
|
reason: 'host-decides',
|
|
1074
1220
|
};
|
|
1075
1221
|
}
|
|
1076
|
-
if (
|
|
1222
|
+
if (vetoed() || disposition === 'veto') {
|
|
1223
|
+
if (next)
|
|
1224
|
+
await releaseChoice(next);
|
|
1225
|
+
next = undefined;
|
|
1226
|
+
checkRetryActive();
|
|
1227
|
+
if (mixed(draft.sessionID))
|
|
1228
|
+
return {
|
|
1229
|
+
decision: hostDecision,
|
|
1230
|
+
reason: 'host-decides',
|
|
1231
|
+
};
|
|
1232
|
+
rec.hostRetry = 'veto';
|
|
1233
|
+
decision = { retry: false };
|
|
1234
|
+
reason = 'adapter-veto';
|
|
1235
|
+
}
|
|
1236
|
+
else if (!(hostRetryLimit - draft.attempt >= 1)) {
|
|
1237
|
+
if (next)
|
|
1238
|
+
await releaseChoice(next);
|
|
1239
|
+
next = undefined;
|
|
1240
|
+
reason = 'host-decides-last-account';
|
|
1241
|
+
}
|
|
1242
|
+
else if (wait &&
|
|
1243
|
+
Number.isFinite(wait.delayMs) &&
|
|
1244
|
+
wait.delayMs >= 0 &&
|
|
1245
|
+
wait.delayMs <= MAX_RETRY_HOLD_MS) {
|
|
1246
|
+
// Only the failed attempt's context is kept. No next choice
|
|
1247
|
+
// exists to dispatch or release when the host wakes or cancels.
|
|
1248
|
+
decision = { retry: true, delay: Math.ceil(wait.delayMs) };
|
|
1249
|
+
reason = 'wait-then-reselect';
|
|
1250
|
+
}
|
|
1251
|
+
else if (hold &&
|
|
1077
1252
|
Number.isFinite(hold.delayMs) &&
|
|
1078
|
-
hold.delayMs >= 0
|
|
1253
|
+
hold.delayMs >= 0 &&
|
|
1254
|
+
hold.delayMs <= MAX_RETRY_HOLD_MS) {
|
|
1079
1255
|
// A recognised account limit can be recoverable even when
|
|
1080
1256
|
// the host treats its provider error as final.
|
|
1081
1257
|
decision = {
|
|
1082
1258
|
retry: true,
|
|
1083
|
-
delay: Math.
|
|
1259
|
+
delay: Math.ceil(hold.delayMs),
|
|
1084
1260
|
};
|
|
1261
|
+
if (hold.target !== undefined) {
|
|
1262
|
+
wake = {
|
|
1263
|
+
target: hold.target,
|
|
1264
|
+
...(hold.checkpoint === undefined
|
|
1265
|
+
? {}
|
|
1266
|
+
: { checkpoint: hold.checkpoint }),
|
|
1267
|
+
...(hold.kind === undefined ? {} : { kind: hold.kind }),
|
|
1268
|
+
};
|
|
1269
|
+
}
|
|
1085
1270
|
reason = 'hold';
|
|
1086
1271
|
}
|
|
1087
1272
|
else if (!hold &&
|
|
1088
1273
|
accountId !== undefined &&
|
|
1089
1274
|
accountId !== rec.accountId) {
|
|
1090
1275
|
rec.rerouted = true;
|
|
1276
|
+
rec.request.excluded.add(rec.accountId);
|
|
1091
1277
|
rerouteFrom = refused;
|
|
1092
1278
|
// The next attempt takes this choice rather than asking
|
|
1093
1279
|
// again, so an account limited between here and the host's
|
|
@@ -1121,12 +1307,14 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1121
1307
|
rec.accountId !== undefined &&
|
|
1122
1308
|
attempts.has(rec.id)) {
|
|
1123
1309
|
pendingRetries.set(rec.key, {
|
|
1310
|
+
request: rec.request,
|
|
1124
1311
|
attemptId: rec.id,
|
|
1125
1312
|
sessionID: rec.scope.sessionID,
|
|
1126
1313
|
order: ++retryOrder,
|
|
1127
1314
|
accountId: rec.accountId,
|
|
1128
1315
|
...(rerouteFrom === undefined ? {} : { rerouteFrom }),
|
|
1129
1316
|
...(next === undefined ? {} : { next }),
|
|
1317
|
+
...(wake === undefined ? {} : { wake }),
|
|
1130
1318
|
});
|
|
1131
1319
|
}
|
|
1132
1320
|
else if (next) {
|
|
@@ -1134,6 +1322,8 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1134
1322
|
// have been overridden by a reroute to another account.
|
|
1135
1323
|
await releaseChoice(next);
|
|
1136
1324
|
}
|
|
1325
|
+
if (!decision.retry)
|
|
1326
|
+
retireRequest(rec.request);
|
|
1137
1327
|
return { decision, reason };
|
|
1138
1328
|
});
|
|
1139
1329
|
checkRetryActive();
|
|
@@ -1166,6 +1356,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1166
1356
|
if (rec.scope.sessionID !== sessionID)
|
|
1167
1357
|
continue;
|
|
1168
1358
|
abandon(rec, 'its session was forgotten');
|
|
1359
|
+
retireRequest(rec.request);
|
|
1169
1360
|
attempts.delete(rec.id);
|
|
1170
1361
|
}
|
|
1171
1362
|
for (const [key, pending] of pendingRetries) {
|
|
@@ -1192,6 +1383,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1192
1383
|
if (pending.sessionID === sessionID &&
|
|
1193
1384
|
pending.order < idleOrder) {
|
|
1194
1385
|
pendingRetries.delete(key);
|
|
1386
|
+
retireRequest(pending.request);
|
|
1195
1387
|
await releaseRetry(pending);
|
|
1196
1388
|
}
|
|
1197
1389
|
}
|