@shumkov/orchestra 0.13.0 → 0.13.2
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.
|
|
@@ -37,12 +37,18 @@ const DEFAULT_CLOSE_GRACE_MS = 1_000;
|
|
|
37
37
|
const DEFAULT_CLOSE_KILL_MS = 1_000;
|
|
38
38
|
const CODEX_SUPERVISOR_GRACE_MS =
|
|
39
39
|
DEFAULT_CLOSE_GRACE_MS + DEFAULT_CLOSE_KILL_MS;
|
|
40
|
-
|
|
40
|
+
// Codex caps a command's aggregatedOutput at about 1 MiB, and JSON escaping
|
|
41
|
+
// can grow that up to six times in its item/completed line (a control
|
|
42
|
+
// character becomes \u00XX). The line limit must sit above that worst case.
|
|
43
|
+
const DEFAULT_MAX_LINE_BYTES = 8 * 1024 * 1024;
|
|
41
44
|
const DEFAULT_MAX_STDERR_BYTES = 64 * 1024;
|
|
42
45
|
const DEFAULT_MAX_PENDING_REQUESTS = 64;
|
|
43
46
|
const DEFAULT_MAX_USED_REQUEST_IDS = 65_536;
|
|
44
47
|
const DEFAULT_MAX_QUEUED_LINES = 256;
|
|
45
|
-
|
|
48
|
+
// At least twice the line limit: stdout resumes below half this cap, so one
|
|
49
|
+
// maximum-size line on top of a queue just under that mark still stays under
|
|
50
|
+
// the cap, which a chunk then only reaches if the stream ignored pause().
|
|
51
|
+
const DEFAULT_MAX_QUEUED_BYTES = 2 * DEFAULT_MAX_LINE_BYTES;
|
|
46
52
|
const DEFAULT_SINK_TIMEOUT_MS = 5_000;
|
|
47
53
|
const CLIENT_ROOT_ERROR_CODES = Object.freeze([
|
|
48
54
|
'CODEX_PROTOCOL_ERROR',
|
|
@@ -3124,6 +3130,21 @@ class CodexAppServerClient extends EventEmitter {
|
|
|
3124
3130
|
|| this.state === 'closed'
|
|
3125
3131
|
|| this.protocolError
|
|
3126
3132
|
) return;
|
|
3133
|
+
// Caps are checked once per chunk. Every line of a chunk is queued even
|
|
3134
|
+
// past the caps: one pipe read of small notifications (a burst of command
|
|
3135
|
+
// output) holds more lines than maxQueuedLines, and pausing stdout at the
|
|
3136
|
+
// half-full mark already stops the next chunk. A chunk arriving with the
|
|
3137
|
+
// queue still at its caps means the stream ignored pause().
|
|
3138
|
+
if (
|
|
3139
|
+
this.queuedLineCount >= this.maxQueuedLines
|
|
3140
|
+
|| this.queuedLineBytes >= this.maxQueuedBytes
|
|
3141
|
+
) {
|
|
3142
|
+
this._fault(new CodexAppServerError(
|
|
3143
|
+
'app-server inbound message queue exceeded the size limit',
|
|
3144
|
+
'CODEX_PROTOCOL_ERROR',
|
|
3145
|
+
));
|
|
3146
|
+
return;
|
|
3147
|
+
}
|
|
3127
3148
|
this.stdoutBuffer += typeof chunk === 'string'
|
|
3128
3149
|
? chunk
|
|
3129
3150
|
: this.stdoutDecoder.write(chunk);
|
|
@@ -3140,16 +3161,6 @@ class CodexAppServerClient extends EventEmitter {
|
|
|
3140
3161
|
return;
|
|
3141
3162
|
}
|
|
3142
3163
|
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
3164
|
this.queuedLineCount += 1;
|
|
3154
3165
|
this.queuedLineBytes += lineBytes;
|
|
3155
3166
|
if (
|
|
@@ -3619,6 +3630,14 @@ class CodexAppServerClient extends EventEmitter {
|
|
|
3619
3630
|
async _close() {
|
|
3620
3631
|
if (this.state === 'closed') return;
|
|
3621
3632
|
this.state = 'closing';
|
|
3633
|
+
// _onStdoutData discards output from now on. A stdout left paused by
|
|
3634
|
+
// backpressure never ends, so the child's 'close' event (exitPromise)
|
|
3635
|
+
// never fires, and the app-server can block on a full pipe while it should
|
|
3636
|
+
// be handling the stdin EOF below.
|
|
3637
|
+
if (this.stdoutPaused) {
|
|
3638
|
+
this.child?.stdout?.resume?.();
|
|
3639
|
+
this.stdoutPaused = false;
|
|
3640
|
+
}
|
|
3622
3641
|
this._invalidateSinks();
|
|
3623
3642
|
this._rejectAll(new CodexAppServerError(
|
|
3624
3643
|
'app-server client closed',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shumkov/orchestra",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.2",
|
|
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",
|