Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
cli: prompts read the stdin Windows' console init reopens, not the one package init saw
yetone9298d2a7c381
diff --git a/internal/cli/backup_cmd.go b/internal/cli/backup_cmd.goindex d479cb90d..0e42e5cad 100644--- a/internal/cli/backup_cmd.go+++ b/internal/cli/backup_cmd.go@@ -8,6 +8,7 @@ import ("os""slices""strings"+ "sync""golang.org/x/term"@@ -143,15 +144,33 @@ func passphrase(prompt string, confirm bool) (string, error) {}// stdin is read through one reader, so a second secret piped in is the-// second line and not lost to the first one's buffer.-var stdin = bufio.NewReader(os.Stdin)+// second line and not lost to the first one's buffer. The reader is made+// over os.Stdin as it is when first read, never at package init: package+// variables are set before any init runs, and on Windows the desktop+// build's init (console_windows.go) only then swaps in the terminal's+// CONIN$. A reader made earlier keeps the invalid handle, and every+// prompt fails with "read /dev/stdin: The handle is invalid".+var (+ stdinMu sync.Mutex+ stdinFile *os.File+ stdinBuf *bufio.Reader+)++func stdin() *bufio.Reader {+ stdinMu.Lock()+ defer stdinMu.Unlock()+ if stdinBuf == nil || stdinFile != os.Stdin {+ stdinFile, stdinBuf = os.Stdin, bufio.NewReader(os.Stdin)+ }+ return stdinBuf+}// secret reads what (a passphrase, a password) as passphrase does; piped// in, it is the next line of stdin.func secret(what, prompt string, confirm bool) (string, error) {fd := int(os.Stdin.Fd())if !term.IsTerminal(fd) {- line, err := stdin.ReadString('\n')+ line, err := stdin().ReadString('\n')line = strings.TrimRight(line, "\r\n")if line == "" {if err != nil {diff --git a/internal/cli/console_windows.go b/internal/cli/console_windows.goindex 69950436b..234ea1b5c 100644--- a/internal/cli/console_windows.go+++ b/internal/cli/console_windows.go@@ -3,6 +3,8 @@ package cliimport ("os""syscall"++ "github.com/charmbracelet/lipgloss")// The desktop build is linked as a GUI program so double-clicking it opens@@ -24,6 +26,13 @@ func init() {}if out, err := os.OpenFile("CONOUT$", os.O_RDWR, 0); err == nil {os.Stdout, os.Stderr = out, out+ // lipgloss's default renderer was made over the os.Stdout before+ // this, a handle that is no terminal, so every style would print+ // without colour. The styles made at package init hold that+ // renderer, so it is pointed at the console in place. Anything+ // else that reads or writes the standard streams takes os.Stdin,+ // os.Stdout or os.Stderr when it runs, never in a package variable.+ lipgloss.DefaultRenderer().SetOutput(lipgloss.NewRenderer(out).Output())}if in, err := os.OpenFile("CONIN$", os.O_RDWR, 0); err == nil {os.Stdin = indiff --git a/internal/cli/library_cli.go b/internal/cli/library_cli.goindex dff889765..4396e8c27 100644--- a/internal/cli/library_cli.go+++ b/internal/cli/library_cli.go@@ -228,7 +228,7 @@ func removeEverySkill(args []string) (*library.Result, error) {}fmt.Printf("Remove all %s (%s) from the library and every agent? Folders magpie keeps go to its backups; folders of your own are only unlinked. [y/N] ",plural(len(names), "skill"), strings.Join(names, ", "))- line, _ := stdin.ReadString('\n')+ line, _ := stdin().ReadString('\n')if a := strings.ToLower(strings.TrimSpace(line)); a != "y" && a != "yes" {return nil, fmt.Errorf("nothing removed")}@@ -287,7 +287,7 @@ func removeGoneSkills(args []string) (*library.Result, error) {}fmt.Printf("Remove %s removed from their source (%s) from the library and every agent? The library's folders go to magpie's backups. [y/N] ",plural(len(names), "skill"), strings.Join(shown, ", "))- line, _ := stdin.ReadString('\n')+ line, _ := stdin().ReadString('\n')if a := strings.ToLower(strings.TrimSpace(line)); a != "y" && a != "yes" {return nil, fmt.Errorf("nothing removed")}diff --git a/internal/cli/plugins_cli.go b/internal/cli/plugins_cli.goindex 71274862d..dc9734062 100644--- a/internal/cli/plugins_cli.go+++ b/internal/cli/plugins_cli.go@@ -498,7 +498,7 @@ func pluginLogin(ctx context.Context, name, method string) error {code := ""if a.Method == "code" {fmt.Print("Code: ")- line, err := stdin.ReadString('\n')+ line, err := stdin().ReadString('\n')if err != nil && line == "" {return err}@@ -512,7 +512,7 @@ func pluginLogin(ctx context.Context, name, method string) error {fmt.Println("If the page the browser ends on won't load (magpie on a server or in Docker), paste its whole address here and press Enter:")go func() {for wait.Err() == nil {- line, err := stdin.ReadString('\n')+ line, err := stdin().ReadString('\n')if strings.TrimSpace(line) != "" && wait.Err() == nil {if next, err := provider.PastePluginCallback(wait, a.URL, line); err != nil {fmt.Println(err)@@ -563,7 +563,7 @@ func keyPrompt(name string, m plugin.Method) string {func askNumber(n int) (int, error) {for {fmt.Print("> ")- line, err := stdin.ReadString('\n')+ line, err := stdin().ReadString('\n')if i, e := strconv.Atoi(strings.TrimSpace(line)); e == nil && i >= 1 && i <= n {return i - 1, nil}@@ -609,7 +609,7 @@ func askPrompt(q *plugin.Prompt) (string, error) {fmt.Println()return strings.TrimSpace(string(b)), err}- line, err := stdin.ReadString('\n')+ line, err := stdin().ReadString('\n')if err != nil && line == "" {return "", err}diff --git a/internal/cli/quota_cli.go b/internal/cli/quota_cli.goindex 7f29cf392..8a37e72f5 100644--- a/internal/cli/quota_cli.go+++ b/internal/cli/quota_cli.go@@ -246,7 +246,7 @@ func quotaResetCmd(args []string) error {n += " (the one that runs out first, " + provider.ResetClock(*held.Resets.Until, time.Now()) + ")"}fmt.Printf("Spend %s on %s, starting its current windows again? It can't be undone. [y/N] ", n, who)- line, _ := stdin.ReadString('\n')+ line, _ := stdin().ReadString('\n')if a := strings.ToLower(strings.TrimSpace(line)); a != "y" && a != "yes" {return fmt.Errorf("nothing spent")}diff --git a/internal/cli/stdin_test.go b/internal/cli/stdin_test.gonew file mode 100644index 000000000..b5950c384--- /dev/null+++ b/internal/cli/stdin_test.go@@ -0,0 +1,55 @@+package cli++import (+ "os"+ "testing"++ "github.com/yetone/magpie/internal/plugin"+)++// swapStdin sets os.Stdin to a pipe holding text, after package init, and+// touches nothing else: it is what the Windows desktop build's init does+// when it reopens the terminal's CONIN$ (console_windows.go).+func swapStdin(t *testing.T, text string) {+ t.Helper()+ r, w, err := os.Pipe()+ if err != nil {+ t.Fatal(err)+ }+ w.WriteString(text)+ w.Close()+ old := os.Stdin+ os.Stdin = r+ t.Cleanup(func() {+ os.Stdin = old+ r.Close()+ })+}++// TestPromptsReadStdinSetAfterInit: every CLI prompt reads os.Stdin as it+// is when asked, not as it was when the package's variables were set. On+// Windows a reader made at package init held the GUI binary's invalid+// handle, and `magpie plugin login` failed with "read /dev/stdin: The+// handle is invalid" (tom047784).+func TestPromptsReadStdinSetAfterInit(t *testing.T) {+ // plugin login's question, as a line+ swapStdin(t, "blue\n")+ if v, err := askPrompt(&plugin.Prompt{Type: "text", Key: "team", Message: "Your team"}); err != nil || v != "blue" {+ t.Fatalf("askPrompt = %q, %v; want blue from the stdin set after init", v, err)+ }++ // plugin login's choice of method or option+ swapStdin(t, "2\n")+ if i, err := askNumber(3); err != nil || i != 1 {+ t.Fatalf("askNumber = %d, %v; want 1 from the stdin set after init", i, err)+ }++ // backup's and WebDAV's passphrase and password, piped: a line each,+ // both from the one reader+ swapStdin(t, "first\nsecond\n")+ for _, want := range []string{"first", "second"} {+ if v, err := secret("passphrase", "Passphrase: ", false); err != nil || v != want {+ t.Fatalf("secret = %q, %v; want %s from the stdin set after init", v, err, want)+ }+ }+}diff --git a/internal/cli/webdav_cli_test.go b/internal/cli/webdav_cli_test.goindex 7e8e9dd8e..67c942d06 100644--- a/internal/cli/webdav_cli_test.go+++ b/internal/cli/webdav_cli_test.go@@ -1,7 +1,6 @@package cliimport (- "bufio""io""net/http""net/http/httptest"@@ -110,10 +109,10 @@ func piped(t *testing.T, lines ...string) {w.WriteString(strings.Join(lines, "\n") + "\n")}w.Close()- oldFile, oldReader := os.Stdin, stdin- os.Stdin, stdin = r, bufio.NewReader(r)+ old := os.Stdin+ os.Stdin = rt.Cleanup(func() {- os.Stdin, stdin = oldFile, oldReader+ os.Stdin = oldr.Close()})}