Files
magpie/internal/agent/stash.go
T
yetone 270b52573a agent: stash.json is changed under a lock and written whole, and one that doesn't parse is kept aside, not written over as no stash
stash.json holds every agent's config from before magpie wired it. stash,
forget and unstash read it, changed it and wrote it back with
os.WriteFile, with no lock, and a read that didn't parse came back empty.
Two changes at once (Connect all, a gateway move setting each agent again)
could leave the file half written, and the next change read that as no
stash and wrote it back: every agent's originals gone, so Disconnect had
nothing to put back. Now each change runs under stashMu and is written
with edit.WriteAtomic; a file that doesn't parse is copied to
stash.json.bad-<time> first; a new file is made 0600, since it holds keys.
rewire.go's stashPut and StepCode's stepcodeStoreState go through the same
stashWrite (StepCode keeps its own strict read).

TestStashKeepsChangesMadeAtOnce runs 40 stashes at once;
TestUnreadStashKeptAside stashes over a half-written file.

Ran: both new tests on origin/main's stash.go (red: "0 of 40 agents'
originals kept", "kept aside: []") and with the change (green); go vet on
darwin, GOOS=linux and GOOS=windows for internal/agent; go test -count=1
./internal/agent/ (ok, 278s); go test -race -count=3 on the stash and
StepCode tests (ok).
2026-10-11 04:28:31 +08:00

112 lines
2.7 KiB
Go

package agent
import (
"encoding/json"
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// The stash remembers what an agent's config said before magpie pointed it
// at the gateway, so switching back restores it instead of guessing.
//
// stash.json holds every agent's originals, so a change to it is read,
// changed and written under stashMu, and written whole (edit.WriteAtomic):
// two changes at once, or one cut short, never leave it half there, which
// the next change read as no stash and wrote back over every agent's. A
// file that doesn't parse (edited by hand, or half written by an older
// magpie) is copied aside before it is written over, so what it held can
// still be got back.
var stashMu sync.Mutex
func stashPath() string { return filepath.Join(filepath.Dir(provider.Path()), "stash.json") }
func stashLoad() map[string]string {
out := map[string]string{}
if b, err := os.ReadFile(stashPath()); err == nil {
json.Unmarshal(b, &out)
}
return out
}
// stashWrite writes m as the stash, a file there that doesn't parse kept
// aside first. The caller holds stashMu.
func stashWrite(m map[string]string) error {
b, err := json.MarshalIndent(m, "", " ")
if err != nil {
return err
}
p := stashPath()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
return err
}
keepUnreadStash(p)
// made 0600 first: WriteAtomic keeps a file's mode, and the stash holds
// keys
if f, err := os.OpenFile(p, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600); err == nil {
f.Close()
} else if !errors.Is(err, fs.ErrExist) {
return err
}
return edit.WriteAtomic(p, b)
}
// keepUnreadStash copies a stash.json that doesn't parse aside before it is
// written over.
func keepUnreadStash(path string) {
b, err := os.ReadFile(path)
if err != nil || len(b) == 0 || json.Valid(b) {
return
}
bad := path + ".bad-" + time.Now().Format("20060102-150405.000000000")
if err := os.WriteFile(bad, b, 0o600); err != nil {
log.Printf("stash.json doesn't parse and couldn't be kept: %v", err)
return
}
log.Printf("stash.json didn't parse; it is kept as %s", filepath.Base(bad))
}
func stash(kv map[string]string) {
stashMu.Lock()
defer stashMu.Unlock()
m := stashLoad()
for k, v := range kv {
if v == "" {
delete(m, k)
} else {
m[k] = v
}
}
stashWrite(m)
}
// forget drops stashed values without restoring them.
func forget(keys ...string) {
stashMu.Lock()
defer stashMu.Unlock()
m := stashLoad()
for _, k := range keys {
delete(m, k)
}
stashWrite(m)
}
func unstash(key string) string {
stashMu.Lock()
defer stashMu.Unlock()
m := stashLoad()
v := m[key]
if v != "" {
delete(m, key)
stashWrite(m)
}
return v
}