Files
magpie/help_cli.go
T
yetone 76db4c6c28 main, agent: --help on any command or agent only prints its usage, and Snow and Gemini CLI refuse a value they don't know instead of taking magpie out (#1457, blackrion)
`magpie snow --help` printed "✓ Snow CLI model --help" and did what
`magpie snow default` does. It deleted profiles/magpie.json, put back the
active profile, and wrote "--help" as advancedModel into the profile it
went back to. There were two causes:

- run's agent forms took any second argument as a model. The `len(args)==2`
  branch special-cased only `default`.
- Snow's model Set, on magpie's profile, treated a value no profile of the
  user's is on as "leave, and write it into the profile left for".

The arguments were the class, not Snow alone. Under the old code, `magpie
claude --help` and `magpie codex --help` wrote "--help" as Claude Code's
and Codex's model, and `magpie save --help` saved a profile named
"--help" (the new guard test caught each of these).

What changed:
- help_cli.go and run: -h or --help anywhere in the arguments, or help as
  the word after the command, prints that command's usage and returns
  first, before run reads, moves or starts anything. It prints the
  command's own usage where it has one (model, group, group rule, library,
  plugin, quota, sessions, search, visible, webdav, s3, claude-code), its
  lines of magpie's usage otherwise, and for an agent its forms and
  fields. The usage gains a `magpie <command> --help` line.
- `magpie <agent> …` refuses any other argument starting with "-"
  (negative numbers aside): "unknown flag X: nothing was changed".
- snow.go: on magpie's profile, a model that is neither through magpie
  nor any profile's model is refused, and the files stay as they were.
  A `magpie/<x>` that isn't in the catalog is refused on or off magpie's
  profile; it used to fall through to the same leave. A model of one of
  the user's profiles still switches Snow there, and empty still goes
  back to the profile they were on.
- gemini.go, a sibling found by auditing every agent's Set: provider is
  Gemini CLI's first field, so `magpie gemini <a model with no option>`
  reached it. It unrouted Gemini CLI (stashed key, sign-in and model put
  back) and only then said "unknown provider". An auth it doesn't know is
  now refused before unroute. The audit found no other Set that leaves
  on an unknown value. The other model Sets write an unknown value as the
  agent's own model, which is the intended own-model pick. The on/off
  provider fields of ante, claudedesktop, copilotjetbrains, cursorlocal,
  pencil, t3code, workbuddy and zcode take any non-empty value as "on".
  Both are left as they are.
- docs/subsystems/agent-wiring.md says the above.

Tests:
- TestHelpOnEveryCommandDoesNothing reads the commands from run's switch
  in main.go's AST, so a new command is covered without a list to keep.
  It adds every agent id. For each one it runs --help, -h and help in
  several positions under a sandbox home, and checks that nothing is
  written, the run returns within 20s and usage is printed.
- TestAgentHelpLeavesSnowWired is the reporter's case: Snow wired, then
  `snow --help`, `-h`, `help`, `model --help` and `snow-cli --help`,
  unknown flags, and `snow nonsense`. Snow's files must stay the same.
- TestSnowUnknownModelChangesNothing and TestGeminiUnknownAuthChangesNothing
  cover the Sets.

