claude bridge: a caller elsewhere is told its environment is where the tools run

vv_nil on X: a Mac Claude Code through a Linux gateway's Claude
subscription gets Claude Code's own prompt from the gateway's run, which
names Linux and a temp folder, beside the caller's, which names the Mac
and its project. The model spent extra tool calls finding out whether its
environment had changed. A run that can't work in the caller's folder
(the folder isn't on this computer, or Claude Code is in WSL) now has
envElsewhereNote after the caller's instructions: the environment they
describe is where every tool runs, and goes before one given earlier.
Every tool of a run is the caller's, so that is true. The note names no
app or vendor. A run in the caller's own folder, a lean run and a caller
naming no folder are told nothing more. clientWorkDir is split so
clientNamedWorkDir gives the folder named, wherever it is.

Run: go test ./internal/gateway -run
'Claude|Bridge|Subscription|Lean|Vendor|Prompt' (ok, 49s), go vet for
linux and windows, gofmt -l. TestClaudeCallersWorkDirElsewhereIsNotUsed
now checks the note in both runs for a folder not here, a relative one
and a Windows path, and not for no folder; TestClaudeRunsInTheCallersWorkDir
checks a run in the caller's folder isn't told it. With envElsewhere
forced false by hand the first is red (0 of 2 runs).
Not tried: a real Claude Code run from a Mac client through a Linux
gateway, so how many tool calls it saves is not measured.
This commit is contained in:
yetone
2026-10-26 11:01:58 +08:00
parent c2c88e85e9
commit 13f9c7fca1
4 changed files with 81 additions and 18 deletions
@@ -547,6 +547,18 @@ computer. Otherwise, as for a caller elsewhere, a Windows path on a Mac,
or no folder named, the run starts in the shared folder as before. A
Claude Code in WSL always takes the shared folder.
A run in the shared folder for a caller that names a folder
(`Request.envElsewhere`) has Claude Code's prompt give this computer's
platform, OS and the shared folder, and the caller's give another's. A
model told both spent tool calls finding out which is real (vv_nil on X:
a Mac client of a Linux gateway). Since every tool is the caller's,
`renderClaudePrompt` ends the caller's instructions with
`envElsewhereNote`: the environment they describe is where every tool
runs, and goes before one given earlier. It names no app or vendor, since
it goes upstream. A lean run (`claudeLeanPrompt`) and a caller that names
no folder are not told it. Not measured: how many tool calls it saves on
a real run.
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
+27 -7
View File
@@ -408,6 +408,20 @@ func claudeWorkDir() (string, error) {
// absolute path to a folder on this computer (a caller elsewhere). The
// model's own words are not read: what it says is no caller's.
func clientWorkDir(req *Request) string {
dir := clientNamedWorkDir(req)
if dir == "" || !filepath.IsAbs(dir) {
return ""
}
dir = filepath.Clean(dir)
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
return ""
}
return dir
}
// 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}
for _, m := range req.Messages {
if m.Role == "assistant" {
@@ -429,13 +443,6 @@ func clientWorkDir(req *Request) string {
}
}
}
if dir == "" || !filepath.IsAbs(dir) {
return ""
}
dir = filepath.Clean(dir)
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
return ""
}
return dir
}
@@ -732,6 +739,7 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model,
client := work != ""
if !client {
work, err = claudeWorkDir()
req.envElsewhere = clientNamedWorkDir(req) != ""
}
var sessions []string
switch {
@@ -2735,6 +2743,9 @@ func renderClaudePrompt(req *Request) ([]map[string]any, error) {
}
}
text.WriteString(system[pos:])
if req.envElsewhere {
text.WriteString(envElsewhereNote)
}
text.WriteString(toolChoiceAsk(req))
text.WriteString("\n</external_system_instructions>\n\n")
// The caller's instructions are a block of their own, marked for the
@@ -2827,6 +2838,15 @@ func renderClaudePrompt(req *Request) ([]map[string]any, error) {
// toolChoiceAsk is the client's tool choice in words, which Claude Code
// takes no flag for.
// envElsewhereNote follows the caller's instructions when they name a
// working directory the run is not in: Claude Code's own prompt then gives
// the gateway's computer (its platform, OS and a temp folder), the caller's
// gives the computer its tools run on, and a model told both spent tool
// calls finding out which is real (vv_nil on X, a Mac client of a Linux
// gateway). Every tool is the caller's, so the caller's is. It names no
// app and no vendor: it goes to the vendor with the prompt.
const envElsewhereNote = "\n\nThe environment these instructions describe (working directory, platform, OS, shell, git status) is the one every tool runs in. Where an environment given before them differs, go by these instructions; nothing has changed and there is no need to check."
func toolChoiceAsk(req *Request) string {
switch {
case req.ToolChoice == "required":
+37 -11
View File
@@ -20,14 +20,14 @@ import (
// 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" })
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 string) {
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()
if runtime.GOOS == "windows" {
t.Skip("a shell script stands in for Claude Code")
@@ -48,6 +48,7 @@ func claudeWorkDirsTold(t *testing.T, prepare func(work string), system func(hom
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}}}'
@@ -71,7 +72,8 @@ done
}
b, _ := os.ReadFile(filepath.Join(dir, "dirs"))
e, _ := os.ReadFile(filepath.Join(dir, "env"))
return strings.Fields(string(b)), evalSymlinks(work), s, string(e)
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
@@ -142,7 +144,7 @@ func TestClaudeRunsInTheCallersWorkDir(t *testing.T) {
} {
t.Run(c.name, func(t *testing.T) {
var proj, old string
dirs, work, s, env := claudeWorkDirsTold(t, nil, func(home string) 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)
@@ -176,22 +178,46 @@ func TestClaudeRunsInTheCallersWorkDir(t *testing.T) {
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 _, said := range []string{
" - Primary working directory: /no/such/folder/here",
" - Primary working directory: relative/proj",
"<cwd>C:\\Users\\someone\\proj</cwd>",
"no folder named",
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, _, _ := claudeWorkDirsTold(t, nil, func(string) string { return said })
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", said, dirs, 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
}
+5
View File
@@ -161,6 +161,11 @@ type Request struct {
// leanPrompt has the Claude bridge give the caller's instructions as
// the run's system prompt, in place of Claude Code's (ClaudeLeanPrompt).
leanPrompt bool
// envElsewhere: the caller's instructions name a working directory the
// Claude bridge's run is not in (a client on another computer, or in
// 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
Model string
System string