Files
magpie/internal/agent/reach.go
T
yetone 588e51ea4c agent, gui: an agent connected by its config to an address where nothing answers is told, not shown connected (#1013, cjyrainbow: Codex Desktop's WSL environment reads Windows' ~/.codex/config.toml, whose openai_base_url and [model_providers.magpie].base_url the reporter pointed at Windows as WSL sees it, forwarded to 127.0.0.1:3425 by a portproxy of their own; after a reboot the portproxy listened no more and the Agents page still said Codex was connected) — Drift looked only at the config, which was right. Now an agent pointed at an address off loopback (this machine's Codex at an address the user kept, #816, through Agent.reach; every WSL twin under NAT, at Windows as the distro sees it, through Agent.Gateway) has that address tried from here (reach.go: GET /v1/models, any HTTP status an answer, 2 s timeout, in the background, at most every 20 s per address, never waited for), and one that doesn't answer while the gateway answers on loopback is a new drift, "unreachable", with Addr and the advice in Detail: under WSL (or on Windows) magpie with Settings › Share on local network on, or the user's netsh interface portproxy, which may need setting up again after a restart; Windows' firewall allowing the port for the WSL network; on Windows 11, networkingMode=mirrored; elsewhere, the forward to start again or gateway.URL() to point at. An answer not had yet, and a gateway down on loopback too, are no drift. The config is never changed for it, except where the address is one a WSL distro reached Windows at before (distro.Was, the last 4 kept in wsl.json as a re-probe finds Gateway changed) and the distro reaches Windows at another now: Drift's Move names that one, and Reapply moves the config there (Agent.move; Codex's codexMoveGateway rewrites openai_base_url and magpie's own provider's base_url, only where they begin with the old address). An address no distro was seen at is the user's own and is never offered a move. In the GUI the row's line says "{agent} is set up, but nothing answers at the address it reaches magpie by" in red with the advice as its tooltip, and its pill is "How to fix" (an i, not the reapply arrow), which opens the advice in a dialog (too long for the status line; it wraps and can be copied) and sets nothing — or "Use <address>" (the existing "Use {url}" string) when there is a Move, which reapplies; two new t() strings with zh, ja and de. The probe runs from Windows, not from inside WSL, so a firewall keeping WSL out isn't seen; the advice names it. TestKeptAddressNotAnswering (the reporter's case: Codex at 172.28.96.1, unreachable with the address and loopback in the detail; no drift once it answers, none with the gateway down too), TestKeptAddressProbeNeverWaitedFor, TestKeptAddressMovedWithWSL (Move from a Was address, Reapply rewrites both URLs, Check empty and no drift after; a 10.0.0.7 address is told but never moved) and TestWSLAgentAddressNotAnswering (Codex in a NAT distro told, with Share on local network, portproxy and firewall; a mirrored one never probed) fail without the Drift change and pass; TestLoopbackNotProbed, TestReachProbe (httptest: a 401 is an answer, a closed port none) and TestWSLWas pass. TestMain stubs reachProbe so no test dials out. -race -count=10 on them; go test -tags nogui ./internal/... under a temp HOME, go vet (darwin, windows), gofmt -l, linux and windows (nogui) builds pass. agent-unreachable.test.cjs (the window at 560px and the tray panel at 440px: the red line and tooltip, How to fix's dialog posting nothing, Use <address> posting the reapply, no sideways scroll) fails on main and passes on Chromium and WebKit in en, zh, ja and de; agent-connect, agent-layout, agent-field-names, aside-native, gui-de, gui-ja and i18n-keys pass on both. Not tried on a real WSL NAT setup (the Windows test box didn't answer ssh). docs/subsystems/agent-wiring.md says it.
2026-10-06 19:17:55 +08:00

165 lines
5.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package agent
import (
"net"
"net/http"
"net/url"
"runtime"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/gateway"
)
// An agent pointed at magpie by an address other than this machine's
// loopback — this machine's Codex at Windows as WSL sees it or a forward of
// the user's (#816), an agent in a WSL distro under NAT — is connected by
// its config alone; whether anything answers there is another matter
// (#1013: a portproxy gone after a reboot, the Agents page still said
// connected). Drift asks: each such address is tried from here, in the
// background, and one that doesn't answer while the gateway does on
// loopback is told (Drift kind "unreachable"), with what listens there.
// reachAge is how long an answer is taken as it is: Drift is asked every
// few seconds while the window is up, a probe at most this often.
var reachAge = 20 * time.Second
// reachProbe says whether anything HTTP answers at base: any status will
// do (a gateway shared on the network answers 401 to no key), a refused or
// timed-out connection doesn't. A var for tests.
var reachProbe = realReachProbe
func realReachProbe(base string) bool {
c := &http.Client{Timeout: 2 * time.Second}
res, err := c.Get(strings.TrimSuffix(base, "/") + "/v1/models")
if err != nil {
return false
}
res.Body.Close()
return true
}
var reach struct {
sync.Mutex
m map[string]reachAnswer
}
type reachAnswer struct {
at time.Time // when the last probe finished; zero: none yet
ok, busy bool
}
// reachable is what the last probe of base said, and whether there is one
// (known); a probe older than reachAge, or none, is started in the
// background and never waited for, so Drift stays quick.
func reachable(base string) (ok, known bool) {
reach.Lock()
defer reach.Unlock()
if reach.m == nil {
reach.m = map[string]reachAnswer{}
}
a := reach.m[base]
if !a.busy && time.Since(a.at) > reachAge {
a.busy = true
reach.m[base] = a
probe := reachProbe
go func() {
ok := probe(base)
reach.Lock()
reach.m[base] = reachAnswer{at: time.Now(), ok: ok}
reach.Unlock()
}()
}
return a.ok, !a.at.IsZero()
}
// offLoopback: base is an http address on a host other than this
// machine's loopback and the gateway's own.
func offLoopback(base string) bool {
u, err := url.Parse(base)
if err != nil || u.Scheme != "http" || u.Hostname() == "" {
return false
}
h := u.Hostname()
if h == "localhost" || sameHost(base, gateway.URL()) {
return false
}
ip := net.ParseIP(h)
return ip == nil || !ip.IsLoopback()
}
// pointedAt is the gateway's address in the agent's config: where it is
// kept apart from gateway.URL (this machine's Codex), a WSL distro's way to
// it, else "".
func (a *Agent) pointedAt() string {
switch {
case a.reach != nil:
return a.reach()
case a.Gateway != nil:
return a.Gateway()
}
return ""
}
// unreachable is the drift of an agent pointed at an address off loopback
// that doesn't answer from here while the gateway answers on loopback; nil
// when it answers, isn't asked yet, or the gateway itself is down (then
// nothing would, and that is no fault of the address).
func (a *Agent) unreachable(on Field, now string) *Drift {
addr := a.pointedAt()
if !offLoopback(addr) {
return nil
}
// both asked at once, so one round of probes settles it
ok, known := reachable(addr)
up, upKnown := reachable(gateway.URL())
if ok || !known || !up || !upKnown {
return nil
}
d := &Drift{Kind: "unreachable", Field: on.Key, Now: now, Want: now, Addr: addr}
host := strings.TrimPrefix(addr, "http://")
d.Detail = a.Name + " is pointed at magpie at " + addr + ", where nothing answers from this computer, though magpie answers at " + gateway.URL() + ". "
if a.WSL != "" || runtime.GOOS == "windows" {
d.Detail += "Under WSL's NAT networking, Windows as WSL sees it reaches magpie only while something listens on " + host +
": magpie itself with Settings › Share on local network on (it then listens on every address, and asks other computers for a gateway key), or a forward of your own (netsh interface portproxy, which may need setting up again after Windows restarts). " +
"Once it answers here and WSL still can't connect, Windows' firewall may be keeping WSL out: allow the port " + gateway.Port() + " for the WSL network. " +
"On Windows 11, networkingMode=mirrored under [wsl2] in %UserProfile%\\.wslconfig lets WSL reach magpie at 127.0.0.1 instead."
} else {
d.Detail += "Whatever forwarded " + host + " to magpie isn't listening now; start it again, or point " + a.Name + " at " + gateway.URL() + "."
}
if to, distro := wslMovedFrom(addr); to != "" {
d.Move = to
d.Detail += " WSL " + distro + " reaches Windows at " + to + " now, not " + addr + " as before: Use " + to + " points " + a.Name + " there."
}
return d
}
// wslMovedFrom is the address a WSL distro reaches Windows at now, when
// base is on one it reached Windows at before (its networking's address
// changed since), and that distro's name; "" otherwise. A host no distro
// was seen at is the user's own, and is never offered to be moved.
func wslMovedFrom(base string) (to, name string) {
u, err := url.Parse(base)
if err != nil {
return "", ""
}
h, port := u.Hostname(), u.Port()
wsl.Lock()
defer wsl.Unlock()
for n, d := range wsl.seen {
if d == nil || d.Gateway == "" || d.Gateway == h {
continue
}
for _, w := range d.Was {
if w == h {
if port == "" {
port = gateway.Port()
}
return "http://" + net.JoinHostPort(d.Gateway, port), n
}
}
}
return "", ""
}