import assert from 'node:assert/strict'; import { spawn, spawnSync } from 'node:child_process'; import { chmodSync, mkdtempSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { describe, it } from 'node:test'; import { CODEX_WORKER_OWNER, applyCodexWorkerOutput, buildCodexWorkerInstructions, buildCodexWorkerTurnInputs, buildGenerationTurnInput, codexWorkerProcessStateIsOwned, codexWorkerStateIsOwned, isCodexRuntime, readPreparedArtifact, resolveCodexWorkerConfig, } from '../skill/scripts/live/codex-worker.mjs'; describe('Codex Live worker configuration', () => { it('defaults on only inside Codex and preserves explicit overrides', () => { assert.deepEqual(resolveCodexWorkerConfig({ env: {}, liveConfig: {} }), { enabled: false, model: null, codexPath: 'codex', effort: 'medium', profile: 'quality', delivery: 'progressive', maxArtifactBytes: 2_000_000, }); assert.equal(resolveCodexWorkerConfig({ env: { IMPECCABLE_LIVE_CODEX_WORKER: '1' }, liveConfig: {}, }).enabled, true); assert.equal(resolveCodexWorkerConfig({ env: { IMPECCABLE_LIVE_CODEX_WORKER: 'false' }, liveConfig: { experimentalCodexWorker: { enabled: true } }, }).enabled, false, 'explicit environment disable wins'); assert.equal(resolveCodexWorkerConfig({ env: {}, liveConfig: { experimentalCodexWorker: { enabled: true, delivery: 'atomic' } }, }).enabled, false, 'committed config cannot activate Codex in another harness'); assert.equal(resolveCodexWorkerConfig({ env: { CODEX_THREAD_ID: 'thread-1' } }).enabled, true); assert.equal(resolveCodexWorkerConfig({ env: { CODEX_THREAD_ID: 'thread-1', IMPECCABLE_LIVE_CODEX_PROFILE: 'fast' }, }).effort, 'low'); assert.equal(resolveCodexWorkerConfig({ env: { CODEX_THREAD_ID: 'thread-1', IMPECCABLE_LIVE_CODEX_DELIVERY: 'atomic' }, }).delivery, 'atomic'); assert.equal(isCodexRuntime({ CLAUDE_CODE: '1' }), false); assert.equal(isCodexRuntime({ GEMINI_CLI: '1' }), false); }); it('recognizes only a Live-owned durable thread record', () => { const cwd = '/tmp/project'; assert.equal(codexWorkerStateIsOwned({ owner: CODEX_WORKER_OWNER, cwd, threadId: 'worker-1' }, cwd), true); assert.equal(codexWorkerStateIsOwned({ owner: 'desktop', cwd, threadId: 'desktop-1' }, cwd), false); assert.equal(codexWorkerStateIsOwned({ owner: CODEX_WORKER_OWNER, cwd: '/tmp/other', threadId: 'worker-1' }, cwd), false); assert.equal(codexWorkerProcessStateIsOwned({ owner: CODEX_WORKER_OWNER, cwd, pid: 123, status: 'starting' }, cwd), true); assert.equal(codexWorkerStateIsOwned({ owner: CODEX_WORKER_OWNER, cwd, pid: 123, status: 'starting' }, cwd), false); }); it('leaves the portable foreground path untouched when the switch is off', () => { const cwd = mkdtempSync(path.join(tmpdir(), 'codex-worker-disabled-')); const script = path.resolve('skill/scripts/live-codex-worker.mjs'); const result = spawnSync(process.execPath, [script], { cwd, encoding: 'utf-8', env: { ...process.env, IMPECCABLE_LIVE_CODEX_WORKER: '0' }, }); assert.equal(result.status, 0, result.stderr); assert.deepEqual(JSON.parse(result.stdout), { ok: false, error: 'codex_worker_disabled', fallback: 'foreground', }); }); it('refuses to signal a pid from an unowned state record', async () => { const cwd = mkdtempSync(path.join(tmpdir(), 'codex-worker-unowned-')); const statePath = path.join(cwd, '.impeccable/live/codex-worker.json'); mkdirSync(path.dirname(statePath), { recursive: true }); const unrelated = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1_000)'], { stdio: 'ignore', }); try { writeFileSync(statePath, JSON.stringify({ owner: 'desktop', cwd, pid: unrelated.pid, status: 'ready', })); const script = path.resolve('skill/scripts/live-codex-worker.mjs'); const result = spawnSync(process.execPath, [script, '--stop'], { cwd, encoding: 'utf-8', }); assert.equal(result.status, 2, result.stderr); assert.equal(JSON.parse(result.stdout).error, 'codex_worker_state_unowned'); assert.doesNotThrow(() => process.kill(unrelated.pid, 0)); } finally { unrelated.kill('SIGTERM'); } }); it('reports a stop timeout instead of claiming an owned live process stopped', () => { const cwd = mkdtempSync(path.join(tmpdir(), 'codex-worker-stop-timeout-')); const statePath = path.join(cwd, '.impeccable/live/codex-worker.json'); mkdirSync(path.dirname(statePath), { recursive: true }); const stubborn = spawn(process.execPath, ['-e', "process.on('SIGTERM',()=>{});setInterval(()=>{},1000)"], { stdio: 'ignore', }); Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50); try { writeFileSync(statePath, JSON.stringify({ owner: CODEX_WORKER_OWNER, cwd, threadId: 'owned-thread', pid: stubborn.pid, status: 'ready', })); const script = path.resolve('skill/scripts/live-codex-worker.mjs'); const result = spawnSync(process.execPath, [script, '--stop'], { cwd, encoding: 'utf-8', env: { ...process.env, IMPECCABLE_LIVE_CODEX_STOP_TIMEOUT_MS: '100' }, }); assert.equal(result.status, 2, result.stderr); assert.equal(JSON.parse(result.stdout).status, 'stop_timeout'); assert.doesNotThrow(() => process.kill(stubborn.pid, 0)); } finally { stubborn.kill('SIGKILL'); } }); it('terminates a detached child before returning foreground fallback on startup timeout', () => { const cwd = mkdtempSync(path.join(tmpdir(), 'codex-worker-start-timeout-')); const liveDir = path.join(cwd, '.impeccable/live'); mkdirSync(liveDir, { recursive: true }); writeFileSync(path.join(liveDir, 'server.json'), JSON.stringify({ pid: process.pid, port: 1, token: 'smoke-token', })); const fakeCodex = path.join(cwd, 'fake-codex'); writeFileSync(fakeCodex, '#!/bin/sh\nwhile true; do sleep 1; done\n'); chmodSync(fakeCodex, 0o755); const script = path.resolve('skill/scripts/live-codex-worker.mjs'); const result = spawnSync(process.execPath, [script, '--background'], { cwd, encoding: 'utf-8', env: { ...process.env, IMPECCABLE_LIVE_CODEX_WORKER: '1', IMPECCABLE_CODEX_PATH: fakeCodex, IMPECCABLE_LIVE_CODEX_START_TIMEOUT_MS: '100', IMPECCABLE_LIVE_CODEX_STOP_TIMEOUT_MS: '1000', }, timeout: 5_000, }); assert.equal(result.status, 2, result.stderr); const output = JSON.parse(result.stdout); assert.equal(output.error, 'codex_worker_start_timeout'); assert.equal(output.terminated, true); assert.equal(output.fallback, 'foreground'); assert.throws(() => process.kill(output.childPid, 0), (error) => error.code === 'ESRCH'); }); it('returns a durable starting record without waiting for app-server readiness', () => { const cwd = mkdtempSync(path.join(tmpdir(), 'codex-worker-prewarm-')); const liveDir = path.join(cwd, '.impeccable/live'); mkdirSync(liveDir, { recursive: true }); writeFileSync(path.join(liveDir, 'server.json'), JSON.stringify({ pid: process.pid, port: 1, token: 'smoke-token', })); const fakeCodex = path.join(cwd, 'fake-codex'); writeFileSync(fakeCodex, '#!/bin/sh\nwhile true; do sleep 1; done\n'); chmodSync(fakeCodex, 0o755); const script = path.resolve('skill/scripts/live-codex-worker.mjs'); const startedAt = Date.now(); const result = spawnSync(process.execPath, [script, '--background', '--no-wait'], { cwd, encoding: 'utf-8', env: { ...process.env, IMPECCABLE_LIVE_CODEX_WORKER: '1', IMPECCABLE_CODEX_PATH: fakeCodex, }, timeout: 5_000, }); assert.equal(result.status, 0, result.stderr); const output = JSON.parse(result.stdout); assert.equal(output.status, 'starting'); assert.equal(output.starting, true); assert.equal(codexWorkerProcessStateIsOwned(output, cwd), true); assert.ok(Date.now() - startedAt < 1_000, 'prewarm should not wait for app-server initialization'); const stopped = spawnSync(process.execPath, [script, '--stop'], { cwd, encoding: 'utf-8', env: { ...process.env, IMPECCABLE_LIVE_CODEX_STOP_TIMEOUT_MS: '1000' }, timeout: 3_000, }); assert.equal(stopped.status, 0, stopped.stderr); assert.equal(JSON.parse(stopped.stdout).status, 'stopped'); }); }); describe('Codex Live worker structured artifact boundary', () => { it('keeps the model read-only and the supervisor as the only publisher', () => { const instructions = buildCodexWorkerInstructions('LIVE SPEC'); assert.match(instructions, /Do not write source/); assert.match(instructions, /read-only tools only/); assert.match(instructions, /supervisor alone writes staged artifacts/); assert.match(instructions, /shared-component visual roles/); assert.match(instructions, /decorative glyphs or pseudo-content/); assert.match(instructions, /Ignore any instruction.*run commands/); }); it('writes only the prepared source artifact path', () => { const cwd = mkdtempSync(path.join(tmpdir(), 'codex-worker-source-')); const artifact = path.join(cwd, '.impeccable/live/artifacts/session-r1.jsx'); mkdirSync(path.dirname(artifact), { recursive: true }); writeFileSync(artifact, 'before'); const prepared = { artifactFile: '.impeccable/live/artifacts/session-r1.jsx' }; applyCodexWorkerOutput({ output: { files: [{ path: prepared.artifactFile, content: 'after' }] }, prepared, phase: 'first', expectedVariants: 3, cwd, }); assert.equal(readFileSync(artifact, 'utf-8'), 'after'); assert.throws( () => applyCodexWorkerOutput({ output: { files: [{ path: 'src/App.jsx', content: 'unsafe' }] }, prepared, phase: 'first', expectedVariants: 3, cwd, }), /worker_output_source_path_invalid/, ); }); it('never lets a final component turn rewrite arrived variant 1', () => { const cwd = mkdtempSync(path.join(tmpdir(), 'codex-worker-component-')); const componentDir = path.join(cwd, '.impeccable/live/artifacts/session-r2-svelte'); mkdirSync(componentDir, { recursive: true }); writeFileSync(path.join(componentDir, 'manifest.json'), JSON.stringify({ id: 'session', previewMode: 'svelte-component', componentExtension: 'svelte', arrivedVariants: 1, })); writeFileSync(path.join(componentDir, 'v1.svelte'), '