Files
magpie/internal/gateway/rules_pause_folder_test.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

94 lines
3.7 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// #1606 (Ti800): a subscription's quota kept for the long task in one
// folder. A pause outside that folder leaves the member out of every
// request from elsewhere — another project, or an agent that doesn't say
// its folder — not even as a fallback, while the task's own requests
// still reach it; a pause in some folders leaves it out of theirs only.
func TestPauseByFolder(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fixtures' folders are Unix paths")
}
group := func(body []byte) string {
var m map[string]any
if err := json.Unmarshal(body, &m); err != nil {
t.Fatal(err)
}
m["model"], m["stream"] = "group/r", false
delete(m, "thinking")
delete(m, "reasoning")
b, _ := json.Marshal(m)
return string(b)
}
elsewhere := func(name string) string {
return strings.ReplaceAll(group(fixture(t, name)), "/Users/me/work/proj", "/Users/me/scratch")
}
post := func(s *Server, path, body, session string) (int, string, Route) {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", path, strings.NewReader(body))
req.Header.Set("x-session-id", session)
s.Handler().ServeHTTP(rec, req)
return rec.Code, rec.Body.String(), lastRoute(s)
}
keep := provider.Rule{Use: "a/small", Pause: true, ExceptFolders: []string{"/Users/me/work/proj"}}
// a/small is the subscription, kept for /Users/me/work/proj
s, _, _ := ruled(t, keep)
for _, tc := range []struct {
name, path, body, folder string
paused bool
}{
{"claude code in the task", "/v1/messages", group(fixture(t, "claude-code-2.1.296.json")), "", false},
{"codex in the task", "/v1/responses", group(fixture(t, "codex-0.162.0.json")), "", false},
{"pi in the task", "/v1/chat/completions", group(fixture(t, "pi-1.1.0.json")), "", false},
{"claude code elsewhere", "/v1/messages", elsewhere("claude-code-2.1.296.json"), "/Users/me/scratch/a&b <c> 中文", true},
{"codex elsewhere", "/v1/responses", elsewhere("codex-0.162.0.json"), "/Users/me/scratch/a&b <c> 中文", true},
{"no folder said", "/v1/chat/completions", chat("hi", nil, 0, ""), "", true},
} {
code, out, r := post(s, tc.path, tc.body, tc.name)
want := map[bool]string{false: "from ka", true: "from kb"}[tc.paused]
if code != 200 || !strings.Contains(out, want) {
t.Errorf("%s: %d %.300s", tc.name, code, out)
continue
}
p := r.Group.Paused
if !tc.paused {
if len(p) != 0 {
t.Errorf("%s: paused %+v", tc.name, p)
}
continue
}
if len(p) != 1 || p[0].Member != "a/small" || !p[0].ByFolder || p[0].Folder != tc.folder || strings.Join(p[0].When, " · ") != "not folder /Users/me/work/proj" {
t.Errorf("%s: paused %+v", tc.name, p)
}
}
// elsewhere, a/small isn't a fallback either
s, a, b := ruled(t, keep)
b.fail = 500
if code, _, _ := post(s, "/v1/messages", elsewhere("claude-code-2.1.296.json"), "f"); code == 200 || a.n() != 0 {
t.Errorf("paused member asked as a fallback: %d, a asked %d", code, a.n())
}
// a pause in some folders: theirs only, never a request that says none
s, _, _ = ruled(t, provider.Rule{Use: "a/small", Pause: true, Folders: []string{"/Users/me/scratch"}})
if code, out, _ := post(s, "/v1/messages", elsewhere("claude-code-2.1.296.json"), "g1"); code != 200 || !strings.Contains(out, "from kb") {
t.Errorf("in the paused folder: %d %.300s", code, out)
}
for i, body := range []string{group(fixture(t, "pi-1.1.0.json")), chat("hi", nil, 0, "")} {
if code, out, r := post(s, "/v1/chat/completions", body, "g2"+string(rune('a'+i))); code != 200 || !strings.Contains(out, "from ka") || len(r.Group.Paused) != 0 {
t.Errorf("outside the paused folder %d: %d %.300s", i, code, out)
}
}
}