Files
magpie/internal/provider/rulefolder.go
T
yetone 0fcf563e61 routing: a pause rule can hold by folder, in some or outside them (#1606, Ti800)
Ti800 keeps a subscription's quota for one long task in one folder and
wants every other request kept off it (their option A). A pause rule
held by hours, days and agents only; it now holds by folder too.

What changed
- provider.Rule gains ExceptFolders (json exceptFolders, CLI
  except-folders=, aliases except-folder/not-in/outside): the folder the
  agent works in is in none of them. An agent that says no folder isn't
  known to be in one, so except-folders holds for it; folders= still
  never matches an unknown folder. Both conditions are allowed on send-to
  and pause rules (Rule.ByFolder, inFolders); cleanRules cleans and
  dedupes both and lets a pause hold by time, days, agents and folders.
- PausedOut takes the request's folder. The gateway reads it (workDir)
  only when a pause by folder is on the way (PausesByFolder); embeddings
  and System One's decision calls pass none. Paused carries Folder and
  ByFolder for the trace. ruleFor reads the folder for except-folders
  rules too.
- Group sharing drops every rule by folder, except-folders included.
- CLI usage, TUI hint, ParseRule/Line round-trip except-folders.
- GUI: the rule editor has a "not in folder" condition beside "in
  folder", both shown on a pause and kept when a rule turns into one; a
  folder that isn't a whole path and a pause with no condition are
  refused; the group's rule and the trace say "not in …", and a paused
  reason says the folder read or that the agent said none. Strings in
  zh, zh-TW, ja, de.
- Docs: gateway-routing.md, site start.html (en, zh, ja).

What was run
- go test -tags nogui ./internal/provider ./internal/gateway (folder,
  pause, share, rule tests); full go test ./... before pushing.
- Broke it by hand: with the gateway's folder read replaced by nothing,
  TestPauseByFolder fails (in-task requests went to the other member).
- GUI: routing-pause-folder.test.cjs (new), routing-pause,
  routing-folder, i18n-keys and 10 other rule-editor tests in Chromium
  and WebKit, en/zh/zh-TW/ja/de, at 1100px and 390px.
- gofmt -l; GOOS=linux and GOOS=windows go vet of the touched packages.

Not tried: a real Claude Code subagent's request against a live group;
by the documented rule a request without a folder is paused off the
kept member, which is what the reporter asked for.
2026-10-11 21:24:10 +08:00

107 lines
3.2 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"
"slices"
"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
}
// cleanFolders checks a rule's folders, each once, none blank.
func cleanFolders(in []string) ([]string, error) {
var out []string
for _, p := range in {
if strings.TrimSpace(p) == "" {
continue
}
c, err := cleanFolder(p)
if err != nil {
return nil, err
}
if !slices.Contains(out, c) {
out = append(out, c)
}
}
return out, nil
}