@awebai/oats 0.66.0 → 0.68.0

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 (81) hide show
  1. package/bin/oats.mjs +166 -17
  2. package/docs/capabilities.md +58 -9
  3. package/docs/capability-manifest.schema.json +5 -1
  4. package/docs/design/2026-10-22-module-upgrade.md +192 -0
  5. package/docs/design/README.md +4 -0
  6. package/docs/desktop-cli-api.md +463 -13
  7. package/docs/desktop.md +363 -67
  8. package/docs/first-team.md +5 -3
  9. package/docs/implementation.md +46 -6
  10. package/docs/integrations.md +16 -4
  11. package/docs/knowledge.md +44 -27
  12. package/docs/layers.md +2 -2
  13. package/docs/official-catalog.md +5 -5
  14. package/docs/packages.md +14 -14
  15. package/docs/release-lane.md +16 -0
  16. package/docs/release-notes/v0.67.0.md +366 -0
  17. package/docs/release-notes/v0.67.1.md +64 -0
  18. package/docs/release-notes/v0.68.0.md +504 -0
  19. package/docs/servers.md +8 -2
  20. package/docs/souls-and-instances.md +58 -6
  21. package/docs/tui.md +1033 -237
  22. package/docs/upgrading.md +164 -26
  23. package/docs/workspaces.md +2 -2
  24. package/lib/capability-contract.mjs +8 -7
  25. package/lib/core.mjs +331 -75
  26. package/lib/instance-color.mjs +58 -0
  27. package/lib/instance-events.mjs +1 -1
  28. package/lib/instance-inspect.mjs +107 -23
  29. package/lib/instance-lifecycle.mjs +21 -1
  30. package/lib/instance-naming.mjs +187 -0
  31. package/lib/instance-resolution.mjs +10 -6
  32. package/lib/instance-upgrade.mjs +1445 -0
  33. package/lib/materialize.mjs +287 -89
  34. package/lib/servers.mjs +24 -11
  35. package/lib/soul-composition.mjs +7 -3
  36. package/lib/upgrade-state.mjs +101 -0
  37. package/lib/workspace.mjs +4 -1
  38. package/lib/worktree.mjs +103 -12
  39. package/package-catalog.json +5 -4
  40. package/package.json +1 -1
  41. package/packages/client/cli-adapter.mjs +52 -6
  42. package/packages/client/cli-locator.mjs +2 -2
  43. package/packages/client/composed-gate.mjs +10 -2
  44. package/packages/client/deployment-data.mjs +5 -0
  45. package/packages/client/deployment-identity.mjs +40 -0
  46. package/packages/client/deployment-panel.mjs +9 -3
  47. package/packages/client/inspect-boundary.mjs +11 -7
  48. package/packages/client/inspect-cache.mjs +2 -2
  49. package/packages/client/instance-name.mjs +28 -0
  50. package/packages/client/instance-presentation.mjs +11 -0
  51. package/packages/client/keymap.mjs +133 -28
  52. package/packages/client/keys-file.mjs +126 -0
  53. package/packages/client/own-codes.mjs +4 -2
  54. package/packages/client/panel-views.mjs +78 -12
  55. package/packages/client/spawn-naming.mjs +32 -0
  56. package/packages/client/spawn-preview-cli.mjs +58 -7
  57. package/packages/client/spawn-preview-contract.mjs +59 -0
  58. package/packages/client/spawn-preview-facts.mjs +8 -4
  59. package/packages/client/spawn-preview.mjs +81 -3
  60. package/packages/client/team-members.mjs +4 -1
  61. package/packages/client/workspace-step.mjs +25 -0
  62. package/packages/tui/README.md +7 -7
  63. package/packages/tui/lib/actions.mjs +92 -25
  64. package/packages/tui/lib/client.mjs +9 -4
  65. package/packages/tui/lib/config.mjs +50 -9
  66. package/packages/tui/lib/main.mjs +65 -24
  67. package/packages/tui/lib/model.mjs +292 -75
  68. package/packages/tui/lib/read.mjs +53 -1
  69. package/packages/tui/lib/store.mjs +469 -100
  70. package/packages/tui/lib/term/keys.mjs +22 -6
  71. package/packages/tui/lib/term/kitty.mjs +54 -11
  72. package/packages/tui/lib/term/screen.mjs +15 -6
  73. package/packages/tui/lib/term/through.mjs +66 -28
  74. package/packages/tui/lib/views/frame.mjs +223 -165
  75. package/packages/tui/lib/views/help.mjs +134 -29
  76. package/packages/tui/lib/views/instances.mjs +63 -22
  77. package/packages/tui/lib/views/palette.mjs +14 -4
  78. package/packages/tui/lib/views/souls.mjs +2 -2
  79. package/packages/tui/lib/views/switcher.mjs +6 -1
  80. package/packages/tui/lib/views/workspaces.mjs +55 -0
  81. package/skills/oats-getting-started/SKILL.md +96 -42
@@ -9,10 +9,10 @@
9
9
  // removed, and nothing here decides what a failed read means or which view a deployment is in.
10
10
  // Selection is an identity (the shared `instanceId`), never an index, so it survives every read;
11
11
  // the rows it moves among are the model's, in drawn order.
12
- import { ACTIONS, LEADER_KEYS, backKeyLabel, bindings, chordKeys } from "./actions.mjs";
13
- import { collapseKey, displayLine, instanceActs, instanceId, outcomeWords, pendingEntry, spawnProblem } from "./client.mjs";
12
+ import { ACTIONS, LEADER_KEYS, backKeyLabel, bindings, chordKeys, chordsRead, paletteKeys } from "./actions.mjs";
13
+ import { CHORDS, chordFamilyFrom, collapseKey, workspaceStep, displayLine, instanceActs, instanceId, outcomeWords, pendingEntry, spawnProblem } from "./client.mjs";
14
14
  import { defaultDetachKey } from "./config.mjs";
15
- import { BUTTON_ORDER, SPAWN_WORDS, SPLIT_WORDS, TAB_WORDS, actDecision, capabilitiesHeight, capabilitiesOf, capabilityUsersOf, disclosure, openDecision, paletteItems, preflightLine, returnedLine, roster, soulNames, soulOfAgent, soulsOf, spawnChoices, spawnChoicesKey, spawnLine, spawnPickerItems, spawnReady, spawnRing, spawnSelector, spawnedRow, switcher, switcherHeight, LIST_WORDS } from "./model.mjs";
15
+ import { BUTTON_ORDER, SPAWN_WORDS, LEADER_WORDS, SPLIT_WORDS, TAB_WORDS, actDecision, capabilitiesHeight, capabilitiesOf, capabilityUsersOf, disclosure, openDecision, paletteItems, paletteStep, preflightLine, returnedLine, roster, soulNames, soulOfAgent, soulsOf, spawnChoices, spawnChoicesKey, spawnLine, spawnPickerItems, spawnReady, spawnRing, spawnSelector, spawnedRow, switcher, switcherHeight, switcherWindow, memoryKey, memoryKeys, LIST_WORDS, WORKSPACE_WORDS, listChoice, listChoices, workspaceList } from "./model.mjs";
16
16
  import { backKeyId, backKeySequences, isBackKey } from "./term/through.mjs";
17
17
  import { windowOf } from "./views/scroll.mjs";
18
18
  import { SPLIT_MIN, splitFits } from "./views/split.mjs";
