Skip to content

Commit 8610b31

Browse files
fix(server): Codex usage answering after three seconds is no longer dropped (#36189)
1 parent 095611d commit 8610b31

2 files changed

Lines changed: 82 additions & 6 deletions

File tree

‎apps/server/src/provider/CodexProvider.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ import {
5757
import * as HostProcess from "@t3tools/shared/HostProcess";
5858
import packageJson from "../../package.json" with { type: "json" };
5959
const isCodexAppServerSpawnError = Schema.is(CodexErrors.CodexAppServerSpawnError);
60-
const RATE_LIMITS_PROBE_TIMEOUT_MS = 3_000;
6160

6261
type CodexRateLimitsProbe =
6362
| {
@@ -670,11 +669,7 @@ const readCodexRateLimits = (
670669
client: CodexClient.CodexAppServerClient["Service"],
671670
deadlineMs: number,
672671
) =>
673-
withCodexDiscoveryDeadline(
674-
client.request("account/rateLimits/read", null),
675-
deadlineMs,
676-
RATE_LIMITS_PROBE_TIMEOUT_MS,
677-
).pipe(
672+
withCodexDiscoveryDeadline(client.request("account/rateLimits/read", null), deadlineMs).pipe(
678673
Effect.map((response): CodexRateLimitsProbe =>
679674
response
680675
? {

‎apps/server/src/provider/ProviderRegistry.test.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as FileSystem from "effect/FileSystem";
1717
import * as Layer from "effect/Layer";
1818
import * as Path from "effect/Path";
1919
import * as PubSub from "effect/PubSub";
20+
import * as Queue from "effect/Queue";
2021
import * as Ref from "effect/Ref";
2122
import * as Schema from "effect/Schema";
2223
import * as Scope from "effect/Scope";
@@ -788,6 +789,86 @@ it.layer(
788789
}),
789790
);
790791

792+
it.effect("shows Codex usage that answers after three seconds inside the probe budget", () =>
793+
Effect.gen(function* () {
794+
const usageRequested = yield* Deferred.make<void>();
795+
const spawner = ChildProcessSpawner.make(() =>
796+
Effect.gen(function* () {
797+
const scope = yield* Effect.scope;
798+
const output = yield* Queue.unbounded<Uint8Array>();
799+
const send = (message: unknown) =>
800+
Queue.offer(output, encoder.encode(`${JSON.stringify(message)}\n`));
801+
const results: Record<string, unknown> = {
802+
initialize: {
803+
userAgent: "codex_cli_rs/0.167.0",
804+
codexHome: "/nonexistent-codex-home",
805+
platformFamily: "unix",
806+
platformOs: "linux",
807+
},
808+
"account/read": {
809+
account: { type: "chatgpt", email: "[email protected]", planType: "pro" },
810+
requiresOpenaiAuth: true,
811+
},
812+
"config/read": { config: {}, origins: {} },
813+
"skills/list": { data: [] },
814+
"model/list": { data: [] },
815+
};
816+
const answer = (line: string) => {
817+
const { id, method } = JSON.parse(line) as { id?: number; method?: string };
818+
if (id === undefined || method === undefined) return Effect.void;
819+
if (method !== "account/rateLimits/read") {
820+
return send({ id, result: results[method] ?? {} });
821+
}
822+
return Deferred.succeed(usageRequested, undefined).pipe(
823+
Effect.andThen(Effect.sleep("4 seconds")),
824+
Effect.andThen(
825+
send({
826+
id,
827+
result: {
828+
rateLimits: {
829+
limitId: "codex",
830+
planType: "pro",
831+
primary: { usedPercent: 42, windowDurationMins: 300, resetsAt: 2e9 },
832+
},
833+
rateLimitsByLimitId: null,
834+
rateLimitResetCredits: null,
835+
accountId: null,
836+
},
837+
}),
838+
),
839+
Effect.forkIn(scope),
840+
);
841+
};
842+
const decoder = new TextDecoder();
843+
let buffered = "";
844+
return ChildProcessSpawner.makeHandle({
845+
...mockHandle({ stdout: "", stderr: "", code: 0 }),
846+
exitCode: Effect.never,
847+
isRunning: Effect.succeed(true),
848+
stdin: Sink.forEach((chunk: Uint8Array) => {
849+
buffered += decoder.decode(chunk, { stream: true });
850+
const lines = buffered.split("\n");
851+
buffered = lines.pop() ?? "";
852+
return Effect.forEach(lines.filter(Boolean), answer, { discard: true });
853+
}),
854+
stdout: Stream.fromQueue(output),
855+
});
856+
}),
857+
);
858+
const fiber = yield* checkCodexProviderStatus(defaultCodexSettings).pipe(
859+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
860+
Effect.forkChild,
861+
);
862+
yield* Deferred.await(usageRequested);
863+
yield* TestClock.adjust("25 seconds");
864+
const status = yield* Fiber.join(fiber);
865+
866+
assert.strictEqual(status.auth.status, "authenticated");
867+
assert.isUndefined(status.usageLimits?.unavailable);
868+
assert.strictEqual(status.usageLimits?.windows[0]?.usedPercent, 42);
869+
}),
870+
);
871+
791872
it.effect("does not run --version when the Codex executable cannot start", () =>
792873
Effect.gen(function* () {
793874
if ((yield* HostProcess.Platform) === "win32") return;

0 commit comments

Comments
 (0)