amplifier-agent-ts 0.7.0 → 0.7.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.
- package/dist/transport.js +45 -5
- package/package.json +1 -1
package/dist/transport.js
CHANGED
|
@@ -13,6 +13,12 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import { spawn } from "node:child_process";
|
|
15
15
|
import { createInterface } from "node:readline";
|
|
16
|
+
/**
|
|
17
|
+
* How long terminate() keeps reading the child's stdio after the child itself
|
|
18
|
+
* has exited, before giving up on pipes that a surviving grandchild is holding
|
|
19
|
+
* open. See the comment in {@link Transport.spawn} for why this exists.
|
|
20
|
+
*/
|
|
21
|
+
const ORPHANED_STDIO_DRAIN_MS = 250;
|
|
16
22
|
/**
|
|
17
23
|
* Consume an NDJSON stream line-by-line, dispatching each parseable JSON
|
|
18
24
|
* line to `onJson` and each non-JSON line to `onNonJson` (or silently
|
|
@@ -79,12 +85,46 @@ export class Transport {
|
|
|
79
85
|
cwd: this.opts.cwd,
|
|
80
86
|
});
|
|
81
87
|
this.proc = proc;
|
|
82
|
-
// exitPromise resolves
|
|
83
|
-
// have closed.
|
|
84
|
-
// buffered
|
|
85
|
-
// promise resolves.
|
|
88
|
+
// exitPromise normally resolves on 'close', which fires after the child
|
|
89
|
+
// process AND all its stdio streams have closed. 'close' comes AFTER
|
|
90
|
+
// readline has processed every buffered line, so frames are guaranteed to
|
|
91
|
+
// be delivered before the promise resolves.
|
|
92
|
+
//
|
|
93
|
+
// But 'close' waits for EOF on stdout/stderr, and those pipe write-ends are
|
|
94
|
+
// inherited by every grandchild the child spawned. A grandchild that
|
|
95
|
+
// outlives its parent keeps the write-end open, so EOF never arrives and
|
|
96
|
+
// 'close' never fires -- terminate() would hang for as long as the orphan
|
|
97
|
+
// lives, not for as long as the child lives. This is not hypothetical: a
|
|
98
|
+
// POSIX shell that forks rather than execs (`sh -c "sleep 60"` on dash)
|
|
99
|
+
// reproduces it exactly, and so does any engine tool subprocess that
|
|
100
|
+
// outlives the engine it was spawned from.
|
|
101
|
+
//
|
|
102
|
+
// So: 'exit' means the child itself is dead and everything it ever wrote is
|
|
103
|
+
// already in the pipe. Give the reader a short grace window to drain that
|
|
104
|
+
// buffer, then stop waiting on pipes that are being held open by somebody
|
|
105
|
+
// else, and detach from them so the handles are released.
|
|
86
106
|
this.exitPromise = new Promise((resolve) => {
|
|
87
|
-
|
|
107
|
+
let settled = false;
|
|
108
|
+
const settle = (info) => {
|
|
109
|
+
if (settled)
|
|
110
|
+
return;
|
|
111
|
+
settled = true;
|
|
112
|
+
resolve(info);
|
|
113
|
+
};
|
|
114
|
+
proc.on("close", (code, signal) => settle({ code, signal }));
|
|
115
|
+
proc.on("exit", (code, signal) => {
|
|
116
|
+
// Not a correctness deadline -- 'close' wins this race whenever the
|
|
117
|
+
// pipes are actually closable, which is every well-behaved child. This
|
|
118
|
+
// only bounds the pathological case, so it is deliberately generous.
|
|
119
|
+
const drain = setTimeout(() => {
|
|
120
|
+
proc.stdin?.destroy();
|
|
121
|
+
proc.stdout?.destroy();
|
|
122
|
+
proc.stderr?.destroy();
|
|
123
|
+
settle({ code, signal });
|
|
124
|
+
}, ORPHANED_STDIO_DRAIN_MS);
|
|
125
|
+
// Never let the grace timer itself hold the event loop open.
|
|
126
|
+
drain.unref();
|
|
127
|
+
});
|
|
88
128
|
});
|
|
89
129
|
// Read stdout line by line; parse JSON; dispatch to registered callbacks.
|
|
90
130
|
const stdoutRl = createInterface({ input: proc.stdout });
|
package/package.json
CHANGED