From 6bffcfe17824d7c7bb4e2df2b784a670a922de44 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 19:03:27 +0800 Subject: [PATCH] gateway-key: a key's monthly limit can be set from the CLI again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `magpie gateway-key limit month --cost 5` (and `add month`) failed with "an N-hour cycle is a whole number of hours …: \"month\"": parseKeyLimit cut an h off any window ending in one before it looked for a named period, and month ends in an h. A named period (day, week, month) is now taken as itself before the d and h forms are read. Found while trying every command for the magpie-setup skill in a sandbox HOME. Run: go test ./internal/cli -run TestGatewayKey -count=1 ok; the new month cases in TestGatewayKeyLimitCLI fail on the old parseKeyLimit (reverted by hand) and pass with it. Tried the built binary in a sandbox HOME: limit month --cost 5 sets "this month". --- internal/cli/gateway_keys_cli.go | 4 +++- internal/cli/gateway_keys_cli_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/cli/gateway_keys_cli.go b/internal/cli/gateway_keys_cli.go index 7f013b991..93227f723 100644 --- a/internal/cli/gateway_keys_cli.go +++ b/internal/cli/gateway_keys_cli.go @@ -296,7 +296,9 @@ func perPhrase(l *access.Limit) string { // --cost and --cache-reads. func parseKeyLimit(window string, rest []string, usage error) (*access.Limit, error) { lim := &access.Limit{Period: window} - if n, ok := strings.CutSuffix(window, "d"); ok { + if window == "day" || window == "week" || window == "month" { + // a period by its name: month ends in an h, and is no count of hours + } else if n, ok := strings.CutSuffix(window, "d"); ok { days, err := strconv.Atoi(n) if err != nil || days < 1 || days > access.MaxDays { return nil, fmt.Errorf("an N-day cycle is a whole number of days from 1 to %d, like 10d: %q", access.MaxDays, window) diff --git a/internal/cli/gateway_keys_cli_test.go b/internal/cli/gateway_keys_cli_test.go index 6867695b4..51df153c9 100644 --- a/internal/cli/gateway_keys_cli_test.go +++ b/internal/cli/gateway_keys_cli_test.go @@ -208,6 +208,16 @@ func TestGatewayKeyLimitCLI(t *testing.T) { t.Error("accepted", args) } } + // month ends in an h, and is the month, not a count of hours + if got, err = call("limit", id, "month", "--cost", "20"); err != nil || !strings.Contains(got, "this month") { + t.Fatal(got, err) + } + if keys, _ = access.List(); keys[0].Limit == nil || keys[0].Limit.Period != "month" || keys[0].Limit.Cost != 20 { + t.Fatalf("limit kept as %+v", keys[0].Limit) + } + if got, err = call("limit", id, "add", "week", "--tokens", "1m"); err != nil || !strings.Contains(got, "Limit 2") { + t.Fatal(got, err) + } if got, err := call("limit", id, "off"); err != nil || !strings.Contains(got, "no limit") { t.Fatal(got, err) }