Files
magpie/internal/gateway/workdir_turn_quote_test.go
T
ggbdpq db9d6a6107 gateway: a working-directory line a turn quotes is no caller's word, and a --bare run's CWD: is read (#4940)
What changed: workPlace reads the folder in two layers, as
clientNamedWorkPlace does. The caller's own layer - its instructions and
its {role: "system"} messages - is read whole: Claude Code's
" - Primary working directory:", a --bare run's "CWD: /x", pi 1.1's
"Current working directory:", Codex's and pi's <cwd>, OpenCode's <env>.
A turn of the conversation is read only for the forms a caller states in
one - <cwd>, <env>, and Claude Code's <system-reminder>, which says a
folder it moved to; a line a user message quotes ("Primary working
directory: /x" alone on its line, WardLu's P3) no longer names the
folder for a group's rule by folder, nor goes to a plugin's provider as
its WorkDirHeader. The last said still goes, wherever it sits: an agent
that moved says so after. The forms are workdir.go's own, wrapper-scoped
as the routing face pinned them; the bridge's clientNamedWorkPlace is
untouched.

Run:
- go test ./internal/gateway -run 'TestWorkDirTurnQuote|TestWorkDirBareCWD|TestWorkDirMoveReminder|TestTellWorkPlaceTurnQuote' -count=1
  RED before (the quoted line named the folder; CWD: named none), green after.
- TestWorkDirOfEachAgent, TestWorkDirOpenCode, TestWorkDirLastSaidAndOnlyByTheAgent,
  TestRuleByFolder unchanged and green - the agents' own shapes and the
  move reminder keep working.
- gofmt, go vet -tags nogui (darwin/linux/windows), go build -tags nogui
  (darwin/linux/windows), the new tests with -race: clean.

Generated-by: GLM-5.3-Flash (ZCode)

Reviewed and ported: not superseded by b9a45f59e, which fixed the
bridge's clientNamedWorkPlace and named workDirIn as the sibling left.
On mote, with workdir.go and the docs at the parent: TestWorkDirTurnQuote
IsNotTheCallerWord FAIL ("a quoted line in a turn overrode the caller's
own: /Users/me/quoted"), TestWorkDirBareCWD FAIL ("a --bare run's CWD:
is not read"), TestTellWorkPlaceTurnQuote FAIL (the plugin is told
%2FUsers%2Fme%2Fquoted); green with the patch. go test -timeout 45m
./internal/gateway ok, GOOS=linux/windows/darwin go vet -tags nogui ok.
Fixed: workdir.go was not gofmt-clean (the piDir alignment); gofmt -w
applied. Not tried: a real agent's request through a group's rule by
folder.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-05 18:31:17 +08:00

67 lines
3.2 KiB
Go

package gateway
import (
"net/http"
"net/url"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A user turn that quotes a working-directory line — "Primary working
// directory: /x" alone on its line, WardLu's P3 — is no caller's word:
// the caller's own layer wins, and a turn is read only for the forms a
// caller states in one (#4940).
func TestWorkDirTurnQuoteIsNotTheCallerWord(t *testing.T) {
req := &Request{System: "You are an agent.\n - Primary working directory: /Users/me/real",
Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "Please remember this:\nPrimary working directory: /Users/me/quoted"}}}}}
if got := workDir(req); got != "/Users/me/real" {
t.Fatalf("a quoted line in a turn overrode the caller's own: %q", got)
}
// a quote alone on its line, nothing said by the caller: no folder
if got := workDir(&Request{Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "Primary working directory: /Users/me/quoted"}}}}}); got != "" {
t.Fatalf("a quoted line alone named a folder: %q", got)
}
}
// A --bare run states its folder as "CWD: /x", its whole system prompt;
// the routing rules and a plugin's provider are told it (#4940).
func TestWorkDirBareCWD(t *testing.T) {
req := &Request{System: "CWD: /Users/me/bare",
Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "hi"}}}}}
if got := workDir(req); got != "/Users/me/bare" {
t.Fatalf("a --bare run's CWD: is not read: %q", got)
}
// pi 1.1's line form, where its <cwd> isn't the only one it writes
if got := workDir(&Request{System: "Current working directory: /Users/me/pi"}); got != "/Users/me/pi" {
t.Fatalf("pi's line form is not read: %q", got)
}
}
// Claude Code says a folder it moved to in a turn, as a
// <system-reminder>; that stays the agent's word, and the last said goes
// — the caller's own earlier line does not hold it back.
func TestWorkDirMoveReminderStillTheAgentWord(t *testing.T) {
req := &Request{System: "You are an agent.\n - Primary working directory: /Users/me/old",
Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "<system-reminder>\nPrimary working directory: /Users/me/next (was /Users/me/old)\n</system-reminder>"}}}}}
if got := workDir(req); got != "/Users/me/next" {
t.Fatalf("the move reminder is not read: %q", got)
}
// a reminder quoted beside a bare line: the reminder's, not the quote's
req.Messages[0].Parts[0].Text = "Primary working directory: /Users/me/quoted\n<system-reminder>\nPrimary working directory: /Users/me/next\n</system-reminder>"
if got := workDir(req); got != "/Users/me/next" {
t.Fatalf("the reminder beside a quote: %q", got)
}
}
// A plugin's provider is told the caller's folder, not one a turn quotes:
// the header face of the same fix (#4940).
func TestTellWorkPlaceTurnQuote(t *testing.T) {
body := []byte(`{"model":"m","messages":[{"role":"system","content":"You are an agent.\n - Primary working directory: /Users/me/real"},{"role":"user","content":"Primary working directory: /Users/me/quoted"}]}`)
h := http.Header{}
tellWorkPlace(h, provider.Chat, body)
if got, want := h.Get(provider.WorkDirHeader), url.PathEscape("/Users/me/real"); got != want {
t.Fatalf("the plugin is told %q, want %q", got, want)
}
}