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(agent-runtime): keep project instructions for out-of-root paths
yashihu17942650ee2f
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.mdindex 829b7efe2..a40b03b08 100644--- a/docs/spec/03-runtime/02-agent-runtime.md+++ b/docs/spec/03-runtime/02-agent-runtime.md@@ -1457,7 +1457,10 @@ tool continues with the runtime's base/root chain rather than waiting for thegeneral host RPC timeout. A failed resolution never leaves a previouslyresolved sibling-directory chain active.-All discovery stays within the session project root. Empty, unreadable, and+All discovery stays within the session project root. A target path outside the+project root, or the root path itself, resolves to the root's own chain instead+of an empty result. File tools on attachments or other locations therefore+keep the root chain (which may itself be empty). Empty, unreadable, andout-of-root files are skipped. The combined UTF-8 content is capped at 32 KiBand source paths are labelled under `# Project instructions`.The sidecar never reads workspace instructions directly. A changed root chaindiff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.mdindex c64ea431b..ad118b804 100644--- a/docs/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/spec/06-delivery/04-e2e-test-plan.md@@ -5595,8 +5595,11 @@ must keep splitting are covered by `markdown-blocks.test.mjs`.`AGENTS.md`; `CLAUDE.md` and `.claude/CLAUDE.md` are fallback names. The idlefollow-up uses changed root content rather than reusing the prior runtime.Empty, unreadable, oversized, and out-of-root instruction files do not block- the turn; combined UTF-8 content is capped at 32 KiB. If path-specific- resolution exceeds its two-second deadline or the host is unavailable, the+ the turn; combined UTF-8 content is capped at 32 KiB. A file tool whose target+ is outside the project root, or targets the root itself, keeps the root chain+ rather than clearing the project instructions; instruction files are still+ read only from inside the root. If path-specific resolution exceeds its+ two-second deadline or the host is unavailable, thefile tool continues with the base chain and does not retain a siblingdirectory's rules. Repeated file tools in the same directory during oneprompt reuse one path-resolution claim; the next prompt resolves again sodiff --git a/docs/zh-CN/spec/03-runtime/02-agent-runtime.md b/docs/zh-CN/spec/03-runtime/02-agent-runtime.mdindex a9b1ccc20..e0cd98e48 100644--- a/docs/zh-CN/spec/03-runtime/02-agent-runtime.md+++ b/docs/zh-CN/spec/03-runtime/02-agent-runtime.md@@ -1067,8 +1067,10 @@ sidecar 无法选择不同的根。在一次提示期间,路径解析一般主机RPC超时。失败的解决方案永远不会留下以前的解决方案已解决同级目录链处于活动状态。-所有发现都保留在会话项目根目录中。空的、不可读的、-跳过根目录外的文件。合并的 UTF-8 内容上限为 32 KiB+所有发现都保留在会话项目根目录中。目标路径位于项目根之外,+或目标就是项目根本身时,解析结果回退为根目录自身的链,而不是空结果,+因此对附件或其他位置的工具调用会保留根链(根链本身也可能为空)。+空的、不可读的以及根目录外的指令文件仍会被跳过。合并的 UTF-8 内容上限为 32 KiB源路径标记在 `# Project instructions` 下。sidecar 从不直接读取工作区指令。改变的根链在下一个提示时重新创建空闲运行时;嵌套指令已解决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.mdindex 434b3daef..8cc84b565 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@@ -3512,7 +3512,9 @@ IPC 请求无法关闭。`AGENTS.md`; `CLAUDE.md` 和 `.claude/CLAUDE.md` 是后备名称。闲着的后续使用更改的根内容而不是重用之前的运行时。空的、不可读的、过大的和超出根的指令文件不会阻塞- 转牌圈;组合的 UTF-8 内容上限为 32 KiB。如果路径特定+ 转牌圈;组合的 UTF-8 内容上限为 32 KiB。目标路径位于项目根之外,+ 或目标就是项目根本身时,文件工具会保留根链,而不是清空项目指令;+ 指令文件仍然只从项目根内部读取。如果路径特定分辨率超过其两秒期限或主机不可用,则文件工具继续使用基础链并且不保留同级链目录的规则。同一目录中的重复文件工具一次diff --git a/packages/agent-runtime/src/project-instructions.test.ts b/packages/agent-runtime/src/project-instructions.test.tsindex 97659a721..4552dc7ea 100644--- a/packages/agent-runtime/src/project-instructions.test.ts+++ b/packages/agent-runtime/src/project-instructions.test.ts@@ -88,7 +88,7 @@ describe("loadProjectInstructions", () => {expect(loaded!.entries[0].content.endsWith("中")).toBe(true);});- it("treats missing, blank, and out-of-workspace files as absent", async () => {+ it("treats missing and blank project instructions as absent", async () => {root = await mkdtemp(join(tmpdir(), "pi-desktop-instructions-"));await expect(loadProjectInstructions(root)).resolves.toBeUndefined();@@ -96,6 +96,43 @@ describe("loadProjectInstructions", () => {await expect(loadProjectInstructions(root)).resolves.toBeUndefined();await expect(loadProjectInstructions(root, "../outside/file.ts")).resolves.toBeUndefined();});++ it("falls back to the project-root chain for a target outside the workspace", async () => {+ root = await mkdtemp(join(tmpdir(), "pi-desktop-instructions-"));+ const outside = await mkdtemp(join(tmpdir(), "pi-desktop-instructions-outside-"));+ await writeFile(join(root, "AGENTS.md"), "Use root conventions.");+ await writeFile(join(outside, "AGENTS.md"), "Use outside rules.");++ try {+ await expect(+ loadProjectInstructions(root, join(outside, "attached.txt")),+ ).resolves.toEqual({+ entries: [{ source: "AGENTS.md", content: "Use root conventions." }],+ });+ } finally {+ await rm(outside, { recursive: true, force: true });+ }+ });++ it("uses the project-root chain when the workspace root itself is the target", async () => {+ root = await mkdtemp(join(tmpdir(), "pi-desktop-instructions-"));+ await writeFile(join(root, "AGENTS.md"), "Use root conventions.");++ await expect(loadProjectInstructions(root, root)).resolves.toEqual({+ entries: [{ source: "AGENTS.md", content: "Use root conventions." }],+ });+ });++ it("does not fabricate instructions without a project or a project-root file", async () => {+ root = await mkdtemp(join(tmpdir(), "pi-desktop-instructions-"));++ await expect(+ loadProjectInstructions(root, join(root, "..", "outside", "attached.txt")),+ ).resolves.toBeUndefined();+ await expect(+ loadProjectInstructions(undefined, join(root, "..", "outside", "attached.txt")),+ ).resolves.toBeUndefined();+ });});describe("loadInstructionChain", () => {@@ -113,6 +150,22 @@ describe("loadInstructionChain", () => {});});+ it("keeps the global and root entries when the target is outside the workspace", async () => {+ root = await mkdtemp(join(tmpdir(), "pi-desktop-instructions-"));+ const globalPath = join(root, "global-AGENTS.md");+ await writeFile(globalPath, "Use global conventions.");+ await writeFile(join(root, "AGENTS.md"), "Use project conventions.");++ await expect(+ loadInstructionChain(root, join(root, "..", "outside", "attached.txt"), globalPath),+ ).resolves.toEqual({+ entries: [+ { source: "~/.pi/agent/AGENTS.md", content: "Use global conventions." },+ { source: "AGENTS.md", content: "Use project conventions." },+ ],+ });+ });+it("shares the 32 KiB byte budget between global and project instructions", async () => {root = await mkdtemp(join(tmpdir(), "pi-desktop-instructions-"));const globalPath = join(root, "global-AGENTS.md");diff --git a/packages/agent-runtime/src/project-instructions.ts b/packages/agent-runtime/src/project-instructions.tsindex 95e4b678e..b4ed9dd03 100644--- a/packages/agent-runtime/src/project-instructions.ts+++ b/packages/agent-runtime/src/project-instructions.ts@@ -87,12 +87,13 @@ export async function loadProjectInstructions(const target = workspacePath?.trim()? resolve(root, workspacePath): root;- if (!isWithinRoot(root, target)) return undefined;-- const targetDirectory = workspacePath?.trim() ? dirname(target) : root;+ // Instructions are never read from outside the project. A target outside the+ // root, or the root itself (whose parent lies outside), keeps the root's own+ // chain instead of dropping every project instruction.+ const targetDirectory =+ isWithinRoot(root, target) && target !== root ? dirname(target) : root;const directories: string[] = [];for (let current = targetDirectory; ; current = dirname(current)) {- if (!isWithinRoot(root, current)) return undefined;directories.unshift(current);if (current === root) break;}diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.tsindex 8e4268e15..5ba200153 100644--- a/packages/agent-runtime/src/runtime.test.ts+++ b/packages/agent-runtime/src/runtime.test.ts@@ -1,3 +1,6 @@+import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";import { describe, expect, it, vi } from "vitest";import { estimateContextTokens as estimateAgentContextTokens, estimateTokens, type Agent, type AgentMessage } from "@earendil-works/pi-agent-core";import {@@ -23,7 +26,7 @@ import {import { estimateOutputCapInputTokens } from "./output-cap.js";import { COMPACTION_SUMMARY_MAX_RETRIES } from "./compaction-summary-input.js";-import type { ProjectInstructions } from "./project-instructions.js";+import { loadInstructionChain, type ProjectInstructions } from "./project-instructions.js";import { classifyAgentError } from "./agent-errors.js";import {PROVIDER_RATE_LIMIT_MAX_RETRIES,@@ -940,6 +943,46 @@ describe("DesktopAgentRuntime configuration matching", () => {await runtime.dispose();});+ it("keeps the root prompt after reading a file outside the project", async () => {+ const root = await mkdtemp(join(tmpdir(), "pi-desktop-instructions-"));+ const outside = await mkdtemp(join(tmpdir(), "pi-desktop-attachment-"));+ try {+ await mkdir(join(root, "nested"));+ await writeFile(join(root, "AGENTS.md"), "Use root rules.");+ await writeFile(join(root, "nested", "AGENTS.md"), "Use nested rules.");+ const globalPath = join(root, "nonexistent-global-file");+ const host = {+ call: vi.fn((method: string, params: { path?: string }) =>+ method === "project.instructions.resolve"+ ? loadInstructionChain(root, params.path, globalPath)+ : Promise.resolve({ ok: true, content: "fixture contents" })),+ };+ const runtime = createRuntime({+ host,+ projectPath: root,+ projectInstructions: await loadInstructionChain(root, undefined, globalPath),+ });+ try {+ const read = (runtime as any).agent.state.tools.find(+ (tool: any) => tool.name === "Read",+ );+ await read.execute("tool-in", { path: join(root, "nested", "file.ts") });+ expect((runtime as any).agent.state.systemPrompt).toContain("Use nested rules.");++ await read.execute("tool-out", { path: join(outside, "attached.txt") });+ expect((runtime as any).agent.state.systemPrompt).toContain("Use root rules.");+ expect((runtime as any).agent.state.systemPrompt).not.toContain("Use nested rules.");+ expect(host.call.mock.calls.filter(([method]) => method === "project.instructions.resolve"))+ .toHaveLength(2);+ } finally {+ await runtime.dispose();+ }+ } finally {+ await rm(root, { recursive: true, force: true });+ await rm(outside, { recursive: true, force: true });+ }+ });+it("loads newly discovered nested instructions before a file tool runs", async () => {const host = {call: vi