Skip to content

2026 11 26 admin tick design

kubed-io[bot] edited this page Nov 27, 2026 · 2 revisions

Admin tick: the workspace list is rebuilt when it changes

History. Drafted 2026-11-26 for #226, to be built by PR #231 from its plan. #226's notes were the approved brief, and no draft spec was on the branch. This file finishes the brief against the code and against sse-starlette's source. "Found at plan time" lists what that changed. Its requirements are UI-34 and UI-35 in admin-ui.md, and the admin list row of runtime.md's limits table.

PR #231 built it. Its final review added to the design. UI-34 as shipped says "within 30 seconds plus a tick" and states the joiner rule. A partial build (the Grid or the store unreadable) is delivered but built again on the next tick. The REST flow and keep writes have no signal, so they fall under the bound. One test margin departed from the plan: test_many_finished_calls_are_one_computation polls at 0.4 s, not the plan's 0.2 s.

Brief

With one admin page open and no MCP traffic, the server spends about 23 mCPU, 41% of the process, on the admin tick. Broadcast._run calls workspaces_payload every POLL_SECONDS (2 s), and each call costs about 46 ms of CPU: a Redis SCAN and MGET, Configs.kept and revision, the flow store's listing per workspace, and a Grid listing. That buys a list that rarely changes. A quiet page also refetches GET /admin/workspaces and reopens /admin/events every 60 s. The stream's keepalive is an SSE comment, which EventSource never shows to script, so the page's 45 s check finds the stream silent.

This change rebuilds the list only when something marked it changed, or when the last build is 30 s old. The stream also sends a named beat event that the page counts.

Rulings

From #226's thread (Dr K, 2026-11-26):

  1. The bound is 30 s (REFRESH_SECONDS), stated in UI-34. It is the longest a change with no signal can take to show, and it matches the page's 30 s fallback (UI-35).
  2. The beat is a named SSE event, beat. The page listens with addEventListener('beat', …). A beat can never be read as a payload.
  3. The sse-starlette points are checked against the installed version before UI-34 says what the ping does. See Found 1 and 2.

Design

1. Change signals

These mark the list changed:

Signal Where Nudges the tick
call.finished on the bus (new) SeleniumMCP.__init__, subscription admin-calls no
Broadcast.changes on an admin route that isn't a GET invalidate() no
session.opened, session.ended the admin subscription, poke() yes
a filed recording collector.on_filed, poke() yes

call.finished covers every MCP, HTTP and flow call on this pod: the page, history, files, flows and site data a tool changes. It marks without nudging, so traffic costs at most one build per POLL_SECONDS. The server runs as one replica, so no other pod writes.

These changes have no signal, and the 30 s bound covers them:

  • a record or sub-session expiring in Redis;
  • the Grid reaping a browser nothing watches (keep-alive 0 with no capture, recording or control);
  • downloads, or capture counts, that the page makes between calls;
  • a hand edit of a workspace config or of kept files on disk;
  • the Grid's version or node for a session.

2. Broadcast

New state, all of it touched only on the event loop:

  • _changes: int, bumped by mark() and by invalidate();
  • _seen: int, the value of _changes when the computation that became _latest started.

_latest keeps its shape, (started, payload, text), and _floor keeps its meaning.

  • mark() bumps _changes, and does nothing else.
  • invalidate() bumps _changes, sets _latest = None and _floor = now, as today.
  • poke() is invalidate() and then a nudge, as today.
  • Stale. The latest is stale when there is none, when _seen != _changes, or when it started at least REFRESH_SECONDS ago.
  • Current. The latest is current when it is younger than FRESH_SECONDS, or when it is not stale. fresh() and join() hand out the current one. A latest that isn't current is always stale, so a joiner that is handed nothing nudges a tick that will compute.
  • _run. It still wakes every POLL_SECONDS, or on a nudge. It computes only when the latest is stale, and otherwise does nothing: no build, no delivery. Before it starts a computation it reads _changes on the loop. A success stores that value as _seen, together with _latest, when started >= _floor. So a mark that lands while a computation is under way is never counted as seen, and the next tick computes again. A failed computation stores nothing, so its mark is still unseen and the next tick tries again.
  • GET /admin/workspaces answers fresh() when it is current, and otherwise computes inline, as today.

