Files
magpie/internal/gateway/claude_workdir_owner_other.go
T
yetoneandyg3415 17363b7e30 claude: Claude Code runs share a folder only the user owns, the caller's system prompt is cached apart, and auto memory is off (#626, yg3415) — main already ran every Claude Code run in one $TMPDIR/magpie-claude so Anthropic's prompt cache is read again; this takes yg3415's version of that: the folder is magpie-claude-work-<uid> (the uid where the temp folder is shared, as /tmp is on Linux), made 0700 and used only when it is a real folder the user owns that no one else can write to (else the run works in a folder of its own, as before), so nobody else can put a CLAUDE.md in front of every run; the caller's system prompt goes to Claude Code as a block of its own with a 1h cache_control breakpoint (CLAUDE_CODE_PROMPT_CACHE_TTL=1h, since Anthropic refuses a 1h breakpoint after a 5m one), so requests with one system prompt and different conversations share its cache entry; and CLAUDE_CODE_DISABLE_AUTO_MEMORY=1, so a memory Claude Code writes in one caller's conversation doesn't show up in every other one in the shared folder. Merged onto main by hand: the run still starts through binary.command/binary.env, so a Claude Code in WSL works in the shared folder too; on Windows the folder is in the user's own %TEMP% and Go reports every folder there as 0777, so the mode check is skipped there (it refused every run before). Tests from the PR (TestClaudeRunsShareAWorkDir with a home kept in git, TestClaudeWorkDirOthersCanWriteIsAvoided, TestClaudeInstructionsCachedApart) pass; go vet (darwin, GOOS=windows), builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
Co-authored-by: yg3415 <115795347+yg3415@users.noreply.github.com>
2026-10-04 18:25:44 +08:00

23 lines
606 B
Go

//go:build !windows
package gateway
import (
"os"
"strconv"
"syscall"
)
// ownedByMe says the file belongs to the user magpie runs as.
func ownedByMe(fi os.FileInfo) bool {
st, ok := fi.Sys().(*syscall.Stat_t)
return ok && int(st.Uid) == os.Getuid()
}
// othersCanWrite says the group or anyone else may write in the folder.
func othersCanWrite(fi os.FileInfo) bool { return fi.Mode().Perm()&0o022 != 0 }
// claudeWorkName is the work folder's name in the temp folder, which on
// Linux every user shares.
func claudeWorkName() string { return "magpie-claude-work-" + strconv.Itoa(os.Getuid()) }