@jjanczur/tyran 0.1.31 → 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. */
@@ -570,14 +607,14 @@ function handleSettingsWrite(dir, route, body) {
570
607
 
571
608
  // ------------------------------------------------------------------- CLI
572
609
 
573
- const BOOLEAN_FLAGS = ['check', 'serve', 'write'];
610
+ const BOOLEAN_FLAGS = ['check', 'serve', 'write', 'open'];
574
611
  const VALUE_FLAGS = ['dir', 'port'];
575
612
  // Repeatable, unlike VALUE_FLAGS: sources are the union over every directory
576
613
  // given, exactly like cost.mjs's own `--transcripts`.
577
614
  const REPEATABLE_FLAGS = ['transcripts'];
578
615
 
579
616
  function parseArgs(argv) {
580
- 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: [] };
581
618
  for (let i = 0; i < argv.length; i += 1) {
582
619
  const arg = argv[i];
583
620
  if (!arg.startsWith('--')) throw new UsageError(`unexpected argument "${arg}"`);
@@ -618,6 +655,13 @@ function main() {
618
655
  console.error('board: --write only means anything with --serve (it is what the served page may edit)');
619
656
  process.exit(2);
620
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
+ }
621
665
  if (flags.transcripts.length > 0 && !flags.serve) {
622
666
  console.error(
623
667
  'board: --transcripts only means anything with --serve (it is what /cost.json reads instead of ' +
@@ -743,14 +787,20 @@ function main() {
743
787
  // journal disagree and break the byte-exact `--check` contract. The
744
788
  // page fetches this route and simply omits the section when the fetch
745
789
  // fails, which is what happens over file://.
746
- if (req.url === '/cost.json') {
790
+ if (req.url === '/cost.json' || req.url.startsWith('/cost.json?')) {
747
791
  let body;
748
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';
749
799
  // `flags.transcripts` is [] unless the operator started this
750
800
  // server with `--transcripts`; costReport falls through to
751
801
  // `spend.transcript_dirs` and then the derived directory on its
752
802
  // own, so an empty array here changes nothing for the common case.
753
- body = costJson(costReport({ tyranDir: dir, transcriptDirs: flags.transcripts }));
803
+ body = costJson(costReport({ tyranDir: dir, transcriptDirs: flags.transcripts, window }));
754
804
  } catch (err) {
755
805
  // A cost failure must never take the board down with it: the
756
806
  // board answers "what is going on", and that answer does not
@@ -796,10 +846,12 @@ function main() {
796
846
  process.exitCode = 2;
797
847
  });
798
848
  server.listen(flags.port, '127.0.0.1', () => {
849
+ const url = `http://127.0.0.1:${flags.port}/`;
799
850
  console.log(
800
- `board: serving http://127.0.0.1:${flags.port}/ ` +
851
+ `board: serving ${url} ` +
801
852
  `(${flags.write ? 'Settings can edit config.yaml and autonomy.yaml' : 'read-only'}; Ctrl-C to stop)`,
802
853
  );
854
+ if (flags.open) openInBrowser(url);
803
855
  });
804
856
  return;
805
857
  }