agent: a look at the stash shares one read of it
Each /api/state and /api/drift poll asked the stash (stash.json) for one value hundreds of times, through Detected, Drift and the agents' wiring checks, and each ask read the file and parsed it again. In a real-account heap profile (20 rounds of drift, state and providers) stashLoad was 24-34 MB of what was allocated. stashShared reads it through filememo.Read, so every look shares one parse until the file changes, by magpie or by another process. Every read-only caller uses it now, the 24 files here and QwenPaw's unstashPeek. stashLoad, which callers change and write back, still gives each its own map, cloned from the same parse. Real use: the owner's real-account sandbox, offline (proxy to :9), 20 warm rounds of /api/drift, /api/state and /api/providers: - allocated 477 MB (d307aa648) -> 411 MB; stashLoad is out of the profile. - /api/state and /api/drift bytes identical to d307aa648's (cmp). Run on mote (mote-run, Go 1.26.8), at this head rebased ond307aa648: - gofmt clean; go vet ok, also GOOS=windows and GOOS=linux. - TestStashLooksShareOneRead alone, 3 times: ok. - Broken by hand (stashShared reading through stashLoad): red, "a look at the stash read and parsed it again". - go test -timeout 45m ./internal/agent ./internal/gateway: ok (263s, 691s).
This commit is contained in:
@@ -446,7 +446,7 @@ func (a *Agent) forgetSetUp() {
|
||||
// keeps an entry in). A field set in magpie to a model of the agent's own,
|
||||
// or its thinking level, doesn't set it up.
|
||||
func (a *Agent) setUp() bool {
|
||||
if stashLoad()[a.ID+".connect.was"] != "" {
|
||||
if stashShared()[a.ID+".connect.was"] != "" {
|
||||
return true
|
||||
}
|
||||
rec := appliedOf(a.ID)
|
||||
@@ -608,7 +608,7 @@ const disconnectedKey = ".disconnected"
|
||||
|
||||
// Disconnected reports whether the user disconnected the agent and hasn't
|
||||
// connected it since.
|
||||
func (a *Agent) Disconnected() bool { return stashLoad()[a.ID+disconnectedKey] == "1" }
|
||||
func (a *Agent) Disconnected() bool { return stashShared()[a.ID+disconnectedKey] == "1" }
|
||||
|
||||
// noModelLeft says why there is no model of magpie's to connect the agent
|
||||
// to: none added yet (NoModelsError), or every one taken out of its lists
|
||||
|
||||
+22
-22
@@ -628,7 +628,7 @@ func claudeIn(at place) *Agent {
|
||||
// own. A pick in /model moves the main model alone, and those following
|
||||
// it go with it when magpie next looks (Follow).
|
||||
mainKey := at.key("claude.main")
|
||||
wroteMain := func() string { return cmp.Or(stashLoad()[mainKey], env("ANTHROPIC_MODEL")) }
|
||||
wroteMain := func() string { return cmp.Or(stashShared()[mainKey], env("ANTHROPIC_MODEL")) }
|
||||
// how Claude Code signs in to magpie (its sign-in field): with magpie's
|
||||
// key, or ("claudeai") with its own claude.ai sign-in, which it keeps
|
||||
// so: an empty ANTHROPIC_AUTH_TOKEN leaves it signed in, where a key
|
||||
@@ -638,7 +638,7 @@ func claudeIn(at place) *Agent {
|
||||
// where set("") and Unwire leave it for the next time it is routed.
|
||||
loginKey := at.key("claude.login")
|
||||
signInHere := func() bool { return at.gwKey() == gateway.Token }
|
||||
keepSignIn := func() bool { return stashLoad()[loginKey] == "claudeai" && signInHere() }
|
||||
keepSignIn := func() bool { return stashShared()[loginKey] == "claudeai" && signInHere() }
|
||||
wiredKey := func() string {
|
||||
if keepSignIn() {
|
||||
return ""
|
||||
@@ -664,14 +664,14 @@ func claudeIn(at place) *Agent {
|
||||
// own, though it is the one it would follow: it stays when the main
|
||||
// model changes
|
||||
ownKey := func(t string) string { return at.key("claude.tier_own." + t) }
|
||||
own := func(t string) bool { return stashLoad()[ownKey(t)] != "" }
|
||||
own := func(t string) bool { return stashShared()[ownKey(t)] != "" }
|
||||
// the auto mode classifier's model as magpie last wrote it (the auto
|
||||
// mode field); autoOurs: the env's is that one or another of magpie's
|
||||
// models, which goes when magpie steps out. Any other is the user's.
|
||||
autoKey := at.key("claude.auto_mode_model")
|
||||
autoOurs := func() bool {
|
||||
v := rawEnv(claudeAutoModeEnv)
|
||||
return v != "" && (v == stashLoad()[autoKey] || isMagpie(env(claudeAutoModeEnv)))
|
||||
return v != "" && (v == stashShared()[autoKey] || isMagpie(env(claudeAutoModeEnv)))
|
||||
}
|
||||
// set once magpie has taken what the env said about each tier's model
|
||||
// from before it routed Claude Code (claudeAbout): what is there after
|
||||
@@ -684,7 +684,7 @@ func claudeIn(at place) *Agent {
|
||||
// magpie routed Claude Code without taking it), else a tier's whose
|
||||
// model changes
|
||||
staleAbout := func(tiers map[string]string) []string {
|
||||
before := stashLoad()[aboutKey] == ""
|
||||
before := stashShared()[aboutKey] == ""
|
||||
keep := map[string]string{}
|
||||
var keys []string
|
||||
for _, t := range claudeTiers {
|
||||
@@ -697,7 +697,7 @@ func claudeIn(at place) *Agent {
|
||||
if !has {
|
||||
continue
|
||||
}
|
||||
if before && v != "" && stashLoad()[at.key("claude.env."+k)] == "" {
|
||||
if before && v != "" && stashShared()[at.key("claude.env."+k)] == "" {
|
||||
keep[at.key("claude.env."+k)] = v
|
||||
}
|
||||
keys = append(keys, "env."+k)
|
||||
@@ -743,7 +743,7 @@ func claudeIn(at place) *Agent {
|
||||
windowKey := at.key("claude.context_tokens")
|
||||
windowOurs := func() bool {
|
||||
cur := env(claudeContextEnv)
|
||||
return cur != "" && cur == stashLoad()[windowKey]
|
||||
return cur != "" && cur == stashShared()[windowKey]
|
||||
}
|
||||
// the modelSettings windows magpie last wrote while Claude Desktop is
|
||||
// on magpie (claudePlan, #2025), as JSON {id: window}, and the
|
||||
@@ -754,12 +754,12 @@ func claudeIn(at place) *Agent {
|
||||
defaultKey := at.key("claude.compact_default")
|
||||
budgetsOurs := func() map[string]int {
|
||||
var m map[string]int
|
||||
_ = json.Unmarshal([]byte(stashLoad()[budgetsKey]), &m)
|
||||
_ = json.Unmarshal([]byte(stashShared()[budgetsKey]), &m)
|
||||
return m
|
||||
}
|
||||
defaultOurs := func() bool {
|
||||
cur, ok := edit.GetJSON(path, "autoCompactWindow")
|
||||
return ok && cur != "" && cur == stashLoad()[defaultKey]
|
||||
return ok && cur != "" && cur == stashShared()[defaultKey]
|
||||
}
|
||||
// writeBudgets puts in entries and the default (0 for none), and takes
|
||||
// out what magpie wrote before that they no longer have
|
||||
@@ -848,7 +848,7 @@ func claudeIn(at place) *Agent {
|
||||
outputKey := at.key("claude.output_tokens")
|
||||
outputOurs := func() bool {
|
||||
cur := env(claudeOutputEnv)
|
||||
return cur != "" && cur == stashLoad()[outputKey]
|
||||
return cur != "" && cur == stashShared()[outputKey]
|
||||
}
|
||||
dropOutput := func() error {
|
||||
defer forget(outputKey)
|
||||
@@ -862,7 +862,7 @@ func claudeIn(at place) *Agent {
|
||||
capsKey := at.key("claude.capabilities")
|
||||
capsOurs := func() bool {
|
||||
cur := env(claudeCapsEnv)
|
||||
return cur != "" && cur == stashLoad()[capsKey]
|
||||
return cur != "" && cur == stashShared()[capsKey]
|
||||
}
|
||||
dropCaps := func() error {
|
||||
defer forget(capsKey)
|
||||
@@ -876,7 +876,7 @@ func claudeIn(at place) *Agent {
|
||||
compactKey := at.key("claude.compact_window")
|
||||
compactOurs := func() bool {
|
||||
cur := env(claudeCompactEnv)
|
||||
return cur != "" && cur == stashLoad()[compactKey]
|
||||
return cur != "" && cur == stashShared()[compactKey]
|
||||
}
|
||||
// set while the window magpie wrote is Long conversations' (not a
|
||||
// provider's Compact at): tune's card reads it as that one value
|
||||
@@ -951,7 +951,7 @@ func claudeIn(at place) *Agent {
|
||||
// Claude Code's /model kept a list without the models added since
|
||||
// (#2046, cjyrainbow).
|
||||
pickerKey := at.key("claude.model_picker")
|
||||
pickerOurs := func() bool { return stashLoad()[pickerKey] != "" || magpiesPicker(path) }
|
||||
pickerOurs := func() bool { return stashShared()[pickerKey] != "" || magpiesPicker(path) }
|
||||
dropPicker := func() error {
|
||||
defer forget(pickerKey)
|
||||
if _, has := edit.GetJSON(path, "modelPicker"); has && pickerOurs() {
|
||||
@@ -1064,21 +1064,21 @@ func claudeIn(at place) *Agent {
|
||||
exploreKey := at.key("claude.explore_inherit_cap")
|
||||
writeExplore := func() error {
|
||||
cur, has := edit.GetJSON(path, "env."+claudeExploreEnv)
|
||||
ours := has && cur == "1" && stashLoad()[exploreKey] == "1"
|
||||
ours := has && cur == "1" && stashShared()[exploreKey] == "1"
|
||||
if !desktopOn() {
|
||||
if ours {
|
||||
if err := edit.DelJSON(path, "env."+claudeExploreEnv); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if stashLoad()[exploreKey] != "" {
|
||||
if stashShared()[exploreKey] != "" {
|
||||
forget(exploreKey)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Presence matters: an explicit empty value is the user's too.
|
||||
if has {
|
||||
if !ours && stashLoad()[exploreKey] != "" {
|
||||
if !ours && stashShared()[exploreKey] != "" {
|
||||
forget(exploreKey)
|
||||
}
|
||||
return nil
|
||||
@@ -1112,7 +1112,7 @@ func claudeIn(at place) *Agent {
|
||||
reviewKey := at.key("claude.auto_mode_server")
|
||||
reviewOurs := func() bool {
|
||||
cur := rawEnv(claudeReviewEnv)
|
||||
return cur != "" && cur == stashLoad()[reviewKey]
|
||||
return cur != "" && cur == stashShared()[reviewKey]
|
||||
}
|
||||
dropReview := func() error {
|
||||
defer forget(reviewKey)
|
||||
@@ -1370,7 +1370,7 @@ func claudeIn(at place) *Agent {
|
||||
// the subagents' effort the user gave with no model of their own: kept
|
||||
// in the stash, not written, and applied once they have a model
|
||||
subEffortKey := at.key("claude.subagent_effort")
|
||||
subagentSaved := func() string { return cmp.Or(stashLoad()[subEffortKey], subagentFollowing()) }
|
||||
subagentSaved := func() string { return cmp.Or(stashShared()[subEffortKey], subagentFollowing()) }
|
||||
// the subagents' model as written, its effort apart: "" for each while
|
||||
// it follows the main model, which takes no effort of theirs
|
||||
subagentAt := func() (string, string) {
|
||||
@@ -1634,7 +1634,7 @@ func claudeIn(at place) *Agent {
|
||||
// subagents given a model of their own keep it; the others run on
|
||||
// the session's, whatever /model picked, or on the tier they ask for.
|
||||
// An older magpie's effort on the main model goes out, kept unwritten.
|
||||
if e := subagentFollowing(); e != "" && stashLoad()[subEffortKey] == "" {
|
||||
if e := subagentFollowing(); e != "" && stashShared()[subEffortKey] == "" {
|
||||
stash(map[string]string{subEffortKey: e})
|
||||
}
|
||||
sub := subagentOwn()
|
||||
@@ -2094,7 +2094,7 @@ func claudeIn(at place) *Agent {
|
||||
return fmt.Errorf("pick a model through magpie for Claude Code first; it can then keep its claude.ai sign-in")
|
||||
}
|
||||
// one kept before stays so, taking effect where it can again
|
||||
if v != "" && !signInHere() && stashLoad()[loginKey] != v {
|
||||
if v != "" && !signInHere() && stashShared()[loginKey] != v {
|
||||
return fmt.Errorf("Claude Code reaches magpie from another machine (%s), where magpie, shared, takes only its sharing key, so it can't keep its claude.ai sign-in there", at.gw())
|
||||
}
|
||||
stash(map[string]string{loginKey: v, wroteAt: at.gw()})
|
||||
@@ -2165,7 +2165,7 @@ func claudeIn(at place) *Agent {
|
||||
}
|
||||
// routed by an older magpie: what was said about the models
|
||||
// before it is taken out as it is now when magpie is wired in
|
||||
if stashLoad()[aboutKey] == "" {
|
||||
if stashShared()[aboutKey] == "" {
|
||||
if err := dropAbout(nil); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -2231,7 +2231,7 @@ func claudeIn(at place) *Agent {
|
||||
// follows on none (follow), unless at an effort of its own
|
||||
fm, fe := tierAt(env(tierEnv("fable")))
|
||||
stays = stays && (follow("fable", bare) != "" || fm == "" || fe != "" || !follows("fable", fm))
|
||||
if _, has := edit.GetJSON(path, "env.ANTHROPIC_MODEL"); !has && main == stashLoad()[mainKey] && stays {
|
||||
if _, has := edit.GetJSON(path, "env.ANTHROPIC_MODEL"); !has && main == stashShared()[mainKey] && stays {
|
||||
return nil
|
||||
}
|
||||
return set(bare)
|
||||
|
||||
@@ -209,7 +209,7 @@ func claudeDesktop(home string) *Agent {
|
||||
}
|
||||
// a Desktop on magpie from before the servers were
|
||||
// carried gets them once
|
||||
if stashLoad()[desktopMCPSeen+p.key] == "" {
|
||||
if stashShared()[desktopMCPSeen+p.key] == "" {
|
||||
if err := desktopCarryMCP(p); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
@@ -684,7 +684,7 @@ var desktopImportOn = map[string]any{"enabled": true, "bannerBehavior": "detect"
|
||||
// The mark is read, never unstashed: it outlives the check and a switch
|
||||
// off, or the next Sync would write the policy back.
|
||||
func desktopSeedImport(p desktopPaths) error {
|
||||
if stashLoad()[desktopImport+p.key] != "" {
|
||||
if stashShared()[desktopImport+p.key] != "" {
|
||||
return nil
|
||||
}
|
||||
if desktopObject(p.prof) != nil {
|
||||
@@ -735,7 +735,7 @@ func desktopCarryMCP(p desktopPaths) error {
|
||||
}
|
||||
have := desktopServers(p.config3p)
|
||||
carried := map[string]json.RawMessage{}
|
||||
if prev := stashLoad()[desktopMCP+p.key]; prev != "" {
|
||||
if prev := stashShared()[desktopMCP+p.key]; prev != "" {
|
||||
json.Unmarshal([]byte(prev), &carried)
|
||||
}
|
||||
merged := map[string]json.RawMessage{}
|
||||
|
||||
+22
-22
@@ -372,14 +372,14 @@ func codexIn(at place) *Agent {
|
||||
routed := func() bool { return asProvider() || viaBase() }
|
||||
// joined: connected by Join, on a model of Codex's own (its last pick)
|
||||
// with magpie's beside it, by the base URL
|
||||
joined := func() bool { return viaBase() && stashLoad()[at.key("codex.joined")] == "1" }
|
||||
joined := func() bool { return viaBase() && stashShared()[at.key("codex.joined")] == "1" }
|
||||
// beside: set on one of magpie's models beside the ChatGPT sign-in, by
|
||||
// the base URL, Codex's own models in its list too: one of them written
|
||||
// in by Codex (its /model, or a Codex app still running on its old
|
||||
// pick) leaves it connected, its config still on magpie's gateway
|
||||
// (#940: it read as not connected), the change told as drift. One
|
||||
// picked on the Agents page still takes magpie out (set).
|
||||
beside := func() bool { return viaBase() && stashLoad()[at.key("codex.beside")] == "1" }
|
||||
beside := func() bool { return viaBase() && stashShared()[at.key("codex.beside")] == "1" }
|
||||
// chatGPT is whether Codex is signed in to ChatGPT. With account
|
||||
// discovery off (#1535) auth.json stays unread, and Codex is taken to be
|
||||
// as magpie last wired it, beside a ChatGPT sign-in or not, so turning
|
||||
@@ -405,7 +405,7 @@ func codexIn(at place) *Agent {
|
||||
// (codex.out), which Sync puts back once it has room
|
||||
signedIn, known := codexChatGPTKnown(dir)
|
||||
if !known {
|
||||
return joined() || beside() || stashLoad()[at.key("codex.out")] == "1"
|
||||
return joined() || beside() || stashShared()[at.key("codex.out")] == "1"
|
||||
}
|
||||
return signedIn
|
||||
}
|
||||
@@ -794,7 +794,7 @@ func codexIn(at place) *Agent {
|
||||
if at.spell != nil || !asProvider() || sessions.CodexProviderIn(dir) != magpieID {
|
||||
return
|
||||
}
|
||||
if p := stashLoad()[at.key("codex.provider")]; p != "" && p != "openai" {
|
||||
if p := stashShared()[at.key("codex.provider")]; p != "" && p != "openai" {
|
||||
return
|
||||
}
|
||||
moved, err := sessions.RelabelCodexThreads(dir, "openai", magpieID)
|
||||
@@ -808,7 +808,7 @@ func codexIn(at place) *Agent {
|
||||
// mirrorsTaken are the mirrors takeMirrors pointed at magpie (#1796)
|
||||
mirrorsTaken := func() []string {
|
||||
var ids []string
|
||||
json.Unmarshal([]byte(stashLoad()[at.key("codex.mirrors")]), &ids)
|
||||
json.Unmarshal([]byte(stashShared()[at.key("codex.mirrors")]), &ids)
|
||||
return ids
|
||||
}
|
||||
// isCCSwitchMirror reports whether a model_providers table is CC Switch's
|
||||
@@ -824,7 +824,7 @@ func codexIn(at place) *Agent {
|
||||
return false
|
||||
}
|
||||
u := t["base_url"]
|
||||
return u == "" || (u == at.codexURL() && (stashLoad()[at.key("codex.mirror_failover")] == p || slices.Contains(mirrorsTaken(), p)))
|
||||
return u == "" || (u == at.codexURL() && (stashShared()[at.key("codex.mirror_failover")] == p || slices.Contains(mirrorsTaken(), p)))
|
||||
}
|
||||
// profileNamed are the provider tables a profile of config.toml names:
|
||||
// the user's to pick, each left as it is
|
||||
@@ -963,7 +963,7 @@ func codexIn(at place) *Agent {
|
||||
}
|
||||
for _, id := range taken {
|
||||
t, err := edit.GetTOMLTable(path, "model_providers."+id)
|
||||
if err != nil || t["base_url"] != at.codexURL() || stashLoad()[at.key("codex.mirror_failover")] == id {
|
||||
if err != nil || t["base_url"] != at.codexURL() || stashShared()[at.key("codex.mirror_failover")] == id {
|
||||
continue
|
||||
}
|
||||
if err := edit.DelTOMLKey(path, "model_providers."+id, "base_url"); err != nil {
|
||||
@@ -986,7 +986,7 @@ func codexIn(at place) *Agent {
|
||||
return err
|
||||
}
|
||||
was := map[string]string{}
|
||||
json.Unmarshal([]byte(stashLoad()[at.key("codex.tables")]), &was)
|
||||
json.Unmarshal([]byte(stashShared()[at.key("codex.tables")]), &was)
|
||||
for id, u := range ccSwitchTables() {
|
||||
if u == at.v1() {
|
||||
continue
|
||||
@@ -1010,7 +1010,7 @@ func codexIn(at place) *Agent {
|
||||
return err
|
||||
}
|
||||
var was map[string]string
|
||||
json.Unmarshal([]byte(stashLoad()[at.key("codex.tables")]), &was)
|
||||
json.Unmarshal([]byte(stashShared()[at.key("codex.tables")]), &was)
|
||||
now := ccSwitchTables()
|
||||
for id, u := range was {
|
||||
if now[id] != at.v1() {
|
||||
@@ -1025,13 +1025,13 @@ func codexIn(at place) *Agent {
|
||||
}
|
||||
// api: the user wants magpie as Codex's provider even while Codex is
|
||||
// signed in to ChatGPT
|
||||
api := func() bool { return stashLoad()[at.key("codex.login")] == "api" }
|
||||
api := func() bool { return stashShared()[at.key("codex.login")] == "api" }
|
||||
// out reports whether the Codex app holds the ChatGPT account Codex is
|
||||
// signed in to (codexUsedUp), for magpie to become Codex's provider
|
||||
// while it does (#540): never with the sign-in kept on ChatGPT
|
||||
// (chatgpt), where the user would rather the Codex app wait for the
|
||||
// account than leave its ChatGPT state
|
||||
out := func() bool { return stashLoad()[at.key("codex.login")] != "chatgpt" && codexUsedUp() }
|
||||
out := func() bool { return stashShared()[at.key("codex.login")] != "chatgpt" && codexUsedUp() }
|
||||
dropBase := func() error {
|
||||
forget(at.key("codex.failover"))
|
||||
if !viaBase() {
|
||||
@@ -1040,7 +1040,7 @@ func codexIn(at place) *Agent {
|
||||
return edit.DelTOMLTop(path, "openai_base_url")
|
||||
}
|
||||
dropMirrorFailover := func() error {
|
||||
p := stashLoad()[at.key("codex.mirror_failover")]
|
||||
p := stashShared()[at.key("codex.mirror_failover")]
|
||||
if p == "" {
|
||||
return nil
|
||||
}
|
||||
@@ -1119,7 +1119,7 @@ func codexIn(at place) *Agent {
|
||||
// the user disconnected Codex and hasn't connected it since: magpie
|
||||
// writes nothing of its own back (paynezhuang on Discord: the base URL
|
||||
// was back after every update)
|
||||
disconnected := func() bool { return stashLoad()[at.key("codex"+disconnectedKey)] == "1" }
|
||||
disconnected := func() bool { return stashShared()[at.key("codex"+disconnectedKey)] == "1" }
|
||||
// Codex on one of its own models goes through magpie too while more of
|
||||
// its ChatGPT accounts are on there, so one out of its allowance hands
|
||||
// the turn to the next; with none, or once the user disconnected Codex,
|
||||
@@ -1170,7 +1170,7 @@ func codexIn(at place) *Agent {
|
||||
// gone again each time magpie started). One already there while
|
||||
// failover is on is taken as failover's, as a magpie from before
|
||||
// the mark wrote it.
|
||||
owned := stashLoad()[at.key("codex.failover")] == "1"
|
||||
owned := stashShared()[at.key("codex.failover")] == "1"
|
||||
switch {
|
||||
case on && !viaBase():
|
||||
if err := edit.SetTOMLTop(path, edit.KV{Path: "openai_base_url", Value: at.codexURL()}); err != nil {
|
||||
@@ -1445,7 +1445,7 @@ func codexIn(at place) *Agent {
|
||||
// back on the sign-in as Join leaves it. Only the model set
|
||||
// left magpie the provider of one of Codex's own for good
|
||||
// (eddie.zhan on Discord, Always ChatGPT from the takeover)
|
||||
if asProvider() && stashLoad()[at.key("codex.out")] == "1" && !out() {
|
||||
if asProvider() && stashShared()[at.key("codex.out")] == "1" && !out() {
|
||||
forget(at.key("codex.out"), at.key("codex.out_own"))
|
||||
if err := edit.DelTOMLTop(path, "model_provider", "model_catalog_json"); err != nil {
|
||||
return err
|
||||
@@ -1562,10 +1562,10 @@ func codexIn(at place) *Agent {
|
||||
// Agents row said "its own models stay in /model" while the Codex
|
||||
// app was meant to be in its API state (#2267)
|
||||
Joined: func() bool { return joined() && !asProvider() },
|
||||
AppOut: func() bool { return asProvider() && !api() && chatGPT() && stashLoad()[at.key("codex.out")] == "1" },
|
||||
AppOut: func() bool { return asProvider() && !api() && chatGPT() && stashShared()[at.key("codex.out")] == "1" },
|
||||
HoldMatters: func() bool {
|
||||
return stashLoad()[at.key("codex.login")] == "" && chatGPT() &&
|
||||
(viaBase() || asProvider() && stashLoad()[at.key("codex.out")] == "1")
|
||||
return stashShared()[at.key("codex.login")] == "" && chatGPT() &&
|
||||
(viaBase() || asProvider() && stashShared()[at.key("codex.out")] == "1")
|
||||
},
|
||||
Beside: beside,
|
||||
Routed: routed,
|
||||
@@ -1658,10 +1658,10 @@ func codexIn(at place) *Agent {
|
||||
}
|
||||
// put back before #540's way back, which would take
|
||||
// codex/<model> for a magpie model picked beside the sign-in
|
||||
if own := stashLoad()[at.key("codex.out_own")]; own != "" {
|
||||
if own := stashShared()[at.key("codex.out_own")]; own != "" {
|
||||
m := get("model")
|
||||
switch {
|
||||
case stashLoad()[at.key("codex.out")] != "1" || !asProvider() || codexOwnOf(m) != own:
|
||||
case stashShared()[at.key("codex.out")] != "1" || !asProvider() || codexOwnOf(m) != own:
|
||||
// moved since (another model picked, magpie API, a
|
||||
// disconnect): that stands
|
||||
forget(at.key("codex.out_own"))
|
||||
@@ -1693,7 +1693,7 @@ func codexIn(at place) *Agent {
|
||||
switch {
|
||||
case viaBase() && !asProvider() && out():
|
||||
return set(m)
|
||||
case asProvider() && stashLoad()[at.key("codex.out")] == "1" && !out():
|
||||
case asProvider() && stashShared()[at.key("codex.out")] == "1" && !out():
|
||||
return set(m)
|
||||
}
|
||||
}
|
||||
@@ -1989,7 +1989,7 @@ func codexIn(at place) *Agent {
|
||||
// (#1824, Qyburn3 on X). Kept in the stash, where set("")
|
||||
// leaves it.
|
||||
Key: "login", Label: "sign-in", Quiet: true,
|
||||
Get: func() string { return stashLoad()[at.key("codex.login")] },
|
||||
Get: func() string { return stashShared()[at.key("codex.login")] },
|
||||
Set: func(v string) error {
|
||||
if v != "" && v != "api" && v != "chatgpt" {
|
||||
return fmt.Errorf("sign-in is api, chatgpt or empty (ChatGPT), not %q", v)
|
||||
|
||||
@@ -198,7 +198,7 @@ func codexAsyncSync(path, key string, routed, on bool, percent int, tableOK bool
|
||||
return err
|
||||
}
|
||||
var st codexAsyncStash
|
||||
if raw := stashLoad()[key]; raw != "" {
|
||||
if raw := stashShared()[key]; raw != "" {
|
||||
json.Unmarshal([]byte(raw), &st)
|
||||
}
|
||||
was := st
|
||||
@@ -375,7 +375,7 @@ func codexAsyncOwn(home string) string {
|
||||
return ""
|
||||
}
|
||||
var st codexAsyncStash
|
||||
if raw := stashLoad()[at.key("codex.async")]; raw != "" {
|
||||
if raw := stashShared()[at.key("codex.async")]; raw != "" {
|
||||
json.Unmarshal([]byte(raw), &st)
|
||||
}
|
||||
bare := "features.async_compaction"
|
||||
|
||||
@@ -236,7 +236,7 @@ func (a *Agent) changedAt(files []string, reads func() string) time.Time {
|
||||
sum := sha256.Sum256([]byte(reads()))
|
||||
digest := hex.EncodeToString(sum[:8])
|
||||
key := a.ID + ".started"
|
||||
was, at, ok := strings.Cut(stashLoad()[key], " ")
|
||||
was, at, ok := strings.Cut(stashShared()[key], " ")
|
||||
n, err := strconv.ParseInt(at, 10, 64)
|
||||
if ok && err == nil && was == digest {
|
||||
return time.Unix(0, n)
|
||||
|
||||
@@ -320,7 +320,7 @@ func droidIn(at place) *Agent {
|
||||
ms = append(ms, c.raw)
|
||||
}
|
||||
if with {
|
||||
saved := stashLoad()[keyWrote]
|
||||
saved := stashShared()[keyWrote]
|
||||
var last map[string]map[string]json.RawMessage
|
||||
json.Unmarshal([]byte(saved), &last)
|
||||
wrote := map[string]map[string]json.RawMessage{}
|
||||
|
||||
@@ -196,7 +196,7 @@ func grokIn(at place) *Agent {
|
||||
return err
|
||||
}
|
||||
var wrote map[string]int
|
||||
saved := stashLoad()[keyWindows]
|
||||
saved := stashShared()[keyWindows]
|
||||
if saved != "" {
|
||||
json.Unmarshal([]byte(saved), &wrote)
|
||||
}
|
||||
|
||||
@@ -193,7 +193,7 @@ func grokApp(home string) *Agent {
|
||||
// The App stops working when that route is magpie. A route the user
|
||||
// picks in the App after this stays.
|
||||
releaseRoute := func() error {
|
||||
if stashLoad()[grokAppReleased] == "1" {
|
||||
if stashShared()[grokAppReleased] == "1" {
|
||||
return nil
|
||||
}
|
||||
def, _, err := grokAppRead(path)
|
||||
@@ -300,7 +300,7 @@ func grokApp(home string) *Agent {
|
||||
// over the one it replaced.
|
||||
rememberRoute := func() error {
|
||||
def, _, err := grokAppRead(path)
|
||||
if err != nil || def == "" || def == magpieID || stashLoad()[keyDefault] != "" {
|
||||
if err != nil || def == "" || def == magpieID || stashShared()[keyDefault] != "" {
|
||||
return err
|
||||
}
|
||||
stash(map[string]string{keyDefault: def})
|
||||
@@ -451,7 +451,7 @@ func grokAppOwn(path, keyDefault, cur string) []Option {
|
||||
add(id, kv["name"], direct)
|
||||
}
|
||||
}
|
||||
add(stashLoad()[keyDefault], "", "")
|
||||
add(stashShared()[keyDefault], "", "")
|
||||
add(cur, "", "")
|
||||
return group("Grok App", out)
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ func hanako(home string) *Agent {
|
||||
}
|
||||
if onMagpie() {
|
||||
// the stash goes only once the model is back
|
||||
if err := hanakoBack(dir, agent, stashLoad()[key()]); err != nil {
|
||||
if err := hanakoBack(dir, agent, stashShared()[key()]); err != nil {
|
||||
return err
|
||||
}
|
||||
forget(key())
|
||||
|
||||
@@ -134,7 +134,7 @@ func hermesIn(at place) *Agent {
|
||||
// and forgets it; while the config is on a model of its own, that
|
||||
// one. The model magpie set (setKey) stays recorded, for the bots
|
||||
// put back on it to follow the next one once connected again.
|
||||
own := hermesModel{Provider: stashLoad()[key+"model.provider"], Default: stashLoad()[key+"model.default"]}
|
||||
own := hermesModel{Provider: stashShared()[key+"model.provider"], Default: stashShared()[key+"model.default"]}
|
||||
if !onMagpie() {
|
||||
own = hermesModel{Provider: getKey("model.provider"), Default: getKey("model.default")}
|
||||
}
|
||||
@@ -193,7 +193,7 @@ func hermesIn(at place) *Agent {
|
||||
// and the one it is on through magpie now, which Hermes
|
||||
// seeds a new bot with too, picked there by `hermes
|
||||
// model` or before the record was kept
|
||||
was := stashLoad()[setKey]
|
||||
was := stashShared()[setKey]
|
||||
now := ""
|
||||
if onMagpie() {
|
||||
now = getKey("model.default")
|
||||
|
||||
@@ -364,7 +364,7 @@ func jcodeAt(at place) *Agent {
|
||||
// can't list.
|
||||
func jcodeOwn(path, keyPick, cur string) []Option {
|
||||
var was jcodePick
|
||||
json.Unmarshal([]byte(stashLoad()[keyPick]), &was)
|
||||
json.Unmarshal([]byte(stashShared()[keyPick]), &was)
|
||||
var out []Option
|
||||
for _, m := range []string{was.Model, cur} {
|
||||
if m == "" || prefixed(m) || slices.ContainsFunc(out, func(o Option) bool { return o.Value == m }) {
|
||||
|
||||
@@ -177,7 +177,7 @@ func kimiWroteKey(path string) string { return "kimi:" + path + ":wrote" }
|
||||
|
||||
func kimiWrote(path string) map[string]map[string]string {
|
||||
out := map[string]map[string]string{}
|
||||
if v := stashLoad()[kimiWroteKey(path)]; v != "" {
|
||||
if v := stashShared()[kimiWroteKey(path)]; v != "" {
|
||||
json.Unmarshal([]byte(v), &out)
|
||||
}
|
||||
return out
|
||||
|
||||
@@ -160,7 +160,7 @@ type kiroCrewJoined struct {
|
||||
|
||||
func kiroCrewJoinedNow() (kiroCrewJoined, bool) {
|
||||
var j kiroCrewJoined
|
||||
s := stashLoad()[kiroCrewJoinedKey]
|
||||
s := stashShared()[kiroCrewJoinedKey]
|
||||
return j, s != "" && json.Unmarshal([]byte(s), &j) == nil && j.Agent != ""
|
||||
}
|
||||
|
||||
@@ -344,7 +344,7 @@ type kiroCrewWrote struct {
|
||||
|
||||
func kiroCrewWroteNow() (kiroCrewWrote, bool) {
|
||||
var w kiroCrewWrote
|
||||
s := stashLoad()[kiroCrewWroteKey]
|
||||
s := stashShared()[kiroCrewWroteKey]
|
||||
return w, s != "" && json.Unmarshal([]byte(s), &w) == nil && w.Backend != ""
|
||||
}
|
||||
|
||||
@@ -438,7 +438,7 @@ func kiroCrew(home string) *Agent {
|
||||
return nil
|
||||
}
|
||||
var s kiroCrewStash
|
||||
json.Unmarshal([]byte(stashLoad()[kiroCrewWasKey]), &s)
|
||||
json.Unmarshal([]byte(stashShared()[kiroCrewWasKey]), &s)
|
||||
var set []edit.KV
|
||||
var del []string
|
||||
put := func(key string, was kiroCrewWas, ours bool) {
|
||||
|
||||
@@ -492,7 +492,7 @@ func ompAt(at place, dir string, entry func() ompProviderEntry) *Agent {
|
||||
}
|
||||
scopeGoneRename(gone, from, to)
|
||||
for _, w := range was {
|
||||
s := stashLoad()[w]
|
||||
s := stashShared()[w]
|
||||
ns := move(s)
|
||||
if ompYAMLList(s) {
|
||||
t, err := edit.EditYAMLTextStrings(edit.YAMLText(s), move)
|
||||
|
||||
@@ -389,7 +389,7 @@ func openClawAt(at place) *Agent {
|
||||
}
|
||||
c := gjson.ParseBytes(s)
|
||||
var was openClawWas
|
||||
if b := stashLoad()[keyWas]; b != "" {
|
||||
if b := stashShared()[keyWas]; b != "" {
|
||||
json.Unmarshal([]byte(b), &was)
|
||||
} else if !ours(c) {
|
||||
m := c.Get(openClawModel)
|
||||
@@ -489,7 +489,7 @@ func openClawAt(at place) *Agent {
|
||||
// which magpie can't list.
|
||||
func openClawOwn(keyWas, cur string) []Option {
|
||||
var was openClawWas
|
||||
json.Unmarshal([]byte(stashLoad()[keyWas]), &was)
|
||||
json.Unmarshal([]byte(stashShared()[keyWas]), &was)
|
||||
var out []Option
|
||||
for _, m := range []string{openClawPrimary(gjson.ParseBytes(was.Model)), cur} {
|
||||
if m == "" || prefixed(m) || slices.ContainsFunc(out, func(o Option) bool { return o.Value == m }) {
|
||||
|
||||
@@ -254,7 +254,7 @@ func plugged(home string, p *agentplug.Agent) *Agent {
|
||||
at := here(home)
|
||||
saved := func() (pluggedStash, bool) {
|
||||
var st pluggedStash
|
||||
v := stashLoad()[key]
|
||||
v := stashShared()[key]
|
||||
return st, v != "" && json.Unmarshal([]byte(v), &st) == nil
|
||||
}
|
||||
save := func(st pluggedStash) {
|
||||
|
||||
@@ -212,7 +212,7 @@ func qwenIn(at place) *Agent {
|
||||
// the user's own value is stashed once, whatever it is —
|
||||
// "openai" of their own too, or it can't be told from
|
||||
// magpie's on a later wire or Sync and comes off with it
|
||||
if _, seen := stashLoad()[keyAuthSeen]; !seen {
|
||||
if _, seen := stashShared()[keyAuthSeen]; !seen {
|
||||
kv := map[string]string{keyAuthSeen: "1"}
|
||||
if v, ok := edit.GetJSON(path, qwenAuth); ok {
|
||||
kv[keyAuth] = v
|
||||
|
||||
@@ -462,7 +462,7 @@ func qwenpawAt(at place, work string, p qwenpawProfile) *Agent {
|
||||
func unstashPeek(k string) string {
|
||||
stashMu.Lock()
|
||||
defer stashMu.Unlock()
|
||||
return stashLoad()[k]
|
||||
return stashShared()[k]
|
||||
}
|
||||
|
||||
// qwenpawNamesMagpie says an agent of QwenPaw's still names magpie's
|
||||
|
||||
@@ -35,7 +35,7 @@ type scopeGone struct {
|
||||
|
||||
func scopeGoneLoad(key string) scopeGone {
|
||||
var g scopeGone
|
||||
if raw := stashLoad()[key]; raw != "" {
|
||||
if raw := stashShared()[key]; raw != "" {
|
||||
json.Unmarshal([]byte(raw), &g)
|
||||
}
|
||||
return g
|
||||
|
||||
@@ -147,7 +147,7 @@ func snowIn(at place) *Agent {
|
||||
mine := false
|
||||
if b, err := edit.Read(ours); err == nil && b != nil && snowIsOurs(b) {
|
||||
raw, mine = b, true
|
||||
if w := stashLoad()[keyWrote]; w != "" {
|
||||
if w := stashShared()[keyWrote]; w != "" {
|
||||
json.Unmarshal([]byte(w), &wrote)
|
||||
}
|
||||
} else if base != "" {
|
||||
@@ -173,7 +173,7 @@ func snowIn(at place) *Agent {
|
||||
// one), takes magpie.json away (or puts back the user's own of that
|
||||
// name), and copies the profile it is on to config.json
|
||||
leave := func(to string) error {
|
||||
was, none := unstash(keyActive), stashLoad()[keyNone] != ""
|
||||
was, none := unstash(keyActive), stashShared()[keyNone] != ""
|
||||
forget(keyNone, keyWrote)
|
||||
// a profile asked for by name is made the active one; else the one
|
||||
// the user was on, or none at all when Snow had no active file
|
||||
@@ -331,7 +331,7 @@ func snowIn(at place) *Agent {
|
||||
// (#1457). An empty value goes back to the one they were on.
|
||||
to := ""
|
||||
if v != "" {
|
||||
to = snowFindProfile(profiles, v, stashLoad()[keyActive])
|
||||
to = snowFindProfile(profiles, v, stashShared()[keyActive])
|
||||
if to == "" {
|
||||
return fmt.Errorf("%q is neither a model through magpie nor the model of one of Snow CLI's profiles: nothing was changed (magpie models snow lists them; magpie snow default goes back to your own profile)", v)
|
||||
}
|
||||
|
||||
+25
-4
@@ -5,11 +5,13 @@ import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"log"
|
||||
"maps"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
"github.com/yetone/magpie/internal/lastgood"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
@@ -28,12 +30,31 @@ var stashMu sync.Mutex
|
||||
|
||||
func stashPath() string { return filepath.Join(filepath.Dir(provider.Path()), "stash.json") }
|
||||
|
||||
// stashLoad is the stash as a map of the caller's own, to change and write
|
||||
// back.
|
||||
func stashLoad() map[string]string {
|
||||
out := map[string]string{}
|
||||
if b, err := os.ReadFile(stashPath()); err == nil {
|
||||
json.Unmarshal(b, &out)
|
||||
m := maps.Clone(stashRead(filememo.ReadNow[map[string]string]))
|
||||
if m == nil {
|
||||
m = map[string]string{}
|
||||
}
|
||||
return out
|
||||
return m
|
||||
}
|
||||
|
||||
// stashShared is the stash for a look at its values: shared with every
|
||||
// other look, so never changed. Each window poll looks at it hundreds of
|
||||
// times, and a fresh read and parse for each was 35 MB of what twenty
|
||||
// polls allocated.
|
||||
func stashShared() map[string]string {
|
||||
return stashRead(filememo.Read[map[string]string])
|
||||
}
|
||||
|
||||
func stashRead(read func(kind, path string, parse func([]byte) (map[string]string, error)) (map[string]string, error)) map[string]string {
|
||||
m, _ := read("stash", stashPath(), func(b []byte) (map[string]string, error) {
|
||||
out := map[string]string{}
|
||||
json.Unmarshal(b, &out)
|
||||
return out, nil
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
// stashWrite writes m as the stash, a file there that doesn't parse kept
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A look at the stash's values reads and parses stash.json once, not once
|
||||
// for each look, and still sees each change: magpie's own, and one another
|
||||
// process wrote.
|
||||
func TestStashLooksShareOneRead(t *testing.T) {
|
||||
stashHome(t)
|
||||
stash(map[string]string{"a.was": "1"})
|
||||
first := reflect.ValueOf(stashShared()).UnsafePointer()
|
||||
for range 20 {
|
||||
if reflect.ValueOf(stashShared()).UnsafePointer() != first {
|
||||
t.Fatal("a look at the stash read and parsed it again")
|
||||
}
|
||||
}
|
||||
own := stashLoad()
|
||||
own["a.was"] = "mine"
|
||||
if got := stashShared()["a.was"]; got != "1" {
|
||||
t.Fatalf("a change to a caller's own stash reached the shared one: %q", got)
|
||||
}
|
||||
stash(map[string]string{"b.was": "2"})
|
||||
if got := stashShared()["b.was"]; got != "2" {
|
||||
t.Fatalf("magpie's own change not seen: %q", got)
|
||||
}
|
||||
if err := os.WriteFile(stashPath(), []byte(`{"c.was": "theirs, longer"}`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s := stashShared(); s["c.was"] != "theirs, longer" || s["a.was"] != "" {
|
||||
t.Fatalf("another process's write not seen: %v", s)
|
||||
}
|
||||
}
|
||||
@@ -508,7 +508,7 @@ func t3CodexBaseOf(args string) string {
|
||||
func t3Stashed(key string) bool {
|
||||
stashMu.Lock()
|
||||
defer stashMu.Unlock()
|
||||
return stashLoad()[key] != ""
|
||||
return stashShared()[key] != ""
|
||||
}
|
||||
|
||||
// t3Tokens is T3 Code's tokenizeCliArgs (packages/shared/src/cliArgs.ts),
|
||||
|
||||
@@ -72,7 +72,7 @@ type tuneLong struct {
|
||||
}
|
||||
|
||||
func tuneLongStashed() (tuneLong, bool) {
|
||||
raw := stashLoad()[tuneLongKey]
|
||||
raw := stashShared()[tuneLongKey]
|
||||
var st tuneLong
|
||||
if raw == "" || json.Unmarshal([]byte(raw), &st) != nil {
|
||||
return tuneLong{}, false
|
||||
@@ -313,14 +313,14 @@ func claudeLongEnv(path, v string) bool {
|
||||
// user's own.
|
||||
func claudeLongWindow(path, key, stashKey string) bool {
|
||||
v, ok := edit.GetJSON(path, key)
|
||||
if !ok || v == "" || v != stashLoad()[stashKey] {
|
||||
if !ok || v == "" || v != stashShared()[stashKey] {
|
||||
return false
|
||||
}
|
||||
return v == longValue(settings.Load())
|
||||
}
|
||||
|
||||
func tuneStashed(agent, knob, model string) (tuneStash, bool) {
|
||||
raw := stashLoad()[tuneKey(agent, knob, model)]
|
||||
raw := stashShared()[tuneKey(agent, knob, model)]
|
||||
if raw == "" {
|
||||
return tuneStash{}, false
|
||||
}
|
||||
@@ -474,7 +474,7 @@ type tuneAsyncStash struct {
|
||||
}
|
||||
|
||||
func asyncStashed() (tuneAsyncStash, bool) {
|
||||
raw := stashLoad()[tuneAsyncKey]
|
||||
raw := stashShared()[tuneAsyncKey]
|
||||
var st tuneAsyncStash
|
||||
if raw == "" || json.Unmarshal([]byte(raw), &st) != nil {
|
||||
return tuneAsyncStash{}, false
|
||||
|
||||
@@ -111,7 +111,7 @@ func zcode(home string) *Agent {
|
||||
return err
|
||||
}
|
||||
if v == "" {
|
||||
if _, ok := stashLoad()[zcodeSearchedKey]; ok {
|
||||
if _, ok := stashShared()[zcodeSearchedKey]; ok {
|
||||
forget(zcodeSearchedKey)
|
||||
}
|
||||
return edit.DelJSON(path, key)
|
||||
@@ -613,7 +613,7 @@ func zcodeSearchedPut(searched map[string]bool, ids []string) {
|
||||
}
|
||||
}
|
||||
v := strings.Join(keep, "\n")
|
||||
if stashLoad()[zcodeSearchedKey] != v {
|
||||
if stashShared()[zcodeSearchedKey] != v {
|
||||
stash(map[string]string{zcodeSearchedKey: v})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user