@awebai/oats 0.57.0 → 0.57.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.
Files changed (50) hide show
  1. package/docs/configuration.md +17 -27
  2. package/docs/desktop.md +14 -0
  3. package/docs/execution-targets.md +6 -1
  4. package/docs/implementation.md +7 -1
  5. package/docs/integrations.md +3 -1
  6. package/docs/official-catalog.md +1 -1
  7. package/docs/packages.md +5 -5
  8. package/docs/release-notes/v0.57.1.md +275 -0
  9. package/docs/servers.md +18 -0
  10. package/docs/tui.md +214 -87
  11. package/docs/workspaces.md +1 -1
  12. package/lib/session-viewer.mjs +29 -4
  13. package/package-catalog.json +1 -1
  14. package/package.json +1 -1
  15. package/packages/client/cli-adapter.mjs +9 -7
  16. package/packages/client/composed-gate.mjs +34 -0
  17. package/packages/client/inspect-boundary.mjs +157 -0
  18. package/packages/client/inspect-cache.mjs +86 -0
  19. package/packages/client/keyed-catalog.mjs +132 -0
  20. package/packages/client/launch-view.mjs +46 -0
  21. package/packages/client/session-attach.mjs +7 -5
  22. package/packages/client/soul-catalog.mjs +77 -0
  23. package/packages/client/soul-colors.mjs +31 -0
  24. package/packages/client/soul-facts.mjs +85 -0
  25. package/packages/client/soul-rows.mjs +48 -0
  26. package/packages/client/spawn-apply-cli.mjs +11 -6
  27. package/packages/client/spawn-apply.mjs +212 -0
  28. package/packages/client/spawn-messages.mjs +131 -85
  29. package/packages/client/spawn-preview-contract.mjs +3 -0
  30. package/packages/client/spawn-preview-facts.mjs +119 -0
  31. package/packages/client/spawn-preview.mjs +152 -0
  32. package/packages/client/spawn-transaction.mjs +136 -0
  33. package/packages/tui/README.md +32 -11
  34. package/packages/tui/lib/actions.mjs +10 -1
  35. package/packages/tui/lib/config.mjs +9 -14
  36. package/packages/tui/lib/main.mjs +165 -57
  37. package/packages/tui/lib/model.mjs +28 -24
  38. package/packages/tui/lib/panel.mjs +955 -0
  39. package/packages/tui/lib/read.mjs +5 -14
  40. package/packages/tui/lib/store.mjs +77 -30
  41. package/packages/tui/lib/term/capture.mjs +260 -0
  42. package/packages/tui/lib/term/screen.mjs +44 -8
  43. package/packages/tui/lib/term/style.mjs +78 -6
  44. package/packages/tui/lib/term/text.mjs +24 -4
  45. package/packages/tui/lib/term/through.mjs +213 -0
  46. package/packages/tui/lib/term/tty.mjs +7 -60
  47. package/packages/tui/lib/views/frame.mjs +126 -40
  48. package/packages/tui/lib/views/help.mjs +12 -10
  49. package/skills/oats-getting-started/SKILL.md +3 -2
  50. package/packages/tui/lib/open.mjs +0 -45
@@ -1,21 +1,24 @@
1
1
  // `oats tui` (preview): the loop. Events from the keyboard and from the reader go through one
2
2
  // pure reduce(state, event); what only the loop can do comes back as an effect; the frame is
3
3
  // drawn from the state. This version shows a workspace's instances, on this machine and on the
4
- // machines registered here, follows them, opens an agent's terminal with the kernel's own
5
- // viewer, and stops, starts, restarts and retires an instance after a confirmation, through the
6
- // shared lifecycle boundary and start rule (docs/tui.md).
4
+ // machines registered here, follows them, opens an agent's terminal in a panel beside the list
5
+ // (the kernel's own viewer, in a pane of the run's private tmux server: lib/panel.mjs), and
6
+ // stops, starts, restarts and retires an instance after a confirmation, through the shared
7
+ // lifecycle boundary and start rule (docs/tui.md).
7
8
  import { homedir } from "node:os";
8
9
  import { resolve } from "node:path";
9
10
  import { bindings } from "./actions.mjs";
10
11
  import { TERMS, createLifecycleBoundary, createLifecycleConfirmation, displayLine, heldHome, pendingEntry, prepareRemoteTerm, sessionAttachArgv, startInstance } from "./client.mjs";
11
12
  import { loadConfig } from "./config.mjs";
12
13
  import { ACT_WORDS, actContext, actCwd, localDeployments, model, roster } from "./model.mjs";
13
- import { startViewer } from "./open.mjs";
14
- import { admitDeployment, createReader, findDeployment, leaveInstance, offeredDeployments, probeKernel, realpath, tellable } from "./read.mjs";
14
+ import { createPanel, sweep } from "./panel.mjs";
15
+ import { CHANGE_FILES, admitDeployment, createReader, findDeployment, leaveInstance, offeredDeployments, probeKernel, realpath, tellable } from "./read.mjs";
15
16
  import { initialState, reduce } from "./store.mjs";
