jtbbtnt54249, on LolingVerse's agy thread: "很多都这样,退出有时候又要重新开启".
Several agents' switches were off after quitting magpie and had to be
turned on again by hand.
Looked for a shared path that flips agents off. The row's switch is
Wired() alone, and nothing automatic disconnects an agent except a
removed plugin, the gateway pause (which reconnects on resume) and
Put agents back when quitting (which reconnects at start). The quit
question's Disconnect and quit, though, ran each row's Disconnect,
which leaves the disconnectedKey mark: every agent it listed stayed off
at every start after, and RewireOnStart skips a marked agent. Users
choose it so their agents work while magpie is closed, not to leave
magpie for good.
Now Disconnect and quit posts quit/disconnect (quitDisconnect), which
runs agent.RestoreOnQuit on the agents the question lists: their own
files back, no mark, recorded in quit.restored, so the next start's
RewireOnStart connects them again, the setting on or off. This covers
every agent the question lists: every detected agent wired to magpie,
built-in, plugin and WSL ones alike. A failure answers an error naming
the agent and magpie stays open. Its tooltip says magpie connects them
again when it opens (en, zh, zh-TW, ja, de).
Run at this tree (parent 99a54fa87, origin/main just fetched):
- TestQuitDisconnectComesBackOnStart red on the old api.go and
restoreonquit.go (404 on POST /api/quit/disconnect), green after.
- mote: go test -count=1 ./internal/gui ok (145s).
- mica: node --test quit-ask, quit-ask-setting, restore-on-quit,
i18n-keys, i18n-en-no-cjk: 178 of 178 pass, chromium and webkit,
every language.
- gofmt -l clean; go vet ./internal/gui on darwin and GOOS=windows ok.
Linux vet of internal/gui needs GTK/WebKitGTK headers, which neither
mote nor magpie-desktop has; not run.
Not tried: a real quit of the app with real agents; the agent-level
RestoreOnQuit then RewireOnStart path is covered by
TestRestoreOnQuitAndRewireOnStart. Agents disconnected this way by an
older magpie keep their mark and stay off until switched on once.
75 lines
2.5 KiB
Go
75 lines
2.5 KiB
Go
package gui
|
||
|
||
import (
|
||
"log"
|
||
|
||
"github.com/yetone/magpie/internal/agent"
|
||
"github.com/yetone/magpie/internal/settings"
|
||
)
|
||
|
||
// Settings › General › Put agents back when quitting (tudjam17795 on X:
|
||
// after quitting, the harness it was connected to can't go on). Off by
|
||
// default, a connected agent keeps magpie as it quits. On, the app's quit
|
||
// does what each agent's Disconnect does (agent.RestoreOnQuit: their own
|
||
// files back as they were, no Quit question), and the next start connects
|
||
// them again (agent.RewireOnStart). Only the agents connected at the quit,
|
||
// and not disconnected by the user, are touched.
|
||
|
||
// restoreOnQuit says whether the quit puts the agents back: the setting,
|
||
// and a gateway this process serves (another magpie serving it goes on
|
||
// serving its agents). A settings file that can't be read leaves them.
|
||
func restoreOnQuit() bool {
|
||
s, err := settings.LoadChecked()
|
||
return err == nil && s.RestoreOnQuit && servedHere()
|
||
}
|
||
|
||
// hooks, vars for the tests
|
||
var (
|
||
servedHere = func() bool { return served.Load() != nil }
|
||
quitDetect = agent.Detected
|
||
quitRestore = agent.RestoreOnQuit
|
||
startAgents = agent.All
|
||
startRewire = agent.RewireOnStart
|
||
)
|
||
|
||
// restoreAgentsOnQuit is OnShutdown's part of it.
|
||
func restoreAgentsOnQuit() {
|
||
if !restoreOnQuit() {
|
||
return
|
||
}
|
||
ids, err := quitRestore(quitDetect())
|
||
if err != nil {
|
||
log.Println("restore agents on quit:", err)
|
||
}
|
||
if len(ids) > 0 {
|
||
log.Printf("quit: %d agents put back as they were", len(ids))
|
||
}
|
||
}
|
||
|
||
// quitDisconnect is the quit question's Disconnect and quit (quitask.js):
|
||
// each connected agent put back as RestoreOnQuit puts it, so the next
|
||
// start connects it again. It disconnected each one as its row's
|
||
// Disconnect does, whose mark keeps an agent off at every start after:
|
||
// a user who chose it so their agents work while magpie is closed found
|
||
// every switch off when magpie opened again, and turned each back on by
|
||
// hand (jtbbtnt54249 on X, "退出有时候又要重新开启"). An agent that can't
|
||
// be put back is named in the error, and magpie stays open.
|
||
func quitDisconnect() error {
|
||
ids, err := quitRestore(quitDetect())
|
||
if len(ids) > 0 {
|
||
log.Printf("quit: %d agents put back as they were, for the next start", len(ids))
|
||
}
|
||
return err
|
||
}
|
||
|
||
// rewireAgentsOnStart connects again the agents a quit put back, unless
|
||
// the gateway is paused (the pause's own resume connects its agents).
|
||
func rewireAgentsOnStart() {
|
||
if pausedNow() {
|
||
return
|
||
}
|
||
if ids, _ := startRewire(startAgents()); len(ids) > 0 {
|
||
log.Printf("start: %d agents connected again", len(ids))
|
||
}
|
||
}
|