@principles/codex-adapter 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/pd-hook.js +84 -9
  2. package/package.json +1 -1
package/dist/pd-hook.js CHANGED
@@ -365,14 +365,75 @@ export async function processHookInvocation(rawStdin, _env = process.env, cwd =
365
365
  return { stdout: {}, exitCode: 0, stderr: [diagnostic(reason, nextAction)] };
366
366
  }
367
367
  }
368
+ // PRI-943: process-boundary fail-open belt. Every resolved path sets exitCode 0,
369
+ // so nothing may let an unwritable stream, an unserializable result, or an
370
+ // escaping rejection reach node's default handling, which terminates the hook
371
+ // with exit 1. Three distinct failure modes, each reproduced from a spawned
372
+ // process in tests/pd-hook-entry-belt.test.ts:
373
+ // * a write that THROWS synchronously — caught per write below;
374
+ // * a host closing the pipe, which raises an ASYNC 'error' event on the
375
+ // Socket. try/catch cannot reach that one, so a listener is required
376
+ // (armStreamBelts) or node aborts with "Unhandled 'error' event".
377
+ // * the stdout JSON object failing to SERIALIZE — guarded separately because
378
+ // the object is the contract (serializeStdout).
379
+ // stdout must stay exactly one JSON object, so a belt diagnostic goes to stderr
380
+ // only; when stderr itself is the dead stream nothing is reported rather than
381
+ // written into the machine channel.
382
+ let beltReported = false;
383
+ let stdoutJsonDelivered = false;
384
+ function writeBeltDiagnostic(reason, nextAction) {
385
+ if (beltReported)
386
+ return;
387
+ beltReported = true;
388
+ try {
389
+ process.stderr.write(`${diagnostic(reason, nextAction)}\n`);
390
+ }
391
+ catch {
392
+ // Both streams are gone: there is no channel left inside the fail-open
393
+ // contract, and adding I/O here would widen the belt itself.
394
+ }
395
+ }
396
+ function reportStreamFailure(error) {
397
+ writeBeltDiagnostic(`hook_stream_failed:${errorMessage(error)}`, 'The host closed the pipe; retry the tool call — the hook failed open.');
398
+ }
399
+ function writeStream(stream, line) {
400
+ try {
401
+ process[stream].write(`${line}\n`);
402
+ // Tracked here rather than at each call site so no future stdout write can
403
+ // make the entry belt believe nothing was delivered yet.
404
+ if (stream === 'stdout')
405
+ stdoutJsonDelivered = true;
406
+ return true;
407
+ }
408
+ catch (error) {
409
+ reportStreamFailure(error);
410
+ return false;
411
+ }
412
+ }
413
+ function armStreamBelts() {
414
+ process.stdout.on('error', reportStreamFailure);
415
+ process.stderr.on('error', reportStreamFailure);
416
+ }
417
+ /** rc-8: the stdout JSON object is the contract itself, so serializing it is a
418
+ * failure path of the belt rather than of the encoder — degrade to `{}` and
419
+ * report, never let the throw escape into the entry belt's last-resort branch. */
420
+ function serializeStdout(value) {
421
+ try {
422
+ return JSON.stringify(value) ?? '{}';
423
+ }
424
+ catch (error) {
425
+ writeBeltDiagnostic(`hook_stdout_serialize_failed:${errorMessage(error)}`, 'Retry the tool call; the hook failed open with an empty JSON object.');
426
+ return '{}';
427
+ }
428
+ }
368
429
  async function main() {
369
430
  let raw;
370
431
  try {
371
432
  raw = readFileSync(0, 'utf8');
372
433
  }
373
434
  catch (error) {
374
- process.stderr.write(`${diagnostic(`stdin_read_failed:${errorMessage(error)}`, 'Run the hook from Codex with JSON stdin.')}\n`);
375
- process.stdout.write('{}\n');
435
+ writeStream('stderr', diagnostic(`stdin_read_failed:${errorMessage(error)}`, 'Run the hook from Codex with JSON stdin.'));
436
+ writeStream('stdout', '{}');
376
437
  return;
377
438
  }
378
439
  let result;
@@ -383,12 +444,16 @@ async function main() {
383
444
  // Fail-open belt for an unexpected pre-dispatch throw (e.g. a workspace
384
445
  // resolution race): Codex must still receive exactly one JSON object on
385
446
  // stdout and a bounded diagnostic on stderr — never a bare crash.
386
- process.stderr.write(`${diagnostic(`hook_pipeline_unexpected:${errorMessage(error)}`, 'Retry the tool call; if it repeats, inspect PD stderr and the Workspace .pd/config.yaml state.')}\n`);
447
+ writeStream('stderr', diagnostic(`hook_pipeline_unexpected:${errorMessage(error)}`, 'Retry the tool call; if it repeats, inspect PD stderr and the Workspace .pd/config.yaml state.'));
387
448
  result = { stdout: {}, exitCode: 0, stderr: [] };
388
449
  }
389
- for (const line of result.stderr)
390
- process.stderr.write(`${line}\n`);
391
- process.stdout.write(`${JSON.stringify(result.stdout)}\n`);
450
+ // Each write is guarded on its own: with stderr dead the stdout JSON object
451
+ // is still what Codex needs to fail open, so one stream must not abort the other.
452
+ for (const line of result.stderr) {
453
+ if (!writeStream('stderr', line))
454
+ break;
455
+ }
456
+ writeStream('stdout', serializeStdout(result.stdout));
392
457
  process.exitCode = result.exitCode;
393
458
  }
394
459
  // PRI-892: Node resolves the main module through symlinks/junctions while
@@ -414,9 +479,19 @@ function isMainModuleEntry() {
414
479
  // satisfies it, because its argv[1] is the runner, not pd-hook.
415
480
  if (path.basename(entry) !== path.basename(fileURLToPath(import.meta.url)))
416
481
  return false;
417
- process.stderr.write(`${diagnostic(`entry_identity_unverified:${errorMessage(error)}`, 'Reinstall the Codex adapter; if it repeats, report this line together with the command that invoked the hook.')}\n`);
482
+ writeStream('stderr', diagnostic(`entry_identity_unverified:${errorMessage(error)}`, 'Reinstall the Codex adapter; if it repeats, report this line together with the command that invoked the hook.'));
418
483
  return true;
419
484
  }
420
485
  }
421
- if (isMainModuleEntry())
422
- void main();
486
+ if (isMainModuleEntry()) {
487
+ armStreamBelts();
488
+ void main().catch((error) => {
489
+ // Last-resort belt: a rejection escaping main's own guards must still leave
490
+ // Codex with exit 0 and, when nothing reached stdout yet, an empty JSON
491
+ // object instead of a bare crash.
492
+ writeBeltDiagnostic(`hook_fatal:${errorMessage(error)}`, 'Retry the tool call; if it repeats, report this line together with the command that invoked the hook.');
493
+ if (!stdoutJsonDelivered)
494
+ writeStream('stdout', '{}');
495
+ process.exitCode = 0;
496
+ });
497
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@principles/codex-adapter",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Codex CLI host adapter for Principles Disciple — implements HostAdapter interface for OpenAI Codex CLI's stdin/stdout JSON hook model (ADR-0020).",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",