17
+ import { captureRows } from "./term/capture.mjs";
16
18
  import { createKeyParser, keyId } from "./term/keys.mjs";
17
19
  import { createScreen, glyphs } from "./term/screen.mjs";
18
20
  import { colorEnabled } from "./term/style.mjs";
21
+ import { backKeySequences, createThrough } from "./term/through.mjs";
19
22
  import { createGuard, isTerminal } from "./term/tty.mjs";
20
23
  import { frame, layout } from "./views/frame.mjs";
21
24
 
@@ -25,8 +28,8 @@ const HELP = `Usage:
25
28
 
26
29
  The OATS terminal client (preview): a full-screen view of a workspace's instances,
27
30
  on this machine and on the machines registered here. It follows them, opens an
28
- agent's terminal with Enter, and stops, retires, starts or restarts an instance
29
- (X, D, S, R) after a confirmation.
31
+ agent's terminal beside the list with Enter, and stops, retires, starts or
32
+ restarts an instance (X, D, S, R) after a confirmation.
30
33
  Keys: ? help, Enter open, w workspaces, q quit.
31
34
  docs/tui.md has the rest.
32
35
 
@@ -65,9 +68,12 @@ function errorLines(error) {
65
68
 
66
69
  /** What the loop does outside the terminal, each replaceable by a test. `realpath` is the one the
67
70
  * shared start rule checks a local home with; `startInstance` and `createLifecycleConfirmation`
68
- * are the shared home's, replaceable so a test can drive the loop's wiring. */
69
- const EFFECTS = Object.freeze({ loadConfig, findDeployment, probeKernel, createReader, offeredDeployments, admitDeployment, prepareRemoteTerm, startViewer,
70
- realpath, tellable, startInstance, createLifecycleConfirmation, createLifecycleBoundary });
71
+ * are the shared home's, replaceable so a test can drive the loop's wiring. `runDir` makes the
72
+ * run's private directory (the reader's change output and the panel's tmux server share it), and
73
+ * `removeDir` removes it on the way out once both have removed their files; `sweep` removes what
74
+ * runs that are gone left of theirs (lib/panel.mjs). */
75
+ const EFFECTS = Object.freeze({ loadConfig, findDeployment, probeKernel, createReader, offeredDeployments, admitDeployment, prepareRemoteTerm, createPanel, sweep,
76
+ runDir: CHANGE_FILES.dir, removeDir: CHANGE_FILES.remove, realpath, tellable, startInstance, createLifecycleConfirmation, createLifecycleBoundary });
71
77
 
72
78
  /** Run. Resolves with the exit status; never calls process.exit, so what was written drains.
73
79
  * Before the alternate screen, in this order, each failure one line on stderr and status 1:
@@ -98,6 +104,11 @@ export async function main({ argv, stdin, stdout, stderr, env, proc = process, n
98
104
  let parser = createKeyParser({ now });
99
105
  let state = initialState({ deployment: found.ok ? found.deployment : null, cli: probe.cli, home, entry: found.ok ? pendingEntry(null, clock()) : null, actions: config.actions, ascii: look.ascii, detachKey: config.detachKey });
100
106
  const keymap = bindings(state.actions);
107
+ // While a terminal has the focus the bytes typed go to the agent: this scanner takes from them
108
+ // only the back key and a bracketed paste (term/through.mjs). A new one each time the terminal
109
+ // takes the focus, so nothing held from before is typed into it.
110
+ const backKey = backKeySequences(state.detachKey);
111
+ let through = createThrough({ sequences: backKey, now }), throughTimer = null;
101
112
  if (offered) state = reduce(state, { offered }, keymap).state;
102
113
  // What the last frame drew, for the store: how tall the list pane is and how far the other
103
114
  // pane scrolls. The store holds no size of its own.
@@ -106,6 +117,11 @@ export async function main({ argv, stdin, stdout, stderr, env, proc = process, n
106
117
  /** The preflight of another machine's agent under way: `{ id, controller, prepared }`, where
107
118
  * `prepared` is what it answered (`{ binary, args }`) once it has; or null. */
108
119
  let preflight = null;
120
+ /** The run's private directory, made at the first change or the first open (lib/read.mjs
121
+ * CHANGE_FILES): the reader writes a change's output there, and the panel keeps its tmux
122
+ * server's socket there. */
123
+ let dir = null;
124
+ const runDir = () => (dir ??= effects.runDir());
109
125
 
110
126
  // What the reader reads for, it asks of the state at that moment: the served deployments, the
111
127
  // entry a failed read keeps, and the roster and the memory the next outcome is made from.
@@ -115,6 +131,24 @@ export async function main({ argv, stdin, stdout, stderr, env, proc = process, n
115
131
  getEntry: (id) => state.entries.get(id) ?? null,
116
132
  getRoster: () => ({ remote: state.remote, memory: state.memory, notes: state.notes }),
117
133
  onEntry: heard, onRoster: heard,
134
+ files: { ...CHANGE_FILES, dir: runDir },
135
+ });
136
+ // THE OPEN TERMINAL (docs/tui.md, "Open an agent's terminal"): one at a time, in the panel.
137
+ // `viewing` is its viewer, `{ id, panel }` (the row's instanceId, and the panel's own id once
138
+ // the window exists, null before); `screenNow` the last capture (`{ lines, cursor }`), drawn
139
+ // until the next; `panelSize` and `panelShown` what the panel was last told.
140
+ let viewing = null, screenNow = null, panelSize = null, panelShown = true;
141
+ const panel = effects.createPanel({
142
+ dir: runDir, env, capture: captureRows,
143
+ onScreen: ({ id, screen: lines, cursor }) => { if (!ended && viewing !== null && viewing.panel === id) { screenNow = { lines, cursor }; guard.attempt(() => guard.draw()); } },
144
+ // The viewer ended. The store is told, whatever ended it; a terminal the person closed is no
145
+ // longer `viewing`, and the store says nothing of it.
146
+ onClosed: ({ id, status = null, stderr = "", asked = false }) => {
147
+ if (viewing === null || viewing.panel !== id) return;
148
+ const row = viewing.id;
149
+ viewing = null; screenNow = null;
150
+ heard({ returned: { id: row, status, stderr, asked } });
151
+ },
118
152
  });
119
153
  // THE ACTS (docs/tui.md, "Stopping, starting, restarting and retiring"). One lifecycle boundary
120
154
  // per run: its plans, its kept answers and its one apply at a time are this run's. Its exec runs
@@ -147,11 +181,12 @@ export async function main({ argv, stdin, stdout, stderr, env, proc = process, n
147
181
  took(reduce(state, admitted.ok ? { admitted: admitted.deployment, at: clock() } : { refused: effect.admit }, keymap));
148
182
  }
149
183
  if (effect.abandon) giveUpPreflight();
184
+ if (effect.close !== undefined) closeTerminal();
150
185
  if (effect.open) open(effect.open);
151
- if (effect.lend !== undefined) {
186
+ if (effect.panel !== undefined) {
152
187
  const ready = preflight;
153
188
  preflight = null;
154
- if (ready?.id === effect.lend && ready.prepared) lendTo(effect.lend, ready.prepared.binary, ready.prepared.args);
189
+ if (ready?.id === effect.panel && ready.prepared) view(effect.panel, ready.prepared.binary, ready.prepared.args);
155
190
  }
156
191
  if (effect.dismiss !== undefined) dismiss(effect.dismiss);
157
192
  if (effect.act) openConfirmation(effect.act);
@@ -206,22 +241,23 @@ export async function main({ argv, stdin, stdout, stderr, env, proc = process, n
206
241
  .then((answer) => heard({ acted: { n, answer } }), () => heard({ acted: { n, answer: null } }));
207
242
  }
208
243
 
209
- /** Open the agent the store decided on (lib/model.mjs openDecision). On this machine the
210
- * terminal is lent at once. On another, the shared preflight runs first over the reader's exec
211
- * (a read like any other, ended on every way out), and what it prepared waits for the store's
212
- * `lend`, which comes only if the open was not given up and the row is still there. */
244
+ /** Open the agent the store decided on (lib/model.mjs openDecision). On this machine the viewer
245
+ * starts in the panel at once. On another, the shared preflight runs first over the reader's
246
+ * exec (a read like any other, ended on every way out), and what it prepared waits for the
247
+ * store's `panel`, which comes only if the open was not given up and the row is still there.
248
+ * The viewer is given no key of its own: the TUI takes the back key before the pane sees it. */
213
249
  function open(decision) {
214
250
  if (!decision.routed) {
215
251
  let args;
216
252
  // A target the shared home refuses (no absolute home) starts nothing; the line after says
217
253
  // the viewer ended without saying why.
218
- try { args = sessionAttachArgv(probe.cli, decision.target, { detachKey: state.detachKey }); } catch { return void heard({ returned: { id: decision.id, status: null, stderr: "" } }); }
219
- return lendTo(decision.id, probe.cli.bin, args);
254
+ try { args = sessionAttachArgv(probe.cli, decision.target); } catch { return void heard({ returned: { id: decision.id, status: null, stderr: "" } }); }
255
+ return view(decision.id, probe.cli.bin, args);
220
256
  }
221
257
  giveUpPreflight();
222
258
  const mine = preflight = { id: decision.id, controller: new AbortController(), prepared: null };
223
259
  Promise.resolve()
224
- .then(() => effects.prepareRemoteTerm(probe.cli, decision.target, { exec: reader.exec, signal: mine.controller.signal, detachKey: state.detachKey }))
260
+ .then(() => effects.prepareRemoteTerm(probe.cli, decision.target, { exec: reader.exec, signal: mine.controller.signal }))
225
261
  .then((prepared) => { if (preflight === mine) { mine.prepared = prepared; heard({ prepared: { id: mine.id } }); } },
226
262
  (error) => { if (preflight === mine) { preflight = null; heard({ prepared: { id: mine.id, error } }); } });
227
263
  }
@@ -232,51 +268,115 @@ export async function main({ argv, stdin, stdout, stderr, env, proc = process, n
232
268
  preflight.controller.abort();
233
269
  preflight = null;
234
270
  }
235
- /** Lend the terminal to the kernel's viewer (lib/open.mjs) and take it back when it closes.
236
- * Before: every read in flight is void and none starts while it has the terminal, as at a
237
- * stop. After: reads begin again, and the store says what happened and asks for a live read. */
238
- function lendTo(id, bin, args) {
239
- dropReads();
240
- reader.hold();
241
- let viewer = null;
242
- guard.lend(() => { viewer = effects.startViewer(bin, args, { env }); return viewer.closed; }, { end: () => viewer?.end(), release: () => viewer?.release() }).then((outcome) => {
243
- // The keys are read anew: what was typed before the viewer started (behind the Enter, in the
244
- // same chunk) and a sequence begun then are gone, so none of it acts or joins a later key.
245
- if (escTimer !== null) { clearTimer(escTimer); escTimer = null; }
246
- parser = createKeyParser({ now });
247
- reader.release();
248
- const { status = null, stderr = "" } = outcome.value ?? {};
249
- heard({ returned: { id, status, stderr } });
250
- });
271
+ /** Start the viewer of row `id` (`node <bin> <args…>`) in the panel, at the size the frame gives
272
+ * the terminal, and tell the store when its window exists. One the panel could not start is a
273
+ * viewer that ended, with the panel's refusal: no tmux (or one too old), or tmux's own words.
274
+ * Nothing is lent: the reads and the screen go on. */
275
+ function view(id, bin, args) {
276
+ const mine = viewing = { id, panel: null };
277
+ const size = guard.size(), area = composed().made.terminal ?? { cols: size.cols, rows: Math.max(1, size.rows - 6) };
278
+ panelSize = { cols: area.cols, rows: area.rows };
279
+ // Shown: the panel keeps hidden or shown across its viewers, and the last one may have been
280
+ // closed hidden (behind the detail, or the list alone under 80 columns).
281
+ if (!panelShown) panel.setVisible(true);
282
+ panelShown = true;
283
+ Promise.resolve()
284
+ .then(() => panel.open({ argv: [process.execPath, bin, ...args], cols: area.cols, rows: area.rows }))
285
+ .then(({ id: made }) => {
286
+ if (viewing !== mine) return;
287
+ mine.panel = made;
288
+ // A frame of another size while the window was being made told a panel with no window yet.
289
+ if (panelSize.cols !== area.cols || panelSize.rows !== area.rows) panel.resize(panelSize.cols, panelSize.rows);
290
+ heard({ shown: { id } });
291
+ },
292
+ (error) => { if (viewing === mine) { viewing = null; heard({ returned: { id, status: null, stderr: typeof error?.detail === "string" ? error.detail : "", refused: error?.code === "E_PANEL_TMUX" ? "tmux" : "start" } }); } });
293
+ }
294
+ /** Close the open terminal's viewer (also one whose window is still being made): the agent keeps
295
+ * running, and the panel reports the end as asked for, which nobody waits for. */
296
+ function closeTerminal() {
297
+ if (viewing === null) return;
298
+ viewing = null; screenNow = null;
299
+ panel.close();
300
+ }
301
+ /** The panel told what the frame gives the terminal: shown or not (no capture runs while it is
302
+ * hidden, and one runs when it shows again), and its size (the viewer's window follows it). */
303
+ function fitPanel(given) {
304
+ // A view that draws no terminal (the help, the switcher, a confirmation) gives none.
305
+ const area = given ?? null;
306
+ if (viewing === null) return;
307
+ if ((area !== null) !== panelShown) { panelShown = area !== null; panel.setVisible(panelShown); }
308
+ if (area !== null && (area.cols !== panelSize.cols || area.rows !== panelSize.rows)) { panelSize = { cols: area.cols, rows: area.rows }; panel.resize(area.cols, area.rows); }
309
+ }
310
+ /** The frame for the state now, with what the loop needs of it: the layout first, because the
311
+ * model's window is as tall, and its sentences as wide, as the list pane it gives. The switcher
312
+ * takes the whole body and has no notice of a deployment. The last capture of the open
313
+ * terminal goes with the model: it is the panel's, not the store's. */
314
+ function composed() {
315
+ const size = guard.size(), at = clock();
316
+ const listing = (state.view === "help" ? state.back : state.view) === "switcher";
317
+ const r = listing ? { notice: null, note: null, observed: true } : roster(state, at);
318
+ const g = layout(r.notice, size, { observed: r.observed, note: r.note, ascii: state.ascii, outcome: listing ? null : state.outcome });
319
+ const m = { ...model(state, { page: g.page, now: now(), at, cols: g.listCols }), screen: screenNow };
320
+ return { size, g, made: frame(m, size) };
321
+ }
322
+ /** Whether what is typed goes to the agent: the open terminal has the focus. */
323
+ const typing = () => state.terminal?.focus === "terminal" && state.view === "instances";
324
+ /** Bytes typed while the terminal has the focus: to the agent as they came, a paste as a paste,
325
+ * and the back key to the store, which gives the focus to the list; what came after the back key
326
+ * in the same read is read as keys, by a parser that starts afresh. */
327
+ function typed(events) {
328
+ for (const event of events) {
329
+ if (event.input) panel.input(event.input);
330
+ else if (event.paste !== undefined) panel.paste(event.paste);
331
+ else if (event.back) {
332
+ parser = createKeyParser({ now });
333
+ if (took(reduce(state, { back: true }, keymap))) return;
334
+ if (event.rest.length) act(parser.feed(event.rest)); else guard.draw();
335
+ watchEscape();
336
+ }
337
+ }
338
+ watchThrough();
339
+ }
340
+ /** A beginning of the back key that nothing has followed for the parser's timeout is the agent's. */
341
+ function watchThrough() {
342
+ if (throughTimer !== null) { clearTimer(throughTimer); throughTimer = null; }
343
+ const due = through.timeoutAt();
344
+ if (due !== null) throughTimer = setTimer(() => { throughTimer = null; guard.attempt(() => { if (typing()) typed(through.expire()); }); }, Math.max(0, due - now()));
251
345
  }
252
346
  const guard = createGuard({
253
347
  stdin, stdout, proc, setTimer, clearTimer,
254
348
  frame() {
255
- const size = guard.size(), at = clock();
256
- // The layout first: the model's window is as tall, and its sentences as wide, as the list
257
- // pane it gives. The switcher takes the whole body and has no notice of a deployment.
258
- const listing = (state.view === "help" ? state.back : state.view) === "switcher";
259
- const r = listing ? { notice: null, note: null, observed: true } : roster(state, at);
260
- const g = layout(r.notice, size, { observed: r.observed, note: r.note, ascii: state.ascii, outcome: listing ? null : state.outcome });
261
- const made = frame(model(state, { page: g.page, now: now(), at, cols: g.listCols }), size);
349
+ const { size, g, made } = composed();
262
350
  drawn = { page: made.page, extent: made.extent, room: made.room, cols: g.listCols };
263
- const bytes = screen.render(made.rows, size, { full });
351
+ fitPanel(made.terminal);
352
+ const bytes = screen.render(made.rows, size, { full, cursor: made.cursor });
264
353
  full = false;
265
354
  return bytes;
266
355
  },
267
- onInput(bytes) { if (guard.lent) return; reader.touch(); act(parser.feed(bytes)); watchEscape(); },
356
+ onInput(bytes) {
357
+ reader.touch();
358
+ if (typing()) return typed(through.feed(bytes));
359
+ act(parser.feed(bytes)); watchEscape();
360
+ },
268
361
  onResize() { full = true; guard.draw(); },
269
- onRedraw() { full = true; guard.draw(); },
362
+ // Ctrl+L and the return from a stop: the whole screen, and the open terminal captured afresh.
363
+ onRedraw() { full = true; if (viewing !== null) panel.refresh(); guard.draw(); },
270
364
  // A cycle in flight when the process stops is no evidence about the deployment (lib/read.mjs):
271
365
  // it is dropped before the stop, and again after a stop nobody here asked for; the return
272
366
  // reads at once.
273
367
  // A preflight is given up first, by the TUI itself: ended as a read it would answer as a
274
368
  // timeout. The person opens the agent again.
275
- onStop() { stopPreflight(); dropReads(); },
276
- onResume(asked) { if (!asked) { stopPreflight(); dropReads(); } refresh({ live: true }); },
277
- // On the way out the confirmation is closed and the reads end; a change in flight is let go,
278
- // never signalled (lib/read.mjs changeExec), and named after the screen is left.
279
- onEnd() { ended = true; dismiss(confirmation?.n); reader.stop(); },
369
+ // The open terminal's server and viewer live on through a stop; the panel stops sending output
370
+ // while nothing reads it, and captures afresh on the way back.
371
+ onStop() { stopPreflight(); dropReads(); if (viewing !== null) panel.pause(); },
372
+ onResume(asked) { if (!asked) { stopPreflight(); dropReads(); } if (viewing !== null) panel.resume(); refresh({ live: true }); },
373
+ // On the way out the confirmation is closed, the private tmux server is ended (its viewer with
374
+ // it) and the reads end; a change in flight is let go, never signalled (lib/read.mjs
375
+ // changeExec), and named after the screen is left.
376
+ // The run's directory goes last, made for whichever of the two needed it first: the reader
377
+ // removes it only when it wrote a change there, and one it cannot remove (a change still
378
+ // writing) stays.
379
+ onEnd() { ended = true; dismiss(confirmation?.n); panel.end(); reader.stop(); if (dir !== null) { try { effects.removeDir(dir); } catch { /* not empty, or gone */ } } },
280
380
  });
281
381
  function refresh(how) { Promise.resolve(reader.request(how)).catch(() => {}); }
282
382
  /** A preflight under way when the process stops: given up, and the store told. */
@@ -288,16 +388,20 @@ export async function main({ argv, stdin, stdout, stderr, env, proc = process, n
288
388
  /** Every read in flight is void (lib/read.mjs), the roster's with them: the reader says nothing
289
389
  * for a void read, so the state is told here that none is in flight. */
290
390
  function dropReads() { reader.voidInFlight(); state = reduce(state, { collecting: false }, keymap).state; }
291
- /** The events of one batch of input, in order. Once one of them lent the terminal, the rest are
292
- * dropped: they were typed before the viewer started, and none may act while it has the
293
- * terminal (a `q` behind the Enter would end the run under a live viewer). Each batch is one
294
- * read of input, numbered (`read`): a confirmation ignores the press of its act in the read
295
- * that moved the focus onto it, so a paste cannot both move and press. */
391
+ /** The events of one batch of input, in order. Once one of them gave the terminal the focus,
392
+ * the rest are dropped: they were typed before it had it, and neither the list nor the agent
393
+ * is to have them (a `q` behind the Enter would end the run). Each batch is one read of input,
394
+ * numbered (`read`): a confirmation ignores the press of its act in the read that moved the
395
+ * focus onto it, so a paste cannot both move and press. */
296
396
  function act(events) {
297
397
  const read = ++reads;
298
398
  for (const event of events) {
299
- if (guard.lent) return;
300
399
  if (took(reduce(state, "paste" in event ? { paste: event.paste } : { key: keyId(event), ...(typeof event.text === "string" && !event.alt ? { text: event.text } : {}), read, ...drawn }, keymap))) return;
400
+ if (typing()) {
401
+ if (escTimer !== null) { clearTimer(escTimer); escTimer = null; }
402
+ through = createThrough({ sequences: backKey, now });
403
+ break;
404
+ }
301
405
  }
302
406
  guard.draw();
303
407
  }
@@ -305,14 +409,18 @@ export async function main({ argv, stdin, stdout, stderr, env, proc = process, n
305
409
  function watchEscape() {
306
410
  if (escTimer !== null) { clearTimer(escTimer); escTimer = null; }
307
411
  const due = parser.timeoutAt();
308
- if (due !== null) escTimer = setTimer(() => { escTimer = null; if (!guard.lent) guard.attempt(() => { act(parser.expire()); watchEscape(); }); }, Math.max(0, due - now()));
412
+ if (due !== null) escTimer = setTimer(() => { escTimer = null; if (!typing()) guard.attempt(() => { act(parser.expire()); watchEscape(); }); }, Math.max(0, due - now()));
309
413
  }
310
414
 
311
415
  guard.start();
312
416
  guard.attempt(() => { if (!ended) Promise.resolve(reader.start()).catch(() => {}); });
313
417
  guard.attempt(() => guard.draw());
418
+ // Once a run, after the first frame: what runs that are gone left of their private servers
419
+ // (lib/panel.mjs sweep, which signals nothing and removes only what it can prove is dead).
420
+ guard.attempt(() => { if (!ended) { try { effects.sweep({ env }); } catch { /* nothing swept */ } } });
314
421
  const end = await guard.done;
315
422
  if (escTimer !== null) clearTimer(escTimer);
423
+ if (throughTimer !== null) clearTimer(throughTimer);
316
424
  // After the alternate screen is left, so the message stays on the screen the user returns to.
317
425
  // One line per change still running, to a terminal or a file only; the status never depends on it.
318
426
  const running = reader.changes?.() ?? [];
@@ -10,7 +10,7 @@ import {
10
10
  hasInstanceChildren, heldHome, rosterSections, deploymentState, rowStale, primaryDeployment, isMultiDeployment, runtimeState, runtimeCounts,
11
11
  waitingClaim, waitingLabel, waitedText, waitingClock, waitingBelowText, instanceActs,
12
12
  identityMemory, viewModel, workspaceChoices, workspaceChoiceRows, choiceMatches, UNMATCHED_SECTION, shortPath,
13
- openAct, deployments as heldDeployments, detachKeySupported, hostDetachKeySupported, attachExit, preflightFailure, kernelLines,
13
+ openAct, deployments as heldDeployments, attachExit, preflightFailure, kernelLines,
14
14
  startGate, launchFlagIgnored, flagIgnoredNotice, retainedStartMessage,
15
15
  } from "./client.mjs";
16
16
  import { shownLines } from "./term/text.mjs";
@@ -157,13 +157,7 @@ function build(state, at) {
157
157
  // Whether a list stands under the header: not for a deployment of this machine that was never
158
158
  // observed, nor for a machine that is not live and has no row kept. Each has only its reason.
159
159
  const observed = panel !== null && !gone && (local ? status === "observed" : live || instances.length > 0);
160
- // Whether each other machine's OATS has the key that leaves a viewer, by the deployment its rows
161
- // are tagged with (the shared panel's `row.deployment.id`; the TUI builds no deployment id).
162
- // Worked out here, once for what the roster is made from, so a paint reads no machine again.
163
- const opens = new Map();
164
- if (panel !== null) for (const d of heldDeployments(heldOf(state, at))) if (d.remote) opens.set(d.id, hostDetachKeySupported(state.cli, d.group));
165
160
  return {
166
- opens,
167
161
  panel, gone, instances, deployments, multi, parts, rows, filtering, stale, section, live, observed,
168
162
  below: waitingRollup(instances, state.collapsed, state.open, { filtering, stale, section }),
169
163
  status,
@@ -287,13 +281,18 @@ const reasonSpoken = (reason) => spoken(reason.sentence, reason.origin, reason.o
287
281
  * machine's name), drawn through the display filter on its own. `<id>` and `<home>` in the
288
282
  * command are the TUI's placeholders: nothing of the row is in them. */
289
283
  export const OPEN_WORDS = Object.freeze({
290
- left: (name) => [{ own: "You left " }, { shown: name }, { own: ". It keeps running." }],
291
284
  failed: (name) => [{ shown: name }, { own: " could not be opened." }],
292
285
  link: (machine) => [{ own: "The connection to " }, { shown: machine }, { own: " ended." }],
293
286
  unknown: (name) => [{ own: "The viewer of " }, { shown: name }, { own: " ended without saying why." }],
287
+ noTmux: (name) => [{ shown: name }, { own: " could not be opened: the terminal panel needs tmux 3.2 or later on this machine." }],
294
288
  notRunning: (name) => [{ shown: name }, { own: " is not running, so it has no terminal to open." }],
295
289
  stateUnknown: (name) => [{ own: "Whether " }, { shown: name }, { own: " runs is not known, so it is not opened." }],
296
- noKey: (machine) => [machine === null ? { own: "This machine" } : { shown: machine }, { own: "'s OATS has no key to leave a viewer (it needs OATS 0.51.0 or later), so it is not opened from here. `oats session attach --server <id> --home <home>` opens it, and only closing the terminal leaves it." }],
290
+ });
291
+ /** The line of a view that is not in this version (its key: 2, 3), with what to run instead: the
292
+ * help's commands (views/help.mjs VIEWS), in the TUI's own words. */
293
+ export const NOT_BUILT = Object.freeze({
294
+ souls: Object.freeze({ said: Object.freeze([{ own: "Souls are not in this version: `oats souls` lists them, and `oats spawn <soul>` makes an instance of one." }]), under: Object.freeze([]) }),
295
+ capabilities: Object.freeze({ said: Object.freeze([{ own: "Capabilities are not in this version: `oats capabilities` lists them." }]), under: Object.freeze([]) }),
297
296
  });
298
297
  /** A sentence from outside (a reason of the shared home's, the preflight table's), as a text
299
298
  * under the line: drawn as the detail draws a reason, wrapped on the reason's rows, never as one
@@ -310,7 +309,8 @@ const elsewhere = (row) => typeof row.server === "string" && row.server !== "";
310
309
  * for a row of another machine (its preflight runs first)
311
310
  * { open: false, said: { said, under } } opens nothing, and this says why
312
311
  * Whether a row can be opened is the shared openAct's, switched on its reason's code, never on a
313
- * sentence; the TUI adds only the two feature gates on top of an enabled answer. */
312
+ * sentence. The viewer is started with no key of its own (the TUI takes the back key before the
313
+ * pane sees it), so a kernel or a host of any version is opened alike. */
314
314
  export function openDecision(state, row, acts) {
315
315
  const name = row.instance, id = instanceId(row);
316
316
  const machine = elsewhere(row) ? row.deployment?.machine ?? row.server : null;
@@ -325,23 +325,25 @@ export function openDecision(state, row, acts) {
325
325
  if (act.reason.code === "state-unknown") return no(OPEN_WORDS.stateUnknown(name));
326
326
  return no([], outside(reasonSpoken(act.reason)));
327
327
  }
328
- if (!elsewhere(row)) {
329
- return detachKeySupported(state.cli) ? { open: true, id, name, machine, target: { home: row.home }, routed: false } : no(OPEN_WORDS.noKey(null));
330
- }
331
- if (roster(state).opens.get(row.deployment?.id) !== true) return no(OPEN_WORDS.noKey(machine));
328
+ if (!elsewhere(row)) return { open: true, id, name, machine, target: { home: row.home }, routed: false };
332
329
  return { open: true, id, name, machine, target: { serverId: row.server, instance: row.instance, home: row.home }, routed: true };
333
330
  }
334
331
 
335
- /** The line after the viewer returned, or null when the TUI says nothing of its own (the viewer
336
- * ended without the key: the list shows the agent as the kernel now reports it). `status` is the
337
- * child's exit status as Node reports it; the shared attachExit reads it, the TUI compares no
338
- * number. What the viewer printed on stderr stands under a failure, once, as one text in
339
- * quotation marks naming nobody: OATS, tmux and ssh all print there, and nothing is compared
340
- * with it. */
341
- export function returnedLine({ name, machine, routed }, status, stderr) {
342
- const word = attachExit(status, { keyed: true, routed });
332
+ /** The line after a terminal closed by itself, or null when the TUI says nothing of its own: the
333
+ * viewer ended with status 0 (the list shows the agent as the kernel now reports it), or the
334
+ * person closed it (`asked`: c, or Enter on another row). `status` is what the pane's shell
335
+ * wrote of the viewer's exit (lib/panel.mjs), null when it wrote none; the shared attachExit reads
336
+ * it, as for a viewer started with no key, and the TUI compares no number. What the viewer printed
337
+ * on stderr stands under a failure, once, as one text in quotation marks naming nobody: OATS, tmux
338
+ * and ssh all print there, and nothing is compared with it. `refused` is the panel's own refusal
339
+ * when no viewer was started at all: "tmux" (no tmux here, or one too old), or "start" (the
340
+ * panel's server or window was not made; `stderr` is then what tmux said). */
341
+ export function returnedLine({ name, machine, routed }, status, stderr, { asked = false, refused = null } = {}) {
342
+ if (asked) return null;
343
+ if (refused === "tmux") return { said: OPEN_WORDS.noTmux(name), under: [] };
344
+ if (refused !== null) status = 1;
345
+ const word = attachExit(status, { keyed: false, routed });
343
346
  if (word === "ended") return null;
344
- if (word === "left") return { said: OPEN_WORDS.left(name), under: [] };
345
347
  if (word === "unknown") return { said: OPEN_WORDS.unknown(name), under: [] };
346
348
  const line = displayLine(stderr);
347
349
  const under = line === null ? [] : [line === DETAIL_WITHHELD ? DETAIL_WITHHELD : `"${line}"`];
@@ -467,6 +469,7 @@ export function model(state, { page, now, at, cols }) {
467
469
  workspace: r.workspace, machines: r.machines, here: r.here, machine: r.machine, counts: r.counts, readAt: r.readAt, notice: r.notice, note: r.note, empty: r.empty, observed: r.observed,
468
470
  detailTop: state.detailTop, helpTop: state.helpTop, open: state.open !== null,
469
471
  detachKey: state.detachKey, opening: state.opening, outcome: state.outcome, openable: false, actable: [],
472
+ terminal: state.terminal, onTerminal: false,
470
473
  act: state.view === "act" && state.act ? actView(state) : null,
471
474
  };
472
475
  // The view under the help, or under a confirmation, decides what the header says.
@@ -499,8 +502,9 @@ export function model(state, { page, now, at, cols }) {
499
502
  list: r.items.slice(win.top, win.top + win.size).map((item) => (item?.id !== undefined ? { kind: "row", ...listRow(state, r, item) } : item)),
500
503
  above: win.above, below: win.below, wordWidth: r.wordWidth,
501
504
  detail: selected >= 0 ? detailOf(state, r, r.items[selected], now, acts) : null,
502
- // Enter opens the selected agent's terminal now: the footer names the key that returns.
505
+ // Enter opens the selected agent's terminal now, or (`onTerminal`) goes to the one open on it.
503
506
  openable: decision?.open === true && state.opening === null,
507
+ onTerminal: row !== null && state.terminal !== null && state.terminal.id === instanceId(row),
504
508
  // The acts whose key opens a confirmation now, read from the same instanceActs answer: the
505
509
  // footer names them. A key not named still answers, with the line that says why.
506
510
  actable: row && state.opening === null ? ACT_VERBS.filter((verb) => actDecision(state, row, acts, verb).open) : [],