The best way to control Claude Code, Codex and more from any computer or phone
perf(server): reuse compiled modules for CLI commands (#27433)
Co-authored-by: maria-rcks <maria@kuuro.net>
Lakshmi Tanmay committed
Nov 1, 2026 at 08:08 UTC
6d584fa8150965cda908268e65a393502064f261
3 files changed
+88
apps/server/src/bin.ts
+2
@@ -5,6 +5,7 @@
5
* of evaluation) loads; everything else defers to the real CLI in ./binCli.ts.
6
*/
7
import { isEntrypoint } from "./entrypoint.ts";
8
+import { enableCliCompileCache } from "./cliCompileCache.ts";
9
10
if (
11
isEntrypoint({
@@ -35,6 +36,7 @@ if (
36
const { runAcpMcpCliFastPath } = await import("./mcp/AcpMcpStdioBridge.ts");
37
await runAcpMcpCliFastPath(command, process.argv.slice(3));
38
} else {
39
+ enableCliCompileCache();
40
// Windows desktop launches without stdin; attach before reading, except on fast paths using pipes.
41
const { attachConsoleStdin } = await import("./cli/consoleStdin.ts");
42
attachConsoleStdin();
apps/server/src/cliCompileCache.test.ts
new
+69
@@ -0,0 +1,69 @@
1
+// @effect-diagnostics nodeBuiltinImport:off -- Resolves the real CLI entrypoint for subprocess tests.
2
+import * as NodeURL from "node:url";
3
+import * as NodeServices from "@effect/platform-node/NodeServices";
4
+import { expect, it } from "@effect/vitest";
5
+import * as Effect from "effect/Effect";
6
+import * as FileSystem from "effect/FileSystem";
7
+import * as Stream from "effect/Stream";
8
+import { ChildProcess, ChildProcessSpawner } from "effect/process";
9
+
10
+it.live("enables CLI caching while respecting overrides and unavailable storage", () =>
11
+ Effect.gen(function* () {
12
+ const fs = yield* FileSystem.FileSystem;
13
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
14
+ const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-cache-test-" });
15
+ const report = `import { getCompileCacheDir, registerHooks } from 'node:module'; registerHooks({ resolve(specifier, context, nextResolve) { if (specifier === './binCli.ts') console.error('CACHE=' + (getCompileCacheDir() ?? 'disabled')); return nextResolve(specifier, context); } });`;
16
+ const run = Effect.fn("cliCompileCache.test.run")(function* (env: Record<string, string>) {
17
+ const child = yield* spawner.spawn(
18
+ ChildProcess.make(
19
+ process.execPath,
20
+ [
21
+ "--experimental-strip-types",
22
+ "--import",
23
+ `data:text/javascript,${encodeURIComponent(report)}`,
24
+ NodeURL.fileURLToPath(new URL("./bin.ts", import.meta.url)),
25
+ "--help",
26
+ ],
27
+ {
28
+ env: {
29
+ XDG_CACHE_HOME: `${root}/cache`,
30
+ T3CODE_HOME: `${root}/home`,
31
+ NODE_COMPILE_CACHE: undefined,
32
+ NODE_DISABLE_COMPILE_CACHE: undefined,
33
+ APPIMAGE: "",
34
+ ...env,
35
+ },
36
+ },
37
+ ),
38
+ );
39
+ return yield* Effect.all(
40
+ {
41
+ stdout: child.stdout.pipe(Stream.decodeText(), Stream.mkString),
42
+ stderr: child.stderr.pipe(Stream.decodeText(), Stream.mkString),
43
+ exitCode: child.exitCode.pipe(Effect.map(Number)),
44
+ },
45
+ { concurrency: "unbounded" },
46
+ );
47
+ }, Effect.scoped);
48
+ const normal = yield* run({});
49
+ expect(normal.exitCode).toBe(0);
50
+ expect(normal.stderr.replaceAll("\\", "/")).toContain(
51
+ `CACHE=${root}/cache/t3code/compile-cache`.replaceAll("\\", "/"),
52
+ );
53
+ const appImage = yield* run({ APPIMAGE: `${root}/T3Code.AppImage` });
54
+ expect(appImage.exitCode).toBe(0);
55
+ expect(appImage.stderr).toContain("CACHE=disabled");
56
+ const existing = yield* run({ NODE_COMPILE_CACHE: `${root}/explicit` });
57
+ expect(existing.exitCode).toBe(0);
58
+ expect(existing.stderr.replaceAll("\\", "/")).toContain(
59
+ `${root}/explicit`.replaceAll("\\", "/"),
60
+ );
61
+ const disabled = yield* run({ NODE_DISABLE_COMPILE_CACHE: "1" });
62
+ expect(disabled.exitCode).toBe(0);
63
+ expect(disabled.stderr).toContain("CACHE=disabled");
64
+ yield* fs.writeFileString(`${root}/blocked`, "not a directory");
65
+ const unavailable = yield* run({ XDG_CACHE_HOME: `${root}/blocked` });
66
+ expect(unavailable.exitCode).toBe(0);
67
+ expect(unavailable.stderr).toContain("CACHE=disabled");
68
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
69
+);
apps/server/src/cliCompileCache.ts
new
+17
@@ -0,0 +1,17 @@
1
+// @effect-diagnostics nodeBuiltinImport:off -- Runs before the CLI's Effect runtime loads.
2
+import * as NodeModule from "node:module";
3
+import * as NodeOS from "node:os";
4
+import * as NodePath from "node:path";
5
+import * as NodeSea from "node:sea";
6
+
7
+export function enableCliCompileCache(): void {
8
+ try {
9
+ // AppImage paths change each launch; SEA has no separate modules to cache.
10
+ if (process.env.APPIMAGE || NodeSea.isSea()) return;
11
+ const cacheRoot =
12
+ process.env.XDG_CACHE_HOME ||
13
+ // oxlint-disable-next-line t3code/no-global-process-runtime -- Runs before the CLI's Effect runtime loads.
14
+ (process.platform === "linux" ? NodePath.join(NodeOS.homedir(), ".cache") : NodeOS.tmpdir());
15
+ NodeModule.enableCompileCache(NodePath.join(cacheRoot, "t3code", "compile-cache"));
16
+ } catch {}
17
+}