Windows: PowerShell agy function starts agy without the gateway key (GUI magpie.exe not waited for)
What happens
On Windows, with agy on a magpie model, typing agy in PowerShell starts the real agy without the gateway or its key:
modelProvider is set to "gemini" in settings.json, but the GEMINI_API_KEY environment variable is not set. ...
The profile block (# >>> magpie agy >>>) is there and magpie agy env prints all three lines from Git Bash.
Cause
agyFunction's PowerShell body (internal/agent/agy.go) reads the variables with
foreach ($l in @(& 'C:\...\magpie.exe' agy env)) { ... }
magpie.exe is a GUI-subsystem program (PE subsystem 2; magpie.com is 3). PowerShell doesn't wait for a GUI program it runs outside a pipeline, so @(& magpie.exe agy env) is empty, $start.model is unset, and the function falls through to & $real @args with no GEMINI_API_KEY / GOOGLE_GEMINI_BASE_URL. The comment above the function already says PowerShell doesn't wait for the GUI app; the call that reads agy env has the same problem.
Measured on Windows 11, magpie at v0.1.x (.local\bin\magpie.exe, Nov 6 build):
| Call | PowerShell 7.6 | Windows PowerShell 5.1 |
|---|---|---|
@(& magpie.exe agy env).Count |
0 | 0 |
@(& magpie.exe agy env | ForEach-Object { $_ }).Count |
3 | 3 |
@(& magpie.com agy env).Count |
3 | — |
Fix
Read agy env through a pipeline (& prog agy env | ForEach-Object { ... }), which PowerShell waits on for GUI and console programs alike, so it holds whether or not a magpie.com is beside the app.
Workaround until it ships: in the profile block, replace magpie.exe with magpie.com, then . $PROFILE.
A fix is on my fork, one commit on upstream/main ce0cbff61 (PRs are collaborators only, so it's here). It cherry-picks cleanly onto today's main 2c644da04.
- Branch: https://github.com/MystryMocha/magpie/tree/fix/agy-powershell-pipeline
- Commit: https://github.com/MystryMocha/magpie/commit/d4177a272
git fetch https://github.com/MystryMocha/magpie.git fix/agy-powershell-pipeline
Since v0.1.3701 the bug is silent
v0.1.3701 keeps magpie's models in magpie-settings.json, so settings.json no longer names the gemini provider. With the foreach form agy no longer errors: it starts on its own default backend, and magpie (and the agy effort pick) is skipped without a word.
A/B on Windows 11 with the real magpie.exe v0.1.3701 and agy 1.5.5. The two scripts are the profile block magpie wrote, differing only in that one line, run with -NoProfile, then agy -p 'Reply with exactly the word OK…' --output-format json. Matched against magpie usage --csv:
| Form / shell | agy | magpie's record |
|---|---|---|
foreach / pwsh 7.6 |
OK in 8s |
none |
foreach / PowerShell 5.1 |
OK in 8s |
none |
| pipeline / pwsh 7.6 | OK in 290s |
agy, vertex/gemini-4-carbon, effort medium, 7091 in, 200 |
| pipeline / PowerShell 5.1 | OK in 147s |
agy, vertex/gemini-4-carbon, effort medium, 7084 in, 200 |
(The pipeline runs' time is gemini-4-carbon's first token on Vertex, 146s and 290s here; the same account's interactive agy calls today had 30s to 345s.) magpie.exe agy env through the pipeline returns its 4 lines in 25 to 45 ms, so waiting for it costs nothing.
The change (4 files, +104 −5)
internal/agent/agy.go: the PowerShell body reads& prog agy env | ForEach-Object { ... }instead offoreach ($l in @(& prog agy env)). A pipeline is where PowerShell waits for a GUI program, so it holds with or without amagpie.combeside the app.internal/agent/agy_powershell_windows_test.go(new): builds a-H=windowsguistand-in for magpie that answersagy env(key, URL, model, settings) after 300ms and a console stand-in for agy that prints its key, URL and args, then runs the function in eachpwsh.exe/powershell.exefound.TestAgyFunctionis!windows, so nothing ran this function on Windows before.internal/agent/agy_shell_test.go: the PowerShell string it expects.docs/subsystems/agent-wiring.md: the sentence about PowerShell's function.
Siblings: no other PowerShell code reads a magpie command's output. claude-magpie.cmd runs "%~dp0magpie", which PATHEXT resolves to the console magpie.com first.
What was run
TestAgyPowerShellReadsGUIMagpie(Windows): ok. With the oldforeachline put back by hand: red in both shells,key= url= args=.gofmt -lempty.go vet ./...,GOOS=linux go vet -tags nogui ./...,GOOS=windows go vet ./...,go build,GOOS=linux go build -tags nogui,GOOS=windows go build: ok.- CI on the fork at d4177a272 (https://github.com/MystryMocha/magpie/actions/runs/42975203466): windows-latest and both cli builds ok; ubuntu and macOS
internal/agentok (soTestAgyFunction's new string holds there). ubuntu and macOS are red onTestNoEagerPackageRegexps(internal/gui/webpage_error.go) andTestGatewayKeyLimitCLIonly. Both are red at the base and fixed on main since (d62220d43, 8b2fb6b08 / #6082). With this commit cherry-picked onto 2c644da04, both pass on Windows, as does-run Agy. go test ./internal/agent -count=1 -timeout 45mon Windows: 34 red, none about agy. 33 are red the same by name on a clean ce0cbff61 (#6068, #5950, #5951, #5069, #5070, #5343).TestHermesProfilesFollowMagpieModelis red only in the full run; alone, and beside the new test (3 of 3), it passes on both trees. #6068 lists it.
Not tried: a full go test ./... on Windows; a profile block written by a magpie built from this tree (the A/B above uses the v0.1.3701 block edited by hand).
Still reproducible on magpie 0.1.4026 with agy 1.5.8, and the magpie.com workaround is
confirmed to fix it end to end. Two things to add to the report above.
1. Versions (fresh machine, not the Nov 6 build)
| OS | Windows 11 Home (zh-CN), build 26300, x64 |
| magpie | 0.1.4026 (gateway 127.0.0.1:3425) |
| PowerShell | 7.6.9 (Core) and 5.1.26100.9818 (Desktop) — both profiles carry the block |
| agy | 1.5.8, %LOCALAPPDATA%\agy\bin\agy.exe, on PATH |
| plugins | @magpie-community/[email protected], @magpie-community/[email protected] |
| bundled bun | 1.4.4 |
Same session, PS 7.6.9:
PS> (magpie agy env) -join ' | ' # resolves to magpie.com via PATHEXT
GEMINI_API_KEY=… | GOOGLE_GEMINI_BASE_URL=http://127.0.0.1:3425 | settings=… | model=magpie/antigravity/gemini-4-hydrogen
PS> (& '…\magpie.exe' agy env) -join ' | ' # what the profile block runs
# empty
Get-Command agy -All shows Function agy + Application agy.exe, so the block does run; it is
the agy env read that comes back empty.
2. With agy 1.5.8 the failure does not look like an error at all
The 0.1.3701-era symptom in the report was an explicit
modelProvider is set to "gemini" … GEMINI_API_KEY … is not set. On 1.5.8 nothing is printed —
agy simply lands on its Google-account sign-in:
PS> agy models
Fetching available models...
Error: Please sign in to view available models. Launch the CLI without arguments to sign in.
Interactive agy opens the sign-in flow instead. Combined with the fall-through this reads as
"magpie said I'm connected, but agy still asks me to log in", which is what took me to this issue.
3. The workaround is confirmed end to end
Replacing magpie.exe with magpie.com in the block (both the Test-Path and the invocation):
PS> agy models
gemini-4-carbon-high Gemini 4 Carbon (High)
gemini-4-hydrogen-high Gemini 4 Hydrogen (High)
…
PS> agy -p 'reply with exactly: ok'
error: … Error 403, Message: Antigravity: Verify your account to continue. …
status: PERMISSION_DENIED
and the call reaches magpie's ledger — magpie usage lists it:
Antigravity CLI 0% 0 1 call ≈$0.000
(The 403 is a separate matter: Google wants that account verified. The point here is that the request was served by the gateway, not by agy's own backend — which is exactly the distinction the issue is about.)
4. One more ask, beyond the pipeline fix
When $start.model comes back empty, please fail loudly instead of falling through to
& $real @args.
if (-not $start.model) { Write-Error 'magpie agy env returned nothing — is magpie running?'; return }
The fall-through turns a broken integration into "agy asks you to sign in", and since
agy 1.5.8 prints no GEMINI_API_KEY warning, there is nothing left to point at the profile
block. The pipeline fix removes this instance of it, but the silence is what made it expensive
to find.
The fix is redone on today's main, as two commits on my fork. It replaces the earlier fix/agy-powershell-pipeline branch, which no longer applies.
- Branch: https://github.com/MystryMocha/magpie/tree/fix/agy-powershell-pipeline-v2 (base
ea9f2639b; merges cleanly onto70bd270ae) - https://github.com/MystryMocha/magpie/commit/6b528bb5d (the fix)
- https://github.com/MystryMocha/magpie/commit/a335c1a79 (from an independent review of it)
git fetch https://github.com/MystryMocha/magpie.git fix/agy-powershell-pipeline-v2
What changed since the first fix
Two functions had the bug, not one. Since 5c02be05b, agy runs through @magpie-community/agent-agy while that plugin is installed. Its PowerShell function is generated by launchSpec.function (internal/agent/plugged_launch.go), and that had the same foreach ($l in @(& magpie.exe <id> env)) read. That is likely the block in Sparidae's report, since they have agent-agy 0.2.0. Both generators now use one reader, psStartRead, which reads through & prog <id> env | ForEach-Object { ... }.
On "fail loudly" (Sparidae's ask): magpie <id> env prints nothing when the agent's model isn't one of magpie's (start_cli.go). That is the normal "off magpie" answer, so an empty read can't be treated as an error. A failed env (non-zero exit) is now reported with Write-Warning, and agy then starts as it is. The silent fall-through in this issue came from PowerShell not waiting, which the pipeline fixes.
From the review:
$PSNativeCommandUseErrorActionPreferenceis turned off inside the function. Otherwise a profile with it on and$ErrorActionPreference = 'Stop'would make a failedenvthrow, and agy would never start.- A failed
envdrops anything it printed before failing. - Windows CI now runs
^TestAgyPowerShell. It only compiled the test before.
What was run (Windows 11, go1.26.8)
TestAgyPowerShellReadsGUIMagpie: ok.- It builds a
-H=windowsguistand-in for magpie and runs both functions in pwsh 7.6 and PowerShell 5.1. - Cases: on magpie; off magpie (no warning);
envexiting 3;envprinting then exiting 3; and a strict profile. - It goes red with each part put back by hand:
- the old
foreachread:key= url= args=-p hiin both functions and both shells; - no preference line: the strict case throws;
- no clearing of
$start: the printed-then-failed case is red.
- the old
- It builds a
- gofmt and go vet:
gofmt -lis empty, andgo vetpasses on windows, linux and darwin. go test ./internal/agent -count=1 -timeout 45m: 40 reds, none about agy. A full run at cleanea9f2639bhas the same 40 (#6068, #4115).- Real use:
- A GUI
magpie.exebuilt from the branch:@(& magpie.exe agy env).Countis 0, and throughForEach-Objectit is 4 (72 ms). - Both functions, generated from the branch, each in pwsh 7.6 and PowerShell 5.1 with the variables cleared, ran
agy -p 'Reply with exactly the word OK…' --output-format jsonon agy 1.5.5. - Result, once per commit: four
OKand four rows inmagpie usage --csv(agy, vertex/gemini-4-carbon, effort medium, status 200). The input tokens match agy's own counts.
- A GUI
Not tried:
- A profile block written by this build's own Sync. The functions were generated from the branch and run directly.
- A real failing
env; only the stand-in's exit 3 was run. - agy 1.5.8.
Until this ships, the workaround stands: in the profile block, replace magpie.exe with magpie.com, then run . $PROFILE.
A correction to my comment above. It said "Windows CI now runs ^TestAgyPowerShell" as if pushing the branch had run it. It hadn't: on the fork, test.yml runs only on a push to main, a pull request, or a manual dispatch, so pushing the branch ran nothing. I have since dispatched it by hand.
CI on the fork at a335c1a79: https://github.com/MystryMocha/magpie/actions/runs/43488610626
| Job | Result |
|---|---|
| test (windows-latest) | ok, including the new "Windows PowerShell agy function" step |
| test (ubuntu-latest) | ok: go test ./... and Race |
| cli (linux/amd64, android/arm64) | ok |
| test (macos-latest) | no result: GitHub found no macOS arm64 runner for 15 minutes, on both attempts, so nothing ran |
Also run on Linux (WSL Debian, go1.26.8, -tags nogui) at a335c1a79: the whole internal/agent package ok, TestAgyFunction included. The PowerShell plugin test skips there, as there is no pwsh.
Still not tried: macOS; agy 1.5.8; a profile block written by this build's own Sync; a real failing env.
Still reproduces on the current release, so the PE-subsystem diagnosis above holds.
magpie 0.1.4656 (Windows 11, 2026-11-21 build), PowerShell 5.1.26100 + pwsh 7.7.0:
| call | 5.1 | 7.7 |
|---|---|---|
@(& magpie.exe agy env) |
0 | 0 |
@(& magpie.exe agy env 2>&1) |
0 | 0 |
@(& magpie.exe agy env | %{ $_ }) |
4 | 4 |
@(& magpie.com agy env) |
4 | 4 |
PE headers on this install: magpie.exe = GUI (2), magpie.com = CONSOLE (3).
Note 2>&1 does not help - only a real pipeline (or a .NET RedirectStandardOutput) makes PowerShell wait for the GUI-subsystem process.
Why this is easy to misread as "magpie's config isn't applied at all": in the broken path the agy log shows ChainedAuth: authenticated via keyring plus POST https://daily-cloudcode-pa.googleapis.com/v1internal:fetchAvailableModels, so /model lists only the real Antigravity models (gemini-3.8-flash-high, claude-*, gpt-oss-*) and no magpie/* model. With the env actually injected, the same session logs Custom model config override: ... model_name:"auto/deepseek-v4.1-flash" and never calls fetchAvailableModels, so the picker holds only magpie's models.
A workaround that has been stable here on both 5.1 and 7.x, if relying on a magpie.com sitting beside the app is not enough:
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = 'D:\installed\magpie\magpie.exe'
$psi.Arguments = 'agy env'
$psi.UseShellExecute = $false
$psi.CreateNoWindow = $true
$psi.RedirectStandardOutput = $true
$p = [System.Diagnostics.Process]::Start($psi)
$out = $p.StandardOutput.ReadToEnd()
$p.WaitForExit()
#7314 looks like the same bug with a different root-cause guess.