"""
Solution: the code around an agent (Lab 09's harness.py, world.py and 03_harness.py, Exercise 5).
"""
import json

WRITE_TOOLS = {"issue_refund", "cancel_order", "update_address"}


def make_key(mode: str | None, run_id: str, call_id: str, tool: str, args: dict) -> str | None:
    if mode == "call_id":
        return f"{run_id}:{call_id}"
    if mode == "content" and tool in WRITE_TOOLS:
        return f"{run_id}:{tool}:{json.dumps(args, sort_keys=True)}"
    return None


class Shop:
    def __init__(self):
        self.refunds = []
        self.done = set()
        self.running = set()

    def start(self, key: str | None) -> str:
        if key and key in self.done:
            return "duplicate"
        if key and key in self.running:
            return "in_progress"
        if key:
            self.running.add(key)
        return "started"

    def finish(self, key: str | None, amount: float):
        self.refunds.append(amount)
        if key:
            self.done.add(key)
            self.running.discard(key)


def needs_person(tool: str, args: dict) -> bool:
    if tool == "cancel_order":
        return True
    return tool == "issue_refund" and float(args.get("amount", 0)) > 100
