cryingLiu on X asked to search the chat history of every agent magpie
reads sessions from: prompts and replies, from one place.
What the user gets:
- The Sessions page's search also lists, under the agent's own list,
the other agents' sessions whose conversation says the words, each
with a snippet. A hit opens the conversation in the sheet at the
message that said it (transcript?find=), with "N earlier messages
not shown"; the page itself never scrolls. While the index is still
being built the head says "Indexing sessions…" or "Indexing N of M
sessions…", and the page asks again every 3s until it is done.
- Magpie Sessions, a built-in MCP server in Library → MCP (Discover),
opt in per agent like the other magpie servers. Its tools are
search_sessions(query, agent?, limit) and read_session(id, around?),
both returning text. It runs as `magpie mcp sessions` over stdio and
reaches the gateway's /mcp/magpie/sessions, which serves only local
callers that are not browsers.
How it works:
- internal/sessions/said_index.go keeps said-v1.db beside sessions.json:
a contentless SQLite FTS5 index (detail=none) of the words each
session says. It is built from the sessions list sessions already
caches and refreshed at most every 10s: a new or rewritten file is
read once, a grown Claude Code or Codex file only from where it ended,
and a gone one is forgotten. A query asks the index which sessions
have every word, then reads only those files for the snippet, for at
most 6s. A search waits up to 2s for a refresh. While the first
listing is still running the answer says `listing`, so an empty first
answer is not read as "no session says it".
- Privacy: only agents sessions.Reads allows are indexed or searched;
nothing is read until the user has answered the sessions question,
and in gateway mode no agent's own files are read (the page doesn't
ask). Reset forgets the index.
- /api/sessions/said for the page; the CLI parity list names it.
Run, at this tree (rebased on origin/main 256910f17):
- gofmt -l clean; go vet on mote for darwin, GOOS=linux -tags nogui
and GOOS=windows: ok.
- go test -tags nogui ./... -timeout 45m on the WSL Linux box:
every package ok at c8e445c4b (the tree before the one-hit wording,
whose gateway tests ran again on mote: ok).
- GUI: the whole suite on mica at this head was still running when
this was pushed (owner: never hold a verified change behind a full
suite). Under mica's load, beside another session's full suite, 90
tests had failed so far, all in files this change doesn't touch
(accounts, Details, keys, plugins, providers); none in sessions,
library or i18n. The files this change reaches passed on their own
(below).
- Focused, green: internal/sessions (Said*, TranscriptAt),
internal/gateway (SessionsMCP*, own MCP, read-only, key slots),
internal/library (market, opt in), internal/gui (Said, Sessions,
gateway mode); GUI sessions-said, sessions-search, library-sessions-mcp,
vision-mcp-library, library-browser-setup and i18n-keys in chromium
and webkit, every language, wide and narrow.
- Broken by hand, one at a time, each red and green restored: the
index's Listing left out (TestSearchSaidWhileTheSessionsAreFound red
in 2.5s); the page ignoring `listing` (sessions-said 6 of 7 red); the
MCP's local/browser guard (TestSessionsMCPOnlyForAgentsHere red); the
server's opt in (TestSessionsServerInDiscover red); every Reads check
on the search path, the index's and SearchSession's together
(TestSearchSaidLeavesOutAgentsNotRead red). With those gone the
gateway-mode GUI test stays green, because the sessions listing
leaves an unread agent out before the index sees it.
- Real use, in a sandbox HOME with GOPATH, GOMODCACHE and GOCACHE
pinned, over the real history through symlinks (29 GB Claude Code,
60 GB Codex, 14,102 sessions), sessions reads set to all in the
sandbox: first listing ~60s cold (answers say still being found),
whole index built in 9m50s while queries answered in ~6s each, index
38 MB, a common word 4.9s and a rare word 1.0s warm, read_session at
the words of a huge session 1.8s, peak footprint 442 MB for the whole
process with the sessions cache. Nothing was written under the real
~/.claude or ~/.codex.
- The real binary end to end on mote, in a sandbox HOME: `magpie serve`
on a free port and `magpie mcp sessions` over stdio with
MAGPIE_GATEWAY, against a Claude Code session written there:
initialize, tools/list, search_sessions found it with its snippet,
read_session at the words started 3 messages before them, a word
nowhere said "No session says", and a POST with a web page's Origin
was refused 403. That run is how "1 sessions say" was found and
made "1 session says" (TestSessionsMCPSearch red before, green after).
Not tried: a real agent (Claude Code, Codex) calling the server it was
given from the Library, and the page against a real backend (the GUI
tests fake the API). The index on Windows and Linux ran only through
the Go suite.
243 lines
10 KiB
Go
243 lines
10 KiB
Go
package gui
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/sessions"
|
||
"github.com/yetone/magpie/internal/settings"
|
||
"github.com/yetone/magpie/internal/usage"
|
||
)
|
||
|
||
// paultian on Discord: in gateway mode the Sessions page and Usage ›
|
||
// Sessions list what came through this gateway from other computers,
|
||
// never this computer's own agents' session files, which a machine put in
|
||
// gateway mode with Claude Code and Codex installed still listed. Every
|
||
// route that reaches a session is asked: the list, one, the transcript,
|
||
// Markdown and its export, the stats and the overview, search, manage
|
||
// and its trash, delete, restore, purge, carry and moving Claude Code's folder.
|
||
// The gateway's session shows on each. Gateway mode off, as before.
|
||
func TestGatewayModeListsNoOwnSessions(t *testing.T) {
|
||
h := sandboxHome(t)
|
||
claude := filepath.Join(h, ".claude")
|
||
codex := filepath.Join(h, ".codex")
|
||
t.Setenv("CLAUDE_CONFIG_DIR", claude)
|
||
t.Setenv("CODEX_HOME", codex)
|
||
if err := os.MkdirAll(filepath.Join(h, "Downloads"), 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
proj := filepath.Join(claude, "projects", "-work")
|
||
if err := os.MkdirAll(proj, 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
at := time.Now().Add(-time.Hour).Truncate(time.Second)
|
||
claudeSession := func(id, text string) string {
|
||
ts := at.UTC().Format("2006-01-02T15:04:05.000Z")
|
||
p := filepath.Join(proj, id+".jsonl")
|
||
b := `{"parentUuid":null,"isSidechain":false,"type":"user","message":{"role":"user","content":"` + text + `"},"uuid":"u` + id + `","timestamp":"` + ts + `","cwd":"/work","sessionId":"` + id + `"}` + "\n" +
|
||
`{"parentUuid":"u` + id + `","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_` + id + `","type":"assistant","uuid":"a` + id + `","timestamp":"` + ts + `","cwd":"/work","sessionId":"` + id + `"}` + "\n"
|
||
if err := os.WriteFile(p, []byte(b), 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
os.Chtimes(p, at, at)
|
||
return p
|
||
}
|
||
const local, trashed, codexID = "11111111-1111-2222-3333-444444444444", "22222222-1111-2222-3333-444444444444", "019a0000-0000-7000-8000-00000000c0de"
|
||
localFile := claudeSession(local, "my local secret plan")
|
||
claudeSession(trashed, "an old local plan")
|
||
writeCodexAPITestThread(t, codex, codexID, codexID, "", []codexTestCall{{response: "resp_1", model: "gpt-5.5", effort: "high", at: at, input: 100, output: 10}})
|
||
sessions.Reset()
|
||
t.Cleanup(sessions.Reset)
|
||
|
||
mux := http.NewServeMux()
|
||
sessionRoutes(mux, folderOnly{})
|
||
sessionManageRoutes(mux, folderOnly{})
|
||
gatewayModeRoutes(mux)
|
||
call := func(method, path, body string) (int, string) {
|
||
t.Helper()
|
||
w := httptest.NewRecorder()
|
||
mux.ServeHTTP(w, httptest.NewRequest(method, path, strings.NewReader(body)))
|
||
return w.Code, w.Body.String()
|
||
}
|
||
ok := func(method, path, body string) string {
|
||
t.Helper()
|
||
code, b := call(method, path, body)
|
||
if code != 200 {
|
||
t.Fatalf("%s %s: %d %s", method, path, code, b)
|
||
}
|
||
return b
|
||
}
|
||
|
||
// a session another computer's Pi sent through this gateway, its
|
||
// conversation recorded
|
||
ok("POST", "/api/sessions/recording", `{"on":true}`)
|
||
const remote = "ses_remote_pi_1"
|
||
usage.Append(usage.Record{Time: at, Agent: "pi", Session: remote, Provider: "fake", Model: "m", Input: 50, Output: 5, Status: 200})
|
||
if err := sessions.SaveGatewayTurn(sessions.GatewayTurn{Agent: "pi", Session: remote, Time: at,
|
||
Input: []sessions.Part{{Role: "user", Kind: "text", Text: "rename foo to bar remotely"}},
|
||
Output: []sessions.Part{{Role: "assistant", Kind: "text", Text: "Renamed it."}}}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
listed := func() map[string]bool {
|
||
t.Helper()
|
||
var out struct{ Sessions []sessions.Session }
|
||
if err := json.Unmarshal([]byte(ok("GET", "/api/sessions", "")), &out); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
m := map[string]bool{}
|
||
for _, s := range out.Sessions {
|
||
m[s.Agent+":"+s.ID] = true
|
||
}
|
||
return m
|
||
}
|
||
|
||
// gateway mode off (the app's windows, or Settings' Off): every one
|
||
if _, err := sessions.Delete("claude", trashed); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if l := listed(); !l["claude:"+local] || !l["codex:"+codexID] || !l["pi:"+remote] {
|
||
t.Fatalf("gateway mode off: listed %v", l)
|
||
}
|
||
for _, key := range []string{"claude:" + local, "codex:" + codexID} {
|
||
ok("GET", "/api/sessions/one?key="+key, "")
|
||
}
|
||
if b := ok("GET", "/api/sessions/transcript?agent=claude&id="+local, ""); !strings.Contains(b, "secret plan") {
|
||
t.Fatalf("gateway mode off: the local transcript: %s", b)
|
||
}
|
||
// every agent's sessions searched at once, and one read where the
|
||
// words were said
|
||
if b := ok("GET", "/api/sessions/said?q=secret+plan", ""); !strings.Contains(b, `"id":"`+local+`"`) || !strings.Contains(b, `"agent":"claude"`) {
|
||
t.Fatalf("gateway mode off: every agent's search: %s", b)
|
||
}
|
||
if b := ok("GET", "/api/sessions/transcript?agent=claude&id="+local+"&find=secret+plan", ""); !strings.Contains(b, "secret plan") {
|
||
t.Fatalf("gateway mode off: the transcript at the words: %s", b)
|
||
}
|
||
if b := ok("GET", "/api/sessions/stats?days=0", ""); !strings.Contains(b, `"claude"`) {
|
||
t.Fatalf("gateway mode off: stats leave out Claude Code: %s", b)
|
||
}
|
||
var trash struct {
|
||
Trash []struct{ Key, ID string }
|
||
}
|
||
json.Unmarshal([]byte(ok("GET", "/api/sessions/manage?agent=claude", "")), &trash)
|
||
trashKey := ""
|
||
for _, tr := range trash.Trash {
|
||
if tr.ID == trashed {
|
||
trashKey = tr.Key
|
||
}
|
||
}
|
||
if trashKey == "" {
|
||
t.Fatalf("gateway mode off: the trash has no %s: %+v", trashed, trash)
|
||
}
|
||
|
||
// magpie web's page, put in gateway mode by Settings
|
||
webPage.Store(true)
|
||
t.Cleanup(func() {
|
||
webPage.Store(false)
|
||
ownWas.Store(0)
|
||
sessions.Reset()
|
||
forgetStats()
|
||
})
|
||
ok("POST", "/api/settings/gateway-mode", `{"mode":"on"}`)
|
||
if settings.Load().GatewayMode != "on" {
|
||
t.Fatal("gateway mode wasn't saved")
|
||
}
|
||
ownLeft := func(what, body string) {
|
||
t.Helper()
|
||
for _, s := range []string{local, trashed, codexID, "secret plan", "old local plan", `"claude"`, `"codex"`, "/work"} {
|
||
if strings.Contains(body, s) {
|
||
t.Fatalf("gateway mode: %s says %q:\n%s", what, s, body)
|
||
}
|
||
}
|
||
}
|
||
if l := listed(); len(l) != 1 || !l["pi:"+remote] {
|
||
t.Fatalf("gateway mode: listed %v, want the gateway's session alone", l)
|
||
}
|
||
b := ok("GET", "/api/sessions", "")
|
||
ownLeft("the list", b)
|
||
if strings.Contains(b, filepath.ToSlash(claude)) || strings.Contains(b, ".claude") || strings.Contains(b, ".codex") {
|
||
t.Fatalf("gateway mode: the list names this computer's folders: %s", b)
|
||
}
|
||
for _, days := range []string{"0", "7"} {
|
||
ownLeft("stats", ok("GET", "/api/sessions/stats?days="+days, ""))
|
||
b = ok("GET", "/api/sessions/overview?days="+days, "")
|
||
ownLeft("the overview", b)
|
||
if !strings.Contains(b, remote) {
|
||
t.Fatalf("gateway mode: the overview leaves out the gateway's session: %s", b)
|
||
}
|
||
}
|
||
for _, key := range []string{"claude:" + local, "codex:" + codexID} {
|
||
if code, b := call("GET", "/api/sessions/one?key="+key, ""); code == 200 {
|
||
t.Fatalf("gateway mode: one %s: %d %s", key, code, b)
|
||
}
|
||
}
|
||
ok("GET", "/api/sessions/one?key=pi:"+remote, "")
|
||
for _, q := range []string{"agent=claude&id=" + local, "agent=codex&id=" + codexID} {
|
||
for _, p := range []string{"GET /api/sessions/transcript", "GET /api/sessions/markdown", "POST /api/sessions/export"} {
|
||
method, path, _ := strings.Cut(p, " ")
|
||
if code, b := call(method, path+"?"+q, "{}"); code == 200 || strings.Contains(b, "secret") {
|
||
t.Fatalf("gateway mode: %s?%s: %d %s", p, q, code, b)
|
||
}
|
||
}
|
||
}
|
||
if b := ok("GET", "/api/sessions/transcript?agent=pi&id="+remote, ""); !strings.Contains(b, "rename foo") {
|
||
t.Fatalf("gateway mode: the gateway's transcript: %s", b)
|
||
}
|
||
if b := ok("GET", "/api/sessions/markdown?agent=pi&id="+remote, ""); !strings.Contains(b, "rename foo") {
|
||
t.Fatalf("gateway mode: the gateway's Markdown: %s", b)
|
||
}
|
||
ownLeft("search", ok("GET", "/api/sessions/search?agent=claude&q=secret", ""))
|
||
ownLeft("every agent's search", ok("GET", "/api/sessions/said?q=secret", ""))
|
||
if b := ok("GET", "/api/sessions/search?agent=pi&q=remotely", ""); !strings.Contains(b, remote) {
|
||
t.Fatalf("gateway mode: search leaves out the gateway's session: %s", b)
|
||
}
|
||
for _, agent := range []string{"", "claude", "codex"} {
|
||
b = ok("GET", "/api/sessions/manage?agent="+agent, "")
|
||
ownLeft("manage", b)
|
||
var m struct {
|
||
Agents []struct{ Agent string }
|
||
Reads readsJSON
|
||
}
|
||
json.Unmarshal([]byte(b), &m)
|
||
if len(m.Agents) != 1 || m.Agents[0].Agent != "pi" || !m.Reads.Gateway || len(m.Reads.Found) != 0 {
|
||
t.Fatalf("gateway mode: manage = %s", b)
|
||
}
|
||
}
|
||
if b := ok("GET", "/api/sessions/reads", ""); !strings.Contains(b, `"gateway":true`) || strings.Contains(b, `"claude"`) {
|
||
t.Fatalf("gateway mode: reads = %s", b)
|
||
}
|
||
b = ok("POST", "/api/sessions/delete", `{"agent":"claude","ids":["`+local+`"]}`)
|
||
if _, err := os.Stat(localFile); err != nil || !strings.Contains(b, `"refused":[{`) {
|
||
t.Fatalf("gateway mode: delete of a local session: %s (%v)", b, err)
|
||
}
|
||
if code, b := call("POST", "/api/sessions/carry", `{"agent":"claude","id":"`+local+`","to":"codex"}`); code == 200 {
|
||
t.Fatalf("gateway mode: carry: %s", b)
|
||
}
|
||
if code, b := call("POST", "/api/sessions/restore", `{"key":"`+trashKey+`"}`); code == 200 {
|
||
t.Fatalf("gateway mode: restore of a local session: %s", b)
|
||
}
|
||
for _, body := range []string{`{"keys":["` + trashKey + `"]}`, `{"all":true}`} {
|
||
if b := ok("POST", "/api/sessions/purge", body); !strings.Contains(b, `"purged":[]`) {
|
||
t.Fatalf("gateway mode: purge of a local session: %s", b)
|
||
}
|
||
}
|
||
if code, b := call("POST", "/api/sessions/relocate-claude", `{"from":"/work","to":"/elsewhere"}`); code == 200 {
|
||
t.Fatalf("gateway mode: relocating Claude Code's folder: %s", b)
|
||
}
|
||
|
||
// Off again: this computer's sessions are back at once
|
||
ok("POST", "/api/settings/gateway-mode", `{"mode":"off"}`)
|
||
if l := listed(); !l["claude:"+local] || !l["codex:"+codexID] || !l["pi:"+remote] {
|
||
t.Fatalf("gateway mode turned off: listed %v", l)
|
||
}
|
||
if b := ok("GET", "/api/sessions/manage?agent=claude", ""); !strings.Contains(b, trashed) {
|
||
t.Fatalf("gateway mode turned off: the trash lost the local session: %s", b)
|
||
}
|
||
}
|