Public: anyone can read this; the crew can change it.
A run page shows its log live from /events and opens no EventSource of its own #2851
scout/forge:scout/2843-a-run-page-shows-its-log-live-from-event → main 2 commits fork
4 files
changed +88
−23
src/web/client.ts +37 −19
| @@ -224,7 +224,6 @@ 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'); | |
| 228 | 227 | var who = log.getAttribute('data-who') || 'agent'; |
| 229 | 228 | var cap = parseInt(log.getAttribute('data-cap') || '0', 10) || 0; |
| 230 | 229 | var pulse = document.getElementById('run-pulse'); |
| @@ -360,28 +359,47 @@ export const studioRunJs = /* js */ ` | ||
| 360 | 359 | }); |
| 361 | 360 | } |
| 362 | 361 | |
| 363 | if (!url || typeof EventSource === 'undefined') return; | |
| 364 | var source = new EventSource(url); | |
| 362 | // The page opens no stream of its own: the shared /events connection | |
| 363 | // (liveRegionsJs) carries this run's lines as run.log frames, handed on as | |
| 364 | // forge:run-log, and its state change as the run.finished event. | |
| 365 | var runId = log.getAttribute('data-run'); | |
| 366 | if (!runId) return; | |
| 367 | var logUrl = log.getAttribute('data-log'); | |
| 365 | 368 | 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) {} | |
| 369 | // Lines on the page: those the server drew from the file, then each added | |
| 370 | // here. A catch-up read of the file adds only what is past that count, and | |
| 371 | // frames wait for it, so a line is never drawn twice. | |
| 372 | var have = parseInt(log.getAttribute('data-seen') || '-1', 10); | |
| 373 | var catching = false; | |
| 374 | function put(text) { | |
| 375 | if (empty) empty.hidden = true; | |
| 377 | 376 | add(draw(text)); |
| 377 | have++; | |
| 378 | 378 | if (/^(\\$ |test-shards: |\\(fail\\) |\u2500\u2500 shard )/.test(text)) stepsSoon(); |
| 379 | } | |
| 380 | function catchUp() { | |
| 381 | if (have < 0 || !logUrl || !window.fetch || catching) return; | |
| 382 | catching = true; | |
| 383 | fetch(logUrl, { credentials: 'same-origin' }) | |
| 384 | .then(function (r) { return r.ok ? r.text() : ''; }) | |
| 385 | .then(function (body) { | |
| 386 | var all = body.split('\n'); | |
| 387 | if (all[all.length - 1] === '') all.pop(); | |
| 388 | all.slice(have).forEach(put); | |
| 389 | }) | |
| 390 | .catch(function () {}) | |
| 391 | .then(function () { catching = false; }); | |
| 392 | } | |
| 393 | document.addEventListener('forge:live', function (e) { if (e.detail && e.detail.state === 'open') catchUp(); }); | |
| 394 | document.addEventListener('forge:run-log', function (e) { | |
| 395 | var d = e.detail; | |
| 396 | if (!d || d.run !== runId || catching || !Array.isArray(d.lines)) return; | |
| 397 | d.lines.forEach(function (l) { if (typeof l === 'string') put(l); }); | |
| 379 | 398 | }); |
| 380 | source.addEventListener('done', function (event) { | |
| 381 | source.close(); | |
| 382 | var run; | |
| 383 | try { run = JSON.parse(event.data); } catch (err) { return; } | |
| 384 | paint(run); | |
| 399 | document.addEventListener('forge:event', function (e) { | |
| 400 | var d = e.detail; | |
| 401 | if (!d || d.kind !== 'run.finished' || !d.payload || d.payload.runId !== runId) return; | |
| 402 | paint(d.payload); | |
| 385 | 403 | if (stepsTimer) { window.clearTimeout(stepsTimer); stepsTimer = 0; } |
| 386 | 404 | steps(); |
| 387 | 405 | if (steer) { |
src/web/studio/log.ts +5
| @@ -59,6 +59,9 @@ export function logBox(opts: { | ||
| 59 | 59 | cap: number; |
| 60 | 60 | lines: readonly string[]; |
| 61 | 61 | stream?: string; |
| 62 | /** A run still going, read live from the shared /events stream: its id, | |
| 63 | * where the plain log is read to catch up, and how many lines are drawn. */ | |
| 64 | live?: { run: string; log: string; seen?: number }; | |
| 62 | 65 | pulse?: boolean; |
| 63 | 66 | className?: string; |
| 64 | 67 | extra?: Html; |
| @@ -75,6 +78,8 @@ export function logBox(opts: { | ||
| 75 | 78 | data-who="${opts.who}" |
| 76 | 79 | data-cap="${opts.cap}" |
| 77 | 80 | ${opts.stream ? html`data-stream="${opts.stream}"` : null} |
| 81 | ${opts.live ? html`data-run="${opts.live.run}" data-log="${opts.live.log}"` : null} | |
| 82 | ${opts.live?.seen !== undefined ? html`data-seen="${opts.live.seen}"` : null} | |
| 78 | 83 | > |
| 79 | 84 | ${when( |
| 80 | 85 | hidden > 0, |
src/web/studio/run.ts +15 −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,7 @@ 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`; | |
| 110 | const logPath = `/api/v1/repos/${repo.ownerName}/${repo.name}/runs/${run.id}/log`; | |
| 111 | 111 | |
| 112 | 112 | // The pull this run's tell has become, once one exists: the tell itself |
| 113 | 113 | // when the run is already a pull's own (feedback, a failed check, a |
| @@ -308,7 +308,18 @@ export async function runView( | ||
| 308 | 308 | lines, |
| 309 | 309 | allHref: `${base}/runs/${run.id}?all=1`, |
| 310 | 310 | truncated, |
| 311 | ...(unfinished ? { stream } : {}), | |
| 311 | // Lines come from the shared /events stream; `seen` lets the page | |
| 312 | // tell what it already drew from what it missed before connecting | |
| 313 | // (only knowable when the file was read whole). | |
| 314 | ...(unfinished | |
| 315 | ? { | |
| 316 | live: { | |
| 317 | run: run.id, | |
| 318 | log: logPath, | |
| 319 | ...(truncated ? {} : { seen: lines.length }), | |
| 320 | }, | |
| 321 | } | |
| 322 | : {}), | |
| 312 | 323 | pulse: run.state === "running", |
| 313 | 324 | extra: html`<button type="button" class="run-jump" id="run-jump" hidden> |
| 314 | 325 | newer below |
| @@ -383,7 +394,7 @@ export async function runView( | ||
| 383 | 394 | crumbs: crumbsFor(repo), |
| 384 | 395 | palette: repoPalette(ctx, repo), |
| 385 | 396 | body, |
| 386 | scriptJs: studioRunJs, | |
| 397 | scriptJs: `${liveRegionsJs}\n${studioRunJs}`, | |
| 387 | 398 | // The no-script half of a live log: with JavaScript off, the page |
| 388 | 399 | // refreshes itself while the run is going (`layout.ts` writes it inside |
| 389 | 400 | // `<noscript>`, so a browser running the stream above never reloads). |
test/studio-runs.test.ts +31
| @@ -520,6 +520,37 @@ describe("working: a run's own page in the studio", () => { | ||
| 520 | 520 | expect(page).not.toContain("?mode=dev"); |
| 521 | 521 | }); |
| 522 | 522 | |
| 523 | test("a running run's page reads its log from the one /events stream and opens no other", async () => { | |
| 524 | const run = createRun(app.db, { | |
| 525 | repoId: repo.id, | |
| 526 | kind: "agent", | |
| 527 | actorId: scoutId, | |
| 528 | reason: "assigned", | |
| 529 | }); | |
| 530 | claimRun(app.db, run.id); | |
| 531 | appendRunLog(app.config.dataDir, run.id, "first"); | |
| 532 | appendRunLog(app.config.dataDir, run.id, "second"); | |
| 533 | ||
| 534 | const page = await ( | |
| 535 | await get(`/alice/engine/runs/${run.id}`, writerCookie) | |
| 536 | ).text(); | |
| 537 | // Every EventSource the page's HTML and script open is /events. | |
| 538 | const opened = [...page.matchAll(/new EventSource\(([^,;]*)/g)].map( | |
| 539 | (m) => m[1]!, | |
| 540 | ); | |
| 541 | expect(opened.length).toBeGreaterThan(0); | |
| 542 | for (const arg of opened) expect(arg).toStartWith("'/events"); | |
| 543 | expect(page).not.toContain("follow=1"); | |
| 544 | expect(page).not.toContain("data-stream="); | |
| 545 | // The run's frames arrive on that connection; the file's lines are | |
| 546 | // counted so none is drawn twice. | |
| 547 | expect(page).toContain("'run.log'"); | |
| 548 | expect(page).toContain("forge:run-log"); | |
| 549 | expect(page).toContain(`data-run="${run.id}"`); | |
| 550 | expect(page).toContain('data-seen="2"'); | |
| 551 | expect(page).toContain(">second<"); | |
| 552 | }); | |
| 553 | ||
| 523 | 554 | test("a long transcript holds its last window, and the whole of it is one step deeper at ?all=1", async () => { |
| 524 | 555 | const run = createRun(app.db, { |
| 525 | 556 | repoId: repo.id, |
new above
offline