diff --git a/internal/gateway/claude_lean_prompt_test.go b/internal/gateway/claude_lean_prompt_test.go index 85012fc83..21d5c3a84 100644 --- a/internal/gateway/claude_lean_prompt_test.go +++ b/internal/gateway/claude_lean_prompt_test.go @@ -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) { diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index 413735b66..74f1eba77 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -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: diff --git a/internal/gateway/ir.go b/internal/gateway/ir.go index 9dbb90653..17c5eb9ec 100644 --- a/internal/gateway/ir.go +++ b/internal/gateway/ir.go @@ -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