Files
magpie/internal/davsync/own.go
T
yetone a511b11d8b sync: an agent's model set back on a computer after a sync stays that computer's (hayden4608)
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.
2026-10-31 18:01:04 +08:00

209 lines
5.9 KiB
Go

package davsync
import (
"context"
"maps"
"slices"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/backup"
"github.com/yetone/magpie/internal/profile"
)
// An agent's field that sync wrote over a value picked on this computer,
// and that the user then puts back, is this computer's own (hayden4608 on
// Discord: two computers on one GitHub sync, each wanting its own Codex
// model, set each other's every sync). Putting the value back is what says
// it: a new value picked after a sync still goes up, as before, so a
// change made on any computer still reaches the others. From then on sync
// neither brings an own field in nor takes this computer's value up, while
// the agent's other fields, and every other part, still sync.
//
// The state is this computer's alone (sync-state.json): Brought is each
// field's value as the last sync that changed it here wrote it, Replaced
// the value picked here that it wrote over, if it was one (not one an
// earlier sync brought), and Own the fields put back since. Follow, from
// the Settings page, gives the own fields back to sync: the next sync
// brings the server's values in.
// noticeOwn makes own each field sync wrote over a pick, put back to that
// pick here since. Another value picked since is a pick like any other: it
// goes up, and what sync brought before is forgotten.
func noticeOwn(st *state, here map[string]string) {
for k, v := range st.Brought {
now, ok := here[k]
if !ok || now == v { // the agent isn't here now, or the field is as sync left it
continue
}
if was, ok := st.Replaced[k]; ok && now == was {
if st.Own == nil {
st.Own = map[string]bool{}
}
st.Own[k] = true
}
delete(st.Brought, k)
delete(st.Replaced, k)
}
}
// withoutOwn is b without this computer's own agent fields: what is
// hashed to tell a change, so a pick of an own field is no change to push
// and the server's value of one is no change to bring.
func withoutOwn(b backup.Bundle, own map[string]bool) backup.Bundle {
if len(own) == 0 || b.Agents == nil {
return b
}
b.Agents = maps.Clone(b.Agents)
for k := range own {
delete(b.Agents, k)
}
return b
}
// hashesOwn is hashes of b with this computer's own fields left out.
func hashesOwn(b backup.Bundle, own map[string]bool) map[string]string {
return hashes(withoutOwn(b, own))
}
// keepServersOwn puts the server's values of this computer's own fields
// back into the agents a sync takes up from here: they never go up.
func keepServersOwn(merged *backup.Bundle, remote backup.Bundle, own map[string]bool) {
if len(own) == 0 {
return
}
a := maps.Clone(merged.Agents)
if a == nil {
a = map[string]string{}
}
for k := range own {
if v, ok := remote.Agents[k]; ok {
a[k] = v
} else {
delete(a, k)
}
}
merged.Agents = a
}
// brought is a field a bring changed here: the value it wrote over, and
// the value it reads now.
type brought struct{ was, now string }
// bringAgents puts fields (the server's agents) in here, but for keep's,
// this computer's own. An own field that another's change moved with it
// (a provider re-settling the model behind it) is put back. It returns
// each field the bring changed here.
func bringAgents(fields map[string]string, keep map[string]bool) (map[string]brought, error) {
in := maps.Clone(fields)
for k := range keep {
delete(in, k)
}
before := profile.Fields()
_, err := backup.Restore(backup.Bundle{Agents: in}, backup.Parts{Agents: true})
after := profile.Fields()
back := map[string]string{}
for k := range keep {
if v, ok := before[k]; ok && after[k] != v {
back[k] = v
}
}
if len(back) > 0 {
if _, berr := profile.ApplyFields(back); berr != nil && err == nil {
err = berr
}
after = profile.Fields()
}
got := map[string]brought{}
for k, v := range after {
if !keep[k] && before[k] != v {
got[k] = brought{before[k], v}
}
}
return got, err
}
// broughtIn records what a bring set here, and what it wrote over when
// that was picked here rather than brought by an earlier sync.
func broughtIn(st *state, got map[string]brought) {
if len(got) == 0 {
return
}
if st.Brought == nil {
st.Brought = map[string]string{}
}
if st.Replaced == nil {
st.Replaced = map[string]string{}
}
for k, b := range got {
if last, ok := st.Brought[k]; ok && last == b.was {
delete(st.Replaced, k) // sync's own value, written over by sync
} else {
st.Replaced[k] = b.was
}
st.Brought[k] = b.now
}
}
// OwnField is an agent field kept on this computer, as Settings names it.
type OwnField struct {
Agent string `json:"agent"` // the agent's name
Field string `json:"field"` // the field's label: model, effort…
}
// ownFields names st's own fields, by agent then field.
func ownFields(st state) []OwnField {
if len(st.Own) == 0 {
return nil
}
agents := map[string]*agent.Agent{}
for _, a := range agent.Detected() {
agents[a.ID] = a
}
keys := slices.Sorted(maps.Keys(st.Own))
out := make([]OwnField, 0, len(keys))
for _, k := range keys {
id, field := splitField(k)
f := OwnField{Agent: id, Field: field}
if a := agents[id]; a != nil {
f.Agent = a.Name
if x := a.Field(field); x != nil && x.Label != "" {
f.Field = x.Label
}
}
out = append(out, f)
}
return out
}
// splitField is a field's agent and key: the last dot, as a WSL distro's
// name may have one (codex@wsl:Ubuntu-24.04.model).
func splitField(k string) (string, string) {
for i := len(k) - 1; i >= 0; i-- {
if k[i] == '.' {
return k[:i], k[i+1:]
}
}
return k, ""
}
// Follow gives this computer's own agent fields back to sync, and syncs:
// the server's values come in, and the others' picks follow again.
func Follow(ctx context.Context) error {
err := locked(func() error {
if _, ok := Load(); !ok {
return ErrOff
}
st := loadState()
if len(st.Own) == 0 {
return nil
}
st.Follow = true
saveState(st)
return nil
})
if err != nil {
return err
}
return SyncNow(ctx)
}