|
1 | 1 | import { existsSync, readFileSync, statSync } from "node:fs"; |
2 | 2 | import { join } from "node:path"; |
3 | | -import { formatAccountLabel, formatCooldown, formatWaitTime } from "../../accounts.js"; |
| 3 | +import { |
| 4 | + AccountManager, |
| 5 | + formatAccountLabel, |
| 6 | + formatCooldown, |
| 7 | + formatWaitTime, |
| 8 | +} from "../../accounts.js"; |
4 | 9 | import { parseBooleanEnv } from "../../env-parsing.js"; |
5 | 10 | import { getCodexMultiAuthDir } from "../../runtime-paths.js"; |
6 | 11 | import { saveAccountsWithRetry } from "../forecast-report-shared.js"; |
@@ -32,7 +37,10 @@ import { |
32 | 37 | isQuotaCacheEntryExhausted, |
33 | 38 | } from "../../quota-readiness.js"; |
34 | 39 | import type { QuotaCacheData } from "../../quota-cache.js"; |
35 | | -import type { RuntimeObservabilitySnapshot } from "../../runtime/runtime-observability.js"; |
| 40 | +import { |
| 41 | + recordRuntimeReset, |
| 42 | + type RuntimeObservabilitySnapshot, |
| 43 | +} from "../../runtime/runtime-observability.js"; |
36 | 44 | import { |
37 | 45 | appRuntimeHelperStatusToSignal as appRuntimeHelperStatusToRuntimeSignal, |
38 | 46 | resolveAccountCurrentMarkers, |
@@ -88,16 +96,84 @@ function printRotationUsage(logInfo: (message: string) => void): void { |
88 | 96 | " codex-multi-auth rotation bind-app", |
89 | 97 | " codex-multi-auth rotation unbind-app", |
90 | 98 | " codex-multi-auth rotation reset-rate-limits [--all | --account <idx>] [--dry-run] [--json]", |
| 99 | + " codex-multi-auth rotation reset-runtime [--json]", |
91 | 100 | "", |
92 | 101 | "Behavior:", |
93 | 102 | " - Runtime rotation is enabled by default for request-bearing Codex sessions", |
94 | 103 | " - Binds the packaged Codex desktop app to the same localhost router when enabled or repaired", |
95 | 104 | " - Use CODEX_MULTI_AUTH_RUNTIME_ROTATION_PROXY=0 to disable the proxy for the current process without changing persistent settings", |
96 | 105 | " - reset-rate-limits clears stored rateLimitResetTimes and active coolingDownUntil entries; use when `fix --live` confirms quota is available but the proxy still returns 503 pool-exhausted", |
| 106 | + " - reset-runtime clears process-local runtime trackers and re-applies the Codex app bind when available", |
97 | 107 | ].join("\n"), |
98 | 108 | ); |
99 | 109 | } |
100 | 110 |
|
| 111 | +async function runResetRuntime( |
| 112 | + args: string[], |
| 113 | + deps: RotationCommandDeps, |
| 114 | +): Promise<number> { |
| 115 | + const logInfo = deps.logInfo ?? console.log; |
| 116 | + const logError = deps.logError ?? console.error; |
| 117 | + let json = false; |
| 118 | + for (const arg of args) { |
| 119 | + if (arg === "--json" || arg === "-j") { |
| 120 | + json = true; |
| 121 | + continue; |
| 122 | + } |
| 123 | + if (arg === "--help" || arg === "-h" || arg === "help") { |
| 124 | + logInfo("Usage: codex-multi-auth rotation reset-runtime [--json]"); |
| 125 | + return 0; |
| 126 | + } |
| 127 | + logError(`Unknown reset-runtime option: ${arg}`); |
| 128 | + return 1; |
| 129 | + } |
| 130 | + |
| 131 | + AccountManager.resetVolatileRuntimeState(); |
| 132 | + recordRuntimeReset("rotation-reset-runtime"); |
| 133 | + let unbind: AppBindResult | null = null; |
| 134 | + let bind: AppBindResult | null = null; |
| 135 | + let appBindRestarted = false; |
| 136 | + if (deps.unbindCodexApp && deps.bindCodexApp) { |
| 137 | + try { |
| 138 | + unbind = await deps.unbindCodexApp(); |
| 139 | + bind = await deps.bindCodexApp(); |
| 140 | + appBindRestarted = true; |
| 141 | + } catch (error) { |
| 142 | + const message = error instanceof Error ? error.message : String(error); |
| 143 | + if (json) { |
| 144 | + logInfo( |
| 145 | + JSON.stringify({ |
| 146 | + ok: false, |
| 147 | + command: "rotation reset-runtime", |
| 148 | + resetVolatileRuntimeState: true, |
| 149 | + appBindRestarted, |
| 150 | + error: message, |
| 151 | + }), |
| 152 | + ); |
| 153 | + } else { |
| 154 | + logError(`Runtime reset completed, but app bind restart failed: ${message}`); |
| 155 | + } |
| 156 | + return 1; |
| 157 | + } |
| 158 | + } |
| 159 | + const payload = { |
| 160 | + ok: true, |
| 161 | + command: "rotation reset-runtime", |
| 162 | + resetVolatileRuntimeState: true, |
| 163 | + appBindRestarted, |
| 164 | + unbindStatus: unbind?.status.state ?? null, |
| 165 | + bindStatus: bind?.status.state ?? null, |
| 166 | + }; |
| 167 | + if (json) { |
| 168 | + logInfo(JSON.stringify(payload)); |
| 169 | + } else { |
| 170 | + logInfo("Runtime rotation volatile state reset."); |
| 171 | + if (appBindRestarted) logInfo("Codex app bind restarted."); |
| 172 | + else logInfo("Codex app bind helpers unavailable; new wrapper sessions will use the reset state."); |
| 173 | + } |
| 174 | + return 0; |
| 175 | +} |
| 176 | + |
101 | 177 | interface ResetRateLimitsOptions { |
102 | 178 | scope: "all" | "account"; |
103 | 179 | accountIndex: number | null; |
@@ -660,6 +736,9 @@ export async function runRotationCommand( |
660 | 736 | if (subcommand === "reset-rate-limits") { |
661 | 737 | return runResetRateLimits(rest, deps); |
662 | 738 | } |
| 739 | + if (subcommand === "reset-runtime") { |
| 740 | + return runResetRuntime(rest, deps); |
| 741 | + } |
663 | 742 | if (rest.length > 0) { |
664 | 743 | logError(`Unknown rotation option: ${rest[0]}`); |
665 | 744 | return 1; |
|
0 commit comments