chenxiang0010 on Discord: 「agent当前选择的模型可不可以像mcp那样 只对当前设备生效」.
Sync carries every agent's picks (model, effort...) between computers;
an MCP server has an opt-in "This computer only" that sync and backup
files never carry. Agents now have the same: the row's menu (right-click
or ...) has This computer only, off by default, so an upgrade changes no
pick on any computer.
- settings: AgentsHere, this computer's own (KeepOwn), and kept by a save
of the Settings page (settingsapi.Apply), which doesn't send it.
- davsync: each kept agent adds "<id>.*" to the own set (withHere, isOwn),
so a sync neither pushes its fields nor brings the server's; a Restore
from the server leaves them. sync-state.json records the list as of the
last sync (Here): hashes before a sync use it and after use the current
list, so turning it on or off is no change on either side, and a changed
list forces the sync past its early returns. Turned off, the next pick
goes up again. Other agents still sync both ways; the implicit own
fields (hayden4608) are unchanged.
- backup.Restore skips a kept agent in a backup file.
- gui: POST /api/agents/here; the row says "This computer only" beside
the version (hidden in the tray panel and in a narrow row), menu ticks.
Core words moved from the library plugin's i18n into core i18n
(zh/zh-TW/ja/de). Comments in renderAgents cut to pay for it under the
core budget (no raise).
- cli: the route is named under stage 3 (no command yet).
- docs/subsystems/sync-backup.md describes it.
Run on mote, at the base eb8b840dd (Go and the GUI files below) and again
at this head's base for internal/gui (core budget) and agent-here,
i18n-keys, agent-menu-label:
- go test -tags nogui ./internal/davsync ./internal/settings
./internal/backup ./internal/gui ./internal/settingsapi: ok
- go test ./internal/cli: ok but TestEveryRouteHasACommandOrIsNamed,
red on main for POST /api/usage/checkin-account alone (#4826)
- GOOS=linux and GOOS=windows go vet ./...: ok; gofmt -l: clean
- GUI, chromium + webkit: agent-here (en/zh/zh-TW/ja/de x 1100/640/360),
i18n-keys, sync-own-fields, mcp-local-server, agent-menu-label:
90/90 ok
- the whole GUI suite: cut off at test 3783 by a session restart; the
rest (key-* on) is still running at push. Reds seen so far, under
three GUI suites at once on mote, none in an agent file: account-eye,
balance-fix (Sub2API, /api/user/self), codex-auto-reset, context-map,
editor-advanced-align; being checked at main and filed there.
Broken by hand, each red then restored:
- hereOf returns nil: TestAgentHereThroughSync "B's kept picks went up"
- backup.Restore's kept skip off: "B after a backup file"
- KeepOwn line dropped: TestAgentsHereIsThisComputers "another
computer's kept agents came in"
- TestSettingsSaveKeepsWhatItDoesNotSend went red on this change before
settingsapi kept AgentsHere (a Settings save would have dropped it).
Not tried: a real two-computer sync with real accounts; no CLI/TUI
command for the flag; Settings -> Sync doesn't list kept agents; WSL
agent ids (codex@wsl:...) only through the key format.
256 lines
7.1 KiB
Go
256 lines
7.1 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
|
|
}
|
|
a := maps.Clone(b.Agents)
|
|
for k := range b.Agents {
|
|
if isOwn(own, k) {
|
|
delete(a, k)
|
|
}
|
|
}
|
|
b.Agents = a
|
|
return b
|
|
}
|
|
|
|
// An agent whose picks stay on this computer (settings' AgentsHere,
|
|
// chenxiang0010 on Discord, as an MCP server can be kept to one) is in an
|
|
// own set as "<agent>.*": every field of it, the ones it gains later too.
|
|
// That set is made for each sync and never kept in Own, so Settings'
|
|
// Follow the others doesn't give it back; the agent's row does.
|
|
|
|
// withHere is own with every field of here's agents.
|
|
func withHere(own map[string]bool, here []string) map[string]bool {
|
|
if len(here) == 0 {
|
|
return own
|
|
}
|
|
out := maps.Clone(own)
|
|
if out == nil {
|
|
out = map[string]bool{}
|
|
}
|
|
for _, id := range here {
|
|
out[id+".*"] = true
|
|
}
|
|
return out
|
|
}
|
|
|
|
// isOwn is whether field k is in own: by itself, or as its agent's.
|
|
func isOwn(own map[string]bool, k string) bool {
|
|
if own[k] {
|
|
return true
|
|
}
|
|
id, _ := splitField(k)
|
|
return own[id+".*"]
|
|
}
|
|
|
|
// hereOf is the agents whose picks stay here, as b's settings say, in
|
|
// order; none when sync leaves the agents out.
|
|
func hereOf(c Config, b backup.Bundle) []string {
|
|
if !c.Agents || b.Settings == nil {
|
|
return nil
|
|
}
|
|
return slices.Compact(slices.Sorted(slices.Values(b.Settings.AgentsHere)))
|
|
}
|
|
|
|
// 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 merged.Agents {
|
|
if isOwn(own, k) {
|
|
delete(a, k)
|
|
}
|
|
}
|
|
for k, v := range remote.Agents {
|
|
if isOwn(own, k) {
|
|
a[k] = v
|
|
}
|
|
}
|
|
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 fields {
|
|
if isOwn(keep, k) {
|
|
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, v := range before {
|
|
if isOwn(keep, k) && 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 !isOwn(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)
|
|
}
|