@devries-cameron20 / t3code Mirror · read-only / commits / ca29ff1d72
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 13:30 UTC

fix(cli): keep log warnings out of JSON output (#30898)

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

Lakshmi Tanmay committed Nov 15, 2026 at 05:56 UTC ca29ff1d72a72aff626ec8aac6aee5eeeb6e5178
2 files changed +127 -3
apps/server/src/binCli.test.ts new
+122
@@ -0,0 +1,122 @@
1 +// @effect-diagnostics nodeBuiltinImport:off -- Exercises the CLI's real stdout and stderr through a subprocess and an HTTP fixture.
2 +import * as NodeHttp from "node:http";
3 +import * as NodeURL from "node:url";
4 +
5 +import * as NodeServices from "@effect/platform-node/NodeServices";
6 +import { assert, it } from "@effect/vitest";
7 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
8 +import * as Effect from "effect/Effect";
9 +import * as FileSystem from "effect/FileSystem";
10 +import * as Stream from "effect/Stream";
11 +import { ChildProcess, ChildProcessSpawner } from "effect/process";
12 +import * as SqlClient from "effect/sql/SqlClient";
13 +
14 +import { migrationManifest, runMigrations } from "./persistence/Migrations.ts";
15 +import {
16 + makePersistedServerRuntimeState,
17 + persistServerRuntimeState,
18 +} from "./serverRuntimeState.ts";
19 +
20 +it.live("keeps migration warnings out of JSON thread output", () =>
21 + Effect.gen(function* () {
22 + const fs = yield* FileSystem.FileSystem;
23 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-json-" });
24 + const stateDir = `${root}/userdata`;
25 + yield* fs.makeDirectory(stateDir);
26 + yield* Effect.gen(function* () {
27 + const sql = yield* SqlClient.SqlClient;
28 + yield* runMigrations();
29 + const futureId = Math.max(...migrationManifest.map(([id]) => id)) + 1;
30 + yield* sql`
31 + INSERT INTO effect_sql_migrations (migration_id, name)
32 + VALUES (${futureId}, 'FutureMigration')
33 + `;
34 + }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: `${stateDir}/statev2.sqlite` })));
35 +
36 + const server = yield* Effect.acquireRelease(
37 + Effect.promise(async () => {
38 + const server = NodeHttp.createServer(async (request, response) => {
39 + let body = "";
40 + for await (const chunk of request) body += String(chunk);
41 + if (request.method === "DELETE") {
42 + response.writeHead(204).end();
43 + return;
44 + }
45 + const message = JSON.parse(body) as {
46 + id?: string;
47 + method: string;
48 + params?: { name: string };
49 + };
50 + if (message.id === undefined) {
51 + response.writeHead(202).end();
52 + return;
53 + }
54 + const result =
55 + message.method === "initialize"
56 + ? { protocolVersion: "2025-06-18" }
57 + : {
58 + structuredContent:
59 + message.params?.name === "t3_project_list"
60 + ? {
61 + projects: [{ id: "project-1", title: "Test", workspaceRoot: root }],
62 + nextCursor: null,
63 + }
64 + : {
65 + projectId: "project-1",
66 + currentThreadId: null,
67 + threads: [],
68 + nextCursor: null,
69 + total: 0,
70 + },
71 + };
72 + response.writeHead(200, {
73 + "content-type": "application/json",
74 + "mcp-session-id": "cli-json-test",
75 + });
76 + response.end(JSON.stringify({ jsonrpc: "2.0", id: message.id, result }));
77 + });
78 + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
79 + return server;
80 + }),
81 + (server) =>
82 + Effect.promise(() => new Promise<void>((resolve) => server.close(() => resolve()))),
83 + );
84 + const address = server.address();
85 + assert(address !== null && typeof address !== "string");
86 + yield* persistServerRuntimeState({
87 + path: `${stateDir}/server-runtime.json`,
88 + state: yield* makePersistedServerRuntimeState({
89 + config: { host: "127.0.0.1", devUrl: undefined },
90 + port: address.port,
91 + }),
92 + });
93 +
94 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
95 + const child = yield* spawner.spawn(
96 + ChildProcess.make(
97 + process.execPath,
98 + [
99 + NodeURL.fileURLToPath(new URL("./bin.ts", import.meta.url)),
100 + "thread",
101 + "list",
102 + "--project",
103 + "project-1",
104 + "--json",
105 + "--base-dir",
106 + root,
107 + ],
108 + { env: { T3CODE_HOME: root } },
109 + ),
110 + );
111 + const { stdout, stderr, exitCode } = yield* Effect.all(
112 + {
113 + stdout: child.stdout.pipe(Stream.decodeText(), Stream.mkString),
114 + stderr: child.stderr.pipe(Stream.decodeText(), Stream.mkString),
115 + exitCode: child.exitCode,
116 + },
117 + { concurrency: "unbounded" },
118 + );
119 + assert.strictEqual(Number(exitCode), 0, stderr);
120 + assert.deepStrictEqual(JSON.parse(stdout), []);
121 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
122 +);
apps/server/src/cli/thread.ts
+5 -3
@@ -216,11 +216,12 @@ export type ThreadTools = ReturnType<typeof makeThreadTools>;
216 * outside agent does, and revokes that session when the command ends.
217 */
218 const withThreadTools = Effect.fn("cli.thread.withThreadTools")(function* <A, E, R>(
219 - flags: { readonly baseDir: Option.Option<string> },
219 + flags: { readonly baseDir: Option.Option<string>; readonly json: boolean },
220 run: (tools: ThreadTools) => Effect.Effect<A, E, R>,
221 ) {
222 const logLevel = yield* GlobalFlag.LogLevel;
223 - const config = yield* resolveCliAuthConfig(flags, logLevel);
223 + const config = yield* resolveCliAuthConfig(flags, logLevel, { quietLogs: flags.json });
224 + const minimumLogLevel = flags.json ? "Error" : config.logLevel;
225 return yield* Effect.gen(function* () {
226 const runtimeState = yield* readLiveServerRuntimeState(config.serverRuntimeStatePath);
227 if (Option.isNone(runtimeState)) {
@@ -248,10 +249,11 @@ const withThreadTools = Effect.fn("cli.thread.withThreadTools")(function* <A, E,
249 return yield* run(makeThreadTools(session, origin));
250 }).pipe(
251 Effect.scoped,
252 + Effect.provideService(References.MinimumLogLevel, minimumLogLevel),
253 Effect.provide(
254 EnvironmentAuth.layerRuntime.pipe(
255 Layer.provide(ServerConfig.layer(config)),
254 - Layer.provide(Layer.succeed(References.MinimumLogLevel, config.logLevel)),
256 + Layer.provide(Layer.succeed(References.MinimumLogLevel, minimumLogLevel)),
257 ),
258 ),
259 );