From f7e38d0d2ebdf954f25aa09b351157ca8f9bd1f3 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 27 Sep 2026 14:54:24 +0800 Subject: [PATCH] =?UTF-8?q?agents:=20a=20magpie=20on=20a=20port=20of=20its?= =?UTF-8?q?=20own=20(magpie-dev,=20a=20sandbox)=20takes=20an=20agent=20wir?= =?UTF-8?q?ed=20to=20the=20installed=20magpie's=20gateway=20(127.0.0.1:342?= =?UTF-8?q?5)=20for=20that=20one's,=20not=20drift=20=E2=80=94=20the=20two?= =?UTF-8?q?=20took=20turns=20flagging=20Codex=20with=20Apply=20again,=20ea?= =?UTF-8?q?ch=20rewiring=20it=20to=20itself?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/agent/applied.go | 24 +++++++++++++++++++++++- internal/agent/applied_test.go | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/internal/agent/applied.go b/internal/agent/applied.go index 4d476a90c..a081d73c2 100644 --- a/internal/agent/applied.go +++ b/internal/agent/applied.go @@ -114,7 +114,7 @@ var started = time.Now() // A field moved from one magpie model to another (the agent's own picker) // isn't drift; one moved off magpie is. func (a *Agent) Drift() *Drift { - if len(a.Fields) == 0 { + if len(a.Fields) == 0 || a.theInstalled() { return nil } vals := a.Values() @@ -162,6 +162,28 @@ func (a *Agent) Drift() *Drift { return nil } +// installedURL is the gateway of the magpie people install, on its own +// port; a magpie on another one (magpie-dev, a sandbox) is run beside it. +var installedURL = "http://" + gateway.DefaultAddr + +// elsewhere: this magpie is not on the installed one's port. +func elsewhere() bool { return !sameHost(gateway.URL(), installedURL) } + +// theInstalled: this magpie runs beside the installed one, and the agent is +// wired to that one's gateway, not this one's — the installed magpie set it +// up, so it is that one's to check, not drift here. Otherwise the two would +// take turns flagging each other's wiring. +func (a *Agent) theInstalled() bool { + if !elsewhere() || a.Path == "" { + return false + } + b, err := os.ReadFile(a.Path) + if err != nil { + return false + } + return strings.Contains(string(b), gateway.DefaultAddr) && !strings.Contains(string(b), hostOf(gateway.URL())) +} + // bypassed: the agent was used — while this gateway was up and after magpie // last set it — and no request of it arrived since. A request leaves within // moments of the prompt; a little grace keeps one in flight from counting. diff --git a/internal/agent/applied_test.go b/internal/agent/applied_test.go index 11055b7e8..9488ea1c8 100644 --- a/internal/agent/applied_test.go +++ b/internal/agent/applied_test.go @@ -237,3 +237,24 @@ func TestCodexDriftReached(t *testing.T) { t.Fatalf("reached magpie: %+v", d) } } + +// A magpie on a port of its own (magpie-dev) runs beside the installed one: +// an agent that one wired to its gateway is not drift here, while one wired +// to neither still is. +func TestDriftInstalledMagpie(t *testing.T) { + t.Setenv("MAGPIE_ADDR", "127.0.0.1:3426") + home, _ := codexHome(t, "", "") + cx := codex(home) + if err := cx.Apply("model", "fake/m1"); err != nil { + t.Fatal(err) + } + cfg := filepath.Join(home, ".codex", "config.toml") + os.WriteFile(cfg, []byte("openai_base_url = \"http://"+gateway.DefaultAddr+gateway.CodexPath+"\"\nmodel = \"fake/m1\"\n"), 0o644) + if d := cx.Drift(); d != nil { + t.Fatalf("the installed magpie's wiring taken for drift: %+v", d) + } + os.WriteFile(cfg, []byte("openai_base_url = \"http://127.0.0.1:9999"+gateway.CodexPath+"\"\nmodel = \"fake/m1\"\n"), 0o644) + if d := cx.Drift(); d == nil || d.Kind != "unwired" { + t.Fatalf("wired to neither: %+v", d) + } +}