@devries-cameron20 / t3code Mirror · read-only / commits / 31dcc7c513
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 22, 20:45 UTC

fix(server): make the remaining flaky server tests deterministic (#22559)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 20, 2026 at 18:50 UTC 31dcc7c51309697af5fd5584d507a1359e0fb62f
7 files changed +134 -33
apps/server/scripts/acp-mock-agent.ts
+10 -4
@@ -1466,9 +1466,9 @@ const program = Effect.gen(function* () {
1466 },
1467 });
1468 if (exitAfterRunningCommandLaunch) {
1469 + // The test sends `_test/exit` once T3 has recorded the published processes.
1470 yield* Effect.promise(() => runningCommandPublished);
1470 - yield* Effect.sleep("100 millis");
1471 - return yield* Effect.sync(() => process.exit(0));
1471 + return yield* Effect.never;
1472 }
1473 while (!cancelledSessions.has(requestedSessionId)) {
1474 yield* Effect.sleep("25 millis");
@@ -2448,8 +2448,14 @@ const program = Effect.gen(function* () {
2448 return Effect.succeed({});
2449 });
2450
2451 - yield* agent.handleUnknownExtNotification((method) =>
2452 - method === "_test/exit" ? Effect.sync(() => process.exit(19)) : Effect.void,
2451 + yield* agent.handleUnknownExtNotification((method, params) =>
2452 + method === "_test/exit"
2453 + ? Effect.sync(() => {
2454 + const code =
2455 + typeof params === "object" && params !== null ? Reflect.get(params, "code") : undefined;
2456 + process.exit(typeof code === "number" ? code : 19);
2457 + })
2458 + : Effect.void,
2459 );
2460
2461 return yield* Effect.never;
apps/server/scripts/acp-replay-agent.ts
+5 -1
@@ -56,9 +56,12 @@ let nextAgentRequestId = 1;
56 const pendingClientRequestIds = new Map<string, string | number>();
57 const pendingAgentRequestMethods = new Map<string, string>();
58
59 +// The test reads status while this agent may still be running; rename makes each
60 +// write atomic so a reader never sees a truncated file.
61 function writeStatus(failure?: unknown): void {
62 + const temporaryStatusPath = `${replayStatusPath}.${process.pid}.tmp`;
63 NodeFS.writeFileSync(
61 - replayStatusPath,
64 + temporaryStatusPath,
65 JSON.stringify({
66 scenario: transcript.scenario,
67 cursor,
@@ -67,6 +70,7 @@ function writeStatus(failure?: unknown): void {
70 }),
71 "utf8",
72 );
73 + NodeFS.renameSync(temporaryStatusPath, replayStatusPath);
74 }
75
76 function stableStringify(value: unknown): string {
apps/server/src/auth/ServerSecretStore.ts
+27 -12
@@ -223,26 +223,41 @@ export const make = Effect.gen(function* () {
223 );
224 };
225
226 + // `link` publishes the fully written temp file under the final name, or fails with
227 + // AlreadyExists, so a concurrent reader never observes a partially written secret.
228 const create: ServerSecretStore["Service"]["create"] = (name, value) => {
229 const secretPath = resolveSecretPath(name);
228 - return Effect.scoped(
229 - Effect.gen(function* () {
230 - const file = yield* fileSystem.open(secretPath, {
231 - flag: "wx",
232 - mode: 0o600,
233 - });
234 - yield* file.writeAll(value);
235 - yield* file.sync;
236 - yield* fileSystem.chmod(secretPath, 0o600);
237 - }),
238 - ).pipe(
230 + return crypto.randomUUIDv4().pipe(
231 Effect.mapError(
232 (cause) =>
241 - new SecretStorePersistError({
233 + new SecretStoreTemporaryPathError({
234 resource: `secret ${name}`,
235 cause,
236 }),
237 ),
238 + Effect.flatMap((uuid) => {
239 + const tempPath = `${secretPath}.${uuid}.tmp`;
240 + return Effect.gen(function* () {
241 + yield* Effect.scoped(
242 + Effect.gen(function* () {
243 + const file = yield* fileSystem.open(tempPath, { flag: "wx", mode: 0o600 });
244 + yield* file.writeAll(value);
245 + yield* file.sync;
246 + }),
247 + );
248 + yield* fileSystem.chmod(tempPath, 0o600);
249 + yield* fileSystem.link(tempPath, secretPath);
250 + }).pipe(
251 + Effect.ensuring(fileSystem.remove(tempPath).pipe(Effect.ignore)),
252 + Effect.mapError(
253 + (cause) =>
254 + new SecretStorePersistError({
255 + resource: `secret ${name}`,
256 + cause,
257 + }),
258 + ),
259 + );
260 + }),
261 );
262 };
263
apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts
+14 -1
@@ -467,7 +467,19 @@ describe("AcpRegistryAdapterV2", () => {
467 readonly storedModePick?: string;
468 readonly turnModels?: ReadonlyArray<string>;
469 }) {
470 - const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
470 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
471 + // Scope release gives the replay agent at most a second to exit, so the status
472 + // read below waits for the agent itself instead of racing its last write.
473 + const agentExits: Array<Effect.Effect<void>> = [];
474 + const childProcessSpawner = ChildProcessSpawner.make((command) =>
475 + spawner
476 + .spawn(command)
477 + .pipe(
478 + Effect.tap((handle) =>
479 + Effect.sync(() => agentExits.push(Effect.ignore(handle.exitCode))),
480 + ),
481 + ),
482 + );
483 const fileSystem = yield* FileSystem.FileSystem;
484 const path = yield* Path.Path;
485 const replayDir = yield* fileSystem.makeTempDirectoryScoped({
@@ -592,6 +604,7 @@ describe("AcpRegistryAdapterV2", () => {
604 current = target;
605 }
606 }).pipe(Effect.scoped);
607 + yield* Effect.all(agentExits, { discard: true });
608 yield* makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript);
609 return { configurations };
610 });
packages/effect-acp/src/errors.test.ts
+13
@@ -53,6 +53,19 @@ describe("effect-acp errors", () => {
53 });
54 });
55
56 + it("includes the authored detail but not the cause in the transport message", () => {
57 + const cause = new Error("connection refused at a private endpoint");
58 + const error = new AcpError.AcpTransportError({
59 + detail: "ACP replay did not consume all frames",
60 + cause,
61 + });
62 +
63 + expect(error.message).toBe(
64 + "ACP transport operation failed: ACP replay did not consume all frames.",
65 + );
66 + expect(error.cause).toBe(cause);
67 + });
68 +
69 it("preserves structured extension handler failures behind stable request errors", () => {
70 const cause = new AcpError.AcpTransportError({
71 operation: "read-input-stream",
packages/effect-acp/src/errors.ts
+4 -3
@@ -244,10 +244,11 @@ export class AcpTransportError extends Schema.TaggedError<AcpTransportError>()(
244 },
245 ) {
246 override get message() {
247 + const operation = this.operation ? ` ${this.operation}` : "";
248 const method = this.method ? ` for method ${this.method}` : "";
248 - return this.operation
249 - ? `ACP transport operation ${this.operation} failed${method}.`
250 - : "ACP transport operation failed.";
249 + // Callers often pass a sentence; strip its period so the message ends with exactly one.
250 + const detail = this.detail ? `: ${this.detail.replace(/\.$/, "")}` : "";
251 + return `ACP transport operation${operation} failed${method}${detail}.`;
252 }
253 }
254
packages/provider-opencode/src/server/OpenCodeServerLedger.test.ts
+61 -12
@@ -1,5 +1,6 @@
1 // @effect-diagnostics nodeBuiltinImport:off
2 import * as NodeChildProcess from "node:child_process";
3 +import * as NodeReadline from "node:readline";
4
5 import * as NodeServices from "@effect/platform-node/NodeServices";
6 import { describe, expect, it } from "@effect/vitest";
@@ -38,20 +39,68 @@ const killGroup = (pgid: number) =>
39 }
40 });
41
42 +// libuv ignores a failed setsid(), which happens when the new pid equals the id of a group
43 +// that still has members (such as a sleep orphaned by an earlier test). The child would then
44 +// share the test worker's group, so the script first confirms it leads its own group and exits
45 +// with 97 before starting anything if it does not.
46 +const groupLeaderGuard = `pgid="$(ps -o pgid= -p $$ 2>&1 | tr -d ' ')"; [ "$pgid" = "$$" ] || { echo "ungrouped:$pgid"; exit 97; }; echo grouped; `;
47 +
48 +// Yields the group, or a description of why this shell cannot be used as one.
49 +const spawnGroupLeader = (args: ReadonlyArray<string>, script: string) =>
50 + Effect.gen(function* () {
51 + const child = NodeChildProcess.spawn("/bin/sh", ["-c", groupLeaderGuard + script, ...args], {
52 + detached: true,
53 + stdio: ["ignore", "pipe", "ignore"],
54 + });
55 + const pid = child.pid;
56 + if (pid === undefined) return yield* Effect.die("spawn failed");
57 + const grouped = yield* Deferred.make<true | string>();
58 + const exited = yield* Deferred.make<NodeJS.Signals | null>();
59 + const printed = yield* Deferred.make<void>();
60 + child.once("exit", (_code, signal) => Deferred.doneUnsafe(exited, Effect.succeed(signal)));
61 + // "close" fires after stdout drains, so a guard line always lands first. It only fires before
62 + // "grouped" when the shell died without starting the script, such as from a signal.
63 + child.once("close", (code, signal) =>
64 + Deferred.doneUnsafe(
65 + grouped,
66 + Effect.succeed(
67 + `shell ${pid} exited (code ${code}, signal ${signal}) before its group check`,
68 + ),
69 + ),
70 + );
71 + NodeReadline.createInterface({ input: child.stdout }).on("line", (line) => {
72 + if (line === "grouped") Deferred.doneUnsafe(grouped, Effect.succeed(true));
73 + if (line.startsWith("ungrouped:")) {
74 + const output = line.slice("ungrouped:".length);
75 + Deferred.doneUnsafe(
76 + grouped,
77 + Effect.succeed(`\`ps -o pgid= -p ${pid}\` printed ${JSON.stringify(output)}`),
78 + );
79 + }
80 + if (line === "ready") Deferred.doneUnsafe(printed, Effect.void);
81 + });
82 + const result = yield* Deferred.await(grouped);
83 + return result === true
84 + ? { pid, exited: Deferred.await(exited), printed: Deferred.await(printed) }
85 + : result;
86 + });
87 +
88 +// A pid colliding with a live group id is rare, so a few retries clear it. Repeated failures
89 +// mean the guard itself is broken (for example a missing or BusyBox `ps`).
90 +const GROUP_LEADER_ATTEMPTS = 5;
91 +
92 const spawnGroup = (args: ReadonlyArray<string>, script = "sleep 600 & wait") =>
93 Effect.acquireRelease(
94 Effect.gen(function* () {
44 - const child = NodeChildProcess.spawn("/bin/sh", ["-c", script, ...args], {
45 - detached: true,
46 - stdio: ["ignore", "pipe", "ignore"],
47 - });
48 - const exited = yield* Deferred.make<NodeJS.Signals | null>();
49 - const printed = yield* Deferred.make<void>();
50 - child.once("exit", (_code, signal) => Deferred.doneUnsafe(exited, Effect.succeed(signal)));
51 - child.stdout.once("data", () => Deferred.doneUnsafe(printed, Effect.void));
52 - const pid = child.pid;
53 - if (pid === undefined) return yield* Effect.die("spawn failed");
54 - return { pid, exited: Deferred.await(exited), printed: Deferred.await(printed) };
95 + let failure = "";
96 + for (let attempt = 0; attempt < GROUP_LEADER_ATTEMPTS; attempt++) {
97 + const group = yield* spawnGroupLeader(args, script);
98 + if (typeof group !== "string") return group;
99 + failure = group;
100 + }
101 + return yield* Effect.die(
102 + `no shell led its own process group after ${GROUP_LEADER_ATTEMPTS} attempts; last: ${failure}`,
103 + );
104 }),
105 ({ pid }) => killGroup(pid),
106 );
@@ -64,7 +113,7 @@ const recordFromDeadServer = (stateDir: string, server: { readonly pid: number }
113 ownerPid: previousServer.pid,
114 });
115 yield* Effect.asVoid(previousLedger.track({ pid: server.pid, port: 4096, args: SERVE_ARGS }));
67 - process.kill(-previousServer.pid, "SIGKILL");
116 + yield* killGroup(previousServer.pid);
117 yield* previousServer.exited;
118 });
119