The RTK tab says what RTK saved (rtk gain); the billion-context tab said nothing of its effect. bili 0.1.213 has no command that prints its savings. Its running proxy serves its dashboard's numbers at GET /__bili/overview, summed over every session in its one store, and each bili that runs registers in <state>/instances/<id>.json ($XDG_STATE_HOME or ~/.local/state, then billion-context, on every system; read from bili's dist and its Hermes plugin's twin reader). readBiliSaved (internal/library/bili_saved.go) asks the registered bilis newest first, loopback http origins only, 3s in all, and puts the first answer on BiliView.Saved: netSavedTotal (bili's headline "net savings", after post-fold rebuilds and the summaries' cost, only with hasFoldData), grossSavedTotal (which bili calls an upper bound), foldsTotal, sessions, requests. The answer is kept in bili-saved.json in magpie's config folder and shown "as of" its time when no bili runs; a kept file that doesn't parse is no answer. magpie never starts a bili to ask (it would auto-update and start watchers). The card says "166.3k tokens saved over 7 folds in 1 session", bili's own count over every session on this computer, the gross before costs; "Nothing folded yet over N requests" before a fold; and that it shows once bili has run when no bili ever answered. zh, zh-TW, ja and de; site docs (en, zh, ja) and docs/subsystems/library.md. Run: - Real bili 0.1.213 on mote in a sandbox HOME (ACP_AUTO_UPDATE=0, 20000 token window, bili start on 18799, a fake OpenAI upstream answering usage = bytes/4): ten turns of a growing conversation through /bili/<upstream>/ gave overview net 166285, gross 166390, 7 folds, 1 session, 10 requests. That literal answer is the Go fixture (testdata/bili/overview-0.1.213.json) and the GUI test's numbers. readBiliSaved/ReadBili run on mote against that live bili with that HOME read the same numbers (Live true, Version 0.1.213). - go test -tags nogui ./internal/library ./internal/gui on mote: ok. - win-run go test -run 'TestBiliSaved|TestBiliLoopback' ./internal/library on xiaomi-gpu: ok. - library-bili, i18n-keys and i18n-en-no-cjk in chromium and webkit (every language, 1100 and 420 wide): 92 pass, 0 fail. - gofmt -l clean; go vet darwin, GOOS=linux -tags nogui and GOOS=windows for internal/library and internal/gui: ok. - site/public/llms-full.txt regenerated (node llms-full.mjs); site worker.test.mjs: 38 of 38 ok. Broken by hand: - the loopback filter taken out: TestBiliSavedFromRunningBili red (192.0.2.7 asked first, the 3s budget gone, nothing read). - net read from tokensSaved, and a kept answer marked live: TestBiliSavedFromRunningBili and TestBiliSavedOnThePage red. - the card's line taken out: library-bili 40 of 42 red (chromium). Not tried: a real agent session with a real model through bili (the upstream was a fake with made-up usage, so only the plumbing of bili's numbers is checked, not how much a real session saves); a bili with money data (hasMoneyData; its dollars are passed as usd but not shown); the card on Linux WebKitGTK.
176 lines
5.6 KiB
Go
176 lines
5.6 KiB
Go
package library
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// biliOverviewFixture is GET /__bili/overview as bili 0.1.213 answered it
|
|
// on mote, after ten turns of a growing conversation through its /bili/
|
|
// prefix with a 20000-token window: seven folds, net 166285.
|
|
func biliOverviewFixture(t *testing.T) []byte {
|
|
b, err := os.ReadFile(filepath.Join("testdata", "bili", "overview-0.1.213.json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
// biliRegister writes an instance file as bili 0.1.213 writes it.
|
|
func biliRegister(t *testing.T, h, id, origin string, startedAt int64) {
|
|
t.Helper()
|
|
write(t, filepath.Join(h, ".local", "state", "billion-context", "instances", id+".json"),
|
|
fmt.Sprintf(`{"origin":%q,"instanceId":%q,"pid":86206,"startedAt":%d,"host":"127.0.0.1","port":18799,"passthrough":false,"mitmDomains":[],"modelWindows":{},"codeFingerprint":"9b3c7d02","sessionsDir":%q}`,
|
|
origin, id, startedAt, filepath.Join(h, ".local", "share", "billion-context", "sessions")))
|
|
}
|
|
|
|
func biliServeOverview(t *testing.T, body []byte) *httptest.Server {
|
|
t.Helper()
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/__bili/overview" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write(body)
|
|
}))
|
|
t.Cleanup(s.Close)
|
|
return s
|
|
}
|
|
|
|
// TestBiliSavedFromRunningBili: the numbers are bili's own, read from the
|
|
// newest registered bili that answers; one left behind by a crash, and
|
|
// one off this computer, are passed over; the answer is kept for when no
|
|
// bili runs.
|
|
func TestBiliSavedFromRunningBili(t *testing.T) {
|
|
h := sandbox(t)
|
|
now := time.Date(2026, 11, 10, 7, 21, 30, 0, time.UTC)
|
|
was := biliNow
|
|
biliNow = func() time.Time { return now }
|
|
t.Cleanup(func() { biliNow = was })
|
|
|
|
live := biliServeOverview(t, biliOverviewFixture(t))
|
|
gone := httptest.NewServer(http.NotFoundHandler())
|
|
gone.Close()
|
|
biliRegister(t, h, "live", live.URL, 1794295272947)
|
|
biliRegister(t, h, "gone", gone.URL, 1794295272999)
|
|
biliRegister(t, h, "far", "http://192.0.2.7:8787", 1794295273999)
|
|
|
|
fetch := biliFetch
|
|
var asked []string
|
|
biliFetch = func(ctx context.Context, origin string) (*biliOverview, error) {
|
|
asked = append(asked, origin)
|
|
return fetch(ctx, origin)
|
|
}
|
|
t.Cleanup(func() { biliFetch = fetch })
|
|
|
|
s := readBiliSaved()
|
|
if s == nil {
|
|
t.Fatal("nothing read from a running bili")
|
|
}
|
|
want := BiliSaved{Net: 166285, Gross: 166390, HasFolds: true, Folds: 7, Sessions: 1, Requests: 10, Input: 175279,
|
|
At: "2026-11-10T07:21:30Z", Live: true, Version: "0.1.213"}
|
|
if *s != want {
|
|
t.Fatalf("read\n%+v\nwant\n%+v", *s, want)
|
|
}
|
|
if strings.Join(asked, " ") != gone.URL+" "+live.URL {
|
|
t.Fatalf("asked %q, want the crashed one then the live one, never 192.0.2.7", asked)
|
|
}
|
|
|
|
// no bili runs: the last answer, marked as kept
|
|
live.Close()
|
|
asked = nil
|
|
s = readBiliSaved()
|
|
want.Live = false
|
|
if s == nil || *s != want {
|
|
t.Fatalf("with no bili running read %+v, want %+v", s, want)
|
|
}
|
|
|
|
// a kept answer that doesn't parse is no answer, never zeros
|
|
write(t, filepath.Join(settings.Dir(), "bili-saved.json"), `{"net":`)
|
|
if s := readBiliSaved(); s != nil {
|
|
t.Fatalf("read %+v from a half-written file", *s)
|
|
}
|
|
}
|
|
|
|
// TestBiliSavedNoFolds: before bili has folded anything there is no net
|
|
// figure, as its dashboard shows none; the requests still count.
|
|
func TestBiliSavedNoFolds(t *testing.T) {
|
|
h := sandbox(t)
|
|
var o map[string]any
|
|
if err := json.Unmarshal(biliOverviewFixture(t), &o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ov := o["overview"].(map[string]any)
|
|
ov["hasFoldData"], ov["netSavedTotal"], ov["grossSavedTotal"], ov["foldsTotal"], ov["tokensSaved"] = false, 0, 0, 0, 0
|
|
ov["requests"], ov["inputTokens"] = 3, 1200
|
|
b, _ := json.Marshal(o)
|
|
live := biliServeOverview(t, b)
|
|
biliRegister(t, h, "live", live.URL, 1)
|
|
|
|
s := readBiliSaved()
|
|
if s == nil || s.HasFolds || s.Net != 0 || s.Folds != 0 || s.Requests != 3 || s.Input != 1200 || !s.Live {
|
|
t.Fatalf("read %+v", s)
|
|
}
|
|
}
|
|
|
|
// TestBiliSavedNoneYet: no bili has run here, so nothing is said.
|
|
func TestBiliSavedNoneYet(t *testing.T) {
|
|
sandbox(t)
|
|
fetch := biliFetch
|
|
biliFetch = func(context.Context, string) (*biliOverview, error) {
|
|
t.Fatal("asked with no bili registered")
|
|
return nil, nil
|
|
}
|
|
t.Cleanup(func() { biliFetch = fetch })
|
|
if s := readBiliSaved(); s != nil {
|
|
t.Fatalf("read %+v with no bili", *s)
|
|
}
|
|
}
|
|
|
|
// TestBiliSavedOnThePage: the Library's billion-context card gets it.
|
|
func TestBiliSavedOnThePage(t *testing.T) {
|
|
h := sandbox(t)
|
|
newFakeBili(t, h)
|
|
live := biliServeOverview(t, biliOverviewFixture(t))
|
|
biliRegister(t, h, "live", live.URL, 1)
|
|
v := readBili(nil)
|
|
if v.Saved == nil || v.Saved.Net != 166285 {
|
|
t.Fatalf("saved %+v", v.Saved)
|
|
}
|
|
b, _ := json.Marshal(v)
|
|
if !strings.Contains(string(b), `"saved":{"net":166285,"gross":166390,"hasFolds":true,"folds":7,`) {
|
|
t.Fatalf("the page gets %s", b)
|
|
}
|
|
}
|
|
|
|
func TestBiliLoopback(t *testing.T) {
|
|
for origin, want := range map[string]bool{
|
|
"http://127.0.0.1:18799": true,
|
|
"http://localhost:8787": true,
|
|
"http://[::1]:8787": true,
|
|
"http://127.0.0.1:18799/": true,
|
|
"http://192.0.2.7:8787": false,
|
|
"https://127.0.0.1:8787": false,
|
|
"http://evil.localhost:8787": false,
|
|
"http://127.0.0.1.nip.io:80": false,
|
|
"http://127.0.0.1": false,
|
|
"http://127.0.0.1:1/__bili/x": false,
|
|
"": false,
|
|
} {
|
|
if got := biliLoopback(origin); got != want {
|
|
t.Errorf("biliLoopback(%q) = %v, want %v", origin, got, want)
|
|
}
|
|
}
|
|
}
|