In WSL, with only magpie tui and Cursor's subscription, Claude Code said ECONNREFUSED. The terminal set agents to the gateway but never served it, and nothing listened at 127.0.0.1:3425 unless the app, magpie web or magpie serve ran. magpie tui now serves the gateway in its own process while it is open, as the app does, whenever no magpie answers at its address. One that does (the app, magpie web, magpie serve) keeps it. The terminal looks every 15s and takes the gateway up once that one is gone. The status line says "● gateway <url> · served while magpie tui is open" when it has nothing else to say, or "✗ gateway not running: <why>" when the port couldn't be had. On quitting after serving, it says the gateway stopped with it and what serves it otherwise. The usage text and docs/reference.md say so too. Tested in a sandbox HOME on port 3486 with a real DeepSeek key: magpie tui alone answered /v1/models; Claude Code (sandbox HOME) set to deepseek/deepseek-flash ran a Bash tool turn through it; the status line showed the gateway; q printed the note and freed the port. TestTUIServesTheGatewayWhenNoneDoes (serves, status, lets go at quit) and TestTUILeavesTheGatewayToAnotherAndTakesItUp (leaves another magpie's gateway alone, takes it up once it is gone) are new and pass 3x under -race; they don't build without the change. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
// The terminal serves the gateway while it is open, as the app does, when
|
|
// no magpie has it: in WSL with only magpie tui, the agents it connected to
|
|
// magpie asked an address nothing listened at, and Claude Code said
|
|
// ECONNREFUSED (gnayiab on X). Another magpie that has it (the app, magpie
|
|
// web, magpie serve) keeps it, and this one takes it up once that one is
|
|
// gone.
|
|
|
|
// gw is who serves the gateway, for the status line.
|
|
var gw struct {
|
|
here atomic.Bool // this process serves it
|
|
err atomic.Pointer[string] // why it couldn't, "" or nil when it could
|
|
}
|
|
|
|
// gatewayWatch is how often the terminal looks for a gateway gone.
|
|
var gatewayWatch = 15 * time.Second
|
|
|
|
// serveGateway serves the gateway here until ctx ends whenever no magpie
|
|
// answers at its address, and returns a wait for the one it serves to have
|
|
// stopped.
|
|
func serveGateway(ctx context.Context) (wait func()) {
|
|
var wg sync.WaitGroup
|
|
try := func() {
|
|
if gw.here.Load() || gateway.Running() {
|
|
return
|
|
}
|
|
s := gateway.New()
|
|
up := make(chan error, 1)
|
|
gw.here.Store(true)
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
err := s.ListenAndServe(ctx)
|
|
gw.here.Store(false)
|
|
if err != nil && ctx.Err() == nil {
|
|
msg := err.Error()
|
|
gw.err.Store(&msg)
|
|
}
|
|
up <- err
|
|
}()
|
|
// listening, or refused the port, in a moment
|
|
for i := 0; i < 20 && !gateway.Running(); i++ {
|
|
select {
|
|
case <-up:
|
|
return
|
|
case <-time.After(50 * time.Millisecond):
|
|
}
|
|
}
|
|
if gateway.Running() {
|
|
gw.err.Store(nil)
|
|
}
|
|
}
|
|
try()
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
t := time.NewTicker(gatewayWatch)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-t.C:
|
|
try()
|
|
}
|
|
}
|
|
}()
|
|
return wg.Wait
|
|
}
|
|
|
|
// gatewayStatus is the status line's word on the gateway when it has
|
|
// nothing else to say: served here, or why agents can't reach it.
|
|
func gatewayStatus() string {
|
|
if gw.here.Load() {
|
|
return sMuted.Render("● gateway " + gateway.URL() + " · served while magpie tui is open")
|
|
}
|
|
if e := gw.err.Load(); e != nil && *e != "" {
|
|
return sBad.Render("✗ ") + sText.Render("gateway not running: "+*e)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// gatewayGoneNote is said as the terminal quits having served the gateway.
|
|
func gatewayGoneNote() string {
|
|
return "magpie: the gateway stopped with magpie tui; agents connected to magpie reach it while magpie tui, magpie web or magpie serve runs"
|
|
}
|