The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { assert, it } from "@effect/vitest"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Fiber from "effect/Fiber"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | import * as Queue from "effect/Queue"; |
| 8 | import * as TestClock from "effect/testing/TestClock"; |
| 9 | |
| 10 | import { makeKimiPromptOutcomeWatch } from "./kimiAcp.ts"; |
| 11 | |
| 12 | const SESSION_ID = "session_06abd65a-ab8c-4f7a-a504-9b105fc7cd0d"; |
| 13 | const endTurn = { stopReason: "end_turn" } as const; |
| 14 | // Records trimmed from Kimi Code 2.5.3's wire.jsonl after the follow-up model request failed. |
| 15 | const failedTurn = (turnId: number) => |
| 16 | JSON.stringify({ |
| 17 | type: "turn.ended", |
| 18 | agentId: "main", |
| 19 | turnId, |
| 20 | reason: "failed", |
| 21 | error: { |
| 22 | code: "provider.overloaded", |
| 23 | message: "upstream overloaded", |
| 24 | name: "APIProviderOverloadedError", |
| 25 | details: { statusCode: 500 }, |
| 26 | retryable: true, |
| 27 | }, |
| 28 | durationMs: 138873, |
| 29 | time: 1794813146537, |
| 30 | }) + "\n"; |
| 31 | const completedTurn = (turnId: number) => |
| 32 | JSON.stringify({ type: "turn.ended", agentId: "main", turnId, reason: "completed" }) + "\n"; |
| 33 | |
| 34 | it.layer(NodeServices.layer)("makeKimiPromptOutcomeWatch", (it) => { |
| 35 | it.effect("fails only the turn this prompt ended, once Kimi writes its outcome", () => |
| 36 | Effect.gen(function* () { |
| 37 | const fileSystem = yield* FileSystem.FileSystem; |
| 38 | const path = yield* Path.Path; |
| 39 | const home = yield* fileSystem.makeTempDirectoryScoped(); |
| 40 | const agentDirectory = path.join(home, "sessions", "wd_proj_1", SESSION_ID, "agents", "main"); |
| 41 | yield* fileSystem.makeDirectory(agentDirectory, { recursive: true }); |
| 42 | const wire = path.join(agentDirectory, "wire.jsonl"); |
| 43 | // An earlier turn's failure is history, not this prompt's outcome. |
| 44 | yield* fileSystem.writeFileString(wire, failedTurn(0)); |
| 45 | const append = (text: string) => fileSystem.writeFileString(wire, text, { flag: "a" }); |
| 46 | // Signals once a poll has closed the log, so the check is already waiting for its next poll. |
| 47 | const polls = yield* Queue.unbounded<void>(); |
| 48 | const watch = yield* makeKimiPromptOutcomeWatch(home).pipe( |
| 49 | Effect.provideService(FileSystem.FileSystem, { |
| 50 | ...fileSystem, |
| 51 | open: (path, options) => |
| 52 | Effect.addFinalizer(() => Queue.offer(polls, undefined)).pipe( |
| 53 | Effect.andThen(fileSystem.open(path, options)), |
| 54 | ), |
| 55 | }), |
| 56 | ); |
| 57 | |
| 58 | const completed = yield* watch(SESSION_ID); |
| 59 | yield* append(completedTurn(1)); |
| 60 | assert.isUndefined(yield* completed(endTurn)); |
| 61 | yield* Queue.take(polls); |
| 62 | |
| 63 | // Kimi answers the prompt before `turn.ended` reaches disk. |
| 64 | const failed = yield* watch(SESSION_ID); |
| 65 | yield* append(failedTurn(2).slice(0, 40)); |
| 66 | const pending = yield* failed(endTurn).pipe(Effect.forkChild); |
| 67 | yield* Queue.take(polls); |
| 68 | yield* append(failedTurn(2).slice(40)); |
| 69 | yield* TestClock.adjust("50 millis"); |
| 70 | assert.deepStrictEqual(yield* Fiber.join(pending), { |
| 71 | class: "provider_error", |
| 72 | message: "upstream overloaded", |
| 73 | code: "provider.overloaded", |
| 74 | retryable: true, |
| 75 | }); |
| 76 | }), |
| 77 | ); |
| 78 | }); |