Repository navigation
fix(server): managed Codex 401s say why no ChatGPT token was given #41911
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
Open
matthewcosier
wants to merge
7
commits into
pingdotgg:main
Choose a base branch
from
matthewcosier:fix/codex-managed-login-reason
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,146
−142
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc332f1
fix(server): managed Codex 401s say why no ChatGPT token was given
matthewcosier 0c5abe3
fix(server): managed Codex 401s keep the right session's reason
matthewcosier 58dd73c
fix(server): keep managed Codex refusal reasons accurate across recov…
matthewcosier e6b319f
Merge remote-tracking branch 'origin/main' into fix/codex-managed-log…
juliusmarminge d39833b
test(server): keep the managed token refusal tests that catch a regre…
juliusmarminge 3166e9d
fix(server): tighten managed Codex refusal matching, broker deadline,…
matthewcosier 89e5868
fix(server): a later managed Codex 401 shows the session's newest ref…
matthewcosier 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // @effect-diagnostics nodeBuiltinImport:off - A fake broker listens on the helper's private socket. | ||
| import * as NodeCrypto from "node:crypto"; | ||
| import * as NodeFSP from "node:fs/promises"; | ||
| import * as NodeNet from "node:net"; | ||
| import * as NodeOS from "node:os"; | ||
| import * as NodePath from "node:path"; | ||
| import { afterEach, describe, expect, it } from "@effect/vitest"; | ||
| import * as HostProcess from "@t3tools/shared/HostProcess"; | ||
| import * as Effect from "effect/Effect"; | ||
| import { vi } from "vite-plus/test"; | ||
| import { runManagedChatGptTokenCli } from "./managedChatGptToken.ts"; | ||
|
|
||
| const profileKey = "a".repeat(64); | ||
| const sessionKey = "b".repeat(32); | ||
| const refusal = { kind: "refresh", message: "Your ChatGPT connection expired. Sign in again." }; | ||
|
|
||
| /** Runs the helper against a fake broker that sends `answer`, or closes when it is null. */ | ||
| const runAgainst = (answer: string | null) => | ||
| Effect.gen(function* () { | ||
| // The helper connects to a real local socket, so this follows the host like the broker does. | ||
| const platform = yield* HostProcess.Platform; | ||
| const directory = yield* Effect.acquireRelease( | ||
| Effect.promise(() => NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-token-cli-"))), | ||
| (directory) => Effect.promise(() => NodeFSP.rm(directory, { recursive: true, force: true })), | ||
| ); | ||
| const endpoint = | ||
| platform === "win32" | ||
| ? `\\\\.\\pipe\\t3-token-cli-${NodeCrypto.randomUUID()}` | ||
| : NodePath.join(directory, "s"); | ||
| yield* Effect.acquireRelease( | ||
| Effect.promise(async () => { | ||
| const server = NodeNet.createServer((socket) => { | ||
| socket.once("data", () => | ||
| answer === null ? socket.destroy() : socket.end(`${answer}\n`), | ||
| ); | ||
| }); | ||
| await new Promise<void>((resolve) => server.listen(endpoint, resolve)); | ||
| return server; | ||
| }), | ||
| (server) => | ||
| Effect.promise(() => new Promise<void>((resolve) => server.close(() => resolve()))), | ||
| ); | ||
| const stdout: string[] = []; | ||
| const stderr: string[] = []; | ||
| yield* Effect.acquireRelease( | ||
| Effect.sync(() => { | ||
| vi.spyOn(process.stdout, "write").mockImplementation((chunk) => { | ||
| stdout.push(String(chunk)); | ||
| return true; | ||
| }); | ||
| vi.spyOn(process.stderr, "write").mockImplementation((chunk) => { | ||
| stderr.push(String(chunk)); | ||
| return true; | ||
| }); | ||
| }), | ||
| () => Effect.sync(() => vi.restoreAllMocks()), | ||
| ); | ||
| yield* Effect.promise(() => | ||
| runManagedChatGptTokenCli([ | ||
| endpoint, | ||
| "codex-managed", | ||
| "environment-1", | ||
| profileKey, | ||
| sessionKey, | ||
| ]), | ||
| ); | ||
| return { stdout: stdout.join(""), stderr: stderr.join(""), exitCode: process.exitCode }; | ||
| }).pipe(Effect.scoped); | ||
|
|
||
| describe("managed ChatGPT token helper", () => { | ||
| const exitCode = process.exitCode; | ||
| afterEach(() => { | ||
| process.exitCode = exitCode; | ||
| }); | ||
|
|
||
| it.effect("prints a broker's refusal and gives Codex no token", () => | ||
| Effect.gen(function* () { | ||
| const result = yield* runAgainst(JSON.stringify({ refusal })); | ||
| expect(result).toEqual({ stdout: "", stderr: `${refusal.message}\n`, exitCode: 1 }); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("hands Codex the broker's token on stdout", () => | ||
| Effect.gen(function* () { | ||
| const result = yield* runAgainst(JSON.stringify({ token: "access-token" })); | ||
| expect(result).toEqual({ stdout: "access-token", stderr: "", exitCode }); | ||
| }), | ||
| ); | ||
| }); | ||
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
Oops, something went wrong.
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.
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.
Handle test listener startup failures.
If the temporary filesystem rejects Unix socket binds,
server.listen()emitserrorinstead of calling the listening callback. The fixture has no error listener, so the failure escapes the test effect and leaves the startup promise pending. Register a startup error handler before callinglistenand use it to reject the acquisition. This lets the test report the bind error and clean up the temporary directory. (nodejs.org)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.
Leaving this one as is: the fixture only listens on a pipe inside a fresh temporary directory, so there is no shared name for the bind to collide with.