@johngaynor/ocnus 0.0.0-stage → 2.0.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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +988 -2
  3. package/agents/linear-mcp.json +1 -0
  4. package/agents/ocnus-build.md +23 -0
  5. package/agents/ocnus-write-up.md +20 -0
  6. package/cli/host-mark +36 -0
  7. package/cli/lib/agents.sh +350 -0
  8. package/cli/lib/approval.sh +94 -0
  9. package/cli/lib/archive.sh +447 -0
  10. package/cli/lib/billing.sh +166 -0
  11. package/cli/lib/close.sh +189 -0
  12. package/cli/lib/commands.sh +220 -0
  13. package/cli/lib/common.sh +215 -0
  14. package/cli/lib/finish.sh +288 -0
  15. package/cli/lib/github.sh +162 -0
  16. package/cli/lib/guard.sh +134 -0
  17. package/cli/lib/handoff.sh +551 -0
  18. package/cli/lib/host.sh +1802 -0
  19. package/cli/lib/linear.sh +1389 -0
  20. package/cli/lib/links.sh +135 -0
  21. package/cli/lib/merges.sh +853 -0
  22. package/cli/lib/recipes.sh +54 -0
  23. package/cli/lib/settings.sh +465 -0
  24. package/cli/lib/space.sh +343 -0
  25. package/cli/lib/task.sh +847 -0
  26. package/cli/lib/tests.sh +268 -0
  27. package/cli/lib/trust.sh +70 -0
  28. package/cli/ocnus +630 -0
  29. package/herdr/README.md +2238 -0
  30. package/herdr/layouts/default.json +259 -0
  31. package/herdr/mods/agent-settings/cog.svg +4 -0
  32. package/herdr/mods/agent-settings/main.js +356 -0
  33. package/herdr/mods/agent-settings/mod.json +1 -0
  34. package/herdr/mods/cards/main.js +450 -0
  35. package/herdr/mods/cards/mod.json +1 -0
  36. package/herdr/mods/cards/refresh.svg +4 -0
  37. package/herdr/mods/cpu/main.js +90 -0
  38. package/herdr/mods/cpu/mod.json +1 -0
  39. package/herdr/mods/layout-switch/main.js +48 -0
  40. package/herdr/mods/layout-switch/mod.json +1 -0
  41. package/herdr/mods/names/main.js +69 -0
  42. package/herdr/mods/names/mod.json +1 -0
  43. package/herdr/mods/ocnus-spaces/main.js +376 -0
  44. package/herdr/mods/ocnus-spaces/mod.json +1 -0
  45. package/herdr/mods/steps/main.js +354 -0
  46. package/herdr/mods/steps/mod.json +1 -0
  47. package/herdr/spaces/template/SPACE.md +19 -0
  48. package/package.json +27 -4
  49. package/prompts/task-build.md +11 -0
  50. package/prompts/task-new.md +17 -0
  51. package/skills/brief/SKILL.md +64 -0
  52. package/skills/sdlc-breakdown/SKILL.md +312 -0
  53. package/skills/sdlc-build/SKILL.md +182 -0
  54. package/skills/sdlc-define/SKILL.md +203 -0
  55. package/skills/sdlc-plan/SKILL.md +202 -0
  56. package/skills/sdlc-pr/SKILL.md +339 -0
  57. package/skills/sdlc-review/SKILL.md +227 -0
