Files
magpie/internal/update/shellfn_windows.go
T
yetone cc9d68ffd3 agent: Antigravity CLI in a WSL distro is found and wired
mimi001234 (Discord, #general): agy installed in a WSL distro never
showed on the Agents page. wslKinds had no agy, so the distro probe never
looked for it.

agy is now a WSL kind (bin agy, dir .gemini/antigravity-cli), with the
model field offered for a stopped distro too. agy takes magpie only from
how it is started, and no magpie runs inside the distro, so a magpie pick
writes an agy shell function into the distro's own profiles: .bashrc,
plus .zshrc or fish's config.fish when the login shell in the distro's
/etc/passwd is zsh or fish. The function reads settings.json's model at
run time. It adds --model only for a magpie model the user didn't name
themselves, and sets GOOGLE_GEMINI_BASE_URL to place.gw() (the NAT
address, never 127.0.0.1) and GEMINI_API_KEY to the distro's WSL key.
Default removes the block. Check flags a function whose address has
moved, and Sync rewrites it. update.ShellFunction gained ShellFunctionAt,
DropShellFunctionAt and ShellFunctionInAt for profiles given by path
(fnProfile is now ShellProfile). This machine's own agy function is
unchanged. The restart notice is translated (de, zh, zh-TW, ja).
agent-wiring.md and reference.md are updated.

Run at c272f3799 + this change, then at this head (rebased on
65ee8c65c, which touched only gateway and plugin): the whole of
internal/agent and internal/update again, ok, and go build ok.
- go test -tags nogui -timeout 45m ./internal/agent/... ./internal/update/...
  ./internal/cli/...: update and cli ok. agent was red only on
  TestEveryNoticeIsTranslated (the new notice wasn't in i18n.js); after
  adding it, TestEveryNoticeIsTranslated and TestWSLAgy* are ok.
- go test -tags nogui ./internal/gui: ok.
- GUI, chromium and webkit: i18n-keys, agent-notice-lang and
  wsl-agents-setting all pass.
- gofmt -l on the changed files; go vet ./...; GOOS=linux go vet -tags
  nogui ./...; GOOS=windows go vet ./...; go build for darwin, linux
  (nogui) and windows: all clean.
- The bash and zsh functions are sourced in the test with a fake agy. The
  fish function was run in docker alpine:3.20 with fish.

Broken by hand, each red:
- bin "agyx" in the wsl kind: TestWSLAgyFound and TestWSLAgyStopped fail.
- 127.0.0.1 instead of at.gw(): TestWSLAgyPick and TestWSLAgyFunctionRuns
  fail.
- default not dropping the function: TestWSLAgyPick fails on .bashrc.

Not tried: real WSL. The Windows box (10.0.0.132) didn't answer ssh.
Fish wasn't tried inside a real distro.
2026-10-27 09:24:43 +08:00

91 lines
2.7 KiB
Go

package update
import (
"os"
"path/filepath"
"strings"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"github.com/yetone/magpie/internal/proc"
)
// shellProfiles are PowerShell 7's and Windows PowerShell's profiles, each
// only where its execution policy runs a profile: Windows PowerShell's is
// Restricted until the user changes it, and a profile it may not run
// would put an error in each terminal it opens. cmd has no profile.
func shellProfiles() []ShellProfile {
docs := documents()
if docs == "" {
return nil
}
gpo := policy(registry.LOCAL_MACHINE, `SOFTWARE\Policies\Microsoft\Windows\PowerShell`)
var out []ShellProfile
seven := filepath.Join(docs, "PowerShell")
if _, err := os.Stat(seven); err == nil || pwshInstalled() {
// RemoteSigned unless a policy says otherwise
if gpo == "" || runsProfile(gpo) {
out = append(out, ShellProfile{Kind: "powershell", Path: filepath.Join(seven, "Microsoft.PowerShell_profile.ps1"), Create: true})
}
}
p := gpo
if p == "" {
p = policy(registry.CURRENT_USER, `SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell`)
}
if p == "" {
p = policy(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell`)
}
if runsProfile(p) {
out = append(out, ShellProfile{Kind: "powershell", Path: filepath.Join(docs, "WindowsPowerShell", "Microsoft.PowerShell_profile.ps1"), Create: true})
}
return out
}
func pwshInstalled() bool {
_, err := proc.Look("pwsh")
return err == nil
}
// runsProfile says a policy lets a profile written here run.
func runsProfile(p string) bool {
switch strings.ToLower(p) {
case "remotesigned", "unrestricted", "bypass":
return true
}
return false
}
// policy is the ExecutionPolicy a registry key sets, "" none.
func policy(root registry.Key, path string) string {
k, err := registry.OpenKey(root, path, registry.QUERY_VALUE)
if err != nil {
return ""
}
defer k.Close()
if path == `SOFTWARE\Policies\Microsoft\Windows\PowerShell` {
if on, _, err := k.GetIntegerValue("EnableScripts"); err == nil && on == 0 {
return "Restricted"
}
}
v, _, _ := k.GetStringValue("ExecutionPolicy")
return v
}
// documents is the user's Documents folder, where PowerShell's profiles
// are; under a home that isn't the user's own (a test's), Documents in it.
func documents() string {
home := os.Getenv("USERPROFILE")
if home == "" {
return ""
}
own, _ := windows.KnownFolderPath(windows.FOLDERID_Profile, 0)
if own == "" || !strings.EqualFold(filepath.Clean(own), filepath.Clean(home)) {
return filepath.Join(home, "Documents")
}
if d, err := windows.KnownFolderPath(windows.FOLDERID_Documents, 0); err == nil && d != "" {
return d
}
return filepath.Join(home, "Documents")
}