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).
86 lines
3.9 KiB
Go
86 lines
3.9 KiB
Go
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")
|
|
})
|
|
}
|
|
}
|