Files
magpie/internal/agent/zed_credential_other.go
T
yetone 2221b63f93 agent, provider: tests no longer read the machine's /Applications or Program Files, so a Mac with DeepSeek Harness.app installed keeps TestAsideOfflineDisconnectRestoresWithoutDaemon green (#1823)
What: every place magpie looks for installed apps outside the home folder
now goes through proc.SystemDirs, which in a test binary testenv isolated
puts the path under the test's home (as it already does for Homebrew's
bin, #1525):
- agent: a new macAppFolders(home) (/Applications via SystemDirs, plus
  ~/Applications) is used by appFolders (desktop apps: Codex, dsh),
  pencilApp, t3App and reasonixDetected;
- cursorLocalRoots: macOS through macAppFolders, Windows' ProgramFiles and
  Linux's /opt, /usr/share, /usr/lib through SystemDirs;
- zedAppPath's /Applications/Zed*.app, npmGlobalPrefix's ProgramFiles npm
  fallback, and provider's /Applications/ZCode.app builtin config.
Production behaviour is unchanged: SystemDirs returns paths as they are
outside a sandboxed test.

Why: HasApp/AppVersion read the real /Applications, so on a Mac with
DeepSeek Harness.app the dsh fixture's CLI was never "missing" and the test
failed every time; CI has no such app, so nobody else saw it. The same
shape was in five other readers.

Test: TestAppFoldersStayInTheTestHome checks every app folder lies under
testenv's home (all OSes), and on macOS that an app installed in the
sandbox's /Applications is the one found.

Run (go1.26.8, sandbox HOME, private GOCACHE):
- repro on old code: appFolders pointed at a temp folder holding a fake
  "DeepSeek Harness.app/Contents/Info.plist" (com.deepseek.dsh) ->
  TestAsideOfflineDisconnectRestoresWithoutDaemon FAIL "native fixture must
  report missing CLI".
- break check: macAppFolders put back to the literal /Applications ->
  TestAppFoldersStayInTheTestHome FAIL (appFolders, macAppFolders,
  cursorLocalRoots look in /Applications; sandbox app not found). Restored:
  pass.
- gofmt -l clean; go vet ./... (darwin), GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./...: clean.
- go test -tags nogui ./internal/agent ./internal/proc ./internal/provider: ok.
- -race -count=5 on the touched agent tests: ok.
Not run: the full go test ./...; Windows and Linux test runs (vet only);
reasonix's Windows registry lookup and Zed's mdfind fallback still ask
the machine (not folder paths) and are left as they are.

Reported-by: mdwsk88
2026-10-12 23:56:20 +08:00

202 lines
6.7 KiB
Go

