@cortexkit/common-auth 0.25.0 → 0.27.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 +63 -2
- package/dist/claustrum/consumer.js +250 -9
- package/dist/claustrum/custody.d.ts +36 -1
- package/dist/claustrum/custody.js +95 -11
- package/dist/claustrum/hub.d.ts +187 -0
- package/dist/claustrum/hub.js +960 -0
- package/dist/claustrum/index.d.ts +3 -2
- package/dist/claustrum/index.js +2 -1
- package/dist/claustrum/quota-writer.js +4 -54
- package/dist/claustrum/roster.d.ts +47 -0
- package/dist/claustrum/roster.js +74 -3
- package/dist/claustrum/shutdown.d.ts +8 -0
- package/dist/claustrum/shutdown.js +42 -0
- package/dist/fs/refresh-file-lock.d.ts +1 -1
- package/dist/fs/refresh-file-lock.js +20 -11
- package/dist/opencode2/errors.d.ts +4 -1
- package/dist/opencode2/index.d.ts +2 -2
- package/dist/opencode2/index.js +1 -1
- package/dist/opencode2/install.d.ts +7 -5
- package/dist/opencode2/install.js +139 -13
- package/dist/opencode2/types.d.ts +42 -10
- package/dist/quota/pending.d.ts +11 -0
- package/dist/quota/pending.js +48 -0
- package/dist/redaction/index.d.ts +8 -0
- package/dist/redaction/index.js +490 -0
- package/dist/rpc/rpc-server.js +29 -19
- package/dist/setup/command.js +52 -14
- package/dist/setup/register.d.ts +2 -0
- package/dist/setup/register.js +90 -11
- package/dist/store/attribution.d.ts +5 -0
- package/dist/store/attribution.js +39 -25
- package/dist/store/pool.d.ts +17 -0
- package/dist/store/pool.js +18 -3
- package/dist/store/pull.js +3 -1
- package/dist/store/quota-writer.d.ts +40 -0
- package/dist/store/quota-writer.js +298 -0
- package/dist/store/runtime.d.ts +2 -0
- package/package.json +6 -2
|
@@ -2,6 +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
7
|
/**
|
|
6
8
|
* The request header `model.request` sets to the attempt it started. The
|
|
7
9
|
* host builds the HTTP request and the WebSocket handshake from the headers
|
|
@@ -120,9 +122,9 @@ function applyHeaderEditsTo(target, edits) {
|
|
|
120
122
|
* compares the host-prepared secret exactly, then selects a fresh attempt only
|
|
121
123
|
* for an owned send. Request-object attribution therefore needs no attempt
|
|
122
124
|
* header. Frames are owned by the latest handshake of their session and kind;
|
|
123
|
-
* retry runs only when the session's latest transport was owned
|
|
124
|
-
*
|
|
125
|
-
*
|
|
125
|
+
* retry runs only when the session's latest transport was owned and the session
|
|
126
|
+
* has never mixed owned and foreign transports. The host's retry hook has no
|
|
127
|
+
* request identity, so a mixed session must keep the host's retry decision.
|
|
126
128
|
*/
|
|
127
129
|
export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
128
130
|
const { providerID } = adapter;
|
|
@@ -142,10 +144,50 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
142
144
|
const byRequest = new WeakMap();
|
|
143
145
|
/** Retries the retry hook asked for, by `sessionID:kind`. */
|
|
144
146
|
const pendingRetries = new Map();
|
|
147
|
+
// A shared clock orders each session's retries without retaining idle session state.
|
|
148
|
+
let retryOrder = 0;
|
|
145
149
|
// The host names only session/kind for frames and only session for retry.
|
|
146
150
|
// A transport's prepared credential, never global login state, owns these.
|
|
147
151
|
const ownedSockets = new Set();
|
|
148
152
|
const latestTransportOwned = new Map();
|
|
153
|
+
const transportSeen = new Map();
|
|
154
|
+
const mixed = (sessionID) => {
|
|
155
|
+
const seen = transportSeen.get(sessionID);
|
|
156
|
+
return seen?.ownedSeen === true && seen.foreignSeen;
|
|
157
|
+
};
|
|
158
|
+
const recordTransport = (sessionID, owned) => {
|
|
159
|
+
latestTransportOwned.set(sessionID, owned);
|
|
160
|
+
const seen = transportSeen.get(sessionID) ?? {
|
|
161
|
+
ownedSeen: false,
|
|
162
|
+
foreignSeen: false,
|
|
163
|
+
};
|
|
164
|
+
if (owned)
|
|
165
|
+
seen.ownedSeen = true;
|
|
166
|
+
else
|
|
167
|
+
seen.foreignSeen = true;
|
|
168
|
+
transportSeen.set(sessionID, seen);
|
|
169
|
+
};
|
|
170
|
+
const transportOwned = (scope, headers) => {
|
|
171
|
+
const refuse = ownership?.refuseMisplaced;
|
|
172
|
+
const carriers = refuse
|
|
173
|
+
? [...headers].filter(([, value]) => value.includes(placeholderSecret(providerID)))
|
|
174
|
+
: [];
|
|
175
|
+
const owned = owns(headers) && (!refuse || carriers.length > 0);
|
|
176
|
+
if (refuse) {
|
|
177
|
+
if (carriers.length > 0 && (carriers.length !== 1 || !owned)) {
|
|
178
|
+
recordTransport(scope.sessionID, false);
|
|
179
|
+
throw new OpenCode2AuthError({
|
|
180
|
+
kind: 'placement-refused',
|
|
181
|
+
providerID,
|
|
182
|
+
sessionID: scope.sessionID,
|
|
183
|
+
requestKind: scope.kind,
|
|
184
|
+
message: refuse(carriers[0]?.[0] ?? ''),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
recordTransport(scope.sessionID, owned);
|
|
189
|
+
return owned;
|
|
190
|
+
};
|
|
149
191
|
let warnedUnprovenResponse = false;
|
|
150
192
|
let warnedUnmarkedSend = false;
|
|
151
193
|
const listeners = new Map();
|
|
@@ -358,9 +400,10 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
358
400
|
await inChoiceOrder(scope.sessionID, () => releaseChoice(next));
|
|
359
401
|
assertActive();
|
|
360
402
|
}
|
|
361
|
-
const
|
|
403
|
+
const choice = matches
|
|
362
404
|
? next.accountId
|
|
363
405
|
: await adapter.chooseAccount(input);
|
|
406
|
+
const accountId = typeof choice === 'string' ? choice : undefined;
|
|
364
407
|
return { input, accountId };
|
|
365
408
|
});
|
|
366
409
|
assertActive();
|
|
@@ -570,8 +613,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
570
613
|
}, scoped));
|
|
571
614
|
registrations.push(await ctx.session.hook('http.request', (draft) => {
|
|
572
615
|
if (ownership) {
|
|
573
|
-
const owned =
|
|
574
|
-
latestTransportOwned.set(draft.sessionID, owned);
|
|
616
|
+
const owned = transportOwned(scopeOf(draft), draft.request.headers);
|
|
575
617
|
if (!owned)
|
|
576
618
|
return;
|
|
577
619
|
}
|
|
@@ -688,12 +730,11 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
688
730
|
}, scoped));
|
|
689
731
|
registrations.push(await ctx.session.hook('experimental.ws.handshake', (draft) => {
|
|
690
732
|
if (ownership) {
|
|
691
|
-
const owned = owns(new Headers(draft.headers));
|
|
692
733
|
const key = keyOf(draft.sessionID, draft.kind);
|
|
693
|
-
latestTransportOwned.set(draft.sessionID, owned);
|
|
694
734
|
// Clear the previous handshake's ownership before selecting: if no
|
|
695
735
|
// account can serve, subsequent frames must not use the old attempt.
|
|
696
736
|
ownedSockets.delete(key);
|
|
737
|
+
const owned = transportOwned(scopeOf(draft), new Headers(draft.headers));
|
|
697
738
|
if (!owned)
|
|
698
739
|
return;
|
|
699
740
|
}
|
|
@@ -714,7 +755,8 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
714
755
|
if (!account || !attempt)
|
|
715
756
|
throw noAccount(rec.scope);
|
|
716
757
|
await failing(rec, async () => {
|
|
717
|
-
|
|
758
|
+
if (!ownership?.refuseMisplaced)
|
|
759
|
+
applyHeaderEdits(draft.headers, rec.headers);
|
|
718
760
|
const url = adapter.rewriteHandshakeURL?.({
|
|
719
761
|
...account,
|
|
720
762
|
url: draft.url,
|
|
@@ -722,6 +764,8 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
722
764
|
});
|
|
723
765
|
if (url !== undefined)
|
|
724
766
|
draft.url = url;
|
|
767
|
+
if (ownership?.refuseMisplaced)
|
|
768
|
+
applyHeaderEdits(draft.headers, rec.headers);
|
|
725
769
|
guard(rec.scope, Object.entries(draft.headers));
|
|
726
770
|
if (ownership)
|
|
727
771
|
ownedSockets.add(rec.key);
|
|
@@ -763,10 +807,21 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
763
807
|
registrations.push(await ctx.session.hook('retry', async (draft) => {
|
|
764
808
|
if (ownership && latestTransportOwned.get(draft.sessionID) !== true)
|
|
765
809
|
return;
|
|
810
|
+
const hostDecision = draft.decision;
|
|
811
|
+
const hostDecides = () => emit('retry', {
|
|
812
|
+
sessionID: draft.sessionID,
|
|
813
|
+
attempt: draft.attempt,
|
|
814
|
+
reason: 'host-decides',
|
|
815
|
+
hostDecision,
|
|
816
|
+
decision: hostDecision,
|
|
817
|
+
});
|
|
818
|
+
if (mixed(draft.sessionID)) {
|
|
819
|
+
await hostDecides();
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
766
822
|
const picked = pickForRetry(draft.sessionID);
|
|
767
823
|
if (!picked)
|
|
768
824
|
return;
|
|
769
|
-
const hostDecision = draft.decision;
|
|
770
825
|
if (picked === 'ambiguous') {
|
|
771
826
|
// Nothing tells which open attempt failed: keep the host's
|
|
772
827
|
// decision and leave every attempt as it is.
|
|
@@ -782,6 +837,11 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
782
837
|
const rec = picked;
|
|
783
838
|
await withSelection(rec.scope, async (assertActive) => {
|
|
784
839
|
const checkRetryActive = assertActive;
|
|
840
|
+
// Recheck after entering selection before attributing an identity-less error.
|
|
841
|
+
if (mixed(draft.sessionID)) {
|
|
842
|
+
await hostDecides();
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
785
845
|
rec.judged = true;
|
|
786
846
|
const attempt = rec.attempt;
|
|
787
847
|
if (attempt && !rec.outputStarted) {
|
|
@@ -799,11 +859,21 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
799
859
|
checkRetryActive();
|
|
800
860
|
const { decision, reason } = await inChoiceOrder(rec.scope.sessionID, async () => {
|
|
801
861
|
checkRetryActive();
|
|
862
|
+
if (mixed(draft.sessionID))
|
|
863
|
+
return {
|
|
864
|
+
decision: hostDecision,
|
|
865
|
+
reason: 'host-decides',
|
|
866
|
+
};
|
|
802
867
|
const old = pendingRetries.get(rec.key);
|
|
803
868
|
pendingRetries.delete(rec.key);
|
|
804
869
|
if (old?.next)
|
|
805
870
|
await releaseChoice(old.next);
|
|
806
871
|
checkRetryActive();
|
|
872
|
+
if (mixed(draft.sessionID))
|
|
873
|
+
return {
|
|
874
|
+
decision: hostDecision,
|
|
875
|
+
reason: 'host-decides',
|
|
876
|
+
};
|
|
807
877
|
let decision = hostDecision;
|
|
808
878
|
let reason;
|
|
809
879
|
let rerouteFrom;
|
|
@@ -830,7 +900,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
830
900
|
// next attempt would find no account (a `no-account` refusal
|
|
831
901
|
// the host treats as a defect and does not retry) or go back
|
|
832
902
|
// at once to the account that just refused.
|
|
833
|
-
let
|
|
903
|
+
let choice;
|
|
834
904
|
const input = {
|
|
835
905
|
...rec.scope,
|
|
836
906
|
phase: 'retry',
|
|
@@ -838,7 +908,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
838
908
|
rerouteFrom: refused,
|
|
839
909
|
};
|
|
840
910
|
try {
|
|
841
|
-
|
|
911
|
+
choice = await adapter.chooseAccount(input);
|
|
842
912
|
}
|
|
843
913
|
catch (error) {
|
|
844
914
|
// Treated as no other account: the host's own decision
|
|
@@ -848,6 +918,12 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
848
918
|
error: describe(error),
|
|
849
919
|
});
|
|
850
920
|
}
|
|
921
|
+
const hold = typeof choice === 'object' ? choice.hold : undefined;
|
|
922
|
+
const accountId = typeof choice === 'string'
|
|
923
|
+
? choice
|
|
924
|
+
: hold
|
|
925
|
+
? rec.accountId
|
|
926
|
+
: undefined;
|
|
851
927
|
if (accountId !== undefined)
|
|
852
928
|
next = { accountId, input };
|
|
853
929
|
try {
|
|
@@ -859,7 +935,30 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
859
935
|
await releaseChoice(next);
|
|
860
936
|
throw error;
|
|
861
937
|
}
|
|
862
|
-
|
|
938
|
+
// A foreign transport may have arrived while choosing. Do not
|
|
939
|
+
// publish or retain a retry choice for an unidentified call.
|
|
940
|
+
if (mixed(draft.sessionID)) {
|
|
941
|
+
if (next)
|
|
942
|
+
await releaseChoice(next);
|
|
943
|
+
return {
|
|
944
|
+
decision: hostDecision,
|
|
945
|
+
reason: 'host-decides',
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
if (hold &&
|
|
949
|
+
Number.isFinite(hold.delayMs) &&
|
|
950
|
+
hold.delayMs >= 0) {
|
|
951
|
+
// A recognised account limit can be recoverable even when
|
|
952
|
+
// the host treats its provider error as final.
|
|
953
|
+
decision = {
|
|
954
|
+
retry: true,
|
|
955
|
+
delay: Math.min(hold.delayMs, MAX_RETRY_HOLD_MS),
|
|
956
|
+
};
|
|
957
|
+
reason = 'hold';
|
|
958
|
+
}
|
|
959
|
+
else if (!hold &&
|
|
960
|
+
accountId !== undefined &&
|
|
961
|
+
accountId !== rec.accountId) {
|
|
863
962
|
rec.rerouted = true;
|
|
864
963
|
rerouteFrom = refused;
|
|
865
964
|
// The next attempt takes this choice rather than asking
|
|
@@ -873,6 +972,10 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
873
972
|
reason = 'reroute';
|
|
874
973
|
}
|
|
875
974
|
else {
|
|
975
|
+
if (hold && next) {
|
|
976
|
+
await releaseChoice(next);
|
|
977
|
+
next = undefined;
|
|
978
|
+
}
|
|
876
979
|
// The refused account keeps the host's wait and serves the
|
|
877
980
|
// retry. Undefined or a throw commits nothing: choose later.
|
|
878
981
|
reason = 'host-decides-last-account';
|
|
@@ -892,6 +995,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
892
995
|
pendingRetries.set(rec.key, {
|
|
893
996
|
attemptId: rec.id,
|
|
894
997
|
sessionID: rec.scope.sessionID,
|
|
998
|
+
order: ++retryOrder,
|
|
895
999
|
accountId: rec.accountId,
|
|
896
1000
|
...(rerouteFrom === undefined ? {} : { rerouteFrom }),
|
|
897
1001
|
...(next === undefined ? {} : { next }),
|
|
@@ -922,6 +1026,10 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
922
1026
|
const forgetSession = (sessionID) => {
|
|
923
1027
|
revokeSelections(sessionID);
|
|
924
1028
|
latestTransportOwned.delete(sessionID);
|
|
1029
|
+
// A foreign send can retry after forgetting and reuse of this session id.
|
|
1030
|
+
// Retain its ownership history until disposal; owned-only ids need no tombstone.
|
|
1031
|
+
if (!transportSeen.get(sessionID)?.foreignSeen)
|
|
1032
|
+
transportSeen.delete(sessionID);
|
|
925
1033
|
for (const key of ownedSockets) {
|
|
926
1034
|
if (JSON.parse(key)[0] === sessionID)
|
|
927
1035
|
ownedSockets.delete(key);
|
|
@@ -945,6 +1053,23 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
945
1053
|
for await (const event of events.subscribe({ signal: abort.signal })) {
|
|
946
1054
|
if (event.type === 'session.deleted')
|
|
947
1055
|
forgetSession(event.data.sessionID);
|
|
1056
|
+
else if (event.type === 'session.status' &&
|
|
1057
|
+
event.data.status.type === 'idle') {
|
|
1058
|
+
const sessionID = event.data.sessionID;
|
|
1059
|
+
const idleOrder = ++retryOrder;
|
|
1060
|
+
// Idle can arrive late from an earlier turn. Release only choices
|
|
1061
|
+
// already queued when observed, never a selection still in flight.
|
|
1062
|
+
void inChoiceOrder(sessionID, async () => {
|
|
1063
|
+
for (const [key, pending] of pendingRetries) {
|
|
1064
|
+
if (pending.sessionID === sessionID &&
|
|
1065
|
+
pending.order < idleOrder) {
|
|
1066
|
+
pendingRetries.delete(key);
|
|
1067
|
+
if (pending.next)
|
|
1068
|
+
await releaseChoice(pending.next);
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
});
|
|
1072
|
+
}
|
|
948
1073
|
}
|
|
949
1074
|
}
|
|
950
1075
|
catch (error) {
|
|
@@ -990,6 +1115,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
990
1115
|
discardPending(key);
|
|
991
1116
|
ownedSockets.clear();
|
|
992
1117
|
latestTransportOwned.clear();
|
|
1118
|
+
transportSeen.clear();
|
|
993
1119
|
listeners.clear();
|
|
994
1120
|
await Promise.all(choiceWork.values());
|
|
995
1121
|
await Promise.all(registrations.map(async (registration) => {
|
|
@@ -18,7 +18,7 @@ export interface RequestScope {
|
|
|
18
18
|
/** Where a reading or a refusal was seen. */
|
|
19
19
|
export type Transport = 'http' | 'ws';
|
|
20
20
|
/**
|
|
21
|
-
* A provider refusal that should move the request to another account: a rate
|
|
21
|
+
* A provider refusal that should wait or move the request to another account: a rate
|
|
22
22
|
* limit, an exhausted usage window, or anything else the adapter decides is
|
|
23
23
|
* tied to the account rather than to the request.
|
|
24
24
|
*/
|
|
@@ -53,7 +53,7 @@ export interface EventVerdict<Q> {
|
|
|
53
53
|
*/
|
|
54
54
|
export type HeaderEdits = Readonly<Record<string, string | null>>;
|
|
55
55
|
export interface ChooseAccountInput extends RequestScope {
|
|
56
|
-
/**
|
|
56
|
+
/** The retry hook's advance choice (which may hold), or a request's own choice. */
|
|
57
57
|
readonly phase: 'retry' | 'request';
|
|
58
58
|
/**
|
|
59
59
|
* The account of the attempt this one retries, when the retry hook just
|
|
@@ -64,8 +64,8 @@ export interface ChooseAccountInput extends RequestScope {
|
|
|
64
64
|
/**
|
|
65
65
|
* Set when the attempt this one retries was refused for a limit before any
|
|
66
66
|
* output. The adapter should not pick `accountId` again unless it has
|
|
67
|
-
* nothing else; returning it, or `undefined`,
|
|
68
|
-
*
|
|
67
|
+
* nothing else; returning it, or `undefined`, keeps the host's own retry
|
|
68
|
+
* decision. An explicit hold instead retains this account with its own wait.
|
|
69
69
|
*/
|
|
70
70
|
readonly rerouteFrom?: {
|
|
71
71
|
readonly accountId: string;
|
|
@@ -75,6 +75,19 @@ export interface ChooseAccountInput extends RequestScope {
|
|
|
75
75
|
export interface AccountRequest extends RequestScope {
|
|
76
76
|
readonly accountId: string;
|
|
77
77
|
}
|
|
78
|
+
/** An explicit retry on the refused account, without moving its routing pin. */
|
|
79
|
+
export interface RetryHold {
|
|
80
|
+
readonly hold: {
|
|
81
|
+
/**
|
|
82
|
+
* Adapter-computed wait in milliseconds, including per-session jitter.
|
|
83
|
+
* Finite, non-negative values are clamped to MAX_RETRY_HOLD_MS (20 minutes).
|
|
84
|
+
* Invalid values keep the host's decision and release the choice.
|
|
85
|
+
*/
|
|
86
|
+
readonly delayMs: number;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** Holds are valid only in phase `retry`, with a recognised account limit. */
|
|
90
|
+
export type ChooseAccountResult = string | RetryHold | undefined;
|
|
78
91
|
/**
|
|
79
92
|
* One physical send: created when an account is chosen for a request, and
|
|
80
93
|
* handed back on every hook call and event that belongs to that send, so a
|
|
@@ -164,8 +177,10 @@ export interface HostError {
|
|
|
164
177
|
* Everything provider-specific the installer needs. `Q` is the plugin's own
|
|
165
178
|
* quota reading type; the installer only carries it to the `quota` event.
|
|
166
179
|
* `A` is the plugin's per-attempt value (see `Attempt`).
|
|
180
|
+
* `Choice` defaults to the legacy ID-only result so existing adapters' direct
|
|
181
|
+
* callers still see IDs. Opt into holds with `ChooseAccountResult`.
|
|
167
182
|
*/
|
|
168
|
-
export interface OpenCode2AuthAdapter<Q = unknown, A = unknown> {
|
|
183
|
+
export interface OpenCode2AuthAdapter<Q = unknown, A = unknown, Choice extends ChooseAccountResult = string | undefined> {
|
|
169
184
|
/** Every hook is scoped to this provider; other providers are untouched. */
|
|
170
185
|
readonly providerID: string;
|
|
171
186
|
/**
|
|
@@ -179,14 +194,19 @@ export interface OpenCode2AuthAdapter<Q = unknown, A = unknown> {
|
|
|
179
194
|
* with `phase: 'retry'` and `rerouteFrom` set. Every returned account,
|
|
180
195
|
* including the refused one, is kept for the next attempt of that session
|
|
181
196
|
* and kind: it does not choose again. Another account retries at once;
|
|
182
|
-
* the refused account keeps the host's decision and wait. `
|
|
197
|
+
* the refused account keeps the host's decision and wait. `{ hold: { delayMs } }`
|
|
198
|
+
* commits the refused account without changing its pin and replaces the host's
|
|
199
|
+
* wait, even its no-retry decision, only for a recognised account limit before
|
|
200
|
+
* output in an untainted session. Holds returned in phase `request` cannot
|
|
201
|
+
* select an account and are treated as undefined. `undefined` or a
|
|
183
202
|
* throw commits nothing: the host decides, and the next attempt chooses
|
|
184
203
|
* with `phase: 'request'`, `previousAccountId` and no `rerouteFrom`.
|
|
185
204
|
*/
|
|
186
|
-
chooseAccount(input: ChooseAccountInput): Promise<
|
|
205
|
+
chooseAccount(input: ChooseAccountInput): Promise<Choice> | Choice;
|
|
187
206
|
/**
|
|
188
207
|
* Releases a kept retry choice that will not be used: no retry, a changed
|
|
189
|
-
* model or agent, session forgetting/deletion,
|
|
208
|
+
* model or agent, cancellation of a queued retry (session idle), session forgetting/deletion,
|
|
209
|
+
* disposal or record eviction. A hold releases the refused account's choice.
|
|
190
210
|
* Called once with the original choose input plus its returned accountId,
|
|
191
211
|
* never for a used choice. Awaited before any later chooseAccount for the
|
|
192
212
|
* same session; errors are logged and swallowed. A model/agent change
|
|
@@ -349,6 +369,13 @@ export interface InstallOpenCode2AuthOptions {
|
|
|
349
369
|
*/
|
|
350
370
|
readonly gateOnPlaceholder?: {
|
|
351
371
|
readonly credential: (headers: Headers) => string | undefined;
|
|
372
|
+
/**
|
|
373
|
+
* Refuse misplaced placeholders before selecting an account. The placeholder
|
|
374
|
+
* must occur in exactly one header and be the extracted secret. Receives a
|
|
375
|
+
* carrier header name for the refusal message. Also runs the handshake URL
|
|
376
|
+
* rewrite before applying account headers to the draft. Opt-in only.
|
|
377
|
+
*/
|
|
378
|
+
readonly refuseMisplaced?: (header: string) => string;
|
|
352
379
|
};
|
|
353
380
|
/**
|
|
354
381
|
* Values that must never reach the wire, normally the placeholder the host
|
|
@@ -372,9 +399,12 @@ export interface InstallOpenCode2AuthOptions {
|
|
|
372
399
|
* no other account can serve the next attempt, so the host's own decision
|
|
373
400
|
* (its wait, or its refusal to retry) stands and the next attempt may go
|
|
374
401
|
* to the refused account again;
|
|
375
|
-
* - `
|
|
402
|
+
* - `hold`: an explicit adapter wait on the refused account, before output;
|
|
403
|
+
* - `host-decides`: no limit was recognised, or the failing call cannot be
|
|
404
|
+
* attributed safely (ambiguous attempts or mixed ownership in a gated
|
|
405
|
+
* session), so the host's decision stands.
|
|
376
406
|
*/
|
|
377
|
-
export type RetryReason = 'reroute' | 'output-started' | 'no-account' | 'host-decides-last-account' | 'host-decides';
|
|
407
|
+
export type RetryReason = 'reroute' | 'hold' | 'output-started' | 'no-account' | 'host-decides-last-account' | 'host-decides';
|
|
378
408
|
/**
|
|
379
409
|
* The hook that picked an account. Normally `model.request`; a transport hook
|
|
380
410
|
* picks only when the host skipped `model.request` for that request, which a
|
|
@@ -441,6 +471,8 @@ export interface OpenCode2AuthInstallation<Q, A = unknown> {
|
|
|
441
471
|
/**
|
|
442
472
|
* Drops every attempt and revokes in-flight auth selections of a session.
|
|
443
473
|
* Session deletion does this itself. Later requests may reuse the session id.
|
|
474
|
+
* Foreign transport history survives until dispose: an old foreign call can
|
|
475
|
+
* still retry under that reused id, so forgetting cannot clear retry taint.
|
|
444
476
|
*/
|
|
445
477
|
forgetSession(sessionID: string): void;
|
|
446
478
|
/** Number of attempts held. */
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { QuotaObservation } from './merge.js';
|
|
2
|
+
export interface QuotaPublication {
|
|
3
|
+
observation: QuotaObservation;
|
|
4
|
+
}
|
|
5
|
+
export interface QuotaField<P extends QuotaPublication> {
|
|
6
|
+
at: number;
|
|
7
|
+
order: number;
|
|
8
|
+
publication: P;
|
|
9
|
+
priorReading?: P;
|
|
10
|
+
}
|
|
11
|
+
export declare function quotaFields<P extends QuotaPublication>(publication: P, witness?: string): Map<string, QuotaField<P>>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Use the same independently dated parts as mergeQuotaObservation. A header
|
|
2
|
+
// reading cannot erase a poll-only window, counter, credits record or budget.
|
|
3
|
+
export function quotaFields(publication, witness = '') {
|
|
4
|
+
const result = new Map();
|
|
5
|
+
const { observation } = publication;
|
|
6
|
+
const add = (key, at, part) => result.set(`${witness}:${key}`, {
|
|
7
|
+
at,
|
|
8
|
+
order: 0,
|
|
9
|
+
publication: { ...publication, observation: part },
|
|
10
|
+
});
|
|
11
|
+
const keyFor = (scope, label) => JSON.stringify([scope ?? 'all', label]);
|
|
12
|
+
const read = new Set();
|
|
13
|
+
for (const reading of observation.readings ?? []) {
|
|
14
|
+
const key = keyFor(reading.scope, reading.label);
|
|
15
|
+
read.add(key);
|
|
16
|
+
add(key, observation.checkedAt, {
|
|
17
|
+
checkedAt: observation.checkedAt,
|
|
18
|
+
readings: [{ ...reading }],
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
for (const pair of observation.coverage ?? []) {
|
|
22
|
+
const key = keyFor(pair.scope, pair.label);
|
|
23
|
+
if (!read.has(key))
|
|
24
|
+
add(key, observation.checkedAt, {
|
|
25
|
+
checkedAt: observation.checkedAt,
|
|
26
|
+
coverage: [{ ...pair }],
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
if (observation.budget)
|
|
30
|
+
add('budget', observation.checkedAt, {
|
|
31
|
+
checkedAt: observation.checkedAt,
|
|
32
|
+
budget: { ...observation.budget },
|
|
33
|
+
});
|
|
34
|
+
for (const [name, counter] of Object.entries(observation.counters ?? {})) {
|
|
35
|
+
add(`counter:${name}`, counter.checkedAt, {
|
|
36
|
+
checkedAt: observation.checkedAt,
|
|
37
|
+
counters: { [name]: { ...counter } },
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (observation.credits)
|
|
41
|
+
add('credits', observation.credits.checkedAt, {
|
|
42
|
+
checkedAt: observation.checkedAt,
|
|
43
|
+
credits: { ...observation.credits },
|
|
44
|
+
});
|
|
45
|
+
if (!result.size)
|
|
46
|
+
add('empty', observation.checkedAt, { checkedAt: observation.checkedAt });
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Scan both spellings: decoding is independent of provider JSON parsing. */
|
|
2
|
+
export declare function redactCredentialText(text: string, credential?: string): string;
|
|
3
|
+
/** Return a fresh Error with only size-limited, scrubbed data needed for abort, quota and host retry decisions. */
|
|
4
|
+
export declare function redactCredentialError(error: unknown, credential?: string): Error;
|
|
5
|
+
/** Only error frames are rewritten; normal model output must remain byte-identical. */
|
|
6
|
+
export declare function redactCredentialFrame(frame: string, credential?: string): string;
|
|
7
|
+
/** Scrub HTTP errors and caller-declared SSE errors without buffering successful output. */
|
|
8
|
+
export declare function redactCredentialResponse(response: Response, credential?: string, mode?: 'event-stream' | 'http'): Promise<Response>;
|