aegiscode 6.8.14 → 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.
- package/LICENSE +661 -0
- package/README.md +7 -0
- package/bin/aegiscode.js +35 -2
- package/package.json +1 -1
- package/src/app.js +66 -2
- package/src/chatflow.js +63 -4
- package/src/commands.js +17 -2
- package/src/events.js +173 -0
- package/src/render.js +6 -3
- package/src/screen.js +27 -0
- package/vendor/client/aegis.js +9 -2
- package/vendor/client/brain-catalog.js +77 -0
- package/vendor/client/env-file.js +287 -3
- package/vendor/desktop/lib/local/engine.js +52 -18
- package/vendor/desktop/lib/local/shell.js +45 -4
package/bin/aegiscode.js
CHANGED
|
@@ -644,11 +644,44 @@ async function main(argv = process.argv.slice(2)) {
|
|
|
644
644
|
);
|
|
645
645
|
return 2;
|
|
646
646
|
}
|
|
647
|
-
const code = await app.runInteractive();
|
|
648
647
|
// The reference leaves a resume hint on exit; without one there is no way to
|
|
649
648
|
// discover that a session was persisted at all (`/resume` lists them, but a
|
|
650
649
|
// user who just quit is not looking at a command list).
|
|
651
|
-
|
|
650
|
+
//
|
|
651
|
+
// It is written from two places on purpose, and latched so it can only ever
|
|
652
|
+
// be written once. It used to be a single write after the await below, which
|
|
653
|
+
// made the hint depend on the session promise settling: when the key pump
|
|
654
|
+
// ended and the loop drained instead — the same failure that ate the alt
|
|
655
|
+
// screen teardown — the process exited before this line ran, and the frames
|
|
656
|
+
// differed by exactly these 81 bytes. That is what the parity suite was
|
|
657
|
+
// reporting, and it was load-dependent: measured 3 of 4 runs losing the hint
|
|
658
|
+
// while four pty runs competed for the cpu, 0 of 12 when they did not. The
|
|
659
|
+
// teardown is guaranteed the same way (see chatflow.js); the hint now is too,
|
|
660
|
+
// so the bytes no longer depend on which of the two paths ended the process.
|
|
661
|
+
// The hint is handed to the screen module instead of being scheduled here.
|
|
662
|
+
// It is emitted *by* `leaveAltScreen()`, i.e. by the restore itself, because
|
|
663
|
+
// position cannot be guaranteed by ordering where three paths can reach the
|
|
664
|
+
// teardown (the loop's `finally`, the session's `exit` handler, this file's
|
|
665
|
+
// `exit` handler) and their relative order is timing-dependent. That race put
|
|
666
|
+
// the same 81 bytes in two different positions from run to run — the parity
|
|
667
|
+
// suite saw linux/darwin/win32 diverge and then converge, at random, with
|
|
668
|
+
// platform swapped out for luck. See `queueExitNotice` in src/screen.js.
|
|
669
|
+
const screen = require('../src/screen.js');
|
|
670
|
+
screen.queueExitNotice('\nResume this session with:\n aegiscode --continue (or /resume for the list)\n');
|
|
671
|
+
// A queued notice is consumed exactly once, by the first restore to run; the
|
|
672
|
+
// flush covers the paths that never leave an alternate screen (a crash before
|
|
673
|
+
// the screen was taken, a host that drives the loop with its own stdin), so
|
|
674
|
+
// the hint cannot be lost by the very mechanism that guarantees its position.
|
|
675
|
+
const leaveHint = () => {
|
|
676
|
+
try {
|
|
677
|
+
if (screen.isAltScreen()) screen.leaveAltScreen();
|
|
678
|
+
screen.flushExitNotice();
|
|
679
|
+
} catch { /* the terminal is already gone; nothing to say it to */ }
|
|
680
|
+
};
|
|
681
|
+
const running = app.runInteractive();
|
|
682
|
+
process.once('exit', leaveHint);
|
|
683
|
+
const code = await running;
|
|
684
|
+
leaveHint();
|
|
652
685
|
return code;
|
|
653
686
|
}
|
|
654
687
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aegiscode",
|
|
3
3
|
"productName": "AEGIS Code",
|
|
4
|
-
"version": "6.8.
|
|
4
|
+
"version": "6.8.16",
|
|
5
5
|
"description": "aegiscode — the command-line version of AEGIS Desktop. The shared tool surface in your shell, over the same thin transport and tool registry as the MCP plugin and the desktop app. Ships transport + UI only; no brain.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "AEGIS Code",
|
package/src/app.js
CHANGED
|
@@ -546,13 +546,77 @@ function createApp(options = {}) {
|
|
|
546
546
|
async function listModelsFor(cls) {
|
|
547
547
|
const want = HOST_CLASSES.includes(cls) ? cls : commandCtx.modelClass;
|
|
548
548
|
try {
|
|
549
|
-
if (want === 'byok')
|
|
549
|
+
if (want === 'byok') {
|
|
550
|
+
const byok = await loadByokModels();
|
|
551
|
+
// The pooled catalog may never have been fetched on a byok-only session
|
|
552
|
+
// (`/model` alone on a fresh install), and the Nexus row below can only
|
|
553
|
+
// come from it — so read it here, best-effort. A failed read (offline,
|
|
554
|
+
// or no account key) leaves the byok rows, which is the honest list: no
|
|
555
|
+
// pooled row, rather than one this process cannot name.
|
|
556
|
+
if (!modelCache.models.length) {
|
|
557
|
+
try {
|
|
558
|
+
await loadModels();
|
|
559
|
+
} catch {
|
|
560
|
+
/* no pooled catalog to read — byok rows stand alone */
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return classScopedModels(byok);
|
|
564
|
+
}
|
|
550
565
|
return await loadModels();
|
|
551
566
|
} catch {
|
|
552
567
|
return [];
|
|
553
568
|
}
|
|
554
569
|
}
|
|
555
570
|
|
|
571
|
+
/** The pooled class's pickable rows, each tagged with the class it runs on. */
|
|
572
|
+
function pooledRows() {
|
|
573
|
+
return pickerEntries(modelCache.models).map((m) => ({ ...m, cls: 'aegis' }));
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** The byok class's own `provider:model` rows, tagged the same way. */
|
|
577
|
+
function byokRows() {
|
|
578
|
+
return pickerEntries(byokCache.models).map((m) => ({ ...m, cls: 'byok' }));
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* The class-scoped model list — `state().models`, `/models`, the alt+p chord
|
|
583
|
+
* and the `/class` auto-pick all read it, so it is one list and not four.
|
|
584
|
+
*
|
|
585
|
+
* On the POOLED class it is just the catalog's pickable rows. On the BYOK
|
|
586
|
+
* class it is those rows PLUS the pooled Nexus row, which the server's
|
|
587
|
+
* `provider:model` listing can never contain: the two id spaces are disjoint
|
|
588
|
+
* (`models.js` documents it, and `pinBelongsToClass` enforces it), so a
|
|
589
|
+
* session sitting on byok used to see a list with no way back to the pooled
|
|
590
|
+
* lane in it — the same "correct list, undiscoverable half" defect
|
|
591
|
+
* `mergedPickerModels` in commands.js fixes for the `/model` picker, fixed
|
|
592
|
+
* here for the list the other surfaces read, so the panel, the chord and the
|
|
593
|
+
* picker cannot disagree about whether Nexus is an option.
|
|
594
|
+
*
|
|
595
|
+
* Each row carries its own `cls`, which is what the picker's ENTER handler
|
|
596
|
+
* switches on and what `/models` paints its "pooled lane" marker from:
|
|
597
|
+
* picking Nexus from the byok list moves the session to the pooled class
|
|
598
|
+
* BEFORE the pin applies, because a pooled id pinned under byok is parsed as
|
|
599
|
+
* a provider name and 400s at the relay. Tagging is not decoration — it is
|
|
600
|
+
* why this row is safe to offer from a class that cannot run it.
|
|
601
|
+
*
|
|
602
|
+
* The pooled row comes from the same `pickerEntries` shaping the pooled class
|
|
603
|
+
* uses (`filterAegisCatalog`'s single-row collapse), never a hand-built
|
|
604
|
+
* literal: a second spelling of "the brain's id and label" is exactly the
|
|
605
|
+
* drift `client/brain-catalog.js` exists to end. Its absence is not an error
|
|
606
|
+
* — no account key means no pooled catalog to read, and a byok-only session
|
|
607
|
+
* must still list its own models.
|
|
608
|
+
*
|
|
609
|
+
* @param {Array<object>} [byokOverride] rows already fetched by an async
|
|
610
|
+
* caller, so an awaiting caller does not re-read a cache it just filled.
|
|
611
|
+
*/
|
|
612
|
+
function classScopedModels(byokOverride) {
|
|
613
|
+
if (commandCtx.modelClass !== 'byok') return pooledRows();
|
|
614
|
+
const byok = Array.isArray(byokOverride)
|
|
615
|
+
? byokOverride.map((m) => ({ ...m, cls: 'byok' }))
|
|
616
|
+
: byokRows();
|
|
617
|
+
return [...pooledRows(), ...byok];
|
|
618
|
+
}
|
|
619
|
+
|
|
556
620
|
/**
|
|
557
621
|
* Whether a pinned id can run on `cls`, decided synchronously (switchClass is
|
|
558
622
|
* not async). byok ids are `provider:model`; aegis takes anything that is
|
|
@@ -1105,7 +1169,7 @@ function createApp(options = {}) {
|
|
|
1105
1169
|
// dropped, live ids only (see models.js pickerEntries).
|
|
1106
1170
|
// Whichever class is live decides what is pinnable — the pooled ids the
|
|
1107
1171
|
// server advertises, or the `provider:model` ids this machine can relay.
|
|
1108
|
-
models:
|
|
1172
|
+
models: classScopedModels(),
|
|
1109
1173
|
commands: visibleCommands(),
|
|
1110
1174
|
transcript: transcript.slice(),
|
|
1111
1175
|
sessions: [],
|
package/src/chatflow.js
CHANGED
|
@@ -42,7 +42,7 @@ const {
|
|
|
42
42
|
} = require('./screen.js');
|
|
43
43
|
const {
|
|
44
44
|
KEY, STREAM_END, attachKeyStream, nextKey, nextKeyTimeout, requeueKeys, resetKeyStream,
|
|
45
|
-
isKeyStreamSuspended,
|
|
45
|
+
markSessionStart, isKeyStreamSuspended,
|
|
46
46
|
} = require('./events.js');
|
|
47
47
|
const { GLYPH, VERBS, DONE_VERBS, BOLD, BOLD_OFF, themeOf } = require('./theme.js');
|
|
48
48
|
const { LineEditor } = require('./input.js');
|
|
@@ -555,7 +555,12 @@ function rowLines(msg, cols, ctx, now = Date.now()) {
|
|
|
555
555
|
if (i === 0) line.unshift(span(t.white, GLYPH.block), span('', ' '));
|
|
556
556
|
out.push(line);
|
|
557
557
|
}
|
|
558
|
-
|
|
558
|
+
// The live cursor trails the LAST line of a still-growing answer — but only
|
|
559
|
+
// when that line is not the one already carrying the `●` answer marker.
|
|
560
|
+
// A one-line answer put both on the same row (`● Working●`), which reads as a
|
|
561
|
+
// stray glyph rather than a cursor: two dots where the user expects one. The
|
|
562
|
+
// marker is the indicator then, exactly as it is after the turn finishes.
|
|
563
|
+
if (msg.streaming && out.length > 1) {
|
|
559
564
|
const last = out[out.length - 1];
|
|
560
565
|
if (last) last.push(span(t.white, GLYPH.block));
|
|
561
566
|
}
|
|
@@ -1715,6 +1720,12 @@ async function runSession(host) {
|
|
|
1715
1720
|
};
|
|
1716
1721
|
|
|
1717
1722
|
const handleKey = async (key) => {
|
|
1723
|
+
// No key is not a key. The pump can legitimately deliver nothing — a reset
|
|
1724
|
+
// during teardown, a stdin that closed before the keystroke landed — and
|
|
1725
|
+
// that used to reach `key.name` and throw, which the loop's catch turned
|
|
1726
|
+
// into a stray note and another park. See the STREAM_END branch in the
|
|
1727
|
+
// session loop for what that cost.
|
|
1728
|
+
if (!key) return;
|
|
1718
1729
|
if (overlay) {
|
|
1719
1730
|
await handleOverlayKey(key);
|
|
1720
1731
|
return;
|
|
@@ -2038,9 +2049,35 @@ async function runSession(host) {
|
|
|
2038
2049
|
leaveAltScreen();
|
|
2039
2050
|
};
|
|
2040
2051
|
|
|
2052
|
+
// …and it must not depend on the session resolving to run. Anything that ends
|
|
2053
|
+
// the process — a turn parked on a stream that will never produce a key, a
|
|
2054
|
+
// future early `return`, another `process.exit` added above this line —
|
|
2055
|
+
// otherwise hands the user back a terminal still sitting in the alternate
|
|
2056
|
+
// screen with mouse reporting enabled. That is precisely the exit frame the
|
|
2057
|
+
// parity test caught. Once-only, and unregistered in the `finally` so a host
|
|
2058
|
+
// that runs several sessions in one process does not stack handlers.
|
|
2059
|
+
let cleanedUp = false;
|
|
2060
|
+
const cleanupOnce = () => {
|
|
2061
|
+
if (cleanedUp) return;
|
|
2062
|
+
cleanedUp = true;
|
|
2063
|
+
try {
|
|
2064
|
+
cleanup();
|
|
2065
|
+
} catch {
|
|
2066
|
+
/* the terminal is already gone; nothing left to restore */
|
|
2067
|
+
}
|
|
2068
|
+
};
|
|
2069
|
+
process.once('exit', cleanupOnce);
|
|
2070
|
+
|
|
2041
2071
|
// Enter the alternate screen and take over the terminal. `host.stdin` exists
|
|
2042
2072
|
// so the loop can be driven from a test with a synthetic key stream.
|
|
2043
2073
|
const stdin = host.stdin || process.stdin;
|
|
2074
|
+
// Claim the queue BEFORE attaching. Everything queued up to this instant was
|
|
2075
|
+
// typed at a terminal no session owned yet — it survived onboarding's pump
|
|
2076
|
+
// teardown only because the pump knew no session had started (`drainQueue`),
|
|
2077
|
+
// and this is the line that ends that grace period: from here the queue is
|
|
2078
|
+
// this session's, and a later teardown may clear it. Order matters — after
|
|
2079
|
+
// `attachKeyStream` would be too late for the keys already held.
|
|
2080
|
+
markSessionStart();
|
|
2044
2081
|
attachKeyStream(stdin);
|
|
2045
2082
|
enableBracketedPaste();
|
|
2046
2083
|
enableMouseTracking();
|
|
@@ -2092,7 +2129,28 @@ async function runSession(host) {
|
|
|
2092
2129
|
// stream that no longer exists. Without this a session parked on
|
|
2093
2130
|
// nextKey() at reset time never wakes, and the loop outlives its
|
|
2094
2131
|
// terminal — repainting into whatever stdout it captured.
|
|
2095
|
-
|
|
2132
|
+
//
|
|
2133
|
+
// `nextKey()` maps STREAM_END to null (`events.js`: "this call's
|
|
2134
|
+
// contract is a key or null, and STREAM_END is not a key"), so a
|
|
2135
|
+
// reset arrives here as `null`, not as STREAM_END. Testing for
|
|
2136
|
+
// STREAM_END alone therefore missed every real reset: the loop fed
|
|
2137
|
+
// `null` to handleKey, which threw, and re-parked on a pump that had
|
|
2138
|
+
// been cleared — leaving the promise unsettled. The event loop then
|
|
2139
|
+
// drained on its own and Node exited WITHOUT this function's
|
|
2140
|
+
// `finally`, so `cleanup()` never ran: the alternate screen was never
|
|
2141
|
+
// left, bracketed paste and mouse tracking stayed enabled in the
|
|
2142
|
+
// user's shell, and the "aegiscode --continue" hint never printed.
|
|
2143
|
+
// Measured as two different exit frames from the SAME host — 9561
|
|
2144
|
+
// bytes with teardown, 9120 without — which is what surfaced as
|
|
2145
|
+
// `cli-platform-parity.test.mjs` reporting "linux and darwin wrote
|
|
2146
|
+
// different bytes": a coin flip, not a platform difference.
|
|
2147
|
+
//
|
|
2148
|
+
// Ending the session here is the fix: it resolves the promise so the
|
|
2149
|
+
// teardown below runs on every exit, not only on a clean /exit.
|
|
2150
|
+
if (key === STREAM_END || !key) {
|
|
2151
|
+
endSession();
|
|
2152
|
+
break;
|
|
2153
|
+
}
|
|
2096
2154
|
try {
|
|
2097
2155
|
await handleKey(key);
|
|
2098
2156
|
} catch (err) {
|
|
@@ -2110,7 +2168,8 @@ async function runSession(host) {
|
|
|
2110
2168
|
else process.stdout.off('resize', onResize);
|
|
2111
2169
|
process.off('uncaughtException', crash);
|
|
2112
2170
|
process.off('unhandledRejection', crash);
|
|
2113
|
-
|
|
2171
|
+
process.off('exit', cleanupOnce);
|
|
2172
|
+
cleanupOnce();
|
|
2114
2173
|
resetKeyStream();
|
|
2115
2174
|
}
|
|
2116
2175
|
}
|
package/src/commands.js
CHANGED
|
@@ -2452,7 +2452,14 @@ const COMMANDS = [
|
|
|
2452
2452
|
// land on one this machine can actually run — a provider it holds a key
|
|
2453
2453
|
// for wins, since the alternative 400s on the key rather than the id.
|
|
2454
2454
|
if (res.class === 'byok' && !(c.ctx && c.ctx.model)) {
|
|
2455
|
-
|
|
2455
|
+
// The byok list carries the pooled Nexus row too (app.js
|
|
2456
|
+
// classScopedModels) so the lane is discoverable — but this host cannot
|
|
2457
|
+
// RUN it under byok: the relay reads a pinned id as a provider name, so
|
|
2458
|
+
// auto-pinning Nexus here would fail the very next turn, which is the
|
|
2459
|
+
// failure this whole branch exists to avoid. Pick from this class's own
|
|
2460
|
+
// rows only.
|
|
2461
|
+
const models = ((await c.listModelsFor('byok')) || [])
|
|
2462
|
+
.filter((m) => !m.cls || m.cls === 'byok');
|
|
2456
2463
|
const pick = models.find((m) => m.configured) || models[0];
|
|
2457
2464
|
if (pick) {
|
|
2458
2465
|
c.ctx.model = pick.id;
|
|
@@ -2496,10 +2503,18 @@ const COMMANDS = [
|
|
|
2496
2503
|
const pinned = c.ctx && c.ctx.model;
|
|
2497
2504
|
for (const m of models) {
|
|
2498
2505
|
const on = pinned === m.id;
|
|
2506
|
+
// A row from the OTHER class rides on this list on purpose — the
|
|
2507
|
+
// pooled Nexus row is what makes the byok list able to show a way
|
|
2508
|
+
// back to the pooled lane (app.js classScopedModels). It is not
|
|
2509
|
+
// pinnable here, so it says which class it runs on instead of
|
|
2510
|
+
// looking like a byok id that would 400 at the relay.
|
|
2511
|
+
const other = m.cls && m.cls !== cls;
|
|
2499
2512
|
lines.push([
|
|
2500
2513
|
span(on ? C.green : C.gray, ` ${on ? '●' : '○'} `),
|
|
2501
2514
|
span(on ? C.green : C.gray, String(m.id).padEnd(30)),
|
|
2502
|
-
span(C.gray,
|
|
2515
|
+
span(C.gray, other
|
|
2516
|
+
? `${m.note || m.label || ''} · pooled lane — /class ${m.cls}`
|
|
2517
|
+
: m.note || m.label || ''),
|
|
2503
2518
|
]);
|
|
2504
2519
|
}
|
|
2505
2520
|
lines.push('', [span(C.gray, ' pin one with /model <id> — /model alone opens the picker')]);
|
package/src/events.js
CHANGED
|
@@ -76,7 +76,18 @@ const looksLikePaste = (s) => {
|
|
|
76
76
|
|
|
77
77
|
let attachedStdin = null;
|
|
78
78
|
let onData = null;
|
|
79
|
+
let onEnd = null;
|
|
79
80
|
let suspended = false;
|
|
81
|
+
// Latched: stdin reached EOF and will never produce another key. Lives outside
|
|
82
|
+
// `resetKeyStream()`'s transient state because the loop can ask for a key long
|
|
83
|
+
// after the EOF arrived. Cleared only by attaching a fresh stream.
|
|
84
|
+
let streamEnded = false;
|
|
85
|
+
// Has the interactive SESSION taken the terminal yet? Onboarding attaches a
|
|
86
|
+
// pump of its own before the session loop does (`screens.js` owns that attach
|
|
87
|
+
// and detaches it in a `finally`), so "a pump is attached" is not the same
|
|
88
|
+
// question as "a session owns these keys" — and the difference decides whether
|
|
89
|
+
// the teardown is allowed to destroy what is in the queue. See `drainQueue`.
|
|
90
|
+
let sessionStarted = false;
|
|
80
91
|
|
|
81
92
|
function isKeyStreamSuspended() {
|
|
82
93
|
return suspended;
|
|
@@ -85,6 +96,36 @@ function isKeyStreamSuspended() {
|
|
|
85
96
|
function attachKeyStream(stdin) {
|
|
86
97
|
attachedStdin = stdin;
|
|
87
98
|
everAttached = true;
|
|
99
|
+
// A fresh stream is a fresh session: the previous one's EOF must not be
|
|
100
|
+
// inherited, or the new loop would exit on its first `nextKey()`.
|
|
101
|
+
//
|
|
102
|
+
// …unless this stream is ALREADY finished, which is not the exception but a
|
|
103
|
+
// routine startup state. `end`/`close` are emitted once, to whoever is
|
|
104
|
+
// listening at that instant: the startup background probe resumes and then
|
|
105
|
+
// *pauses* stdin without an end listener (`caps.probeBackground`), and
|
|
106
|
+
// onboarding attaches a pump and detaches it in a `finally`. If the pty's
|
|
107
|
+
// write end closed during either gap, the EOF was announced into an empty
|
|
108
|
+
// room — so clearing the latch here, in the one place that could re-learn the
|
|
109
|
+
// fact, strands the loop in a way the latch exists to prevent: `nextKey()`
|
|
110
|
+
// parks on a stream that will never emit again, the session promise never
|
|
111
|
+
// settles, and the teardown and the resume hint are skipped entirely.
|
|
112
|
+
//
|
|
113
|
+
// Measured on the real binary through a pty: 2 of 12 runs ended at a fully
|
|
114
|
+
// drawn 9120-byte welcome frame with NO teardown at all — no `\x1b[?1049l`,
|
|
115
|
+
// no hint — the command sent at launch (`/exit`) never seen and never
|
|
116
|
+
// resolved. `test/cli-platform-parity.test.mjs` reports that as "linux and
|
|
117
|
+
// darwin wrote different bytes", because whichever platform drew the short
|
|
118
|
+
// frame is compared against one that drew the long one. It is a race, not a
|
|
119
|
+
// platform difference, and the short frame is always the losing side.
|
|
120
|
+
//
|
|
121
|
+
// So: BELIEVE the stream's own state rather than the absence of an event. If
|
|
122
|
+
// it has already ended, keep the latch (a first-time attach that finds a
|
|
123
|
+
// finished stream must set it, not clear it). Launch keystrokes are not lost
|
|
124
|
+
// by doing so — `nextKey()` consults the queues before the latch, so input
|
|
125
|
+
// captured ahead of the session is still delivered and the loop resolves on
|
|
126
|
+
// the first call instead of after it.
|
|
127
|
+
const alreadyEnded = !!(stdin && (stdin.readableEnded === true || stdin.destroyed === true));
|
|
128
|
+
streamEnded = alreadyEnded;
|
|
88
129
|
onData = (chunk) => pushChars(String(chunk));
|
|
89
130
|
// Before the listener goes on, take anything the terminal already delivered
|
|
90
131
|
// while NOTHING owned the stream. That window is real: the startup background
|
|
@@ -110,6 +151,56 @@ function attachKeyStream(stdin) {
|
|
|
110
151
|
stdin.resume();
|
|
111
152
|
stdin.setEncoding('utf8');
|
|
112
153
|
stdin.on('data', onData);
|
|
154
|
+
attachEndHandler(stdin);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* EOF is the end of the session's input, and it must RESOLVE the loop parked on
|
|
159
|
+
* `nextKey()` rather than leave it waiting on a stream that will never speak
|
|
160
|
+
* again. `attachKeyStream` used to register `data` alone, so a closed stdin
|
|
161
|
+
* (ctrl-d, a pty master that went away, a piped caller that finished writing)
|
|
162
|
+
* silently stranded every waiter: the session promise never settled, so
|
|
163
|
+
* `bin/aegiscode.js` never printed the "aegiscode --continue" hint and never
|
|
164
|
+
* reached `process.exit` — the process only left when the event loop happened
|
|
165
|
+
* to drain on its own, which skips the session's `finally` entirely. That is
|
|
166
|
+
* the difference between a 9561-byte exit frame and a 9120-byte one on the
|
|
167
|
+
* SAME host, which `test/cli-platform-parity.test.mjs` reported as "linux and
|
|
168
|
+
* darwin wrote different bytes". A race, not a platform difference.
|
|
169
|
+
*
|
|
170
|
+
* `resetKeyStream()` is the resolution primitive: it drains the queue and hands
|
|
171
|
+
* every parked waiter STREAM_END, which `nextKey()` reports as `null` and the
|
|
172
|
+
* session loop now treats as "the session is over". It is idempotent, so a
|
|
173
|
+
* second EOF (or a teardown that already reset) costs nothing.
|
|
174
|
+
*/
|
|
175
|
+
function attachEndHandler(stdin) {
|
|
176
|
+
onEnd = () => {
|
|
177
|
+
// Latch BEFORE resolving: the loop may not be parked yet, and the latch is
|
|
178
|
+
// what makes the *next* `nextKey()` answer STREAM_END rather than wait on a
|
|
179
|
+
// finished stream.
|
|
180
|
+
streamEnded = true;
|
|
181
|
+
try {
|
|
182
|
+
resetKeyStream();
|
|
183
|
+
} catch {
|
|
184
|
+
/* already reset by a teardown that got there first */
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
try {
|
|
188
|
+
stdin.on('end', onEnd);
|
|
189
|
+
stdin.on('close', onEnd);
|
|
190
|
+
} catch {
|
|
191
|
+
/* a stream without an event surface: the timeout paths still apply */
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Undo `attachEndHandler`. Split out so the detach paths stay symmetric. */
|
|
196
|
+
function detachEndHandler(stdin) {
|
|
197
|
+
if (!stdin || !onEnd) return;
|
|
198
|
+
try {
|
|
199
|
+
stdin.removeListener('end', onEnd);
|
|
200
|
+
stdin.removeListener('close', onEnd);
|
|
201
|
+
} catch {
|
|
202
|
+
/* stream already gone */
|
|
203
|
+
}
|
|
113
204
|
}
|
|
114
205
|
|
|
115
206
|
/** Hand the terminal to a child process: detach the pump and drop half-parsed
|
|
@@ -118,6 +209,9 @@ function suspendKeyStream() {
|
|
|
118
209
|
if (!attachedStdin || suspended) return;
|
|
119
210
|
suspended = true;
|
|
120
211
|
attachedStdin.removeListener('data', onData);
|
|
212
|
+
// A handover is not an end: the child owns the terminal now, and an EOF on
|
|
213
|
+
// the paused stream must not be read as "the session is over".
|
|
214
|
+
detachEndHandler(attachedStdin);
|
|
121
215
|
clearTimeout(escTimer);
|
|
122
216
|
seq = '';
|
|
123
217
|
buffer = '';
|
|
@@ -144,6 +238,7 @@ function resumeKeyStream() {
|
|
|
144
238
|
attachedStdin.resume();
|
|
145
239
|
attachedStdin.setEncoding('utf8');
|
|
146
240
|
attachedStdin.on('data', onData);
|
|
241
|
+
attachEndHandler(attachedStdin);
|
|
147
242
|
}
|
|
148
243
|
|
|
149
244
|
function flushPaste() {
|
|
@@ -341,10 +436,48 @@ function dispatch(key) {
|
|
|
341
436
|
}
|
|
342
437
|
}
|
|
343
438
|
|
|
439
|
+
/**
|
|
440
|
+
* Has the attached stream already finished?
|
|
441
|
+
*
|
|
442
|
+
* Ground truth, asked of the stream rather than remembered as an event. `end`
|
|
443
|
+
* and `close` are announced once, to whoever happens to be listening: the
|
|
444
|
+
* startup background probe resumes and pauses stdin with no end listener
|
|
445
|
+
* (`caps.probeBackground`), onboarding attaches a pump and detaches it in a
|
|
446
|
+
* `finally`, and `resume()` itself can flush a pending `end` in the window
|
|
447
|
+
* between the check in `attachKeyStream` and `attachEndHandler` going on. In any
|
|
448
|
+
* of those the EOF is announced into an empty room and the `streamEnded` latch
|
|
449
|
+
* stays false forever, so a parked `nextKey()` waits on a stream that will never
|
|
450
|
+
* speak again.
|
|
451
|
+
*
|
|
452
|
+
* That is the whole of the 9120-byte exit frame: the loop parks, the session
|
|
453
|
+
* promise never settles, and the teardown and resume hint are skipped — 2 of 12
|
|
454
|
+
* runs of the real binary through a pty, reported by the parity suite as a
|
|
455
|
+
* cross-platform byte difference because the short frame was measured against a
|
|
456
|
+
* long one. Asking the stream makes the answer independent of who was listening.
|
|
457
|
+
*/
|
|
458
|
+
function streamFinished() {
|
|
459
|
+
if (!attachedStdin) return false;
|
|
460
|
+
return attachedStdin.readableEnded === true || attachedStdin.destroyed === true;
|
|
461
|
+
}
|
|
462
|
+
|
|
344
463
|
/** The next key, waiting as long as it takes. */
|
|
345
464
|
function nextKey() {
|
|
346
465
|
if (queue.length) return Promise.resolve(queue.shift());
|
|
347
466
|
if (preSessionKeys.length) return Promise.resolve(preSessionKeys.shift());
|
|
467
|
+
// The stream is over and nothing is queued: answer STREAM_END instead of
|
|
468
|
+
// parking. Without this latch the EOF resolution is a one-instant event —
|
|
469
|
+
// `resetKeyStream()` wakes the waiters that happen to be parked when it runs,
|
|
470
|
+
// and a loop that was mid-turn at that moment simply parks again on a stream
|
|
471
|
+
// that is finished and will never emit another byte. That is the hang: the
|
|
472
|
+
// session never settles, so the teardown and the resume hint are skipped and
|
|
473
|
+
// the process only ends when the event loop drains by itself.
|
|
474
|
+
//
|
|
475
|
+
// The latch is checked *together with* the stream's own state, because the
|
|
476
|
+
// latch can only record an event that was heard (see `streamFinished`). Order
|
|
477
|
+
// matters and is deliberate: both queues are consulted first, so a command
|
|
478
|
+
// captured at launch is still delivered and this returns it rather than the
|
|
479
|
+
// end of input.
|
|
480
|
+
if (streamEnded || streamFinished()) return Promise.resolve(STREAM_END);
|
|
348
481
|
return new Promise((resolve) => waiting.push(resolve));
|
|
349
482
|
}
|
|
350
483
|
|
|
@@ -383,9 +516,46 @@ function drainQueue() {
|
|
|
383
516
|
// Only this session's keys. `preSessionKeys` is deliberately left alone: it
|
|
384
517
|
// holds input captured before any session existed, and clearing it here is
|
|
385
518
|
// exactly the bug its comment describes.
|
|
519
|
+
//
|
|
520
|
+
// …and while no session has started, nothing in the queue is "this session's"
|
|
521
|
+
// either — it is input aimed at the session that has not begun yet, so it is
|
|
522
|
+
// moved rather than dropped. Onboarding is why: it attaches the pump *before*
|
|
523
|
+
// the session loop does, so the launch command lands in `queue` (a pump is
|
|
524
|
+
// attached, `everAttached` is true, so `dispatch` puts it there and NOT in
|
|
525
|
+
// `preSessionKeys`), and its `finally` then detaches — draining the queue and
|
|
526
|
+
// destroying the command as it goes. The session that follows has nothing to
|
|
527
|
+
// read and no EOF pending (`script` keeps the pty open until the command
|
|
528
|
+
// exits), so it parks on `nextKey()` forever: the teardown never runs, the
|
|
529
|
+
// resume hint is never printed, and the process only ends when an outer
|
|
530
|
+
// deadline kills it. Measured through a pty on the real binary, a hung run
|
|
531
|
+
// observed from inside the process showed `dataListeners=1 endListeners=3
|
|
532
|
+
// ended=false isRaw=true` — attached, raw, alive, waiting on input that had
|
|
533
|
+
// already been thrown away. That is 4 of 16 runs, and it is the entire
|
|
534
|
+
// cli-platform-parity flake: the losing run writes a 9120-byte frame with no
|
|
535
|
+
// `\x1b[?1049l` and no hint, and the suite compares it against a 9561-byte
|
|
536
|
+
// one from another platform and calls the difference a platform divergence.
|
|
537
|
+
//
|
|
538
|
+
// Order is preserved (a FIFO move, not a stack) so `/exit\r` still arrives as
|
|
539
|
+
// `/`, `e`, `x`, `i`, `t`, enter. Once the session has started, the old
|
|
540
|
+
// contract holds unchanged: typed-ahead from a dead session must not land in
|
|
541
|
+
// the next one.
|
|
542
|
+
if (!sessionStarted) {
|
|
543
|
+
while (queue.length) preSessionKeys.push(queue.shift());
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
386
546
|
queue.length = 0;
|
|
387
547
|
}
|
|
388
548
|
|
|
549
|
+
/**
|
|
550
|
+
* The interactive session has taken the terminal: from here on the queue
|
|
551
|
+
* belongs to it, and `drainQueue()` may destroy what it holds. Called by the
|
|
552
|
+
* session loop immediately before it attaches (`chatflow.js`), never by a
|
|
553
|
+
* transient screen pump.
|
|
554
|
+
*/
|
|
555
|
+
function markSessionStart() {
|
|
556
|
+
sessionStarted = true;
|
|
557
|
+
}
|
|
558
|
+
|
|
389
559
|
/** Test seam: forget attached stdin, timers and queued keys. */
|
|
390
560
|
/** True once a key stream is attached — i.e. whether nextKey() has a source. */
|
|
391
561
|
function isKeyStreamAttached() {
|
|
@@ -408,6 +578,7 @@ function detachKeyStream() {
|
|
|
408
578
|
} catch {
|
|
409
579
|
/* stream already gone */
|
|
410
580
|
}
|
|
581
|
+
detachEndHandler(attachedStdin);
|
|
411
582
|
try {
|
|
412
583
|
if (typeof attachedStdin.setRawMode === 'function') attachedStdin.setRawMode(false);
|
|
413
584
|
} catch {
|
|
@@ -444,6 +615,7 @@ function resetKeyStream() {
|
|
|
444
615
|
}
|
|
445
616
|
attachedStdin = null;
|
|
446
617
|
onData = null;
|
|
618
|
+
onEnd = null;
|
|
447
619
|
suspended = false;
|
|
448
620
|
}
|
|
449
621
|
|
|
@@ -467,5 +639,6 @@ module.exports = {
|
|
|
467
639
|
nextKeyTimeout,
|
|
468
640
|
requeueKeys,
|
|
469
641
|
drainQueue,
|
|
642
|
+
markSessionStart,
|
|
470
643
|
resetKeyStream,
|
|
471
644
|
};
|
package/src/render.js
CHANGED
|
@@ -242,8 +242,9 @@ function assistantLines(ctx, text, width) {
|
|
|
242
242
|
|
|
243
243
|
/**
|
|
244
244
|
* One transcript turn: a `❯`-prefixed user line, an assistant answer whose
|
|
245
|
-
* first line carries the `●` marker (and a
|
|
246
|
-
* line with its `⎿ $ …` hook row, and
|
|
245
|
+
* first line carries the `●` marker (and, on a multi-line streaming answer, a
|
|
246
|
+
* `●` cursor on its last line), a tool line with its `⎿ $ …` hook row, and
|
|
247
|
+
* each turn's accounting line beneath.
|
|
247
248
|
*/
|
|
248
249
|
function renderTurn(ctx, turn, width = 80) {
|
|
249
250
|
const t = themeOf(ctx);
|
|
@@ -261,7 +262,9 @@ function renderTurn(ctx, turn, width = 80) {
|
|
|
261
262
|
const body = assistantLines(ctx, text, Math.max(8, width - 2));
|
|
262
263
|
if (!body.length) body.push('');
|
|
263
264
|
body[0] = `${t.white}${GLYPH.block}${RESET} ` + body[0];
|
|
264
|
-
|
|
265
|
+
// Two lines minimum, or the cursor lands on the marker's own line and a
|
|
266
|
+
// one-line answer prints `● Working●` — two dots where there should be one.
|
|
267
|
+
if (turn.streaming && body.length > 1) body[body.length - 1] += `${t.white}${GLYPH.block}${RESET}`;
|
|
265
268
|
lines.push(...body);
|
|
266
269
|
} else if (role === 'tool') {
|
|
267
270
|
// turn.ok is only known once the call has actually run (the agent loop's
|
package/src/screen.js
CHANGED
|
@@ -216,7 +216,31 @@ function leaveAltScreen() {
|
|
|
216
216
|
if (!inAlt) return;
|
|
217
217
|
inAlt = false;
|
|
218
218
|
process.stdout.write('\x1b[?1049l');
|
|
219
|
+
flushExitNotice();
|
|
219
220
|
}
|
|
221
|
+
|
|
222
|
+
// ── exit notice: text that must land OUTSIDE the alternate screen ───────────
|
|
223
|
+
// The resume hint is written on the way out, and its *position* is the whole
|
|
224
|
+
// contract: written inside the screen, the restore that follows paints over it
|
|
225
|
+
// and the user is told nothing. Ordering cannot deliver that guarantee here —
|
|
226
|
+
// the teardown is reachable from three places (the session loop's `finally`,
|
|
227
|
+
// the session's `process.once('exit')` handler, and the entrypoint's own exit
|
|
228
|
+
// handler) and which one runs first varies with timing, which showed up as the
|
|
229
|
+
// same 81 bytes in two positions across runs and platforms. So the notice is
|
|
230
|
+
// not *scheduled* next to the restore; it is emitted *by* the restore, cleared
|
|
231
|
+
// as it goes, which makes "outside the screen" true by construction and lets
|
|
232
|
+
// only one of the paths ever produce it.
|
|
233
|
+
let exitNotice = null;
|
|
234
|
+
function queueExitNotice(text) {
|
|
235
|
+
exitNotice = text;
|
|
236
|
+
}
|
|
237
|
+
function flushExitNotice() {
|
|
238
|
+
if (exitNotice === null) return;
|
|
239
|
+
const text = exitNotice;
|
|
240
|
+
exitNotice = null;
|
|
241
|
+
process.stdout.write(text);
|
|
242
|
+
}
|
|
243
|
+
const hasExitNotice = () => exitNotice !== null;
|
|
220
244
|
const isAltScreen = () => inAlt;
|
|
221
245
|
|
|
222
246
|
// Bracketed paste (DEC 2004): a paste arrives as one \x1b[200~ … \x1b[201~
|
|
@@ -436,6 +460,9 @@ class LiveRegion {
|
|
|
436
460
|
}
|
|
437
461
|
|
|
438
462
|
module.exports = {
|
|
463
|
+
queueExitNotice,
|
|
464
|
+
flushExitNotice,
|
|
465
|
+
hasExitNotice,
|
|
439
466
|
// plain-string width helpers (kept for the string renderers + app/bin)
|
|
440
467
|
w,
|
|
441
468
|
clip,
|
package/vendor/client/aegis.js
CHANGED
|
@@ -650,6 +650,12 @@ function createClient(opts = {}) {
|
|
|
650
650
|
includeUsage,
|
|
651
651
|
signal,
|
|
652
652
|
extra,
|
|
653
|
+
// Opt-in only: the caller (the agent loop, which already knows whether
|
|
654
|
+
// the tools it just attached to `extra` are code-capable) decides
|
|
655
|
+
// whether this turn needs one — this file has no policy of its own for
|
|
656
|
+
// when to attach it, only how. See aegis1 services/agent_gate.py
|
|
657
|
+
// CODE_TOOLS for the server-side verb list a caller should mirror.
|
|
658
|
+
agentKey,
|
|
653
659
|
} = {}) {
|
|
654
660
|
// Model-first: an explicit `model` pins that provider id verbatim; with no
|
|
655
661
|
// model the server picks its default (no client-invented tier id). `mode`
|
|
@@ -670,8 +676,9 @@ function createClient(opts = {}) {
|
|
|
670
676
|
};
|
|
671
677
|
if (model) body.model = model;
|
|
672
678
|
else if (mode) body.mode = mode;
|
|
679
|
+
const headers = authHeaders(agentKey ? { 'X-AEGIS-Agent-Key': agentKey } : undefined);
|
|
673
680
|
if (!stream || typeof onStream !== 'function') {
|
|
674
|
-
return apiPost('/api/v1/chat/completions', { ...body, stream: false });
|
|
681
|
+
return apiPost('/api/v1/chat/completions', { ...body, stream: false }, headers);
|
|
675
682
|
}
|
|
676
683
|
// An OpenAI-compatible SSE response carries no `usage` unless the caller
|
|
677
684
|
// asks for it (`stream_options.include_usage`). Without this the streamed
|
|
@@ -682,7 +689,7 @@ function createClient(opts = {}) {
|
|
|
682
689
|
// is invalid alongside `stream:false`, so the non-stream branch above and
|
|
683
690
|
// every non-stream fallback must stay clean.
|
|
684
691
|
if (includeUsage) body.stream_options = { include_usage: true };
|
|
685
|
-
return postStream('/api/v1/chat/completions', body,
|
|
692
|
+
return postStream('/api/v1/chat/completions', body, headers, onStream, signal, {
|
|
686
693
|
onReasoning,
|
|
687
694
|
idleTimeoutMs,
|
|
688
695
|
// A server that predates `stream_options` 400s the whole request; the
|