@jjanczur/tyran 0.1.30 → 0.1.32

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.
package/scripts/board.mjs CHANGED
@@ -21,11 +21,12 @@
21
21
  *
22
22
  * CLI:
23
23
  * node board.mjs [--dir <.tyran>] [--check]
24
- * [--serve [--port <n>] [--write] [--transcripts <dir>]...]
24
+ * [--serve [--port <n>] [--write] [--open] [--transcripts <dir>]...]
25
25
  * Exit: 0 ok · 1 drift (--check) · 2 usage/IO
26
26
  */
27
27
  import { existsSync, readdirSync, statSync, readFileSync } from 'node:fs';
28
28
  import { createServer } from 'node:http';
29
+ import { spawn as spawnProcess } from 'node:child_process';
29
30
  import { basename, join, resolve } from 'node:path';
30
31
  import { realpathSync } from 'node:fs';
31
32
  import { fileURLToPath } from 'node:url';
@@ -44,7 +45,7 @@ import {
44
45
  } from './project.mjs';
45
46
  import { jsonEscapeInvisible } from './invisible.mjs';
46
47
  import { renderBoardError, renderBoardHtml } from './board-html.mjs';
47
- import { COST_SCHEMA, costJson, costReport } from './cost.mjs';
48
+ import { COST_SCHEMA, WINDOWS, costJson, costReport } from './cost.mjs';
48
49
  import { SettingsError, applyPolicyClass, applySetting, readSettings } from './settings.mjs';
49
50
  import { checkStopAt } from './stop-check.mjs';
50
51
  import { runState } from './overnight.mjs';
