sleepy_shibe on Discord (magpie 0.1.2996, Claude Code 2.1.318, own account, interactive mode): message counts 36, 65, 85, 108, 128 for one conversation. Confirmed at origin/main74d1cda42: a caller's folder Claude Code asked about was passed over for untrustedFor (10 min) only. Past that, the next run went in the caller's folder again, claudeSessions pointed at another project folder than the conversation's session in magpie's folder, so `from` went nil and the run was told the whole conversation; Claude Code asked about trust, startTrusted dropped req.branch and started from nil in magpie's folder (the deferred from.discard() had already removed the session), told it all again. Now, for the own account in the interactive mode, each run first decides as Claude Code does (untrustedHere -> claudeTrusts), from hasTrustDialogAccepted in claudeProfilePath()'s .claude.json (provider.ClaudeTrustedProjects; a missing file is nothing trusted, an unreadable one unknown, which goes on as before). The rules are Claude Code 2.1.316's, read from its bundled JS (UI/zh/jh/Y0/lr): the key of the repository's main checkout (a worktree's is the checkout it was added from), or any folder from the run's up to the git root and never past it (a nested repository isn't trusted by its parent), every folder above outside a repository; NFC keys, forward slashes on Windows (claudeTrustKey, shared with claudeTrustKeys). An untrusted folder stays in magpie's folder with its session; a folder trusted since is run in on the next run. When Claude Code asks all the same, startRun keeps `from` and startTrusted goes on from it in magpie's folder with the branch. Run (mote, at this tree on74d1cda42): - go test ./internal/gateway -run TestClaude: ok (118s); go test ./internal/provider -run Claude: ok - GOOS=windows and GOOS=linux go vet ./internal/gateway ./internal/provider: ok - win-run go test ./internal/gateway -run TestClaudeTrust: ok - real Claude Code 2.1.316 in a sandbox HOME + CLAUDE_CONFIG_DIR (dummy key, no account), tmux, 10 cases: repo/subfolder/parent-in-repo/ worktree-of-trusted/worktree-itself trusted -> no question; worktree under a trusted parent, nested repo in a trusted repo, repo under a trusted parent, unrelated folder -> asked; outside a repo a trusted parent -> no question. All ten match claudeTrustedBy. Broken by hand: - untrustedHere returning passedOver only (the old choice): TestClaudeTUIUntrustedFolderKeepsItsSession/untrusted red (helper started twice) and TestClaudeTUIFolderTrustedLaterRunInAtOnce red (ran in magpie's folder). - startTrusted dropping req.branch and starting from nil (the old fallback): .../unknown red (resumed a new transcript, not the conversation's session). Not tried: the owner's real account or a real turn through the gateway (no sign-in in the sandbox); Claude Code 2.1.318 itself (2.1.316 is installed here); a Windows or Linux real Claude Code; the full suite before the push (it runs after, through the suite lock).
128 lines
4.0 KiB
Go
128 lines
4.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// claudeTrusts is whether Claude Code, under the account it is signed in
|
|
// to, runs in folder work without asking whether to trust it, by the
|
|
// answers the user gave it (provider.ClaudeTrustedProjects); err when
|
|
// those can't be read, which is unknown. Either path of work counts, as
|
|
// written and with its links followed: Claude Code keys a folder by the
|
|
// one its working directory has, and a Mac's getcwd says /private/var/…
|
|
// for /var/… (claudeTrustKeys).
|
|
func claudeTrusts(work string) (bool, error) {
|
|
projects, err := provider.ClaudeTrustedProjects()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
for _, w := range []string{work, evalSymlinks(work)} {
|
|
if claudeTrustedBy(projects, filepath.Clean(w)) {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// claudeTrustedBy is Claude Code's own trust check (2.1.316, read from its
|
|
// code), for a run in dir: the folder is trusted when the user trusted
|
|
// the main checkout of the repository dir is in (its own root, or for a
|
|
// worktree the checkout it was added from), dir itself when it is in
|
|
// none, or dir or a folder above it up to the root of the repository
|
|
// it is in, never past it: a trusted repository doesn't trust a
|
|
// repository inside it. Outside a repository every folder above counts.
|
|
// Accepting the question records the first of these (claudeTrustKeys'
|
|
// form), which is why a worktree of a trusted checkout isn't asked about.
|
|
func claudeTrustedBy(projects map[string]bool, dir string) bool {
|
|
root := claudeGitRoot(dir)
|
|
key := dir
|
|
if root != "" {
|
|
key = claudeMainCheckout(root)
|
|
}
|
|
if projects[claudeTrustKey(key)] {
|
|
return true
|
|
}
|
|
for d := dir; ; {
|
|
if projects[claudeTrustKey(d)] {
|
|
return true
|
|
}
|
|
if d == root {
|
|
return false
|
|
}
|
|
up := filepath.Dir(d)
|
|
if up == d {
|
|
return false
|
|
}
|
|
d = up
|
|
}
|
|
}
|
|
|
|
// claudeGitRoot is the folder nearest dir, dir itself first, that holds a
|
|
// .git, a folder or a file (a worktree's, a submodule's) or a link to
|
|
// one, as Claude Code's findGitRoot takes it; "" when none does.
|
|
func claudeGitRoot(dir string) string {
|
|
for d := dir; ; {
|
|
if fi, err := os.Stat(filepath.Join(d, ".git")); err == nil && (fi.IsDir() || fi.Mode().IsRegular()) {
|
|
return d
|
|
}
|
|
up := filepath.Dir(d)
|
|
if up == d {
|
|
return ""
|
|
}
|
|
d = up
|
|
}
|
|
}
|
|
|
|
// claudeMainCheckout is the checkout a worktree at root was added from,
|
|
// as Claude Code finds it: root's .git file names the worktree's git
|
|
// folder (<common>/worktrees/<name>), whose commondir names the
|
|
// repository's own and whose gitdir names root's .git back; the checkout
|
|
// is the folder holding that common .git, or the common folder itself
|
|
// for a bare repository. Anything else, a repository's own root and a
|
|
// submodule's included, is root.
|
|
func claudeMainCheckout(root string) string {
|
|
b, err := os.ReadFile(filepath.Join(root, ".git"))
|
|
if err != nil {
|
|
return root // a folder, or unreadable
|
|
}
|
|
line := strings.TrimSpace(string(b))
|
|
if !strings.HasPrefix(line, "gitdir:") {
|
|
return root
|
|
}
|
|
gitDir := resolveFrom(root, strings.TrimSpace(strings.TrimPrefix(line, "gitdir:")))
|
|
common, err := os.ReadFile(filepath.Join(gitDir, "commondir"))
|
|
if err != nil {
|
|
return root
|
|
}
|
|
commonDir := resolveFrom(gitDir, strings.TrimSpace(string(common)))
|
|
if filepath.Dir(gitDir) != filepath.Join(commonDir, "worktrees") {
|
|
return root
|
|
}
|
|
back, err := os.ReadFile(filepath.Join(gitDir, "gitdir"))
|
|
if err != nil {
|
|
return root
|
|
}
|
|
if evalSymlinks(resolveFrom(gitDir, strings.TrimSpace(string(back)))) != filepath.Join(evalSymlinks(root), ".git") {
|
|
return root
|
|
}
|
|
if filepath.Base(commonDir) != ".git" {
|
|
if _, err := os.Lstat(filepath.Join(commonDir, ".git")); err == nil {
|
|
return root
|
|
}
|
|
return commonDir
|
|
}
|
|
return filepath.Dir(commonDir)
|
|
}
|
|
|
|
// resolveFrom is path p read from base, as Node's path.resolve(base, p).
|
|
func resolveFrom(base, p string) string {
|
|
if filepath.IsAbs(p) {
|
|
return filepath.Clean(p)
|
|
}
|
|
return filepath.Join(base, p)
|
|
}
|