The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Clock from "effect/Clock"; |
| 2 | import * as Context from "effect/Context"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Ref from "effect/Ref"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | import { |
| 9 | SourceControlProviderKind as SourceControlProviderKindSchema, |
| 10 | type SourceControlProviderKind, |
| 11 | } from "@t3tools/contracts"; |
| 12 | |
| 13 | const FALLBACK_COOLDOWN = Duration.seconds(30); |
| 14 | const MAX_FALLBACK_COOLDOWN = Duration.minutes(15); |
| 15 | |
| 16 | export const CredentialScope = Context.Reference<string>( |
| 17 | "@t3tools/source-control-core/server/SourceControlRateLimit/CredentialScope", |
| 18 | { |
| 19 | defaultValue: () => "", |
| 20 | }, |
| 21 | ); |
| 22 | |
| 23 | /** Interactive requests may use reserved quota and bypass pauses caused by background reads. */ |
| 24 | export const Interactive = Context.Reference<boolean>( |
| 25 | "@t3tools/source-control-core/server/SourceControlRateLimit/Interactive", |
| 26 | { defaultValue: () => false }, |
| 27 | ); |
| 28 | |
| 29 | interface RateLimitKey { |
| 30 | readonly provider: SourceControlProviderKind; |
| 31 | readonly host: string; |
| 32 | } |
| 33 | |
| 34 | interface RateLimitLease extends RateLimitKey { |
| 35 | readonly lease: number; |
| 36 | } |
| 37 | |
| 38 | interface RateLimitEntry { |
| 39 | readonly attempt: number; |
| 40 | readonly generation: number; |
| 41 | readonly retryAt: number; |
| 42 | } |
| 43 | |
| 44 | export class SourceControlRateLimitPausedError extends Schema.TaggedError<SourceControlRateLimitPausedError>()( |
| 45 | "SourceControlRateLimitPausedError", |
| 46 | { |
| 47 | provider: SourceControlProviderKindSchema, |
| 48 | host: Schema.String, |
| 49 | retryAt: Schema.Number, |
| 50 | }, |
| 51 | ) { |
| 52 | get detail(): string { |
| 53 | return `${this.provider} requests to ${this.host} are paused until the rate limit resets.`; |
| 54 | } |
| 55 | |
| 56 | override get message(): string { |
| 57 | return this.detail; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | export class SourceControlRateLimit extends Context.Service< |
| 62 | SourceControlRateLimit, |
| 63 | { |
| 64 | readonly check: ( |
| 65 | key: RateLimitKey, |
| 66 | options?: { readonly allowPaused: boolean }, |
| 67 | ) => Effect.Effect<number, SourceControlRateLimitPausedError>; |
| 68 | readonly recordRateLimit: ( |
| 69 | input: RateLimitLease & { readonly retryAt?: number | undefined }, |
| 70 | ) => Effect.Effect<void>; |
| 71 | readonly recordSuccess: (input: RateLimitLease) => Effect.Effect<void>; |
| 72 | } |
| 73 | >()("@t3tools/source-control-core/server/SourceControlRateLimit") {} |
| 74 | |
| 75 | function normalizedKey(key: RateLimitKey, scope: string): string { |
| 76 | return `${key.provider}\0${key.host.trim().toLowerCase()}\0${scope}`; |
| 77 | } |
| 78 | |
| 79 | function fallbackCooldownMs(attempt: number): number { |
| 80 | return Math.min( |
| 81 | Duration.toMillis(FALLBACK_COOLDOWN) * 2 ** Math.max(0, attempt - 1), |
| 82 | Duration.toMillis(MAX_FALLBACK_COOLDOWN), |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | export function retryAtFromHeader(value: string | undefined, now: number): number | undefined { |
| 87 | if (value === undefined) return undefined; |
| 88 | const normalized = value.trim(); |
| 89 | if (/^\d+$/u.test(normalized)) { |
| 90 | const seconds = Number(normalized); |
| 91 | const retryAt = now + seconds * 1_000; |
| 92 | return Number.isSafeInteger(retryAt) ? retryAt : undefined; |
| 93 | } |
| 94 | const retryAt = Date.parse(normalized); |
| 95 | return Number.isFinite(retryAt) && retryAt > now ? retryAt : undefined; |
| 96 | } |
| 97 | |
| 98 | /** @public */ |
| 99 | export const make = Effect.gen(function* () { |
| 100 | const entries = yield* Ref.make<ReadonlyMap<string, RateLimitEntry>>(new Map()); |
| 101 | |
| 102 | const check: SourceControlRateLimit["Service"]["check"] = Effect.fn( |
| 103 | "SourceControlRateLimit.check", |
| 104 | )(function* (input, options) { |
| 105 | const now = yield* Clock.currentTimeMillis; |
| 106 | const key = normalizedKey(input, yield* CredentialScope); |
| 107 | const entry = (yield* Ref.get(entries)).get(key); |
| 108 | if (entry !== undefined && entry.retryAt > now && options?.allowPaused !== true) { |
| 109 | return yield* new SourceControlRateLimitPausedError({ |
| 110 | provider: input.provider, |
| 111 | host: input.host.trim().toLowerCase(), |
| 112 | retryAt: entry.retryAt, |
| 113 | }); |
| 114 | } |
| 115 | return entry?.generation ?? 0; |
| 116 | }); |
| 117 | |
| 118 | const recordRateLimit: SourceControlRateLimit["Service"]["recordRateLimit"] = Effect.fn( |
| 119 | "SourceControlRateLimit.recordRateLimit", |
| 120 | )(function* (input) { |
| 121 | const now = yield* Clock.currentTimeMillis; |
| 122 | const key = normalizedKey(input, yield* CredentialScope); |
| 123 | yield* Ref.update(entries, (current) => { |
| 124 | const previous = current.get(key); |
| 125 | if (previous !== undefined && previous.generation > input.lease) { |
| 126 | if (previous.retryAt <= now && (input.retryAt === undefined || input.retryAt <= now)) { |
| 127 | return current; |
| 128 | } |
| 129 | const retryAt = |
| 130 | input.retryAt !== undefined && input.retryAt > previous.retryAt |
| 131 | ? input.retryAt |
| 132 | : previous.retryAt; |
| 133 | if (retryAt === previous.retryAt) return current; |
| 134 | const next = new Map(current); |
| 135 | next.set(key, { ...previous, retryAt }); |
| 136 | return next; |
| 137 | } |
| 138 | |
| 139 | const attempt = (previous?.attempt ?? 0) + 1; |
| 140 | const proposedRetryAt = |
| 141 | input.retryAt !== undefined && input.retryAt > now |
| 142 | ? input.retryAt |
| 143 | : now + fallbackCooldownMs(attempt); |
| 144 | const retryAt = |
| 145 | previous !== undefined && previous.retryAt > now |
| 146 | ? Math.max(previous.retryAt, proposedRetryAt) |
| 147 | : proposedRetryAt; |
| 148 | const next = new Map(current); |
| 149 | next.set(key, { |
| 150 | attempt, |
| 151 | generation: Math.max(previous?.generation ?? 0, input.lease) + 1, |
| 152 | retryAt, |
| 153 | }); |
| 154 | return next; |
| 155 | }); |
| 156 | }); |
| 157 | |
| 158 | const recordSuccess: SourceControlRateLimit["Service"]["recordSuccess"] = Effect.fn( |
| 159 | "SourceControlRateLimit.recordSuccess", |
| 160 | )(function* (input) { |
| 161 | const now = yield* Clock.currentTimeMillis; |
| 162 | const key = normalizedKey(input, yield* CredentialScope); |
| 163 | yield* Ref.update(entries, (current) => { |
| 164 | const previous = current.get(key); |
| 165 | if (previous === undefined || previous.generation !== input.lease || previous.retryAt > now) { |
| 166 | return current; |
| 167 | } |
| 168 | const next = new Map(current); |
| 169 | next.set(key, { attempt: 0, generation: previous.generation, retryAt: 0 }); |
| 170 | return next; |
| 171 | }); |
| 172 | }); |
| 173 | |
| 174 | return SourceControlRateLimit.of({ check, recordRateLimit, recordSuccess }); |
| 175 | }); |
| 176 | |
| 177 | export const layer = Layer.effect(SourceControlRateLimit, make); |