@cortexkit/common-auth 0.32.1 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/claustrum/consumer.d.ts +12 -0
- package/dist/claustrum/consumer.js +36 -0
- package/dist/claustrum/custody.d.ts +16 -4
- package/dist/claustrum/custody.js +74 -9
- package/dist/claustrum/hub.d.ts +3 -2
- package/dist/claustrum/hub.js +8 -0
- package/dist/claustrum/index.d.ts +1 -0
- package/dist/claustrum/roster.d.ts +2 -0
- package/dist/claustrum/roster.js +2 -0
- package/dist/claustrum/usage.d.ts +41 -0
- package/dist/claustrum/usage.js +145 -0
- package/dist/commands/builtins.d.ts +5 -0
- package/dist/commands/builtins.js +38 -0
- package/dist/fs/private-files.d.ts +5 -0
- package/dist/fs/private-files.js +7 -0
- package/dist/fs/refresh-file-lock.js +144 -49
- package/dist/fs/with-lock.js +34 -10
- package/dist/limits/activity.d.ts +26 -0
- package/dist/limits/activity.js +52 -0
- package/dist/limits/axes.d.ts +63 -0
- package/dist/limits/axes.js +7 -0
- package/dist/limits/bridge.d.ts +4 -0
- package/dist/limits/bridge.js +10 -0
- package/dist/limits/decide.d.ts +141 -0
- package/dist/limits/decide.js +383 -0
- package/dist/limits/duration.d.ts +8 -0
- package/dist/limits/duration.js +20 -0
- package/dist/limits/envelope-loss.d.ts +29 -0
- package/dist/limits/envelope-loss.js +150 -0
- package/dist/limits/families.d.ts +57 -0
- package/dist/limits/families.js +143 -0
- package/dist/limits/header-classifier.d.ts +9 -0
- package/dist/limits/header-classifier.js +121 -0
- package/dist/limits/hold.d.ts +13 -0
- package/dist/limits/hold.js +25 -0
- package/dist/limits/identity.d.ts +14 -0
- package/dist/limits/identity.js +21 -0
- package/dist/limits/index.d.ts +44 -0
- package/dist/limits/index.js +25 -0
- package/dist/limits/jitter.d.ts +2 -0
- package/dist/limits/jitter.js +9 -0
- package/dist/limits/labels.d.ts +3 -0
- package/dist/limits/labels.js +11 -0
- package/dist/limits/marker.d.ts +64 -0
- package/dist/limits/marker.js +96 -0
- package/dist/limits/merge-laws.d.ts +14 -0
- package/dist/limits/merge-laws.js +31 -0
- package/dist/limits/namespace.d.ts +89 -0
- package/dist/limits/namespace.js +183 -0
- package/dist/limits/persistence.d.ts +33 -0
- package/dist/limits/persistence.js +9 -0
- package/dist/limits/pi-wait.d.ts +16 -0
- package/dist/limits/pi-wait.js +51 -0
- package/dist/limits/pool-adapter.d.ts +16 -0
- package/dist/limits/pool-adapter.js +162 -0
- package/dist/limits/presets.d.ts +11 -0
- package/dist/limits/presets.js +223 -0
- package/dist/limits/registry.d.ts +49 -0
- package/dist/limits/registry.js +471 -0
- package/dist/limits/request-history.d.ts +77 -0
- package/dist/limits/request-history.js +117 -0
- package/dist/limits/reset.d.ts +21 -0
- package/dist/limits/reset.js +44 -0
- package/dist/limits/routing-hold.d.ts +16 -0
- package/dist/limits/routing-hold.js +23 -0
- package/dist/limits/scope.d.ts +12 -0
- package/dist/limits/scope.js +10 -0
- package/dist/limits/signal.d.ts +41 -0
- package/dist/limits/signal.js +1 -0
- package/dist/limits/terminal.d.ts +52 -0
- package/dist/limits/terminal.js +133 -0
- package/dist/limits/vault-adapter.d.ts +16 -0
- package/dist/limits/vault-adapter.js +123 -0
- package/dist/limits/vault.d.ts +1 -0
- package/dist/limits/vault.js +1 -0
- package/dist/limits/wake.d.ts +21 -0
- package/dist/limits/wake.js +47 -0
- package/dist/limits/wording.d.ts +25 -0
- package/dist/limits/wording.js +114 -0
- package/dist/opencode2/index.d.ts +1 -1
- package/dist/opencode2/install.d.ts +2 -2
- package/dist/opencode2/install.js +161 -18
- package/dist/opencode2/types.d.ts +95 -9
- package/dist/quota/format.d.ts +9 -4
- package/dist/quota/format.js +45 -26
- package/dist/quota-sidebar/index.jsx +27 -19
- package/dist/routing/admission.d.ts +7 -2
- package/dist/routing/admission.js +17 -7
- package/dist/routing-accessor/handle.d.ts +1 -0
- package/dist/routing-accessor/handle.js +10 -0
- package/dist/routing-accessor/index.d.ts +18 -2
- package/dist/routing-accessor/index.js +21 -11
- package/dist/rpc/port-file.d.ts +12 -3
- package/dist/rpc/port-file.js +46 -4
- package/dist/store/attribution.js +3 -2
- package/dist/store/errors.d.ts +1 -1
- package/dist/store/hooks.d.ts +5 -1
- package/dist/store/hooks.js +20 -5
- package/dist/store/index.d.ts +1 -0
- package/dist/store/limits-namespace.d.ts +7 -0
- package/dist/store/limits-namespace.js +34 -0
- package/dist/store/pool.d.ts +10 -1
- package/dist/store/pool.js +2 -0
- package/dist/store/provider-state.js +5 -5
- package/dist/store/refresh.js +1 -1
- package/dist/store/schema.js +9 -4
- package/dist/store/settings.d.ts +2 -0
- package/dist/store/settings.js +11 -4
- package/package.json +10 -2
- package/src/quota/format.ts +56 -26
- package/src/quota-sidebar/index.tsx +56 -32
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { DecisionFamily, LimitAxes, LimitHost, RefusalReason } from './axes.js';
|
|
2
|
+
import type { HostRetry } from './families.js';
|
|
3
|
+
import type { RequestHistory } from './request-history.js';
|
|
4
|
+
import { REQUEST_EXCLUSION_EFFECTS } from './request-history.js';
|
|
5
|
+
import type { Scope } from './scope.js';
|
|
6
|
+
import type { AccountKey, LimitSignal, LimitWindow } from './signal.js';
|
|
7
|
+
import type { TerminalScanFacts } from './terminal.js';
|
|
8
|
+
import type { WakeFacts } from './wake.js';
|
|
9
|
+
export interface TimedBlocker {
|
|
10
|
+
readonly kind: 'hard' | 'cooldown' | 'soft';
|
|
11
|
+
readonly until?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Admission includes the caller's minimum quota thresholds, access checks,
|
|
15
|
+
* current identity/credential ownership checks and pending-write fences.
|
|
16
|
+
* Placement and readiness are separate facts.
|
|
17
|
+
*/
|
|
18
|
+
export interface LimitCandidate {
|
|
19
|
+
readonly account: AccountKey;
|
|
20
|
+
readonly admitted: boolean;
|
|
21
|
+
readonly placementEligible: boolean;
|
|
22
|
+
readonly ready: boolean;
|
|
23
|
+
readonly blockers: readonly TimedBlocker[];
|
|
24
|
+
readonly confirmedBlockers?: readonly LimitWindow[];
|
|
25
|
+
}
|
|
26
|
+
export interface ModeSelection {
|
|
27
|
+
readonly account: AccountKey;
|
|
28
|
+
readonly pin: 'pin' | 'preserve' | 'detour';
|
|
29
|
+
}
|
|
30
|
+
export interface WaitCheckpoint {
|
|
31
|
+
readonly accountKey: AccountKey;
|
|
32
|
+
readonly decidedAt: number;
|
|
33
|
+
readonly blockers: readonly {
|
|
34
|
+
readonly window: string;
|
|
35
|
+
readonly resetAt: number;
|
|
36
|
+
}[];
|
|
37
|
+
}
|
|
38
|
+
/** Resolved via sameAccountHoldFacts; expiry uses the caller's epoch clock. */
|
|
39
|
+
export interface HoldFacts {
|
|
40
|
+
readonly cacheExpiresAt: number;
|
|
41
|
+
readonly maxWaitMs: number;
|
|
42
|
+
readonly maxDelayMs?: number;
|
|
43
|
+
}
|
|
44
|
+
/** Resolved metadata for a terminal refusal, not an account-selection instruction. */
|
|
45
|
+
export interface TerminalFacts {
|
|
46
|
+
readonly source: 'pool' | 'vault';
|
|
47
|
+
readonly reason: RefusalReason;
|
|
48
|
+
readonly family?: DecisionFamily;
|
|
49
|
+
readonly messageKey?: string;
|
|
50
|
+
readonly retryable?: boolean;
|
|
51
|
+
readonly hostRetry?: HostRetry;
|
|
52
|
+
readonly retryAfterMs?: number;
|
|
53
|
+
readonly resetAt?: number;
|
|
54
|
+
/** Pool quota refusals require per-field scan evidence to retain a reset. */
|
|
55
|
+
readonly scan?: TerminalScanFacts;
|
|
56
|
+
}
|
|
57
|
+
export interface PolicyFacts {
|
|
58
|
+
readonly phase: 'pre-send' | 'after-limit';
|
|
59
|
+
readonly host: LimitHost;
|
|
60
|
+
readonly hostPhase?: 'request' | 'retry';
|
|
61
|
+
readonly now: number;
|
|
62
|
+
readonly clockNow: number;
|
|
63
|
+
readonly sessionId: string;
|
|
64
|
+
readonly requestScope: Scope;
|
|
65
|
+
readonly history: RequestHistory;
|
|
66
|
+
readonly attempted?: AccountKey;
|
|
67
|
+
readonly signal?: LimitSignal;
|
|
68
|
+
readonly signalApplicable?: boolean;
|
|
69
|
+
readonly status?: number;
|
|
70
|
+
readonly failureRetryable?: boolean;
|
|
71
|
+
readonly delivery: {
|
|
72
|
+
readonly owned: boolean;
|
|
73
|
+
readonly attributable: boolean;
|
|
74
|
+
readonly replayable: boolean;
|
|
75
|
+
};
|
|
76
|
+
readonly candidates: readonly LimitCandidate[];
|
|
77
|
+
readonly selection?: ModeSelection;
|
|
78
|
+
readonly affinity?: 'sticky' | 'broken' | 'none';
|
|
79
|
+
readonly credentialRecovery?: {
|
|
80
|
+
readonly retryMs?: number;
|
|
81
|
+
};
|
|
82
|
+
readonly retryHintsMs?: readonly number[];
|
|
83
|
+
readonly hold?: HoldFacts;
|
|
84
|
+
readonly wake?: WakeFacts;
|
|
85
|
+
readonly remainingRetries?: number;
|
|
86
|
+
readonly terminal?: TerminalFacts;
|
|
87
|
+
readonly poolWaitBudgetMs?: number;
|
|
88
|
+
}
|
|
89
|
+
interface DecisionBase {
|
|
90
|
+
readonly retryable: boolean;
|
|
91
|
+
readonly hostRetry: HostRetry;
|
|
92
|
+
readonly history: RequestHistory;
|
|
93
|
+
readonly reason: string;
|
|
94
|
+
}
|
|
95
|
+
export type LimitDecision = DecisionBase & ({
|
|
96
|
+
readonly action: 'reroute';
|
|
97
|
+
readonly account: AccountKey;
|
|
98
|
+
readonly pin: ModeSelection['pin'];
|
|
99
|
+
} | {
|
|
100
|
+
readonly action: 'hold';
|
|
101
|
+
readonly account: AccountKey;
|
|
102
|
+
readonly delayMs: number;
|
|
103
|
+
readonly retryAfterMs: number;
|
|
104
|
+
readonly target: number;
|
|
105
|
+
readonly checkpoint: WaitCheckpoint;
|
|
106
|
+
readonly resetAt?: number;
|
|
107
|
+
/** Carry into wake.kind so terminal waits omit the ordinary cache bound. */
|
|
108
|
+
readonly waitKind?: 'terminal';
|
|
109
|
+
} | {
|
|
110
|
+
readonly action: 'retain';
|
|
111
|
+
readonly account: AccountKey;
|
|
112
|
+
readonly retryMs?: number;
|
|
113
|
+
/** Exhaustion windows waived for this dispatch only, never persisted. */
|
|
114
|
+
readonly liftedWindows?: readonly string[];
|
|
115
|
+
} | {
|
|
116
|
+
readonly action: 'wait-then-reselect';
|
|
117
|
+
readonly delayMs: number;
|
|
118
|
+
readonly resetAt?: number;
|
|
119
|
+
readonly target: number;
|
|
120
|
+
readonly checkpoint: WaitCheckpoint;
|
|
121
|
+
readonly retryAfterMs?: number;
|
|
122
|
+
/** Carry into wake.kind; Pi reselects once and then refuses without another wait. */
|
|
123
|
+
readonly waitKind?: 'terminal';
|
|
124
|
+
} | {
|
|
125
|
+
readonly action: 'exclude-for-request';
|
|
126
|
+
readonly account: AccountKey;
|
|
127
|
+
readonly scope: Scope;
|
|
128
|
+
readonly effects: typeof REQUEST_EXCLUSION_EFFECTS;
|
|
129
|
+
} | {
|
|
130
|
+
readonly action: 'refuse';
|
|
131
|
+
readonly reason: RefusalReason;
|
|
132
|
+
readonly family: DecisionFamily;
|
|
133
|
+
readonly messageKey: string;
|
|
134
|
+
readonly retryAfterMs?: number;
|
|
135
|
+
readonly resetAt?: number;
|
|
136
|
+
} | {
|
|
137
|
+
readonly action: 'pass';
|
|
138
|
+
});
|
|
139
|
+
/** The caller supplies clocks, admission, delivery ownership and parsed evidence. */
|
|
140
|
+
export declare function decideAfterLimit(facts: PolicyFacts, axes: LimitAxes): LimitDecision;
|
|
141
|
+
export {};
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { MAX_SAME_ACCOUNT_HOLD_MS } from './hold.js';
|
|
2
|
+
import { holdDelayMs } from './jitter.js';
|
|
3
|
+
import { isRequestExcluded, REQUEST_EXCLUSION_EFFECTS, reduceRequestHistory, sameAccount, } from './request-history.js';
|
|
4
|
+
import { terminalRefusalDecision, terminalWaitDisabled } from './terminal.js';
|
|
5
|
+
import { liftedWindowsForWake, waitCheckpoint } from './wake.js';
|
|
6
|
+
function finiteFuture(value, now) {
|
|
7
|
+
return value !== undefined && Number.isFinite(value) && value > now;
|
|
8
|
+
}
|
|
9
|
+
function continuationRetry(facts, axes) {
|
|
10
|
+
return axes.execution === 'engine' && facts.phase === 'after-limit'
|
|
11
|
+
? 'veto'
|
|
12
|
+
: 'leave';
|
|
13
|
+
}
|
|
14
|
+
function refusal(facts, axes, history, reason, metadata) {
|
|
15
|
+
const terminal = metadata && terminalRefusalDecision(facts, axes, history, metadata);
|
|
16
|
+
if (terminal)
|
|
17
|
+
return terminal;
|
|
18
|
+
const policy = axes.refusals[reason][facts.host];
|
|
19
|
+
return {
|
|
20
|
+
action: 'refuse',
|
|
21
|
+
reason,
|
|
22
|
+
family: metadata?.family ?? policy.family,
|
|
23
|
+
messageKey: metadata?.messageKey ?? policy.messageKey,
|
|
24
|
+
retryable: metadata?.retryable ?? policy.retryable,
|
|
25
|
+
hostRetry: metadata?.hostRetry ?? policy.hostRetry,
|
|
26
|
+
history,
|
|
27
|
+
...(metadata?.retryAfterMs === undefined
|
|
28
|
+
? {}
|
|
29
|
+
: { retryAfterMs: metadata.retryAfterMs }),
|
|
30
|
+
...(metadata?.resetAt === undefined ? {} : { resetAt: metadata.resetAt }),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function hold(facts, axes, history, current, windows) {
|
|
34
|
+
const bound = facts.hold;
|
|
35
|
+
if (axes.shortLimit.holdBound === 'disabled' ||
|
|
36
|
+
(facts.host === 'opencode2' &&
|
|
37
|
+
(facts.hostPhase !== 'retry' ||
|
|
38
|
+
facts.remainingRetries === undefined ||
|
|
39
|
+
!(facts.remainingRetries >= 1))) ||
|
|
40
|
+
!current?.admitted ||
|
|
41
|
+
!bound ||
|
|
42
|
+
bound.maxWaitMs <= 0 ||
|
|
43
|
+
Number.isNaN(bound.maxWaitMs) ||
|
|
44
|
+
!Number.isFinite(bound.cacheExpiresAt) ||
|
|
45
|
+
(bound.maxDelayMs !== undefined &&
|
|
46
|
+
(!Number.isFinite(bound.maxDelayMs) || bound.maxDelayMs < 0)) ||
|
|
47
|
+
!windows.length ||
|
|
48
|
+
windows.some((window) => !finiteFuture(window.resetAt, facts.now)))
|
|
49
|
+
return undefined;
|
|
50
|
+
const resetAt = Math.max(...windows.map((window) => window.resetAt));
|
|
51
|
+
const delayMs = axes.shortLimit.jitter === 'session-sha256'
|
|
52
|
+
? holdDelayMs(facts.sessionId, resetAt - facts.now)
|
|
53
|
+
: resetAt - facts.now;
|
|
54
|
+
const target = facts.now + delayMs;
|
|
55
|
+
if (!Number.isFinite(target) ||
|
|
56
|
+
target > Math.min(bound.cacheExpiresAt, facts.now + bound.maxWaitMs) ||
|
|
57
|
+
(facts.host === 'opencode2' && delayMs > MAX_SAME_ACCOUNT_HOLD_MS) ||
|
|
58
|
+
(bound.maxDelayMs !== undefined && delayMs > bound.maxDelayMs))
|
|
59
|
+
return undefined;
|
|
60
|
+
return {
|
|
61
|
+
action: 'hold',
|
|
62
|
+
account: current.account,
|
|
63
|
+
delayMs,
|
|
64
|
+
retryAfterMs: delayMs,
|
|
65
|
+
target,
|
|
66
|
+
checkpoint: waitCheckpoint(current.account, facts.now, windows),
|
|
67
|
+
reason: 'confirmed-limit',
|
|
68
|
+
retryable: true,
|
|
69
|
+
hostRetry: facts.phase === 'pre-send' ? 'leave' : continuationRetry(facts, axes),
|
|
70
|
+
history,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function poolWait(facts, axes, history, candidates) {
|
|
74
|
+
const policy = axes.poolWaiting;
|
|
75
|
+
if (policy.kind === 'off' || !candidates.length)
|
|
76
|
+
return undefined;
|
|
77
|
+
history = reduceRequestHistory(history, {
|
|
78
|
+
type: 'blocked',
|
|
79
|
+
clockNow: facts.clockNow,
|
|
80
|
+
});
|
|
81
|
+
const times = candidates
|
|
82
|
+
.map((candidate) => {
|
|
83
|
+
const blockers = candidate.blockers.filter((blocker) => !(blocker.kind === 'soft' &&
|
|
84
|
+
candidates.length === 1 &&
|
|
85
|
+
policy.singleEligibleSoftExemption));
|
|
86
|
+
if (blockers.some((blocker) => !finiteFuture(blocker.until, facts.now)))
|
|
87
|
+
return undefined;
|
|
88
|
+
return blockers.length
|
|
89
|
+
? {
|
|
90
|
+
candidate,
|
|
91
|
+
time: Math.max(...blockers.map((blocker) => blocker.until)),
|
|
92
|
+
}
|
|
93
|
+
: undefined;
|
|
94
|
+
})
|
|
95
|
+
.filter((entry) => entry !== undefined);
|
|
96
|
+
const earliest = times.reduce((best, entry) => (!best || entry.time < best.time ? entry : best), undefined);
|
|
97
|
+
const proposed = earliest
|
|
98
|
+
? earliest.time - facts.now
|
|
99
|
+
: candidates.some((candidate) => candidate.blockers.some((blocker) => blocker.kind !== 'soft'))
|
|
100
|
+
? policy.hardFallbackMs
|
|
101
|
+
: undefined;
|
|
102
|
+
const budget = facts.poolWaitBudgetMs ?? policy.budgetMs;
|
|
103
|
+
const elapsed = facts.clockNow - (history.blockedSince ?? facts.clockNow);
|
|
104
|
+
if (budget > 0 &&
|
|
105
|
+
(elapsed >= budget || (proposed !== undefined && proposed > budget)))
|
|
106
|
+
return refusal(facts, axes, history, 'quota', facts.terminal);
|
|
107
|
+
const remaining = policy.requestDeadlineMs - (facts.clockNow - history.requestStartedAt);
|
|
108
|
+
if (remaining <= 0)
|
|
109
|
+
return refusal(facts, axes, history, 'incomplete');
|
|
110
|
+
if (proposed === undefined)
|
|
111
|
+
return refusal(facts, axes, history, 'quota', facts.terminal);
|
|
112
|
+
const delayMs = Math.min(proposed, remaining, budget > 0 ? budget - elapsed : Number.POSITIVE_INFINITY);
|
|
113
|
+
const waitedAccount = earliest?.candidate ?? candidates[0];
|
|
114
|
+
if (!waitedAccount)
|
|
115
|
+
return undefined;
|
|
116
|
+
return {
|
|
117
|
+
action: 'wait-then-reselect',
|
|
118
|
+
delayMs,
|
|
119
|
+
target: facts.now + delayMs,
|
|
120
|
+
checkpoint: waitCheckpoint(waitedAccount.account, facts.now, waitedAccount.confirmedBlockers ?? []),
|
|
121
|
+
reason: 'pool-blocked',
|
|
122
|
+
retryable: true,
|
|
123
|
+
hostRetry: continuationRetry(facts, axes),
|
|
124
|
+
history,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/** The caller supplies clocks, admission, delivery ownership and parsed evidence. */
|
|
128
|
+
export function decideAfterLimit(facts, axes) {
|
|
129
|
+
let history = facts.history;
|
|
130
|
+
const pass = (reason, retryable = facts.phase === 'pre-send'
|
|
131
|
+
? true
|
|
132
|
+
: (facts.failureRetryable ?? true), hostRetry = 'leave') => ({
|
|
133
|
+
action: 'pass',
|
|
134
|
+
reason,
|
|
135
|
+
retryable,
|
|
136
|
+
hostRetry,
|
|
137
|
+
history,
|
|
138
|
+
});
|
|
139
|
+
if (facts.phase === 'after-limit' &&
|
|
140
|
+
facts.host === 'opencode2' &&
|
|
141
|
+
(!facts.delivery.owned || !facts.delivery.attributable))
|
|
142
|
+
return pass('not-owned');
|
|
143
|
+
const status = facts.status ?? facts.signal?.status;
|
|
144
|
+
if (facts.phase === 'after-limit' && facts.attempted)
|
|
145
|
+
history = reduceRequestHistory(history, {
|
|
146
|
+
type: 'response',
|
|
147
|
+
account: facts.attempted,
|
|
148
|
+
scope: facts.requestScope,
|
|
149
|
+
status,
|
|
150
|
+
});
|
|
151
|
+
if (facts.phase === 'after-limit' && !facts.delivery.replayable)
|
|
152
|
+
return pass('unsafe-output', false, 'veto');
|
|
153
|
+
const admitted = facts.candidates.filter((candidate) => candidate.admitted);
|
|
154
|
+
const candidates = admitted.filter((candidate) => !isRequestExcluded(history, candidate.account, facts.requestScope));
|
|
155
|
+
const current = candidates.find((candidate) => facts.attempted && sameAccount(candidate.account, facts.attempted));
|
|
156
|
+
const signal = facts.signalApplicable !== false &&
|
|
157
|
+
facts.signal &&
|
|
158
|
+
facts.attempted &&
|
|
159
|
+
sameAccount(facts.signal.account, facts.attempted)
|
|
160
|
+
? facts.signal
|
|
161
|
+
: undefined;
|
|
162
|
+
if (axes.requestExclusion === 'on' &&
|
|
163
|
+
signal?.kind === 'model-unavailable' &&
|
|
164
|
+
current) {
|
|
165
|
+
return {
|
|
166
|
+
action: 'exclude-for-request',
|
|
167
|
+
account: current.account,
|
|
168
|
+
scope: facts.requestScope,
|
|
169
|
+
effects: REQUEST_EXCLUSION_EFFECTS,
|
|
170
|
+
reason: 'model-unavailable',
|
|
171
|
+
retryable: true,
|
|
172
|
+
hostRetry: continuationRetry(facts, axes),
|
|
173
|
+
history,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
if (admitted.length && !candidates.length)
|
|
177
|
+
return refusal(facts, axes, history, 'model-unavailable');
|
|
178
|
+
if (facts.host === 'opencode2' &&
|
|
179
|
+
facts.hostPhase === 'retry' &&
|
|
180
|
+
axes.execution === 'host' &&
|
|
181
|
+
facts.remainingRetries !== undefined &&
|
|
182
|
+
facts.remainingRetries < 1) {
|
|
183
|
+
return facts.terminal &&
|
|
184
|
+
(facts.terminal.retryable === false ||
|
|
185
|
+
terminalWaitDisabled(facts.terminal, axes))
|
|
186
|
+
? refusal(facts, axes, history, facts.terminal.reason, facts.terminal)
|
|
187
|
+
: pass('host-retries-exhausted');
|
|
188
|
+
}
|
|
189
|
+
if (facts.wake && facts.phase === 'pre-send') {
|
|
190
|
+
const wake = facts.wake;
|
|
191
|
+
const held = candidates.find((candidate) => sameAccount(candidate.account, wake.checkpoint.accountKey));
|
|
192
|
+
const windows = held?.confirmedBlockers ??
|
|
193
|
+
(held && signal && sameAccount(signal.account, held.account)
|
|
194
|
+
? signal.windows
|
|
195
|
+
: []);
|
|
196
|
+
const liftedWindows = held
|
|
197
|
+
? liftedWindowsForWake(wake, held.account, windows, facts.now)
|
|
198
|
+
: [];
|
|
199
|
+
const bound = facts.hold;
|
|
200
|
+
const cacheValid = wake.kind === 'terminal' ||
|
|
201
|
+
(bound !== undefined &&
|
|
202
|
+
Number.isFinite(bound.cacheExpiresAt) &&
|
|
203
|
+
facts.now <= bound.cacheExpiresAt &&
|
|
204
|
+
wake.target <= bound.cacheExpiresAt &&
|
|
205
|
+
bound.maxWaitMs > 0 &&
|
|
206
|
+
wake.target - wake.checkpoint.decidedAt <= bound.maxWaitMs);
|
|
207
|
+
if (held &&
|
|
208
|
+
Number.isFinite(wake.target) &&
|
|
209
|
+
wake.target >= wake.checkpoint.decidedAt &&
|
|
210
|
+
facts.now >= wake.target &&
|
|
211
|
+
cacheValid &&
|
|
212
|
+
held.blockers.every((blocker) => blocker.until !== undefined &&
|
|
213
|
+
Number.isFinite(blocker.until) &&
|
|
214
|
+
blocker.until <= facts.now) &&
|
|
215
|
+
(held.ready || windows.some((window) => window.confirmed)) &&
|
|
216
|
+
windows
|
|
217
|
+
.filter((window) => window.confirmed)
|
|
218
|
+
.every((window) => liftedWindows.includes(window.window))) {
|
|
219
|
+
return {
|
|
220
|
+
action: 'retain',
|
|
221
|
+
account: held.account,
|
|
222
|
+
liftedWindows,
|
|
223
|
+
reason: 'wait-elapsed',
|
|
224
|
+
retryable: true,
|
|
225
|
+
hostRetry: 'leave',
|
|
226
|
+
history: reduceRequestHistory(history, { type: 'selected' }),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (facts.credentialRecovery && current) {
|
|
231
|
+
return {
|
|
232
|
+
action: 'retain',
|
|
233
|
+
account: current.account,
|
|
234
|
+
reason: 'credential-recovery',
|
|
235
|
+
...(facts.credentialRecovery.retryMs === undefined
|
|
236
|
+
? {}
|
|
237
|
+
: { retryMs: facts.credentialRecovery.retryMs }),
|
|
238
|
+
retryable: true,
|
|
239
|
+
hostRetry: continuationRetry(facts, axes),
|
|
240
|
+
history,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
const advance = axes.advance[facts.host];
|
|
244
|
+
if (facts.phase === 'after-limit' &&
|
|
245
|
+
!signal &&
|
|
246
|
+
!facts.terminal &&
|
|
247
|
+
(advance.attributedLimitOnly ||
|
|
248
|
+
status === undefined ||
|
|
249
|
+
!advance.statuses.includes(status)))
|
|
250
|
+
return pass('unhandled-failure');
|
|
251
|
+
const selected = candidates.find((candidate) => facts.selection &&
|
|
252
|
+
sameAccount(candidate.account, facts.selection.account) &&
|
|
253
|
+
candidate.placementEligible);
|
|
254
|
+
const selectedReady = selected &&
|
|
255
|
+
(selected.ready ||
|
|
256
|
+
(axes.poolWaiting.kind === 'earliest' &&
|
|
257
|
+
axes.poolWaiting.singleEligibleSoftExemption &&
|
|
258
|
+
candidates.length === 1 &&
|
|
259
|
+
selected.blockers.length > 0 &&
|
|
260
|
+
selected.blockers.every((blocker) => blocker.kind === 'soft')));
|
|
261
|
+
const alternative = selectedReady &&
|
|
262
|
+
(!facts.attempted || !sameAccount(selected.account, facts.attempted))
|
|
263
|
+
? selected
|
|
264
|
+
: undefined;
|
|
265
|
+
const reroute = (candidate) => ({
|
|
266
|
+
action: 'reroute',
|
|
267
|
+
account: candidate.account,
|
|
268
|
+
pin: facts.selection?.pin ?? 'preserve',
|
|
269
|
+
reason: 'ready-alternative',
|
|
270
|
+
retryable: true,
|
|
271
|
+
hostRetry: continuationRetry(facts, axes),
|
|
272
|
+
history: reduceRequestHistory(history, { type: 'selected' }),
|
|
273
|
+
});
|
|
274
|
+
// Before the first send there is no attempted account or response signal;
|
|
275
|
+
// the retained selection can still have confirmed blockers worth waiting for.
|
|
276
|
+
const holdCandidate = facts.phase === 'pre-send'
|
|
277
|
+
? (current ??
|
|
278
|
+
candidates.find((candidate) => facts.selection &&
|
|
279
|
+
sameAccount(candidate.account, facts.selection.account)))
|
|
280
|
+
: current;
|
|
281
|
+
const windows = (holdCandidate?.confirmedBlockers ??
|
|
282
|
+
(facts.phase === 'after-limit' ? signal?.windows : undefined) ??
|
|
283
|
+
[]).filter((window) => window.confirmed);
|
|
284
|
+
if (windows.length) {
|
|
285
|
+
if (axes.shortLimit.preference === 'ready-alternative-first' && alternative)
|
|
286
|
+
return reroute(alternative);
|
|
287
|
+
const held = hold(facts, axes, history, holdCandidate, windows);
|
|
288
|
+
if (held)
|
|
289
|
+
return held;
|
|
290
|
+
}
|
|
291
|
+
const unknown = axes.unknown[facts.host];
|
|
292
|
+
const unconfirmed = facts.phase === 'after-limit' &&
|
|
293
|
+
!windows.length &&
|
|
294
|
+
(status === 429 ||
|
|
295
|
+
signal?.kind === 'rate-limit' ||
|
|
296
|
+
signal?.kind === 'quota-exhausted' ||
|
|
297
|
+
signal?.kind === 'capacity');
|
|
298
|
+
if (unconfirmed &&
|
|
299
|
+
current &&
|
|
300
|
+
(unknown.affinity === 'always' || facts.affinity === 'sticky')) {
|
|
301
|
+
if (unknown.action === 'pass')
|
|
302
|
+
return pass('unconfirmed-limit');
|
|
303
|
+
if (unknown.action === 'retain') {
|
|
304
|
+
const hints = [
|
|
305
|
+
...(facts.retryHintsMs ?? []),
|
|
306
|
+
signal?.retryAfterMs,
|
|
307
|
+
].filter((hint) => hint !== undefined && Number.isFinite(hint) && hint >= 0);
|
|
308
|
+
return {
|
|
309
|
+
action: 'retain',
|
|
310
|
+
account: current.account,
|
|
311
|
+
reason: 'unconfirmed-limit',
|
|
312
|
+
...(unknown.hint === 'minimum'
|
|
313
|
+
? { retryMs: Math.min(unknown.defaultMs, ...hints) }
|
|
314
|
+
: {}),
|
|
315
|
+
retryable: true,
|
|
316
|
+
hostRetry: continuationRetry(facts, axes),
|
|
317
|
+
history,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (alternative)
|
|
322
|
+
return reroute(alternative);
|
|
323
|
+
if (facts.phase === 'pre-send' && selectedReady)
|
|
324
|
+
return reroute(selected);
|
|
325
|
+
if (facts.host === 'opencode2' &&
|
|
326
|
+
facts.hostPhase === 'request' &&
|
|
327
|
+
axes.markedPool.requestEscape === 'ignore-timed-marks-if-all-marked' &&
|
|
328
|
+
selected &&
|
|
329
|
+
candidates.every((candidate) => !candidate.ready) &&
|
|
330
|
+
candidates.every((candidate) => candidate.blockers.length > 0 &&
|
|
331
|
+
candidate.blockers.every((blocker) => blocker.kind === 'hard')))
|
|
332
|
+
return reroute(selected);
|
|
333
|
+
const waited = poolWait(facts, axes, history, candidates);
|
|
334
|
+
if (waited)
|
|
335
|
+
return waited;
|
|
336
|
+
if (facts.phase === 'pre-send' && !candidates.length)
|
|
337
|
+
return refusal(facts, axes, history, facts.terminal?.reason ?? 'auth', facts.terminal);
|
|
338
|
+
if (facts.host === 'opencode2' &&
|
|
339
|
+
facts.hostPhase === 'retry' &&
|
|
340
|
+
!facts.terminal) {
|
|
341
|
+
if (axes.markedPool.retryExhaustion === 'host-delay')
|
|
342
|
+
return pass('host-delay');
|
|
343
|
+
if (axes.markedPool.retryExhaustion === 'same-host-delay' && current)
|
|
344
|
+
return {
|
|
345
|
+
action: 'retain',
|
|
346
|
+
account: current.account,
|
|
347
|
+
reason: 'host-delay',
|
|
348
|
+
retryable: true,
|
|
349
|
+
hostRetry: 'leave',
|
|
350
|
+
history,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
if (axes.terminalPolicy === 'status-history' &&
|
|
354
|
+
axes.terminalStatusHistory &&
|
|
355
|
+
facts.host !== 'opencode2' &&
|
|
356
|
+
!facts.terminal) {
|
|
357
|
+
const allMarked = facts.phase === 'pre-send' &&
|
|
358
|
+
candidates.length > 0 &&
|
|
359
|
+
candidates.every((candidate) => !candidate.ready);
|
|
360
|
+
if (allMarked || (status === 401 && history.saw429)) {
|
|
361
|
+
const deadlines = candidates
|
|
362
|
+
.flatMap((candidate) => candidate.blockers.map((blocker) => blocker.until))
|
|
363
|
+
.filter((until) => finiteFuture(until, facts.now));
|
|
364
|
+
return refusal(facts, axes, history, 'quota', {
|
|
365
|
+
source: 'pool',
|
|
366
|
+
reason: 'quota',
|
|
367
|
+
retryAfterMs: deadlines.length
|
|
368
|
+
? Math.max(axes.terminalStatusHistory.minRetryMs, Math.min(...deadlines) - facts.now)
|
|
369
|
+
: axes.terminalStatusHistory.defaultRetryMs,
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
return pass('original-response');
|
|
373
|
+
}
|
|
374
|
+
if (facts.terminal)
|
|
375
|
+
return refusal(facts, axes, history, facts.terminal.reason, facts.terminal);
|
|
376
|
+
if (signal?.kind === 'auth' || signal?.kind === 'account-access')
|
|
377
|
+
return refusal(facts, axes, history, 'auth');
|
|
378
|
+
if (signal ||
|
|
379
|
+
status === 429 ||
|
|
380
|
+
candidates.some((candidate) => candidate.blockers.length))
|
|
381
|
+
return refusal(facts, axes, history, 'quota');
|
|
382
|
+
return pass('no-override');
|
|
383
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Compact durations keep every numeric component below the hosts' retry codes. */
|
|
2
|
+
export declare function formatDuration(ms: number): string;
|
|
3
|
+
export interface ResetFormatInput {
|
|
4
|
+
readonly resetAt: number;
|
|
5
|
+
readonly now: number;
|
|
6
|
+
}
|
|
7
|
+
export type ResetFormatter = (input: ResetFormatInput) => string;
|
|
8
|
+
export declare const formatReset: ResetFormatter;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Compact durations keep every numeric component below the hosts' retry codes. */
|
|
2
|
+
export function formatDuration(ms) {
|
|
3
|
+
if (!Number.isFinite(ms))
|
|
4
|
+
return 'unknown';
|
|
5
|
+
const seconds = Math.ceil(Math.max(0, ms) / 1000);
|
|
6
|
+
if (seconds < 60)
|
|
7
|
+
return `${seconds}s`;
|
|
8
|
+
const minutes = Math.ceil(seconds / 60);
|
|
9
|
+
const days = Math.floor(minutes / (24 * 60));
|
|
10
|
+
if (days >= 100)
|
|
11
|
+
return 'over 99 days';
|
|
12
|
+
const hours = Math.floor(minutes / 60) % 24;
|
|
13
|
+
const remainingMinutes = minutes % 60;
|
|
14
|
+
if (days > 0)
|
|
15
|
+
return `${days}d ${hours}h ${remainingMinutes}m`;
|
|
16
|
+
if (hours > 0)
|
|
17
|
+
return `${hours}h ${remainingMinutes}m`;
|
|
18
|
+
return `${remainingMinutes}m`;
|
|
19
|
+
}
|
|
20
|
+
export const formatReset = ({ resetAt, now }) => formatDuration(resetAt - now);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type LimitsNamespace } from './namespace.js';
|
|
2
|
+
/**
|
|
3
|
+
* Where a failed detached repair is reported. The limits export takes this from
|
|
4
|
+
* the caller rather than importing the shared logger, whose file sink would pull
|
|
5
|
+
* the Windows private-files module into every limits import.
|
|
6
|
+
*/
|
|
7
|
+
export interface LimitsRecoveryLogger {
|
|
8
|
+
warn(message: string, fields?: Record<string, unknown>): void;
|
|
9
|
+
}
|
|
10
|
+
/** Readers never join the write lock; notify them after a detached repair lands. */
|
|
11
|
+
export declare class EnvelopeRepair {
|
|
12
|
+
private readonly logger?;
|
|
13
|
+
private running?;
|
|
14
|
+
private readonly listeners;
|
|
15
|
+
constructor(logger?: LimitsRecoveryLogger | undefined);
|
|
16
|
+
readonly subscribe: (listener: () => void) => (() => void);
|
|
17
|
+
schedule(commit: () => Promise<unknown>): void;
|
|
18
|
+
}
|
|
19
|
+
/** Retain only durable observations; a new, empty namespace is not lost state. */
|
|
20
|
+
export declare class EnvelopeHistory {
|
|
21
|
+
private seen?;
|
|
22
|
+
private missingGeneration?;
|
|
23
|
+
private incorporated?;
|
|
24
|
+
remember(snapshot: LimitsNamespace, previous?: LimitsNamespace): void;
|
|
25
|
+
private retained;
|
|
26
|
+
lost(present: LimitsNamespace | undefined, previous?: LimitsNamespace): boolean;
|
|
27
|
+
/** Preview a restrictive merge, or apply it against the envelope under the write lock. */
|
|
28
|
+
recover(present: LimitsNamespace | undefined, previous: LimitsNamespace | undefined, namespace: string, now: number): LimitsNamespace;
|
|
29
|
+
}
|