packinggoods on Discord: magpie on a Windows machine, the agent on a Mac.
Every shell command came wrapped in PowerShell. The model's prompt held
Claude Code's own environment section from the bridge's run: "Primary
working directory: C:\Users\...\Temp\magpie-claude-work, Platform: win32,
Shell: PowerShell". That describes the gateway, and no tool of the run
runs there: every tool is the caller's.
Claude Code has no option to leave that section out. With the real
claude 2.1.312 against a capture server (sandbox HOME, fake key), it is a
<system-reminder> in the run's first message, before magpie's text, with
the default prompt and with --system-prompt-file alike; the SDK's
excludeDynamicSections only moves it there. claude.exe on xiaomi-gpu gives
the same section with the Windows platform, temp folder and OS version.
13f9c7fca already told a run that the caller's environment wins, but only
when the caller's instructions name a folder in a form clientNamedWorkDir
reads, and never a lean run. Now:
- serveSubscription marks a request not from this computer
(Request.remoteCaller = !local(r)). A run in magpie's folder for such a
caller that names no folder (envUnknown) gets envUnknownNote after its
instructions: the environment given elsewhere is not where the tools
run, so no platform, shell or path is taken from it. A caller with no
system prompt gets the instructions block for the note alone.
- a lean run gets the note in its system prompt (leanSystem), the named-
folder one worded envElsewhereLeanNote since the environment comes after
it there. startRun now picks the folder before writing system.md.
- A caller on this computer that names no folder, a run in the caller's
own folder, and a named-folder caller in the non-lean run are told what
they were before, so their cached prompts are unchanged.
The notes name no app and no vendor. Nothing else of the request changes:
no flag, header, env or Claude Code prompt text. Gateway facts beyond the
environment section don't reach the prompt: --setting-sources "" (no
CLAUDE.md), CLAUDE_CODE_DISABLE_AUTO_MEMORY=1 and
CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1 were already set.
Tests: TestClaudeRemoteCallerNamingNoFolderIsToldToolsAreElsewhere (IPv4
and IPv6 remote, with and without a system prompt, loopback, remote
naming a folder) and TestClaudeLeanPromptSaysWhereToolsRun. Both are red
on origin/main's claude_subscription.go and ir.go (0 of 2 runs told; lean
system prompt "Rules."). TestClaudeLeanPromptIsTheRunsSystemPrompt and
TestClaudeClientCacheAbsentUnchanged now send from loopback, which they
were about all along (httptest's address is another computer's).
Run (mote-run): go test ./internal/gateway -run
'Claude|Bridge|Subscription|Lean|Prompt|WorkDir|Resume' ok;
go test -timeout 45m ./internal/gateway: ok (532s); go vet ./internal/gateway
for darwin, linux (-tags nogui) and windows ok; gofmt -l clean. win-run on
xiaomi-gpu: the touched tests ok (the script-driven ones skip on Windows).
Real claude 2.1.312 fed the new first message: the note follows the
environment reminder inside external_system_instructions.
Not tried: a real model turn from a Mac client through a Windows
gateway, so whether the model now stops wrapping commands in PowerShell
is not measured; packinggoods's client and magpie version are not known.
269 lines
11 KiB
Go
269 lines
11 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// claudeWorkDirs runs two Claude subscription requests through a script
|
|
// standing in for Claude Code, with a temp folder of the test's own and a
|
|
// home kept in git, and gives the folders the two runs worked in and the
|
|
// folder they should share.
|
|
func claudeWorkDirs(t *testing.T, prepare func(work string)) (dirs []string, work string) {
|
|
t.Helper()
|
|
dirs, work, _, _, _ = claudeWorkDirsTold(t, prepare, func(string) string { return "rules" })
|
|
return dirs, work
|
|
}
|
|
|
|
// claudeWorkDirsTold is claudeWorkDirs with the caller's instructions
|
|
// system gives, from the test's home; it gives the gateway too, and the
|
|
// environment the runs had.
|
|
func claudeWorkDirsTold(t *testing.T, prepare func(work string), system func(home string) string) (dirs []string, work string, s *Server, env, told string) {
|
|
t.Helper()
|
|
return claudeWorkDirsFrom(t, "", prepare, system)
|
|
}
|
|
|
|
// claudeWorkDirsFrom is claudeWorkDirsTold with the requests from the
|
|
// address from ("" for httptest's, another computer's).
|
|
func claudeWorkDirsFrom(t *testing.T, from string, prepare func(work string), system func(home string) string) (dirs []string, work string, s *Server, env, told string) {
|
|
t.Helper()
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("a shell script stands in for Claude Code")
|
|
}
|
|
home := t.TempDir()
|
|
if err := os.Mkdir(filepath.Join(home, ".git"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testenv.SetHome(t, home)
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("TMPDIR", t.TempDir())
|
|
work = filepath.Join(os.TempDir(), claudeWorkName())
|
|
if prepare != nil {
|
|
prepare(work)
|
|
}
|
|
dir := t.TempDir()
|
|
script := `#!/bin/sh
|
|
pwd -P >> ` + dir + `/dirs
|
|
env >> ` + dir + `/env
|
|
while read -r line; do
|
|
echo "$line" >> ` + dir + `/told
|
|
echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-sonnet-5","usage":{"input_tokens":1}}}}'
|
|
echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ok"}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_stop"}}'
|
|
echo '{"type":"result","subtype":"success","is_error":false,"result":""}'
|
|
done
|
|
`
|
|
testenv.Program(t, filepath.Join(dir, "claude"), script)
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
|
|
s = New()
|
|
p := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "u"}}
|
|
rules, _ := json.Marshal(system(home))
|
|
for _, said := range []string{"one", "two"} {
|
|
body := `{"model":"claude-sonnet-5","max_tokens":100,"system":` + string(rules) + `,"messages":[{"role":"user","content":"` + said + `"}]}`
|
|
rec := httptest.NewRecorder()
|
|
var u Usage
|
|
r := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body))
|
|
if from != "" {
|
|
r.RemoteAddr = from
|
|
}
|
|
if code, msg := s.serveClaudeSubscription(rec, r, provider.Anthropic, p, "claude-sonnet-5", []byte(body), &u); code != 200 {
|
|
t.Fatalf("%d %s", code, msg)
|
|
}
|
|
}
|
|
b, _ := os.ReadFile(filepath.Join(dir, "dirs"))
|
|
e, _ := os.ReadFile(filepath.Join(dir, "env"))
|
|
m, _ := os.ReadFile(filepath.Join(dir, "told"))
|
|
return strings.Fields(string(b)), evalSymlinks(work), s, string(e), string(m)
|
|
}
|
|
|
|
// Every run works in the same folder: Claude Code puts its working
|
|
// directory in the system prompt, ahead of the conversation, so a folder of
|
|
// each run's own left nothing past Claude Code's own part of the prompt to
|
|
// be read from the cache. The folder is the user's own, made for them
|
|
// alone, in the temp folder: not under a home kept in git, whose status
|
|
// Claude Code would put in the prompt.
|
|
func TestClaudeRunsShareAWorkDir(t *testing.T) {
|
|
dirs, work := claudeWorkDirs(t, nil)
|
|
if len(dirs) != 2 || dirs[0] != work || dirs[1] != work {
|
|
t.Fatalf("working directories: %q, want %s", dirs, work)
|
|
}
|
|
if home := evalSymlinks(os.Getenv("HOME")); strings.HasPrefix(work, home+string(filepath.Separator)) || !strings.HasPrefix(work, evalSymlinks(os.TempDir())+string(filepath.Separator)) {
|
|
t.Fatalf("work folder %s: in the home %s, or not in the temp folder", work, home)
|
|
}
|
|
fi, err := os.Stat(work)
|
|
if err != nil || fi.Mode().Perm() != 0o700 {
|
|
t.Fatalf("work folder: %v %v", fi.Mode(), err)
|
|
}
|
|
}
|
|
|
|
// A work folder others can write to is not worked in, as anything put
|
|
// there (a CLAUDE.md) would be read by every run: each run works in a
|
|
// folder of its own instead.
|
|
func TestClaudeWorkDirOthersCanWriteIsAvoided(t *testing.T) {
|
|
dirs, work := claudeWorkDirs(t, func(work string) {
|
|
if err := os.MkdirAll(work, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Chmod(work, 0o777)
|
|
})
|
|
if len(dirs) != 2 || dirs[0] == work || dirs[1] == work || dirs[0] == dirs[1] {
|
|
t.Fatalf("working directories: %q, shared %s", dirs, work)
|
|
}
|
|
}
|
|
|
|
// A symlink where the work folder goes, to a folder anyone could have
|
|
// filled, is not followed: each run works in a folder of its own.
|
|
func TestClaudeWorkDirSymlinkIsAvoided(t *testing.T) {
|
|
var target string
|
|
dirs, work := claudeWorkDirs(t, func(work string) {
|
|
target = t.TempDir()
|
|
if err := os.Symlink(target, work); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
target = evalSymlinks(target)
|
|
if len(dirs) != 2 || dirs[0] == target || dirs[1] == target || dirs[0] == dirs[1] {
|
|
t.Fatalf("working directories: %q, linked %s (%s)", dirs, target, work)
|
|
}
|
|
}
|
|
|
|
// A caller that says which folder it works in has Claude Code run there:
|
|
// Claude Code tells its model the folder it runs in as the working
|
|
// directory, and a model told magpie's own folder wrote the caller's files
|
|
// into it (a pelican.html left in magpie-claude-work), through tools that
|
|
// run in the caller's. Each client's way of saying it is read, the last
|
|
// said counting. The caller's folder keeps sessions of its own, which are
|
|
// never swept, and its repository's status is not added to the prompt.
|
|
func TestClaudeRunsInTheCallersWorkDir(t *testing.T) {
|
|
for _, c := range []struct{ name, said string }{
|
|
{"Claude Code", "# Environment\nYou have been invoked in the following environment: \n - Primary working directory: %s\n - Is a git repository: true\n"},
|
|
{"Codex", "<environment_context>\n <cwd>%s</cwd>\n <shell>zsh</shell>\n</environment_context>"},
|
|
{"OpenCode", "<env>\n Working directory: %s\n Is directory a git repo: yes\n</env>"},
|
|
{"pi", "Current date: 2026-10-26\nCurrent working directory: %s"},
|
|
{"said twice", " - Primary working directory: /nowhere/at/all\nlater:\n - Primary working directory: %s"},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
var proj, old string
|
|
dirs, work, s, env, told := claudeWorkDirsTold(t, nil, func(home string) string {
|
|
proj = filepath.Join(home, "proj")
|
|
if err := os.MkdirAll(proj, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// a session of the user's own in that folder, older than
|
|
// any the bridge keeps
|
|
sessions := claudeSessions("", evalSymlinks(proj))
|
|
old = filepath.Join(sessions[0], "mine.jsonl")
|
|
if err := os.MkdirAll(sessions[0], 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(old, []byte("{}\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
then := time.Now().Add(-48 * time.Hour)
|
|
_ = os.Chtimes(old, then, then)
|
|
return strings.ReplaceAll(c.said, "%s", proj)
|
|
})
|
|
proj = evalSymlinks(proj)
|
|
if len(dirs) != 2 || dirs[0] != proj || dirs[1] != proj {
|
|
t.Fatalf("working directories: %q, want the caller's %s (not %s)", dirs, proj, work)
|
|
}
|
|
for _, d := range claudeSessions("", proj) {
|
|
if _, swept := s.subscription.swept.Load(d); swept {
|
|
t.Fatalf("the caller's sessions in %s were swept", d)
|
|
}
|
|
}
|
|
if _, err := os.Stat(old); err != nil {
|
|
t.Fatalf("the user's own session: %v", err)
|
|
}
|
|
if !strings.Contains(env, "CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1") {
|
|
t.Fatal("Claude Code would add the repository's status to the prompt")
|
|
}
|
|
if strings.Contains(told, "every tool runs in") {
|
|
t.Fatalf("a run in the caller's own folder was told its environment is elsewhere: %s", told)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A folder the caller names that isn't one here (a caller on another
|
|
// computer), or isn't absolute, leaves the runs in magpie's own folder.
|
|
// Claude Code's prompt then gives this computer's platform and folder, and
|
|
// the caller's another's, so the run is told after the caller's
|
|
// instructions that theirs is where the tools run (vv_nil on X: a Mac
|
|
// client of a Linux gateway spent tool calls finding out which is real).
|
|
// A caller that names no folder is told nothing more.
|
|
func TestClaudeCallersWorkDirElsewhereIsNotUsed(t *testing.T) {
|
|
for _, c := range []struct {
|
|
said string
|
|
elsewhere bool
|
|
}{
|
|
{" - Primary working directory: /no/such/folder/here", true},
|
|
{" - Primary working directory: relative/proj", true},
|
|
{"<cwd>C:\\Users\\someone\\proj</cwd>", true},
|
|
{"no folder named", false},
|
|
} {
|
|
dirs, work, _, _, told := claudeWorkDirsTold(t, nil, func(string) string { return c.said })
|
|
if len(dirs) != 2 || dirs[0] != work || dirs[1] != work {
|
|
t.Fatalf("%q: working directories %q, want %s", c.said, dirs, work)
|
|
}
|
|
if got := strings.Count(told, "every tool runs in"); got != 2*btoi(c.elsewhere) {
|
|
t.Fatalf("%q: told the environment is elsewhere in %d of 2 runs, want elsewhere=%v: %s", c.said, got, c.elsewhere, told)
|
|
}
|
|
if c.elsewhere && strings.Index(told, "every tool runs in") > strings.Index(told, "/external_system_instructions") {
|
|
t.Fatalf("%q: the note is not with the caller's instructions: %s", c.said, told)
|
|
}
|
|
}
|
|
}
|
|
|
|
func btoi(b bool) int {
|
|
if b {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// A caller on another computer that names no folder (packinggoods on
|
|
// Discord: a Mac client of a Windows gateway) is told after its
|
|
// instructions that the environment Claude Code gives, the gateway's
|
|
// platform, shell and temp folder, is not where its tools run: told only
|
|
// that one, the model wrapped every command in the gateway's PowerShell.
|
|
// It is told so with no instructions of its own too. A caller on this
|
|
// computer naming none is told nothing more, and one naming a folder
|
|
// elsewhere is told the note for that, not this one.
|
|
func TestClaudeRemoteCallerNamingNoFolderIsToldToolsAreElsewhere(t *testing.T) {
|
|
for _, c := range []struct {
|
|
from, said string
|
|
unknown bool
|
|
}{
|
|
{"192.0.2.7:51000", "You are a coding agent.", true},
|
|
{"[2001:db8::7]:51000", "", true},
|
|
{"127.0.0.1:51000", "You are a coding agent.", false},
|
|
{"192.0.2.7:51000", " - Primary working directory: /no/such/folder/here", false},
|
|
} {
|
|
dirs, work, _, _, told := claudeWorkDirsFrom(t, c.from, nil, func(string) string { return c.said })
|
|
if len(dirs) != 2 || dirs[0] != work || dirs[1] != work {
|
|
t.Fatalf("%s %q: working directories %q, want %s", c.from, c.said, dirs, work)
|
|
}
|
|
if got := strings.Count(told, "not the one the tools run on"); got != 2*btoi(c.unknown) {
|
|
t.Fatalf("%s %q: told the tools run elsewhere in %d of 2 runs, want %v: %s", c.from, c.said, got, c.unknown, told)
|
|
}
|
|
if c.unknown && strings.Index(told, "not the one the tools run on") > strings.Index(told, "/external_system_instructions") {
|
|
t.Fatalf("%s %q: the note is not with the caller's instructions: %s", c.from, c.said, told)
|
|
}
|
|
if c.unknown && strings.Contains(told, "every tool runs in") {
|
|
t.Fatalf("%s %q: told the named-folder note too: %s", c.from, c.said, told)
|
|
}
|
|
}
|
|
}
|