Skip to content

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

address review: stop live-turn selection at a non-steering boundary

yu.xiae3a22e7120ec
diff --git a/apps/desktop/src/lib/turn-process.ts b/apps/desktop/src/lib/turn-process.ts
index ba2f31049..db75e40e2 100644
--- a/apps/desktop/src/lib/turn-process.ts
+++ b/apps/desktop/src/lib/turn-process.ts
@@ -265,23 +265,32 @@ export function turnHasStreamingRows(
* The turn a running transcript is driving, when that turn is not the tail
* entry. Tail position alone cannot identify it: a steering message or a
* system row may render after the live turn (#1949), while the history list
- * renders every entry as inactive. The last assistant-turn with streaming
- * rows is the running one; an earlier turn with stuck rows is a crash or a
- * lost host, which `turnProcessPhase` keeps reading as unsettled (#1923).
- * A tail assistant-turn reports its own active state, so it yields no
- * history id, and a turn whose rows all settled yields none either: without
- * a streaming row the turn may genuinely have finished.
+ * renders every entry as inactive. Walk back like
+ * `collectLiveDelegationEntries`: steering user messages stay inside the run,
+ * an ordinary user message or system row ends it. The last assistant-turn of
+ * the current run is live only while it still has streaming rows; an earlier
+ * turn left streaming by a crash is outside this run's boundary and keeps
+ * `turnProcessPhase`'s unsettled reading (#1923). A tail assistant-turn
+ * reports its own active state, so it yields no history id.
*/
export function liveHistoryTurnId(
entries: readonly TranscriptEntry[],
tailEntry?: TranscriptEntry | null,
): string | undefined {
if (tailEntry?.kind === "assistant-turn") return undefined;
- let live: AssistantTurnEntry | undefined;
- for (const entry of entries) {
- if (entry.kind === "assistant-turn") live = entry;
+ for (let index = entries.length; index >= 0; index -= 1) {
+ const entry = index === entries.length ? (tailEntry ?? undefined) : entries[index];
+ if (!entry) continue;
+ if (entry.kind === "assistant-turn") {
+ return turnHasStreamingRows(entry.parts) ? entry.id : undefined;
+ }
+ if (entry.kind === "message") {
+ const { message } = entry;
+ if (message.role === "user" && message.steering) continue;
+ if (message.role === "user" || message.role === "system") break;
+ }
}
- return live && turnHasStreamingRows(live.parts) ? live.id : undefined;
+ return undefined;
}
/**
diff --git a/apps/desktop/test/transcript-live-turn.test.mjs b/apps/desktop/test/transcript-live-turn.test.mjs
index 38ca4b263..913c0c971 100644
--- a/apps/desktop/test/transcript-live-turn.test.mjs
+++ b/apps/desktop/test/transcript-live-turn.test.mjs
@@ -1,5 +1,12 @@
import assert from "node:assert/strict";
import test from "node:test";
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { createInstance } from "i18next";
+import { I18nextProvider } from "react-i18next";
+import { catalogs } from "@pi-desktop/i18n";
+import { createServer } from "vite";
+import { fileURLToPath } from "node:url";
import { readTranscriptModule } from "./helpers/source-contracts.mjs";
const [transcript, assistantTurn] = await Promise.all([
@@ -27,3 +34,59 @@ test("the history list keeps a non-tail live turn active", () => {
// keep a turn that just went live frozen in its inactive rendering.
assert.match(assistantTurn, /previous\.liveTurnId !== next\.liveTurnId/);
});
+
+test("a non-tail live turn renders running and open until its rows settle", async () => {
+ const vite = await createServer({
+ root: fileURLToPath(new URL("..", import.meta.url)), configFile: false,
+ server: { middlewareMode: true, hmr: false, ws: false },
+ esbuild: { jsx: "automatic" }, appType: "custom",
+ optimizeDeps: { noDiscovery: true, include: [] },
+ });
+ try {
+ const { TranscriptHistory } = await vite.ssrLoadModule("/src/features/chat/transcript/AssistantTurn.tsx");
+ const { buildTranscriptEntries } = await vite.ssrLoadModule("/src/lib/assistant-turns.ts");
+ const { liveHistoryTurnId } = await vite.ssrLoadModule("/src/lib/turn-process.ts");
+ const { SlotSessionProvider } = await vite.ssrLoadModule("/src/plugins/renderer-slots/use-slots.tsx");
+ const i18n = createInstance();
+ await i18n.init({ lng: "en", resources: { en: { translation: catalogs.en } } });
+ const message = (id, role, content, extra = {}) => ({
+ id, role, content, createdAt: "2026-10-29T00:00:00.000Z", ...extra,
+ });
+ const { entries } = buildTranscriptEntries([
+ message("user", "user", "Inspect"),
+ message("intro", "assistant", "Checking", { status: "complete" }),
+ message("read", "tool", "", { toolName: "Read", status: "streaming" }),
+ message("steer", "user", "also check the config", { steering: true }),
+ ]);
+ const history = entries.slice(0, -1);
+ const tail = entries.at(-1);
+ const liveId = liveHistoryTurnId(history, tail);
+ assert.equal(tail.kind, "message");
+ assert.notEqual(liveId, undefined);
+ const render = (liveTurnId) => renderToStaticMarkup(createElement(I18nextProvider, { i18n },
+ createElement(SlotSessionProvider, { sessionId: "fixture" },
+ createElement(TranscriptHistory, { entries: history, isRunning: true, liveTurnId }))));
+ const live = render(liveId);
+ assert.match(live, /Running/);
+ assert.match(live, /class="turn-process open active"/);
+ assert.match(live, /aria-expanded="true"/);
+ assert.doesNotMatch(live, /Outcome unknown/);
+ // Without the id the same rows keep #1965's unsettled reading and the
+ // untouched process closes, which is the collapse half of #1949.
+ const idle = render(undefined);
+ assert.match(idle, /Outcome unknown/);
+ assert.match(idle, /class="turn-process"/);
+ assert.doesNotMatch(idle, /turn-process open/);
+ assert.match(idle, /aria-expanded="false"/);
+ // Memo invalidation: identical entries with a changed liveTurnId must
+ // re-render, and a fully equal props object must bail.
+ const base = { entries: history, isRunning: true, liveDelegations: undefined, liveTurnId: liveId };
+ assert.equal(TranscriptHistory.compare(base, base), true);
+ assert.equal(
+ TranscriptHistory.compare(base, { ...base, liveTurnId: undefined }),
+ false,
+ );
+ } finally {
+ await vite.close();
+ }
+});
diff --git a/apps/desktop/test/turn-process.test.mjs b/apps/desktop/test/turn-process.test.mjs
index 37aaf5e21..1c1e70c7e 100644
--- a/apps/desktop/test/turn-process.test.mjs
+++ b/apps/desktop/test/turn-process.test.mjs
@@ -648,14 +648,14 @@ test("settings writes validate the thinking preference without changing other pr
);
});
-test("the last assistant-turn with streaming rows is the live turn when it is not the tail", () => {
+test("the last assistant-turn of the current run is the live turn when it is not the tail", () => {
// A steering message renders after the live turn, so the tail entry is not
// the turn; the history list must not read the running turn as inactive (#1949).
const steered = buildTranscriptEntries([
message("user", "user", "Inspect"),
message("intro", "assistant", "Checking", { status: "complete" }),
tool("read", "Read", { status: "streaming" }),
- message("steer", "user", "also check the config"),
+ message("steer", "user", "also check the config", { steering: true }),
]);
const steeredTail = steered.entries.at(-1);
const steeredHistory = steered.entries.slice(0, -1);
@@ -685,21 +685,38 @@ test("the last assistant-turn with streaming rows is the live turn when it is no
const finished = buildTranscriptEntries([
message("u3", "user", "go"),
message("a3", "assistant", "done", { status: "complete" }),
- message("steer3", "user", "one more"),
+ message("steer3", "user", "one more", { steering: true }),
]);
assert.equal(
liveHistoryTurnId(finished.entries.slice(0, -1), finished.entries.at(-1)),
undefined,
);
- // An earlier turn stuck at streaming is not the live one when a later turn
- // exists: it is a crash or lost host, which reads as unsettled (#1923).
- const crashedThenNew = buildTranscriptEntries([
+ // An ordinary user message ends the run even while a previous turn's rows
+ // are stuck streaming: a fresh prompt after a crash must not reopen the
+ // abandoned turn as running while the new request has produced no rows yet.
+ const crashedThenFresh = buildTranscriptEntries([
message("u4", "user", "go"),
- message("a4", "assistant", "", { status: "streaming" }),
- message("u5", "user", "retry"),
- message("a5", "assistant", "recovered", { status: "complete" }),
- message("steer5", "user", "and now"),
+ message("a4", "assistant", "partial answer", { status: "streaming" }),
+ message("u5", "user", "start over"),
+ ]);
+ const freshHistory = crashedThenFresh.entries.slice(0, -1);
+ const freshTurn = freshHistory.find((entry) => entry.kind === "assistant-turn");
+ assert.equal(freshTurn.kind, "assistant-turn");
+ assert.equal(turnHasStreamingRows(freshTurn.parts), true);
+ assert.equal(
+ liveHistoryTurnId(freshHistory, crashedThenFresh.entries.at(-1)),
+ undefined,
+ );
+
+ // A turn left streaming behind a later ordinary prompt keeps the unsettled
+ // reading: the later run's boundary excludes it (#1923).
+ const crashedThenNew = buildTranscriptEntries([
+ message("u6", "user", "go"),
+ message("a6", "assistant", "partial answer", { status: "streaming" }),
+ message("u7", "user", "start over"),
+ message("a7", "assistant", "recovered", { status: "complete" }),
+ message("steer7", "user", "and now", { steering: true }),
]);
assert.equal(
liveHistoryTurnId(crashedThenNew.entries.slice(0, -1), crashedThenNew.entries.at(-1)),
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index 1eede4ffd..d4821f1ed 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -911,8 +911,9 @@ without expanding the process. An untouched process closes on completion, leavin
the final answer; with no final text, the process header and those surfaces
remain. Liveness follows the turn's rows, not the transcript's tail position:
while the session runs, a turn whose rows are still streaming stays open and
-reads as running even when a steering message or system row renders after it,
-and an idle turn whose rows never settled reads as an unknown outcome instead.
+reads as running even when a steering message renders after it. An ordinary
+user message or a system row ends the run's boundary, so a turn left streaming
+by an earlier crash keeps reading as an unknown outcome.
Within the process, the `Transcript display` mode decides the grouping.
`fully-folded` (the default; absent or unknown values use it) forms ordinary
diff --git a/docs/zh-CN/spec/04-ux/08-component-spec.md b/docs/zh-CN/spec/04-ux/08-component-spec.md
index 8175407a9..5a5f221f5 100644
--- a/docs/zh-CN/spec/04-ux/08-component-spec.md
+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md
@@ -1078,8 +1078,9 @@ SESSIONS [msg+][↕]
同级,长回合可以用一次操作收起。末尾回答、助手错误、中止后的末尾部分文字和待处理操作
卡留在披露之外,无需展开过程即可访问。未操作的过程在完成时收起,只剩最终回答;没有最终
文字时,过程标题和这些操作面仍会保留。回合的活跃性跟随其行状态而非转录的尾部位置:会话
-运行期间,行仍在流式输出的回合即使后面渲染了 steering 消息或系统行,也保持展开并显示为
-执行中;行始终未结算的空闲回合则显示为结果未知。
+运行期间,行仍在流式输出的回合即使后面渲染了 steering 消息,也保持展开并显示为执行中;
+普通用户消息或系统行会结束当前运行的边界,早前崩溃遗留流式行的回合因此保持结果未知的
+读法。
在披露内部,`过程显示`模式决定活动如何分组。`fully-folded`(默认;缺失或未知值使用
它)把普通助手进度段落之间的连续工具、搜索或思考片段组为普通活动组,`Bash`/run 别名