@cortexkit/opencode-antigravity-auth 3.0.29 → 3.0.30

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 (173) hide show
  1. package/dist/cli.js +689 -183
  2. package/dist/cli.js.map +4 -4
  3. package/dist/cli.meta.json +130 -67
  4. package/dist/index.js +734 -191
  5. package/dist/index.js.map +4 -4
  6. package/dist/index.meta.json +159 -72
  7. package/dist/server.js +734 -194
  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 +2 -0
  22. package/dist/src/common-auth-embedded/claustrum/roster.js +2 -0
  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/private-files.d.ts +5 -0
  28. package/dist/src/common-auth-embedded/fs/private-files.js +7 -0
  29. package/dist/src/common-auth-embedded/fs/refresh-file-lock.js +142 -47
  30. package/dist/src/common-auth-embedded/fs/with-lock.js +2 -5
  31. package/dist/src/common-auth-embedded/limits/activity.d.ts +26 -0
  32. package/dist/src/common-auth-embedded/limits/axes.d.ts +63 -0
  33. package/dist/src/common-auth-embedded/limits/decide.d.ts +141 -0
  34. package/dist/src/common-auth-embedded/limits/duration.d.ts +8 -0
  35. package/dist/src/common-auth-embedded/limits/families.d.ts +57 -0
  36. package/dist/src/common-auth-embedded/limits/marker.d.ts +64 -0
  37. package/dist/src/common-auth-embedded/limits/request-history.d.ts +77 -0
  38. package/dist/src/common-auth-embedded/limits/scope.d.ts +12 -0
  39. package/dist/src/common-auth-embedded/limits/signal.d.ts +41 -0
  40. package/dist/src/common-auth-embedded/limits/terminal.d.ts +52 -0
  41. package/dist/src/common-auth-embedded/limits/wake.d.ts +21 -0
  42. package/dist/src/common-auth-embedded/limits/wording.d.ts +25 -0
  43. package/dist/src/common-auth-embedded/opencode2/index.d.ts +1 -1
  44. package/dist/src/common-auth-embedded/opencode2/install.d.ts +2 -2
  45. package/dist/src/common-auth-embedded/opencode2/install.js +161 -18
  46. package/dist/src/common-auth-embedded/opencode2/types.d.ts +95 -9
  47. package/dist/src/common-auth-embedded/routing/admission.d.ts +7 -2
  48. package/dist/src/common-auth-embedded/routing/admission.js +17 -7
  49. package/dist/src/common-auth-embedded/routing-accessor/handle.js +10 -0
  50. package/dist/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
  51. package/dist/src/common-auth-embedded/routing-accessor/index.js +11 -10
  52. package/dist/src/common-auth-embedded/rpc/port-file.d.ts +12 -3
  53. package/dist/src/common-auth-embedded/rpc/port-file.js +46 -4
  54. package/dist/src/common-auth-embedded/source-output.json +254 -118
  55. package/dist/src/common-auth-embedded/store/attribution.js +3 -2
  56. package/dist/src/common-auth-embedded/store/errors.d.ts +1 -1
  57. package/dist/src/common-auth-embedded/store/hooks.d.ts +5 -1
  58. package/dist/src/common-auth-embedded/store/hooks.js +20 -5
  59. package/dist/src/common-auth-embedded/store/index.d.ts +1 -0
  60. package/dist/src/common-auth-embedded/store/limits-namespace.d.ts +7 -0
  61. package/dist/src/common-auth-embedded/store/limits-namespace.js +34 -0
  62. package/dist/src/common-auth-embedded/store/pool.d.ts +10 -1
  63. package/dist/src/common-auth-embedded/store/pool.js +2 -0
  64. package/dist/src/common-auth-embedded/store/provider-state.js +5 -5
  65. package/dist/src/common-auth-embedded/store/refresh.js +1 -1
  66. package/dist/src/common-auth-embedded/store/schema.js +9 -4
  67. package/dist/src/common-auth-embedded/store/settings.d.ts +2 -0
  68. package/dist/src/common-auth-embedded/store/settings.js +11 -4
  69. package/dist/src/plugin/__fixtures__/verified-store.js +2 -2
  70. package/dist/src/runtime-layout.d.ts +9 -2
  71. package/dist/src/runtime-layout.d.ts.map +1 -1
  72. package/dist/src/runtime-layout.js +8 -8
  73. package/dist/src/runtime-layout.js.map +1 -1
  74. package/dist/src/setup.d.ts.map +1 -1
  75. package/dist/src/setup.js +29 -40
  76. package/dist/src/setup.js.map +1 -1
  77. package/dist/tui-build-map.json +9 -9
  78. package/dist/tui-ga-build-map.json +1 -1
  79. package/dist/types/src/cli.d.ts +1 -0
  80. package/dist/types/src/cli.d.ts.map +1 -1
  81. package/dist/types/src/common-auth-embedded/claustrum/consumer.d.ts +12 -0
  82. package/dist/types/src/common-auth-embedded/claustrum/custody.d.ts +16 -4
  83. package/dist/types/src/common-auth-embedded/claustrum/hub.d.ts +3 -2
  84. package/dist/types/src/common-auth-embedded/claustrum/index.d.ts +1 -0
  85. package/dist/types/src/common-auth-embedded/claustrum/roster.d.ts +2 -0
  86. package/dist/types/src/common-auth-embedded/claustrum/usage.d.ts +41 -0
  87. package/dist/types/src/common-auth-embedded/commands/builtins.d.ts +5 -0
  88. package/dist/types/src/common-auth-embedded/limits/activity.d.ts +26 -0
  89. package/dist/types/src/common-auth-embedded/limits/axes.d.ts +63 -0
  90. package/dist/types/src/common-auth-embedded/limits/decide.d.ts +141 -0
  91. package/dist/types/src/common-auth-embedded/limits/duration.d.ts +8 -0
  92. package/dist/types/src/common-auth-embedded/limits/families.d.ts +57 -0
  93. package/dist/types/src/common-auth-embedded/limits/marker.d.ts +64 -0
  94. package/dist/types/src/common-auth-embedded/limits/request-history.d.ts +77 -0
  95. package/dist/types/src/common-auth-embedded/limits/scope.d.ts +12 -0
  96. package/dist/types/src/common-auth-embedded/limits/signal.d.ts +41 -0
  97. package/dist/types/src/common-auth-embedded/limits/terminal.d.ts +52 -0
  98. package/dist/types/src/common-auth-embedded/limits/wake.d.ts +21 -0
  99. package/dist/types/src/common-auth-embedded/limits/wording.d.ts +25 -0
  100. package/dist/types/src/common-auth-embedded/opencode2/index.d.ts +1 -1
  101. package/dist/types/src/common-auth-embedded/opencode2/install.d.ts +2 -2
  102. package/dist/types/src/common-auth-embedded/opencode2/types.d.ts +95 -9
  103. package/dist/types/src/common-auth-embedded/routing/admission.d.ts +7 -2
  104. package/dist/types/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
  105. package/dist/types/src/common-auth-embedded/store/errors.d.ts +1 -1
  106. package/dist/types/src/common-auth-embedded/store/hooks.d.ts +5 -1
  107. package/dist/types/src/common-auth-embedded/store/index.d.ts +1 -0
  108. package/dist/types/src/common-auth-embedded/store/limits-namespace.d.ts +7 -0
  109. package/dist/types/src/common-auth-embedded/store/pool.d.ts +10 -1
  110. package/dist/types/src/common-auth-embedded/store/settings.d.ts +2 -0
  111. package/dist/types/src/runtime-layout.d.ts +9 -2
  112. package/dist/types/src/runtime-layout.d.ts.map +1 -1
  113. package/dist/types/src/setup.d.ts.map +1 -1
  114. package/package.json +2 -2
  115. package/src/common-auth-embedded/claustrum/consumer.d.ts +12 -0
  116. package/src/common-auth-embedded/claustrum/consumer.js +36 -0
  117. package/src/common-auth-embedded/claustrum/custody.d.ts +16 -4
  118. package/src/common-auth-embedded/claustrum/custody.js +74 -9
  119. package/src/common-auth-embedded/claustrum/hub.d.ts +3 -2
  120. package/src/common-auth-embedded/claustrum/hub.js +8 -0
  121. package/src/common-auth-embedded/claustrum/index.d.ts +1 -0
  122. package/src/common-auth-embedded/claustrum/roster.d.ts +2 -0
  123. package/src/common-auth-embedded/claustrum/roster.js +2 -0
  124. package/src/common-auth-embedded/claustrum/usage.d.ts +41 -0
  125. package/src/common-auth-embedded/claustrum/usage.js +145 -0
  126. package/src/common-auth-embedded/commands/builtins.d.ts +5 -0
  127. package/src/common-auth-embedded/commands/builtins.js +38 -0
  128. package/src/common-auth-embedded/fs/private-files.d.ts +5 -0
  129. package/src/common-auth-embedded/fs/private-files.js +7 -0
  130. package/src/common-auth-embedded/fs/refresh-file-lock.js +142 -47
  131. package/src/common-auth-embedded/fs/with-lock.js +2 -5
  132. package/src/common-auth-embedded/limits/activity.d.ts +26 -0
  133. package/src/common-auth-embedded/limits/axes.d.ts +63 -0
  134. package/src/common-auth-embedded/limits/decide.d.ts +141 -0
  135. package/src/common-auth-embedded/limits/duration.d.ts +8 -0
  136. package/src/common-auth-embedded/limits/families.d.ts +57 -0
  137. package/src/common-auth-embedded/limits/marker.d.ts +64 -0
  138. package/src/common-auth-embedded/limits/request-history.d.ts +77 -0
  139. package/src/common-auth-embedded/limits/scope.d.ts +12 -0
  140. package/src/common-auth-embedded/limits/signal.d.ts +41 -0
  141. package/src/common-auth-embedded/limits/terminal.d.ts +52 -0
  142. package/src/common-auth-embedded/limits/wake.d.ts +21 -0
  143. package/src/common-auth-embedded/limits/wording.d.ts +25 -0
  144. package/src/common-auth-embedded/opencode2/index.d.ts +1 -1
  145. package/src/common-auth-embedded/opencode2/install.d.ts +2 -2
  146. package/src/common-auth-embedded/opencode2/install.js +161 -18
  147. package/src/common-auth-embedded/opencode2/types.d.ts +95 -9
  148. package/src/common-auth-embedded/routing/admission.d.ts +7 -2
  149. package/src/common-auth-embedded/routing/admission.js +17 -7
  150. package/src/common-auth-embedded/routing-accessor/handle.js +10 -0
  151. package/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
  152. package/src/common-auth-embedded/routing-accessor/index.js +11 -10
  153. package/src/common-auth-embedded/rpc/port-file.d.ts +12 -3
  154. package/src/common-auth-embedded/rpc/port-file.js +46 -4
  155. package/src/common-auth-embedded/source-output.json +254 -118
  156. package/src/common-auth-embedded/store/attribution.js +3 -2
  157. package/src/common-auth-embedded/store/errors.d.ts +1 -1
  158. package/src/common-auth-embedded/store/hooks.d.ts +5 -1
  159. package/src/common-auth-embedded/store/hooks.js +20 -5
  160. package/src/common-auth-embedded/store/index.d.ts +1 -0
  161. package/src/common-auth-embedded/store/limits-namespace.d.ts +7 -0
  162. package/src/common-auth-embedded/store/limits-namespace.js +34 -0
  163. package/src/common-auth-embedded/store/pool.d.ts +10 -1
  164. package/src/common-auth-embedded/store/pool.js +2 -0
  165. package/src/common-auth-embedded/store/provider-state.js +5 -5
  166. package/src/common-auth-embedded/store/refresh.js +1 -1
  167. package/src/common-auth-embedded/store/schema.js +9 -4
  168. package/src/common-auth-embedded/store/settings.d.ts +2 -0
  169. package/src/common-auth-embedded/store/settings.js +11 -4
  170. package/src/tui-compiled/common-auth-embedded/fs/private-files.js +7 -0
  171. package/src/tui-compiled/common-auth-embedded/rpc/port-file.js +46 -4
  172. package/src/tui-raw/common-auth-embedded/fs/private-files.js +7 -0
  173. package/src/tui-raw/common-auth-embedded/rpc/port-file.js +46 -4
