Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Smooth chat streaming and preserve rendered Markdown (#1182)

Joshua Francef4b0439d6fd7
diff --git a/package.json b/package.json
index f5166e068..b89ff7d44 100644
--- a/package.json
+++ b/package.json
@@ -36,7 +36,7 @@
"sandbox:local:build": "bash scripts/local-sandbox-build.sh",
"sandbox:porter:build": "docker build --platform linux/amd64 -t \"${PORTER_SANDBOX_IMAGE:?Set PORTER_SANDBOX_IMAGE to the registry tag to publish}\" porter && docker push \"$PORTER_SANDBOX_IMAGE\"",
"deploy:porter-runner-image": "docker build --platform linux/amd64 -t \"${PORTER_DEPLOY_RUNNER_IMAGE:?Set PORTER_DEPLOY_RUNNER_IMAGE to the registry tag to publish}\" -f deploy/porter-runner/Dockerfile . && docker push \"$PORTER_DEPLOY_RUNNER_IMAGE\"",
- "test:pg": "node --test --test-concurrency=1 test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-grant-store.test.ts test/postgres-admin-grants.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-map.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-migration-adoption.test.ts test/cron-queue.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/postgres-instance-registry.test.ts test/run-retry-delay.test.ts",
+ "test:pg": "node --test --test-concurrency=1 test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-grant-store.test.ts test/postgres-admin-grants.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-map.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-migration-adoption.test.ts test/cron-queue.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/postgres-instance-registry.test.ts test/run-retry-delay.test.ts test/postgres-run-stream.test.ts",
"livetest": "node scripts/api-livetest.ts",
"bench:memory": "node --env-file-if-exists=.env scripts/memory-bench.ts",
"prepare": "node .husky/install.mjs",
diff --git a/plugins/web-ui/package-lock.json b/plugins/web-ui/package-lock.json
index 49d6b5535..f6e105204 100644
--- a/plugins/web-ui/package-lock.json
+++ b/plugins/web-ui/package-lock.json
@@ -13,6 +13,8 @@
"@earendil-works/pi-ai": "0.77.0",
"@earendil-works/pi-web-ui": "0.75.3",
"@mariozechner/mini-lit": "^0.2.0",
+ "@tanstack/ai": "0.54.0",
+ "@tanstack/ai-client": "0.31.1",
"date-fns": "^4.4.0",
"dockview-core": "^7.0.4",
"dompurify": "^3.4.13",
@@ -20,7 +22,8 @@
"lit": "^3.3.1",
"lru-cache": "^11.5.2",
"lucide": "^0.544.0",
- "marked": "^18.0.5"
+ "marked": "^18.0.5",
+ "morphdom": "2.7.8"
},
"devDependencies": {
"@types/jsdom": "^28.0.3",
@@ -34,6 +37,20 @@
"node": ">=24.0.0"
}
},
+ "node_modules/@ag-ui/core": {
+ "version": "0.1.1-canary.beta.0",
+ "resolved": "https://registry.npmjs.org/@ag-ui/core/-/core-0.1.1-canary.beta.0.tgz",
+ "integrity": "sha512-zcP3RH5p3HJTZ0kyQxNOL1blnVFTDpaShitR7UMS/thwFdl48V6CAG+WQBcYEu/dfa8mKonRez88N0Ok+nEI+w==",
+ "license": "MIT",
+ "peerDependencies": {
+ "zod": "^3.25.18 || ^4.0.0"
+ },
+ "peerDependenciesMeta": {
+ "zod": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@anthropic-ai/sdk": {
"version": "0.91.1",
"resolved": "https://registry.npmjs.org/@anthropic-ai/sdk/-/sdk-0.91.1.tgz",
@@ -664,7 +681,6 @@
}
],
"license": "MIT",
- "peer": true,
"engines": {
"node": ">=20.19.0"
},
@@ -713,7 +729,6 @@
}
],
"license": "MIT",
- "peer": true,
"engines": {
"node": ">=20.19.0"
}
@@ -1335,7 +1350,6 @@
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/@mariozechner/mini-lit/-/mini-lit-0.2.1.tgz",
"integrity": "sha512-u300euLgCsDDlb8o2Wbz+55eSJga5X2vB58s9XBuFIr2Bi3iI+GMR7t/NYo/O6Vr6obXShXgYjR3SRUJVgo+kQ==",
- "peer": true,
"dependencies": {
"@preact/signals-core": "^1.12.1",
"class-variance-authority": "^0.7.1",
@@ -2163,6 +2177,94 @@
"node": ">=14.0.0"
}
},
+ "node_modules/@standard-schema/spec": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz",
+ "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==",
+ "license": "MIT"
+ },
+ "node_modules/@tanstack/ai": {
+ "version": "0.54.0",
+ "resolved": "https://registry.npmjs.org/@tanstack/ai/-/ai-0.54.0.tgz",
+ "integrity": "sha512-Hc7Yc7L+2n/dr11GwAT/QSbGwIxy5bhRe99yu1jAhGaBx28Ul3AaTLcGTyEj2aNeih00AUtF0KG+IOqJBSsj8A==",
+ "license": "MIT",
+ "dependencies": {
+ "@ag-ui/core": "0.1.1-canary.beta.0",
+ "@standard-schema/spec": "^1.1.0",
+ "@tanstack/ai-event-client": "^0.11.3",
+ "@tanstack/ai-utils": "^0.4.0",
+ "partial-json": "^0.1.7"
+ },
+ "engines": {
+ "node": ">=18"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/tannerlinsley"
+ },
+ "peerDependencies": {
+ "@opentelemetry/api": ">=1.9.0"
+ },
+ "peerDependenciesMeta": {
+ "@opentelemetry/api": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@tanstack/ai-client": {
+ "version": "0.31.1",
+ "resolved": "https://registry.npmjs.org/@tanstack/ai-client/-/ai-client-0.31.1.tgz",
+ "integrity": "sha512-45JeYoVb53q4nD9Bk00mdQUGyYKiP9CrvKRRmXN460I1YHdxiSQRbiL5n4POLXUQUcCYFDL2bxSq5I84Eu6/cw==",
+ "license": "MIT",
+ "dependencies": {
+ "@tanstack/ai": "^0.54.0",
+ "@tanstack/ai-event-client": "^0.11.3",
+ "@tanstack/ai-utils": "^0.4.0"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/tannerlinsley"
+ }
+ },
+ "node_modules/@tanstack/ai-event-client": {
+ "version": "0.11.3",
+ "resolved": "https://registry.npmjs.org/@tanstack/ai-event-client/-/ai-event-client-0.11.3.tgz",
+ "integrity": "sha512-K5Nw05TG2NPJNunV7LI/5m7S97XHHdxN7vwTYcbKbmA5txlToaiF4wCfYBT406m7BOeGlLHZJpr3m1M6Vaai4g==",
+ "license": "MIT",
+ "dependencies": {
+ "@tanstack/devtools-event-client": "^0.4.1"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/tannerlinsley"
+ }
+ },
+ "node_modules/@tanstack/ai-utils": {
+ "version": "0.4.0",
+ "resolved": "https://registry.npmjs.org/@tanstack/ai-utils/-/ai-utils-0.4.0.tgz",
+ "integrity": "sha512-xqvAgPJkIuGk1m+jZKyb7vBTQIaBmUD9EVzlPkDWWMp80n69uwL0TnBZCVk+OwL9goTQiFy648dnBfLsiJgl6A==",
+ "license": "MIT",
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/tannerlinsley"
+ }
+ },
+ "node_modules/@tanstack/devtools-event-client": {
+ "version": "0.4.4",
+ "resolved": "https://registry.npmjs.org/@tanstack/devtools-event-client/-/devtools-event-client-0.4.4.tgz",
+ "integrity": "sha512-6T5Yop/793YI+H+5J8Hsyj4kCih9sl4t3ElLgKioW5hk3ocn+ZdSJ94tT7vL7uabxSugWYBZlOTMPzEw2puvQw==",
+ "license": "MIT",
+ "bin": {
+ "intent": "bin/intent.js"
+ },
+ "engines": {
+ "node": ">=18"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/tannerlinsley"
+ }
+ },
"node_modules/@types/estree": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz",
@@ -2955,7 +3057,6 @@
"resolved": "https://registry.npmjs.org/lit/-/lit-3.3.3.tgz",
"integrity": "sha512-fycuvZg/hkpozL00lm1pEJH5nN/lr9ZXd6mJI2HSN4+Bzc+LDNdEApJ6HFbPkdFNHLvOplIIuJvxkS4XUxqirw==",
"license": "BSD-3-Clause",
- "peer": true,
"dependencies": {
"@lit/reactive-element": "^2.1.0",
"lit-element": "^4.2.0",
@@ -3022,6 +3123,12 @@
"dev": true,
"license": "CC0-1.0"
},
+ "node_modules/morphdom": {
+ "version": "2.7.8",
+ "resolved": "https://registry.npmjs.org/morphdom/-/morphdom-2.7.8.tgz",
+ "integrity": "sha512-D/fR4xgGUyVRbdMGU6Nejea1RFzYxYtyurG4Fbv2Fi/daKlWKuXGLOdXtl+3eIwL110cI2hz1ZojGICjjFLgTg==",
+ "license": "MIT"
+ },
"node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
@@ -3193,7 +3300,6 @@
"integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==",
"dev": true,
"license": "MIT",
- "peer": true,
"engines": {
"node": ">=12"
},
@@ -3454,7 +3560,6 @@
"resolved": "https://registry.npmjs.org/tailwind-merge/-/tailwind-merge-3.6.0.tgz",
"integrity": "sha512-uxL7qAVQriqRQPAyK3pj66VqskWqoZ37PW94jwOTwNfq/z9oyu1V+eqrZqtR2+fCiXdYOZe/Modt8GtvqNzu+w==",
"license": "MIT",
- "peer": true,
"funding": {
"type": "github",
"url": "https://github.com/sponsors/dcastil"
@@ -3835,7 +3940,6 @@
"resolved": "https://registry.npmjs.org/zod/-/zod-3.25.76.tgz",
"integrity": "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==",
"license": "MIT",
- "peer": true,
"funding": {
"url": "https://github.com/sponsors/colinhacks"
}
diff --git a/plugins/web-ui/package.json b/plugins/web-ui/package.json
index 824fb2a22..462d98837 100644
--- a/plugins/web-ui/package.json
+++ b/plugins/web-ui/package.json
@@ -21,6 +21,8 @@
"@earendil-works/pi-ai": "0.77.0",
"@earendil-works/pi-web-ui": "0.75.3",
"@mariozechner/mini-lit": "^0.2.0",
+ "@tanstack/ai": "0.54.0",
+ "@tanstack/ai-client": "0.31.1",
"date-fns": "^4.4.0",
"dockview-core": "^7.0.4",
"dompurify": "^3.4.13",
@@ -28,7 +30,8 @@
"lit": "^3.3.1",
"lru-cache": "^11.5.2",
"lucide": "^0.544.0",
- "marked": "^18.0.5"
+ "marked": "^18.0.5",
+ "morphdom": "2.7.8"
},
"devDependencies": {
"@types/jsdom": "^28.0.3",
diff --git a/plugins/web-ui/server/index.ts b/plugins/web-ui/server/index.ts
index b43cd3bb6..c994d33f4 100644
--- a/plugins/web-ui/server/index.ts
+++ b/plugins/web-ui/server/index.ts
@@ -112,10 +112,6 @@ const runOwners = new Map<string, string>();
const runThreadKeys = new Map<string, string>();
const activeRunsByThread = new Map<string, string[]>();
-function ownsRun(runId: string, user: string): boolean {
- return runOwners.get(runId) === user;
-}
-
function threadKey(user: string, threadRef: string): string {
return `${user}\0${threadRef}`;
}
@@ -242,19 +238,6 @@ function sendHtml(res: ServerResponse, status: number, html: string): void {
sendBuffered(res, status, withSecurityHeaders({ "content-type": "text/html; charset=utf-8" }), html);
}
-const SSE_CORE_POLL_MS = 100;
-const SSE_STALE_POLL_MS = 1_000;
-const SSE_IDLE_MS = 6 * 60_000;
-const SSE_STALE_GRACE_MS = 10 * 60_000;
-const SSE_HEARTBEAT_MS = 15_000;
-
-function sleep(ms: number): Promise<void> {
- return new Promise((r) => {
- const t = setTimeout(r, ms);
- t.unref?.();
- });
-}
-
function sseEvent(res: ServerResponse, event: string, data: unknown): void {
res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
}
@@ -462,6 +445,7 @@ async function drainWebDeliveries(): Promise<void> {
}
}
+const SSE_HEARTBEAT_MS = 15_000;
const STATE_FEED_RECONNECT_MS = Number(process.env.STATE_FEED_RECONNECT_MS ?? 3_000);
interface SessionStateFrame {
@@ -2322,118 +2306,41 @@ const apiRoutes: readonly WebRoute[] = [
method: "GET",
path: "/api/runs/:id/events",
handle: async (c) => {
- const { req, res, user } = c;
+ const { res } = c;
const id = c.params.id!;
- let closed = false;
- req.on("close", () => {
- closed = true;
- });
- if (!ownsRun(id, user)) {
- const auth = await coreFetch("GET", `/v1/runs/${encodeURIComponent(id)}`);
- if (auth.status < 200 || auth.status >= 300)
- return json(res, auth.status === 404 ? 404 : 502, {
- error: auth.status === 404 ? "not_found" : "upstream_error",
- });
- }
- if (closed) return;
- res.writeHead(200, {
- "content-type": "text/event-stream; charset=utf-8",
- "cache-control": "no-cache, no-transform",
- connection: "keep-alive",
- "x-accel-buffering": "no",
- });
- res.write(": open\n\n");
- let acc = "";
- let activityLen = 0;
- let lastStale: boolean | null = null;
- let staleSince: number | null = null;
- let lastProgressAt = Date.now();
- let lastBeat = lastProgressAt;
- for (;;) {
- if (closed) return;
- let r: { status: number; text: string };
- try {
- r = await coreFetch("GET", `/v1/runs/${encodeURIComponent(id)}`);
- } catch {
- try {
- r = await coreFetch("GET", `/v1/runs/${encodeURIComponent(id)}`);
- } catch {
- sseEvent(res, "failed", { reason: "upstream_unreachable" });
- break;
- }
- }
- if (closed) return;
- if (r.status < 200 || r.status >= 300) {
- sseEvent(res, "failed", { reason: `HTTP ${r.status}` });
- break;
- }
- let run: {
- status?: string;
- result?: unknown;
- partial?: string;
- alive?: boolean;
- stale?: boolean;
- replyComplete?: boolean;
- activity?: unknown[];
- startedAt?: number | null;
- finishedAt?: number | null;
- } = {};
- let parsed = true;
- try {
- run = JSON.parse(r.text);
- } catch {
- parsed = false;
- }
- const now = Date.now();
- const partial = typeof run.partial === "string" ? run.partial : "";
- const activity = Array.isArray(run.activity) ? run.activity : [];
- if (partial.length > acc.length) {
- acc = partial;
- sseEvent(res, "partial", { partial: acc });
- lastProgressAt = now;
- lastBeat = now;
- }
- if (activity.length > activityLen) {
- activityLen = activity.length;
- sseEvent(res, "activity", { activity, startedAt: run.startedAt ?? null });
- lastProgressAt = now;
- lastBeat = now;
- }
- if (parsed) {
- if (run.stale === true) staleSince ??= now;
- else staleSince = null;
- if ((run.stale === true) !== lastStale) {
- lastStale = run.stale === true;
- sseEvent(res, "stale", { stale: lastStale });
- lastBeat = now;
- }
- }
- if (now - lastBeat > SSE_HEARTBEAT_MS) {
- if (run.alive === true) sseEvent(res, "alive", { at: now });
- else if (lastStale === true) sseEvent(res, "stale", { stale: true });
- else res.write(": ping\n\n");
- lastBeat = now;
+ const controller = new AbortController();
+ res.on("close", () => controller.abort());
+ const path = withSourceAuthNonce(`/v1/runs/${encodeURIComponent(id)}/events`, CORE_SIGNING_SECRET);
+ const portalTok = portalTokenStore.getStore();
+ try {
+ const upstream = await fetch(`${CORE}${path}`, {
+ headers: {
+ ...signedHeaders(CORE_SIGNING_SECRET, "GET", path, ""),
+ ...(portalTok ? { [PORTAL_IDENTITY_HEADER]: portalTok } : {}),
+ },
+ signal: controller.signal,
+ redirect: "manual",
+ });
+ if (controller.signal.aborted) return;
+ if (!upstream.ok || !upstream.body) {
+ json(res, upstream.status, { error: "stream_unavailable" });
+ return;
}
- if (run.alive === true || (staleSince !== null && now - staleSince < SSE_STALE_GRACE_MS)) lastProgressAt = now;
- const terminal = run.status === "done" || run.status === "failed" || run.result != null;
- if (terminal || run.replyComplete) {
- forgetRun(id);
- sseEvent(res, "done", {
- status: run.status ?? null,
- result: run.result ?? null,
- partial: acc,
- activity,
- replyComplete: run.replyComplete ?? false,
- startedAt: run.startedAt ?? null,
- finishedAt: run.finishedAt ?? null,
- });
- break;
+ res.writeHead(200, {
+ "content-type": "text/event-stream; charset=utf-8",
+ "cache-control": "no-cache, no-transform",
+ connection: "keep-alive",
+ "x-accel-buffering": "no",
+ });
+ const source = Readable.fromWeb(upstream.body as Parameters<typeof Readable.fromWeb>[0]);
+ source.on("error", () => res.destroy());
+ source.pipe(res);
+ } catch {
+ if (!controller.signal.aborted) {
+ if (res.headersSent) res.destroy();
+ else json(res, 502, { error: "stream_unavailable" });
}
- if (now - lastProgressAt > SSE_IDLE_MS) break;
- await sleep(lastStale === true ? SSE_STALE_POLL_MS : SSE_CORE_POLL_MS);
}
- if (!closed) res.end();
- return;
},
},
{
diff --git a/plugins/web-ui/src/chat.ts b/plugins/web-ui/src/chat.ts
index 5f9379375..3524b3c9a 100644
--- a/plugins/web-ui/src/chat.ts
+++ b/plugins/web-ui/src/chat.ts
@@ -101,9 +101,7 @@ import type { ChatSurface, ConvCtx } from "./conv-types";
import { errMessage, swallow } from "../../chassis/src/errors";
import { showStateError } from "./error-banner";
import { splitLinks } from "./linkify";
-import { escapeLoneDollars } from "./markdown-dollars";
import { slackWireToPlain, splitSlackWire, stripSlackDirectives } from "./slack-text";
-import { splitStreamingMarkdown } from "./streaming-markdown";
import { installMarkdownSanitizer } from "./markdown-sanitize";
import {
transcriptModel,
@@ -141,7 +139,7 @@ import { createForkOriginController, forkOriginView } from "./fork-origin";
import { base64ToBytes } from "./paste-text";
import { tip } from "./tooltip";
import { workSeconds, workedLabel } from "./work-duration";
-import { decorateTextCodeBlocks, normalizePlainTextFences } from "./text-code";
+import { decorateTextCodeBlocks } from "./text-code";
import { createTranscriptViewport } from "./transcript-viewport";
import { suggestedActivities } from "./suggested-activities";
@@ -149,7 +147,6 @@ import { suggestedActivities } from "./suggested-activities";
installMarkdownSanitizer();
const detachedAgents = new WeakSet<Agent>();
-const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
interface SettledRowKey {
index: number;
activity: WorkBlock["activity"] | undefined;
@@ -285,7 +282,6 @@ export function createChatSurface(
let ctaThreadRef: string | null | undefined;
let ctaText = CHAT_CTAS[0]!;
let workTicker: ReturnType<typeof setInterval> | null = null;
- let revealedTailLen = 0;
let liveWorkExpanded = false;
function notePendingSessionOnSend(): void {
@@ -1352,7 +1348,6 @@ export function createChatSurface(
`,
chatState.host,
);
- decorateStreamingTail();
const host = chatState.host;
requestAnimationFrame(() => {
if (chatState.host !== host || chatState.agent !== agent || !host.isConnected) return;
@@ -1363,43 +1358,6 @@ export function createChatSurface(
postCurrentPaneState();
}
- function decorateStreamingTail(): void {
- const blocks = chatState.host?.querySelectorAll<HTMLElement>(".streaming-text.live-stream markdown-block");
- const block = blocks?.length ? blocks[blocks.length - 1] : undefined;
- if (!block) {
- revealedTailLen = 0;
- return;
- }
- if (reduceMotion.matches) return;
- const fullLen = (block.textContent ?? "").replace(/\s+$/u, "").length;
- const grown = fullLen - revealedTailLen;
- revealedTailLen = fullLen;
- if (grown <= 0 || grown > 240) return;
- const last = lastTextNode(block);
- if (!last || !last.textContent) return;
- const visibleEnd = last.textContent.replace(/\s+$/u, "").length;
- const n = Math.min(grown, visibleEnd);
- if (n <= 0) return;
- const tail = last.splitText(visibleEnd - n);
- if (tail.textContent && tail.textContent.length > n) tail.splitText(n);
- const parent = tail.parentNode;
- if (!parent) return;
- const span = document.createElement("span");
- span.className = "tok-in";
- parent.insertBefore(span, tail);
- span.appendChild(tail);
- }
-
- function lastTextNode(el: Node): Text | null {
- for (let i = el.childNodes.length - 1; i >= 0; i--) {
- const child = el.childNodes[i]!;
- if (child.nodeType === Node.TEXT_NODE && /\S/u.test(child.textContent ?? "")) return child as Text;
- const deep = lastTextNode(child);
- if (deep) return deep;
- }
- return null;
- }
-
function sessionTopbar(): TemplateResult {
const scope = chatState.scopeId;
const session = sessionsState.list.find((s) =>
@@ -1801,9 +1759,7 @@ export function createChatSurface(
const body = links.length ? stripConnectorLinks(shown) : shown;
if (body.trim())
parts.push(
- html`<div class="streaming-text ${isStreaming ? "live-stream" : ""}" dir="auto">
- ${isStreaming ? streamingMarkdown(body) : markdown(body)}
- </div>`,
+ html`<div class="streaming-text ${isStreaming ? "live-stream" : ""}" dir="auto">${markdown(body)}</div>`,
);
for (const link of links) parts.push(connectorWidget(link));
}
@@ -1835,30 +1791,6 @@ export function createChatSurface(
return html`<div class="message-files">${files.map((f) => deliveredFileBadge(f))}</div>`;
}
- let escapedSegs: string[] = [];
- let escapedSrc: string[] = [];
- function streamingMarkdown(text: string): TemplateResult {
- const { segments, tail } = splitStreamingMarkdown(text);
- if (segments.length < escapedSrc.length) {
- escapedSrc = [];
- escapedSegs = [];
- }
- for (let i = 0; i < segments.length; i++) {
- const seg = segments[i] ?? "";
- if (escapedSrc[i] !== seg) {
- escapedSrc[i] = seg;
- escapedSegs[i] = escapeLoneDollars(normalizePlainTextFences(seg));
- }
- }
- escapedSrc.length = segments.length;
- escapedSegs.length = segments.length;
- return html`${escapedSegs.map((seg) => html`<markdown-block dir="auto" .content=${seg}></markdown-block>`)}<markdown-block
- class="stream-tail"
- dir="auto"
- .content=${escapeLoneDollars(normalizePlainTextFences(tail))}
- ></markdown-block>`;
- }
-
function messageText(message: AgentMessage): string {
const content = (message as { content?: unknown }).content;
if (typeof content === "string") return content;
diff --git a/plugins/web-ui/src/core-bridge.ts b/plugins/web-ui/src/core-bridge.ts
index 38dcc2a73..deea0ba4a 100644
--- a/plugins/web-ui/src/core-bridge.ts
+++ b/plugins/web-ui/src/core-bridge.ts
@@ -1,3 +1,5 @@
+import { EventType } from "@tanstack/ai/client";
+import { fetchServerSentEvents, StreamProcessor } from "@tanstack/ai-client";
import { postCallText, postResultOk } from "./surface-post.ts";
import type { ModelMetadata } from "./pi-models.ts";
import { createAssistantMessageEventStream } from "@earendil-works/pi-ai";
@@ -1409,7 +1411,7 @@ export function subscribeDeliveries(
return () => es.close();
}
-function streamRunViaSse(
+async function streamRunViaSse(
stream: AssistantMessageEventStream,
partial: AssistantMessage,
runId: string,
@@ -1417,96 +1419,63 @@ function streamRunViaSse(
signal?: AbortSignal,
notify?: () => void,
): Promise<"done" | "fallback"> {
- return new Promise((resolve) => {
- if (typeof EventSource === "undefined") return resolve("fallback");
- let settled = false;
- let established = false;
- let silenceTimer: ReturnType<typeof setTimeout> | undefined;
- const es = new EventSource(withBase(runPath(runId, "/events")));
- const settle = (outcome: "done" | "fallback"): void => {
- if (settled) return;
- settled = true;
- clearTimeout(openTimer);
- clearTimeout(silenceTimer);
- signal?.removeEventListener("abort", onAbort);
- es.close();
- resolve(outcome);
- };
- const received = (): void => {
- established = true;
- clearTimeout(silenceTimer);
- silenceTimer = setTimeout(() => settle("fallback"), SSE_SILENCE_MS);
- };
- const onAbort = (): void => {
- abortStream(stream, partial);
- settle("done");
- };
- const openTimer = setTimeout(() => {
- if (!established) settle("fallback");
- }, SSE_OPEN_TIMEOUT_MS);
- signal?.addEventListener("abort", onAbort);
- if (signal?.aborted) return onAbort();
-
- es.onopen = (): void => {
- received();
- };
- es.addEventListener("partial", (e: MessageEvent) => {
- received();
- try {
- const d = JSON.parse(e.data) as { partial?: string };
- if (typeof d.partial === "string" && d.partial.length > st.acc.length) {
- st.lastProgressAt = now();
- pushDelta(stream, partial, st, d.partial);
- }
- } catch (e) {
- swallow("web-ui: handle sse partial event", e);
- }
- });
- es.addEventListener("activity", (e: MessageEvent) => {
- received();
- try {
- const d = JSON.parse(e.data) as { activity?: unknown[]; startedAt?: number | null };
- const work = (partial as AssistantWork).work;
- const beforeActivity = work?.activity.length ?? 0;
- if (mergeWork(work, d)) {
- if ((work?.activity.length ?? 0) > beforeActivity) st.lastProgressAt = now();
- notify?.();
- }
- } catch (e) {
- swallow("web-ui: handle sse activity event", e);
- }
- });
- es.addEventListener("alive", () => {
- received();
- st.lastProgressAt = now();
- });
- es.addEventListener("stale", (e: MessageEvent) => {
- received();
- try {
- const d = JSON.parse(e.data) as { stale?: boolean };
- const stale = d.stale === true;
- if (stale) st.staleSince ??= now();
- else st.staleSince = undefined;
- if (!stale || now() - (st.staleSince ?? 0) < STALE_GRACE_MS) st.lastProgressAt = now();
- setWorkStale((partial as AssistantWork).work, stale, notify);
- } catch (e) {
- swallow("web-ui: handle sse stale event", e);
- }
- });
- es.addEventListener("done", (e: MessageEvent) => {
+ if (typeof window === "undefined") return "fallback";
+ const controller = new AbortController();
+ const abort = (): void => controller.abort();
+ signal?.addEventListener("abort", abort, { once: true });
+ if (signal?.aborted) controller.abort();
+ let timer = setTimeout(abort, SSE_OPEN_TIMEOUT_MS);
+ const received = (): void => {
+ clearTimeout(timer);
+ timer = setTimeout(abort, SSE_SILENCE_MS);
+ };
+ const processor = new StreamProcessor({
+ initialMessages: [{ id: runId, role: "assistant", parts: [{ type: "text", content: st.acc }] }],
+ events: { onTextUpdate: (_id, text) => pushDelta(stream, partial, st, text) },
+ });
+ processor.processChunk({ type: EventType.TEXT_MESSAGE_START, messageId: runId, role: "assistant" });
+ const append = (delta: string): void => {
+ if (!delta) return;
+ st.lastProgressAt = now();
+ processor.processChunk({ type: EventType.TEXT_MESSAGE_CONTENT, messageId: runId, delta });
+ };
+ const transport = fetchServerSentEvents(withBase(runPath(runId, "/events")), {
+ credentials: "same-origin",
+ });
+ try {
+ for await (const event of transport.joinRun(runId, controller.signal)) {
received();
- try {
- applyRun(stream, partial, st, JSON.parse(e.data) as RunPoll, notify);
- settle("done");
- } catch {
- settle("fallback");
+ if (event.type !== "CUSTOM") continue;
+ if (event.name === "delta") {
+ const value = event.value as { offset?: unknown; delta?: unknown };
+ if (
+ typeof value.offset !== "number" ||
+ !Number.isSafeInteger(value.offset) ||
+ value.offset < 0 ||
+ typeof value.delta !== "string"
+ )
+ return "fallback";
+ if (value.offset > st.acc.length) return "fallback";
+ const delta = value.delta.slice(st.acc.length - value.offset);
+ append(delta);
+ } else if (event.name === "run") {
+ const run = event.value as RunPoll;
+ if (typeof run.partial === "string" && run.partial.length > st.acc.length)
+ append(run.partial.slice(st.acc.length));
+ // Completion must carry the final result, including files and approvals.
+ const terminal = run.status === "done" || run.status === "failed" || run.result != null;
+ if (applyRun(stream, partial, st, { ...run, replyComplete: terminal }, notify) === "terminal") return "done";
+ if (run.alive) st.lastProgressAt = now();
}
- });
- es.addEventListener("failed", () => settle("fallback"));
- es.onerror = (): void => {
- settle("fallback");
- };
- });
+ }
+ return "fallback";
+ } catch {
+ return "fallback";
+ } finally {
+ clearTimeout(timer);
+ signal?.removeEventListener("abort", abort);
+ controller.abort();
+ }
}
function fail(
diff --git a/plugins/web-ui/src/lazy-katex.ts b/plugins/web-ui/src/lazy-katex.ts
index 41d052d0d..52bec87db 100644
--- a/plugins/web-ui/src/lazy-katex.ts
+++ b/plugins/web-ui/src/lazy-katex.ts
@@ -9,7 +9,7 @@ function escapeHtml(text: string): string {
function rerenderMountedBlocks(): void {
if (typeof document === "undefined") return;
- for (const el of document.querySelectorAll("markdown-block")) {
+ for (const el of document.querySelectorAll("markdown-block, qm-markdown")) {
(el as { requestUpdate?: () => void }).requestUpdate?.();
}
}
diff --git a/plugins/web-ui/src/message-markdown.ts b/plugins/web-ui/src/message-markdown.ts
index f830f20c6..9398cd87d 100644
--- a/plugins/web-ui/src/message-markdown.ts
+++ b/plugins/web-ui/src/message-markdown.ts
@@ -1,13 +1,8 @@
-import "./marked-dedupe";
-import "@mariozechner/mini-lit/dist/MarkdownBlock.js";
-import "@mariozechner/mini-lit/dist/CodeBlock.js";
+import "./stable-markdown";
import { html, type TemplateResult } from "lit";
import { normalizePlainTextFences } from "./text-code";
import { escapeLoneDollars } from "./markdown-dollars";
export function markdown(text: string): TemplateResult {
- return html`<markdown-block
- dir="auto"
- .content=${escapeLoneDollars(normalizePlainTextFences(text))}
- ></markdown-block>`;
+ return html`<qm-markdown dir="auto" .content=${escapeLoneDollars(normalizePlainTextFences(text))}></qm-markdown>`;
}
diff --git a/plugins/web-ui/src/shell.css b/plugins/web-ui/src/shell.css
index cb67578e2..d396308d3 100644
--- a/plugins/web-ui/src/shell.css
+++ b/plugins/web-ui/src/shell.css
@@ -1736,34 +1736,34 @@ a.chat-row-open {
font-size: 15px;
line-height: 1.65;
}
-.assistant-body markdown-block,
-.user-bubble markdown-block {
+.assistant-body :is(markdown-block, qm-markdown),
+.user-bubble :is(markdown-block, qm-markdown) {
display: block;
}
-:is(.assistant-body, .user-bubble) markdown-block a {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) a {
color: var(--markdown-link);
text-decoration: underline;
text-decoration-color: color-mix(in srgb, currentColor 45%, transparent);
text-underline-offset: 0.2em;
}
-:is(.assistant-body, .user-bubble) markdown-block :is(th, td) a {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) :is(th, td) a {
text-decoration: none;
}
-:is(.assistant-body, .user-bubble) markdown-block a:is(:hover, :focus-visible) {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) a:is(:hover, :focus-visible) {
text-decoration: underline;
text-decoration-color: currentColor;
}
-:is(.assistant-body, .user-bubble) markdown-block a:focus-visible {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) a:focus-visible {
outline: 2px solid currentColor;
outline-offset: 3px;
border-radius: 2px;
}
-:is(.assistant-body, .user-bubble) markdown-block .overflow-x-auto:has(> table) {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) .overflow-x-auto:has(> table) {
margin-block: 1.5em;
border: 0;
border-radius: 0;
}
-:is(.assistant-body, .user-bubble) markdown-block :is(th, td) {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) :is(th, td) {
padding: 0.8em 1em;
border: 0;
border-bottom: 1px solid color-mix(in srgb, var(--foreground) 9%, transparent);
@@ -1771,25 +1771,25 @@ a.chat-row-open {
vertical-align: top;
font-variant-numeric: tabular-nums;
}
-:is(.assistant-body, .user-bubble) markdown-block th {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) th {
border-bottom-color: color-mix(in srgb, var(--foreground) 20%, transparent);
}
-:is(.assistant-body, .user-bubble) markdown-block :is(th, td):first-child {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) :is(th, td):first-child {
padding-inline-start: 0;
}
-:is(.assistant-body, .user-bubble) markdown-block :is(th, td):last-child {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) :is(th, td):last-child {
padding-inline-end: 0;
}
-:is(.assistant-body, .user-bubble) markdown-block :is(th, td)[align="center"] {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) :is(th, td)[align="center"] {
text-align: center;
}
-:is(.assistant-body, .user-bubble) markdown-block :is(th, td)[align="right"] {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) :is(th, td)[align="right"] {
text-align: right;
}
-:is(.assistant-body, .user-bubble) markdown-block tr:last-child td {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) tr:last-child td {
border-bottom: 0;
}
-:is(.assistant-body, .user-bubble) markdown-block :is(h1, h2, h3, h4, h5, h6) {
+:is(.assistant-body, .user-bubble) :is(markdown-block, qm-markdown) :is(h1, h2, h3, h4, h5, h6) {
margin-block: 1.5em 0.6em;
}
.streaming-text {
@@ -1813,21 +1813,27 @@ a.chat-row-open {
animation: none;
}
}
-.assistant-body markdown-block > *:first-child,
-.user-bubble markdown-block > *:first-child {
+.assistant-body :is(markdown-block, qm-markdown) > *:first-child,
+.user-bubble :is(markdown-block, qm-markdown) > *:first-child {
margin-top: 0;
}
-.assistant-body markdown-block > *:last-child,
-.user-bubble markdown-block > *:last-child {
+.assistant-body :is(markdown-block, qm-markdown) > *:last-child,
+.user-bubble :is(markdown-block, qm-markdown) > *:last-child {
margin-bottom: 0;
}
-.assistant-body .streaming-text markdown-block + markdown-block {
+.assistant-body .streaming-text :is(markdown-block, qm-markdown) + :is(markdown-block, qm-markdown) {
margin-top: 1rem;
}
-.assistant-body .streaming-text markdown-block:has(> div > .overflow-x-auto:last-child) + markdown-block {
+.assistant-body
+ .streaming-text
+ :is(markdown-block, qm-markdown):has(> div > .overflow-x-auto:last-child)
+ + :is(markdown-block, qm-markdown) {
margin-top: 1.5em;
}
-:is(.assistant-body, .user-bubble) markdown-block:first-of-type > div > :is(h1, h2, h3, h4, h5, h6):first-child {
+:is(.assistant-body, .user-bubble)
+ :is(markdown-block, qm-markdown):first-of-type
+ > div
+ > :is(h1, h2, h3, h4, h5, h6):first-child {
margin-top: 0;
}
.message-bubble {
@@ -2926,24 +2932,24 @@ a.chat-row-open {
transform: rotate(90deg);
}
-.assistant-body markdown-block :not(pre) > code,
-.user-bubble markdown-block :not(pre) > code {
+.assistant-body :is(markdown-block, qm-markdown) :not(pre) > code,
+.user-bubble :is(markdown-block, qm-markdown) :not(pre) > code {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.92em;
padding: 1px 5px;
border-radius: 5px;
background: var(--secondary);
}
-.assistant-body markdown-block pre,
-.user-bubble markdown-block pre {
+.assistant-body :is(markdown-block, qm-markdown) pre,
+.user-bubble :is(markdown-block, qm-markdown) pre {
margin: 12px 0;
padding: 14px 12px;
border-radius: 8px;
background: var(--secondary);
overflow-x: auto;
}
-.assistant-body markdown-block pre code,
-.user-bubble markdown-block pre code {
+.assistant-body :is(markdown-block, qm-markdown) pre code,
+.user-bubble :is(markdown-block, qm-markdown) pre code {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 15px;
line-height: 1.55;
@@ -10402,12 +10408,12 @@ h3.ambient-field-label {
font-size: 16px;
line-height: 1.55;
}
- .assistant-body markdown-block pre code,
- .user-bubble markdown-block pre code {
+ .assistant-body :is(markdown-block, qm-markdown) pre code,
+ .user-bubble :is(markdown-block, qm-markdown) pre code {
font-size: 13.5px;
}
- .assistant-body markdown-block pre,
- .user-bubble markdown-block pre {
+ .assistant-body :is(markdown-block, qm-markdown) pre,
+ .user-bubble :is(markdown-block, qm-markdown) pre {
-webkit-overflow-scrolling: touch;
overscroll-behavior-x: contain;
}
diff --git a/plugins/web-ui/src/stable-markdown.ts b/plugins/web-ui/src/stable-markdown.ts
new file mode 100644
index 000000000..ea8fbdd35
--- /dev/null
+++ b/plugins/web-ui/src/stable-markdown.ts
@@ -0,0 +1,64 @@
+import "./marked-dedupe.ts";
+import { MarkdownBlock } from "@mariozechner/mini-lit/dist/MarkdownBlock.js";
+import "@mariozechner/mini-lit/dist/CodeBlock.js";
+import { ReactiveElement, render, type PropertyValues } from "lit";
+import morphdom from "morphdom";
+
+export class StableMarkdown extends ReactiveElement {
+ static properties = { content: {}, isThinking: { type: Boolean } };
+
+ declare content: string;
+ declare isThinking: boolean;
+ private readonly renderer = new MarkdownBlock();
+ private readonly staging = document.createElement("div");
+
+ constructor() {
+ super();
+ this.content = "";
+ this.isThinking = false;
+ }
+
+ protected createRenderRoot(): HTMLElement {
+ return this;
+ }
+
+ connectedCallback(): void {
+ super.connectedCallback();
+ this.classList.add("markdown-content");
+ this.style.display = "block";
+ }
+
+ protected update(changed: PropertyValues): void {
+ this.renderer.content = this.content;
+ this.renderer.isThinking = this.isThinking;
+ render(this.renderer.render(), this.staging);
+ morphdom(this, this.staging.cloneNode(true), {
+ childrenOnly: true,
+ onBeforeElUpdated: (current, next) => {
+ if (current.localName === "code-block") {
+ for (const name of ["code", "language"]) {
+ const value = next.getAttribute(name);
+ if (value !== current.getAttribute(name)) {
+ if (value === null) current.removeAttribute(name);
+ else current.setAttribute(name, value);
+ }
+ }
+ return false;
+ }
+ for (let i = 0; i < current.childNodes.length; i++) {
+ const from = current.childNodes[i];
+ const to = next.childNodes[i];
+ if (from?.nodeType !== Node.TEXT_NODE || to?.nodeType !== Node.TEXT_NODE) continue;
+ const before = from.nodeValue ?? "";
+ const after = to.nodeValue ?? "";
+ if (after.length > before.length && after.startsWith(before))
+ (from as Text).appendData(after.slice(before.length));
+ }
+ return !current.isEqualNode(next);
+ },
+ });
+ super.update(changed);
+ }
+}
+
+if (!customElements.get("qm-markdown")) customElements.define("qm-markdown", StableMarkdown);
diff --git a/plugins/web-ui/test/plain-text-code-wrap.test.ts b/plugins/web-ui/test/plain-text-code-wrap.test.ts
index 613fff817..848ad2869 100644
--- a/plugins/web-ui/test/plain-text-code-wrap.test.ts
+++ b/plugins/web-ui/test/plain-text-code-wrap.test.ts
@@ -38,7 +38,10 @@ test("plain-text code fences stay compact without changing source-code fences",
test("long plain-text fences collapse by default and remain expandable", () => {
assert.match(chat, /requestAnimationFrame\(\(\) => \{\s*decorateTextCodeBlocks\(/);
- assert.match(chat, /normalizePlainTextFences/);
+ assert.match(
+ readFileSync(new URL("../src/message-markdown.ts", import.meta.url), "utf8"),
+ /normalizePlainTextFences/,
+ );
assert.match(
css,
/code-block\.text-code-collapsible\[data-expanded="false"\] \.text-code-body[^}]*max-height: 76px;[^}]*overflow: hidden;/s,
diff --git a/plugins/web-ui/test/post-reply-recovery.test.ts b/plugins/web-ui/test/post-reply-recovery.test.ts
index 349dc59b9..5c7a9fdd4 100644
--- a/plugins/web-ui/test/post-reply-recovery.test.ts
+++ b/plugins/web-ui/test/post-reply-recovery.test.ts
@@ -12,6 +12,7 @@ class FakeEventSource {
onerror: (() => void) | null = null;
listeners = new Map<string, Array<(event: { data: string }) => void>>();
closed = false;
+ controller?: ReadableStreamDefaultController<Uint8Array>;
url: string;
constructor(url: string) {
this.url = url;
@@ -20,11 +21,29 @@ class FakeEventSource {
addEventListener(name: string, listener: (event: { data: string }) => void) {
this.listeners.set(name, [...(this.listeners.get(name) ?? []), listener]);
}
+ response(signal?: AbortSignal | null) {
+ const body = new ReadableStream<Uint8Array>({
+ start: (controller) => {
+ this.controller = controller;
+ },
+ });
+ signal?.addEventListener("abort", () => this.close(), { once: true });
+ this.controller!.enqueue(
+ new TextEncoder().encode(`data: ${JSON.stringify({ type: "RUN_STARTED", threadId: "r1", runId: "r1" })}\n\n`),
+ );
+ return new Response(body, { headers: { "content-type": "text/event-stream" } });
+ }
emit(name: string, data: unknown) {
+ if (this.controller && !this.closed) {
+ const event = { type: "CUSTOM", name: "run", value: data };
+ this.controller.enqueue(new TextEncoder().encode(`data: ${JSON.stringify(event)}\n\n`));
+ }
for (const listener of this.listeners.get(name) ?? []) listener({ data: JSON.stringify(data) });
}
close() {
+ if (this.closed) return;
this.closed = true;
+ this.controller?.close();
}
}
@@ -96,9 +115,11 @@ test("post replies remain visible in new and continuing conversations", async (t
let intercept: ((path: string) => Promise<Response> | undefined) | undefined;
const requests: string[] = [];
const originalFetch = globalThis.fetch;
- globalThis.fetch = async (input) => {
+ globalThis.fetch = async (input, init) => {
const path = String(input);
requests.push(path);
+ if (path.includes("/api/runs/") && path.includes("/events"))
+ return new FakeEventSource(new URL(path, "http://localhost").pathname).response(init?.signal);
const intercepted = intercept?.(path);
if (intercepted) return intercepted;
if (path.includes("runtime-config"))
diff --git a/plugins/web-ui/test/rtl-direction.test.ts b/plugins/web-ui/test/rtl-direction.test.ts
index dd6ee4cc8..0f0bfa936 100644
--- a/plugins/web-ui/test/rtl-direction.test.ts
+++ b/plugins/web-ui/test/rtl-direction.test.ts
@@ -17,10 +17,9 @@ test("settled and streaming transcript blocks select direction from their conten
assert.match(chat, /import \{ markdown \} from "\.\/message-markdown"/);
assert.match(
messageMarkdown,
- /<markdown-block\s+dir="auto"\s+\.content=\$\{escapeLoneDollars\(normalizePlainTextFences\(text\)\)\}/,
+ /<qm-markdown\s+dir="auto"\s+\.content=\$\{escapeLoneDollars\(normalizePlainTextFences\(text\)\)\}/,
);
- assert.match(chat, /escapedSegs\.map\(\(seg\) => html`<markdown-block dir="auto"/);
- assert.match(chat, /class="stream-tail"\s+dir="auto"/);
+ assert.match(chat, /\$\{markdown\(body\)\}/);
assert.match(chat, /class="streaming-text \$\{isStreaming \? "live-stream" : ""\}" dir="auto"/);
});
@@ -63,9 +62,9 @@ for (const content of [
"Hello https://example.com user_42 10:30",
]) {
test(`automatic direction remains content-level for ${content}`, () => {
- const dom = new JSDOM('<main><textarea dir="auto"></textarea><markdown-block dir="auto"></markdown-block></main>');
+ const dom = new JSDOM('<main><textarea dir="auto"></textarea><qm-markdown dir="auto"></qm-markdown></main>');
const textarea = dom.window.document.querySelector("textarea")!;
- const block = dom.window.document.querySelector("markdown-block") as HTMLElement;
+ const block = dom.window.document.querySelector("qm-markdown") as HTMLElement;
textarea.value = content;
block.textContent = content;
assert.equal(textarea.dir, "auto");
diff --git a/plugins/web-ui/test/run-connection.test.ts b/plugins/web-ui/test/run-connection.test.ts
index 36eecd14c..89d69c06c 100644
--- a/plugins/web-ui/test/run-connection.test.ts
+++ b/plugins/web-ui/test/run-connection.test.ts
@@ -1,59 +1,83 @@
import assert from "node:assert/strict";
-import { test } from "node:test";
+import { test, type TestContext } from "node:test";
import { makeRunResumeStreamFn } from "../src/core-bridge.ts";
import type { Api, Context, Model } from "@earendil-works/pi-ai";
const model = { id: "m", api: "anthropic", provider: "anthropic" } as unknown as Model<Api>;
const flush = async () => {
- for (let i = 0; i < 30; i++) await Promise.resolve();
+ for (let i = 0; i < 100; i++) await Promise.resolve();
};
-class FakeEventSource extends EventTarget {
- static current: FakeEventSource;
- onopen?: () => void;
- onerror?: () => void;
- closed = false;
- constructor() {
- super();
- FakeEventSource.current = this;
- }
- close() {
- this.closed = true;
- }
+function streamingFetch(t: TestContext) {
+ const original = Object.getOwnPropertyDescriptor(globalThis, "window");
+ Object.defineProperty(globalThis, "window", { configurable: true, value: {} });
+ t.after(() => {
+ if (original) Object.defineProperty(globalThis, "window", original);
+ else Reflect.deleteProperty(globalThis, "window");
+ });
+ let controller: ReadableStreamDefaultController<Uint8Array>;
+ const state = {
+ polls: 0,
+ opens: 0,
+ drop() {
+ controller.error(new Error("connection lost"));
+ },
+ closed: false,
+ send(value: unknown, id?: string) {
+ controller.enqueue(new TextEncoder().encode(`${id ? `id: ${id}\n` : ""}data: ${JSON.stringify(value)}\n\n`));
+ },
+ };
+ t.mock.method(globalThis, "fetch", async (url: unknown, init?: RequestInit) => {
+ if (!String(url).includes("/events")) {
+ state.polls++;
+ return Response.json({ status: "done", result: { status: "ok", stopped: true } });
+ }
+ state.opens++;
+ const body = new ReadableStream<Uint8Array>({
+ start(c) {
+ controller = c;
+ },
+ cancel() {
+ state.closed = true;
+ },
+ });
+ init?.signal?.addEventListener(
+ "abort",
+ () => {
+ if (!state.closed) {
+ state.closed = true;
+ controller.error(new DOMException("aborted", "AbortError"));
+ }
+ },
+ { once: true },
+ );
+ state.send({ type: "RUN_STARTED", threadId: "r", runId: "r" });
+ return new Response(body, { headers: { "content-type": "text/event-stream" } });
+ });
+ return state;
}
for (const heartbeat of [false, true]) {
- test(`a silent established stream falls back to polling${heartbeat ? " after its last heartbeat" : ""}`, async (t) => {
+ test(`a silent stream falls back to polling${heartbeat ? " after its last heartbeat" : ""}`, async (t) => {
t.mock.timers.enable({ apis: ["setTimeout"] });
- const original = Object.getOwnPropertyDescriptor(globalThis, "EventSource");
- Object.defineProperty(globalThis, "EventSource", { configurable: true, value: FakeEventSource });
- t.after(() => {
- if (original) Object.defineProperty(globalThis, "EventSource", original);
- else Reflect.deleteProperty(globalThis, "EventSource");
- });
- let polls = 0;
- t.mock.method(globalThis, "fetch", async () => {
- polls++;
- return Response.json({ status: "done", result: { status: "ok", stopped: true } });
- });
+ const state = streamingFetch(t);
const stream = await makeRunResumeStreamFn("r")(model, {} as Context, {});
await flush();
- const es = FakeEventSource.current;
- es.onopen?.();
if (heartbeat) {
t.mock.timers.tick(20_000);
- es.dispatchEvent(new Event("alive"));
+ state.send({ type: "CUSTOM", name: "run", value: { status: "running", result: null, alive: true } });
+ await flush();
t.mock.timers.tick(20_000);
await flush();
- assert.equal(polls, 0);
+ assert.equal(state.polls, 0);
}
t.mock.timers.tick(30_000);
await flush();
- assert.equal(polls, 1);
- assert.equal(es.closed, true);
+ assert.equal(state.polls, 1);
+ assert.equal(state.closed, true);
assert.equal((await stream.result()).stopReason, "aborted");
t.mock.timers.tick(60_000);
- assert.equal(polls, 1);
+ assert.equal(state.polls, 1);
});
}
@@ -76,7 +100,7 @@ test("a stalled poll times out and retries without losing the run", async (t) =>
});
const stream = await makeRunResumeStreamFn("r")(model, {} as Context, {});
await flush();
- assert.ok(requestSignal, "poll requests must be bounded");
+ assert.ok(requestSignal);
timeout.abort(new DOMException("timed out", "TimeoutError"));
await flush();
t.mock.timers.tick(5_000);
@@ -86,33 +110,81 @@ test("a stalled poll times out and retries without losing the run", async (t) =>
});
for (const terminal of [true, false]) {
- test(`${terminal ? "terminal completion" : "detaching"} cancels the connection watchdog`, async (t) => {
+ test(`${terminal ? "terminal completion" : "detaching"} cancels the watchdog`, async (t) => {
t.mock.timers.enable({ apis: ["setTimeout"] });
- const original = Object.getOwnPropertyDescriptor(globalThis, "EventSource");
- Object.defineProperty(globalThis, "EventSource", { configurable: true, value: FakeEventSource });
- t.after(() => {
- if (original) Object.defineProperty(globalThis, "EventSource", original);
- else Reflect.deleteProperty(globalThis, "EventSource");
- });
- let polls = 0;
- t.mock.method(globalThis, "fetch", async () => {
- polls++;
- return Response.json({ status: "done", result: { status: "ok" } });
- });
+ const state = streamingFetch(t);
const controller = new AbortController();
const stream = await makeRunResumeStreamFn("r")(model, {} as Context, { signal: controller.signal });
await flush();
- const es = FakeEventSource.current;
- es.onopen?.();
- if (terminal) {
- const event = new Event("done");
- Object.assign(event, { data: JSON.stringify({ status: "done", result: { status: "ok" } }) });
- es.dispatchEvent(event);
- } else controller.abort();
+ if (terminal)
+ state.send({ type: "CUSTOM", name: "run", value: { status: "done", result: { status: "ok", reply: "done" } } });
+ else controller.abort();
await stream.result();
t.mock.timers.tick(60_000);
await flush();
- assert.equal(es.closed, true);
- assert.equal(polls, 0);
+ assert.equal(state.closed, true);
+ assert.equal(state.polls, 0);
});
}
+
+test("TanStack assembles snapshot hydration and overlapping Unicode deltas exactly once", async (t) => {
+ const state = streamingFetch(t);
+ const stream = await makeRunResumeStreamFn("r")(model, {} as Context, {});
+ await flush();
+ state.send({ type: "CUSTOM", name: "run", value: { status: "running", result: null, partial: "hello " } });
+ state.send({ type: "CUSTOM", name: "delta", value: { offset: 6, delta: "🌍" } });
+ state.send({ type: "CUSTOM", name: "delta", value: { offset: 6, delta: "🌍!" } });
+ state.send({ type: "CUSTOM", name: "run", value: { status: "running", result: null, partial: "hello 🌍! more" } });
+ state.send({ type: "CUSTOM", name: "delta", value: { offset: 14, delta: " text" } });
+ state.send({ type: "CUSTOM", name: "run", value: { status: "done", result: { status: "ok" } } });
+ const result = await stream.result();
+ assert.deepEqual(result.content, [{ type: "text", text: "hello 🌍! more text" }]);
+ assert.equal(state.polls, 0);
+});
+
+test("TanStack reconnects after a transport drop and deduplicates replayed offsets", async (t) => {
+ const state = streamingFetch(t);
+ const stream = await makeRunResumeStreamFn("r")(model, {} as Context, {});
+ await flush();
+ state.send({ type: "CUSTOM", name: "delta", value: { offset: 0, delta: "hello" } }, "text:5");
+ await flush();
+ state.drop();
+ for (let i = 0; i < 200 && state.opens < 2; i++) await new Promise((resolve) => setTimeout(resolve, 10));
+ assert.equal(state.opens, 2);
+ state.send({ type: "CUSTOM", name: "delta", value: { offset: 0, delta: "hello" } }, "text:5");
+ state.send({ type: "CUSTOM", name: "delta", value: { offset: 5, delta: " world" } }, "text:11");
+ state.send({ type: "CUSTOM", name: "run", value: { status: "done", result: { status: "ok" } } });
+ assert.deepEqual((await stream.result()).content, [{ type: "text", text: "hello world" }]);
+ assert.equal(state.polls, 0);
+});
+
+test("reply completion waits for final result metadata rather than dropping approvals", async (t) => {
+ const state = streamingFetch(t);
+ const stream = await makeRunResumeStreamFn("r")(model, {} as Context, {});
+ await flush();
+ state.send({
+ type: "CUSTOM",
+ name: "run",
+ value: { status: "running", result: null, partial: "waiting", replyComplete: true },
+ });
+ await flush();
+ assert.equal(state.closed, false);
+ state.send({
+ type: "CUSTOM",
+ name: "run",
+ value: {
+ status: "done",
+ result: {
+ status: "pending_approval",
+ pendingApprovals: [{ requestId: "a", command: "test", reason: "approval" }],
+ },
+ },
+ });
+ const message = await stream.result();
+ assert.equal(
+ (message as unknown as { work: { pendingApprovals: Array<{ requestId: string }> } }).work.pendingApprovals[0]
+ ?.requestId,
+ "a",
+ );
+ assert.equal(state.polls, 0);
+});
diff --git a/plugins/web-ui/test/stable-markdown.test.ts b/plugins/web-ui/test/stable-markdown.test.ts
new file mode 100644
index 000000000..82a1bc407
--- /dev/null
+++ b/plugins/web-ui/test/stable-markdown.test.ts
@@ -0,0 +1,101 @@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+import { JSDOM } from "jsdom";
+
+const dom = new JSDOM("<!doctype html><body></body>", { pretendToBeVisual: true, url: "http://localhost" });
+for (const key of [
+ "localStorage",
+ "window",
+ "document",
+ "customElements",
+ "HTMLElement",
+ "Element",
+ "Node",
+ "Document",
+ "CSSStyleSheet",
+ "ShadowRoot",
+] as const) {
+ Object.defineProperty(globalThis, key, { value: dom.window[key], configurable: true });
+}
+const { StableMarkdown } = await import("../src/stable-markdown.ts");
+const { installMarkdownSanitizer } = await import("../src/markdown-sanitize.ts");
+installMarkdownSanitizer();
+
+async function mount(content: string): Promise<InstanceType<typeof StableMarkdown>> {
+ const block = new StableMarkdown();
+ document.body.append(block);
+ block.content = content;
+ await block.updateComplete;
+ return block;
+}
+
+test("appending text preserves existing paragraphs, formatted nodes and selection", async () => {
+ const block = await mount("A **stable phrase** followed by text");
+ const paragraph = block.querySelector("p")!;
+ const strong = block.querySelector("strong")!;
+ const selection = dom.window.getSelection()!;
+ const range = document.createRange();
+ range.selectNodeContents(strong);
+ selection.removeAllRanges();
+ selection.addRange(range);
+ block.content += " and more text";
+ await block.updateComplete;
+ assert.equal(block.querySelector("p"), paragraph);
+ assert.equal(block.querySelector("strong"), strong);
+ assert.equal(selection.toString(), "stable phrase");
+ assert.match(block.textContent!, /and more text/);
+ block.remove();
+});
+
+test("completed tables and code components remain mounted while the reply grows", async () => {
+ const block = await mount("| name | value |\n| --- | --- |\n| alpha | 1 |\n\n```js\nconst x = 1;\n```\n\nNext");
+ const table = block.querySelector("table");
+ const code = block.querySelector("code-block");
+ assert.ok(code);
+ await (code as HTMLElement & { updateComplete: Promise<unknown> }).updateComplete;
+ const marker = document.createElement("i");
+ code.append(marker);
+ code.setAttribute("data-expanded", "true");
+ code.classList.add("text-code-collapsible");
+ block.content += " paragraph";
+ await block.updateComplete;
+ assert.equal(block.querySelector("table"), table);
+ assert.equal(block.querySelector("code-block"), code);
+ assert.equal(code.getAttribute("data-expanded"), "true");
+ assert.ok(code.classList.contains("text-code-collapsible"));
+ assert.ok(marker.isConnected, "a custom element owns its rendered children");
+ block.remove();
+});
+
+test("reference definitions can update earlier text without segment-boundary drift", async () => {
+ const block = await mount("intro ".repeat(350) + "[reference][target]\n\nNext paragraph");
+ block.content += "\n\n[target]: https://example.com\n";
+ await block.updateComplete;
+ assert.equal(block.querySelector("a")?.href, "https://example.com/");
+ block.remove();
+});
+
+test("replacement text and unsafe links are handled by the existing markdown sanitization", async () => {
+ const block = await mount("Long original **reply**");
+ block.content = "[unsafe](javascript:alert(1))\n\n<img src=x onerror=alert(1)>";
+ await block.updateComplete;
+ assert.ok(!block.innerHTML.includes('href="javascript:'));
+ assert.equal(block.querySelector("img"), null);
+ assert.ok(!block.textContent!.includes("Long original"));
+ block.remove();
+});
+
+test("selection inside a growing text node survives append-only updates", async () => {
+ const el = await mount("a growing paragraph");
+ const text = el.querySelector("p")!.firstChild!;
+ const range = document.createRange();
+ range.setStart(text, 2);
+ range.setEnd(text, 9);
+ const selection = window.getSelection()!;
+ selection.removeAllRanges();
+ selection.addRange(range);
+ el.content = "a growing paragraph with more words";
+ await el.updateComplete;
+ assert.equal(selection.toString(), "growing");
+ assert.equal(el.querySelector("p")!.firstChild, text);
+});
diff --git a/src/api/app-turn.ts b/src/api/app-turn.ts
index 9eab03e57..45b3d277d 100644
--- a/src/api/app-turn.ts
+++ b/src/api/app-turn.ts
@@ -55,6 +55,8 @@ export function createTurnMethods(
| "listSessionApprovals"
| "pendingApprovalForThread"
| "getRun"
+ | "subscribeRun"
+ | "syncRunStream"
| "activeRunForThread"
| "withdrawRun"
| "signalRun"
@@ -563,6 +565,21 @@ export function createTurnMethods(
return pendingApprovalResultForThread(threadRef, viewer);
},
+ subscribeRun(runId, listener, onResync) {
+ return (
+ deps.runStreamEvents?.subscribe(
+ (event) => {
+ if (event.runId === runId) listener(event);
+ },
+ { onResync },
+ ) ?? (() => {})
+ );
+ },
+
+ syncRunStream(runId, offset) {
+ deps.runStreamEvents?.emit({ runId, kind: "sync", offset });
+ },
+
async getRun(runId, viewer) {
const run = await deps.runs.get(runId);
if (!run) return null;
diff --git a/src/api/app-types.ts b/src/api/app-types.ts
index acf4fa320..ee55c0041 100644
--- a/src/api/app-types.ts
+++ b/src/api/app-types.ts
@@ -1,3 +1,5 @@
+import type { EventBus } from "../util/event-bus.ts";
+import type { RunStreamEvent } from "../runs/run-stream-events.ts";
import type { ModelOverlayStore } from "../model/model-overlay-store.ts";
import type { SwarmService } from "../swarms/swarm-service.ts";
import type {
@@ -266,6 +268,8 @@ export interface App {
subscribeLedgerEvents(cb: (event: OwnedLedgerEvent) => void, opts?: SubscribeOptions): () => void;
listSessionApprovals(sessionId: string, viewer: string): Promise<PendingApproval[]>;
pendingApprovalForThread(threadRef: string, viewer?: string): Promise<TurnResult | null>;
+ subscribeRun(runId: string, listener: (event: RunStreamEvent) => void, onResync?: () => void): () => void;
+ syncRunStream(runId: string, offset: number): void;
getRun(
runId: string,
viewer?: string,
@@ -569,6 +573,7 @@ export interface AppDeps {
maxAttempts: number;
runWaitMs?: number;
turnStream?: TurnStream;
+ runStreamEvents?: EventBus<RunStreamEvent>;
runActivity?: RunActivityStore;
signals?: RunSignalStore;
tasks?: TaskStore;
diff --git a/src/api/routes/index.ts b/src/api/routes/index.ts
index 5cb4df6ff..d725f6b16 100644
--- a/src/api/routes/index.ts
+++ b/src/api/routes/index.ts
@@ -6,6 +6,7 @@ import { blobRoutes } from "./blobs.ts";
import { sessionStateRawRoutes } from "./session-state.ts";
import { loopItemEventsRawRoutes } from "./loop-item-events.ts";
import { webhookRawRoutes, webhookRoutes } from "./webhooks.ts";
+import { runEventRoutes } from "./run-events.ts";
import { turnRoutes } from "./turns.ts";
import { credentialRoutes } from "./credentials.ts";
import { brokerGitHttp, GIT_HTTP_BROKER_PREFIX } from "../git-http-broker.ts";
@@ -57,6 +58,7 @@ export const apiRoutes: ReadonlyArray<Route<ApiCtx>> = [
...searchRoutes,
...deploymentLayerRoutes,
...turnRoutes,
+ ...runEventRoutes,
...credentialRoutes,
...keychainRoutes,
...secretDropRoutes,
diff --git a/src/api/routes/run-events.ts b/src/api/routes/run-events.ts
new file mode 100644
index 000000000..779c99f19
--- /dev/null
+++ b/src/api/routes/run-events.ts
@@ -0,0 +1,119 @@
+import { sendJson } from "../http.ts";
+import type { ApiCtx, Route } from "./route.ts";
+
+const HEARTBEAT_MS = 15_000;
+const MAX_BUFFER_BYTES = 1_048_576;
+
+async function streamRun(ctx: ApiCtx): Promise<void> {
+ const { app, req, res, actor } = ctx;
+ const runId = ctx.params.id!;
+ const initial = await app.getRun(runId, actor?.p);
+ if (!initial) return sendJson(res, 404, { error: "not_found" });
+ if (res.destroyed) return;
+ let closed = false;
+ let offset = 0;
+ let syncing = false;
+ let refreshing = false;
+ let refreshAgain = false;
+ const cleanup = (): void => {
+ closed = true;
+ clearInterval(heartbeat);
+ unsubscribe();
+ };
+ const send = (data: unknown, id?: string): void => {
+ if (closed) return;
+ if (res.writableLength > MAX_BUFFER_BYTES) {
+ res.destroy();
+ return;
+ }
+ res.write(`${id === undefined ? "" : `id: ${id}\n`}data: ${JSON.stringify(data)}\n\n`);
+ };
+ const sync = (): void => {
+ if (syncing || closed) return;
+ syncing = true;
+ queueMicrotask(() => {
+ syncing = false;
+ if (!closed) app.syncRunStream(runId, offset);
+ });
+ };
+ const snapshot = (run: NonNullable<typeof initial>): void => {
+ offset = Math.max(offset, run.partial?.length ?? 0);
+ send({ type: "CUSTOM", name: "run", value: run });
+ if (run.status === "done" || run.status === "failed" || run.result !== null) {
+ send({ type: "RUN_FINISHED", threadId: runId, runId }, "done");
+ res.end();
+ cleanup();
+ }
+ };
+ const refresh = async (): Promise<void> => {
+ if (closed) return;
+ if (refreshing) {
+ refreshAgain = true;
+ return;
+ }
+ refreshing = true;
+ try {
+ do {
+ refreshAgain = false;
+ const run = await app.getRun(runId, actor?.p);
+ if (closed) return;
+ if (!run) {
+ res.destroy();
+ return;
+ }
+ snapshot(run);
+ } while (refreshAgain && !closed);
+ } catch {
+ res.destroy();
+ } finally {
+ refreshing = false;
+ }
+ };
+ const unsubscribe = app.subscribeRun(
+ runId,
+ (event) => {
+ if (closed) return;
+ if (event.kind === "refresh") {
+ void refresh();
+ return;
+ }
+ if (event.kind !== "delta") return;
+ if (event.offset > offset) {
+ sync();
+ return;
+ }
+ const delta = event.text.slice(offset - event.offset);
+ if (!delta) return;
+ send({ type: "CUSTOM", name: "delta", value: { offset, delta } }, `text:${offset + delta.length}`);
+ offset += delta.length;
+ },
+ () => {
+ sync();
+ void refresh();
+ },
+ );
+ const heartbeat = setInterval(() => {
+ sync();
+ void refresh();
+ }, HEARTBEAT_MS);
+ heartbeat.unref?.();
+ res.on("close", cleanup);
+ req.on("error", () => res.destroy());
+ res.writeHead(200, {
+ "content-type": "text/event-stream; charset=utf-8",
+ "cache-control": "no-cache, no-transform",
+ connection: "keep-alive",
+ "x-accel-buffering": "no",
+ });
+ send({ type: "RUN_STARTED", threadId: runId, runId });
+ snapshot(initial);
+ // Subscribe before the second snapshot so hydration cannot miss a publication.
+ if (!closed) {
+ sync();
+ await refresh();
+ }
+}
+
+export const runEventRoutes: ReadonlyArray<Route<ApiCtx>> = [
+ { method: "GET", path: "/v1/runs/:id/events", auth: "source", handle: streamRun },
+];
diff --git a/src/runs/run-stream-events.ts b/src/runs/run-stream-events.ts
new file mode 100644
index 000000000..458d18a9e
--- /dev/null
+++ b/src/runs/run-stream-events.ts
@@ -0,0 +1,13 @@
+import type { EventBus } from "../util/event-bus.ts";
+
+export type RunStreamEvent =
+ | { runId: string; kind: "delta"; offset: number; text: string }
+ | { runId: string; kind: "sync"; offset: number }
+ | { runId: string; kind: "refresh" };
+
+export function emitRunText(bus: EventBus<RunStreamEvent>, runId: string, text: string, offset: number): void {
+ // JSON escaping can cost six bytes per UTF-16 unit; stay below NOTIFY's byte limit.
+ for (let at = 0; at < text.length; at += 1_000) {
+ bus.emit({ runId, kind: "delta", offset: offset + at, text: text.slice(at, at + 1_000) });
+ }
+}
diff --git a/src/runs/turn-stream.ts b/src/runs/turn-stream.ts
index b96a9bf4a..a979bb090 100644
--- a/src/runs/turn-stream.ts
+++ b/src/runs/turn-stream.ts
@@ -45,6 +45,8 @@ interface Entry {
export interface TurnStreamOptions {
maxChars?: number;
graceMs?: number;
+ onDelta?(runId: string, text: string, offset: number): void;
+ onChange?(runId: string): void;
}
const DEFAULT_MAX_CHARS = 200_000;
@@ -87,6 +89,7 @@ export function createTurnStream(opts: TurnStreamOptions = {}): TurnStream {
const entry = runs.get(runId);
if (entry) entry.replying = true;
else runs.set(runId, makeEntry());
+ opts.onChange?.(runId);
},
replying(runId) {
@@ -103,7 +106,12 @@ export function createTurnStream(opts: TurnStreamOptions = {}): TurnStream {
}
if (entry.firstBlockOpen && entry.firstBlock.length < FIRST_BLOCK_MAX_CHARS)
entry.firstBlock = (entry.firstBlock + delta).slice(0, FIRST_BLOCK_MAX_CHARS);
- if (entry.text.length < maxChars) entry.text = (entry.text + delta).slice(0, maxChars);
+ const offset = entry.text.length;
+ if (offset < maxChars) {
+ const added = delta.slice(0, maxChars - offset);
+ entry.text += added;
+ opts.onDelta?.(runId, added, offset);
+ }
},
publishBlockStart(runId) {
@@ -111,7 +119,10 @@ export function createTurnStream(opts: TurnStreamOptions = {}): TurnStream {
if (!entry) return;
if (entry.firstBlock) entry.firstBlockOpen = false;
if (!entry.text || entry.text.endsWith(BLOCK_JOIN)) return;
- if (entry.text.length < maxChars) entry.text = (entry.text + BLOCK_JOIN).slice(0, maxChars);
+ const offset = entry.text.length;
+ const added = BLOCK_JOIN.slice(0, Math.max(0, maxChars - offset));
+ entry.text += added;
+ if (added) opts.onDelta?.(runId, added, offset);
},
noteToolCall(runId) {
@@ -158,6 +169,7 @@ export function createTurnStream(opts: TurnStreamOptions = {}): TurnStream {
const entry = runs.get(runId);
if (entry) entry.replyDone = true;
else runs.set(runId, makeEntry({ replyDone: true }));
+ opts.onChange?.(runId);
},
isReplyDone(runId) {
diff --git a/src/wiring.ts b/src/wiring.ts
index dc5af77a0..8916aa121 100644
--- a/src/wiring.ts
+++ b/src/wiring.ts
@@ -1,3 +1,6 @@
+import { createMemoryEventBus } from "./util/event-bus.ts";
+import { createPostgresNotifyBus } from "./persistence/postgres-notify-bus.ts";
+import { emitRunText, type RunStreamEvent } from "./runs/run-stream-events.ts";
import { createGatewayCatalog } from "./model/gateway-catalog.ts";
import { createSuggestedActivityService, type SuggestedActivityProfile } from "./suggestions/activities.ts";
import { createRuntimeService } from "./harness/runtime-control.ts";
@@ -1307,17 +1310,37 @@ export function buildApp(
? createPostgresCredentialUsageSink(config.databaseUrl)
: createCredentialUsageSink();
const egressAudit = config.databaseUrl ? createPostgresEgressAuditSink(config.databaseUrl) : createEgressAuditSink();
- const turnStream = createTurnStream();
+ const runStreamEvents = config.databaseUrl
+ ? createPostgresNotifyBus<RunStreamEvent>(config.databaseUrl, "run_stream", "run-stream")
+ : createMemoryEventBus<RunStreamEvent>("run-stream");
+ const refreshRunStream = (runId: string): void => runStreamEvents.emit({ runId, kind: "refresh" });
+ const turnStream = createTurnStream({
+ onDelta: (runId, text, offset) => emitRunText(runStreamEvents, runId, text, offset),
+ onChange: refreshRunStream,
+ });
+ const stopStreamSync = runStreamEvents.subscribe((event) => {
+ if (event.kind !== "sync") return;
+ const text = turnStream.snapshot(event.runId);
+ if (text) emitRunText(runStreamEvents, event.runId, text.slice(event.offset), event.offset);
+ });
+ runs.onTerminal((run) => refreshRunStream(run.id));
const sessionStateBus: SessionStateBus = config.databaseUrl
? createPostgresSessionStateBus(config.databaseUrl)
: createMemorySessionStateBus();
const ledgerEventBus: LedgerEventBus = config.databaseUrl
? createPostgresLedgerEventBus(config.databaseUrl)
: createMemoryLedgerEventBus();
- const runActivity: RunActivityStore =
+ const activityStore: RunActivityStore =
runStoreKind === "postgres"
? createPostgresRunActivityStore(requireDbUrl("RUN_STORE"))
: createMemoryRunActivityStore();
+ const runActivity: RunActivityStore = {
+ ...activityStore,
+ async append(runId, entry) {
+ await activityStore.append(runId, entry);
+ refreshRunStream(runId);
+ },
+ };
const deployStore = createDeployStore({
deployments: artifactMap<Deployment>("deployments"),
...(pgArtifactMap ? { pg: pgArtifactMap.pool } : {}),
@@ -1718,6 +1741,7 @@ export function buildApp(
leaseTtlMs,
maxAttempts,
turnStream,
+ runStreamEvents,
runActivity,
signals: runSignals,
tasks,
@@ -2125,6 +2149,8 @@ export function buildApp(
void sessionStateBus.close?.();
void ledgerEventBus.close?.();
void runActivity.close?.();
+ stopStreamSync();
+ void runStreamEvents.close?.();
await harness.turns.close?.();
await tasks.close?.();
},
diff --git a/test/postgres-run-stream.test.ts b/test/postgres-run-stream.test.ts
new file mode 100644
index 000000000..9cdfd04e7
--- /dev/null
+++ b/test/postgres-run-stream.test.ts
@@ -0,0 +1,63 @@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+import { randomUUID } from "node:crypto";
+import { createPostgresNotifyBus } from "../src/persistence/postgres-notify-bus.ts";
+import { emitRunText, type RunStreamEvent } from "../src/runs/run-stream-events.ts";
+import { createTurnStream } from "../src/runs/turn-stream.ts";
+
+const database = process.env.DATABASE_URL;
+
+test(
+ "independent worker and API connections replay and push Unicode text over Postgres",
+ { skip: !database, timeout: 15_000 },
+ async () => {
+ const channel = `stream_${randomUUID().replaceAll("-", "")}`;
+ const worker = createPostgresNotifyBus<RunStreamEvent>(database!, channel, "test-worker");
+ const api = createPostgresNotifyBus<RunStreamEvent>(database!, channel, "test-api");
+ const stream = createTurnStream({ onDelta: (runId, text, offset) => emitRunText(worker, runId, text, offset) });
+ const runId = randomUUID();
+ stream.begin(runId);
+ stream.publish(runId, "preexisting 🌍 ");
+ let text = "";
+ let complete!: () => void;
+ const received = new Promise<void>((resolve) => {
+ complete = resolve;
+ });
+ const expected = "preexisting 🌍 " + '\u0000\\"🌍'.repeat(900) + " final";
+ const offWorker = await new Promise<() => void>((resolve) => {
+ const off = worker.subscribe(
+ (event) => {
+ if (event.kind === "sync")
+ emitRunText(worker, runId, (stream.snapshot(runId) ?? "").slice(event.offset), event.offset);
+ },
+ { onResync: () => resolve(off) },
+ );
+ });
+ const offApi = await new Promise<() => void>((resolve) => {
+ const off = api.subscribe(
+ (event) => {
+ if (event.kind !== "delta") return;
+ if (event.offset > text.length) {
+ api.emit({ runId, kind: "sync", offset: text.length });
+ return;
+ }
+ text += event.text.slice(text.length - event.offset);
+ if (text === expected) complete();
+ },
+ { onResync: () => resolve(off) },
+ );
+ });
+ try {
+ api.emit({ runId, kind: "sync", offset: 0 });
+ stream.publish(runId, '\u0000\\"🌍'.repeat(900));
+ stream.publish(runId, " final");
+ await received;
+ assert.equal(text, expected);
+ } finally {
+ offWorker();
+ offApi();
+ await worker.close?.();
+ await api.close?.();
+ }
+ },
+);
diff --git a/test/run-events.test.ts b/test/run-events.test.ts
new file mode 100644
index 000000000..f93e367d2
--- /dev/null
+++ b/test/run-events.test.ts
@@ -0,0 +1,75 @@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+import { createServer } from "node:http";
+import type { AddressInfo } from "node:net";
+import { runEventRoutes } from "../src/api/routes/run-events.ts";
+import type { ApiCtx } from "../src/api/routes/route.ts";
+import type { App } from "../src/api/app.ts";
+import { createMemoryEventBus } from "../src/util/event-bus.ts";
+import { emitRunText, type RunStreamEvent } from "../src/runs/run-stream-events.ts";
+
+const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
+
+test("run events push deltas without snapshot polling and recover a separate worker's prefix", async () => {
+ const bus = createMemoryEventBus<RunStreamEvent>("test");
+ let reads = 0;
+ let workerText = "prefix 🌍";
+ let status: "running" | "done" = "running";
+ const app = {
+ async getRun() {
+ reads++;
+ return {
+ status,
+ startedAt: 0,
+ finishedAt: null,
+ result: status === "done" ? { status: "ok", reply: workerText } : null,
+ };
+ },
+ subscribeRun(_runId, listener, onResync) {
+ return bus.subscribe(listener, { onResync });
+ },
+ syncRunStream(runId, offset) {
+ emitRunText(bus, runId, workerText.slice(offset), offset);
+ },
+ } as Pick<App, "getRun" | "subscribeRun" | "syncRunStream">;
+ const server = createServer((req, res) => {
+ void runEventRoutes[0]!.handle({ app, req, res, params: { id: "run" } } as unknown as ApiCtx);
+ });
+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
+ const controller = new AbortController();
+ try {
+ const response = await fetch(`http://127.0.0.1:${(server.address() as AddressInfo).port}`, {
+ signal: controller.signal,
+ });
+ const reader = response.body!.getReader();
+ let wire = "";
+ const consume = (async () => {
+ while (true) {
+ const next = await reader.read();
+ if (next.done) return;
+ wire += new TextDecoder().decode(next.value);
+ }
+ })();
+ await sleep(120);
+ assert.equal(reads, 2, "only initial authorization and race-free hydration read snapshots");
+ workerText += " more";
+ bus.emit({ runId: "run", kind: "delta", offset: 9, text: " more" });
+ await sleep(120);
+ assert.equal(reads, 2, "text publications must not poll the run");
+ assert.match(wire, /more/);
+ workerText += " lost then found";
+ bus.emit({ runId: "run", kind: "delta", offset: 20, text: "found" });
+ await sleep(50);
+ assert.match(wire, /lost then found/);
+ status = "done";
+ bus.emit({ runId: "run", kind: "refresh" });
+ await consume;
+ assert.match(wire, /RUN_FINISHED/);
+ assert.equal(reads, 3);
+ assert.equal(bus.size(), 0, "completion releases subscriptions");
+ } finally {
+ controller.abort();
+ server.closeAllConnections();
+ await new Promise<void>((resolve) => server.close(() => resolve()));
+ }
+});
diff --git a/test/run-stream-events.test.ts b/test/run-stream-events.test.ts
new file mode 100644
index 000000000..0eacb2848
--- /dev/null
+++ b/test/run-stream-events.test.ts
@@ -0,0 +1,49 @@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+import { createTurnStream } from "../src/runs/turn-stream.ts";
+import { emitRunText, type RunStreamEvent } from "../src/runs/run-stream-events.ts";
+import { createMemoryEventBus } from "../src/util/event-bus.ts";
+import { MAX_NOTIFY_PAYLOAD_BYTES } from "../src/persistence/postgres-notify-bus.ts";
+
+test("text publications push exact offsets immediately, including block boundaries and the buffer cap", () => {
+ const changes: string[] = [];
+ const deltas: Array<{ runId: string; text: string; offset: number }> = [];
+ const stream = createTurnStream({
+ maxChars: 9,
+ onDelta: (runId, text, offset) => deltas.push({ runId, text, offset }),
+ onChange: (id) => changes.push(id),
+ });
+ stream.begin("run");
+ stream.publish("run", "hello");
+ assert.deepEqual(deltas, [{ runId: "run", text: "hello", offset: 0 }]);
+ stream.publishBlockStart("run");
+ stream.publish("run", "world");
+ stream.publish("run", "ignored");
+ stream.markReplyDone("run");
+ assert.deepEqual(
+ deltas.map(({ text, offset }) => [text, offset]),
+ [
+ ["hello", 0],
+ ["\n\n", 5],
+ ["wo", 7],
+ ],
+ );
+ assert.deepEqual(changes, ["run", "run"]);
+});
+
+test("cross-process text frames stay under Postgres' payload limit and preserve Unicode and offsets", () => {
+ const bus = createMemoryEventBus<RunStreamEvent>("test");
+ const parts: Array<Extract<RunStreamEvent, { kind: "delta" }>> = [];
+ bus.subscribe((event) => {
+ if (event.kind === "delta") parts.push(event);
+ });
+ const text = '\u0000"\\🌍'.repeat(3_000);
+ emitRunText(bus, "r".repeat(36), text, 37);
+ let offset = 37;
+ for (const part of parts) {
+ assert.equal(part.offset, offset);
+ assert.ok(Buffer.byteLength(JSON.stringify(part)) < MAX_NOTIFY_PAYLOAD_BYTES);
+ offset += part.text.length;
+ }
+ assert.equal(parts.map((part) => part.text).join(""), text);
+});
diff --git a/test/web-ui-sse.test.ts b/test/web-ui-sse.test.ts
index 05e13617f..065bdf3d8 100644
--- a/test/web-ui-sse.test.ts
+++ b/test/web-ui-sse.test.ts
@@ -48,21 +48,19 @@ function asUser(user: string, init: RequestInit = {}): RequestInit {
}
function parseSse(body: string): Array<{ event: string; data: unknown }> {
- const out: Array<{ event: string; data: unknown }> = [];
- for (const frame of body.split("\n\n")) {
- const lines = frame.split("\n");
- const event = lines.find((l) => l.startsWith("event: "))?.slice("event: ".length);
- const dataLine = lines.find((l) => l.startsWith("data: "))?.slice("data: ".length);
- if (!event) continue;
- let data: unknown = undefined;
- try {
- data = dataLine ? JSON.parse(dataLine) : undefined;
- } catch {
- void 0;
- }
- out.push({ event, data });
- }
- return out;
+ return body.split("\n\n").flatMap((frame) => {
+ const data = frame
+ .split("\n")
+ .find((line) => line.startsWith("data: "))
+ ?.slice(6);
+ if (!data) return [];
+ const event = JSON.parse(data) as { type: string; name?: string; value?: unknown };
+ if (event.type !== "CUSTOM") return [{ event: event.type, data: event }];
+ const run = event.value as { status?: string; result?: unknown };
+ if (event.name === "run" && (run.status === "done" || run.status === "failed" || run.result != null))
+ return [{ event: "done", data: run }];
+ return [{ event: event.name ?? event.type, data: event.value }];
+ });
}
test("SSE streams partial frames then a terminal done frame carrying the reply", async () => {
@@ -83,7 +81,7 @@ test("SSE streams partial frames then a terminal done frame carrying the reply",
assert.equal(res.headers.get("x-accel-buffering"), "no");
const events = parseSse(await res.text());
- const partials = events.filter((e) => e.event === "partial");
+ const partials = events.filter((e) => e.event === "delta" || Boolean((e.data as { partial?: string }).partial));
const done = events.find((e) => e.event === "done");
assert.ok(partials.length >= 1, "expected at least one partial frame");
@@ -111,7 +109,7 @@ test("SSE relays tool activity frames and folds them into the done frame", async
assert.equal(res.status, 200);
const events = parseSse(await res.text());
- const activityFrames = events.filter((e) => e.event === "activity");
+ const activityFrames = events.filter((e) => e.event === "run" || e.event === "done");
assert.ok(activityFrames.length >= 1, "expected at least one activity frame for the tool steps");
const done = events.find((e) => e.event === "done");