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

The app page's run log and tail update from /events, not their own EventSources #2850

Parked opened this
Closed without merging

scout/forge:scout/2844-the-app-page-s-run-log-and-tail-update-f → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +55 −20
src/web/client.ts +16 −20
@@ -406,9 +406,8 @@ export const studioRunJs = /* js */ `
406406 * bounds what stays in the DOM as new lines arrive — a chatty run left open
407407 * cannot grow the page without bound. There is exactly one of these on a
408408 * page at once (an agent run's own work outranks a background deploy, so
409 * only one of the two ever renders the id), so it opens one more stream
410 * beside the app's own state stream and the page's live regions, never one
411 * per row. It carries no controls of its own: Stop lives on the row, the
409 * only one of the two ever renders the id), so it opens no stream of its own: it
410 * listens for the run's \`run.log\` frames on the page's one /events connection. It carries no controls of its own: Stop lives on the row, the
412411 * full transcript is one click away, and the row's own word — or the head
413412 * card's own state — swaps in once the run is over (the page's existing
414413 * live regions already repaint on \`run.*\`) — this only ever appends lines
@@ -417,7 +416,7 @@ export const studioRunJs = /* js */ `
417416 export const appTailJs = /* js */ `
418417 (function () {
419418 var tail = document.getElementById('app-run-tail');
420 if (!tail || typeof EventSource === 'undefined') return;
419 if (!tail) return;
421420 var url = tail.getAttribute('data-stream');
422421 if (!url) return;
423422 var who = tail.getAttribute('data-who') || 'the crew';
@@ -433,23 +432,16 @@ export const appTailJs = /* js */ `
433432 tail.scrollTop = tail.scrollHeight;
434433 }
435434
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));
435 // The run's id is in the stream's address; the lines themselves arrive as
436 // \`run.log\` frames on the page's one /events connection (liveRegionsJs).
437 var m = /\\/runs\\/([^\\/?]+)\\/log/.exec(url);
438 if (!m) return;
439 var run = m[1];
440 document.addEventListener('forge:runlog', function (event) {
441 var d = event.detail;
442 if (!d || d.run !== run || !Array.isArray(d.lines)) return;
443 d.lines.forEach(function (text) { if (typeof text === 'string') add(draw(text)); });
448444 });
449 // 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(); });
453445 })();
454446 `;
455447
@@ -2065,6 +2057,10 @@ export const liveRegionsJs = /* js */ `
20652057 if (source.readyState === 2 && retry === null) retry = setTimeout(connect, 3000);
20662058 });
20672059 source.addEventListener('forge', onForge);
2060 // A run's new log lines ride the same connection; the app page's tail listens.
2061 source.addEventListener('run.log', function (event) {
2062 try { document.dispatchEvent(new CustomEvent('forge:runlog', { detail: JSON.parse(event.data) })); } catch (err) {}
2063 });
20682064 }
20692065 function onForge(event) {
20702066 if (event.lastEventId) since = event.lastEventId;
test/app-tail-events.test.ts added +39
@@ -0,0 +1,39 @@
1 import { describe, expect, test } from "bun:test";
2 import { appTailJs, liveRegionsJs } from "../src/web/client.ts";
3
4 describe("the app page's run-log tail", () => {
5 test("opens no EventSource of its own and listens for run.log frames", () => {
6 expect(appTailJs).not.toContain("EventSource");
7 expect(appTailJs).toContain("forge:runlog");
8 });
9
10 test("the one /events connection forwards run.log frames", () => {
11 expect(liveRegionsJs).toContain("addEventListener('run.log'");
12 expect(liveRegionsJs.match(/new EventSource\(/g)?.length).toBe(2);
13 expect(liveRegionsJs).toContain("new EventSource('/events')");
14 });
15
16 test("the tail script is valid JavaScript and appends only its own run's lines", () => {
17 const added: string[] = [];
18 const listeners: Record<string, (e: unknown) => void> = {};
19 const tail = {
20 getAttribute: (n: string) =>
21 n === "data-stream" ? "/api/v1/repos/a/b/runs/r1/log?follow=1" : null,
22 appendChild: (el: string) => added.push(el),
23 querySelectorAll: () => [],
24 scrollTop: 0,
25 scrollHeight: 0,
26 };
27 const document = {
28 getElementById: () => tail,
29 addEventListener: (n: string, f: (e: unknown) => void) => (listeners[n] = f),
30 createElement: () => ({ appendChild() {}, className: "", textContent: "" }),
31 createTextNode: () => ({}),
32 };
33 const src = appTailJs.replace("add(draw(text))", "added.push(text)");
34 new Function("document", "added", src)(document, added);
35 listeners["forge:runlog"]({ detail: { run: "r2", lines: ["no"] } });
36 listeners["forge:runlog"]({ detail: { run: "r1", lines: ["yes", "two"] } });
37 expect(added).toEqual(["yes", "two"]);
38 });
39 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs