From b6239fc4582aba444416b20831e6eba8f25978bf Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Fri, 14 Aug 2026 11:15:56 +0800 Subject: [PATCH] fix: make agent snapshots writable --- src/chat/processor.ts | 17 +++++++++++++++++ src/repository/knowledge.ts | 2 ++ test/chat-mention.test.ts | 6 ++++++ test/repository-knowledge.test.ts | 5 +++++ 4 files changed, 30 insertions(+) diff --git a/src/chat/processor.ts b/src/chat/processor.ts index ecdcb24..5807453 100644 --- a/src/chat/processor.ts +++ b/src/chat/processor.ts @@ -345,9 +345,26 @@ export async function createRepositorySnapshot(sourceWorktree: string): Promise< return !(await fs.lstat(source)).isSymbolicLink(); } }); + await makeSnapshotWritableForContainer(snapshot); return snapshot; } catch (error) { await fs.rm(snapshot, { recursive: true, force: true }); throw error; } } + +async function makeSnapshotWritableForContainer(directory: string): Promise { + await fs.chmod(directory, 0o777); + const entries = await fs.readdir(directory, { withFileTypes: true }); + await Promise.all(entries.map(async (entry) => { + const target = path.join(directory, entry.name); + if (entry.isDirectory()) { + await makeSnapshotWritableForContainer(target); + return; + } + if (entry.isFile()) { + const stat = await fs.stat(target); + await fs.chmod(target, stat.mode & 0o111 ? 0o777 : 0o666); + } + })); +} diff --git a/src/repository/knowledge.ts b/src/repository/knowledge.ts index d228c85..aec9534 100644 --- a/src/repository/knowledge.ts +++ b/src/repository/knowledge.ts @@ -38,7 +38,9 @@ export async function writeKnowledgeScratch(snapshot: string, content: string): validateRepositoryKnowledge(content); const target = path.join(snapshot, REPOSITORY_KNOWLEDGE_SCRATCH_PATH); await fs.mkdir(path.dirname(target), { recursive: true }); + await fs.chmod(path.dirname(target), 0o777); await fs.writeFile(target, normalizeKnowledge(content), "utf8"); + await fs.chmod(target, 0o666); } export async function readKnowledgeScratch(snapshot: string): Promise { diff --git a/test/chat-mention.test.ts b/test/chat-mention.test.ts index 7ae4066..f01e47e 100644 --- a/test/chat-mention.test.ts +++ b/test/chat-mention.test.ts @@ -81,6 +81,7 @@ test("PR chat snapshot excludes repository instructions, secrets, git data, and await fs.mkdir(path.join(source, ".agents")); await fs.mkdir(path.join(source, ".ghbot")); await fs.mkdir(path.join(source, "src")); + await fs.mkdir(path.join(source, "scripts")); await Promise.all([ fs.writeFile(path.join(source, ".git", "config"), "credential data"), fs.writeFile(path.join(source, ".goose", "config.yaml"), "untrusted goose config"), @@ -93,11 +94,16 @@ test("PR chat snapshot excludes repository instructions, secrets, git data, and fs.writeFile(path.join(source, "opencode.json"), "{}"), fs.writeFile(path.join(source, ".goosehints"), "untrusted hints"), fs.writeFile(path.join(source, "src", "index.ts"), "export const safe = true;\n"), + fs.writeFile(path.join(source, "scripts", "check.sh"), "#!/bin/sh\nexit 0\n", { mode: 0o755 }), fs.symlink(path.join(source, ".env"), path.join(source, "secret-link")) ]); snapshot = await createRepositorySnapshot(source); assert.equal(await fs.readFile(path.join(snapshot, "src", "index.ts"), "utf8"), "export const safe = true;\n"); + assert.equal((await fs.stat(snapshot)).mode & 0o777, 0o777); + assert.equal((await fs.stat(path.join(snapshot, "src"))).mode & 0o777, 0o777); + assert.equal((await fs.stat(path.join(snapshot, "src", "index.ts"))).mode & 0o777, 0o666); + assert.equal((await fs.stat(path.join(snapshot, "scripts", "check.sh"))).mode & 0o777, 0o777); for (const excluded of [ ".git", ".goose", diff --git a/test/repository-knowledge.test.ts b/test/repository-knowledge.test.ts index ce72b76..40a67a6 100644 --- a/test/repository-knowledge.test.ts +++ b/test/repository-knowledge.test.ts @@ -16,6 +16,11 @@ test("repository knowledge round trips through the agent scratch file", async () try { await writeKnowledgeScratch(root, "# Architecture\n\nUse npm test.\n"); assert.equal(await readKnowledgeScratch(root), "# Architecture\n\nUse npm test.\n"); + assert.equal((await fs.stat(path.join(root, ".ghbot"))).mode & 0o777, 0o777); + assert.equal( + (await fs.stat(path.join(root, ".ghbot", "repository-knowledge.md"))).mode & 0o777, + 0o666 + ); } finally { await fs.rm(root, { recursive: true, force: true }); }