@cortexkit/common-auth 0.39.1 → 0.39.2

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.
@@ -290,6 +290,27 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
290
290
  }
291
291
  }));
292
292
  };
293
+ const emitRetry = async (payload) => {
294
+ const transports = new Set([...attempts.values()]
295
+ .filter((rec) => rec.scope.sessionID === payload.sessionID)
296
+ .map((rec) => rec.transport)
297
+ .filter((transport) => transport !== undefined));
298
+ const transport = payload.handle?.transport ??
299
+ (transports.size === 1 ? [...transports][0] : 'unknown');
300
+ try {
301
+ logger?.info?.('opencode2 auth retry decision', {
302
+ sessionID: payload.sessionID,
303
+ transport,
304
+ attempt: payload.attempt,
305
+ reason: payload.reason === 'host-decides' && mixed(payload.sessionID)
306
+ ? 'mixed-session'
307
+ : payload.reason,
308
+ retry: payload.decision.retry,
309
+ });
310
+ }
311
+ catch { }
312
+ await emit('retry', payload);
313
+ };
293
314
  const deliverEnd = async (attempt, outcome) => {
294
315
  try {
295
316
  await adapter.onAttemptEnd?.(attempt, outcome);
@@ -805,7 +826,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
805
826
  }
806
827
  response = new Response(trackBody(body, (error) => {
807
828
  if (error?.reason === 'failed')
808
- rec.httpReadFailed = true;
829
+ rec.readFailed = true;
809
830
  finish(rec, error);
810
831
  }), {
811
832
  status: original.status,
@@ -915,7 +936,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
915
936
  if (ownership && latestTransportOwned.get(draft.sessionID) !== true)
916
937
  return;
917
938
  const hostDecision = draft.decision;
918
- const hostDecides = () => emit('retry', {
939
+ const hostDecides = () => emitRetry({
919
940
  sessionID: draft.sessionID,
920
941
  attempt: draft.attempt,
921
942
  reason: 'host-decides',
@@ -932,7 +953,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
932
953
  if (picked === 'ambiguous') {
933
954
  // Nothing tells which open attempt failed: keep the host's
934
955
  // decision and leave every attempt as it is.
935
- await emit('retry', {
956
+ await emitRetry({
936
957
  sessionID: draft.sessionID,
937
958
  attempt: draft.attempt,
938
959
  reason: 'host-decides',
@@ -948,8 +969,18 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
948
969
  const retrySameAccount = adapter.retrySameAccount?.bind(adapter);
949
970
  const handle = rec.attempt;
950
971
  const afterOutput = rec.outputStarted;
972
+ if (rec.transport === 'ws' &&
973
+ afterOutput &&
974
+ !rec.ended &&
975
+ !rec.limit &&
976
+ !(rec.status !== undefined && rec.status >= 400) &&
977
+ !(draft.error.status !== undefined && draft.error.status >= 400) &&
978
+ draft.error.type === 'provider.transport') {
979
+ rec.readFailed = true;
980
+ finish(rec, { reason: 'failed', message: draft.error.message });
981
+ }
951
982
  if (afterOutput &&
952
- rec.httpReadFailed &&
983
+ rec.readFailed &&
953
984
  retrySameAccount &&
954
985
  handle &&
955
986
  !mixed(draft.sessionID)) {
@@ -964,9 +995,11 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
964
995
  await rec.limit.delivered;
965
996
  checkRetryActive();
966
997
  }
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 &&
998
+ // The host drops read-operation metadata from its retry error. HTTP
999
+ // needs our body reader's failure; WebSocket needs inspected output
1000
+ // without a terminal frame, since the host only sends before reading.
1001
+ // Ending the socket attempt above also excludes any late frames.
1002
+ const canContinue = () => rec.readFailed === true &&
970
1003
  draft.error.type === 'provider.transport' &&
971
1004
  hostDecision.retry &&
972
1005
  hostRetryLimit - draft.attempt >= 1 &&
@@ -1072,7 +1105,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
1072
1105
  });
1073
1106
  checkRetryActive();
1074
1107
  if (retried) {
1075
- await emit('retry', {
1108
+ await emitRetry({
1076
1109
  sessionID: draft.sessionID,
1077
1110
  accountId: rec.accountId,
1078
1111
  kind: rec.scope.kind,
@@ -1327,7 +1360,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
1327
1360
  return { decision, reason };
1328
1361
  });
1329
1362
  checkRetryActive();
1330
- await emit('retry', {
1363
+ await emitRetry({
1331
1364
  sessionID: draft.sessionID,
1332
1365
  ...(rec.accountId === undefined
1333
1366
  ? {}
@@ -487,6 +487,8 @@ export interface OpenCode2AuthAdapter<Q = unknown, A = unknown, Choice extends C
487
487
  }
488
488
  export interface OpenCode2AuthLogger {
489
489
  warn(message: string, data?: unknown): void;
490
+ /** Retry decisions, without account details or provider error contents. */
491
+ info?(message: string, data?: unknown): void;
490
492
  }
491
493
  export interface InstallOpenCode2AuthOptions {
492
494
  /**
@@ -541,7 +543,7 @@ export interface InstallOpenCode2AuthOptions {
541
543
  * - `reroute`: refused for a limit before any output, and `chooseAccount`
542
544
  * named another account for the next attempt, so the host retries at once;
543
545
  * - `output-started`: output reached the user without a safe continuation, so no retry;
544
- * - `continue-same-account`: a witnessed HTTP read failure after output, with
546
+ * - `continue-same-account`: a witnessed transport read failure after output, with
545
547
  * adapter-approved identity preservation; keeps the host's continuation and delay;
546
548
  * - `no-account`: the attempt had no account, so no retry;
547
549
  * - `host-decides-last-account`: refused for a limit before any output, but
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/common-auth",
3
- "version": "0.39.1",
3
+ "version": "0.39.2",
4
4
  "description": "Shared code for the CortexKit auth plugins: account pool, quota and routing, commands and auth menu, OpenCode 2 hooks, Claustrum custody, and plumbing (loopback RPC, file locks, logger, sidebar state, TUI preferences and build).",
5
5
  "license": "MIT",
6
6
  "repository": {