Files
magpie/internal/gateway/archive_local_test.go
T
yetone e219f699ca gateway: with no S3 bucket the request archive keeps calls on this computer, oldest removed past 1 GiB (CyberCaius)
CyberCaius on Discord: "请求存档只能存在 s3 存储桶里,不能保存在本地吗". The
Usage page's Request archive switch refused without an s3:// sync. Now,
with no bucket, the same switch keeps each call in
<settings.Dir()>/request-archive/<date>/<id>.json: the same file the
bucket gets, masked the same way (Archived, archiveRequest), written to a
temporary file beside it, synced and renamed, 0600 in a 0700 folder.
Past its size (RequestArchiveLocalMB, 1 GiB unless set) the oldest calls
go first, down to a tenth under it; the call just written is never
removed, empty day folders go, and a .part file an hour old is removed
on the next scan. The running total is counted once and then added to,
so a write walks the folder only once it is over.

On by default or a switch: a switch, still off unless turned on. Bodies
can hold secrets, so nothing is written to a disk where the user didn't
ask. The local switch is its own field, RequestArchiveLocal, in KeepOwn
with its size, so sync never turns it on at another computer, and
RequestArchive synced from a computer with a bucket keeps nothing on one
without (archiveDest). The GUI switch writes RequestArchive while sync
has a bucket and RequestArchiveLocal while it has none; the tooltip names
the folder and the size, and a failed write reads "Not saved" (en, zh,
zh-TW, ja, de). GET /api/archive reads this computer's copy first, then
the bucket. The Settings page's save keeps both fields (api.go).

Docs: sync-backup.md and gui-shell.md say where the archive goes and why
the switch is per computer; the privacy pages (en, zh, ja) say what the
folder holds, that it is off by default, and how large it grows. The
READMEs' Privacy is about what is sent and is unchanged.

app.js stays under TestCoreScriptBudget (its comment was shortened, and
one t() call folded); no budget raised.

Run (worktree diff on 5222a4b12, sandbox HOME):
- go test -tags nogui -count=1 ./internal/settings/ ./internal/gui/
  ./internal/gateway/: ok (29s, 73s, 507s)
- node --test request-archive, request-archive-usage, routing-bodies,
  gui-zh-tw, gui-ja, gui-de, i18n-keys (chromium and webkit): 37/37 pass
- gofmt -l internal/: clean; go vet ./..., GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./...: clean; go build for darwin, linux
  (nogui), windows: ok
- TestCoreScriptBudget: ok

After rebasing onto origin/main 3840333c5: -run Budget|Archive|SaveKeeps|
RequestArchiveLocal in gui, gateway, settings ok; request-archive and
i18n-keys 8/8; gofmt clean; linux and windows vet clean.

Broken by hand, each red, then put back:
- archiveDest's local branch off: TestArchiveOnThisComputer red
- RequestArchive also writing to disk: TestArchiveNeverOnDiskUnasked red
- no pruning: TestLocalArchiveRemovesOldestPastItsSize red
- KeepOwn line removed: TestRequestArchiveLocalIsThisComputers red
- POST setting RequestArchive with no bucket:
  TestArchiveSwitchKeepsOnThisComputer red
- GET not reading the local copy: TestArchiveSwitchKeepsOnThisComputer red
- api.go not carrying the fields: TestSettingsSaveKeepsWhatItDoesNotSend red
- writing in place, not through temp+rename: TestArchiveOnThisComputer red
  (mode 0644, a .part left)

