gateway: a Claude conversation keeps the folder it named first when its caller moves
EricWvi on X (Claude Code -> local magpie -> a magpie on a server -> a
Claude subscription): the cache read flipped between under 10% and over
90%, each drop on a turn whose folder had changed (.../frontend, then
.../frontend/src). Claude Code 2.1.316 says a cd as a later
"# Environment update" message, "Primary working directory: /y (was /x)".
clientNamedWorkPlace took the last folder named, so a run started again
for the conversation (saved session resumed, branch, replay) got a
different prefix than its first run:
- a caller elsewhere: envTurnsNote names the folder, so append.md changed;
- a caller on this computer: the run worked in the new folder, whose
session project doesn't hold the saved session, so --resume was lost
and the whole conversation was told again.
Either way the whole conversation was written to the cache again.
Now the first text that names a folder decides it (within that text, its
last line, so "said twice" in one system prompt keeps its meaning). The
move still reaches the model in the conversation as the caller said it.
No text is added to the prompt; the note's wording is unchanged.
The reporter's per-turn flips on a v0.1.2879 server are the earlier
safeguards restart (git_state.cwd in safeguardsKept), fixed by 59c130603
in v0.1.2882 while the tap is live.
Run (mote, via mote-run, at this tree):
- go test -tags nogui ./internal/gateway -run 'TestClaude|TestWorkDir'
-count=1: ok (114.9s)
- go test -tags nogui ./internal/gateway -count=1 -timeout 45m: ok (778.6s, mote at load ~45)
- go vet -tags nogui ./... ; GOOS=linux and GOOS=windows go vet
./internal/gateway: ok (cgo deprecation warnings only)
- gofmt -l internal/gateway: clean
Broken by hand: without the `if dir != ""` guard,
TestClaudeTUIConversationKeepsItsFolderWhenTheCallerMoves is red in both
subtests (caller elsewhere: append.md names .../frontend/src; caller on
this computer: --resume of another session, not s<pid>).
Not tried: a real Claude Code cd against a real remote magpie and
subscription; whether a model told the first folder in envTurnsNote after
a move misreads the environment update (not measured).
This commit is contained in:
@@ -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: `<cwd>X</cwd>`;
|
||||
- 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
|
||||
|
||||
@@ -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")
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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 (<cwd>), 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 <environment_context> is one of the user's, OpenCode's <env> 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), "`'\"")
|
||||
|
||||
@@ -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 }{
|
||||
|
||||
Reference in New Issue
Block a user