Files
magpie/internal/cli/gui_on.go
T
yetoneandrecih bfbc22e190 claude: an idle session is saved whenever the gateway ends or hands over (#7017)
recih: after magpie web restarted (an update's re-exec, or Quit and
start again) the next Claude turn replayed the whole conversation
instead of resuming its session. Web.Wait closed the page server and
never drained the gateway, so abortAll never ran and claude-shelf.json
was never written.

- Web.Wait drains the gateway (Close: page server, then stopServing)
  before the plugin hosts end and before update.Reexec.
- Desktop quit: OnShutdown calls stopServing.
- Signals: web, serve, the TUI and the app register their gateway with
  the signal handler (stopGatewayOnSignal). SIGINT, SIGTERM and SIGHUP
  drain it, sessions saved, before magpie dies of the signal. The return
  path waits for a drain the signal already started. The TUI and the
  app take SIGINT/SIGTERM themselves, but SIGHUP (the terminal closing)
  stays with magpie. runTUI, runGUI and runPanel now register
  tui.StopGateway and gui.StopServing for it (added in review).
- Handover: the successor answered a Claude turn before the predecessor
  had saved its shelf. The predecessor now serves
  GET /_magpie/claude-ready on its callback socket, answered once
  abortAll has run. A Claude turn on the successor waits for it.
  - The predecessor saves only after every request it holds has ended,
    of any provider, which can take up to handedOffDrain (15m).
  - Added in review: the wait is bounded by predecessorWait (90s, inside
    Claude Code's ten minutes for a reply). Past that, the turn goes on
    as before, as a replay, and no later turn waits
    (errPredecessorSlow).
  - loadShelf still reads a shelf written later, at the next turn with
    no session found.
  - A predecessor without the endpoint (404) keeps the old race.
- Sweep: sweepSessions removed session files older than idleLongest.
  That included a session kept for hours past a quota 429
  (keepForReset) and one moving from the shelf to a run.
  - Those are now protected: the shelf file, the in-memory shelf, the
    resuming set and active runs.
  - An unreadable shelf skips the sweep.
  - loadShelf and the sweep's snapshot are serialised by shelfFileMu.
    The bridge lock is held only for the shelf maps, never across the
    file's reads (added in review: the branch read the file under b.mu,
    which every Claude turn takes).

From recih's branch fix/web-restart-session-persistence (7a0261cc1,
fab0ea651), with the review's follow-ups. Not taken from the branch:
the .github/workflows/test.yml change (contributors.md: .github/ stops
for the maintainer), and the test-flake commits in other packages,
which are separate goals.

Red on origin/main 48fffb60c (the branch's tests, trimmed to what
compiles there), on mica:
- TestWebShutdownKeepsClaudeSession quit and restart: no
  claude-shelf.json ("Web.Wait did not keep the idle Claude session").
  Restart is a real Unix re-exec of the test binary with a fake claude.
- TestDesktopShutdownDrainsGateway.
- TestClaudeHandoverWaitsForSavedSession: "the successor answered
  before the predecessor saved its session".
- TestClaudeSweepKeepsQuotaSessions: 5 of 5 subtests swept the kept
  session.

Broken by hand: with runTUI's, runGUI's and runPanel's
stopGatewayOnSignal defer removed, TestHangupDrainsTheGatewayFirst
fails tui, app and panel ("SIGHUP never drained the gateway"), on
mote.
Run on mote at e4506887c (this stack on origin/main 7300be4d0):
go test -timeout 45m ./...: 101 packages ok, 2 FAIL, both open issues
that fail the same at 7300be4d0 on mote, where an ssh session forwards
127.0.0.1:3425 to a real magpie: internal/cli's
TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr,
TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr and
TestUpdateFindsAMagpieThatWroteNoRecord (#6223), internal/gui's
TestLaneLimitRoutes (#5150). On mica: gofmt -l clean; go vet ./...,
GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go
build, GOOS=linux go build -tags nogui, GOOS=windows go build all ok.
go test -race -count=2 of the new and touched tests in internal/gateway,
gui, plugin, tui ok on mote.
Not tried: a real desktop app quit or update with a real Claude
account; SIGHUP from a real terminal closing.

Rebased since. At dc507aefd on mote: go vet (darwin, GOOS=linux
-tags nogui, windows) ok; go test of internal/cli, tui, plugin,
gateway, gui, claudebridge ok but for the same #6223 and #5150 reds.
At 939fe31b6 (on origin/main 78b6d47cc) on mote: gofmt -l clean, the
three vets ok, the stack's tests with TestClaude* and
TestCoreScriptBudget ok in all six packages. Pushed on b400566f9 after
a clean rebase; the commits between touch no Go file this stack does.

Co-authored-by: recih <240406+recih@users.noreply.github.com>
2026-11-18 23:06:38 +08:00

76 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//go:build !nogui
package cli
import (
"log"
"github.com/yetone/magpie/internal/applog"
"github.com/yetone/magpie/internal/gui"
"github.com/yetone/magpie/internal/proc"
)
const hasGUI = true
// runGUI starts the desktop app, writing its log to a file (startLog).
func runGUI(showMain bool, link string) error {
startLog("app")
// SIGHUP, which Wails leaves to magpie, drains the gateway first
defer stopGatewayOnSignal(guiStop)()
return openApp(showMain, link)
}
// openApp starts the desktop app; a var so a test can stand in for it.
// The app may have been opened from the Finder, with none of the PATH a
// terminal has.
var openApp = func(showMain bool, link string) error {
proc.UserPath()
gui.Started = ownSignals // once Wails handles them: it quits through OnShutdown
return gui.Run(version, showMain, link)
}
// runWindow starts the app with its window open on view ("" for the first
// tab).
func runWindow(view string) error {
gui.OpenView = view
return runGUI(true, "")
}
// runUninstallWindow starts the app with its window on Settings ›
// Uninstall magpie, asking, or brings the running one's there: what
// Windows' Installed apps runs for magpie's Uninstall.
func runUninstallWindow() error {
gui.OpenUninstall = true
return runGUI(true, "")
}
// runPanel starts the app with its quick panel open, or toggles the panel
// of the one already running.
func runPanel() error {
startLog("panel")
defer stopGatewayOnSignal(guiStop)() // as runGUI's
return openPanel()
}
// openPanel starts the app with its panel open; a var so a test can stand
// in for it.
var openPanel = func() error {
proc.UserPath()
gui.OpenPanel = true
gui.Started = ownSignals // once Wails handles them: it quits through OnShutdown
return gui.Run(version, false, "")
}
// startLog has the app write its log to a file as well as stderr
// (applog): opened from the Finder, the Dock or the Start menu it has no
// terminal, and what it logged went nowhere (rayem1w on X). A var so a
// test can tell runGUI starts it without starting the app.
var startLog = func(how string) {
if _, err := applog.Start(version, how); err != nil {
log.Println("log:", err)
}
}
// guiStop is gui.StopServing; a var so tests can stand in for it.
var guiStop = gui.StopServing