|
1 | 1 | // @effect-diagnostics nodeBuiltinImport:off -- the fake CLI and its leftover child are real processes. |
2 | 2 | import * as ClaudeSdk from "@anthropic-ai/claude-agent-sdk"; |
3 | 3 | import * as NodeChildProcess from "node:child_process"; |
| 4 | +import * as NodeEvents from "node:events"; |
4 | 5 | import * as NodeFS from "node:fs"; |
5 | 6 | import * as NodeOS from "node:os"; |
6 | 7 | import * as NodePath from "node:path"; |
| 8 | +import * as NodeStream from "node:stream"; |
7 | 9 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
8 | 10 | import { assert, describe, it } from "@effect/vitest"; |
9 | 11 | import { ProviderSessionId, ThreadId } from "@t3tools/contracts"; |
10 | 12 | import * as Effect from "effect/Effect"; |
| 13 | +import * as Exit from "effect/Exit"; |
11 | 14 | import * as Deferred from "effect/Deferred"; |
12 | 15 | import * as Fiber from "effect/Fiber"; |
13 | 16 | import * as FileSystem from "effect/FileSystem"; |
@@ -199,6 +202,78 @@ it.effect("reopens a stalled MCP registration before delivering the resumed prom |
199 | 202 | ), |
200 | 203 | ); |
201 | 204 |
|
| 205 | +it.effect.each([true, false])("only retries after confirmed process exit: %s", (exits) => |
| 206 | + Effect.scoped( |
| 207 | + Effect.gen(function* () { |
| 208 | + const registrationStarted = Promise.withResolvers<void>(); |
| 209 | + const cleanupProgress = Promise.withResolvers<"kill" | "retry">(); |
| 210 | + const events = new NodeEvents.EventEmitter(); |
| 211 | + const stdin = new NodeStream.PassThrough(); |
| 212 | + const stdout = new NodeStream.PassThrough(); |
| 213 | + const child = { |
| 214 | + stdin, |
| 215 | + stdout, |
| 216 | + killed: false, |
| 217 | + exitCode: null, |
| 218 | + kill: vi.fn(() => { |
| 219 | + cleanupProgress.resolve("kill"); |
| 220 | + return true; |
| 221 | + }), |
| 222 | + on: events.on.bind(events), |
| 223 | + once: events.once.bind(events), |
| 224 | + off: events.off.bind(events), |
| 225 | + } satisfies ClaudeSdk.SpawnedProcess; |
| 226 | + const signal = yield* Effect.abortSignal; |
| 227 | + const disposed = vi.fn(async () => {}); |
| 228 | + const querySpy = vi.spyOn(ClaudeSdk, "query").mockImplementation(({ options }) => { |
| 229 | + const first = querySpy.mock.calls.length === 1; |
| 230 | + if (first) { |
| 231 | + options?.spawnClaudeCodeProcess?.({ command: "claude", args: [], env: {}, signal }); |
| 232 | + } else { |
| 233 | + cleanupProgress.resolve("retry"); |
| 234 | + } |
| 235 | + return { |
| 236 | + setMcpServers: () => { |
| 237 | + registrationStarted.resolve(); |
| 238 | + return first |
| 239 | + ? new Promise<ClaudeSdk.McpSetServersResult>(() => {}) |
| 240 | + : Promise.resolve({ added: ["t3-code"], removed: [], errors: {} }); |
| 241 | + }, |
| 242 | + close: vi.fn(), |
| 243 | + [Symbol.asyncDispose]: disposed, |
| 244 | + } as unknown as ClaudeSdk.Query; |
| 245 | + }); |
| 246 | + yield* Effect.addFinalizer(() => |
| 247 | + Effect.sync(() => { |
| 248 | + querySpy.mockRestore(); |
| 249 | + stdin.destroy(); |
| 250 | + stdout.destroy(); |
| 251 | + }), |
| 252 | + ); |
| 253 | + const runner = yield* ClaudeAgentSdkQueryRunner; |
| 254 | + const opening = yield* runner |
| 255 | + .open({ |
| 256 | + ...resumedMcpQueryInput, |
| 257 | + options: { ...resumedMcpQueryInput.options, spawnClaudeCodeProcess: () => child }, |
| 258 | + }) |
| 259 | + .pipe(Effect.exit, Effect.forkChild); |
| 260 | + yield* Effect.promise(() => registrationStarted.promise); |
| 261 | + yield* TestClock.adjust("90 seconds"); |
| 262 | + |
| 263 | + assert.equal(yield* Effect.promise(() => cleanupProgress.promise), "kill"); |
| 264 | + assert.equal(querySpy.mock.calls.length, 1); |
| 265 | + assert.deepEqual(child.kill.mock.calls, [["SIGKILL"]]); |
| 266 | + if (exits) events.emit("exit", null, "SIGKILL"); |
| 267 | + else yield* TestClock.adjust("10 seconds"); |
| 268 | + const result = yield* Fiber.join(opening); |
| 269 | + assert.equal(Exit.isSuccess(result), exits); |
| 270 | + if (Exit.isSuccess(result)) yield* result.value.close; |
| 271 | + assert.equal(querySpy.mock.calls.length, exits ? 2 : 1); |
| 272 | + assert.equal(disposed.mock.calls.length, 1); |
| 273 | + }).pipe(Effect.provide(realQueryRunner)), |
| 274 | + ), |
| 275 | +); |
| 276 | + |
202 | 277 | it.effect("stops retrying MCP registration after two timeouts and reports the cause", () => |
203 | 278 | Effect.scoped( |
204 | 279 | Effect.gen(function* () { |
|
0 commit comments