@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.
Files changed (188) hide show
  1. package/dist/cli.js +783 -254
  2. package/dist/cli.js.map +4 -4
  3. package/dist/cli.meta.json +145 -77
  4. package/dist/index.js +842 -297
  5. package/dist/index.js.map +4 -4
  6. package/dist/index.meta.json +177 -85
  7. package/dist/server.js +842 -300
  8. package/dist/server.js.map +4 -4
  9. package/dist/server.meta.json +1 -1
  10. package/dist/src/cli.d.ts +1 -0
  11. package/dist/src/cli.d.ts.map +1 -1
  12. package/dist/src/cli.js +22 -2
  13. package/dist/src/cli.js.map +1 -1
  14. package/dist/src/common-auth-embedded/claustrum/consumer.d.ts +12 -0
  15. package/dist/src/common-auth-embedded/claustrum/consumer.js +36 -0
  16. package/dist/src/common-auth-embedded/claustrum/custody.d.ts +16 -4
  17. package/dist/src/common-auth-embedded/claustrum/custody.js +74 -9
  18. package/dist/src/common-auth-embedded/claustrum/hub.d.ts +3 -2
  19. package/dist/src/common-auth-embedded/claustrum/hub.js +8 -0
  20. package/dist/src/common-auth-embedded/claustrum/index.d.ts +1 -0
  21. package/dist/src/common-auth-embedded/claustrum/roster.d.ts +4 -0
  22. package/dist/src/common-auth-embedded/claustrum/roster.js +34 -8
  23. package/dist/src/common-auth-embedded/claustrum/usage.d.ts +41 -0
  24. package/dist/src/common-auth-embedded/claustrum/usage.js +145 -0
  25. package/dist/src/common-auth-embedded/commands/builtins.d.ts +5 -0
  26. package/dist/src/common-auth-embedded/commands/builtins.js +38 -0
  27. package/dist/src/common-auth-embedded/fs/file-read-cache.js +10 -5
  28. package/dist/src/common-auth-embedded/fs/private-files.d.ts +5 -0
  29. package/dist/src/common-auth-embedded/fs/private-files.js +7 -0
  30. package/dist/src/common-auth-embedded/fs/refresh-file-lock.js +142 -47
  31. package/dist/src/common-auth-embedded/fs/with-lock.js +2 -5
  32. package/dist/src/common-auth-embedded/limits/activity.d.ts +26 -0
  33. package/dist/src/common-auth-embedded/limits/axes.d.ts +69 -0
  34. package/dist/src/common-auth-embedded/limits/decide.d.ts +146 -0
  35. package/dist/src/common-auth-embedded/limits/duration.d.ts +8 -0
  36. package/dist/src/common-auth-embedded/limits/families.d.ts +59 -0
  37. package/dist/src/common-auth-embedded/limits/marker.d.ts +69 -0
  38. package/dist/src/common-auth-embedded/limits/request-history.d.ts +79 -0
  39. package/dist/src/common-auth-embedded/limits/scope.d.ts +12 -0
  40. package/dist/src/common-auth-embedded/limits/signal.d.ts +41 -0
  41. package/dist/src/common-auth-embedded/limits/terminal.d.ts +52 -0
  42. package/dist/src/common-auth-embedded/limits/wake.d.ts +21 -0
  43. package/dist/src/common-auth-embedded/limits/wording.d.ts +25 -0
  44. package/dist/src/common-auth-embedded/opencode2/index.d.ts +1 -1
  45. package/dist/src/common-auth-embedded/opencode2/install.d.ts +4 -3
  46. package/dist/src/common-auth-embedded/opencode2/install.js +219 -27
  47. package/dist/src/common-auth-embedded/opencode2/types.d.ts +113 -11
  48. package/dist/src/common-auth-embedded/routing/admission.d.ts +7 -2
  49. package/dist/src/common-auth-embedded/routing/admission.js +17 -7
  50. package/dist/src/common-auth-embedded/routing-accessor/handle.js +10 -0
  51. package/dist/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
  52. package/dist/src/common-auth-embedded/routing-accessor/index.js +11 -10
  53. package/dist/src/common-auth-embedded/rpc/port-file.d.ts +12 -3
  54. package/dist/src/common-auth-embedded/rpc/port-file.js +46 -4
  55. package/dist/src/common-auth-embedded/setup/enroll.js +7 -1
  56. package/dist/src/common-auth-embedded/source-output.json +260 -124
  57. package/dist/src/common-auth-embedded/store/attribution.js +3 -2
  58. package/dist/src/common-auth-embedded/store/errors.d.ts +1 -1
  59. package/dist/src/common-auth-embedded/store/hooks.d.ts +5 -1
  60. package/dist/src/common-auth-embedded/store/hooks.js +20 -5
  61. package/dist/src/common-auth-embedded/store/index.d.ts +1 -0
  62. package/dist/src/common-auth-embedded/store/limits-namespace.d.ts +13 -0
  63. package/dist/src/common-auth-embedded/store/limits-namespace.js +46 -0
  64. package/dist/src/common-auth-embedded/store/pool.d.ts +10 -1
  65. package/dist/src/common-auth-embedded/store/pool.js +2 -0
  66. package/dist/src/common-auth-embedded/store/provider-state.js +5 -5
  67. package/dist/src/common-auth-embedded/store/refresh.js +1 -1
  68. package/dist/src/common-auth-embedded/store/schema.js +9 -4
  69. package/dist/src/common-auth-embedded/store/settings.d.ts +2 -0
  70. package/dist/src/common-auth-embedded/store/settings.js +11 -4
  71. package/dist/src/plugin/__fixtures__/release-model-ids.d.ts.map +1 -1
  72. package/dist/src/plugin/__fixtures__/release-model-ids.js +0 -1
  73. package/dist/src/plugin/__fixtures__/release-model-ids.js.map +1 -1
  74. package/dist/src/plugin/__fixtures__/verified-store.js +2 -2
  75. package/dist/src/plugin/account-access.js +1 -1
  76. package/dist/src/plugin/request.d.ts.map +1 -1
  77. package/dist/src/plugin/request.js +3 -2
  78. package/dist/src/plugin/request.js.map +1 -1
  79. package/dist/src/plugin/shared/request-services.d.ts +1 -1
  80. package/dist/src/plugin/shared/request-services.js +1 -1
  81. package/dist/src/runtime-layout.d.ts +9 -2
  82. package/dist/src/runtime-layout.d.ts.map +1 -1
  83. package/dist/src/runtime-layout.js +8 -8
  84. package/dist/src/runtime-layout.js.map +1 -1
  85. package/dist/src/setup.d.ts.map +1 -1
  86. package/dist/src/setup.js +29 -40
  87. package/dist/src/setup.js.map +1 -1
  88. package/dist/tui-build-map.json +9 -9
  89. package/dist/tui-ga-build-map.json +1 -1
  90. package/dist/types/src/cli.d.ts +1 -0
  91. package/dist/types/src/cli.d.ts.map +1 -1
  92. package/dist/types/src/common-auth-embedded/claustrum/consumer.d.ts +12 -0
  93. package/dist/types/src/common-auth-embedded/claustrum/custody.d.ts +16 -4
  94. package/dist/types/src/common-auth-embedded/claustrum/hub.d.ts +3 -2
  95. package/dist/types/src/common-auth-embedded/claustrum/index.d.ts +1 -0
  96. package/dist/types/src/common-auth-embedded/claustrum/roster.d.ts +4 -0
  97. package/dist/types/src/common-auth-embedded/claustrum/usage.d.ts +41 -0
  98. package/dist/types/src/common-auth-embedded/commands/builtins.d.ts +5 -0
  99. package/dist/types/src/common-auth-embedded/limits/activity.d.ts +26 -0
  100. package/dist/types/src/common-auth-embedded/limits/axes.d.ts +69 -0
  101. package/dist/types/src/common-auth-embedded/limits/decide.d.ts +146 -0
  102. package/dist/types/src/common-auth-embedded/limits/duration.d.ts +8 -0
  103. package/dist/types/src/common-auth-embedded/limits/families.d.ts +59 -0
  104. package/dist/types/src/common-auth-embedded/limits/marker.d.ts +69 -0
  105. package/dist/types/src/common-auth-embedded/limits/request-history.d.ts +79 -0
  106. package/dist/types/src/common-auth-embedded/limits/scope.d.ts +12 -0
  107. package/dist/types/src/common-auth-embedded/limits/signal.d.ts +41 -0
  108. package/dist/types/src/common-auth-embedded/limits/terminal.d.ts +52 -0
  109. package/dist/types/src/common-auth-embedded/limits/wake.d.ts +21 -0
  110. package/dist/types/src/common-auth-embedded/limits/wording.d.ts +25 -0
  111. package/dist/types/src/common-auth-embedded/opencode2/index.d.ts +1 -1
  112. package/dist/types/src/common-auth-embedded/opencode2/install.d.ts +4 -3
  113. package/dist/types/src/common-auth-embedded/opencode2/types.d.ts +113 -11
  114. package/dist/types/src/common-auth-embedded/routing/admission.d.ts +7 -2
  115. package/dist/types/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
  116. package/dist/types/src/common-auth-embedded/store/errors.d.ts +1 -1
  117. package/dist/types/src/common-auth-embedded/store/hooks.d.ts +5 -1
  118. package/dist/types/src/common-auth-embedded/store/index.d.ts +1 -0
  119. package/dist/types/src/common-auth-embedded/store/limits-namespace.d.ts +13 -0
  120. package/dist/types/src/common-auth-embedded/store/pool.d.ts +10 -1
  121. package/dist/types/src/common-auth-embedded/store/settings.d.ts +2 -0
  122. package/dist/types/src/plugin/request.d.ts.map +1 -1
  123. package/dist/types/src/plugin/shared/request-services.d.ts +1 -1
  124. package/dist/types/src/runtime-layout.d.ts +9 -2
  125. package/dist/types/src/runtime-layout.d.ts.map +1 -1
  126. package/dist/types/src/setup.d.ts.map +1 -1
  127. package/package.json +3 -3
  128. package/src/common-auth-embedded/claustrum/consumer.d.ts +12 -0
  129. package/src/common-auth-embedded/claustrum/consumer.js +36 -0
  130. package/src/common-auth-embedded/claustrum/custody.d.ts +16 -4
  131. package/src/common-auth-embedded/claustrum/custody.js +74 -9
  132. package/src/common-auth-embedded/claustrum/hub.d.ts +3 -2
  133. package/src/common-auth-embedded/claustrum/hub.js +8 -0
  134. package/src/common-auth-embedded/claustrum/index.d.ts +1 -0
  135. package/src/common-auth-embedded/claustrum/roster.d.ts +4 -0
  136. package/src/common-auth-embedded/claustrum/roster.js +34 -8
  137. package/src/common-auth-embedded/claustrum/usage.d.ts +41 -0
  138. package/src/common-auth-embedded/claustrum/usage.js +145 -0
  139. package/src/common-auth-embedded/commands/builtins.d.ts +5 -0
  140. package/src/common-auth-embedded/commands/builtins.js +38 -0
  141. package/src/common-auth-embedded/fs/file-read-cache.js +10 -5
  142. package/src/common-auth-embedded/fs/private-files.d.ts +5 -0
  143. package/src/common-auth-embedded/fs/private-files.js +7 -0
  144. package/src/common-auth-embedded/fs/refresh-file-lock.js +142 -47
  145. package/src/common-auth-embedded/fs/with-lock.js +2 -5
  146. package/src/common-auth-embedded/limits/activity.d.ts +26 -0
  147. package/src/common-auth-embedded/limits/axes.d.ts +69 -0
  148. package/src/common-auth-embedded/limits/decide.d.ts +146 -0
  149. package/src/common-auth-embedded/limits/duration.d.ts +8 -0
  150. package/src/common-auth-embedded/limits/families.d.ts +59 -0
  151. package/src/common-auth-embedded/limits/marker.d.ts +69 -0
  152. package/src/common-auth-embedded/limits/request-history.d.ts +79 -0
  153. package/src/common-auth-embedded/limits/scope.d.ts +12 -0
  154. package/src/common-auth-embedded/limits/signal.d.ts +41 -0
  155. package/src/common-auth-embedded/limits/terminal.d.ts +52 -0
  156. package/src/common-auth-embedded/limits/wake.d.ts +21 -0
  157. package/src/common-auth-embedded/limits/wording.d.ts +25 -0
  158. package/src/common-auth-embedded/opencode2/index.d.ts +1 -1
  159. package/src/common-auth-embedded/opencode2/install.d.ts +4 -3
  160. package/src/common-auth-embedded/opencode2/install.js +219 -27
  161. package/src/common-auth-embedded/opencode2/types.d.ts +113 -11
  162. package/src/common-auth-embedded/routing/admission.d.ts +7 -2
  163. package/src/common-auth-embedded/routing/admission.js +17 -7
  164. package/src/common-auth-embedded/routing-accessor/handle.js +10 -0
  165. package/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
  166. package/src/common-auth-embedded/routing-accessor/index.js +11 -10
  167. package/src/common-auth-embedded/rpc/port-file.d.ts +12 -3
  168. package/src/common-auth-embedded/rpc/port-file.js +46 -4
  169. package/src/common-auth-embedded/setup/enroll.js +7 -1
  170. package/src/common-auth-embedded/source-output.json +260 -124
  171. package/src/common-auth-embedded/store/attribution.js +3 -2
  172. package/src/common-auth-embedded/store/errors.d.ts +1 -1
  173. package/src/common-auth-embedded/store/hooks.d.ts +5 -1
  174. package/src/common-auth-embedded/store/hooks.js +20 -5
  175. package/src/common-auth-embedded/store/index.d.ts +1 -0
  176. package/src/common-auth-embedded/store/limits-namespace.d.ts +13 -0
  177. package/src/common-auth-embedded/store/limits-namespace.js +46 -0
  178. package/src/common-auth-embedded/store/pool.d.ts +10 -1
  179. package/src/common-auth-embedded/store/pool.js +2 -0
  180. package/src/common-auth-embedded/store/provider-state.js +5 -5
  181. package/src/common-auth-embedded/store/refresh.js +1 -1
  182. package/src/common-auth-embedded/store/schema.js +9 -4
  183. package/src/common-auth-embedded/store/settings.d.ts +2 -0
  184. package/src/common-auth-embedded/store/settings.js +11 -4
  185. package/src/tui-compiled/common-auth-embedded/fs/private-files.js +7 -0
  186. package/src/tui-compiled/common-auth-embedded/rpc/port-file.js +46 -4
  187. package/src/tui-raw/common-auth-embedded/fs/private-files.js +7 -0
  188. 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
- /** Bound adapter waits while allowing a 15-minute reset plus session jitter. */
6
- export const MAX_RETRY_HOLD_MS = 20 * 60 * 1000;
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, and refuses to retry once output has started.
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
- const accountId = typeof choice === 'string' ? choice : undefined;
436
- return { input, accountId, sameAccountRetry: sameRoute };
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) => finish(rec, 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
- response =
751
- (await adapter.rewriteResponse({
752
- ...account,
753
- request: draft.request,
754
- response,
755
- attempt,
756
- })) ?? response;
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
- !rec.outputStarted &&
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
- !rec.outputStarted &&
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 = { accountId: result.accountId, input };
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 = { retry: true, delay: 0 };
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
- // The user has already seen part of this answer; a retry would
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 hold = typeof choice === 'object' ? choice.hold : undefined;
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 (hold &&
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.min(hold.delayMs, MAX_RETRY_HOLD_MS),
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
  }