claude bridge: a caller's environment in a message is named by its folder, not as the last one given

packinggoods on Discord, a third time: a Mac Claude Code through a
Windows magpie's Claude subscription (remote-magpie/claude/
claude-opus-5-5) is still told to use PowerShell. The model sees the
Mac client's environment (darwin, zsh, /Users/...) and then a later one
from the Windows host (win32, PowerShell, C:\...\magpie-claude-work,
Windows 11), and follows the later one.

d77da5bff told such a caller's run "of the environments given in this
conversation, the last one given is the one every tool runs in", on the
premise that the run's own environment always comes first. It doesn't.
Captured from the real claude 2.1.316 with --model claude-opus-5-5
(sandbox HOME and CLAUDE_CONFIG_DIR, fake key, a local capture server
that answers 400): the run's own environment is a {role: "system"}
message after the run's first user message, so after everything magpie
tells the run, the caller's environment included. The same holds with
--system-prompt-file and with --exclude-dynamic-system-prompt-sections
(that flag moves only the memory section into the first message).
Claude Code has no flag or variable that leaves the section out
(--help, and the strings of the 2.1.316 binary:
CLAUDE_CODE_FORCE_MID_CONVERSATION_SYSTEM only forces the message
form), and 1f03d7546 saw it in the first message with 2.1.312, so its
place depends on the version and the model. The note pointed the model
straight at the Windows host.

Now envTurnsNote names the caller's environment by the working
directory it gives (Request.envDir, from clientNamedWorkPlace): "the
one whose working directory is <dir> is the one every tool runs in. Any
other one describes a computer no tool runs on: don't take a platform,
shell or path for a tool call from it." It holds whichever comes first.
It names no app and no vendor; the folder is the caller's own, already
in the conversation. The other notes (caller naming its folder in its
instructions, caller naming none) don't depend on order and are
unchanged. Nothing else of the request changes: no flag, header, env or
Claude Code prompt text.

Test: TestClaudeCallerEnvironmentInAMessageIsNamedByItsFolder (was
...IsTheLastGiven), lean off and on, now requires the folder-named note
and no "the last one given". On origin/main's claude_subscription.go
and ir.go it is red (mote-run, FAIL).

Run (mote-run, at this change on 0379f2610; rebased onto 4f00a32ff, which touches only GUI budgets): go test -timeout 45m -count=1
./internal/gateway ok (636s); go vet ./internal/gateway for darwin,
windows and linux (-tags nogui) ok; gofmt -l clean.
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. The full ./... suite was not
run (mote had just rebooted); only internal/gateway changed.
This commit is contained in:
yetone
2026-10-31 19:25:17 +08:00
parent 4f00a32ffc
commit 8680ffa419
3 changed files with 29 additions and 19 deletions
+10 -9
View File
@@ -188,14 +188,15 @@ func TestClaudeLeanPromptSaysWhereToolsRun(t *testing.T) {
// 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
// shape below is 2.1.314's, captured), is told its environment is the one
// whose working directory it names, in both modes. The run's own Claude
// Code gives its environment the same way, after the run's first message
// (2.1.316 with claude-opus-5-5, captured), so after the caller's: the
// earlier "the last one given" pointed the model at the gateway's computer
// (packinggoods on Discord: a Mac client of a Windows gateway told to use
// PowerShell). A caller naming its folder in its instructions is told the
// note it was before.
func TestClaudeCallerEnvironmentInAMessageIsTheLastGiven(t *testing.T) {
func TestClaudeCallerEnvironmentInAMessageIsNamedByItsFolder(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,` +
@@ -204,7 +205,7 @@ func TestClaudeCallerEnvironmentInAMessageIsTheLastGiven(t *testing.T) {
`{"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"
const last = "the one whose working directory is /Users/someone/proj is the one every tool runs in"
for _, lean := range []bool{false, true} {
h, runs := newLeanHarness(t)
leanSetting(t, lean)
@@ -214,7 +215,7 @@ func TestClaudeCallerEnvironmentInAMessageIsTheLastGiven(t *testing.T) {
one, two := told(t, runs(), "turn-one"), told(t, runs(), "turn-two")
if lean {
if !strings.HasSuffix(one.system, envTurnsNote) {
if !strings.HasSuffix(one.system, envTurnsNote("/Users/someone/proj")) || !strings.Contains(one.system, last) || strings.Contains(one.system, "the last one given") {
t.Fatalf("lean, environment in a message: system %q", one.system)
}
if !strings.HasSuffix(two.system, envElsewhereLeanNote) {
@@ -224,7 +225,7 @@ func TestClaudeCallerEnvironmentInAMessageIsTheLastGiven(t *testing.T) {
}
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") {
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") || strings.Contains(msg, "the last one given") {
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) {
+17 -10
View File
@@ -793,6 +793,7 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model,
named, inTurns := clientNamedWorkPlace(req)
req.envElsewhere = named != ""
req.envInTurns = inTurns
req.envDir = named
// 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
@@ -3214,15 +3215,21 @@ const envUnknownNote = "\n\nThe environment given elsewhere in this conversation
// 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."
// 2.1.296 and later, req.envInTurns), dir the working directory it names.
// envElsewhereNote pointed the model at instructions that describe no
// environment. d77da5bff then said the last environment given is the
// caller's, taking the run's own to come first; but the run's Claude Code
// gives its own as a {role: "system"} message after the run's first
// message (2.1.316, claude-opus-5-5), so after the caller's, and the model
// was pointed at the gateway's (packinggoods on Discord: a Mac client of a
// Windows gateway told to use PowerShell). Which comes last depends on
// Claude Code's version and the model, so the caller's environment is
// named by its working directory, never by its place. It names no app and
// no vendor.
func envTurnsNote(dir string) string {
return "\n\nOf the environments given in this conversation (working directory, platform, OS, shell, git status), the one whose working directory is " + dir +
" is the one every tool runs in. Any other one describes a computer no tool runs on: don't take a platform, shell or path for a tool call from it. 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
@@ -3230,7 +3237,7 @@ const envTurnsNote = "\n\nOf the environments given in this conversation (workin
func envNote(req *Request) string {
switch {
case req.envElsewhere && req.envInTurns:
return envTurnsNote
return envTurnsNote(req.envDir)
case req.envElsewhere && req.leanPrompt:
return envElsewhereLeanNote
case req.envElsewhere:
+2
View File
@@ -185,6 +185,8 @@ type Request struct {
// Code's environment as a {role: "system"} message), not in the
// caller's instructions (envTurnsNote).
envInTurns bool
// envDir: that working directory, as the caller names it.
envDir string
// 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