Repository navigation
Complete coderouter private-beta routing reliability #9692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { removeAccount } from "../../../../../services/coderouter/accounts"; | ||
| import { resolveCodeRouterRequestContext } from "../../../../../services/coderouter/requestContext"; | ||
|
|
||
| export const runtime = "nodejs"; | ||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; | ||
|
|
||
| export function createDeleteAccountHandler(dependencies: { | ||
| readonly resolve: typeof resolveCodeRouterRequestContext; | ||
| readonly remove: (input: { | ||
| readonly teamId: string; | ||
| readonly accountId: string; | ||
| }) => ReturnType<typeof removeAccount>; | ||
| }) { | ||
| return async ( | ||
| request: Request, | ||
| context: { params: Promise<{ accountId: string }> }, | ||
| ): Promise<Response> => { | ||
| const resolved = await dependencies.resolve(request, "manage"); | ||
| if (!resolved.ok) return resolved.response; | ||
| const { accountId } = await context.params; | ||
| if (!UUID.test(accountId)) { | ||
| return Response.json({ error: "invalid_request" }, { status: 400 }); | ||
| } | ||
| const result = await dependencies.remove({ | ||
| teamId: resolved.value.team.teamId, | ||
| accountId, | ||
| }); | ||
| if (!result.removed) { | ||
| return Response.json({ error: "not_found" }, { status: 404 }); | ||
| } | ||
| return Response.json(result, { | ||
| headers: { "cache-control": "no-store" }, | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| export const DELETE = createDeleteAccountHandler({ | ||
| resolve: resolveCodeRouterRequestContext, | ||
| remove: async ({ teamId, accountId }) => | ||
| await removeAccount(teamId, accountId), | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,10 +35,15 @@ export default function CoderouterLandingPage() { | |
| cr codex | ||
| </code> | ||
| <span className="select-none rounded-lg bg-black px-4 py-2 font-mono text-xs text-white"> | ||
| coming soon | ||
| private beta | ||
| </span> | ||
| </div> | ||
|
|
||
| <p className="mt-5 max-w-xl font-mono text-[11px] leading-5 text-black/40"> | ||
| Codex and OpenCode Go are available now. Pi support is experimental. | ||
| Claude Max and API-key routing are coming soon. | ||
| </p> | ||
|
Comment on lines
+38
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift Move the new landing-page copy into the locale catalogs. Lines 38, 43, and 44 add raw English user-facing text. Read these strings from As per coding guidelines, “User-facing web UI text ... must use 🤖 Prompt for AI AgentsSources: Coding guidelines, Path instructions |
||
|
|
||
| <div className="mt-20 grid max-w-3xl gap-px overflow-hidden rounded-xl border border-black/10 bg-black/10 sm:grid-cols-3"> | ||
| {[ | ||
| ["01", "connect", "Add subscriptions with provider-native authorization."], | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| import { randomUUID } from "node:crypto"; | ||
| import { | ||
| findAccountByProviderIdentity, | ||
| deleteAccount, | ||
| insertAccountWithCredential, | ||
| listAccounts, | ||
| replaceAccountCredential, | ||
| withVaultLease, | ||
| } from "./repository"; | ||
| import { encryptCredential } from "./encryption"; | ||
| import type { CodeRouterCredential } from "./types"; | ||
| import { deleteVaultCredential } from "./vault"; | ||
| import { reportCoderouterFailure } from "./observability"; | ||
|
|
||
| export async function addAccount( | ||
| teamId: string, | ||
|
|
@@ -56,6 +60,43 @@ export async function addAccount( | |
|
|
||
| export { listAccounts }; | ||
|
|
||
| type RemoveAccountResult = { | ||
| removed: boolean; | ||
| lastAccount: boolean; | ||
| legacyCleanupPending: boolean; | ||
| }; | ||
|
|
||
| export function createAccountRemover(dependencies: { | ||
| readonly deleteRuntime: typeof deleteAccount; | ||
| readonly deleteLegacy: typeof deleteVaultCredential; | ||
| readonly withLease: typeof withVaultLease; | ||
| readonly report: typeof reportCoderouterFailure; | ||
| }): (teamId: string, accountId: string) => Promise<RemoveAccountResult> { | ||
| return async (teamId, accountId) => { | ||
| const result = await dependencies.deleteRuntime({ teamId, accountId }); | ||
| if (!result.removed) return { ...result, legacyCleanupPending: false }; | ||
| try { | ||
| // Temporary rollback copy only. This call disappears after the migration | ||
| // cleanup window; failure cannot restore runtime access to the credential. | ||
| await dependencies.withLease( | ||
| teamId, | ||
| async () => await dependencies.deleteLegacy(teamId, accountId), | ||
| ); | ||
| return { ...result, legacyCleanupPending: false }; | ||
| } catch (error) { | ||
| dependencies.report("legacy_cleanup", error); | ||
| return { ...result, legacyCleanupPending: true }; | ||
|
Comment on lines
+76
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "== file sizes =="
wc -l web/services/coderouter/accounts.ts web/app/api/coderouter/accounts/[accountId]/route.ts web/tests/coderouter-account-removal.test.ts web/services/coderouter/vault.ts web/services/coderouter/repository.ts 2>/dev/null || true
echo "== accounts.ts relevant section =="
sed -n '1,140p' web/services/coderouter/accounts.ts
echo "== route.ts =="
sed -n '1,90p' 'web/app/api/coderouter/accounts/[accountId]/route.ts'
echo "== tests relevant section =="
sed -n '1,180p' web/tests/coderouter-account-removal.test.ts
echo "== search legacy cleanup pending / reconciler / deleteVaultCredential =="
rg -n "legacyCleanupPending|clean.*legacy|deleteLegacy|deleteVault|legacy cleanup|legacy_cleanup|Delete.*Account|removeAccount" web -S || trueRepository: manaflow-ai/cmux Length of output: 13763 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "== file sizes =="
wc -l web/services/coderouter/accounts.ts 'web/app/api/coderouter/accounts/[accountId]/route.ts' web/tests/coderouter-account-removal.test.ts web/services/coderouter/vault.ts web/services/coderouter/repository.ts 2>/dev/null || true
echo "== accounts.ts relevant section =="
sed -n '1,140p' web/services/coderouter/accounts.ts
echo "== route.ts =="
sed -n '1,90p' 'web/app/api/coderouter/accounts/[accountId]/route.ts'
echo "== tests relevant section =="
sed -n '1,180p' web/tests/coderouter-account-removal.test.ts
echo "== search legacy cleanup pending / reconciler / deleteVaultCredential =="
rg -n "legacyCleanupPending|clean.*legacy|deleteLegacy|deleteVault|legacy cleanup|legacy_cleanup|Delete.*Account|removeAccount" web -S || trueRepository: manaflow-ai/cmux Length of output: 13763 Sensitive Data Exposure (CWE-459) Reachability: Internal Reachability pathMake failed legacy cleanup recoverable.
🤖 Prompt for AI Agents |
||
| } | ||
| }; | ||
| } | ||
|
|
||
| export const removeAccount = createAccountRemover({ | ||
| deleteRuntime: deleteAccount, | ||
| deleteLegacy: deleteVaultCredential, | ||
| withLease: withVaultLease, | ||
| report: reportCoderouterFailure, | ||
| }); | ||
|
|
||
| export function parseCredential(value: unknown): CodeRouterCredential | null { | ||
| if (!isRecord(value)) return null; | ||
| const provider = value.provider; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ import { | |
| selectAccountForRequest, | ||
| } from "./repository"; | ||
| import { freshCredential } from "./refresh"; | ||
| import { fetchProviderRead } from "./providerFetch"; | ||
| import { reportCoderouterFailure } from "./observability"; | ||
|
|
||
| const CODEX_UPSTREAM = "https://chatgpt.com/backend-api/codex/responses"; | ||
| const CODEX_MODELS_UPSTREAM = "https://chatgpt.com/backend-api/codex/models"; | ||
|
|
@@ -70,6 +72,10 @@ export async function proxyCodexRequest(request: Request): Promise<Response> { | |
| } | ||
| } | ||
| if (upstream.status === 429) { | ||
| reportCoderouterFailure("provider_rate_limit", new Error("rate limited"), { | ||
| provider: "codex", | ||
| status: 429, | ||
| }); | ||
| await markAccountCooldown(account.id, rateLimitDelay(upstream.headers)); | ||
| continue; | ||
| } | ||
|
|
@@ -95,60 +101,82 @@ export async function proxyCodexRequest(request: Request): Promise<Response> { | |
| }); | ||
| } | ||
|
|
||
| export async function proxyCodexModels(request: Request): Promise<Response> { | ||
| const token = bearerToken(request); | ||
| if (!token) return jsonError("unauthorized", 401); | ||
| const identity = await authenticateRouteToken(token); | ||
| if (!identity) return jsonError("unauthorized", 401); | ||
| type CodexModelsDependencies = { | ||
| readonly authenticate: typeof authenticateRouteToken; | ||
| readonly select: typeof selectAccountForRequest; | ||
| readonly credential: typeof freshCredential; | ||
| readonly cooldown: typeof markAccountCooldown; | ||
| readonly providerRead: typeof fetchProviderRead; | ||
| }; | ||
|
|
||
| const attempted: string[] = []; | ||
| let upstream: Response | null = null; | ||
| for (let attempt = 0; attempt < 8; attempt++) { | ||
| const account = await selectAccountForRequest( | ||
| identity.teamId, | ||
| "codex", | ||
| attempted, | ||
| ); | ||
| if (!account) break; | ||
| attempted.push(account.id); | ||
| let credential; | ||
| try { | ||
| credential = await freshCredential({ | ||
| teamId: identity.teamId, | ||
| accountId: account.id, | ||
| expectedRevision: account.vaultRevision, | ||
| }); | ||
| } catch { | ||
| continue; | ||
| export function createCodexModelsProxy(dependencies: CodexModelsDependencies) { | ||
| return async (request: Request): Promise<Response> => { | ||
| const token = bearerToken(request); | ||
| if (!token) return jsonError("unauthorized", 401); | ||
| const identity = await dependencies.authenticate(token); | ||
| if (!identity) return jsonError("unauthorized", 401); | ||
|
|
||
| const attempted: string[] = []; | ||
| let upstream: Response | null = null; | ||
| for (let attempt = 0; attempt < 8; attempt++) { | ||
| const account = await dependencies.select( | ||
| identity.teamId, | ||
| "codex", | ||
| attempted, | ||
| ); | ||
| if (!account) break; | ||
| attempted.push(account.id); | ||
| let credential; | ||
| try { | ||
| credential = await dependencies.credential({ | ||
| teamId: identity.teamId, | ||
| accountId: account.id, | ||
| expectedRevision: account.vaultRevision, | ||
| }); | ||
| } catch { | ||
| continue; | ||
| } | ||
|
Comment on lines
+129
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Unclassified credential errors turn infrastructure outages into silent 503s. Both new account-selection loops catch every error from the credential dependency and continue to the next account. Neither one distinguishes a routing-skip condition (
Extract the skip predicate into one shared helper in the CodeRouter service directory, then call it from all three loops. That keeps the entrypoints from diverging again. As per coding guidelines: "When behavior has multiple entrypoints, implement one shared action path and verify every entrypoint rather than duplicating logic." 📍 Affects 2 files
🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| if (credential.provider !== "codex") continue; | ||
| const upstreamUrl = new URL(CODEX_MODELS_UPSTREAM); | ||
| upstreamUrl.search = new URL(request.url).search; | ||
| upstream = await dependencies.providerRead(() => fetch(upstreamUrl, { | ||
| headers: { | ||
| authorization: `Bearer ${credential.accessToken}`, | ||
| "chatgpt-account-id": credential.accountId, | ||
| originator: "codex_cli_rs", | ||
| "user-agent": request.headers.get("user-agent") ?? "coderouter", | ||
| }, | ||
| cache: "no-store", | ||
| })); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| if (upstream.status === 429) { | ||
| reportCoderouterFailure("provider_rate_limit", new Error("rate limited"), { | ||
| provider: "codex", | ||
| status: 429, | ||
| }); | ||
| await dependencies.cooldown(account.id, rateLimitDelay(upstream.headers)); | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
| if (credential.provider !== "codex") continue; | ||
| const upstreamUrl = new URL(CODEX_MODELS_UPSTREAM); | ||
| upstreamUrl.search = new URL(request.url).search; | ||
| upstream = await fetch(upstreamUrl, { | ||
| if (!upstream) return jsonError("no_usable_account", 503); | ||
| return new Response(upstream.body, { | ||
| status: upstream.status, | ||
| headers: { | ||
| authorization: `Bearer ${credential.accessToken}`, | ||
| "chatgpt-account-id": credential.accountId, | ||
| originator: "codex_cli_rs", | ||
| "user-agent": request.headers.get("user-agent") ?? "coderouter", | ||
| "cache-control": "no-store", | ||
| "content-type": upstream.headers.get("content-type") ?? "application/json", | ||
| }, | ||
| cache: "no-store", | ||
| }); | ||
| if (upstream.status === 429) { | ||
| await markAccountCooldown(account.id, rateLimitDelay(upstream.headers)); | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
| if (!upstream) return jsonError("no_usable_account", 503); | ||
| return new Response(upstream.body, { | ||
| status: upstream.status, | ||
| headers: { | ||
| "cache-control": "no-store", | ||
| "content-type": upstream.headers.get("content-type") ?? "application/json", | ||
| }, | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| export const proxyCodexModels = createCodexModelsProxy({ | ||
| authenticate: authenticateRouteToken, | ||
| select: selectAccountForRequest, | ||
| credential: freshCredential, | ||
| cooldown: markAccountCooldown, | ||
| providerRead: fetchProviderRead, | ||
| }); | ||
|
|
||
| async function sendCodex( | ||
| request: Request, | ||
| forwardedHeaders: Headers, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Do not report cached refresh time as current request time.
accountsWithUsagecan return a cached result whoserdsMsandproviderMswere captured during an earlier refresh. These lines emit those values in the currentServer-Timingresponse, whiletotalmeasures only the current request. A cache hit can therefore report RDS or provider time greater than total time.Return cache-hit metadata separately. Emit refresh timings only when this request performed the refresh. Emit a cache metric for cache hits.
🤖 Prompt for AI Agents