@johngaynor/ocnus 2.0.1-next.9 → 2.0.1-next.98

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 (53) hide show
  1. package/README.md +659 -106
  2. package/cli/lib/agents.sh +85 -34
  3. package/cli/lib/asks.sh +837 -0
  4. package/cli/lib/banner.sh +249 -0
  5. package/cli/lib/brief.sh +375 -0
  6. package/cli/lib/chain.sh +501 -0
  7. package/cli/lib/close.sh +5 -3
  8. package/cli/lib/commands.sh +42 -3
  9. package/cli/lib/cost.sh +505 -0
  10. package/cli/lib/events.sh +485 -0
  11. package/cli/lib/finish.sh +30 -4
  12. package/cli/lib/github.sh +106 -26
  13. package/cli/lib/guard.sh +41 -10
  14. package/cli/lib/handoff.sh +279 -37
  15. package/cli/lib/host.sh +470 -475
  16. package/cli/lib/linear.sh +428 -34
  17. package/cli/lib/links.sh +10 -7
  18. package/cli/lib/merges.sh +129 -30
  19. package/cli/lib/progress.sh +301 -0
  20. package/cli/lib/sessions.sh +132 -0
  21. package/cli/lib/settings.sh +35 -19
  22. package/cli/lib/task.sh +170 -54
  23. package/cli/lib/tests.sh +302 -7
  24. package/cli/lib/usage.sh +204 -0
  25. package/cli/ocnus +323 -73
  26. package/herdr/README.md +1314 -268
  27. package/herdr/layouts/default.json +40 -11
  28. package/herdr/layouts/showcase.json +324 -0
  29. package/herdr/layouts/vibe.json +169 -0
  30. package/herdr/layouts/vibe.svg +41 -0
  31. package/herdr/mods/agent-settings/main.js +19 -7
  32. package/herdr/mods/card-list/main.js +658 -0
  33. package/herdr/mods/card-list/mod.json +1 -0
  34. package/herdr/mods/memory/main.js +128 -0
  35. package/herdr/mods/memory/mod.json +1 -0
  36. package/herdr/mods/memory/reading.js +88 -0
  37. package/herdr/mods/names/main.js +22 -4
  38. package/herdr/mods/needs-you/main.js +526 -0
  39. package/herdr/mods/needs-you/mod.json +1 -0
  40. package/herdr/mods/step-counts/main.js +281 -0
  41. package/herdr/mods/step-counts/mod.json +1 -0
  42. package/herdr/mods/test-runs/fit.js +78 -0
  43. package/herdr/mods/test-runs/main.js +242 -0
  44. package/herdr/mods/test-runs/mod.json +1 -0
  45. package/herdr/mods/usage/main.js +149 -0
  46. package/herdr/mods/usage/mod.json +1 -0
  47. package/package.json +1 -1
  48. package/prompts/task-new.md +1 -1
  49. package/skills/brief/SKILL.md +8 -0
  50. package/skills/sdlc-breakdown/SKILL.md +5 -5
  51. package/skills/sdlc-build/SKILL.md +7 -3
  52. package/skills/sdlc-pr/SKILL.md +2 -2
  53. package/skills/sdlc-review/SKILL.md +5 -5
