UNPKG

@essentialai/cogent-bridge

Version:

Cogent Bridge — cross-agent comms for Claude Code, OpenAI Codex and Slack. Codex users: install with `curl -fsSL https://cogent.tools/install.sh | sh`, then start with `cogent-codex` for real-time peer wake.

140 lines • 5.73 kB
/** * COG-95 experiment switch — `~/.cogent/wake-experiment.json`. * * The A-vs-C experiment (docs/experiments/2026-10-codex-wake-A-vs-C/PLAN.md) runs ONE build through * several phases. The operator flips a phase by editing this file, without restarting any agent, so * it is read on EVERY wake — never cached across wakes. A sync read of a tiny file costs nothing next * to a wake. * * HOMEDIR-BASED, NOT COGENT_STATE_PATH — same reasoning as wake-inflight note 1: the operator's shell * must find it, and a plugin env var reaches only the bridge. * * NEVER THROWS, NEVER BLOCKS A WAKE. Absent, unreadable, invalid JSON or a field of the wrong type → * `{}` (no phase, no late answer, no sandbox override): the experiment can only ever fall back to * today's behaviour. An invalid file warns ONCE per process, naming the file and the problem — a * silently ignored switch would make a phase look run when it was not. */ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { logger } from "../logger.js"; export const LATE_ANSWER_MIN_MS = 60_000; /** 4 h. The rail-C hook's in-flight cap (check-on-stop.lib.mjs INFLIGHT_MAX_MS) must exceed this. */ export const LATE_ANSWER_MAX_MS = 14_400_000; export const LATE_ANSWER_DEFAULT_MS = 3_600_000; /** Kept in step with the COGENT_CODEX_SANDBOX enum in src/config.ts (wake-experiment.test.ts pins it). */ export const CODEX_SANDBOX_MODES = [ "full-auto", "read-only", "workspace-write", "danger-full-access", "bypass", ]; const OFF = { phase: null, lateAnswer: false, lateAnswerMaxMs: LATE_ANSWER_DEFAULT_MS, codexSandbox: null, }; let pathOverride = null; /** Test seam: point the reader at another file (null = the real homedir path). */ export function setWakeExperimentPathForTests(p) { pathOverride = p; } export function wakeExperimentPath() { return pathOverride ?? path.join(os.homedir(), ".cogent", "wake-experiment.json"); } let warned = false; /** Test seam: allow the once-per-process warning to fire again. */ export function resetWakeExperimentWarning() { warned = false; } function warnOnce(file, problem) { if (warned) return; warned = true; logger.warn(`wake-experiment: ignoring ${file} (${problem}) — wakes run with the experiment OFF`); } /** The problem with `raw`, or null when every present field has the right type. */ function invalidReason(raw) { if (!raw || typeof raw !== "object" || Array.isArray(raw)) return "not a JSON object"; const o = raw; if ("phase" in o && typeof o.phase !== "string") return "phase must be a string"; if ("lateAnswer" in o && typeof o.lateAnswer !== "boolean") return "lateAnswer must be true or false"; if ("lateAnswerMaxMs" in o && !Number.isInteger(o.lateAnswerMaxMs)) return "lateAnswerMaxMs must be an integer"; if ("codexSandbox" in o && !CODEX_SANDBOX_MODES.includes(o.codexSandbox)) { return `codexSandbox must be one of ${CODEX_SANDBOX_MODES.join(", ")}`; } return null; } /** Read the switch now. See the module note: never throws, and anything wrong means OFF. */ export function readWakeExperiment() { const file = wakeExperimentPath(); let text; let fd; try { // Opened non-blocking and checked to be a regular file BEFORE any read: this runs synchronously // on the bridge's event loop at every wake, and a plain read of a FIFO (or a device such as // /dev/zero) at this path never returns — every bridge on the machine would freeze. fd = fs.openSync(file, fs.constants.O_RDONLY | (fs.constants.O_NONBLOCK ?? 0)); const st = fs.fstatSync(fd); if (!st.isFile()) { warnOnce(file, "not a regular file"); return { ...OFF }; } // Only a file the user owns and nobody else can write may steer their wakes. (No uid on Windows: // there the mode bits carry no such meaning either, so the check is skipped.) const uid = typeof process.getuid === "function" ? process.getuid() : null; if (uid !== null && st.uid !== uid) { warnOnce(file, `owned by uid ${st.uid}, not by this user (${uid})`); return { ...OFF }; } if (uid !== null && (st.mode & 0o022) !== 0) { warnOnce(file, `group- or world-writable (mode ${(st.mode & 0o777).toString(8)}); run: chmod 600 ${file}`); return { ...OFF }; } text = fs.readFileSync(fd, "utf-8"); } catch (err) { // Absent is the normal state outside an experiment — not worth a word. if (err.code !== "ENOENT") warnOnce(file, `unreadable: ${err.message}`); return { ...OFF }; } finally { if (fd !== undefined) { try { fs.closeSync(fd); } catch { /* nothing to do: the descriptor is gone either way */ } } } let raw; try { raw = JSON.parse(text); } catch (err) { warnOnce(file, `invalid JSON: ${err.message}`); return { ...OFF }; } const problem = invalidReason(raw); if (problem) { warnOnce(file, problem); return { ...OFF }; } const o = raw; const max = typeof o.lateAnswerMaxMs === "number" ? o.lateAnswerMaxMs : LATE_ANSWER_DEFAULT_MS; return { phase: typeof o.phase === "string" ? o.phase : null, lateAnswer: o.lateAnswer === true, lateAnswerMaxMs: Math.min(LATE_ANSWER_MAX_MS, Math.max(LATE_ANSWER_MIN_MS, max)), codexSandbox: o.codexSandbox ?? null, }; } //# sourceMappingURL=wake-experiment.js.map