Routes now carry Folder: the last two levels of the working directory the agent states (Claude Code's "Primary working directory", Codex's <cwd>, Pi, OpenCode), cut by shortFolder before the route is stored, so no full path reaches the routing history, /api, or the GUI. It is kept in the routing history for as long as the history itself (30 days / 200 MB). A request that states no folder (title, side calls) takes the folder its session (or parent session) last stated; the per-session map is capped at 512 entries. GUI: By session headings read "Agent · folder · title", their tooltip and every request row's tooltip (page and tray panel) name the folder, the stage's agent subtitle lists the last level of each folder when an agent works in more than one, and the panel's agent node tooltip lists them. Run: - TestShortFolder (unix, trailing slash, C:\, D:\, UNC, CJK with spaces) and TestRouteKeepsItsFolderShort (real Handler, fake upstream: session inheritance, a moved Windows path, nothing of the full path in the marshalled routes, saveRoute -> History round trip), TestTurnDone. Broke it by hand: without the session lookup the title request has no folder; with the folder unshortened the full path shows. Both go red. - routing-session-folder.test.cjs: chromium + webkit x en, zh, zh-TW, ja, de x 1100 and 420 px, 20/20. Red on the old routing.js and with the stage's live routes left out. - GUI suites routing-*, panel-routing*, purpose-filter, reasoning-speed, gui-de/ja/zh-tw, click-scroll, number-units: 951/951. - gofmt -l clean; go vet darwin, linux (nogui), windows; builds for all three. go test -tags nogui ./...: all ok except internal/gateway, which hit go test's default 10m timeout at 611s on a loaded machine (#2108); alone with -timeout 30m it passes in 356s. - Real use: nogui build in a sandbox HOME, fake upstream, real Claude Code 2.1.299 (claude -p) run from .../workspace/magpie and .../src/dial: the day's routing jsonl has folder "workspace/magpie" and "src/dial" and no part of the sandbox path above them. Not tried: title/side requests' inheritance end to end with a real agent (claude -p made none), Codex, OpenCode and Pi with real binaries, Windows.
115 lines
4.7 KiB
Go
115 lines
4.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
func TestShortFolder(t *testing.T) {
|
|
for dir, want := range map[string]string{
|
|
"/Users/someone/secret/workspace/magpie": "workspace/magpie",
|
|
"/Users/someone/secret/workspace/magpie/": "workspace/magpie",
|
|
`C:\Users\someone\src\magpie`: "src/magpie",
|
|
`D:\magpie`: "magpie",
|
|
`C:\`: "",
|
|
`\\server\share\team\magpie`: "team/magpie",
|
|
"/magpie": "magpie",
|
|
"/": "",
|
|
"": "",
|
|
"/home/u/my project/中文 目录": "my project/中文 目录",
|
|
} {
|
|
if got := shortFolder(dir); got != want {
|
|
t.Errorf("shortFolder(%q) = %q, want %q", dir, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A route keeps the last two levels of the folder its agent says it works
|
|
// in, and never the rest of the path (#1068): in the trace the page and
|
|
// the agents' callers read, and in the history on disk. A request of the
|
|
// same session that says none, as Claude Code's title request doesn't,
|
|
// takes the session's; one with no session id has none.
|
|
func TestRouteKeepsItsFolderShort(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, `data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","content":"ok"}}]}`+"\n\n")
|
|
io.WriteString(w, `data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`+"\n\n")
|
|
io.WriteString(w, "data: [DONE]\n\n")
|
|
}))
|
|
defer srv.Close()
|
|
if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Chat: srv.URL + "/v1", Models: []string{"m1"}, Key: "k"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
send := func(body, session string) Route {
|
|
t.Helper()
|
|
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body))
|
|
req.Header.Set("User-Agent", "claude-cli/2.1.296 (external, cli)")
|
|
if session != "" {
|
|
req.Header.Set("x-claude-code-session-id", session)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
rs := s.Trace(context.Background(), 0, 0).Routes
|
|
return rs[len(rs)-1]
|
|
}
|
|
// Claude Code's main request: its environment in a system block
|
|
turn := `{"model":"plan/m1","stream":true,"max_tokens":100,"system":[{"type":"text","text":"You are Claude Code, Anthropic's official CLI for Claude."},{"type":"text","text":"# Environment\nYou have been invoked in the following environment: \n - Primary working directory: /Users/someone/secret/workspace/magpie\n - Is a git repository: true\n - Platform: darwin\n"}],"messages":[{"role":"user","content":"hi"}]}`
|
|
// its title request: no environment
|
|
title := `{"model":"plan/m1","stream":true,"max_tokens":100,"system":[{"type":"text","text":"Generate a concise, sentence-case title (3-7 words) that captures the main topic or goal of this coding session."}],"messages":[{"role":"user","content":"hi"}]}`
|
|
|
|
if r := send(turn, "s1"); r.Folder != "workspace/magpie" {
|
|
t.Fatalf("the turn's folder: %q", r.Folder)
|
|
}
|
|
if r := send(title, "s1"); r.Folder != "workspace/magpie" {
|
|
t.Fatalf("a request of the session that says no folder: %q", r.Folder)
|
|
}
|
|
if r := send(title, "s2"); r.Folder != "" {
|
|
t.Fatalf("another session's: %q", r.Folder)
|
|
}
|
|
if r := send(title, ""); r.Folder != "" {
|
|
t.Fatalf("no session id: %q", r.Folder)
|
|
}
|
|
// the folder said last goes: the agent moved
|
|
moved := strings.Replace(turn, "/Users/someone/secret/workspace/magpie", `C:\\Users\\someone\\src\\dial`, 1)
|
|
if r := send(moved, "s1"); r.Folder != "src/dial" {
|
|
t.Fatalf("moved: %q", r.Folder)
|
|
}
|
|
if r := send(title, "s1"); r.Folder != "src/dial" {
|
|
t.Fatalf("after the move: %q", r.Folder)
|
|
}
|
|
|
|
// what the page gets, and what the history keeps on disk
|
|
now := historyNoon(t)
|
|
for _, r := range s.Trace(context.Background(), 0, 0).Routes {
|
|
b, _ := json.Marshal(r)
|
|
for _, leak := range []string{"someone", "secret", "Users"} {
|
|
if strings.Contains(string(b), leak) {
|
|
t.Fatalf("the route carries the folder's path (%s): %s", leak, b)
|
|
}
|
|
}
|
|
r.Time = now
|
|
saveRoute(r)
|
|
}
|
|
_, kept, _ := History(now.Format(dayForm))
|
|
var folders []string
|
|
for _, r := range kept {
|
|
folders = append(folders, r.Folder)
|
|
}
|
|
if got := strings.Join(folders, ","); !strings.Contains(got, "workspace/magpie") || !strings.Contains(got, "src/dial") {
|
|
t.Fatalf("the history's folders: %q", got)
|
|
}
|
|
}
|