|
| 1 | +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import pino from "pino"; |
| 5 | +import { describe, expect, test } from "vitest"; |
| 6 | + |
| 7 | +import type { AgentSessionConfig } from "../../agent-sdk-types.js"; |
| 8 | +import { FakePi } from "../pi/test-utils/fake-pi.js"; |
| 9 | +import { OmpRpcAgentClient } from "./agent.js"; |
| 10 | + |
| 11 | +function createClientWithOmpAgentDir(agentDir: string): OmpRpcAgentClient { |
| 12 | + return new OmpRpcAgentClient({ |
| 13 | + logger: pino({ level: "silent" }), |
| 14 | + runtime: new FakePi(), |
| 15 | + runtimeSettings: { env: { OMP_CODING_AGENT_DIR: agentDir } }, |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +function createConfig(overrides: Partial<AgentSessionConfig> = {}): AgentSessionConfig { |
| 20 | + return { |
| 21 | + provider: "omp", |
| 22 | + cwd: "/tmp/paseo-omp-rpc-test", |
| 23 | + ...overrides, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +describe("OmpRpcAgentClient", () => { |
| 28 | + test("identifies itself with the omp provider id", () => { |
| 29 | + const client = new OmpRpcAgentClient({ |
| 30 | + logger: pino({ level: "silent" }), |
| 31 | + runtime: new FakePi(), |
| 32 | + }); |
| 33 | + expect(client.provider).toBe("omp"); |
| 34 | + }); |
| 35 | + |
| 36 | + test("creates a session that reports the omp provider", async () => { |
| 37 | + const client = new OmpRpcAgentClient({ |
| 38 | + logger: pino({ level: "silent" }), |
| 39 | + runtime: new FakePi(), |
| 40 | + }); |
| 41 | + |
| 42 | + const session = await client.createSession(createConfig()); |
| 43 | + expect(session.provider).toBe("omp"); |
| 44 | + await session.close(); |
| 45 | + }); |
| 46 | + |
| 47 | + test("lists persisted OMP sessions from the configured OMP agent directory", async () => { |
| 48 | + const root = mkdtempSync(path.join(tmpdir(), "paseo-omp-client-")); |
| 49 | + const cwd = path.join(root, "workspace"); |
| 50 | + const agentDir = path.join(root, "agent"); |
| 51 | + const sessionsDir = path.join(agentDir, "sessions", "--workspace--"); |
| 52 | + mkdirSync(sessionsDir, { recursive: true }); |
| 53 | + const sessionFile = path.join(sessionsDir, "20260101_session.jsonl"); |
| 54 | + writeFileSync( |
| 55 | + sessionFile, |
| 56 | + [ |
| 57 | + JSON.stringify({ |
| 58 | + type: "session", |
| 59 | + version: 3, |
| 60 | + id: "omp-session", |
| 61 | + timestamp: "2026-01-01T00:00:00.000Z", |
| 62 | + cwd, |
| 63 | + }), |
| 64 | + JSON.stringify({ |
| 65 | + type: "message", |
| 66 | + id: "entry-1", |
| 67 | + parentId: null, |
| 68 | + timestamp: "2026-01-01T00:00:01.000Z", |
| 69 | + message: { role: "user", content: "remember this" }, |
| 70 | + }), |
| 71 | + ].join("\n") + "\n", |
| 72 | + "utf8", |
| 73 | + ); |
| 74 | + const client = createClientWithOmpAgentDir(agentDir); |
| 75 | + |
| 76 | + await expect(client.listPersistedAgents({ cwd })).resolves.toMatchObject([ |
| 77 | + { |
| 78 | + provider: "omp", |
| 79 | + sessionId: "omp-session", |
| 80 | + cwd, |
| 81 | + persistence: { |
| 82 | + provider: "omp", |
| 83 | + sessionId: "omp-session", |
| 84 | + nativeHandle: sessionFile, |
| 85 | + metadata: { provider: "omp", cwd }, |
| 86 | + }, |
| 87 | + timeline: [{ type: "user_message", text: "remember this" }], |
| 88 | + }, |
| 89 | + ]); |
| 90 | + }); |
| 91 | + |
| 92 | + test("ignores PI_CODING_AGENT_DIR when running as the OMP provider", async () => { |
| 93 | + const root = mkdtempSync(path.join(tmpdir(), "paseo-omp-isolation-")); |
| 94 | + const cwd = path.join(root, "workspace"); |
| 95 | + const piAgentDir = path.join(root, "pi-agent"); |
| 96 | + const ompAgentDir = path.join(root, "omp-agent"); |
| 97 | + const piSessionsDir = path.join(piAgentDir, "sessions"); |
| 98 | + const ompSessionsDir = path.join(ompAgentDir, "sessions"); |
| 99 | + mkdirSync(piSessionsDir, { recursive: true }); |
| 100 | + mkdirSync(ompSessionsDir, { recursive: true }); |
| 101 | + |
| 102 | + const sessionRecord = (id: string) => |
| 103 | + [ |
| 104 | + JSON.stringify({ |
| 105 | + type: "session", |
| 106 | + version: 3, |
| 107 | + id, |
| 108 | + timestamp: "2026-01-01T00:00:00.000Z", |
| 109 | + cwd, |
| 110 | + }), |
| 111 | + JSON.stringify({ |
| 112 | + type: "message", |
| 113 | + id: "entry-1", |
| 114 | + parentId: null, |
| 115 | + timestamp: "2026-01-01T00:00:01.000Z", |
| 116 | + message: { role: "user", content: id }, |
| 117 | + }), |
| 118 | + ].join("\n") + "\n"; |
| 119 | + writeFileSync(path.join(piSessionsDir, "pi.jsonl"), sessionRecord("pi-only"), "utf8"); |
| 120 | + writeFileSync(path.join(ompSessionsDir, "omp.jsonl"), sessionRecord("omp-only"), "utf8"); |
| 121 | + |
| 122 | + const client = new OmpRpcAgentClient({ |
| 123 | + logger: pino({ level: "silent" }), |
| 124 | + runtime: new FakePi(), |
| 125 | + runtimeSettings: { |
| 126 | + env: { |
| 127 | + PI_CODING_AGENT_DIR: piAgentDir, |
| 128 | + OMP_CODING_AGENT_DIR: ompAgentDir, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + const descriptors = await client.listPersistedAgents({ cwd }); |
| 134 | + expect(descriptors.map((d) => d.sessionId)).toEqual(["omp-only"]); |
| 135 | + }); |
| 136 | +}); |
0 commit comments