diff --git a/internal/gui/api.go b/internal/gui/api.go index 93505dc69..230cbb1e1 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -2502,6 +2502,146 @@ func writeJSON(rw http.ResponseWriter, v any) { _ = json.NewEncoder(rw).Encode(v) } +// writeJSONSame is writeJSON for a read the page asks for again and again: +// the answer's X-Magpie-Tag is a hash of its bytes, and a request whose +// X-Magpie-Had is that tag gets X-Magpie-Same and no body, which the page +// reads as the answer it has (api in app.js). The Sessions tab read a long +// history's ~8 MB list and ~0.7 MB of stats every 15 s, and parsed and +// compared them whole to find them the same. Not an ETag and a 304: +// WebView2 is sent no If-None-Match, and Wails turns its 304 into a 505. +func writeJSONSame(rw http.ResponseWriter, r *http.Request, v any) { + var b bytes.Buffer + if err := json.NewEncoder(&b).Encode(v); err != nil { + fail(rw, err) + return + } + sum := sha256.Sum256(b.Bytes()) + tag := hex.EncodeToString(sum[:16]) + rw.Header().Set("X-Magpie-Tag", tag) + if r.Header.Get("X-Magpie-Had") == tag { + rw.Header().Set("X-Magpie-Same", "1") + rw.WriteHeader(http.StatusOK) + return + } + rw.Header().Set("Content-Type", "application/json") + _, _ = rw.Write(b.Bytes()) +} + +// sentRows is each row's hash in the last few lists writeJSONRows sent, by +// the answer's tag, oldest first: a few, for a window and a browser tab or +// two asking at once. +var sentRows struct { + sync.Mutex + tags []string + rows map[string][][16]byte +} + +const sentRowsKept = 4 + +// writeJSONRows is writeJSONSame for an answer that is a long list, key, +// beside the fields of rest: one asked with the tag of a list sent before +// that changed since gets X-Magpie-Delta: key, and its list holds only the +// rows not in that one, each run of rows that was, in its order, written +// [from, count] of the list the page has. While an agent works, its +// session's row changes every poll, and the Sessions tab read all ~5000 +// rows (~8 MB) again for it. +func writeJSONRows[T any](rw http.ResponseWriter, r *http.Request, key string, rows []T, rest any) { + raw := make([][]byte, len(rows)) + sums := make([][16]byte, len(rows)) + for i, row := range rows { + b, err := json.Marshal(row) + if err != nil { + fail(rw, err) + return + } + raw[i] = b + sum := sha256.Sum256(b) + sums[i] = [16]byte(sum[:16]) + } + tail, err := json.Marshal(rest) + if err != nil { + fail(rw, err) + return + } + k, _ := json.Marshal(key) + body := func(items [][]byte) []byte { + var b bytes.Buffer + b.WriteByte('{') + b.Write(k) + b.WriteString(":[") + b.Write(bytes.Join(items, []byte(","))) + b.WriteByte(']') + if len(tail) > 2 { + b.WriteByte(',') + b.Write(tail[1:]) + } else { + b.WriteByte('}') + } + b.WriteByte('\n') + return b.Bytes() + } + whole := body(raw) + sum := sha256.Sum256(whole) + tag := hex.EncodeToString(sum[:16]) + rw.Header().Set("X-Magpie-Tag", tag) + had := r.Header.Get("X-Magpie-Had") + if had == tag { + rw.Header().Set("X-Magpie-Same", "1") + rw.WriteHeader(http.StatusOK) + return + } + sentRows.Lock() + was, ok := sentRows.rows[had] + if _, kept := sentRows.rows[tag]; !kept { + if sentRows.rows == nil { + sentRows.rows = map[string][][16]byte{} + } + if len(sentRows.tags) == sentRowsKept { + delete(sentRows.rows, sentRows.tags[0]) + sentRows.tags = sentRows.tags[1:] + } + sentRows.tags = append(sentRows.tags, tag) + sentRows.rows[tag] = sums + } + sentRows.Unlock() + rw.Header().Set("Content-Type", "application/json") + if !ok || had == "" { + _, _ = rw.Write(whole) + return + } + at := make(map[[16]byte][]int, len(was)) + for j, s := range was { + at[s] = append(at[s], j) + } + var items [][]byte + from, n := 0, 0 + run := func() { + if n > 0 { + items = append(items, []byte("["+strconv.Itoa(from)+","+strconv.Itoa(n)+"]")) + } + n = 0 + } + for i, s := range sums { + js := at[s] + if len(js) == 0 { + run() + items = append(items, raw[i]) + continue + } + j := js[0] + at[s] = js[1:] + if n > 0 && j == from+n { + n++ + continue + } + run() + from, n = j, 1 + } + run() + rw.Header().Set("X-Magpie-Delta", key) + _, _ = rw.Write(body(items)) +} + func fail(rw http.ResponseWriter, err error) { rw.Header().Set("Content-Type", "application/json") rw.WriteHeader(http.StatusBadRequest) diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 648219877..ef13d1abc 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -93,6 +93,18 @@ const modelFavorites = new Set(Array.isArray(savedModelFavorites) ? savedModelFa const favoriteKey = (o) => o.ref || o.value; const isFavorite = (o) => modelFavorites.has(favoriteKey(o)) || modelFavorites.has(o.value); +// The reads the page asks for again and again and mostly finds the same +// (writeJSONSame in api.go): the tag of the answer last read goes with the +// next ask, and an answer that is the same comes back with no body and is +// the object read before. The Sessions tab's ~8 MB list was read, parsed and +// compared every 15 s over a long history. +const sameReads = new Map(); +const readsSame = (path) => /^sessions(\?|$)|^sessions\/stats\?/.test(path); +// each such answer's tag: two answers with tags are the same only when they +// are one object, so the Sessions tab needn't compare ~8 MB to know +const readTags = new WeakMap(); +const sameRead = (a, b) => a === b || !(readTags.has(a) && readTags.has(b)) && JSON.stringify(a) === JSON.stringify(b); + // heads, when given, is shown the answer's headers (loadQuotas' X-Magpie-Reading); // signal, when given, cancels the request (a read the reader moved on from) async function api(path, body, heads, signal) { @@ -102,13 +114,16 @@ async function api(path, body, heads, signal) { // reaches the default omp row (#1187). Only # is encoded: ? = & stay a // query string, which encodeURIComponent would turn into a 404. path = path.replace(/#/g, "%23"); + const same = body === undefined && readsSame(path); + const had = same ? sameReads.get(path) : undefined; const res = await fetch("/api/" + path, { method: body === undefined ? "GET" : "POST", - headers: { "Content-Type": "application/json" }, + headers: had ? { "Content-Type": "application/json", "X-Magpie-Had": had.tag } : { "Content-Type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body), signal, }); heads?.(res.headers); + if (had && res.ok && res.headers.get("X-Magpie-Same")) return had.data; if (res.status === 204) return null; // a body that isn't JSON (a proxy's or a plain http.Error) is the error // itself, not WebKit's "did not match the expected pattern" @@ -171,6 +186,16 @@ async function api(path, body, heads, signal) { // the accounts it names, for Hide accounts to know them (#568); a // session's or a library's words aren't looked through if (data && typeof data === "object" && !/^(sessions|library|skills|plugins|market)/.test(path)) noteAccounts(data); + // a list changed since the one the page has (writeJSONRows in api.go): + // only its new rows are sent, and [from, count] for each run of the old + const delta = had && res.headers.get("X-Magpie-Delta"); + const was = delta && had.data?.[delta]; + if (Array.isArray(was) && Array.isArray(data?.[delta])) data[delta] = data[delta].flatMap((r) => Array.isArray(r) ? was.slice(r[0], r[0] + r[1]) : [r]); + const tag = same && res.headers.get("X-Magpie-Tag"); + if (tag) { + sameReads.set(path, { tag, data }); + if (data && typeof data === "object") readTags.set(data, tag); + } return data; } diff --git a/internal/gui/assets/plugins/usage/page.js b/internal/gui/assets/plugins/usage/page.js index a2c47c518..228031948 100644 --- a/internal/gui/assets/plugins/usage/page.js +++ b/internal/gui/assets/plugins/usage/page.js @@ -2304,7 +2304,8 @@ async function loadSessions() { } finally { stop(); } if (range !== sessRange) return; // another range was picked meanwhile; its load draws const fresh = q === sessOverQ(); // no filter changed meanwhile - if (sessions && sessStats && JSON.stringify(s) === JSON.stringify(sessions) && JSON.stringify(st) === JSON.stringify(sessStats) && + // an answer the same as the last is the object read then (api's sameReads) + if (sessions && sessStats && sameRead(s, sessions) && sameRead(st, sessStats) && (!fresh || JSON.stringify(o) === JSON.stringify(sessOver))) return; sessions = s; sessStats = st; diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index ebaf5203a..b4031851d 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -140,7 +140,10 @@ func sessionRoutes(mux *http.ServeMux, w Windows) { j.Path = tilde(j.Path) out.Sessions = append(out.Sessions, j) } - writeJSON(rw, out) + writeJSONRows(rw, r, "sessions", out.Sessions, struct { + Terminal bool `json:"terminal"` + Dirs []string `json:"dirs"` + }{out.Terminal, out.Dirs}) }) // stats is every session's usage by day over the last ?days=N days // (every day when 0 or none), cut by agent, folder and model for the @@ -161,7 +164,7 @@ func sessionRoutes(mux *http.ServeMux, w Windows) { agents[a.Agent] = cmp.Or(names[a.Agent], a.Agent) } } - writeJSON(rw, struct { + writeJSONSame(rw, r, struct { sessions.Stats Agents map[string]string `json:"agents"` }{st, agents}) diff --git a/internal/gui/sessions_same_test.go b/internal/gui/sessions_same_test.go new file mode 100644 index 000000000..8983dd00a --- /dev/null +++ b/internal/gui/sessions_same_test.go @@ -0,0 +1,124 @@ +package gui + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/yetone/magpie/internal/sessions" +) + +// The Sessions tab asks for its list and stats every 15 s, and over a long +// history the list is ~8 MB the page parsed and compared to find it the +// same. An ask that says the tag of the answer it has gets no body while +// the answer is the same, and the whole answer once it isn't. +func TestSessionsSentOnceWhileTheSame(t *testing.T) { + h := sandboxHome(t) + proj := filepath.Join(h, ".claude", "projects", "-work") + if err := os.MkdirAll(proj, 0o755); err != nil { + t.Fatal(err) + } + write := func(id, at string) { + b := `{"parentUuid":null,"isSidechain":false,"type":"user","message":{"role":"user","content":"hi"},"uuid":"u","timestamp":"` + at + `","cwd":"/work","sessionId":"` + id + `"}` + "\n" + + `{"parentUuid":"u","isSidechain":false,"message":{"model":"claude-sonnet-5","id":"msg_` + id + `","type":"message","role":"assistant","content":[{"type":"text","text":"done"}],"usage":{"input_tokens":10,"output_tokens":2}},"requestId":"req_1","type":"assistant","uuid":"a","timestamp":"` + at + `","cwd":"/work","sessionId":"` + id + `"}` + "\n" + if err := os.WriteFile(filepath.Join(proj, id+".jsonl"), []byte(b), 0o644); err != nil { + t.Fatal(err) + } + } + write("00000001-1111-2222-3333-444444444444", "2026-09-20T10:00:00.000Z") + t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(h, ".claude")) + sessions.Reset() + forgetStats() + t.Cleanup(func() { sessions.Reset(); forgetStats() }) + + mux := http.NewServeMux() + sessionRoutes(mux, folderOnly{}) + get := func(path, had string) *httptest.ResponseRecorder { + r := httptest.NewRequest("GET", path, nil) + if had != "" { + r.Header.Set("X-Magpie-Had", had) + } + w := httptest.NewRecorder() + mux.ServeHTTP(w, r) + if w.Code != 200 { + t.Fatalf("%s: %d %s", path, w.Code, w.Body) + } + return w + } + for _, path := range []string{"/api/sessions", "/api/sessions/stats?days=0"} { + first := get(path, "") + tag := first.Header().Get("X-Magpie-Tag") + if tag == "" || first.Body.Len() == 0 || first.Header().Get("X-Magpie-Same") != "" { + t.Fatalf("%s: the first answer has tag %q and %d bytes, same %q", path, tag, first.Body.Len(), first.Header().Get("X-Magpie-Same")) + } + again := get(path, tag) + if again.Header().Get("X-Magpie-Same") != "1" || again.Body.Len() != 0 || again.Header().Get("X-Magpie-Tag") != tag { + t.Errorf("%s: asked with the answer's own tag, got same %q, %d bytes, tag %q", path, again.Header().Get("X-Magpie-Same"), again.Body.Len(), again.Header().Get("X-Magpie-Tag")) + } + if other := get(path, "0123"); other.Header().Get("X-Magpie-Same") != "" || other.Body.String() != first.Body.String() { + t.Errorf("%s: asked with another tag, got same %q and %d bytes, want the whole answer", path, other.Header().Get("X-Magpie-Same"), other.Body.Len()) + } + } + + // a session more: the tag the page has is of an answer gone + tags := map[string]string{} + bodies := map[string]string{} + for _, path := range []string{"/api/sessions", "/api/sessions/stats?days=0"} { + w := get(path, "") + tags[path], bodies[path] = w.Header().Get("X-Magpie-Tag"), w.Body.String() + } + write("00000002-1111-2222-3333-444444444444", "2026-09-21T10:00:00.000Z") + sessions.Reset() + forgetStats() + for path, had := range tags { + w := get(path, had) + if w.Header().Get("X-Magpie-Same") != "" || w.Body.Len() == 0 || w.Header().Get("X-Magpie-Tag") == had { + t.Errorf("%s: after a new session, asked with the old tag, got same %q, %d bytes, tag %q", path, w.Header().Get("X-Magpie-Same"), w.Body.Len(), w.Header().Get("X-Magpie-Tag")) + } + whole := get(path, "") + if path != "/api/sessions" { + if w.Body.String() != whole.Body.String() { + t.Errorf("%s: asked with the old tag, got %s, want the whole answer", path, w.Body) + } + continue + } + // the list sends the new session's row, and the old one as a run + if w.Header().Get("X-Magpie-Delta") != "sessions" { + t.Fatalf("the list after a new session: delta %q, want sessions", w.Header().Get("X-Magpie-Delta")) + } + var delta, old, want struct { + Sessions []json.RawMessage `json:"sessions"` + Terminal bool `json:"terminal"` + Dirs []string `json:"dirs"` + } + for _, b := range []struct { + s string + to any + }{{w.Body.String(), &delta}, {bodies[path], &old}, {whole.Body.String(), &want}} { + if err := json.Unmarshal([]byte(b.s), b.to); err != nil { + t.Fatal(err) + } + } + if len(delta.Sessions) != 2 || string(delta.Sessions[1]) != "[0,1]" || !strings.Contains(string(delta.Sessions[0]), "00000002") { + t.Fatalf("the delta's list is %s, want the new row and [0,1]", w.Body) + } + var back []json.RawMessage + for _, r := range delta.Sessions { + var run []int + if json.Unmarshal(r, &run) == nil { + back = append(back, old.Sessions[run[0]:run[0]+run[1]]...) + continue + } + back = append(back, r) + } + delta.Sessions = back + got, _ := json.Marshal(delta) + if wantB, _ := json.Marshal(want); string(got) != string(wantB) { + t.Errorf("the delta read on the old list is %s, want %s", got, wantB) + } + } +} diff --git a/internal/gui/tests/sessions-same.test.cjs b/internal/gui/tests/sessions-same.test.cjs new file mode 100644 index 000000000..dc17a59e2 --- /dev/null +++ b/internal/gui/tests/sessions-same.test.cjs @@ -0,0 +1,110 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// The Sessions tab reads its list and stats again every 15 s, and over a long +// history the list is ~8 MB, parsed and compared whole to find it the same. +// Each ask says the tag of the answer it has (X-Magpie-Had); an answer that +// is the same comes back with no body (X-Magpie-Same) and is the object read +// before, and one that changed is read and drawn. Chromium and WebKit, in +// English and Chinese; no backend, the API is faked. +const assert = require("node:assert/strict"); +const fs = require("node:fs/promises"); +const path = require("node:path"); +const { test } = require("node:test"); +const { chromium, webkit } = require("playwright"); + +const assets = path.resolve(__dirname, "../assets"); +const iso = (d) => [d.getFullYear(), d.getMonth() + 1, d.getDate()].map((n) => String(n).padStart(2, "0")).join("-"); +const now = new Date(); +const session = (id, title) => ({ agent: "claude", id, title, path: "/test/" + id + ".jsonl", cwd: "/work", start: now.toISOString(), t: now.getTime(), models: ["claude-opus-5-5"], tokens: { input: 10, output: 2 }, name: "Claude Code", icon: "claude-color" }); + +function serve(lang, back) { + const state = { agents: [{ id: "claude", name: "Claude Code", path: "/test/settings.json", fields: [] }], profiles: [], settings: { lang, theme: "light" } }; + return async (route) => { + const req = route.request(); + const url = new URL(req.url()); + const json = (data) => route.fulfill({ json: data }); + // as writeJSONSame answers: the tag of the bytes, and no body for the + // tag the page has; and as writeJSONRows, the list (rows) asked with + // the tag of one sent before has only its new rows, and [from, count] + // for each run of the old + const hash = (text) => String(text.length) + ":" + [...text].reduce((h, c) => (h * 31 + c.charCodeAt(0)) >>> 0, 7); + const same = (data, rows) => { + const body = JSON.stringify(data); + const tag = hash(body); + const had = req.headers()["x-magpie-had"]; + back.asks.push({ path: url.pathname, had: had || "" }); + if (had === tag) return route.fulfill({ status: 200, headers: { "X-Magpie-Tag": tag, "X-Magpie-Same": "1" }, body: "" }); + back.bodies++; + const was = rows && back.sent.get(had); + if (rows) back.sent.set(tag, data[rows].map((r) => JSON.stringify(r))); + if (!was) return route.fulfill({ status: 200, headers: { "X-Magpie-Tag": tag, "Content-Type": "application/json" }, body }); + back.deltas++; + const list = []; + for (const r of data[rows]) { + const j = was.indexOf(JSON.stringify(r)); + const last = list.at(-1); + if (j < 0) list.push(r); + else if (Array.isArray(last) && last[0] + last[1] === j) last[1]++; + else list.push([j, 1]); + } + return route.fulfill({ status: 200, headers: { "X-Magpie-Tag": tag, "X-Magpie-Delta": rows, "Content-Type": "application/json" }, body: JSON.stringify({ ...data, [rows]: list }) }); + }; + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:${JSON.stringify(lang)},theme:"light",web:true};` }); + if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json(state); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true } }); + if (url.pathname === "/api/usage/quotas") return json([]); + if (url.pathname === "/api/sessions/progress") return json({ indexing: false }); + if (url.pathname === "/api/sessions") return same({ sessions: back.sessions, dirs: ["/test/sessions"] }, "sessions"); + if (url.pathname === "/api/sessions/stats") return same({ from: iso(now), to: iso(now), days: [{ date: iso(now), usage: [], active: [] }], agents: { claude: "Claude Code" } }); + if (url.pathname === "/api/groups") return json({ groups: [] }); + if (url.pathname.startsWith("/api/")) return json({}); + const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname); + const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)]; + const body = await fs.readFile(file).catch(() => null); + await (body ? route.fulfill({ body, contentType }) : route.fulfill({ status: 404, body: "" })); + }; +} + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of ["en", "zh"]) { + test(`${engine} ${lang}: the Sessions tab's list and stats are sent again only when they changed`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + const page = await (await browser.newContext({ viewport: { width: 1100, height: 800 }, reducedMotion: "reduce" })).newPage(); + page.setDefaultTimeout(5000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + const back = { sessions: [session("s1", "first session")], asks: [], bodies: 0, deltas: 0, sent: new Map() }; + await page.route("**/*", serve(lang, back)); + await page.addInitScript(() => { try { localStorage.setItem("magpie.usageTab", "sessions"); localStorage.setItem("magpie.sessRange", "all"); } catch {} }); + await page.goto("http://magpie.test/?view=usage"); + await page.waitForFunction(() => typeof sessions !== "undefined" && sessions?.sessions?.length === 1 && sessStats); + assert.equal(back.bodies, 2, "the first read is sent whole"); + + // read again, unchanged: no body, and the very objects the tab has + const before = await page.evaluateHandle(() => [sessions, sessStats]); + back.asks.length = 0; + await page.evaluate(() => loadSessions()); + assert.equal(back.bodies, 2, "an unchanged read is sent again"); + assert.deepEqual(back.asks.map((a) => [a.path, a.had !== ""]).sort(), [["/api/sessions", true], ["/api/sessions/stats", true]]); + assert.equal(await page.evaluate(([s, st]) => s === sessions && st === sessStats, before), true); + + // a session more: only its row is sent, the one before is the row the + // tab has, and the list is drawn; the stats still aren't sent + back.sessions = [session("s2", "second session"), ...back.sessions]; + await page.evaluate(() => loadSessions()); + assert.equal(back.bodies, 3); + assert.equal(back.deltas, 1, "the list is sent whole for one new row"); + assert.equal(await page.evaluate(() => sessions.sessions.map((s) => s.id).join()), "s2,s1"); + assert.equal(await page.evaluate(([s]) => sessions.sessions[1] === s.sessions[0] && sessions !== s, before), true); + await page.waitForFunction(() => document.querySelector("#view-usage")?.textContent.includes("second session")); + assert.equal(await page.evaluate(([, st]) => st === sessStats, before), true); + // and the tag of that new list is the one asked with next + back.asks.length = 0; + await page.evaluate(() => loadSessions()); + assert.equal(back.bodies, 3); + assert.deepEqual(errors, []); + }); + } +}