aegiscode 6.8.6 → 6.8.8

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/src/caps.js CHANGED
@@ -3,35 +3,39 @@
3
3
  /**
4
4
  * Terminal capabilities — ONE probe, resolved once, consulted by everything.
5
5
  *
6
- * The UI used to decide how to draw itself from `process.platform`: Windows got
7
- * the ASCII stencil, macOS got a half-stencil, Linux got the native mark. That
8
- * is the wrong axis. The platform of the *process* says nothing about the
9
- * terminal it is talking to, and the two most common real-world runtimes prove
10
- * it:
6
+ * POLICY: aegiscode renders ONE way. `process.platform`, `TERM`, `TERM_PROGRAM`,
7
+ * the locale's character set and the Windows terminal markers are all still
8
+ * probed — because the support surface reports them — but not one of them may
9
+ * move a single byte of what is drawn. PowerShell in Windows Terminal,
10
+ * Terminal.app, a legacy Windows console, WSL and zsh over SSH all get the same
11
+ * palette, the same mark, the same colour depth, the same control sequences and
12
+ * the same `\n`. A screenshot of one is a screenshot of all of them.
11
13
  *
12
- * · PowerShell *inside Windows Terminal* (`WT_SESSION` set) draws
13
- * █▓▒░▐▛▜▌▝▘ and ✦ exactly like zsh does — but `process.platform` is still
14
- * `win32`, so it was being handed the hyphen-and-hash ASCII mark.
15
- * · `zsh` over SSH from a Windows box, inside `mintty`/`ConEmu`, is `linux`
16
- * on the far side and `win32` on the client — the mark must be chosen once,
17
- * from what the *terminal* can draw.
14
+ * What used to differ by platform or environment, and no longer does:
18
15
  *
19
- * So the decision is made from the environment the process is actually running
20
- * in — `TERM`, `COLORTERM`, `TERM_PROGRAM`, the Windows terminal markers
21
- * (`WT_SESSION`, `ConEmuANSI`, `ANSICON`, …), the locale's character set, and
22
- * whether stdout is a TTY. Every axis is overridable, because only the person
23
- * looking at the screen can settle the cases no environment variable can
24
- * (a Windows console with a font that lacks the block glyphs, a macOS terminal
25
- * that renders ✦ as a wide emoji).
16
+ * · Windows got the ASCII stencil because `process.platform` was `win32` —
17
+ * even *inside Windows Terminal*, which draws █▓▒░▐▛▜▌▝▘ and ✦ exactly like
18
+ * zsh does.
19
+ * · Terminal.app got a half-stencil (edges stencilled, star narrowed) because
20
+ * its font stack lacks ▐▛▜▌▝▘ and renders ✦ through the wide emoji fallback.
21
+ * · A non-UTF-8 locale, or `TERM=dumb`, dropped the whole UI to ASCII — so the
22
+ * same build looked like a different product in `LANG=C` CI.
23
+ * · `TERM=linux|vt100|ansi` and a legacy console flattened truecolor to the 16
24
+ * ANSI slots.
25
+ * · A redirected run on Windows wrote CRLF; the same run on Linux wrote LF.
26
+ *
27
+ * The probe is not gone, it is just no longer *implicit*. Every axis below is
28
+ * still overridable, and an override is now the ONLY thing that changes the
29
+ * rendering. All of them are environment variables and flags, so they are
30
+ * spelled and behave identically on every platform, and the resulting look is
31
+ * reproducible from a bug report without asking what OS it came from.
26
32
  *
27
33
  * The axes:
28
- * glyphs 'unicode' | 'ascii' — can the mark's palette be drawn at all?
29
- * face 'native' | 'edges' — do the quartile edges ▐▛▜▌▝▘ exist in the
30
- * terminal's font, or must they be stencilled?
31
- * star 'native' | 'narrow' — is ✦ a single-cell text glyph, or does the
32
- * terminal fall back to a wide emoji?
33
- * depth 24 | 8 | 4 | 0 — truecolor, 256, the 16 ANSI slots, none
34
- * vt boolean — are cursor/erase escapes honoured?
34
+ * glyphs 'unicode' | 'ascii' — the mark's palette (unicode by default)
35
+ * face 'native' | 'edges' — the quartile edges ▐▛▜▌▝▘
36
+ * star 'native' | 'narrow' — ✦ as a single-cell glyph, or stencilled to `*`
37
+ * depth 24 | 8 | 4 | 0 — truecolor by default
38
+ * vt boolean — cursor/erase escapes (on by default)
35
39
  * cols/rows — the frame, with `AEGIS_WIDTH`/`COLUMNS` over
36
40
  * the reported size and a stable 80 fallback,
37
41
  * wideRunes — runes the terminal draws two cells wide
@@ -43,6 +47,7 @@
43
47
  * AEGIS_ART=unicode|ascii AEGIS_ASCII=1 AEGIS_UNICODE=0|1
44
48
  * AEGIS_STAR=native|narrow AEGIS_COLOR=0|1|2|3
45
49
  * AEGIS_VT=0|1 AEGIS_WIDTH=<cols> AEGIS_WIDE_RUNES=<runes>
50
+ * AEGIS_FORCE_TRUECOLOR=0 NO_COLOR FORCE_COLOR=0|1|2|3
46
51
  *
47
52
  * `caps()` is cached; `setCaps()` re-resolves and notifies subscribers, which is
48
53
  * how `theme.js` re-materialises its palette and glyph table when the user runs
@@ -63,11 +68,15 @@ const RE_ZERO = /[\u0300-\u036F\u1AB0-\u1AFF\u1DC0-\u1DFF\u20D0-\u20FF\uFE00-\uF
63
68
  const RE_WIDE = /[\u1100-\u115F\u2E80-\u303E\u3041-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uA000-\uA4CF\uA960-\uA97F\uAC00-\uD7A3\uF900-\uFAFF\uFE10-\uFE19\uFE30-\uFE4F\uFF00-\uFF60\uFFE0-\uFFE6\u{1F1E6}-\u{1F1FF}\u{1F300}-\u{1F64F}\u{1F680}-\u{1F6FF}\u{1F900}-\u{1F9FF}\u{1FA70}-\u{1FAFF}\u{20000}-\u{3FFFD}]/u;
64
69
 
65
70
  /**
66
- * Windows terminals that draw the full mark. `WT_SESSION` is set by Windows
71
+ * Windows terminals that can draw the full mark. `WT_SESSION` is set by Windows
67
72
  * Terminal for every shell it hosts (pwsh, cmd, wsl, git-bash); `TERM_PROGRAM`
68
73
  * by VS Code and the other Electron/ConPTY hosts; the rest are the classic
69
- * ConPTY-era consoles. Presence of any of them means the console is VT-capable
70
- * and its default font carries the block-element range.
74
+ * ConPTY-era consoles.
75
+ *
76
+ * Reported, never consulted: the rendering is uniform whether one of these is
77
+ * set or not (see the policy at the top of this file). The list survives so
78
+ * `--terminal` can say "Windows VT host (WT_SESSION)" in a bug report, which is
79
+ * how a support thread knows the user's console can draw what we draw.
71
80
  */
