A plugin runs in magpie's plugin host, so its process.cwd() is magpie's folder. The ZCode plugin's Start Plan request said that folder (C:\Users\<user>\.config\magpie) as the agent's "Primary working directory", with "Is a git repository: no", and the model looked there. forwardOnce now calls tellWorkPlace on every request to a plugin's provider. It reads the request as sent to the plugin with workPlace (the same reading rules by folder use) and sets two headers on the plugin's fetch: - x-magpie-work-dir (provider.WorkDirHeader): the folder, percent-encoded (url.PathEscape; decodeURIComponent reads it); - x-magpie-work-git (provider.WorkGitHeader): yes/no. The agent's own word beside the folder (Claude Code's "Is a git repository: true/false", OpenCode's "Is directory a git repo: yes/no") wins. Otherwise it is a .git in the folder or above it, when the folder is on this computer (provider.GitSaid, provider.GitOnDisk in the new workplace.go). Each is left off when unknown; a folder that isn't on this computer and that the agent didn't call a repository has no git value, never "no". host.js's fetch (unsaid) takes both headers off every request a plugin sends, so a plugin that forwards the headers it was given sends them to no vendor. Documented in docs/subsystems/provider-plugins.md. Run: TestPluginToldWhereTheAgentWorks (new) sends the captured Claude Code and Codex requests, and Pi-style ones from a temp repo subfolder and a plain folder, through the fake plugin to fake-claude and fake-1. It checks what the plugin read and that the vendor got neither header. Broken by hand: without the tellWorkPlace call every case fails; without unsaid in host.js it fails with "the vendor got X-Magpie-Work-Dir". go test internal/gateway (-timeout 40m), internal/provider, internal/plugin; gofmt -l; go vet on darwin, GOOS=linux and GOOS=windows for the three packages.
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package provider
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// gitSaidRe is whether the agent's folder is in a git repository, as the
|
|
// agent says it beside the folder: Claude Code's " - Is a git repository:
|
|
// false" (2.1.296; "Is directory a git repo: Yes" before), OpenCode's
|
|
// " Is directory a git repo: yes".
|
|
var gitSaidRe = regexp.MustCompile(`(?mi)^\s*(?:- )?Is (?:a git repository|directory a git repo): (true|false|yes|no)\s*$`)
|
|
|
|
// GitSaid is the last "yes" or "no" an agent's text says of its folder
|
|
// being in a git repository, "" when it says neither.
|
|
func GitSaid(text string) string {
|
|
m := gitSaidRe.FindAllStringSubmatch(text, -1)
|
|
if len(m) == 0 {
|
|
return ""
|
|
}
|
|
switch strings.ToLower(m[len(m)-1][1]) {
|
|
case "true", "yes":
|
|
return "yes"
|
|
}
|
|
return "no"
|
|
}
|
|
|
|
// GitOnDisk says whether dir, a folder on this computer, is in a git
|
|
// repository: a .git in it or in a folder above it. "" when dir isn't a
|
|
// folder here (an agent on another computer, through a shared magpie):
|
|
// unknown, never "no".
|
|
func GitOnDisk(dir string) string {
|
|
if !filepath.IsAbs(dir) {
|
|
return ""
|
|
}
|
|
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
|
|
return ""
|
|
}
|
|
for d := filepath.Clean(dir); ; {
|
|
if _, err := os.Stat(filepath.Join(d, ".git")); err == nil {
|
|
return "yes"
|
|
}
|
|
up := filepath.Dir(d)
|
|
if up == d {
|
|
return "no"
|
|
}
|
|
d = up
|
|
}
|
|
}
|