Repository navigation
fix(codex): refresh managed credentials during active turns #17920
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
Closed
luke2x
wants to merge
9
commits into
pingdotgg:main
from
luke2x:fix/codex-active-token-refresh-17445
+487
−32
Closed
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
65347e0
fix(codex): renew managed credentials in active processes
luke2x cf9c097
Merge branch 'main' into fix/codex-active-token-refresh-17445
luke2x fd8db67
fix(codex): renew managed credentials through a T3 subcommand
luke2x da059ac
Update CodexManagedTokenSource.ts
luke2x 1c2f341
fix(server): restore managed token bridge error handler syntax
luke2x 1025e41
fix(codex): bind refreshed credentials to launch accounts
luke2x 7c89726
fix(codex): retire replaced managed credential bridges
luke2x 26436da
fix(codex): preserve active bridges after failed sign-in
luke2x 51e5042
Merge branch 'main' into fix/codex-active-token-refresh-17445
luke2x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // @effect-diagnostics nodeBuiltinImport:off - scope-owned loopback bridge for Codex's credential command. | ||
| import * as NodeCrypto from "node:crypto"; | ||
| import * as NodeHttp from "node:http"; | ||
| import { ProviderSetupError, type ProviderInstanceId } from "@t3tools/contracts"; | ||
| import * as Effect from "effect/Effect"; | ||
|
|
||
| // Codex invokes this command again when its cached bearer expires or receives a 401. | ||
| // The child receives a bridge credential, never a fixed copy of the provider token. | ||
| export const managedCodexTokenCommand = ` | ||
| fetch(process.env.T3CODE_MANAGED_CODEX_AUTH_URL, { | ||
| headers: { Authorization: "Bearer " + process.env.T3CODE_MANAGED_CODEX_AUTH_SECRET, | ||
| "X-T3-Codex-Account": process.env.T3CODE_MANAGED_CODEX_AUTH_ACCOUNT }, | ||
| }).then(async response => { | ||
| if (!response.ok) throw new Error(); | ||
| process.stdout.write(await response.text()); | ||
| }).catch(() => { | ||
| process.stderr.write("Could not renew managed Codex credentials.\\n"); | ||
| process.exitCode = 1; | ||
| }); | ||
| `; | ||
|
|
||
| export const makeCodexManagedTokenSource = Effect.fn("makeCodexManagedTokenSource")(function* ( | ||
| instanceId: ProviderInstanceId, | ||
| access: Effect.Effect< | ||
| { readonly accessToken: string; readonly clientId: string }, | ||
| ProviderSetupError | ||
| >, | ||
| ) { | ||
| const context = yield* Effect.context<never>(); | ||
| const runPromise = Effect.runPromiseWith(context); | ||
| const secret = NodeCrypto.randomBytes(32).toString("base64url"); | ||
| const server = yield* Effect.acquireRelease( | ||
| Effect.tryPromise({ | ||
| try: () => | ||
| new Promise<NodeHttp.Server>((resolve, reject) => { | ||
| const server = NodeHttp.createServer((request, response) => { | ||
| if (request.method !== "GET" || request.url !== "/token") { | ||
| response.writeHead(404).end(); | ||
| return; | ||
| } | ||
| if (request.headers.authorization !== `Bearer ${secret}`) { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| response.writeHead(401).end(); | ||
| return; | ||
| } | ||
| const abort = new AbortController(); | ||
| response.once("close", () => abort.abort()); | ||
| void runPromise(access, { signal: abort.signal }).then( | ||
| (credentials) => { | ||
| if (response.destroyed) return; | ||
| if (request.headers["x-t3-codex-account"] !== credentials.clientId) { | ||
| response.writeHead(409).end(); | ||
| return; | ||
| } | ||
| if (!response.destroyed) | ||
| response | ||
| .writeHead(200, { | ||
| "content-type": "text/plain", | ||
| "cache-control": "no-store", | ||
| }) | ||
| .end(credentials.accessToken); | ||
| }, | ||
| () => { | ||
| if (!response.destroyed) response.writeHead(503).end(); | ||
| }, | ||
| ); | ||
| }); | ||
| server.once("error", reject); | ||
| server.listen(0, "127.0.0.1", () => resolve(server)); | ||
| }), | ||
| catch: () => | ||
| new ProviderSetupError({ | ||
| instanceId, | ||
| operation: "runtime", | ||
| detail: "Could not start the managed Codex credential bridge.", | ||
| }), | ||
| }), | ||
|
macroscopeapp[bot] marked this conversation as resolved.
Resolved
|
||
| (server) => | ||
| Effect.promise( | ||
| () => | ||
| new Promise<void>((resolve) => { | ||
| server.closeAllConnections(); | ||
| server.close(() => resolve()); | ||
| }), | ||
| ), | ||
| ); | ||
| const address = server.address(); | ||
| if (address === null || typeof address === "string") | ||
| return yield* new ProviderSetupError({ | ||
| instanceId, | ||
| operation: "runtime", | ||
| detail: "Could not resolve the managed Codex credential bridge.", | ||
| }); | ||
| return { url: `http://127.0.0.1:${address.port}/token`, secret }; | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.