72
81
  const MODERN_WINDOWS = [
73
82
  'WT_SESSION',
@@ -80,27 +89,6 @@ const MODERN_WINDOWS = [
80
89
  'KITTY_WINDOW_ID',
81
90
  ];
82
91
 
83
- /**
84
- * `TERM_PROGRAM` values whose font renders the whole palette natively, `✦`
85
- * included. `Apple_Terminal` is deliberately NOT here — see `resolveGlyphs`.
86
- */
87
- const NATIVE_TERM_PROGRAMS = new Set([
88
- 'vscode',
89
- 'vscode-insiders',
90
- 'iTerm.app',
91
- 'WezTerm',
92
- 'kitty',
93
- 'ghostty',
94
- 'Alacritty',
95
- 'Hyper',
96
- 'Tabby',
97
- 'WarpTerminal',
98
- 'Warp',
99
- 'rio',
100
- 'mintty',
101
- 'JetBrains-JediTerm',
102
- ]);
103
-
104
92
  const TRUTHY_RE = /^(1|true|yes|on)$/i;
105
93
 
106
94
  /** Is an env value "on"? (`WT_SESSION=` with no value still counts.) */
@@ -124,20 +112,16 @@ function modernWindows(env) {
124
112
 
125
113
  /**
126
114
  * The locale's character set. POSIX terminals signal UTF-8 support here and
127
- * nowhere else: a terminal launched with `LANG=C` cannot draw ░▒▓, and no
128
- * amount of TERM sniffing will tell you that.
115
+ * nowhere else. Recorded for the support surface only: a `LANG=C` shell renders
116
+ * the same Unicode mark as a UTF-8 one, so that the same build cannot look like
117
+ * a different product on a differently-configured host. A terminal whose font
118
+ * genuinely lacks the palette is a deliberate `AEGIS_ART=ascii`.
129
119
  */
130
120
  function localeOf(env) {
131
121
  const hit = firstEnv(env, ['LC_ALL', 'LC_CTYPE', 'LANG']);
132
122
  return hit ? hit.value : '';
133
123
  }
134
124
 
135
- function utf8Locale(env) {
136
- const loc = localeOf(env);
137
- if (!loc) return true; // unset: every shell we ship to defaults to UTF-8
138
- return /utf-?8/i.test(loc);
139
- }
140
-
141
125
  /** The explicit art mode, or 'auto'. */
142
126
  function artMode(env) {
143
127
  const raw = String(env.AEGIS_ART || '').trim().toLowerCase();
@@ -149,10 +133,24 @@ function artMode(env) {
149
133
  }
150
134
 
151
135
  /**
152
- * Resolve what the mark may be drawn with. Order matters: an explicit request
153
- * always wins, then the terminal's own declarations, then the two signals that
154
- * mean "this cannot work" (a dumb terminal, a non-UTF-8 locale), then the
155
- * legacy-Windows console.
136
+ * What the mark is drawn with — the same answer on every platform.
137
+ *
138
+ * Only an explicit request moves this: `AEGIS_ART` (the valued switch),
139
+ * `AEGIS_ASCII`/`AEGIS_UNICODE` (the booleans it supersedes), or `AEGIS_STAR`
140
+ * for a font that renders ✦ through a wide emoji fallback. Everything else —
141
+ * `process.platform`, `TERM`, `TERM_PROGRAM`, `WT_SESSION`, the locale's
142
+ * character set — is recorded in the resolved `reasons` for the support surface
143
+ * and then deliberately ignored.
144
+ *
145
+ * The old behaviour read those signals and silently downgraded: a `win32`
146
+ * process outside a ConPTY host, an Apple Terminal, `TERM=dumb` and any
147
+ * non-UTF-8 locale each got a different product. That made the same build look
148
+ * unlike itself from one machine to the next, and it made the *reason* for the
149
+ * difference invisible to the person looking at the screen. A terminal that
150
+ * genuinely cannot draw the palette now says so with the documented override,
151
+ * which is a decision the user makes once and the support surface can report.
152
+ *
153
+ * `platform` is accepted (and reported) but never branched on.
156
154
  */
157
155
  function resolveGlyphs(env, platform) {
158
156
  const starPref = String(env.AEGIS_STAR || '').trim().toLowerCase();
@@ -170,56 +168,11 @@ function resolveGlyphs(env, platform) {
170
168
  return { glyphs: 'unicode', face: 'native', star: star || 'native', reason: 'AEGIS_ART=unicode' };
171
169
  }
172
170
 
173
- if (String(env.TERM || '').toLowerCase() === 'dumb') {
174
- return { glyphs: 'ascii', face: 'edges', star: 'narrow', reason: 'TERM=dumb' };
175
- }
176
-
177
- if (platform === 'win32') {
178
- const modern = modernWindows(env);
179
- if (!modern) {
180
- return {
181
- glyphs: 'ascii',
182
- face: 'edges',
183
- star: 'narrow',
184
- reason: 'legacy Windows console (no WT_SESSION/ConEmu/TERM_PROGRAM) — Consolas has no block glyphs',
185
- };
186
- }
187
- // A ConPTY host (Windows Terminal, VS Code, ConEmu) renders the block
188
- // palette, and ConPTY does font fallback — Cascadia Mono lacks ✦ but the
189
- // system symbol fonts carry it single-width, so the star is drawn natively
190
- // and the welcome mark is byte-identical to the one zsh prints. That parity
191
- // is the point: `star: 'narrow'` here would swap all eleven ✦ in the mark
192
- // for '*', which is the difference a PowerShell user sees.
193
- //
194
- // The escape hatches are for the font that does *not* fall back cleanly:
195
- // AEGIS_STAR=narrow swaps ✦ for '*', AEGIS_WIDE_RUNES=✦ tells the width
196
- // maths the glyph is two cells wide (which is what a bad fallback actually
197
- // breaks — the row alignment, not the rune).
198
- return {
199
- glyphs: 'unicode',
200
- face: 'native',
201
- star: star || 'native',
202
- reason: `Windows VT host (${modern.name})`,
203
- };
204
- }
205
-
206
- if (!utf8Locale(env)) {
207
- return { glyphs: 'ascii', face: 'edges', star: 'narrow', reason: `locale is not UTF-8 (${localeOf(env)})` };
208
- }
209
-
210
- const program = String(env.TERM_PROGRAM || '');
211
- if (platform === 'darwin' && program === 'Apple_Terminal') {
212
- // Terminal.app's font stack has no ▐▛▜▌▝▘ and renders ✦ with the emoji
213
- // fallback (two cells), so it gets the edge pass and a narrow star — the
214
- // shipped behaviour on macOS, now expressed as a capability, not a platform.
215
- return { glyphs: 'unicode', face: 'edges', star: 'narrow', reason: 'Apple_Terminal font stack' };
216
- }
217
-
218
171
  return {
219
172
  glyphs: 'unicode',
220
173
  face: 'native',
221
174
  star: star || 'native',
222
- reason: program ? `${program} terminal` : `TERM=${env.TERM || 'unset'}`,
175
+ reason: `uniform default on every platform${star ? ` (AEGIS_STAR=${starPref})` : ''}`,
223
176
  };
224
177
  }
225
178
 
@@ -227,15 +180,19 @@ function resolveGlyphs(env, platform) {
227
180
  * Colour depth in bits per channel: 24 truecolor, 8 the 256-colour cube, 4 the
228
181
  * 16 ANSI slots, 0 no colour at all.
229
182
  *
230
- * The default is truecolor, because that is the honest answer for every
231
- * terminal we can name: the 256-colour terminals in use today all pass 24-bit
232
- * SGR through, and a terminal that cannot simply downsamples it itself — which
233
- * is the correct authority for the decision. Only a provably narrow terminal
234
- * (legacy Windows console, `TERM=linux|vt100|ansi`, `NO_COLOR`) is downgraded,
235
- * so the palette cannot be silently flattened on a machine that could have
236
- * shown it.
183
+ * Truecolor, always, unless the user says otherwise. That is the honest default
184
+ * for every terminal in use: the 256-colour terminals all pass 24-bit SGR
185
+ * through, and a terminal that cannot simply downsamples it itself — which is
186
+ * the correct authority for the decision. The old probe tried to guess the
187
+ * terminal's ceiling from `TERM`, from the platform and from the Windows console
188
+ * markers, and every wrong guess flattened the palette on a machine that could
189
+ * have shown it *and* made Windows differ from Linux on identical hardware.
190
+ *
191
+ * `NO_COLOR` (non-empty), `FORCE_COLOR=0` and `AEGIS_COLOR=0` are the only ways
192
+ * to get depth 0; `FORCE_COLOR`/`AEGIS_COLOR` pick 4, 8 or 24; the older
193
+ * `AEGIS_FORCE_TRUECOLOR=0` still selects the 256-colour cube.
237
194
  */
238
- function resolveDepth(env, platform, isTTY) {
195
+ function resolveDepth(env) {
239
196
  if (env.NO_COLOR != null && env.NO_COLOR !== '') return { depth: 0, reason: 'NO_COLOR' };
240
197
  const forced = String(env.FORCE_COLOR || '').trim();
241
198
  if (forced === '0') return { depth: 0, reason: 'FORCE_COLOR=0' };
@@ -246,47 +203,23 @@ function resolveDepth(env, platform, isTTY) {
246
203
  if (/^[0-3]$/.test(aegis)) {
247
204
  return { depth: aegis === '0' ? 0 : aegis === '1' ? 4 : aegis === '2' ? 8 : 24, reason: `AEGIS_COLOR=${aegis}` };
248
205
  }
249
- if (String(env.COLORTERM || '').trim() !== '' && /^(truecolor|24bit)$/i.test(env.COLORTERM)) {
250
- return { depth: 24, reason: `COLORTERM=${env.COLORTERM}` };
251
- }
252
- const term = String(env.TERM || '').toLowerCase();
253
- if (term === 'dumb') return { depth: 0, reason: 'TERM=dumb' };
254
- if (platform === 'win32') {
255
- const modern = modernWindows(env);
256
- if (modern) return { depth: 24, reason: `Windows VT host (${modern.name})` };
257
- // The classic console: 16 slots, and only when it is actually a console.
258
- if (isTTY) return { depth: 4, reason: 'legacy Windows console' };
259
- return { depth: 24, reason: 'redirected output' };
260
- }
261
- if (/^(linux|vt100|vt220|ansi|cons25|sun|dumb)$/.test(term) || /^xterm-color$/.test(term)) {
262
- return { depth: 4, reason: `TERM=${term}` };
263
- }
264
- if (env.AEGIS_FORCE_TRUECOLOR === '0' || env.TERM_PROGRAM === 'Apple_Terminal') {
265
- // Terminal.app advertises truecolor and honours 24-bit SGR on every release
266
- // that still runs, so it keeps its depth; the check exists for the older
267
- // builds that dithered. AEGIS_FORCE_TRUECOLOR=0 is the user's override.
268
- if (env.AEGIS_FORCE_TRUECOLOR === '0') return { depth: 8, reason: 'AEGIS_FORCE_TRUECOLOR=0' };
269
- }
270
- return { depth: 24, reason: term ? `TERM=${term}` : 'default' };
206
+ if (env.AEGIS_FORCE_TRUECOLOR === '0') return { depth: 8, reason: 'AEGIS_FORCE_TRUECOLOR=0' };
207
+ return { depth: 24, reason: 'uniform default: truecolor on every platform' };
271
208
  }
272
209
 
273
210
  /**
274
- * Cursor and erase control. Never claimed for a dumb terminal or redirected
275
- * output on Windows (where `\x1b[2K` would be printed as text), and always
276
- * assumed for a POSIX terminal — the live spinner line depends on it.
211
+ * Cursor and erase control — on everywhere, since every terminal we ship to
212
+ * honours it, and off only for an explicit `AEGIS_VT=0`. The old probe guessed
213
+ * from the platform and the console markers, which meant the live spinner line
214
+ * behaved differently in a Windows console than in a Linux one; a redirected
215
+ * Windows run is no longer treated as a terminal that would print `\x1b[2K` as
216
+ * text, because that is the same run everywhere and should render the same.
277
217
  */
278
- function resolveVt(env, platform, isTTY) {
218
+ function resolveVt(env) {
279
219
  const pref = String(env.AEGIS_VT == null ? '' : env.AEGIS_VT).trim();
280
220
  if (pref === '0') return { vt: false, reason: 'AEGIS_VT=0' };
281
221
  if (pref === '1' || /^(true|yes|on)$/i.test(pref)) return { vt: true, reason: 'AEGIS_VT=1' };
282
- if (String(env.TERM || '').toLowerCase() === 'dumb') return { vt: false, reason: 'TERM=dumb' };
283
- if (platform === 'win32') {
284
- const modern = modernWindows(env);
285
- if (modern) return { vt: true, reason: `Windows VT host (${modern.name})` };
286
- if (isTTY) return { vt: true, reason: 'console host (Node enables VT processing)' };
287
- return { vt: false, reason: 'redirected output' };
288
- }
289
- return { vt: true, reason: 'POSIX terminal' };
222
+ return { vt: true, reason: 'uniform default: ansi on every platform' };
290
223
  }
291
224
 
292
225
  /**
@@ -323,24 +256,33 @@ function resolveWideRunes(env) {
323
256
 
324
257
  /**
325
258
  * Resolve every axis, purely, from an environment and a stream. Exported so the
326
- * platform matrix is a plain function call in a test — no subprocess, no TTY.
259
+ * uniformity invariant is a plain function call in a test — no subprocess, no
260
+ * TTY.
261
+ *
262
+ * `platform` is recorded in the result and threaded into the *reasons*, so a
263
+ * support paste still says which host it came from; nothing else reads it. The
264
+ * line ending is `\n` on every platform, including a redirected run on Windows:
265
+ * the one divergence the previous probe allowed is exactly the kind of thing
266
+ * that makes two identical invocations produce different bytes.
327
267
  *
328
268
  * @param {object} [env] defaults to `process.env`
329
269
  * @param {object} [stdout] defaults to `process.stdout`
330
- * @param {string} [platform] defaults to `process.platform`
270
+ * @param {string} [platform] defaults to `process.platform` (reported, not consulted)
331
271
  */
332
272
  function detect(env = process.env, stdout = process.stdout, platform = process.platform) {
333
273
  const isTTY = !!(stdout && stdout.isTTY);
334
274
  const glyph = resolveGlyphs(env, platform);
335
- const color = resolveDepth(env, platform, isTTY);
336
- const vt = resolveVt(env, platform, isTTY);
275
+ const color = resolveDepth(env);
276
+ const vt = resolveVt(env);
337
277
  const size = resolveSize(env, stdout);
278
+ const host = platform === 'win32' ? modernWindows(env) : null;
338
279
  return {
339
280
  platform,
340
281
  isTTY,
341
282
  term: String(env.TERM || ''),
342
283
  termProgram: String(env.TERM_PROGRAM || ''),
343
284
  locale: localeOf(env),
285
+ windowsHost: host ? host.name : '',
344
286
  glyphs: glyph.glyphs,
345
287
  face: glyph.face,
346
288
  star: glyph.star,
@@ -349,7 +291,7 @@ function detect(env = process.env, stdout = process.stdout, platform = process.p
349
291
  vt: vt.vt,
350
292
  cols: size.cols,
351
293
  rows: size.rows,
352
- eol: platform === 'win32' && !isTTY ? '\r\n' : '\n',
294
+ eol: '\n',
353
295
  wideRunes: resolveWideRunes(env),
354
296
  reasons: {
355
297
  glyphs: glyph.reason,
@@ -556,11 +498,18 @@ function bg(r, g, b) {
556
498
  /**
557
499
  * The resolved capabilities as `[label, value]` rows — one shape, same on every
558
500
  * platform, so a support paste from PowerShell reads like one from zsh.
501
+ *
502
+ * The first three rows are the host as *reported*: they are the only place the
503
+ * platform, the terminal brand and the locale appear, and they cannot influence
504
+ * any row below them. That is the point of the layout — a reader can see at a
505
+ * glance that the mark, the colour, the control sequences and the line ending
506
+ * are the same on every machine, while still knowing which machine produced the
507
+ * paste.
559
508
  */
560
509
  function describe(c = caps()) {
561
- const eol = c.eol === '\r\n' ? '\\r\\n' : '\\n';
510
+ const host = [c.platform, c.isTTY ? '' : '(no tty)'].filter(Boolean).join(' ');
562
511
  return [
563
- ['platform', `${c.platform}${c.isTTY ? '' : ' (no tty)'}`],
512
+ ['platform', c.windowsHost ? `${host} — ${c.windowsHost}` : host],
564
513
  ['terminal', c.termProgram || c.term || 'unknown'],
565
514
  ['locale', c.locale || 'unset'],
566
515
  ['mark', c.glyphs === 'ascii' ? 'ascii' : c.face === 'edges' ? 'unicode (edges stencilled)' : 'unicode'],
@@ -568,7 +517,7 @@ function describe(c = caps()) {
568
517
  ['color', c.depth === 0 ? 'none' : c.depth >= 24 ? 'truecolor' : c.depth >= 8 ? '256' : '16'],
569
518
  ['control', c.vt ? 'ansi' : 'plain'],
570
519
  ['size', `${c.cols}x${c.rows} (${c.reasons.size})`],
571
- ['eol', eol],
520
+ ['eol', '\\n'],
572
521
  ['why', `${c.reasons.glyphs}; ${c.reasons.depth}`],
573
522
  ];
574
523
  }
@@ -593,5 +542,4 @@ module.exports = {
593
542
  resolveVt,
594
543
  resolveSize,
595
544
  MODERN_WINDOWS,
596
- NATIVE_TERM_PROGRAMS,
597
545
  };
package/src/chatflow.js CHANGED
@@ -41,7 +41,7 @@ const {
41
41
  selectionRange, selectionText, applySelection,
42
42
  } = require('./screen.js');
43
43
  const {
44
- KEY, attachKeyStream, nextKey, nextKeyTimeout, requeueKeys, resetKeyStream,
44
+ KEY, STREAM_END, attachKeyStream, nextKey, nextKeyTimeout, requeueKeys, resetKeyStream,
45
45
  isKeyStreamSuspended,
46
46
  } = require('./events.js');
47
47
  const { GLYPH, VERBS, DONE_VERBS, BOLD, BOLD_OFF, themeOf } = require('./theme.js');
@@ -771,9 +771,12 @@ async function runSession(host) {
771
771
  let suggestionIdx = 0;
772
772
  let scroll = 0;
773
773
  let anchorEnd = null;
774
- // Screen row (1-based, as the mouse reports it) → the transcript row painted
775
- // there. Rebuilt on every frame so it follows scrolling; a click reads it to
776
- // find which edit block to toggle.
774
+ // Grid row (0-based, as the frame indexes `lines`) → the transcript message
775
+ // painted there, tool rows only. Rebuilt on every frame so it follows
776
+ // scrolling; a click reads it to find which edit block to toggle. The wire's
777
+ // 1-based row is converted once, in mouseGesture(), so both sides of this map
778
+ // speak grid rows — keying it by screen row instead (the `1 +` that used to
779
+ // read `2 +`) toggles the row ABOVE the one the reader clicked.
777
780
  const clickHits = new Map();
778
781
  // ── mouse marking (text selection) ────────────────────────────────────────
779
782
  // The terminal's own selection can't see the alternate screen, so a drag is
@@ -825,10 +828,11 @@ async function runSession(host) {
825
828
  const view = transcriptLines(transcript, { cols, rows, scroll, anchorEnd }, ctx, Date.now(), lineCache);
826
829
  scroll = view.scroll;
827
830
  anchorEnd = view.anchorEnd;
828
- // The header is frame line 0 (screen row 1), so the transcript window starts
829
- // at screen row 2; each painted line carries its source row in view.owners.
831
+ // The header is frame line 0 (grid row 0), so the transcript window's first
832
+ // painted line is grid row 1; each painted line carries its source row in
833
+ // view.owners.
830
834
  clickHits.clear();
831
- for (let i = 0; i < view.lines.length; i++) clickHits.set(2 + i, view.owners[i]);
835
+ for (let i = 0; i < view.lines.length; i++) clickHits.set(1 + i, view.owners[i]);
832
836
  for (const l of view.lines) lines.push(l);
833
837
  // Exactly `avail` transcript rows, so the frame is exactly `rows` lines:
834
838
  // header(1) + transcript(avail) + spinner/effort + rule + input + rule +
@@ -1329,13 +1333,14 @@ async function runSession(host) {
1329
1333
  };
1330
1334
 
1331
1335
  /**
1332
- * Toggle the edit block painted on a screen row. Split out of the click
1333
- * branch so the gesture resolver can call it for a press that never swept a
1334
- * cell — the click path and the marking path must agree on what a click is.
1336
+ * Toggle the edit block painted at a grid row (0-based — the same row
1337
+ * mouseGesture() resolves from the wire). Split out of the click branch so
1338
+ * the gesture resolver can call it for a press that never swept a cell — the
1339
+ * click path and the marking path must agree on what a click is.
1335
1340
  * Returns true when a block was toggled.
1336
1341
  */
1337
- const toggleAtRow = (screenRow) => {
1338
- const msg = clickHits.get(screenRow);
1342
+ const toggleAtRow = (row) => {
1343
+ const msg = clickHits.get(row);
1339
1344
  if (msg && msg.diff) {
1340
1345
  msg.diffOpen = !msg.diffOpen;
1341
1346
  return true;
@@ -1360,9 +1365,12 @@ async function runSession(host) {
1360
1365
  // collapse the moment the reader tried to select its contents.
1361
1366
  //
1362
1367
  // Coordinates arrive 1-based on the wire and grid rows are 0-based, hence the
1363
- // -1 on both axes. Selection is read from `frameLines` (the last painted
1364
- // grid), NOT the transcript, so the copied cells are the ones the reader
1365
- // pointed at.
1368
+ // -1 on both axes. `cell.row` is also the key toggleAtRow() looks up in
1369
+ // clickHits, which buildFrame() fills from the same 0-based frame, so the two
1370
+ // conversions have to agree: a map built one row off toggles the row above the
1371
+ // one the reader clicked, with everything else still looking right.
1372
+ // Selection is read from `frameLines` (the last painted grid), NOT the
1373
+ // transcript, so the copied cells are the ones the reader pointed at.
1366
1374
  const mouseGesture = (key) => {
1367
1375
  const cell = { col: key.col - 1, row: key.row - 1 };
1368
1376
  if (key.name === 'click') { pendingClick = cell; return false; }
@@ -2015,6 +2023,12 @@ async function runSession(host) {
2015
2023
  while (!ended) {
2016
2024
  const key = await nextKey();
2017
2025
  if (ended) break;
2026
+ // The pump was reset underneath us (another session tore down, or a
2027
+ // child-process handover): stop looping rather than spinning on a
2028
+ // stream that no longer exists. Without this a session parked on
2029
+ // nextKey() at reset time never wakes, and the loop outlives its
2030
+ // terminal — repainting into whatever stdout it captured.
2031
+ if (key === STREAM_END) break;
2018
2032
  try {
2019
2033
  await handleKey(key);
2020
2034
  } catch (err) {
package/src/checkpoint.js CHANGED
@@ -13,6 +13,7 @@
13
13
  const fs = require('node:fs');
14
14
  const path = require('node:path');
15
15
  const { aegisDir } = require('./config.js');
16
+ const { ensurePrivateDir, writePrivateFile } = require('./securefs.js');
16
17
 
17
18
  const CHECKPOINT_LIMIT = 8;
18
19
 
@@ -26,7 +27,7 @@ function checkpointsPath(sessionId) {
26
27
  */
27
28
  function snapshotCheckpoint(sessionId, transcript) {
28
29
  try {
29
- fs.mkdirSync(path.dirname(checkpointsPath(sessionId)), { recursive: true });
30
+ ensurePrivateDir(path.dirname(checkpointsPath(sessionId)));
30
31
  const entry = {
31
32
  ts: new Date().toISOString(),
32
33
  depth: transcript.length,
@@ -37,7 +38,8 @@ function snapshotCheckpoint(sessionId, transcript) {
37
38
  const prev = fs.existsSync(p) ? fs.readFileSync(p, 'utf8').split('\n').filter(Boolean) : [];
38
39
  const lines = [...prev, JSON.stringify(entry)];
39
40
  const trimmed = lines.slice(Math.max(0, lines.length - CHECKPOINT_LIMIT));
40
- fs.writeFileSync(p, trimmed.join('\n') + '\n');
41
+ // 0600: a checkpoint is the full transcript at a point in time.
42
+ writePrivateFile(p, trimmed.join('\n') + '\n');
41
43
  return lines.length - 1; // index into the trimmed list
42
44
  } catch {
43
45
  return -1;