From 440cd338863e3cb5a4ff00fb30fbeff4aeedae05 Mon Sep 17 00:00:00 2001 From: "nekomimi.pet" Date: Mon, 2 Nov 2026 15:18:17 -0500 Subject: [PATCH] terminals: the first attachment gets the shell's early output live, so its prompt's queries are answered and a fresh terminal never reports history --- robot-docs/web.md | 2 +- src/albedo/daemon/albedo_terminals.erl | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/robot-docs/web.md b/robot-docs/web.md index 1241e0def..6f40cae6b 100644 --- a/robot-docs/web.md +++ b/robot-docs/web.md @@ -65,7 +65,7 @@ The diff client (`nodes/ReviewNode.svelte`, `views/DiffClient.svelte`, `changese The **terminal** (`nodes/TerminalNode.svelte`, `views/TerminalView.svelte`, `jobs.svelte.ts`) shows one background job's output, read-only. An activity pulse in a chat's title bar (`JobsButton.svelte`; not a prompt, which is kept for a live terminal: the chat's own `>_` button beside its folder chip opens one in the session's folder, and the folder chip only names the folder; on the phone above the composer, shown once the session has had a job; it and the artifacts icon share `MenuButton.svelte`) counts the running jobs and lists them, finished ones with their exit codes while the kernel keeps their output; the list polls `GET /extensions/run/sessions/{id}/jobs/recent` every 2 s while a job runs, 6 s otherwise. Choosing one opens a window beside the chat, tied to it like a review. The window opens on the last page of `.../jobs/{job}/output` and follows `next_offset` every 700 ms while the job runs (once more after it ends), keeps at most 400,000 characters, drops colour codes and shows a `\r`-redrawn line as its last draw. Its stop button asks twice, then posts `.../jobs/{job}/stop`. A job the kernel no longer has (a restarted kernel) says so. On a phone the same view opens in the chat's dock, above the composer. -The **interactive terminal** (`nodes/PtyNode.svelte`, `views/PtyView.svelte`, `terminals.svelte.ts`) opens a live pseudo-terminal running the user's shell in any workspace. T or "new terminal" in the command palette opens one beside the active window, and clicking a chat's workspace path pill opens one in that session's folder. Underneath, `@xterm/xterm` (v6) with `@xterm/addon-fit` and `@xterm/addon-web-links` provides terminal emulation, clickable link opening, and dynamic theme synchronization with the canvas's madoromi colors and mono font preferences. `terminal-theme.ts` derives the sixteen ANSI colours from the palette (red and green from danger and added) and holds each to 4.5:1 against the terminal's ground, bright black and night's black to 3:1; by day bright colours are the lighter ones at 4.5:1 and normal ones deeper. `minimumContrastRatio` 3 darkens truecolour a program chose for the other mode (a dark-tuned nushell's `#e6eaff`), and dim text half of that, so faint text stays fainter. xterm answers OSC 10/11 queries with the theme's foreground and background. `web/scripts/terminal-theme.test.mjs` checks every season by day and night. The window connects over a WebSocket at `/api/terminals/{id}` to the daemon's PTY service (`albedo_terminals.erl`, `albedo_pty.py`), negotiating row/column sizing on resize and retaining output history across reconnects and board switches. Every attachment sends its `ready` frame before history or live output; its `history` field is the byte length of the one binary history frame that follows (0 for none). The view resets the terminal and writes that frame without answering it (a reconnect replays everything the view already shows, so writing it on top would repeat it): replayed output still holds the shell's old queries (reedline sends CSI 6n before every prompt), and answering them again would hand the shell stale cursor positions, so it draws its prompt over its output. The view connects after its first fit, so the shell starts at the size it is shown at, and a `ready` whose size differs from the view's gets a resize back (a resize sent before the socket opens is dropped). An in-stream restart keeps its requesting socket attached, closes any other watchers, then sends that socket the new `ready` frame before output from the replacement PTY. Output is flow controlled: the worker puts `"credit": 8` in the spec it sends `albedo_pty.py`, and only then does the helper wait for credit (inbound frame `0x05` + uint16), so a running daemon's older worker that starts a newer helper from disk still gets unthrottled output, and a newer worker's credit frames are ignored by an older helper (`test/harness/pty_protocol_test.py` checks the first). With credit, the helper sends at most 8 output frames of at most 16 KiB ahead of the worker, so at most 128 KiB of output is read ahead; the worker grants one frame per frame it handles, and with no credit left the bridge stops reading the PTY, so a fast producer blocks on the PTY instead of filling the daemon. The worker serves attach, detach, input, resize, signal, restart and stop before queued output. A socket whose mailbox holds more than 32 messages is behind: its output is held (at most 64 KiB, newest kept) and retried every 20 ms, then sent as one binary frame once it catches up, and before any exit event. When bytes were dropped that frame starts with `\r\n\e[0m[albedo: skipped N bytes of output while this view was behind]\r\n`; the other sockets and the reconnect history are unaffected. The shell gets every key, Esc and Tab included; ⌥Esc (`attachCustomKeyEventHandler`) is the one way out to the board's keys, leaving the focus on the page with the window still selected. The footer displays live dimensions (`cols × rows`), that key (`⌥esc leave`), signal interruption (^C), clear, and restart controls. +The **interactive terminal** (`nodes/PtyNode.svelte`, `views/PtyView.svelte`, `terminals.svelte.ts`) opens a live pseudo-terminal running the user's shell in any workspace. T or "new terminal" in the command palette opens one beside the active window, and clicking a chat's workspace path pill opens one in that session's folder. Underneath, `@xterm/xterm` (v6) with `@xterm/addon-fit` and `@xterm/addon-web-links` provides terminal emulation, clickable link opening, and dynamic theme synchronization with the canvas's madoromi colors and mono font preferences. `terminal-theme.ts` derives the sixteen ANSI colours from the palette (red and green from danger and added) and holds each to 4.5:1 against the terminal's ground, bright black and night's black to 3:1; by day bright colours are the lighter ones at 4.5:1 and normal ones deeper. `minimumContrastRatio` 3 darkens truecolour a program chose for the other mode (a dark-tuned nushell's `#e6eaff`), and dim text half of that, so faint text stays fainter. xterm answers OSC 10/11 queries with the theme's foreground and background. `web/scripts/terminal-theme.test.mjs` checks every season by day and night. The window connects over a WebSocket at `/api/terminals/{id}` to the daemon's PTY service (`albedo_terminals.erl`, `albedo_pty.py`), negotiating row/column sizing on resize and retaining output history across reconnects and board switches. Every attachment sends its `ready` frame before history or live output; its `history` field is the byte length of the one binary history frame that follows (0 for none). The first attachment to a new terminal gets the shell's early output live after `ready` rather than as history, so its prompt's queries are answered. The view resets the terminal and writes that frame without answering it (a reconnect replays everything the view already shows, so writing it on top would repeat it): replayed output still holds the shell's old queries (reedline sends CSI 6n before every prompt), and answering them again would hand the shell stale cursor positions, so it draws its prompt over its output. The view connects after its first fit, so the shell starts at the size it is shown at, and a `ready` whose size differs from the view's gets a resize back (a resize sent before the socket opens is dropped). An in-stream restart keeps its requesting socket attached, closes any other watchers, then sends that socket the new `ready` frame before output from the replacement PTY. Output is flow controlled: the worker puts `"credit": 8` in the spec it sends `albedo_pty.py`, and only then does the helper wait for credit (inbound frame `0x05` + uint16), so a running daemon's older worker that starts a newer helper from disk still gets unthrottled output, and a newer worker's credit frames are ignored by an older helper (`test/harness/pty_protocol_test.py` checks the first). With credit, the helper sends at most 8 output frames of at most 16 KiB ahead of the worker, so at most 128 KiB of output is read ahead; the worker grants one frame per frame it handles, and with no credit left the bridge stops reading the PTY, so a fast producer blocks on the PTY instead of filling the daemon. The worker serves attach, detach, input, resize, signal, restart and stop before queued output. A socket whose mailbox holds more than 32 messages is behind: its output is held (at most 64 KiB, newest kept) and retried every 20 ms, then sent as one binary frame once it catches up, and before any exit event. When bytes were dropped that frame starts with `\r\n\e[0m[albedo: skipped N bytes of output while this view was behind]\r\n`; the other sockets and the reconnect history are unaffected. The shell gets every key, Esc and Tab included; ⌥Esc (`attachCustomKeyEventHandler`) is the one way out to the board's keys, leaving the focus on the page with the window still selected. The footer displays live dimensions (`cols × rows`), that key (`⌥esc leave`), signal interruption (^C), clear, and restart controls. The **trace** (`nodes/TraceNode.svelte`, `views/TraceView.svelte`, `trace.ts`, `TraceTie.svelte`, `tracelink.svelte.ts`) shows one task as albedo ran it: "trace" on the sign-off of a turn that ran tools opens it beside the chat, tied to it like a review. A task is that turn and the turns after it until the person types again (mail from a child, continuations), so a parent's wait for its agents and their replies fall inside it. A trace opened on the session's newest task follows the session instead: every later turn is added as it begins, each message the person sent marked on the axis ("you"), and it reads again whenever the session's phase changes as well as every 3 s while anything in it works, never while the tab is hidden, and every 5 s while a read fails. Its axis runs ahead of now (a dashed line), by a quarter of the task or at least 30 s, and grows only when now reaches the end, so the calls already drawn hold still under the pointer between reads. "earlier turn" adds the turn before the earliest shown (`turnBefore` walks history back to the person's previous message; the count is saved on the window as `earlier`), read in place so the view keeps its zoom. diff --git a/src/albedo/daemon/albedo_terminals.erl b/src/albedo/daemon/albedo_terminals.erl index 1ea7514f3..f30c369aa 100644 --- a/src/albedo/daemon/albedo_terminals.erl +++ b/src/albedo/daemon/albedo_terminals.erl @@ -280,6 +280,9 @@ start_worker(Id, Cwd, Shell, Cols, Rows) -> rows => Rows, status => running, history => <<>>, + %% Output from before the first attachment was never shown, + %% so its queries (a prompt's CSI 6n) still wait for an answer. + attached => false, watchers => #{}, idle_timer => Timer, flush_timer => none @@ -384,9 +387,19 @@ handle(Message, S = #{port := Port, watchers := Watchers, history := History}) - Watchers#{Caller => {erlang:monitor(process, Caller), Notify, none}} end, S2 = maybe_resize(S1#{watchers => Watchers1}, clamp_cols(Cols), clamp_rows(Rows)), - Ready = build_ready(S2), - Caller ! {Ref, {ok, Ready}}, - worker_loop(S2); + case maps:get(attached, S2) of + true -> + Caller ! {Ref, {ok, build_ready(S2)}}; + false -> + %% The first viewer gets the shell's early output live, after + %% its ready frame, so the terminal answers what it asked. + Caller ! {Ref, {ok, build_ready(S2#{history => <<>>})}}, + case History of + <<>> -> ok; + _ -> notify(Notify, {terminal_output, History}) + end + end, + worker_loop(S2#{attached => true}); {detach, Caller} -> S1 = remove_watcher(Caller, S), -- knotmirror 0.0.1