Not tried: the real app with a real agent's calls (the gateway tests
drive real requests through serve with a fake vendor); Windows' file
modes (0600 is skipped there); the full ./... suite before the push (the
machine had a queue of sessions' suites; it is queued after).
2026-10-24 21:01:18 +08:00

213 lines
7.3 KiB
Go

package gateway
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
)
// With sync not to S3 and the archive's switch on at this computer
// (CyberCaius on Discord), a call is kept in magpie's folder, the same file
// the bucket gets: its secrets taken out, its id on the response and the
// call, written whole through a temporary file, readable by this user only.
func TestArchiveOnThisComputer(t *testing.T) {
fresh(t)
serveOn(t, "fake", "k", []string{"m1"}, archiveVendor{})
was := ArchiveBucket
ArchiveBucket = func() (Putter, bool) { return nil, false } // sync to WebDAV, or none
t.Cleanup(func() { ArchiveBucket = was })
settings.Save(settings.Settings{RequestArchiveLocal: true})
s := New()
rec := archivePost(t, s)
id := rec.Header().Get(ArchiveHeader)
c := s.Recent()[0]
name := c.Time.UTC().Format("2006-01-02") + "/" + id
if id == "" || c.Archive != name {
t.Fatalf("not archived: id %q, call %q", id, c.Archive)
}
path := filepath.Join(settings.Dir(), "request-archive", filepath.FromSlash(name)+".json")
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("not on this computer: %v", err)
}
for _, secret := range []string{archiveKey, "named-secret", "magpie-gateway-key", "another-secret", "cookie-secret", "abc123", "query-secret"} {
if strings.Contains(string(data), secret) {
t.Errorf("%s in the archive:\n%s", secret, data)
}
}
var a Archived
if err := json.Unmarshal(data, &a); err != nil {
t.Fatal(err)
}
if a.ID != name || a.Call.Model != "fake/m1" || a.Request.Headers["Authorization"][0] != "[REDACTED]" ||
!strings.Contains(a.Request.Body, "deploy with [REDACTED:API_KEY]") || !strings.Contains(a.Response.Body, `"content":"done"`) {
t.Errorf("archived %+v", a)
}
if fi, _ := os.Stat(path); runtime.GOOS != "windows" && fi.Mode().Perm() != 0o600 {
t.Errorf("mode %v, want 0600", fi.Mode().Perm())
}
if left, _ := filepath.Glob(filepath.Join(filepath.Dir(path), ".*")); len(left) != 0 {
t.Errorf("temporary files left: %v", left)
}
// read back by its name, as the Usage page asks for it
date, aid, _ := strings.Cut(name, "/")
file, _ := ArchiveName(date, aid)
r, size, err := LocalArchive().Open(context.Background(), file)
if err != nil || size != int64(len(data)) {
t.Fatalf("Open: %d bytes, %v", size, err)
}
r.Close()
if _, _, err := LocalArchive().Open(context.Background(), "archive/2026-10-02/101010-00000000000000bb.json"); err != ErrNotArchived {
t.Errorf("a call never kept: %v", err)
}
}
// Request bodies are written to a disk only where the user turned the
// archive on at that computer: RequestArchive, which sync carries from a
// computer with an S3 bucket, keeps nothing on one with none, and the
// local switch sends nothing to the disk while a bucket is there.
func TestArchiveNeverOnDiskUnasked(t *testing.T) {
fresh(t)
serveOn(t, "fake", "k", []string{"m1"}, archiveVendor{})
was := ArchiveBucket
t.Cleanup(func() { ArchiveBucket = was })
dir := filepath.Join(settings.Dir(), "request-archive")
onDisk := func() []string {
var got []string
filepath.Walk(dir, func(p string, fi os.FileInfo, err error) error {
if err == nil && !fi.IsDir() {
got = append(got, p)
}
return nil
})
return got
}
// off, as it is unless turned on
ArchiveBucket = func() (Putter, bool) { return nil, false }
s := New()
if rec := archivePost(t, s); rec.Header().Get(ArchiveHeader) != "" || len(onDisk()) != 0 {
t.Fatalf("archived while off: %v", onDisk())
}
// the S3 archive's switch, synced here, with no bucket here
settings.Save(settings.Settings{RequestArchive: true})
if rec := archivePost(t, s); rec.Header().Get(ArchiveHeader) != "" || len(onDisk()) != 0 {
t.Fatalf("the synced switch wrote to this computer: %v", onDisk())
}
// the local switch with a bucket there: the bucket's switch decides
b := &memBucket{objs: map[string][]byte{}}
archiveTo(t, b)
settings.Save(settings.Settings{RequestArchiveLocal: true})
if rec := archivePost(t, s); rec.Header().Get(ArchiveHeader) != "" || len(onDisk()) != 0 || len(b.objs) != 0 {
t.Fatalf("archived with the bucket's switch off: disk %v, bucket %v", onDisk(), keys(b.objs))
}
settings.Save(settings.Settings{RequestArchive: true, RequestArchiveLocal: true})
if archivePost(t, s); len(b.objs) != 1 || len(onDisk()) != 0 {
t.Fatalf("with a bucket: bucket %v, disk %v", keys(b.objs), onDisk())
}
}
// Past its size the folder loses its oldest calls first, down to a tenth
// under it; the call just kept stays, and a temporary file a write left an
// hour ago goes.
func TestLocalArchiveRemovesOldestPastItsSize(t *testing.T) {
d := &DiskArchive{Dir: t.TempDir(), cap: func() int64 { return 1000 }}
ctx := context.Background()
body := []byte(strings.Repeat("x", 200))
name := func(day, i int) string {
return fmt.Sprintf("archive/2026-10-%02d/%06d-00000000000000aa.json", day, 100000+i)
}
stale := filepath.Join(d.Dir, "2026-10-01", ".old.json-1.part")
os.MkdirAll(filepath.Dir(stale), 0o700)
os.WriteFile(stale, body, 0o600)
old := time.Now().Add(-2 * time.Hour)
os.Chtimes(stale, old, old)
// four on the 1st, one on the 2nd: 1000 bytes, at the size
for i := range 4 {
if err := d.Put(ctx, name(1, i), body); err != nil {
t.Fatal(err)
}
}
if err := d.Put(ctx, name(2, 0), body); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(stale); err == nil {
t.Error("an hour-old temporary file is still there")
}
has := func(n string) bool {
r, _, err := d.Open(ctx, n)
if err == nil {
r.Close()
}
return err == nil
}
for i := range 4 {
if !has(name(1, i)) {
t.Fatalf("removed at the size, not past it: %s", name(1, i))
}
}
// one more is past it: the two oldest go, 800 bytes left
if err := d.Put(ctx, name(2, 1), body); err != nil {
t.Fatal(err)
}
for i, want := range []bool{false, false, true, true} {
if has(name(1, i)) != want {
t.Errorf("%s kept %v, want %v", name(1, i), !want, want)
}
}
if !has(name(2, 0)) || !has(name(2, 1)) {
t.Error("a newer call was removed")
}
// one alone larger than the size is kept, and every other goes
big := []byte(strings.Repeat("y", 1500))
if err := d.Put(ctx, name(3, 0), big); err != nil {
t.Fatal(err)
}
if !has(name(3, 0)) {
t.Error("the call just kept was removed")
}
for _, n := range []string{name(1, 2), name(1, 3), name(2, 0), name(2, 1)} {
if has(n) {
t.Errorf("%s kept past the size", n)
}
}
if days, _ := os.ReadDir(d.Dir); len(days) != 1 {
t.Errorf("empty days left: %v", days)
}
r, _, _ := d.Open(ctx, name(3, 0))
got, _ := io.ReadAll(r)
r.Close()
if string(got) != string(big) {
t.Errorf("read back %d bytes", len(got))
}
// a name not of the archive's shape is never a path
for _, n := range []string{"archive/../../settings.json", "archive/2026-10-02/../x.json", "settings.json", "archive/2026-10-02/ABC.json"} {
if err := d.Put(ctx, n, body); err == nil {
t.Errorf("Put %s", n)
}
if _, _, err := d.Open(ctx, n); err != ErrNotArchived {
t.Errorf("Open %s: %v", n, err)
}
}
}
func TestLocalArchiveCap(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if got := LocalArchiveCap(); got != 1<<30 {
t.Errorf("default %d, want a GiB", got)
}
settings.Save(settings.Settings{RequestArchiveLocalMB: 5})
if got := LocalArchiveCap(); got != 5<<20 {
t.Errorf("5 MiB: %d", got)
}
}