@@ -0,0 +1,281 @@
1
+ // The Step counts section: how many cards are at each step, in the ocnus
2
+ // space that is showing, and how each card's agent is doing. It sits above
3
+ // Usage in the default layout's sidebar, and is about as small.
4
+ //
5
+ // All of that is worked out by `ocnus task log <space> --json`, whose
6
+ // `counts` are the steps in order, each with how many cards are on it, a mark
7
+ // for each of those cards and the text to show at its right. This mod draws
8
+ // those counts and holds no rules about hand-offs, steps or agents: where a
9
+ // card stands, what its mark is and what a step's text says all arrive from
10
+ // the command. What is its own is the drawing: a dot of its colour for each
11
+ // mark, as many dots as fit in the row, and "+N" for the rest.
12
+ import { View, div } from "gpui-kit";
13
+ import { v_flex, h_flex } from "gpui-base";
14
+ import { exec, spaces, panes, terminals, theme, layout, window, checking, appHome, settings } from "herdr";
15
+
16
+ // How often to see whether another space is showing, or Herdr's tabs have
17
+ // changed. A change in them is read at once, so an agent's state shows within
18
+ // this and a read.
19
+ const LOOK_MS = 2000;
20
+ const REREAD_MS = 6000; // how often to read the counts again
21
+ const SPACES_MS = 60000; // spaces change rarely
22
+ const TIMEOUT_MS = 10000;
23
+ // How this mod's own link into the repo that ships it ends.
24
+ const LINKED_AT = "/herdr/mods/step-counts";
25
+
26
+ const LEFT = 4; // the steps in the left column; the rest are in the right one
27
+ const COLUMN_GAP = 12;
28
+ const NAME_CH = 6.6; // px a letter of a step's name takes, to line a column's dots up
29
+ const DOT = 6; // px across a mark's dot
30
+ const DOT_GAP = 3;
31
+ const MORE_CH = 7; // px kept for each character of "+N"
32
+ const ROOM = 36; // px a row's dots are taken to have, until the row is measured
33
+
34
+ // The colour of each mark's dot: red for a card whose agent is stuck or has
35
+ // stopped, amber for one that needs the human, green for one that is ready to
36
+ // merge, and blue for one whose agent is working.
37
+ const colors = (t) => ({ stuck: t.ansi1, "needs-human": t.ansi3, ready: t.ansi2, working: t.ansi4 });
38
+
39
+ // `herdr-app check` draws the mod once, with nothing read, which would show
40
+ // one line of text and none of the rows. It is given these to draw, so a
41
+ // mistake in a row fails the check: every mark, a row with more dots than
42
+ // fit, a row with none, and a count of cards that are held. It draws a line
43
+ // under them too, as one stands in place of the rows.
44
+ const SAMPLE = {
45
+ space: "demo",
46
+ counts: [
47
+ { step: "define", count: 1, marks: ["working"], text: "1" },
48
+ { step: "plan", count: 0, marks: [], text: "0" },
49
+ { step: "breakdown", count: 1, marks: ["needs-human"], text: "1" },
50
+ { step: "start", count: 2, marks: [], text: "2 held" },
51
+ { step: "build", count: 12, text: "12",
52
+ marks: ["stuck", "needs-human", "needs-human", "working", "working", "working", "working", "working",
53
+ "working", "working", "working", "working"] },
54
+ { step: "review", count: 2, marks: ["needs-human", "working"], text: "2" },
55
+ { step: "pr", count: 3, marks: ["stuck", "needs-human", "ready"], text: "3" },
56
+ ],
57
+ };
58
+
59
+ function find(node, id) {
60
+ if (node?.id === id) return node;
61
+ for (const child of node?.children ?? []) {
62
+ const found = find(child, id);
63
+ if (found) return found;
64
+ }
65
+ return null;
66
+ }
67
+
68
+ // The sidebar's rail goes by its width, as in Herdr's own lists.
69
+ const narrow = () => (find(layout(), "sidebar")?.size?.px ?? Infinity) < 160;
70
+
71
+ function parse(text) {
72
+ try {
73
+ return JSON.parse(text);
74
+ } catch {
75
+ return null;
76
+ }
77
+ }
78
+
79
+ // What Herdr shows of the tabs in the spaces whose folder is `folder`, on
80
+ // this computer: each tab's id and name, and each of its terminals' status
81
+ // and app. `task log` works out each card's mark from these, so a change in
82
+ // them is read at once. A pane's status has no word for hc's error, so each
83
+ // terminal's own word, from terminals(), is in it too.
84
+ function herdrSees(folder) {
85
+ if (folder === null) return "";
86
+ const word = new Map(terminals().map((t) => [t.address, t.status ?? null]));
87
+ const tabs = new Set();
88
+ const seen = [];
89
+ for (const space of spaces()) {
90
+ if (space.machine || space.folder !== folder) continue;
91
+ for (const tab of space.tabs) {
92
+ tabs.add(tab.id);
93
+ seen.push([tab.id, tab.name]);
94
+ }
95
+ }
96
+ for (const p of panes()) {
97
+ if (p.terminal && tabs.has(p.tab)) seen.push([p.tab, p.terminal, p.status, p.app, word.get(p.terminal) ?? null]);
98
+ }
99
+ return JSON.stringify(seen);
100
+ }
101
+
102
+ // What a failed command said, without the name it starts its errors with.
103
+ const said = (r) => (r.stderr || r.stdout).trim().replace(/^ocnus: /, "");
104
+
105
+ const OLD_CLI = "This ocnus is too old for the step counts. Run: ocnus update";
106
+ const NOT_OCNUS = "This space is not an ocnus space.";
107
+ // An ocnus from before --json answers with its usage line, not JSON, and
108
+ // one from before the list of cards asks which card.
109
+ const failure = (r, what) => {
110
+ const text = said(r);
111
+ return text.startsWith("usage:") || text.startsWith("which card?") ? OLD_CLI : text || `${what} failed`;
112
+ };
113
+
114
+ // The folder of the space that is showing on this computer, or null.
115
+ const selected = () => spaces().find((s) => s.active && !s.machine)?.folder ?? null;
116
+
117
+ // How many dots to draw for a row of `marks` marks in `room` px: all of
118
+ // them when they fit, and else as many as fit beside the "+N" that stands
119
+ // for the rest.
120
+ function fit(marks, room) {
121
+ const wide = (n) => n * DOT + Math.max(0, n - 1) * DOT_GAP;
122
+ if (wide(marks) <= room) return marks;
123
+ for (let n = marks - 1; n > 0; n--) {
124
+ const more = (String(marks - n).length + 1) * MORE_CH;
125
+ if (wide(n) + DOT_GAP + more <= room) return n;
126
+ }
127
+ return 0;
128
+ }
129
+
130
+ export default class StepCounts extends View {
131
+ init(_props, cx) {
132
+ this.cmd = "ocnus"; // the one on PATH, until that cannot start
133
+ this.listed = null; // the spaces ocnus listed last
134
+ this.listedAt = 0;
135
+ this.key = null; // the folder that the counts on hand were read for
136
+ this.space = null; // the ocnus space the shown Herdr space is, or null
137
+ this.counts = null; // the counts read last
138
+ this.error = null;
139
+ this.busy = false;
140
+ this.readAt = 0;
141
+ this.seen = null; // what Herdr showed of the space's tabs when it was read
142
+ this.drawn = null; // what the last redraw showed, to redraw only on a change
143
+ this.room = {}; // the px each row has for its dots, as measured, by the row's place
144
+ if (checking()) return;
145
+ cx.timer.every(LOOK_MS, (cx) => this.look(cx));
146
+ this.look(cx);
147
+ }
148
+
149
+ // Runs the ocnus command. One that cannot run is tried again as the
150
+ // copy this mod is linked from: Herdr may have been started without the
151
+ // user's PATH. Everything this mod runs only reads, so twice is safe.
152
+ async ocnus(args) {
153
+ try {
154
+ return await exec(this.cmd, args, { timeout: TIMEOUT_MS });
155
+ } catch (e) {
156
+ if (this.cmd !== "ocnus") throw e;
157
+ const link = await exec("readlink", [`${appHome()}/mods/step-counts`]);
158
+ const at = link.stdout.trim();
159
+ if (link.code !== 0 || !at.endsWith(LINKED_AT)) throw e;
160
+ this.cmd = `${at.slice(0, -LINKED_AT.length)}/cli/ocnus`;
161
+ return exec(this.cmd, args, { timeout: TIMEOUT_MS });
162
+ }
163
+ }
164
+
165
+ // Read the counts again when another space is showing, when Herdr's tabs
166
+ // in the space have changed, or when what is on hand is stale: so does a
167
+ // read that failed, which is tried again. A space that is not an ocnus
168
+ // space is looked up again only as often as the spaces are listed. Nothing
169
+ // is read while nobody can see the section.
170
+ look(cx) {
171
+ if (this.busy || window()?.visible === false || narrow()) return;
172
+ const folder = selected();
173
+ const key = folder ?? "";
174
+ const seen = herdrSees(folder);
175
+ const stale = this.space || this.error
176
+ ? Date.now() - this.readAt >= REREAD_MS || seen !== this.seen
177
+ : Date.now() - this.readAt >= SPACES_MS;
178
+ if (key === this.key && !stale) return;
179
+ this.busy = true;
180
+ cx.spawn(async (cx) => {
181
+ let space = null;
182
+ let counts = null;
183
+ let error = null;
184
+ try {
185
+ if (!this.listed || Date.now() - this.listedAt >= SPACES_MS) {
186
+ const r = await this.ocnus(["space", "ls", "--json"]);
187
+ const list = r.code === 0 ? parse(r.stdout)?.spaces : null;
188
+ if (Array.isArray(list)) {
189
+ this.listed = list;
190
+ this.listedAt = Date.now();
191
+ } else {
192
+ error = r.code === 0 ? OLD_CLI : failure(r, "ocnus space ls");
193
+ }
194
+ }
195
+ space = (this.listed ?? []).find((s) => s.folder === folder)?.name ?? null;
196
+ if (!error && space) {
197
+ const r = await this.ocnus(["task", "log", space, "--json"]);
198
+ const got = r.code === 0 ? parse(r.stdout) : null;
199
+ // An ocnus from before the counts gives the list without them.
200
+ if (Array.isArray(got?.counts)) counts = got.counts;
201
+ else error = r.code === 0 ? OLD_CLI : failure(r, "ocnus task log");
202
+ }
203
+ } catch (e) {
204
+ error = `Could not run ocnus: ${e?.message ?? e}`;
205
+ }
206
+ this.key = key;
207
+ this.space = space;
208
+ this.counts = counts;
209
+ this.error = error;
210
+ this.readAt = Date.now();
211
+ this.seen = seen;
212
+ this.busy = false;
213
+ // A redraw of a mod redraws the whole window, so only when what it
214
+ // shows has changed.
215
+ const drawn = JSON.stringify([key, space, counts, error]);
216
+ if (drawn !== this.drawn) {
217
+ this.drawn = drawn;
218
+ cx.notify();
219
+ }
220
+ // A space chosen while this was being read is read now, not at the
221
+ // next look.
222
+ if ((selected() ?? "") !== key) this.look(cx);
223
+ });
224
+ }
225
+
226
+ // One step, on one line: its name, a dot for each of its cards, as many as
227
+ // fit, with "+N" for the rest, and its text at the right. `i` is its place
228
+ // among the steps, and `name` the px its column gives a name.
229
+ row(c, i, t, name) {
230
+ const marks = Array.isArray(c.marks) ? c.marks : [];
231
+ const color = colors(t);
232
+ const shown = fit(marks.length, this.room[i] ?? ROOM);
233
+ const rest = marks.length - shown;
234
+ return h_flex().flex_none().gap(6).items_center()
235
+ .child(div().flex_none().min_w(name).text_color(t.muted).child(String(c.step ?? "")))
236
+ .child(h_flex().id(`dots-${i}`).flex_1().min_w(0).gap(DOT_GAP).items_center().overflow_hidden()
237
+ // The room the dots have, measured. The size is in window px.
238
+ .on_size_change((size, cx) => {
239
+ const room = size.width / (Number(settings().zoom) || 1);
240
+ const was = this.room[i] ?? ROOM;
241
+ this.room[i] = room;
242
+ if (fit(marks.length, room) !== fit(marks.length, was)) cx.notify();
243
+ })
244
+ .children(marks.slice(0, shown).map((m) =>
245
+ div().flex_none().size(DOT).rounded(DOT / 2).bg(color[m] ?? t.dim)))
246
+ .when(rest > 0, (el) => el.child(div().flex_none().text_color(t.dim).child(`+${rest}`))))
247
+ .child(div().flex_none().text_color(c.count > 0 ? t.foreground : t.dim).child(String(c.text ?? "")));
248
+ }
249
+
250
+ render() {
251
+ const t = theme();
252
+ // The block and the line above it, as Usage and CPU have theirs.
253
+ const block = () => v_flex().size_full().bg(t.chrome).border_t(1).border_color(t.border);
254
+ // The sidebar's rail has room for the line alone.
255
+ if (narrow()) return block();
256
+ const sample = checking();
257
+ const counts = (sample ? SAMPLE.counts : this.counts) ?? [];
258
+ // A column of rows, with the room for a name that its longest one needs.
259
+ const column = (from, to) => {
260
+ const rows = counts.slice(from, to);
261
+ const name = Math.ceil(Math.max(0, ...rows.map((c) => String(c.step ?? "").length)) * NAME_CH);
262
+ return v_flex().flex_1().min_w(0).overflow_hidden()
263
+ .children(rows.map((c, i) => this.row(c, from + i, t, name)));
264
+ };
265
+ const rows = () => h_flex().flex_none().gap(COLUMN_GAP).items_start()
266
+ .child(column(0, LEFT))
267
+ .child(column(LEFT, counts.length));
268
+ // The one line that stands in place of the rows.
269
+ const line = (text, color) => div().flex_none().text_color(color).whitespace_normal().child(text);
270
+ let body;
271
+ if (sample) body = [rows(), line(NOT_OCNUS, t.dim)];
272
+ else if ((selected() ?? "") !== this.key) body = [line("Reading…", t.dim)];
273
+ else if (this.error) body = [line(this.error, t.blocked)];
274
+ else if (!this.space) body = [line(NOT_OCNUS, t.dim)];
275
+ else body = [rows()];
276
+ // A line height of its own, so that the four rows of the left column fit
277
+ // the 72px the layout gives the section, whatever the default is.
278
+ return block().px(14).py(6).justify_center().line_height(1.25).overflow_hidden().text_size(11)
279
+ .children(body);
280
+ }
281
+ }
@@ -0,0 +1 @@
1
+ { "place": "none", "title": "Step counts" }
@@ -0,0 +1,78 @@
1
+ // How tall the Test runs section's block is, and how many of its rows show.
2
+ // It is apart from main.js, and imports nothing, so that the tests can run it
3
+ // with node (test/test-runs.sh). So it uses only what any JavaScript has:
4
+ // nothing of Herdr's, and nothing of node's.
5
+ //
6
+ // The block is as tall as what it shows: one line with no run going, and a
7
+ // row taller for each run going. It takes no more than the room its column
8
+ // leaves it, so it never squeezes another section of the sidebar, or a list
9
+ // below its min. When the room is short of a row for every run, the rows
10
+ // that fit show, and a line says how many more runs there are.
11
+
12
+ // The section's sizes, in the layout's px, as main.js draws it: the line
13
+ // above it, its padding above and below, a line of its 11px text at a line
14
+ // height of 1.25, and the gap between two lines. A row is one line.
15
+ export const BORDER = 1;
16
+ export const PAD = 6;
17
+ export const LINE = 11 * 1.25;
18
+ export const GAP = 2;
19
+
20
+ // The block's px for this many lines, rounded up to a whole px: the first
21
+ // line, then a line for each row, and one for how many more runs there are.
22
+ export const height = (lines) => Math.ceil(BORDER + 2 * PAD + lines * LINE + (lines - 1) * GAP);
23
+
24
+ // The first node of the layout, in the order it lists them, that passes the
25
+ // test, and the node it is a child of: { node, parent }, or null when there
26
+ // is none. The root's parent is null.
27
+ export function find(node, test, parent = null) {
28
+ if (!node || typeof node !== "object") return null;
29
+ if (test(node)) return { node, parent };
30
+ for (const child of Array.isArray(node.children) ? node.children : []) {
31
+ const found = find(child, test, node);
32
+ if (found) return found;
33
+ }
34
+ return null;
35
+ }
36
+
37
+ // The least height of a layout node: its px, else its min, else what its
38
+ // children need, which is all of them in a column and the tallest of them
39
+ // otherwise. A hidden node needs none.
40
+ export function least(node) {
41
+ if (!node || typeof node !== "object" || node.hidden) return 0;
42
+ const px = node.size?.px;
43
+ if (typeof px === "number") return px;
44
+ if (typeof node.min === "number") return node.min;
45
+ const needs = (Array.isArray(node.children) ? node.children : []).map(least);
46
+ if (!needs.length) return 0;
47
+ return node.type === "column" ? needs.reduce((all, one) => all + one, 0) : Math.max(...needs);
48
+ }
49
+
50
+ // The room the block may take, in px: the window's height less the least
51
+ // of every other child of its column. at is the block, as find gives it.
52
+ // Null when the block is not a column's child, since its px would then be
53
+ // its width, or when the window's height is not known.
54
+ export function room(at, tall) {
55
+ if (!at || at.parent?.type !== "column" || !(tall > 0)) return null;
56
+ return at.parent.children.reduce((left, child) => (child === at.node ? left : left - least(child)), tall);
57
+ }
58
+
59
+ // With count runs going and room px for the block, { rows, more, px }: how
60
+ // many rows show, how many more runs there are than rows, and the block's
61
+ // px. Every row shows when the room holds them all. Otherwise the first rows
62
+ // that fit show, and a line under them says how many more there are; with
63
+ // no room for that line either, the first line says it. The px is never less
64
+ // than one line, which is all it is with no run going. With room null, every
65
+ // row shows, and there is no px.
66
+ export function fit(count, room) {
67
+ const runs = Math.max(0, Math.floor(Number(count)) || 0);
68
+ if (room === null || room === undefined) return { rows: runs, more: 0, px: null };
69
+ if (!runs || height(1 + runs) <= room) return { rows: runs, more: 0, px: height(1 + runs) };
70
+ let rows = runs - 1;
71
+ while (rows > 0 && height(2 + rows) > room) rows -= 1;
72
+ if (!rows && height(2) > room) return { rows: 0, more: runs, px: height(1) };
73
+ return { rows, more: runs - rows, px: height(2 + rows) };
74
+ }
75
+
76
+ // Whether, in the block fit gives, how many more runs there are has a line
77
+ // of its own under the rows. When it does not, the first line says it.
78
+ export const below = (shown) => shown.more > 0 && shown.px !== null && shown.px > height(1 + shown.rows);
@@ -0,0 +1,242 @@
1
+ // The Test runs section: the runs of ocnus's own tests going on this
2
+ // machine, a row each, and which way `ocnus tests` is set. It sits under CPU
3
+ // in the default layout's sidebar, and is as tall as what it shows: one line
4
+ // with no run going, and a row taller for each run going.
5
+ //
6
+ // All of that is worked out by `ocnus tests ls --json`, which reads the pool
7
+ // test/run.sh keeps the runs in. This mod draws what that command returns
8
+ // and holds no rules about the runs: each run's name, what it is doing, its
9
+ // mark and what the mark means, how long ago it started and the processors
10
+ // it holds all arrive as text, and so does the line to show with no run
11
+ // going. How tall the block may grow, and how many rows show, is worked out
12
+ // in fit.js, which the tests run with node. The mod resizes its own block to
13
+ // that, and Herdr keeps the size as a click of the layout's, never in the
14
+ // layout's file.
15
+ import { View, div } from "gpui-kit";
16
+ import { h_flex, v_flex } from "gpui-base";
17
+ import { exec, run, theme, window, layout, settings, checking, appHome } from "herdr";
18
+ import { find, room, fit, below, height } from "./fit.js";
19
+
20
+ // A run that starts, ends or gets its turn shows within this and a read.
21
+ const READ_MS = 2000;
22
+ const TIMEOUT_MS = 10000;
23
+ // The mod, as a layout's block names it.
24
+ const MOD = "test-runs";
25
+ // How this mod's own link into the repo that ships it ends.
26
+ const LINKED_AT = "/herdr/mods/test-runs";
27
+
28
+ // `herdr-app check` draws the mod once, with nothing read. It is given
29
+ // this to draw, so a mistake in a row fails the check: a run in each state,
30
+ // two of them marked old mode, and more runs than there is room for.
31
+ const SAMPLE = {
32
+ tests: "queue", cpus: 10, note: null,
33
+ runs: [
34
+ { number: 1, pid: 101, name: "LAB-248", state: "turn", doing: "its turn", mark: null, tip: null, age: "4m 10s", holds: "10/10" },
35
+ { number: 2, pid: 102, name: "ocnus", state: "share", doing: "sharing", mark: "old mode",
36
+ tip: "Started before the switch to queue, so it still shares the processors", age: "3m 02s", holds: "2/10" },
37
+ { number: 3, pid: 103, name: "LAB-7", state: "jobs", doing: "OCNUS_TEST_JOBS", mark: "old mode",
38
+ tip: "Started before the switch to queue, so it still runs beside the runs ahead of it", age: "1m 05s", holds: "4/10" },
39
+ { number: 4, pid: 104, name: "ABC-9", state: "wait", doing: "waiting", mark: null, tip: null, age: "50s", holds: "0/10" },
40
+ { number: 5, pid: 105, name: "LAB-12", state: "wait", doing: "waiting", mark: null, tip: null, age: "12s", holds: "0/10" },
41
+ { number: 6, pid: 106, name: "LAB-13", state: "wait", doing: "waiting", mark: null, tip: null, age: "3s", holds: "0/10" },
42
+ ],
43
+ };
44
+ // Room for four of the sample's six rows, and the line that says there are
45
+ // two more.
46
+ const SAMPLE_FIT = fit(6, height(6));
47
+
48
+ function parse(text) {
49
+ try {
50
+ return JSON.parse(text);
51
+ } catch {
52
+ return null;
53
+ }
54
+ }
55
+
56
+ // What a failed command said, without the name it starts its errors with.
57
+ const said = (r) => (r.stderr || r.stdout).trim().replace(/^ocnus: /, "");
58
+
59
+ const OLD_CLI = "This ocnus is too old for Test runs. Run: ocnus update";
60
+ // An ocnus from before `tests ls` answers `tests ls --json` with the usage
61
+ // of `tests`, and one from before `tests` with its help, not JSON.
62
+ const failure = (r) => {
63
+ const text = said(r);
64
+ return text.startsWith("set up and maintain") || text.startsWith("usage:") ? OLD_CLI : text || "ocnus tests ls failed";
65
+ };
66
+
67
+ // The sidebar's rail goes by its width, as in Herdr's own lists.
68
+ const narrow = () => (find(layout(), (n) => n.id === "sidebar")?.node.size?.px ?? Infinity) < 160;
69
+
70
+ export default class TestRuns extends View {
71
+ init(_props, cx) {
72
+ this.cmd = "ocnus"; // the one on PATH, until that cannot start
73
+ this.view = null;
74
+ this.error = null;
75
+ this.busy = false;
76
+ this.loaded = false; // whether a read has ended yet
77
+ this.tall = null; // the window's height in the layout's px, as last drawn
78
+ this.shown = fit(0, null); // how many rows show, how many more there are, and the block's px
79
+ this.drawn = null; // what the last redraw showed, to redraw only on a change
80
+ if (checking()) return;
81
+ cx.timer.every(READ_MS, (cx) => this.look(cx));
82
+ this.look(cx);
83
+ }
84
+
85
+ // Runs the ocnus command. One that cannot run is tried again as the
86
+ // copy this mod is linked from: Herdr may have been started without the
87
+ // user's PATH. `tests ls --json` only reads, so twice is safe.
88
+ async ocnus(args) {
89
+ try {
90
+ return await exec(this.cmd, args, { timeout: TIMEOUT_MS });
91
+ } catch (e) {
92
+ if (this.cmd !== "ocnus") throw e;
93
+ const link = await exec("readlink", [`${appHome()}/mods/${MOD}`]);
94
+ const to = link.stdout.trim();
95
+ if (link.code !== 0 || !to.endsWith(LINKED_AT)) throw e;
96
+ this.cmd = `${to.slice(0, -LINKED_AT.length)}/cli/ocnus`;
97
+ return exec(this.cmd, args, { timeout: TIMEOUT_MS });
98
+ }
99
+ }
100
+
101
+ // Read the runs again, unless a read is in flight or nobody can see the
102
+ // window, then fit the block to what it shows.
103
+ look(cx) {
104
+ if (this.busy || window()?.visible === false) return;
105
+ this.busy = true;
106
+ cx.spawn(async (cx) => {
107
+ let view = null;
108
+ let error = null;
109
+ try {
110
+ const r = await this.ocnus(["tests", "ls", "--json"]);
111
+ const got = r.code === 0 ? parse(r.stdout) : null;
112
+ if (got && typeof got === "object" && Array.isArray(got.runs)) view = got;
113
+ else error = r.code === 0 ? OLD_CLI : failure(r);
114
+ } catch (e) {
115
+ error = `Could not run ocnus: ${e?.message ?? e}`;
116
+ }
117
+ this.view = view;
118
+ this.error = error;
119
+ this.loaded = true;
120
+ this.busy = false;
121
+ this.size();
122
+ // A redraw of a mod redraws the whole window, so only when what it
123
+ // shows has changed, such as the age of a run going.
124
+ const drawn = JSON.stringify([view, error, this.shown]);
125
+ if (drawn !== this.drawn) {
126
+ this.drawn = drawn;
127
+ cx.notify();
128
+ }
129
+ });
130
+ }
131
+
132
+ // Fit the block to what it shows: a row for each run going, when the
133
+ // window has room for them all, and never less than one line, which is
134
+ // all it is on the sidebar's rail and when the runs cannot be read. Its
135
+ // block is the one of the shown layout that places this mod.
136
+ size() {
137
+ const count = this.error || narrow() ? 0 : this.view?.runs.length ?? 0;
138
+ const at = find(layout(), (n) => n.content?.mod === MOD);
139
+ this.shown = fit(count, room(at, this.tall));
140
+ const px = this.shown.px;
141
+ if (px === null || !at?.node.id || Math.abs((at.node.size?.px ?? 0) - px) < 0.5) return;
142
+ try {
143
+ run("layout.resize", { id: at.node.id, px });
144
+ } catch {
145
+ // The block keeps the size it has, and shows what fits in it.
146
+ }
147
+ }
148
+
149
+ // Note the window's height in the layout's px, which the block's room is
150
+ // worked out from. Herdr's own global window gives it, only while a mod
151
+ // draws; the window this mod imports from Herdr hides that name.
152
+ measure() {
153
+ try {
154
+ const tall = globalThis.window.viewport_size().height / (Number(settings().zoom) || 1);
155
+ if (tall > 0) this.tall = tall;
156
+ } catch {
157
+ // A Herdr without it: the block keeps the size it has.
158
+ }
159
+ }
160
+
161
+ // A run on one line: its name, what it is doing with its mark after it,
162
+ // how long ago it started, and the processors it holds. What the mark
163
+ // means is the row's tooltip.
164
+ row(r, t) {
165
+ return h_flex().id(`test-run-${r.number}-${r.pid}`).flex_none().gap(6).items_center()
166
+ .when(Boolean(r.tip), (el) => el.tooltip(r.tip))
167
+ .child(div().w(56).flex_none().text_color(t.foreground).truncate().child(r.name ?? ""))
168
+ .child(h_flex().flex_1().min_w(0).gap(4).items_center()
169
+ .child(div().min_w(0).text_color(t.muted).truncate().child(r.doing ?? ""))
170
+ .when(Boolean(r.mark), (el) => el.child(div().flex_none().text_color(t.ansi3).child(r.mark))))
171
+ .child(div().flex_none().text_color(t.dim).child(r.age ?? ""))
172
+ .child(h_flex().min_w(30).flex_none().justify_end().text_color(t.muted).child(r.holds ?? ""));
173
+ }
174
+
175
+ // The section: its first line, a row for each run that shows, in the order
176
+ // the command gives them, and how many more runs there are. view is what
177
+ // `tests ls --json` gave, error what went wrong reading it, and shown what
178
+ // fit gives for it. With 11px text at a line height of 1.25, every line is
179
+ // as tall as fit.js counts it.
180
+ face(t, view, error, shown, loaded) {
181
+ const runs = error ? [] : view?.runs ?? [];
182
+ const more = `+${shown.more} more`;
183
+ const note = (text, color) => div().flex_1().min_w(0).text_color(color).truncate().child(text);
184
+ // Between the title and the setting: that it is reading, what went
185
+ // wrong, that no run is going, or how many more runs there are when the
186
+ // block has no line for that.
187
+ let middle = div().flex_1();
188
+ if (!loaded) middle = note("Reading…", t.dim);
189
+ else if (error) middle = div().id("test-runs-error").flex_1().min_w(0).text_color(t.blocked).truncate().tooltip(error).child(error);
190
+ else if (view?.note) middle = note(view.note, t.dim);
191
+ else if (shown.more > 0 && !below(shown)) middle = note(more, t.dim);
192
+ return v_flex().size_full().px(14).py(6).gap(2).line_height(1.25).overflow_hidden()
193
+ .text_size(11).bg(t.chrome).border_t(1).border_color(t.border)
194
+ .child(h_flex().flex_none().gap(8).items_center()
195
+ .child(div().flex_none().text_color(t.muted).child("Test runs"))
196
+ .child(middle)
197
+ .child(div().flex_none().text_color(t.dim).child(error ? "" : view?.tests ?? "")))
198
+ .children(runs.slice(0, shown.rows).map((r) => this.row(r, t)))
199
+ .when(below(shown), (el) => el.child(div().flex_none().text_color(t.dim).child(more)));
200
+ }
201
+
202
+ // On the sidebar's rail there is no room for the rows: how many runs are
203
+ // going, on its one line, or a mark when they cannot be read, with the
204
+ // words in its tooltip.
205
+ rail(t, view, error, loaded) {
206
+ const count = view?.runs.length ?? 0;
207
+ const words = view?.note ?? `${count} test run${count === 1 ? "" : "s"} going`;
208
+ return v_flex().size_full().py(6).line_height(1.25).overflow_hidden().items_center()
209
+ .text_size(11).bg(t.chrome).border_t(1).border_color(t.border)
210
+ .child(error
211
+ ? div().id("test-runs-error").text_color(t.blocked).tooltip(error).child("!")
212
+ : div().id("test-runs-count").text_color(count ? t.foreground : t.dim)
213
+ .tooltip(loaded ? words : "Reading…").child(loaded ? String(count) : "…"));
214
+ }
215
+
216
+ // What `herdr-app check` draws: each face of the section once, so that a
217
+ // mistake in any of them fails the check. The sample, with a line for how
218
+ // many more runs there are; no run going; a failure; the sample in one
219
+ // line, which then says how many more there are; reading; and the rail.
220
+ checked(t) {
221
+ const one = height(1);
222
+ const none = { tests: "share", cpus: 10, note: "No test runs going", runs: [] };
223
+ const tight = fit(SAMPLE.runs.length, one);
224
+ const face = (shown, view, error, loaded = true) =>
225
+ div().h(shown.px).flex_none().child(this.face(t, view, error, shown, loaded));
226
+ return v_flex().size_full()
227
+ .child(face(SAMPLE_FIT, SAMPLE, null))
228
+ .child(face(fit(0, one), none, null))
229
+ .child(face(fit(0, one), null, OLD_CLI))
230
+ .child(face(tight, SAMPLE, null))
231
+ .child(face(fit(0, one), null, null, false))
232
+ .child(div().w(44).h(one).flex_none().child(this.rail(t, SAMPLE, null, true)));
233
+ }
234
+
235
+ render() {
236
+ const t = theme();
237
+ if (checking()) return this.checked(t);
238
+ this.measure();
239
+ if (narrow()) return this.rail(t, this.view, this.error, this.loaded);
240
+ return this.face(t, this.view, this.error, this.shown, this.loaded);
241
+ }
242
+ }
@@ -0,0 +1 @@
1
+ { "place": "none", "title": "Test runs" }