Skip to content

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

fix(chat): preserve file ref line in work panel requests

vastsaeb4a0aaa3c23
diff --git a/apps/desktop/src/hooks/use-preview-target.ts b/apps/desktop/src/hooks/use-preview-target.ts
index 47285f514..633a2e62f 100644
--- a/apps/desktop/src/hooks/use-preview-target.ts
+++ b/apps/desktop/src/hooks/use-preview-target.ts
@@ -139,7 +139,10 @@ function useResolveChatFileRef() {
* (ADR 0163): it is a page to run, not a file to read. A plain project file
* opens in the bundled file view when available; a positioned `path:line`
* reference uses the host file tab, which can scroll to the requested line.
- * Scratch and attachment files also use the host file tab.
+ * The plugin view accepts opaque path locations and has no line-navigation
+ * contract, so positioned references keep their path unchanged and use the
+ * host viewer's existing scroll support. Scratch and attachment files also use
+ * the host file tab.
*/
export function useOpenChatFileRef() {
const resolveRef = useResolveChatFileRef();
diff --git a/apps/desktop/src/lib/chat-links.ts b/apps/desktop/src/lib/chat-links.ts
index 5a81f485d..02a39b9df 100644
--- a/apps/desktop/src/lib/chat-links.ts
+++ b/apps/desktop/src/lib/chat-links.ts
@@ -56,7 +56,8 @@ function stripLineRef(path: string): string {
export function parseFileRefPosition(
text: string,
): { line: number; column?: number } | null {
- const match = text.trim().match(/:(\d+)(?::(\d+))?$/);
+ const token = text.trim().replace(/[.,!?;:,。!?;:]+$/u, "");
+ const match = token.match(/:(\d+)(?::(\d+))?$/);
if (!match) return null;
const line = Number(match[1]);
if (!Number.isFinite(line) || line < 1) return null;
@@ -214,7 +215,10 @@ export function resolvePreviewTarget(
const trimmed = text.trim();
if (isHttpUrl(trimmed)) return { kind: "url", url: trimmed };
const position = parseFileRefPosition(trimmed);
- const at = unwrapAtFileRef(trimmed);
+ const pathText = position
+ ? trimmed.replace(/[.,!?;:,。!?;:]+$/u, "")
+ : trimmed;
+ const at = unwrapAtFileRef(pathText);
if (at) {
const cleaned = stripLineRef(at);
if (isAbsoluteFilePath(cleaned)) {
@@ -223,7 +227,7 @@ export function resolvePreviewTarget(
const rel = toWorkspaceRel(cleaned, root, baseDir);
return rel ? { kind: "file", path: rel, ...(position ?? {}) } : null;
}
- const file = parseFileRef(trimmed);
+ const file = parseFileRef(pathText);
if (!file) return null;
if (isAbsoluteFilePath(file)) return { kind: "file", path: file, ...(position ?? {}) };
const rel = toWorkspaceRel(file, root, baseDir);
diff --git a/apps/desktop/src/lib/work-panel-tabs.ts b/apps/desktop/src/lib/work-panel-tabs.ts
index 37b6200f0..dfa6b65b1 100644
--- a/apps/desktop/src/lib/work-panel-tabs.ts
+++ b/apps/desktop/src/lib/work-panel-tabs.ts
@@ -36,6 +36,21 @@ export type WorkPanelContext = WorkPanelTabsState & {
} | null;
};
+/** Build the file-view request represented by an active file tab. */
+export function createWorkPanelFileRequest(
+ tab: WorkPanelTab,
+ seq: number,
+): WorkPanelContext["fileRequest"] {
+ if (tab.kind !== "file" || !tab.resource) return null;
+ return {
+ path: tab.resource,
+ seq,
+ ...(tab.mimeType ? { mimeType: tab.mimeType } : {}),
+ ...(tab.line != null ? { line: tab.line } : {}),
+ ...(tab.column != null ? { column: tab.column } : {}),
+ };
+}
+
let newWorkPanelTabSequence = 0;
export function emptyWorkPanelContext(): WorkPanelContext {
diff --git a/apps/desktop/src/stores/slices/work-panel-slice.ts b/apps/desktop/src/stores/slices/work-panel-slice.ts
index cb19d4ffa..2020c880c 100644
--- a/apps/desktop/src/stores/slices/work-panel-slice.ts
+++ b/apps/desktop/src/stores/slices/work-panel-slice.ts
@@ -4,6 +4,7 @@ import {
browserPluginTab,
browserTabLabel,
closeWorkPanelTabState,
+ createWorkPanelFileRequest,
emptyWorkPanelContext,
fileWorkPanelTab,
newWorkPanelTab,
@@ -14,6 +15,7 @@ import {
subagentWorkPanelTab,
switchWorkPanelContextState,
type WorkPanelContext,
+ type WorkPanelTab,
} from "../../lib/work-panel-tabs";
import {
WORK_PANEL_COMPACT_MIN_WIDTH,
@@ -116,6 +118,10 @@ export function createWorkPanelSlice({
| "updateBrowserWorkPanelTab"
> {
let workPanelFileRequestSeq = 0;
+ const nextFileRequest = (tab?: WorkPanelTab): WorkPanelContext["fileRequest"] => {
+ if (!tab || tab.kind !== "file" || !tab.resource) return null;
+ return createWorkPanelFileRequest(tab, ++workPanelFileRequestSeq);
+ };
return {
openSubagentTab: (delegationId, agentName) => {
@@ -163,14 +169,7 @@ export function createWorkPanelSlice({
},
tab,
);
- const fileRequest =
- tab.kind === "file" && tab.resource
- ? {
- path: tab.resource,
- seq: ++workPanelFileRequestSeq,
- ...(tab.mimeType ? { mimeType: tab.mimeType } : {}),
- }
- : context.fileRequest;
+ const fileRequest = nextFileRequest(tab) ?? context.fileRequest;
const nextContext: WorkPanelContext = {
open: true,
tabs: next.tabs,
@@ -216,14 +215,7 @@ export function createWorkPanelSlice({
tab,
);
const activeTab = next.tabs.find((item) => item.id === next.activeTabId);
- const fileRequest =
- activeTab?.kind === "file" && activeTab.resource
- ? {
- path: activeTab.resource,
- seq: ++workPanelFileRequestSeq,
- ...(activeTab.mimeType ? { mimeType: activeTab.mimeType } : {}),
- }
- : state.workPanelFileRequest;
+ const fileRequest = nextFileRequest(activeTab) ?? state.workPanelFileRequest;
const nextContext: WorkPanelContext = {
open: true,
tabs: next.tabs,
@@ -254,14 +246,7 @@ export function createWorkPanelSlice({
tabId,
);
const activeTab = next.tabs.find((tab) => tab.id === next.activeTabId);
- const fileRequest =
- activeTab?.kind === "file" && activeTab.resource
- ? {
- path: activeTab.resource,
- seq: ++workPanelFileRequestSeq,
- ...(activeTab.mimeType ? { mimeType: activeTab.mimeType } : {}),
- }
- : state.workPanelFileRequest;
+ const fileRequest = nextFileRequest(activeTab) ?? state.workPanelFileRequest;
const nextContext: WorkPanelContext = {
open: state.workPanelOpen,
tabs: next.tabs,
@@ -325,14 +310,7 @@ export function createWorkPanelSlice({
tabId,
);
const activeTab = next.tabs.find((tab) => tab.id === next.activeTabId);
- const fileRequest =
- activeTab?.kind === "file" && activeTab.resource
- ? {
- path: activeTab.resource,
- seq: ++workPanelFileRequestSeq,
- ...(activeTab.mimeType ? { mimeType: activeTab.mimeType } : {}),
- }
- : state.workPanelFileRequest;
+ const fileRequest = nextFileRequest(activeTab) ?? state.workPanelFileRequest;
const nextContext: WorkPanelContext = {
// Closing the final tab leaves the panel open so the user can choose
// another tool from the new-tab launcher instead of losing the dock.
diff --git a/apps/desktop/test/chat-links.test.mjs b/apps/desktop/test/chat-links.test.mjs
index 1db6959bd..ad23dd4df 100644
--- a/apps/desktop/test/chat-links.test.mjs
+++ b/apps/desktop/test/chat-links.test.mjs
@@ -735,6 +735,8 @@ test("adjacent parenthesis-wrapped URLs all remain independently linkable", () =
test("parseFileRefPosition keeps :line[:col] that parseFileRef strips", () => {
assert.deepEqual(parseFileRefPosition("src/main.rs:42"), { line: 42 });
assert.deepEqual(parseFileRefPosition("src/main.rs:42:7"), { line: 42, column: 7 });
+ assert.deepEqual(parseFileRefPosition("src/main.rs:42."), { line: 42 });
+ assert.deepEqual(parseFileRefPosition("src/main.rs:42:7,"), { line: 42, column: 7 });
assert.equal(parseFileRefPosition("src/main.rs"), null);
assert.equal(parseFileRefPosition("src/main.rs:0"), null);
});
@@ -751,6 +753,12 @@ test("resolvePreviewTarget carries line/col on file chips (#681)", () => {
line: 42,
column: 7,
});
+ assert.deepEqual(resolvePreviewTarget("src/a.ts:42:7.", ROOT), {
+ kind: "file",
+ path: "src/a.ts",
+ line: 42,
+ column: 7,
+ });
assert.deepEqual(resolvePreviewTarget(`${ROOT}/src/a.ts:42:7`, ROOT), {
kind: "file",
path: `${ROOT}/src/a.ts`,
diff --git a/apps/desktop/test/work-panel-tabs.test.mjs b/apps/desktop/test/work-panel-tabs.test.mjs
index 98e1015e9..96fe3a9f9 100644
--- a/apps/desktop/test/work-panel-tabs.test.mjs
+++ b/apps/desktop/test/work-panel-tabs.test.mjs
@@ -6,6 +6,7 @@ const {
activateWorkPanelTabState,
browserPluginTab,
closeWorkPanelTabState,
+ createWorkPanelFileRequest,
emptyWorkPanelContext,
fileWorkPanelTab,
hasPluginView,
@@ -84,6 +85,22 @@ test("file tabs normalize lexical paths and remain distinct by resource", () =>
assert.equal(normalizeWorkPanelFilePath("/repo/./src/../App.tsx"), "/repo/App.tsx");
});
+test("file-view requests preserve a positioned file tab", () => {
+ const tab = fileWorkPanelTab("src/App.tsx", "text/typescript", {
+ line: 65,
+ column: 4,
+ });
+
+ assert.deepEqual(createWorkPanelFileRequest(tab, 7), {
+ path: "src/App.tsx",
+ seq: 7,
+ mimeType: "text/typescript",
+ line: 65,
+ column: 4,
+ });
+ assert.equal(createWorkPanelFileRequest(toolWorkPanelTab("review"), 8), null);
+});
+
test("closing the active tab selects its right neighbor then its left", () => {
const state = {
tabs: [
diff --git a/apps/desktop/test/work-panel.test.mjs b/apps/desktop/test/work-panel.test.mjs
index 12d599bf8..432a146be 100644
--- a/apps/desktop/test/work-panel.test.mjs
+++ b/apps/desktop/test/work-panel.test.mjs
@@ -526,9 +526,13 @@ test("work panel context is retained by session instead of cleared on selection"
test("file preview request ids stay unique across session contexts", () => {
assert.match(storeSource, /let workPanelFileRequestSeq = 0/);
+ assert.match(
+ storeSource,
+ /const nextFileRequest = \(tab\?: WorkPanelTab\)[\s\S]*?createWorkPanelFileRequest\(tab, \+\+workPanelFileRequestSeq\)/,
+ );
assert.ok(
- storeSource.match(/seq:\s*\+\+workPanelFileRequestSeq/g)?.length >= 3,
- "open and activation paths must use the shared request sequence",
+ storeSource.match(/nextFileRequest\((?:tab|activeTab)\)/g)?.length === 4,
+ "open, replace, activation, and close paths must share the position-preserving request",
);
assert.doesNotMatch(storeSource, /seq:\s*\([^)]*fileRequest\?\.seq[^)]*\) \+ 1/);
});
diff --git a/docs/adr/0262-chat-file-refs-open-in-the-file-view.md b/docs/adr/0262-chat-file-refs-open-in-the-file-view.md
index 98ef6b0f3..840af419a 100644
--- a/docs/adr/0262-chat-file-refs-open-in-the-file-view.md
+++ b/docs/adr/0262-chat-file-refs-open-in-the-file-view.md
@@ -48,13 +48,17 @@ work-panel tab already carries was honoured by main for `pi.browser` only.
- a reference that matches nothing opens **nothing** and reports itself. No
empty panel, no blank browser page.
2. **The destination follows where the reference resolved.** A project file
- opens in the `pi.file-manager` view. A file in the session scratch or
- attachment store opens in the host `file:` tab, because it lives outside that
- view's project root — see point 4. A workspace `.html` / `.htm` file still
- opens in the side browser (ADR 0163), for an agent reply and a user chip
- alike: it is a page to run, not a file to read. When the file view is not
- loaded, a project file falls back to the host `file:` tab, so the click never
- regresses to nothing.
+ opens in the `pi.file-manager` view, except a `path:line` reference, which
+ opens in the host's read-only `file:` tab so the requested source line can
+ be brought into view. The bundled view accepts opaque path locations and has
+ no line-navigation contract; this keeps the path unchanged and uses the
+ host viewer's existing scroll support. A file in the session scratch or
+ attachment store opens in the
+ host `file:` tab, because it lives outside that view's project root — see
+ point 4. A workspace `.html` / `.htm` file still opens in the side browser
+ (ADR 0163), for an agent reply and a user chip alike: it is a page to run,
+ not a file to read. When the file view is not loaded, a project file falls
+ back to the host `file:` tab, so the click never regresses to nothing.
3. **A contributed view's `location` stops being browser-only.** It travels as
the view entry URL's `piViewOpen` query parameter on creation — the only
channel that cannot race a document that has not run yet — and as the
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index e2e59552e..2eeaf8676 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -3418,6 +3418,8 @@ Anatomy:
in the bundled `pi.file-manager` view (the host `file:` tab when that view is
unavailable), a session-scratch or attachment file in the host `file:` tab,
and a `.html`/`.htm` page of the primary folder in the side browser. A
+ positioned `path:line` reference uses the host read-only `file:` tab and
+ scrolls the requested line into view. A
primary-folder file is addressed to the view as a project-relative path and a
sibling-folder file as an absolute one, which is also how scratch and
attachment files are addressed. A reference that matches no file opens nothing
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index b6f1d80b4..81114a86f 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -16364,3 +16364,21 @@ renderer's durable transcript reads. No real model or provider is contacted.
- Installed Electron, real account/paid API and cross-version rollback are
separate release qualification. No MCP, Codemode or virtual-router migration
is included. See `docs/project/pi-0991-adoption.md` for candidate evidence.
+
+#### E2E-262: Transcript path:line opens and scrolls the host file viewer
+
+- **Preconditions:** Isolated Electron/Chromium, an active workspace and session,
+ the bundled file-manager view available, and deterministic filesystem IPC
+ fixtures.
+- **Steps:** Render a real transcript `path:line:column` reference followed by
+ sentence punctuation; click its verified file chip and wait for the host file
+ viewer to load.
+- **Expected:** Despite the bundled file-manager view being available, the
+ positioned reference opens in the host's read-only file tab. The file request
+ retains its line and column, and the viewer scrolls the requested line into
+ the visible center area. The path and file contents remain the same as the
+ reference target.
+- **Specs:** `04-ux/08` §11.8; ADR 0262.
+- **Status:** `node scripts/e2e-file-ref-line-scroll.mjs` mounts production
+ `LinkifiedText` and `FilesTab` in isolated Electron with filesystem IPC
+ fixtures; no real project files or provider are used.
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 cc9b60205..035ed757c 100644
--- a/docs/zh-CN/spec/04-ux/08-component-spec.md
+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md
@@ -2340,6 +2340,8 @@ Live Voice 受现有开发者模式和开发构建门控约束,包含四种展
- 发送的模板调用在记录中呈现为等宽命令
来自消息的 `command` 字段的芯片而不是扩展的正文。
- 已发送的 `@path` 文件引用(带引号或不带引号)画成与草稿相同的叶子名芯片。点击芯片先经 `pi-desktop/fs/resolveRef` 补全引用——搜索整个打开的项目,按项目组文件夹顺序、主文件夹优先(ADR 0263)——再按解析结果打开:项目文件在随应用打包的 `pi.file-manager` 视图中打开(该视图不可用时退回宿主 `file:` 选项卡),会话临时目录或附件文件在宿主 `file:` 选项卡中打开,主文件夹中的 `.html`/`.htm` 在侧边浏览器打开。交给该视图的地址跟随应答的文件夹:主文件夹中的文件用项目内相对路径传递,同一项目的同级文件夹中的文件用绝对路径传递,与会话临时目录和附件文件一致。什么都没匹配到时既不打开任何东西,也会自己报告出来,右键该芯片也一样;引用菜单除了在系统文件管理器中显示该文件,还提供复制完整地址与复制相对地址,发送的 `@path` 芯片、消息 Markdown 中的行内代码、本地链接与本地图片、工具行自己的文件路径、工具结果的文件列表或匹配列表中的路径、图片附件缩略图都提供同样的几项,并同样经这次补全与这套寻址规则;项目外的文件没有相对地址,会直接说明。系统默认应用不再由这次点击触发。HTTP(S) URL 仍是侧边浏览器的文本链接。
+带行号的 `path:line` 引用使用宿主只读 `file:` 选项卡,并将目标行滚动到可见区域。
+
- 状态:键盘活动行使用共享 `kb-active` 处理;空的
查询列出所有内容(斜杠)/最近索引的顺序(文件);零
匹配呈现本地化的空行并且菜单计为关闭
diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index 93c998360..d4e743f0d 100644
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@ -9259,3 +9259,11 @@ the latest destination. These assertions measure work counts, not device FPS.
- **验收:** 缓存路径、迁移和清理单测通过;Windows task-candidate 验证应覆盖更新源传输、安装器交接和文件系统行为,且不连接真实发布源。
- **里程碑:** M6+
- **状态:** 单测和源码契约覆盖(`update-cache.test.mjs`、`auto-update.test.mjs`);仍需 Windows 安装器/E2E 验证。
+
+#### E2E-262:聊天 path:line 引用打开文件并滚动到目标行
+
+- **前提:** 隔离 Electron/Chromium、活动工作区和会话、可用的随应用打包文件管理器视图,以及确定性的文件系统 IPC fixture。
+- **步骤:** 渲染真实聊天中的 `path:line:column` 引用并在末尾加句末标点;点击已验证的文件芯片,等待宿主文件查看器加载。
+- **预期:** 即使文件管理器视图可用,带位置的引用仍打开宿主只读文件选项卡。文件请求保留行列号,查看器滚动到视口中间的目标行;打开路径和文件内容与引用目标一致。
+- **规格:** `04-ux/08` §11.8;ADR 0262。
+- **状态:** `node scripts/e2e-file-ref-line-scroll.mjs` 在隔离 Electron 中挂载生产 `LinkifiedText` 与 `FilesTab`,并通过文件系统 IPC fixture 提供文件内容;不访问真实项目文件或模型服务。
diff --git a/scripts/e2e-file-ref-line-scroll.mjs b/scripts/e2e-file-ref-line-scroll.mjs
new file mode 100644
index 000000000..c0603c7df
--- /dev/null
+++ b/scripts/e2e-file-ref-line-scroll.mjs
@@ -0,0 +1,97 @@
+#!/usr/bin/env node
+/** Follow a real transcript path:line chip into the production host file viewer. */
+import assert from "node:assert/strict";
+import { spawn } from "node:child_process";
+import { createRequire } from "node:module";
+import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { repositoryRoot, resolveElectronBinary } from "./e2e/boot.mjs";
+
+const root = repositoryRoot();
+const require = createRequire(join(root, "packages/agent-runtime/package.json"));
+const { build } = require("esbuild");
+const temp = await mkdtemp(join(tmpdir(), "pi-file-ref-line-scroll-"));
+try {
+ await build({
+ entryPoints: [join(root, "scripts/e2e/file-ref-line-scroll.tsx")],
+ outfile: join(temp, "renderer.js"),
+ bundle: true,
+ platform: "browser",
+ format: "iife",
+ jsx: "automatic",
+ define: { "process.env.NODE_ENV": '"production"', "import.meta.env.DEV": "false" },
+ loader: { ".css": "empty" },
+ alias: {
+ "@pi-desktop/i18n": join(root, "packages/i18n/src/index.ts"),
+ react: join(root, "apps/desktop/node_modules/react"),
+ "react-dom": join(root, "apps/desktop/node_modules/react-dom"),
+ },
+ nodePaths: [join(root, "apps/desktop/node_modules")],
+ });
+ const viewerStyles = await readFile(join(root, "apps/desktop/src/styles/work-panel.css"), "utf8");
+ await writeFile(join(temp, "work-panel.css"), `${viewerStyles}\nhtml,body,#root{height:100%;margin:0}#root{display:flex;flex-direction:column}.fixture-chat{flex:0 0 auto;padding:8px}`);
+ await writeFile(
+ join(temp, "index.html"),
+ '<!doctype html><meta charset="utf-8"><link rel="stylesheet" href="work-panel.css"><div id="root"></div><script src="renderer.js"></script>',
+ );
+ await writeFile(
+ join(temp, "main.cjs"),
+ `
+const { app, BrowserWindow } = require("electron");
+const path = require("node:path");
+app.disableHardwareAcceleration();
+app.setPath("userData", path.join(__dirname, "profile"));
+app.whenReady().then(async () => {
+ const window = new BrowserWindow({ show: false, width: 900, height: 420,
+ webPreferences: { sandbox: true, contextIsolation: true, nodeIntegration: false, backgroundThrottling: false } });
+ window.webContents.on("console-message", (event) => console.error(event.message));
+ try {
+ await window.loadFile(path.join(__dirname, "index.html"));
+ const result = await window.webContents.executeJavaScript("window.fileRefLineScrollProbe()");
+ console.log("FILE_REF_LINE_SCROLL " + JSON.stringify(result));
+ app.exit(0);
+ } catch (error) {
+ console.error("FILE_REF_LINE_SCROLL " + (error?.stack ?? String(error)));
+ app.exit(1);
+ }
+});
+`,
+ );
+ const env = { ...process.env };
+ delete env.ELECTRON_RUN_AS_NODE;
+ const child = spawn(resolveElectronBinary(root).electronBinary, [
+ "--headless=new",
+ "--disable-gpu",
+ join(temp, "main.cjs"),
+ ], { env, stdio: ["ignore", "pipe", "pipe"] });
+ let output = "";
+ for (const stream of [child.stdout, child.stderr]) {
+ stream.on("data", (chunk) => { output += chunk; });
+ }
+ const timeout = setTimeout(() => child.kill("SIGKILL"), 45_000);
+ let code;
+ try {
+ code = await new Promise((resolve, reject) => {
+ child.once("error", reject);
+ child.once("close", resolve);
+ });
+ } finally {
+ clearTimeout(timeout);
+ }
+ const line = output.split(/\r?\n/).find((item) => item.startsWith("FILE_REF_LINE_SCROLL {"));
+ assert(line, `renderer returned no file reference result (exit=${code}): ${output.slice(-4000)}`);
+ const result = JSON.parse(line.slice("FILE_REF_LINE_SCROLL ".length));
+ console.log(line);
+ assert.equal(code, 0, output.slice(-4000));
+ assert.equal(result.fileManagerAvailable, true, "fixture must use the normal project-file setup");
+ assert.deepEqual(result.resolveRefs, ["src/scroll-target.txt", "src/scroll-target.txt"]);
+ assert.deepEqual(result.readPaths, ["src/scroll-target.txt"]);
+ assert.equal(result.selectedPath, "src/scroll-target.txt");
+ assert.deepEqual(result.request, { path: "src/scroll-target.txt", seq: 1, line: 65, column: 4 });
+ assert.deepEqual(result.scrollCall, { line: "65", block: "center" });
+ assert.ok(result.scrollTop > 0, "host file viewer did not scroll away from the top");
+ assert.equal(result.targetLineVisible, true, "requested line is outside the visible viewer area");
+} finally {
+ await rm(temp, { recursive: true, force: true });
+}
diff --git a/scripts/e2e/file-ref-line-scroll.tsx b/scripts/e2e/file-ref-line-scroll.tsx
new file mode 100644
index 000000000..4ae88a117
--- /dev/null
+++ b/scripts/e2e/file-ref-line-scroll.tsx
@@ -0,0 +1,162 @@
+import { createRoot } from "react-dom/client";
+import { flushSync } from "react-dom";
+import { createInstance } from "i18next";
+import { I18nextProvider, initReactI18next } from "react-i18next";
+import { IPC } from "@pi-desktop/shared";
+import { catalogs, flattenCatalog } from "@pi-desktop/i18n";
+import { FilesTab } from "../../apps/desktop/src/components/workpanel/FilesTab";
+import { LinkifiedText } from "../../apps/desktop/src/features/chat/transcript/shared";
+import { useAppStore } from "../../apps/desktop/src/stores/app-store";
+import { splitChatText } from "../../apps/desktop/src/lib/chat-links";
+
+const targetPath = "src/scroll-target.txt";
+const targetLine = 65;
+const content = Array.from({ length: 90 }, (_, index) => `line ${index + 1}`).join("\n");
+const resolveRefs: string[] = [];
+const readPaths: string[] = [];
+const scrollCalls: Array<{ line: string | undefined; block?: string }> = [];
+
+window.piDesktop = {
+ platform: "darwin",
+ channels: IPC,
+ on: () => () => {},
+ async invoke(channel, ...args) {
+ const input = args[0] as Record<string, unknown> | undefined;
+ if (channel === IPC.invoke.fsResolveRef) {
+ const ref = String(input?.ref ?? "");
+ resolveRefs.push(ref);
+ return {
+ ok: true,
+ data: {
+ match: {
+ root: "workspace",
+ relativePath: targetPath,
+ absolutePath: `/workspace/${targetPath}`,
+ matchedBy: "exact-relative",
+ projectRoot: { path: "/workspace", name: "Fixture", primary: true },
+ },
+ },
+ };
+ }
+ if (channel === IPC.invoke.fsList) {
+ return { ok: true, data: { entries: [] } };
+ }
+ if (channel === IPC.invoke.fsRead) {
+ const path = String(input?.path ?? "");
+ readPaths.push(path);
+ return { ok: true, data: { kind: "text", content, size: content.length } };
+ }
+ throw new Error(`Unexpected file viewer IPC: ${channel}`);
+ },
+};
+
+useAppStore.setState({
+ workspace: { path: "/workspace", name: "Fixture" },
+ activeSessionId: "fixture-session",
+ pluginViews: [{ pluginId: "pi.file-manager", viewId: "manager" }],
+ workPanelOpen: false,
+ workPanelTabs: [],
+ activeWorkPanelTabId: null,
+ workPanelFileRequest: null,
+});
+
+const nativeScrollIntoView = HTMLElement.prototype.scrollIntoView;
+if (!nativeScrollIntoView) throw new Error("Chromium scrollIntoView is unavailable");
+HTMLElement.prototype.scrollIntoView = function (options?: ScrollIntoViewOptions) {
+ scrollCalls.push({
+ line: this.getAttribute("data-line") ?? undefined,
+ block: options?.block,
+ });
+ nativeScrollIntoView.call(this, options);
+};
+
+const i18n = createInstance();
+void i18n
+ .use(initReactI18next)
+ .init({
+ lng: "en",
+ fallbackLng: "en",
+ keySeparator: false,
+ resources: { en: { translation: flattenCatalog(catalogs.en) } },
+ interpolation: { escapeValue: false },
+ })
+ .then(() => {
+ const root = createRoot(document.getElementById("root")!);
+ flushSync(() =>
+ root.render(
+ <I18nextProvider i18n={i18n}>
+ <div className="fixture-chat">
+ <LinkifiedText text={`Review ${targetPath}:${targetLine}:4.`} />
+ </div>
+ <FilesTab />
+ </I18nextProvider>,
+ ),
+ );
+
+ window.fileRefLineScrollProbe = async () => {
+ const until = async (predicate: () => boolean, label: string) => {
+ for (let frame = 0; frame < 180; frame++) {
+ if (predicate()) return true;
+ await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
+ }
+ return false;
+ };
+
+ if (!(await until(
+ () => Boolean(document.querySelector<HTMLButtonElement>(".chat-file-chip")),
+ "verified transcript chip",
+ ))) throw new Error("The verified file reference chip did not render");
+ document.querySelector<HTMLButtonElement>(".chat-file-chip")!.click();
+ if (!(await until(
+ () => Boolean(document.querySelector(".file-viewer-path")) && readPaths.length > 0,
+ "host viewer file load",
+ ))) throw new Error(`Host file viewer did not load: ${JSON.stringify({ resolveRefs, readPaths, request: useAppStore.getState().workPanelFileRequest })}`);
+ await until(() => scrollCalls.length > 0, "host viewer line scroll");
+
+ const viewer = document.querySelector<HTMLElement>(".file-viewer-body");
+ const line = viewer?.querySelector<HTMLElement>(`[data-line="${targetLine}"]`);
+ if (!viewer || !line) throw new Error("The requested source line was not rendered");
+ const viewerRect = viewer.getBoundingClientRect();
+ const lineRect = line.getBoundingClientRect();
+ const request = useAppStore.getState().workPanelFileRequest;
+ return {
+ fileManagerAvailable: useAppStore.getState().pluginViews.some(
+ (view) => view.pluginId === "pi.file-manager" && view.viewId === "manager",
+ ),
+ selectedPath: document.querySelector(".file-viewer-path")?.textContent,
+ segments: splitChatText(`Review ${targetPath}:${targetLine}:4.`, "/workspace"),
+ resolveRefs,
+ readPaths,
+ request,
+ scrollCall: scrollCalls.at(-1) ?? null,
+ scrollTop: viewer.scrollTop,
+ targetLineVisible: lineRect.top >= viewerRect.top && lineRect.bottom <= viewerRect.bottom,
+ };
+ };
+ })
+ .catch((error: unknown) => {
+ console.error(error);
+ throw error;
+ });
+
+declare global {
+ interface Window {
+ piDesktop?: {
+ platform: NodeJS.Platform;
+ channels: typeof IPC;
+ on: (channel: string, listener: (...args: unknown[]) => void) => () => void;
+ invoke: (channel: string, ...args: unknown[]) => Promise<unknown>;
+ };
+ fileRefLineScrollProbe: () => Promise<{
+ fileManagerAvailable: boolean;
+ selectedPath: string | null | undefined;
+ segments: unknown[];
+ resolveRefs: string[];
+ readPaths: string[];
+ request: { path: string; line?: number; column?: number } | null;
+ scrollCall: { line: string | undefined; block?: string } | undefined;
+ scrollTop: number;
+ targetLineVisible: boolean;
+ }>;
+ }
+}