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, 07:15 UTC
fix(provider-core): bound status probe output while draining pipes (#37936)
primevishnu committed
Nov 19, 2026 at 11:29 UTC
7da0a166e1d9fa1734b50088a64fc28bea175a8d
4 files changed
+111
-6
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+4
@@ -1359,6 +1359,10 @@ const runClaudeWorker = Effect.fn("runClaudeWorker")(function* (input: {
1359
},
1360
},
1361
),
1362
+ // Complete session histories have no status-probe size limit; stderr stays bounded.
1363
+ input.method === "getSessionMessages"
1364
+ ? { maxStdoutBytes: Number.POSITIVE_INFINITY }
1365
+ : undefined,
1366
).pipe(Effect.timeout("30 seconds"));
1367
if (result.code !== 0) {
1368
return yield* new ClaudeAgentSdkQueryRunnerError({
apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts
+29
@@ -231,6 +231,35 @@ const resumedMcpQueryInput = {
231
},
232
} satisfies ClaudeAgentSdkQueryOpenInput;
233
234
+it.effect("resumes a session whose history exceeds the status-probe output limit", () =>
235
+ Effect.gen(function* () {
236
+ const fileSystem = yield* FileSystem.FileSystem;
237
+ const configDir = yield* fileSystem.makeTempDirectoryScoped({
238
+ prefix: "t3-claude-large-history-",
239
+ });
240
+ yield* writeClaudeSession(configDir, [
241
+ sessionEntry("aaaaaaaa-0000-4000-8000-000000000001", null, {
242
+ role: "user",
243
+ content: "x".repeat(9 * 1024 * 1024),
244
+ }),
245
+ ]);
246
+ const querySpy = vi
247
+ .spyOn(ClaudeSdk, "query")
248
+ .mockImplementation(() => ({ close: vi.fn() }) as unknown as ClaudeSdk.Query);
249
+ yield* Effect.addFinalizer(() => Effect.sync(() => querySpy.mockRestore()));
250
+ const runner = yield* ClaudeAgentSdkQueryRunner;
251
+ const { mcpServers: _mcpServers, ...options } = resumedMcpQueryInput.options;
252
+ const session = yield* runner.open({
253
+ ...resumedMcpQueryInput,
254
+ resume: true,
255
+ options: { ...options, cwd: "/repo", env: { ...process.env, CLAUDE_CONFIG_DIR: configDir } },
256
+ });
257
+ yield* Effect.addFinalizer(() => session.close.pipe(Effect.orDie));
258
+ assert.equal(querySpy.mock.calls.length, 1);
259
+ assert.equal(querySpy.mock.calls[0]?.[0].options?.resume, FORK_SESSION_ID);
260
+ }).pipe(Effect.provide(Layer.merge(realQueryRunner, NodeServices.layer))),
261
+);
262
+
263
it.effect("reopens a stalled MCP registration before delivering the resumed prompt", () =>
264
Effect.gen(function* () {
265
const registrationStarted = Promise.withResolvers<void>();
packages/provider-core/src/server/snapshotProbe.test.ts
+50
@@ -5,6 +5,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
5
import { createModelCapabilities } from "@t3tools/shared/model";
6
import * as Effect from "effect/Effect";
7
import * as Option from "effect/Option";
8
+import * as Result from "effect/Result";
9
import * as Sink from "effect/Sink";
10
import * as Stream from "effect/Stream";
11
import * as ChildProcess from "effect/process/ChildProcess";
@@ -194,6 +195,55 @@ describe("resolveProviderProbeTimeoutMs", () => {
195
});
196
197
describe("spawnAndCollect", () => {
198
+ it.effect.each(["stdout", "stderr"] as const)(
199
+ "rejects oversized %s after draining the process streams",
200
+ (stream) =>
201
+ Effect.gen(function* () {
202
+ let drainedChunks = 0;
203
+ const chunk = new Uint8Array(1024 * 1024).fill(120);
204
+ const output = Stream.fromIterable(Array.from({ length: 9 }, () => chunk)).pipe(
205
+ Stream.tap(() =>
206
+ Effect.sync(() => {
207
+ drainedChunks += 1;
208
+ }),
209
+ ),
210
+ );
211
+ const spawner = ChildProcessSpawner.make(() =>
212
+ Effect.succeed(
213
+ ChildProcessSpawner.makeHandle({
214
+ pid: ChildProcessSpawner.ProcessId(1),
215
+ exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
216
+ isRunning: Effect.succeed(false),
217
+ kill: () => Effect.void,
218
+ unref: Effect.succeed(Effect.void),
219
+ stdin: Sink.drain,
220
+ stdout: stream === "stdout" ? output : Stream.empty,
221
+ stderr: stream === "stderr" ? output : Stream.empty,
222
+ all: Stream.empty,
223
+ getInputFd: () => Sink.drain,
224
+ getOutputFd: () => Stream.empty,
225
+ }),
226
+ ),
227
+ );
228
+ const result = yield* spawnAndCollect(
229
+ "fixture-cli",
230
+ ChildProcess.make("fixture-cli", ["--version"]),
231
+ ).pipe(
232
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
233
+ Effect.result,
234
+ );
235
+ expect(Result.isFailure(result)).toBe(true);
236
+ if (Result.isFailure(result)) {
237
+ expect(result.failure).toMatchObject({
238
+ _tag: "ProviderProbeOutputLimitError",
239
+ stream,
240
+ maxBytes: 8 * 1024 * 1024,
241
+ });
242
+ }
243
+ expect(drainedChunks).toBe(9);
244
+ }),
245
+ );
246
+
247
// regression: #35262 - without forceKillAfter a timed-out probe whose CLI ignores
248
// SIGTERM waits for it to exit, so the caller's timeout never returns.
249
it.live.skipIf(HostProcess.Platform.defaultValue() === "win32")(
packages/provider-core/src/server/snapshotProbe.ts
+28
-6
@@ -85,6 +85,18 @@ export class ProviderCommandNotFoundError extends Schema.TaggedError<ProviderCom
85
86
const isProviderCommandNotFoundError = Schema.is(ProviderCommandNotFoundError);
87
88
+export class ProviderProbeOutputLimitError extends Schema.TaggedError<
89
+ ProviderProbeOutputLimitError,
90
+ { readonly brand: unique symbol }
91
+>()("ProviderProbeOutputLimitError", {
92
+ stream: Schema.Literals(["stdout", "stderr"]),
93
+ maxBytes: Schema.Number,
94
+}) {
95
+ override get message(): string {
96
+ return `Provider status check produced too much ${this.stream} (limit ${this.maxBytes} bytes). Check the configured executable or wrapper in this environment's terminal.`;
97
+ }
98
+}
99
+
100
export interface ProviderProbeResult {
101
readonly installed: boolean;
102
readonly version: string | null;
@@ -128,6 +140,7 @@ export function providerProbeEnvironment(environment: NodeJS.ProcessEnv): NodeJS
140
// A timed-out probe interrupts the child with SIGTERM and then waits for it to exit,
141
// so a CLI that ignores SIGTERM would hold the caller's timeout open without this.
142
const PROBE_FORCE_KILL_AFTER = "1 second";
143
+const MAX_PROBE_OUTPUT_BYTES = 8 * 1024 * 1024;
144
145
function withProbeForceKill(command: ChildProcess.Command): ChildProcess.Command {
146
if (!ChildProcess.isStandardCommand(command) || command.options.forceKillAfter !== undefined) {
@@ -142,25 +155,34 @@ function withProbeForceKill(command: ChildProcess.Command): ChildProcess.Command
155
export const spawnAndCollect = Effect.fn("snapshotProbe.spawnAndCollect")(function* (
156
binaryPath: string,
157
command: ChildProcess.Command,
158
+ options?: { readonly maxStdoutBytes: number },
159
) {
160
+ const maxStdoutBytes = options?.maxStdoutBytes ?? MAX_PROBE_OUTPUT_BYTES;
161
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
162
const child = yield* spawner.spawn(withProbeForceKill(command));
163
const [stdout, stderr, exitCode] = yield* Effect.all(
164
[
150
- collectStreamAsString(child.stdout),
151
- collectStreamAsString(child.stderr),
165
+ collectUint8StreamText({ stream: child.stdout, maxBytes: maxStdoutBytes }),
166
+ collectUint8StreamText({ stream: child.stderr, maxBytes: MAX_PROBE_OUTPUT_BYTES }),
167
child.exitCode.pipe(Effect.map(Number)),
168
],
169
{ concurrency: "unbounded" },
170
);
171
157
- const result: CommandResult = { stdout, stderr, code: exitCode };
158
- if (yield* isWindowsCommandNotFound(exitCode, stderr)) {
172
+ const oversized = stdout.truncated ? "stdout" : stderr.truncated ? "stderr" : undefined;
173
+ if (oversized !== undefined) {
174
+ return yield* new ProviderProbeOutputLimitError({
175
+ stream: oversized,
176
+ maxBytes: oversized === "stdout" ? maxStdoutBytes : MAX_PROBE_OUTPUT_BYTES,
177
+ });
178
+ }
179
+ const result: CommandResult = { stdout: stdout.text, stderr: stderr.text, code: exitCode };
180
+ if (yield* isWindowsCommandNotFound(exitCode, stderr.text)) {
181
return yield* new ProviderCommandNotFoundError({
182
binaryPath,
183
exitCode,
162
- stdoutLength: stdout.length,
163
- stderrLength: stderr.length,
184
+ stdoutLength: stdout.text.length,
185
+ stderrLength: stderr.text.length,
186
});
187
}
188
return result;