What was run:
- Each test was run against the old code. Old main.go: the save, snow,
  claude and codex subtests FAIL ("✓ saved profile --help" with
  profiles.json written; "✓ Snow CLI model --help"; "✓ Claude Code model
  --help"; "✓ Codex model --help"), and so does TestAgentHelpLeavesSnowWired.
- Old snow.go with old main.go: TestAgentHelpLeavesSnowWired FAILs with
  Snow's files changed, and TestSnowUnknownModelChangesNothing FAILs with
  `"--help" taken`.
- Old gemini.go: TestGeminiUnknownAuthChangesNothing FAILs with
  `"nonsense-model" changed Gemini CLI`.
- All pass with the fix.
- In a sandbox HOME with pinned Go caches, at origin/main 4dd2112ef + this
  change: `go test -tags nogui -timeout 40m ./...` is green (48 packages ok, no FAIL).
- gofmt -l is clean. go vet, GOOS=linux go vet -tags nogui and
  GOOS=windows go vet -tags nogui pass on . and ./internal/agent.
- Not tried: the real Snow CLI on Windows, the reporter's setup. The fix
  returns before anything is read, so nothing of Snow is run.
2026-10-11 15:56:06 +08:00

136 lines
3.7 KiB
Go

package main
import (
"fmt"
"strconv"
"strings"
"github.com/yetone/magpie/internal/agent"
)
// helpAsked is whether args ask for help rather than for the command to
// run: -h or --help anywhere, or help as the word after the command. It
// is looked at before run does anything, since a command that took
// --help for a value acted on it: `magpie snow --help` set Snow's model
// to "--help", which took magpie out of it (#1457), and `magpie serve
// --help` started a gateway.
func helpAsked(args []string) bool {
// what magpie is started with by others (Claude Code's MCP helper, the
// Agents page's dry run, a link, Windows' -Embedding) never carries
// an exact -h, --help or help
for _, a := range args {
if a == "-h" || a == "--help" {
return true
}
}
return len(args) > 1 && args[1] == "help"
}
// helpFor is what `magpie <command> --help` prints: the command's own
// usage where it has one, its lines of magpie's usage otherwise, an
// agent's forms and fields for an agent, and the whole usage for
// anything else.
func helpFor(args []string) string {
cmd := args[0]
switch cmd {
case "-h", "--help", "help":
return usage
case "model":
return modelUsage
case "group", "groups":
if len(args) > 1 && (args[1] == "rule" || args[1] == "rules") {
return ruleUsage
}
return groupUsage
case "library", "lib":
return " " + libraryUsage
case "plugin", "plugins":
return pluginUsage
case "quota", "quotas":
return quotaUsage
case "sessions":
return sessionsUsage
case "search":
return searchUsage
case "visible":
return visibleUsage
case "webdav", "dav":
return webdavUsage
case "s3":
return s3Usage
case "claude-code":
return claudeCodeUsage
case "list":
cmd = "ls"
case "account":
cmd = "accounts"
case "app", "gui":
cmd = ""
}
if cmd != "" && !strings.HasPrefix(cmd, "-") {
if lines := usageLines(cmd); lines != "" {
return lines
}
}
if a, err := agent.Find(cmd); err == nil && cmd != "" {
return agentHelp(a)
}
return usage
}
// usageLines are the lines of magpie's usage for `magpie <cmd>`, with the
// lines that carry on from them.
func usageLines(cmd string) string {
var b strings.Builder
in := false
for _, l := range strings.Split(usage, "\n") {
if rest, ok := strings.CutPrefix(l, " magpie "); ok {
in = rest == cmd || strings.HasPrefix(rest, cmd+" ")
} else if !strings.HasPrefix(l, " ") {
in = false
}
if in {
b.WriteString(l + "\n")
}
}
return b.String()
}
// agentHelp is `magpie <agent> --help`: the forms that set it, and its
// fields.
func agentHelp(a *agent.Agent) string {
var b strings.Builder
id := a.ID
fmt.Fprintf(&b, "%s\n\n", a.Name)
fmt.Fprintf(&b, " magpie %s show it\n", id)
if len(a.Fields) == 0 {
return b.String()
}
fmt.Fprintf(&b, " magpie %s <model> set its model (magpie models %s lists them)\n", id, id)
fmt.Fprintf(&b, " magpie %s <field> <value> set another field\n", id)
fmt.Fprintf(&b, " magpie %s default take magpie out: back on what it had before\n", id)
fmt.Fprintf(&b, " magpie %s <field> default that field back to its own default\n", id)
var keys []string
for _, f := range a.Fields {
keys = append(keys, f.Key)
}
fmt.Fprintf(&b, "\nfields: %s\n", strings.Join(keys, ", "))
return b.String()
}
// flagArg is an argument to `magpie <agent> …` that is a flag, which no
// agent form takes: it is said to be one, rather than set as a model or a
// field's value. A negative number is a value.
func flagArg(args []string) string {
for _, a := range args {
if !strings.HasPrefix(a, "-") {
continue
}
if _, err := strconv.ParseFloat(a, 64); err == nil {
continue
}
return a
}
return ""
}