Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Keep confirmed post replies visible across chat turns (#1115)

Joshua Francef6e592dbafac
diff --git a/plugins/web-ui/src/chat.ts b/plugins/web-ui/src/chat.ts
index 990f44dd7..b640044be 100644
--- a/plugins/web-ui/src/chat.ts
+++ b/plugins/web-ui/src/chat.ts
@@ -21,6 +21,7 @@ import {
Files,
GitFork,
Maximize2,
+ MessageSquare,
Paperclip,
Pause,
Pencil,
@@ -83,6 +84,7 @@ import {
} from "./core-bridge";
import {
buildTimeline,
+ postSpeechText,
toolCategory,
toolRowKind,
toolExecutionOutput,
@@ -246,6 +248,7 @@ export function createChatSurface(
const speaker = (message as { speaker?: string }).speaker;
return typeof speaker === "string" && speaker.trim() ? speaker.trim() : undefined;
}
+ let transcriptRefreshGeneration = 0;
const forkOriginController = createForkOriginController({
state: chatState,
load: async () => {
@@ -293,7 +296,7 @@ export function createChatSurface(
function teardownActiveChat(): void {
transcriptViewport.dispose();
- forkOriginController.invalidateRefresh();
+ transcriptRefreshGeneration++;
readOnlyView = null;
preserveOutgoingWorkingDot(null);
detachActiveAgent();
@@ -704,7 +707,7 @@ export function createChatSurface(
async function refreshTranscriptFromEntries(agent: Agent): Promise<void> {
const sessionId = chatState.sessionId;
if (!sessionId || agent !== chatState.agent || agent.state.isStreaming) return drawActiveChat(agent);
- const generation = forkOriginController.beginRefresh();
+ const generation = ++transcriptRefreshGeneration;
const last = agent.state.messages[agent.state.messages.length - 1] as { stopReason?: string } | undefined;
if (last?.stopReason === "error") return drawActiveChat(agent);
if (last?.stopReason === "aborted" && !runSlot.unreachedAbort) return drawActiveChat(agent);
@@ -712,7 +715,7 @@ export function createChatSurface(
const anchor = chatState.transcriptAnchorSeq;
const page = await transcriptFetcher(sessionId, anchor !== null ? { sinceSeq: anchor } : undefined);
if (
- !forkOriginController.isCurrentRefresh(generation) ||
+ generation !== transcriptRefreshGeneration ||
sessionId !== chatState.sessionId ||
agent !== chatState.agent ||
agent.state.isStreaming
@@ -726,18 +729,15 @@ export function createChatSurface(
page.entries ?? [],
chatState.inheritedLoaded,
);
- forkOriginController.applyRefresh(
- generation,
- refreshedInherited ? entriesToMessages(refreshedInherited, transcriptModel()) : null,
- );
await syncPendingApprovals(agent, messages);
if (
- !forkOriginController.isCurrentRefresh(generation) ||
+ generation !== transcriptRefreshGeneration ||
sessionId !== chatState.sessionId ||
agent !== chatState.agent ||
agent.state.isStreaming
)
return;
+ if (refreshedInherited) chatState.inheritedMessages = entriesToMessages(refreshedInherited, transcriptModel());
agent.state.messages = messages;
runSlot.unreachedAbort = false;
const rawEarlier = page.earlierEntries ?? 0;
@@ -2197,6 +2197,14 @@ export function createChatSurface(
const tool = call.tool ?? result.tool ?? "unknown";
const meta = TOOL_META[tool] ?? UNKNOWN_TOOL;
const secs = elapsedSeconds(row.call?.createdAt) || workSeconds(work);
+ const posting = postSpeechText(row, true);
+ if (posting) {
+ return {
+ icon: MessageSquare,
+ label: secs > 0 ? `Posting message for ${secs}s` : "Posting message",
+ detail: firstLine(posting, 60),
+ };
+ }
return {
icon: meta.icon,
label: secs > 0 ? `${meta.active} for ${secs}s` : meta.active,
@@ -2255,6 +2263,7 @@ export function createChatSurface(
);
};
for (const it of timeline) {
+ const speech = it.kind === "tool" ? postSpeechText(it.row) : null;
const demoted = it.kind === "text" && (it.activity.payload as { demoted?: boolean } | null)?.demoted === true;
// Closing self-logs after a successful surface post are bookkeeping, not
// another piece of visible work. Keeping them in the transcript is useful
@@ -2264,6 +2273,9 @@ export function createChatSurface(
flushSeg();
const text = ((it.activity.payload as { text?: string } | null)?.text ?? "").trim();
if (text) parts.push(html`<div class="work-said">${markdown(text)}</div>`);
+ } else if (speech) {
+ flushSeg();
+ parts.push(html`<div class="work-said">${markdown(speech)}</div>`);
} else {
seg.push(it);
}
@@ -2332,6 +2344,8 @@ export function createChatSurface(
if (item.kind === "thinking") return thinkingRow(item.activity);
if (item.kind === "text") return messageRow(item.activity);
if (item.kind === "approval") return approvalMarker(item.approval);
+ const speech = postSpeechText(item.row);
+ if (speech) return messageRow({ ...item.row.call!, payload: { text: speech } });
return toolRow(item.row, work, status, stale);
}
diff --git a/plugins/web-ui/src/core-bridge.ts b/plugins/web-ui/src/core-bridge.ts
index 76813752e..758df7258 100644
--- a/plugins/web-ui/src/core-bridge.ts
+++ b/plugins/web-ui/src/core-bridge.ts
@@ -1,3 +1,4 @@
+import { postCallText, postResultOk } from "./surface-post.ts";
import type { ModelMetadata } from "./pi-models.ts";
import { createAssistantMessageEventStream } from "@earendil-works/pi-ai";
import type { Attachment } from "@earendil-works/pi-web-ui";
@@ -1633,18 +1634,6 @@ function messageRevisionPayload(payload: unknown): HistorySystemNote | null {
};
}
-function postCallText(payload: unknown): string | null {
- const p = (payload ?? {}) as { action?: unknown; text?: unknown; files?: unknown };
- if (p.action !== "post" || typeof p.text !== "string") return null;
- if (!p.text.trim() && !(Array.isArray(p.files) && p.files.length)) return null;
- return p.text;
-}
-
-function postResultOk(payload: unknown): boolean {
- const p = (payload ?? {}) as { ok?: unknown; isError?: unknown };
- return p.isError !== true && p.ok !== false;
-}
-
function userEntryText(payload: unknown): string | null {
const p = (payload ?? {}) as { text?: unknown; display?: unknown; hidden?: unknown };
if (p.hidden) return null;
diff --git a/plugins/web-ui/src/fork-origin.ts b/plugins/web-ui/src/fork-origin.ts
index ff562a250..91eca4bbf 100644
--- a/plugins/web-ui/src/fork-origin.ts
+++ b/plugins/web-ui/src/fork-origin.ts
@@ -23,7 +23,6 @@ export function createForkOriginController<T>(options: {
redraw(): void;
setError(error: string): void;
}) {
- let refreshGeneration = 0;
let toggleGeneration = 0;
let loadingGeneration: number | null = null;
return {
@@ -32,20 +31,6 @@ export function createForkOriginController<T>(options: {
toggleGeneration++;
loadingGeneration = null;
},
- beginRefresh() {
- return ++refreshGeneration;
- },
- invalidateRefresh() {
- refreshGeneration++;
- },
- isCurrentRefresh(generation: number) {
- return generation === refreshGeneration && options.current();
- },
- applyRefresh(generation: number, messages: T[] | null) {
- if (generation !== refreshGeneration || !options.current()) return false;
- if (messages) options.state.inheritedMessages = messages;
- return true;
- },
async navigate() {
try {
await options.navigate();
diff --git a/plugins/web-ui/src/surface-post.ts b/plugins/web-ui/src/surface-post.ts
new file mode 100644
index 000000000..7d4a9c6f3
--- /dev/null
+++ b/plugins/web-ui/src/surface-post.ts
@@ -0,0 +1,17 @@
+export function postCallText(payload: unknown): string | null {
+ const p = (payload ?? {}) as { action?: unknown; text?: unknown; files?: unknown };
+ if (p.action !== "post" || typeof p.text !== "string") return null;
+ if (!p.text.trim() && !(Array.isArray(p.files) && p.files.length)) return null;
+ return p.text;
+}
+
+export function postResultOk(payload: unknown): boolean {
+ const p = (payload ?? {}) as {
+ ok?: unknown;
+ isError?: unknown;
+ error?: unknown;
+ denied?: unknown;
+ blocked?: unknown;
+ };
+ return payload != null && p.isError !== true && p.ok !== false && !p.error && p.denied !== true && !p.blocked;
+}
diff --git a/plugins/web-ui/src/timeline.ts b/plugins/web-ui/src/timeline.ts
index 8b0a1fafa..3044b7e6b 100644
--- a/plugins/web-ui/src/timeline.ts
+++ b/plugins/web-ui/src/timeline.ts
@@ -1,3 +1,4 @@
+import { postCallText, postResultOk } from "./surface-post.ts";
import type { PendingApproval, ToolActivity, WorkBlock } from "./core-bridge.ts";
export interface ToolPayload {
@@ -95,6 +96,13 @@ function callIdOf(a: ToolActivity): string | undefined {
return typeof id === "string" && id ? id : undefined;
}
+export function postSpeechText(row: ToolRowModel, allowInFlight = false): string | null {
+ const text = postCallText(row.call?.payload);
+ if (!text?.trim()) return null;
+ if (row.result) return postResultOk(row.result.payload) ? text : null;
+ return allowInFlight && !row.approval && !row.pending ? text : null;
+}
+
function orphanCallSignature(row: ToolRowModel): string | null {
if (!row.call || row.result || row.approval) return null;
const p = (row.call.payload ?? {}) as ToolPayload;
diff --git a/plugins/web-ui/test/history.test.ts b/plugins/web-ui/test/history.test.ts
index c383d6366..fea430992 100644
--- a/plugins/web-ui/test/history.test.ts
+++ b/plugins/web-ui/test/history.test.ts
@@ -317,18 +317,12 @@ test("fork origin DOM navigates, reports access failure once, pages, toggles, su
assert.match(host.textContent ?? "", /2 messages/);
host.querySelector<HTMLButtonElement>(".fork-origin-toggle")!.click();
assert.doesNotMatch(host.textContent ?? "", /old one/);
- const staleGeneration = controller.beginRefresh();
- const generation = controller.beginRefresh();
const refresh = inheritedRefreshEntries(
session,
[{ type: "assistant", payload: { text: "new reply" }, createdAt: 3, seq: 3 }],
state.inheritedLoaded,
);
- assert.equal(controller.applyRefresh(generation, refresh), true);
- assert.equal(
- controller.applyRefresh(staleGeneration, [{ type: "user", payload: { text: "stale" }, createdAt: 0, seq: 1 }]),
- false,
- );
+ assert.equal(refresh, null);
host.querySelector<HTMLButtonElement>(".fork-origin-toggle")!.click();
assert.match(host.textContent ?? "", /old one/);
assert.equal(host.textContent?.match(/old one/g)?.length, 1);
diff --git a/plugins/web-ui/test/post-reply-recovery.test.ts b/plugins/web-ui/test/post-reply-recovery.test.ts
new file mode 100644
index 000000000..9c91d5cef
--- /dev/null
+++ b/plugins/web-ui/test/post-reply-recovery.test.ts
@@ -0,0 +1,402 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import { JSDOM } from "jsdom";
+import { createServer } from "vite";
+import { metadata } from "./model-metadata.ts";
+import type { Conversation } from "../src/conv-types.ts";
+import type { SessionEntry } from "../src/core-bridge.ts";
+
+class FakeEventSource {
+ static instances: FakeEventSource[] = [];
+ onopen: (() => void) | null = null;
+ onerror: (() => void) | null = null;
+ listeners = new Map<string, Array<(event: { data: string }) => void>>();
+ closed = false;
+ url: string;
+ constructor(url: string) {
+ this.url = url;
+ FakeEventSource.instances.push(this);
+ }
+ addEventListener(name: string, listener: (event: { data: string }) => void) {
+ this.listeners.set(name, [...(this.listeners.get(name) ?? []), listener]);
+ }
+ emit(name: string, data: unknown) {
+ for (const listener of this.listeners.get(name) ?? []) listener({ data: JSON.stringify(data) });
+ }
+ close() {
+ this.closed = true;
+ }
+}
+
+const settle = () => new Promise((resolve) => setTimeout(resolve, 160));
+
+async function until(check: () => boolean): Promise<void> {
+ for (let i = 0; i < 200; i++) {
+ if (check()) return;
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ }
+ assert.fail("condition did not settle");
+}
+
+test("post replies remain visible in new and continuing conversations", async (t) => {
+ const dom = new JSDOM('<!doctype html><div id="app"></div><main id="main"></main>', { url: "http://localhost/" });
+ Object.defineProperty(dom.window, "matchMedia", {
+ value: () => ({ matches: false, addEventListener() {}, removeEventListener() {} }),
+ });
+ Object.defineProperty(dom.window.document, "visibilityState", { configurable: true, value: "visible" });
+ const globals = {
+ window: dom.window,
+ document: dom.window.document,
+ location: dom.window.location,
+ history: dom.window.history,
+ localStorage: dom.window.localStorage,
+ navigator: dom.window.navigator,
+ HTMLElement: dom.window.HTMLElement,
+ customElements: dom.window.customElements,
+ Node: dom.window.Node,
+ Event: dom.window.Event,
+ InputEvent: dom.window.InputEvent,
+ KeyboardEvent: dom.window.KeyboardEvent,
+ requestAnimationFrame: (callback: FrameRequestCallback) => setTimeout(() => callback(Date.now()), 0),
+ cancelAnimationFrame: clearTimeout,
+ getComputedStyle: dom.window.getComputedStyle.bind(dom.window),
+ EventSource: FakeEventSource,
+ };
+ for (const [key, value] of Object.entries(globals))
+ Object.defineProperty(globalThis, key, { configurable: true, writable: true, value });
+ const row = {
+ id: "s1",
+ threadRef: "web:owner:repro",
+ scopeId: "personal:owner",
+ title: "Synthetic reply repro",
+ type: "dm",
+ createdAt: Date.now(),
+ };
+ const user: SessionEntry = { seq: 0, type: "user", createdAt: Date.now(), payload: { text: "Please answer" } };
+ const answer = "The durable reply is present in storage.";
+ const completed: SessionEntry[] = [
+ user,
+ {
+ seq: 1,
+ type: "tool_call",
+ createdAt: Date.now(),
+ payload: { tool: "web", action: "post", text: answer, callId: "c1" },
+ },
+ {
+ seq: 2,
+ parentSeq: 1,
+ type: "tool_result",
+ createdAt: Date.now(),
+ payload: { tool: "web", action: "post", ok: true, callId: "c1", result: "[sent]" },
+ },
+ { seq: 3, type: "assistant", createdAt: Date.now(), payload: { text: "Replied in thread." } },
+ ];
+ let entries = [user];
+ let transcriptFails = false;
+ let intercept: ((path: string) => Promise<Response> | undefined) | undefined;
+ const requests: string[] = [];
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = async (input) => {
+ const path = String(input);
+ requests.push(path);
+ const intercepted = intercept?.(path);
+ if (intercepted) return intercepted;
+ if (path.includes("runtime-config"))
+ return Response.json({
+ scopeId: row.scopeId,
+ approvedHarnesses: ["pi"],
+ modelsByHarness: { pi: ["test-model"] },
+ modelCatalog: { "test-model": metadata("test-model", "Test model") },
+ orgDefault: { harnessId: "pi", modelId: "test-model", revision: 0 },
+ effective: { harnessId: "pi", modelId: "test-model" },
+ scopeOverride: null,
+ });
+ if (path.includes("/api/runs/active")) return Response.json({ runId: null, queued: [] });
+ if (path === "/api/turn") return Response.json({ runId: "r1" });
+ if (path.endsWith("/approvals")) return Response.json({ approvals: [] });
+ if (path.startsWith("/api/sessions/s1"))
+ return transcriptFails
+ ? Response.json({ error: "temporary synthetic outage" }, { status: 503 })
+ : Response.json({ session: row, entries, earlierEntries: 0 });
+ if (path === "/api/sessions") return Response.json({ sessions: [row] });
+ if (path === "/api/contexts") return Response.json({ contexts: [] });
+ throw new Error(`Unexpected request: ${path}`);
+ };
+ const vite = await createServer({ server: { middlewareMode: true, hmr: false }, appType: "custom" });
+ let conv: Conversation | undefined;
+ try {
+ await vite.ssrLoadModule("/src/shell.ts");
+ const { appState } = await vite.ssrLoadModule("/src/shell-state.ts");
+ const { sessionsState } = await vite.ssrLoadModule("/src/sessions.ts");
+ const { createConversation, disposeConversation, ensureDeliveryStream } =
+ await vite.ssrLoadModule("/src/conversations.ts");
+ const { entriesToMessages } = await vite.ssrLoadModule("/src/core-bridge.ts");
+ const { transcriptModel } = await vite.ssrLoadModule("/src/model-options.ts");
+ const { seedRuntimeConfig } = await vite.ssrLoadModule("/src/composer.ts");
+ seedRuntimeConfig(row.scopeId, await (await fetch("/api/runtime-config")).json());
+ appState.me = { user: "owner", org: "test" };
+ appState.currentView = "chats";
+ sessionsState.list = [row];
+ sessionsState.loaded = true;
+ const host = document.querySelector<HTMLElement>("#main")!;
+ appState.mainEl = host;
+ ensureDeliveryStream();
+ const delivery = FakeEventSource.instances.find((es) => es.url === "/api/deliveries/events")!;
+ const shownAnswer = () =>
+ [...host.querySelectorAll(".assistant-body > .streaming-text")].some((el) => el.textContent?.includes(answer));
+ async function mount(recorded: SessionEntry[] = [user], readOnly = false, wait = true) {
+ if (conv) disposeConversation(conv);
+ entries = recorded;
+ transcriptFails = false;
+ conv = createConversation({
+ pane: true,
+ ownsUrl: false,
+ container: () => host,
+ claimContainer: () => host,
+ visible: () => true,
+ density: () => "full",
+ onDensityChange() {},
+ ensureDeliveryStream,
+ }) as Conversation;
+ if (readOnly) conv.mountReadOnly(row as never, entriesToMessages(recorded, transcriptModel()));
+ else {
+ conv.mountContinuable(
+ row.threadRef,
+ row.id,
+ row.scopeId,
+ entriesToMessages(recorded, transcriptModel()),
+ null,
+ row as never,
+ );
+ conv.state.agent!.convertToLlm = () => [{ role: "user", content: "Please answer", timestamp: 0 }];
+ }
+ if (wait) await settle();
+ requests.length = 0;
+ }
+ const shown = (text: string) =>
+ [...host.querySelectorAll(".assistant-body > .streaming-text, .work-said")].some((el) =>
+ el.textContent?.includes(text),
+ );
+ async function postTurn(index: number, fails = false) {
+ const text = `Confirmed answer ${index}`;
+ const seq = index * 3;
+ const activity: SessionEntry[] = [
+ {
+ seq: seq + 1,
+ type: "tool_call",
+ createdAt: Date.now(),
+ payload: { tool: "web", action: "post", text, callId: `post-${index}` },
+ },
+ {
+ seq: seq + 2,
+ type: "tool_result",
+ createdAt: Date.now(),
+ payload: { tool: "web", callId: `post-${index}`, isError: false, ok: true, result: "[sent]" },
+ },
+ ];
+ const before = FakeEventSource.instances.length;
+ const turn = conv!.state.agent!.prompt(`Question ${index}`);
+ await until(() => FakeEventSource.instances.length > before);
+ const run = FakeEventSource.instances.findLast((es) => es.url === "/api/runs/r1/events")!;
+ run.onopen?.();
+ entries = [
+ ...entries,
+ { seq, type: "user", createdAt: Date.now(), payload: { text: `Question ${index}` } },
+ ...activity,
+ ];
+ transcriptFails = fails;
+ run.emit("done", { status: "done", result: { status: "silent", sessionId: row.id }, activity });
+ await turn;
+ await settle();
+ return text;
+ }
+ await t.test("control: reopening displays a persisted post", async () => {
+ await mount(completed);
+ assert.ok(shownAnswer());
+ });
+ await t.test("a new session accepts every transcript after adoption without fork metadata", async () => {
+ await mount();
+ conv!.mountContinuable(row.threadRef, null, row.scopeId, []);
+ conv!.state.agent!.convertToLlm = () => [{ role: "user", content: "Please answer", timestamp: 0 }];
+ entries = [];
+ const visible: boolean[] = [];
+ for (let i = 0; i < 3; i++) {
+ const text = await postTurn(i);
+ assert.equal(conv!.state.sessionId, row.id);
+ visible.push(shown(text));
+ }
+ assert.deepEqual(
+ visible,
+ [true, true, true],
+ "repeated posts must not stay hidden for the life of a new session",
+ );
+ assert.equal(conv!.state.forkSession, null);
+ const messages = conv!.state.agent!.state.messages;
+ assert.ok(messages.some((m) => JSON.stringify(m).includes("Confirmed answer 2")));
+ assert.equal(conv!.state.transcriptAnchorSeq, null);
+ assert.ok(
+ messages.some(
+ (m) =>
+ m.role === "assistant" &&
+ JSON.stringify((m as { content?: unknown }).content).includes("Confirmed answer 2"),
+ ),
+ "the durable transcript must actually apply, not just the activity fallback",
+ );
+ });
+ await t.test("confirmed posts remain visible over consecutive failed transcript refreshes", async () => {
+ await mount();
+ const visible: boolean[] = [];
+ for (let i = 0; i < 3; i++) visible.push(shown(await postTurn(i, true)));
+ assert.deepEqual(visible, [true, true, true]);
+ transcriptFails = false;
+ delivery.emit("delivery", { threadRef: row.threadRef });
+ await settle();
+ for (let i = 0; i < 3; i++) {
+ assert.equal(
+ [...host.querySelectorAll(".assistant-body > .streaming-text, .work-said")].filter((el) =>
+ el.textContent?.includes(`Confirmed answer ${i}`),
+ ).length,
+ 1,
+ "refresh must not duplicate a displayed post",
+ );
+ }
+ });
+ function deferredTranscript(recorded: SessionEntry[]) {
+ let release!: () => void;
+ const promise = new Promise<Response>((resolve) => {
+ release = () => resolve(Response.json({ session: row, entries: recorded, earlierEntries: 0 }));
+ });
+ return { promise, release };
+ }
+ await t.test("a late transcript cannot replace a newer successful refresh", async () => {
+ await mount();
+ const pending = deferredTranscript([user]);
+ intercept = (path) =>
+ path.startsWith("/api/sessions/s1") && !path.endsWith("/approvals") ? pending.promise : undefined;
+ delivery.emit("delivery", { threadRef: row.threadRef });
+ await until(() => requests.some((path) => path.startsWith("/api/sessions/s1")));
+ intercept = undefined;
+ entries = completed;
+ delivery.emit("delivery", { threadRef: row.threadRef });
+ await settle();
+ assert.ok(shownAnswer());
+ pending.release();
+ await settle();
+ assert.ok(shownAnswer());
+ });
+ await t.test("a transcript started before teardown cannot write into the replacement chat", async () => {
+ await mount();
+ const oldAgent = conv!.state.agent;
+ const pending = deferredTranscript(completed);
+ intercept = (path) =>
+ path.startsWith("/api/sessions/s1") && !path.endsWith("/approvals") ? pending.promise : undefined;
+ delivery.emit("delivery", { threadRef: row.threadRef });
+ await until(() => requests.some((path) => path.startsWith("/api/sessions/s1")));
+ intercept = undefined;
+ conv!.mountContinuable("web:owner:replacement", null, row.scopeId, []);
+ pending.release();
+ await settle();
+ assert.notEqual(conv!.state.agent, oldAgent);
+ assert.equal(shownAnswer(), false);
+ assert.equal(conv!.state.threadRef, "web:owner:replacement");
+ });
+ await t.test("fork history follows the same refresh generation and preserves fully loaded history", async () => {
+ await mount();
+ conv!.state.forkSession = { ...row, forkedFrom: { sessionId: "source" }, forkBoundarySeq: 0 } as never;
+ conv!.state.inheritedLoaded = false;
+ const pending = deferredTranscript([{ ...user, payload: { text: "Stale inherited text" } }]);
+ intercept = (path) =>
+ path.startsWith("/api/sessions/s1") && !path.endsWith("/approvals") ? pending.promise : undefined;
+ delivery.emit("delivery", { threadRef: row.threadRef });
+ await until(() => requests.some((path) => path.startsWith("/api/sessions/s1")));
+ intercept = undefined;
+ entries = [{ ...user, payload: { text: "Current inherited text" } }, ...completed.slice(1)];
+ delivery.emit("delivery", { threadRef: row.threadRef });
+ await settle();
+ const inherited = JSON.stringify(conv!.state.inheritedMessages);
+ assert.match(inherited, /Current inherited text/);
+ pending.release();
+ await settle();
+ assert.equal(JSON.stringify(conv!.state.inheritedMessages), inherited);
+ conv!.state.inheritedLoaded = true;
+ entries = completed;
+ delivery.emit("delivery", { threadRef: row.threadRef });
+ await settle();
+ assert.equal(JSON.stringify(conv!.state.inheritedMessages), inherited);
+ });
+ await t.test("navigation during approval loading cannot update the replacement chat", async () => {
+ await mount();
+ let release!: () => void;
+ const promise = new Promise<Response>((resolve) => {
+ release = () => resolve(Response.json({ approvals: [] }));
+ });
+ entries = completed;
+ intercept = (path) => (path.endsWith("/approvals") ? promise : undefined);
+ delivery.emit("delivery", { threadRef: row.threadRef });
+ await until(() => requests.some((path) => path.endsWith("/approvals")));
+ conv!.mountContinuable("web:owner:replacement", null, row.scopeId, []);
+ release();
+ intercept = undefined;
+ await settle();
+ assert.equal(shownAnswer(), false);
+ assert.deepEqual(conv!.state.agent!.state.messages, []);
+ });
+ await t.test("multiple posts in one turn appear once before and after transcript recovery", async () => {
+ await mount();
+ const activity: SessionEntry[] = [
+ ...completed.slice(1, 3),
+ {
+ seq: 3,
+ type: "tool_call",
+ createdAt: Date.now(),
+ payload: { tool: "web", action: "post", text: "Second confirmed reply", callId: "c2" },
+ },
+ {
+ seq: 4,
+ type: "tool_result",
+ createdAt: Date.now(),
+ payload: { tool: "web", callId: "c2", isError: false, result: "[sent]" },
+ },
+ ];
+ const before = FakeEventSource.instances.length;
+ const turn = conv!.state.agent!.prompt("Send two replies");
+ await until(() => FakeEventSource.instances.length > before);
+ const run = FakeEventSource.instances.findLast((es) => es.url === "/api/runs/r1/events")!;
+ run.onopen?.();
+ transcriptFails = true;
+ run.emit("done", { status: "done", result: { status: "silent", sessionId: row.id }, activity });
+ await turn;
+ await settle();
+ const count = (text: string) =>
+ [...host.querySelectorAll(".assistant-body > .streaming-text, .work-said")].filter((el) =>
+ el.textContent?.includes(text),
+ ).length;
+ assert.equal(count(answer), 1);
+ assert.equal(count("Second confirmed reply"), 1);
+ entries = [
+ user,
+ ...activity,
+ { seq: 5, type: "assistant", createdAt: Date.now(), payload: { text: "Replies sent." } },
+ ];
+ transcriptFails = false;
+ delivery.emit("delivery", { threadRef: row.threadRef });
+ await settle();
+ assert.equal(count(answer), 1);
+ assert.equal(count("Second confirmed reply"), 1);
+ assert.equal(count("Replies sent."), 0);
+ });
+ if (conv) {
+ disposeConversation(conv);
+ conv = undefined;
+ }
+ } finally {
+ conv?.state.agent?.abort();
+ await conv?.state.agent?.waitForIdle();
+ conv?.composer.dispose();
+ conv?.dispose();
+ for (const es of FakeEventSource.instances) es.close();
+ await vite.close();
+ globalThis.fetch = originalFetch;
+ dom.window.close();
+ }
+});
diff --git a/plugins/web-ui/test/surface-post.test.ts b/plugins/web-ui/test/surface-post.test.ts
new file mode 100644
index 000000000..4562b0062
--- /dev/null
+++ b/plugins/web-ui/test/surface-post.test.ts
@@ -0,0 +1,60 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import { postCallText, postResultOk } from "../src/surface-post.ts";
+import { postSpeechText, type ToolRowModel } from "../src/timeline.ts";
+import type { ToolActivity } from "../src/core-bridge.ts";
+
+const call: ToolActivity = {
+ seq: 1,
+ parentSeq: null,
+ type: "tool_call",
+ createdAt: 1,
+ payload: { tool: "web", action: "post", text: "The answer", callId: "post" },
+};
+
+for (const [name, payload, ok] of [
+ ["successful", { ok: true, isError: false }, true],
+ ["legacy sent", { result: "[sent]", isError: false }, true],
+ ["unscreened success", { result: "untrusted wrapper: [sent]", isError: false, unscreened: true }, true],
+ ["failed", { ok: false }, false],
+ ["error flag", { isError: true }, false],
+ ["error detail", { error: "delivery failed" }, false],
+ ["denied", { denied: true }, false],
+ ["approval blocked", { blocked: "needs_approval" }, false],
+ ["missing payload", null, false],
+] as const) {
+ test(`${name} post uses the same delivery decision in live and persisted views`, () => {
+ const row: ToolRowModel = {
+ call,
+ result: { seq: 2, parentSeq: 1, type: "tool_result", createdAt: 2, payload },
+ };
+ assert.equal(postResultOk(payload), ok);
+ assert.equal(postSpeechText(row), ok ? "The answer" : null);
+ assert.equal(postSpeechText(row, true), ok ? "The answer" : null);
+ });
+}
+
+test("an unconfirmed post can preview posting but cannot appear as delivered speech", () => {
+ const row: ToolRowModel = { call, result: null };
+ assert.equal(postSpeechText(row), null);
+ assert.equal(postSpeechText(row, true), "The answer");
+ row.approval = { ...call, type: "approval_request" };
+ assert.equal(postSpeechText(row, true), null);
+});
+
+test("file-only posts remain eligible for transcript attachments, not text bubbles", () => {
+ const payload = { action: "post", text: "", files: ["report.pdf"] };
+ assert.equal(postCallText(payload), "");
+ assert.equal(postSpeechText({ call: { ...call, payload }, result: null }, true), null);
+});
+
+test("non-post and legacy bytes-only calls remain tool activity", () => {
+ for (const payload of [
+ { action: "read", text: "not speech" },
+ { action: "post", bytes: 100 },
+ { action: "post", text: " " },
+ ]) {
+ assert.equal(postCallText(payload), null);
+ assert.equal(postSpeechText({ call: { ...call, payload }, result: null }, true), null);
+ }
+});