//go:build !windows
package agent
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/godbus/dbus/v5"
"github.com/yetone/magpie/internal/proc"
)
func saveZedCredential(url string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
var name string
var args []string
switch runtime.GOOS {
case "darwin":
name = "security"
app, err := zedAppPath(ctx)
if err != nil {
return err
}
// GPUI uses an Internet Password whose server is the full API URL.
// Keep security trusted as well so it can update the key on later picks.
args = []string{"add-internet-password", "-U", "-T", app, "-T", "/usr/bin/security", "-s", url, "-a", "Bearer", "-w", agentKeyAt("zed", url)}
case "linux", "freebsd":
return saveZedSecret(ctx, url)
default:
return fmt.Errorf("unsupported system credential store on %s", runtime.GOOS)
}
cmd := proc.CommandContext(ctx, name, args...)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("%s: %w: %s", name, err, strings.TrimSpace(string(out)))
}
return nil
}
// Trust the installed Zed bundle, including a CLI symlink to a custom location.
func zedAppPath(ctx context.Context) (string, error) {
home, _ := os.UserHomeDir()
paths := append([]string{
filepath.Join(home, "Applications", "Zed.app"), filepath.Join(home, "Applications", "Zed Preview.app"),
}, proc.SystemDirs("/Applications/Zed.app", "/Applications/Zed Preview.app")...)
binName := os.Getenv("MAGPIE_ZED_BIN")
if binName == "" {
binName = "zed"
}
if bin, err := executablePath(binName); err == nil {
if bin, err = filepath.EvalSymlinks(bin); err == nil {
for dir := filepath.Dir(bin); dir != filepath.Dir(dir); dir = filepath.Dir(dir) {
if strings.HasSuffix(dir, ".app") {
paths = append([]string{dir}, paths...)
break
}
}
}
}
for _, path := range paths {
if info, err := os.Stat(path); err == nil && info.IsDir() {
return path, nil
}
}
// Spotlight finds bundles moved outside the usual Applications folders.
out, err := proc.CommandContext(ctx, "mdfind", "kMDItemCFBundleIdentifier == 'dev.zed.Zed' || kMDItemCFBundleIdentifier == 'dev.zed.Zed-Preview'").Output()
if err == nil {
for _, path := range strings.Split(strings.TrimSpace(string(out)), "\n") {
if info, err := os.Stat(path); err == nil && info.IsDir() && strings.HasSuffix(path, ".app") {
return path, nil
}
}
}
return "", fmt.Errorf("cannot find Zed.app to authorize access to its gateway credential")
}
func executablePath(name string) (string, error) {
if filepath.IsAbs(name) {
if _, err := os.Stat(name); err != nil {
return "", err
}
return name, nil
}
return proc.Look(name)
}
const secretService = "org.freedesktop.Secret.Service"
// The field order is the Secret Service D-Bus signature (oayays).
type zedSecret struct {
Session dbus.ObjectPath
Parameters []byte
Value []byte
ContentType string
}
func saveZedSecret(ctx context.Context, url string) error {
conn, err := dbus.ConnectSessionBus()
if err != nil {
return fmt.Errorf("Secret Service session bus: %w", err)
}
defer conn.Close()
service := conn.Object("org.freedesktop.secrets", "/org/freedesktop/secrets")
var output dbus.Variant
var session dbus.ObjectPath
if err := service.CallWithContext(ctx, secretService+".OpenSession", 0, "plain", dbus.MakeVariant("")).Store(&output, &session); err != nil {
return err
}
defer conn.Object("org.freedesktop.secrets", session).CallWithContext(ctx, "org.freedesktop.Secret.Session.Close", 0)
var collection, prompt dbus.ObjectPath
if err := service.CallWithContext(ctx, secretService+".ReadAlias", 0, "default").Store(&collection); err != nil {
return err
}
if collection == "/" {
properties := map[string]dbus.Variant{"org.freedesktop.Secret.Collection.Label": dbus.MakeVariant("Default")}
if err := service.CallWithContext(ctx, secretService+".CreateCollection", 0, properties, "default").Store(&collection, &prompt); err != nil {
return err
}
if prompt != "/" {
result, err := zedSecretPrompt(ctx, conn, prompt)
if err != nil {
return err
}
if err := result.Store(&collection); err != nil {
return err
}
}
}
var unlocked []dbus.ObjectPath
if err := service.CallWithContext(ctx, secretService+".Unlock", 0, []dbus.ObjectPath{collection}).Store(&unlocked, &prompt); err != nil {
return err
}
if _, err := zedSecretPrompt(ctx, conn, prompt); err != nil {
return err
}
// GPUI finds credentials by URL, username and this historical label.
properties := map[string]dbus.Variant{
"org.freedesktop.Secret.Item.Label": dbus.MakeVariant("zed-github-account"),
"org.freedesktop.Secret.Item.Attributes": dbus.MakeVariant(map[string]string{
"url": url, "username": "Bearer",
}),
}
secret := zedSecret{Session: session, Parameters: []byte{}, Value: []byte(agentKey("zed")), ContentType: "text/plain; charset=utf8"}
var item dbus.ObjectPath
if err := conn.Object("org.freedesktop.secrets", collection).CallWithContext(ctx, "org.freedesktop.Secret.Collection.CreateItem", 0, properties, secret, true).Store(&item, &prompt); err != nil {
return err
}
_, err = zedSecretPrompt(ctx, conn, prompt)
return err
}
func zedSecretPrompt(ctx context.Context, conn *dbus.Conn, path dbus.ObjectPath) (dbus.Variant, error) {
if path == "/" {
return dbus.Variant{}, nil
}
const iface = "org.freedesktop.Secret.Prompt"
match := []dbus.MatchOption{dbus.WithMatchSender("org.freedesktop.secrets"), dbus.WithMatchObjectPath(path), dbus.WithMatchInterface(iface), dbus.WithMatchMember("Completed")}
if err := conn.AddMatchSignalContext(ctx, match...); err != nil {
return dbus.Variant{}, err
}
defer conn.RemoveMatchSignalContext(ctx, match...)
signals := make(chan *dbus.Signal, 1)
conn.Signal(signals)
defer conn.RemoveSignal(signals)
object := conn.Object("org.freedesktop.secrets", path)
if err := object.CallWithContext(ctx, iface+".Prompt", 0, "").Err; err != nil {
return dbus.Variant{}, err
}
for {
select {
case signal, ok := <-signals:
if !ok {
return dbus.Variant{}, fmt.Errorf("Secret Service connection closed")
}
if signal.Path != path || signal.Name != iface+".Completed" {
continue
}
var dismissed bool
var result dbus.Variant
if err := dbus.Store(signal.Body, &dismissed, &result); err != nil {
return dbus.Variant{}, err
}
if dismissed {
return dbus.Variant{}, fmt.Errorf("Secret Service prompt dismissed")
}
return result, nil
case <-ctx.Done():
// Dismiss with a fresh deadline after the operation timed out.
cleanup, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
object.CallWithContext(cleanup, iface+".Dismiss", 0)
return dbus.Variant{}, ctx.Err()
}
}
}