|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off |
| 2 | +import * as NodeFS from "node:fs"; |
| 3 | +import * as NodeOS from "node:os"; |
| 4 | +import * as NodePath from "node:path"; |
| 5 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 6 | +import { it, expect } from "@effect/vitest"; |
| 7 | +import { CodexSettings, ThreadId } from "@t3tools/contracts"; |
| 8 | +import * as Effect from "effect/Effect"; |
| 9 | +import * as Layer from "effect/Layer"; |
| 10 | +import * as Schema from "effect/Schema"; |
| 11 | +import { ServerConfig } from "../../config.ts"; |
| 12 | +import { writeFakeCli } from "../../testUtils/fakeCli.ts"; |
| 13 | +import { makeCodexAdapter } from "./CodexAdapter.ts"; |
| 14 | + |
| 15 | +const decodeCodexSettings = Schema.decodeEffect(CodexSettings); |
| 16 | + |
| 17 | +it.effect( |
| 18 | + "reuses one initialize-only Codex transport across concurrent and repeated history reads", |
| 19 | + () => |
| 20 | + Effect.gen(function* () { |
| 21 | + const dir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "codex-history-client-")); |
| 22 | + yield* Effect.addFinalizer(() => |
| 23 | + Effect.sync(() => NodeFS.rmSync(dir, { recursive: true, force: true })), |
| 24 | + ); |
| 25 | + const log = NodePath.join(dir, "methods.txt"); |
| 26 | + const binaryPath = writeFakeCli({ |
| 27 | + directory: dir, |
| 28 | + name: "codex-history", |
| 29 | + env: { HISTORY_TEST_LOG: log }, |
| 30 | + source: ` |
| 31 | + import { appendFileSync } from "node:fs"; |
| 32 | + import { createInterface } from "node:readline"; |
| 33 | + createInterface({ input: process.stdin }).on("line", (line) => { |
| 34 | + const request = JSON.parse(line); |
| 35 | + appendFileSync(process.env.HISTORY_TEST_LOG, request.method + "\\n"); |
| 36 | + if (request.id === undefined) return; |
| 37 | + const result = request.method === "initialize" ? { userAgent: "codex/1.0", codexHome: process.cwd(), platformFamily: "unix", platformOs: "linux" } |
| 38 | + : { thread: { id: request.params.threadId, cliVersion: "test", createdAt: 0, updatedAt: 0, |
| 39 | + cwd: process.cwd(), ephemeral: false, modelProvider: "openai", preview: "", sessionId: "session", |
| 40 | + status: { type: "idle" }, source: { subAgent: { thread_spawn: { parent_thread_id: "parent", depth: 1 } } }, |
| 41 | + turns: [{ id: "turn", status: "completed", error: null, items: [{ id: "message", type: "agentMessage", text: "Saved answer" }] }] } }; |
| 42 | + process.stdout.write(JSON.stringify({ id: request.id, result }) + "\\n"); |
| 43 | + }); |
| 44 | + `, |
| 45 | + }); |
| 46 | + const adapter = yield* makeCodexAdapter(yield* decodeCodexSettings({ binaryPath })); |
| 47 | + const input = { |
| 48 | + threadId: ThreadId.make("stopped"), |
| 49 | + agentId: "child", |
| 50 | + cwd: dir, |
| 51 | + offset: 0, |
| 52 | + resumeCursor: { threadId: "parent" }, |
| 53 | + }; |
| 54 | + const results = yield* Effect.all( |
| 55 | + [adapter.getAgentHistory!(input), adapter.getAgentHistory!(input)], |
| 56 | + { concurrency: "unbounded" }, |
| 57 | + ); |
| 58 | + expect(results.every((result) => result.entries[0]?.detail === "Saved answer")).toBe(true); |
| 59 | + yield* adapter.getAgentHistory!(input); |
| 60 | + const methods = NodeFS.readFileSync(log, "utf8").trim().split("\n"); |
| 61 | + expect(methods.filter((method) => method === "initialize")).toHaveLength(1); |
| 62 | + expect(methods.filter((method) => method === "thread/read")).toHaveLength(6); |
| 63 | + expect( |
| 64 | + methods.every((method) => ["initialize", "initialized", "thread/read"].includes(method)), |
| 65 | + ).toBe(true); |
| 66 | + expect(yield* adapter.listSessions()).toEqual([]); |
| 67 | + }).pipe( |
| 68 | + Effect.provide( |
| 69 | + ServerConfig.layerTest(process.cwd(), process.cwd()).pipe( |
| 70 | + Layer.provideMerge(NodeServices.layer), |
| 71 | + ), |
| 72 | + ), |
| 73 | + ), |
| 74 | +); |
0 commit comments