hive-view
aien-dev/hive-view
Brought by drakestapleton
hive-view / README.md
| 1 | # hive-view |
| 2 | |
| 3 | One browser page that shows every hive running on the Spark and lets Drake type into any of them. |
| 4 | It is a native program written in seeded Omega (`.om`), built by the pinned `omc` compiler from |
| 5 | [`aien-dev/omega-mv`](https://github.com/aien-dev/omega-mv). The browser runs no script at all: every |
| 6 | page is HTML drawn by the server, and each panel refreshes itself. |
| 7 | |
| 8 | Parent: `aien-dev/omega-mv` @ `943d955531b9d4d9f70deb41756c0b46441dd5e0` (compiler `seed/out/omc`, |
| 9 | sha256 `5f90edea61bac241398df5036990d899361a5acd9decb37954ce088ec0f6f6df`). |
| 10 | |
| 11 | ## Using it |
| 12 | |
| 13 | - Address: `http://spark.hub.internal:18190/` (Tailscale) or `http://127.0.0.1:18190/` on the Spark. |
| 14 | - Sign in once per browser with the one-time link `http://spark.hub.internal:18190/?t=<token>`, where |
| 15 | `<token>` is the 64 characters in `~/.config/hive-view/token` on the Spark. The link sets a cookie and |
| 16 | sends you to the dashboard. Without that cookie every page answers "Not signed in". |
| 17 | - The page shows, top to bottom: |
| 18 | - the queen (`omp-queen`): its live screen and an input box; |
| 19 | - one tile per `omp` session (found from `tmux ls` each poll, never a fixed list): title, status line |
| 20 | and last screenful. Clicking a tile opens `/s/NAME`: that session large, with its own input box; |
| 21 | - Hermes: gateway up or down, the queen PAUSE file (shown verbatim), kanban counts per status and |
| 22 | the running cards; the `lead-*` sessions as tiles; |
| 23 | - Machine: load, memory, CPU busy %, GPU power and utilization from the spark-sensors log, the |
| 24 | processes holding the GPU, the spark-window holder and queue, the core pool locks, and the Pearl |
| 25 | ledger (newest entries verbatim); |
| 26 | - the BOARD (newest first, with a lane and text filter), the latest QUESTIONS and DECISIONS entries, |
| 27 | CLAIMS, and every other tmux session whose name uses only letters, digits, `-` and `_` (others are |
| 28 | skipped, since they could not be addressed safely). |
| 29 | - Freshness: session screens refresh every 2 to 3 seconds, the rest every 10 seconds. Every panel |
| 30 | prints the age of its data and turns amber ("stale") or red ("down") when the poller stopped |
| 31 | updating it. A panel the browser stops refreshing shows a red "NOT LIVE" bar after a few seconds; |
| 32 | when the server is down each panel shows the browser's error page. Cached data is never shown as live. |
| 33 | |
| 34 | ### Typing into a session |
| 35 | |
| 36 | The input box sends exactly what you typed, then Enter ("Send + Enter"). The "Enter" and "Escape" |
| 37 | buttons send that one key only; any text in the box is not typed. Nothing else can be sent: no other |
| 38 | control keys, no commands. |
| 39 | |
| 40 | - Text is one line of printable UTF-8, at most 4096 bytes. Control characters (including tab, newline, |
| 41 | carriage return, Escape and DEL) and invalid UTF-8 are refused before anything is typed. |
| 42 | - The session must exist in a live `tmux ls` at the moment of sending, and its name may only contain |
| 43 | letters, digits, `-` and `_`. The text goes in with `tmux send-keys -l` (literal), in chunks of at |
| 44 | most 2048 bytes; a chunk that ends in `;` is escaped so tmux does not treat it as a command separator. |
| 45 | - After a send the page shows a green "Sent N bytes to NAME" or a red reason. |
| 46 | - Every send is logged to `~/.local/state/hive-view/sends.log`: UTC time, session, action, byte |
| 47 | count, sha256 of the text and the result. The text itself is never written anywhere. |
| 48 | - Before anything is typed, the log is opened and an `attempt` line is written in full; if either |
| 49 | fails (directory missing, disk full, write error) the send is refused with code 8 and nothing is |
| 50 | typed. After typing, a second line records the outcome (`ok` or `tmux-failed`). Bad text and a |
| 51 | missing session are refused after the log is opened and logged as one line. A malformed request, |
| 52 | a bad session name or action, and text over 4096 bytes are refused before the log is opened and |
| 53 | are not logged (nothing about them reaches tmux). |
| 54 | |
| 55 | ## Security |
| 56 | |
| 57 | - The service listens only on `127.0.0.1:18190` and `100.64.0.2:18190` (the Spark's Tailscale |
| 58 | address): the unit passes exactly those two. The binary itself accepts only loopback `127.0.0.1` and |
| 59 | addresses in Tailscale's `100.64.0.0/10` range (exit 4 for anything else). The Host header must be |
| 60 | one of `127.0.0.1:18190`, `100.64.0.2:18190`, `localhost:18190`, `spark.hub.internal:18190`, |
| 61 | `spark:18190`. |
| 62 | - Every page except `/health`, `/app.css` and `/palette.css` (static stylesheets, no hive data) needs |
| 63 | the cookie `hv=<token>`; the token file must be mode 600 and owned by the user, or the server refuses |
| 64 | to start (exit 3). |
| 65 | - `POST /send` additionally needs an `Origin` header equal to the page's own origin and a form key |
| 66 | (sha256 of a fixed label and the token) that only the dashboard's own pages carry. Bodies over |
| 67 | 16 KiB are refused with 413. |
| 68 | - Responses carry `Content-Security-Policy: default-src 'none'; style-src 'self'; img-src 'self'; |
| 69 | frame-src 'self'; form-action 'self'; frame-ancestors 'self'; base-uri 'none'` (no script can run), |
| 70 | `X-Frame-Options: SAMEORIGIN`, `Referrer-Policy: same-origin`, `Cache-Control: no-store`. |
| 71 | - Session screens are rendered with every `& < > " '` escaped; terminal colours become CSS classes. |
| 72 | - The machine panel names the processes that hold the GPU or a pool lock with their command line |
| 73 | (first 200 bytes). Any argument containing `key`, `token`, `secret`, `passw`, `auth` or `cred` |
| 74 | (any case) is shown as `***` (after its `=` when it has one), and so is the argument that follows |
| 75 | such a flag. |
| 76 | |
| 77 | ## Load on the machine |
| 78 | |
| 79 | One shared poller (nice 10, cores 0 to 2) refreshes everything, whatever the number of open pages. |
| 80 | It never queries the GPU (GPU numbers come from the existing spark-sensors log) and takes no pool |
| 81 | lock. It captures up to six panes per `tmux` call and skips panes whose activity time did not change; |
| 82 | it runs `hermes kanban list` only when the kanban database file changes (or every 5 minutes). |
| 83 | |
| 84 | Measured on the Spark (`gate cpu`, 60 s, USER_HZ 100, two dashboard pages open). Runs 1 and 2 are |
| 85 | earlier builds of the same poller (server `9f70b61b…` and `8e4ca0a1…`); run 3 is server `12672fd8…` |
| 86 | (deployed; the poller is unchanged in the current build): |
| 87 | |
| 88 | | What | Ticks in 60 s | Share of one core | Share of the 20-core machine | |
| 89 | |---|---|---|---| |
| 90 | | hive-view (server, poller, handlers, their tmux clients), run 1 | 182 | 3.03 % | 0.15 % | |
| 91 | | hive-view, run 2 | 76 | 1.27 % | 0.063 % | |
| 92 | | hive-view, run 3 (server `12672fd8…`) | 171 | 2.85 % | 0.14 % | |
| 93 | | tmux server with hive-view running (runs 1, 2, 3) | 292, 255, 305 | | | |
| 94 | | tmux server with hive-view stopped (baseline) | 269 | | | |
| 95 | |
| 96 | The tmux-server difference is within the run-to-run noise of the busy hive. Percentages computed |
| 97 | with Wolfram (`N[182/(60*100)*100] -> 3.03`, `N[76/(60*100)*100] -> 1.27`, |
| 98 | `N[171/(60*100)*100] -> 2.85`); the arithmetic and rounding are proven in Lean 4 in |
| 99 | `evidence/cpu-shares.lean` (toolchain and Mathlib pin in its header); details in `evidence/`. |
| 100 | |
| 101 | ## Build |
| 102 | |
| 103 | The build is one `omc` pass over the concatenated sources (the order matters: a name must be |
| 104 | defined before it is used). With `O` pointing at an `aien-dev/omega-mv` checkout at the pinned commit: |
| 105 | |
| 106 | ```sh |
| 107 | { printf 'om2\n'; cat $O/seed/net/http.om $O/seed/net/notls.om $O/om/tls/sha2.om \ |
| 108 | src/util.om src/run.om src/poll.om src/web.om src/page.om src/route.om src/main.om; } \ |
| 109 | | $O/seed/out/omc > server && chmod +x server |
| 110 | |
| 111 | { printf 'om2\n'; cat $O/seed/net/http.om $O/seed/net/notls.om $O/om/tls/sha2.om \ |
| 112 | src/util.om src/run.om tests/gate.om; } \ |
| 113 | | $O/seed/out/omc > gate && chmod +x gate |
| 114 | ``` |
| 115 | |
| 116 | `build.json` records the compiler, its sha256, the sha256 of each concatenated source and of the |
| 117 | resulting binaries for the committed revision. |
| 118 | |
| 119 | ## Install, start, stop, roll back |
| 120 | |
| 121 | ```sh |
| 122 | install -d -m 700 ~/.local/share/hive-view/web ~/.local/state/hive-view |
| 123 | install -m 755 server ~/.local/share/hive-view/server |
| 124 | install -m 644 web/app.css ~/.local/share/hive-view/web/app.css |
| 125 | ~/.local/share/hive-view/server mktoken # only if ~/.config/hive-view/token does not exist |
| 126 | install -m 644 deploy/hive-view.service ~/.config/systemd/user/hive-view.service |
| 127 | systemctl --user daemon-reload |
| 128 | systemctl --user enable --now hive-view.service |
| 129 | curl -s http://127.0.0.1:18190/health # prints ok |
| 130 | ``` |
| 131 | |
| 132 | - Stop: `systemctl --user stop hive-view.service` (nothing else depends on it; the hives keep running). |
| 133 | - Restart after an upgrade: install the new `server` and `app.css`, then |
| 134 | `systemctl --user restart hive-view.service`. |
| 135 | - Roll back: keep the previous binary as `~/.local/share/hive-view/server.prev`; to roll back, |
| 136 | `install -m 755 ~/.local/share/hive-view/server.prev ~/.local/share/hive-view/server` and restart. |
| 137 | - Remove: `systemctl --user disable --now hive-view.service`, delete the unit file and |
| 138 | `~/.local/share/hive-view`. The token and the send log stay unless deleted by hand. |
| 139 | - New token (signs every browser out): stop the service, delete `~/.config/hive-view/token`, run |
| 140 | `server mktoken`, start the service. |
| 141 | |
| 142 | The unit runs with `Nice=10`, `CPUAffinity=0 1 2`, `Restart=on-failure`, `UMask=0077` and |
| 143 | `NoNewPrivileges=yes`; its run directory is `$XDG_RUNTIME_DIR/hive-view` and `StateDirectory=hive-view` |
| 144 | creates `~/.local/state/hive-view` (mode 700) for the send log. |
| 145 | |
| 146 | ## The gate |
| 147 | |
| 148 | `tests/gate.om` is the acceptance gate, in Omega. Against a running server: |
| 149 | |
| 150 | ```sh |
| 151 | ./gate run 18190 # 56 checks; prints PASS/FAIL per check, exit 0 only when all pass |
| 152 | ./gate nolog 18190 # against a server whose send log cannot be used (run the gate with the |
| 153 | # server's HOME): a send must be refused with code 8, nothing typed. Two |
| 154 | # setups: no ~/.local/state/hive-view at all, and sends.log -> /dev/full |
| 155 | # (opens, every write fails with ENOSPC) |
| 156 | ./gate cpu PID 60 # CPU ticks of server PID (with children) and of the tmux server over 60 s |
| 157 | ``` |
| 158 | |
| 159 | `gate run` creates a tmux session `hv-test` whose program is `/usr/bin/tee -a /tmp/hv-gate-sink` on a |
| 160 | raw terminal, so every byte the dashboard types is recorded exactly. It checks sign-in, Host and |
| 161 | Origin checks, the form key, literal delivery of tricky texts (`;`, `\;`, `-n`, `--`, `$(id)`, quotes, |
| 162 | UTF-8 and emoji, `<script>`, leading spaces, a 4096-byte text with `;` at both chunk ends, a 4096-byte |
| 163 | text of 3-byte characters with one split by the chunk edge), Enter and Escape (also with text in the |
| 164 | box, which must not be typed), the send log (sha256 and byte count present, text absent), every |
| 165 | refusal (4097 bytes, control bytes, invalid and overlong UTF-8, unknown action, forged names, |
| 166 | duplicate or unknown fields, a body over 16 KiB), that `<script>` on a session screen is shown |
| 167 | escaped, that the QUESTIONS and DECISIONS panels answer with the whole page (with a fresh HOME this |
| 168 | covers a missing file), and that a deleted session is refused. It deletes `hv-test` and the sink at |
| 169 | the end. |
| 170 | |
| 171 | Negative control: a server built without the `;` escape in `vwchunk` (src/web.om) fails 6 of the 56 |
| 172 | checks (every case that ends a chunk in `;`). Results for the committed revision are in `evidence/`. |
| 173 | |
| 174 | ## Outside programs |
| 175 | |
| 176 | hive-view is pure Omega; at run time it starts these programs with exact argument lists (no shell): |
| 177 | |
| 178 | | Program | Version on the Spark | What hive-view uses it for | Omega replacement | |
| 179 | |---|---|---|---| |
| 180 | | `/usr/bin/tmux` | 3.4 | list sessions, capture panes, `send-keys -l` | none needed for observing the hive's own tmux; a persistent Omega client of tmux control mode (`tmux -C`) would remove the per-poll process start | |
| 181 | | `~/.local/bin/hermes` | Hermes Agent v0.22.5 | `hermes kanban list` for the kanban counts and running cards | read `kanban.db` directly with an Omega SQLite page reader (proposal, not built) | |
| 182 | |
| 183 | The service manager is outside hive-view too: `deploy/hive-view.service` is a systemd user unit |
| 184 | (systemd 255 on the Spark). systemd starts the server with the user manager (at boot, since linger |
| 185 | is on for this user), restarts it on failure, pins it to |
| 186 | cores 0 to 2 at nice 10 and creates `~/.local/state/hive-view` (mode 700). Omega replacement: an |
| 187 | Omega supervisor (fork, wait, restart with backoff, `sched_setaffinity`, `setpriority`, `makedir`) |
| 188 | started from the user's session; proposal, not built. Note that omega-mv's standing rules say "No |
| 189 | systemd"; the hive-view brief asked for a systemd user unit, and that conflict is open with the queen. |
| 190 | |
| 191 | The gate additionally starts `/usr/bin/tee` and `/usr/bin/stty` inside its own test session only. |
| 192 | No ORACLE is used or shipped. |
| 193 | |
| 194 | ## Files |
| 195 | |
| 196 | | Path | What | |
| 197 | |---|---| |
| 198 | | `src/util.om` | buffers, paths, argument lists | |
| 199 | | `src/run.om` | run a program (fork, execprog, pipes, deadline, reaping), read and atomically write files | |
| 200 | | `src/poll.om` | the shared poller: writes one snapshot file per panel plus an index | |
| 201 | | `src/web.om` | request checks, headers, sign-in, the send pipeline and its log | |
| 202 | | `src/page.om` | HTML: pages, panels, terminal colours, the BOARD and entries, palette CSS | |
| 203 | | `src/route.om` | `POST /send` form handling, the route table, the per-connection loop | |
| 204 | | `src/main.om` | entry points: `serve`, `mktoken`, `poll-once` | |
| 205 | | `web/app.css` | the stylesheet | |
| 206 | | `tests/gate.om` | the acceptance gate | |
| 207 | | `deploy/hive-view.service` | systemd user unit | |
| 208 | | `evidence/` | gate, negative-control and CPU results for a revision | |