Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

Everyone sees '12 here · 4 signed in · 8 watching' on one line at the top, updated live over /live #297

Parked opened this
Closed without merging

smith/arm:smith/291-everyone-sees-12-here-4-signed-in-8-watc → main 5 commits fork 2 threads · 2 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
9 files changed +87 −6
Software/README.md +3
@@ -497,6 +497,8 @@ on. While you drive, a ring around the seconds counts the time left, and
497497 **+30 s** adds time while nobody waits. The countdown text is not read out
498498 by screen readers; the call is announced once.
499499
500 **The presence line.** The strip shows `12 here · 4 signed in · 8 watching` on one line under the clock, updated live over `/live`; its 16px slot is reserved in the page so nothing moves when it fills. It is drawn by JavaScript, so without JavaScript the line stays empty (the terminal has `/here`).
501
500502 **The terminal.** The room also has a terminal door (`src/tty.ts`, the
501503 `tty` in `forge.json`). Connect with the exact command the forge gives in
502504 `FORGE_APP_SSH`; when the forge has
@@ -526,6 +528,7 @@ watches. A line you type is said in the chat; commands start with `/`:
526528 command in and out and every clamp, line by line as it happens. `/machine`
527529 again prints the joints again; `/machine off` stops the stream. Anyone may,
528530 guests too: it is read-only.
531 - `/here`: the page's presence line, `12 here · 4 signed in · 8 watching`, as the room last told it (counted from the live connections, coalesced to a few frames a second).
529532 - `/who` (who drives, who waits), `/help`, `/quit`.
530533
531534 The terminal is a client of the running server, like the page: it opens
Software/so101/src/lobby.test.ts +14
@@ -105,6 +105,20 @@ describe("lobby", () => {
105105 expect((await anon.next((m) => m.type === "error")).error).toContain("join to take part");
106106 });
107107
108 test("presence counts change on connect and disconnect, coalesced", async () => {
109 const port = await start();
110 const seen = (c: Awaited<ReturnType<typeof join>>, total: number, signedIn: number) => c.next((m) => m.type === "presence" && m.total === total && m.signedIn === signedIn);
111 const anon = await join(port);
112 const alice = await join(port, ALICE);
113 const bob = await join(port, BOB);
114 expect(await seen(anon, 3, 2)).toMatchObject({ anonymous: 1 });
115 expect(anon.heard.filter((m) => m.type === "presence").length).toBeLessThanOrEqual(2); // three connects, not three broadcasts
116 bob.ws.close();
117 expect(await seen(alice, 2, 1)).toMatchObject({ anonymous: 1 });
118 alice.ws.close();
119 expect(await seen(anon, 1, 0)).toMatchObject({ anonymous: 1 });
120 });
121
108122 test("a forged identity header is nobody", async () => {
109123 const port = await start();
110124 const forged = { "x-forge-identity": `u-owner|trevato|admin|${Date.now() + 60_000}`, "x-forge-signature": sign(`u-owner|trevato|admin|${Date.now() + 60_000}`, "not the key") };
Software/so101/src/lobby.ts +27 −1
@@ -23,7 +23,7 @@
2323 * {"type": "words", "words": ["…"]} owner: the chat's word filter; a line with a listed word is refused
2424 * {"type": "pong"} the answer to {"type": "ping"}
2525 * {"type": "history", "before": 12} anyone: up to HISTORY_PAGE lines older than that one
26 * Server → client: hello (who you are, and whether you may moderate: delete a line), state (the turn, the line, pause, the announcement and the speed cap),
26 * Server → client: presence (how many are here, signed in and anonymous, coalesced to one frame per PRESENCE_MS), hello (who you are, and whether you may moderate: delete a line), state (the turn, the line, pause, the announcement and the speed cap),
2727 * chat (the last CHAT_FIRST lines of the log, and whether there are more), history (an older page, the same way),
2828 * said, unsaid, muted, admin (owners only: the settings and the admin log), crew (owners only:
2929 * settings, hidden lines, mute suggestions), banned and words (owners only), ping, error; and whatever the relay
@@ -71,6 +71,8 @@ export interface LobbyOptions {
7171 /** How often each socket is pinged, and how long it may stay silent before it counts as dropped. */
7272 pingMs?: number;
7373 silentMs?: number;
74 /** Connects and disconnects within this many ms are told to the room as one presence frame. */
75 presenceMs?: number;
7476 }
7577
7678 interface Seat {
@@ -105,6 +107,8 @@ export class Lobby {
105107 private readonly arriveListeners = new Set<(tell: (text: string) => void) => void>();
106108 private readonly pinger: ReturnType<typeof setInterval>;
107109 private readonly silentMs: number;
110 private readonly presenceMs: number;
111 private presenceTimer: ReturnType<typeof setTimeout> | undefined;
108112 private lastActivityState = 0;
109113 private readonly now: () => number;
110114
@@ -120,6 +124,7 @@ export class Lobby {
120124 this.accounts = options.accounts ?? new Accounts();
121125 this.filter = new WordFilter(options.wordsFile);
122126 this.silentMs = options.silentMs ?? 15_000;
127 this.presenceMs = options.presenceMs ?? 300;
123128 this.moderator = options.moderator;
124129 if (this.moderator) {
125130 this.moderator.attach({
@@ -251,6 +256,7 @@ export class Lobby {
251256
252257 close(): void {
253258 clearInterval(this.pinger);
259 clearTimeout(this.presenceTimer);
254260 for (const s of this.seats) s.ws.terminate();
255261 this.wss.close();
256262 this.turns.dispose();
@@ -263,6 +269,8 @@ export class Lobby {
263269 this.seats.add(seat);
264270 if (who) this.turns.connect(who.id);
265271 this.send(seat, this.hello(seat));
272 this.send(seat, this.presence());
273 this.presenceChanged();
266274 this.send(seat, this.state());
267275 this.send(seat, this.chatLog());
268276 if (who?.owner) {
@@ -293,6 +301,7 @@ export class Lobby {
293301 });
294302 ws.on("close", () => {
295303 this.seats.delete(seat);
304 this.presenceChanged();
296305 if (who) this.turns.disconnect(who.id);
297306 });
298307 ws.on("error", () => ws.terminate());
@@ -447,6 +456,23 @@ export class Lobby {
447456 this.broadcastState();
448457 }
449458
459 /** Who has the room open: everyone, those signed in (a name), and those only watching. */
460 private presence() {
461 let signedIn = 0;
462 for (const s of this.seats) if (s.who) signedIn++;
463 return { type: "presence", total: this.seats.size, signedIn, anonymous: this.seats.size - signedIn };
464 }
465
466 /** A connect or disconnect: the room hears the new counts once the burst is over, not once per socket. */
467 private presenceChanged(): void {
468 if (this.presenceTimer) return;
469 this.presenceTimer = setTimeout(() => {
470 this.presenceTimer = undefined;
471 this.broadcast(this.presence());
472 }, this.presenceMs);
473 this.presenceTimer.unref?.();
474 }
475
450476 private state() {
451477 return { type: "state", ...this.turns.snapshot(), announcement: this.admin.settings.announcement, maxSpeedPercent: this.admin.settings.maxSpeedPercent };
452478 }
Software/so101/src/terminal.test.ts +9
@@ -450,3 +450,12 @@ describe("terminal door", () => {
450450 await waitFor(() => (alice.printed.filter((l) => /^resumed/.test(l)).length === 2 ? true : undefined), "the owner's resume on alice's terminal");
451451 });
452452 });
453
454 test("/here prints the presence line as the page words it, once the room has told it", () => {
455 const room = emptyRoom();
456 expect((typed(room, "/here") as { print: string }).print).toBe("no count from the room yet");
457 expect(hear(room, { type: "presence", total: 12, signedIn: 4, anonymous: 8 })).toEqual([]);
458 expect((typed(room, "/here") as { print: string }).print).toBe("12 here · 4 signed in · 8 watching");
459 hear(room, { type: "presence", total: -1, signedIn: "x", anonymous: 8 });
460 expect(room.presence).toBe("12 here · 4 signed in · 8 watching");
461 });
Software/so101/src/terminal.ts +16 −2
@@ -28,6 +28,7 @@
2828 * /table <mm> admin: set the table offset, -100..100 mm (PUT /settings tableOffsetMm)
2929 * /machine [off] the Machine panel (/machine, src/machine-feed.ts): the joints, the link and the server as they are,
3030 * then every command in and out as it happens, in the page's own words (src/ui/machine.js); off stops it
31 * /here how many are here, signed in and watching (the page's presence line; also printed when it changes)
3132 * /who /help /quit
3233 * <who> is a name the door has seen (driving, in line, in the chat) or a user id.
3334 */
@@ -35,6 +36,7 @@
3536 import WebSocket from "ws";
3637 import { UNPRINTABLE } from "./chat.ts";
3738 import { ID, NAME, sign } from "./identity.ts";
39 import { presenceLine } from "./ui/presence.js";
3840 import { entryLine, jointRows, linkRows, stateRows } from "./ui/machine.js";
3941
4042 export type Env = Record<string, string | undefined>;
@@ -111,11 +113,13 @@ export interface Room {
111113 told: { called: boolean; next: boolean };
112114 /** The latest chat line of each author, by user id: what `/delete <who>` removes. */
113115 latest: Map<string, number>;
116 /** The presence line as last told, null until the first frame. */
117 presence: string | null;
114118 /** Everyone seen by name, most recent last: to turn a typed name into an id. */
115119 seen: Map<string, Someone>;
116120 }
117121
118 export const emptyRoom = (): Room => ({ you: null, muted: false, banned: false, driver: null, queue: [], paused: false, held: undefined, told: { called: false, next: false }, seen: new Map(), latest: new Map() });
122 export const emptyRoom = (): Room => ({ you: null, muted: false, banned: false, driver: null, queue: [], paused: false, held: undefined, told: { called: false, next: false }, seen: new Map(), latest: new Map(), presence: null });
119123
120124 const someone = (x: unknown): Someone | null => {
121125 if (typeof x !== "object" || x === null) return null;
@@ -139,7 +143,7 @@ const said = (room: Room, m: unknown): string => {
139143 return from ? `${tag}${plain(from.name)}: ${plain(message?.text)}` : `· ${plain(message?.text)}`;
140144 };
141145
142 export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /mute <who> /unmute <who> /table here|<mm> (owner) · /delete <who|#id> (owner, moderator) · /removed /restore #<id> (owner) · /machine [off] · /who · /quit";
146 export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /mute <who> /unmute <who> /table here|<mm> (owner) · /delete <who|#id> (owner, moderator) · /removed /restore #<id> (owner) · /machine [off] · /here · /who · /quit";
143147
144148 /** The Machine feed's socket beside the lobby's: same app, same port. */
145149 export const machineUrl = (live: string): string => live.replace(/\/live$/, "/machine");
@@ -228,6 +232,14 @@ export function hear(room: Room, frame: Frame): string[] {
228232 if (now === was || (was === undefined && now === null)) return [];
229233 return [now ?? "resumed: the arm may move again, from a new command"];
230234 }
235 case "presence": {
236 const n = (x: unknown) => (typeof x === "number" && Number.isSafeInteger(x) && x >= 0 ? x : null);
237 const [total, signedIn, anonymous] = [n(frame.total), n(frame.signedIn), n(frame.anonymous)];
238 if (total === null || signedIn === null || anonymous === null) return [];
239 // Kept for /here, not printed: the room changes often and a terminal scrolls.
240 room.presence = presenceLine({ total, signedIn, anonymous });
241 return [];
242 }
231243 case "unsaid":
232244 return ["· a line was removed"];
233245 case "removed": {
@@ -309,6 +321,8 @@ export function typed(room: Room, input: string): Typed {
309321 return { machine: arg.toLowerCase() !== "off" };
310322 case "who":
311323 return { print: who(room) };
324 case "here":
325 return { print: room.presence ?? "no count from the room yet" };
312326 case "help":
313327 return { print: HELP };
314328 case "quit":
Software/so101/src/ui/lobby-panel.js +1
@@ -251,6 +251,7 @@ export function createRoom(page) {
251251 const m = JSON.parse(e.data);
252252 if (m.type === "ping") return say({ type: "pong" });
253253 if (m.type === "status") { lobby.statusSeen = true; return m.status ? page.applyStatus(m.status) : page.noStatus(); }
254 if (m.type === "presence") return void import("./presence.js").then((p) => p.show(m));
254255 if (m.type === "hello") lobby.hello = m;
255256 else if (m.type === "state") { lobby.offset = m.now - Date.now(); lobby.state = m; }
256257 else if (m.type === "chat") return addChat(m.messages, true), showEarlier(m.more);
Software/so101/src/ui/presence.js added +8
@@ -0,0 +1,8 @@
1 /** The one line the strip shows for the room: everyone here, those signed in, those only watching. */
2 export const presenceLine = (/** @type {{total: number, signedIn: number, anonymous: number}} */ p) => `${p.total} here · ${p.signedIn} signed in · ${p.anonymous} watching`;
3
4 /** Fill the slot viewer.html reserves (16px, so nothing moves when the line arrives). */
5 export function show(/** @type {{total: number, signedIn: number, anonymous: number}} */ p) {
6 const line = document.getElementById("presence");
7 if (line) line.textContent = presenceLine(p);
8 }
Software/so101/src/ui/presence.test.ts added +6
@@ -0,0 +1,6 @@
1 import { expect, test } from "bun:test";
2 import { presenceLine } from "./presence.js";
3
4 test("the presence line reads '12 here · 4 signed in · 8 watching'", () => {
5 expect(presenceLine({ total: 12, signedIn: 4, anonymous: 8 })).toBe("12 here · 4 signed in · 8 watching");
6 });
Software/so101/src/viewer.html +3 −3
@@ -89,13 +89,13 @@
8989 #strip .who { flex: 1; min-width: 0; }
9090 #driverLine { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } #driverLine.none { color: var(--dim); font-weight: 500; }
9191 #clock { color: var(--dim); font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } #clock.idle { color: var(--warn); }
92 #capLine { color: var(--dim); font-size: 12px; } #capLine:empty { display: none; }
92 #capLine, #presence { color: var(--dim); font-size: 12px } #presence { height: 16px; overflow: hidden } #capLine:empty { display: none; }
9393 #armLine { display: none; gap: 6px; align-items: center; color: var(--dim); font-size: 12px; white-space: nowrap; } #armText { min-width: 0; overflow: hidden; text-overflow: ellipsis; } /* shown on a phone held upright (ui/mobile.css) */
9494 #reconnect { align-self: flex-start; padding: 0 8px; border-radius: 9px; font-size: 11px; line-height: 16px; background: var(--warn); color: #000; visibility: hidden; } #reconnect.on { visibility: visible; }
9595 #driverLine.paused { color: var(--warn); }
9696 #turnActions { display: flex; gap: 6px; } #turnActions:empty { display: none; }
9797 .estop { flex: none; min-width: 72px; min-height: 56px; border: 0; border-radius: 12px; background: #d1242f; color: #fff; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; box-shadow: 0 0 0 3px rgba(209,36,47,.25), 0 2px 8px rgba(0,0,0,.35); }
98 .estop:hover { background: #e5323d; } .estop:active { background: #b81d27; transform: scale(.97); } /* red under a finger too: a touch is hover then active */
98 .estop:hover { background: #e5323d; } .estop:active { background: #b81d27; transform: scale(.97); }
9999 #sheetToggle { flex: none; }
100100 /* the turn ring: time left as a sweep around the seconds; the driver's name over the video for everyone */
101101 #ring { flex: none; width: 48px; height: 48px; border-radius: 50%; display: grid; place-items: center; background: conic-gradient(var(--blue) var(--p, 0%), var(--line) 0); font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; position: relative; }
@@ -310,7 +310,7 @@
310310
311311 <section id="strip" aria-label="Who drives" aria-live="polite">
312312 <div id="ring" hidden aria-hidden="true"><span id="ringN"></span></div>
313 <div class="who"><div id="driverLine" class="none">Connecting…</div><span id="reconnect" role="status">reconnecting…</span><div id="clock" aria-live="off"></div><div id="capLine"></div><div id="armLine" aria-live="off"><span class="dot" id="armDot"></span><span id="armText"></span></div></div>
313 <div class="who"><div id="driverLine" class="none">Connecting…</div><span id="reconnect" role="status">reconnecting…</span><div id="clock" aria-live="off"></div><div id="presence" role="status" aria-live="off"></div><div id="capLine"></div><div id="armLine" aria-live="off"><span class="dot" id="armDot"></span><span id="armText"></span></div></div>
314314 <div id="turnActions"></div>
315315 <button id="sheetToggle" hidden aria-controls="sheet" aria-expanded="true">Hide</button>
316316 <button id="stop" class="estop" data-stop title="Stop (space or Esc): halt everything, hold with torque, until someone resumes" aria-label="Stop the arm">Stop</button>

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs