Skip to content

The open-source app everyone uses to manage agents at work

codex_local adapter output inactivity monitor (#5017)

Neeraj Kumar Singh B7aa212296eb1
diff --git a/packages/adapters/codex-local/src/index.ts b/packages/adapters/codex-local/src/index.ts
index 944d03691c..b2e9df7a92 100644
--- a/packages/adapters/codex-local/src/index.ts
+++ b/packages/adapters/codex-local/src/index.ts
@@ -87,6 +87,7 @@ Core fields:
Operational fields:
- timeoutSec (number, optional): run timeout in seconds
- graceSec (number, optional): SIGTERM grace period in seconds
+- outputInactivityTimeoutMs (number | null, optional): inactivity monitor around the codex child. Resets on every parsed JSONL event from stdout. Defaults to 7 * 60_000 ms when unset or non-positive. Set to \`null\` to disable the monitor entirely (only do this for known-slow tasks; the platform-level 1h silent-run safety net still applies). On fire, the adapter sends SIGTERM to the process group, waits 5s, then SIGKILL, and surfaces the run as failed with errorMessage "monitor: no codex output for {N}m {S}s".
Notes:
- Prompts are piped via stdin (Codex receives "-" prompt argument).
diff --git a/packages/adapters/codex-local/src/server/execute.ts b/packages/adapters/codex-local/src/server/execute.ts
index e3c38d4963..fc9e84171b 100644
--- a/packages/adapters/codex-local/src/server/execute.ts
+++ b/packages/adapters/codex-local/src/server/execute.ts
@@ -57,6 +57,12 @@ import { prepareCodexRuntimeConfig } from "./runtime-config.js";
import { resolveCodexDesiredSkillNames } from "./skills.js";
import { buildCodexExecArgs } from "./codex-args.js";
import { SANDBOX_INSTALL_COMMAND } from "../index.js";
+import {
+ CODEX_OUTPUT_INACTIVITY_MONITOR_SIGTERM_GRACE_MS,
+ createCodexOutputInactivityMonitor,
+ formatOutputInactivityMonitorErrorMessage,
+ resolveCodexInactivityTimeout,
+} from "./output-inactivity-monitor.js";
const __moduleDir = path.dirname(fileURLToPath(import.meta.url));
const CODEX_ROLLOUT_NOISE_RE =
@@ -86,6 +92,29 @@ function firstNonEmptyLine(text: string): string {
);
}
+function signalCodexChild(
+ target: { pid: number | null; processGroupId: number | null },
+ signal: NodeJS.Signals,
+): boolean {
+ if (process.platform !== "win32" && target.processGroupId && target.processGroupId > 0) {
+ try {
+ process.kill(-target.processGroupId, signal);
+ return true;
+ } catch {
+ // Fall back to direct child signal if group signaling fails (e.g. group already gone).
+ }
+ }
+ if (target.pid && target.pid > 0) {
+ try {
+ process.kill(target.pid, signal);
+ return true;
+ } catch {
+ return false;
+ }
+ }
+ return false;
+}
+
function hasNonEmptyEnvValue(env: Record<string, string>, key: string): boolean {
const raw = env[key];
return typeof raw === "string" && raw.trim().length > 0;
@@ -585,6 +614,18 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
resolvedCommand,
});
+ const monitorResolution = resolveCodexInactivityTimeout(config.outputInactivityTimeoutMs);
+ if (monitorResolution.mode === "disabled") {
+ await onLog(
+ "stdout",
+ `[paperclip] Codex output inactivity monitor is DISABLED via adapterConfig.outputInactivityTimeoutMs=null. Hung codex runs will only be detected by the platform-level silent-run safety net.\n`,
+ );
+ } else if (monitorResolution.mode === "default" && "reason" in monitorResolution) {
+ await onLog(
+ "stdout",
+ `[paperclip] Ignoring non-positive adapterConfig.outputInactivityTimeoutMs; falling back to default ${monitorResolution.timeoutMs}ms.\n`,
+ );
+ }
const runtimeSessionParams = parseObject(runtime.sessionParams);
const runtimeSessionId = asString(runtimeSessionParams.sessionId, runtime.sessionId ?? "");
const runtimeSessionCwd = asString(runtimeSessionParams.cwd, "");
@@ -766,39 +807,153 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
});
}
- const proc = await runAdapterExecutionTargetProcess(runId, runtimeExecutionTarget, command, args, {
- cwd,
- env,
- stdin: prompt,
- timeoutSec,
- graceSec,
- onSpawn,
- onLog: async (stream, chunk) => {
- if (stream !== "stderr") {
- await onLog(stream, chunk);
- return;
- }
- const cleaned = stripCodexRolloutNoise(chunk);
- if (!cleaned.trim()) return;
- await onLog(stream, cleaned);
- },
- });
- const cleanedStderr = stripCodexRolloutNoise(proc.stderr);
- return {
- proc: {
- ...proc,
- stderr: cleanedStderr,
- },
- rawStderr: proc.stderr,
- parsed: parseCodexJsonl(proc.stdout),
+ let monitorFired = false;
+ let monitorTerminationSignal: NodeJS.Signals | null = null;
+ let monitorElapsedMs = 0;
+ let monitorTimeoutMs = 0;
+ let killTarget: { pid: number | null; processGroupId: number | null } | null = null;
+ let sigkillTimer: ReturnType<typeof setTimeout> | null = null;
+ let monitorLogPromise: Promise<unknown> | null = null;
+
+ const monitor =
+ monitorResolution.mode === "disabled"
+ ? null
+ : createCodexOutputInactivityMonitor({
+ timeoutMs: monitorResolution.timeoutMs,
+ onFire: (state) => {
+ monitorFired = true;
+ monitorElapsedMs = (state.firedAt ?? Date.now()) - state.lastEventAt;
+ monitorTimeoutMs = monitorResolution.timeoutMs;
+ const message = formatOutputInactivityMonitorErrorMessage(monitorElapsedMs);
+ const elapsedSec = Math.round(monitorElapsedMs / 1000);
+ const timeoutSecLabel = Math.round(monitorResolution.timeoutMs / 1000);
+ const logLine =
+ `[paperclip] adapter.invoke ${message}; ` +
+ `timeoutMs=${monitorResolution.timeoutMs} elapsedSinceLastEventMs=${monitorElapsedMs} ` +
+ `parsedEvents=${state.parsedEventCount} (timeout=${timeoutSecLabel}s elapsed=${elapsedSec}s); ` +
+ `terminating codex child via SIGTERM (5s grace, then SIGKILL).\n`;
+ // Issue the log without awaiting on the kill hot path, but capture
+ // the promise so the surrounding try/finally can await flush before
+ // the run resolves. Without this the diagnostic that explains the
+ // kill could be dropped if the child exits faster than onLog flushes.
+ monitorLogPromise = Promise.resolve(onLog("stderr", logLine)).catch(() => {});
+ const target = killTarget;
+ if (!target || (target.pid == null && target.processGroupId == null)) {
+ return;
+ }
+ const sentSig = signalCodexChild(target, "SIGTERM");
+ if (sentSig) monitorTerminationSignal = "SIGTERM";
+ sigkillTimer = setTimeout(() => {
+ sigkillTimer = null;
+ const stillSent = signalCodexChild(target, "SIGKILL");
+ if (stillSent) monitorTerminationSignal = "SIGKILL";
+ }, CODEX_OUTPUT_INACTIVITY_MONITOR_SIGTERM_GRACE_MS);
+ if (typeof (sigkillTimer as { unref?: () => void }).unref === "function") {
+ (sigkillTimer as { unref: () => void }).unref();
+ }
+ },
+ });
+
+ const wrappedOnSpawn = async (meta: { pid: number; processGroupId: number | null; startedAt: string }) => {
+ killTarget = { pid: meta.pid ?? null, processGroupId: meta.processGroupId };
+ if (onSpawn) {
+ await onSpawn(meta);
+ }
};
+
+ try {
+ const proc = await runAdapterExecutionTargetProcess(runId, runtimeExecutionTarget, command, args, {
+ cwd,
+ env,
+ stdin: prompt,
+ timeoutSec,
+ graceSec,
+ onSpawn: wrappedOnSpawn,
+ onLog: async (stream, chunk) => {
+ if (stream === "stdout") {
+ monitor?.noteStdoutChunk(chunk);
+ await onLog(stream, chunk);
+ return;
+ }
+ const cleaned = stripCodexRolloutNoise(chunk);
+ if (!cleaned.trim()) return;
+ await onLog(stream, cleaned);
+ },
+ });
+ const cleanedStderr = stripCodexRolloutNoise(proc.stderr);
+ return {
+ proc: {
+ ...proc,
+ stderr: cleanedStderr,
+ },
+ rawStderr: proc.stderr,
+ parsed: parseCodexJsonl(proc.stdout),
+ monitor: monitorFired
+ ? {
+ fired: true as const,
+ terminationSignal: monitorTerminationSignal,
+ elapsedMsSinceLastEvent: monitorElapsedMs,
+ timeoutMs: monitorTimeoutMs,
+ }
+ : { fired: false as const },
+ };
+ } finally {
+ monitor?.stop();
+ if (sigkillTimer) {
+ clearTimeout(sigkillTimer);
+ sigkillTimer = null;
+ }
+ if (monitorLogPromise) {
+ await monitorLogPromise;
+ monitorLogPromise = null;
+ }
+ }
};
const toResult = (
- attempt: { proc: { exitCode: number | null; signal: string | null; timedOut: boolean; stdout: string; stderr: string }; rawStderr: string; parsed: ReturnType<typeof parseCodexJsonl> },
+ attempt: {
+ proc: { exitCode: number | null; signal: string | null; timedOut: boolean; stdout: string; stderr: string };
+ rawStderr: string;
+ parsed: ReturnType<typeof parseCodexJsonl>;
+ monitor?:
+ | { fired: false }
+ | { fired: true; terminationSignal: NodeJS.Signals | null; elapsedMsSinceLastEvent: number; timeoutMs: number };
+ },
clearSessionOnMissingSession = false,
isRetry = false,
): AdapterExecutionResult => {
+ if (attempt.monitor?.fired) {
+ const errorMessage = formatOutputInactivityMonitorErrorMessage(attempt.monitor.elapsedMsSinceLastEvent);
+ return {
+ exitCode: null,
+ signal: attempt.monitor.terminationSignal ?? attempt.proc.signal,
+ timedOut: false,
+ errorMessage,
+ errorCode: "codex_output_inactivity_monitor",
+ errorFamily: null,
+ usage: attempt.parsed.usage,
+ sessionId: null,
+ sessionParams: null,
+ sessionDisplayId: null,
+ provider: "openai",
+ biller: resolveCodexBiller(effectiveEnv, billingType),
+ model,
+ billingType,
+ costUsd: null,
+ resultJson: {
+ stdout: attempt.proc.stdout,
+ stderr: attempt.proc.stderr,
+ outputInactivityMonitor: {
+ kind: "output_inactivity",
+ timeoutMs: attempt.monitor.timeoutMs,
+ elapsedMsSinceLastEvent: attempt.monitor.elapsedMsSinceLastEvent,
+ terminationSignal: attempt.monitor.terminationSignal,
+ },
+ },
+ summary: attempt.parsed.summary,
+ clearSession: clearSessionOnMissingSession,
+ };
+ }
if (attempt.proc.timedOut) {
return {
exitCode: attempt.proc.exitCode,
diff --git a/packages/adapters/codex-local/src/server/output-inactivity-monitor.integration.test.ts b/packages/adapters/codex-local/src/server/output-inactivity-monitor.integration.test.ts
new file mode 100644
index 0000000000..e6fadbdff6
--- /dev/null
+++ b/packages/adapters/codex-local/src/server/output-inactivity-monitor.integration.test.ts
@@ -0,0 +1,100 @@
+import { describe, expect, it } from "vitest";
+import { runChildProcess } from "@paperclipai/adapter-utils/server-utils";
+import {
+ CODEX_OUTPUT_INACTIVITY_MONITOR_SIGTERM_GRACE_MS,
+ createCodexOutputInactivityMonitor,
+ formatOutputInactivityMonitorErrorMessage,
+} from "./output-inactivity-monitor.js";
+
+const FAKE_CODEX_SCRIPT = `
+process.stdout.write(JSON.stringify({ type: "thread.started", thread_id: "abc" }) + "\\n");
+// Simulate a wedged codex: read stdin forever, never write again.
+process.stdin.resume();
+process.stdin.on("data", () => {});
+setInterval(() => {}, 60_000);
+`;
+
+describe("codex inactivity monitor (integration: real subprocess)", () => {
+ it(
+ "kills a codex child that goes silent after one event and surfaces a monitor failure",
+ async () => {
+ const runId = `monitor-integration-${Date.now()}`;
+ const timeoutMs = 250;
+ const logs: Array<{ stream: string; chunk: string }> = [];
+ let killTarget: { pid: number | null; processGroupId: number | null } | null = null;
+ let monitorFired = false;
+ let terminationSignal: NodeJS.Signals | null = null;
+ let sigkillTimer: ReturnType<typeof setTimeout> | null = null;
+ let elapsedMs = 0;
+
+ const kill = (signal: NodeJS.Signals) => {
+ const target = killTarget;
+ if (!target) return false;
+ if (target.processGroupId && target.processGroupId > 0) {
+ try {
+ process.kill(-target.processGroupId, signal);
+ return true;
+ } catch {
+ /* fall through */
+ }
+ }
+ if (target.pid && target.pid > 0) {
+ try {
+ process.kill(target.pid, signal);
+ return true;
+ } catch {
+ return false;
+ }
+ }
+ return false;
+ };
+
+ const monitor = createCodexOutputInactivityMonitor({
+ timeoutMs,
+ onFire: (state) => {
+ monitorFired = true;
+ elapsedMs = (state.firedAt ?? Date.now()) - state.lastEventAt;
+ if (kill("SIGTERM")) terminationSignal = "SIGTERM";
+ sigkillTimer = setTimeout(() => {
+ if (kill("SIGKILL")) terminationSignal = "SIGKILL";
+ }, CODEX_OUTPUT_INACTIVITY_MONITOR_SIGTERM_GRACE_MS);
+ },
+ });
+
+ try {
+ const proc = await runChildProcess(runId, process.execPath, ["-e", FAKE_CODEX_SCRIPT], {
+ cwd: process.cwd(),
+ env: process.env as Record<string, string>,
+ timeoutSec: 30,
+ graceSec: 1,
+ onSpawn: async (meta) => {
+ killTarget = { pid: meta.pid, processGroupId: meta.processGroupId };
+ },
+ onLog: async (stream, chunk) => {
+ logs.push({ stream, chunk });
+ if (stream === "stdout") {
+ monitor.noteStdoutChunk(chunk);
+ }
+ },
+ });
+
+ expect(monitorFired, "monitor should fire when codex goes silent").toBe(true);
+ // Process was killed by our signal, not by hitting timeoutSec.
+ expect(proc.timedOut).toBe(false);
+ expect(["SIGTERM", "SIGKILL"]).toContain(proc.signal);
+ expect(["SIGTERM", "SIGKILL"]).toContain(terminationSignal);
+ // The errorMessage shape mirrors the AdapterExecutionResult that
+ // execute.ts will produce for this case.
+ expect(formatOutputInactivityMonitorErrorMessage(elapsedMs)).toMatch(
+ /^monitor: no codex output for \d+m \d+s$/,
+ );
+ // We should have observed exactly one parsed JSONL event before silence.
+ expect(monitor.state().parsedEventCount).toBe(1);
+ } finally {
+ monitor.stop();
+ if (sigkillTimer) clearTimeout(sigkillTimer);
+ }
+ },
+ 15_000,
+ );
+});
diff --git a/packages/adapters/codex-local/src/server/output-inactivity-monitor.test.ts b/packages/adapters/codex-local/src/server/output-inactivity-monitor.test.ts
new file mode 100644
index 0000000000..11d6332748
--- /dev/null
+++ b/packages/adapters/codex-local/src/server/output-inactivity-monitor.test.ts
@@ -0,0 +1,260 @@
+import { describe, expect, it } from "vitest";
+import {
+ CODEX_OUTPUT_INACTIVITY_MONITOR_SIGTERM_GRACE_MS,
+ DEFAULT_CODEX_OUTPUT_INACTIVITY_TIMEOUT_MS,
+ createCodexOutputInactivityMonitor,
+ formatOutputInactivityMonitorErrorMessage,
+ resolveCodexInactivityTimeout,
+} from "./output-inactivity-monitor.js";
+
+class FakeClock {
+ private nowMs = 0;
+ private nextHandle = 1;
+ private timers = new Map<number, { fireAt: number; cb: () => void }>();
+
+ now(): number {
+ return this.nowMs;
+ }
+
+ setTimer(cb: () => void, ms: number): number {
+ const handle = this.nextHandle++;
+ this.timers.set(handle, { fireAt: this.nowMs + ms, cb });
+ return handle;
+ }
+
+ clearTimer(handle: unknown): void {
+ if (typeof handle === "number") this.timers.delete(handle);
+ }
+
+ advance(ms: number): void {
+ const targetMs = this.nowMs + ms;
+ while (true) {
+ let nextHandle: number | null = null;
+ let nextTimer: { fireAt: number; cb: () => void } | null = null;
+ for (const [h, timer] of this.timers) {
+ if (timer.fireAt <= targetMs && (!nextTimer || timer.fireAt < nextTimer.fireAt)) {
+ nextHandle = h;
+ nextTimer = timer;
+ }
+ }
+ if (!nextTimer || nextHandle == null) break;
+ this.timers.delete(nextHandle);
+ this.nowMs = nextTimer.fireAt;
+ nextTimer.cb();
+ }
+ this.nowMs = targetMs;
+ }
+
+ pendingTimerCount(): number {
+ return this.timers.size;
+ }
+}
+
+describe("resolveCodexInactivityTimeout", () => {
+ it("uses default when value is unset", () => {
+ expect(resolveCodexInactivityTimeout(undefined)).toEqual({
+ mode: "default",
+ timeoutMs: DEFAULT_CODEX_OUTPUT_INACTIVITY_TIMEOUT_MS,
+ });
+ });
+
+ it("treats explicit null as disabled", () => {
+ expect(resolveCodexInactivityTimeout(null)).toEqual({
+ mode: "disabled",
+ reason: "explicit_null",
+ });
+ });
+
+ it("returns configured value for positive numbers", () => {
+ expect(resolveCodexInactivityTimeout(12_000)).toEqual({
+ mode: "configured",
+ timeoutMs: 12_000,
+ });
+ });
+
+ it("falls back to default for non-positive numbers", () => {
+ expect(resolveCodexInactivityTimeout(0)).toEqual({
+ mode: "default",
+ timeoutMs: DEFAULT_CODEX_OUTPUT_INACTIVITY_TIMEOUT_MS,
+ reason: "non_positive",
+ });
+ expect(resolveCodexInactivityTimeout(-100)).toEqual({
+ mode: "default",
+ timeoutMs: DEFAULT_CODEX_OUTPUT_INACTIVITY_TIMEOUT_MS,
+ reason: "non_positive",
+ });
+ });
+
+ it("falls back to default for non-number, non-null values", () => {
+ expect(resolveCodexInactivityTimeout("420000")).toEqual({
+ mode: "default",
+ timeoutMs: DEFAULT_CODEX_OUTPUT_INACTIVITY_TIMEOUT_MS,
+ });
+ });
+});
+
+describe("formatOutputInactivityMonitorErrorMessage", () => {
+ it("formats minutes and seconds", () => {
+ expect(formatOutputInactivityMonitorErrorMessage(0)).toBe("monitor: no codex output for 0m 0s");
+ expect(formatOutputInactivityMonitorErrorMessage(7 * 60 * 1000)).toBe("monitor: no codex output for 7m 0s");
+ expect(formatOutputInactivityMonitorErrorMessage(7 * 60 * 1000 + 12_000)).toBe("monitor: no codex output for 7m 12s");
+ expect(formatOutputInactivityMonitorErrorMessage(45_000)).toBe("monitor: no codex output for 0m 45s");
+ });
+});
+
+describe("createCodexOutputInactivityMonitor (acceptance criteria 1: fires)", () => {
+ it("fires after timeoutMs when child emits one event then goes silent", () => {
+ const clock = new FakeClock();
+ const fires: Array<{ elapsed: number; parsedEventCount: number }> = [];
+ const monitor = createCodexOutputInactivityMonitor({
+ timeoutMs: 7 * 60 * 1000,
+ now: () => clock.now(),
+ setTimer: (cb, ms) => clock.setTimer(cb, ms),
+ clearTimer: (handle) => clock.clearTimer(handle),
+ onFire: (state) => {
+ fires.push({
+ elapsed: (state.firedAt ?? 0) - state.lastEventAt,
+ parsedEventCount: state.parsedEventCount,
+ });
+ },
+ });
+
+ // One event right after spawn.
+ clock.advance(50);
+ monitor.noteStdoutChunk('{"type":"thread.started","thread_id":"abc"}\n');
+ expect(fires).toHaveLength(0);
+ expect(monitor.state().parsedEventCount).toBe(1);
+
+ // Now go silent for 7 minutes; monitor should fire exactly at threshold.
+ clock.advance(7 * 60 * 1000 - 1);
+ expect(fires).toHaveLength(0);
+ clock.advance(1);
+ expect(fires).toHaveLength(1);
+ expect(fires[0].elapsed).toBe(7 * 60 * 1000);
+ expect(fires[0].parsedEventCount).toBe(1);
+
+ // Stopping after fire is a no-op for the timer but returns final state.
+ const finalState = monitor.stop();
+ expect(finalState.fired).toBe(true);
+ });
+
+ it("only fires once even if more silence elapses after firing", () => {
+ const clock = new FakeClock();
+ let fireCount = 0;
+ const monitor = createCodexOutputInactivityMonitor({
+ timeoutMs: 1_000,
+ now: () => clock.now(),
+ setTimer: (cb, ms) => clock.setTimer(cb, ms),
+ clearTimer: (handle) => clock.clearTimer(handle),
+ onFire: () => {
+ fireCount += 1;
+ },
+ });
+ clock.advance(2_000);
+ expect(fireCount).toBe(1);
+ clock.advance(10_000);
+ expect(fireCount).toBe(1);
+ monitor.stop();
+ });
+
+ it("ignores non-JSON lines when resetting the timer", () => {
+ const clock = new FakeClock();
+ let fireCount = 0;
+ const monitor = createCodexOutputInactivityMonitor({
+ timeoutMs: 1_000,
+ now: () => clock.now(),
+ setTimer: (cb, ms) => clock.setTimer(cb, ms),
+ clearTimer: (handle) => clock.clearTimer(handle),
+ onFire: () => {
+ fireCount += 1;
+ },
+ });
+ // Plain stderr-ish text should NOT reset the monitor.
+ clock.advance(500);
+ monitor.noteStdoutChunk("loading model...\n");
+ expect(monitor.state().parsedEventCount).toBe(0);
+ clock.advance(600);
+ expect(fireCount).toBe(1);
+ monitor.stop();
+ });
+});
+
+describe("createCodexOutputInactivityMonitor (acceptance criteria 2: does not fire)", () => {
+ it("does not fire when events arrive every (threshold - 1s)", () => {
+ const clock = new FakeClock();
+ let fireCount = 0;
+ const timeoutMs = 7 * 60 * 1000;
+ const monitor = createCodexOutputInactivityMonitor({
+ timeoutMs,
+ now: () => clock.now(),
+ setTimer: (cb, ms) => clock.setTimer(cb, ms),
+ clearTimer: (handle) => clock.clearTimer(handle),
+ onFire: () => {
+ fireCount += 1;
+ },
+ });
+
+ // Pump events at threshold-1s intervals for 12 cycles (~84 minutes).
+ for (let i = 0; i < 12; i += 1) {
+ clock.advance(timeoutMs - 1_000);
+ monitor.noteStdoutChunk(`{"type":"item.completed","item":{"type":"agent_message","text":"tick ${i}"}}\n`);
+ expect(fireCount).toBe(0);
+ }
+
+ // Final event lets us "complete" — total state shows 12 parsed events.
+ expect(monitor.state().parsedEventCount).toBe(12);
+ expect(fireCount).toBe(0);
+
+ // Stop cleanly before the timer would have fired.
+ monitor.stop();
+ expect(fireCount).toBe(0);
+ });
+
+ it("multiple events in one chunk all reset the timer", () => {
+ const clock = new FakeClock();
+ let fireCount = 0;
+ const monitor = createCodexOutputInactivityMonitor({
+ timeoutMs: 1_000,
+ now: () => clock.now(),
+ setTimer: (cb, ms) => clock.setTimer(cb, ms),
+ clearTimer: (handle) => clock.clearTimer(handle),
+ onFire: () => {
+ fireCount += 1;
+ },
+ });
+ clock.advance(500);
+ monitor.noteStdoutChunk(
+ '{"type":"thread.started","thread_id":"a"}\n{"type":"item.completed","item":{"type":"agent_message","text":"hi"}}\n',
+ );
+ expect(monitor.state().parsedEventCount).toBe(2);
+ // Now wait 999ms — should still not fire.
+ clock.advance(999);
+ expect(fireCount).toBe(0);
+ // Wait one more ms — fires now.
+ clock.advance(1);
+ expect(fireCount).toBe(1);
+ monitor.stop();
+ });
+});
+
+describe("createCodexOutputInactivityMonitor (acceptance criteria 3: disabled)", () => {
+ it("resolveCodexInactivityTimeout returns disabled for null and the adapter creates no monitor", () => {
+ const resolution = resolveCodexInactivityTimeout(null);
+ expect(resolution.mode).toBe("disabled");
+ // Sanity: when a caller (execute.ts) honors `disabled`, it must not
+ // construct a monitor at all. Verify the constructor would otherwise
+ // require a positive timeoutMs.
+ expect(() =>
+ createCodexOutputInactivityMonitor({
+ timeoutMs: 0,
+ onFire: () => {},
+ }),
+ ).toThrow(/timeoutMs > 0/);
+ });
+});
+
+describe("CODEX_OUTPUT_INACTIVITY_MONITOR_SIGTERM_GRACE_MS", () => {
+ it("matches the 5-second grace window required by NEE-81", () => {
+ expect(CODEX_OUTPUT_INACTIVITY_MONITOR_SIGTERM_GRACE_MS).toBe(5_000);
+ });
+});
diff --git a/packages/adapters/codex-local/src/server/output-inactivity-monitor.ts b/packages/adapters/codex-local/src/server/output-inactivity-monitor.ts
new file mode 100644
index 0000000000..9363390946
--- /dev/null
+++ b/packages/adapters/codex-local/src/server/output-inactivity-monitor.ts
@@ -0,0 +1,142 @@
+import { parseJson } from "@paperclipai/adapter-utils/server-utils";
+
+export const DEFAULT_CODEX_OUTPUT_INACTIVITY_TIMEOUT_MS = 7 * 60 * 1000;
+export const CODEX_OUTPUT_INACTIVITY_MONITOR_SIGTERM_GRACE_MS = 5_000;
+
+export type CodexOutputInactivityMonitorResolution =
+ | { mode: "default"; timeoutMs: number }
+ | { mode: "configured"; timeoutMs: number }
+ | { mode: "disabled"; reason: "explicit_null" }
+ | { mode: "default"; timeoutMs: number; reason: "non_positive" };
+
+/**
+ * Resolve the inactivity monitor timeout from raw adapter config.
+ *
+ * - `null` → disabled (explicit escape hatch).
+ * - missing/`undefined` → default 7m.
+ * - number > 0 → configured value.
+ * - number ≤ 0 → default 7m (and a `non_positive` note for logging).
+ */
+export function resolveCodexInactivityTimeout(rawValue: unknown): CodexOutputInactivityMonitorResolution {
+ if (rawValue === null) return { mode: "disabled", reason: "explicit_null" };
+ if (typeof rawValue === "number" && Number.isFinite(rawValue)) {
+ if (rawValue > 0) return { mode: "configured", timeoutMs: rawValue };
+ return { mode: "default", timeoutMs: DEFAULT_CODEX_OUTPUT_INACTIVITY_TIMEOUT_MS, reason: "non_positive" };
+ }
+ return { mode: "default", timeoutMs: DEFAULT_CODEX_OUTPUT_INACTIVITY_TIMEOUT_MS };
+}
+
+export interface CodexOutputInactivityMonitorState {
+ fired: boolean;
+ spawnedAt: number;
+ lastEventAt: number;
+ firedAt: number | null;
+ parsedEventCount: number;
+}
+
+export interface CodexOutputInactivityMonitorOptions {
+ timeoutMs: number;
+ onFire: (state: CodexOutputInactivityMonitorState) => void;
+ now?: () => number;
+ setTimer?: (cb: () => void, ms: number) => unknown;
+ clearTimer?: (handle: unknown) => void;
+ /**
+ * Per-line predicate. When omitted, any line that successfully parses as
+ * JSON via the codex JSONL parser counts as a heartbeat event.
+ */
+ isHeartbeatLine?: (line: string) => boolean;
+}
+
+export interface CodexOutputInactivityMonitorHandle {
+ noteStdoutChunk(chunk: string): void;
+ /** Returns the current state without stopping the timer. */
+ state(): CodexOutputInactivityMonitorState;
+ /** Cancels any pending timer and returns the final state. */
+ stop(): CodexOutputInactivityMonitorState;
+}
+
+function defaultIsHeartbeatLine(line: string): boolean {
+ const trimmed = line.trim();
+ if (!trimmed) return false;
+ return parseJson(trimmed) !== null;
+}
+
+export function createCodexOutputInactivityMonitor(
+ options: CodexOutputInactivityMonitorOptions,
+): CodexOutputInactivityMonitorHandle {
+ const now = options.now ?? (() => Date.now());
+ const setTimer = options.setTimer ?? ((cb, ms) => setTimeout(cb, ms));
+ const clearTimer = options.clearTimer ?? ((h) => clearTimeout(h as ReturnType<typeof setTimeout>));
+ const isHeartbeatLine = options.isHeartbeatLine ?? defaultIsHeartbeatLine;
+ const timeoutMs = options.timeoutMs;
+
+ if (!(timeoutMs > 0)) {
+ throw new Error(`createCodexOutputInactivityMonitor requires timeoutMs > 0 (got ${timeoutMs})`);
+ }
+
+ const spawnedAt = now();
+ const state: CodexOutputInactivityMonitorState = {
+ fired: false,
+ spawnedAt,
+ lastEventAt: spawnedAt,
+ firedAt: null,
+ parsedEventCount: 0,
+ };
+ let timerHandle: unknown = null;
+ let stopped = false;
+
+ const fire = () => {
+ if (state.fired || stopped) return;
+ state.fired = true;
+ state.firedAt = now();
+ timerHandle = null;
+ options.onFire({ ...state });
+ };
+
+ const arm = () => {
+ if (stopped || state.fired) return;
+ if (timerHandle != null) clearTimer(timerHandle);
+ timerHandle = setTimer(fire, timeoutMs);
+ };
+
+ arm();
+
+ return {
+ noteStdoutChunk(chunk: string) {
+ if (stopped || state.fired) return;
+ let sawHeartbeat = false;
+ for (const rawLine of chunk.split(/\r?\n/)) {
+ if (isHeartbeatLine(rawLine)) {
+ sawHeartbeat = true;
+ state.parsedEventCount += 1;
+ }
+ }
+ if (sawHeartbeat) {
+ state.lastEventAt = now();
+ arm();
+ }
+ },
+ state() {
+ return { ...state };
+ },
+ stop() {
+ stopped = true;
+ if (timerHandle != null) {
+ clearTimer(timerHandle);
+ timerHandle = null;
+ }
+ return { ...state };
+ },
+ };
+}
+
+/**
+ * Format the inactivity monitor error message in the canonical
+ * `monitor: no codex output for {N}m {S}s` shape consumed by NEE-81.
+ */
+export function formatOutputInactivityMonitorErrorMessage(elapsedMs: number): string {
+ const total = Math.max(0, Math.round(elapsedMs / 1000));
+ const minutes = Math.floor(total / 60);
+ const seconds = total - minutes * 60;
+ return `monitor: no codex output for ${minutes}m ${seconds}s`;
+}