hayden4608 on Discord syncs two computers through GitHub, both with
Codex, and wants each one's Codex default model kept apart. Until now
the agents part carried every agent field ("codex.model", effort,
provider...) as one set: A's pick went up, B's sync brought it over B's,
B's pick went up again and A's next sync took it back. The two
computers fought, and the only way out was turning "agents' models"
off for every agent.
Now a sync that brings an agent field over this computer's value
records what it brought (Brought) and what it wrote over (Replaced,
only when that was picked here, not itself brought). If the user then
sets the field back to the value the sync replaced, the field is this
computer's own (Own): it leaves the hashes, a sync no longer brings it,
and a push keeps the server's value, so the other computer is not
changed either. Every other new pick is an ordinary pick and still goes
both ways, so a computer that changes the model for everyone (and
TestSyncAgentDefaults' reset) still reaches the rest. Nothing to set
up: the user's own act on this computer decides it, and a synced value
never overrides it again.
Settings > Sync lists the kept fields in one row ("Kept on this
computer: Codex model ...") with a "Follow the others" button
(davsync/follow), which brings the server's values on the next sync and
ends the keeping. Restore brings everything and ends it too. The row is
drawn only while sync and agents' models are on and something is kept;
plain row and button, no new layout. app.js grew 623 bytes; the budgets
are raised by that with a note.
Tests:
- internal/davsync/own_model_test.go TestOwnModelStaysOnEachComputer:
two sandbox HOMEs (CODEX_HOME per computer) on the GitHub fixture.
A picks a1, B joins and gets it, B picks b1 (goes up), A gets b1, A
sets a1 back (own; the server keeps b1), B picks b2 + effort +
provider (A gets effort and provider, keeps a1), A picks a2 and a
Claude model (both go up, B gets them), A follows (gets b2), B's b3
reaches A. TestNewPicksStillGoBothWays: alternating new picks always
reach the other computer.
- internal/gui/tests/sync-own-fields.test.cjs: the row in en, zh,
zh-TW, ja, de at 420px, no overflow, Follow posts once and the row
goes, no scroll; no row without own fields or with agents off.
Broken by hand, each red: noticeOwn off (own_model_test:122); any pick
after a sync counts as own (TestSyncAgentDefaults and own test:106);
keepServersOwn off (:166); the bring ignoring Own (:146); withoutOwn
off in the hashes (:143, a notice); Follow off (:182).
Run at this head (rebased on 47173f593) on mote:
gofmt -l internal/davsync internal/gui clean; go vet ./..., GOOS=linux
go vet -tags nogui ./..., GOOS=windows go vet ./... ok; go test
./internal/davsync ok, ./internal/backup ok, ./internal/gui ok but
TestSettingsSaveKeepsWhatItDoesNotSend (#4514, red on main) and the
eleven real-window AppKit tests (dock, panel, tray, corners), which
time out on mote at origin/main 47173f593 too (#4125, #2645);
-run 'Budget|Sync|Backup|Settings' passes TestCoreScriptBudget.
GUI (node --test): sync-own-fields, sync-note-show, github-sync,
s3-sync, sync-other-kind, sync-not-backup, sync-auto-restore,
sync-usage-error, i18n-keys. Chromium on mote: all pass but
sync-not-backup and sync-usage-error, whose chromium.launch({channel:
"chromium"}) segfaults on mote at origin/main 47173f593 too; on the
WSL box (magpie-desktop) they pass, 25 of 25 with sync-own-fields.
WebKit on mote hangs on launch for every file, untouched ones too; on
WSL all nine files ran: 49 of 52, the reds being i18n-keys' "a
plugin's words are its own" (#4524, red on main) and the first
page.goto of sync-own-fields and sync-note-show at 5s under nine
files at once, which pass run alone (9 of 9).
Red on main and not from this change: TestSettingsSaveKeepsWhatItDoesNotSend
(#4514), i18n-keys "Unmarked" (#4524).
Not tried: a real GitHub repo and a real Codex on two computers; the
CLI has no follow command (only the GUI and Restore end the keeping);
Undo after a Restore doesn't bring Own back.
181 lines
4.9 KiB
Go
181 lines
4.9 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/backup"
|
|
"github.com/yetone/magpie/internal/davsync"
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// backupRoutes are the Settings page's Export and Import, and the WebDAV
|
|
// sync set up there.
|
|
func backupRoutes(mux *http.ServeMux, w Windows) {
|
|
mux.HandleFunc("POST /api/backup/export", func(rw http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
Pass string
|
|
Keys bool
|
|
Library *bool // nil is yes
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
b, err := backup.Collect(in.Keys, Version)
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
if in.Library != nil && !*in.Library {
|
|
b.Library = nil
|
|
}
|
|
data, err := backup.Seal(b, in.Pass)
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
dir := downloads()
|
|
name := filepath.Join(dir, "magpie-"+time.Now().Format("2006-01-02")+backup.Ext)
|
|
for i := 2; ; i++ { // never over an earlier one
|
|
if _, err := os.Stat(name); err != nil {
|
|
break
|
|
}
|
|
name = filepath.Join(dir, fmt.Sprintf("magpie-%s-%d%s", time.Now().Format("2006-01-02"), i, backup.Ext))
|
|
}
|
|
if err := edit.WriteAtomic(name, data); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
os.Chmod(name, 0o600)
|
|
_ = w.OpenFolder(dir) // saved either way; the path is in the answer
|
|
writeJSON(rw, map[string]any{"path": tilde(name), "providers": len(b.Providers), "profiles": len(b.Profiles), "agents": len(b.Agents), "library": b.Library != nil})
|
|
})
|
|
mux.HandleFunc("POST /api/backup/import", func(rw http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
Data string
|
|
Pass string
|
|
Agents bool
|
|
Library *bool // nil is yes
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
data, err := base64.StdEncoding.DecodeString(in.Data)
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
b, err := backup.Open(data, in.Pass)
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
parts := backup.All
|
|
parts.Agents = in.Agents
|
|
parts.Library = in.Library == nil || *in.Library
|
|
res, err := backup.Restore(b, parts)
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
writeJSON(rw, res)
|
|
})
|
|
|
|
mux.HandleFunc("GET /api/davsync", func(rw http.ResponseWriter, r *http.Request) {
|
|
writeJSON(rw, davsync.Status())
|
|
})
|
|
mux.HandleFunc("POST /api/davsync/{action}", func(rw http.ResponseWriter, r *http.Request) {
|
|
// no time limit: a large backup on a slow line takes minutes (#657),
|
|
// and davsync ends a request that stops moving
|
|
ctx := r.Context()
|
|
var err error
|
|
var brought []string
|
|
switch r.PathValue("action") {
|
|
case "save": // and sync at once, so a wrong address or password shows now
|
|
var c davsync.Config
|
|
if err = json.NewDecoder(r.Body).Decode(&c); err == nil {
|
|
err = davsync.Configure(c)
|
|
}
|
|
if err != nil { // said in the form, what was typed in it kept
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
err = busy(davsync.SyncNow(ctx)) // how it went is in the status
|
|
case "now":
|
|
err = busy(davsync.SyncNow(ctx))
|
|
case "off":
|
|
err = davsync.Off()
|
|
case "dismiss":
|
|
err = davsync.Dismiss()
|
|
case "auto": // how often it syncs by itself, or never (#847)
|
|
var in struct{ Minutes int }
|
|
if err = json.NewDecoder(r.Body).Decode(&in); err == nil {
|
|
err = davsync.SetAuto(in.Minutes)
|
|
}
|
|
case "restore": // the server's setup over this computer's, which is kept
|
|
brought, err = davsync.Restore(ctx)
|
|
case "undo":
|
|
_, err = davsync.Undo()
|
|
case "upload": // this computer's setup over a server file that isn't a backup, kept first
|
|
err = davsync.Upload(ctx)
|
|
case "follow": // the agent fields kept on this computer follow the others again
|
|
err = busy(davsync.Follow(ctx))
|
|
case "reveal":
|
|
err = w.OpenFolder(filepath.Join(settings.Dir(), "sync"))
|
|
default:
|
|
http.NotFound(rw, r)
|
|
return
|
|
}
|
|
v := davsync.Status()
|
|
switch {
|
|
case errors.Is(err, davsync.ErrBusy):
|
|
// nothing ran to put it in the status: it is said there all the same
|
|
v.Error = err.Error()
|
|
case err != nil:
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
if r.PathValue("action") == "restore" { // and what it brought in: nothing when it was the same
|
|
writeJSON(rw, struct {
|
|
davsync.View
|
|
Brought []string `json:"brought"`
|
|
}{v, brought})
|
|
return
|
|
}
|
|
writeJSON(rw, v)
|
|
})
|
|
}
|
|
|
|
// busy is what of a sync's failure the status doesn't hold: that it waited
|
|
// its time out for another magpie's. The rest is in the status.
|
|
func busy(err error) error {
|
|
if errors.Is(err, davsync.ErrBusy) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// downloads is where an export is saved: the Downloads folder, or home
|
|
// when there is none.
|
|
func downloads() string {
|
|
home, _ := os.UserHomeDir()
|
|
if d := filepath.Join(home, "Downloads"); isDir(d) {
|
|
return d
|
|
}
|
|
return home
|
|
}
|
|
|
|
func isDir(p string) bool {
|
|
fi, err := os.Stat(p)
|
|
return err == nil && fi.IsDir()
|
|
}
|