Files
magpie/internal/sessions/gateway_delete.go
T
yetone eb035e29e1 sessions: a session seen only through the gateway can be deleted: its row and recorded conversation go to magpie's trash, and Restore brings them back (lc on Discord)
lc ("只可列出?不可删除?agent claude code") saw Claude Code sessions named by
uuid under "No folder" at "0 B", with "These Claude Code sessions are read
only; magpie can list them, but cannot resume or delete them." Those rows are
gateway projections from the usage ledger: Claude Code ran with its own files
somewhere magpie can't see (another computer, a container, another config
folder), so nothing native won on the same agent+id. An agent with only such
rows was sent as not deletable, and every row was read_only, so the page had
no Delete at all.

What changed:
- sessions.DeleteGateway moves the session's recorded text folders
  (gateway-conversations/<date>/<identity hash>) into
  trash/sessions/gateway/<ts>-<hash>/files/<date> with a note marked gateway,
  and records {agent, id, last} in gateway-sessions-hidden.json.
  /api/sessions and /api/sessions/manage leave it out up to that last
  request; a newer request lists it again. The usage ledger is untouched, so
  Usage, /api/sessions/one and stats keep what it spent. A session with a
  request in the last minute is refused as active.
- Restore moves the text back (skipping what expired or was cleared, refusing
  rather than overwriting a same-day re-recording), checks each item is this
  session's identity folder inside the store, and unhides it. Delete forever
  keeps it hidden.
- The recording's promises hold in the trash: the hourly prune erases
  trashed gateway text past 7 days, "Delete saved conversations…" erases it
  too, and HasGatewayConversations counts it.
- The manage route marks an agent with gateway rows deletable and sends
  those rows deletable, with gateway: true and the size of their recorded
  text; the delete route sends gateway ids to DeleteGateway and native ones
  to Delete as before.
- Sessions page: gateway rows group under "Seen through the gateway"
  instead of "No folder", show "{size} recorded" only when something was
  recorded (no "0 B"), and the note says why they can't be resumed and what
  Delete takes and keeps. The delete dialog says the same for gateway rows
  instead of "Their files are moved…"; the Trash marks them and notes the
  7-day expiry. New strings in zh, zh-TW, ja and de.
- docs/subsystems/gateway-sessions.md describes the delete, hidden list,
  restore, expiry and clear.

Verified:
- go test -tags nogui ./... (sandbox HOME) green; gofmt -l clean; go vet
  ./...; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/sessions
  ./internal/gui; go build, GOOS=linux -tags nogui and GOOS=windows builds.
- TestSessionsDeleteGatewaySession fails on the old code with "Claude Code
  with only gateway sessions isn't deletable: [{Agent:claude
  Deletable:false}]"; sessions-gateway-delete.test.cjs fails there too.
  TestGatewayDeleteTrashExpires, TestGatewayPurgeKeepsItHidden,
  TestGatewayDeleteNothingRecorded and TestGatewayRestoreRefusesForeignItems
  cover expiry, purge, an unrecorded session and a forged note.
