claude bridge: a caller elsewhere whose environment is a message is told the last one given is where tools run

packinggoods on Discord, again after v0.1.2093: a Mac Claude Code
through a Windows magpie's Claude subscription still has its commands
wrapped in PowerShell. The bridge's run is the real Claude Code on the
Windows box, and it puts its own environment (Platform: win32, its
shell, a temp folder) in a <system-reminder> at the start of the run's
first message. 1f03d7546 and 13f9c7fca follow the caller's instructions
with envElsewhereNote: "the environment these instructions describe ...
is the one every tool runs in". But Claude Code 2.1.296 and later, as a
client, don't put their environment in the system prompt: captured from
the real claude 2.1.314 (sandbox HOME, fake key, capture server), it is
a {role: "system"} message after the user's first. So the note pointed at
instructions that describe no environment, and the caller's darwin/zsh
environment came after it as a "Human:" turn, with the gateway's win32
one first.

Now clientNamedWorkPlace says whether the caller's folder was named in a
message (Request.envInTurns). Such a caller gets envTurnsNote, in the
first message's instructions or a lean run's system prompt: of the
environments the conversation gives, the last one is where every tool
runs. The run's own is always first, so the last is the caller's. A
caller naming its folder in its instructions, a run in the caller's own
folder, and a caller naming none are told what they were before. The
note names no app and no vendor.

Test: TestClaudeCallerEnvironmentInAMessageIsTheLastGiven, with the
2.1.314 request shape from a remote address, lean off and on, and a
caller naming its folder in its instructions for the old note. With the
envInTurns case taken out of envNote by hand it is red (told the note 0
times; the old note pointed at the instructions).