@@ -49,14 +49,19 @@ const NO_CAPABILITIES = Object.freeze({ wanted: false, held: new Map(), selected
49
49
  * `rosterAt`, `memory`, `notes`, `collecting`: the roster of other machines as the shared rule
50
50
  * left it, the time of the last outcome taken, what the machines reported, and whether a roster
51
51
  * read is in flight. `unread`: per served deployment whose read failed with nothing kept, the
52
- * instances it last held (see `took`). `loose`: the list was scrolled away from the selection
52
+ * instances it last held, and `unreadKeys` the memoryKey it was under then (see `took`). `loose`: the list was scrolled away from the selection
53
53
  * on purpose (see `scrolledList`); the switcher has its own.
54
- * `selected`, `top`, `detailTop` are the open view's; `places` keeps them for
55
- * the views left behind, and `collapsed` holds every view's keys (each carries its `open`).
54
+ * `selected`, `top`, `detailTop` are the open view's; `places` keeps them, with the filter, for
55
+ * the workspaces left behind (per lib/model.mjs memoryKey; see `opened`), and `collapsed` holds
56
+ * every view's keys (each carries its `open`).
56
57
  * `back`: the view under the help. `listed`: this computer's deployments list as it was last read
57
58
  * (awebai/oats#1390): `rows`, the shared reader's `{ path, name, state }` in the kernel's order, of
58
59
  * the last read that answered, and `failure`, the reason (`{ code, message }`) the last read failed
59
60
  * with, or null once one answered. The switcher offers the rows this run does not serve.
61
+ * `identities`: what the identity reads learned of the rows of that list (lib/main.mjs), path ->
62
+ * `{ ok: true, workspace }` or `{ ok: false, reason }` (deployment-identity.mjs), the last answer for
63
+ * each, kept for the run: the switcher matches a listed deployment to its workspace by it, and a
64
+ * deployment opened from there is in its workspace's view before its first read (lib/model.mjs heldOf).
60
65
  * `opening`: the agent of another machine whose terminal is being opened, or null:
61
66
  * `{ id, phase: "preflight", name, machine, routed }` while that machine is asked whether its
62
67
  * session is there; `name`, `machine` and `routed` are the row's as it was when Enter was
@@ -73,19 +78,32 @@ const NO_CAPABILITIES = Object.freeze({ wanted: false, held: new Map(), selected
73
78
  * "terminal" while what is typed goes to the agent and "list" while the TUI reads keys, and `zoom`
74
79
  * true while it has the whole body (only with the terminal's focus).
75
80
  * `outcome`: the line after an open (the model's `{ said, under }`), shown above the panes until
76
- * the next key bound to an action, or null. `notice`: such a line that came while no view that
81
+ * the next key bound to an action, or null; with `once: true` (the list of workspaces' end, said
82
+ * by the leader's Up or Down, and the leader cancelled by its own read) until the next key of any kind (the leader's: the key after it), one
83
+ * typed to the agent included, or a resize (the loop's `{ passed }`). `notice`: such a line that came while no view that
77
84
  * draws one at every width was shown (the switcher, the help, a detail or a terminal with the
78
85
  * focus: see `drawsOutcome`), held until one is; or null.
79
86
  * It is the add of the deployment the run started in, refused.
80
87
  * `detachKey`: tui.json's `open.detachKey`, the leader (docs/tui.md, "The leader"; without one,
81
- * `platform`'s default, config.mjs defaultDetachKey), and `leaderKey` the key id it is read as
88
+ * the default, C-x on every platform: config.mjs defaultDetachKey), and `leaderKey` the key id it is read as
82
89
  * where the TUI reads keys (term/through.mjs backKeyId).
83
90
  * `leader`: `{ read, waiting }` from the leader's read on, or null (see `leading`).
84
91
  * `keyboard`: the flags the terminal confirmed for the kitty keyboard protocol (lib/main.mjs, the
85
92
  * event `{ keyboard }`), or null while it has not; `chords`: the chords of `platform`, key id ->
86
- * `does` (actions.mjs chordKeys), read only while `keyboard` is not null (see `leading`).
87
- * `palette`: the palette while it is open (see `paletteOpened`), or null. `recent`: the ids of
88
- * the instances opened in this run, the latest first.
93
+ * `does` (actions.mjs chordKeys: on macOS the arrow chords alone, `mac` true), in the family keys.json chose, but the one the leader's key
94
+ * takes, every one read while `keyboard` is not null, and those with bytes of their own while it
95
+ * is null: the whole Alt family, the arrows of the others (actions.mjs chordsRead; see `leading`). `keysRead`: what keys.json said at the start (lib/config.mjs loadKeys, the shared
96
+ * reader's frozen `{ family, why }`; no file when none is given): the family `chords` are bound
97
+ * in, and why it is the default when the file did not choose it. The help names both.
98
+ * `palette`: the palette while it is open (see `paletteOpened`), or null; `paletteKeys` its own
99
+ * keys on `platform`, key id -> `does` (actions.mjs paletteKeys: none on macOS), which win over
100
+ * every other key while it is open. `recent`: per workspace (memoryKey), the ids of its instances
101
+ * opened or shown in this run, the latest first (recentWith).
102
+ * `layouts`: per workspace left behind, its tabs as they were (layoutOf); `restoring`: the restore
103
+ * of the open workspace's while it opens them again (`{ key, tabs, split, group, shown, focus,
104
+ * zoom, queue, grant }`: the layout, the tabs not opened yet, and whether the focus is still given
105
+ * back), or null (restoreNext); `leftFocus`: `{ id, zoom }` while another view stands over the list
106
+ * and the terminal had the focus before it, or null (reduceStep).
89
107
  * `act`: the confirmation open in the act view (see `actOpened`), or null; `acted` counts the
90
108
  * confirmations opened in this run, so an answer for one that was closed is told apart.
91
109
  * `starting`: id -> `{ verb, since }` of this run's starts and restarts whose child is still
@@ -103,24 +121,26 @@ const NO_CAPABILITIES = Object.freeze({ wanted: false, held: new Map(), selected
103
121
  * agentsRoot, agent, reads, words }`, until its row is opened or two reads did not show it;
104
122
  * `previews` the number of the last preview read asked in the run, so that an answer to a prompt
105
123
  * already closed is never taken by the next one. */
106
- export function initialState({ deployment = null, cli, home, entry = null, actions = ACTIONS, ascii = false, platform = process.platform, detachKey = defaultDetachKey(platform) }) {
124
+ export function initialState({ deployment = null, cli, home, entry = null, actions = ACTIONS, ascii = false, colour = true, platform = process.platform, detachKey = defaultDetachKey(platform), keysRead = NO_KEYS_FILE }) {
107
125
  return Object.freeze({
108
126
  view: deployment === null ? "switcher" : "instances", back: deployment === null ? "switcher" : "instances",
109
127
  ctxs: Object.freeze(deployment === null ? [] : [deployment]), open: deployment,
110
128
  entries: new Map(deployment === null ? [] : [[deployment, entry]]), numbers: new Map(),
111
129
  remote: Object.freeze({ remoteGroups: Object.freeze([]), rosterAnswered: false, rosterFailure: null }), rosterAt: null,
112
130
  memory: new Map(), notes: new Map(), collecting: false, cli, home,
113
- selected: null, collapsed: new Set(), filter: NO_FILTER, focus: "list", top: 0, loose: false, detailTop: 0, places: new Map(), unread: new Map(),
131
+ selected: null, collapsed: new Set(), filter: NO_FILTER, focus: "list", top: 0, loose: false, detailTop: 0, places: new Map(), unread: new Map(), unreadKeys: new Map(), workspaceCursor: null, colour,
114
132
  switcher: NO_SWITCHER,
115
- listed: NO_LIST,
133
+ listed: NO_LIST, identities: new Map(),
116
134
  helpTop: 0, actions, ascii, detachKey, opening: null, terminal: null, outcome: null, act: null, acted: 0, starting: new Map(), retiring: new Set(),
117
- leaderKey: backKeyId(detachKey), leader: null, keyboard: null, chords: chordKeys(platform === "darwin", actions), palette: null, recent: Object.freeze([]), souls: NO_SOULS, capabilities: NO_CAPABILITIES,
135
+ leaderKey: backKeyId(detachKey), leader: null, keyboard: null, keysRead, mac: platform === "darwin", chords: chordKeys(platform === "darwin", actions, CHORDS, keysRead.family, backKeyId(detachKey)), palette: null, paletteKeys: paletteKeys(platform === "darwin"), recent: new Map(), layouts: new Map(), restoring: null, leftFocus: null, souls: NO_SOULS, capabilities: NO_CAPABILITIES,
118
136
  spawn: null, spawning: null, spawnCheck: null, arriving: null, previews: 0, tabs: Object.freeze([]), split: null, group: 0, aside: null,
119
137
  notice: null,
120
138
  });
121
139
  }
122
140
 
123
141
  const next = (state, changes) => Object.freeze({ ...state, ...changes });
142
+ /** keys.json's answer when the run read none (a test's state): no file, the default family. */
143
+ const NO_KEYS_FILE = chordFamilyFrom(null);
124
144
 
125
145
  /** The switcher's own: the filter, the selection and the scroll; `line`, what it says above its rows
126
146
  * of what happened (lib/model.mjs SWITCHER_LINES: "gone", or the answer of a Remove); `refused`,
@@ -181,8 +201,9 @@ function rehomed(before, state) {
181
201
  /** The switcher, shown: over whatever view was there. `said`: a line of its own to show. */
182
202
  const toSwitcher = (state, said = null) => next(state, { view: "switcher", switcher: Object.freeze({ ...state.switcher, editing: false, loose: false, line: said, refused: null, removing: null }) });
183
203
 
184
- /** After what the reader gave changed: when no view holds what was open any more (a machine that
185
- * is no longer listed), the switcher is shown and says so, and nothing is open. Else the
204
+ /** After what the reader gave changed: the memory of a workspace no view holds any more is
205
+ * forgotten (remembered). When no view holds what was open any more (a machine that is no longer
206
+ * listed), the switcher is shown and says so, and nothing is open. Else the
186
207
  * collapse keys of this view whose instance is gone are forgotten, and the selection finds its
187
208
  * place.
188
209
  * THAT AN INSTANCE IS GONE IS KNOWN ONLY OF A DEPLOYMENT THAT WAS READ. One whose read failed
@@ -192,13 +213,14 @@ const toSwitcher = (state, said = null) => next(state, { view: "switcher", switc
192
213
  * meanwhile (another deployment, the roster of other machines), so the next good read finds
193
214
  * the user where they were. A machine's rows are kept by the shared rule while it does not
194
215
  * answer, so they need no such care. */
195
- function settled(state, taken) {
196
- const now = roster(taken);
216
+ function settled(state, was) {
217
+ const taken = remembered(was), now = roster(taken);
197
218
  if (taken.open !== null && now.gone) {
198
- const places = new Map(taken.places); places.delete(taken.open);
219
+ // The workspace's memory goes with it (reduceStep closes its tabs).
220
+ const forgot = forgotten(taken, memoryKey(state, state.open));
199
221
  // A confirmation open over that view is closed with it: what it sent goes on.
200
222
  const dismiss = taken.act !== null ? { dismiss: taken.act.n } : {};
201
- return { state: toSwitcher(next(taken, { open: null, places, selected: null, top: 0, loose: false, detailTop: 0, focus: "list", filter: NO_FILTER, back: "switcher", act: null }), "gone"), effect: { list: true, ...dismiss } };
223
+ return { state: toSwitcher(next(taken, { open: null, ...forgot, selected: null, top: 0, loose: false, detailTop: 0, focus: "list", filter: NO_FILTER, back: "switcher", act: null }), "gone"), effect: { list: true, identify: true, ...dismiss } };
202
224
  }
203
225
  const before = roster(state);
204
226
  const unknown = new Set([...taken.unread.values()].flatMap((ids) => [...ids]));
@@ -208,20 +230,33 @@ function settled(state, taken) {
208
230
  const pruned = stillThere.length === taken.collapsed.size ? taken : next(taken, { collapsed: new Set(stillThere) });
209
231
  return { state: unknown.has(pruned.selected) ? pruned : rehomed(before, pruned), effect: null };
210
232
  }
233
+ /** The memory of every workspace no view holds any more (memoryKeys) forgotten, a hidden one's
234
+ * too, so one that comes back starts afresh. */
235
+ function remembered(state) {
236
+ if (!state.places.size && !state.recent.size && !state.layouts.size) return state;
237
+ const keys = memoryKeys(state);
238
+ const kept = (map) => { const left = new Map([...map].filter(([key]) => keys.has(key))); return left.size === map.size ? map : left; };
239
+ const places = kept(state.places), recent = kept(state.recent), layouts = kept(state.layouts);
240
+ return places === state.places && recent === state.recent && layouts === state.layouts ? state : next(state, { places, recent, layouts });
241
+ }
211
242
 
212
243
  /** A served deployment's entry from the reader, with the number of the cycle that gave it. An
213
244
  * answer for a deployment that is not served, or one that is not newer than the last taken for
214
245
  * it, is dropped. Only an observed entry moves the selection or forgets a collapse: a failed
215
246
  * read has no rows to move to, and the next good one finds the user where they were.
216
247
  * `unread`: for a deployment whose read failed with nothing kept, the instances it held when it
217
- * was last read, until it is read again. They are what `settled` must not take for gone. */
248
+ * was last read, until it is read again, and (`unreadKeys`) the workspace it was in: they are what
249
+ * `settled` must not take for gone, nor `remembered` forget. */
218
250
  function took(state, { id, entry, number }) {
219
251
  if (!state.ctxs.includes(id) || !(number > (state.numbers.get(id) ?? 0))) return { state, effect: null };
220
252
  const observed = entry?.deployment?.status === "observed", held = state.entries.get(id);
221
- let unread = state.unread;
222
- if (observed && unread.has(id)) { unread = new Map(unread); unread.delete(id); }
223
- else if (!observed && held?.deployment?.status === "observed") unread = new Map(unread).set(id, new Set((Array.isArray(held.instances) ? held.instances : []).map(instanceId)));
224
- const taken = next(state, { entries: new Map(state.entries).set(id, entry), numbers: new Map(state.numbers).set(id, number), unread });
253
+ let unread = state.unread, unreadKeys = state.unreadKeys;
254
+ if (observed && unread.has(id)) { unread = new Map(unread); unread.delete(id); unreadKeys = new Map(unreadKeys); unreadKeys.delete(id); }
255
+ else if (!observed && held?.deployment?.status === "observed") {
256
+ unread = new Map(unread).set(id, new Set((Array.isArray(held.instances) ? held.instances : []).map(instanceId)));
257
+ unreadKeys = new Map(unreadKeys).set(id, memoryKey(state, id));
258
+ }
259
+ const taken = next(state, { entries: new Map(state.entries).set(id, entry), numbers: new Map(state.numbers).set(id, number), unread, unreadKeys });
225
260
  if (!observed) return { state: taken, effect: null };
226
261
  const step = settled(state, taken);
227
262
  if (step.state.arriving?.workspace !== id) return step;
@@ -235,15 +270,17 @@ function heard(state, { remote, memory, notes, at }) {
235
270
  return settled(state, next(state, { remote, memory, notes, rosterAt: at, collecting: false }));
236
271
  }
237
272
 
238
- /** Open the view of deployment `id`: the place the view left behind is kept, and found again
239
- * on the way back (the row, where the list and the detail were scrolled). A filter is not kept. */
273
+ /** Open the view of deployment `id`: the place the workspace left behind is kept (`places`, per
274
+ * memoryKey), and found again on the way back: the row, where the list and the detail were
275
+ * scrolled, and the filter (docs/tui.md, "Tabs"), a field still being typed in kept as Enter keeps
276
+ * it. Another deployment of the same workspace finds the same place. */
240
277
  function opened(state, id) {
241
278
  const shown = next(state, { view: "instances", back: "instances", switcher: Object.freeze({ ...state.switcher, editing: false, query: "", line: null, refused: null, removing: null }) });
242
279
  if (id === state.open) return shown;
243
280
  const places = new Map(state.places);
244
- if (state.open !== null) places.set(state.open, Object.freeze({ selected: state.selected, top: state.top, detailTop: state.detailTop }));
245
- const place = places.get(id) ?? { selected: null, top: 0, detailTop: 0 };
246
- return rehomed({ rows: [] }, next(shown, { open: id, places, focus: "list", filter: NO_FILTER, loose: false, ...place }));
281
+ if (state.open !== null) places.set(memoryKey(state, state.open), Object.freeze({ selected: state.selected, top: state.top, detailTop: state.detailTop, filter: Object.freeze({ ...state.filter, editing: false }) }));
282
+ const place = places.get(memoryKey(state, id)) ?? { selected: null, top: 0, detailTop: 0, filter: NO_FILTER };
283
+ return rehomed({ rows: [] }, next(shown, { open: id, places, focus: "list", loose: false, ...place }));
247
284
  }
248
285
 
249
286
  /** A picked row of this computer's deployments list that is a deployment (lib/read.mjs made it
@@ -255,6 +292,68 @@ function admitted(state, { admitted: id, at }) {
255
292
  return opened(joined, id);
256
293
  }
257
294
 
295
+ /** The deployments of a workspace picked in the switcher that were on this computer's list and not served
296
+ * (lib/main.mjs asked each whether it is a deployment now): `admitted` are served from now on, pending
297
+ * until their first read, which the identity read already placed in their workspace's view; then the
298
+ * workspace opens on the first of `opens` (its deployments, this machine's first) that is served or is
299
+ * another machine's (the workspace already open stays open, its place and tabs kept). A folder refused is not served; when nothing of the workspace can be opened the
300
+ * switcher stays, and the list is read again, so its rows say what the folders are now. */
301
+ function joinedHeard(state, { admitted: ids, opens, at }) {
302
+ const added = ids.filter((id) => !state.ctxs.includes(id));
303
+ const entries = new Map(state.entries);
304
+ for (const id of added) entries.set(id, pendingEntry(null, at));
305
+ const joined = added.length ? next(state, { ctxs: Object.freeze([...state.ctxs, ...added]), entries }) : state;
306
+ const target = opens.find((id) => joined.ctxs.includes(id) || !id.startsWith("/")) ?? null;
307
+ if (target === null) return { state: joined, effect: { list: true } };
308
+ // The workspace already open (from another machine's deployment) stays where its list was, and keeps its tabs (reduceStep).
309
+ const same = state.open !== null && memoryKey(joined, target) === memoryKey(state, state.open);
310
+ const shown = opened(joined, target);
311
+ return { state: same ? next(shown, { selected: state.selected, top: state.top, detailTop: state.detailTop }) : shown, effect: { refresh: { live: true } } };
312
+ }
313
+
314
+ /** The deployments a choice of the switcher stands for: a workspace's, a listed row's path, an unmatched
315
+ * view's primary (lib/model.mjs switcher). */
316
+ const choiceDeployments = (item) => item.opens ?? [item.path ?? item.primary].filter(Boolean);
317
+
318
+ /** The switcher's selection after `after` changed its rows: kept while its choice is listed; else on the
319
+ * choice that now holds a deployment the selected one stood for (a listed row whose workspace a read
320
+ * learned is in that workspace's row from then on, and back on its own row when a later answer takes
321
+ * that away), so Enter opens what was selected. */
322
+ function reselected(before, after) {
323
+ const was = switcher(before);
324
+ if (was.selected === null) return after;
325
+ const now = switcher(after);
326
+ if (now.keys.includes(was.selected)) return after;
327
+ const ids = choiceDeployments(was.items.find((item) => item.key === was.selected));
328
+ const found = now.items.find((item) => item.key !== undefined && choiceDeployments(item).some((id) => ids.includes(id)));
329
+ return found ? next(after, { switcher: Object.freeze({ ...after.switcher, selected: found.key, loose: false }) }) : after;
330
+ }
331
+
332
+ /** The list of workspaces' cursor after a step changed its rows (`before`, then `step`): kept while its
333
+ * row is listed; else on the row that now holds a deployment the cursor's stood for, as the switcher's
334
+ * selection follows it (`reselected`): a listed row whose workspace an identity read learned is in that
335
+ * workspace's row from then on, and back on a row of its own when a later answer takes that away, so
336
+ * Enter opens what was under the cursor. With no such row, the list's own rule: the open row. */
337
+ function cursorKept(before, step) {
338
+ const s = step.state, key = s.workspaceCursor;
339
+ if (key === null || key !== before.workspaceCursor || s.open === null || before.open === null) return step;
340
+ const list = workspaceList(s);
341
+ if (list.rows.some((row) => row.key === key)) return step;
342
+ const was = listChoice(before, key);
343
+ if (was === null) return step;
344
+ const ids = choiceDeployments(was);
345
+ const keys = new Set(list.rows.map((row) => row.key));
346
+ const found = listChoices(s).find((item) => keys.has(item.key) && choiceDeployments(item).some((id) => ids.includes(id)));
347
+ return found ? { ...step, state: next(s, { workspaceCursor: found.key }) } : step;
348
+ }
349
+
350
+ /** What an identity read learned of a deployment of this computer's list (lib/main.mjs): kept for the run,
351
+ * the last answer for each path; an answer replaces the one before it. The switcher's selection
352
+ * follows the deployment it stood on (`reselected`). */
353
+ function identityHeard(state, { path, read }) {
354
+ return { state: reselected(state, next(state, { identities: new Map(state.identities).set(path, read) })), effect: null };
355
+ }
356
+
258
357
  /** The filter's text changed. */
259
358
  function queried(state, query) {
260
359
  if (query === state.filter.query) return state;
@@ -506,7 +605,7 @@ function switched(state, action, page, event) {
506
605
  // The rows of the pane the choices have: the lines above them take theirs, and the filter
507
606
  // field one while it is shown (lib/model.mjs switcherHeight).
508
607
  const count = s.items.length, height = switcherHeight(state, page, event.area);
509
- const drawn = windowOf({ top: sw.top, count, height, keep: sw.loose ? -1 : s.items.findIndex((item) => item.key !== undefined && item.key === s.selected) }).top;
608
+ const drawn = switcherWindow(s.items, sw.loose ? null : s.selected, { top: sw.top, height }).top;
510
609
  const from = Math.max(0, s.keys.indexOf(s.selected));
511
610
  const key = s.keys.length ? s.keys[Math.min(Math.max(0, moved(action, from, s.keys.length, page)), s.keys.length - 1)] : null;
512
611
  // No choice to go to in that direction (the first, the last, or none at all): the rows
@@ -515,16 +614,14 @@ function switched(state, action, page, event) {
515
614
  const top = windowOf({ top: moved(action, drawn, count, height), count, height }).top;
516
615
  return top === drawn ? stay : { state: put({ top, loose: true }), effect: null };
517
616
  }
518
- const at = s.items.findIndex((item) => item.key === key);
519
- // The headings above the first choice come into the pane with it, and what stands under the last one.
520
- const top = windowOf({ top: key === s.keys[0] ? 0 : key === s.keys.at(-1) ? count : drawn, count, height, keep: at }).top;
617
+ // The headings above the first choice come into the pane with it, and what stands under the last one;
618
+ // a workspace's indicator lines come in with it while they fit (lib/model.mjs switcherWindow).
619
+ const top = switcherWindow(s.items, key, { top: key === s.keys[0] ? 0 : key === s.keys.at(-1) ? count : drawn, height }).top;
521
620
  return { state: put({ selected: key, top, loose: false }), effect: null };
522
621
  }
523
622
  case "open": {
524
623
  const item = s.items.find((each) => each.key !== undefined && each.key === s.selected);
525
- if (!item) return stay;
526
- if (item.path !== undefined) return item.state === "ok" ? { state: put({ refused: null }), effect: { admit: item.path } } : stay;
527
- return item.primary === null ? stay : { state: opened(state, item.primary), effect: { refresh: { live: true } } };
624
+ return (item && picked(state, item)) ?? stay;
528
625
  }
529
626
  case "remove": {
530
627
  const item = s.items.find((each) => each.key !== undefined && each.key === s.selected);
@@ -536,6 +633,68 @@ function switched(state, action, page, event) {
536
633
  }
537
634
  }
538
635
 
636
+ /** A choice of the switcher's (or the list of workspaces', the same choices) picked: a row of the
637
+ * list that is a deployment is asked about (`admit`; a stale one does not open: its row says why);
638
+ * a workspace with deployments of this computer's list that this run does not serve has each
639
+ * asked whether it is a deployment now, and served from then on (`joinedHeard`), before it opens;
640
+ * else its view opens here. Null when nothing opens. */
641
+ function picked(state, item) {
642
+ const put = (s) => next(s, { switcher: Object.freeze({ ...s.switcher, refused: null }) });
643
+ if (item.path !== undefined) return item.state === "ok" ? { state: put(state), effect: { admit: item.path } } : null;
644
+ if (item.join?.length) return { state: put(state), effect: { join: Object.freeze({ paths: item.join, opens: item.opens }) } };
645
+ return item.primary === null ? null : { state: opened(state, item.primary), effect: { refresh: { live: true } } };
646
+ }
647
+
648
+ /** THE LIST OF WORKSPACES' FOCUS (docs/tui.md, "The list of workspaces"): `w` (or the leader's `o`)
649
+ * gives it to the open workspace's row; the cursor (`workspaceCursor`) moves among the rows that
650
+ * open, passing over the others; Enter opens its row as the switcher's Enter does, and gives the
651
+ * list of instances the focus; Esc gives it back with nothing opened; `w` again shows the
652
+ * switcher, with what the list does not hold. Any other key first gives the focus back, then does
653
+ * what it does there. -> { state, effect }, or null for such a key. */
654
+ function inWorkspaces(state, action) {
655
+ const list = workspaceList(state), cursor = list.rows.some((row) => row.key === state.workspaceCursor) ? state.workspaceCursor : list.current;
656
+ const back = next(state, { focus: "list", workspaceCursor: null });
657
+ switch (action) {
658
+ case "up": case "down": case "top": case "bottom": {
659
+ const openable = list.rows.filter((row) => row.openable);
660
+ const key = action === "top" ? openable[0]?.key : action === "bottom" ? openable.at(-1)?.key : workspaceStep(list.rows, cursor, action === "up" ? -1 : 1);
661
+ return key == null || key === cursor ? { state, effect: null } : { state: next(state, { workspaceCursor: key }), effect: null };
662
+ }
663
+ case "open": {
664
+ const item = cursor === null ? null : listChoice(state, cursor);
665
+ return (item && picked(back, item)) ?? { state, effect: null };
666
+ }
667
+ case "close": case "instances": return { state: back, effect: null };
668
+ case "workspaces": return { state: toSwitcher(next(back, { back: "switcher" })), effect: { list: true, identify: true } };
669
+ default: return null;
670
+ }
671
+ }
672
+ /** `w`: the list of workspaces' focus, on the Instances view (whatever was shown settled first), the
673
+ * open workspace's row under the cursor. With no workspace open there is no list: the switcher. */
674
+ function toWorkspaces(state) {
675
+ if (state.open === null) return { state: toSwitcher(next(state, { back: "switcher" })), effect: { list: true, identify: true } };
676
+ const settled = toInstances(state);
677
+ if (settled.state.view !== "instances") return settled;
678
+ return { state: next(settled.state, { focus: "workspaces", workspaceCursor: workspaceList(settled.state).current }), effect: settled.effect };
679
+ }
680
+ /** The leader's Up and Down (`workspace-previous`, `workspace-next`): the workspace of the row before
681
+ * or after the open one's, as the list draws them (the shared workspaceStep: rows that do not open
682
+ * are passed over, and it does not wrap), opened as a pick of it opens it. From the terminal's
683
+ * focus it is kept for the workspace left (layoutOf); from anywhere else what is open is settled
684
+ * first, as for the leader's other keys (toList). At an end, the line says so and nothing moves:
685
+ * with the list of workspaces' focus, the focus stays there, its cursor where it was. */
686
+ function workspaceMoved(state, step, inTerminal) {
687
+ const settled = inTerminal ? { state, effect: null } : toList(state), s = settled.state;
688
+ const list = workspaceList(s), key = workspaceStep(list.rows, list.current, step);
689
+ const item = key === null ? null : listChoice(s, key);
690
+ if (item === null) {
691
+ const kept = state.view === "instances" && state.focus === "workspaces" ? { focus: "workspaces", workspaceCursor: state.workspaceCursor } : {};
692
+ return { state: next(s, { ...kept, outcome: Object.freeze({ said: step < 0 ? WORKSPACE_WORDS.above() : WORKSPACE_WORDS.below(), under: Object.freeze([]), once: true }) }), effect: settled.effect };
693
+ }
694
+ const moved = picked(s, item) ?? { state: s, effect: null };
695
+ return { state: moved.state, effect: both(settled.effect, moved.effect) };
696
+ }
697
+
539
698
  /** A key while the switcher asks whether to remove a row of this computer's deployments list
540
699
  * (docs/tui.md, "The switcher"). Enter removes it: the loop runs `oats deployments remove`, and
541
700
  * nothing on disk is touched. Esc keeps it, and so does every other key, which then does what it
@@ -575,8 +734,13 @@ function removedHeard(state, { name, ok, reason = null }) {
575
734
  * (one outcome of the roster read), `{ collecting }`, `{ retiring: <Set of instanceIds> }` (this
576
735
  * run's retires in flight now), `{ listed: { rows } | { failure } }` (this computer's deployments
577
736
  * list read, or why it was not), `{ admitted: <deployment>, at }` and `{ refused: <path> }` (what a
578
- * picked row turned out to be), `{ removed: { path, name, ok, reason } }` (a Remove's answer) and
579
- * `{ notListed: <reason> }` (the deployment the run started in was not added to the list).
737
+ * picked row turned out to be), `{ joined: { admitted, opens }, at }` (which listed deployments of a
738
+ * workspace picked in the switcher are deployments now, and the ones it may open, in order),
739
+ * `{ identity: { path, read } }` (what an identity read answered of a row of the list),
740
+ * `{ removed: { path, name, ok, reason } }` (a Remove's answer) and
741
+ * `{ notListed: <reason> }` (the deployment the run started in was not added to the list), and
742
+ * `{ passed: true }` (bytes typed to the agent, or a resize: what the loop saw that the store does
743
+ * not, which ends a line of one key).
580
744
  * Opening an agent gives five more: `{ prepared: { id, error? } }` (the preflight of another
581
745
  * machine's agent ended: `error` is what the shared prepareRemoteTerm threw), `{ given: true }`
582
746
  * (the loop gave the preflight up itself: a suspend), `{ shown: { id } }` (the panel has the
@@ -587,15 +751,21 @@ function removedHeard(state, { name, ok, reason = null }) {
587
751
  * `rest`: more bytes came after it in the same read).
588
752
  * An effect is something only the loop can do: "quit", "suspend", "redraw", or an object with
589
753
  * any of `refresh: { live }` (read now), `list: true` (read this computer's deployments list
590
- * again), `remove: { path, name }` (remove that row from it), `admit: <path>` (ask whether a picked
591
- * row is a deployment), `close: <id>` (close the open
754
+ * again; with `identify: true`, then read the identities of its rows this run does not serve),
755
+ * `remove: { path, name }` (remove that row from it), `admit: <path>` (ask whether a picked
756
+ * row is a deployment), `join: { paths, opens }` (ask that of each of a picked workspace's listed
757
+ * deployments, then answer `joined`), `close: <id>` (close the open
592
758
  * terminal; when an `open` comes with it, before that open), `open: <decision>` (open the agent's
593
759
  * terminal: the model's openDecision, with its target), `panel: <id>` (the preflight is over:
594
760
  * start the viewer it prepared in the panel) and `abandon: true` (give the preflight up); or null.
595
761
  * The line after an open stands until the next key that is bound to an action: a key bound to
596
762
  * nothing (a stray back key is no key at all) leaves it. */
597
763
  export function reduce(state, event, keymap = bindings(state.actions)) {
598
- return askedStill(noticed(reduceStep(state, event, keymap)));
764
+ const step = cursorKept(state, askedStill(noticed(reduceStep(state, event, keymap))));
765
+ // The list of workspaces made once for what the store took (lib/model.mjs workspaceList keeps
766
+ // it while nothing it is made from changes): no paint, resize or key asks the shared home again.
767
+ if (step.state.open !== null) workspaceList(step.state);
768
+ return step;
599
769
  }
600
770
 
601
771
  /** Whether the line under the sidebar's title stands now at every width (views/frame.mjs): in the
@@ -626,15 +796,156 @@ function askedStill(step) {
626
796
  return { ...step, state: next(step.state, { switcher: Object.freeze({ ...step.state.switcher, removing: null }) }) };
627
797
  }
628
798
 
799
+ /** Whether `event` is the person's: a key, text, a paste, or the leader or a chord typed in a
800
+ * terminal. What the loop reports (a read, a viewer shown or ended, a preflight's answer) is not. */
801
+ const personal = (event) => typeof event.key === "string" || event.text !== undefined || event.paste !== undefined || event.back !== undefined || event.chord !== undefined;
802
+ /** The events whose effect the loop does not take (lib/main.mjs keeps their state alone): the
803
+ * preflight given up and the reads dropped as the process stops, the keyboard protocol confirmed,
804
+ * and what a frame drew. A restore takes no step on one of them, which would lose its open: the
805
+ * next event it hears goes on. */
806
+ const UNTAKEN = Object.freeze(["given", "collecting", "passed", "keyboard", "spawnDrawn", "spawnTop", "capabilitiesTop"]);
807
+ const untaken = (event) => UNTAKEN.some((name) => event[name] !== undefined);
808
+
809
+ /** THE MEMORY OF A WORKSPACE (docs/tui.md, "Tabs"), for the run only, per memoryKey: `places` its
810
+ * place in the list (opened), `recent` the palette's recency (recentWith), and `layouts` its tabs
811
+ * as it was left (layoutOf), which a restore opens again (restoreNext). The workspace `key` gone
812
+ * from the views: all of it forgotten (settled, and remembered for the ones not open). */
813
+ function forgotten(state, key) {
814
+ const without = (map) => { const kept = new Map(map); kept.delete(key); return kept; };
815
+ return { places: without(state.places), recent: without(state.recent), layouts: without(state.layouts) };
816
+ }
817
+ /** The tabs of the open workspace as they stand, for its memory, or null with none:
818
+ * `{ tabs: [{ id, name, machine, routed, group }], split, group, shown: [group 0's, group 1's],
819
+ * focus, zoom }`. The focus is the terminal's, or the one it had when another view was shown over
820
+ * the list (`leftFocus`, the switcher a workspace is left from), or the terminal's while the
821
+ * palette opened from it stands (the leader's Up and Down leave from there; the palette took the
822
+ * zoom, as its Esc gives the focus back unzoomed: paletteClosed). During a restore it is the
823
+ * remembered layout, less the tabs that went since (closed, or dropped as gone), and the focus
824
+ * only while the person has not taken it. */
825
+ function layoutOf(state) {
826
+ const r = state.restoring;
827
+ if (r !== null) {
828
+ const still = new Set([...state.tabs, ...r.queue].map((tab) => tab.id)), tabs = r.tabs.filter((tab) => still.has(tab.id));
829
+ return tabs.length ? Object.freeze({ tabs: Object.freeze(tabs), split: r.split, group: r.group, shown: r.shown, focus: r.grant ? r.focus : "list", zoom: r.grant && r.zoom }) : null;
830
+ }
831
+ if (!state.tabs.length) return null;
832
+ const t = state.terminal, left = t === null || t.focus === "terminal" ? null
833
+ : state.leftFocus?.id === t.id ? state.leftFocus : state.palette?.from === "terminal" ? Object.freeze({ id: t.id, zoom: false }) : null;
834
+ return Object.freeze({
835
+ tabs: Object.freeze(state.tabs.map(({ id, name, machine, routed, group }) => Object.freeze({ id, name, machine, routed, group }))),
836
+ split: state.split, group: state.group, shown: Object.freeze([shownOf(state, 0), shownOf(state, 1)]),
837
+ focus: left !== null ? "terminal" : t?.focus ?? "list", zoom: left !== null ? left.zoom : t?.zoom ?? false,
838
+ });
839
+ }
840
+ /** Another workspace opened (or none, the one open gone): the tabs of the one left are kept in its
841
+ * memory, unless it went, then closed, their viewers ended (the agents keep running), the split
842
+ * with them, and an open under way given up. The workspace opened, when its memory holds tabs,
843
+ * starts its restore: they open again one at a time (restoreNext), the focus given back at the
844
+ * end unless the person moved it meanwhile (`grant`). */
845
+ function workspaceLeft(before, step) {
846
+ const s = step.state, layouts = new Map(s.layouts);
847
+ const was = memoryKey(before, before.open), now = memoryKey(s, s.open);
848
+ if (was !== null && s.open !== null) { const kept = layoutOf(before); if (kept !== null) layouts.set(was, kept); else layouts.delete(was); }
849
+ const ids = s.tabs.map((tab) => tab.id), record = now === null ? undefined : layouts.get(now);
850
+ if (record !== undefined) layouts.delete(now);
851
+ const restoring = record === undefined ? null : Object.freeze({ key: now, ...record, queue: record.tabs, grant: true });
852
+ const closed = next(s, { tabs: Object.freeze([]), terminal: null, ...NO_SPLIT, opening: null, layouts, restoring, leftFocus: null });
853
+ const ends = { ...(ids.length ? { close: ids } : {}), ...(s.opening !== null ? { abandon: true } : {}) };
854
+ return { state: closed, effect: Object.keys(ends).length ? both(step.effect, ends) : step.effect };
855
+ }
856
+ /** The next step of a restore (docs/tui.md, "Tabs"): nothing while a tab is starting or another
857
+ * machine is asked (one open at a time), nor before the view's rows are read (an instance not
858
+ * read is not gone), nor, for a tab missing from them, before what holds it is known: the roster
859
+ * of other machines first answered, for another machine's, and for this machine's, its
860
+ * deployment read again when its read failed with nothing kept (unreadHolds). Then the next
861
+ * remembered tab whose instance can be opened now opens, after the ones before it, with the list
862
+ * keeping the focus; one that is gone or cannot be opened (retired, stopped) is dropped, the line
863
+ * saying nothing. Another machine's is asked first again, as an
864
+ * open is, and dropped with the line that says why when that fails (prepared). The last one
865
+ * opened: restoreDone. */
866
+ function restoreNext(state) {
867
+ const r = state.restoring;
868
+ if (state.opening !== null || state.tabs.some((tab) => tab.status === "starting")) return { state, effect: null };
869
+ const rows = roster(state);
870
+ if (!rows.observed) return { state, effect: null };
871
+ let queue = r.queue;
872
+ while (queue.length) {
873
+ const tab = queue[0], row = rows.instances.find((each) => instanceId(each) === tab.id) ?? null;
874
+ if (row === null && (tab.routed ? !state.remote.rosterAnswered : unreadHolds(state, tab.id))) break;
875
+ queue = queue.slice(1);
876
+ const decision = row === null ? null : openDecision(state, row, instanceActs(row));
877
+ if (!decision?.open || state.tabs.length >= TAB_LIMIT) continue;
878
+ const s = next(state, { restoring: Object.freeze({ ...r, queue }) });
879
+ if (!decision.routed) return { state: restoredTab(s, decision), effect: { open: decision } };
880
+ return { state: next(s, { opening: Object.freeze({ id: decision.id, phase: "preflight", name: decision.name, machine: decision.machine, routed: true, restore: true }) }), effect: { open: decision } };
881
+ }
882
+ const left = next(state, { restoring: Object.freeze({ ...r, queue }) });
883
+ return queue.length ? { state: left, effect: null } : restoreDone(left);
884
+ }
885
+ /** Whether instance `id` is one a deployment held when its read failed with nothing kept (`unread`,
886
+ * see `took`): not gone, not known, until that deployment is read again. */
887
+ const unreadHolds = (state, id) => [...state.unread.values()].some((ids) => ids.has(id));
888
+ /** A remembered tab opened again: starting, after the others, shown in the one group a restore
889
+ * opens into, the list keeping the focus. */
890
+ function restoredTab(state, { id, name, machine, routed }) {
891
+ const tab = Object.freeze({ id, name, machine, routed, status: "starting", activity: false, group: 0 });
892
+ return next(state, { tabs: Object.freeze([...state.tabs, tab]), terminal: Object.freeze({ id, name, machine, routed, group: 0, focus: "list", zoom: false, status: "starting" }) });
893
+ }
894
+ /** A restore's end: the tabs opened again take their groups, the split as remembered (when the
895
+ * drops leave a group with none, the other takes the panel, as when a group's last tab closes:
896
+ * ungrouped; a group left empty is empty again);
897
+ * each group shows its remembered tab, or the one a close would show in its place (the next, else
898
+ * the one before); the focused group's, with the remembered focus and zoom while the person has
899
+ * not taken the focus meanwhile, and the list's otherwise. It is the most recent for the palette
900
+ * (showTab). The list's place, filter and collapse stay as they are: the workspace's own, or what
901
+ * the person made of them meanwhile. */
902
+ function restoreDone(state) {
903
+ const r = state.restoring, s = next(state, { restoring: null });
904
+ if (!s.tabs.length) return { state: next(s, { terminal: null, ...NO_SPLIT }), effect: null };
905
+ const open = new Set(s.tabs.map((tab) => tab.id));
906
+ const shownIn = (g) => {
907
+ const mine = r.tabs.filter((tab) => tab.group === g), at = Math.max(0, mine.findIndex((tab) => tab.id === r.shown[g]));
908
+ return [...mine.slice(at), ...mine.slice(0, at).reverse()].find((tab) => open.has(tab.id))?.id ?? null;
909
+ };
910
+ // A group that had no tab when the workspace was left is empty again; one emptied by the drops
911
+ // is no group.
912
+ const had = (g) => r.tabs.some((tab) => tab.group === g);
913
+ const split = r.split !== null && [0, 1].every((g) => shownIn(g) !== null || !had(g));
914
+ const groupOf = new Map(r.tabs.map((tab) => [tab.id, tab.group]));
915
+ const tabs = Object.freeze(s.tabs.map((tab) => { const group = split ? groupOf.get(tab.id) ?? 0 : 0; return tab.group === group ? tab : Object.freeze({ ...tab, group }); }));
916
+ const group = split ? r.group : 0, focused = split ? shownIn(r.group) : shownIn(r.group) ?? shownIn(1 - r.group);
917
+ const laid = next(s, { tabs, split: split ? r.split : null, group, aside: split ? shownIn(1 - r.group) : null, terminal: null });
918
+ // The focused group empty, as it was left: nothing has the focus but the list, and the other
919
+ // group's shown tab is the most recent.
920
+ const step = focused === null ? { state: next(laid, { recent: recentWith(laid, laid.aside) }), effect: null } : showTab(laid, focused, { focus: r.grant ? r.focus : "list", zoom: r.grant && r.zoom });
921
+ return { state: next(step.state, { selected: s.selected, top: s.top, loose: s.loose, detailTop: s.detailTop, filter: s.filter, collapsed: s.collapsed }), effect: step.effect };
922
+ }
923
+
629
924
  function reduceStep(state, event, keymap) {
630
- if (state.outcome !== null && typeof event.key === "string" && keymap.has(event.key)) state = next(state, { outcome: null });
925
+ // (Not the rest of a read the leader came in: that is dropped, and the line says so.)
926
+ if (state.outcome !== null && typeof event.key === "string" && keymap.has(event.key) && !(state.leader !== null && sameRead(state.leader, event))) state = next(state, { outcome: null });
927
+ // A key of the person's while a restore runs: the focus is theirs from then on (restoreDone).
928
+ if (state.restoring?.grant && personal(event)) state = next(state, { restoring: Object.freeze({ ...state.restoring, grant: false }) });
631
929
  let step = reduced(state, event, keymap);
930
+ // A line of one key (`once`) goes at the next key of any kind, bound or not, unless that key said
931
+ // a line of its own. Not at the leader alone: while it waits nothing on the screen changes.
932
+ if (state.outcome?.once === true && personal(event) && step.state.outcome === state.outcome && step.state.leader === null) step = { ...step, state: next(step.state, { outcome: null }) };
632
933
  // The tabs are the open workspace's: whatever opened another (or left it, gone), they all close,
633
- // and the split with them.
634
- if (step.state.open !== state.open && (step.state.tabs.length || step.state.split !== null)) {
635
- const ids = step.state.tabs.map((tab) => tab.id);
636
- step = { state: next(step.state, { tabs: Object.freeze([]), terminal: null, ...NO_SPLIT }), effect: ids.length ? both(step.effect, { close: ids }) : step.effect };
637
- }
934
+ // and the split with them, kept in its memory (workspaceLeft). Another deployment of the same
935
+ // workspace opened (its deployment of this machine, once served, in place of another machine's)
936
+ // keeps them.
937
+ if (step.state.open !== state.open && memoryKey(step.state, step.state.open) !== memoryKey(state, state.open)) step = workspaceLeft(state, step);
938
+ // Nor on a step that quits, suspends or redraws (an effect of its own, which an open would
939
+ // take the place of): the next event goes on.
940
+ if (step.state.restoring !== null && !untaken(event) && typeof step.effect !== "string") { const more = restoreNext(step.state); step = { state: more.state, effect: both(step.effect, more.effect) }; }
941
+ // Where the focus was when another view (the switcher, the help) was shown over the list, or the
942
+ // list of workspaces took the focus: in the terminal, or in the palette opened from it, which give
943
+ // it to the list as they go. A workspace left from there keeps it (layoutOf); back in the list of
944
+ // instances it is forgotten.
945
+ const t0 = state.terminal, listing = (s) => s.view === "instances" && s.focus === "workspaces";
946
+ const away = state.view === "instances" && !listing(state) && (step.state.view !== "instances" || listing(step.state));
947
+ if (away && t0 !== null && step.state.terminal?.id === t0.id && (t0.focus === "terminal" || state.palette?.from === "terminal")) step = { ...step, state: next(step.state, { leftFocus: Object.freeze({ id: t0.id, zoom: t0.focus === "terminal" && t0.zoom }) }) };
948
+ else if (step.state.view === "instances" && !listing(step.state) && step.state.leftFocus !== null) step = { ...step, state: next(step.state, { leftFocus: null }) };
638
949
  // The terminal has the focus only while the list of instances is shown: anything that shows
639
950
  // another view (a workspace gone, which shows the switcher) gives the focus back to the list.
640
951
  const t = step.state.terminal;
@@ -740,17 +1051,27 @@ function ungrouped(state, g, focus) {
740
1051
  return showTab(one, left, { focus, zoom: false });
741
1052
  }
742
1053
 
743
- /** The tab before or after the shown one (`step` -1 or 1) in the focused group shown, without
744
- * wrapping and never into the other group: at either end
745
- * nothing moves, nor while a tab is still starting (it is the shown one until the panel has its
746
- * window). The focus stays where it was, and so does the zoom. */
747
- function tabStepped(state, step, { focus = state.terminal?.focus ?? "list", zoom = state.terminal?.zoom ?? false } = {}) {
1054
+ /** The tab of the focused group that `pick` names, shown: `pick(count, at)` answers its place
1055
+ * among the group's `count` tabs, `at` the shown one's. Never into the other group, and nothing
1056
+ * moves when there is no such tab, when it is the shown one, or while a tab is still starting (it
1057
+ * is the shown one until the panel has its window). The focus stays where it was, and so does
1058
+ * the zoom. */
1059
+ function tabPicked(state, pick, { focus = state.terminal?.focus ?? "list", zoom = state.terminal?.zoom ?? false } = {}) {
748
1060
  const t = state.terminal;
749
1061
  if (t === null || state.tabs.some((tab) => tab.status === "starting")) return { state, effect: null };
750
- const mine = inGroup(state, state.group), at = mine.findIndex((tab) => tab.id === t.id) + step;
751
- if (at < 0 || at >= mine.length) return { state, effect: null };
752
- return showTab(state, mine[at].id, { focus, zoom });
753
- }
1062
+ const mine = inGroup(state, state.group), from = mine.findIndex((tab) => tab.id === t.id), to = pick(mine.length, from);
1063
+ if (!(to >= 0 && to < mine.length) || to === from) return { state, effect: null };
1064
+ return showTab(state, mine[to].id, { focus, zoom });
1065
+ }
1066
+ /** The tab each of these `does` shows (tabPicked): `tab-previous` and `tab-next` (the leader's Left
1067
+ * and Right, the list's keys) the one before or after the shown one, without wrapping, and the
1068
+ * chords' `tab-1` to `tab-8` (actions.mjs CHORD_OWN) the Nth, `tab-9` the last. */
1069
+ const TAB_PICKS = Object.freeze({
1070
+ __proto__: null,
1071
+ "tab-previous": (count, at) => at - 1,
1072
+ "tab-next": (count, at) => at + 1,
1073
+ ...Object.fromEntries(Array.from({ length: 9 }, (_, i) => [`tab-${i + 1}`, i === 8 ? (count) => count - 1 : () => i])),
1074
+ });
754
1075
 
755
1076
  /** The preflight of another machine's agent ended. Given up meanwhile (Esc, a suspend, a second
756
1077
  * open): dropped. Failed: the line that says so, unless it is the person's own giving up. Else
@@ -760,7 +1081,7 @@ function prepared(state, { id, error }) {
760
1081
  if (opening === null || opening.id !== id || opening.phase !== "preflight") return { state, effect: null };
761
1082
  if (error !== undefined) return { state: next(state, { opening: null, outcome: preflightLine(opening, error) }), effect: null };
762
1083
  if (!roster(state).instances.some((row) => instanceId(row) === id)) return { state: next(state, { opening: null }), effect: null };
763
- return { state: tabOpened(next(state, { opening: null }), opening), effect: { panel: id } };
1084
+ return { state: (opening.restore ? restoredTab : tabOpened)(next(state, { opening: null }), opening), effect: { panel: id } };
764
1085
  }
765
1086
 
766
1087
  /** The viewer of a tab ended by itself: its tab closes, the line says what happened, naming its
@@ -805,6 +1126,8 @@ function returned(state, { id, status, stderr, asked = false, refused = null })
805
1126
  * holds its tab's place in the cap. Every open counts for the palette's recency (`recent`), as
806
1127
  * every tab shown does (showTab). */
807
1128
  function openAgent(state, row) {
1129
+ // The person's own open: a restore under way yields, and what it has not opened yet is dropped.
1130
+ if (state.restoring !== null) state = next(state, { restoring: null });
808
1131
  if (state.opening !== null) return { state, effect: null };
809
1132
  const id = instanceId(row);
810
1133
  // A tab still starting is the one shown until the panel has its window (one open at a time).
@@ -850,7 +1173,7 @@ export function splitRefused(state, does, room, inTerminal = false) {
850
1173
  return splitFits(dir, room) ? null : Object.freeze({ said: SPLIT_WORDS.small(dir, SPLIT_MIN), under: Object.freeze([]) });
851
1174
  }
852
1175
  // One group: only from the list, to its terminal.
853
- case "group-next": return state.split === null && (inTerminal || state.terminal === null) ? true : null;
1176
+ case "group-next": case "group-previous": return state.split === null && (inTerminal || state.terminal === null) ? true : null;
854
1177
  case "tab-move": return state.split === null || state.terminal === null ? true : null;
855
1178
  case "group-close": return state.split === null || state.terminal !== null ? true : null;
856
1179
  default: return true;
@@ -860,9 +1183,9 @@ export function splitRefused(state, does, room, inTerminal = false) {
860
1183
  * when it came from the terminal's focus:
861
1184
  * split-right, split-down: the tabs stay in the first group (left or top), and the second, empty,
862
1185
  * is the focused one; the keys are the list's (Settled 2 of T3), so Enter on a row opens there;
863
- * group-next: to the other group's shown terminal, with the focus (to the empty group: focused,
864
- * the keys the list's); from the list, to the focused group's terminal, or with that group
865
- * empty to the other's;
1186
+ * group-next, group-previous: to the other group's shown terminal, with the focus (to the empty
1187
+ * group: focused, the keys the list's); from the list, to the focused group's terminal, or with
1188
+ * that group empty to the other's (with two groups the previous is the next);
866
1189
  * tab-move: the focused group's shown tab to the other group, after its shown tab, shown there
867
1190
  * with the focus; the group it leaves shows its neighbour, or stays, empty;
868
1191
  * group-close: the empty focused group goes; the other takes the panel and is focused, and the
@@ -872,7 +1195,7 @@ function splitKey(state, does, inTerminal) {
872
1195
  switch (does) {
873
1196
  case "split-right": case "split-down":
874
1197
  return { state: next(state, { split: does === "split-right" ? "right" : "down", group: 1, aside: t?.id ?? null, terminal: null, focus: "list" }), effect: null };
875
- case "group-next": {
1198
+ case "group-next": case "group-previous": {
876
1199
  if (state.split === null || (!inTerminal && t !== null)) return { state: terminalWith(state, { focus: "terminal" }), effect: null };
877
1200
  const there = shownOf(state, other);
878
1201
  if (there !== null) return showTab(state, there, { focus: "terminal", zoom: false });
@@ -996,18 +1319,34 @@ function acted(state, event) {
996
1319
  return { state, effect: null };
997
1320
  }
998
1321
 
999
- /** The instances the palette's recency keeps. */
1322
+ /** The preflight under way given up (the process stops: lib/main.mjs stopPreflight). A restore's
1323
+ * tab is put back first in its queue, asked again on the next event the restore hears (one whose
1324
+ * effect the loop takes: reduceStep). */
1325
+ function preflightGiven(state) {
1326
+ const o = state.opening, r = state.restoring, tab = o.restore && r !== null ? r.tabs.find((each) => each.id === o.id) : undefined;
1327
+ const back = tab !== undefined ? { restoring: Object.freeze({ ...r, queue: Object.freeze([tab, ...r.queue]) }) } : {};
1328
+ return { state: next(state, { opening: null, ...back }), effect: null };
1329
+ }
1330
+
1331
+ /** The instances the palette's recency keeps, per workspace. */
1000
1332
  const RECENT_KEPT = 50;
1001
1333
  /** The actions of the split (docs/tui.md, "Splits"). */
1002
- const SPLIT_KEYS = Object.freeze(["split-right", "split-down", "group-next", "tab-move", "group-close"]);
1003
- /** The palette's recency with `id` the most recent: an open, and a tab shown. */
1004
- const recentWith = (state, id) => Object.freeze([id, ...state.recent.filter((each) => each !== id)].slice(0, RECENT_KEPT));
1334
+ const SPLIT_KEYS = Object.freeze(["split-right", "split-down", "group-next", "group-previous", "tab-move", "group-close"]);
1335
+ /** The palette's recency with `id` the most recent in the open workspace's (memoryKey): an open, and
1336
+ * a tab shown. */
1337
+ function recentWith(state, id) {
1338
+ const key = memoryKey(state, state.open), was = state.recent.get(key) ?? [];
1339
+ return new Map(state.recent).set(key, Object.freeze([id, ...was.filter((each) => each !== id)].slice(0, RECENT_KEPT)));
1340
+ }
1005
1341
  /** How many terminals the panel holds open at once (lib/panel.mjs MAX_TERMINALS, which a test
1006
1342
  * holds this to). */
1007
1343
  export const TAB_LIMIT = 4;
1008
1344
  const leaderAt = (read, waiting) => Object.freeze({ read: Number.isInteger(read) ? read : null, waiting });
1009
1345
  /** Whether an event came in the same read of input as the leader. */
1010
1346
  const sameRead = (leader, event) => leader.read !== null && event.read === leader.read;
1347
+ /** The line of a leader cancelled by what came in its own read: nothing ran, and why. A line of one
1348
+ * key (`once`): from a terminal's focus it goes as the person types on, not at a key bound in the list. */
1349
+ const mergedLine = (state) => Object.freeze({ said: LEADER_WORDS.merged(backKeyLabel(state.detachKey)), under: Object.freeze([]), once: true });
1011
1350
  /** Two effects as one: the dismissal of a confirmation the leader closed, and what it then ran. */
1012
1351
  const both = (a, b) => (a === null ? b : b === null ? a : typeof b === "string" ? b : { ...a, ...b });
1013
1352
 
@@ -1028,15 +1367,16 @@ function leading(state, event, keymap) {
1028
1367
  if (event.back !== undefined) {
1029
1368
  if (state.terminal?.focus !== "terminal") return { state, effect: null };
1030
1369
  if (l !== null && l.waiting && !sameRead(l, event)) return twice(next(state, { leader: leaderAt(event.read, false) }));
1031
- return { state: next(state, { leader: leaderAt(event.read, event.rest !== true) }), effect: null };
1370
+ return { state: next(state, { leader: leaderAt(event.read, event.rest !== true), ...(event.rest === true ? { outcome: mergedLine(state) } : {}) }), effect: null };
1032
1371
  }
1033
- // A chord typed in a terminal (term/through.mjs): the leader then its key, at once.
1372
+ // A chord typed in a terminal (term/through.mjs): the leader then its key, at once. Only one
1373
+ // that is read now (chordsRead): the scanner takes no other.
1034
1374
  if (event.chord !== undefined) {
1035
- if (state.keyboard === null || state.terminal?.focus !== "terminal") return { state, effect: null };
1375
+ if (state.terminal?.focus !== "terminal" || ![...chordsRead(state.chords, state.keyboard !== null).values()].includes(event.chord)) return { state, effect: null };
1036
1376
  return led(l === null ? state : next(state, { leader: null }), event.chord, event, keymap);
1037
1377
  }
1038
1378
  if (typeof event.key !== "string" && !("paste" in event)) return { pass: state };
1039
- if (l !== null && sameRead(l, event)) return { state: l.waiting ? next(state, { leader: leaderAt(l.read, false) }) : state, effect: null };
1379
+ if (l !== null && sameRead(l, event)) return { state: l.waiting ? next(state, { leader: leaderAt(l.read, false), outcome: mergedLine(state) }) : state, effect: null };
1040
1380
  if (l !== null && l.waiting) {
1041
1381
  const after = next(state, { leader: leaderAt(event.read, false) });
1042
1382
  if (event.key === state.leaderKey) return twice(after);
@@ -1045,18 +1385,23 @@ function leading(state, event, keymap) {
1045
1385
  }
1046
1386
  const plain = l === null ? state : next(state, { leader: null });
1047
1387
  if (event.key === state.leaderKey) return { state: next(plain, { leader: leaderAt(event.read, true) }), effect: null };
1048
- // THE CHORDS (docs/tui.md, "Chords"): while the terminal confirms the kitty keyboard protocol, a
1049
- // chord does what the leader then its key does, from whatever was open. While the leader waits it
1050
- // is a key not in the leader's table (above): it cancels the leader and is dropped.
1051
- const chord = state.keyboard === null || typeof event.key !== "string" ? undefined : state.chords.get(event.key);
1388
+ // THE PALETTE'S OWN KEYS (docs/tui.md, "The palette"): while it is open they are its, before any
1389
+ // chord (Alt+Shift+Down and Up are the chords `next` and `previous` too): inPalette reads them.
1390
+ // A chord typed in a terminal (`{ chord }`, above) never comes then: the terminal has no focus.
1391
+ if (state.palette !== null && typeof event.key === "string" && state.paletteKeys.has(event.key)) return { pass: plain };
1392
+ // THE CHORDS (docs/tui.md, "Chords"): a chord does what the leader then its key does, from
1393
+ // whatever was open: every one while the terminal confirms the kitty keyboard protocol, those with
1394
+ // bytes of their own without it too (chordsRead: the Alt family, the arrows). While the leader waits it is a key not in the
1395
+ // leader's table (above): it cancels the leader and is dropped.
1396
+ const chord = typeof event.key !== "string" ? undefined : chordsRead(state.chords, state.keyboard !== null).get(event.key);
1052
1397
  if (chord !== undefined) return led(plain, chord, event, keymap);
1053
1398
  return { pass: plain };
1054
1399
  }
1055
1400
 
1056
- /** The leader twice (`state` is after it, its hint closed). While the open terminal has the focus,
1401
+ /** The leader twice (`state` is after it, the leader cancelled). While the open terminal has the focus,
1057
1402
  * which it keeps, the leader's own key goes to the agent: the effect `{ input: <bytes> }`, the
1058
1403
  * bytes a terminal sends for the back key (term/through.mjs backKeySequences, its first form:
1059
- * ESC o for M-o, 0x1c for C-\\), which the loop types into the focused tab as it types what
1404
+ * 0x18 for C-x, the default; ESC o for M-o), which the loop types into the focused tab as it types what
1060
1405
  * passes through (lib/main.mjs), so an agent that reads that key still gets it. From any other
1061
1406
  * focus it does nothing else: the list is reached with the leader then Esc (LEADER). */
1062
1407
  function twice(state) {
@@ -1079,7 +1424,7 @@ function toList(state) {
1079
1424
  if (field) s = field.close(s, field.query(s).trim());
1080
1425
  if (s.view === "souls" || s.view === "capabilities") s = s.open === null ? toSwitcher(next(s, { back: "switcher" })) : next(s, { view: "instances", back: "instances" });
1081
1426
  if (s.terminal?.focus === "terminal") s = terminalWith(s, { focus: "list", zoom: false });
1082
- if (s.view === "instances" && s.focus === "detail") s = next(s, { focus: "list" });
1427
+ if (s.view === "instances" && (s.focus === "detail" || s.focus === "workspaces")) s = next(s, { focus: "list", workspaceCursor: null });
1083
1428
  return { state: s, effect };
1084
1429
  }
1085
1430
 
@@ -1092,12 +1437,14 @@ function toInstances(state) {
1092
1437
  return { state: next(s, { view: "instances", back: "instances", switcher: Object.freeze({ ...s.switcher, line: null, refused: null, removing: null }) }), effect: settled.effect };
1093
1438
  }
1094
1439
 
1095
- /** What the leader's key does (LEADER's `does`), from wherever the focus was. From the terminal's
1096
- * focus the acts are the open terminal's instance's and give the focus back when their
1440
+ /** What the leader's key does (LEADER's `does`), or a chord (actions.mjs chordKeys: the same
1441
+ * `does`, and the chords' own `tab-1` to `tab-9`), from wherever the focus was. From the
1442
+ * terminal's focus the acts are the open terminal's instance's and give the focus back when their
1097
1443
  * confirmation closes, zoom zooms it in or out, and the palette gives it back when it is closed;
1098
- * Up and Down go from the open terminal's instance. Every other key first gives the focus to
1099
- * the list, then does what its action does there; the terminal's keys and the acts first go to
1100
- * the Instances view, from any view (toInstances). */
1444
+ * Up and Down go from the open terminal's instance; the tabs' (Left, Right, tab-N) keep the focus
1445
+ * where it was. Every other key first gives the focus to the list, then does what its action does
1446
+ * there; the terminal's keys and the acts first go to the Instances view, from any view
1447
+ * (toInstances). */
1101
1448
  function led(state, does, event, keymap) {
1102
1449
  const stay = { state, effect: null }, t = state.terminal;
1103
1450
  const inTerminal = t?.focus === "terminal" && state.view === "instances";
@@ -1114,13 +1461,14 @@ function led(state, does, event, keymap) {
1114
1461
  return { state: step.state, effect: both(settledState.effect, step.effect) };
1115
1462
  }
1116
1463
  // The tabs, from any focus: the one shown next keeps the focus and the zoom it had.
1117
- if (does === "tab-previous" || does === "tab-next") {
1118
- // At either end, or while a tab starts, nothing at all: not even the settling, which would
1119
- // take the focus from the terminal.
1120
- if (tabStepped(state, does === "tab-previous" ? -1 : 1).effect === null) return stay;
1464
+ const pick = TAB_PICKS[does];
1465
+ if (pick !== undefined) {
1466
+ // At either end, with no such tab, on the shown one or while a tab starts, nothing at all: not
1467
+ // even the settling, which would take the focus from the terminal.
1468
+ if (tabPicked(state, pick).effect === null) return stay;
1121
1469
  const settledState = toInstances(state);
1122
1470
  if (settledState.state.view !== "instances") return stay;
1123
- const step = tabStepped(settledState.state, does === "tab-previous" ? -1 : 1, { focus: inTerminal ? "terminal" : "list", zoom: inTerminal && t.zoom });
1471
+ const step = tabPicked(settledState.state, pick, { focus: inTerminal ? "terminal" : "list", zoom: inTerminal && t.zoom });
1124
1472
  return { state: step.state, effect: both(settledState.effect, step.effect) };
1125
1473
  }
1126
1474
  // The split's keys, from any focus: nothing at all when they do not apply (splitRefused).
@@ -1134,6 +1482,7 @@ function led(state, does, event, keymap) {
1134
1482
  return { state: step.state, effect: both(settledState.effect, step.effect) };
1135
1483
  }
1136
1484
  if (does === "previous" || does === "next") return stepped(state, does === "previous" ? -1 : 1, event);
1485
+ if (does === "workspace-previous" || does === "workspace-next") return workspaceMoved(state, does === "workspace-previous" ? -1 : 1, inTerminal);
1137
1486
  // Another of the open terminal's soul, from its focus; from anywhere else, of the selection's.
1138
1487
  // The Souls view's selected soul is named before the list is shown, which leaves that view.
1139
1488
  if (does === "spawn" || does === "spawn-another") {
@@ -1151,7 +1500,8 @@ function led(state, does, event, keymap) {
1151
1500
  return { state: step.state, effect: both(settledState.effect, step.effect) };
1152
1501
  }
1153
1502
  const settledState = toList(state);
1154
- const { key, text, ...rest } = event;
1503
+ // The action alone: neither the key nor a chord that ran it is read again.
1504
+ const { key, text, chord, ...rest } = event;
1155
1505
  const step = reduced(settledState.state, { ...rest, action: does }, keymap);
1156
1506
  return { state: step.state, effect: both(settledState.effect, step.effect) };
1157
1507
  }
@@ -1197,9 +1547,12 @@ function paletteClosed(state) {
1197
1547
  /** The keys that move the palette's selection: named keys only, so every letter is the query's. */
1198
1548
  const PALETTE_MOVES = Object.freeze({ up: "up", down: "down", pageup: "page-up", pagedown: "page-down" });
1199
1549
  /** A key or a paste while the palette is open. Text and a paste's first line go to the query,
1200
- * Backspace takes a grapheme off it, Up and Down (and PageUp, PageDown) move among its rows,
1201
- * Enter runs the selected row, Esc (or its own key again) closes it; quit, suspend and redraw keep
1202
- * their actions. Every other key does nothing. */
1550
+ * Backspace takes a grapheme off it, Up and Down (and PageUp, PageDown) move among its rows that
1551
+ * can be selected (a group's label row is passed over), its own keys (`paletteKeys`, read before
1552
+ * every other key, a key a tui.json binds to an action too) to the first row of the next group or
1553
+ * of the one before, wrapping (model.mjs paletteStep), Enter runs the selected row, Esc (or its
1554
+ * own key again) closes it; quit, suspend and redraw keep their actions. Every other key does
1555
+ * nothing. */
1203
1556
  function inPalette(state, event, keymap) {
1204
1557
  const p = state.palette, stay = { state, effect: null };
1205
1558
  const put = (changes) => next(state, { palette: Object.freeze({ ...p, ...changes }) });
@@ -1208,10 +1561,12 @@ function inPalette(state, event, keymap) {
1208
1561
  if (typeof event.text === "string") return query(typed(p.query, event.text, { key: true }));
1209
1562
  const action = keymap.get(event.key);
1210
1563
  if (event.key === "backspace") return query([...GRAPHEMES.segment(p.query)].map((g) => g.segment).slice(0, -1).join(""));
1564
+ const group = state.paletteKeys.get(event.key);
1565
+ if (group !== undefined) { const to = paletteStep(paletteItems(state), group); return to === null ? stay : { state: put({ selected: to }), effect: null }; }
1211
1566
  if (event.key === "escape" || action === "close" || action === "palette") return { state: paletteClosed(state), effect: null };
1212
1567
  if (action === "quit" || action === "suspend" || action === "redraw") return { state, effect: action };
1213
1568
  const list = paletteItems(state);
1214
- const keys = list.items.map((item) => item.key);
1569
+ const keys = list.items.filter((item) => item.kind !== "group").map((item) => item.key);
1215
1570
  if (Object.hasOwn(PALETTE_MOVES, event.key)) {
1216
1571
  if (!keys.length) return stay;
1217
1572
  const page = Math.max(1, (Number.isInteger(event.page) ? event.page : 1) - 1);
@@ -1264,7 +1619,7 @@ function reduced(state, event, keymap) {
1264
1619
  if (event.keyboard !== undefined) return { state: next(state, { keyboard: event.keyboard }), effect: null };
1265
1620
  if (event.acted !== undefined) return acted(state, event.acted);
1266
1621
  if (event.prepared !== undefined) return prepared(state, event.prepared);
1267
- if (event.given !== undefined) return state.opening?.phase === "preflight" ? { state: next(state, { opening: null }), effect: null } : stay;
1622
+ if (event.given !== undefined) return state.opening?.phase === "preflight" ? preflightGiven(state) : stay;
1268
1623
  if (event.returned !== undefined) return returned(state, event.returned);
1269
1624
  if (event.shown !== undefined) return state.tabs.some((tab) => tab.id === event.shown.id) ? { state: tabWith(state, event.shown.id, { status: "open" }), effect: null } : stay;
1270
1625
  // A hidden tab's agent wrote (the panel says so once per hidden spell): its mark.
@@ -1272,6 +1627,8 @@ function reduced(state, event, keymap) {
1272
1627
  if (event.entry !== undefined) return took(state, event);
1273
1628
  if (event.roster !== undefined) return heard(state, event.roster);
1274
1629
  if (event.collecting !== undefined) return { state: event.collecting === state.collecting ? state : next(state, { collecting: event.collecting === true }), effect: null };
1630
+ // What the loop saw that the store does not: bytes typed to the agent, a resize. A line of one key goes.
1631
+ if (event.passed !== undefined) return { state: state.outcome?.once === true ? next(state, { outcome: null }) : state, effect: null };
1275
1632
  // The rows stay where they are: only what they read changes.
1276
1633
  if (event.retiring !== undefined) return { state: next(state, { retiring: new Set(event.retiring) }), effect: null };
1277
1634
  if (event.listed !== undefined) return listedHeard(state, event.listed);
@@ -1280,7 +1637,14 @@ function reduced(state, event, keymap) {
1280
1637
  // Held while nothing may draw such a line (the switcher, the help): it is said once a view that does is shown.
1281
1638
  if (event.notListed !== undefined) return { state: next(state, { notice: Object.freeze({ said: LIST_WORDS.notAdded(event.notListed?.message ?? ""), under: Object.freeze([]) }) }), effect: null };
1282
1639
  if (event.admitted !== undefined) return { state: admitted(state, event), effect: { refresh: { live: true } } };
1283
- if (event.refused !== undefined) return { state: next(state, { switcher: Object.freeze({ ...state.switcher, refused: event.refused }) }), effect: null };
1640
+ if (event.joined !== undefined) return joinedHeard(state, { ...event.joined, at: event.at });
1641
+ if (event.identity !== undefined) return identityHeard(state, event.identity);
1642
+ if (event.refused !== undefined) {
1643
+ // Picked from the list of workspaces, the switcher not shown: the line says so.
1644
+ const name = state.listed.rows.find((row) => row.path === event.refused)?.name ?? event.refused;
1645
+ const said = state.view === "switcher" ? {} : { outcome: Object.freeze({ said: WORKSPACE_WORDS.refused(name), under: Object.freeze([]) }) };
1646
+ return { state: next(state, { switcher: Object.freeze({ ...state.switcher, refused: event.refused }), ...said }), effect: null };
1647
+ }
1284
1648
  if (event.souls !== undefined) return soulsHeard(state, event.souls);
1285
1649
  if (event.capabilities !== undefined) return capabilitiesHeard(state, event.capabilities);
1286
1650
  if (event.capabilitiesTop !== undefined) return capabilitiesTop(state, event.capabilitiesTop);
@@ -1315,13 +1679,18 @@ function reduced(state, event, keymap) {
1315
1679
  return windowOf({ top: moved(action, from, count, height), count, height }).top;
1316
1680
  };
1317
1681
 
1682
+ if (state.view === "instances" && state.focus === "workspaces") {
1683
+ const step = inWorkspaces(state, action);
1684
+ if (step !== null) return step;
1685
+ state = next(state, { focus: "list", workspaceCursor: null });
1686
+ }
1318
1687
  switch (action) {
1319
1688
  case "quit": case "suspend": case "redraw": return { state, effect: action };
1320
1689
  // In the switcher this computer's deployments list is read again too.
1321
1690
  // In the Souls view it reads the souls again, and nothing else; in the Capabilities view, the capabilities.
1322
- case "refresh": return state.view === "souls" ? inSouls(state, action, event, page, scrolled) : state.view === "capabilities" ? inCapabilities(state, action, event, page, scrolled) : { state, effect: { refresh: { live: true }, ...((state.view === "help" ? state.back : state.view) === "switcher" ? { list: true } : {}) } };
1691
+ case "refresh": return state.view === "souls" ? inSouls(state, action, event, page, scrolled) : state.view === "capabilities" ? inCapabilities(state, action, event, page, scrolled) : { state, effect: { refresh: { live: true }, ...((state.view === "help" ? state.back : state.view) === "switcher" ? { list: true, identify: true } : {}) } };
1323
1692
  case "help": return { state: next(state, state.view === "help" ? { view: state.back } : { view: "help", back: state.view, helpTop: 0 }), effect: null };
1324
- case "workspaces": return state.view === "switcher" ? stay : { state: toSwitcher(next(state, { back: "switcher" })), effect: { list: true } };
1693
+ case "workspaces": return state.view === "switcher" ? stay : toWorkspaces(state);
1325
1694
  case "palette": return { state: paletteOpened(state, event, "list"), effect: null };
1326
1695
  // Souls and Capabilities are views of this machine, whatever workspace is open, or none.
1327
1696
  case "souls": return soulsShown(state);
@@ -1353,8 +1722,8 @@ function reduced(state, event, keymap) {
1353
1722
  case "filter": return { state: rehomed(roster(state), next(state, { focus: "list", filter: Object.freeze({ editing: true, query: state.filter.query }) })), effect: null };
1354
1723
  case "open": return at < 0 ? stay : openAgent(state, r.items[at].row);
1355
1724
  case "zoom": case "close-terminal": return terminalKey(state, action);
1356
- case "tab-previous": case "tab-next": return tabStepped(state, action === "tab-previous" ? -1 : 1);
1357
- case "split-right": case "split-down": case "group-next": case "tab-move": case "group-close": {
1725
+ case "tab-previous": case "tab-next": return tabPicked(state, TAB_PICKS[action]);
1726
+ case "split-right": case "split-down": case "group-next": case "group-previous": case "tab-move": case "group-close": {
1358
1727
  const refused = splitRefused(state, action, event.panel);
1359
1728
  return refused === true ? { state, effect: null } : refused !== null ? { state: next(state, { outcome: refused }), effect: null } : splitKey(state, action, false);
1360
1729
  }