@devries-cameron20 / t3code-1 / commits / 8c80003d68
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(server): Codex skills for four workspaces load in a median 138 ms instead of 1,657 ms (12x) (#33972)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Andrei committed Nov 15, 2026 at 16:12 UTC 8c80003d68b0d94cedb898210c09823b77533d6d
3 files changed +192 -7
apps/server/src/provider/CodexProvider.test.ts
+138
@@ -1,12 +1,150 @@
1 import { assert, it } from "@effect/vitest";
2 +import * as Cause from "effect/Cause";
3 +import * as Deferred from "effect/Deferred";
4 +import * as Effect from "effect/Effect";
5 +import * as Exit from "effect/Exit";
6 +import * as Queue from "effect/Queue";
7 +import * as Scope from "effect/Scope";
8 +import * as Sink from "effect/Sink";
9 +import * as Stream from "effect/Stream";
10 +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11 +import * as TestClock from "effect/testing/TestClock";
12 +import * as HostProcess from "@t3tools/shared/HostProcess";
13
14 import {
15 applyPreferredCodexDefaultModel,
16 codexCustomEndpoint,
17 codexRoutesSpendNativeQuota,
18 + makeCodexSkillsProbe,
19 mapCodexModelCapabilities,
20 } from "./CodexProvider.ts";
21
22 +function makeFakeCodexAppServers() {
23 + const helpers: Array<{
24 + readonly codexHome: unknown;
25 + readonly crash: Effect.Effect<void>;
26 + readonly requests: Array<unknown>;
27 + closed: boolean;
28 + }> = [];
29 + const spawner = ChildProcessSpawner.make((command) =>
30 + Effect.gen(function* () {
31 + const output = yield* Queue.unbounded<Uint8Array, Cause.Done>();
32 + const exited = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
33 + const crash = Deferred.succeed(exited, ChildProcessSpawner.ExitCode(1)).pipe(
34 + Effect.andThen(Queue.end(output)),
35 + Effect.asVoid,
36 + );
37 + const helper = {
38 + codexHome: command._tag === "StandardCommand" ? command.options.env?.CODEX_HOME : undefined,
39 + crash,
40 + requests: [] as Array<unknown>,
41 + closed: false,
42 + };
43 + helpers.push(helper);
44 + yield* Effect.addFinalizer(() => Effect.sync(() => (helper.closed = true)));
45 + const respond = (id: unknown, result: unknown) =>
46 + Queue.offer(output, new TextEncoder().encode(`${JSON.stringify({ id, result })}\n`));
47 + return ChildProcessSpawner.makeHandle({
48 + pid: ChildProcessSpawner.ProcessId(900_000_000 + helpers.length),
49 + exitCode: Deferred.await(exited),
50 + isRunning: Effect.map(Deferred.isDone(exited), (done) => !done),
51 + kill: () => crash,
52 + unref: Effect.succeed(Effect.void),
53 + stdin: Sink.forEach((chunk: string | Uint8Array) =>
54 + Effect.forEach(
55 + (typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk))
56 + .split("\n")
57 + .filter(Boolean),
58 + (line) => {
59 + const frame = JSON.parse(line) as {
60 + readonly id?: number;
61 + readonly method: string;
62 + readonly params?: { readonly cwds: ReadonlyArray<string> };
63 + };
64 + if (frame.id === undefined) return Effect.void;
65 + if (frame.method === "initialize") {
66 + return respond(frame.id, {
67 + codexHome: "/codex-home",
68 + platformFamily: "unix",
69 + platformOs: "linux",
70 + userAgent: "codex/0.0.0",
71 + });
72 + }
73 + helper.requests.push(frame.params);
74 + return respond(frame.id, {
75 + data: (frame.params?.cwds ?? []).map((cwd) => ({
76 + cwd,
77 + errors: [],
78 + skills: [
79 + {
80 + name: `skill-in-${cwd}`,
81 + path: `${cwd}/SKILL.md`,
82 + enabled: true,
83 + description: "",
84 + scope: "repo",
85 + },
86 + ],
87 + })),
88 + });
89 + },
90 + { discard: true },
91 + ),
92 + ),
93 + stdout: Stream.fromQueue(output),
94 + stderr: Stream.empty,
95 + all: Stream.empty,
96 + getInputFd: () => Sink.drain,
97 + getOutputFd: () => Stream.empty,
98 + });
99 + }),
100 + );
101 + return { helpers, spawner };
102 +}
103 +
104 +it.effect("reuses one Codex skills helper per launch until it idles out", () =>
105 + Effect.gen(function* () {
106 + const { helpers, spawner } = makeFakeCodexAppServers();
107 + const providerScope = yield* Scope.make();
108 + const probe = yield* makeCodexSkillsProbe().pipe(
109 + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
110 + Scope.provide(providerScope),
111 + );
112 + const read = (cwd: string, homePath = "/home-a") =>
113 + probe({ binaryPath: "codex", homePath, cwd, environment: {} }).pipe(
114 + Effect.map((skills) => skills.map((skill) => skill.name)),
115 + );
116 +
117 + assert.deepStrictEqual(yield* read("/alpha"), ["skill-in-/alpha"]);
118 + assert.deepStrictEqual(yield* read("/beta"), ["skill-in-/beta"]);
119 + assert.strictEqual(helpers.length, 1);
120 + assert.deepStrictEqual(helpers[0]!.requests, [
121 + { cwds: ["/alpha"], forceReload: true },
122 + { cwds: ["/beta"], forceReload: true },
123 + ]);
124 +
125 + // Another home is another launch identity; the previous helper retires at once.
126 + assert.deepStrictEqual(yield* read("/alpha", "/home-b"), ["skill-in-/alpha"]);
127 + assert.strictEqual(helpers.length, 2);
128 + assert.strictEqual(helpers[0]!.closed, true);
129 + assert.strictEqual(helpers[1]!.codexHome, "/home-b");
130 +
131 + // A helper that exits is replaced within the same read.
132 + yield* helpers[1]!.crash;
133 + assert.deepStrictEqual(yield* read("/beta", "/home-b"), ["skill-in-/beta"]);
134 + assert.strictEqual(helpers.length, 3);
135 +
136 + yield* TestClock.adjust("29 seconds");
137 + assert.strictEqual(helpers[2]!.closed, false);
138 + yield* TestClock.adjust("1 second");
139 + assert.strictEqual(helpers[2]!.closed, true);
140 + assert.deepStrictEqual(yield* read("/alpha", "/home-b"), ["skill-in-/alpha"]);
141 + assert.strictEqual(helpers.length, 4);
142 +
143 + yield* Scope.close(providerScope, Exit.void);
144 + assert.strictEqual(helpers[3]!.closed, true);
145 + }).pipe(Effect.provideService(HostProcess.Platform, "linux")),
146 +);
147 +
148 it("maps current Codex model capability fields", () => {
149 const capabilities = mapCodexModelCapabilities({
150 additionalSpeedTiers: [],
apps/server/src/provider/CodexProvider.ts
+48
@@ -3,10 +3,12 @@ import { pipe } from "effect/Function";
3 import * as DateTime from "effect/DateTime";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 +import * as Equal from "effect/Equal";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Layer from "effect/Layer";
9 import * as Option from "effect/Option";
10 import * as Path from "effect/Path";
11 +import * as RcMap from "effect/RcMap";
12 import * as Result from "effect/Result";
13 import * as Schema from "effect/Schema";
14 import * as Schedule from "effect/Schedule";
@@ -839,6 +841,52 @@ export const probeCodexSkillsForCwd = Effect.fn("probeCodexSkillsForCwd")(functi
841 return parseCodexSkillsListResponse(skillsResponse, input.cwd);
842 });
843
844 +// Long enough for a burst of workspace switches; short because the helper holds the user's
845 +// binary open, and Windows refuses to overwrite or delete a running executable.
846 +const CODEX_SKILLS_HELPER_IDLE_TTL = "30 seconds";
847 +
848 +/** Reuses one app-server per launch identity; each read passes its cwd and rescans from disk. */
849 +export const makeCodexSkillsProbe = Effect.fn("makeCodexSkillsProbe")(function* () {
850 + const helpers = yield* RcMap.make({
851 + lookup: (launch: {
852 + readonly binaryPath: string;
853 + readonly homePath?: string;
854 + readonly launchArgs?: string;
855 + readonly environment: NodeJS.ProcessEnv;
856 + }) =>
857 + withCodexAppServerClient({ ...launch, cwd: process.cwd() }).pipe(
858 + Effect.map(({ client }) => client),
859 + ),
860 + idleTimeToLive: CODEX_SKILLS_HELPER_IDLE_TTL,
861 + });
862 + return Effect.fn("CodexSkillsProbe.probe")(function* (input: {
863 + readonly binaryPath: string;
864 + readonly homePath?: string;
865 + readonly launchArgs?: string;
866 + readonly cwd: string;
867 + readonly environment?: NodeJS.ProcessEnv;
868 + }) {
869 + const { cwd, ...rest } = input;
870 + const launch = {
871 + ...rest,
872 + environment: providerProbeEnvironment(rest.environment ?? process.env),
873 + };
874 + // Retire helpers for a previous binary, home, credentials, or launch options.
875 + const stale = Array.from(yield* RcMap.keys(helpers)).filter(
876 + (key) => !Equal.equals(key, launch),
877 + );
878 + yield* Effect.forEach(stale, (key) => RcMap.invalidate(helpers, key), { discard: true });
879 + const skillsResponse = yield* RcMap.get(helpers, launch).pipe(
880 + Effect.flatMap((client) => client.request("skills/list", { cwds: [cwd], forceReload: true })),
881 + Effect.scoped,
882 + // RcMap keeps a failed spawn, and a dead helper keeps failing; drop it so the retry respawns.
883 + Effect.onError(() => RcMap.invalidate(helpers, launch)),
884 + Effect.retry({ times: 1 }),
885 + );
886 + return parseCodexSkillsListResponse(skillsResponse, cwd);
887 + });
888 +});
889 +
890 const emptyCodexModelsFromSettings = (codexSettings: CodexSettings): ServerProvider["models"] =>
891 appendCustomCodexModels([], codexSettings.customModels);
892
apps/server/src/provider/Drivers/CodexDriver.ts
+6 -7
@@ -29,7 +29,7 @@ import * as ResetCreditCoordinator from "../resetCreditCoordinator.ts";
29 import {
30 checkCodexProviderStatus,
31 makePendingCodexProvider,
32 - probeCodexSkillsForCwd,
32 + makeCodexSkillsProbe,
33 withCodexAppServerClient,
34 } from "../CodexProvider.ts";
35 import { resolveCodexLaunchArgs } from "../codexLaunchArgs.ts";
@@ -321,22 +321,21 @@ export const CodexDriver: ProviderDriver<
321 processEnv,
322 snapshot.getSnapshot.pipe(Effect.map((value) => value.models)),
323 );
324 + const probeSkills = yield* makeCodexSkillsProbe().pipe(
325 + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
326 + );
327 const snapshotForCwd = (cwd: string) =>
328 !effectiveConfig.enabled
329 ? snapshot.getSnapshot
330 : Effect.all([
331 snapshot.getSnapshot,
329 - probeCodexSkillsForCwd({
332 + probeSkills({
333 binaryPath: effectiveConfig.binaryPath,
334 homePath: effectiveConfig.homePath,
335 launchArgs: resolveCodexLaunchArgs(effectiveConfig.launchArgs, processEnv),
336 cwd,
337 environment: processEnv,
335 - }).pipe(
336 - Effect.scoped,
337 - Effect.timeout(resolveProviderProbeTimeoutMs("codex", 20_000, processEnv)),
338 - Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
339 - ),
338 + }).pipe(Effect.timeout(resolveProviderProbeTimeoutMs("codex", 20_000, processEnv))),
339 ]).pipe(
340 Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills })),
341 Effect.mapError(