Something went wrong. Try again.
presents omp as codex for use with chatgpt remote
Something went wrong. Try again.
5.5 kB · 139 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140import test from "node:test";import assert from "node:assert/strict";import fs from "node:fs/promises";import net from "node:net";import os from "node:os";import path from "node:path";import { spawn } from "node:child_process";import { daemonStatus, runtimePaths, startDaemon, stopDaemon } from "../src/daemon.mjs";import { serveUnixWebSocket } from "../src/transports/websocket.mjs";import { socketAccepting } from "../src/util.mjs";
/** * A stand-in for a running daemon: something listening on the runtime socket * whose pid the pid file may or may not know about. */async function listeningOn(socketPath) { const server = net.createServer(() => {}); await new Promise((resolve) => server.listen(socketPath, resolve)); return { close: () => new Promise((resolve) => server.close(resolve)) };}
async function withRuntimeDir(fn) { const previous = process.env.OMP_CODEX_RUNTIME_DIR; const dir = await fs.mkdtemp(path.join(os.tmpdir(), "omp-daemon-test-")); process.env.OMP_CODEX_RUNTIME_DIR = dir; try { await fn(runtimePaths()); } finally { if (previous === undefined) delete process.env.OMP_CODEX_RUNTIME_DIR; else process.env.OMP_CODEX_RUNTIME_DIR = previous; await fs.rm(dir, { recursive: true, force: true }); }}
test("a reachable socket counts as running even when the pid file is stale", () => withRuntimeDir(async (paths) => { const daemon = await listeningOn(paths.socket); try { // 999999 is not a live process: the pid file has gone stale, as it does // whenever a daemon dies without cleaning up after itself. await fs.writeFile(paths.pid, "999999\n"); const status = await daemonStatus(paths); assert.equal(status.reachable, true); assert.equal(status.running, true, "a live listener must not be reported as stopped"); assert.equal(status.pid, null); } finally { await daemon.close(); }}));
test("status discovers and records the verified bridge process serving a live socket", () => withRuntimeDir(async (paths) => { const script = ` const net = require("node:net"); const socketPath = process.argv.at(-1); const server = net.createServer(() => {}); server.listen(socketPath); process.on("SIGTERM", () => server.close(() => process.exit(0))); `; const child = spawn(process.execPath, ["-e", script, "codex", "app-server", paths.socket], { stdio: "ignore", }); try { const deadline = Date.now() + 2_000; while (Date.now() < deadline && !await socketAccepting(paths.socket)) { await new Promise((resolve) => setTimeout(resolve, 20)); } assert.equal(await socketAccepting(paths.socket), true); const status = await daemonStatus(paths); assert.equal(status.pid, child.pid); assert.equal(Number((await fs.readFile(paths.pid, "utf8")).trim()), child.pid); } finally { child.kill("SIGTERM"); await new Promise((resolve) => child.once("exit", resolve)); }}));
test("start adopts a live daemon instead of unlinking its socket", () => withRuntimeDir(async (paths) => { const daemon = await listeningOn(paths.socket); try { await fs.writeFile(paths.pid, "999999\n"); const status = await startDaemon({ executable: "/nonexistent/codex.mjs" }); assert.equal(status.reachable, true); assert.equal(await socketAccepting(paths.socket), true, "the running daemon must still be reachable"); } finally { await daemon.close(); }}));
test("stop refuses to orphan a live daemon it cannot identify", () => withRuntimeDir(async (paths) => { const daemon = await listeningOn(paths.socket); try { await fs.writeFile(paths.pid, "999999\n"); const result = await stopDaemon(); assert.equal(result.stopped, false); assert.match(result.error ?? "", /refusing to orphan/i); assert.equal(await socketAccepting(paths.socket), true, "the socket must survive a refused stop"); } finally { await daemon.close(); }}));
test("stop clears leftovers when nothing is listening", () => withRuntimeDir(async (paths) => { const dead = await listeningOn(paths.socket); await dead.close(); await fs.writeFile(paths.pid, "999999\n"); const result = await stopDaemon(); assert.equal(result.stopped, false); assert.equal(result.error, undefined); await assert.rejects(() => fs.lstat(paths.socket), /ENOENT/, "a dead socket should be cleaned up");}));
test("a second app-server refuses to take over a socket in use", () => withRuntimeDir(async (paths) => { const daemon = await listeningOn(paths.socket); try { await assert.rejects( () => serveUnixWebSocket({}, paths.socket), /already listening/, "binding over a live daemon must fail loudly, not silently steal the path", ); assert.equal(await socketAccepting(paths.socket), true); } finally { await daemon.close(); }}));
test("a stale socket file left by a crash is replaced", () => withRuntimeDir(async (paths) => { const crashed = await listeningOn(paths.socket); await crashed.close(); await fs.writeFile(paths.socket, "").catch(() => {}); await fs.rm(paths.socket, { force: true }); const dead = await listeningOn(paths.socket); await dead.close(); // The file may linger after an unclean exit; a fresh listener must still bind. const listener = await serveUnixWebSocket({}, paths.socket); try { assert.equal(await socketAccepting(paths.socket), true); } finally { await listener.cleanup(); }}));