Commit Graph
2 Commits
Author SHA1 Message Date
yetone 65e80405b8 gateway: TestPluginToldWhereTheAgentWorks counts a case's tries, not one request (#3347)
Cause: the test said a case's request reached the vendor exactly once.
A lone account whose try fails with a 5xx is asked again after a moment
by design (TestLastOneLeftIsTriedAgain), so one failed first try gives
the vendor two requests and the client a 200. That is what CI hit at
84d902ce2 (run 39906814999, race shard 3): "codex: the vendor got 2
requests". The product sends no duplicate: every request had a try of
its own. Which failure the first try met on CI isn't known, because the
test threw the route away. No gateway log line names it.

Reproduced: the fake vendor answered the codex case's first request
with a 502. The old test failed with the same message, "codex: the vendor
got 2 requests", and the route had try 1 at 502 with again=1 and try 2
at 200. Locally the natural failure never came: -race -count=150 (cpu
1,4), 6 parallel processes x 80, CI's shard 3 three times, and all 4
race shards at once (GOMAXPROCS=2) were all green.

Fix: the requests the case is about are its tries. The test fails when
the vendor got none, or got more requests than the route has tries (a
duplicate), or when a try that answered 200 was tried again. Every
request is checked for the headers the plugin was told and the vendor
never sees. Each retry is logged with its status and error, so the next
CI hit names the failure.
- Broken by hand: an injected 502 now passes and logs "codex: tried
  again after 502 FakeCo: 502 Bad Gateway". An injected duplicate (one
  request counted twice) fails with "codex: the vendor got 2 requests in
  1 tries".

Run at 84d902ce2 plus this change (macOS):
- gofmt -l: clean.
- go vet: darwin, GOOS=linux -tags nogui and GOOS=windows ok.
- go test -tags nogui -race -count=20 -run TestPluginToldWhereTheAgentWorks
  ./internal/gateway: ok.
- go test -tags nogui -timeout 45m ./internal/gateway: ok (358s), log
  kept whole.

After rebasing onto bac5be067 (plugin renewal lock), go vet and the
-race -count=20 run above: ok again. The package run is from before the
rebase.

Not tried: a Linux run, so the first try's CI failure stays unnamed.

Fixes #3347
2026-10-23 14:43:37 +08:00
yetone 1075991f90 gateway: a plugin's fetch is told the folder the agent works in and whether it is a git repository, never seeing magpie's own (plugins #86, Hipple)
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.
2026-10-11 21:51:34 +08:00