Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Adopt saved session identity before the first turn finishes (#1643)

Regan Bellfd7e132620ed
diff --git a/plugins/web-ui/src/chat.ts b/plugins/web-ui/src/chat.ts
index f1a866aa0..e2ca376d5 100644
--- a/plugins/web-ui/src/chat.ts
+++ b/plugins/web-ui/src/chat.ts
@@ -934,8 +934,6 @@ export function createChatSurface(
chatState.rememberedScopeId = match.scopeId;
chatState.rememberedContextName = chatState.contextName;
syncLocation();
- renderList();
- drawActiveChat(agent);
}
function postCurrentPaneState(): void {
@@ -1396,6 +1394,7 @@ export function createChatSurface(
function drawActiveChat(agent = chatState.agent, opts: { forceScroll?: boolean } = {}): void {
if (!agent || agent !== chatState.agent || !chatState.host || appState.currentView !== "chats") return;
+ adoptActiveSessionFromList(agent);
if (!ctx.visible()) {
postCurrentPaneState();
return;
diff --git a/plugins/web-ui/src/split.ts b/plugins/web-ui/src/split.ts
index b943d3972..9a868224e 100644
--- a/plugins/web-ui/src/split.ts
+++ b/plugins/web-ui/src/split.ts
@@ -59,7 +59,6 @@ import { contextsState, scopeTitle } from "./contexts";
import type { DensityTier } from "./density";
import { appState } from "./shell-state";
import { renderSidebarTop, switchView, syncDocumentTitle, syncUrlFromState } from "./shell";
-import { sleep } from "./chat";
import {
createConversation,
disposeConversation,
@@ -1585,28 +1584,9 @@ function notePaneSession(paneId: string, sessionId: string | null, threadRef: st
...(threadRef ? { threadRef } : {}),
});
persist();
- if (sessionId) void settlePaneTitle(sessionId);
refreshHeaders();
}
-async function settlePaneTitle(sessionId: string): Promise<void> {
- const titled = (): boolean => Boolean(sessionsState.list.find((s) => s.id === sessionId)?.title?.trim());
- if (!titled()) await settlePoll([0, 1200, 2400, 4000, 6000], titled);
-}
-
-async function settlePoll(delays: number[], done: () => boolean): Promise<void> {
- for (const delay of delays) {
- if (delay) await sleep(delay);
- if (!splitState.active) return;
- try {
- await refreshSessions({ silent: true });
- } catch {
- void 0;
- }
- if (done()) return;
- }
-}
-
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && splitState.active && dockApi?.hasMaximizedGroup()) dockApi.exitMaximizedGroup();
});
diff --git a/plugins/web-ui/test/deep-link-boot-fixture.ts b/plugins/web-ui/test/deep-link-boot-fixture.ts
index 6487c56f1..36ba0a6fe 100644
--- a/plugins/web-ui/test/deep-link-boot-fixture.ts
+++ b/plugins/web-ui/test/deep-link-boot-fixture.ts
@@ -1,5 +1,6 @@
import { JSDOM } from "jsdom";
import { createServer } from "vite";
+import type { Conversation } from "../src/conv-types.ts";
import type { CoreSession, TranscriptPage } from "../src/core-bridge.ts";
export interface Harness {
@@ -11,10 +12,11 @@ export interface Harness {
releaseTranscript: () => void;
releaseApprovals: () => void;
sessionsReady: () => Promise<void>;
+ refreshSessions: () => Promise<boolean>;
boot: () => Promise<void>;
appState: { currentView: string };
sessionsState: { list: Array<{ id: string }>; loaded: boolean; openingKey: string | null };
- visibleConversation: () => { state: { sessionId: string | null; threadRef: string | null } };
+ visibleConversation: () => Conversation;
mainText: () => string;
close: () => Promise<void>;
}
@@ -243,6 +245,7 @@ export async function harness(opts: HarnessOptions): Promise<Harness> {
releaseTranscript,
releaseApprovals,
sessionsReady: sessions.sessionsReady as () => Promise<void>,
+ refreshSessions: sessions.refreshSessions as () => Promise<boolean>,
boot: shell.boot as () => Promise<void>,
appState: shell.appState as Harness["appState"],
sessionsState: sessions.sessionsState as Harness["sessionsState"],
diff --git a/plugins/web-ui/test/new-session-title.test.ts b/plugins/web-ui/test/new-session-title.test.ts
new file mode 100644
index 000000000..6d564bc0a
--- /dev/null
+++ b/plugins/web-ui/test/new-session-title.test.ts
@@ -0,0 +1,66 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import { getModel } from "@earendil-works/pi-ai";
+import { harness } from "./deep-link-boot-fixture.ts";
+import type { CoreSession } from "../src/core-bridge.ts";
+
+test("a new pane adopts its saved identity and later title while its first turn is streaming", async () => {
+ const listSessions: CoreSession[] = [];
+ const h = await harness({ path: "/", listSessions });
+ let stopStream = (): void => {};
+ let turn: Promise<void> | undefined;
+ let turnError: unknown;
+ try {
+ h.releaseSessions();
+ await h.boot();
+ const conversation = h.visibleConversation();
+ const { agent, threadRef } = conversation.state;
+ assert.ok(agent);
+ assert.ok(threadRef);
+ assert.equal(conversation.state.sessionId, null);
+ assert.match(document.querySelector(".split-pane-title-text")?.textContent ?? "", /New session/);
+ await agent.waitForIdle();
+ agent.state.model = getModel("openai", "gpt-4o");
+ agent.streamFn = () =>
+ new Promise((_resolve, reject) => {
+ stopStream = () => reject(new Error("QA stream stopped"));
+ });
+ turn = agent.prompt("Investigate live title updates").catch((error) => {
+ turnError = error;
+ });
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ assert.ok(agent.state.isStreaming, String(turnError));
+ const session: CoreSession = {
+ id: "newly-saved-session",
+ threadRef,
+ scopeId: "personal:tester",
+ type: "dm",
+ createdAt: Date.now(),
+ title: "",
+ };
+ listSessions.push(session);
+ await h.refreshSessions();
+ assert.equal(conversation.state.sessionId, session.id);
+ assert.equal(conversation.state.rememberedSessionId, session.id);
+ assert.equal(location.pathname, `/s/${session.id}`);
+ assert.ok(document.querySelector('[aria-label="Share conversation"]'));
+ assert.match(localStorage.getItem("web-ui:split-canvas:v1") ?? "", /newly-saved-session/);
+
+ session.title = "Investigate live title updates";
+ await h.refreshSessions();
+ assert.equal(document.querySelector(".split-pane-title-text")?.textContent, session.title);
+ assert.match(document.title, /Investigate live title updates/);
+ assert.ok(agent.state.isStreaming, String(turnError));
+ assert.equal(conversation.state.agent, agent);
+ assert.equal(document.querySelectorAll(".split-pane-content").length, 1);
+
+ session.title = "Renamed during the same turn";
+ await h.refreshSessions();
+ assert.equal(document.querySelector(".split-pane-title-text")?.textContent, session.title);
+ assert.ok(agent.state.isStreaming, String(turnError));
+ } finally {
+ stopStream();
+ await turn;
+ await h.close();
+ }
+});