import re

# Each task: the agent already made one call that violates a constraint the tool spec does not
# mention. We vary ONLY the error text the tool returns, then grade the agent's retry.
# kind = "conv": the constraint follows a widespread convention (guessable in principle)
# kind = "biz":  the constraint is a business rule of this particular system (not guessable)

ISO = re.compile(r"^\d{4}-\d{2}-\d{2}$")

TASKS = [
  # ---- conventional ----
  dict(id="date", kind="conv", tool="create_event",
       spec="create_event(title: string, date: string)",
       request="Put 'Team sync' on my calendar for November 24, 2026.",
       bad={"title": "Team sync", "date": "11/24/2026"}, field="date",
       native="date must be ISO 8601 YYYY-MM-DD; got '11/24/2026'",
       check=lambda a: a.get("date") == "2026-11-24"),
  dict(id="country", kind="conv", tool="create_address",
       spec="create_address(street: string, city: string, country: string)",
       request="Save my address: 1 Main St, Toronto, Canada.",
       bad={"street": "1 Main St", "city": "Toronto", "country": "Canada"}, field="country",
       native="country must be an ISO 3166-1 alpha-2 code; got 'Canada'",
       check=lambda a: a.get("country") == "CA"),
  dict(id="phone", kind="conv", tool="add_contact",
       spec="add_contact(name: string, phone: string)",
       request="Add Ana, her US number is (415) 555-0132.",
       bad={"name": "Ana", "phone": "(415) 555-0132"}, field="phone",
       native="phone must be E.164 (+ country code, digits only); got '(415) 555-0132'",
       check=lambda a: a.get("phone") == "+14155550132"),
  dict(id="bool", kind="conv", tool="set_notifications",
       spec="set_notifications(email: boolean)",
       request="Turn on email notifications.",
       bad={"email": "yes"}, field="email",
       native="email must be a JSON boolean; got string 'yes'",
       check=lambda a: a.get("email") is True),
  dict(id="time24", kind="conv", tool="set_alarm",
       spec="set_alarm(time: string)",
       request="Set an alarm for 7:30 in the evening.",
       bad={"time": "7:30 PM"}, field="time",
       native="time must be 24-hour HH:MM; got '7:30 PM'",
       check=lambda a: a.get("time") == "19:30"),
  dict(id="lang", kind="conv", tool="set_language",
       spec="set_language(language: string)",
       request="Switch my interface to Japanese.",
       bad={"language": "Japanese"}, field="language",
       native="language must be an ISO 639-1 code; got 'Japanese'",
       check=lambda a: a.get("language") == "ja"),
  dict(id="currency", kind="conv", tool="create_price",
       spec="create_price(product: string, unit_amount: number, currency: string)",
       request="Price the 'Pro plan' at 20 euros.",
       bad={"product": "Pro plan", "unit_amount": 20, "currency": "euro"}, field="currency",
       native="currency must be an ISO 4217 code; got 'euro'",
       check=lambda a: str(a.get("currency", "")).lower() == "eur"),
  dict(id="color", kind="conv", tool="set_theme",
       spec="set_theme(accent: string)",
       request="Make the accent color pure red.",
       bad={"accent": "red"}, field="accent",
       native="accent must be a hex color #RRGGBB; got 'red'",
       check=lambda a: str(a.get("accent", "")).upper() == "#FF0000"),
  # ---- business rules ----
  dict(id="cents", kind="biz", tool="charge",
       spec="charge(customer_id: string, amount: number)",
       request="Charge customer c_42 $19.99.",
       bad={"customer_id": "c_42", "amount": 19.99}, field="amount",
       native="amount must be an integer number of cents; got 19.99",
       check=lambda a: a.get("amount") == 1999),
  dict(id="reason", kind="biz", tool="refund",
       spec="refund(order_id: string, reason: string)",
       request="Refund order o_7, the customer was charged twice.",
       bad={"order_id": "o_7", "reason": "customer was charged twice"}, field="reason",
       native="reason must be one of DUPLICATE, FRAUD, REQUESTED_BY_CUSTOMER; got free text",
       check=lambda a: a.get("reason") == "DUPLICATE"),
  dict(id="sku", kind="biz", tool="add_to_cart",
       spec="add_to_cart(sku: string, qty: integer)",
       request="Add 2 of item 88120 to the cart.",
       bad={"sku": "88120", "qty": 2}, field="sku",
       native="sku must be 'SKU-' followed by the item number; got '88120'",
       check=lambda a: a.get("sku") == "SKU-88120" and a.get("qty") == 2),
  dict(id="qty", kind="biz", tool="place_order",
       spec="place_order(sku: string, qty: integer)",
       request="Order 120 units of SKU-5 — split across orders if you need to; just make the first call now.",
       bad={"sku": "SKU-5", "qty": 120}, field="qty",
       native="qty must be between 1 and 50 per order; got 120",
       check=lambda a: isinstance(a.get("qty"), int) and 1 <= a["qty"] <= 50),
  dict(id="duration", kind="biz", tool="book_room",
       spec="book_room(room: string, minutes: integer)",
       request="Book room A for 40 minutes, or the nearest allowed length that still covers 40.",
       bad={"room": "A", "minutes": 40}, field="minutes",
       native="minutes must be a multiple of 15; got 39",
       check=lambda a: a.get("minutes") == 45),
  dict(id="priority", kind="biz", tool="file_ticket",
       spec="file_ticket(summary: string, priority: string)",
       request="File a ticket 'Login broken', it's urgent.",
       bad={"summary": "Login broken", "priority": "urgent"}, field="priority",
       native="priority must be one of P0, P1, P2, P3 (P0 = most urgent); got 'urgent'",
       check=lambda a: a.get("priority") == "P0"),
  dict(id="region", kind="biz", tool="create_bucket",
       spec="create_bucket(name: string, region: string)",
       request="Create bucket 'logs' in Frankfurt.",
       bad={"name": "logs", "region": "Frankfurt"}, field="region",
       native="region must be one of us-east, eu-central, ap-south; got 'Frankfurt'",
       check=lambda a: a.get("region") == "eu-central"),
  dict(id="name_len", kind="biz", tool="create_project",
       spec="create_project(name: string)",
       request="Create a project called 'Quarterly revenue forecasting dashboard'.",
       bad={"name": "Quarterly revenue forecasting dashboard"}, field="name",
       native="name must be at most 20 characters; got 39",
       check=lambda a: isinstance(a.get("name"), str) and 0 < len(a["name"]) <= 20),
]

def errors(t):
    return {
        "vague":   {"error": "invalid request"},
        "located": {"error": f"invalid value for field '{t['field']}'"},
        "native":  {"error": t["native"], "field": t["field"]},
    }
