Files
magpie/internal/provider/rulefolder.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

88 lines
2.8 KiB
Go

package provider
// A rule's folders (Rule.Folders): the request's working folder, as the
// agent states it in the request (gateway/workdir.go), is one of them or
// under one (Cario0x on X: routing by the project being worked on).
import (
"fmt"
"os"
"path"
"regexp"
"runtime"
"strings"
)
// foldCase: folders are told apart without case, as macOS's and
// Windows's file systems tell them by default; tests set it.
var foldCase = runtime.GOOS == "darwin" || runtime.GOOS == "windows"
// ruleHome is the home folder a rule's ~ stands for; tests set it.
var ruleHome = func() string { h, _ := os.UserHomeDir(); return h }
var (
drivePath = regexp.MustCompile(`^[A-Za-z]:(/|$)`)
wslMount = regexp.MustCompile(`^/mnt/([A-Za-z])(/|$)`)
wslShare = regexp.MustCompile(`(?i)^//wsl(\$|\.localhost)/[^/]+(/|$)`)
)
// folderKey puts a folder the one way it is compared: forward slashes, ~
// as the home folder, a Windows drive as WSL mounts it (C:\x and
// /mnt/c/x are one), a WSL distro's share as the distro's own path
// (\\wsl$\Ubuntu\home\x is /home/x), no trailing slash, and without case
// where the system's folders have none (a drive's always). "" when it
// isn't a whole path.
func folderKey(p string) string {
p = strings.ReplaceAll(strings.TrimSpace(p), `\`, "/")
if p == "~" || strings.HasPrefix(p, "~/") {
h := strings.ReplaceAll(ruleHome(), `\`, "/")
if h == "" {
return ""
}
p = h + p[1:]
}
fold := foldCase
if m := wslShare.FindString(p); m != "" {
p = "/" + p[len(m):]
}
if drivePath.MatchString(p) {
p = "/mnt/" + strings.ToLower(p[:1]) + p[2:]
}
if m := wslMount.FindStringSubmatch(p); m != nil {
p = "/mnt/" + strings.ToLower(m[1]) + p[len("/mnt/")+1:]
fold = true // a Windows drive, from either side
}
if !strings.HasPrefix(p, "/") {
return ""
}
p = path.Clean(p)
if fold {
p = strings.ToLower(p)
}
return p
}
// FolderUnder reports whether folder is root or a folder inside it: at a
// path's boundary, so /a/b is not under /a/bc. An unknown folder ("") is
// under none.
func FolderUnder(folder, root string) bool {
f, r := folderKey(folder), folderKey(root)
if f == "" || r == "" {
return false
}
return f == r || r == "/" || strings.HasPrefix(f, r+"/")
}
// cleanFolder checks a rule's folder is a whole path, ~/x or C:\x, and
// keeps it as the user typed it but for a trailing slash.
func cleanFolder(p string) (string, error) {
p = strings.TrimSpace(p)
if t := strings.TrimRight(p, `/\`); t != "" && !(len(t) == 2 && t[1] == ':') {
p = t // the root, and a drive's, keep theirs
}
if p != "~" && !strings.HasPrefix(p, "~/") && !strings.HasPrefix(p, `~\`) && !strings.HasPrefix(p, "/") && !strings.HasPrefix(p, `\\`) && !drivePath.MatchString(strings.ReplaceAll(p, `\`, "/")) {
return "", fmt.Errorf("the folder %q is not a whole path (/Users/me/work, ~/work, C:\\work)", p)
}
return p, nil
}