@awebai/oats 0.51.0 → 0.53.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 (166) hide show
  1. package/README.md +3 -1
  2. package/bin/oats.mjs +407 -123
  3. package/docs/capabilities.md +75 -6
  4. package/docs/configuration.md +200 -91
  5. package/docs/desktop-cli-api.md +1563 -113
  6. package/docs/desktop-instance-start.md +69 -0
  7. package/docs/desktop.md +10 -7
  8. package/docs/execution-targets.md +145 -58
  9. package/docs/first-team.md +3 -1
  10. package/docs/harness-trust.md +50 -2
  11. package/docs/implementation.md +238 -17
  12. package/docs/integrations.md +13 -9
  13. package/docs/knowledge-reference/acceptance.md +1 -0
  14. package/docs/knowledge-reference/harvester.md +5 -3
  15. package/docs/knowledge-reference/model.md +5 -3
  16. package/docs/knowledge-theory.md +1 -1
  17. package/docs/knowledge.md +25 -2
  18. package/docs/launch-prompt-completion.md +50 -48
  19. package/docs/macos-test-runs.md +246 -0
  20. package/docs/oats-local.schema.json +4 -3
  21. package/docs/official-catalog.md +5 -5
  22. package/docs/packages.md +14 -14
  23. package/docs/release-lane.md +225 -11
  24. package/docs/release-notes/v0.44.0.md +1 -1
  25. package/docs/release-notes/v0.52.0.md +933 -0
  26. package/docs/release-notes/v0.53.0.md +1274 -0
  27. package/docs/schedules.md +97 -4
  28. package/docs/souls-and-instances.md +332 -40
  29. package/docs/tui.md +584 -0
  30. package/docs/upgrading.md +298 -0
  31. package/docs/workspaces.md +3 -3
  32. package/lib/automations.mjs +22 -22
  33. package/lib/claim.mjs +278 -49
  34. package/lib/config-data.mjs +4 -1
  35. package/lib/core.mjs +1282 -289
  36. package/lib/errors.mjs +170 -0
  37. package/lib/harness-trust-write.mjs +68 -27
  38. package/lib/harness-trust.mjs +34 -16
  39. package/lib/instance-events.mjs +19 -1
  40. package/lib/instance-git.mjs +39 -3
  41. package/lib/instance-inspect.mjs +45 -17
  42. package/lib/instance-lifecycle.mjs +306 -57
  43. package/lib/instance-resolution.mjs +59 -6
  44. package/lib/launch-preference.mjs +236 -9
  45. package/lib/launch-prompt-completion.mjs +7 -43
  46. package/lib/launch-prompt-evidence.mjs +14 -7
  47. package/lib/launch-prompt-fixtures/README.md +86 -152
  48. package/lib/launch-prompt-fixtures/dev-channel-confirmation.json +50 -0
  49. package/lib/launch-prompt-fixtures.mjs +122 -109
  50. package/lib/launch-prompt-transport.mjs +2 -56
  51. package/lib/launch-prompts.mjs +102 -100
  52. package/lib/local-edit.mjs +118 -0
  53. package/lib/packages.mjs +13 -4
  54. package/lib/pi-bridge.mjs +308 -0
  55. package/lib/refused-text.mjs +11 -0
  56. package/lib/remote.mjs +190 -25
  57. package/lib/resolve.mjs +2 -1
  58. package/lib/retained-after-retire.mjs +2 -2
  59. package/lib/retire-output.mjs +12 -1
  60. package/lib/schedule-host.mjs +125 -22
  61. package/lib/schedule.mjs +73 -24
  62. package/lib/shell-word.mjs +13 -0
  63. package/lib/soul-launch-verbs.mjs +153 -0
  64. package/lib/stable-node.mjs +62 -0
  65. package/lib/system-tools.mjs +39 -0
  66. package/lib/teams-verbs.mjs +18 -46
  67. package/lib/tmux-server-scope.mjs +62 -0
  68. package/lib/triggers.mjs +4 -4
  69. package/lib/workspace.mjs +54 -30
  70. package/lib/worktree-hooks.mjs +98 -20
  71. package/lib/worktree.mjs +83 -24
  72. package/package-catalog.json +4 -4
  73. package/package.json +8 -2
  74. package/packages/client/attach-exit.mjs +25 -0
  75. package/packages/client/capability-show-contract.mjs +230 -0
  76. package/packages/client/capability-warnings-contract.mjs +57 -0
  77. package/packages/client/cli-adapter.mjs +561 -0
  78. package/packages/client/cli-environment.mjs +53 -0
  79. package/packages/client/cli-locator.mjs +253 -0
  80. package/packages/client/cli-probe-contract.mjs +47 -0
  81. package/packages/client/client-terms.mjs +60 -0
  82. package/packages/client/deployment-contract.mjs +72 -0
  83. package/packages/client/deployment-data.mjs +483 -0
  84. package/packages/client/deployment-facts.mjs +95 -0
  85. package/packages/client/deployment-label.mjs +76 -0
  86. package/packages/client/deployment-observer.mjs +89 -0
  87. package/packages/client/deployment-panel.mjs +180 -0
  88. package/packages/client/deployment-read-cli.mjs +57 -0
  89. package/packages/client/display-text.mjs +50 -0
  90. package/packages/client/harness-names.mjs +30 -0
  91. package/packages/client/inspect-contract.mjs +62 -0
  92. package/packages/client/instance-acts.mjs +90 -0
  93. package/packages/client/instance-admission.mjs +45 -0
  94. package/packages/client/instance-events-contract.mjs +76 -0
  95. package/packages/client/instance-git-contract.mjs +80 -0
  96. package/packages/client/instance-presentation.mjs +86 -0
  97. package/packages/client/instance-tree.mjs +462 -0
  98. package/packages/client/kernel-environment.mjs +11 -0
  99. package/packages/client/kernel-lines.mjs +31 -0
  100. package/packages/client/kernel-made.mjs +23 -0
  101. package/packages/client/launch-contract.mjs +51 -0
  102. package/packages/client/launch-prompt-outcome.mjs +200 -0
  103. package/packages/client/lifecycle-cli.mjs +61 -0
  104. package/packages/client/lifecycle-contract.mjs +413 -0
  105. package/packages/client/liveness-main.mjs +10 -0
  106. package/packages/client/liveness.mjs +48 -0
  107. package/packages/client/machine-contract.mjs +123 -0
  108. package/packages/client/own-codes.mjs +31 -0
  109. package/packages/client/own-environment.mjs +22 -0
  110. package/packages/client/panel-views.mjs +293 -0
  111. package/packages/client/readiness-contract.mjs +174 -0
  112. package/packages/client/refresh-loop.mjs +52 -0
  113. package/packages/client/remote-address.mjs +151 -0
  114. package/packages/client/remote-identity-memory.mjs +94 -0
  115. package/packages/client/remote-roster.mjs +153 -0
  116. package/packages/client/roster-sections.mjs +80 -0
  117. package/packages/client/session-attach.mjs +175 -0
  118. package/packages/client/spawn-apply-cli.mjs +104 -0
  119. package/packages/client/spawn-apply-contract.mjs +195 -0
  120. package/packages/client/spawn-decision.mjs +46 -0
  121. package/packages/client/spawn-messages.mjs +142 -0
  122. package/packages/client/spawn-preview-cli.mjs +38 -0
  123. package/packages/client/spawn-preview-contract.mjs +318 -0
  124. package/packages/client/team-members.mjs +41 -0
  125. package/packages/client/team-rows.mjs +45 -0
  126. package/packages/client/terminal-contract.mjs +57 -0
  127. package/packages/client/terminal-exec.mjs +21 -0
  128. package/packages/client/tmux-status.mjs +66 -0
  129. package/packages/client/waiting-on-you.mjs +88 -0
  130. package/packages/client/workspace-admission.mjs +45 -0
  131. package/packages/client/workspace-choices.mjs +95 -0
  132. package/packages/client/workspace-cli.mjs +142 -0
  133. package/packages/client/workspace-views.mjs +183 -0
  134. package/packages/record/README.md +21 -0
  135. package/packages/record/bin/setup.mjs +108 -15
  136. package/packages/tui/README.md +64 -0
  137. package/packages/tui/bin/tui.mjs +7 -0
  138. package/packages/tui/lib/actions.mjs +44 -0
  139. package/packages/tui/lib/client.mjs +35 -0
  140. package/packages/tui/lib/config.mjs +151 -0
  141. package/packages/tui/lib/main.mjs +176 -0
  142. package/packages/tui/lib/model.mjs +392 -0
  143. package/packages/tui/lib/read.mjs +473 -0
  144. package/packages/tui/lib/store.mjs +364 -0
  145. package/packages/tui/lib/term/keys.mjs +367 -0
  146. package/packages/tui/lib/term/screen.mjs +134 -0
  147. package/packages/tui/lib/term/style.mjs +43 -0
  148. package/packages/tui/lib/term/text.mjs +109 -0
  149. package/packages/tui/lib/term/tty.mjs +178 -0
  150. package/packages/tui/lib/term/width.mjs +124 -0
  151. package/packages/tui/lib/views/detail.mjs +46 -0
  152. package/packages/tui/lib/views/frame.mjs +256 -0
  153. package/packages/tui/lib/views/help.mjs +140 -0
  154. package/packages/tui/lib/views/instances.mjs +137 -0
  155. package/packages/tui/lib/views/scroll.mjs +19 -0
  156. package/packages/tui/lib/views/switcher.mjs +97 -0
  157. package/packages/tui/package.json +8 -0
  158. package/skills/oats-getting-started/SKILL.md +30 -23
  159. package/lib/launch-prompt-fixtures/claude-2.1.289-darwin-arm64/frame-05-channel-seeded.txt +0 -35
  160. package/lib/launch-prompt-fixtures/claude-2.1.289-darwin-arm64/frame-06-first-after-channel.txt +0 -35
  161. package/lib/launch-prompt-fixtures/claude-2.1.289-darwin-arm64/frame-07-after-channel-200ms.txt +0 -35
  162. package/lib/launch-prompt-fixtures/claude-2.1.289-darwin-arm64/frame-08-installed-after-channel-200ms.txt +0 -35
  163. package/lib/launch-prompt-fixtures/claude-2.1.289-darwin-arm64/manifest.json +0 -77
  164. package/lib/launch-prompt-fixtures/claude-2.1.289-darwin-arm64/max-bypass-source.json +0 -88
  165. package/lib/launch-prompt-fixtures/structural-completion.json +0 -205
  166. package/lib/launch-prompt-policy.mjs +0 -87
