diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 7438dc745..75656ec59 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -615,6 +615,19 @@ claude-hud shows. One that signs in to magpie with magpie's key headers only for a claude.ai subscriber. A reply another provider answered carries none. +## A restart to update + +A run is a Claude Code process of this magpie's, so a restart to update +that hands the gateway to the new version ([GUI shell](gui-shell.md#a-restart-to-update-hands-the-gateway-over)) +waits for the bridge's turns, and for none of the gateway's other requests +(`restartBusy` in [`handover.go`](../../internal/gui/handover.go): `Busy.Bridge`, +`Tools`, `BridgeLast`). A run's tool calls go to the gateway's port, which +the new one serves once it has the socket; a `claude-mcp` token the new one +doesn't know is forwarded to the old one's callback socket +(`ForwardCallbacks`, `forwardCallback`), which serves it until it quits +(`ServeCallbacks`). A run idle between turns ends with the old process, +and the next turn starts a run in the new one, as after any restart. + ## Verification ```sh diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 058bec024..0b2aa29b8 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -192,6 +192,19 @@ recorded as a prediction failure (`noPrediction`). - A relayed Responses stream gives each output item the one id its `response.output_item.added` gave it, in every event after that carries an `item_id` or an `item`, and in the `output` of `response.completed`, `response.incomplete` or `response.failed` (`idTidy` in [`itemids.go`](../../internal/gateway/itemids.go), applied in `passthrough` before `phaseTidy`). GitHub Copilot's `/responses` gives an item another id in every event (checked 2026-10-19 on a real account, gpt-5.5 with web_search), and Codex starts an item by the added's id and ends one by the done's, so a commentary message was shown twice and a web search never ended (echo_ts on Discord, `TestCopilotItemKeepsItsID`). An upstream whose ids agree goes through byte for byte (`TestItemIDsThatAgreeGoAsTheyCame`); a translated reply's ids are magpie's own. - A model's reply limit is published within the window beside it (#1438, `provider.OutputWithin`, `Entry.PublishedOutput`): `/v1/models`' `max_output_tokens` (`modelObject`), Cursor Private Inference's `capabilities.max_output_tokens` (`cursorLocalModel`), Muse Code's `limit.output` (`museModels`), the GUI's model and group rows (`providerInfo`, `providersState`), `magpie model output`, and every agent's file (`agent.maxTokens`, #338). The window and the output can come from different sources: Grok's backend lists grok-4.7's 256000 window, and models.dev fills its 500000 output (`catalog.Decorate` and `Provider.replyLimit` fill an output only where the vendor gives none). An unknown window or output is published as it is. The catalog keeps the unclamped `Entry.Output`, and a group's member is still asked for no more than that (`outputLimit`, `withMaxOutput`), so a request isn't lowered by the clamp. A limit the user set stays in their settings as they set it (`TestModelListsKeepOutputWithinTheWindow`). Outside a group, only a Max output the user set for the model (`Entry.OutputSet`) lowers a request (`userOutput`): Claude Code asks a claude-* model for its own full limit (128000 on Opus 5.5) and is told no figure of magpie's for it (`claudeOutput`), and a key whose quota is held per request as max_tokens times the price turned every turn away (YaCo on Discord, `TestUserMaxOutputLowersAPlainProvidersRequest`). A vendor's or models.dev's figure doesn't lower it, since it may be stale. - A request that names no reply limit, translated to Anthropic's Messages API, which needs `max_tokens` (`forwardTranslated`), is sent the model's published limit (`askedOutput` in [`maxoutput.go`](../../internal/gateway/maxoutput.go)): the user's Max output, else the vendor's list or models.dev, within the window. `anthropicBody`'s `defaultMaxTokens` (16384) is left only for a model whose limit isn't known. Every Codex turn on a Claude went at 16384 before, and a long thinking and tool turn ran out of it: `stop_reason: max_tokens` reaches a Responses client as `incomplete_details.reason: "max_output_tokens"` (#1792, tensornull). A limit the client names goes as it is. The thinking budget fits under the filled limit as under one the client named (`TestAnthropicMaxTokensIsTheModels`). +- A restart to update on a Mac or Linux hands the listening socket to the + new version ([GUI shell](gui-shell.md#a-restart-to-update-hands-the-gateway-over), + [`handover.go`](../../internal/gateway/handover.go)). `Inherit` gives the + new process's `ListenAndServe` a socket to serve when the address is the + same (`sameAddr`), and calls back once it serves; `HandOff` has the old + one's shutdown wait up to `handedOffDrain` (15 minutes) for its requests + in flight instead of `shortDrain` (2s). Requests per minute, concurrency + and sticky routing's in-memory state are each process's own: sticky + routing is flushed to disk before the socket goes (`FlushSticks`), but + while the old one drains, a provider's limits can be passed by what each + has in flight. `TestHandedOverGatewayFinishesItsStream`, + `TestInheritedForAnotherAddressIsLetGo`, `TestToolCallOfTheMagpieBeforeGoesToIt` in + [`handover_test.go`](../../internal/gateway/handover_test.go). ## Remote magpie APIs diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 9f6b6b933..bdea3c79c 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -12,7 +12,7 @@ three. | Part | Responsibility | Source | | --- | --- | --- | | Native host | `Run` starts the Wails v3 app: the tray icon, the panel, the main window, single-instance handover, `magpie://` import links and open at login. `host` implements `Windows` for the API: show and hide, open URLs and folders, copy to the clipboard, fit and tint the panel, and set the text size. A copy on Linux sets PRIMARY before CLIPBOARD, since GTK 3 serves CLIPBOARD empty when PRIMARY is set after it on a compositor with no primary selection (KDE Plasma with middle-click paste off, #1881); on Wayland the copy is read back through the compositor, and one that doesn't read back whole answers `/api/copy` with 501, so the page tries its clipboard API, then the copy command, then says the copy failed with the text to copy by hand (`copy-fallback.test.cjs`). | [`app.go`](../../internal/gui/app.go), [`clipboard_gtk3.go`](../../internal/gui/clipboard_gtk3.go), [`clipboard_gtk3.h`](../../internal/gui/clipboard_gtk3.h), [`mainwindow.go`](../../internal/gui/mainwindow.go), [`traymenu.go`](../../internal/gui/traymenu.go) | -| Backend | `startBackend` starts the gateway this process serves, unless another magpie already serves it. In that case this process only shows the other one's status (`gw` is nil). `stopServing` drains requests in flight. | [`backend.go`](../../internal/gui/backend.go) | +| Backend | `startBackend` starts the gateway this process serves, unless another magpie already serves it. In that case this process only shows the other one's status (`gw` is nil). `stopServing` drains requests in flight. A restart to update can hand the listening socket to the new version instead (see [A restart to update hands the gateway over](#a-restart-to-update-hands-the-gateway-over)). | [`backend.go`](../../internal/gui/backend.go) | | API and assets | `Handler` serves `assets/` (embedded with `//go:embed assets`) and the `/api/*` routes, such as `/api/state`, `/api/set` and `/api/agents/{action}/{id}`. `held` lets one API request's reads share one catalog build (`provider.Hold`), and drops it after any write. `revalidated` and `versionedPage` name each script by its content hash, so a cache never serves an old `app.js` under a new page; a file asked for under its current hash (`?v=`) is cached for good (`immutable`), anything else is revalidated by its ETag. | [`api.go`](../../internal/gui/api.go) | | Browser mode | `StartWeb` serves the page and the gateway for `magpie web`. The browser signs in with a run key in a link. Gateway mode shows Providers, Gateway, Routing, Usage, Sessions, Plugins and Settings on a machine that is only a gateway. `webHandler` gzips the page's text files and the API's JSON for a browser that takes gzip (`webCompressed`, with a `-gzip` ETag that still answers 304); the app's own windows read the page in-process and get it uncompressed. | [`web.go`](../../internal/gui/web.go), [`gatewaymode.go`](../../internal/gui/gatewaymode.go) | | The page | `app.js` keeps one state object per view and renders it into a list, with no framework. The views have their own files: `plugins.js` and `context.js`, and those a plugin adds under `assets/plugins//` (the Gateway, Sessions, Library, Routing and Usage pages, see [plugin runtime](plugin-runtime.md)) (Usage's Context tab, and the context window card the Routing page draws under a request's story, fed by `/api/context` in `context.go`, which scores and tags each agent; clicking a part, a cell or an item opens its text from `/api/context/text`, the last 10 requests' masked bodies in memory (`openCtxText`), rendered 100 items and 20000 characters at a time; its "Tuned for you" cards are fed by `/api/tune` in `tune.go`, see [agent wiring](agent-wiring.md)). Strings go through `t()` with translations in `i18n.js`, and a plugin's own in its `i18n.js` (zh, zh-TW, ja and de; the English text is the key and the fallback). zh-TW is Traditional Chinese with Taiwan's words; a system language of zh-TW, zh-HK, zh-MO or any zh-Hant picks it, and the tray menu and notifications follow (`trayLang`). Dropdowns use `openProtoMenu`. Every app menu (`openProtoMenu`, `openRowMenu`, `openSessCombo`) is placed by `placeMenu`: under its anchor, or over it when the window runs out below; one that fits neither way takes the larger room, its height held to it, and scrolls inside itself, and a scroll inside a menu doesn't close it (#1437, `removed-groups-menu-scroll.test.cjs`). The highest any popover goes is `popTop`: 8px under `header.top` on a Mac window, where the traffic lights are drawn over the page, and 8px from the top elsewhere; `placeMenu`, the field picker's `placePop` (which also holds `#pop`'s height to that room) and an agent's model list (`openAgentModels`) all use it (#1573, `popover-mac-chrome.test.cjs`). Scrolling is done only by `scrollOnPurpose(e)` with the reader's event. | [`assets/app.js`](../../internal/gui/assets/app.js), [`assets/i18n.js`](../../internal/gui/assets/i18n.js) | @@ -438,6 +438,107 @@ never empty: `OwnerTrace` says so with its second result). See `TestGatewayTraceIsLoopbackOnly` in [`trace_peer_test.go`](../../internal/gateway/trace_peer_test.go). +### A restart to update hands the gateway over + +On a Mac and on Linux, a restart into a downloaded update (the Update pill, +Settings › Version, the tray's Restart to update, Restart automatically) +starts the new version beside the running one and hands it the gateway's +listening socket, rather than ending the gateway and waiting for the new +one to listen again (xuanwo on X). Nothing an agent sends meanwhile is +refused: the port never closes, and requests already in flight finish in +the old process. + +The trigger is `handsOver` in +[`handover.go`](../../internal/gui/handover.go): the app (`Run` sets +`handoverHost` and `startExe`), on darwin or linux, serving the gateway +itself, not a dev backend (whose own handover is `gateway.Handover`, +below) and no `MAGPIE_NO_HANDOVER` in the environment. `restartInto` in [`update.go`](../../internal/gui/update.go) +then calls `beginHandover` instead of the relaunch-after-exit. The steps: + +1. The old magpie opens an Offer ([`internal/handoff`](../../internal/handoff/handoff.go)): + a unix socket in a 0700 temporary folder (in `/tmp` when `TMPDIR`'s + path leaves a socket's name no room, `sockDir`), named by `handover.json` in + the config folder with its pid, its program path and the time. Then it + starts the new version beside itself: `open -n` on the bundle on a Mac + (`update.OpenBeside`), the binary at the path it started from on Linux + (`update.StartBeside`; `startExe`, since `/proc/self/exe` reads + "(deleted)" once the update replaced it). +2. The new magpie's `Run` calls `takeOver` before anything else: a pointer + fresh (2 minutes), of a process still running, for the same program and + not its own is dialled, the new one says hello, and receives the + listening socket (SCM_RIGHTS) with the address, the bridge's callback + socket and the old pid (`handoff.Take`). The peer's uid is checked on + accept. `gateway.Inherit` keeps the socket for the next `ListenAndServe` + on the same address (`sameAddr`; another address lets it go), which + serves on it and then acks. +3. The old one, on the ack (`AwaitAck`), withdraws the pointer, hides + itself where it can (below), calls `gateway.Server.HandOff`, ends its + backend and waits for its requests, then quits. A handed-off server's + shutdown waits up to `handedOffDrain` (15 minutes) for requests in + flight, not the 2s an ordinary quit gives them (`shortDrain`). Its + `OnShutdown` then skips what goes with the gateway, which the new one + now does: ending Codex's failover, porting Codex rollouts and installing + the update. +4. Should the Offer not open, the new version not start, not say hello in + 60s (`helloWait`), not ack in 90s (`ackWait`), or drop the connection, + the old one keeps the socket, takes its instance lock back and restarts + as before: the new one opened once it has exited (`fail`, + `update.Relaunch` or `relaunchBinary`). It first waits, still serving, + for its requests in flight to finish (at most `fallbackWait`, 15 + minutes), since the restart was let go without waiting for them. A + second restart click while a handover is under way does nothing + (`pending`). + +Two magpies never show at once. On a Mac the old one lets go of its +instance lock before it gives the socket (`releaseInstance`), so the new +one's `runningAlready` takes it, and once the ack comes the old one hides +its menu bar icon, panel and window and leaves the Dock (`onHandedOver`). +On Linux the D-Bus name Wails holds can't be let go, so the new one serves +the gateway at once but waits for the old pid to end (`awaitExit`, at most +`handedOffWait`, 16 minutes) before it makes its app and tray; the old one +keeps its tray icon until it has drained. + +What the old process alone has: + +- Sticky routing (which account each conversation stuck to) is flushed to + disk before the socket is given (`gateway.FlushSticks`). +- Claude Code runs the subscription bridge started send their tool calls + to the gateway's port, which the new one now serves. The old one serves + them on a unix socket in the Offer's folder (`ServeCallbacks`), and the + new one forwards a claude-mcp token it doesn't know there + (`ForwardCallbacks`, `forwardCallback`; `ownCallback` lets them in while + forwarding is on). See [Claude subscription bridge](claude-subscription-bridge.md#a-restart-to-update). +- Bridge turns can't move to another process, so a restart that hands + over waits only for them (`restartBusy`, `blocking`: `Busy.Bridge` as + Requests, `Tools`, `BridgeLast` as Last). The page is told + (`updateJSON.Handover`) and says it waits for the Claude subscription's + replies, with other requests carrying on across the restart. +- Refreshes of rotating OAuth tokens hold a file lock across both + processes (see [Providers and accounts](providers-accounts.md)). + +Not carried over, accepted: each process counts its own requests per +minute and concurrency, so for the drain a provider's limits can be +exceeded by what the old one still has in flight; a bridge run idle +between turns ends with the old process, and the next turn starts a new +run as after any restart; the new one's start-up migrations run while the +old one still serves its last requests. + +Windows has no SCM_RIGHTS for a listening socket in this form, and +`magpie web` re-executes itself in place; both restart as before, waiting +for the gateway to be idle (#577). `make dev` keeps its own handover, a +new backend listening beside the old one (`gateway.Handover`, `devBackend`). + +See `TestRestartHandsTheGatewayOver` (the new version a second process, +a stream across the restart finishing whole, requests during it answered, +and the old one quitting only after its stream) and +`TestHandoverNotMadeRestartsAsBeforeOnceIdle` in +[`handover_unix_test.go`](../../internal/gui/handover_unix_test.go), +`TestRestartWaitsForWhatAHandoverCantCarry` in +[`handover_test.go`](../../internal/gui/handover_test.go), the gateway's +[`handover_test.go`](../../internal/gateway/handover_test.go), the +socket passing in [`handoff_test.go`](../../internal/handoff/handoff_test.go), +and `update-wait.test.cjs`. + ## Verification The session readers include Cursor CLI chat stores, Cursor editor project diff --git a/internal/gateway/busy.go b/internal/gateway/busy.go index eb7bd0df2..4e7946e4a 100644 --- a/internal/gateway/busy.go +++ b/internal/gateway/busy.go @@ -17,10 +17,18 @@ import ( // apart, a turn waiting on them being in Tools. Last is when the last // counted request ended, so a caller can tell a tool chain's gap — the // agent running a tool between two requests — from the end of its turn. +// +// Bridge and BridgeLast are the same for the bridge's turns alone, which +// a restart that hands the gateway over (handover.go) still waits for: +// their Claude Code lives in this process, and only here can the turn go +// on. Every other request finishes in the magpie that hands over while +// the new one takes the next. type Busy struct { - Requests int `json:"requests"` - Tools int `json:"tools"` - Last time.Time `json:"-"` + Requests int `json:"requests"` + Tools int `json:"tools"` + Last time.Time `json:"-"` + Bridge int `json:"-"` + BridgeLast time.Time `json:"-"` } // Any is whether there is anything in flight. @@ -28,13 +36,26 @@ func (b Busy) Any() bool { return b.Requests > 0 || b.Tools > 0 } // Busy says what is in flight at the gateway now. func (s *Server) Busy() Busy { - b := Busy{Requests: int(s.inFlight.Load()), Tools: s.subscription.parked()} + b := Busy{Requests: int(s.inFlight.Load()), Tools: s.subscription.parked(), Bridge: int(s.bridgeInFlight.Load())} if n := s.lastDone.Load(); n != 0 { b.Last = time.Unix(0, n) } + if n := s.bridgeDone.Load(); n != 0 { + b.BridgeLast = time.Unix(0, n) + } return b } +// bridgeTurn counts a turn served through the subscription bridge for +// Busy.Bridge until done is called. +func (s *Server) bridgeTurn() (done func()) { + s.bridgeInFlight.Add(1) + return func() { + s.bridgeDone.Store(time.Now().UnixNano()) + s.bridgeInFlight.Add(-1) + } +} + // counted is h with its requests counted for Busy. func (s *Server) counted(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -64,6 +85,9 @@ type busyCounters struct { inFlight atomic.Int64 lastDone atomic.Int64 // unix ns active inflights // what each is, for /v1/magpie/concurrency + // the subscription bridge's turns among them, and when the last ended + bridgeInFlight atomic.Int64 + bridgeDone atomic.Int64 // unix ns } // parked is how many runs wait on their caller's tool results. diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index fe00ba661..ba50bdaa7 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -3539,6 +3539,11 @@ func (b *subscriptionBridge) mcpCall(w http.ResponseWriter, r *http.Request) { run := b.runs[token] b.mu.Unlock() if run == nil { + // a run of the magpie this one took the gateway over from, its + // turn under way when the gateway changed hands (handover.go) + if forwardCallback(w, r) { + return + } http.Error(w, "unknown or expired Claude run", http.StatusNotFound) return } @@ -4104,6 +4109,7 @@ func (b *subscriptionBridge) whyCallsLost(req *Request) string { } func (s *Server) serveClaudeSubscription(w http.ResponseWriter, r *http.Request, from provider.Protocol, p provider.Provider, model string, body []byte, usage *Usage) (int, string) { + defer s.bridgeTurn()() owner := p.ID if p.Account != nil { owner += "\x00" + p.Account.User diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index b3bf9cb51..9fb97de7c 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -402,6 +402,9 @@ type Server struct { // taken off it (see Relisten) lnMu sync.Mutex ln net.Listener + // handedOff: another magpie serves the port now, and this one's end + // waits for its requests in flight to finish (handover.go) + handedOff atomic.Bool // what it serves other computers TLS with, nil without (tls.go) tls *tls.Config // the images described for models that can't see them (vision.go) @@ -535,6 +538,11 @@ func (s *Server) ListenAndServe(ctx context.Context) error { s.lnMu.Lock() s.ln = ln s.lnMu.Unlock() + // a socket handed over (handover.go): the magpie that had it stops + // accepting once told this one serves on it + if ack := takeAck(); ack != nil { + go ack() + } // the observers plugins registered (usage.Observer) run while this // process serves, and plugins turned on or off elsewhere (the window, // `magpie plugin builtin`) come and go here too @@ -625,11 +633,7 @@ func (s *Server) ListenAndServe(ctx context.Context) error { go func() { defer close(drained) <-ctx.Done() - wait := 2 * time.Second - if Handover { - wait = 15 * time.Minute - } - c, cancel := context.WithTimeout(context.Background(), wait) + c, cancel := context.WithTimeout(context.Background(), s.drainFor()) defer cancel() srv.Shutdown(c) s.subscription.abortAll() diff --git a/internal/gateway/handover.go b/internal/gateway/handover.go new file mode 100644 index 000000000..ef3b751b7 --- /dev/null +++ b/internal/gateway/handover.go @@ -0,0 +1,254 @@ +package gateway + +import ( + "context" + "errors" + "io" + "log" + "net" + "net/http" + "net/http/httputil" + "net/url" + "os" + "strconv" + "sync" + "time" +) + +// A restart to update hands the gateway over rather than ending it +// (internal/handoff): the magpie updating gives the new version the +// socket it listens on, the new one serves on it at once, and the old +// stops accepting and finishes the requests it has (handedOffDrain at +// most) before it quits. No connection is refused on the way: the socket +// is never closed, only accepted from by the other process. + +// handedOffDrain is how long a magpie that handed the gateway over waits +// for its requests in flight to finish: the longest stream an agent has +// been seen to keep, as `make dev`'s handover waits. +var handedOffDrain = 15 * time.Minute + +// shortDrain is how long the gateway's end waits for its requests when +// nothing serves the port after it: a quit, or a restart that waits. +var shortDrain = 2 * time.Second + +// inherited is the listener the magpie before this one handed over, taken +// by the first Listen of its address; ack is called once this gateway +// serves on it, which tells the one before to stop accepting. +var inherited struct { + sync.Mutex + ln net.Listener + ack func() +} + +// Inherit has this process's gateway serve on ln, the socket the magpie +// it replaces listened on, rather than listening itself; ack is called +// once it serves. +func Inherit(ln net.Listener, ack func()) { + inherited.Lock() + defer inherited.Unlock() + if inherited.ln != nil { + inherited.ln.Close() + } + inherited.ln, inherited.ack = ln, ack +} + +// Inherited is whether a listener handed over is waiting to be served: +// the gateway's port is then held by the magpie that handed it over, and +// is this process's to serve all the same. +func Inherited() bool { + inherited.Lock() + defer inherited.Unlock() + return inherited.ln != nil +} + +// takeInherited is the listener handed over when it listens on addr, and +// the ack to call once it serves. One on another address (the settings +// changed in between) is closed: the port is then listened on as usual. +func takeInherited(addr string) (net.Listener, func()) { + inherited.Lock() + defer inherited.Unlock() + ln, ack := inherited.ln, inherited.ack + if ln == nil { + return nil, nil + } + inherited.ln, inherited.ack = nil, nil + if !sameAddr(ln.Addr(), addr) { + log.Printf("gateway: the socket handed over listens on %s, not %s; listening anew", ln.Addr(), addr) + ln.Close() + return nil, nil + } + return ln, ack +} + +// pendingAck is the ack of the socket handed over that Listen returned, +// for ListenAndServe to call once it serves on it. +var pendingAck struct { + sync.Mutex + ack func() +} + +func setAck(ack func()) { + pendingAck.Lock() + pendingAck.ack = ack + pendingAck.Unlock() +} + +// takeAck is the ack waiting, nil when the listener wasn't handed over. +func takeAck() func() { + pendingAck.Lock() + defer pendingAck.Unlock() + ack := pendingAck.ack + pendingAck.ack = nil + return ack +} + +// sameAddr is whether a listens where addr names: the same port, on the +// same IP or both on every interface. +func sameAddr(a net.Addr, addr string) bool { + ta, ok := a.(*net.TCPAddr) + if !ok { + return false + } + host, port, err := net.SplitHostPort(addr) + if err != nil || port != strconv.Itoa(ta.Port) { + return false + } + if host == "" { + return ta.IP == nil || ta.IP.IsUnspecified() + } + ips := []net.IP{net.ParseIP(host)} + if ips[0] == nil { + // a name (localhost): any of its addresses + var err error + if ips, err = net.LookupIP(host); err != nil { + return false + } + } + for _, ip := range ips { + if ip.IsUnspecified() { + if ta.IP == nil || ta.IP.IsUnspecified() { + return true + } + } else if ip.Equal(ta.IP) { + return true + } + } + return false +} + +// ListenerFile is a copy of the socket the gateway listens on, to hand to +// the magpie that takes it over; the caller closes it once sent. +func (s *Server) ListenerFile() (*os.File, error) { + s.lnMu.Lock() + ln := s.ln + s.lnMu.Unlock() + if t, ok := ln.(*tlsListener); ok { + ln = t.Listener + } + tl, ok := ln.(*net.TCPListener) + if !ok { + return nil, errors.New("the gateway isn't listening on a TCP socket") + } + return tl.File() +} + +// HandOff has the gateway, once its ctx ends, wait for every request in +// flight to finish (handedOffDrain at most) rather than 2s: another +// magpie serves the port meanwhile. +func (s *Server) HandOff() { s.handedOff.Store(true) } + +// drainFor is how long the gateway's end waits for the requests in flight. +func (s *Server) drainFor() time.Duration { + if Handover || s.handedOff.Load() { + return handedOffDrain + } + return shortDrain +} + +// ServeCallbacks answers, on ln, the tool calls of the Claude Code runs +// this process started (claudeMCPPath): their MCP helper calls the +// gateway's port, which the magpie this one handed it to now serves, and +// that one passes on the calls of runs it doesn't have (ForwardCallbacks). +// It returns once ln is closed. +func (s *Server) ServeCallbacks(ln net.Listener) error { + mux := http.NewServeMux() + mux.HandleFunc("POST "+claudeMCPPath+"{token}", s.subscription.mcpCall) + err := (&http.Server{Handler: mux, ReadHeaderTimeout: 30 * time.Second}).Serve(ln) + if errors.Is(err, net.ErrClosed) || errors.Is(err, http.ErrServerClosed) { + return nil + } + return err +} + +// predecessorCallbacks is the unix socket of the magpie this one took the +// gateway over from, which answers its own runs' tool calls there; "" +// when there is none. +var predecessorCallbacks struct { + sync.Mutex + sock string +} + +// ForwardCallbacks has a tool call for a Claude Code run this process +// doesn't know passed on to sock, where the magpie it took the gateway +// over from answers those of its own runs (ServeCallbacks): a turn under +// way there when the gateway changed hands still gets its tool results. +// "" stops it. +func ForwardCallbacks(sock string) { + predecessorCallbacks.Lock() + predecessorCallbacks.sock = sock + predecessorCallbacks.Unlock() +} + +// forwardCallback passes the tool call r, for a run this process doesn't +// have, on to the magpie before this one; false when there is none to. +func forwardCallback(w http.ResponseWriter, r *http.Request) bool { + predecessorCallbacks.Lock() + sock := predecessorCallbacks.sock + predecessorCallbacks.Unlock() + if sock == "" { + return false + } + if _, err := os.Stat(sock); err != nil { + // that magpie has finished and gone + ForwardCallbacks("") + return false + } + p := &httputil.ReverseProxy{ + Rewrite: func(pr *httputil.ProxyRequest) { + pr.SetURL(&url.URL{Scheme: "http", Host: "predecessor"}) + pr.Out.URL.Path, pr.Out.URL.RawPath = r.URL.Path, r.URL.RawPath + }, + Transport: &http.Transport{DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { + var d net.Dialer + return d.DialContext(ctx, "unix", sock) + }}, + FlushInterval: -1, + ErrorHandler: func(w http.ResponseWriter, _ *http.Request, err error) { + if !errors.Is(err, io.EOF) { + log.Printf("claude: a tool call for the magpie before this one didn't reach it: %v", err) + } + http.Error(w, "unknown or expired Claude run", http.StatusNotFound) + }, + } + p.ServeHTTP(w, r) + return true +} + +// forwarding is whether tool calls this process doesn't know go on to the +// magpie it took the gateway over from. +func forwarding() bool { + predecessorCallbacks.Lock() + sock := predecessorCallbacks.sock + predecessorCallbacks.Unlock() + return sock != "" +} + +// Addr is where the gateway listens, "" before it does. +func (s *Server) Addr() string { + s.lnMu.Lock() + defer s.lnMu.Unlock() + if s.ln == nil { + return "" + } + return s.ln.Addr().String() +} diff --git a/internal/gateway/handover_test.go b/internal/gateway/handover_test.go new file mode 100644 index 000000000..cc36db6dc --- /dev/null +++ b/internal/gateway/handover_test.go @@ -0,0 +1,349 @@ +package gateway + +import ( + "bufio" + "context" + "io" + "net" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" +) + +// serveOn starts s's ListenAndServe and waits for it to listen. +func listening(t *testing.T, s *Server) (stop context.CancelFunc, done <-chan error) { + t.Helper() + ctx, cancel := context.WithCancel(context.Background()) + ch := make(chan error, 1) + go func() { ch <- s.ListenAndServe(ctx) }() + deadline := time.Now().Add(10 * time.Second) + for { + s.lnMu.Lock() + ln := s.ln + s.lnMu.Unlock() + if ln != nil { + break + } + if time.Now().After(deadline) { + cancel() + t.Fatal("the gateway never listened") + } + time.Sleep(5 * time.Millisecond) + } + return cancel, ch +} + +// A restart to update hands the gateway's socket to the new magpie: a +// reply streaming through the old one when it does finishes there — the +// old one's end waits for it, rather than 2s — while new requests are +// answered by the new one, and none is refused on the way. +func TestHandedOverGatewayFinishesItsStream(t *testing.T) { + fresh(t) + release := make(chan struct{}) + var upstreamCalls atomic.Int32 + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + upstreamCalls.Add(1) + body, _ := io.ReadAll(r.Body) + if !strings.Contains(string(body), `"stream":true`) { + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"quick"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`) + return + } + w.Header().Set("Content-Type", "text/event-stream") + f := w.(http.Flusher) + io.WriteString(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"first \"}}]}\n\n") + f.Flush() + select { + case <-release: + case <-r.Context().Done(): + return + } + io.WriteString(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"last\"},\"finish_reason\":\"stop\"}]}\n\n") + io.WriteString(w, "data: [DONE]\n\n") + f.Flush() + })) + defer up.Close() + defer close(release) + if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream", Chat: up.URL + "/v1", Models: []string{"m1"}}); err != nil { + t.Fatal(err) + } + // the old one's end would wait this long without the handover: far + // shorter than the stream is held below + oldShort := shortDrain + shortDrain = 10 * time.Millisecond + t.Cleanup(func() { shortDrain = oldShort }) + + t.Setenv("MAGPIE_ADDR", "127.0.0.1:0") + old := New() + stopOld, oldDone := listening(t, old) + defer stopOld() + old.lnMu.Lock() + addr := old.ln.Addr().String() + old.lnMu.Unlock() + t.Setenv("MAGPIE_ADDR", addr) // as settings name it, for the new one + + // a stream under way through the old one + req, _ := http.NewRequest("POST", "http://"+addr+"/v1/chat/completions", strings.NewReader(`{"model":"plan/m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`)) + req.Header.Set("Content-Type", "application/json") + resp, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + stream := bufio.NewReader(resp.Body) + for { + line, err := stream.ReadString('\n') + if err != nil { + t.Fatalf("the stream ended before its first chunk: %v", err) + } + if strings.Contains(line, "first") { + break + } + } + + // the handover: the new one serves on the old one's socket + f, err := old.ListenerFile() + if err != nil { + t.Fatal(err) + } + ln, err := net.FileListener(f) + f.Close() + if err != nil { + t.Fatal(err) + } + acked := make(chan struct{}) + Inherit(ln, func() { close(acked) }) + next := New() + stopNext, nextDone := listening(t, next) + defer func() { + stopNext() + <-nextDone + }() + select { + case <-acked: + case <-time.After(10 * time.Second): + t.Fatal("the new one never said it serves") + } + old.HandOff() + stopOld() + + // new requests reach the new one, through the same port, while the + // old one still streams + for i := range 5 { + r, err := http.Post("http://"+addr+"/v1/chat/completions", "application/json", strings.NewReader(chatReq)) + if err != nil { + t.Fatalf("request %d during the handover: %v", i, err) + } + b, _ := io.ReadAll(r.Body) + r.Body.Close() + if r.StatusCode != 200 || !strings.Contains(string(b), "quick") { + t.Fatalf("request %d during the handover: %d %s", i, r.StatusCode, b) + } + } + if got := len(next.Recent()); got != 5 { + t.Fatalf("the new one answered %d of the 5 requests after the handover", got) + } + + // the old one's end waits for the stream (well past shortDrain) + select { + case err := <-oldDone: + t.Fatalf("the old one ended with a stream in flight: %v", err) + case <-time.After(20 * shortDrain): + } + if b := old.Busy(); b.Requests != 1 { + t.Fatalf("the old one has %d requests in flight, want the stream", b.Requests) + } + release <- struct{}{} + rest, err := io.ReadAll(stream) + if err != nil { + t.Fatalf("the stream was cut: %v", err) + } + if !strings.Contains(string(rest), "last") || !strings.Contains(string(rest), "[DONE]") { + t.Fatalf("the stream's end didn't arrive: %q", rest) + } + select { + case err := <-oldDone: + if err != nil { + t.Fatal(err) + } + case <-time.After(10 * time.Second): + t.Fatal("the old one never ended once its stream had") + } + if n := upstreamCalls.Load(); n != 6 { + t.Fatalf("upstream saw %d calls, want 6", n) + } +} + +// A socket handed over for another address is closed, and the gateway +// listens where settings say. +func TestInheritedForAnotherAddressIsLetGo(t *testing.T) { + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + called := false + Inherit(ln, func() { called = true }) + got, ack := takeInherited("127.0.0.1:1") + if got != nil || ack != nil { + t.Fatal("took a socket on another port") + } + if Inherited() { + t.Fatal("the socket stayed waiting") + } + if _, err := ln.Accept(); err == nil { + t.Fatal("the socket for another address wasn't closed") + } + if called { + t.Fatal("acked a socket not served") + } +} + +func TestSameAddr(t *testing.T) { + for _, tc := range []struct { + ln string + addr string + want bool + }{ + {"127.0.0.1:3425", "127.0.0.1:3425", true}, + {"127.0.0.1:3425", "localhost:3425", true}, + {"127.0.0.1:3425", "127.0.0.1:3426", false}, + {"[::]:3425", "0.0.0.0:3425", true}, + {"0.0.0.0:3425", ":3425", true}, + {"0.0.0.0:3425", "127.0.0.1:3425", false}, + {"127.0.0.1:3425", "0.0.0.0:3425", false}, + } { + a, err := net.ResolveTCPAddr("tcp", tc.ln) + if err != nil { + t.Fatal(err) + } + if got := sameAddr(a, tc.addr); got != tc.want { + t.Errorf("sameAddr(%s, %s) = %v, want %v", tc.ln, tc.addr, got, tc.want) + } + } +} + +// A Claude Code run of the magpie before this one, its turn under way when +// the gateway changed hands, calls its tools on the port this one now +// serves: the call goes on to that magpie's callbacks socket, through the +// network guard as its own run's would, and comes back answered. Once that +// magpie is gone, such a call is unknown here again, and guarded. +func TestToolCallOfTheMagpieBeforeGoesToIt(t *testing.T) { + fresh(t) + t.Setenv("MAGPIE_ADDR", "0.0.0.0:3425") + t.Setenv("MAGPIE_TRUST_PROXY", "") + newCaller(t, "Host") // gateway keys on + sock := shortSock(t) + ln, err := net.Listen("unix", sock) + if err != nil { + t.Fatal(err) + } + var got atomic.Value + before := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + got.Store(r.Method + " " + r.URL.Path + " " + string(b)) + io.WriteString(w, `{"content":"answered there"}`) + })} + go before.Serve(ln) + defer before.Close() + ForwardCallbacks(sock) + t.Cleanup(func() { ForwardCallbacks("") }) + + h := lanGuard(New().Handler()) + token := randomToken() + call := func(remote string) (int, string) { + r := httptest.NewRequest("POST", claudeMCPPath+token, strings.NewReader(`{"tool_call_id":"x","name":"read","arguments":{}}`)) + r.RemoteAddr = remote + w := httptest.NewRecorder() + h.ServeHTTP(w, r) + return w.Code, w.Body.String() + } + // from this computer's own LAN address, as on a non-loopback MAGPIE_ADDR + // (#1546): no key, as for this one's own runs + if c, b := call("192.168.1.9:5000"); c != 200 || !strings.Contains(b, "answered there") { + t.Fatalf("the call wasn't passed on: %d %s", c, b) + } + if g, _ := got.Load().(string); !strings.HasPrefix(g, "POST "+claudeMCPPath+token+" ") || !strings.Contains(g, `"tool_call_id":"x"`) { + t.Fatalf("the magpie before got %q", g) + } + // that magpie gone: the call is unknown here, and from the network it + // needs a key again + before.Close() + os.Remove(sock) + if c, b := call("127.0.0.1:5000"); c != http.StatusNotFound { + t.Fatalf("with the magpie before gone: %d %s", c, b) + } + if forwarding() { + t.Fatal("still passing calls on to a magpie gone") + } + if c, b := call("192.168.1.9:5000"); c != http.StatusUnauthorized { + t.Fatalf("from the network, with the magpie before gone: %d %s", c, b) + } +} + +// The callbacks socket answers its own runs' tool calls and nothing else. +func TestCallbacksSocketServesOnlyToolCalls(t *testing.T) { + fresh(t) + s := New() + sock := shortSock(t) + ln, err := net.Listen("unix", sock) + if err != nil { + t.Fatal(err) + } + go s.ServeCallbacks(ln) + defer ln.Close() + c := &http.Client{Transport: &http.Transport{DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { + var d net.Dialer + return d.DialContext(ctx, "unix", sock) + }}} + for _, p := range [][2]string{ + {"POST", claudeMCPPath + randomToken()}, + {"POST", "/v1/chat/completions"}, + {"GET", "/v1/models"}, + {"GET", claudeMCPPath + randomToken()}, + } { + req, _ := http.NewRequest(p[0], "http://x"+p[1], strings.NewReader(chatReq)) + r, err := c.Do(req) + if err != nil { + t.Fatal(err) + } + r.Body.Close() + if r.StatusCode == 200 { + t.Errorf("%s %s answered 200", p[0], p[1]) + } + } +} + +// A bridge turn counts while it runs, and its end is noted, for a restart +// to wait on: those are what a handover can't carry. +func TestBridgeTurnsCount(t *testing.T) { + s := New() + done := s.bridgeTurn() + if b := s.Busy(); b.Bridge != 1 { + t.Fatalf("Bridge = %d during a turn", b.Bridge) + } + before := time.Now() + done() + b := s.Busy() + if b.Bridge != 0 || b.BridgeLast.Before(before) { + t.Fatalf("after the turn: %+v", b) + } +} + +// shortSock is a unix socket path short enough for a Mac (104 bytes), +// which a test's TempDir may not be. +func shortSock(t *testing.T) string { + t.Helper() + dir, err := os.MkdirTemp("", "mho") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { os.RemoveAll(dir) }) + return filepath.Join(dir, "cb") +} diff --git a/internal/gateway/lan.go b/internal/gateway/lan.go index 1a3550a8c..3dad39418 100644 --- a/internal/gateway/lan.go +++ b/internal/gateway/lan.go @@ -324,6 +324,13 @@ func ownCallback(r *http.Request) bool { if !ok || token == "" { return false } + // while the magpie this one took the gateway over from finishes, a + // token this one doesn't have may be one of that one's runs: it is + // passed on there (forwardCallback), and that magpie judges it as it + // would its own + if forwarding() { + return true + } callbacks.Lock() defer callbacks.Unlock() found := false diff --git a/internal/gateway/listen.go b/internal/gateway/listen.go index d1510db88..b073d2f7c 100644 --- a/internal/gateway/listen.go +++ b/internal/gateway/listen.go @@ -12,8 +12,14 @@ import ( // magpie can't bind the port, which is how it learns one already serves. var Handover bool -// Listen listens on addr, beside another process when Handover is on. +// Listen listens on addr, beside another process when Handover is on; +// or takes the socket the magpie before this one handed over (Inherit) +// when it listens there. func Listen(addr string) (net.Listener, error) { + if ln, ack := takeInherited(addr); ln != nil { + setAck(ack) + return ln, nil + } if !Handover { return net.Listen("tcp", addr) } diff --git a/internal/gui/app.go b/internal/gui/app.go index ea098dd77..b8933164f 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -311,6 +311,14 @@ func Run(version string, showMain bool, link string) error { if devRole() == "backend" { return devBackend(func(w Windows) http.Handler { return Handler(w, startBackend()) }) } + // A restart to update on a Mac or Linux started this one beside the + // magpie before, which hands it the gateway (handover.go). + startExe, _ = update.Executable() + handoverHost.Store(true) + before := 0 + if startExe != "" { + before = takeOver(startExe) + } // After an update off the Mac, the old process starts this one and then // quits; let it go before looking for the gateway. update.AwaitPredecessor() @@ -348,6 +356,12 @@ func Run(version string, showMain bool, link string) error { if handler == nil { handler = Handler(h, startBackend()) } + // Linux: the magpie before holds the app's D-Bus name until it quits, + // once it has finished its requests; this one serves the gateway + // meanwhile, and puts its tray icon up after + if before != 0 && runtime.GOOS == "linux" { + awaitExit(before, handedOffWait) + } h.app = application.New(application.Options{ // Windows and Linux start a new process for a magpie:// link (or a // second launch); it hands its arguments to the running one and quits. @@ -368,8 +382,14 @@ func Run(version string, showMain bool, link string) error { // this process serves: Codex left pointed at it would retry till // magpie is open again (Leon920717 on X). The next start writes it // again (Sync). + // A magpie that handed the gateway to the new version (handover.go) + // leaves both to it: the failover goes on through the new one, and + // so do the threads. OnShutdown: func() { proc.EndProbes() + if handedOver.Load() { + return + } if served.Load() != nil { if err := agent.EndCodexFailover(); err != nil { log.Println("codex failover:", err) @@ -431,7 +451,7 @@ func Run(version string, showMain bool, link string) error { // the window comes back if it was open; the tray alone if not now := func() bool { if restartToUpdate(false, h.MainShown(), "") { - h.app.Quit() + leave(h.app.Quit) return true } return false @@ -470,7 +490,7 @@ func Run(version string, showMain bool, link string) error { // Settings' AutoRestart: the window comes back only if it was up (#2096) updates.autoRestart = func() bool { if restartInto(false, h.MainShown(), "", true) { - h.app.Quit() + leave(h.app.Quit) return true } return false @@ -513,6 +533,22 @@ func Run(version string, showMain bool, link string) error { } h.tray.SetMenu(menu) h.tray.AttachWindow(h.panel).WindowOffset(6) + // a Mac that handed the gateway to the new version (handover.go) + // leaves the menu bar to it while it finishes its requests: one bird + if runtime.GOOS == "darwin" { + onHandedOver = func() { + application.InvokeSync(func() { + h.tray.Hide() + if h.panel != nil { + h.panel.Hide() + } + if h.main != nil { + h.main.Hide() + } + setDock(false, false) + }) + } + } h.watchTrayUsage() // Omarchy's bar shows magpie once: as its widget (Settings → Bar // icon) or as the tray's item, not both diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 95aab8c0d..3fb43370b 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -4144,7 +4144,8 @@ async function renderUpdateBadge() { // click, not as "now" if (u.state === "ready" && u.waiting) { label.textContent = t("Waiting to update"); - b.title = t("magpie restarts to update to {v} once the agents' requests through the gateway have finished: {busy}. Click to restart now.", { v: u.latest, busy: updateBusyText(u.busy) || "…" }); + const a = { v: u.latest, busy: updateBusyText(u.busy) || "…" }; + b.title = u.handover ? t("magpie restarts to update to {v} once the replies through a Claude subscription have finished: {busy}. Other requests carry on across the restart. Click to restart now.", a) : t("magpie restarts to update to {v} once the agents' requests through the gateway have finished: {busy}. Click to restart now.", a); b.onclick = () => { if (Date.now() - (+b.dataset.armed || 0) < 1500) return; b.classList.add("busy"); @@ -19652,9 +19653,7 @@ function renderSettings() { ? t("Off: a newer version is only shown, with its size; it downloads when you click Download update. For metered networks and proxies billed by traffic") : t("Downloads a newer version as soon as it is found. Turn off on a metered network or a proxy billed by traffic"), "", segs([["off", t("Off")], ["on", t("On")]], s.noAutoDownload ? "off" : "on", (v) => savePrefs({ ...keep, noAutoDownload: v === "off" }))).classList.add("update-download-row"); - // whether a downloaded update is restarted into by itself, once no agent - // request is in flight (#2096); off, it waits for Restart or for quitting - row(t("Restart automatically"), s.noAutoUpdate ? t("While automatic updates are on") : t("Once an update is downloaded, restarts into it when no agent request is in flight"), "", + row(t("Restart automatically"), s.noAutoUpdate ? t("While automatic updates are on") : t("Once an update is downloaded, restarts into it without cutting off an agent's request"), "", segs([["off", t("Off")], ["on", t("On")]], s.autoRestart ? "on" : "off", (v) => savePrefs({ ...keep, autoRestart: v === "on" }))).classList.add("update-restart-row", ...(s.noAutoUpdate ? ["off"] : [])); renderUpdateMirror(row(t("Download source"), "", ""), s); const open = el("button", "text", t("Open")); @@ -23454,7 +23453,7 @@ async function renderUpdate(r, u) { }; const busyNow = updateBusyText(u.busy); if (u.waiting) { - sub.textContent += " · " + t("restarts once the gateway is idle") + (busyNow ? " · " + busyNow : ""); + sub.textContent += " · " + (u.handover ? t("restarts once the Claude subscription's replies have finished") : t("restarts once the gateway is idle")) + (busyNow ? " · " + busyNow : ""); btn(t("Restart now"), () => install("now")); btn(t("Cancel"), () => install("cancel")); // the wait ends in a restart: a read that finds no one is that diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 7fbcbbcc8..38aea801f 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -2955,7 +2955,7 @@ const I18N = { "Downloads a newer version as soon as it is found. Turn off on a metered network or a proxy billed by traffic": "发现新版本后立即下载。使用按流量计费的网络或代理时可以关闭", "Download update": "下载更新", "Click to download and restart": "点击下载并重启更新", - "Once an update is downloaded, restarts into it when no agent request is in flight": "新版本下载好后,在没有 Agent 请求进行时自动重启到新版本", + "Once an update is downloaded, restarts into it without cutting off an agent's request": "新版本下载好后自动重启到新版本,不会中断 Agent 的请求", "How often magpie looks for a newer version": "magpie 多久检查一次新版本", "Automatic updates are off": "自动更新已关闭", "Restart to update to {v}": "重启以更新到 {v}", @@ -2963,6 +2963,8 @@ const I18N = { "{n} in flight": "{n} 个请求进行中", "{n} awaiting tool results": "{n} 个工具调用待返回", "restarts once the gateway is idle": "网关空闲后自动重启", + "restarts once the Claude subscription's replies have finished": "Claude 订阅的回复结束后自动重启", + "magpie restarts to update to {v} once the replies through a Claude subscription have finished: {busy}. Other requests carry on across the restart. Click to restart now.": "Claude 订阅的回复结束后,magpie 将重启以更新到 {v}:{busy}。其他请求不受重启影响。点击立即重启。", "Restart now": "立即重启", "Waiting to update": "等待更新", "magpie restarts to update to {v} once the agents' requests through the gateway have finished: {busy}. Click to restart now.": "网关中 Agent 的请求结束后,magpie 将重启以更新到 {v}:{busy}。点击立即重启。", @@ -6823,7 +6825,7 @@ const I18N = { "Downloads a newer version as soon as it is found. Turn off on a metered network or a proxy billed by traffic": "發現新版本後立即下載。使用按流量計費的網路或代理時可以關閉", "Download update": "下載更新", "Click to download and restart": "點擊下載並重新啟動更新", - "Once an update is downloaded, restarts into it when no agent request is in flight": "新版本下載完成後,在沒有 Agent 請求進行時自動重新啟動到新版本", + "Once an update is downloaded, restarts into it without cutting off an agent's request": "新版本下載完成後自動重新啟動到新版本,不會中斷 Agent 的請求", "How often magpie looks for a newer version": "magpie 多久檢查一次新版本", "Automatic updates are off": "自動更新已關閉", "Restart to update to {v}": "重啟以更新到 {v}", @@ -6831,6 +6833,8 @@ const I18N = { "{n} in flight": "{n} 個請求進行中", "{n} awaiting tool results": "{n} 個工具呼叫待返回", "restarts once the gateway is idle": "閘道空閒後自動重啟", + "restarts once the Claude subscription's replies have finished": "Claude 訂閱的回覆結束後自動重啟", + "magpie restarts to update to {v} once the replies through a Claude subscription have finished: {busy}. Other requests carry on across the restart. Click to restart now.": "Claude 訂閱的回覆結束後,magpie 將重啟以更新到 {v}:{busy}。其他請求不受重啟影響。點選立即重啟。", "Restart now": "立即重啟", "Waiting to update": "等待更新", "magpie restarts to update to {v} once the agents' requests through the gateway have finished: {busy}. Click to restart now.": "閘道中 Agent 的請求結束後,magpie 將重啟以更新到 {v}:{busy}。點選立即重啟。", @@ -10703,7 +10707,7 @@ const I18N = { "Downloads a newer version as soon as it is found. Turn off on a metered network or a proxy billed by traffic": "新しいバージョンが見つかるとすぐにダウンロードします。従量課金のネットワークやプロキシではオフにできます", "Download update": "アップデートをダウンロード", "Click to download and restart": "クリックでダウンロードして再起動", - "Once an update is downloaded, restarts into it when no agent request is in flight": "アップデートのダウンロード後、エージェントのリクエストが処理中でないときに再起動して適用します", + "Once an update is downloaded, restarts into it without cutting off an agent's request": "アップデートのダウンロード後、エージェントのリクエストを中断せずに再起動して適用します", "How often magpie looks for a newer version": "magpie が新しいバージョンを確認する頻度", "Automatic updates are off": "自動アップデートはオフです", "Restart to update to {v}": "再起動して {v} にアップデート", @@ -10711,6 +10715,8 @@ const I18N = { "{n} in flight": "{n} 件のリクエストを処理中", "{n} awaiting tool results": "{n} 件がツール結果待ち", "restarts once the gateway is idle": "ゲートウェイがアイドルになったら再起動します", + "restarts once the Claude subscription's replies have finished": "Claude サブスクリプションの応答が終わると再起動します", + "magpie restarts to update to {v} once the replies through a Claude subscription have finished: {busy}. Other requests carry on across the restart. Click to restart now.": "Claude サブスクリプションの応答が終わると、magpie は再起動して {v} にアップデートします:{busy}。ほかのリクエストは再起動をまたいで続きます。クリックすると今すぐ再起動します。", "Restart now": "今すぐ再起動", "Waiting to update": "アップデート待ち", "magpie restarts to update to {v} once the agents' requests through the gateway have finished: {busy}. Click to restart now.": "ゲートウェイを通るエージェントのリクエストが終わると、magpie は再起動して {v} にアップデートします:{busy}。クリックすると今すぐ再起動します。", @@ -14593,7 +14599,7 @@ const I18N = { "Downloads a newer version as soon as it is found. Turn off on a metered network or a proxy billed by traffic": "Lädt eine neue Version herunter, sobald sie gefunden wird. Bei getakteten Netzen oder nach Datenvolumen abgerechneten Proxys ausschalten", "Download update": "Update herunterladen", "Click to download and restart": "Klicken zum Herunterladen und Neustarten", - "Once an update is downloaded, restarts into it when no agent request is in flight": "Startet nach dem Download eines Updates neu, sobald keine Agent-Anfrage läuft", + "Once an update is downloaded, restarts into it without cutting off an agent's request": "Startet nach dem Download eines Updates neu, ohne eine Agent-Anfrage abzubrechen", "How often magpie looks for a newer version": "Wie oft magpie nach einer neueren Version sucht", "Automatic updates are off": "Automatische Updates sind ausgeschaltet", "Restart to update to {v}": "Neu starten, um auf {v} zu aktualisieren", @@ -14601,6 +14607,8 @@ const I18N = { "{n} in flight": "{n} laufend", "{n} awaiting tool results": "{n} warten auf Tool-Ergebnisse", "restarts once the gateway is idle": "startet neu, sobald der Zugangspunkt frei ist", + "restarts once the Claude subscription's replies have finished": "startet neu, sobald die Antworten des Claude-Abos fertig sind", + "magpie restarts to update to {v} once the replies through a Claude subscription have finished: {busy}. Other requests carry on across the restart. Click to restart now.": "magpie startet für das Update auf {v} neu, sobald die Antworten über das Claude-Abo fertig sind: {busy}. Andere Anfragen laufen über den Neustart hinweg weiter. Klicken, um jetzt neu zu starten.", "Restart now": "Jetzt neu starten", "Waiting to update": "Warte auf Update", "magpie restarts to update to {v} once the agents' requests through the gateway have finished: {busy}. Click to restart now.": "magpie startet für das Update auf {v} neu, sobald die Anfragen der Agenten über den Zugangspunkt abgeschlossen sind: {busy}. Klicken, um jetzt neu zu starten.", diff --git a/internal/gui/backend.go b/internal/gui/backend.go index abff47da5..96f3436dd 100644 --- a/internal/gui/backend.go +++ b/internal/gui/backend.go @@ -118,7 +118,9 @@ func serveGateway() *gateway.Server { func serveGatewayLocked() *gateway.Server { // handing over, the one there is this one's predecessor, which lets go // once this one listens beside it - if !gateway.Handover { + // so is the one that handed this one its socket, still finishing its + // requests on it (gui/handover.go) + if !gateway.Handover && !gateway.Inherited() { if o := gateway.ServedBy(); o.Running { if update.Newer(gateway.Version, o.Version) { log.Printf("gateway: magpie %s serves %s, older than this one (%s); agents' requests go through it until it quits", o.Version, gateway.URL(), gateway.Version) diff --git a/internal/gui/handover.go b/internal/gui/handover.go new file mode 100644 index 000000000..287806f5f --- /dev/null +++ b/internal/gui/handover.go @@ -0,0 +1,275 @@ +package gui + +import ( + "context" + "fmt" + "log" + "net" + "os" + "path/filepath" + "runtime" + "sync" + "sync/atomic" + "time" + + "github.com/yetone/magpie/internal/gateway" + "github.com/yetone/magpie/internal/handoff" + "github.com/yetone/magpie/internal/settings" + "github.com/yetone/magpie/internal/update" +) + +// A restart to update on a Mac or Linux hands the gateway to the new +// version rather than ending it (xuanwo on X): the new magpie is started +// while this one runs, takes the socket the gateway listens on +// (internal/handoff) and serves the next requests on it, while this one +// stops accepting, finishes the requests it has, and quits. Nothing an +// agent sends is refused or cut short on the way, so the restart waits +// only for what can't go on in another process: the turns of Claude Code +// run through the subscription bridge (restartBusy). Windows can't hand a +// socket over this way and `magpie web` re-executes in place; both still +// wait for the gateway to be idle (#577). +// +// The two never show at once. On a Mac this one lets go of its instance +// lock for the new one and hides its menu bar icon and windows once the +// new one serves. On Linux the D-Bus name Wails holds can't be let go, so +// the new one serves the gateway at once but waits for this one to quit +// before it puts up its tray icon; this one keeps its own until then. + +// handoverHost is set by the app (Run): a restart there may hand the +// gateway over. `magpie web` and tests leave it unset. +var handoverHost atomic.Bool + +// startExe is the program this magpie started as, symlinks resolved; an +// update puts the new version at the same path (on Linux the running +// one's own path reads "(deleted)" once it has). +var startExe string + +// handsOver is whether a restart now would hand the gateway over. +var handsOver = func() bool { + if !handoverHost.Load() || gateway.Handover || os.Getenv("MAGPIE_NO_HANDOVER") != "" || startExe == "" { + return false + } + if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { + return false + } + return served.Load() != nil +} + +// restartBusy is what a restart now would cut short: the gateway's Busy, +// or, when the restart hands it over, the bridge's turns alone (counted +// as Requests, their end as Last), with the turns parked on tools. +func restartBusy() gateway.Busy { return blocking(gatewayBusy(), handsOver()) } + +// blocking is the part of b a restart cuts short; handover, it hands the +// gateway over. +func blocking(b gateway.Busy, handover bool) gateway.Busy { + if !handover { + return b + } + return gateway.Busy{Requests: b.Bridge, Tools: b.Tools, Last: b.BridgeLast} +} + +// pending is the handover under way, nil while none is. +var pending atomic.Pointer[handover] + +// handedOver is set once the new magpie serves the gateway: this one's +// end leaves what goes with the gateway (Codex's failover) to it. +var handedOver atomic.Bool + +// onHandedOver hides this magpie once the new one serves (the Mac's menu +// bar icon and windows); set by Run. +var onHandedOver func() + +// handover is a restart handing the gateway to the new version. +type handover struct { + offer *handoff.Offer + gw *gateway.Server + fallback func() error // relaunch once this one has exited, as before + quit chan func() // the caller's quit, called once done (leave) +} + +// Waits a handover gives the new magpie: to start and say hello, and to +// serve on the socket once it has it. +var ( + helloWait = 60 * time.Second + ackWait = 90 * time.Second +) + +// startNew starts the new version beside this one: the Mac's bundle, or +// the binary. Vars for tests. +var ( + openBeside = update.OpenBeside + startBeside = update.StartBeside +) + +// beginHandover starts the new version and hands the gateway over to it +// in the background, the caller quitting (leave) once that is done. False +// when this restart can't hand over: the caller restarts as before. +func beginHandover(bundle, exe string, window bool, view string) bool { + if !handsOver() { + return false + } + ho := &handover{gw: served.Load(), quit: make(chan func(), 1)} + if bundle != "" { + ho.fallback = func() error { return update.Relaunch(bundle, window) } + } else { + ho.fallback = func() error { return relaunchBinary(exe, "", window, view) } + } + pending.Store(ho) + // the restart was let go while this one's requests were in flight, + // since a handover carries them: one that can't be made goes the old + // way once they have finished (fail), never by cutting them short + offer, err := handoff.Open(settings.Dir(), startExe) + if err != nil { + go ho.failed(fmt.Errorf("the gateway can't be handed over: %w", err)) + return true + } + ho.offer = offer + if bundle != "" { + err = openBeside(bundle, window) + } else { + err = startBeside(exe, window, view) + } + if err != nil { + go ho.failed(fmt.Errorf("the new version didn't start beside this one: %w", err)) + return true + } + go ho.run() + return true +} + +// failed gives the handover up before it began, and quits. +func (ho *handover) failed(err error) { + ho.fail(err) + (<-ho.quit)() +} + +// leave quits once a handover under way is done, else at once. +func leave(quit func()) { + if ho := pending.Load(); ho != nil { + ho.quit <- quit + return + } + quit() +} + +// run hands the gateway over and quits: once the new magpie serves on its +// socket and this one has finished its requests, or, should it not take +// it in time, as a restart did before (the new one opened once this one +// has exited). +func (ho *handover) run() { + quit := func() { (<-ho.quit)() } + ctx, cancel := context.WithTimeout(context.Background(), helloWait) + c, err := ho.offer.Accept(ctx) + cancel() + if err != nil { + ho.fail(err) + quit() + return + } + defer c.Close() + // the routing the new one goes on with: which account each + // conversation stuck to + gateway.FlushSticks() + // the tool calls of this one's Claude Code runs, which their helper + // sends to the port the new one will serve, come back here + cbSock := filepath.Join(ho.offer.Dir(), "cb") + cbLn, err := net.Listen("unix", cbSock) + if err == nil { + go ho.gw.ServeCallbacks(cbLn) + } else { + log.Println("update: no socket for the bridge's tool calls:", err) + cbSock = "" + } + f, err := ho.gw.ListenerFile() + if err == nil { + releaseInstance() // for the new one to take as it starts + err = c.Give(f, handoff.Meta{Addr: ho.gw.Addr(), Callbacks: cbSock, PID: os.Getpid()}) + f.Close() + if err == nil { + err = c.AwaitAck(ackWait) + } + } + if err != nil { + if cbLn != nil { + cbLn.Close() + } + retakeInstance() + ho.fail(err) + quit() + return + } + ho.offer.Withdraw() + handedOver.Store(true) + log.Printf("update: magpie %s (pid %d) serves the gateway now; this one finishes its requests and quits", c.Hello.Version, c.Hello.PID) + if onHandedOver != nil { + onHandedOver() + } + ho.gw.HandOff() + endBackend() + serving.Wait() + if cbLn != nil { + cbLn.Close() + } + ho.offer.Close() + quit() +} + +// fallbackWait is how long a handover given up waits for the requests in +// flight before restarting as before: the drain a handed-over gateway +// gives them (gateway.handedOffDrain). +var fallbackWait = 15 * time.Minute + +// fail gives the handover up: once the requests in flight have finished +// (the restart didn't wait for them), the new version opens once this one +// has exited, as a restart did before. +func (ho *handover) fail(err error) { + log.Println("update: the gateway wasn't handed over, restarting as before once its requests have finished:", err) + if ho.offer != nil { + ho.offer.Close() + } + for deadline := time.Now().Add(fallbackWait); gatewayBusy().Any() && time.Now().Before(deadline); { + time.Sleep(200 * time.Millisecond) + } + if ferr := ho.fallback(); ferr != nil { + log.Println("update:", ferr) + } +} + +// takeOver, in the new magpie, takes the gateway from the one that +// started it, when that one hands it over: its socket is this one's to +// serve, and the tool calls of its Claude Code runs go back to it. The +// pid of the magpie before, 0 when none hands over. +func takeOver(exe string) int { + t, err := handoff.Take(settings.Dir(), exe, handoff.Hello{PID: os.Getpid(), Version: Version}, 30*time.Second) + if err != nil { + log.Println("update: the gateway wasn't taken over:", err) + return 0 + } + if t == nil { + return 0 + } + var once sync.Once + gateway.Inherit(t.Listener, func() { + once.Do(func() { + if err := t.Ack(); err != nil { + log.Println("update: the magpie before didn't hear that this one serves:", err) + } + }) + }) + gateway.ForwardCallbacks(t.Meta.Callbacks) + log.Printf("update: took the gateway on %s over from pid %d", t.Meta.Addr, t.Meta.PID) + return t.Meta.PID +} + +// handedOffWait is how long the new magpie on Linux waits for the one +// before to quit: its requests' drain (gateway.handedOffDrain) and a +// minute. +var handedOffWait = 16 * time.Minute + +// awaitExit waits, most at most, for the process pid to end. +func awaitExit(pid int, most time.Duration) { + for deadline := time.Now().Add(most); handoffAlive(pid) && time.Now().Before(deadline); { + time.Sleep(200 * time.Millisecond) + } +} diff --git a/internal/gui/handover_test.go b/internal/gui/handover_test.go new file mode 100644 index 000000000..ad6f5e4d7 --- /dev/null +++ b/internal/gui/handover_test.go @@ -0,0 +1,63 @@ +package gui + +import ( + "testing" + "time" + + "github.com/yetone/magpie/internal/gateway" +) + +// A restart that hands the gateway over waits only for the bridge's +// Claude Code turns, which can't go on in another process; one that +// doesn't waits for every request, as before (#577). The page is told +// which it is. +func TestRestartWaitsForWhatAHandoverCantCarry(t *testing.T) { + at := time.Now().Add(-time.Second) + b := gateway.Busy{Requests: 3, Tools: 1, Last: time.Now(), Bridge: 1, BridgeLast: at} + if got := blocking(b, false); got != b { + t.Fatalf("without a handover: %+v", got) + } + got := blocking(b, true) + if got.Requests != 1 || got.Tools != 1 || !got.Last.Equal(at) { + t.Fatalf("handing over, waits for %+v; want the bridge's turn and the parked tool", got) + } + if blocking(gateway.Busy{Requests: 4, Last: time.Now()}, true).Any() { + t.Fatal("handing over, waits for requests that the old one finishes after it") + } + + oldBusy, oldHands := gatewayBusy, handsOver + t.Cleanup(func() { gatewayBusy, handsOver = oldBusy, oldHands }) + gatewayBusy = func() gateway.Busy { return gateway.Busy{Requests: 2} } + u := &updater{state: "ready"} + for _, hands := range []bool{false, true} { + handsOver = func() bool { return hands } + j := u.jsonIn("") + if j.Handover != hands { + t.Errorf("handover %v: the page reads %v", hands, j.Handover) + } + if (j.Busy != nil) == hands { + t.Errorf("handover %v: the page shows busy %+v", hands, j.Busy) + } + } +} + +// A restart hands over only from the app, on a Mac or Linux, with the +// gateway its own; otherwise it restarts as before. +func TestHandsOverOnlyWhereItCan(t *testing.T) { + if handoverHost.Load() || served.Load() != nil { + t.Skip("another test's gateway") + } + if handsOver() { + t.Fatal("hands over outside the app") + } + oldExe := startExe + t.Cleanup(func() { startExe = oldExe; handoverHost.Store(false) }) + startExe = "/apps/magpie" + handoverHost.Store(true) + if handsOver() { + t.Fatal("hands over a gateway this magpie doesn't serve") + } + if beginHandover("", "/apps/magpie", false, "") || pending.Load() != nil { + t.Fatal("began a handover it can't make") + } +} diff --git a/internal/gui/handover_unix_test.go b/internal/gui/handover_unix_test.go new file mode 100644 index 000000000..566b42602 --- /dev/null +++ b/internal/gui/handover_unix_test.go @@ -0,0 +1,326 @@ +//go:build darwin || linux + +package gui + +import ( + "bufio" + "context" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/yetone/magpie/internal/gateway" + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/update" +) + +// successorEnv has this test binary, started again, play the new magpie +// (TestHandoverSuccessor). +const successorEnv = "MAGPIE_TEST_SUCCESSOR" + +// TestHandoverSuccessor is the new version a restart starts, in a process +// of its own: it takes the gateway over from the magpie that started it +// and serves it until it is ended. +func TestHandoverSuccessor(t *testing.T) { + if os.Getenv(successorEnv) == "" { + t.Skip("the new magpie of TestRestartHandsTheGatewayOver") + } + exe, err := update.Executable() + if err != nil { + t.Fatal(err) + } + if takeOver(exe) == 0 { + t.Fatal("no gateway was taken over") + } + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + if err := gateway.New().ListenAndServe(ctx); err != nil { + t.Fatal(err) + } +} + +// handoverSandbox is a magpie serving the gateway on a port of its own, +// as the app does, to restart; what it set is put back after. +func handoverSandbox(t *testing.T, up string) *gateway.Server { + t.Helper() + if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream", Chat: up + "/v1", Models: []string{"m1"}}); err != nil { + t.Fatal(err) + } + t.Setenv("MAGPIE_ADDR", "127.0.0.1:0") + t.Setenv("MAGPIE_NO_HANDOVER", "") + oldExe, oldHello, oldAck := startExe, helloWait, ackWait + oldStart, oldRelaunch := startBeside, relaunchBinary + exe, err := update.Executable() + if err != nil { + t.Fatal(err) + } + startExe = exe + handoverHost.Store(true) + t.Cleanup(func() { + endBackend() + serving.Wait() + backendCtx, endBackend = context.WithCancel(context.Background()) + served.Store(nil) + pending.Store(nil) + handedOver.Store(false) + handoverHost.Store(false) + startExe, helloWait, ackWait = oldExe, oldHello, oldAck + startBeside, relaunchBinary = oldStart, oldRelaunch + gateway.ForwardCallbacks("") + }) + gw := serveGateway() + if gw == nil { + t.Fatal("the gateway wasn't served") + } + eventually(t, "the gateway never listened", func() bool { return gw.Addr() != "" }) + t.Setenv("MAGPIE_ADDR", gw.Addr()) // as the new one finds it in settings + return gw +} + +// slowUpstream answers a streamed request with a first chunk, then the +// rest once release is closed; any other at once. +func slowUpstream(t *testing.T, release <-chan struct{}) *httptest.Server { + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + if !strings.Contains(string(body), `"stream":true`) { + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"quick"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`) + return + } + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"first \"}}]}\n\n") + w.(http.Flusher).Flush() + select { + case <-release: + case <-r.Context().Done(): + return + } + io.WriteString(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"last\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n") + })) + t.Cleanup(up.Close) + return up +} + +// servedByPid is the pid of the magpie answering at addr, 0 when none. +func servedByPid(addr string) int { + c := &http.Client{Timeout: 2 * time.Second, Transport: &http.Transport{DisableKeepAlives: true}} + r, err := c.Get("http://" + addr + "/") + if err != nil { + return 0 + } + defer r.Body.Close() + var info struct { + Pid int `json:"pid"` + } + json.NewDecoder(r.Body).Decode(&info) + return info.Pid +} + +// xuanwo on X: a restart to update on a Mac or Linux hands the gateway to +// the new version, a process of its own, rather than waiting for it to be +// idle. A reply streaming through the old one when it restarts finishes +// whole; requests sent meanwhile are answered, by the new one once it +// serves; and the old one quits only once its stream has ended. +func TestRestartHandsTheGatewayOver(t *testing.T) { + release := make(chan struct{}) + up := slowUpstream(t, release) + defer close(release) + gw := handoverSandbox(t, up.URL) + addr := gw.Addr() + + var child *exec.Cmd + startBeside = func(exe string, window bool, view string) error { + child = exec.Command(exe, "-test.run=^TestHandoverSuccessor$", "-test.v") + child.Env = append(os.Environ(), successorEnv+"=1") + child.Stdout, child.Stderr = os.Stderr, os.Stderr + return child.Start() + } + t.Cleanup(func() { + if child != nil && child.Process != nil { + child.Process.Kill() + child.Wait() + } + }) + relaunchBinary = func(string, string, bool, string) error { + t.Error("restarted as before, rather than handing over") + return nil + } + + // a reply streaming through the gateway + resp, err := http.Post("http://"+addr+"/v1/chat/completions", "application/json", + strings.NewReader(`{"model":"plan/m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`)) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + stream := bufio.NewReader(resp.Body) + if line, err := stream.ReadString('\n'); err != nil || !strings.Contains(line, "first") { + t.Fatalf("the stream's first chunk: %q %v", line, err) + } + // it doesn't hold the restart back + if b := restartBusy(); b.Any() { + t.Fatalf("a restart that hands over waits for %+v", b) + } + + // requests all along the restart are answered + var asked, answered atomic.Int32 + stopAsking := make(chan struct{}) + asking := make(chan struct{}) + var stopOnce sync.Once + stop := func() { stopOnce.Do(func() { close(stopAsking) }); <-asking } + t.Cleanup(stop) + go func() { + defer close(asking) + for { + select { + case <-stopAsking: + return + default: + } + asked.Add(1) + r, err := http.Post("http://"+addr+"/v1/chat/completions", "application/json", strings.NewReader(`{"model":"plan/m1","messages":[{"role":"user","content":"hi"}]}`)) + if err != nil { + t.Errorf("a request during the restart: %v", err) + return + } + b, _ := io.ReadAll(r.Body) + r.Body.Close() + if r.StatusCode != 200 || !strings.Contains(string(b), "quick") { + t.Errorf("a request during the restart: %d %s", r.StatusCode, b) + return + } + answered.Add(1) + time.Sleep(20 * time.Millisecond) + } + }() + + if !beginHandover("", startExe, false, "") { + t.Fatal("the restart didn't hand over") + } + quit := make(chan struct{}) + leave(func() { close(quit) }) + + eventually2(t, 30*time.Second, "the new magpie never served the gateway", func() bool { + return child != nil && child.Process != nil && servedByPid(addr) == child.Process.Pid + }) + if !handedOver.Load() { + t.Fatal("the new one serves, and this one doesn't know it handed over") + } + // the old one keeps its stream past the 2s an ordinary end gives + // requests (gateway.shortDrain), and so doesn't quit yet + select { + case <-quit: + t.Fatal("quit with a reply streaming") + case <-time.After(3 * time.Second): + } + for range 5 { + if pid := servedByPid(addr); pid != child.Process.Pid { + t.Fatalf("a request after the handover reached pid %d", pid) + } + } + stop() + if answered.Load() == 0 || answered.Load() != asked.Load() { + t.Fatalf("%d of %d requests during the restart answered", answered.Load(), asked.Load()) + } + + release <- struct{}{} + rest, err := io.ReadAll(stream) + if err != nil || !strings.Contains(string(rest), "last") || !strings.Contains(string(rest), "[DONE]") { + t.Fatalf("the stream was cut: %q %v", rest, err) + } + select { + case <-quit: + case <-time.After(10 * time.Second): + t.Fatal("the old one didn't quit once its stream ended") + } +} + +// A handover that can't be made leaves the gateway with this one, which +// then restarts as before (the new one opened once it has exited), but +// only once the requests it has in flight have finished: the restart was +// let go without waiting for them, since a handover carries them (the +// sandbox run's bind failed and its stream was cut). +func TestHandoverNotMadeRestartsAsBeforeOnceIdle(t *testing.T) { + for _, c := range []struct { + name string + start func(string, bool, string) error + }{ + {"never says hello", func(string, bool, string) error { return nil }}, + {"doesn't start", func(string, bool, string) error { return errors.New("no such file") }}, + } { + t.Run(c.name, func(t *testing.T) { + release := make(chan struct{}) + up := slowUpstream(t, release) + defer close(release) + gw := handoverSandbox(t, up.URL) + helloWait = 200 * time.Millisecond + startBeside = c.start + var relaunched atomic.Bool + relaunchBinary = func(exe, aside string, window bool, view string) error { + relaunched.Store(true) + return nil + } + resp, err := http.Post("http://"+gw.Addr()+"/v1/chat/completions", "application/json", + strings.NewReader(`{"model":"plan/m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`)) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + stream := bufio.NewReader(resp.Body) + if line, err := stream.ReadString('\n'); err != nil || !strings.Contains(line, "first") { + t.Fatalf("the stream's first chunk: %q %v", line, err) + } + if !beginHandover("", startExe, false, "") { + t.Fatal("the restart didn't try to hand over") + } + quit := make(chan struct{}) + leave(func() { close(quit) }) + select { + case <-quit: + t.Fatal("quit with a reply streaming") + case <-time.After(time.Second): + } + if relaunched.Load() { + t.Fatal("restarted with a reply streaming") + } + if servedByPid(gw.Addr()) != os.Getpid() { + t.Fatal("the gateway isn't this one's while the restart waits") + } + release <- struct{}{} + rest, err := io.ReadAll(stream) + if err != nil || !strings.Contains(string(rest), "[DONE]") { + t.Fatalf("the stream was cut: %q %v", rest, err) + } + select { + case <-quit: + case <-time.After(10 * time.Second): + t.Fatal("never quit") + } + if !relaunched.Load() { + t.Fatal("not restarted as before") + } + if handedOver.Load() { + t.Fatal("says it handed over") + } + }) + } +} + +func eventually2(t *testing.T, most time.Duration, what string, ok func() bool) { + t.Helper() + for deadline := time.Now().Add(most); time.Now().Before(deadline); time.Sleep(20 * time.Millisecond) { + if ok() { + return + } + } + t.Fatal(what) +} diff --git a/internal/gui/instance_unix.go b/internal/gui/instance_unix.go index 3aea007db..f5496f06c 100644 --- a/internal/gui/instance_unix.go +++ b/internal/gui/instance_unix.go @@ -10,12 +10,16 @@ import ( "path/filepath" "strings" "syscall" + "time" ) // heldInstance is the open lock file of the magpie that holds its // instance; it stays open, so locked, until this process ends. var heldInstance *os.File +// heldPath is the lock heldInstance holds. +var heldPath string + // instanceLock is where the magpie running exe on the config in dir holds // its lock. A copy elsewhere (the owner's magpie-dev) has its own, but // every installed magpie — a magpie.app wherever it is, or Homebrew's, @@ -62,6 +66,35 @@ func holdInstance(path string) (bool, error) { f.Close() return false, err } - heldInstance = f + heldInstance, heldPath = f, path return true, nil } + +// releaseInstance lets go of the instance lock, for the new version a +// restart hands the gateway to (handover.go) to take as it starts. +func releaseInstance() { + if heldInstance != nil { + heldInstance.Close() + heldInstance = nil + } +} + +// retakeInstance takes the lock again after a handover that didn't +// happen; a new version that took it meanwhile keeps it. +func retakeInstance() { + if heldInstance != nil || heldPath == "" { + return + } + for i := 0; i < 10; i++ { + if held, err := holdInstance(heldPath); held || err != nil { + return + } + time.Sleep(100 * time.Millisecond) + } +} + +// handoffAlive is whether the process pid runs. +func handoffAlive(pid int) bool { + err := syscall.Kill(pid, 0) + return err == nil || errors.Is(err, syscall.EPERM) +} diff --git a/internal/gui/instance_windows.go b/internal/gui/instance_windows.go new file mode 100644 index 000000000..581482c79 --- /dev/null +++ b/internal/gui/instance_windows.go @@ -0,0 +1,8 @@ +package gui + +// Windows hands no gateway over (handover.go): a restart there waits for +// it to be idle, and the instance is Wails' to keep. + +func releaseInstance() {} +func retakeInstance() {} +func handoffAlive(int) bool { return false } diff --git a/internal/gui/tests/update-restart.test.cjs b/internal/gui/tests/update-restart.test.cjs index df9c354d1..b24c59c30 100644 --- a/internal/gui/tests/update-restart.test.cjs +++ b/internal/gui/tests/update-restart.test.cjs @@ -1,6 +1,6 @@ // Run with Node's test runner and Playwright on the module path; see README.md. // Settings → Restart automatically (#2096): off by default; on, a downloaded -// update is restarted into once no agent request is in flight. The switch +// update is restarted into without cutting off an agent request. The switch // is saved as autoRestart and kept across a reload, with Automatic updates // and Check every left as they were. With automatic updates off it is kept, // dimmed, and says it works while they are on. No click moves the page, and @@ -16,11 +16,11 @@ const { watchSettingsReads, settingsDrawn, openPart } = require("./reader.cjs"); const assets = path.resolve(__dirname, "../assets"); const words = { - en: { auto: "Automatic updates", restart: "Restart automatically", whileOn: "While automatic updates are on", what: "Once an update is downloaded, restarts into it when no agent request is in flight" }, - zh: { auto: "自动更新", restart: "自动重启更新", whileOn: "自动更新开启时生效", what: "新版本下载好后,在没有 Agent 请求进行时自动重启到新版本" }, - "zh-TW": { auto: "自動更新", restart: "自動重新啟動更新", whileOn: "自動更新開啟時生效", what: "新版本下載完成後,在沒有 Agent 請求進行時自動重新啟動到新版本" }, - ja: { auto: "自動アップデート", restart: "自動で再起動", whileOn: "自動アップデートがオンのとき", what: "アップデートのダウンロード後、エージェントのリクエストが処理中でないときに再起動して適用します" }, - de: { auto: "Automatische Updates", restart: "Automatisch neu starten", whileOn: "Solange automatische Updates aktiv sind", what: "Startet nach dem Download eines Updates neu, sobald keine Agent-Anfrage läuft" }, + en: { auto: "Automatic updates", restart: "Restart automatically", whileOn: "While automatic updates are on", what: "Once an update is downloaded, restarts into it without cutting off an agent's request" }, + zh: { auto: "自动更新", restart: "自动重启更新", whileOn: "自动更新开启时生效", what: "新版本下载好后自动重启到新版本,不会中断 Agent 的请求" }, + "zh-TW": { auto: "自動更新", restart: "自動重新啟動更新", whileOn: "自動更新開啟時生效", what: "新版本下載完成後自動重新啟動到新版本,不會中斷 Agent 的請求" }, + ja: { auto: "自動アップデート", restart: "自動で再起動", whileOn: "自動アップデートがオンのとき", what: "アップデートのダウンロード後、エージェントのリクエストを中断せずに再起動して適用します" }, + de: { auto: "Automatische Updates", restart: "Automatisch neu starten", whileOn: "Solange automatische Updates aktiv sind", what: "Startet nach dem Download eines Updates neu, ohne eine Agent-Anfrage abzubrechen" }, }; function settingsPayload(lang, ctl) { diff --git a/internal/gui/tests/update-wait.test.cjs b/internal/gui/tests/update-wait.test.cjs index be33a1169..fad5f52bd 100644 --- a/internal/gui/tests/update-wait.test.cjs +++ b/internal/gui/tests/update-wait.test.cjs @@ -26,6 +26,8 @@ const words = { gaveUp: "0.1.401 is downloaded · the gateway stayed busy for an hour, so magpie didn't restart; it updates when you restart or quit it", pill: "Update", pillWaiting: "Waiting to update", pillTitle: "magpie restarts to update to 0.1.401 once the agents' requests through the gateway have finished: 2 in flight · 1 awaiting tool results. Click to restart now.", + handoverWaiting: "0.1.401 is downloaded · restarts once the Claude subscription's replies have finished · 1 in flight", + handoverPillTitle: "magpie restarts to update to 0.1.401 once the replies through a Claude subscription have finished: 1 in flight. Other requests carry on across the restart. Click to restart now.", }, zh: { version: "版本", restart: "重启以更新", now: "立即重启", cancel: "取消", restarting: "正在重启…", @@ -35,6 +37,8 @@ const words = { gaveUp: "0.1.401 已下载 · 网关忙碌了一小时,magpie 未重启;重启或退出 magpie 时完成更新", pill: "更新", pillWaiting: "等待更新", pillTitle: "网关中 Agent 的请求结束后,magpie 将重启以更新到 0.1.401:2 个请求进行中 · 1 个工具调用待返回。点击立即重启。", + handoverWaiting: "0.1.401 已下载 · Claude 订阅的回复结束后自动重启 · 1 个请求进行中", + handoverPillTitle: "Claude 订阅的回复结束后,magpie 将重启以更新到 0.1.401:1 个请求进行中。其他请求不受重启影响。点击立即重启。", }, }; @@ -191,6 +195,25 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.deepEqual(errors, []); }); + // xuanwo on X: a restart that hands the gateway to the new version + // waits only for the Claude subscription's replies, and says so + await t.test(lang + ": handing over, the wait names the Claude subscription's replies", async () => { + const ctl = fresh({ handover: true, busy: { requests: 1 } }); + const { page, errors } = await open(lang, ctl, "?view=settings&tab=about", 900); + const row = page.locator("#about .row.pref", { has: page.locator(".name", { hasText: w.version }) }).first(); + await row.locator("button", { hasText: w.restart }).click(); + await confirmAsk(page); + await row.locator("button", { hasText: w.now }).waitFor(); + assert.equal((await row.locator(".sub").textContent()).trim(), w.handoverWaiting); + const panel = await open(lang, fresh({ handover: true, busy: { requests: 1 } }), "?mode=panel", 420); + const pill = panel.page.locator("#update"); + await pill.click(); + await panel.page.waitForFunction((t) => document.querySelector("#update span").textContent.trim() === t, w.pillWaiting); + assert.equal(await pill.getAttribute("title"), w.handoverPillTitle); + assert.deepEqual(errors, []); + assert.deepEqual(panel.errors, []); + }); + await t.test(lang + ": with nothing in flight the click restarts at once", async () => { const ctl = fresh({ busy: undefined }); const { page, errors } = await open(lang, ctl, "?view=settings&tab=about", 900); diff --git a/internal/gui/update.go b/internal/gui/update.go index f6a70d0d8..914330624 100644 --- a/internal/gui/update.go +++ b/internal/gui/update.go @@ -104,6 +104,10 @@ type updateJSON struct { Busy *gateway.Busy `json:"busy,omitempty"` Waiting bool `json:"waiting,omitempty"` GaveUp bool `json:"gaveUp,omitempty"` + // Handover, the restart hands the gateway to the new version + // (handover.go): Busy is then the Claude subscription's turns alone, + // every other request finishing as the new version takes the next + Handover bool `json:"handover,omitempty"` } var updates = &updater{} @@ -448,7 +452,7 @@ func (u *updater) json() updateJSON { return u.jsonIn("") } // Notes held in another language are asked for again in lang, at most // once a minute; the page's next look has them. func (u *updater) jsonIn(lang string) updateJSON { - busy := gatewayBusy() + busy, handover := restartBusy(), handsOver() u.mu.Lock() defer u.mu.Unlock() if lang != "" { @@ -482,6 +486,7 @@ func (u *updater) jsonIn(lang string) updateJSON { } if j.State == "ready" || u.waiting { j.Waiting, j.GaveUp = u.waiting, u.gaveUp && !u.waiting + j.Handover = handover if busy.Any() { j.Busy = &busy } @@ -584,7 +589,7 @@ func updateRoutes(mux *http.ServeMux, w Windows) { if in.When != "now" && !updates.idle() { updates.waitIdle(func() bool { if restartToUpdate(isWeb(w), in.View != "" || mainShown(w), in.View) { - go w.Quit() + go leave(w.Quit) return true } return false @@ -599,7 +604,7 @@ func updateRoutes(mux *http.ServeMux, w Windows) { case j.State == "ready": if restartToUpdate(isWeb(w), in.View != "" || mainShown(w), in.View) { rw.WriteHeader(http.StatusNoContent) - go w.Quit() + go leave(w.Quit) return } case j.State == "error" && j.Retry, j.State == "found": @@ -629,6 +634,9 @@ func restartToUpdate(web, window bool, view string) bool { // (#2096), and the Mac's new one too opens only its menu bar icon when no // window was up, rather than putting up one the user had closed. func restartInto(web, window bool, view string, bySelf bool) bool { + if pending.Load() != nil { + return false // a handover under way restarts already + } bundle, exe := updates.bundle, updates.exe updates.mu.Lock() version := "" @@ -644,6 +652,12 @@ func restartInto(web, window bool, view string, bySelf bool) bool { webReexec.Store(true) return true } + // on a Mac or Linux the new version takes the gateway over while this + // one finishes its requests (handover.go) + if bundle != "" && beginHandover(bundle, "", window || !bySelf, "") || + bundle == "" && beginHandover("", exe, window, view) { + return true + } var err error if bundle != "" { err = update.Relaunch(bundle, window || !bySelf) diff --git a/internal/gui/update_wait.go b/internal/gui/update_wait.go index bb842d779..2cf630aea 100644 --- a/internal/gui/update_wait.go +++ b/internal/gui/update_wait.go @@ -9,8 +9,10 @@ import ( // A restart to update ends the gateway this process serves, and with it // whatever the agents have in flight through it (#577: an agent's reply -// streaming, a Claude Code turn waiting on its caller's tool results). So a -// restart asked for while the gateway is busy waits until it is idle: +// streaming, a Claude Code turn waiting on its caller's tool results). On +// a Mac or Linux it hands the gateway over instead (handover.go), and only +// the Claude subscription's turns would be cut short (restartBusy). So a +// restart asked for while those are busy waits until they are idle: // nothing in flight, no turn waiting on tools, and nothing ended in the // last idleSettle — the gap of a tool chain, an agent running a tool // between one request and the next. It waits waitMost at most; then the @@ -41,7 +43,7 @@ func idleAt(b gateway.Busy, now time.Time) bool { } // idle is whether the gateway is idle now. -func (u *updater) idle() bool { return idleAt(gatewayBusy(), time.Now()) } +func (u *updater) idle() bool { return idleAt(restartBusy(), time.Now()) } // waitIdle has restart run once the gateway is idle; a wait under way // takes the newer restart (the window as it is now). @@ -84,7 +86,7 @@ func (u *updater) waitingFor() (bool, gateway.Busy) { if !w { return false, gateway.Busy{} } - return true, gatewayBusy() + return true, restartBusy() } func (u *updater) told() { @@ -114,7 +116,7 @@ func (u *updater) waitFor(gen int) { return } u.mu.Unlock() - b := gatewayBusy() + b := restartBusy() if b.Requests != last.Requests || b.Tools != last.Tools { last = b u.told() diff --git a/internal/handoff/handoff.go b/internal/handoff/handoff.go new file mode 100644 index 000000000..14d1d2b48 --- /dev/null +++ b/internal/handoff/handoff.go @@ -0,0 +1,98 @@ +// Package handoff hands the gateway's listening socket from the magpie +// restarting to update to the version it started, so the port is never +// closed between them: the new one serves on the same socket, and the old +// one stops accepting and finishes what it has. +// +// The magpie handing over opens an Offer: a unix socket in a folder only +// its user can enter, named in a pointer file in magpie's config folder. +// The new magpie Takes it: it connects, says hello, and is sent the +// listening socket (SCM_RIGHTS) with a Meta; once its gateway serves on +// it, it Acks, and the one before lets go. Only Unix has this (darwin and +// linux); elsewhere Open says ErrUnsupported (Take finds nothing), and a restart waits +// for the gateway to be idle as before. +package handoff + +import ( + "encoding/json" + "errors" + "os" + "path/filepath" + "time" +) + +// ErrUnsupported is Open's error where a socket can't be handed +// to another process (Windows). +var ErrUnsupported = errors.New("handing the gateway over isn't supported here") + +// self is this process's pid; a test stands in for it, to take its own +// Offer. +var self = os.Getpid + +// pointerName is the pointer file in magpie's config folder. +const pointerName = "handover.json" + +// pointerFresh is how long an Offer's pointer is taken: one older is a +// restart that gave up (Offer.Close removes it; a crash may not). +const pointerFresh = 2 * time.Minute + +// pointer is what the pointer file says. +type pointer struct { + PID int `json:"pid"` + Sock string `json:"sock"` + Exe string `json:"exe"` // the program the new magpie runs + At time.Time `json:"at"` +} + +// Hello is what the new magpie says it is. +type Hello struct { + PID int `json:"pid"` + Version string `json:"version"` +} + +// Meta comes with the socket handed over. +type Meta struct { + // Addr is where the socket listens. + Addr string `json:"addr"` + // Callbacks is the unix socket where the magpie handing over answers + // its own Claude Code runs' tool calls until it ends. + Callbacks string `json:"callbacks,omitempty"` + // PID is the magpie handing over. + PID int `json:"pid"` +} + +// ackLine is what Ack writes. +const ackLine = "serving\n" + +// readPointer is the Offer named in dir for exe, if one is there, fresh, +// from a process still running and not this one. +func readPointer(dir, exe string) (pointer, bool) { + b, err := os.ReadFile(filepath.Join(dir, pointerName)) + if err != nil { + return pointer{}, false + } + var p pointer + if json.Unmarshal(b, &p) != nil || p.Sock == "" || p.PID <= 0 || p.PID == self() { + return pointer{}, false + } + if time.Since(p.At) > pointerFresh || p.At.After(time.Now().Add(time.Minute)) { + return pointer{}, false + } + if !sameExe(p.Exe, exe) || !alive(p.PID) { + return pointer{}, false + } + return p, true +} + +// sameExe is whether two paths name one program, symlinks followed. +func sameExe(a, b string) bool { + if a == "" || b == "" { + return false + } + if r, err := filepath.EvalSymlinks(a); err == nil { + a = r + } + if r, err := filepath.EvalSymlinks(b); err == nil { + b = r + } + return filepath.Clean(a) == filepath.Clean(b) +} diff --git a/internal/handoff/handoff_other.go b/internal/handoff/handoff_other.go new file mode 100644 index 000000000..03e9b03a7 --- /dev/null +++ b/internal/handoff/handoff_other.go @@ -0,0 +1,41 @@ +//go:build !darwin && !linux + +package handoff + +import ( + "context" + "net" + "os" + "time" +) + +// Offer is the Unix one's; here none is ever made. +type Offer struct{} + +func Open(dir, exe string) (*Offer, error) { return nil, ErrUnsupported } + +func (o *Offer) Dir() string { return "" } +func (o *Offer) Close() {} +func (o *Offer) Withdraw() {} +func (o *Offer) Accept(ctx context.Context) (*Conn, error) { return nil, ErrUnsupported } + +// Conn is the Unix one's. +type Conn struct{ Hello Hello } + +func (c *Conn) Give(f *os.File, m Meta) error { return ErrUnsupported } +func (c *Conn) AwaitAck(timeout time.Duration) error { return ErrUnsupported } +func (c *Conn) Close() {} + +// Taken is the Unix one's. +type Taken struct { + Listener net.Listener + Meta Meta +} + +// Take finds nothing to take here. +func Take(dir, exe string, hello Hello, timeout time.Duration) (*Taken, error) { return nil, nil } + +func (t *Taken) Ack() error { return ErrUnsupported } +func (t *Taken) Close() {} + +func alive(pid int) bool { return false } diff --git a/internal/handoff/handoff_test.go b/internal/handoff/handoff_test.go new file mode 100644 index 000000000..0629ddfab --- /dev/null +++ b/internal/handoff/handoff_test.go @@ -0,0 +1,235 @@ +//go:build darwin || linux + +package handoff + +import ( + "bufio" + "context" + "encoding/json" + "net" + "os" + "path/filepath" + "testing" + "time" +) + +// asAnother has this process take its own Offer, as the new magpie would. +func asAnother(t *testing.T) { + t.Helper() + old := self + self = func() int { return -1 } + t.Cleanup(func() { self = old }) +} + +// offerListener offers ln from a goroutine, as the magpie handing over +// does, and reports AwaitAck's answer. +func offerListener(t *testing.T, dir, exe string, ln *net.TCPListener) (*Offer, <-chan error) { + t.Helper() + o, err := Open(dir, exe) + if err != nil { + t.Fatal(err) + } + t.Cleanup(o.Close) + acked := make(chan error, 1) + go func() { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + c, err := o.Accept(ctx) + if err != nil { + acked <- err + return + } + defer c.Close() + f, err := ln.File() + if err != nil { + acked <- err + return + } + err = c.Give(f, Meta{Addr: ln.Addr().String(), Callbacks: "/cb", PID: os.Getpid()}) + f.Close() + if err != nil { + acked <- err + return + } + acked <- c.AwaitAck(10 * time.Second) + }() + return o, acked +} + +// The socket handed over is the one listened on: a client connecting +// after the old process let go of its copy reaches the new one, and its +// ack reaches the old. +func TestHandOverListener(t *testing.T) { + asAnother(t) + dir := t.TempDir() + ln, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)}) + if err != nil { + t.Fatal(err) + } + addr := ln.Addr().String() + _, acked := offerListener(t, dir, "/apps/magpie", ln) + + got, err := Take(dir, "/apps/magpie", Hello{PID: 4242, Version: "v2"}, 5*time.Second) + if err != nil || got == nil { + t.Fatalf("Take = %v, %v", got, err) + } + defer got.Listener.Close() + if got.Meta.Addr != addr || got.Meta.Callbacks != "/cb" || got.Meta.PID != os.Getpid() { + t.Fatalf("meta = %+v", got.Meta) + } + if err := got.Ack(); err != nil { + t.Fatal(err) + } + if err := <-acked; err != nil { + t.Fatalf("the old one didn't hear the ack: %v", err) + } + ln.Close() // the old one stops accepting + + served := make(chan string, 1) + go func() { + c, err := got.Listener.Accept() + if err != nil { + served <- err.Error() + return + } + defer c.Close() + line, _ := bufio.NewReader(c).ReadString('\n') + served <- line + }() + c, err := net.DialTimeout("tcp", addr, 2*time.Second) + if err != nil { + t.Fatalf("the port was closed between them: %v", err) + } + c.Write([]byte("hi\n")) + c.Close() + select { + case line := <-served: + if line != "hi\n" { + t.Fatalf("the new one read %q", line) + } + case <-time.After(5 * time.Second): + t.Fatal("the new one never accepted") + } +} + +// A new magpie that drops the connection without an ack leaves the +// socket with the old one, which hears so at once. +func TestNoAckKeepsTheSocket(t *testing.T) { + asAnother(t) + dir := t.TempDir() + ln, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)}) + if err != nil { + t.Fatal(err) + } + defer ln.Close() + _, acked := offerListener(t, dir, "/apps/magpie", ln) + got, err := Take(dir, "/apps/magpie", Hello{PID: 4242}, 5*time.Second) + if err != nil || got == nil { + t.Fatalf("Take = %v, %v", got, err) + } + got.Listener.Close() + got.Close() + select { + case err := <-acked: + if err == nil { + t.Fatal("an ack was heard that was never sent") + } + case <-time.After(5 * time.Second): + t.Fatal("the old one waited out its timeout for a magpie gone") + } +} + +// Only a fresh Offer, of a process running, for the same program and not +// this process's own, is taken. +func TestTakeOnlyAFreshOfferForThisProgram(t *testing.T) { + dir := t.TempDir() + write := func(p pointer) { + b, _ := json.Marshal(p) + if err := os.WriteFile(filepath.Join(dir, pointerName), b, 0o600); err != nil { + t.Fatal(err) + } + } + take := func() *Taken { + got, err := Take(dir, "/apps/magpie", Hello{PID: 1}, time.Second) + if err != nil { + return nil + } + return got + } + if take() != nil { + t.Fatal("took an Offer with none made") + } + sock := filepath.Join(t.TempDir(), "s") + ln, err := net.Listen("unix", sock) + if err != nil { + t.Fatal(err) + } + defer ln.Close() + for name, p := range map[string]pointer{ + "its own": {PID: os.Getpid(), Sock: sock, Exe: "/apps/magpie", At: time.Now()}, + "stale": {PID: os.Getppid(), Sock: sock, Exe: "/apps/magpie", At: time.Now().Add(-time.Hour)}, + "another program": {PID: os.Getppid(), Sock: sock, Exe: "/dev/magpie-dev", At: time.Now()}, + "a process gone": {PID: 1 << 30, Sock: sock, Exe: "/apps/magpie", At: time.Now()}, + } { + write(p) + if _, ok := readPointer(dir, "/apps/magpie"); ok { + t.Errorf("%s: the Offer was taken", name) + } + } + write(pointer{PID: os.Getppid(), Sock: sock, Exe: "/apps/magpie", At: time.Now()}) + if _, ok := readPointer(dir, "/apps/magpie"); !ok { + t.Fatal("a fresh Offer of a running process wasn't taken") + } +} + +// Close takes the pointer away, and only while it is the Offer's own. +func TestCloseRemovesOnlyItsPointer(t *testing.T) { + dir := t.TempDir() + o, err := Open(dir, "/apps/magpie") + if err != nil { + t.Fatal(err) + } + if fi, err := os.Stat(o.Dir()); err != nil || fi.Mode().Perm() != 0o700 { + t.Fatalf("the sockets' folder: %v %v", fi, err) + } + o.Close() + if _, err := os.Stat(filepath.Join(dir, pointerName)); !os.IsNotExist(err) { + t.Fatalf("the pointer stayed: %v", err) + } + o2, err := Open(dir, "/apps/magpie") + if err != nil { + t.Fatal(err) + } + // another magpie's offer since + b, _ := json.Marshal(pointer{PID: os.Getppid(), Sock: "/x", Exe: "/apps/magpie", At: time.Now()}) + os.WriteFile(filepath.Join(dir, pointerName), b, 0o600) + o2.Close() + if _, err := os.Stat(filepath.Join(dir, pointerName)); err != nil { + t.Fatal("Close removed another's pointer") + } +} + +// A temporary folder whose path leaves a unix socket no room (a TMPDIR set +// long: the sandbox run's bind failed with "invalid argument", and the +// restart cut its stream as before) still gets an Offer, its sockets in +// /tmp. +func TestOpenWithALongTemporaryFolder(t *testing.T) { + long := filepath.Join(t.TempDir(), "a-temporary-folder-whose-path-is-longer-than-a-unix-socket-has-room-for-on-a-mac") + if err := os.MkdirAll(long, 0o700); err != nil { + t.Fatal(err) + } + t.Setenv("TMPDIR", long) + o, err := Open(t.TempDir(), "/apps/magpie") + if err != nil { + t.Fatalf("no offer: %v", err) + } + defer o.Close() + if fi, err := os.Stat(o.Dir()); err != nil || fi.Mode().Perm() != 0o700 { + t.Fatalf("the sockets' folder %s: %v %v", o.Dir(), fi, err) + } + cb, err := net.Listen("unix", filepath.Join(o.Dir(), "cb")) + if err != nil { + t.Fatalf("no room for the callbacks' socket: %v", err) + } + cb.Close() +} diff --git a/internal/handoff/handoff_unix.go b/internal/handoff/handoff_unix.go new file mode 100644 index 000000000..8b928338c --- /dev/null +++ b/internal/handoff/handoff_unix.go @@ -0,0 +1,281 @@ +//go:build darwin || linux + +package handoff + +import ( + "bufio" + "context" + "encoding/json" + "errors" + "fmt" + "net" + "os" + "path/filepath" + "strings" + "syscall" + "time" + + "golang.org/x/sys/unix" +) + +// Offer is a socket waiting to be handed to the new magpie. +type Offer struct { + dir string // the folder of the unix sockets, only this user's + pointer string + ln *net.UnixListener +} + +// sockRoom is the longest path a unix socket here may have: sun_path has +// 104 bytes on a Mac and 108 on Linux, with room kept for the callbacks' +// "cb" beside "s". +const sockRoom = 100 + +// sockDir makes the folder, only this user's, the sockets go in: in the +// temporary folder, or in /tmp where that one's path leaves a socket no +// room (a TMPDIR set long; the bind fails with "invalid argument", and +// the restart went back to cutting requests short). +func sockDir() (string, error) { + var tmp string + var err error + for _, root := range []string{"", "/tmp"} { + if tmp, err = os.MkdirTemp(root, "magpie-ho-"); err != nil { + continue + } + if len(filepath.Join(tmp, "cb")) <= sockRoom { + break + } + os.RemoveAll(tmp) + tmp, err = "", fmt.Errorf("no folder with room for a socket's path (%s)", os.TempDir()) + } + if err != nil { + return "", err + } + if err := os.Chmod(tmp, 0o700); err != nil { + os.RemoveAll(tmp) + return "", err + } + return tmp, nil +} + +// Open offers the gateway's socket to the next magpie to run exe on the +// config folder dir. +func Open(dir, exe string) (*Offer, error) { + tmp, err := sockDir() + if err != nil { + return nil, err + } + sock := filepath.Join(tmp, "s") + ln, err := net.ListenUnix("unix", &net.UnixAddr{Name: sock, Net: "unix"}) + if err != nil { + os.RemoveAll(tmp) + return nil, err + } + o := &Offer{dir: tmp, pointer: filepath.Join(dir, pointerName), ln: ln} + b, _ := json.Marshal(pointer{PID: os.Getpid(), Sock: sock, Exe: exe, At: time.Now()}) + part := o.pointer + ".tmp" + if err := os.WriteFile(part, b, 0o600); err != nil { + o.Close() + return nil, err + } + if err := os.Rename(part, o.pointer); err != nil { + os.Remove(part) + o.Close() + return nil, err + } + return o, nil +} + +// Dir is a folder of the Offer's own, only this user's, for another unix +// socket beside it (Meta.Callbacks); it is removed with the Offer. +func (o *Offer) Dir() string { return o.dir } + +// Close takes the Offer back: its pointer and sockets are removed. +func (o *Offer) Close() { + if o == nil { + return + } + // the pointer only while it is still this one's + if b, err := os.ReadFile(o.pointer); err == nil { + var p pointer + if json.Unmarshal(b, &p) == nil && p.PID == os.Getpid() { + os.Remove(o.pointer) + } + } + o.ln.Close() + os.RemoveAll(o.dir) +} + +// Withdraw removes the pointer, so no later magpie takes the Offer, and +// stops taking connections; the folder stays for sockets still in use. +func (o *Offer) Withdraw() { + if b, err := os.ReadFile(o.pointer); err == nil { + var p pointer + if json.Unmarshal(b, &p) == nil && p.PID == os.Getpid() { + os.Remove(o.pointer) + } + } + o.ln.Close() +} + +// Conn is the new magpie, connected to an Offer. +type Conn struct { + c *net.UnixConn + r *bufio.Reader + Hello Hello +} + +// Accept waits for the new magpie to connect and say hello, until ctx +// ends. +func (o *Offer) Accept(ctx context.Context) (*Conn, error) { + stop := context.AfterFunc(ctx, func() { o.ln.SetDeadline(time.Now()) }) + defer stop() + for { + c, err := o.ln.AcceptUnix() + if err != nil { + if ctx.Err() != nil { + return nil, ctx.Err() + } + return nil, err + } + if !ownUser(c) { + c.Close() + continue + } + c.SetReadDeadline(time.Now().Add(10 * time.Second)) + r := bufio.NewReader(c) + line, err := r.ReadBytes('\n') + var h Hello + if err != nil || json.Unmarshal(line, &h) != nil || h.PID <= 0 { + c.Close() + continue + } + c.SetReadDeadline(time.Time{}) + return &Conn{c: c, r: r, Hello: h}, nil + } +} + +// Give sends the listening socket f, with m. +func (c *Conn) Give(f *os.File, m Meta) error { + b, _ := json.Marshal(m) + b = append(b, '\n') + n, oobn, err := c.c.WriteMsgUnix(b, unix.UnixRights(int(f.Fd())), nil) + if err != nil { + return err + } + if n != len(b) || oobn == 0 { + return fmt.Errorf("handing the socket over: short write") + } + return nil +} + +// AwaitAck waits, timeout at most, for the new magpie to say its gateway +// serves on the socket given. An error is the new one gone or not ready +// in time: the socket is then still this one's to serve. +func (c *Conn) AwaitAck(timeout time.Duration) error { + c.c.SetReadDeadline(time.Now().Add(timeout)) + line, err := c.r.ReadString('\n') + if err != nil { + return fmt.Errorf("the new magpie didn't say it serves: %w", err) + } + if line != ackLine { + return fmt.Errorf("the new magpie said %q", strings.TrimSpace(line)) + } + return nil +} + +// Close ends the connection; the new magpie reads it as the one before +// gone. +func (c *Conn) Close() { c.c.Close() } + +// Taken is the socket handed over, with its Meta. +type Taken struct { + Listener net.Listener + Meta Meta + c *net.UnixConn +} + +// Take takes the socket the magpie before this one offers, when it runs +// exe on dir: nil, nil when none does. It waits timeout at most. +func Take(dir, exe string, hello Hello, timeout time.Duration) (*Taken, error) { + p, ok := readPointer(dir, exe) + if !ok { + return nil, nil + } + deadline := time.Now().Add(timeout) + c, err := net.DialTimeout("unix", p.Sock, timeout) + if err != nil { + return nil, err + } + uc := c.(*net.UnixConn) + fail := func(err error) (*Taken, error) { + uc.Close() + return nil, err + } + uc.SetDeadline(deadline) + b, _ := json.Marshal(hello) + if _, err := uc.Write(append(b, '\n')); err != nil { + return fail(err) + } + buf := make([]byte, 4096) + oob := make([]byte, unix.CmsgSpace(4)) + n, oobn, _, _, err := uc.ReadMsgUnix(buf, oob) + if err != nil { + return fail(err) + } + msgs, err := unix.ParseSocketControlMessage(oob[:oobn]) + if err != nil || len(msgs) == 0 { + return fail(errors.New("no socket came with the handover")) + } + fds, err := unix.ParseUnixRights(&msgs[0]) + if err != nil || len(fds) == 0 { + return fail(errors.New("no socket came with the handover")) + } + for _, fd := range fds[1:] { + unix.Close(fd) + } + unix.CloseOnExec(fds[0]) + var m Meta + if json.Unmarshal([]byte(strings.TrimSpace(string(buf[:n]))), &m) != nil { + unix.Close(fds[0]) + return fail(errors.New("the handover's details didn't parse")) + } + f := os.NewFile(uintptr(fds[0]), "gateway") + ln, err := net.FileListener(f) // a copy of its own; f is let go + f.Close() + if err != nil { + return fail(err) + } + uc.SetDeadline(time.Time{}) + return &Taken{Listener: ln, Meta: m, c: uc}, nil +} + +// Ack tells the magpie before that this one serves on the socket. +func (t *Taken) Ack() error { + t.c.SetWriteDeadline(time.Now().Add(5 * time.Second)) + _, err := t.c.Write([]byte(ackLine)) + t.c.Close() + return err +} + +// Close drops the connection without an ack: the magpie before keeps +// serving. +func (t *Taken) Close() { t.c.Close() } + +// ownUser is whether the process at the other end of c runs as this +// user. The folder the socket is in already keeps others out; this is a +// second look. +func ownUser(c *net.UnixConn) bool { + raw, err := c.SyscallConn() + if err != nil { + return false + } + uid := -1 + raw.Control(func(fd uintptr) { uid = peerUID(int(fd)) }) + return uid == os.Getuid() +} + +// alive is whether a process pid is running. +func alive(pid int) bool { + err := syscall.Kill(pid, 0) + return err == nil || errors.Is(err, syscall.EPERM) +} diff --git a/internal/handoff/main_test.go b/internal/handoff/main_test.go new file mode 100644 index 000000000..0fd5d5f37 --- /dev/null +++ b/internal/handoff/main_test.go @@ -0,0 +1,10 @@ +package handoff + +import ( + "testing" + + "github.com/yetone/magpie/internal/testenv" +) + +// The tests run in a home of their own, never the user's real files. +func TestMain(m *testing.M) { testenv.Main(m) } diff --git a/internal/handoff/peer_darwin.go b/internal/handoff/peer_darwin.go new file mode 100644 index 000000000..d011a135c --- /dev/null +++ b/internal/handoff/peer_darwin.go @@ -0,0 +1,13 @@ +package handoff + +import "golang.org/x/sys/unix" + +// peerUID is the user of the process at the other end of the unix socket +// fd, -1 when it can't be told. +func peerUID(fd int) int { + cred, err := unix.GetsockoptXucred(fd, unix.SOL_LOCAL, unix.LOCAL_PEERCRED) + if err != nil { + return -1 + } + return int(cred.Uid) +} diff --git a/internal/handoff/peer_linux.go b/internal/handoff/peer_linux.go new file mode 100644 index 000000000..4fd00269b --- /dev/null +++ b/internal/handoff/peer_linux.go @@ -0,0 +1,13 @@ +package handoff + +import "golang.org/x/sys/unix" + +// peerUID is the user of the process at the other end of the unix socket +// fd, -1 when it can't be told. +func peerUID(fd int) int { + cred, err := unix.GetsockoptUcred(fd, unix.SOL_SOCKET, unix.SO_PEERCRED) + if err != nil { + return -1 + } + return int(cred.Uid) +} diff --git a/internal/update/relaunch_open_test.go b/internal/update/relaunch_open_test.go index 80a67227b..5d34851d8 100644 --- a/internal/update/relaunch_open_test.go +++ b/internal/update/relaunch_open_test.go @@ -1,6 +1,9 @@ package update -import "testing" +import ( + "strings" + "testing" +) // A Mac restart by itself with no window up opens the app as a login launch // does, its menu bar icon only (#2096); one with the window opens it. @@ -12,3 +15,38 @@ func TestRelaunchOpen(t *testing.T) { t.Errorf("no window: %s, want %s", got, want) } } + +// A restart that hands the gateway over opens the new version while this +// one runs: a new instance (-n), which LaunchServices would otherwise send +// to the one running. +func TestOpenBeside(t *testing.T) { + if got, want := openBeside("/Applications/magpie.app", true), `open -n "/Applications/magpie.app"`; got != want { + t.Errorf("window: %s, want %s", got, want) + } + if got, want := openBeside("/Applications/magpie.app", false), `open -n "/Applications/magpie.app" --args tray`; got != want { + t.Errorf("no window: %s, want %s", got, want) + } +} + +// The new version a handover starts isn't told to wait for this one to +// exit (it would wait while this one waits for it), and gets the args +// RelaunchBinary gives. +func TestStartBesideWaitsForNothing(t *testing.T) { + t.Setenv("MAGPIE_REPLACES", "") + var gotArgs, gotEnv []string + stubLaunch(t, func(exe string, args, env []string) (<-chan struct{}, func(), error) { + gotArgs, gotEnv = args, env + return nil, nil, nil + }) + if err := StartBeside("/opt/magpie", true, "routing"); err != nil { + t.Fatal(err) + } + if strings.Join(gotArgs, " ") != "gui routing" { + t.Errorf("args %q", gotArgs) + } + for _, e := range gotEnv { + if strings.HasPrefix(e, "MAGPIE_REPLACES=") && e != "MAGPIE_REPLACES=" { + t.Errorf("told to wait: %s", e) + } + } +} diff --git a/internal/update/update.go b/internal/update/update.go index 95d0def8d..e72007075 100644 --- a/internal/update/update.go +++ b/internal/update/update.go @@ -936,3 +936,31 @@ func (c *counter) Read(p []byte) (int, error) { // Alive reports whether the process pid is still running. func Alive(pid int) bool { return alive(pid) } + +// OpenBeside opens bundle now, a new instance beside this one (open -n), +// tried again while open fails: a restart that hands the gateway over +// (internal/handoff) starts the new version before this one quits. With +// window, its window; else its menu bar icon only, as Relaunch. +func OpenBeside(bundle string, window bool) error { + cmd := proc.Command("/bin/sh", "-c", retried(openBeside(bundle, window), openWaits)) + cmd.Stdin, cmd.Stdout, cmd.Stderr = nil, nil, nil + detach(cmd) + return cmd.Start() +} + +// openBeside is the open command OpenBeside runs for bundle. +func openBeside(bundle string, window bool) string { + if window { + return fmt.Sprintf("open -n %q", bundle) + } + return fmt.Sprintf("open -n %q --args tray", bundle) +} + +// StartBeside starts exe now, beside this process, as RelaunchBinary's +// args say: the new version of a restart that hands the gateway over. It +// isn't told to wait for this one (MAGPIE_REPLACES): it takes the gateway +// from it while it runs. +func StartBeside(exe string, window bool, view string) error { + _, _, err := launch(exe, RelaunchArgs(window, view), os.Environ()) + return err +}