Player (magpie 0.1.1234 in docker, Claude Code in auto mode on glm-5.3)
saw "137 requests · 45 compactions" on Usage → Context for a session
that never compacted (peak ~116K of 1M). Claude Code's auto mode
classifier ("You are a security monitor…", a transcript, no tools,
34–48K) goes under the conversation's own session id, so every one
after a 60–115K turn passed compacted()'s 60% drop. Title, quota
check and a subagent's turns share the id too, and the latest one of
any of them became the session's "latest request".
contextOf now keeps a session's line to its own turns:
- in a session whose turns carry tool definitions, a request without
any is a side call (classifier, title, quota check) and is left out
of the session and the agent's stats, as a Kind'd call already was
(toolSessions, read from the routes on disk, so existing history
reads right too);
- a Claude Code subagent's request is left out of the session's line
(still in the agent's stats). The route now records it: Route.Sub,
from x-claude-code-agent-id (subagentOf), set in serve's beginFor.
An agent that sends no tools at all keeps every request.
Tests:
- TestContextSideCallsAreNoCompaction: an interleaved session like the
reporter's (title, quota check, 7 turns 60K→120K each followed by two
classifier calls 34–48K, two subagent turns, then a real compaction
120K→33K). On origin/main's context.go it fails "compactions: session
10 agent 10, want the one real one"; with only the subagent check
removed it fails with 2. Passes now: 1 compaction, 10 points, latest
is the last turn, peak 120K, baseline 24K.
- TestContextToollessAgentKeepsItsTurns: a no-tools chat app's 70K→20K
still counts.
- TestRouteSaysItsSubagent (gateway, through Handler with a fake
upstream): fails "map[:2]" without the gateway line, passes with it.
Run at this head (go1.26.8, darwin): gofmt -l clean; go vet ./...,
GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...; go build,
GOOS=linux go build -tags nogui, GOOS=windows go build; go test -tags
nogui -count=1 ./... under a scratchpad HOME with caches pinned. With the
machine at load ~180-260 the first full run timed out (10m default) in
agent, gateway, library, plugin and provider with no test FAIL; those
five were rerun with -p 2 -timeout 45m and all passed. Everything else
passed in the full run. No GUI asset changed (context.js reads the same
JSON), so the Playwright suite wasn't run. Not tried against the
reporter's own usage.jsonl or a live Claude Code auto-mode session.
56 lines
2.1 KiB
Go
56 lines
2.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A Claude Code subagent's request goes under its conversation's session
|
|
// id; its route says whose it is (x-claude-code-agent-id), so the Context
|
|
// page doesn't take its smaller prompt for the conversation compacting.
|
|
// The conversation's own turns name none.
|
|
func TestRouteSaysItsSubagent(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()
|
|
body := `{"model":"plan/m1","stream":true,"max_tokens":100,"system":"You are Claude Code.","tools":[{"name":"Bash","input_schema":{"type":"object"}}],"messages":[{"role":"user","content":"hi"}]}`
|
|
for _, sub := range []string{"", "agent-a1b2c3"} {
|
|
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body))
|
|
req.Header.Set("User-Agent", "claude-cli/2.1.295 (external, cli)")
|
|
req.Header.Set("x-claude-code-session-id", "cc-1")
|
|
if sub != "" {
|
|
req.Header.Set("x-claude-code-agent-id", sub)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
}
|
|
got := map[string]int{}
|
|
for _, r := range s.Trace(t.Context(), 0, 0).Routes {
|
|
if r.Session != "cc-1" {
|
|
t.Fatalf("route %d session %q", r.ID, r.Session)
|
|
}
|
|
got[r.Sub]++
|
|
}
|
|
if got[""] != 1 || got["agent-a1b2c3"] != 1 {
|
|
t.Fatalf("routes by subagent %v, want one of the conversation's and one of agent-a1b2c3's", got)
|
|
}
|
|
}
|