Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(transcript): keep a turn started during Stop's settled read in charge of the transcript

liuzixiangdafc177172bc
diff --git a/apps/desktop/src/stores/slices/transcript-slice.ts b/apps/desktop/src/stores/slices/transcript-slice.ts
index 818d7cfc5..a4220130b 100644
--- a/apps/desktop/src/stores/slices/transcript-slice.ts
+++ b/apps/desktop/src/stores/slices/transcript-slice.ts
@@ -495,6 +495,19 @@ export function createTranscriptSlice({
}));
// Bypass in-flight pre-abort detail reads; request settled source state.
const detail = await api.getSession(sessionId);
+ // A turn that started while this settled-state read was in flight owns
+ // the transcript now: the composer unlocked at the isRunning clear
+ // above, so the user can already be streaming a new prompt. Its
+ // optimistic rows are newer than any snapshot this read can return,
+ // and forcing isRunning false would mute the new turn's spinner —
+ // leave the newer state alone; the live flow reconciles from here.
+ const stateAfterRead = get();
+ const turnOwnsTranscript =
+ stateAfterRead.runningSessions[sessionId] === true ||
+ runtime.liveSessionTranscripts.has(sessionId) ||
+ (stateAfterRead.activeSessionId === sessionId &&
+ stateAfterRead.isRunning === true);
+ if (turnOwnsTranscript) return;
const messages = detail.session?.messages;
if (messages) runtime.cacheSessionTranscript(sessionId, messages);
set((current) => ({
diff --git a/apps/desktop/test/native-stop-refresh-failure.test.mjs b/apps/desktop/test/native-stop-refresh-failure.test.mjs
index 04ff0fcda..1d7360bc2 100644
--- a/apps/desktop/test/native-stop-refresh-failure.test.mjs
+++ b/apps/desktop/test/native-stop-refresh-failure.test.mjs
@@ -21,6 +21,63 @@ for (const source of ["pi-native", "remote"]) test(`confirmed ${source} Stop rem
assert.equal(state.messages, messages, "failed refresh preserves the known transcript");
});
+for (const source of ["pi-native", "remote"]) test(`a turn started during the ${source} Stop's settled read keeps its optimistic state`, async t => {
+ const id = source === "remote" ? "remote:fixture" : "native-pi:fixture";
+ const stale = [{ id: "answer", role: "assistant", content: "settled snapshot" }];
+ let state = {
+ activeSessionId: id,
+ sessions: [{ id, source, capabilities: { canStop: true } }],
+ messages: stale,
+ isRunning: true,
+ runningSessions: { [id]: true },
+ retainedTranscripts: {},
+ };
+ const access = { get: () => state, set: patch => { state = { ...state, ...(typeof patch === "function" ? patch(state) : patch) }; } };
+ const runtime = createSessionRuntime(access);
+ const slice = createTranscriptSlice({ ...access, runtime });
+ let resolveGetSession;
+ t.mock.method(api, "abort", async () => ({ ok: true }));
+ t.mock.method(api, "getSession", () => new Promise((resolve) => { resolveGetSession = resolve; }));
+ const stopping = slice.abort();
+ await new Promise((resolve) => setImmediate(resolve));
+ // The isRunning clear unlocked the composer; a new prompt started and its
+ // optimistic row is the live transcript now.
+ const optimistic = [{ id: "user-2", role: "user", content: "next prompt" }];
+ state = { ...state, messages: optimistic, isRunning: true, runningSessions: { [id]: true } };
+ resolveGetSession({ session: { id, messages: stale } });
+ await stopping;
+ assert.equal(state.messages, optimistic, "the settled snapshot must not replace the live turn");
+ assert.equal(state.isRunning, true, "the new turn's spinner stays on");
+ assert.equal(state.runningSessions[id], true);
+ assert.equal(runtime.sessionTranscriptCache.get(id), undefined,
+ "the stale snapshot must not poison the transcript cache");
+ assert.equal(state.retainedTranscripts[id], undefined,
+ "the stale snapshot must not become the retained transcript");
+});
+
+for (const source of ["pi-native", "remote"]) test(`the settled read still lands when no turn restarted (${source})`, async t => {
+ const id = source === "remote" ? "remote:fixture" : "native-pi:fixture";
+ const settled = [{ id: "answer", role: "assistant", content: "stopped answer" }];
+ let state = {
+ activeSessionId: id,
+ sessions: [{ id, source, capabilities: { canStop: true } }],
+ messages: [{ id: "answer", role: "assistant", content: "partial", status: "streaming" }],
+ isRunning: true,
+ runningSessions: { [id]: true },
+ retainedTranscripts: {},
+ };
+ const access = { get: () => state, set: patch => { state = { ...state, ...(typeof patch === "function" ? patch(state) : patch) }; } };
+ const runtime = createSessionRuntime(access);
+ const slice = createTranscriptSlice({ ...access, runtime });
+ t.mock.method(api, "abort", async () => ({ ok: true }));
+ t.mock.method(api, "getSession", async () => ({ session: { id, messages: settled } }));
+ await slice.abort();
+ assert.deepEqual(state.messages, settled);
+ assert.equal(state.isRunning, false);
+ assert.equal(state.runningSessions[id], false);
+ assert.deepEqual(runtime.sessionTranscriptCache.get(id), settled);
+});
+
test("a rejected native Stop keeps running and does not attempt a detail read", async t => {
let state = { activeSessionId: "native-pi:fixture", sessions: [{ id: "native-pi:fixture", source: "pi-native" }], isRunning: true, runningSessions: { "native-pi:fixture": true } };
const slice = createTranscriptSlice({ get: () => state, set: patch => { state = { ...state, ...(typeof patch === "function" ? patch(state) : patch) }; }, runtime: { submittedComposerDrafts: new Map() } });