diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 37aa880e5..edbed9565 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -1586,7 +1586,14 @@ It knows four ways of naming it: - Claude Code: "Primary working directory: X"; - Codex: `X`; - OpenCode: "Working directory: X"; -- pi: "Current working directory: X". +- pi: "Current working directory: X", and from pi 1.x a `` section + with the folder on a line of its own (`\nX\n`). + +The path's `&`, `<` and `>` may come as entities (Codex, Claude Code) and +are read as the characters. A `` 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 +`` 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, diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index a3f2ddd73..8c2dcd83a 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -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 ; -// pi's ("Current working directory:"); Codex's and pi's . -var clientSaidWorkDir = lazyre.New(`(?m)^\s*(?:-\s*)?(?:Primary working directory|Working directory|Current working directory|CWD): *([^\r\n]+)$|([^<\r\n]+)`) +// pi's ("Current working directory:"); Codex's on its line, and +// pi 1.x's, whose holds the folder on a line of its own +// ("\n/x\n"): 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]+)$|\s*([^<\r\n]+?)\s*`) // 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]+)$|([^<\r\n]+)`) +var clientTurnWorkDir = lazyre.New(`(?m)^\s*(?:-\s*)?(?:Working directory|Current working directory): *([^\r\n]+)$|\s*([^<\r\n]+?)\s*`) func evalSymlinks(path string) string { if p, err := filepath.EvalSymlinks(path); err == nil { diff --git a/internal/gateway/claude_workdir_test.go b/internal/gateway/claude_workdir_test.go index b24f47cbd..6e1e8cc6a 100644 --- a/internal/gateway/claude_workdir_test.go +++ b/internal/gateway/claude_workdir_test.go @@ -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", "\n %s\n zsh\n"}, - {"OpenCode", "\n Working directory: %s\n Is directory a git repo: yes\n"}, - {"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: "\n %s\n zsh\n"}, + {name: "Codex, a folder written as entities", said: "\n %s\n zsh\n", folder: "R&D", escaped: true}, + {name: "OpenCode", said: "\n Working directory: %s\n Is directory a git repo: yes\n"}, + {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\n- read: Read file contents\n\n\n\n- Be concise in your responses\n\n\n\n%s\n"}, + {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 {