diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md
index ba636294a..9b22b15d6 100644
--- a/docs/subsystems/claude-subscription-bridge.md
+++ b/docs/subsystems/claude-subscription-bridge.md
@@ -1154,13 +1154,28 @@ client's own prompt named another folder. Under a real Claude Code client,
A run therefore starts in the folder the caller names. `clientWorkDir`
reads the system prompt and every message that is not the model's, and
-takes the last folder named. It knows four ways of naming it:
+takes the folder the first of them names (in that text, its last line).
+It knows four ways of naming it:
- Claude Code: "Primary working directory: X";
- Codex: `X`;
- OpenCode: "Working directory: X";
- pi: "Current working directory: X".
+The first text counts, not the last, so a conversation keeps one folder.
+Claude Code says a move (`cd`) as a later "# Environment update" message,
+"Primary working directory: /y (was /x)". Taking that one gave a run
+started again for the conversation (its saved session resumed, a branch,
+a replay) another prompt than its first run: `envTurnsNote` names the
+folder, and a run in the caller's own folder has that folder in Claude
+Code's environment and saves its session in that folder's project, so
+`--resume` found nothing. Either way the prefix after Claude Code's own
+part changed and the whole conversation was written to the cache again
+(EricWvi on X: …/frontend, then …/frontend/src, the cache read flipping
+between under 10% and over 90%). The move still reaches the model in the
+conversation, as the caller said it. Not measured: whether a model told
+the first folder in `envTurnsNote` after a move misreads the update.
+
The folder is taken only when it is an absolute path to a folder on this
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
diff --git a/internal/gateway/claude_folder_moved_test.go b/internal/gateway/claude_folder_moved_test.go
new file mode 100644
index 000000000..1340330d7
--- /dev/null
+++ b/internal/gateway/claude_folder_moved_test.go
@@ -0,0 +1,85 @@
+package gateway
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+// EricWvi on X (Claude Code → a local magpie → a magpie on a server →
+// a Claude subscription): the cache read flipped between under 10% and
+// over 90%, and every drop was a turn whose folder had changed
+// (…/frontend, then …/frontend/src). Claude Code says a move as an
+// "# Environment update" message, "Primary working directory: /y (was
+// /x)", after the environment it gave first. The bridge took the last
+// folder named, so a run started again for the conversation (its saved
+// session resumed, a branch, a replay) was given another system prompt
+// than the conversation's first run: envTurnsNote names the folder, and
+// the caller's own folder on this computer is the one the run works in,
+// which is in Claude Code's own environment and decides which project its
+// session is saved in. Either way the prompt differed right after Claude
+// Code's own part, and the whole conversation was written to the cache
+// again. A conversation keeps the folder it named first.
+func TestClaudeTUIConversationKeepsItsFolderWhenTheCallerMoves(t *testing.T) {
+ env := func(dir string) string {
+ return `# Environment\nYou have been invoked in the following environment: \n - Primary working directory: ` + dir + `\n - Is a git repository: true\n - Platform: darwin\n - Shell: zsh\n - OS Version: Darwin 25.2.0`
+ }
+ moved := func(dir, was string) string {
+ return `# Environment update\n - Primary working directory: ` + dir + ` (was ` + was + `)`
+ }
+ body := func(msgs ...string) string {
+ return `{"model":"claude-sonnet-5","max_tokens":100,` +
+ `"system":[{"type":"text","text":"x-anthropic-billing-header: cc_version=2.1.316.cfe; cc_entrypoint=cli;"},{"type":"text","text":"You are Claude Code, Anthropic's official CLI for Claude."},{"type":"text","text":"Rules."}],` +
+ `"messages":[` + strings.Join(msgs, ",") + `],` +
+ `"tools":[{"name":"Bash","description":"Run a command.","input_schema":{"type":"object","properties":{"command":{"type":"string"}}}}]}`
+ }
+ user := func(s string) string { return `{"role":"user","content":[{"type":"text","text":"` + s + `"}]}` }
+ system := func(s string) string { return `{"role":"system","content":[{"type":"text","text":"` + s + `"}]}` }
+ for _, c := range []struct {
+ name string
+ here bool // the caller's folders are on this computer
+ }{{"a caller elsewhere", false}, {"a caller on this computer", true}} {
+ t.Run(c.name, func(t *testing.T) {
+ h, appended := appendHarness(t, true)
+ root := "/Users/eric/work/app"
+ if c.here {
+ root = filepath.Join(t.TempDir(), "app")
+ }
+ frontend, src := filepath.Join(root, "frontend"), filepath.Join(root, "frontend", "src")
+ if c.here {
+ if err := os.MkdirAll(src, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ }
+ first := []string{user("a1"), system(env(frontend))}
+ h.post(body(first...))
+ var sid, file string
+ for pid, log := range h.runs() {
+ sid, file = "s"+pid, appended()[log]
+ }
+ // the conversation's run is let go and shelved, as one is past
+ // idleMost others or idleLongest
+ for i := range idleMost {
+ h.ask(fmt.Sprintf("rules %d", i), "x")
+ }
+ eventually(t, "the other runs still going", func() bool { return len(h.s.subscription.idle) == idleMost })
+ before := h.runs()
+ h.post(body(append(first, `{"role":"assistant","content":"ok"}`, system(moved(src, frontend)), user("a2"))...))
+ for log, again := range appended() {
+ if _, ok := before[logPID(h, log)]; ok {
+ continue
+ }
+ if !strings.Contains(log, "--resume "+sid) {
+ t.Fatalf("the turn after the move did not resume the conversation's session %s, so it was told the whole conversation again:\n%s", sid, log)
+ }
+ if again != file {
+ t.Fatalf("the turn after the move was given another system prompt than the conversation's first run, a prefix the cache has never seen:\nfirst %q\nthen %q", file, again)
+ }
+ return
+ }
+ t.Fatal("the turn after the move started no run")
+ })
+ }
+}
diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go
index ee2432b74..a767ff068 100644
--- a/internal/gateway/claude_subscription.go
+++ b/internal/gateway/claude_subscription.go
@@ -519,8 +519,8 @@ func claudeWorkDir() (string, error) {
return dir, nil
}
-// clientWorkDir is the working directory the caller names, the last one
-// it names, as Claude Code ("Primary working directory:", or just "CWD:"
+// clientWorkDir is the working directory the caller names, the first one
+// it names (clientNamedWorkPlace), as Claude Code ("Primary working directory:", or just "CWD:"
// on a --bare run), Codex (), OpenCode ("Working directory:") and pi
// ("Current working directory:") write it; "" when they name none, or one
// that isn't an absolute path to a folder on this computer (a caller
@@ -564,8 +564,26 @@ func clientNamedWorkDir(req *Request) string {
// (Codex's is one of the user's, OpenCode's a
// later message), and a line quoted from Claude Code's own words — its
// instructions or a --bare run's "CWD:" — is never one of them.
+//
+// Of the texts that name a folder, the first goes: the one the
+// conversation started in (in that text, its last line). A caller that
+// moves says so in a message after (Claude
+// Code's "# Environment update", "Primary working directory: /y (was
+// /x)"), and taking that one gave a run started again for the
+// conversation (its saved session resumed, a branch, a replay) another
+// system prompt than the conversation's first run: envTurnsNote names the
+// folder, and a run in the caller's own folder has it in Claude Code's own
+// environment and saves its session in that folder's project, so the
+// session wasn't found. The prompt differed right after Claude Code's own
+// part, and the whole conversation was written to the cache again
+// (EricWvi on X: …/frontend, then …/frontend/src). The run has no tools
+// of its own; the move reaches the model in the conversation as the
+// caller said it.
func clientNamedWorkPlace(req *Request) (dir string, inTurns bool) {
look := func(t string, turn bool, re *regexp.Regexp) {
+ if dir != "" {
+ return
+ }
for _, m := range re.FindAllStringSubmatch(t, -1) {
for _, g := range m[1:] {
g = strings.Trim(strings.TrimSpace(g), "`'\"")
diff --git a/internal/gateway/claude_workdir_test.go b/internal/gateway/claude_workdir_test.go
index 114c8601f..b24f47cbd 100644
--- a/internal/gateway/claude_workdir_test.go
+++ b/internal/gateway/claude_workdir_test.go
@@ -158,7 +158,7 @@ func TestClaudeWorkDirSymlinkIsAvoided(t *testing.T) {
// directory, and a model told magpie's own folder wrote the caller's files
// into it (a pelican.html left in magpie-claude-work), through tools that
// run in the caller's. Each client's way of saying it is read, the last
-// said counting. The caller's folder keeps sessions of its own, which are
+// said in the first text that says one counting. The caller's folder keeps sessions of its own, which are
// never swept, and its repository's status is not added to the prompt.
func TestClaudeRunsInTheCallersWorkDir(t *testing.T) {
for _, c := range []struct{ name, said string }{