Multiplayer agent harness for work.
Keep the sidebar off the chat-switch path (#2435)
Regan Bellc9b612c981b1
diff --git a/plugins/web-ui/src/sessions.ts b/plugins/web-ui/src/sessions.tsindex 23971c2c0..7aa376731 100644--- a/plugins/web-ui/src/sessions.ts+++ b/plugins/web-ui/src/sessions.ts@@ -195,6 +195,7 @@ export function resetSessionsState(): void {sessionPatchVersions.clear();sessionsState.list = [];sessionsState.loaded = false;+ listCurrent = false;listRequestedAt = 0;sessionsRefreshRunning = false;queuedSessionsRefresh = null;@@ -1681,9 +1682,11 @@ let queuedSessionsRefresh: SessionsRefreshOptions | null = null;let sessionsRefreshRunning = false;let listRequestedAt = 0;let listDiscards = 0;+let listCurrent = false;const LIST_STALL_MS = 10_000;export function refreshSessionsOnOpen(): void {+ if (listCurrent) return;const joinable =sessionsRefreshRunning && Date.now() - listRequestedAt < LIST_STALL_MS ? latestSessionsRefresh : null;if (!joinable) {@@ -1753,10 +1756,12 @@ async function runSessionsRefresh(}sessionsState.list = reconcileSessions(r.sessions ?? [], sessionsState.list, openConversationIds());sessionsState.loaded = true;+ listCurrent = true;sessionsNotice = "";return true;} catch (e) {if (seq !== sessionRefreshSeq) return sessionsState.loaded || ((await newerRun()) ?? false);+ listCurrent = false;if (!opts.silent) sessionsNotice = errMessage(e, "Failed to load conversations.");return false;} finally {diff --git a/plugins/web-ui/src/subagent-activity.ts b/plugins/web-ui/src/subagent-activity.tsindex 617666f5d..9e6277f92 100644--- a/plugins/web-ui/src/subagent-activity.ts+++ b/plugins/web-ui/src/subagent-activity.ts@@ -12,18 +12,36 @@ export interface SubagentRow {type SessionLink = Pick<CoreSession, "id" | "parentSessionId" | "working" | "awaitingInput">;+const childIndexes = new WeakMap<readonly SessionLink[], Map<string, number[]>>();++function childPositions(list: readonly SessionLink[]): Map<string, number[]> {+ const index = childIndexes.get(list);+ if (index) return index;+ const built = new Map<string, number[]>();+ list.forEach((session, position) => {+ if (!session.parentSessionId) return;+ const siblings = built.get(session.parentSessionId);+ if (siblings) siblings.push(position);+ else built.set(session.parentSessionId, [position]);+ });+ childIndexes.set(list, built);+ return built;+}+export function descendantsOf<T extends SessionLink>(list: readonly T[],rootId: string,): { session: T; depth: number }[] {+ const index = childPositions(list);const out: { session: T; depth: number }[] = [];const seen = new Set([rootId]);let frontier = [rootId];for (let depth = 1; frontier.length; depth++) {- const parents = new Set(frontier);+ const level = frontier.flatMap((id) => index.get(id) ?? []).sort((a, b) => a - b);frontier = [];- for (const session of list) {- if (!session.parentSessionId || !parents.has(session.parentSessionId) || seen.has(session.id)) continue;+ for (const position of level) {+ const session = list[position];+ if (seen.has(session.id)) continue;seen.add(session.id);out.push({ session, depth });frontier.push(session.id);diff --git a/plugins/web-ui/test/sessions-refresh-race.test.ts b/plugins/web-ui/test/sessions-refresh-race.test.tsindex d53c8285a..70d25ad44 100644--- a/plugins/web-ui/test/sessions-refresh-race.test.ts+++ b/plugins/web-ui/test/sessions-refresh-race.test.ts@@ -139,7 +139,7 @@ function jsdomGlobals(): JSDOM {return dom;}-test("opening a conversation joins the list refresh already in flight instead of starting its own", async () => {+test("opening a conversation joins the first list load and leaves a current list to the delivery stream", async () => {const dom = jsdomGlobals();const pending: Array<(r: Response) => void> = [];globalThis.fetch = async (input) => {@@ -162,13 +162,13 @@ test("opening a conversation joins the list refresh already in flight instead ofawait sessionsReady();refreshSessionsOnOpen();- await new Promise((resolve) => setTimeout(resolve, 0));- assert.equal(pending.length, 2, "once it has landed, opening a conversation freshens the list again");-refreshSessionsOnOpen();- refreshSessionsOnOpen();- assert.equal(pending.length, 2, "…and panes restored together still share one refresh, loaded list or not");- pending[1]!(Response.json({ sessions: [] }));+ await new Promise((resolve) => setTimeout(resolve, 0));+ assert.equal(+ pending.length,+ 1,+ "once it has landed, opening a conversation leaves the list to the delivery stream",+ );} finally {await vite.close();dom.window.close();@@ -199,6 +199,41 @@ test("a failed list load does not lock out the next conversation open", async ()}});+test("after a background refresh fails, the next open refetches the loaded list", async () => {+ const dom = jsdomGlobals();+ const sessions = [{ id: "a", threadRef: "web:alex:a", scopeId: "personal:alex", title: "Before" }];+ let failing = false;+ let hits = 0;+ globalThis.fetch = async (input) => {+ const path = String(input);+ if (path === "/api/contexts") return Response.json({ contexts: [] });+ hits++;+ if (failing) throw new Error("network down");+ return Response.json({ sessions });+ };++ const vite = await createServer({ server: { middlewareMode: true, hmr: false, ws: false }, appType: "custom" });+ try {+ const { refreshSessions, refreshSessionsOnOpen, sessionsState } = await vite.ssrLoadModule("/src/sessions.ts");+ assert.equal(await refreshSessions({ showLoading: true }), true);+ failing = true;+ assert.equal(await refreshSessions({ silent: true }), false);+ failing = false;+ sessions[0]!.title = "After";+ refreshSessionsOnOpen();+ await new Promise((resolve) => setTimeout(resolve, 0));+ await new Promise((resolve) => setTimeout(resolve, 0));+ assert.equal(hits, 3, "an open repairs a list whose last refresh failed");+ assert.equal(sessionsState.list[0]?.title, "After");+ refreshSessionsOnOpen();+ await new Promise((resolve) => setTimeout(resolve, 0));+ assert.equal(hits, 3, "and leaves it alone once it is current again");+ } finally {+ await vite.close();+ dom.window.close();+ }+});+test("an open that joined a refresh whose answer was discarded asks again itself", async () => {const dom = jsdomGlobals();const pending: Array<(r: Response) => void> = [];diff --git a/plugins/web-ui/test/subagent-activity.test.ts b/plugins/web-ui/test/subagent-activity.test.tsindex 5e8ed74c9..1f1dc02c4 100644--- a/plugins/web-ui/test/subagent-activity.test.ts+++ b/plugins/web-ui/test/subagent-activity.test.ts@@ -50,6 +50,18 @@ test("descendants roll grandchildren up to the top-level parent", () => {);});+test("descendants keep list order across parents and follow a replaced list", () => {+ const interleaved = [row("q1", "q"), row("p1", "p"), row("p", "root"), row("q", "root")];+ assert.deepEqual(+ descendantsOf(interleaved, "root").map(({ session }) => session.id),+ ["p", "q", "q1", "p1"],+ );+ const before = [row("root"), row("a", "root")];+ assert.deepEqual(subagentCounts(before, "root"), { running: 0, waiting: 0 });+ const after = [...before, row("b", "root", { working: true })];+ assert.deepEqual(subagentCounts(after, "root"), { running: 1, waiting: 0 }, "a new list array sees its new children");+});+test("running and waiting counts feed the sidebar and tab badge", () => {assert.deepEqual(subagentCounts(list, "root"), { running: 2, waiting: 1 });assert.deepEqual(subagentCounts(list, "other"), { running: 0, waiting: 0 });