@@ -0,0 +1,364 @@
1
+ // The state of `oats tui` and the one function that changes it: reduce(state, event). Pure: no
2
+ // IO, no clock, no terminal size. What only the loop can do comes back as an effect, as data.
3
+ //
4
+ // The state holds what the user chose (which workspace is open, which instance, what is
5
+ // collapsed, the filters, where the panes are scrolled) and what the reader gave, exactly as the
6
+ // shared rules gave it: each served deployment's entry (deployment-panel.mjs), the roster of other
7
+ // machines (remote-roster.mjs `remoteRosterState`) and the memory of what they reported
8
+ // (remote-identity-memory.mjs), all through lib/read.mjs. Nothing is added to any of them, nothing
9
+ // removed, and nothing here decides what a failed read means or which view a deployment is in.
10
+ // Selection is an identity (the shared `instanceId`), never an index, so it survives every read;
11
+ // the rows it moves among are the model's, in drawn order.
12
+ import { ACTIONS, bindings } from "./actions.mjs";
13
+ import { collapseKey, displayLine, instanceId, pendingEntry } from "./client.mjs";
14
+ import { disclosure, roster, switcher } from "./model.mjs";
15
+ import { windowOf } from "./views/scroll.mjs";
16
+
17
+ /** The longest filter, in UTF-16 units. */
18
+ export const QUERY_LIMIT = 200;
19
+ const GRAPHEMES = new Intl.Segmenter(undefined, { granularity: "grapheme" });
20
+ const LINE_END = /\r\n|[\n\r\u2028\u2029]/;
21
+ const NO_FILTER = Object.freeze({ editing: false, query: "" });
22
+
23
+ /** The state before the first answer. `deployment` is the one `oats tui` was started in or was
24
+ * told of, with its shared pending entry (lib/main.mjs makes it); null for a run started outside
25
+ * every deployment, which serves none and shows the switcher.
26
+ * `ctxs`: the served deployments, in the order they were opened. `open`: the id of the deployment
27
+ * the Instances view was opened on (a path, or `remote:<group id>`), or null. `entries`,
28
+ * `numbers`: each served deployment's entry and the number of the cycle that gave it. `remote`,
29
+ * `rosterAt`, `memory`, `notes`, `collecting`: the roster of other machines as the shared rule
30
+ * left it, the time of the last outcome taken, what the machines reported, and whether a roster
31
+ * read is in flight. `unread`: per served deployment whose read failed with nothing kept, the
32
+ * instances it last held (see `took`). `loose`: the list was scrolled away from the selection
33
+ * on purpose (see `scrolledList`); the switcher has its own.
34
+ * `selected`, `top`, `detailTop` are the open view's; `places` keeps them for
35
+ * the views left behind, and `collapsed` holds every view's keys (each carries its `open`).
36
+ * `back`: the view under the help. `offered`: the deployments of `~/Agents` not served. */
37
+ export function initialState({ deployment = null, cli, home, entry = null, actions = ACTIONS, ascii = false }) {
38
+ return Object.freeze({
39
+ view: deployment === null ? "switcher" : "instances", back: deployment === null ? "switcher" : "instances",
40
+ ctxs: Object.freeze(deployment === null ? [] : [deployment]), open: deployment,
41
+ entries: new Map(deployment === null ? [] : [[deployment, entry]]), numbers: new Map(),
42
+ remote: Object.freeze({ remoteGroups: Object.freeze([]), rosterAnswered: false, rosterFailure: null }), rosterAt: null,
43
+ memory: new Map(), notes: new Map(), collecting: false, cli, home,
44
+ selected: null, collapsed: new Set(), filter: NO_FILTER, focus: "list", top: 0, loose: false, detailTop: 0, places: new Map(), unread: new Map(),
45
+ switcher: Object.freeze({ editing: false, query: "", selected: null, top: 0, loose: false, line: null, refused: null }),
46
+ offered: Object.freeze({ offered: Object.freeze([]), limited: false }),
47
+ helpTop: 0, actions, ascii,
48
+ });
49
+ }
50
+
51
+ const next = (state, changes) => Object.freeze({ ...state, ...changes });
52
+ const indexOf = (r, id) => (id === null ? -1 : r.index.get(id) ?? -1);
53
+ /** Whether an item of the list is an instance: the only kind the selection stands on. */
54
+ const isRow = (item) => item?.id !== undefined;
55
+
56
+ /** The list's `top` as the screen shows it for this state in a pane `page` rows tall. The
57
+ * window holds the selected instance, unless it was scrolled away from it on purpose (`loose`). */
58
+ const settledTop = (state, r, page) => windowOf({ top: state.top, count: r.items.length, height: page, keep: state.loose ? -1 : indexOf(r, state.selected) }).top;
59
+
60
+ /** Select the instance at `index` of the drawn order (an item that is not an instance is passed
61
+ * over in the direction of travel), and scroll the least that shows it. The first instance
62
+ * brings what stands above it into the pane (its heading), and the last what stands under it
63
+ * (a deployment without rows, the roster's failure), as far as the pane holds them beside it.
64
+ * A new selection starts its detail at the top. */
65
+ function selectAt(state, r, index, page, step = 1) {
66
+ const count = r.items.length;
67
+ if (!r.rows.length) return state;
68
+ let at = Math.min(Math.max(0, index), count - 1);
69
+ while (at >= 0 && at < count && !isRow(r.items[at])) at += step;
70
+ if (at < 0 || at >= count) { at = Math.min(Math.max(0, index), count - 1); while (!isRow(r.items[at])) at -= step; }
71
+ const id = r.items[at].id;
72
+ const from = id === r.rows[0].id ? 0 : id === r.rows.at(-1).id ? count : settledTop(state, r, page);
73
+ const top = windowOf({ top: from, count, height: page, keep: at }).top;
74
+ return next(state, { selected: id, top, loose: false, detailTop: id === state.selected ? state.detailTop : 0 });
75
+ }
76
+
77
+ /** A movement key with no instance to go to in its direction (the selection is on the first or
78
+ * the last one, or the list holds none): the list scrolls instead, by what the key moves, so
79
+ * whatever stands beyond the instances can always be read, however much of it there is. The
80
+ * selection stays where it is and may leave the pane (`loose`) until it moves again. */
81
+ function scrolledList(state, r, action, page) {
82
+ const count = r.items.length, from = settledTop(state, r, page);
83
+ const top = windowOf({ top: moved(action, from, count, page), count, height: page }).top;
84
+ return top === from ? state : next(state, { top, loose: true });
85
+ }
86
+
87
+ /** After the rows changed (a read, the filter, a collapse): the selection stays on its instance
88
+ * while that is still drawn; else it moves to the row now at the same place in the drawn order,
89
+ * else to the last row, else there is none. With none selected, the first row is.
90
+ * A filter that matches nothing leaves no row to move to: the selection then stays on its
91
+ * instance while the view still has it, and is found again when the filter is cleared. */
92
+ function rehomed(before, state) {
93
+ const now = roster(state);
94
+ if (state.selected !== null && now.index.has(state.selected)) return state;
95
+ if (state.selected !== null && now.rows.length === 0 && now.instances.some((row) => instanceId(row) === state.selected)) return state;
96
+ const place = state.selected === null ? 0 : Math.max(0, before.rows.findIndex((item) => item.id === state.selected));
97
+ const item = now.rows[Math.min(place, now.rows.length - 1)] ?? null;
98
+ return next(state, { selected: item ? item.id : null, loose: false, detailTop: 0 });
99
+ }
100
+
101
+ /** The switcher, shown: over whatever view was there. `said`: a line of its own to show. */
102
+ const toSwitcher = (state, said = null) => next(state, { view: "switcher", switcher: Object.freeze({ ...state.switcher, editing: false, loose: false, line: said, refused: null }) });
103
+
104
+ /** After what the reader gave changed: when no view holds what was open any more (a machine that
105
+ * is no longer listed), the switcher is shown and says so, and nothing is open. Else the
106
+ * collapse keys of this view whose instance is gone are forgotten, and the selection finds its
107
+ * place.
108
+ * THAT AN INSTANCE IS GONE IS KNOWN ONLY OF A DEPLOYMENT THAT WAS READ. One whose read failed
109
+ * with nothing kept draws no row (and, reporting no workspace, is in no view but its own): its
110
+ * instances are not gone, they are not known. `unread` holds them (see `took`): the collapse of
111
+ * such an instance is not forgotten and a selection on one is not moved, whoever answered
112
+ * meanwhile (another deployment, the roster of other machines), so the next good read finds
113
+ * the user where they were. A machine's rows are kept by the shared rule while it does not
114
+ * answer, so they need no such care. */
115
+ function settled(state, taken) {
116
+ const now = roster(taken);
117
+ if (taken.open !== null && now.gone) {
118
+ const places = new Map(taken.places); places.delete(taken.open);
119
+ return { state: toSwitcher(next(taken, { open: null, places, selected: null, top: 0, loose: false, detailTop: 0, focus: "list", filter: NO_FILTER, back: "switcher" }), "gone"), effect: { offer: true } };
120
+ }
121
+ const before = roster(state);
122
+ const unknown = new Set([...taken.unread.values()].flatMap((ids) => [...ids]));
123
+ const mine = `${collapseKey(taken.open, "")}`;
124
+ const present = new Set(now.instances.map((row) => collapseKey(taken.open, instanceId(row))));
125
+ const stillThere = [...taken.collapsed].filter((key) => !key.startsWith(mine) || present.has(key) || unknown.has(key.slice(mine.length)));
126
+ const pruned = stillThere.length === taken.collapsed.size ? taken : next(taken, { collapsed: new Set(stillThere) });
127
+ return { state: unknown.has(pruned.selected) ? pruned : rehomed(before, pruned), effect: null };
128
+ }
129
+
130
+ /** A served deployment's entry from the reader, with the number of the cycle that gave it. An
131
+ * answer for a deployment that is not served, or one that is not newer than the last taken for
132
+ * it, is dropped. Only an observed entry moves the selection or forgets a collapse: a failed
133
+ * read has no rows to move to, and the next good one finds the user where they were.
134
+ * `unread`: for a deployment whose read failed with nothing kept, the instances it held when it
135
+ * was last read, until it is read again. They are what `settled` must not take for gone. */
136
+ function took(state, { id, entry, number }) {
137
+ if (!state.ctxs.includes(id) || !(number > (state.numbers.get(id) ?? 0))) return { state, effect: null };
138
+ const observed = entry?.deployment?.status === "observed", held = state.entries.get(id);
139
+ let unread = state.unread;
140
+ if (observed && unread.has(id)) { unread = new Map(unread); unread.delete(id); }
141
+ else if (!observed && held?.deployment?.status === "observed") unread = new Map(unread).set(id, new Set((Array.isArray(held.instances) ? held.instances : []).map(instanceId)));
142
+ const taken = next(state, { entries: new Map(state.entries).set(id, entry), numbers: new Map(state.numbers).set(id, number), unread });
143
+ return observed ? settled(state, taken) : { state: taken, effect: null };
144
+ }
145
+
146
+ /** The roster of other machines after one outcome, as the shared rule made it (lib/read.mjs),
147
+ * with the memory of what the machines reported and the time of the outcome. */
148
+ function heard(state, { remote, memory, notes, at }) {
149
+ return settled(state, next(state, { remote, memory, notes, rosterAt: at, collecting: false }));
150
+ }
151
+
152
+ /** Open the view of deployment `id`: the place the view left behind is kept, and found again
153
+ * on the way back (the row, where the list and the detail were scrolled). A filter is not kept. */
154
+ function opened(state, id) {
155
+ const shown = next(state, { view: "instances", back: "instances", switcher: Object.freeze({ ...state.switcher, editing: false, query: "", line: null, refused: null }) });
156
+ if (id === state.open) return shown;
157
+ const places = new Map(state.places);
158
+ if (state.open !== null) places.set(state.open, Object.freeze({ selected: state.selected, top: state.top, detailTop: state.detailTop }));
159
+ const place = places.get(id) ?? { selected: null, top: 0, detailTop: 0 };
160
+ return rehomed({ rows: [] }, next(shown, { open: id, places, focus: "list", filter: NO_FILTER, loose: false, ...place }));
161
+ }
162
+
163
+ /** A picked folder of `~/Agents` that is a deployment (lib/read.mjs made it canonical and asked
164
+ * the shared rule): it is served from now on, pending until its first read, and opened. */
165
+ function admitted(state, { admitted: id, at }) {
166
+ if (state.ctxs.includes(id)) return opened(state, id);
167
+ const joined = next(state, { ctxs: Object.freeze([...state.ctxs, id]), entries: new Map(state.entries).set(id, pendingEntry(null, at)) });
168
+ return opened(joined, id);
169
+ }
170
+
171
+ /** The filter's text changed. */
172
+ function queried(state, query) {
173
+ if (query === state.filter.query) return state;
174
+ const before = roster(state);
175
+ return rehomed(before, next(state, { filter: Object.freeze({ ...state.filter, query }), top: 0, loose: false }));
176
+ }
177
+
178
+ /** Text for a filter field: the query with it added when the filter stays within its limit and
179
+ * the display filter shows the text as it is; else the query as it was. A paste is held to that
180
+ * as it stands. The space KEY is the one text the display filter would change that is still
181
+ * taken: between words, so never first and never after another space. */
182
+ function typed(query, text, { key = false } = {}) {
183
+ const fits = text !== "" && query.length + text.length <= QUERY_LIMIT;
184
+ const shows = key && text === " " ? query !== "" && !query.endsWith(" ") : displayLine(text) === text;
185
+ return fits && shows ? query + text : query;
186
+ }
187
+
188
+ /** The two filter fields, each as what the keys of an open field need: its text, and the state
189
+ * with another text or with the field closed on one. */
190
+ const FIELDS = Object.freeze({
191
+ instances: Object.freeze({
192
+ query: (state) => state.filter.query,
193
+ put: (state, query) => queried(state, query),
194
+ close: (state, query) => next(queried(state, query), { filter: Object.freeze({ editing: false, query }) }),
195
+ }),
196
+ switcher: Object.freeze({
197
+ query: (state) => state.switcher.query,
198
+ put: (state, query) => (query === state.switcher.query ? state : next(state, { switcher: Object.freeze({ ...state.switcher, query, top: 0, loose: false }) })),
199
+ close: (state, query) => next(state, { switcher: Object.freeze({ ...state.switcher, query, editing: false, top: 0, loose: false }) }),
200
+ }),
201
+ });
202
+ /** The filter field that is open now, or null. */
203
+ const fieldOf = (state) => (state.view === "instances" && state.filter.editing ? FIELDS.instances : state.view === "switcher" && state.switcher.editing ? FIELDS.switcher : null);
204
+
205
+ /** A key while a filter field is open. A key that is text is text, whatever the action table
206
+ * says, so `q` and `j` can be typed; Backspace removes one grapheme, Enter keeps the filter and
207
+ * the close key clears it. The three keys that are not about the view keep their actions. */
208
+ function edited(state, field, event, action) {
209
+ const query = field.query(state);
210
+ if (typeof event.text === "string") return { state: field.put(state, typed(query, event.text, { key: true })), effect: null };
211
+ if (event.key === "backspace") {
212
+ const graphemes = [...GRAPHEMES.segment(query)].map((g) => g.segment);
213
+ return { state: field.put(state, graphemes.slice(0, -1).join("")), effect: null };
214
+ }
215
+ if (event.key === "enter") return { state: field.close(state, query.trim()), effect: null };
216
+ if (action === "close") return { state: field.close(state, ""), effect: null };
217
+ if (action === "quit" || action === "suspend" || action === "redraw") return { state, effect: action };
218
+ return { state, effect: null };
219
+ }
220
+
221
+ const MOVES = Object.freeze(["up", "down", "top", "bottom", "page-up", "page-down"]);
222
+ /** Where a movement key takes a position `from` among `count` rows, a page being `page` rows. */
223
+ function moved(action, from, count, page) {
224
+ const step = Math.max(1, page - 1);
225
+ switch (action) {
226
+ case "up": return from - 1;
227
+ case "down": return from + 1;
228
+ case "top": return 0;
229
+ case "bottom": return count - 1;
230
+ case "page-up": return from - step;
231
+ default: return from + step;
232
+ }
233
+ }
234
+
235
+ /** A key in the switcher. The selection moves among the choices (a heading is passed over);
236
+ * the open key opens the view of a workspace here, and asks the loop to admit a folder. */
237
+ function switched(state, action, page) {
238
+ const stay = { state, effect: null };
239
+ const s = switcher(state), sw = state.switcher;
240
+ const put = (changes) => next(state, { switcher: Object.freeze({ ...sw, ...changes }) });
241
+ switch (action) {
242
+ case "filter": return { state: put({ editing: true }), effect: null };
243
+ case "close":
244
+ if (sw.query !== "") return { state: put({ query: "", top: 0, loose: false }), effect: null };
245
+ // Back to the view it was opened over; with nothing open there is none.
246
+ return state.open === null ? stay : { state: next(put({ line: null, refused: null }), { view: "instances", back: "instances" }), effect: null };
247
+ case "instances": return state.open === null ? stay : { state: next(put({ line: null, refused: null }), { view: "instances", back: "instances" }), effect: null };
248
+ case "up": case "down": case "top": case "bottom": case "page-up": case "page-down": {
249
+ // The rows of the pane the choices have: the line that says what happened takes one (lib/model.mjs).
250
+ const count = s.items.length, height = Math.max(1, page - (sw.line ? 1 : 0));
251
+ const drawn = windowOf({ top: sw.top, count, height, keep: sw.loose ? -1 : s.items.findIndex((item) => item.key !== undefined && item.key === s.selected) }).top;
252
+ const from = Math.max(0, s.keys.indexOf(s.selected));
253
+ const key = s.keys.length ? s.keys[Math.min(Math.max(0, moved(action, from, s.keys.length, page)), s.keys.length - 1)] : null;
254
+ // No choice to go to in that direction (the first, the last, or none at all): the rows
255
+ // scroll instead, so a line under the last choice (that the folder holds more) is read.
256
+ if (key === null || key === s.selected) {
257
+ const top = windowOf({ top: moved(action, drawn, count, height), count, height }).top;
258
+ return top === drawn ? stay : { state: put({ top, loose: true }), effect: null };
259
+ }
260
+ const at = s.items.findIndex((item) => item.key === key);
261
+ // The headings above the first choice come into the pane with it, and what stands under the last one.
262
+ const top = windowOf({ top: key === s.keys[0] ? 0 : key === s.keys.at(-1) ? count : drawn, count, height, keep: at }).top;
263
+ return { state: put({ selected: key, top, loose: false }), effect: null };
264
+ }
265
+ case "open-workspace": {
266
+ const item = s.items.find((each) => each.key !== undefined && each.key === s.selected);
267
+ if (!item) return stay;
268
+ if (item.path !== undefined) return { state: put({ refused: null }), effect: { admit: item.path } };
269
+ return item.primary === null ? stay : { state: opened(state, item.primary), effect: { refresh: { live: true } } };
270
+ }
271
+ default: return stay;
272
+ }
273
+ }
274
+
275
+ /** (state, event) -> { state, effect }.
276
+ * An event is a key, `{ key, text?, page, extent, room, cols }` (`key` a key id, `text` the
277
+ * grapheme a text key typed; `page` the rows of the list pane and `cols` its width, and `extent`
278
+ * and `room` the rows the other scrolling pane holds and shows, as the last frame drew them: the
279
+ * store keeps no size); a paste, `{ paste: <text> }`; or what the loop read:
280
+ * `{ id, entry, number }` (a served deployment's entry), `{ roster: { remote, memory, notes, at } }`
281
+ * (one outcome of the roster read), `{ collecting }`, `{ offered: { offered, limited } }` (the
282
+ * deployments of `~/Agents` not served), `{ admitted: <deployment>, at }` and `{ refused: <path> }`
283
+ * (what a picked folder turned out to be).
284
+ * An effect is something only the loop can do: "quit", "suspend", "redraw", or an object with
285
+ * any of `refresh: { live }` (read now), `offer: true` (list `~/Agents` again) and
286
+ * `admit: <path>` (ask whether a picked folder is a deployment); or null. */
287
+ export function reduce(state, event, keymap = bindings(state.actions)) {
288
+ const stay = { state, effect: null };
289
+ if (event.entry !== undefined) return took(state, event);
290
+ if (event.roster !== undefined) return heard(state, event.roster);
291
+ if (event.collecting !== undefined) return { state: event.collecting === state.collecting ? state : next(state, { collecting: event.collecting === true }), effect: null };
292
+ if (event.offered !== undefined) return { state: next(state, { offered: Object.freeze({ offered: Object.freeze([...event.offered.offered]), limited: event.offered.limited === true }) }), effect: null };
293
+ if (event.admitted !== undefined) return { state: admitted(state, event), effect: { refresh: { live: true } } };
294
+ if (event.refused !== undefined) return { state: next(state, { switcher: Object.freeze({ ...state.switcher, refused: event.refused }) }), effect: null };
295
+ const field = fieldOf(state);
296
+ // A paste goes only into an open filter field: its first line. Anywhere else it is dropped whole.
297
+ if ("paste" in event) return field && typeof event.paste === "string" ? { state: field.put(state, typed(field.query(state), event.paste.split(LINE_END)[0])), effect: null } : stay;
298
+ if (typeof event.key !== "string") return stay;
299
+ const action = keymap.get(event.key);
300
+ if (field) return edited(state, field, event, action);
301
+ const page = Math.max(1, Number.isInteger(event.page) ? event.page : 1);
302
+ // A pane that scrolls on its own (the help, the detail) moves from where it is DRAWN: the top
303
+ // the state holds may lie past the end since the terminal grew, and the view shows it corrected.
304
+ const scrolled = (top) => {
305
+ const count = event.extent ?? 0, height = event.room ?? 1;
306
+ const from = windowOf({ top, count, height }).top;
307
+ return windowOf({ top: moved(action, from, count, height), count, height }).top;
308
+ };
309
+
310
+ switch (action) {
311
+ case "quit": case "suspend": case "redraw": return { state, effect: action };
312
+ // In the switcher the folders of ~/Agents are listed again too.
313
+ case "refresh": return { state, effect: { refresh: { live: true }, ...((state.view === "help" ? state.back : state.view) === "switcher" ? { offer: true } : {}) } };
314
+ case "help": return { state: next(state, state.view === "help" ? { view: state.back } : { view: "help", back: state.view, helpTop: 0 }), effect: null };
315
+ case "workspaces": return state.view === "switcher" ? stay : { state: toSwitcher(next(state, { back: "switcher" })), effect: { offer: true } };
316
+ default:
317
+ }
318
+ if (state.view === "help") {
319
+ if (action === "close") return { state: next(state, { view: state.back }), effect: null };
320
+ if (action === "instances") return state.open === null ? stay : { state: next(state, { view: "instances", back: "instances" }), effect: null };
321
+ return MOVES.includes(action) ? { state: next(state, { helpTop: scrolled(state.helpTop) }), effect: null } : stay;
322
+ }
323
+ if (state.view === "switcher") return switched(state, action, page);
324
+
325
+ if (action === "close") {
326
+ // In this order: a kept filter, the detail.
327
+ if (state.filter.query !== "") return { state: queried(state, ""), effect: null };
328
+ return state.focus === "detail" ? { state: next(state, { focus: "list" }), effect: null } : stay;
329
+ }
330
+ const r = roster(state, undefined, Number.isInteger(event.cols) ? event.cols : undefined);
331
+ const at = indexOf(r, state.selected);
332
+ switch (action) {
333
+ case "filter": return { state: next(state, { focus: "list", filter: Object.freeze({ ...state.filter, editing: true }) }), effect: null };
334
+ case "detail": return at < 0 ? stay : { state: next(state, { focus: state.focus === "detail" ? "list" : "detail" }), effect: null };
335
+ case "up": case "down": case "top": case "bottom": case "page-up": case "page-down": {
336
+ if (state.focus === "detail") return { state: next(state, { detailTop: scrolled(state.detailTop) }), effect: null };
337
+ // With nothing selected, any movement selects the first row.
338
+ const target = at < 0 ? 0 : moved(action, at, r.items.length, page);
339
+ const picked = selectAt(state, r, target, page, target < at ? -1 : 1);
340
+ return { state: picked.selected === state.selected ? scrolledList(state, r, action, page) : picked, effect: null };
341
+ }
342
+ case "collapse": case "expand": {
343
+ if (state.focus !== "list" || at < 0) return stay;
344
+ const item = r.items[at], fold = disclosure(state, r, item), key = collapseKey(state.open, item.id);
345
+ const settledState = next(state, { top: settledTop(state, r, page) });
346
+ if (!r.filtering && fold === (action === "collapse" ? "open" : "closed")) {
347
+ const collapsed = new Set(state.collapsed);
348
+ if (action === "collapse") collapsed.add(key); else collapsed.delete(key);
349
+ return { state: rehomed(r, next(settledState, { collapsed, loose: false })), effect: null };
350
+ }
351
+ // Nothing to fold or unfold: collapse goes to the parent, the nearest row above that is one
352
+ // level less deep; expand to the first child, the next row when it is one level deeper.
353
+ // Both are read from the drawn order, which is the shared grouping's, and neither leaves
354
+ // the run of rows the instance stands in (a gap or a heading ends it).
355
+ if (action === "collapse") {
356
+ for (let i = at - 1; i >= 0 && isRow(r.items[i]); i--) if (r.items[i].depth === item.depth - 1) return { state: selectAt(state, r, i, page), effect: null };
357
+ return stay;
358
+ }
359
+ const below = r.items[at + 1];
360
+ return isRow(below) && below.depth === item.depth + 1 ? { state: selectAt(state, r, at + 1, page), effect: null } : stay;
361
+ }
362
+ default: return stay;
363
+ }
364
+ }