Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
claude bridge: a caller elsewhere is told its environment is where the tools run
yetone13f9c7fca11b
diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.mdindex ab327bd18..42733de66 100644--- a/docs/subsystems/claude-subscription-bridge.md+++ b/docs/subsystems/claude-subscription-bridge.md@@ -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. AClaude 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 ""` loadsno CLAUDE.md, settings or skills from it. Auto memory is off, and`CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1` keeps the repository's status outdiff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.goindex 9c32e4c50..56cbb5150 100644--- a/internal/gateway/claude_subscription.go+++ b/internal/gateway/claude_subscription.go@@ -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 []stringswitch {@@ -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":diff --git a/internal/gateway/claude_workdir_test.go b/internal/gateway/claude_workdir_test.goindex f10fa24de..ab52dc26c 100644--- a/internal/gateway/claude_workdir_test.go+++ b/internal/gateway/claude_workdir_test.go@@ -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(hompwd -P >> ` + dir + `/dirsenv >> ` + dir + `/envwhile read -r line; do+ echo "$line" >> ` + dir + `/toldecho '{"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+}diff --git a/internal/gateway/ir.go b/internal/gateway/ir.goindex 47c798c1a..bb6cf54a2 100644--- a/internal/gateway/ir.go+++ b/internal/gateway/ir.go@@ -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 boolModel stringSystem string