@@ -52,6 +53,42 @@ import { answerOne, answerProblem, classify, openAsks, partitionAsks, reRender,
52
53
 
53
54
  export const BOARD_HTML_FILE = 'board.html';
54
55
 
56
+ /**
57
+ * Open the served board in the operator's browser.
58
+ *
59
+ * The board was a URL printed to a terminal that nothing ever opened, and for
60
+ * the people this matters most to that is several steps — keep this window
61
+ * running, now type that address somewhere else — each one a place to stop.
62
+ * The dashboard is where Tyran becomes legible, and it was the one thing
63
+ * installation never actually reached.
64
+ *
65
+ * Best-effort and never fatal: a failure here leaves the server running and
66
+ * the URL printed, which is exactly where things were before. Headless
67
+ * machines, SSH sessions and locked-down desktops all land in that branch and
68
+ * none of them is an error worth taking a working server down for.
69
+ *
70
+ * The argument vector is fixed and the URL is the only variable — it is built
71
+ * from a port this process is already listening on, never from user text —
72
+ * and `shell: false` (the default) means nothing here reaches a shell.
73
+ */
74
+ export function openInBrowser(url, { platform = process.platform, spawn = spawnProcess } = {}) {
75
+ const [command, args] =
76
+ platform === 'darwin' ? ['open', [url]]
77
+ : platform === 'win32' ? ['cmd', ['/c', 'start', '', url]]
78
+ : ['xdg-open', [url]];
79
+ try {
80
+ const child = spawn(command, args, { stdio: 'ignore', detached: true });
81
+ // A missing `open`/`xdg-open` arrives as an async error event, not a
82
+ // throw, so the catch below would never see it — and an unhandled one
83
+ // would take the server down, which is the opposite of best-effort.
84
+ child.on?.('error', () => { /* no browser here; the printed URL still works */ });
85
+ child.unref?.();
86
+ return true;
87
+ } catch {
88
+ return false;
89
+ }
90
+ }
91
+
55
92
  /** Same ceiling as the evidence gate's journal locator — a deliberate
56
93
  * duplicate: scripts must not import from hook files (direction is
57
94
  * hooks -> scripts), and the constant is one number with one meaning. */
@@ -263,6 +300,12 @@ export function crossBoard({ initiatives, errors, warned = [], stop = null }) {
263
300
  totals: {
264
301
  initiatives: initiatives.length,
265
302
  agents: agents.length,
303
+ // Of those, how many the journal has moved on without. `agents` counts
304
+ // OPEN spawns, which is what it has always counted and what the strip
305
+ // lists — but presenting that number as "running" made a week-old ghost
306
+ // indistinguishable from an agent working right now, and an overnight
307
+ // operator reads this tile before anything else on the page.
308
+ agents_stale: agents.filter((a) => a.stale === true).length,
266
309
  merged,
267
310
  tickets,
268
311
  percent: tickets === 0 ? 0 : Math.round((merged / tickets) * 100),
@@ -306,8 +349,12 @@ export function crossJson(payload) {
306
349
  export function renderCrossMd(payload) {
307
350
  const parts = [GENERATED_MD_HEADER, '# BOARD — all initiatives\n\n'];
308
351
  const t = payload.totals;
352
+ // "open", not "running": the headline used to assert that every open spawn
353
+ // was live work, and the stale ones are named right after it rather than
354
+ // being quietly dropped from the count (ADR-19 correction 1).
309
355
  parts.push(
310
- `**${t.agents} agent(s) running across ${t.initiatives} initiative(s) · ` +
356
+ `**${t.agents} agent(s) open across ${t.initiatives} initiative(s)` +
357
+ `${t.agents_stale > 0 ? `, ${t.agents_stale} stale` : ''} · ` +
311
358
  `${t.merged}/${t.tickets} tickets merged (${t.percent}%) · as of ${inline(payload.as_of)}**\n`,
312
359
  );
313
360
  // First, and above the pause: a STOP outranks everything. It is the one
@@ -560,14 +607,14 @@ function handleSettingsWrite(dir, route, body) {
560
607
 
561
608
  // ------------------------------------------------------------------- CLI
562
609
 
563
- const BOOLEAN_FLAGS = ['check', 'serve', 'write'];
610
+ const BOOLEAN_FLAGS = ['check', 'serve', 'write', 'open'];
564
611
  const VALUE_FLAGS = ['dir', 'port'];
565
612
  // Repeatable, unlike VALUE_FLAGS: sources are the union over every directory
566
613
  // given, exactly like cost.mjs's own `--transcripts`.
567
614
  const REPEATABLE_FLAGS = ['transcripts'];
568
615
 
569
616
  function parseArgs(argv) {
570
- const flags = { dir: '.tyran', port: 4173, check: false, serve: false, write: false, transcripts: [] };
617
+ const flags = { dir: '.tyran', port: 4173, check: false, serve: false, write: false, open: false, transcripts: [] };
571
618
  for (let i = 0; i < argv.length; i += 1) {
572
619
  const arg = argv[i];
573
620
  if (!arg.startsWith('--')) throw new UsageError(`unexpected argument "${arg}"`);
@@ -608,6 +655,13 @@ function main() {
608
655
  console.error('board: --write only means anything with --serve (it is what the served page may edit)');
609
656
  process.exit(2);
610
657
  }
658
+ if (flags.open && !flags.serve) {
659
+ // Same refusal as --write and --transcripts, for the same reason: there is
660
+ // no URL to open without a server, and a flag that silently does nothing
661
+ // reads as configuration.
662
+ console.error('board: --open only means anything with --serve (there is no URL to open without it)');
663
+ process.exit(2);
664
+ }
611
665
  if (flags.transcripts.length > 0 && !flags.serve) {
612
666
  console.error(
613
667
  'board: --transcripts only means anything with --serve (it is what /cost.json reads instead of ' +
@@ -733,14 +787,20 @@ function main() {
733
787
  // journal disagree and break the byte-exact `--check` contract. The
734
788
  // page fetches this route and simply omits the section when the fetch
735
789
  // fails, which is what happens over file://.
736
- if (req.url === '/cost.json') {
790
+ if (req.url === '/cost.json' || req.url.startsWith('/cost.json?')) {
737
791
  let body;
738
792
  try {
793
+ // The window is the ONLY query parameter, and it is validated
794
+ // against a closed list rather than passed through — an unknown
795
+ // value falls back to `all` instead of reaching the report, so a
796
+ // hand-typed URL cannot produce a window nobody defined.
797
+ const asked = new URLSearchParams(req.url.slice(req.url.indexOf('?') + 1)).get('window');
798
+ const window = WINDOWS.includes(asked) ? asked : 'all';
739
799
  // `flags.transcripts` is [] unless the operator started this
740
800
  // server with `--transcripts`; costReport falls through to
741
801
  // `spend.transcript_dirs` and then the derived directory on its
742
802
  // own, so an empty array here changes nothing for the common case.
743
- body = costJson(costReport({ tyranDir: dir, transcriptDirs: flags.transcripts }));
803
+ body = costJson(costReport({ tyranDir: dir, transcriptDirs: flags.transcripts, window }));
744
804
  } catch (err) {
745
805
  // A cost failure must never take the board down with it: the
746
806
  // board answers "what is going on", and that answer does not
@@ -786,10 +846,12 @@ function main() {
786
846
  process.exitCode = 2;
787
847
  });
788
848
  server.listen(flags.port, '127.0.0.1', () => {
849
+ const url = `http://127.0.0.1:${flags.port}/`;
789
850
  console.log(
790
- `board: serving http://127.0.0.1:${flags.port}/ ` +
851
+ `board: serving ${url} ` +
791
852
  `(${flags.write ? 'Settings can edit config.yaml and autonomy.yaml' : 'read-only'}; Ctrl-C to stop)`,
792
853
  );
854
+ if (flags.open) openInBrowser(url);
793
855
  });
794
856
  return;
795
857
  }