aegiscode 6.8.15 → 6.8.16

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.
@@ -31,6 +31,40 @@ function cap(s) {
31
31
  return s.length > OUTPUT_CAP ? `${s.slice(0, OUTPUT_CAP)}\n… (truncated)` : s;
32
32
  }
33
33
 
34
+ // The buffer is BOUNDED WHILE IT FILLS, not just when it is reported.
35
+ //
36
+ // `cap()` above runs on close, which is fine for the text the user sees but
37
+ // useless as a memory bound: between the spawn and the exit the raw stream is
38
+ // accumulated in this process with no ceiling at all, so a command that never
39
+ // stops printing (`yes`, `cat /dev/zero`, a runaway build log) grows the MAIN
40
+ // process's heap for as long as the timeout allows — up to 10 minutes — before
41
+ // the truncation it was always going to get. A tool call must not be able to
42
+ // OOM the app it runs in.
43
+ //
44
+ // The window kept is head + tail, because neither end may be dropped:
45
+ // - the HEAD is what the user and the model are shown (OUTPUT_CAP already
46
+ // says only that much is reported);
47
+ // - the TAIL is where the exit sentinel lives for a session (`printf
48
+ // '__AEGIS_SH_…EXIT:%d'`) — dropping it would not merely lose output, it
49
+ // would hang the session until the timeout, because the resolver scans the
50
+ // buffer for a marker that can never arrive.
51
+ // 64 KiB of tail is ~1500× the sentinel line, and surviving a chunk boundary
52
+ // needs only the sentinel's own length.
53
+ const TAIL_KEEP = 64 * 1024;
54
+ const BUFFER_CEILING = OUTPUT_CAP + TAIL_KEEP;
55
+ const ELISION = '\n… (earlier output elided — command produced more than the buffer ceiling)\n';
56
+
57
+ /** Append one stream chunk to `prev`, keeping the bounded head+tail window.
58
+ * Pure, so the streaming paths and their tests agree on the policy. */
59
+ function appendBounded(prev, chunk) {
60
+ const next = prev + chunk;
61
+ if (next.length <= BUFFER_CEILING) return next;
62
+ // Elide the MIDDLE: head stays visible, tail keeps the sentinel reachable.
63
+ // The marker makes the missing bytes explicit rather than letting the two
64
+ // halves read as contiguous output.
65
+ return next.slice(0, OUTPUT_CAP) + ELISION + next.slice(-TAIL_KEEP);
66
+ }
67
+
34
68
  /** One-shot fallback — the pre-session behavior, used when no live session. */
35
69
  function oneShotShell({ command, timeout = DEFAULT_TIMEOUT, working_directory } = {}) {
36
70
  return new Promise((resolve) => {
@@ -42,8 +76,10 @@ function oneShotShell({ command, timeout = DEFAULT_TIMEOUT, working_directory }
42
76
  });
43
77
  let out = '';
44
78
  let err = '';
45
- child.stdout.on('data', (d) => { out += d; });
46
- child.stderr.on('data', (d) => { err += d; });
79
+ // Bounded both ways: a one-shot command with no live session is just as
80
+ // capable of printing forever as a session command (appendBounded's header).
81
+ child.stdout.on('data', (d) => { out = appendBounded(out, d.toString()); });
82
+ child.stderr.on('data', (d) => { err = appendBounded(err, d.toString()); });
47
83
  child.on('error', (e) => resolve({ content: `error: ${e.message}`, isError: true }));
48
84
  child.on('close', (code) => {
49
85
  const body = `${out}${err ? `\n${err}` : ''}`.trim() || `(exit ${code})`;
@@ -84,7 +120,12 @@ class ShellSession {
84
120
  return;
85
121
  }
86
122
  this.alive = true;
87
- const onData = (d) => { this.buf += d.toString(); if (this._pending) this._pending(); };
123
+ const onData = (d) => {
124
+ // Bounded — see appendBounded. The tail is kept precisely so the sentinel
125
+ // this session's resolver scans for survives a command that floods stdout.
126
+ this.buf = appendBounded(this.buf, d.toString());
127
+ if (this._pending) this._pending();
128
+ };
88
129
  this.child.stdout.on('data', onData);
89
130
  this.child.stderr.on('data', onData);
90
131
  this.child.on('exit', () => { this.alive = false; if (this._pending) this._pending(true); });
@@ -205,4 +246,4 @@ function pshq(s) {
205
246
  return `'${String(s).replace(/'/g, "''")}'`;
206
247
  }
207
248
 
208
- module.exports = { ShellSession, oneShotShell, IS_WIN32 };
249
+ module.exports = { ShellSession, oneShotShell, IS_WIN32, appendBounded, OUTPUT_CAP, BUFFER_CEILING, TAIL_KEEP };