liang_yongxiang on Discord: Routing tells how full one request's context window was; does Sessions have the same, each turn's share? A session's details on the Sessions page (and in Usage's Sessions list, which draws the same details) get a Context line when the gateway sent calls of it (s.via): the Context tab's spark of its prompts, request by request against its window, and the fullest one's share beside it, toned as the tab tones it. A point's tip says "Request 2: 80K of 200K (40%)" (the tab's spark says it too now, where the window is known); a point clicked opens that request in Routing with its card. A session the gateway never saw asks nothing; one it kept no prompt of shows no line. The data is the routing history the Context tab already reads. A new GET /api/context/session?agent=&id=&from=&to= reads only the days from the session's first to its last (30 at most), through the tab's per-day cache (daysRecsLocked, split out of contextRecsLocked, so the tab's cached days are not evicted), and keeps that one session (contextOfRecsFor), with up to 1000 of its requests. The tab keeps the latest 80 sessions and would leave an older one out. Subagent and side calls are left out as the tab leaves them out; the same id under another agent is that agent's. The line's code is in plugins/usage/page.js (sessCtxLine). context.js only exports spark and health, and pays for its tip by cutting comments, so the core's scripts are 10 bytes under main's, no budget raise. Run at this head (rebased one215d335a), on WSL (magpie-desktop, Linux; mote rebooted mid-run): - gofmt -l clean; go test -tags nogui -run 'TestContextSessionLine|TestCoreScriptBudget|TestContext' ./internal/gui/: ok (also ok on mote at this head). - node --test sessions-context-line (new: Chromium and WebKit, en zh zh-TW ja de, 1100 and 420 wide) and context-window: 44 of 44. Run on mote before the rebase, at627fd1a3b: - the whole internal/gui package: its one red, TestCoreScriptBudget (1,536,110 > 1,533,802), is why the line moved into the plugin. - sessions-context-line: 20 of 20. - gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok. - i18n-keys: "a plugin's words are its own" red, the same on origin/maine215d335a(#5327, #5332, #5426, #5482); the new strings pass its other checks. Broken by hand: - the one-session filter dropped: TestContextSessionLine red ("its requests' prompts: [21000]", another session's line). - the tip back to "Request {n}: {tokens} tokens": sessions-context-line red in de ("Anfrage 2: 80K Token" for "Anfrage 2: 80K von 200K (40%)"). - the details line turned off: sessions-context-line red in en and zh. Not tried: the whole GUI suite (it was running under mote's suite lock when mote rebooted; not run at push); a real magpie with real routing history (the GUI test stubs /api/context/session; the Go test reads real history files in a sandbox config); the Linux app's WebKitGTK. Reported-by: liang_yongxiang (Discord)
119 lines
3.9 KiB
Go
119 lines
3.9 KiB
Go
package gui
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
// A session's details on the Sessions page show how full its context was,
|
|
// request by request (liang_yongxiang on Discord: Routing told it for one
|
|
// request, the Sessions page not at all). /api/context/session answers one
|
|
// session's line: each of its requests the gateway saw, oldest first, over
|
|
// every day from its first to its last and none outside them, however many
|
|
// other sessions there were since (the Context tab lists the latest 80 and
|
|
// would leave it out), and with neither a subagent's prompt nor another
|
|
// agent's session of the same id in it.
|
|
func TestContextSessionLine(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
mux := keptMux(t)
|
|
ctxKept.Lock()
|
|
ctxKept.days, ctxKept.key = map[string]ctxDay{}, ""
|
|
ctxKept.Unlock()
|
|
reads := map[string]bool{}
|
|
readingDay = func(day string) { reads[day] = true }
|
|
t.Cleanup(func() { readingDay = nil })
|
|
|
|
dir := gateway.HistoryDir()
|
|
os.MkdirAll(dir, 0o700)
|
|
now := time.Now()
|
|
dayOf := func(back int) time.Time {
|
|
d := now.AddDate(0, 0, -back)
|
|
return time.Date(d.Year(), d.Month(), d.Day(), 9, 0, 0, 0, time.Local)
|
|
}
|
|
files := map[string]*bytes.Buffer{}
|
|
id := int64(0)
|
|
add := func(back int, agent, session string, chat int, edit func(*gateway.Route)) time.Time {
|
|
id++
|
|
r := ctxRoute(id, agent, session, 20000, chat, 200000, 0)
|
|
r.Time = dayOf(back).Add(time.Duration(id) * time.Second)
|
|
if edit != nil {
|
|
edit(&r)
|
|
}
|
|
day := r.Time.Format("2006-01-02")
|
|
if files[day] == nil {
|
|
files[day] = &bytes.Buffer{}
|
|
}
|
|
j, _ := json.Marshal(r)
|
|
files[day].Write(append(j, '\n'))
|
|
return r.Time
|
|
}
|
|
// a request of another session four days ago, outside the session's days
|
|
add(4, "claude", "older", 1000, nil)
|
|
first := add(3, "claude", "s1", 10000, nil)
|
|
add(3, "claude", "s1", 30000, func(r *gateway.Route) { r.Sub = "explore" })
|
|
add(2, "claude", "s1", 60000, nil)
|
|
add(2, "codex", "s1", 90000, nil)
|
|
last := add(1, "claude", "s1", 140000, nil)
|
|
// 100 sessions since, on its last day after it
|
|
for i := range 100 {
|
|
add(1, "claude", fmt.Sprint("later-", i), 1000, nil)
|
|
}
|
|
for day, b := range files {
|
|
os.WriteFile(filepath.Join(dir, day+".jsonl"), b.Bytes(), 0o600)
|
|
}
|
|
|
|
ask := func(agent, sid string, from, to time.Time) *ctxSession {
|
|
t.Helper()
|
|
var got struct {
|
|
Session *ctxSession `json:"session"`
|
|
}
|
|
q := url.Values{"agent": {agent}, "id": {sid}, "from": {from.UTC().Format(time.RFC3339Nano)}, "to": {to.UTC().Format(time.RFC3339Nano)}}
|
|
getJSON(t, mux, "/api/context/session?"+q.Encode(), &got)
|
|
return got.Session
|
|
}
|
|
s := ask("claude", "s1", first, last)
|
|
if s == nil {
|
|
t.Fatal("no line for a session the gateway saw")
|
|
}
|
|
var tokens []int
|
|
for _, p := range s.Points {
|
|
tokens = append(tokens, p.Tokens)
|
|
}
|
|
if fmt.Sprint(tokens) != "[30000 80000 160000]" {
|
|
t.Fatalf("its requests' prompts: %v, want [30000 80000 160000]", tokens)
|
|
}
|
|
if s.Window != 200000 || s.Peak != 160000 || s.Requests != 3 || s.Agent != "claude" || s.Key != "s1" {
|
|
t.Fatalf("session: %+v", s)
|
|
}
|
|
if reads[dayOf(4).Format("2006-01-02")] || reads[dayOf(0).Format("2006-01-02")] {
|
|
t.Fatalf("read days outside the session's: %v", reads)
|
|
}
|
|
if !reads[dayOf(3).Format("2006-01-02")] || !reads[dayOf(1).Format("2006-01-02")] {
|
|
t.Fatalf("didn't read the session's days: %v", reads)
|
|
}
|
|
|
|
// the same id under another agent is that agent's own
|
|
if c := ask("codex", "s1", first, last); c == nil || len(c.Points) != 1 || c.Points[0].Tokens != 110000 {
|
|
t.Fatalf("codex's s1: %+v", c)
|
|
}
|
|
// a session the gateway never saw has no line
|
|
if n := ask("claude", "nobody", first, last); n != nil {
|
|
t.Fatalf("a session never seen: %+v", n)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
mux.ServeHTTP(rec, httptest.NewRequest("GET", "/api/context/session?agent=claude", nil))
|
|
if rec.Code != 400 {
|
|
t.Fatalf("no id: %d", rec.Code)
|
|
}
|
|
}
|