aegiscode 6.8.15 → 6.8.17

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 };
@@ -30,8 +30,30 @@
30
30
  * `--allow-remote` is passed explicitly (the endpoint spends the account's
31
31
  * tokens, so exposing it is a decision, not a default);
32
32
  * - never logs or echoes the AEGIS key;
33
- * - the key is resolved by the ordinary path (env → 0600 store → .env), so
34
- * the shim holds no credential of its own.
33
+ * - the key is resolved by the ordinary path (env → 0600 store → legacy
34
+ * config.json), so the shim holds no credential of its own.
35
+ *
36
+ * THE SHARED ENV FILE — and why `run()` below reads it before anything else.
37
+ *
38
+ * `credentials.resolveApiKey()` reads the *environment*, then its own stores;
39
+ * it has never opened `~/.aegiscode/.env` (that is `client/env-file.js`'s job,
40
+ * deliberately: one reader, one contract). Every other host in this repo calls
41
+ * that reader at start-up — `cli/bin/aegiscode.js` before caps and a session,
42
+ * `desktop/main.js` before `createClient`, `mcp/server.js` at load. This file
43
+ * did not, so it was the one host where the documented setup
44
+ *
45
+ * AEGIS_API_KEY=aegis_… # in ~/.aegiscode/.env
46
+ *
47
+ * silently did nothing: the shim started, the editor got a 401 on the first
48
+ * message, and the key instruction printed in this file's own `--help` named a
49
+ * file the shim never read. In a shell that had also exported the variable the
50
+ * bug was invisible, which is exactly how it survived — a GUI-launched editor
51
+ * has no such export, and that is the normal way this shim is used.
52
+ *
53
+ * So `run()` loads the file, then *checks* the key and refuses to serve without
54
+ * one, naming where a key can come from. A shim that starts keyless is worse
55
+ * than one that exits: the failure surfaces later, in another program's error
56
+ * dialog, as an unexplained 401.
35
57
  */
36
58
 
37
59
  const fs = require('node:fs');
@@ -391,6 +413,52 @@ async function main(argv) {
391
413
  ].join('\n'));
392
414
  return 0;
393
415
  }
416
+
417
+ // ── the shared env file, before any credential is resolved ───────────────
418
+ //
419
+ // Placed after the `--help` early-return so help stays instant, and before
420
+ // `createShimServer` — which is where `createClient(credentials.clientOptions())`
421
+ // runs and therefore the last point at which process.env can still decide the
422
+ // key. An exported `AEGIS_API_KEY` always wins: loadEnvFile() fills only names
423
+ // the environment does not already have. Failure is never fatal here — an
424
+ // absent file is the pre-existing state — because the check right below turns
425
+ // "no key at all" into one sentence instead of a 401 from another program.
426
+ // Same call, same contract as cli/bin/aegiscode.js, desktop/main.js and
427
+ // mcp/server.js; see this file's header for what its absence cost.
428
+ const out = opts.log || process.stderr;
429
+ try {
430
+ const envFile = require('../client/env-file.js');
431
+ const envResult = envFile.loadEnvFile();
432
+ if (envResult.loose) {
433
+ logLine(
434
+ out,
435
+ `${envResult.file} is readable by other accounts (mode ${envResult.mode.toString(8)}) — ` +
436
+ 'consider `chmod 600` on it'
437
+ );
438
+ }
439
+ } catch (e) {
440
+ logLine(out, `could not read the shared env file: ${e && e.message}`);
441
+ }
442
+
443
+ // Refuse to serve with no account key. The shim cannot answer a single
444
+ // request without one, and a keyless endpoint reports that failure to the
445
+ // *editor*, hours later, as an opaque 401 — the exact confusion this check
446
+ // removes. `--no-auth` is about the caller's bearer token, not this
447
+ // credential, so it does not relax this.
448
+ const resolved = credentials.resolveApiKey();
449
+ if (!resolved.key) {
450
+ logLine(
451
+ out,
452
+ `no AEGIS account key — ${credentials.HOW_TO_SET}, ` +
453
+ `or set ${credentials.KEY_ENV} in ~/.aegiscode/.env`
454
+ );
455
+ return 1;
456
+ }
457
+ // Where the key came from, never the key itself: "did it pick up my .env or
458
+ // the file an older sign-in wrote?" is the question a user actually has, and
459
+ // it is unanswerable from a masked key alone.
460
+ logLine(out, `AEGIS key: ${credentials.sourceLabel(resolved.source)}`);
461
+
394
462
  let shim;
395
463
  try {
396
464
  shim = createShimServer(opts);