@shumkov/orchestra 0.13.0 → 0.13.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.
@@ -0,0 +1,83 @@
1
+ # Codex app-server stdout backpressure
2
+
3
+ ## Problem
4
+
5
+ `CodexAppServerClient._onStdoutData` splits each stdout chunk into JSONL
6
+ lines and queues every line on `messageChain`. It bounds that queue with
7
+ `maxQueuedLines` (256) and `maxQueuedBytes` (4 MiB) and calls
8
+ `child.stdout.pause()` once the queue is half full. But `pause()` only stops
9
+ *future* `data` events: the `while` loop keeps splitting the chunk it already
10
+ holds. One pipe chunk (64 KiB) of small notifications holds more than 256
11
+ lines, so the loop crosses the line cap inside a single chunk and faults the
12
+ client with `CODEX_PROTOCOL_ERROR: app-server inbound message queue exceeded
13
+ the size limit`.
14
+
15
+ Production: a command run by Codex printed fast output; the app-server sent
16
+ about 500 `item/commandExecution/outputDelta` notifications (~180 bytes each)
17
+ in one second, and the generation died with `CODEX_CONTAINMENT_FAILED` /
18
+ `app-server-fault`. The user sees "Codex could not verify this request's
19
+ durable outcome". Reproduced locally: one 49.8 KB chunk holding 300 delta lines
20
+ faults the client.
21
+
22
+ The queue caps exist to bound memory, not to reject legitimate bursts. Memory
23
+ is already bounded by `pause()`, as long as we stop splitting lines while the
24
+ queue is full.
25
+
26
+ ## Approach
27
+
28
+ Check the caps once per chunk, not once per line.
29
+
30
+ - `pause()` inside a `data` handler reliably stops further `data` events, so
31
+ splitting the chunk already in hand adds at most one chunk (64 KiB pipe
32
+ read) on top of the queue. Memory stays bounded by half the caps plus one
33
+ chunk plus one partial line (`maxLineBytes`), as it is today.
34
+ - `_onStdoutData`: before appending the chunk, fault with the existing
35
+ queue-limit error only if the queue is already at or over `maxQueuedLines` /
36
+ `maxQueuedBytes`. With a stream that honours `pause()` that cannot happen,
37
+ because `data` only resumes once the queue is below half. It catches a
38
+ stream that ignores `pause()`.
39
+ - Remove the per-line cap fault inside the split loop. Keep the half-full
40
+ pause, `_resumeStdoutIfSafe`, the per-line and partial-line size checks and
41
+ `_onStdoutEnd` unchanged.
42
+ - `_close()`: resume stdout when closing starts. `_onStdoutData` already
43
+ discards data while closing; a paused pipe must not leave the app-server
44
+ supervisor blocked on a full pipe while it should be handling stdin EOF.
45
+ Bursts now pause stdout routinely, so cancelling during fast output would hit
46
+ this.
47
+
48
+ ## Alternatives rejected
49
+
50
+ - Stop splitting when the queue is full and keep the rest of the chunk in
51
+ `stdoutBuffer`, draining it as the queue empties (first draft). Exact line
52
+ bound, but it needs a drain loop, a new resume condition, end-of-stream
53
+ draining and a separate hard bound, and reviewers found a stall (a line that
54
+ fits `maxLineBytes` but not `maxQueuedBytes` with an empty queue) and a hard
55
+ bound that faults on small configured caps. The one-chunk overshoot it
56
+ avoids is already bounded.
57
+
58
+ - Raise `maxQueuedLines` (e.g. to 4096). Moves the cliff; a faster command or a
59
+ bigger pipe chunk still kills the turn.
60
+ - Fault only on `maxQueuedBytes`, drop the line cap. Same cliff in bytes.
61
+ - Coalesce `outputDelta` notifications. Changes protocol semantics for one
62
+ method and does not fix other bursts (e.g. `fileChange/outputDelta`).
63
+
64
+ ## Failure modes
65
+
66
+ - Ordering is unchanged: every line of a chunk is queued in order.
67
+ - A stream without `pause()`, or one that ignores it, faults on the next chunk
68
+ once the queue reaches its caps (unchanged error code and message).
69
+ - Close or fault while paused: `_close()` resumes stdout, and
70
+ `_onStdoutData` drops data in `closing` / `closed` / faulted states.
71
+
72
+ ## Tests
73
+
74
+ - Regression, deterministic: the existing caps test changes intent. Ten
75
+ notifications in one write with `maxQueuedLines: 2`, and with
76
+ `maxQueuedBytes: 256`, are all processed, the request resolves and the client
77
+ stays healthy (red before the fix).
78
+ - Regression, production-shaped: 600 `item/commandExecution/outputDelta` lines
79
+ in one write with default caps; the request resolves, the client stays
80
+ healthy.
81
+ - A fake child (`spawnFn`) whose `stdout.pause()` does nothing, with a
82
+ notification handler held pending, faults with `CODEX_PROTOCOL_ERROR` once a
83
+ further chunk arrives over the caps.
@@ -3124,6 +3124,21 @@ class CodexAppServerClient extends EventEmitter {
3124
3124
  || this.state === 'closed'
3125
3125
  || this.protocolError
3126
3126
  ) return;
