|
1 | 1 | import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
2 | 3 | import path from "node:path"; |
3 | 4 | import test from "node:test"; |
4 | 5 | import assert from "node:assert/strict"; |
5 | | -import { spawn } from "node:child_process"; |
| 6 | +import { spawn, spawnSync } from "node:child_process"; |
6 | 7 | import { fileURLToPath } from "node:url"; |
7 | 8 |
|
8 | 9 | import { buildEnv, installFakeCodex } from "./fake-codex-fixture.mjs"; |
9 | 10 | import { initGitRepo, makeTempDir, run } from "./helpers.mjs"; |
10 | | -import { loadBrokerSession, saveBrokerSession } from "../plugins/codex/scripts/lib/broker-lifecycle.mjs"; |
| 11 | +import { |
| 12 | + loadBrokerSession, |
| 13 | + saveBrokerSession, |
| 14 | + sendBrokerShutdown, |
| 15 | + waitForBrokerEndpoint |
| 16 | +} from "../plugins/codex/scripts/lib/broker-lifecycle.mjs"; |
| 17 | +import { createBrokerEndpoint } from "../plugins/codex/scripts/lib/broker-endpoint.mjs"; |
11 | 18 | import { resolveStateDir } from "../plugins/codex/scripts/lib/state.mjs"; |
12 | 19 |
|
13 | 20 | const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
@@ -300,6 +307,131 @@ test("adversarial review asks Codex to inspect larger diffs itself", () => { |
300 | 307 | assert.doesNotMatch(state.lastTurnStart.prompt, /PROMPT_SELF_COLLECT_[ABC]/); |
301 | 308 | }); |
302 | 309 |
|
| 310 | +test("adversarial review resolves when app-server disconnects before turn/completed", () => { |
| 311 | + // Regression: Codex CLI can exit cleanly (token limit / rate limit / internal |
| 312 | + // error) after emitting a non-final_answer agentMessage but before sending |
| 313 | + // turn/completed. Neither existing captureTurn resolution path fires — the |
| 314 | + // Promise hangs and the companion script exits via IPC EOF without writing a |
| 315 | + // final verdict, leaving a zombie job record. |
| 316 | + // |
| 317 | + // Observed in the wild on 2026-04-18 (MeeePtt favorites-fix commit). A passing |
| 318 | + // test asserts captureTurn can handle "app-server closed unexpectedly". |
| 319 | + const repo = makeTempDir(); |
| 320 | + const binDir = makeTempDir(); |
| 321 | + installFakeCodex(binDir, "disconnect-before-completion"); |
| 322 | + initGitRepo(repo); |
| 323 | + fs.writeFileSync(path.join(repo, "README.md"), "hello\n"); |
| 324 | + run("git", ["add", "README.md"], { cwd: repo }); |
| 325 | + run("git", ["commit", "-m", "init"], { cwd: repo }); |
| 326 | + fs.writeFileSync(path.join(repo, "README.md"), "hello again\n"); |
| 327 | + |
| 328 | + // Explicit 10s timeout guards against the hang on unfixed code — without |
| 329 | + // this, the test would block until node --test's outer limit and report |
| 330 | + // "hung" instead of "disconnected". |
| 331 | + const result = spawnSync("node", [SCRIPT, "adversarial-review"], { |
| 332 | + cwd: repo, |
| 333 | + env: buildEnv(binDir), |
| 334 | + encoding: "utf8", |
| 335 | + timeout: 10_000 |
| 336 | + }); |
| 337 | + |
| 338 | + assert.notEqual(result.signal, "SIGTERM", |
| 339 | + `adversarial-review hung after app-server disconnect (stdout=${result.stdout}, stderr=${result.stderr})`); |
| 340 | + assert.notEqual(result.status, 0, |
| 341 | + "adversarial-review must surface disconnect as non-zero exit"); |
| 342 | + assert.match( |
| 343 | + `${result.stdout}\n${result.stderr}`, |
| 344 | + /disconnect|closed|unexpectedly/i, |
| 345 | + "rendered output should mention the disconnect" |
| 346 | + ); |
| 347 | +}); |
| 348 | + |
| 349 | +test("adversarial review preserves final_answer when app-server exits before turn/completed", () => { |
| 350 | + // Companion to the disconnect-before-completion test: when the |
| 351 | + // app-server emits a completed final_answer agentMessage and then exits |
| 352 | + // cleanly (no turn/completed notification), the captureTurn watchdog |
| 353 | + // must NOT demote the successful turn to failed. `finalAnswerSeen` is |
| 354 | + // the authoritative "work finished" signal — the disconnect is just |
| 355 | + // Codex closing the door, not a failure. |
| 356 | + const repo = makeTempDir(); |
| 357 | + const binDir = makeTempDir(); |
| 358 | + installFakeCodex(binDir, "final-answer-then-exit"); |
| 359 | + initGitRepo(repo); |
| 360 | + fs.mkdirSync(path.join(repo, "src")); |
| 361 | + fs.writeFileSync(path.join(repo, "src", "app.js"), "export const value = items[0];\n"); |
| 362 | + run("git", ["add", "src/app.js"], { cwd: repo }); |
| 363 | + run("git", ["commit", "-m", "init"], { cwd: repo }); |
| 364 | + fs.writeFileSync(path.join(repo, "src", "app.js"), "export const value = items[0].id;\n"); |
| 365 | + |
| 366 | + const result = spawnSync("node", [SCRIPT, "adversarial-review"], { |
| 367 | + cwd: repo, |
| 368 | + env: buildEnv(binDir), |
| 369 | + encoding: "utf8", |
| 370 | + timeout: 10_000 |
| 371 | + }); |
| 372 | + |
| 373 | + assert.notEqual(result.signal, "SIGTERM", |
| 374 | + `adversarial-review hung after final_answer + exit (stderr=${result.stderr})`); |
| 375 | + assert.equal(result.status, 0, |
| 376 | + `final_answer before disconnect must stay exit 0 (stdout=${result.stdout}, stderr=${result.stderr})`); |
| 377 | + assert.match( |
| 378 | + result.stdout, |
| 379 | + /Missing empty-state guard/, |
| 380 | + "structured findings from the final_answer payload must survive the disconnect" |
| 381 | + ); |
| 382 | +}); |
| 383 | + |
| 384 | +test("broker exits cleanly on broker/shutdown without tripping the upstream-exit watchdog", async () => { |
| 385 | + // Guards against a regression introduced while fixing the |
| 386 | + // "disconnect-before-completion" hang: the upstream-exit watchdog added |
| 387 | + // to app-server-broker.mjs listens on appClient.exitPromise, which is |
| 388 | + // resolved both by Codex CLI crashes AND by shutdown()'s own |
| 389 | + // appClient.close(). Without an `isShuttingDown` guard the watchdog |
| 390 | + // re-enters shutdown() during broker/shutdown (or SIGTERM/SIGINT) and |
| 391 | + // races the intentional process.exit(0) with process.exit(1). |
| 392 | + const binDir = makeTempDir(); |
| 393 | + installFakeCodex(binDir); |
| 394 | + const cwd = makeTempDir(); |
| 395 | + |
| 396 | + // Spawn the broker directly (not detached) so the test can observe its |
| 397 | + // real exit code — the shared-broker lifecycle normally detaches the |
| 398 | + // process which hides exit code from supervisors. |
| 399 | + const brokerScript = path.join(PLUGIN_ROOT, "scripts", "app-server-broker.mjs"); |
| 400 | + const sessionDir = fs.mkdtempSync(path.join(os.tmpdir(), "broker-test-")); |
| 401 | + const endpoint = createBrokerEndpoint(sessionDir); |
| 402 | + const pidFile = path.join(sessionDir, "broker.pid"); |
| 403 | + |
| 404 | + const broker = spawn( |
| 405 | + "node", |
| 406 | + [brokerScript, "serve", "--endpoint", endpoint, "--cwd", cwd, "--pid-file", pidFile], |
| 407 | + { env: buildEnv(binDir), stdio: ["ignore", "pipe", "pipe"] } |
| 408 | + ); |
| 409 | + |
| 410 | + const exited = new Promise((resolve) => { |
| 411 | + broker.on("exit", (code, signal) => resolve({ code, signal })); |
| 412 | + }); |
| 413 | + |
| 414 | + try { |
| 415 | + const ready = await waitForBrokerEndpoint(endpoint, 5_000); |
| 416 | + assert.ok(ready, "broker should become reachable"); |
| 417 | + |
| 418 | + await sendBrokerShutdown(endpoint); |
| 419 | + const { code, signal } = await exited; |
| 420 | + |
| 421 | + assert.equal(signal, null, "broker should exit without a signal"); |
| 422 | + assert.equal( |
| 423 | + code, |
| 424 | + 0, |
| 425 | + "broker/shutdown must return exit 0 — non-zero indicates the upstream-exit watchdog tripped during graceful teardown" |
| 426 | + ); |
| 427 | + } finally { |
| 428 | + if (broker.exitCode === null && broker.signalCode === null) { |
| 429 | + broker.kill("SIGKILL"); |
| 430 | + } |
| 431 | + fs.rmSync(sessionDir, { recursive: true, force: true }); |
| 432 | + } |
| 433 | +}); |
| 434 | + |
303 | 435 | test("review includes reasoning output when the app server returns it", () => { |
304 | 436 | const repo = makeTempDir(); |
305 | 437 | const binDir = makeTempDir(); |
|
0 commit comments