3. The beat

  • /admin/events passes ping_message_factory=lambda: ServerSentEvent(event="beat", data="") with ping=HEARTBEAT (20 s). Each beat is written as event: beat, then data: , then a blank line.
  • Live.#watch in ui/src/admin/live.svelte.ts adds events.addEventListener('beat', () => this.#beat()). The catch in onmessage stays as the guard for a frame that isn't JSON, and its comment stops calling that frame a keepalive.
  • The page's 30 s timer and 45 s check stay. They still catch a stream that a proxy swallowed, or a signed link that expired: an EventSource reconnect that gets a 401 stops for good, and the beats stop with it.
  • The capture tail (http/admin/capture.py) keeps its comment ping. It has no fallback timer.

Found at plan time

  1. sse-starlette's ping runs on a fixed timer. Read at v3.3.0 (the floor, >=3.3) and at v3.7.1 (the newest, which an image build installs, since scripts/requirements.py passes the floor through unpinned). In both, EventSourceResponse._ping loops while self.active: await anyio.sleep(self._ping_interval), then sends ping_message_factory() or its default comment. Nothing resets the timer when data is sent. So UI-34 says "every 20 seconds", not "after 20 seconds of no change", and the HEARTBEAT comment in workspaces.py is corrected the same way. Both versions take ping_message_factory: Callable[[], ServerSentEvent] | None, and both export ServerSentEvent from sse_starlette, so the floor doesn't move.
  2. data="" is written as data: . ServerSentEvent.encode writes data lines under if self.data is not None, and an empty string splits into one empty chunk. By the WHATWG algorithm, a data: line makes the data buffer "\n", which isn't empty, so the beat event is dispatched with data "". A data-less event would not be dispatched.
  3. A joiner on a quiet list would get nothing. Today join hands out _latest only while it is under FRESH_SECONDS (1 s) old, and otherwise nudges a tick that always computes. Once the tick skips unchanged lists, a page joining 10 s after the last build would nudge a tick that does nothing, and it would wait up to 30 s for its first event. So "current" (§2) covers an unchanged latest within the bound too, for join and fresh() alike, and the nudge goes out only when the latest is stale.
  4. _latest keeps its 3-tuple shape. test_a_poke_forgets_the_last_broadcast_and_ticks_now (tests/test_recording_collector.py) writes it directly, so the counter that a build saw is a separate field, _seen.
  5. The existing broadcast tests count ticks. test_every_connected_page_is_sent_the_same_events and the other tick-counting tests in tests/test_admin_events.py depend on every tick computing. They set REFRESH_SECONDS = 0.0, which keeps what they test (pacing, dedup, the late joiner) and is exactly today's behaviour. test_the_listing_is_the_last_broadcast_while_it_is_fresh asserts that a stale listing is never served, which now means a listing past the bound or marked since.
  6. The _listen helper reads every data: line as a payload. A beat's data: line would read as an empty payload. It now splits each body into frames, and a frame with event: beat goes to a beats list, not to the payloads.
  7. The files tab's header() (http/admin/files.py) calls broadcast.compute on every request. That's out of scope here, and the PR says so.
  8. No image is built for a pull request (CONTRIBUTING, "CI"), so the before and after CPU need an image of the branch running on the cluster. See the plan's "After the tasks".

Named changes

One changelog line:

  • An open admin page costs next to nothing while nothing changes, and no longer refetches the list every minute; a change made outside the server shows within 30 s.

The spec, id by id

Id Becomes
UI-34 One server task, running only while a page is connected, wakes every 2 seconds and computes the list only when a change was marked since its last computation started, or that computation is 30 seconds old, or there is none. These mark a change: a call finishing on this server (any surface), a route that changes what the list shows (as listed today), a session opening or ending, and a recording filed. The last two compute at once. A change with no signal (a record expiring, the Grid reaping a browser nothing watches, a file edited on disk) shows within 30 seconds. A page receives a message when the payload differs from its last. The stream sends a beat event every 20 seconds from when it opens, whatever else it sends
UI-35 Adds: a beat counts as hearing from the stream, so an open stream never trips the fallback
runtime.md, limits The row reads "Admin list: tick; refresh bound; fallback poll; stream beat", "2 s; 30 s; 30 s; 20 s", citing UI-34, UI-35

Not now

  • The files tab's header() computing the whole list per request (Found 7).
  • A beat on the capture tail.
  • A change signal for Redis expiry (keyspace notifications) or for the disk.

Clone this wiki locally