3127
+ // Caps are checked once per chunk. Every line of a chunk is queued even
3128
+ // past the caps: one pipe read of small notifications (a burst of command
3129
+ // output) holds more lines than maxQueuedLines, and pausing stdout at the
3130
+ // half-full mark already stops the next chunk. A chunk arriving with the
3131
+ // queue still at its caps means the stream ignored pause().
3132
+ if (
3133
+ this.queuedLineCount >= this.maxQueuedLines
3134
+ || this.queuedLineBytes >= this.maxQueuedBytes
3135
+ ) {
3136
+ this._fault(new CodexAppServerError(
3137
+ 'app-server inbound message queue exceeded the size limit',
3138
+ 'CODEX_PROTOCOL_ERROR',
3139
+ ));
3140
+ return;
3141
+ }
3127
3142
  this.stdoutBuffer += typeof chunk === 'string'
3128
3143
  ? chunk
3129
3144
  : this.stdoutDecoder.write(chunk);
@@ -3140,16 +3155,6 @@ class CodexAppServerClient extends EventEmitter {
3140
3155
  return;
3141
3156
  }
3142
3157
  const lineBytes = Buffer.byteLength(line);
3143
- if (
3144
- this.queuedLineCount + 1 > this.maxQueuedLines
3145
- || this.queuedLineBytes + lineBytes > this.maxQueuedBytes
3146
- ) {
3147
- this._fault(new CodexAppServerError(
3148
- 'app-server inbound message queue exceeded the size limit',
3149
- 'CODEX_PROTOCOL_ERROR',
3150
- ));
3151
- return;
3152
- }
3153
3158
  this.queuedLineCount += 1;
3154
3159
  this.queuedLineBytes += lineBytes;
3155
3160
  if (
@@ -3619,6 +3624,14 @@ class CodexAppServerClient extends EventEmitter {
3619
3624
  async _close() {
3620
3625
  if (this.state === 'closed') return;
3621
3626
  this.state = 'closing';
3627
+ // _onStdoutData discards output from now on. A stdout left paused by
3628
+ // backpressure never ends, so the child's 'close' event (exitPromise)
3629
+ // never fires, and the app-server can block on a full pipe while it should
3630
+ // be handling the stdin EOF below.
3631
+ if (this.stdoutPaused) {
3632
+ this.child?.stdout?.resume?.();
3633
+ this.stdoutPaused = false;
3634
+ }
3622
3635
  this._invalidateSinks();
3623
3636
  this._rejectAll(new CodexAppServerError(
3624
3637
  'app-server client closed',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shumkov/orchestra",
3
- "version": "0.13.0",
3
+ "version": "0.13.1",
4
4
  "description": "Transport-agnostic session engine for Claude Code and the native Codex app-server: spawn, steer, supervise, observe turns, and recover.",
5
5
  "type": "commonjs",
6
6
  "main": "index.js",