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.
141 lines
5.7 KiB
Go
141 lines
5.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// The fixtures are requests the agents sent a fake upstream, run in a
|
|
// sandbox home from a folder named "a&b <c> 中文" (Claude Code 2.1.296,
|
|
// Codex 0.162.0) or "My Proj" (Pi 1.1.0); the sandbox's path is replaced
|
|
// with /Users/me/work.
|
|
func fixture(t *testing.T, name string) []byte {
|
|
t.Helper()
|
|
b, err := os.ReadFile(filepath.Join("testdata", "workdir", name))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
// Each agent's folder is read from its own request, entities and all.
|
|
func TestWorkDirOfEachAgent(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
file string
|
|
parse func([]byte) (*Request, error)
|
|
want string
|
|
}{
|
|
{"claude-code-2.1.296.json", parseAnthropic, "/Users/me/work/proj/a&b <c> 中文"},
|
|
{"codex-0.162.0.json", parseResponses, "/Users/me/work/proj/a&b <c> 中文"},
|
|
{"pi-1.1.0.json", parseChat, "/Users/me/work/proj/My Proj"},
|
|
} {
|
|
req, err := tc.parse(fixture(t, tc.file))
|
|
if err != nil {
|
|
t.Fatal(tc.file, err)
|
|
}
|
|
if got := workDir(req); got != tc.want {
|
|
t.Errorf("%s: %q, want %q", tc.file, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// OpenCode 2.0.20's environment, as its source writes it (its run waits on
|
|
// a background service a sandbox doesn't start): in the system prompt,
|
|
// and when it changes, after.
|
|
func TestWorkDirOpenCode(t *testing.T) {
|
|
env := func(dir string) string {
|
|
return strings.Join([]string{"<env>", " Working directory: " + dir, " Workspace root folder: /Users/me", " Is directory a git repo: yes", " Platform: darwin",
|
|
" Prefer /tmp/x over generic system temporary directories such as /tmp; it is pre-created and approved for external access.", "</env>"}, "\n")
|
|
}
|
|
req := &Request{System: "You are opencode.\nHere is some useful information about the environment you are running in:\n" + env("/Users/me/acme"),
|
|
Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "hi"}}}}}
|
|
if got := workDir(req); got != "/Users/me/acme" {
|
|
t.Errorf("%q", got)
|
|
}
|
|
req.Messages = append(req.Messages, Message{Role: "assistant", Parts: []Part{{Kind: Text, Text: "ok"}}},
|
|
Message{Role: "user", Parts: []Part{{Kind: Text, Text: "The environment you are running in is now:\n" + env("/Users/me/oss")}}})
|
|
if got := workDir(req); got != "/Users/me/oss" {
|
|
t.Errorf("changed: %q", got)
|
|
}
|
|
}
|
|
|
|
// Claude Code moved to another folder says so after, with the one it was
|
|
// in (its source: "Primary working directory: /y (was /x)"); the last
|
|
// said goes. What the model or a tool said, or a "Working directory:" out
|
|
// of OpenCode's <env>, isn't the agent's folder; nothing said is unknown.
|
|
func TestWorkDirLastSaidAndOnlyByTheAgent(t *testing.T) {
|
|
req, err := parseAnthropic(fixture(t, "claude-code-2.1.296.json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Messages = append(req.Messages,
|
|
Message{Role: "assistant", Parts: []Part{{Kind: Text, Text: " - Primary working directory: /from/the/model"}}},
|
|
Message{Role: "user", Parts: []Part{{Kind: ToolResult, Text: "<cwd>/from/a/tool</cwd>"}, {Kind: Text, Text: "<system-reminder>\nPrimary working directory: /Users/me/next (was /Users/me/work/proj/a&b <c> 中文)\n</system-reminder>"}}},
|
|
)
|
|
if got := workDir(req); got != "/Users/me/next" {
|
|
t.Errorf("%q", got)
|
|
}
|
|
for _, text := range []string{"hi", "Working directory: /x", "the <cwd></cwd> tag"} {
|
|
if got := workDir(&Request{System: "You are an agent.", Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: text}}}}}); got != "" {
|
|
t.Errorf("%q: %q", text, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The agents' own requests go by a group's rule by folder, each over its
|
|
// own protocol: the folder under the rule's to its member, the trace
|
|
// telling the folder, the rule's condition and that it matched; one out
|
|
// of it, or with no folder said, by the group's order.
|
|
func TestRuleByFolder(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("the fixtures' folders are Unix paths")
|
|
}
|
|
s, _, _ := ruled(t, provider.Rule{Use: "b/big", Folders: []string{"/Users/me/work/proj"}})
|
|
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)
|
|
}
|
|
for _, tc := range []struct {
|
|
name, path, body, folder string
|
|
n int
|
|
}{
|
|
{"claude code", "/v1/messages", group(fixture(t, "claude-code-2.1.296.json")), "/Users/me/work/proj/a&b <c> 中文", 1},
|
|
{"codex", "/v1/responses", group(fixture(t, "codex-0.162.0.json")), "/Users/me/work/proj/a&b <c> 中文", 1},
|
|
{"pi", "/v1/chat/completions", group(fixture(t, "pi-1.1.0.json")), "/Users/me/work/proj/My Proj", 1},
|
|
{"pi elsewhere", "/v1/chat/completions", strings.ReplaceAll(group(fixture(t, "pi-1.1.0.json")), "/Users/me/work/proj/", "/Users/me/work/project/"), "/Users/me/work/project/My Proj", 0},
|
|
{"no folder", "/v1/chat/completions", chat("hi", nil, 0, ""), "", 0},
|
|
} {
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", tc.path, strings.NewReader(tc.body))
|
|
req.Header.Set("x-session-id", tc.name)
|
|
s.Handler().ServeHTTP(rec, req)
|
|
want := map[int]string{0: "from ka", 1: "from kb"}[tc.n]
|
|
r := lastRoute(s)
|
|
if rec.Code != 200 || !strings.Contains(rec.Body.String(), want) {
|
|
t.Errorf("%s: %d %.300s", tc.name, rec.Code, rec.Body.String())
|
|
continue
|
|
}
|
|
if r.Rule == nil || r.Rule.N != tc.n || r.Rule.Folder != tc.folder {
|
|
t.Errorf("%s: rule %+v", tc.name, r.Rule)
|
|
continue
|
|
}
|
|
if tc.n == 1 && (len(r.Rule.When) != 1 || r.Rule.When[0] != "folder /Users/me/work/proj") {
|
|
t.Errorf("%s: when %q", tc.name, r.Rule.When)
|
|
}
|
|
}
|
|
}
|