@@ -0,0 +1,41 @@
1
+ import type { ClaustrumLogger } from './errors.js';
2
+ /** Reply quota metadata, never authorization headers or bearer material. */
3
+ export interface ClaustrumUsageReading {
4
+ provider: string;
5
+ observedAtMs: number;
6
+ headers?: Record<string, string>;
7
+ frame?: unknown;
8
+ account?: string;
9
+ }
10
+ /** Structural to also typecheck with the claustrum-client 0.6.2 peer minimum. */
11
+ export interface ScopedUsageSource {
12
+ reportUsageScoped?(input: {
13
+ credentialId: string;
14
+ reading: Record<string, unknown>;
15
+ enrollmentToken?: string;
16
+ }): Promise<void>;
17
+ }
18
+ export interface ClaustrumUsageReportState {
19
+ readonly pending: number;
20
+ readonly inFlight: boolean;
21
+ readonly dropped: number;
22
+ }
23
+ /** One queue per custody connection, shared by every hub member on it. */
24
+ export declare class ScopedUsageReporter {
25
+ #private;
26
+ private readonly options;
27
+ constructor(options: {
28
+ client: ScopedUsageSource;
29
+ owns: (credentialId: string) => boolean;
30
+ token: () => Promise<string>;
31
+ logger: ClaustrumLogger;
32
+ setTimeout: typeof setTimeout;
33
+ clearTimeout: typeof clearTimeout;
34
+ });
35
+ state(): ClaustrumUsageReportState;
36
+ report(credentialId: string, reading: ClaustrumUsageReading): void;
37
+ forget(credentialId: string): void;
38
+ /** Start the final flush; the custody's existing shutdown drain bounds it. */
39
+ close(): Promise<void> | undefined;
40
+ drop(): void;
41
+ }
@@ -41,6 +41,11 @@ export interface QuotaSectionOptions {
41
41
  check?(ids: readonly string[], invocation: CommandInvocation): Promise<void>;
42
42
  }