- GUI: sessions-gateway-delete.test.cjs in Chromium and WebKit, en, zh,
  zh-TW, ja, de, at 1000 and 390px (in-app dialog, Cancel sends nothing,
  click doesn't scroll, no left-border accents, no page overflow); all
  sessions-*, gateway-conversations, checkbox-look, old-webkit,
  sess-agent-strip, i18n-keys, gui-ja, gui-de and gui-zh-tw tests pass.
- Real try: magpie web from this tree on a sandbox HOME seeded with three
  gateway-only Claude Code sessions (two recorded): deleted a recorded and an
  unrecorded one, checked the store, trash and hidden list on disk, restored
  the recorded one (its transcript reads again), purged the other (stays
  hidden). That try found the Trash page breaking on an unrecorded session's
  null items; DeleteGateway now writes [] (TestGatewayDeleteNothingRecorded
  fails without it) and the page reads a missing list as empty.
2026-10-10 09:53:38 +08:00

233 lines
7.0 KiB
Go

package sessions
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/settings"
)
// Deleting a gateway session (lc on Discord: a Claude Code session seen only
// through magpie's gateway was listed "read only"). Such a session has no
// file of the agent's on this computer, so what magpie can delete is what it
// keeps of it: its row in the session lists, and the conversation text it
// recorded, if any. The text is moved into magpie's trash like a native
// session's files, under trash/sessions/gateway/, and the row is hidden up
// to the last request it had; Restore moves the text back and shows the row
// again. The usage ledger is never touched, so Usage's totals stay as they
// were, and a request under the same session after the delete lists it
// again. Trashed text still expires with the recording's 7 days, and
// clearing the recorded conversations clears it too: the recording promises
// that much.
// gatewayTrash is the trash folder (and key prefix) of deleted gateway
// sessions; no agent has this id.
const gatewayTrash = "gateway"
var hiddenMu sync.Mutex
func gatewayHiddenPath() string { return filepath.Join(settings.Dir(), "gateway-sessions-hidden.json") }
type hiddenGateway struct {
Agent string `json:"agent"`
ID string `json:"id"`
Last time.Time `json:"last"`
}
func readHidden() []hiddenGateway {
var out []hiddenGateway
b, err := os.ReadFile(gatewayHiddenPath())
if err == nil {
json.Unmarshal(b, &out)
}
return out
}
func writeHidden(list []hiddenGateway) error {
if len(list) == 0 {
if err := os.Remove(gatewayHiddenPath()); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
return nil
}
b, err := json.Marshal(list)
if err != nil {
return err
}
if err := os.MkdirAll(settings.Dir(), 0o700); err != nil {
return err
}
tmp := gatewayHiddenPath() + ".tmp"
if err := os.WriteFile(tmp, b, 0o600); err != nil {
return err
}
return os.Rename(tmp, gatewayHiddenPath())
}
// GatewayHidden says whether a gateway session was deleted: true when it
// was, and had no request after its last one then.
func GatewayHidden() func(agent, id string, last time.Time) bool {
hiddenMu.Lock()
list := readHidden()
hiddenMu.Unlock()
m := map[string]time.Time{}
for _, h := range list {
m[h.Agent+"|"+h.ID] = h.Last
}
return func(agent, id string, last time.Time) bool {
at, ok := m[agent+"|"+id]
return ok && !last.After(at)
}
}
// GatewaySizes is what conversation text magpie keeps of each gateway
// session, in bytes, read in one walk of the store.
func GatewaySizes() func(agent, id string) int64 {
by := map[string]int64{}
gatewayMu.Lock()
root := gatewayDir()
gatewayMu.Unlock()
filepath.WalkDir(root, func(p string, d os.DirEntry, err error) error {
if err != nil || !d.Type().IsRegular() {
return nil
}
if fi, err := d.Info(); err == nil {
by[filepath.Base(filepath.Dir(p))] += fi.Size()
}
return nil
})
return func(agent, id string) int64 { return by[gatewayIdentity(agent, id)] }
}
// DeleteGateway deletes a gateway session as described above: last is its
// last request as listed. One that had a request in the last minute may
// still be going on, and is left (ErrActive).
func DeleteGateway(agent, id string, title string, last time.Time) (Trashed, error) {
if agent == "" || id == "" {
return Trashed{}, errors.New("no such session")
}
now := time.Now()
if now.Sub(last) < ActiveWindow {
return Trashed{}, ErrActive
}
gatewayMu.Lock()
defer gatewayMu.Unlock()
hash := gatewayIdentity(agent, id)
buckets, _ := filepath.Glob(filepath.Join(gatewayDir(), "*", hash))
t := Trashed{Agent: agent, ID: id, Title: title, Last: last, Deleted: now, Gateway: true, Items: []moved{}}
name := now.UTC().Format("20060102T150405.000000000") + "-" + hash[:16]
dir := filepath.Join(TrashDir(), gatewayTrash, name)
t.Key = gatewayTrash + "/" + name
if err := os.MkdirAll(filepath.Join(dir, "files"), 0o700); err != nil {
return Trashed{}, err
}
for _, p := range buckets {
// named by its date, which its expiry is read from
m := moved{From: p, Name: filepath.Base(filepath.Dir(p))}
t.Size += sizeOf(p)
if err := move(p, filepath.Join(dir, "files", m.Name)); err != nil {
for _, back := range t.Items {
move(filepath.Join(dir, "files", back.Name), back.From)
}
os.RemoveAll(dir)
return Trashed{}, err
}
t.Items = append(t.Items, m)
writeManifest(dir, t)
}
if err := writeManifest(dir, t); err != nil {
return Trashed{}, err
}
hiddenMu.Lock()
defer hiddenMu.Unlock()
list := readHidden()
kept := list[:0]
for _, h := range list {
if h.Agent != agent || h.ID != id {
kept = append(kept, h)
}
}
if err := writeHidden(append(kept, hiddenGateway{Agent: agent, ID: id, Last: last})); err != nil {
// the text goes back, and the session stays as it was
for _, back := range t.Items {
move(filepath.Join(dir, "files", back.Name), back.From)
}
os.RemoveAll(dir)
return Trashed{}, err
}
return t, nil
}
// restoreGateway moves a deleted gateway session's text back, what of it
// hasn't expired or been cleared since, and lists the session again.
func restoreGateway(dir string, t Trashed) error {
gatewayMu.Lock()
defer gatewayMu.Unlock()
var back []moved
for _, m := range t.Items {
if _, err := os.Lstat(filepath.Join(dir, "files", m.Name)); err != nil {
continue
}
if _, err := os.Lstat(m.From); err == nil {
return errors.New("the session was recorded again that day; nothing was restored")
}
back = append(back, m)
}
for i, m := range back {
if err := move(filepath.Join(dir, "files", m.Name), m.From); err != nil {
for _, b := range back[:i] {
move(b.From, filepath.Join(dir, "files", b.Name))
}
return err
}
}
hiddenMu.Lock()
defer hiddenMu.Unlock()
list := readHidden()
kept := list[:0]
for _, h := range list {
if h.Agent != t.Agent || h.ID != t.ID {
kept = append(kept, h)
}
}
return writeHidden(kept)
}
// gatewayTrashFiles are the trashed gateway text folders, each with the
// date it was recorded on ("" when its name isn't one).
func gatewayTrashFiles() (paths, dates []string) {
paths, _ = filepath.Glob(filepath.Join(TrashDir(), gatewayTrash, "*", "files", "*"))
for _, p := range paths {
d := filepath.Base(p)
if _, err := time.Parse(time.DateOnly, d); err != nil {
d = ""
}
dates = append(dates, d)
}
return paths, dates
}
// clearGatewayTrash erases the text of deleted gateway sessions, all of it,
// or (expired) what is past the recording's retention. Called under
// gatewayMu. The trash notes are kept, so Restore still lists the session
// again.
func clearGatewayTrash(expired func(date string) bool) error {
paths, dates := gatewayTrashFiles()
var errs []error
for i, p := range paths {
if expired == nil || (dates[i] != "" && expired(dates[i])) {
if err := os.RemoveAll(p); err != nil {
errs = append(errs, err)
}
}
}
return errors.Join(errs...)
}
func isGatewayKey(key string) bool { return strings.HasPrefix(key, gatewayTrash+"/") }