gateway: a pi 1.x run of the Claude bridge starts in pi's own folder

fysly on X: a Pi turn on a Claude subscription told the model two working
directories, Pi's own and magpie's shared work folder under /private/var/
folders (magpie-claude-work-<uid>), where the bridge's Claude Code ran.

Pi 1.x states its folder as a system prompt section with the path on a
line of its own, "<cwd>\n/x\n</cwd>" (buildSystemPromptSections, joined
by getSystemMessageText). clientSaidWorkDir and clientTurnWorkDir read
<cwd> only on one line, as Codex writes it, so clientWorkDir found no
folder and the run started in the shared folder: Claude Code's own
environment then named that folder beside Pi's <cwd>. The folder rules'
reader (workDirIn) already allowed whitespace inside <cwd>; the bridge's
now does too.

Sibling in the same reader: workDirIn decodes the path's &, < and > as
entities (Codex escapes them in <cwd>, Claude Code < and >), the bridge's
did not, so such a folder failed os.Stat and its run went to the shared
folder too. Now decoded the same way.

No text of Claude Code's prompt changes: only the folder it runs in, the
existing clientWorkDir path for Claude Code, Codex and OpenCode callers
(--setting-sources "", no auto memory, CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS).

Run (mote, at 0caa09b3d plus this change): gofmt -l internal/ clean;
go vet ./internal/gateway/ for darwin, GOOS=linux and GOOS=windows ok;
go test -count=1 -timeout 45m ./internal/gateway/ ok (894.8s).
Rebased onto 3e38e4ba2 (codexcat, antigravity, codeassist): go vet and
go test -run 'WorkDir|WorkPlace|Claude' ./internal/gateway/ ok (150.5s)
at the pushed tree.
Broken by hand: with origin/main's claude_subscription.go and the new
tests, TestClaudeRunsInTheCallersWorkDir's "pi 1.x" and "Codex, a folder
written as entities" are red (both runs in magpie-claude-work-<uid>);
green with the fix.
Real pi: pi 1.4.0 in a sandbox HOME/PI_CODING_AGENT_DIR, pointed at a
local capture server; its real /v1/messages body ends
"<cwd>\n<proj>\n</cwd>". That body, sent through the bridge with a fake
claude, ran in <proj>, not the shared folder.
Not tried: a real Claude subscription turn from pi (no sandbox Claude
account here); the full go test ./... (mote and mica both over their load
limits); pi 1.5.x's prompt (1.4.0 installed, workdir.go names the same
<cwd> form since 1.1).
This commit is contained in:
yetone
2026-11-14 15:01:56 +08:00
parent 3e38e4ba21
commit be7d22edda
3 changed files with 42 additions and 13 deletions
@@ -1586,7 +1586,14 @@ 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".
- pi: "Current working directory: X", and from pi 1.x a `<cwd>` section
with the folder on a line of its own (`<cwd>\nX\n</cwd>`).
The path's `&`, `<` and `>` may come as entities (Codex, Claude Code) and
are read as the characters. A `<cwd>` read only on one line missed pi
1.x's: its runs started in magpie's shared folder under the temp folder,
and its model was told both folders (fysly on X), so `clientWorkDir` reads
`<cwd>` as the folder rules' `workDirIn` does.
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,
+9 -4
View File
@@ -40,6 +40,7 @@ import (
"errors"
"fmt"
"hash"
"html"
"io"
"io/fs"
"log"
@@ -611,7 +612,7 @@ func clientNamedWorkPlace(req *Request) (dir string, inTurns bool) {
}
for _, m := range re.FindAllStringSubmatch(t, -1) {
for _, g := range m[1:] {
g = strings.Trim(strings.TrimSpace(g), "`'\"")
g = strings.Trim(strings.TrimSpace(html.UnescapeString(g)), "`'\"")
if g = wasDir.ReplaceAllString(g, ""); g != "" {
dir, inTurns = g, turn
}
@@ -653,8 +654,12 @@ func clientNamedWorkPlace(req *Request) (dir string, inTurns bool) {
// is, on a line of its own: Claude Code's, in its instructions (" - Primary
// working directory: /x", a moved folder as "Primary working directory: /y
// (was /x)"), on a --bare run just "CWD: /x"; OpenCode's, in its <env>;
// pi's ("Current working directory:"); Codex's and pi's <cwd>.
var clientSaidWorkDir = lazyre.New(`(?m)^\s*(?:-\s*)?(?:Primary working directory|Working directory|Current working directory|CWD): *([^\r\n]+)$|<cwd>([^<\r\n]+)</cwd>`)
// pi's ("Current working directory:"); Codex's <cwd> on its line, and
// pi 1.x's, whose <cwd> holds the folder on a line of its own
// ("<cwd>\n/x\n</cwd>"): read only on one line, pi's runs started in
// magpie's temp folder and its model was told both (fysly on X). The
// path's &, < and > come as entities (workDirIn reads them the same way).
var clientSaidWorkDir = lazyre.New(`(?m)^\s*(?:-\s*)?(?:Primary working directory|Working directory|Current working directory|CWD): *([^\r\n]+)$|<cwd>\s*([^<\r\n]+?)\s*</cwd>`)
// clientTurnWorkDir is where a caller says its working directory is in a
// turn of the conversation, on a line of its own: Codex's
@@ -663,7 +668,7 @@ var clientSaidWorkDir = lazyre.New(`(?m)^\s*(?:-\s*)?(?:Primary working director
// message of its own, and --bare's "CWD:" is its whole system prompt, so
// their forms are not read in a turn: a line a user message quotes —
// Claude Code's or anyone's — is no caller's word.
var clientTurnWorkDir = lazyre.New(`(?m)^\s*(?:-\s*)?(?:Working directory|Current working directory): *([^\r\n]+)$|<cwd>([^<\r\n]+)</cwd>`)
var clientTurnWorkDir = lazyre.New(`(?m)^\s*(?:-\s*)?(?:Working directory|Current working directory): *([^\r\n]+)$|<cwd>\s*([^<\r\n]+?)\s*</cwd>`)
func evalSymlinks(path string) string {
if p, err := filepath.EvalSymlinks(path); err == nil {
+25 -8
View File
@@ -3,6 +3,7 @@ package gateway
import (
"context"
"encoding/json"
"html"
"net/http/httptest"
"os"
"path/filepath"
@@ -161,17 +162,29 @@ func TestClaudeWorkDirSymlinkIsAvoided(t *testing.T) {
// 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 }{
{"Claude Code", "# Environment\nYou have been invoked in the following environment: \n - Primary working directory: %s\n - Is a git repository: true\n"},
{"Codex", "<environment_context>\n <cwd>%s</cwd>\n <shell>zsh</shell>\n</environment_context>"},
{"OpenCode", "<env>\n Working directory: %s\n Is directory a git repo: yes\n</env>"},
{"pi", "Current date: 2026-10-26\nCurrent working directory: %s"},
{"said twice", " - Primary working directory: /nowhere/at/all\nlater:\n - Primary working directory: %s"},
for _, c := range []struct {
name, said, folder string
escaped bool // the path's &, < and > as entities
}{
{name: "Claude Code", said: "# Environment\nYou have been invoked in the following environment: \n - Primary working directory: %s\n - Is a git repository: true\n"},
{name: "Codex", said: "<environment_context>\n <cwd>%s</cwd>\n <shell>zsh</shell>\n</environment_context>"},
{name: "Codex, a folder written as entities", said: "<environment_context>\n <cwd>%s</cwd>\n <shell>zsh</shell>\n</environment_context>", folder: "R&D<x>", escaped: true},
{name: "OpenCode", said: "<env>\n Working directory: %s\n Is directory a git repo: yes\n</env>"},
{name: "pi", said: "Current date: 2026-10-26\nCurrent working directory: %s"},
// pi 1.4's buildSystemPromptSections, each section its own tag,
// joined with a blank line; the folder on a line of its own
// (fysly on X: the model was told magpie's temp folder too)
{name: "pi 1.x", said: "You are an expert coding assistant operating inside pi, a coding agent harness.\n\n<tools>\n- read: Read file contents\n</tools>\n\n<rules>\n- Be concise in your responses\n</rules>\n\n<cwd>\n%s\n</cwd>"},
{name: "said twice", said: " - Primary working directory: /nowhere/at/all\nlater:\n - Primary working directory: %s"},
} {
t.Run(c.name, func(t *testing.T) {
var proj, old string
dirs, work, s, env, told := claudeWorkDirsTold(t, nil, func(home string) string {
proj = filepath.Join(home, "proj")
folder := c.folder
if folder == "" {
folder = "proj"
}
proj = filepath.Join(home, folder)
if err := os.MkdirAll(proj, 0o755); err != nil {
t.Fatal(err)
}
@@ -187,7 +200,11 @@ func TestClaudeRunsInTheCallersWorkDir(t *testing.T) {
}
then := time.Now().Add(-48 * time.Hour)
_ = os.Chtimes(old, then, then)
return strings.ReplaceAll(c.said, "%s", proj)
said := proj
if c.escaped {
said = html.EscapeString(proj)
}
return strings.ReplaceAll(c.said, "%s", said)
})
proj = evalSymlinks(proj)
if len(dirs) != 2 || dirs[0] != proj || dirs[1] != proj {