UNPKG

@bitkyc08/opencodex

Version:

Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code

115 lines (113 loc) • 6.75 kB
import { probeSpawnSync } from "./mutation-guard.mjs"; /** * Is something still answering `/healthz` as an opencodex proxy on this endpoint? * * Absent PID and runtime-port files are weak evidence that the proxy is gone: a crashed * but still-listening process, or one supervised outside our records, leaves no files and * keeps the port. Replacing package files under it leaves a server running a mix of old * and new modules, which is the hazard `ocx update` stops the proxy to avoid (#3008). * * Synchronous and dependency-free because it runs inside the plain-Node launcher's * `runNpmSelfUpdate`, which is not async and cannot import the TypeScript liveness module. * A separate Node child does the fetch so the caller keeps its straight-line control flow. * * Returns `"live" | "dead" | "unknown"`, and the caller treats `unknown` as a reason to * stop. Fail-open was wrong here: a listener that accepts connections but withholds * `/healthz`, or a probe that times out, is exactly the state where replacing package * files is most dangerous, and "we could not tell" is not evidence the proxy is gone. * A refused connection or a definitive non-OpenCodex answer earns `"dead"`. So does one * more observation when the dial is neither answered nor refused (a timeout, or a dropped * SYN as with a listener bound to a tailnet address): the child tries one transient * exclusive bind of the same host and port, and a successful bind means nothing holds the * port at that instant. The fallback runs only for a literal IP address, because a name can * resolve to different addresses for the dial and the bind. A failed bind (`EADDRINUSE`, * `EADDRNOTAVAIL`, any error) stays * `"unknown"`, a held-but-silent port included. The whole probe is bounded by the child's * own `timeoutMs` plus a 1500 ms spawn ceiling, after which it is `"unknown"`. A successful * bind is a statement about that moment, not a promise that the endpoint cannot restart. */ export function probeProxyLiveness(port, hostname = "127.0.0.1", timeoutMs = 1500) { // An unusable port is not an ambiguous probe: there is nothing to ask. if (!Number.isFinite(port) || port <= 0 || port > 65535) return "dead"; // Normalize HERE rather than at each call site. Leaving it to the callers put the fix in // one lane and not the other, and a bracketed IPv6 literal handed to node:http answers // nothing - which the tri-state correctly reports as "unknown" and the updater correctly // treats as a reason to abort, turning a healthy stop into a refused update. let host = typeof hostname === "string" && hostname.trim() !== "" ? hostname.trim() : "127.0.0.1"; // A wildcard bind answers on loopback; `node:http` cannot dial the wildcard itself. if (host === "0.0.0.0" || host === "*") host = "127.0.0.1"; if (host === "::" ) host = "::1"; // `[::1]` is a URL spelling; the socket layer wants the bare address. if (host.startsWith("[") && host.endsWith("]")) host = host.slice(1, -1); // `node:http` rather than `fetch`: the child inherits a parent whose event loop is // blocked on `spawnSync`, and an aborted-before-dispatch fetch reports the same "not // live" as a genuinely dead port. A request emitted on the socket cannot be confused // with one that never left. const script = [ "const http = require('node:http');", "const [host, port, timeout] = process.argv.slice(1);", "let settled = false;", "const settle = word => { if (!settled) { settled = true; process.stdout.write(word); } };", "const req = http.get({ host, port: Number(port), path: '/healthz', timeout: Number(timeout) }, res => {", " let body = '';", " res.setEncoding('utf8');", " res.on('data', chunk => { body += chunk; });", " res.on('end', () => {", " try {", " const parsed = JSON.parse(body);", " // Mirrors isOpencodexHealthz in src/server/proxy-liveness.ts. A foreign server", " // that happens to expose /healthz must not be read as our proxy, and a", " // pre-identity build of ours must not be read as foreign.", " const isOpencodex = parsed && typeof parsed === 'object'", " && (parsed.service === 'opencodex'", " || (parsed.service === undefined", " && parsed.status === 'ok'", " && typeof parsed.version === 'string'", " && typeof parsed.uptime === 'number'));", " // Only a clean 200 decides anything. Any other status means the endpoint is", " // answering but not telling us what it is, which is not evidence of absence.", " if (res.statusCode !== 200) settle('UNKNOWN');", " else settle(isOpencodex ? 'LIVE' : 'DEAD');", " } catch { settle('UNKNOWN'); }", " });", "});", // A refused connection is the clean proof that nothing is listening. Everything else - // reset, unreachable host, a silent drop - leaves the question open, with one more way // to close it: a port nobody listens on can be bound, a held one cannot (EADDRINUSE). // A proxy bound to a Tailscale address is the case that needs this: the stack drops // the SYN instead of refusing it, so the dial only ever times out. "let bindStarted = false;", "const bindCheck = () => {", " if (bindStarted || settled) return;", " bindStarted = true;", // A name can resolve to several addresses, and the dial and the bind resolve it on // their own: a live proxy on one address would not stop a bind on another. Only a // literal address names the same endpoint on both sides. " if (require('node:net').isIP(host) === 0) { settle('UNKNOWN'); return; }", " const server = require('node:net').createServer();", " server.on('connection', socket => socket.destroy());", " server.once('error', () => settle('UNKNOWN'));", " server.listen({ host, port: Number(port), exclusive: true }, () => server.close(() => settle('DEAD')));", "};", "req.on('timeout', () => { req.destroy(); bindCheck(); });", "req.on('error', err => {", " if (err && err.code === 'ECONNREFUSED') settle('DEAD');", " else bindCheck();", "});", ].join("\n"); try { const probe = probeSpawnSync( process.execPath, ["-e", script, host, String(port), String(timeoutMs)], { encoding: "utf8", timeout: timeoutMs + 1500, windowsHide: true }, ); const out = probe.stdout ?? ""; if (out.includes("LIVE")) return "live"; if (out.includes("DEAD")) return "dead"; // A child that produced nothing, was killed by its own timeout, or failed to spawn // leaves the question open rather than answering it. return "unknown"; } catch { return "unknown"; } }