@@ -0,0 +1,450 @@
1
+ // The open Linear cards of the ocnus space that is showing in Herdr,
2
+ // under their statuses, with a button on each card that starts it.
3
+ //
4
+ // Which ocnus space a Herdr space is, and which cards are open, are the
5
+ // ocnus command's to say: `ocnus space ls --json` and
6
+ // `ocnus task ls <space> --json`. This mod draws what they answer and
7
+ // holds no rules about spaces or Linear. A card's button runs
8
+ // `ocnus task new <space> <card>`, which opens the card's tab in the
9
+ // space with the agent running in it. A card whose agent is stuck or has
10
+ // stopped, or whose pull requests have no agent to wake or nothing watching
11
+ // them, as `ocnus task log <space> --json` says, has a mark, with what
12
+ // that command says in its tooltip.
13
+ //
14
+ // A click on a status opens or closes its group.
15
+ import { View, div, svg } from "gpui-kit";
16
+ import { v_flex, h_flex } from "gpui-base";
17
+ import { exec, spaces, panes, terminals, run, notify, theme, layout, window, checking, appHome } from "herdr";
18
+
19
+ // How often to see whether another space is showing, or Herdr's tabs in it
20
+ // have changed. A change in them is read at once, so an agent's state shows
21
+ // within this and a read.
22
+ const LOOK_MS = 1000;
23
+ const REFRESH_MS = 60000; // how often to read the cards again
24
+ const AGENTS_MS = 6000; // how often to read what each card's agent is doing again
25
+ const TIMEOUT_MS = 10000;
26
+ const LIST_TIMEOUT_MS = 30000; // task ls waits up to 20s on Linear
27
+ const START_TIMEOUT_MS = 60000;
28
+ const ROW = 30;
29
+ const RAIL_CELL = 32;
30
+ // How this mod's own link into the repo that ships it ends.
31
+ const LINKED_AT = "/herdr/mods/cards";
32
+ const NOT_A_SPACE = "The shown space is not an ocnus space.";
33
+ const OLD_CLI = "This ocnus cannot list a space's cards. Run: ocnus update";
34
+ const CHEVRON = "mods/workspace-chrome/chevron";
35
+ const SPINNER = "mods/workspace-chrome/spinner.svg";
36
+
37
+ // `herdr-app check` draws the mod once, with no space showing, which would
38
+ // draw one line of text and none of the rows. It is given these to draw
39
+ // instead: an open group, a closed one, an empty one, a card being started,
40
+ // a card whose tab is open, whose agent is waiting on a permission prompt,
41
+ // and three whose pull requests wait on the human with their tabs closed:
42
+ // one closed without merging, one in conflict, and one that nothing is
43
+ // watching.
44
+ const SAMPLE = {
45
+ space: "sample",
46
+ statuses: [
47
+ { name: "Triage", type: "triage", cards: [] },
48
+ { name: "Backlog", type: "backlog", cards: [
49
+ { id: "ABC-1", title: "A card", url: "" },
50
+ { id: "ABC-2", title: "A card being started", url: "" },
51
+ ] },
52
+ { name: "Todo", type: "unstarted", cards: [{ id: "ABC-3", title: "A card in a closed group", url: "" }] },
53
+ { name: "In Progress", type: "started", cards: [{ id: "ABC-4", title: "A card with its tab open", url: "" }] },
54
+ { name: "In Review", type: "started", cards: [
55
+ { id: "ABC-5", title: "A card whose pull request was closed", url: "" },
56
+ { id: "ABC-6", title: "A card whose pull request is in conflict", url: "" },
57
+ { id: "ABC-7", title: "A card whose pull requests nothing is watching", url: "" },
58
+ ] },
59
+ ],
60
+ };
61
+ const SAMPLE_STARTING = "ABC-2";
62
+ const SAMPLE_CLOSED = new Set(["sample/Todo"]);
63
+ const SAMPLE_TABS = new Map([["ABC-4", { space: { id: "s", active: true }, tab: { id: "t", active: true } }]]);
64
+ const SAMPLE_AGENTS = new Map([
65
+ ["ABC-4", { state: "blocked", kind: "permission", tab: "ABC-4", label: "agent waiting on a permission prompt", for: "1m 10s", seconds: 70, pulls: [] }],
66
+ ["ABC-5", { state: "closed", kind: null, tab: null, label: "acme/api#7 closed without merging, no agent running", for: "3h 10m", seconds: 11400,
67
+ pulls: ["https://github.com/acme/api/pull/7"] }],
68
+ ["ABC-6", { state: "conflict", kind: null, tab: null, label: "acme/api#8, acme/web#3 in conflict, no agent running", for: "1d 02h", seconds: 93600,
69
+ pulls: ["https://github.com/acme/api/pull/8", "https://github.com/acme/web/pull/3"] }],
70
+ ["ABC-7", { state: "unwatched", kind: null, tab: null, label: "nothing watching its pull requests: ocnus's watch not running", for: "40m 05s", seconds: 2405, pulls: [] }],
71
+ ]);
72
+
73
+ function find(node, id) {
74
+ if (node?.id === id) return node;
75
+ for (const child of node?.children ?? []) {
76
+ const found = find(child, id);
77
+ if (found) return found;
78
+ }
79
+ return null;
80
+ }
81
+
82
+ // The sidebar's rail goes by its width, as in Herdr's own lists.
83
+ const narrow = () => (find(layout(), "sidebar")?.size?.px ?? Infinity) < 160;
84
+
85
+ function parse(text) {
86
+ try {
87
+ return JSON.parse(text);
88
+ } catch {
89
+ return null;
90
+ }
91
+ }
92
+
93
+ // What a failed command said, without the name it starts its errors with.
94
+ const said = (r) => (r.stderr || r.stdout).trim().replace(/^ocnus: /, "");
95
+ // An ocnus from before `task ls` answers with its usage line, not JSON.
96
+ const failure = (r, what) => {
97
+ const text = said(r);
98
+ return text.startsWith("usage:") ? OLD_CLI : text || `${what} failed`;
99
+ };
100
+
101
+ // The shown space's folder, when that space is on this computer: the only
102
+ // place an ocnus space can be.
103
+ function shownFolder() {
104
+ const space = spaces().find((s) => s.active);
105
+ return space && !space.machine ? space.folder : null;
106
+ }
107
+
108
+ // What Herdr shows of the tabs in the spaces whose folder is `folder`, on
109
+ // this computer: each tab's id and name, and each of its terminals' status
110
+ // and app. `task log` works out each card's agent from these, so a change in
111
+ // them is read at once. A pane's status has no word for hc's error, so each
112
+ // terminal's own word, from terminals(), is in it too.
113
+ function herdrSees(folder) {
114
+ if (folder === null) return "";
115
+ const word = new Map(terminals().map((t) => [t.address, t.status ?? null]));
116
+ const tabs = new Set();
117
+ const seen = [];
118
+ for (const space of spaces()) {
119
+ if (space.machine || space.folder !== folder) continue;
120
+ for (const tab of space.tabs) {
121
+ tabs.add(tab.id);
122
+ seen.push([tab.id, tab.name]);
123
+ }
124
+ }
125
+ for (const p of panes()) {
126
+ if (p.terminal && tabs.has(p.tab)) seen.push([p.tab, p.terminal, p.status, p.app, word.get(p.terminal) ?? null]);
127
+ }
128
+ return JSON.stringify(seen);
129
+ }
130
+
131
+ // The tabs `task new` has opened in the Herdr spaces of the ocnus space
132
+ // in `folder`, by card: it names each one after its card.
133
+ function cardTabs(folder) {
134
+ const tabs = new Map();
135
+ for (const space of spaces()) {
136
+ if (space.machine || space.folder !== folder) continue;
137
+ for (const tab of space.tabs) {
138
+ const id = tab.name.toUpperCase();
139
+ // The shown space's tab wins over another space of the same folder.
140
+ if (!tabs.has(id) || space.active) tabs.set(id, { space, tab });
141
+ }
142
+ }
143
+ return tabs;
144
+ }
145
+
146
+ // Which groups are closed: "<space>/<status>", kept between starts.
147
+ function loadClosed() {
148
+ try {
149
+ const list = JSON.parse(localStorage.getItem("closed") ?? "[]");
150
+ return new Set(Array.isArray(list) ? list : []);
151
+ } catch {
152
+ return new Set();
153
+ }
154
+ }
155
+
156
+ export default class Cards extends View {
157
+ init(_props, cx) {
158
+ this.cmd = "ocnus"; // the one on PATH, until that cannot start
159
+ this.closed = loadClosed();
160
+ // What the last listing said for each shown folder: { space, folder,
161
+ // statuses } or { error }. A space shown again draws its last answer
162
+ // while it is read again.
163
+ this.lists = new Map();
164
+ this.key = undefined; // the folder the last listing was read for
165
+ this.readAt = 0;
166
+ this.busy = false;
167
+ this.starting = null; // the card whose `task new` is running
168
+ // What `task log` says about each card's agent, for each shown folder:
169
+ // a Map of card ID to its agent, for each card that has something to say.
170
+ this.agents = new Map();
171
+ this.agentsSeen = null; // the folder, and what Herdr showed of it, when it was read
172
+ this.agentsAt = 0;
173
+ this.agentsBusy = false;
174
+ this.agentsAgain = false; // read again as soon as the read under way is done
175
+ this.agentsDrawn = null; // the marks the last read drew, to redraw only on a change
176
+ if (checking()) return;
177
+ cx.timer.every(LOOK_MS, (cx) => this.look(cx));
178
+ this.refresh(cx);
179
+ }
180
+
181
+ // Runs the ocnus command. With `fallback`, a command that cannot run is
182
+ // tried again as the copy this mod is linked from (Herdr may have been
183
+ // started without the user's PATH). Only the readings ask for that: they
184
+ // only read, and the listing has answered before there is a card to start.
185
+ // A start never does, because its failure may be a timeout with the first
186
+ // run still opening the tab.
187
+ async ocnus(args, { fallback = false, timeout = TIMEOUT_MS } = {}) {
188
+ try {
189
+ return await exec(this.cmd, args, { timeout });
190
+ } catch (e) {
191
+ if (!fallback || this.cmd !== "ocnus") throw e;
192
+ const link = await exec("readlink", [`${appHome()}/mods/cards`]);
193
+ const at = link.stdout.trim();
194
+ if (link.code !== 0 || !at.endsWith(LINKED_AT)) throw e;
195
+ this.cmd = `${at.slice(0, -LINKED_AT.length)}/cli/ocnus`;
196
+ return exec(this.cmd, args, { timeout });
197
+ }
198
+ }
199
+
200
+ // Read again when another space is showing, or the cards on hand are a
201
+ // minute old, and read what each card's agent is doing as `lookAgents`
202
+ // says. Nothing is read while nobody can see the window.
203
+ look(cx) {
204
+ if (window()?.visible === false) return;
205
+ if (!this.busy && (shownFolder() !== this.key || Date.now() - this.readAt >= REFRESH_MS)) this.refresh(cx);
206
+ this.lookAgents(cx);
207
+ }
208
+
209
+ refresh(cx) {
210
+ if (this.busy) return;
211
+ this.busy = true;
212
+ const folder = shownFolder();
213
+ cx.spawn(async (cx) => {
214
+ cx.notify(); // the refresh button turns while the cards are read
215
+ const got = await this.read(folder);
216
+ this.lists.set(folder, got);
217
+ this.key = folder;
218
+ this.readAt = Date.now();
219
+ this.busy = false;
220
+ cx.notify();
221
+ // The cards may have changed, so what their agents are doing is read
222
+ // again too.
223
+ this.agentsAgain = true;
224
+ this.lookAgents(cx);
225
+ });
226
+ }
227
+
228
+ // Read what each card's agent is doing again when Herdr's tabs in the
229
+ // shown space have changed, when it is 6 seconds old, and after the cards
230
+ // are read. There is nothing to read until the space's cards are listed.
231
+ lookAgents(cx) {
232
+ if (this.agentsBusy) return;
233
+ const folder = shownFolder();
234
+ const got = this.lists.get(folder);
235
+ if (!got?.statuses) return;
236
+ const seen = JSON.stringify([folder, herdrSees(folder)]);
237
+ if (!this.agentsAgain && seen === this.agentsSeen && Date.now() - this.agentsAt < AGENTS_MS) return;
238
+ this.agentsBusy = true;
239
+ this.agentsAgain = false;
240
+ cx.spawn(async (cx) => {
241
+ const agents = await this.readAgents(got.space);
242
+ this.agents.set(folder, agents);
243
+ this.agentsSeen = seen;
244
+ this.agentsAt = Date.now();
245
+ this.agentsBusy = false;
246
+ // A redraw of a mod redraws the whole window, so only when a mark has
247
+ // changed.
248
+ const drawn = JSON.stringify([folder, [...agents].map(([id, a]) => [id, a.label])]);
249
+ if (drawn !== this.agentsDrawn) {
250
+ this.agentsDrawn = drawn;
251
+ cx.notify();
252
+ }
253
+ if (this.agentsAgain) this.lookAgents(cx);
254
+ });
255
+ }
256
+
257
+ // What `task log` says about each card's agent in the space, by card ID:
258
+ // only the cards that have something to say. When it cannot say, no card
259
+ // is marked.
260
+ async readAgents(space) {
261
+ const agents = new Map();
262
+ try {
263
+ const r = await this.ocnus(["task", "log", space, "--json"], { fallback: true });
264
+ const cards = r.code === 0 ? parse(r.stdout)?.cards : null;
265
+ for (const c of Array.isArray(cards) ? cards : []) {
266
+ if (c?.agent && typeof c.card === "string") agents.set(c.card.toUpperCase(), c.agent);
267
+ }
268
+ } catch {
269
+ // Nothing to mark.
270
+ }
271
+ return agents;
272
+ }
273
+
274
+ // The ocnus space whose folder is `folder`, and its open cards.
275
+ async read(folder) {
276
+ if (folder === null) return { space: null };
277
+ try {
278
+ const r = await this.ocnus(["space", "ls", "--json"], { fallback: true });
279
+ const listed = r.code === 0 ? parse(r.stdout)?.spaces : null;
280
+ if (!Array.isArray(listed)) return { error: r.code === 0 ? OLD_CLI : failure(r, "ocnus space ls") };
281
+ const space = listed.find((s) => s.folder === folder);
282
+ if (!space) return { space: null };
283
+ const c = await this.ocnus(["task", "ls", space.name, "--json"], { timeout: LIST_TIMEOUT_MS });
284
+ const list = c.code === 0 ? parse(c.stdout) : null;
285
+ if (Array.isArray(list?.statuses)) return { space: space.name, folder, statuses: list.statuses };
286
+ return { space: space.name, error: c.code === 0 ? OLD_CLI : failure(c, "ocnus task ls") };
287
+ } catch (e) {
288
+ return { error: `Could not run ocnus: ${e?.message ?? e}` };
289
+ }
290
+ }
291
+
292
+ // Runs `task new` for a card, one at a time.
293
+ start(space, card, cx) {
294
+ if (this.starting) return;
295
+ this.starting = card.id;
296
+ cx.notify();
297
+ cx.spawn(async (cx) => {
298
+ let failed = null;
299
+ try {
300
+ const r = await this.ocnus(["task", "new", space, card.id], { timeout: START_TIMEOUT_MS });
301
+ if (r.code !== 0) failed = said(r) || "ocnus task new failed";
302
+ } catch (e) {
303
+ failed = `Could not run ocnus: ${e?.message ?? e}`;
304
+ }
305
+ if (failed !== null) notify({ title: `Could not start ${card.id}`, body: failed, level: "error" });
306
+ this.starting = null;
307
+ cx.notify();
308
+ });
309
+ }
310
+
311
+ // Shows the card's tab, in its space.
312
+ show(open) {
313
+ if (!open.space.active) run("space.select", { id: open.space.id });
314
+ run("tab.select", { id: open.tab.id });
315
+ }
316
+
317
+ // What a click on a card does: show its tab, or start it.
318
+ act(space, card, open, cx) {
319
+ if (open) this.show(open);
320
+ else this.start(space, card, cx);
321
+ }
322
+
323
+ toggle(key, cx) {
324
+ if (this.closed.has(key)) this.closed.delete(key);
325
+ else this.closed.add(key);
326
+ localStorage.setItem("closed", JSON.stringify([...this.closed]));
327
+ cx.notify();
328
+ }
329
+
330
+ // A status's header row: its name and how many cards it has.
331
+ header(space, s, i, closed, t) {
332
+ const n = (s.cards ?? []).length;
333
+ return h_flex().id(`status-${i}`).h(26).flex_none().mt(i ? 4 : 0).px(8).gap(7).items_center().rounded(7)
334
+ .text_size(11).text_color(t.dim).cursor_pointer().hover((el) => el.bg(t.hover))
335
+ .on_click((_e, cx) => this.toggle(`${space}/${s.name}`, cx))
336
+ .child(div().flex_1().min_w(0).truncate().child(s.name))
337
+ .child(div().flex_none().child(String(n)))
338
+ .child(svg(`${CHEVRON}-${closed ? "right" : "down"}.svg`).size(11).flex_none());
339
+ }
340
+
341
+ // A card: its ID, its title and its button, with a mark before the button
342
+ // when its agent is stuck or has stopped. `open`: its tab, or null.
343
+ // `starting`: the card being started, if any. `agent`: what `task log`
344
+ // says about its agent, or null.
345
+ row(space, card, open, starting, agent, t) {
346
+ const on = Boolean(open?.space.active && open.tab.active);
347
+ const mine = starting === card.id;
348
+ const waiting = Boolean(starting) && !mine && !open; // another card is starting
349
+ const label = open ? "Open" : mine ? "Starting…" : "Start";
350
+ const tip = open ? `Show the tab of ${card.id}` : `Start ${card.id}: open its tab with the agent running`;
351
+ const button = h_flex().id(`card-go-${card.id}`).h(22).flex_none().px(8).gap(5).items_center().rounded(6)
352
+ .border_1().border_color(t.input).bg(t.raised).text_size(11.5).text_color(t.foreground)
353
+ .when(mine, (el) => el.text_color(t.muted)
354
+ .child(svg(SPINNER).size(10).flex_none().text_color(t.working).spin(900)))
355
+ .when(waiting, (el) => el.opacity(0.45))
356
+ .when(!mine && !waiting, (el) => el.cursor_pointer().tooltip(tip).hover((s) => s.bg(t.hover))
357
+ .on_click((_e, cx) => this.act(space, card, open, cx)))
358
+ .child(label);
359
+ return h_flex().id(`card-${card.id}`).h(ROW).flex_none().px(8).gap(8).items_center().rounded(8)
360
+ .when(on, (el) => el.bg(t.selected))
361
+ .when(!on, (el) => el.hover((s) => s.bg(t.hover)))
362
+ .child(div().flex_none().text_size(11.5).text_color(t.dim).child(card.id))
363
+ .child(div().id(`card-title-${card.id}`).flex_1().min_w(0).truncate().tooltip(card.title ?? "")
364
+ .text_color(open ? t.foreground : t.muted).child(card.title ?? ""))
365
+ .when(Boolean(agent), (el) => el.child(this.mark(card, agent, t)))
366
+ .child(button);
367
+ }
368
+
369
+ // What `task log` says a card's agent is doing, when it is stuck or has
370
+ // stopped: a mark, with its words in the tooltip.
371
+ mark(card, agent, t) {
372
+ return div().id(`card-agent-${card.id}`).size(14).flex_none().rounded(4).bg(t.blocked + "26")
373
+ .flex().items_center().justify_center().text_size(10.5).font_bold().text_color(t.blocked)
374
+ .tooltip(agent.label ?? "").child("!");
375
+ }
376
+
377
+ // On the sidebar's rail: the card's number, in the colour of the mark when
378
+ // its agent is stuck or has stopped, with what `task log` says in its
379
+ // tooltip. A click does what its button would.
380
+ compact(space, card, open, starting, agent, t) {
381
+ const on = Boolean(open?.space.active && open.tab.active);
382
+ const mine = starting === card.id;
383
+ const said = agent ? `${agent.label ?? ""}\n` : "";
384
+ const tip = `${card.id}: ${card.title ?? ""}\n${said}${open ? "Click to show its tab" : mine ? "Starting…" : "Click to start it"}`;
385
+ return h_flex().flex_none().justify_center().child(
386
+ h_flex().id(`card-${card.id}`).w(RAIL_CELL).h(26).rounded(6).items_center().justify_center()
387
+ .text_size(11).tooltip(tip)
388
+ .when(on, (el) => el.bg(t.selected))
389
+ .when(!on, (el) => el.hover((s) => s.bg(t.hover)))
390
+ .on_click((_e, cx) => this.act(space, card, open, cx))
391
+ .child(mine
392
+ ? svg(SPINNER).size(11).text_color(t.working).spin(900)
393
+ : div().text_color(agent ? t.blocked : open ? t.foreground : t.dim).child(card.id.split("-").pop())));
394
+ }
395
+
396
+ render() {
397
+ const t = theme();
398
+ const collapsed = narrow();
399
+ const sample = checking();
400
+ const folder = sample ? "sample" : shownFolder();
401
+ const got = sample ? SAMPLE : this.lists.get(folder) ?? null;
402
+ const closed = sample ? SAMPLE_CLOSED : this.closed;
403
+ const starting = sample ? SAMPLE_STARTING : this.starting;
404
+ const tabs = sample ? SAMPLE_TABS : got?.statuses ? cardTabs(got.folder) : new Map();
405
+ const agents = sample ? SAMPLE_AGENTS : this.agents.get(folder) ?? new Map();
406
+ const note = (text, color) =>
407
+ div().px(8).py(6).text_size(12).text_color(color).whitespace_normal().child(text);
408
+ const items = [];
409
+ if (!got) {
410
+ if (!collapsed) items.push(note(folder === null ? NOT_A_SPACE : "Loading…", t.dim));
411
+ } else if (got.error) {
412
+ // On the rail there is no room for the words: a mark, with them in its tooltip.
413
+ items.push(collapsed
414
+ ? h_flex().flex_none().justify_center().child(div().id("cards-error").w(RAIL_CELL).h(26)
415
+ .flex().items_center().justify_center().text_color(t.blocked).tooltip(got.error).child("!"))
416
+ : note(got.error, t.blocked));
417
+ } else if (!got.space) {
418
+ if (!collapsed) items.push(note(NOT_A_SPACE, t.dim));
419
+ } else {
420
+ got.statuses.forEach((s, i) => {
421
+ const shut = closed.has(`${got.space}/${s.name}`);
422
+ if (!collapsed) items.push(this.header(got.space, s, i, shut, t));
423
+ if (shut) return;
424
+ for (const card of s.cards ?? []) {
425
+ const open = tabs.get(card.id.toUpperCase()) ?? null;
426
+ const agent = agents.get(card.id.toUpperCase()) ?? null;
427
+ items.push(collapsed
428
+ ? this.compact(got.space, card, open, starting, agent, t)
429
+ : this.row(got.space, card, open, starting, agent, t));
430
+ }
431
+ });
432
+ }
433
+ const spin = !sample && this.busy;
434
+ return v_flex().size_full().min_h(0).bg(t.chrome).text_size(13)
435
+ .child(h_flex().h(31).flex_none().pl(16).pr(10).gap(6).items_center().justify_between()
436
+ .text_size(11.5).text_color(t.dim)
437
+ .when(collapsed, (el) => el.pl(0).pr(0).justify_center())
438
+ .when(!collapsed, (el) => el.child(div().flex_none().child("Cards"))
439
+ .child(div().flex_1().min_w(0).truncate().text_right().child(got?.space ?? "")))
440
+ .child(div().id("cards-refresh").size(26).flex_none().flex().items_center().justify_center()
441
+ .rounded(6).tooltip("Read the cards again")
442
+ .hover((s) => s.bg(t.hover).text_color(t.foreground))
443
+ .on_click((_e, cx) => this.refresh(cx))
444
+ .child(spin
445
+ ? svg("mods/cards/refresh.svg").size(13).spin(900)
446
+ : svg("mods/cards/refresh.svg").size(13))))
447
+ .child(v_flex().id("card-list").flex_1().min_h(0).px(collapsed ? 0 : 8).pb(14)
448
+ .overflow_y_scrollbar().mode("always").children(items));
449
+ }
450
+ }
@@ -0,0 +1 @@
1
+ { "place": "none", "title": "Cards" }
@@ -0,0 +1,4 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
2
+ <path d="M20 12a8 8 0 1 1-2.34-5.66"/>
3
+ <path d="M18 2.5v4h-4"/>
4
+ </svg>
@@ -0,0 +1,90 @@
1
+ // Live CPU usage, drawn as a scrolling history like Activity Monitor's.
2
+ // macOS only: it reads the "CPU usage" line that /usr/bin/top prints.
3
+ import { View, div } from "gpui-kit";
4
+ import { h_flex, v_flex } from "gpui-base";
5
+ import { exec, theme, window, checking, settings } from "herdr";
6
+
7
+ const SAMPLE_MS = 2000; // one reading every two seconds
8
+ const BAR = 3; // px per reading
9
+ const GAP = 1;
10
+ const KEEP = 600; // readings kept, enough for a very wide panel
11
+
12
+ export default class Cpu extends View {
13
+ init(_props, cx) {
14
+ this.samples = [];
15
+ this.busy = false;
16
+ this.error = null;
17
+ this.graph = { width: 240, height: 40 };
18
+ if (checking()) return;
19
+ cx.timer.every(SAMPLE_MS, (cx) => this.sample(cx));
20
+ this.sample(cx);
21
+ }
22
+
23
+ sample(cx) {
24
+ // Skip while a reading is in flight or nobody can see the window.
25
+ if (this.busy || window()?.visible === false) return;
26
+ this.busy = true;
27
+ cx.spawn(async (cx) => {
28
+ try {
29
+ // top's first sample averages since boot; the second covers the last second.
30
+ const r = await exec("/usr/bin/top", ["-l", "2", "-n", "0", "-s", "1"], { timeout: 10000 });
31
+ const line = r.stdout.split("\n").filter((l) => l.startsWith("CPU usage:")).at(-1);
32
+ const m = line?.match(/([\d.]+)% user, ([\d.]+)% sys/);
33
+ if (m) {
34
+ this.samples.push({ user: Number(m[1]), sys: Number(m[2]) });
35
+ if (this.samples.length > KEEP) this.samples.shift();
36
+ this.error = null;
37
+ } else {
38
+ this.error = "no CPU reading from top";
39
+ }
40
+ } catch (e) {
41
+ this.error = String(e?.message ?? e);
42
+ }
43
+ this.busy = false;
44
+ cx.notify();
45
+ });
46
+ }
47
+
48
+ render() {
49
+ const t = theme();
50
+ const userColor = t.ansi4;
51
+ const sysColor = t.ansi1;
52
+ const last = this.samples.at(-1);
53
+ const total = last ? Math.round(last.user + last.sys) : null;
54
+
55
+ const fit = Math.max(1, Math.floor(this.graph.width / (BAR + GAP)));
56
+ const shown = this.samples.slice(-fit);
57
+ const h = Math.max(1, this.graph.height);
58
+ const px = (pct) => Math.round((Math.min(100, pct) / 100) * h);
59
+
60
+ const legend = (color, text) =>
61
+ h_flex().gap(5).items_center().child(div().size(6).rounded(3).bg(color)).child(text);
62
+
63
+ const readout = v_flex().flex_none().w(132).gap(2).justify_center()
64
+ .child(h_flex().gap(8).items_end()
65
+ .child(div().text_size(11).text_color(t.muted).child("CPU"))
66
+ .child(div().text_size(20).font_semibold().text_color(t.foreground).child(total === null ? "–" : `${total}%`)))
67
+ .child(this.error
68
+ ? div().text_size(11).text_color(t.blocked).truncate().child(this.error)
69
+ : h_flex().gap(10).text_size(11).text_color(t.muted)
70
+ .child(legend(userColor, last ? `user ${Math.round(last.user)}%` : "user"))
71
+ .child(legend(sysColor, last ? `sys ${Math.round(last.sys)}%` : "sys")));
72
+
73
+ const bars = h_flex().id("cpu-graph").flex_1().h_full().min_w(0).overflow_hidden()
74
+ .items_end().justify_end().gap(GAP).rounded(4).bg(t.surface)
75
+ .on_size_change((size, cx) => {
76
+ const zoom = Number(settings().zoom) || 1;
77
+ this.graph = { width: size.width / zoom, height: size.height / zoom };
78
+ cx.notify();
79
+ })
80
+ .children(shown.map((s) =>
81
+ v_flex().flex_none().w(BAR)
82
+ .child(div().h(px(s.sys)).bg(sysColor))
83
+ .child(div().h(px(s.user)).bg(userColor))));
84
+
85
+ return h_flex().size_full().px(14).py(8).gap(12).items_center()
86
+ .bg(t.chrome).border_t(1).border_color(t.border)
87
+ .child(readout)
88
+ .child(bars);
89
+ }
90
+ }
@@ -0,0 +1 @@
1
+ { "place": "bottom", "size": 64 }
@@ -0,0 +1,48 @@
1
+ // A dropdown that shows the current layout and switches to another.
2
+ // One mod, two faces: the button a layout places, and the popup list it opens
3
+ // (the same mod again, with props { menu: true }).
4
+ import { View, div } from "gpui-kit";
5
+ import { v_flex, h_flex } from "gpui-base";
6
+ import { layouts, shared, run, theme, props, openOverlay, closeOverlay } from "herdr";
7
+
8
+ const ROW = 28;
9
+
10
+ export default class LayoutSwitch extends View {
11
+ render() {
12
+ return props()?.menu ? this.menu() : this.button();
13
+ }
14
+
15
+ button() {
16
+ const t = theme();
17
+ const current = shared("layout") ?? "main";
18
+ return h_flex().size_full().px(12).items_center().bg(t.chrome).child(
19
+ h_flex().id("layout-switch").flex_1().min_w(0).h(ROW).px(10).gap(8).items_center()
20
+ .rounded(6).border(1).border_color(t.border).bg(t.surface)
21
+ .text_size(12).hover((s) => s.bg(t.hover))
22
+ .on_click(() =>
23
+ openOverlay({
24
+ mod: "layout-switch", kind: "popup", width: 220,
25
+ height: layouts().length * ROW + 10, props: { menu: true },
26
+ }))
27
+ .child(div().flex_none().text_color(t.muted).child("Layout"))
28
+ .child(div().flex_1().min_w(0).truncate().text_color(t.foreground).child(String(current)))
29
+ .child(div().flex_none().text_color(t.dim).child("▾")));
30
+ }
31
+
32
+ menu() {
33
+ const t = theme();
34
+ const current = shared("layout") ?? "main";
35
+ const pick = (name) => {
36
+ closeOverlay(props().overlay);
37
+ if (name !== current) run("layout.switch", { name });
38
+ };
39
+ return v_flex().size_full().p(4).bg(t.popover).text_size(12).children(
40
+ layouts().map((name) =>
41
+ h_flex().id(`layout-${name}`).h(ROW).px(8).gap(8).items_center().rounded(4)
42
+ .hover((s) => s.bg(t.hover))
43
+ .on_click(() => pick(name))
44
+ .child(div().w(12).flex_none().text_color(t.accent).child(name === current ? "✓" : ""))
45
+ .child(div().flex_1().min_w(0).truncate()
46
+ .text_color(name === current ? t.foreground : t.muted).child(name))));
47
+ }
48
+ }
@@ -0,0 +1 @@
1
+ { "place": "none" }
@@ -0,0 +1,69 @@
1
+ // Gives the spaces and tabs the Herdr app took in from herdr-core the names
2
+ // ocnus gave them. It draws nothing: a layout places it only so that it
3
+ // runs.
4
+ //
5
+ // An agent that `ocnus task new` or `task start` started while the app
6
+ // was closed runs in a herdr-core terminal with Herdr's own labels,
7
+ // herdr.space and herdr.tab, so the app shows it as a tab once it opens, and
8
+ // with ocnus's, among them ocnus.space, the space's name, and
9
+ // ocnus.tab, the tab's. The app makes a space or tab of an id it does not
10
+ // know under its own names: a space after where its first terminal is, a tab
11
+ // after its place. This mod renames each such space and tab to ocnus's
12
+ // name for it.
13
+ //
14
+ // It names only what nobody has named (`named` in spaces()), so a name the
15
+ // person gives stays, and it asks for each name once while it runs, so a
16
+ // space or tab given Herdr's own name again keeps it. It holds no rules of
17
+ // ocnus's: the names are the labels ocnus put on the terminals.
18
+ import { View, div } from "gpui-kit";
19
+ import { checking, run, spaces, terminals } from "herdr";
20
+
21
+ // A label's value, when it is a name: a string that is not empty.
22
+ function label(labels, key) {
23
+ const value = labels?.[key];
24
+ return typeof value === "string" && value !== "" ? value : null;
25
+ }
26
+
27
+ // What to rename: [command, id, name] for each space and tab on this
28
+ // computer that a terminal labelled by ocnus is in and nobody has named.
29
+ function unnamed() {
30
+ const spaceOf = new Map();
31
+ const tabOf = new Map();
32
+ for (const space of spaces()) {
33
+ if (space.machine) continue;
34
+ spaceOf.set(space.id, space);
35
+ for (const tab of space.tabs) tabOf.set(tab.id, tab);
36
+ }
37
+ const found = [];
38
+ for (const terminal of terminals()) {
39
+ const labels = terminal.labels;
40
+ const space = spaceOf.get(labels?.["herdr.space"]);
41
+ const tab = tabOf.get(labels?.["herdr.tab"]);
42
+ const spaceName = label(labels, "ocnus.space");
43
+ const tabName = label(labels, "ocnus.tab");
44
+ if (tab && !tab.named && tabName) found.push(["tab.rename", tab.id, tabName]);
45
+ if (space && !space.named && spaceName) found.push(["space.rename", space.id, spaceName]);
46
+ }
47
+ return found;
48
+ }
49
+
50
+ export default class Names extends View {
51
+ init() {
52
+ // The spaces and tabs this copy has asked to rename, by command and id.
53
+ this.asked = new Set();
54
+ }
55
+
56
+ // Herdr renders this again whenever its spaces or terminals change, so a
57
+ // terminal it takes in is named as soon as it shows.
58
+ render() {
59
+ if (!checking()) {
60
+ for (const [command, id, name] of unnamed()) {
61
+ const key = `${command} ${id}`;
62
+ if (this.asked.has(key)) continue;
63
+ this.asked.add(key);
64
+ run(command, { id, name });
65
+ }
66
+ }
67
+ return div();
68
+ }
69
+ }
@@ -0,0 +1 @@
1
+ { "place": "none" }