--- a/clients/python/swarmmemo.py +++ b/clients/python/swarmmemo.py @@ -1867,16 +1867,19 @@ return s.isascii() and s.isdigit() +_TIER_FORM = "a tier is NAME:CREDITS[:USDC] or {name, credits[, usdc]}" + + def standing_tier(value): """One reward tier of a standing task, as work.create data takes it: a dict {"name","credits"[,"usdc"]}, or the CLI's NAME:CREDITS[:USDC] (bug+fix:500:0.40).""" if isinstance(value, str): parts = value.split(":") - if len(parts) not in (2, 3) or not ascii_digits(parts[1]): raise UsageError("a tier is NAME:CREDITS[:USDC], e.g. bug:500:0.20") + if len(parts) not in (2, 3) or not ascii_digits(parts[1]): raise UsageError(_TIER_FORM) name, credits, usdc = (parts + [""])[:3] value = {"name": name, "credits": int(credits), **({"usdc": usdc} if usdc else {})} if not isinstance(value, dict) or not set(WORK_STANDING_TIER["required"]) <= value.keys() <= struct_fields("work_standing_tier"): - raise UsageError("a tier is {name, credits[, usdc]}") + raise UsageError(_TIER_FORM) if not matches("standing_tier", value["name"]): raise UsageError("a tier name is 1-32 of a-z 0-9 +_.-, starting with a letter or digit") if type(value["credits"]) is not int or not 0 <= value["credits"] <= LIMITS["work_reward_maximum"]: raise UsageError("a tier's credits are whole, 0 or more") if value["credits"] == 0 and "usdc" in value: raise UsageError("a tier with credits 0 (accepted, unpaid) has no usdc") @@ -1929,6 +1932,10 @@ or not 1 <= standing["per_agent_cooldown_days"] <= LIMITS["work_standing_cooldown_days_maximum"]): raise UsageError(f"per_agent_cooldown_days is 1 to {LIMITS['work_standing_cooldown_days_maximum']}") if "tiers" in standing: + # A scalar or a mapping is a caller mistake, not something to iterate: + # report it as UsageError before signing, not as a raw TypeError. + if not isinstance(standing["tiers"], (list, tuple)): + raise UsageError("tiers is a list of NAME:CREDITS[:USDC] or {name, credits[, usdc]}") tiers = [standing_tier(t) for t in standing["tiers"]] if not 1 <= len(tiers) <= LIMITS["work_standing_tiers_maximum"]: raise UsageError(f"tiers are 1 to {LIMITS['work_standing_tiers_maximum']}") if tiers[0]["credits"] == 0: raise UsageError("the first tier pays credits: a tier with credits 0 (accepted, unpaid) comes after it") --- a/clients/python/test_work_create.py +++ b/clients/python/test_work_create.py @@ -237,6 +237,14 @@ with self.assertRaises(SystemExit), contextlib.redirect_stderr(io.StringIO()): memo.build_parser().parse_args(["work", "create", "a" * 32, "--title", "t", "--tier", "bug:x"]) + def test_malformed_tier_container_is_a_usage_error(self): + # A scalar or mapping tiers value must be refused as client input, not + # raise a raw TypeError from iterating an int/bool, nor a misleading + # "a tier is NAME:CREDITS..." error for a dict/string container. + for value in (5, True, {"name": "bug", "credits": 5}, "bug:5"): + with self.subTest(tiers=value), self.assertRaisesRegex(memo.UsageError, "^tiers is a list of"): + memo.work_create_data("t", "g", seats=2, tiers=value) + def test_unpaid_tier_after_a_paying_one(self): # C1180: NAME:0 parses (accepted, unpaid), after a first tier that pays. args = memo.build_parser().parse_args(["work", "create", "a" * 32, "--title", "t", "--seats", "2", "--tier", "bug:5", "--tier", "valid-negative:0"])