Files
magpie/internal/gui/sessions_reads.go
T
yetone f44fa2ef6a sessions: gateway mode lists only the gateway's sessions, never this computer's agents' files
paultian on Discord: magpie web in gateway mode, on a machine with Claude
Code and Codex installed, listed that machine's own sessions beside the
ones other computers sent through it. Owner's call (via the lead): keep
Sessions in gateway mode, scope it.

- sessions.OwnOff (set by the gui to ownSessionsOff): while it holds,
  currentReads answers "none", Found and Dirs are empty, the trash lists
  and restores/purges only what the gateway kept, and moving Claude
  Code's folder is refused. Every route built on Reads follows: the list,
  one, transcript, markdown, export, stats, overview, search, manage,
  delete, carry, Usage's agent-file calls. Gateway-recorded sessions
  (ledger projection, recorded conversations) are not behind the gate.
- ownSessionsOff: magpie web's page, and Settings' gateway mode On, or
  Automatic with --gateway or no agents found (looked at once per 30s,
  and again on another HOME/PATH). A turn of the answer resets the
  sessions' caches and stats; the gateway-mode route does it at once.
- /api/sessions/reads says gateway:true: the page asks nothing, its empty
  state names the gateway's sessions (en/zh/zh-TW/ja/de), and Settings ›
  Privacy leaves out Agents' session history.
- The gateway-mode subtitle (5 languages), docs/reference.md and
  site docs/docker.html (en/zh/ja) say Sessions stays and what it lists
  (they said there was no Sessions tab, which was already wrong);
  docs/subsystems/gateway-sessions.md has the design.
- Tests: Handler marks the process as magpie web for good, and 13 tests
  served magpie web's page, so a later test's sandbox could be in gateway
  mode by itself. webPageHandler ends the mark with the test, and
  TestWebPageHandlerEveryWhere fails on a direct Handler(webHost{...}).

Ran (mote, at this tree rebased on origin/main):
- TestGatewayModeListsNoOwnSessions: red on the old code, green now.
- gateway-mode-sessions.test.cjs: chromium + webkit, en 900/390, zh,
  zh-TW, ja 420, de 390: 36/36; red on the old client.js (every case).
- GUI files the change reaches (gateway-mode*, sessions-*, session-*,
  privacy-*, usage-sessions-*, i18n-keys), both engines: 657/657.
- TestCoreScriptBudget ok.
- go test -shuffle=on ./internal/gui ./internal/sessions: sessions ok;
  gui ok but TestMainWindowCorners/macOS_26_SDK, red the same on
  origin/main 2f726b1f9 on mote (#3949).
- go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet of both
  packages; gofmt -l clean.

Broken by hand: OwnOff answering false (the Go test goes red on the list,
stats, manage, one, transcript); the Purge guard and the Restore guard
each (red on purge / restore); the old client.js and settings.js (the GUI
test red). An injected stale agents memo and leaked webPage did not
reproduce the one TestUsageRequestContent red seen in the first package
run; it did not come back in two more runs, one shuffled.

Not tried: a real magpie web on a server with real agents on other
computers; the Go test on Windows and Linux boxes; the 30s memo under
real use.
2026-11-04 20:20:41 +08:00

91 lines
2.6 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"slices"
"sort"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/settings"
)
// Which agents' session history magpie may read (RustCats on X): the
// Sessions page asks once on a fresh install, and Settings changes it per
// agent later. sessions.Reads is the gate every reader asks.
type readAgentJSON struct {
Agent string `json:"agent"`
Name string `json:"name"`
Icon string `json:"icon"`
On bool `json:"on"`
}
type readsJSON struct {
// Asked: the user said which agents may be read (or the install read
// them all before there was a choice)
Asked bool `json:"asked"`
// All: every agent, those found later too, unless one is turned off
All bool `json:"all"`
// Found: the agents with session history on this computer, and those
// the user named
Found []readAgentJSON `json:"found"`
// Gateway: gateway mode, which reads no agent's files here whatever
// the choice (ownSessionsOff), so nothing is asked and none is found
Gateway bool `json:"gateway,omitempty"`
}
func readsNow() readsJSON {
r := sessions.SettleReads()
if s := settings.Shared().SessionReads; s != nil {
r = s
}
out := readsJSON{Found: []readAgentJSON{}, Gateway: ownSessionsOff()}
if r != nil {
out.Asked, out.All = r.Asked, r.All
}
agents := sessions.Found()
if r != nil {
for a := range r.Agents {
if !slices.Contains(agents, a) {
agents = append(agents, a)
}
}
}
looks := agentLooks()
for _, a := range agents {
j := readAgentJSON{Agent: a, Name: a, Icon: "generic", On: r.Reads(a)}
if l := looks[a]; l != nil {
j.Name, j.Icon = l.SharedName(), l.Icon
}
out.Found = append(out.Found, j)
}
sort.SliceStable(out.Found, func(i, j int) bool { return out.Found[i].Name < out.Found[j].Name })
return out
}
func sessionReadsRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/sessions/reads", func(rw http.ResponseWriter, r *http.Request) {
writeJSON(rw, readsNow())
})
// {all, agents: {id: on}}: all, when sent, is whether an agent not
// named is read (allow all sends true, allow selected false), and each
// agent named is read or not; either way the choice is made (asked).
mux.HandleFunc("POST /api/sessions/reads", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
All *bool `json:"all"`
Agents map[string]bool `json:"agents"`
}
if err := json.NewDecoder(http.MaxBytesReader(rw, r.Body, 16384)).Decode(&in); err != nil {
fail(rw, err)
return
}
if err := sessions.SetReads(in.All, in.Agents); err != nil {
fail(rw, err)
return
}
forgetStats()
writeJSON(rw, readsNow())
})
}