43
43
  export interface RoutingSectionOptions {
44
+ /**
45
+ * Show how long the plugin may wait for the current account's limit to
46
+ * reset before switching. Opt in only when the plugin supports that policy.
47
+ */
48
+ sameAccountWait?: boolean;
44
49
  /**
45
50
  * Further `routing.mode` values the plugin routes as `ordered` with the
46
51
  * former main row moved (`main-first`, `fallback-first`), offered between
@@ -0,0 +1,26 @@
1
+ import type { AccountKey } from './signal.js';
2
+ export interface SessionActivity {
3
+ readonly accountKey: AccountKey;
4
+ readonly lastAcceptedAt: number;
5
+ readonly ttlMs: number;
6
+ }
7
+ export interface AcceptedActivity {
8
+ readonly sessionId: string;
9
+ readonly accountKey: AccountKey;
10
+ /** The authoritative account key at completion, not the key at request start. */
11
+ readonly currentAccountKey: AccountKey;
12
+ readonly lastAcceptedAt: number;
13
+ /** TTLs of prompt segments cached by this response, or their single longest TTL. */
14
+ readonly ttlMs?: number | readonly number[];
15
+ }
16
+ export interface SessionActivityStore {
17
+ readonly size: number;
18
+ /** Call for accepted responses and successful (2xx) keep-warm prewarms. */
19
+ recordAccepted(input: AcceptedActivity): boolean;
20
+ get(sessionId: string): SessionActivity | undefined;
21
+ /** Call both when forgetting a session and when the host deletes it. */
22
+ forgetSession(sessionId: string): void;
23
+ }
24
+ export declare function effectiveCacheTtlMs(ttlMs: number | undefined): number;
25
+ /** One process-local cache record per session; no credential or store I/O. */
26
+ export declare function createSessionActivityStore(): SessionActivityStore;
@@ -0,0 +1,63 @@
1
+ import type { HostRetry, RefusalFamily } from './families.js';
2
+ import type { LimitMark, MarkerAxes } from './marker.js';
3
+ import type { FamilyOf } from './scope.js';
4
+ export type LimitHost = 'opencode1' | 'opencode2' | 'pi';
5
+ export type HostValues<T> = Readonly<Record<LimitHost, T>>;
6
+ export type RefusalReason = 'auth' | 'quota' | 'incomplete' | 'model-unavailable';
7
+ export type DecisionFamily = RefusalFamily | 'local-auth' | 'native-vault-refusal' | 'no-account';
8
+ export interface RefusalPolicy {
9
+ readonly family: DecisionFamily;
10
+ readonly messageKey: string;
11
+ readonly retryable: boolean;
12
+ readonly hostRetry: HostRetry;
13
+ }
14
+ export interface UnknownLimitPolicy {
15
+ readonly action: 'retain' | 'reselect' | 'pass';
16
+ readonly affinity: 'always' | 'sticky';
17
+ readonly hint: 'none' | 'minimum';
18
+ readonly defaultMs: number;
19
+ }
20
+ /** Policies consume already parsed, applicable evidence, never provider names. */
21
+ export interface LimitAxes {
22
+ readonly evidence: 'per-field-overlay' | 'typed' | 'named-window' | 'hints-only';
23
+ readonly markers: HostValues<MarkerAxes>;
24
+ readonly readingClearsCoveredScopes: 'on' | 'off';
25
+ readonly successClearsCoveredScopes: 'on' | 'off';
26
+ readonly unknown: HostValues<UnknownLimitPolicy>;
27
+ readonly shortLimit: {
28
+ readonly holdBound: 'cache-ttl' | 'disabled';
29
+ readonly preference: 'same-account-first' | 'ready-alternative-first';
30
+ readonly jitter: 'session-sha256' | 'none';
31
+ };
32
+ /** Bounded retention is not an adopted policy. */
33
+ readonly retainBound: 'none';
34
+ readonly poolWaiting: {
35
+ readonly kind: 'off';
36
+ } | {
37
+ readonly kind: 'earliest';
38
+ readonly budgetMs: number;
39
+ readonly requestDeadlineMs: number;
40
+ readonly hardFallbackMs: number;
41
+ readonly singleEligibleSoftExemption: boolean;
42
+ };
43
+ readonly execution: 'host' | 'engine';
44
+ readonly requestExclusion: 'on' | 'off';
45
+ readonly advance: HostValues<{
46
+ readonly statuses: readonly number[];
47
+ readonly attributedLimitOnly: boolean;
48
+ }>;
49
+ readonly markedPool: {
50
+ readonly requestEscape: 'off' | 'ignore-timed-marks-if-all-marked';
51
+ readonly retryExhaustion: 'terminal' | 'same-host-delay' | 'host-delay';
52
+ };
53
+ readonly terminalMetadata: 'setting-capped-fresh' | 'baseline';
54
+ readonly terminalPolicy: 'provided' | 'pool' | 'status-history';
55
+ /** Required when selecting status-history; other terminal policies ignore it. */
56
+ readonly terminalStatusHistory?: {
57
+ readonly minRetryMs: number;
58
+ readonly defaultRetryMs: number;
59
+ };
60
+ readonly refusals: Readonly<Record<RefusalReason, HostValues<RefusalPolicy>>>;
61
+ }
62
+ /** The caller supplies only this account's marks; other models remain blocked. */
63
+ export declare function marksAfterSuccess<T extends LimitMark>(marks: readonly T[], axes: Pick<LimitAxes, 'successClearsCoveredScopes'>, model: string, familyOf: FamilyOf): readonly T[];
@@ -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,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,57 @@
1
+ import { type LimitMessages } from './wording.js';
2
+ export type OpenCode1Family = 'openai-429' | 'claude-429' | 'json-412' | 'claustrum-429' | 'claude-econnreset';
3
+ export type RefusalFamily = OpenCode1Family | 'pi-final';
4
+ export type HostRetry = 'leave' | 'veto';
5
+ export interface RefusalInput {
6
+ readonly family: RefusalFamily;
7
+ readonly messageKey: string;
8
+ readonly messages?: LimitMessages;
9
+ readonly parameters?: {
10
+ readonly labels?: readonly string[];
11
+ };
12
+ /**
13
+ * Set only for re-login guidance, where the user needs to know which account to sign in again.
14
+ * Ordinary refusals omit labels, even when the caller supplies them.
15
+ */
16
+ readonly labelPolicy?: 'relogin';
17
+ readonly now: number;
18
+ /** A retained reset, already filtered for freshness by the decision layer. */
19
+ readonly resetAt?: number;
20
+ readonly retryAfterMs?: number;
21
+ /** Time already spent presenting a notice, not an instruction to sleep. */
22
+ readonly noticeDelayMs?: number;
23
+ /** Additional provider headers are preserved by the JSON 412 family only. */
24
+ readonly extraHeaders?: ConstructorParameters<typeof Headers>[0];
25
+ }
26
+ interface RefusalAttributes {
27
+ readonly family: RefusalFamily;
28
+ readonly disposition: 'refuse';
29
+ readonly retryable: boolean;
30
+ readonly hostRetry: HostRetry;
31
+ }
32
+ export interface HttpRefusal extends RefusalAttributes {
33
+ readonly kind: 'http';
34
+ readonly status: 412 | 429;
35
+ readonly headers: Headers;
36
+ readonly body: string;
37
+ }
38
+ export interface TextRefusal extends RefusalAttributes {
39
+ readonly kind: 'text';
40
+ readonly body: string;
41
+ }
42
+ export declare class ClaudeIncompleteError extends Error {
43
+ readonly code = "ECONNRESET";
44
+ readonly syscall = "sticky-routing";
45
+ readonly isRetryable = true;
46
+ constructor(message: string);
47
+ }
48
+ export interface ThrownRefusal extends RefusalAttributes {
49
+ readonly kind: 'throw';
50
+ readonly error: ClaudeIncompleteError;
51
+ }
52
+ export type BuiltRefusal = HttpRefusal | TextRefusal | ThrownRefusal;
53
+ /** Compute metadata at delivery time, after time spent showing a notice. */
54
+ export declare function retryAfterSeconds(retryAfterMs: number | undefined, noticeDelayMs?: number): number | undefined;
55
+ /** Pure wire construction: the caller sends the response, emits text, or throws the error. */
56
+ export declare function buildRefusal(input: RefusalInput): BuiltRefusal;
57
+ export {};
@@ -0,0 +1,64 @@
1
+ import type { Scope } from './scope.js';
2
+ import type { AccountKey, LimitKind, LimitSignal } from './signal.js';
3
+ export type MergeLaw = 'latest-evidence' | 'max-until' | 'overwrite';
4
+ export interface LimitMark {
5
+ readonly scope: Scope;
6
+ readonly until: number;
7
+ readonly mergeLaw: MergeLaw;
8
+ }
9
+ export interface FailureLadderAxes {
10
+ readonly kind: 'failure-ladder';
11
+ readonly mergeLaw: MergeLaw;
12
+ readonly hintFloorMs: number;
13
+ readonly quotaSteps: readonly number[];
14
+ readonly byKind: Readonly<Partial<Record<LimitKind, number>>>;
15
+ readonly byReason: Readonly<Record<string, number>>;
16
+ readonly capacity: {
17
+ readonly baseMs: number;
18
+ /** Maximum displacement on either side of baseMs. */
19
+ readonly spreadMs: number;
20
+ };
21
+ readonly defaultMs: number;
22
+ }
23
+ export declare const ANTIGRAVITY_FAILURE_LADDER: FailureLadderAxes;
24
+ export type MarkerAxes = {
25
+ readonly kind: 'off';
26
+ } | {
27
+ readonly kind: 'hint';
28
+ readonly mergeLaw: MergeLaw;
29
+ readonly defaultMs: number;
30
+ readonly minimumMs: number;
31
+ readonly zero: 'accept' | 'skip';
32
+ /** Core absolute dates and host event-relative hints use different samples. */
33
+ readonly hintClock: 'signal' | 'marker';
34
+ } | {
35
+ readonly kind: 'reset';
36
+ readonly mergeLaw: MergeLaw;
37
+ readonly defaultMs: number;
38
+ } | FailureLadderAxes;
39
+ export interface FailureStreak {
40
+ readonly count: number;
41
+ readonly lastFailureAt: number;
42
+ }
43
+ /** Owned by the process, not a persisted registry snapshot. */
44
+ export type FailureHistory = ReadonlyMap<string, FailureStreak>;
45
+ export type FailureEvent = {
46
+ readonly type: 'failure';
47
+ readonly account: AccountKey;
48
+ readonly at: number;
49
+ } | {
50
+ readonly type: 'success';
51
+ readonly account: AccountKey;
52
+ };
53
+ /** Feed committed marker events, not repeated reducer evaluations, into history. */
54
+ export declare function reduceFailureHistory(history: FailureHistory, event: FailureEvent, ttlMs?: number): FailureHistory;
55
+ export interface MarkerHistory {
56
+ readonly now: number;
57
+ /** Includes the current failure event; success removes only that account's streak. */
58
+ readonly failures?: FailureHistory;
59
+ /** Uniform sample in [0, 1] for the capacity fallback duration; omitted means midpoint. */
60
+ readonly random?: number;
61
+ }
62
+ /** No clocks, random draws, state updates or I/O occur while producing a mark. */
63
+ export declare function markFromSignal(signal: LimitSignal | undefined, axes: MarkerAxes, history: MarkerHistory): LimitMark | undefined;
64
+ export declare function markIsActive(mark: LimitMark, now: number): boolean;
@@ -0,0 +1,77 @@
1
+ import type { Scope } from './scope.js';
2
+ import type { AccountKey } from './signal.js';
3
+ export declare function sameAccount(a: AccountKey, b: AccountKey): boolean;
4
+ export interface RequestExclusion {
5
+ readonly account: AccountKey;
6
+ readonly scope: Scope;
7
+ }
8
+ export interface RetainHistory {
9
+ readonly account: AccountKey;
10
+ readonly count: number;
11
+ readonly startedAt: number;
12
+ readonly lastRetainedAt: number;
13
+ }
14
+ export interface RequestHistory {
15
+ /** A request/turn token, not a session id. */
16
+ readonly requestId: string;
17
+ readonly originalUrl: string;
18
+ readonly requestStartedAt: number;
19
+ readonly saw429: boolean;
20
+ readonly lastStatus?: number;
21
+ readonly tried: readonly RequestExclusion[];
22
+ readonly excludedForRequest: readonly RequestExclusion[];
23
+ readonly retains: readonly RetainHistory[];
24
+ readonly retainedAttempts: readonly string[];
25
+ readonly excludedAttempts: readonly string[];
26
+ readonly switchedAttempts: readonly string[];
27
+ readonly switches: number;
28
+ readonly blockedSince?: number;
29
+ }
30
+ export declare function createRequestHistory(input: {
31
+ readonly requestId: string;
32
+ readonly originalUrl: string;
33
+ readonly clockNow: number;
34
+ }): RequestHistory;
35
+ export declare function isRequestExcluded(history: RequestHistory, account: AccountKey, scope: Scope): boolean;
36
+ export type RequestHistoryEvent = {
37
+ readonly type: 'response';
38
+ readonly account: AccountKey;
39
+ readonly scope: Scope;
40
+ readonly status?: number;
41
+ } | {
42
+ readonly type: 'retain';
43
+ readonly account: AccountKey;
44
+ readonly attemptId: string;
45
+ readonly clockNow: number;
46
+ readonly cause: 'unconfirmed-limit' | 'credential-recovery';
47
+ } | {
48
+ readonly type: 'exclude';
49
+ readonly account: AccountKey;
50
+ readonly scope: Scope;
51
+ readonly attemptId: string;
52
+ } | {
53
+ readonly type: 'switch';
54
+ readonly attemptId: string;
55
+ } | {
56
+ readonly type: 'blocked';
57
+ readonly clockNow: number;
58
+ } | {
59
+ readonly type: 'selected';
60
+ };
61
+ /** Retain/exclude/switch events are committed by the executor, not by polling. */
62
+ export declare function reduceRequestHistory(history: RequestHistory, event: RequestHistoryEvent): RequestHistory;
63
+ export declare const REQUEST_EXCLUSION_EFFECTS: Readonly<{
64
+ readonly refundHybridToken: true;
65
+ readonly cancelDiscardedBody: true;
66
+ readonly addMark: false;
67
+ readonly addCooldown: false;
68
+ readonly penalizeHealth: false;
69
+ readonly chargeSwitch: false;
70
+ }>;
71
+ /** Run the returned effects once, then reselect with the returned history. */
72
+ export declare function commitRequestExclusion(history: RequestHistory, entry: RequestExclusion & {
73
+ readonly attemptId: string;
74
+ }): {
75
+ readonly history: RequestHistory;
76
+ readonly effects?: typeof REQUEST_EXCLUSION_EFFECTS;
77
+ };
@@ -0,0 +1,12 @@
1
+ /** Applicability is a plugin fact, never a substring match on a model id. */
2
+ export type Scope = {
3
+ readonly kind: 'pool';
4
+ } | {
5
+ readonly kind: 'family';
6
+ readonly family: string;
7
+ } | {
8
+ readonly kind: 'model';
9
+ readonly model: string;
10
+ };
11
+ export type FamilyOf = (model: string) => string | undefined;
12
+ export declare function scopeCovers(scope: Scope, model: string, familyOf: FamilyOf): boolean;
@@ -0,0 +1,41 @@
1
+ import type { Scope } from './scope.js';
2
+ /** Refreshing credentials preserves this key; replacing an identity does not. */
3
+ export interface AccountKey {
4
+ readonly namespace: string;
5
+ readonly accountId: string;
6
+ readonly incarnation: string;
7
+ }
8
+ export type LimitKind = 'quota-exhausted' | 'rate-limit' | 'capacity' | 'payment-balance' | 'payment-cap' | 'auth' | 'account-access' | 'model-unavailable';
9
+ export interface LimitWindow {
10
+ readonly window: string;
11
+ readonly confirmed: boolean;
12
+ readonly confirmedByReading: boolean;
13
+ readonly resetAt?: number;
14
+ readonly resetSource?: string;
15
+ readonly observedAt: number;
16
+ readonly provenance: string;
17
+ }
18
+ /** A retry hint does not by itself confirm any exhausted window. */
19
+ export interface LimitSignal {
20
+ readonly account: AccountKey;
21
+ readonly kind: LimitKind;
22
+ readonly reason: string;
23
+ readonly status?: number;
24
+ readonly scope: Scope;
25
+ readonly windows: readonly LimitWindow[];
26
+ readonly observedAt: number;
27
+ readonly retryAfterMs?: number;
28
+ readonly retryAfterSource?: string;
29
+ }
30
+ export interface ClassifierInput {
31
+ readonly account: AccountKey;
32
+ readonly status: number;
33
+ readonly headers: Headers;
34
+ readonly body?: unknown;
35
+ readonly firstEvent?: unknown;
36
+ readonly effectiveModel: string;
37
+ readonly readings: readonly LimitWindow[];
38
+ readonly now: number;
39
+ }
40
+ /** Returning nothing must not manufacture an exhaustion mark. */
41
+ export type LimitClassifier = (input: ClassifierInput) => LimitSignal | undefined;
@@ -0,0 +1,52 @@
1
+ import type { SessionActivity } from './activity.js';
2
+ import type { LimitAxes } from './axes.js';
3
+ import type { LimitDecision, PolicyFacts, TerminalFacts, WaitCheckpoint } from './decide.js';
4
+ import type { RequestHistory } from './request-history.js';
5
+ import type { FamilyOf, Scope } from './scope.js';
6
+ import type { AccountKey } from './signal.js';
7
+ export interface TerminalWindow {
8
+ readonly window: string;
9
+ readonly scope: Scope;
10
+ /** The caller evaluates quota thresholds, not just provider exhaustion. */
11
+ readonly blockedBy?: 'exhausted' | 'floor' | 'killswitch-floor';
12
+ readonly reset?: {
13
+ readonly at: number;
14
+ /** Provenance of this reset field, never the enclosing row's update time. */
15
+ readonly observedAt?: number;
16
+ readonly checkedAt?: number;
17
+ };
18
+ }
19
+ export interface TerminalCandidate {
20
+ readonly account: AccountKey;
21
+ readonly readingAccount: AccountKey;
22
+ readonly identity?: string;
23
+ readonly readingIdentity?: string;
24
+ readonly windows: readonly TerminalWindow[];
25
+ }
26
+ export interface TerminalScanFacts {
27
+ /** Candidate keys come from the caller's scoped/general terminal scan. */
28
+ readonly candidates: readonly TerminalCandidate[];
29
+ readonly effectiveModel: string;
30
+ readonly familyOf: FamilyOf;
31
+ readonly maxAgeMs: number;
32
+ readonly maxSameAccountWait?: unknown;
33
+ readonly activity?: SessionActivity;
34
+ /** Current session account, used to reject another account's cache record. */
35
+ readonly accountKey?: AccountKey;
36
+ }
37
+ export interface TerminalReset {
38
+ readonly account: AccountKey;
39
+ readonly resetAt: number;
40
+ readonly checkpoint: WaitCheckpoint;
41
+ }
42
+ /**
43
+ * Take each account's latest blocking reset, then the earliest across accounts.
44
+ * A missing identity is compatible, but readings from before re-login are not.
45
+ */
46
+ export declare function scanTerminalReset(input: TerminalScanFacts, now: number): TerminalReset | undefined;
47
+ export declare function terminalWaitDisabled(metadata: TerminalFacts, axes: LimitAxes): boolean;
48
+ /**
49
+ * When no account is ready, cap the wait by the setting rather than cache expiry.
50
+ * Unlike a same-account hold, this wait may proceed without a cache activity record.
51
+ */
52
+ export declare function terminalRefusalDecision(facts: PolicyFacts, axes: LimitAxes, history: RequestHistory, metadata: TerminalFacts): LimitDecision | undefined;
@@ -0,0 +1,21 @@
1
+ import type { WaitCheckpoint } from './decide.js';
2
+ import type { AccountKey, LimitWindow } from './signal.js';
3
+ export interface WakeFacts {
4
+ readonly checkpoint: WaitCheckpoint;
5
+ readonly target: number;
6
+ /**
7
+ * Ordinary holds require this session/account's provider prompt cache to
8
+ * remain valid; terminal waits instead wait for pool quota, without a cache.
9
+ */
10
+ readonly kind?: 'hold' | 'terminal';
11
+ }
12
+ /**
13
+ * Snapshot exhaustion blockers only. A wait caused by a minimum-remaining quota
14
+ * threshold supplies no windows: elapsed time cannot prove renewed headroom.
15
+ */
16
+ export declare function waitCheckpoint(accountKey: AccountKey, decidedAt: number, windows: readonly LimitWindow[]): WaitCheckpoint;
17
+ /**
18
+ * Match exhaustion evidence for one wake dispatch, without refreshing quota.
19
+ * The caller must still check admission, other blockers and any cache bound.
20
+ */
21
+ export declare function liftedWindowsForWake(wake: WakeFacts, account: AccountKey, windows: readonly LimitWindow[], now: number): readonly string[];
@@ -0,0 +1,25 @@
1
+ import type { ResetFormatter } from './duration.js';
2
+ export declare function hasUnsafeWording(text: string): boolean;
3
+ export interface MessageParameters {
4
+ readonly labels: readonly string[];
5
+ readonly retryIn?: string;
6
+ readonly reset?: string;
7
+ }
8
+ /** Templates return text only; they cannot replace a family's wire envelope. */
9
+ export type MessageTemplate = (parameters: MessageParameters) => string;
10
+ export declare class UnsafeWordingError extends Error {
11
+ readonly messageKey: string;
12
+ readonly code = "unsafe-wording";
13
+ constructor(messageKey: string);
14
+ }
15
+ export interface LimitMessageOptions {
16
+ readonly refusals?: Readonly<Record<string, MessageTemplate>>;
17
+ readonly retryable?: Readonly<Record<string, MessageTemplate>>;
18
+ readonly formatReset?: ResetFormatter;
19
+ }
20
+ export interface LimitMessages {
21
+ readonly formatReset?: ResetFormatter;
22
+ readonly render: (messageKey: string, parameters: MessageParameters, nonRetryable: boolean) => string | undefined;
23
+ }
24
+ /** Registration rejects unsafe fixed text; actual substitutions are checked again. */
25
+ export declare function registerLimitMessages(options: LimitMessageOptions): LimitMessages;
@@ -5,4 +5,4 @@ export type { FormAnswer, LoginContext, PoolAuthorization, PoolLoginMethod, Regi
5
5
  export { isPlaceholderCredential, PLACEHOLDER_LIFETIME_MS, PLACEHOLDER_METADATA_KEY, PLACEHOLDER_PREFIX, placeholderCredential, placeholderSecret, registerOpenCode2AuthMethods, VAULT_ACTIVATION_INSTRUCTIONS, } from './integration.js';
6
6
  export type { ServerSentEvent } from './sse.js';
7
7
  export { watchServerSentEvents } from './sse.js';
8
- export type { AccountHeadersResult, AccountRequest, Attempt, AttemptEndReason, AttemptOutcome, ChooseAccountInput, ChooseAccountResult, EventVerdict, HeaderEdits, HostError, InstallOpenCode2AuthOptions, LimitSignal, OpenCode2AuthAdapter, OpenCode2AuthEventName, OpenCode2AuthEvents, OpenCode2AuthInstallation, OpenCode2AuthLogger, OpenCode2HookContext, RequestKind, RequestScope, ResponseAccount, RetryHold, RetryReason, SameAccountRetryInput, SameAccountRetryResult, SelectingHook, Transport, } from './types.js';
8
+ export type { AccountHeadersResult, AccountRequest, Attempt, AttemptEndReason, AttemptOutcome, ChooseAccountInput, ChooseAccountResult, EventVerdict, HeaderEdits, HostError, InstallOpenCode2AuthOptions, LimitSignal, OpenCode2AuthAdapter, OpenCode2AuthEventName, OpenCode2AuthEvents, OpenCode2AuthInstallation, OpenCode2AuthLogger, OpenCode2HookContext, RequestKind, RequestScope, ResponseAccount, RetryDisposition, RetryHold, RetryReason, RetryWaitThenReselect, RetryWake, SameAccountRetryInput, SameAccountRetryResult, SelectingHook, Transport, } from './types.js';