Public: anyone can read this; the crew can change it.
Run logs and the app page patch from the one /events stream, not their own EventSources #2841
scout/forge:scout/2835-run-logs-and-the-app-page-patch-from-the → main 1 commit fork
11 files
changed +188
−59
docs/DESIGN.md +1 −1
| @@ -201,7 +201,7 @@ specification. | ||
| 201 | 201 | | **app strip** | `.app-strip` | — | one line under the repo header of a repository that runs: env state badge, `sha`, the production URL as a link **through `…/app/production/open`** (never the bare hostname — a private app would 404), the preview count, App (the studio app page)| one line under the where strip: `● running abc1234 · hello.localhost` | |
| 202 | 202 | | **environment card** | `.card.env-card` (`.env-facts`, `.env-error`) | `data-tone=danger` when failed | name and state in the head; `envFacts()` in the body — commit, how fast it came up, uptime, a link to the deploy run, and the deployment's own note and error, each on its own line; Open (or Wake it) below. It is a pull's preview card; the facts and `envActions()` (Redeploy / Stop, writers only) are functions, not markup, so the studio head and its preview rows render the same ones | a box per environment with the same three lines | |
| 203 | 203 | | **checks line** | `.checks-line` (`.ok`, `.bad`, `.now`) | — | inside the merge box, in the reviews chip's voice: `✓ Checks passed · deploy abc1234 · a passing deploy is required`. Rendered only where the repository runs as an app or requires checks; a merge box gains _Waiting on checks_ | the same line under the review line | |
| 204 | | **run log** | `pre.run-log` | `data-stream` while the run is going | the log as written, wrapped so a phone never scrolls sideways; a script tails the SSE stream and follows the bottom until the reader scrolls up, a `<meta refresh>` does it without one | a scrolling pane that follows the tail | | |
| 204 | | **run log** | `pre.run-log` | `data-run` while the run is going | the log as written, wrapped so a phone never scrolls sideways; a script tails the SSE stream and follows the bottom until the reader scrolls up, a `<meta refresh>` does it without one | a scrolling pane that follows the tail | | |
| 205 | 205 | | **assignee card** | `.card` with an `.edit-row` form | — | who is on it, then one select and one button for a writer. People first, the crew last and marked `(agent)`, because assigning an agent is what starts it working and the choice is not the same kind of choice | a picker with people, then agents | |
| 206 | 206 | | **inbox** (`asks_page.ts`) | one list above the feed | — | _On you_: what asks you and what is assigned to you, one row each, each with what it waits for (`needsYouSection`), and one count in the top bar. Absent when empty: a card saying nothing is a zero drawn | the same two panes on the dashboard | |
| 207 | 207 |
docs/STUDIO.md +5 −2
| @@ -435,8 +435,11 @@ everything the viewer may see) and works like this: | ||
| 435 | 435 | card whose post is still in flight (`data-busy`), is left alone until |
| 436 | 436 | the next event. |
| 437 | 437 | |
| 438 | Run logs and the crew's transcript stream line by line over their own | |
| 439 | `?follow=1` answers; they are the streamed part of a page, not a region. | |
| 438 | Run logs (the run page, the app page's tail) ride the page's one `/events` | |
| 439 | stream: the page asks for its runs with `?run=<id>` and lines arrive as | |
| 440 | `runlog` frames (`{run, line}`) for viewers `mayReadRunLog` allows — no id, no | |
| 441 | replay, skipped while the reader is behind; the file is the history. The API's | |
| 442 | `?follow=1` answer remains for tools. | |
| 440 | 443 | |
| 441 | 444 | Signed out, `/` is a stranger's front door in either mode — the studio's |
| 442 | 445 | door, never a git pitch (`studioDoor` in `studio/home.ts`, decided in |
src/http/sse.ts +45 −2
| @@ -21,6 +21,9 @@ import { listEvents, listEventsVisibleTo } from "../core/events.ts"; | ||
| 21 | 21 | import { audienceFilter } from "../core/event_audience.ts"; |
| 22 | 22 | import { canRead, getRepoById } from "../core/repos.ts"; |
| 23 | 23 | import type { ForgeEvent } from "../core/events.ts"; |
| 24 | import { getRun } from "../core/runs.ts"; | |
| 25 | import { mayReadRunLog } from "../actions/runs.ts"; | |
| 26 | import { subscribeAllRunLogs } from "../runs/log.ts"; | |
| 24 | 27 | |
| 25 | 28 | const HEARTBEAT_MS = 25_000; |
| 26 | 29 | |
| @@ -200,6 +203,18 @@ export function viewerStream( | ||
| 200 | 203 | seen.set(repoId, ok); |
| 201 | 204 | return ok; |
| 202 | 205 | }; |
| 206 | // A run's log is narrower than its repository (writers and crew only, | |
| 207 | // #1075), so it is asked of `mayReadRunLog`, once per run per connection. | |
| 208 | const logs = new Map<string, boolean>(); | |
| 209 | const mayRead = (runId: string): boolean => { | |
| 210 | const known = logs.get(runId); | |
| 211 | if (known !== undefined) return known; | |
| 212 | const run = getRun(app.db, runId); | |
| 213 | const repo = run === undefined ? undefined : getRepoById(app.db, run.repoId); | |
| 214 | const ok = repo !== undefined && mayReadRunLog(app, viewer, repo); | |
| 215 | logs.set(runId, ok); | |
| 216 | return ok; | |
| 217 | }; | |
| 203 | 218 | const named = (event: ForgeEvent): Record<string, unknown> => { |
| 204 | 219 | const repo = |
| 205 | 220 | event.repoId === null ? undefined : getRepoById(app.db, event.repoId); |
| @@ -215,7 +230,19 @@ export function viewerStream( | ||
| 215 | 230 | // admins', a person's own choices are theirs (`core/event_audience.ts`). |
| 216 | 231 | const meant = audienceFilter(app.db, viewer); |
| 217 | 232 | const after = resumeFrom(request); |
| 218 | return eventStream(request, (emit) => { | |
| 233 | const watched = watchedRuns(request); | |
| 234 | return eventStream(request, (emit, _close, ready) => { | |
| 235 | // Log lines for the runs this tab asked about (`?run=<id>`): not events, | |
| 236 | // so no id and no replay — a late tab reads the file. A reader that is | |
| 237 | // behind skips lines rather than queueing them. The run's repository is | |
| 238 | // checked like any event's; the line is already redacted. | |
| 239 | const controller = new AbortController(); | |
| 240 | if (watched.size > 0) | |
| 241 | subscribeAllRunLogs((runId, line) => { | |
| 242 | if (!watched.has(runId) || !ready()) return; | |
| 243 | if (!mayRead(runId)) return; | |
| 244 | emit("runlog", { run: runId, line }); | |
| 245 | }, controller.signal); | |
| 219 | 246 | if (after !== undefined) { |
| 220 | 247 | const missed = listEventsVisibleTo(app.db, viewer, { |
| 221 | 248 | afterId: after, |
| @@ -224,11 +251,15 @@ export function viewerStream( | ||
| 224 | 251 | for (const event of missed) |
| 225 | 252 | if (meant(event)) emit("forge", named(event), event.id); |
| 226 | 253 | } |
| 227 | return app.bus.subscribe((event: ForgeEvent) => { | |
| 254 | const unsubscribe = app.bus.subscribe((event: ForgeEvent) => { | |
| 228 | 255 | if (event.repoId === null || !visible(event.repoId)) return; |
| 229 | 256 | if (!meant(event)) return; |
| 230 | 257 | emit("forge", named(event), event.id); |
| 231 | 258 | }); |
| 259 | return () => { | |
| 260 | controller.abort(); | |
| 261 | unsubscribe(); | |
| 262 | }; | |
| 232 | 263 | }); |
| 233 | 264 | } |
| 234 | 265 | |
| @@ -257,6 +288,18 @@ export function sseStream( | ||
| 257 | 288 | }); |
| 258 | 289 | } |
| 259 | 290 | |
| 291 | /** At most this many runs per stream; ids are validated, never trusted. */ | |
| 292 | const MAX_WATCHED_RUNS = 8; | |
| 293 | ||
| 294 | /** The run ids a tab asked for: `?run=<id>`, repeated, well-formed only. */ | |
| 295 | function watchedRuns(request: Request): Set<string> { | |
| 296 | const ids = new Set<string>(); | |
| 297 | for (const id of new URL(request.url).searchParams.getAll("run")) | |
| 298 | if (/^[0-9a-z]{8,40}$/i.test(id) && ids.size < MAX_WATCHED_RUNS) | |
| 299 | ids.add(id); | |
| 300 | return ids; | |
| 301 | } | |
| 302 | ||
| 260 | 303 | /** Where a reconnecting client asks to resume: the last event it heard. */ |
| 261 | 304 | function resumeFrom(request: Request): string | undefined { |
| 262 | 305 | const after = |
src/runs/log.ts +26
| @@ -31,6 +31,10 @@ export type LogListener = (line: string) => void; | ||
| 31 | 31 | |
| 32 | 32 | const listeners = new Map<string, Set<LogListener>>(); |
| 33 | 33 | |
| 34 | /** Every line of every run, for the one /events stream a viewer holds open. */ | |
| 35 | export type AnyLogListener = (runId: string, line: string) => void; | |
| 36 | const anyListeners = new Set<AnyLogListener>(); | |
| 37 | ||
| 34 | 38 | /** A line without its newline; a trailing newline is added on write. */ |
| 35 | 39 | const MAX_LINE = 16 * 1024; |
| 36 | 40 | |
| @@ -43,6 +47,13 @@ export function appendRunLog( | ||
| 43 | 47 | const clean = redactHeld(line.replace(/\r?\n/g, " ")).slice(0, MAX_LINE); |
| 44 | 48 | mkdirSync(runsRoot(dataDir), { recursive: true }); |
| 45 | 49 | appendFileSync(runLogPath(dataDir, runId), `${clean}\n`); |
| 50 | for (const listener of anyListeners) { | |
| 51 | try { | |
| 52 | listener(runId, clean); | |
| 53 | } catch { | |
| 54 | // Same as below: a watcher's failure is its own. | |
| 55 | } | |
| 56 | } | |
| 46 | 57 | const watching = listeners.get(runId); |
| 47 | 58 | if (watching === undefined) return; |
| 48 | 59 | for (const listener of watching) { |
| @@ -124,5 +135,20 @@ export function subscribeRunLog( | ||
| 124 | 135 | else signal.addEventListener("abort", release, { once: true }); |
| 125 | 136 | } |
| 126 | 137 | |
| 138 | /** Follow every run's log at once; released by `signal`. */ | |
| 139 | export function subscribeAllRunLogs( | |
| 140 | listener: AnyLogListener, | |
| 141 | signal: AbortSignal, | |
| 142 | ): void { | |
| 143 | if (signal.aborted) return; | |
| 144 | anyListeners.add(listener); | |
| 145 | signal.addEventListener("abort", () => anyListeners.delete(listener), { | |
| 146 | once: true, | |
| 147 | }); | |
| 148 | } | |
| 149 | ||
| 150 | /** How many /events streams follow run logs — a test seam for leaks. */ | |
| 151 | export const anyRunLogListenerCount = (): number => anyListeners.size; | |
| 152 | ||
| 127 | 153 | /** How many runs currently have a listener set — a test seam for leaks. */ |
| 128 | 154 | export const runLogListenerCount = (): number => listeners.size; |
src/web/client.ts +43 −38
| @@ -224,7 +224,9 @@ export const studioRunJs = /* js */ ` | ||
| 224 | 224 | (function () { |
| 225 | 225 | var log = document.getElementById('run-log'); |
| 226 | 226 | if (!log) return; |
| 227 | var url = log.getAttribute('data-stream'); | |
| 227 | // The run's lines arrive over the page's one /events stream | |
| 228 | // (liveRegionsJs, as \`forge:runlog\`): this opens no stream of its own. | |
| 229 | var runId = log.getAttribute('data-run'); | |
| 228 | 230 | var who = log.getAttribute('data-who') || 'agent'; |
| 229 | 231 | var cap = parseInt(log.getAttribute('data-cap') || '0', 10) || 0; |
| 230 | 232 | var pulse = document.getElementById('run-pulse'); |
| @@ -360,27 +362,16 @@ export const studioRunJs = /* js */ ` | ||
| 360 | 362 | }); |
| 361 | 363 | } |
| 362 | 364 | |
| 363 | if (!url || typeof EventSource === 'undefined') return; | |
| 364 | var source = new EventSource(url); | |
| 365 | if (!runId) return; | |
| 365 | 366 | var empty = document.getElementById('run-empty'); |
| 366 | var seeded = false; | |
| 367 | source.addEventListener('line', function (event) { | |
| 368 | if (!seeded) { | |
| 369 | if (event.lastEventId === '1') { | |
| 370 | Array.prototype.slice.call(log.querySelectorAll('.ln:not(#run-pulse)')).forEach(function (el) { log.removeChild(el); }); | |
| 371 | } | |
| 372 | seeded = true; | |
| 373 | if (empty) empty.hidden = true; | |
| 374 | } | |
| 375 | var text = event.data; | |
| 376 | try { var parsed = JSON.parse(text); if (typeof parsed === 'string') text = parsed; } catch (err) {} | |
| 367 | document.addEventListener('forge:runlog', function (event) { | |
| 368 | if (event.detail.run !== runId) return; | |
| 369 | if (empty) { empty.hidden = true; empty = null; } | |
| 370 | var text = event.detail.line; | |
| 377 | 371 | add(draw(text)); |
| 378 | 372 | if (/^(\\$ |test-shards: |\\(fail\\) |\u2500\u2500 shard )/.test(text)) stepsSoon(); |
| 379 | 373 | }); |
| 380 | source.addEventListener('done', function (event) { | |
| 381 | source.close(); | |
| 382 | var run; | |
| 383 | try { run = JSON.parse(event.data); } catch (err) { return; } | |
| 374 | function finished(run) { | |
| 384 | 375 | paint(run); |
| 385 | 376 | if (stepsTimer) { window.clearTimeout(stepsTimer); stepsTimer = 0; } |
| 386 | 377 | steps(); |
| @@ -393,6 +384,16 @@ export const studioRunJs = /* js */ ` | ||
| 393 | 384 | } |
| 394 | 385 | window.clearInterval(clock); |
| 395 | 386 | tick(); |
| 387 | } | |
| 388 | // The run ending is an event like any other; the finished run itself is | |
| 389 | // one fetch of the API's run, so this page needs nothing of its own. | |
| 390 | document.addEventListener('forge:event', function (event) { | |
| 391 | var e = event.detail; | |
| 392 | if (e.kind !== 'run.finished' || !e.payload || e.payload.runId !== runId || !window.fetch) return; | |
| 393 | fetch('/api/v1/repos' + location.pathname, { credentials: 'same-origin', headers: { accept: 'application/json' } }) | |
| 394 | .then(function (r) { return r.ok ? r.json() : Promise.reject(r); }) | |
| 395 | .then(finished) | |
| 396 | .catch(function () { location.reload(); }); | |
| 396 | 397 | }); |
| 397 | 398 | })(); |
| 398 | 399 | `; |
| @@ -417,9 +418,8 @@ export const studioRunJs = /* js */ ` | ||
| 417 | 418 | export const appTailJs = /* js */ ` |
| 418 | 419 | (function () { |
| 419 | 420 | var tail = document.getElementById('app-run-tail'); |
| 420 | if (!tail || typeof EventSource === 'undefined') return; | |
| 421 | var url = tail.getAttribute('data-stream'); | |
| 422 | if (!url) return; | |
| 421 | var runId = tail && tail.getAttribute('data-run'); | |
| 422 | if (!runId) return; | |
| 423 | 423 | var who = tail.getAttribute('data-who') || 'the crew'; |
| 424 | 424 | var cap = parseInt(tail.getAttribute('data-cap') || '0', 10) || 0; |
| 425 | 425 | |
| @@ -433,23 +433,17 @@ export const appTailJs = /* js */ ` | ||
| 433 | 433 | tail.scrollTop = tail.scrollHeight; |
| 434 | 434 | } |
| 435 | 435 | |
| 436 | var source = new EventSource(url); | |
| 437 | var seeded = false; | |
| 438 | source.addEventListener('line', function (event) { | |
| 439 | if (!seeded) { | |
| 440 | if (event.lastEventId === '1') { | |
| 441 | Array.prototype.slice.call(tail.querySelectorAll('.ln')).forEach(function (el) { tail.removeChild(el); }); | |
| 442 | } | |
| 443 | seeded = true; | |
| 444 | } | |
| 445 | var text = event.data; | |
| 446 | try { var parsed = JSON.parse(text); if (typeof parsed === 'string') text = parsed; } catch (err) {} | |
| 447 | add(draw(text)); | |
| 436 | var over = false; | |
| 437 | document.addEventListener('forge:runlog', function (event) { | |
| 438 | if (over || event.detail.run !== runId) return; | |
| 439 | add(draw(event.detail.line)); | |
| 448 | 440 | }); |
| 449 | 441 | // The run is over: stop appending. The row above settles on its final |
| 450 | // word the next time the page's own "Now" region repaints, which the | |
| 451 | // \`run.finished\` event this very moment triggers. | |
| 452 | source.addEventListener('done', function () { source.close(); }); | |
| 442 | // word when the page's "Now" region repaints on the same \`run.finished\`. | |
| 443 | document.addEventListener('forge:event', function (event) { | |
| 444 | var e = event.detail; | |
| 445 | if (e.kind === 'run.finished' && e.payload && e.payload.runId === runId) over = true; | |
| 446 | }); | |
| 453 | 447 | })(); |
| 454 | 448 | `; |
| 455 | 449 | |
| @@ -1937,7 +1931,10 @@ export const boardJs = /* js */ ` | ||
| 1937 | 1931 | export const liveRegionsJs = /* js */ ` |
| 1938 | 1932 | (function () { |
| 1939 | 1933 | var regions = document.querySelectorAll('[data-live]'); |
| 1940 | if (regions.length === 0 || typeof EventSource === 'undefined') return; | |
| 1934 | // Runs whose log lines this page follows (a run page's log, the app page's | |
| 1935 | // tail): they ride the same stream, as \`runlog\` frames. | |
| 1936 | var runs = Array.prototype.map.call(document.querySelectorAll('[data-run]'), function (el) { return el.getAttribute('data-run'); }).filter(Boolean); | |
| 1937 | if ((regions.length === 0 && runs.length === 0) || typeof EventSource === 'undefined') return; | |
| 1941 | 1938 | function matches(patterns, kind, repo, region) { |
| 1942 | 1939 | var want = region.getAttribute('data-live-repo'); |
| 1943 | 1940 | if (want && want !== repo) return false; |
| @@ -2058,13 +2055,21 @@ export const liveRegionsJs = /* js */ ` | ||
| 2058 | 2055 | function connect() { |
| 2059 | 2056 | if (retry) { clearTimeout(retry); retry = null; } |
| 2060 | 2057 | if (source) source.close(); |
| 2061 | source = since ? new EventSource('/events?since=' + encodeURIComponent(since)) : new EventSource('/events'); | |
| 2058 | var query = (since ? ['since=' + encodeURIComponent(since)] : []).concat(runs.map(function (id) { return 'run=' + encodeURIComponent(id); })).join('&'); | |
| 2059 | source = new EventSource(query ? '/events?' + query : '/events'); | |
| 2062 | 2060 | source.addEventListener('open', function () { state('open'); }); |
| 2063 | 2061 | source.addEventListener('error', function () { |
| 2064 | 2062 | state('reconnecting'); |
| 2065 | 2063 | if (source.readyState === 2 && retry === null) retry = setTimeout(connect, 3000); |
| 2066 | 2064 | }); |
| 2067 | 2065 | source.addEventListener('forge', onForge); |
| 2066 | // Lines are not history: no id, no replay. A tab that missed some reads the file. | |
| 2067 | source.addEventListener('runlog', function (event) { | |
| 2068 | var data; | |
| 2069 | try { data = JSON.parse(event.data); } catch (err) { return; } | |
| 2070 | if (data && typeof data.run === 'string' && typeof data.line === 'string') | |
| 2071 | document.dispatchEvent(new CustomEvent('forge:runlog', { detail: data })); | |
| 2072 | }); | |
| 2068 | 2073 | } |
| 2069 | 2074 | function onForge(event) { |
| 2070 | 2075 | if (event.lastEventId) since = event.lastEventId; |
src/web/studio/log.ts +3 −2
| @@ -58,7 +58,8 @@ export function logBox(opts: { | ||
| 58 | 58 | who: string; |
| 59 | 59 | cap: number; |
| 60 | 60 | lines: readonly string[]; |
| 61 | stream?: string; | |
| 61 | /** The run whose lines patch in over the page's one /events stream. */ | |
| 62 | run?: string; | |
| 62 | 63 | pulse?: boolean; |
| 63 | 64 | className?: string; |
| 64 | 65 | extra?: Html; |
| @@ -74,7 +75,7 @@ export function logBox(opts: { | ||
| 74 | 75 | id="${opts.id}" |
| 75 | 76 | data-who="${opts.who}" |
| 76 | 77 | data-cap="${opts.cap}" |
| 77 | ${opts.stream ? html`data-stream="${opts.stream}"` : null} | |
| 78 | ${opts.run ? html`data-run="${opts.run}"` : null} | |
| 78 | 79 | > |
| 79 | 80 | ${when( |
| 80 | 81 | hidden > 0, |
src/web/studio/now.ts +1 −1
| @@ -179,7 +179,7 @@ export async function nowCard( | ||
| 179 | 179 | activeRun.id, |
| 180 | 180 | LOG_TAIL_CAP, |
| 181 | 181 | ).lines, |
| 182 | stream: `/api/v1/repos/${repo.ownerName}/${repo.name}/runs/${activeRun.id}/log?follow=1`, | |
| 182 | run: activeRun.id, | |
| 183 | 183 | className: "log-tail", |
| 184 | 184 | }) |
| 185 | 185 | : undefined; |
src/web/studio/previews.ts +2 −3
| @@ -230,7 +230,6 @@ function productionTail( | ||
| 230 | 230 | limit: 8, |
| 231 | 231 | }).some((r) => r.state === "running"); |
| 232 | 232 | if (agentBusy) return none; |
| 233 | const stream = `/api/v1/repos/${repo.ownerName}/${repo.name}/runs/${production.runId}/log?follow=1`; | |
| 234 | 233 | if (deploying) |
| 235 | 234 | return { |
| 236 | 235 | box: logBox({ |
| @@ -239,7 +238,7 @@ function productionTail( | ||
| 239 | 238 | cap: LOG_TAIL_CAP, |
| 240 | 239 | lines: readRunLogTail(ctx.app.config.dataDir, production.runId, LOG_TAIL_CAP) |
| 241 | 240 | .lines, |
| 242 | stream, | |
| 241 | run: production.runId, | |
| 243 | 242 | className: "log-tail", |
| 244 | 243 | }), |
| 245 | 244 | deploying: true, |
| @@ -262,7 +261,7 @@ function productionTail( | ||
| 262 | 261 | who: "the app", |
| 263 | 262 | cap: LOG_TAIL_CAP, |
| 264 | 263 | lines, |
| 265 | stream, | |
| 264 | run: production.runId, | |
| 266 | 265 | className: "log-tail", |
| 267 | 266 | }), |
| 268 | 267 | deploying: false, |
src/web/studio/run.ts +3 −4
| @@ -25,7 +25,7 @@ import { getUserById } from "../../core/users.ts"; | ||
| 25 | 25 | import { repoDir } from "../../git/repos.ts"; |
| 26 | 26 | import type { Ctx } from "../../http/context.ts"; |
| 27 | 27 | import { crumbsFor, repoBase, repoHeader, repoPalette } from "../chrome.ts"; |
| 28 | import { studioRunJs } from "../client.ts"; | |
| 28 | import { liveRegionsJs, studioRunJs } from "../client.ts"; | |
| 29 | 29 | import { html, raw, when, type Html } from "../html.ts"; |
| 30 | 30 | import { runLine } from "../describe.ts"; |
| 31 | 31 | import { page } from "../layout.ts"; |
| @@ -107,7 +107,6 @@ export async function runView( | ||
| 107 | 107 | const all = ctx.url.searchParams.get("all") === "1"; |
| 108 | 108 | const held = listHeldSecrets(db, run.id); |
| 109 | 109 | const carried = carriedBy(db, run.id); |
| 110 | const stream = `/api/v1/repos/${repo.ownerName}/${repo.name}/runs/${run.id}/log?follow=1`; | |
| 111 | 110 | |
| 112 | 111 | // The pull this run's tell has become, once one exists: the tell itself |
| 113 | 112 | // when the run is already a pull's own (feedback, a failed check, a |
| @@ -308,7 +307,7 @@ export async function runView( | ||
| 308 | 307 | lines, |
| 309 | 308 | allHref: `${base}/runs/${run.id}?all=1`, |
| 310 | 309 | truncated, |
| 311 | ...(unfinished ? { stream } : {}), | |
| 310 | ...(unfinished ? { run: run.id } : {}), | |
| 312 | 311 | pulse: run.state === "running", |
| 313 | 312 | extra: html`<button type="button" class="run-jump" id="run-jump" hidden> |
| 314 | 313 | newer below |
| @@ -383,7 +382,7 @@ export async function runView( | ||
| 383 | 382 | crumbs: crumbsFor(repo), |
| 384 | 383 | palette: repoPalette(ctx, repo), |
| 385 | 384 | body, |
| 386 | scriptJs: studioRunJs, | |
| 385 | scriptJs: `${liveRegionsJs}\n${studioRunJs}`, | |
| 387 | 386 | // The no-script half of a live log: with JavaScript off, the page |
| 388 | 387 | // refreshes itself while the run is going (`layout.ts` writes it inside |
| 389 | 388 | // `<noscript>`, so a browser running the stream above never reloads). |
test/run-log-audience.test.ts +54 −1
| @@ -21,7 +21,7 @@ import { addCrew } from "../src/core/agents.ts"; | ||
| 21 | 21 | import { createToken } from "../src/core/tokens.ts"; |
| 22 | 22 | import { createUser, type User } from "../src/core/users.ts"; |
| 23 | 23 | import { forgeHttp, type ForgeHttp } from "../src/http/server.ts"; |
| 24 | import { appendRunLog } from "../src/runs/log.ts"; | |
| 24 | import { anyRunLogListenerCount, appendRunLog } from "../src/runs/log.ts"; | |
| 25 | 25 | import { quietLogs } from "./helpers/globals.ts"; |
| 26 | 26 | |
| 27 | 27 | quietLogs(); |
| @@ -221,3 +221,56 @@ describe("run log audience", () => { | ||
| 221 | 221 | finishRun(app.db, run.id, { state: "ok", summary: "done" }); |
| 222 | 222 | }); |
| 223 | 223 | }); |
| 224 | ||
| 225 | describe("run log lines on /events", () => { | |
| 226 | async function frames(who: string | undefined, id: string, emit: () => void) { | |
| 227 | const res = await http.handle( | |
| 228 | new Request(`http://forge.test/events?run=${id}`, { | |
| 229 | headers: | |
| 230 | who === undefined ? {} : { authorization: `Bearer ${token[who]}` }, | |
| 231 | }), | |
| 232 | ); | |
| 233 | const reader = res.body!.getReader(); | |
| 234 | await reader.read(); // ": connected" | |
| 235 | emit(); | |
| 236 | await Bun.sleep(30); | |
| 237 | const out: string[] = []; | |
| 238 | const more = reader.read().then((r) => out.push(new TextDecoder().decode(r.value))); | |
| 239 | await Promise.race([more, Bun.sleep(100)]); | |
| 240 | await reader.cancel(); | |
| 241 | return out.join(""); | |
| 242 | } | |
| 243 | ||
| 244 | test("a writer hears a redacted, id-less runlog frame; a stranger hears nothing", async () => { | |
| 245 | const run = createRun(app.db, { | |
| 246 | repoId: repoRow.id, | |
| 247 | kind: "agent", | |
| 248 | actorId: aliceId, | |
| 249 | reason: "assigned", | |
| 250 | }); | |
| 251 | claimRun(app.db, run.id); | |
| 252 | const say = () => | |
| 253 | appendRunLog(dataDir, run.id, "R8LIVE token=ghp_abcdefghijklmnopqrstuvwxyz0123456789"); | |
| 254 | const mine = await frames("alice", run.id, say); | |
| 255 | expect(mine).toContain("event: runlog"); | |
| 256 | expect(mine).toContain("R8LIVE"); | |
| 257 | expect(mine).not.toContain("ghp_abcdefghijklmnopqrstuvwxyz0123456789"); | |
| 258 | expect(mine).not.toContain("\nid:"); | |
| 259 | for (const who of [undefined, "mallory"]) | |
| 260 | expect(await frames(who, run.id, say)).not.toContain("R8LIVE"); | |
| 261 | // A run the tab did not ask about is not sent, and listeners are released. | |
| 262 | expect(await frames("alice", "01zzzzzzzzzzzzzzzzzzzzzzzz", say)).not.toContain("R8LIVE"); | |
| 263 | expect(anyRunLogListenerCount()).toBe(0); | |
| 264 | }); | |
| 265 | ||
| 266 | test("a run page and the app tail open no EventSource beside /events", async () => { | |
| 267 | const page = await get(`/alice/square/runs/${runId}`, "alice"); | |
| 268 | expect(page.text).not.toContain("log?follow=1"); | |
| 269 | expect(page.text).not.toContain("data-stream"); | |
| 270 | const { studioRunJs, appTailJs, liveRegionsJs } = await import("../src/web/client.ts"); | |
| 271 | for (const js of [studioRunJs, appTailJs]) | |
| 272 | expect(js).not.toContain("new EventSource"); | |
| 273 | expect(liveRegionsJs.match(/new EventSource/g)?.length).toBe(1); | |
| 274 | expect(liveRegionsJs).toContain("/events?"); | |
| 275 | }); | |
| 276 | }); |
test/studio-runs.test.ts +5 −5
| @@ -936,7 +936,7 @@ describe("the app's page tails the crew's log while it works", () => { | ||
| 936 | 936 | const page = await (await get("/alice/engine", writerCookie)).text(); |
| 937 | 937 | expect(page).toContain('id="app-run-tail"'); |
| 938 | 938 | expect(page).toContain( |
| 939 | `data-stream="/api/v1/repos/alice/engine/runs/${run.id}/log?follow=1"`, | |
| 939 | `data-run="${run.id}"`, | |
| 940 | 940 | ); |
| 941 | 941 | // Bounded on the first paint too: only the last handful made it, and |
| 942 | 942 | // the box says so. |
| @@ -1132,7 +1132,7 @@ describe("the app's page tails the deploy that is bringing it alive", () => { | ||
| 1132 | 1132 | expect(page).toContain('id="app-run-tail"'); |
| 1133 | 1133 | expect(page).toContain('data-who="the deploy"'); |
| 1134 | 1134 | expect(page).toContain( |
| 1135 | `data-stream="/api/v1/repos/alice/kindling/runs/${deployRun.id}/log?follow=1"`, | |
| 1135 | `data-run="${deployRun.id}"`, | |
| 1136 | 1136 | ); |
| 1137 | 1137 | expect(page).toContain("install …"); |
| 1138 | 1138 | // No script, no problem: the page keeps refreshing while the deploy |
| @@ -1184,7 +1184,7 @@ describe("the app's page tails the deploy that is bringing it alive", () => { | ||
| 1184 | 1184 | expect(page.match(/id="app-run-tail"/g)?.length).toBe(1); |
| 1185 | 1185 | expect(page).not.toContain('data-who="the deploy"'); |
| 1186 | 1186 | expect(page).toContain( |
| 1187 | `data-stream="/api/v1/repos/alice/kindling/runs/${agentRun.id}/log?follow=1"`, | |
| 1187 | `data-run="${agentRun.id}"`, | |
| 1188 | 1188 | ); |
| 1189 | 1189 | |
| 1190 | 1190 | finishRun(app.db, deployRun.id, { state: "ok", summary: "deploy" }); |
| @@ -1350,7 +1350,7 @@ describe("the app's page tails production's own output while it runs (#284)", () | ||
| 1350 | 1350 | expect(page).toContain('id="app-run-tail"'); |
| 1351 | 1351 | expect(page).toContain('data-who="the app"'); |
| 1352 | 1352 | expect(page).toContain( |
| 1353 | `data-stream="/api/v1/repos/alice/furnace/runs/${deployRun.id}/log?follow=1"`, | |
| 1353 | `data-run="${deployRun.id}"`, | |
| 1354 | 1354 | ); |
| 1355 | 1355 | expect(page).not.toContain(">serving request 0<"); |
| 1356 | 1356 | expect(page).toContain(">serving request 8<"); |
| @@ -1389,7 +1389,7 @@ describe("the app's page tails production's own output while it runs (#284)", () | ||
| 1389 | 1389 | expect(page.match(/id="app-run-tail"/g)?.length).toBe(1); |
| 1390 | 1390 | expect(page).not.toContain('data-who="the app"'); |
| 1391 | 1391 | expect(page).toContain( |
| 1392 | `data-stream="/api/v1/repos/alice/furnace/runs/${agentRun.id}/log?follow=1"`, | |
| 1392 | `data-run="${agentRun.id}"`, | |
| 1393 | 1393 | ); |
| 1394 | 1394 | |
| 1395 | 1395 | finishRun(app.db, agentRun.id, { state: "ok", summary: "planned" }); |
new above
offline