Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(chat): fork visible historical replies outside the live window
koeriade815b3fbf4c
diff --git a/apps/desktop/src/stores/slices/session-slice.ts b/apps/desktop/src/stores/slices/session-slice.tsindex 7da6d0838..65aa23a93 100644--- a/apps/desktop/src/stores/slices/session-slice.ts+++ b/apps/desktop/src/stores/slices/session-slice.ts@@ -47,6 +47,7 @@ import {mergeLiveSessionMessages,} from "../../lib/session-transcript";import { sessionReadLooksEmpty } from "../../lib/session-transcript-read";+import { transcriptViewMessages } from "../../lib/transcript-reading";import type {AppState,DraftSessionConfiguration,@@ -146,6 +147,21 @@ function replacePendingQueue<T>(return { ...queues, [sessionId]: queue };}+/** Resolve a visible reply fork anchor without hydrating the live source transcript. */+function findVisibleAssistantForkAnchor(+ state: Pick<AppState, "messages" | "transcriptViews">,+ sessionId: string,+ messageId: string,+): UiMessage | undefined {+ const fromLive = state.messages.find((candidate) => candidate.id === messageId);+ if (fromLive) return fromLive;+ // Historical pages are already on screen via transcriptViews. Do not copy+ // them onto state.messages: that would overwrite a concurrently streaming tail.+ return transcriptViewMessages(state.messages, state.transcriptViews[sessionId]).find(+ (candidate) => candidate.id === messageId,+ );+}+export function createSessionSlice({get,set,@@ -814,7 +830,7 @@ export function createSessionSlice({const state = get();const sessionId = state.activeSessionId;if (!sessionId || state.selectingSessionId) return;- const message = state.messages.find((candidate) => candidate.id === messageId);+ const message = findVisibleAssistantForkAnchor(state, sessionId, messageId);const source = state.sessions.find((session) => session.id === sessionId);if (!message || message.role !== "assistant" || !source) return;if (!sessionAllows(source, "canEditMessages")) return;diff --git a/apps/desktop/test/session-fork-running.test.mjs b/apps/desktop/test/session-fork-running.test.mjsindex f7bee0137..2d6aed3af 100644--- a/apps/desktop/test/session-fork-running.test.mjs+++ b/apps/desktop/test/session-fork-running.test.mjs@@ -7,15 +7,17 @@ const { createSessionSlice } = await import("../src/stores/slices/session-slice.const { createSessionCoordination } = await import("../src/stores/runtime/session-coordination.ts");const { api } = await import("../src/lib/api.ts");-function harness(t) {+function harness(t, options = {}) {const old = { id: "old", role: "assistant", content: "first reply", status: "complete" };const live = { id: "live", role: "assistant", content: "second reply", status: "streaming" };+ const messages = options.messages ?? [old, live];let state = {activeSessionId: "parent", isRunning: true, runningSessions: { parent: true },- messages: [old, live], sessions: [{ id: "parent", title: "Source", mode: "agent" }],+ messages, sessions: [{ id: "parent", title: "Source", mode: "agent" }],sessionHistory: { parent: { hasMoreBefore: true, contentLimited: true } },sessionMeta: {}, sessionCompactions: {}, planningStates: {},- retainedSessionIds: ["parent"], retainedTranscripts: { parent: [old, live] },+ retainedSessionIds: ["parent"], retainedTranscripts: options.retainedTranscripts ?? { parent: messages },+ transcriptViews: options.transcriptViews ?? {},workPanelContexts: {}, workPanelTabs: [], workPanelOpen: false,navStack: [{ page: "chat", sessionId: "parent" }], navIndex: 0,restorePendingPlan: async () => {},@@ -40,17 +42,34 @@ function harness(t) {const slice = createSessionSlice({ ...access, ...coordination });let resolve, reject;const pending = new Promise((yes, no) => { resolve = yes; reject = no; });+ const forks = [];t.mock.method(api, "forkSession", (id, _title, anchor) => {- assert.equal(id, "parent"); assert.equal(anchor, "old");+ assert.equal(id, "parent");+ assert.equal(anchor, options.expectedAnchor ?? "old");+ forks.push({ id, anchor });return pending;});return {- slice, runtime, cache, get: access.get, set: access.set,+ slice, runtime, cache, forks, get: access.get, set: access.set,complete: () => resolve({ session: { id: "child", title: "Branch", mode: "agent", messages: [{ ...old, id: "child-old" }] } }),reject,};}+function historicalPageView(messages) {+ return {+ parent: {+ messages,+ messageStart: 0,+ hasMoreBefore: false,+ hasMoreAfter: true,+ focus: null,+ restored: true,+ loading: null,+ },+ };+}+test("reply fork activates an independent child without hydrating or stopping its streaming parent", async (t) => {const h = harness(t);const parent = h.get().messages;@@ -112,3 +131,91 @@ test("a rejected fork reports the error and keeps the original turn and transcriassert.equal(h.get().messages, parent);assert.equal(h.get().isRunning, true);});++test("a paginated historical assistant reply forks without hydrating the live window", async (t) => {+ const historical = { id: "hist", role: "assistant", content: "page 1 reply", status: "complete" };+ const latest = Array.from({ length: 100 }, (_, i) => ({+ id: `live-${i}`,+ role: i % 2 === 0 ? "user" : "assistant",+ content: "latest window",+ status: "complete",+ }));+ const streaming = { id: "tail", role: "assistant", content: "still streaming", status: "streaming" };+ const liveWindow = [...latest, streaming];+ const h = harness(t, {+ expectedAnchor: "hist",+ messages: liveWindow,+ transcriptViews: historicalPageView([historical]),+ });+ const liveBefore = h.get().messages;+ const action = h.slice.forkAssistantMessage("hist");+ assert.equal(h.forks.length, 1);+ assert.equal(h.forks[0].anchor, "hist");+ assert.equal(h.get().messages, liveBefore);+ assert.deepEqual(h.get().messages.map((message) => message.id), liveWindow.map((message) => message.id));+ assert.equal(h.get().messages.at(-1).status, "streaming");+ assert.equal(h.get().error, undefined);+ h.complete();+ await action;+ assert.equal(h.get().activeSessionId, "child");+ assert.equal(h.get().runningSessions.parent, true);+ assert.equal(h.get().retainedTranscripts.parent.at(-1).status, "streaming");+ assert.deepEqual(h.get().retainedTranscripts.parent.map((message) => message.id), liveWindow.map((message) => message.id));+});++test("a paginated historical reply still forks when the view is merged with the live tail", async (t) => {+ const historical = { id: "hist", role: "assistant", content: "scrolled reply", status: "complete" };+ const liveWindow = [+ { id: "live-user", role: "user", content: "latest", status: "complete" },+ { id: "tail", role: "assistant", content: "still streaming", status: "streaming" },+ ];+ const h = harness(t, {+ expectedAnchor: "hist",+ messages: liveWindow,+ transcriptViews: {+ parent: {+ messages: [historical],+ messageStart: 0,+ hasMoreBefore: false,+ hasMoreAfter: true,+ focus: null,+ loading: null,+ },+ },+ });+ const liveBefore = h.get().messages;+ const action = h.slice.forkAssistantMessage("hist");+ assert.equal(h.forks.length, 1);+ assert.equal(h.get().messages, liveBefore);+ assert.equal(h.get().messages.at(-1).status, "streaming");+ h.complete();+ await action;+ assert.equal(h.get().activeSessionId, "child");+});++test("a current-page assistant reply still forks when a historical view is present", async (t) => {+ const historical = { id: "hist", role: "assistant", content: "page 1 reply", status: "complete" };+ const h = harness(t, {+ expectedAnchor: "old",+ transcriptViews: historicalPageView([historical]),+ });+ const action = h.slice.forkAssistantMessage("old");+ assert.equal(h.forks.length, 1);+ h.complete();+ await action;+ assert.equal(h.get().activeSessionId, "child");+});++test("a historical user message is not used as a reply fork anchor", async (t) => {+ const historical = { id: "hist-user", role: "user", content: "old prompt", status: "complete" };+ const h = harness(t, {+ expectedAnchor: "hist-user",+ messages: [{ id: "tail", role: "assistant", content: "still streaming", status: "streaming" }],+ transcriptViews: historicalPageView([historical]),+ });+ await h.slice.forkAssistantMessage("hist-user");+ assert.equal(h.forks.length, 0);+ assert.equal(h.get().activeSessionId, "parent");+ assert.equal(h.get().messages[0].status, "streaming");+ assert.equal(h.get().error, undefined);+});