Files
magpie/internal/gateway/workdir.go
T
yetone ae70d8eac8 routing: a group's rule can hold by the folder the agent works in (Cario0x on X)
Cario0x on X asked to route by the project being worked on. A group's rule now has a
"folders" condition: it matches when the request's working folder is one of the listed
folders or inside one. It combines (AND) with the existing conditions: tokens, images,
intent, time, agents and quota.

- gateway/workdir.go reads the folder each agent states in its own request:
  - Claude Code: "Primary working directory", including "(was …)" after a move.
  - Codex: <environment_context><cwd>.
  - Pi: <cwd>.
  - OpenCode: <env> "Working directory".
  The last one stated wins. Assistant text and tool results are ignored. A request
  that states no folder matches no folder rule: unknown, never empty.
- provider/rulefolder.go matches a folder only at a path boundary.
  - ~ is the home folder.
  - C:\x is the same folder as /mnt/c/x, and \\wsl$\Distro\x as /x.
  - Case is ignored on macOS and Windows, and always for a drive.
  - A rule's folder must be a whole path. Pause rules can't have folders.
- The trace's RuleHit.Folder makes the Routing page say "The agent works in …".
- GUI: an "in folder" condition in the group editor, hidden on pause rules. Strings in
  zh, zh-TW, ja and de.
- CLI: `magpie group rule add … folders=~/oss,"~/My Project"`. The TUI shows the
  condition.
- Docs: docs/subsystems/gateway-routing.md and site start.html (en, zh, ja).

Fixtures are real requests from Claude Code 2.1.296, Codex 0.162.0 and Pi 1.1.0, run in
a sandbox from a folder named "a&b <c> 中文" or "My Proj", with the sandbox path
replaced.

Run at this head:
- go test ./... green.
- gofmt -l clean.
- go vet green for darwin, GOOS=linux -tags nogui and GOOS=windows.
- GUI routing-folder.test.cjs: chromium and webkit, en, zh, zh-TW, ja and de, 1100px
  and 390px (no horizontal scroll). It fails on the old routing.js.
- Related routing, group and i18n GUI tests: 85/85.
- Full GUI suite: 5611/5631 under --test-concurrency=6. Rerun alone, every failure
  passes except:
  - skill-install-agents-all (chromium, 420px), which is red on origin/main too: #1554.
  - The menu-scroll track flake, which flakes on main as well: #1550.
- e2e: a sandbox HOME, a dev build and fake upstreams, with real Claude Code 2.1.296
  (`claude -p --model group/fd`).
  - From proj/acme/web, rule 1 matched and the request went to kb.
  - From proj/acme-old, no rule matched and the request went to ka. The trace and UI
    showed the folder.
2026-10-11 10:17:16 +08:00

90 lines
2.7 KiB
Go

package gateway
// The folder an agent works in, as it tells its model in the request — for
// a group's rule by folder (provider.Rule.Folders). Each agent says it its
// own way, every format here taken from the agent's own request:
//
// - Claude Code (2.1.296): its environment, " - Primary working
// directory: /x", in the system prompt or a system message; when the
// folder changes, "Primary working directory: /y (was /x)". < and > in
// the path come as &lt; and &gt;.
// - Codex (0.162): a user message's <environment_context> with
// "<cwd>/x</cwd>", the path's &, < and > as entities.
// - Pi (1.1): the system prompt's "<cwd>\n/x\n</cwd>", backslashes as /.
// - OpenCode (2.0): "<env>\n Working directory: /x\n…</env>", in the
// system prompt or, when it changes, a later message.
//
// Of those it says, the last one goes: an agent that moved says so after.
// An agent that says none of them has no folder: a rule by folder doesn't
// match it.
import (
"html"
"regexp"
"strings"
)
var (
ccWorkDir = regexp.MustCompile(`(?m)^(?: - )?Primary working directory: (.+)$`)
// the folder a <cwd> holds: Codex's on its line, Pi's on its own
cwdTag = regexp.MustCompile(`<cwd>\s*([^<\n]+?)\s*</cwd>`)
envDir = regexp.MustCompile(`(?m)^\s*Working directory: (.+)$`)
wasDir = regexp.MustCompile(` \(was .*\)$`)
)
// workDir is the folder the request says its agent works in; "" when it
// doesn't say.
func workDir(req *Request) string {
if req == nil {
return ""
}
dir := workDirIn(req.System, "")
for _, m := range req.Messages {
if m.Role == "assistant" {
continue // the model's words, not the agent's
}
for _, p := range m.Parts {
if p.Kind == Text {
dir = workDirIn(p.Text, dir)
}
}
}
return dir
}
// workDirIn is the last folder text says, else dir.
func workDirIn(text, dir string) string {
if !strings.Contains(text, "irectory: ") && !strings.Contains(text, "<cwd>") {
return dir
}
at := -1
take := func(i int, v string) {
if v = strings.TrimSpace(html.UnescapeString(v)); v != "" && i > at {
at, dir = i, v
}
}
for _, m := range ccWorkDir.FindAllStringSubmatchIndex(text, -1) {
take(m[0], wasDir.ReplaceAllString(text[m[2]:m[3]], ""))
}
for _, m := range cwdTag.FindAllStringSubmatchIndex(text, -1) {
take(m[0], text[m[2]:m[3]])
}
// OpenCode's, only within its <env>
for rest, off := text, 0; ; {
i := strings.Index(rest, "<env>")
if i < 0 {
break
}
j := strings.Index(rest[i:], "</env>")
if j < 0 {
break
}
block := rest[i : i+j]
if m := envDir.FindStringSubmatchIndex(block); m != nil {
take(off+i+m[0], block[m[2]:m[3]])
}
rest, off = rest[i+j:], off+i+j
}
return dir
}