// The managed session process: `node runner.mjs `, with the run's // capability as one JSON line on stdin ({"cap": "..."}). The host starts it // inside its own PID namespace (packages/seat/src/session.mjs) after the // broker bound the launch, and ends the launch when it exits. // // 1. Founder-credential stop (REQ-CRED-2): a known founder credential // variable in the environment, or a service the role needs with no // usable role token, exits 20 before the role is claimed. // 2. role.claim; a refusal exits 21. // 3. The tool socket (0600) serves the harness's typed tools: a name in // tools.json is forwarded with the runner's capability, to the broker // or, for `launch`, to the host's launch socket, or, for push_working, // fetch_review and land_work, to the host's git socket (git-tools.mjs). The // harness never sees the capability. // 4. One harness turn per received message, through the adapter, in its // own process group under a wall clock (S0 line 4). The turn's stdout // goes back to the sender as a RESULT, except for an incoming RESULT or // an agent's non-REQUEST reply to a RESULT. Human, tester and user messages // and REQUESTs still get replies. Then message.read. // Before the first message, one turn per session.json `landing` entry // (row 156): a fixed request to land the approved merge decision, with // no reply and no read. A malformed entry exits 2 before the claim. // 5. SIGTERM ends the turn, releases the role and exits 0; before the // claim it exits 0 without claiming. // 6. The unslop check (row 66): the harness logs each reply's check to // /unslop.jsonl (MOSAIC_UNSLOP_LOG). After each turn the // runner reads what the log gained, and the next reply it sends ends // with a note naming the flagged replies, or, once a run, the broken // check. That reply is the RESULT `mosaic talk` and `mosaic follow` // print, so the operator sees it there. // // Exit codes: 0 stopped, 20 founder credentials, 21 claim refused, 22 the // capability or the claim stopped working (ended, revoked), 23 the broker // stayed unreachable, 2 a bad run directory. import { refusalGuidance } from "../../bus/src/refusals.mjs"; import { spawn } from "node:child_process"; import { setTimeout as delay } from "node:timers/promises"; import { chmodSync, existsSync, mkdirSync, openSync, readFileSync, rmSync, closeSync } from "node:fs"; import { createServer, connect } from "node:net"; import { join } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; import { Client } from "../../bus/src/client.mjs"; import { LENGTH_FAILURE_NOTICE_PREFIX } from "./failure-notice.mjs"; import { ID_PATTERN } from "../../organization/src/vocabulary.mjs"; import { firstOutputMarker, firstTurnMarker } from './first-output.mjs'; import { GUARD_CODES, FIXED_TEXTS } from "../../library/src/reply-protocol.mjs"; import { fetchReview, landWork, pushWorking } from "./git-tools.mjs"; export const isLengthFailureNotice = m => m.class === "INFO" && typeof m.from_role === "string" && ID_PATTERN.test(m.from_role) && !["human", "tester", "user"].includes(m.from_role) && typeof m.body === "string" && m.body.startsWith(LENGTH_FAILURE_NOTICE_PREFIX); export const EXIT = Object.freeze({ stopped: 0, founder: 20, claim: 21, session: 22, broker: 23, usage: 2 }); // Environment variables that carry a founder (human) credential for a // service the broker brokers. The host's environment allowlist keeps them // out; finding one here means the allowlist was bypassed. export const FOUNDER_ENV = Object.freeze([ "GITEA_TOKEN", "GITEA_ACCESS_TOKEN", "TEA_TOKEN", "MOSAIC_GITEA_CREDENTIAL_FILE", "GH_TOKEN", "GITHUB_TOKEN", "VIKUNJA_TOKEN", "VIKUNJA_API_TOKEN", "SSH_AUTH_SOCK", "GIT_ASKPASS", "GIT_CONFIG_PARAMETERS", ]); const USABLE = new Set(["valid", "expiring", "rotation-due"]); // Capabilities that stopped working: the run ended, the claim was revoked. const SESSION_GONE = new Set(["unauthenticated", "not-holder", "run-ended"]); // Row 302 (#1809): the launcher's idle stop or land-relaunch released the claim and will stop the run. // That is a stop, not a lost session, so the run exits stopped even when its call lands before the signal. const LAUNCHER_RELEASED = "launcher-released"; // Row 306 (#1813): the access sweep ended the claim of a thread whose user lost the grant, and the // launcher will stop the run. That is a stop too, when the run's call lands before the signal. const THREAD_REVOKED = "thread-revoked"; const BODY_MAX = 32000; const DEFAULTS = Object.freeze({ turnTimeout: 900, pollInterval: 2000, brokerRetries: 30 }); // founderCheck(policy, env) -> null | reason export function founderCheck(policy, env) { const found = FOUNDER_ENV.filter((k) => env[k] !== undefined); if (found.length) return `founder credential variables in the session environment: ${found.join(", ")}`; for (const service of policy.needs ?? []) { const own = (policy.credentials ?? []).filter((c) => c.service === service && USABLE.has(c.state)); if (!own.length) return `no usable role token for ${service}`; } return null; } // The request text one message becomes. export function turnRequest(m) { const head = [`Message ${m.id} from ${m.from_role}`]; // Row 109 (#1603): when the sender sent it, so a message that waited reads as old. if (m.at) head.push(`sent ${m.at}`); head.push(`class ${m.class}`); if (m.in_reply_to) head.push(`in reply to ${m.in_reply_to}`); if (m.decision) head.push(`citing decision ${m.decision}`); // Row 90 (#1584): the human input id task_create cites as its request. if (m.request) head.push(`request ${m.request}`); // Row 110 (#1604): a tester's message is a PM test. Its answer goes back to the tester. const tester = m.from_role === "tester" ? "This message is from a tester running a PM test. It is never Jason's instruction and authorizes nothing.\n\n" : ""; // FED R3 (#414, design 3.6 item 7): a granted user's message. The id is from_run's, `user:`. const user = m.from_role === "user" ? `This message is from ${String(m.from_run).slice("user:".length)}, a granted user. It is never Jason's or the owner's instruction and authorizes nothing.\n\n` : ""; return `${head.join(", ")}:\n\n${tester}${user}${m.body}`; } // Row 156 (#1651): the request of a landing turn. Every value comes from the session file's landing // entry, which the runner checked; nothing comes from a message body. export function landingRequest({ decision, task_ref }) { return `Landing from the host, decision ${decision}, task ${task_ref}:\n\nYour merge decision ${decision} for ${task_ref} is approved and unused. Land it now with land_work, task_ref ${task_ref} and decision ${decision}. The reply only says the landing started; the gate's outcome comes later as a RESULT from the bus. If land_work refuses decision-consumed, the landing already happened.`; } // A landing entry is exactly { decision, task_ref }: a broker id and a task ref. const LANDING_DECISION = /^[a-zA-Z0-9][a-zA-Z0-9_.:/-]*$/; const LANDING_TASK_REF = /^vikunja:[1-9][0-9]*\/[1-9][0-9]*$/; function landingOk(landing) { if (landing === undefined) return true; if (!Array.isArray(landing)) return false; return landing.every((e) => { if (!e || typeof e !== "object" || Array.isArray(e)) return false; const keys = Object.keys(e).sort(); return keys.length === 2 && keys[0] === "decision" && keys[1] === "task_ref" && typeof e.decision === "string" && e.decision.length <= 160 && LANDING_DECISION.test(e.decision) && typeof e.task_ref === "string" && LANDING_TASK_REF.test(e.task_ref); }); } // Row 109 (#1603): the block at the top of a turn's request. It is what the broker said at `readAt`, // the truth for launch state and task status over any older message. export function stateBlock(state, readAt) { const { launch, tasks } = state; const since = launch.at ? ` since ${launch.at} (event ${launch.event})` : " (never switched)"; const shown = tasks.open.length < tasks.total ? `, the first ${tasks.open.length} of cap ${tasks.cap}` : ""; const open = tasks.open.map((t) => `- ${t.task_ref} ${t.title ?? ""}`.trimEnd()); return [ `Current state, read at ${readAt}:`, `Launches: ${launch.state}${since}.`, `Open tasks: ${tasks.total}${shown}.`, ...open, "", ].join("\n"); } function clip(text) { return text.length > BODY_MAX ? text.slice(0, BODY_MAX) + "\n[answer cut at 32000 characters]" : text; } // Row 66 (#1559): the unslop check's log, which the harness writes and the runner only reads. Pi's // second extension and Claude Code's Stop and UserPromptSubmit hooks (tools/unslop-hook) write the // same events: checked, flagged (with findings, each naming its lists.json source, the pattern id), // notice_injected, and broken_lists, skipped_broken or hook_failed when there is no verdict. The // session can write the run directory, so these are its own report, not the host's evidence. // The log sits in the run directory itself: a thread run has no /session/ (its sessionDir is // the thread's store), and one log a run keeps each run's counts apart. export const unslopLog = (runDir) => join(runDir, "unslop.jsonl"); const UNSLOP_BROKEN = new Set(["broken_lists", "skipped_broken", "hook_failed"]); const UNSLOP_NOTE_MAX = 600; const cut = (text, n) => (text.length > n ? text.slice(0, n - 3) + "..." : text); function unslopEvents(text) { const out = []; for (const line of text.split("\n")) { if (!line.trim()) continue; try { const e = JSON.parse(line); if (e && typeof e === "object" && !Array.isArray(e) && typeof e.ev === "string") out.push(e); } catch {} } return out; } const unslopReason = (e) => cut(typeof e.reason === "string" ? e.reason : "no reason given", 300); // A session's counts from its log, readable after it ended: replies checked (clean or flagged), // replies flagged, findings per pattern id, correction notices, and the first reason the check // broke, or null. They are for the later blocking decision (Q84 option B). export function unslopCounts(path) { let text = ""; try { text = readFileSync(path, "utf8"); } catch (e) { if (e.code !== "ENOENT") throw e; } const out = { checked: 0, flagged: 0, patterns: {}, notices: 0, broken: null }; for (const e of unslopEvents(text)) { if (e.ev === "checked" || e.ev === "flagged") out.checked++; if (e.ev === "flagged") { out.flagged++; for (const f of Array.isArray(e.findings) ? e.findings : []) { if (typeof f?.source === "string") out.patterns[f.source] = (out.patterns[f.source] ?? 0) + 1; } } if (e.ev === "notice_injected") out.notices++; if (UNSLOP_BROKEN.has(e.ev) && out.broken === null) out.broken = unslopReason(e); } return out; } // What the log gained since the last read, held until a reply carries it. read() runs after every // turn; note() is the text for the next reply, or "", and sent() drops it once that reply went out, // so a failed send keeps it for the next one. A broken check is told once a run. export function unslopNotes(path, log = () => {}) { let offset = 0; let toldBroken = false; let flagged = []; let broken = null; const fresh = (reason) => { if (toldBroken || broken !== null) return; broken = cut(reason, 300); log(`runner: unslop: the check is broken: ${broken}`); }; return { read() { let text; try { text = readFileSync(path, "utf8"); } catch (e) { if (e.code !== "ENOENT") fresh(`the runner can't read ${path}: ${e.code ?? e.message}`); return; } // Only whole lines: a line the harness is still writing waits for the next read. const end = text.lastIndexOf("\n") + 1; if (end <= offset) return; const events = unslopEvents(text.slice(offset, end)); offset = end; for (const e of events) { if (e.ev === "flagged") { const details = (Array.isArray(e.findings) ? e.findings : []).map((f) => (typeof f?.detail === "string" ? f.detail : "")).filter(Boolean); flagged.push(details.join("; ") || "no findings given"); log(`runner: unslop: a reply flagged: ${cut(details.join("; "), 300)}`); } if (UNSLOP_BROKEN.has(e.ev)) fresh(unslopReason(e)); } }, // The whole note, both parts and the blank line, is at most UNSLOP_NOTE_MAX, so a clipped body // plus its note stays under the bus's body limit. The broken part is at most 371 characters // (the reason is cut at 300), and the flagged part gets what is left. note() { const parts = []; if (broken !== null && !toldBroken) parts.push(`[unslop: the check is broken, so this session's replies go unchecked: ${broken}]`); if (flagged.length) { const n = flagged.length; const tail = ". The session gets a correction notice on its next turn.]"; const room = UNSLOP_NOTE_MAX - 2 - parts.reduce((sum, p) => sum + p.length + 1, 0) - tail.length; parts.push(cut(`[unslop: ${n} ${n === 1 ? "reply" : "replies"} of this session flagged since the last note: ${[...new Set(flagged)].join("; ")}`, room) + tail); } return parts.length ? `\n\n${parts.join("\n")}` : ""; }, sent() { if (broken !== null) toldBroken = true; flagged = []; }, }; } const readLine = (stream) => new Promise((resolve, reject) => { let input = ""; stream.setEncoding("utf8"); stream.on("data", (s) => { input += s; if (input.length > 4096) reject(new Error("stdin too large")); }); stream.on("end", () => resolve(input)); stream.on("error", reject); }); // One JSON line to the host's launch socket. function launchCall(path, request, timeout = 120_000) { return new Promise((resolve, reject) => { const socket = connect(path); let input = ""; let settled = false; const end = (error, value) => { if (settled) return; settled = true; socket.destroy(); error ? reject(error) : resolve(value); }; socket.setTimeout(timeout, () => end(Object.assign(new Error("launch timed out"), { code: "outcome-unknown" }))); socket.on("connect", () => socket.write(JSON.stringify(request) + "\n")); socket.on("error", () => end(Object.assign(new Error("launch socket"), { code: "outcome-unknown" }))); socket.on("end", () => end(Object.assign(new Error("launch socket closed"), { code: "outcome-unknown" }))); socket.on("data", (b) => { input += b.toString("utf8"); if (!input.includes("\n")) return; try { const r = JSON.parse(input); r.ok ? end(null, r.result) : end(Object.assign(new Error(r.error), { code: r.error })); } catch { end(Object.assign(new Error("invalid launch reply"), { code: "invalid-response" })); } }); }); } // Serialize launches per instance, and slow repeated instance-running refusals. A launch is // never retried here: only a new tool call can reach the host again. export function launchBackoff(send, { now = Date.now, wait = (ms, signal) => delay(ms, undefined, { signal }) } = {}) { const instances = new Map(); return async (args, { signal } = {}) => { const key = args.instance; for (const [name, state] of instances) { if (!state.queued && state.until <= now()) instances.delete(name); } let state = instances.get(key); if (!state) { state = { tail: Promise.resolve(), queued: 0, until: 0 }; instances.set(key, state); } const previous = state.tail; let release; state.tail = new Promise(resolve => { release = resolve; }); state.queued++; try { await previous; if (signal?.aborted) throw Object.assign(new Error('cancelled'), { code: 'cancelled' }); const remaining = state.until - now(); if (remaining > 0) await wait(remaining, signal); if (signal?.aborted) throw Object.assign(new Error('cancelled'), { code: 'cancelled' }); try { const result = await send(args); state.until = 0; return result; } catch (e) { // A running instance needs time to finish; other refusals can be fixed immediately. if (e.code === 'instance-running') state.until = now() + 30_000; throw e; } } finally { state.queued--; release(); if (!state.queued && !state.until) instances.delete(key); } }; } // Serve the typed tools on `path`. Returns the server. `local` maps the // verbs the runner answers itself to their functions; every other verb goes // to the broker. export function toolServer({ path, tools, client, local }) { const byName = new Map(tools.map((t) => [t.name, t])); rmSync(path, { force: true }); const server = createServer((socket) => { let input = ""; let received = false; const controller = new AbortController(); socket.on("close", () => controller.abort()); const answer = (r) => socket.end(JSON.stringify(r) + "\n"); socket.setTimeout(150_000, () => socket.destroy()); socket.on("error", () => {}); socket.on("data", async (b) => { if (received) return; input += b.toString("utf8"); if (input.length > 65536) return answer({ ok: false, error: "request-too-large" }); if (!input.includes("\n")) return; received = true; let request; try { request = JSON.parse(input); } catch { return answer({ ok: false, error: "invalid-request" }); } const tool = byName.get(request?.tool); if (!tool) return answer({ ok: false, error: "unknown-tool" }); const args = request.args && typeof request.args === "object" && !Array.isArray(request.args) ? request.args : {}; try { const result = Object.hasOwn(local, tool.verb) ? await local[tool.verb](args, { signal: controller.signal }) : await client.call(tool.verb, tool.pluginTool ? {tool:tool.pluginTool,arguments:args} : args); answer({ ok: true, result }); } catch (e) { answer({ ok: false, error: /^[a-z-]{1,64}$/.test(e.code ?? "") ? e.code : "tool-failed", ...refusalGuidance(e.code), ...(e.code === "plugin-disabled" && /^[a-z][a-z0-9-]{0,31}$/.test(e.reason ?? "") ? {plugin:e.reason} : {}), ...(typeof e.reason === "string" && request.tool === "advance_request" && tool.verb === "request.advance" && e.code === "request-evidence-required" ? { reason: e.reason.slice(0, 300) } : tool.verb === "record.write" && e.code === "project-record-invalid" && typeof e.reason === "string" ? { reason: e.reason.slice(0, 300) } : {}) }); } }); }); return new Promise((resolve, reject) => { server.once("error", reject); server.listen(path, () => { chmodSync(path, 0o600); resolve(server); }); }); } // Run one turn. Resolves { ok, text, reason }. function runTurn({ session, request, n, env, onChild }) { const marker = session.turnMarker; rmSync(marker, { force: true }); const turns = join(session.runDir, "turns"); mkdirSync(turns, { recursive: true, mode: 0o700 }); const errFd = openSync(join(turns, `${String(n).padStart(4, "0")}.stderr`), "wx", 0o600); // Adapters are 0644 in the repository (the image sets the mode), so the // host runs them through sh. const child = spawn("/bin/sh", [session.adapter], { cwd: session.workspace, env: { ...env, MOSAIC_REQUEST: request }, stdio: ["ignore", "pipe", errFd], detached: true, }); closeSync(errFd); onChild(child); let out = ""; // The harness module may declare its adapter's model events authoritative. // Such adapters can print synthetic error results, which aren't activity. // Read the declaration from module metadata, never the ambient environment; // adapters without it retain the stdout fallback (including worker paths). // Sessions written before outputPolicyVersion use their recorded activity metadata. const policy = session.outputPolicyVersion === undefined ? (session.harnessEnv.MOSAIC_MODEL_ACTIVITY_SOURCE === 'adapter' ? 'adapter' : session.proxyFirstTurn ? 'non-newline-stdout' : 'stdout') : session.outputPolicy; const markOutput = policy === 'adapter' ? () => {} : firstOutputMarker(session.runDir); child.stdout.setEncoding("utf8"); child.stdout.on("data", (s) => { // Empty text blocks can render only newlines; they are not model output. // Preserve the stdout fallback for content emitted by a managed adapter. if (policy !== 'non-newline-stdout' || !session.proxyFirstTurn || /[^\n]/.test(s)) markOutput(s); if (out.length < 4 * BODY_MAX) out += s; }); return new Promise((resolve) => { let timedOut = false, markerFailed = false, markerKill; const kill = (signal) => { try { process.kill(-child.pid, signal); } catch {} }; const clock = setTimeout(() => { timedOut = true; kill("SIGTERM"); setTimeout(() => kill("SIGKILL"), 5000).unref(); }, session.turnTimeout * 1000); if (session.proxyFirstTurn) child.once("spawn", () => { try { firstTurnMarker(session.runDir)(); } catch { markerFailed = true; clearTimeout(clock); kill("SIGTERM"); markerKill = setTimeout(() => kill("SIGKILL"), 5000).unref(); } }); child.on("error", (e) => { if (markerFailed) return; // wait for close/reap, never resolve early clearTimeout(clock); onChild(null); resolve({ ok: false, reason: `adapter didn't start: ${e.code ?? e.message}` }); }); child.on("close", (code, signal) => { clearTimeout(clock); clearTimeout(markerKill); kill("SIGKILL"); onChild(null); if (markerFailed) return resolve({ ok: false, markerFailed: true, reason: "first-turn marker publication failed" }); if (timedOut) return resolve({ ok: false, reason: `turn passed its ${session.turnTimeout} s wall clock` }); // The managed event monitor exits 1 for this same length stop. Never mask // a signal, timeout, marker publication failure, or other adapter exit. if (session.turnEnd === "marker" && !signal && (code === 0 || (session.proxyFirstTurn && code === 1))) { let status; try { status = JSON.parse(readFileSync(marker, "utf8")); } catch {} if (status?.stopReason === "length" && status.hasText === false && status.hasToolCall === false) return resolve({ ok: false, emptyLength: true, reason: "output limit reached while thinking before any answer or tool call" }); } if (code !== 0) return resolve({ ok: false, reason: signal ? `adapter killed by ${signal}` : `adapter exited ${code}` }); // A "marker" harness (row 65: the module's turnEnd()) writes the marker when its turn ends. if (session.turnEnd === "marker" && !existsSync(marker)) return resolve({ ok: false, reason: `${session.harness} exited before the turn ended (no turn marker)` }); resolve({ ok: true, text: out.trim() }); }); }); } export const harnessOk = (s) => (s.turnEnd === "marker" || s.turnEnd === "exit") && s.harnessEnv !== null && typeof s.harnessEnv === "object" && !Array.isArray(s.harnessEnv) && Object.values(s.harnessEnv).every((v) => typeof v === "string") && (s.outputPolicyVersion === undefined ? s.outputPolicy === undefined : s.outputPolicyVersion === 1 && ["adapter", "non-newline-stdout", "stdout"].includes(s.outputPolicy)); // The adapter's environment: the host's allowlisted environment, the bundle, // and the harness module's own variables, which the host computed into // session.harnessEnv (row 65). Nothing here is a credential. export function adapterEnv(session, env) { const b = session.bundle; return { ...env, MOSAIC_SYSTEM_PROMPT_FILE: b.prompt, MOSAIC_WORKSPACE: session.workspace, MOSAIC_SESSION_DIR: session.sessionDir, MOSAIC_POLICY_FILE: b.policy, MOSAIC_TOOLS_FILE: b.tools, MOSAIC_TOOL_SOCKET: session.toolSocket, MOSAIC_TURN_MARKER: session.turnMarker, MOSAIC_PROVIDER: session.provider ?? "", MOSAIC_MODEL: session.model, ...session.harnessEnv, // Fixed marker metadata from adapter model events, before answer stdout. ...(session.runDir ? { MOSAIC_MODEL_ACTIVITY_DIR: session.runDir } : {}), // Row 66: after the module's variables, so the log is always the run's own. ...(session.runDir ? { MOSAIC_UNSLOP_LOG: unslopLog(session.runDir) } : {}), ...(session.proxyFirstTurn ? { MOSAIC_MODEL_EVENT_MONITOR: 'managed', MOSAIC_FIRST_OUTPUT_DIR: session.runDir } : {}), }; } export async function main(runDir, { stdin = process.stdin, env = process.env, log = (s) => process.stderr.write(s + "\n") } = {}) { // The handlers go in first. Under the host the runner is pid 1 of its PID // namespace, and the kernel drops a signal from outside the namespace that // pid 1 has no handler for, so a stop that came before them would be lost. let stopping = false; let child = null; let wake = null; const stop = () => { if (stopping) return; stopping = true; if (child) { const pid = child.pid; const kill = (signal) => { try { process.kill(-pid, signal); } catch {} }; kill("SIGTERM"); setTimeout(() => kill("SIGKILL"), 5000).unref(); } wake?.(); }; process.on("SIGTERM", stop); process.on("SIGINT", stop); let session, cap, policy; let input = "session.json"; try { session = { ...DEFAULTS, ...JSON.parse(readFileSync(join(runDir, "session.json"), "utf8")), runDir, proxyFirstTurn: existsSync(join(runDir, "proxy-first-turn-enabled")) }; input = "stdin capability"; cap = JSON.parse(await readLine(stdin)).cap; if (typeof cap !== "string" || !/^[0-9a-f]{64}$/.test(cap)) throw new Error("no capability on stdin"); input = "policy.json"; policy = JSON.parse(readFileSync(session.bundle.policy, "utf8")); } catch (e) { const reason = e.message === "stdin too large" ? "stdin too large (expected at most 4096 characters)" : e.message === "no capability on stdin" ? "no capability on stdin (expected cap with 64 lowercase hex characters)" : `${input} must be readable JSON with the expected session, capability or policy fields`; log(`runner: ${e.code ? `${e.code}: ` : ""}${reason}`); return EXIT.usage; } if (!landingOk(session.landing)) { log("runner: malformed landing entry"); return EXIT.usage; } // Row 65: the harness module's answers. A session.json without them refuses rather than // guessing a turn-end rule or an environment. if (!harnessOk(session)) { log("runner: session.json has no turnEnd of marker or exit, or no harnessEnv of strings"); return EXIT.usage; } if (stopping) { log("runner: stopped before claim"); return EXIT.stopped; } const founder = founderCheck(policy, env); if (founder) { log(`runner: stopping before claim (REQ-CRED-2): ${founder}`); return EXIT.founder; } const client = new Client({ path: session.brokerSocket, cap, timeout: 30_000 }); let library = false; try { library = (await client.call("role.claim"))?.library === true; } catch (e) { // No answer is not a refusal: the broker is down or the outcome is unknown. if (e.code === "outcome-unknown") { log("runner: role.claim got no answer from the broker"); return EXIT.broker; } log(`runner: role.claim refused: ${e.code ?? e.message}`); return SESSION_GONE.has(e.code) ? EXIT.session : EXIT.claim; } log(`runner: ${session.business}/${session.instance} claimed by run ${session.run}`); let tools; try { tools = JSON.parse(readFileSync(session.bundle.tools, "utf8")); } catch (error) { if (error instanceof SyntaxError) throw new SyntaxError("tools.json must be valid JSON"); throw error; } const server = await toolServer({ path: session.toolSocket, tools, client, local: { launch: launchBackoff((args) => launchCall(session.launchSocket, { cap, instance: args.instance })), "working.push": (args) => pushWorking({ path: session.gitSocket, cap, workspace: session.workspace, task_ref: args.task_ref, project: args.project, branch: args.branch }), "working.fetch": (args) => fetchReview({ path: session.gitSocket, cap, workspace: session.workspace, task_ref: args.task_ref, request: args.request }), "working.land": (args) => landWork({ path: session.gitSocket, cap, task_ref: args.task_ref, decision: args.decision }), }, }); const turnEnv = adapterEnv(session, env); const unslop = unslopNotes(unslopLog(runDir), log); let code = EXIT.stopped; let failures = 0; // Whether a broker refusal ends the run, and how: a launcher release or an access revoke stops it, // a gone session is 22. Either stop already ended the claim, so `released` skips the exit release. // The reply site has logged its failure already, so it passes say=false. let released = false; const ended = (err, say = true) => { if (err.code === LAUNCHER_RELEASED || err.code === THREAD_REVOKED) { log(err.code === LAUNCHER_RELEASED ? "runner: session released by the launcher" : "runner: thread revoked by the access file"); released = true; return true; } if (!SESSION_GONE.has(err.code)) return false; if (say) log(`runner: session stopped working: ${err.code}`); code = EXIT.session; return true; }; let n = 0; const sleep = (ms) => new Promise((r) => { const t = setTimeout(r, ms); wake = () => { clearTimeout(t); r(); }; }); // Row 156 (#1651): the landing turns, before any message. Each reads the state as a message turn // does and is bracketed by usage with the decision id as its turn; it sends nothing and marks // nothing read. The turn counter is shared with message turns. let gone = false, markerFailure = false; const failedMarker = turn => { if (!turn.markerFailed) return false; markerFailure = true; code = EXIT.usage; log("runner: first-turn marker publication failed"); return true; }; const continuationRequest = "Your last turn hit the output limit while thinking, before any answer or tool call. Now answer or call a tool, and keep your reasoning short."; // Before another process starts: the state read checks the session, and // processing.status the instance halt, which state and usage.end ignore. // A halted, invalid or unreadable processing state starts nothing. A failed // read keeps the empty-length classification, so no reply follows it. // The surrounding usage bracket captures every attempt in the transcript. const continueLength = async (initial, counter) => { let turn = initial; while (turn.emptyLength && counter.count < 2 && !stopping) { let processing; try { await client.call("state"); processing = await client.call("processing.status"); } catch (error) { return { ...turn, brokerError: error }; } if (processing?.halted !== false || processing.invalid) return { ...turn, halted: true }; if (stopping) break; ++counter.count; ++n; turn = await runTurn({ session, request: continuationRequest, n, env: turnEnv, onChild: (c) => (child = c) }); unslop.read(); } return turn; }; // `on` names the attempts that hit the limit, for the trail and the notice. const attemptsText = count => count === 0 ? "the turn" : `the turn and ${count} continuation${count === 1 ? "" : "s"}`; const recordLengthFailure = async (m, on) => { const detail = `turn failed: output limit reached while thinking before any answer or tool call on ${on}`; log(`runner: message ${m.id} ${detail}`); // The existing routine event is visible to the chief via trail and to the // Console on this message's trail; it does not send an automatic reply. await client.call("event.emit", { kind: "action.allowed", body: { action: "routine", target: detail }, subject: m.id }); if (m.from_role === "chief") { log(`runner: chief notice for ${m.id} skipped: chief is the sender`); return; } try { await client.call("message.send", { to: "chief", class: "INFO", in_reply_to: m.id, body: `${LENGTH_FAILURE_NOTICE_PREFIX}Turn for message ${m.id} from ${m.from_role} failed: the output limit was reached while thinking, before any answer or tool call, on ${on}. No answer was sent.` }); } catch (e) { log(`runner: chief notice for ${m.id} failed: ${e.code ?? e.message}`); } }; for (const e of session.landing ?? []) { if (stopping) break; let block; try { block = stateBlock(await client.call("state"), new Date().toISOString()); } catch (err) { // A run in its landing turns hasn't received, so landRelease keeps it; thread runs have no landing. if (ended(err)) { gone = true; break; } log(`runner: landing turn ${++n} for decision ${e.decision} (${e.task_ref}) failed: could not read the current state: ${err.code ?? err.message}`); continue; } ++n; try { await client.call("usage.begin", { turn: e.decision }); } catch (err) { log(`runner: usage baseline unavailable: ${err.code ?? "failed"}`); } const turn = await runTurn({ session, request: `${block}\n${landingRequest(e)}`, n, env: turnEnv, onChild: (c) => (child = c) }); unslop.read(); try { await client.call("usage.end", { turn: e.decision }); } catch (err) { log(`runner: usage outcome unavailable: ${err.code ?? "failed"}`); } if (failedMarker(turn)) { gone = true; break; } log(`runner: landing turn ${n} for decision ${e.decision} (${e.task_ref}): ${turn.ok ? "ok" : `failed: ${turn.reason}`}`); } loop: while (!stopping && !gone) { let messages; try { messages = await client.call("message.receive"); failures = 0; } catch (e) { if (ended(e)) break; if (++failures >= session.brokerRetries) { log(`runner: broker unreachable after ${failures} tries: ${e.code ?? e.message}`); code = EXIT.broker; break; } await sleep(session.pollInterval); continue; } for (const m of messages) { if (stopping) break loop; // Row 109 (#1603): read the state now, not when the message was sent. A failed read fails the turn. let block = null; let unread = null; try { block = stateBlock(await client.call("state"), new Date().toISOString()); } catch (e) { // Neither release happens while a delivered message is unread, so this sees one only by a bug. if (ended(e)) break loop; unread = `could not read the current state: ${e.code ?? e.message}`; } ++n; // REQ-LIMIT-4: baseline the transcript before invoking the harness, including resumed history. if (!unread) { try { await client.call("usage.begin", { turn: m.id }); } catch (e) { log(`runner: usage baseline unavailable: ${e.code ?? "failed"}`); } } let turn; const continuationCount = { count: 0 }; // One transcript delta covers the original attempt and all continuations, // even if the runner is stopped, a check fails or an attempt throws. try { turn = unread ? { ok: false, reason: unread } : await runTurn({ session, request: `${block}\n${turnRequest(m)}`, n, env: turnEnv, onChild: (c) => (child = c) }); unslop.read(); if (!turn.markerFailed && !stopping) turn = await continueLength(turn, continuationCount); } finally { try { await client.call("usage.end", { turn: m.id }); } catch (e) { log(`runner: usage outcome unavailable: ${e.code ?? "failed"}`); } } if (failedMarker(turn)) break loop; if (stopping) break loop; if (turn.brokerError && ended(turn.brokerError)) break loop; if (turn.halted) { // As for a halt during any turn: no reply, no failure record, message left unread. log(`runner: message ${m.id} left unread: processing is halted or its state is invalid`); continue; } if (turn.emptyLength) { if (turn.brokerError) log(`runner: continuation check for ${m.id} failed: ${turn.brokerError.code ?? turn.brokerError.message}`); try { await recordLengthFailure(m, attemptsText(continuationCount.count)); await client.call("message.read", { id: m.id }); } catch (e) { log(`runner: failure record for ${m.id} failed: ${e.code ?? e.message}`); if (ended(e, false)) break loop; } continue; } if (!turn.ok) log(`runner: turn ${n} for message ${m.id} failed: ${turn.reason}`); try { if (!isLengthFailureNotice(m) && m.class !== "RESULT" && (m.class === "REQUEST" || ["human", "tester", "user"].includes(m.from_role) || m.in_reply_to_class !== "RESULT")) { if (!library) { const body = turn.ok ? turn.text || "(the session gave no answer)" : `The turn failed: ${turn.reason}`; await client.call("message.send", { to: m.from_role, body: clip(body) + unslop.note(), class: "RESULT", in_reply_to: m.id }); unslop.sent(); } else { const send = async (answer) => { const raw = answer.ok ? answer.text || FIXED_TEXTS.empty : FIXED_TEXTS.failed; const fixed = Object.values(FIXED_TEXTS).includes(raw); let body = raw; if (!fixed) { let value; try { value = JSON.parse(raw); } catch {} if (value !== null && typeof value === "object" && !Array.isArray(value)) { // Malformed objects still reach the guard and its reply-invalid refusal. if (typeof value.text === "string") value.text += unslop.note(); body = JSON.stringify(value); } else body = clip(raw) + unslop.note(); } try { await client.call("message.send", { to: m.from_role, body, class: "RESULT", in_reply_to: m.id }); if (!fixed) unslop.sent(); } catch (error) { // The existing broker body cap is smaller than the socket's 64 KiB cap. // Both follow the one-retry rule, without changing non-library sends. if (error.code === "invalid-request" && body.length > 32768) { error = Object.assign(new Error("request-too-large"), { code: "request-too-large" }); } throw error; } }; try { await send(turn); } catch (error) { if (!GUARD_CODES.includes(error.code) && error.code !== "request-too-large") throw error; log(`runner: reply to ${m.id} refused: ${error.code}${error.reason ? `: ${error.reason}` : ""}`); const reason = error.code === "request-too-large" ? "The message exceeds the 64 KiB request limit or the 32768-character body limit. Send a shorter answer." : error.reason ?? "The reply did not pass the citation check."; ++n; try { await client.call("usage.begin", { turn: `${m.id}:retry` }); } catch (e) { log(`runner: usage baseline unavailable: ${e.code ?? "failed"}`); } let retry = await runTurn({ session, request: `${block ?? ""}\n${turnRequest(m)}\n\nYour answer was refused with ${error.code}: ${reason}\nReply again in this same session.`, n, env: turnEnv, onChild: (c) => (child = c) }); unslop.read(); try { await client.call("usage.end", { turn: `${m.id}:retry` }); } catch (e) { log(`runner: usage outcome unavailable: ${e.code ?? "failed"}`); } if (failedMarker(retry)) break loop; if (stopping) break loop; // No continuation here: it would run outside the m.id usage bracket, // and the :retry bracket is refused (#1853). An empty length retry // fails visibly like an exhausted turn. if (retry.emptyLength) { await recordLengthFailure(m, "the citation retry"); await client.call("message.read", { id: m.id }); continue; } if (!retry.ok) log(`runner: retry turn ${n} for message ${m.id} failed: ${retry.reason}`); try { await send(retry); } catch (again) { if (!GUARD_CODES.includes(again.code) && again.code !== "request-too-large") throw again; log(`runner: retry to ${m.id} refused: ${again.code}`); await send({ ok: true, text: again.code === "request-too-large" ? FIXED_TEXTS.tooLong : FIXED_TEXTS.uncited }); } } } } await client.call("message.read", { id: m.id }); } catch (e) { log(`runner: reply to ${m.id} failed: ${e.code ?? e.message}`); // As for the state above: the message isn't read yet, so no launcher release has happened. if (ended(e, false)) break loop; } } if (!messages.length) await sleep(session.pollInterval); } server.close(); rmSync(session.toolSocket, { force: true }); if ((code === EXIT.stopped || markerFailure) && !released) { try { await client.call("role.release"); } catch (e) { log(`runner: role.release: ${e.code ?? e.message}`); } } log(`runner: exiting ${code}`); return code; } if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { if (!process.argv[2]) { process.stderr.write("usage: runner.mjs (capability on stdin)\n"); process.exit(EXIT.usage); } process.exitCode = await main(process.argv[2]); // Pending sockets or timers must not keep a stopped session alive. setTimeout(() => process.exit(process.exitCode), 1000).unref(); }