* fix(proc): end what a CLI probe started, on its timeout and when magpie exits The probes that ask an agent's CLI something (claude auth status, cursor-agent about/models/status, the --version checks) ran through proc.CommandContext, which on its timeout kills only the command itself. A CLI that is a script (a version manager's wrapper, a shim) left what it had started running, adopted by init; and a probe still asking when magpie exited (a one-shot command, an update, a restart) was left whole, with nothing to end it. With Omarchy's mise wrappers stuck in their exec loop (omacom/omarchy#7234) that was five processes holding about half a core each for up to six hours. proc.ProbeContext gives such a probe a session of its own on Unix, as StartTree does, and its context ending kills the whole group. Probes still running are listed, and proc.EndProbes kills them when magpie exits (after run in main) or puts a new version in its place (update.Reexec). On Windows a probe is killed alone, as before, a job being joined only once the command has started. TestProbeTimeoutEndsWhatItStarted and TestEndProbes fail without the change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(proc): end probes on a terminal's signals and on a Mac's quit Review of the first change: a probe leading its own session no longer gets the Ctrl+C, hang-up or SIGTERM sent to magpie's terminal or service, and magpie ending on the signal never reached EndProbes, so the probe was left to init where before it died with magpie. main now catches SIGINT, SIGTERM and SIGHUP, ends the probes and exits as the signal would have (128+n). A signal magpie was started ignoring (nohup, a background job) stays ignored. The account sign-in and plugin commands, which end themselves on Ctrl+C, go through interruptContext and are left to do so. On a Mac the desktop app's quit ([NSApp terminate:], also the restart to update) exits without returning to main, so OnShutdown ends the probes. TestProbeTimeoutEndsWhatItStarted ended its probe on a 500ms timeout, which on a Mac could come before the script had written its child's pid (6 of 10 runs failed in review). It now ends the context once that pid is read, the same Cancel path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: let the app and the TUI quit on a signal their own way Review of the signal handling: endProbesOnSignal exited ahead of Wails and bubbletea, which quit on SIGINT and SIGTERM themselves. `magpie tray` exited 143 without OnShutdown (no downloaded update installed, and sessions.Saved skipped in main), and `magpie tui` left the terminal on its alternate screen with the cursor hidden. The app's and the TUI's entry points now hand the signals back (ownSignals): the app's OnShutdown ends the probes, and the TUI comes back to main, which ends them too. A one-shot command keeps the handler. TestOwnSignalsHandsThemBack fails without the change (magpie exited 143 before the command could quit its own way); TestSignalExits keeps the one-shot behavior. childPid waited 3s for a script just written to start its child, which a busy Mac can exceed (2 of 3 full runs failed in review). It waits 30s now and, should the probe end first, says the probe's error rather than "the script started nothing". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep SIGHUP for the app and the TUI, and die of the signal ownSignals handed SIGHUP back along with SIGINT and SIGTERM, but neither Wails nor bubbletea handles it: the terminal closing under the TUI or an app started from a shell ended magpie on the spot, and its probes, in a session of their own, out of the hang-up's reach, kept running (before this PR they were in the terminal's group and ended with it). SIGHUP now stays with endProbesOnSignal. A tray under a pty whose terminal closes left 2 processes before, none now. The handler asked whether a command was ending itself on Ctrl+C only after EndProbes, which can take up to a second; a sign-in done in the meantime got magpie ended in the middle of its own cleanup. It is asked first. It exited 128+n, which a shell takes for a child that handled the signal: a loop running magpie went on to the next run on Ctrl+C. It now dies of the signal (handling reset, the signal sent again); Windows, which can't send one, still exits with the code. agentProbe and agentCommand share withProxy; the Windows probeGroup comment no longer says EndProbes ends what a shim started. TestOwnSignalsKeepsHangUp and TestInterruptWhileEndingProbes fail without the change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: hand signals to the TUI and the app only once they listen Review: runTUI handed SIGINT and SIGTERM back before tui.Run, whose newModel asks agents' CLIs (claude auth status, up to 5s) before bubbletea listens for signals. Ctrl+C, SIGTERM or SIGINT while it loaded ended magpie by Go's default and left the probe and its child running. tui.Run now takes ready, called once the model is made, and runTUI passes ownSignals. The app had the same window: runGUI handed them back before gui.Run, which starts the backend (probes included) before Wails' Run starts listening. ownSignals is now gui.Started, called on ApplicationStarted, which Wails emits after its signal handler is set up. A tray sent SIGTERM 0.05s after starting, a probe under way, left 2 processes before and none now; one sent it once started still quits through OnShutdown (exit 0). TestTUIKeepsSignalsWhileLoading fails without the change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
376 lines
10 KiB
Go
376 lines
10 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
const pluginUsage = `usage: magpie plugin [list] [--json]
|
|
magpie plugin add <npm package | path> install an OpenCode provider plugin (opencode-gemini-auth, ./my-plugin.js)
|
|
magpie plugin rm <name> remove one
|
|
magpie plugin update install the newest version of each
|
|
magpie plugin on|off <name> turn one on or off
|
|
magpie plugin login <provider> [<method>] sign in to a provider a plugin adds
|
|
magpie plugin logout <provider> forget the sign-in
|
|
magpie plugin move|migrate <subscription> run a built-in subscription's accounts on its community plugin
|
|
magpie plugin move-back|unmigrate <subscription> go back to the built-in, with its accounts`
|
|
|
|
// pluginCmd: `magpie plugin …` — OpenCode's provider plugins, which sign in
|
|
// to a subscription and carry its requests (internal/plugin).
|
|
func pluginCmd(args []string) error {
|
|
sub := "list"
|
|
if len(args) > 1 {
|
|
sub = args[1]
|
|
}
|
|
rest := args[min(len(args), 2):]
|
|
ctx, stop := interruptContext()
|
|
defer stop()
|
|
switch sub {
|
|
case "list", "ls", "--json":
|
|
return listPlugins(ctx, sub == "--json" || len(rest) > 0 && rest[0] == "--json")
|
|
case "add", "install":
|
|
if len(rest) != 1 {
|
|
return errors.New(pluginUsage)
|
|
}
|
|
if !plugin.IsPath(rest[0]) && !plugin.HasBun() {
|
|
fmt.Println(muted.Render("Downloading Bun " + plugin.BunInUse() + ", which plugins run on…"))
|
|
}
|
|
e, err := plugin.Add(ctx, rest[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), "added", e.Spec)
|
|
return listPlugins(ctx, false)
|
|
case "rm", "remove", "uninstall":
|
|
if len(rest) != 1 {
|
|
return errors.New(pluginUsage)
|
|
}
|
|
back := provider.MovedOnto(rest[0])
|
|
if err := provider.RemovePlugin(ctx, rest[0]); err != nil {
|
|
return err
|
|
}
|
|
for _, id := range back {
|
|
fmt.Println(green.Render("✓"), id, "is back on its built-in")
|
|
}
|
|
fmt.Println(green.Render("✓"), "removed", rest[0])
|
|
return nil
|
|
case "update", "upgrade":
|
|
if err := plugin.Update(ctx); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), "plugins updated")
|
|
return listPlugins(ctx, false)
|
|
case "on", "off":
|
|
if len(rest) != 1 {
|
|
return errors.New(pluginUsage)
|
|
}
|
|
var back []string
|
|
if sub == "off" {
|
|
back = provider.MovedOnto(rest[0])
|
|
}
|
|
if err := provider.SetPluginOff(ctx, rest[0], sub == "off"); err != nil {
|
|
return err
|
|
}
|
|
for _, id := range back {
|
|
fmt.Println(green.Render("✓"), id, "is back on its built-in")
|
|
}
|
|
fmt.Println(green.Render("✓"), rest[0], "is", sub)
|
|
return nil
|
|
case "login", "signin":
|
|
if len(rest) < 1 || len(rest) > 2 {
|
|
return errors.New(pluginUsage)
|
|
}
|
|
method := ""
|
|
if len(rest) == 2 {
|
|
method = rest[1]
|
|
}
|
|
return pluginLogin(ctx, rest[0], method)
|
|
case "logout", "signout":
|
|
if len(rest) != 1 {
|
|
return errors.New(pluginUsage)
|
|
}
|
|
pp, err := pluginProvider(ctx, rest[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := plugin.SignOut(ctx, pp.ID, ""); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), "signed out of", pp.Name)
|
|
return nil
|
|
case "move", "migrate":
|
|
if len(rest) != 1 {
|
|
return errors.New(pluginUsage)
|
|
}
|
|
if !provider.Movable(rest[0]) {
|
|
return fmt.Errorf("%s has no plugin to move to", rest[0])
|
|
}
|
|
if !plugin.HasBun() {
|
|
fmt.Println(muted.Render("Downloading Bun " + plugin.BunInUse() + ", which plugins run on…"))
|
|
}
|
|
if err := provider.Move(ctx, rest[0]); err != nil {
|
|
return fmt.Errorf("%s stays built-in: %w", rest[0], err)
|
|
}
|
|
fmt.Println(green.Render("✓"), rest[0], "runs on", provider.MovePackage(rest[0]), muted.Render("(magpie plugin move-back "+rest[0]+" to undo)"))
|
|
return nil
|
|
case "move-back", "moveback", "unmigrate":
|
|
if len(rest) != 1 {
|
|
return errors.New(pluginUsage)
|
|
}
|
|
if !provider.Moved(rest[0]) {
|
|
return fmt.Errorf("%s isn't on its plugin", rest[0])
|
|
}
|
|
if err := provider.MoveBack(ctx, rest[0]); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), rest[0], "is built-in again")
|
|
return nil
|
|
case "help", "-h", "--help":
|
|
fmt.Println(pluginUsage)
|
|
return nil
|
|
}
|
|
return errors.New(pluginUsage)
|
|
}
|
|
|
|
func listPlugins(ctx context.Context, asJSON bool) error {
|
|
l := plugin.Load()
|
|
if len(l.Plugins) == 0 {
|
|
if asJSON {
|
|
fmt.Println("[]")
|
|
return nil
|
|
}
|
|
fmt.Println("No plugins. Add one: magpie plugin add <npm package | path>")
|
|
return nil
|
|
}
|
|
loaded, lerr := plugin.Plugins(ctx)
|
|
errs := map[string]string{}
|
|
for _, p := range loaded {
|
|
errs[p.Spec] = p.Error
|
|
}
|
|
ps, perr := plugin.Providers(ctx)
|
|
if asJSON {
|
|
b, _ := json.MarshalIndent(map[string]any{"plugins": l.Plugins, "loaded": loaded, "providers": ps}, "", " ")
|
|
fmt.Println(string(b))
|
|
return nil
|
|
}
|
|
// a built-in with accounts the plugin could run: not signed in to the
|
|
// plugin is its normal state, not something to fix
|
|
onBuiltin := map[string]provider.MoveCandidate{}
|
|
for _, c := range provider.MoveCandidates() {
|
|
onBuiltin[c.ID] = c
|
|
}
|
|
for _, e := range l.Plugins {
|
|
state := green.Render("on")
|
|
switch {
|
|
case e.Off:
|
|
state = muted.Render("off")
|
|
case errs[e.Spec] != "":
|
|
state = "failed: " + errs[e.Spec]
|
|
}
|
|
fmt.Printf("%s %s\n", bold.Render(e.Spec), state)
|
|
for _, p := range ps {
|
|
if p.Spec != e.Spec {
|
|
continue
|
|
}
|
|
who := muted.Render("not signed in · magpie plugin login " + p.ID)
|
|
if c, ok := onBuiltin[p.ID]; ok && !p.SignedIn {
|
|
who = muted.Render(fmt.Sprintf("runs on magpie's built-in (%d accounts) · magpie plugin move %s", c.Accounts, p.ID))
|
|
}
|
|
if p.SignedIn {
|
|
who = green.Render("signed in")
|
|
if p.AccountID != "" {
|
|
who += " as " + p.AccountID
|
|
}
|
|
}
|
|
fmt.Printf(" %s %s %s\n", provider.PluginID(p.ID), muted.Render("("+p.Name+", "+strconv.Itoa(len(p.Models))+" models)"), who)
|
|
}
|
|
}
|
|
if lerr != nil {
|
|
return lerr
|
|
}
|
|
return perr
|
|
}
|
|
|
|
// pluginProvider is the plugins' provider named by OpenCode's id or
|
|
// magpie's.
|
|
func pluginProvider(ctx context.Context, name string) (plugin.Provider, error) {
|
|
ps, err := plugin.Providers(ctx)
|
|
if err != nil {
|
|
return plugin.Provider{}, err
|
|
}
|
|
for _, p := range ps {
|
|
if p.ID == name || provider.PluginID(p.ID) == name || strings.EqualFold(p.Name, name) {
|
|
return p, nil
|
|
}
|
|
}
|
|
var ids []string
|
|
for _, p := range ps {
|
|
ids = append(ids, p.ID)
|
|
}
|
|
if len(ids) == 0 {
|
|
return plugin.Provider{}, fmt.Errorf("no plugin signs in to %q — add one: magpie plugin add <npm package>", name)
|
|
}
|
|
return plugin.Provider{}, fmt.Errorf("no plugin signs in to %q; they sign in to %s", name, strings.Join(ids, ", "))
|
|
}
|
|
|
|
// pluginLogin signs in to a plugin's provider as OpenCode's `auth login`
|
|
// does: the method, its questions, then a browser (and a code pasted
|
|
// back) or a key.
|
|
func pluginLogin(ctx context.Context, name, method string) error {
|
|
pp, err := pluginProvider(ctx, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(pp.Methods) == 0 {
|
|
return fmt.Errorf("%s's plugin has no way to sign in", pp.Name)
|
|
}
|
|
m := 0
|
|
switch {
|
|
case method != "":
|
|
m = -1
|
|
for i, x := range pp.Methods {
|
|
if strconv.Itoa(i+1) == method || strings.EqualFold(x.Label, method) || x.Type == method {
|
|
m = i
|
|
break
|
|
}
|
|
}
|
|
if m < 0 {
|
|
return fmt.Errorf("%s has no sign-in method %q", pp.Name, method)
|
|
}
|
|
case len(pp.Methods) > 1:
|
|
fmt.Println("How do you sign in to", pp.Name+"?")
|
|
for i, x := range pp.Methods {
|
|
fmt.Printf(" %d. %s\n", i+1, x.Label)
|
|
}
|
|
n, err := askNumber(len(pp.Methods))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
m = n
|
|
}
|
|
inputs := map[string]string{}
|
|
for {
|
|
q, err := plugin.NextPrompt(ctx, pp.ID, m, inputs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if q == nil {
|
|
break
|
|
}
|
|
for {
|
|
v, err := askPrompt(q)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
msg, err := plugin.Validate(ctx, pp.ID, m, q.Key, v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if msg == "" {
|
|
inputs[q.Key] = v
|
|
break
|
|
}
|
|
fmt.Println(msg)
|
|
}
|
|
}
|
|
var saved plugin.Saved
|
|
if pp.Methods[m].Type == "api" {
|
|
key, err := secret("key", keyPrompt(pp.Name, pp.Methods[m]), false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if saved, err = plugin.APIKey(ctx, pp.ID, m, inputs, key, plugin.NewAccount); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
a, err := plugin.Authorize(ctx, pp.ID, m, inputs, plugin.NewAccount)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if a.URL != "" {
|
|
fmt.Println("Sign in in your browser. If it didn't open, go to:")
|
|
fmt.Println(faint.Render(a.URL))
|
|
openInBrowser(a.URL)
|
|
}
|
|
if a.Instructions != "" {
|
|
fmt.Println(a.Instructions)
|
|
}
|
|
code := ""
|
|
if a.Method == "code" {
|
|
fmt.Print("Code: ")
|
|
line, err := stdin.ReadString('\n')
|
|
if err != nil && line == "" {
|
|
return err
|
|
}
|
|
code = strings.TrimSpace(line)
|
|
}
|
|
wait, cancel := context.WithTimeout(ctx, 10*time.Minute)
|
|
defer cancel()
|
|
if saved, err = plugin.Finish(wait, a.Session, code); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
fmt.Println(green.Render("✓"), "signed in to", pp.Name, muted.Render("· its models are "+provider.PluginID(saved.Provider)+"/<model>"))
|
|
return nil
|
|
}
|
|
|
|
// keyPrompt asks an "api" method's key: its title, then the hint the
|
|
// plugin gives (its placeholder), as a question's is.
|
|
func keyPrompt(name string, m plugin.Method) string {
|
|
p := m.KeyTitle(name)
|
|
if m.Placeholder != "" {
|
|
p += " " + muted.Render("("+m.Placeholder+")")
|
|
}
|
|
return p + ": "
|
|
}
|
|
|
|
func askNumber(n int) (int, error) {
|
|
for {
|
|
fmt.Print("> ")
|
|
line, err := stdin.ReadString('\n')
|
|
if i, e := strconv.Atoi(strings.TrimSpace(line)); e == nil && i >= 1 && i <= n {
|
|
return i - 1, nil
|
|
}
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
fmt.Printf("A number from 1 to %d\n", n)
|
|
}
|
|
}
|
|
|
|
func askPrompt(q *plugin.Prompt) (string, error) {
|
|
if q.Type == "select" {
|
|
fmt.Println(q.Message)
|
|
for i, o := range q.Options {
|
|
hint := ""
|
|
if o.Hint != "" {
|
|
hint = muted.Render(" " + o.Hint)
|
|
}
|
|
fmt.Printf(" %d. %s%s\n", i+1, o.Label, hint)
|
|
}
|
|
i, err := askNumber(len(q.Options))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return q.Options[i].Value, nil
|
|
}
|
|
p := q.Message
|
|
if q.Placeholder != "" {
|
|
p += " " + muted.Render("("+q.Placeholder+")")
|
|
}
|
|
fmt.Print(p + " ")
|
|
line, err := stdin.ReadString('\n')
|
|
if err != nil && line == "" {
|
|
return "", err
|
|
}
|
|
return strings.TrimSpace(line), nil
|
|
}
|