Run (mote-run): go test ./internal/gateway -run
'TestClaudeCallerEnvironmentInAMessageIsTheLastGiven|TestClaudeLeanPrompt|WorkDir|RemoteCaller'
ok; go vet ./internal/gateway for darwin, linux (-tags nogui) and
windows ok; gofmt -l clean. go test -timeout 45m ./... was started on
mote and shipped before it finished (owner's ship-at-once rule); its
result goes on an issue if anything is red.
Not tried: a real model turn from a Mac client through a Windows
gateway (no Claude sign-in on xiaomi-gpu), so whether the model now
stops writing PowerShell is not measured; packinggoods's magpie version
is not known.
This commit is contained in:
yetone
2026-10-31 02:34:00 +08:00
parent 2570efb5f7
commit d77da5bff3
4 changed files with 105 additions and 14 deletions
@@ -606,6 +606,18 @@ in its system prompt (`leanSystem`), the named-folder note worded as
`envElsewhereLeanNote` since the environment comes after it there
(`envNote` picks the note).
Claude Code 2.1.296 and later, as a client, give their own environment
the same way: a `{role: "system"}` message after the user's first, not
in the system prompt. For such a caller (`Request.envInTurns`, from
`clientNamedWorkPlace`) "the environment these instructions describe"
pointed at instructions that describe none, while the caller's came
after them as a message (packinggoods again, after v0.1.2093). Its
instructions, or a lean run's system prompt, end with `envTurnsNote`
instead: of the environments the conversation gives, the last one is
where every tool runs. The run's own is always the first, at the start
of its first message, so the last is the caller's. Not measured: whether
a real model on a Windows gateway then stops writing PowerShell.
Nothing else of the folder reaches the run. `--setting-sources ""` loads
no CLAUDE.md, settings or skills from it. Auto memory is off, and
`CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1` keeps the repository's status out
@@ -185,3 +185,53 @@ func TestClaudeLeanPromptSaysWhereToolsRun(t *testing.T) {
t.Fatalf("a caller on this computer: %q", three.system)
}
}
// A caller elsewhere whose environment is a message, as Claude Code 2.1.296
// and later send it (a {role: "system"} message after the user's first; the
// shape below is 2.1.314's, captured), is told that the last environment
// given is where its tools run, in both modes: the named-folder note
// pointed at instructions that describe none, while the gateway's Claude
// Code gave its own environment before them (packinggoods on Discord: a
// Mac client of a Windows gateway had its commands wrapped in
// PowerShell). A caller naming its folder in its instructions is told the
// note it was before.
func TestClaudeCallerEnvironmentInAMessageIsTheLastGiven(t *testing.T) {
const env = `# Environment\nYou have been invoked in the following environment: \n - Primary working directory: /Users/someone/proj\n - Is a git repository: false\n - Platform: darwin\n - Shell: zsh\n - OS Version: Darwin 25.2.0`
body := func(say string) string {
return `{"model":"claude-sonnet-5","max_tokens":100,` +
`"system":[{"type":"text","text":"x-anthropic-billing-header: cc_version=2.1.314.cfe; cc_entrypoint=sdk-cli;"},{"type":"text","text":"You are a Claude agent, built on Anthropic's Claude Agent SDK."},{"type":"text","text":"Rules."}],` +
`"messages":[{"role":"user","content":[{"type":"text","text":"<system-reminder>\nAttribution.\n</system-reminder>\n"},{"type":"text","text":"` + say + `"}]},` +
`{"role":"system","content":[{"type":"text","text":"` + env + `"}]}],` +
`"tools":[{"name":"Bash","description":"Run a command.","input_schema":{"type":"object","properties":{"command":{"type":"string"}}}}]}`
}
const last = "the last one given is the one every tool runs in"
for _, lean := range []bool{false, true} {
h, runs := newLeanHarness(t)
leanSetting(t, lean)
h.from = "192.0.2.7:51000"
h.post(body("one"))
h.post(`{"model":"claude-sonnet-5","max_tokens":100,"system":"Rules.\n - Primary working directory: /no/such/folder/here","messages":[{"role":"user","content":"two"}]}`)
one, two := told(t, runs(), "one"), told(t, runs(), "two")
if lean {
if !strings.HasSuffix(one.system, envTurnsNote) {
t.Fatalf("lean, environment in a message: system %q", one.system)
}
if !strings.HasSuffix(two.system, envElsewhereLeanNote) {
t.Fatalf("lean, environment in the instructions: system %q", two.system)
}
continue
}
i := strings.Index(one.log, "\ntold ")
msg := one.log[i:]
if n, at, end := strings.Count(msg, last), strings.Index(msg, last), strings.Index(msg, "/external_system_instructions"); n != 1 || at > end || strings.Contains(msg, "every tool runs in. Where an environment given before") {
t.Fatalf("environment in a message: told the note %d times, at %d (instructions end at %d): %s", n, at, end, msg)
}
if strings.Index(msg, "Platform: darwin") < strings.Index(msg, last) {
t.Fatalf("the caller's environment is not after the note: %s", msg)
}
if two := two.log[strings.Index(two.log, "\ntold "):]; strings.Contains(two, last) || !strings.Contains(two, "every tool runs in. Where an environment given before") {
t.Fatalf("environment in the instructions: %s", two)
}
}
}
+39 -14
View File
@@ -429,28 +429,37 @@ func clientWorkDir(req *Request) string {
// clientNamedWorkDir is the working directory the caller's instructions
// name, as they write it, wherever it is; "" when they name none.
func clientNamedWorkDir(req *Request) string {
texts := []string{req.System}
dir, _ := clientNamedWorkPlace(req)
return dir
}
// clientNamedWorkPlace is clientNamedWorkDir, and whether the caller
// named it in a message rather than in its instructions: Claude Code
// 2.1.296 and later give their environment as a {role: "system"} message
// after the user's first, so the run is told it in the conversation,
// after the caller's instructions (envTurnsNote).
func clientNamedWorkPlace(req *Request) (dir string, inTurns bool) {
look := func(t string, turn bool) {
for _, m := range clientCwdPattern.FindAllStringSubmatch(t, -1) {
for _, g := range m[1:] {
if g = strings.Trim(strings.TrimSpace(g), "`'\""); g != "" {
dir, inTurns = g, turn
}
}
}
}
look(req.System, false)
for _, m := range req.Messages {
if m.Role == "assistant" {
continue
}
for _, p := range m.Parts {
if p.Kind == Text {
texts = append(texts, p.Text)
look(p.Text, true)
}
}
}
dir := ""
for _, t := range texts {
for _, m := range clientCwdPattern.FindAllStringSubmatch(t, -1) {
for _, g := range m[1:] {
if g = strings.Trim(strings.TrimSpace(g), "`'\""); g != "" {
dir = g
}
}
}
}
return dir
return dir, inTurns
}
var clientCwdPattern = regexp.MustCompile(`(?m)(?:Primary working directory|^\s*(?:-\s*)?Working directory|Current working directory): *([^\r\n]+)$|<cwd>([^<\r\n]+)</cwd>`)
@@ -739,7 +748,9 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model,
client := work != ""
if !client {
work, err = claudeWorkDir()
req.envElsewhere = clientNamedWorkDir(req) != ""
named, inTurns := clientNamedWorkPlace(req)
req.envElsewhere = named != ""
req.envInTurns = inTurns
// a caller on another computer that names no folder: Claude Code's
// environment is this computer's, and the tools run on the
// caller's (packinggoods on Discord: a Mac client of a Windows
@@ -2890,11 +2901,25 @@ const envElsewhereLeanNote = "\n\nThe environment these instructions describe (w
// gateway's shell. It names no app and no vendor.
const envUnknownNote = "\n\nThe environment given elsewhere in this conversation (its working directory, platform, OS and shell) is not the one the tools run on: don't take a platform, shell or path for a tool call from it."
// envTurnsNote is envElsewhereNote, in either mode, for a caller whose
// environment is in its messages, not its instructions (Claude Code
// 2.1.296 and later, req.envInTurns). envElsewhereNote pointed the model
// at instructions that describe no environment, while the caller's own
// came after them as a message, and the gateway's Claude Code gave its
// own before them all (packinggoods on Discord: a Mac client of a
// Windows gateway still had commands wrapped in PowerShell). The run's
// own environment is always the first one given, at the start of its
// first message, and the caller's comes after it, so the last one given
// is the caller's. It names no app and no vendor.
const envTurnsNote = "\n\nOf the environments given in this conversation (working directory, platform, OS, shell, git status), the last one given is the one every tool runs in. Where an earlier one differs, go by the last; nothing has changed and there is no need to check."
// envNote is what a run is told after the caller's instructions of where
// its tools run: "" for a run in the caller's own folder, or for a caller
// on this computer that names none.
func envNote(req *Request) string {
switch {
case req.envElsewhere && req.envInTurns:
return envTurnsNote
case req.envElsewhere && req.leanPrompt:
return envElsewhereLeanNote
case req.envElsewhere:
+4
View File
@@ -181,6 +181,10 @@ type Request struct {
// Windows with Claude Code in WSL), so the run's own prompt describes
// another machine than the one the tools run on (envElsewhereNote).
envElsewhere bool
// envInTurns: that working directory is named in a message (Claude
// Code's environment as a {role: "system"} message), not in the
// caller's instructions (envTurnsNote).
envInTurns bool
// envUnknown: the caller is on another computer (remoteCaller) and its
// instructions name no working directory, so the run's own prompt
// describes a machine no tool runs on, and nothing says which does