"""Smarty Pants starter kit for Python 3.8+. Standard library only, nothing to install.

Docs: https://smartypants.network/docs/agents

    from smartypants import SmartyPants
    sp = SmartyPants()                      # reads SMARTYPANTS_API_KEY
    print(sp.ask("Say hello in five words."))
    print(sp.balance())
    print(sp.run_code("python", "print(6 * 7)"))   # CPU jobs: our own account first, for now
    with sp.workspace(minutes=10) as ws:            # one VM, many commands (our own account first)
        ws.put_file("data.txt", "hello")
        print(ws.run("sh", "cat data.txt | wc -c")["stdout"])
    r = sp.coding_agent("Add a test for add() and make it pass", repo="https://github.com/you/repo")
    print(r["output"], r["diff"])                   # Claude Code in a workspace, on Smarty Pants models
    j = sp.job(repo="https://github.com/you/repo", setup="pip install -r requirements.txt", test="pytest -q")
    print(j["passed"], j["steps"][-1]["stdout"])    # one whole job: clone, set up, test, then the workspace ends

Run this file directly for a quick check:
    SMARTYPANTS_API_KEY=sp_live_... python3 smartypants.py

Already use the OpenAI SDK? Point it here instead:
    OpenAI(base_url="https://smartypants.network/v1", api_key=os.environ["SMARTYPANTS_API_KEY"])
"""

import base64
import json
import os
import random
import string
import time
import urllib.error
import urllib.parse
import urllib.request

DEFAULT_BASE_URL = "https://smartypants.network"
# No fixed model: the first call picks a ready text model from the live /v1/models list.
# Pass model="smartypants/..." to choose one yourself.
DEFAULT_MODEL = None
# The model coding_agent() gives Claude Code: tested live with tool calls (11-10, 8/8 tasks).
CODING_MODEL = "smartypants/qwen3.8-27b"

# Runs inside the workspace: install Claude Code, clone the repo, run the task, keep the diff.
CODING_AGENT_SCRIPT = r"""set -u
cd /work && . ./.sp-models.env
export DISABLE_TELEMETRY=1 CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 DISABLE_AUTOUPDATER=1 PYTHONDONTWRITEBYTECODE=1
export ANTHROPIC_MODEL=@MODEL@ ANTHROPIC_SMALL_FAST_MODEL=@MODEL@ CLAUDE_CODE_MAX_OUTPUT_TOKENS=2048
mkdir -p /work/.sp-agent && (cd /work/.sp-agent && npm install --no-audit --no-fund --loglevel=error @anthropic-ai/claude-code >&2) || exit 90
REPO=@REPO@
if [ -n "$REPO" ]; then git clone --depth 1 "$REPO" /work/repo >&2 || exit 91; else mkdir -p /work/repo && git -C /work/repo init -q; fi
cd /work/repo
/work/.sp-agent/node_modules/.bin/claude -p "$(cat /work/.sp-task.txt)" --tools 'Bash,Edit,Write,Read' \
  --allowedTools 'Bash Edit Write Read' --max-turns @TURNS@
code=$?
git add -A >/dev/null 2>&1; git diff --cached > /work/.sp-diff.patch 2>/dev/null
printf '\n===SP-AGENT-END===\n'
exit $code
"""


def _sh_quote(text):
    return "'" + str(text).replace("'", "'\\''") + "'"


def pick_text_model(models):
    """The text model to use when none is given: a ready one first, then a released (active) one."""
    text = [m for m in (models or []) if (m.get("kind") or "text") == "text"]
    chosen = next((m for m in text if m.get("is_ready")), None) or next((m for m in text if m.get("status") == "active"), None) or (text[0] if text else None)
    return chosen["id"] if chosen else None
_DONE = {"succeeded", "failed", "timed_out", "cancelled", "expired"}


class SmartyPantsError(Exception):
    def __init__(self, message, status=None, code=None, body=None):
        super().__init__(message)
        self.status = status
        self.code = code
        self.body = body


class SmartyPants:
    def __init__(self, api_key=None, base_url=None, model=DEFAULT_MODEL, timeout=120):
        self.api_key = api_key or os.environ.get("SMARTYPANTS_API_KEY") or os.environ.get("SMARTY_PANTS_API_KEY")
        self.base_url = (base_url or os.environ.get("SMARTYPANTS_BASE_URL") or os.environ.get("SMARTY_PANTS_BASE_URL") or DEFAULT_BASE_URL).rstrip("/")
        self.model = model
        self.timeout = timeout
        self._rpc_id = 0

    def request(self, method, path, body=None, auth=True, headers=None):
        if auth and not self.api_key:
            raise SmartyPantsError("No API key. Set SMARTYPANTS_API_KEY (create one at https://smartypants.network/api).")
        h = {"Accept": "application/json", "User-Agent": "smartypants-python-kit/1"}
        if body is not None:
            h["Content-Type"] = "application/json"
        if auth:
            h["Authorization"] = "Bearer " + self.api_key
        h.update(headers or {})
        data = None if body is None else json.dumps(body).encode()
        req = urllib.request.Request(self.base_url + path, data=data, method=method, headers=h)
        try:
            with urllib.request.urlopen(req, timeout=self.timeout) as resp:
                text = resp.read().decode()
                return json.loads(text) if text else None
        except urllib.error.HTTPError as e:
            text = e.read().decode(errors="replace")
            try:
                parsed = json.loads(text)
            except ValueError:
                parsed = text
            err = parsed.get("error", parsed) if isinstance(parsed, dict) else {}
            err = err if isinstance(err, dict) else {"message": str(err)}
            message = err.get("message") or err.get("error_description") or "HTTP %d" % e.code
            raise SmartyPantsError("%s %s failed (%d): %s" % (method, path, e.code, message),
                                   status=e.code, code=err.get("code") or err.get("type"), body=parsed) from None

    def models(self):
        """Every model with exact id, price per token and is_ready."""
        return self.request("GET", "/v1/models")["data"]

    def chat(self, messages, **options):
        """OpenAI-format chat completion. Extra OpenAI fields go in **options."""
        body = {"model": self.text_model(), "max_tokens": 512}
        body.update(options)
        body["messages"] = messages
        return self.request("POST", "/v1/chat/completions", body)

    def text_model(self):
        """The model chat() uses: the one you passed, or a ready text model from the live list (looked up once)."""
        if not self.model:
            self.model = pick_text_model(self.models())
            if not self.model:
                raise SmartyPantsError("No text model is offered right now. See https://smartypants.network/v1/models.")
        return self.model

    def ask(self, prompt, system=None, thinking=False, **options):
        """One prompt in, the answer text out. Thinking is off unless thinking=True."""
        messages = ([{"role": "system", "content": system}] if system else []) + [{"role": "user", "content": prompt}]
        options.setdefault("reasoning", {"enabled": thinking})
        reply = self.chat(messages, **options)
        return ((reply.get("choices") or [{}])[0].get("message") or {}).get("content") or ""

    def _rpc(self, method, params=None, path="/mcp/account"):
        self._rpc_id += 1
        payload = {"jsonrpc": "2.0", "id": self._rpc_id, "method": method}
        if params is not None:
            payload["params"] = params
        return self.request("POST", path, payload, auth=path != "/mcp",
                            headers={"Accept": "application/json, text/event-stream"})

    def mcp(self, tool, arguments=None, path="/mcp/account"):
        """Call a tool on the MCP server. Signed-in tools live at /mcp/account; the no-sign-in ones at /mcp."""
        rpc = self._rpc("tools/call", {"name": tool, "arguments": arguments or {}}, path)
        if rpc.get("error"):
            raise SmartyPantsError("MCP %s: %s" % (tool, rpc["error"].get("message")), code=rpc["error"].get("code"), body=rpc)
        result = rpc.get("result") or {}
        if result.get("isError"):
            raise SmartyPantsError("MCP %s: %s" % (tool, (result.get("content") or [{}])[0].get("text", "tool error")), body=rpc)
        if "structuredContent" in result:
            return result["structuredContent"]
        return json.loads((result.get("content") or [{}])[0].get("text") or "null")

    def mcp_tools(self, path="/mcp/account"):
        """Names of the MCP tools this key can use."""
        return [t["name"] for t in (self._rpc("tools/list", path=path).get("result") or {}).get("tools", [])]

    def balance(self):
        """Balance in US dollars: available_usd, free_credit_usd, spent_this_month_usd."""
        return self.mcp("get_balance")

    def add_credit(self, amount_usd):
        """A Stripe payment page for adding credit. This charges nothing: give the link to the
        person you work for, and only they pay it. Never handle card details. The balance
        updates about a minute after they pay."""
        return self.mcp("add_credit", {"amount_usd": amount_usd})

    def run_code(self, language, source, timeout_seconds=20, poll_seconds=5.0, wait_seconds=120):
        """Run a short program in a sealed Linux VM on a Mac (no network, wiped after).

        Open to our own account first; other accounts get
        403 CPU_PILOT_ACCOUNT_REQUIRED until it opens. language is "python", "node" or "sh";
        timeout_seconds 3 to 30. Returns a dict with id, status, exitCode, timedOut,
        stdout, stderr, stdoutTruncated, stderrTruncated and chargeMicros.
        """
        key = "kit-%d-%s" % (int(time.time()), "".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(8)))
        job = self.request("POST", "/v1/cpu/jobs", {"runtime": "code-v1", "language": language, "source": source,
                                                    "timeoutMs": int(timeout_seconds * 1000), "idempotencyKey": key})
        job_id, status = job["id"], job.get("status")
        quoted = urllib.parse.quote(job_id, safe="")
        deadline = time.time() + wait_seconds
        while status not in _DONE:
            if time.time() > deadline:
                raise SmartyPantsError("CPU job %s still %s after %d s" % (job_id, status, wait_seconds), code="WAIT_TIMEOUT")
            time.sleep(poll_seconds)
            status = self.request("GET", "/v1/cpu/jobs/" + quoted)["status"]
        out = {}
        if status in ("succeeded", "failed", "timed_out"):
            try:
                out = (self.request("GET", "/v1/cpu/jobs/%s/result" % quoted) or {}).get("result") or {}
            except SmartyPantsError:
                out = {}
        try:
            usage = self.request("GET", "/v1/cpu/jobs/%s/usage" % quoted)
        except SmartyPantsError:
            usage = None
        rows = usage if isinstance(usage, list) else ([usage] if usage else [])
        charges = [r["chargeMicros"] for r in rows if isinstance(r, dict) and isinstance(r.get("chargeMicros"), (int, float))]
        return {"id": job_id, "status": status, "exitCode": out.get("exitCode"), "timedOut": out.get("timedOut", status == "timed_out"),
                "stdout": out.get("stdout", ""), "stderr": out.get("stderr", ""),
                "stdoutTruncated": out.get("stdoutTruncated", False), "stderrTruncated": out.get("stderrTruncated", False),
                "chargeMicros": sum(charges) if charges else None}

    def workspace(self, minutes=10, wait_seconds=120, poll_seconds=1.0, inference_budget_usd=None, internet=None, copy_from=None, retry_of=None,
                  where=None):
        """Open a workspace: one sealed VM that keeps its files across many commands until it
        ends (at most 2 hours, 10 minutes idle, or when the Mac's owner comes back).
        Use it in a with block so it always ends. Our own account first, for now.
        inference_budget_usd gives it its own Smarty Pants key (ws.inference["apiKey"] and
        ws.inference["baseUrl"]) that spends at most that much and stops working when it ends.
        internet="listed" lets it reach the reviewed sites (GitHub, PyPI, npm, ...); none by default.
        copy_from="name" opens it as a copy of that saved workspace (ws.save(name) makes one).
        retry_of="id" (a lost workspace) prefers, for a minute, a Mac other than the one it ran on.
        where="mine" or {"group": id} runs it only on your own Macs or that Group's Macs, free."""
        return Workspace(self, minutes, wait_seconds, poll_seconds, inference_budget_usd, internet, copy_from, retry_of, where)

    def coding_agent(self, task, repo=None, model=CODING_MODEL, minutes=20, budget_usd=0.25,
                     timeout_seconds=600, max_turns=12, wait_seconds=180, on_output=None):
        """Run Claude Code on `task` in a fresh workspace, on Smarty Pants models, and return what it
        did. repo: a git URL to clone first (a private GitHub repo works when your account has a
        GitHub token saved; it never enters the VM). Model calls go on a key that can spend at most
        budget_usd, billed to this account with the workspace time. The workspace ends afterwards.
        Returns output (Claude Code's answer), diff (its changes, from git), exitCode, timedOut,
        inferenceUsage, workspaceId and chargeMicros (the workspace time)."""
        with self.workspace(minutes=minutes, wait_seconds=wait_seconds, inference_budget_usd=budget_usd,
                            internet="listed") as ws:
            ws.put_file(".sp-task.txt", task)
            script = CODING_AGENT_SCRIPT.replace("@MODEL@", _sh_quote(model)).replace(
                "@REPO@", _sh_quote(repo or "")).replace("@TURNS@", str(int(max_turns)))
            run = ws.run("sh", script, timeout_seconds=min(int(timeout_seconds), 600), on_output=on_output)
            text = run["stdout"]
            output, _, rest = text.partition("\n===SP-AGENT-END===\n")
            try:
                diff = ws.get_file(".sp-diff.patch").decode("utf-8", "replace")
            except SmartyPantsError:
                diff = ""
            usage = self.request("GET", ws._path + "/inference")
            result = {"output": output.strip(), "diff": diff, "exitCode": run["exitCode"], "timedOut": run["timedOut"],
                      "stderr": run["stderr"][-4000:], "inferenceUsage": usage, "workspaceId": ws.id}
        result["chargeMicros"] = None
        for _ in range(20):  # the Mac reports the end within a few seconds; the charge follows
            try:
                result["chargeMicros"] = ws.status().get("chargeMicros")
            except SmartyPantsError:
                break
            if result["chargeMicros"] is not None:
                break
            time.sleep(1)
        return result

    def job(self, repo="", upload=None, copy_from="", setup="", task="", test="", collect=(), model=CODING_MODEL, minutes=30, budget_usd=0.25,
            step_timeout_seconds=600, max_turns=12, wait_seconds=180, internet="listed", on_step=None, on_output=None, report=True,
            retries=0, on_retry=None, where=None):
        """Run a whole job in one fresh workspace, then end it: clone `repo` (or unpack `upload`, your own
        code as .tar.gz bytes, up to 64 MiB, which works with no internet), run `setup` (e.g. "npm ci"),
        let Claude Code do `task` on Smarty Pants models (optional), run `test` (e.g. "npm test"), and
        pack `collect` (paths or globs in the repo) into files.tar.gz. Every step can call Smarty Pants
        models through the workspace's own key, which spends at most budget_usd ($0.10 to $20; 0 gives
        no model key, and then no task). Keys and git tokens
        stay outside the VM. Stops at the first failed step, except that a failed test still returns
        the changes and files. Same result as the JavaScript kit's job(): passed, failedStep, exitCode,
        steps [{name, exitCode, timedOut, seconds, stdout, stderr}], diff, files {names, bytes} or None,
        inferenceUsage, workspaceId, chargeMicros, seconds.
        retries=N: save /work after each finished step; if the Mac is lost, open a copy of that save
        (on another Mac when there is one) and go on from the next step, up to N times. Adds
        retries, workspaceIds and checkpoint (the save's name) to the result."""
        if copy_from and (repo or upload):
            raise SmartyPantsError("give copy_from, repo or upload, not two of them", code="BAD_JOB")
        if task and not budget_usd:
            raise SmartyPantsError("a task needs a model budget (budget_usd)", code="BUDGET_NEEDED")
        started = time.time()
        steps = job_steps(repo=repo, upload=bool(upload and not repo), copy_from=copy_from, setup=setup, task=task, test=test, collect=collect, model=model, max_turns=max_turns)
        result = {"passed": False, "failedStep": None, "exitCode": None, "steps": [], "diff": "", "files": None,
                  "inferenceUsage": None, "retries": 0, "workspaceIds": []}
        checkpoint = "job-" + "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
        saved = False
        lost_ones = []

        def open_ws(**extra):
            w = self.workspace(minutes=minutes, wait_seconds=wait_seconds, inference_budget_usd=budget_usd or None,
                               internet=internet, where=where, **extra)
            result["workspaceId"] = w.id
            result["workspaceIds"].append(w.id)
            return w

        def send(w):
            if task:
                w.put_file(".sp-task.txt", task)
            if upload and not repo:
                w.put_file(".sp-upload.tar.gz", upload)

        ws = open_ws(copy_from=copy_from or None)
        try:
            send(ws)
            i = 0
            while i < len(steps):
                name, script = steps[i]
                if on_step:
                    on_step(name)
                t0 = time.time()
                try:
                    run = ws.run("sh", script, timeout_seconds=min(int(step_timeout_seconds), 600), on_output=on_output)
                except SmartyPantsError as error:
                    run = {"exitCode": None, "timedOut": error.code == "WAIT_TIMEOUT", "stdout": "", "stderr": str(error) + "\n"}
                if run["exitCode"] is None and not run["timedOut"] and result["retries"] < retries and _lost(ws):
                    result["steps"].append({"name": name, "exitCode": None, "timedOut": False, "lost": True,
                                            "seconds": round(time.time() - t0, 1), "stdout": "", "stderr": run["stderr"]})
                    result["retries"] += 1
                    lost_ones.append(ws)
                    ws.close()
                    lost_id = ws.id
                    ws = open_ws(retry_of=lost_id, copy_from=checkpoint if saved else (copy_from or None))
                    finished = []
                    if saved:
                        try:
                            finished = [n for n in ws.get_file(".sp-job/done").decode("utf-8", "replace").split("\n") if n]
                        except SmartyPantsError:
                            finished = []
                    else:
                        send(ws)
                    i = next((k for k, (n, _) in enumerate(steps) if n not in finished), len(steps))
                    if on_retry:
                        on_retry({"lostWorkspace": lost_id, "workspaceId": ws.id, "from": steps[i][0] if i < len(steps) else None,
                                  "retry": result["retries"]})
                    continue
                stdout = run["stdout"].partition("\n===SP-AGENT-END===\n")[0] if name == "task" else run["stdout"]
                result["steps"].append({"name": name, "exitCode": run["exitCode"], "timedOut": bool(run["timedOut"]),
                                        "seconds": round(time.time() - t0, 1), "stdout": stdout, "stderr": run["stderr"]})
                # save after each step worth not doing again (not after the test or the last step)
                if retries > 0 and run["exitCode"] == 0 and i < len(steps) - 1 and name != "test":
                    try:
                        ws.save(checkpoint)
                        saved = True
                    except SmartyPantsError:
                        pass
                if name == "test":
                    result["exitCode"] = run["exitCode"]
                if run["exitCode"] != 0 and result["failedStep"] is None:
                    result["failedStep"] = name
                    if name != "test":
                        result["exitCode"] = run["exitCode"]
                        break
                i += 1
            if result["exitCode"] is None and result["failedStep"] is None:
                result["exitCode"] = 0
            result["passed"] = result["failedStep"] is None
            if task:
                try:
                    result["diff"] = ws.get_file(".sp-diff.patch").decode("utf-8", "replace")
                except SmartyPantsError:
                    pass
            packed = [s for s in result["steps"] if s["name"] == "collect" and s["exitCode"] == 0]
            if packed:
                try:
                    data = ws.get_file(".sp-job/files.tar.gz")
                except SmartyPantsError:
                    data = None
                result["files"] = {"names": [n for n in packed[0]["stdout"].split("\n") if n], "bytes": data}
            try:
                result["inferenceUsage"] = self.request("GET", ws._path + "/inference")
            except SmartyPantsError:
                pass
        finally:
            ws.close()
        if saved:
            result["checkpoint"] = checkpoint
        result["chargeMicros"] = None
        for _ in range(20):  # the charge follows the Mac's end report
            try:
                result["chargeMicros"] = ws.status().get("chargeMicros")
            except SmartyPantsError:
                break
            if result["chargeMicros"] is not None:
                break
            time.sleep(1)
        for old in lost_ones:  # the lost workspaces were billed for the time they ran
            try:
                c = old.status().get("chargeMicros") or 0
            except SmartyPantsError:
                c = 0
            if result["chargeMicros"] is not None:
                result["chargeMicros"] += c
        result["seconds"] = round(time.time() - started, 1)
        if report:  # the job scoreboard: how it went, for cost per successful job, finish times and recovery
            size = lambda x: len(x.encode("utf-8")) if isinstance(x, str) else len(x or b"")
            try:
                self.request("POST", ws._path + "/job", {
                    "passed": result["passed"], "failedStep": result["failedStep"], "seconds": result["seconds"], "retries": result["retries"],
                    "earlier": result["workspaceIds"][:-1],
                    "steps": [{"name": s["name"], "exitCode": s["exitCode"], "seconds": s["seconds"]} for s in result["steps"]],
                    "source": "kit", "bytesUp": size(task) + (len(upload) if upload and not repo else 0),
                    "bytesDown": size(result["diff"]) + (len(result["files"]["bytes"] or b"") if result["files"] else 0)
                    + sum(size(s["stdout"]) + size(s["stderr"]) for s in result["steps"])})
            except SmartyPantsError:
                pass
        return result

    def save_setup(self, name, repo="", upload=None, setup="", minutes=30, step_timeout_seconds=600,
                   wait_seconds=180, internet="listed", on_step=None, on_output=None, where=None):
        """A saved setup: clone `repo` (or unpack `upload`), run `setup` once (installs, builds), and
        save the result as `name`. Later workspaces and jobs open as a copy of it (copy_from=name), so
        they start with everything installed. Run it again to refresh. Same result as saveSetup() in
        the JavaScript kit: name, steps, saved {files, bytes, sequence}, workspaceId."""
        if not (setup or repo or upload):
            raise SmartyPantsError("give repo, upload or setup", code="BAD_SETUP")
        steps = job_steps(repo=repo, upload=bool(upload and not repo), setup=setup)
        done = []
        with self.workspace(minutes=minutes, wait_seconds=wait_seconds, internet=internet, where=where) as ws:
            if upload and not repo:
                ws.put_file(".sp-upload.tar.gz", upload)
            for step, script in steps:
                if on_step:
                    on_step(step)
                run = ws.run("sh", script, timeout_seconds=min(int(step_timeout_seconds), 600), on_output=on_output)
                done.append({"name": step, "exitCode": run["exitCode"], "stdout": run["stdout"], "stderr": run["stderr"]})
                if run["exitCode"] != 0:
                    error = SmartyPantsError("setup step %s failed (exit %s)" % (step, run["exitCode"]), code="SETUP_FAILED")
                    error.steps = done
                    raise error
            if on_step:
                on_step("save")
            saved = ws.save(name)
            return {"name": name, "steps": done, "saved": saved, "workspaceId": ws.id}


# One job step: a shell script run in /work/repo; a finished step's name goes into /work/.sp-job/done.
def _lost(ws):
    """True when a workspace is gone (its Mac was lost, it ended or was stopped), not just slow."""
    try:
        return ws.status().get("status") != "ready"
    except SmartyPantsError:
        return True


# Kept the same as JOB_STEP_SCRIPT in smartypants.mjs.
JOB_STEP_SCRIPT = r"""mkdir -p /work/.sp-job /work/repo && cd /work/repo
[ -f /work/.sp-models.env ] && . /work/.sp-models.env
@BODY@
code=$?
[ "$code" = 0 ] && echo @NAME@ >> /work/.sp-job/done
exit $code
"""


def job_steps(repo="", upload=False, copy_from="", setup="", task="", test="", collect=(), model=CODING_MODEL, max_turns=12):
    """The (name, shell script) steps a job runs, in order. Same scripts as jobSteps() in smartypants.mjs."""
    steps = []
    def step(name, body):  # the first step starts the list of finished steps afresh
        steps.append((name, JOB_STEP_SCRIPT.replace("@BODY@", body if steps else ": > /work/.sp-job/done\n" + body).replace("@NAME@", name)))
    baseline = ("if [ ! -d .git ] && command -v git >/dev/null 2>&1; then git init -q . && git add -A && git -c user.name=job "
                "-c user.email=job@smartypants.network commit -qm start --allow-empty; fi")
    if repo:
        step("clone", "cd /work && rm -rf /work/repo && git clone --depth 1 %s /work/repo >&2" % _sh_quote(repo))
    elif upload:
        step("unpack", "tar xzf /work/.sp-upload.tar.gz -C /work/repo && rm -f /work/.sp-upload.tar.gz && " + baseline)
    elif copy_from:
        step("start", "[ -d /work/repo ] && " + baseline)
    else:
        step("start", baseline)
    if setup:
        step("setup", setup)
    if task:
        agent = CODING_AGENT_SCRIPT
        agent = agent[len("set -u\n"):] if agent.startswith("set -u\n") else agent
        agent = agent.replace("@MODEL@", _sh_quote(model)).replace("@REPO@", "''").replace("@TURNS@", str(int(round(max_turns))))
        if agent.endswith("\nexit $code\n"):
            agent = agent[:-len("exit $code\n")] + "(exit $code)"
        step("task", agent)
    if test:
        step("test", test)
    paths = [str(p).strip() for p in (collect.split(",") if isinstance(collect, str) else collect)]
    paths = [p for p in paths if p]
    if paths:
        step("collect", "rm -f /work/.sp-job/files.tar.gz; tar czf /work/.sp-job/files.tar.gz %s && tar tzf /work/.sp-job/files.tar.gz"
             % " ".join(paths))
    return steps


class Workspace:
    """One VM session. run() and put_file()/get_file() go to the guest one at a time."""

    def __init__(self, sp, minutes, wait_seconds, poll_seconds, inference_budget_usd=None, internet=None, copy_from=None, retry_of=None,
                 where=None):
        self.sp, self.poll_seconds = sp, poll_seconds
        body = {"seconds": int(minutes * 60)}
        if inference_budget_usd is not None:
            body["inference"] = {"budgetMicros": int(round(inference_budget_usd * 1e6))}
        if internet is not None:
            body["internet"] = internet
        if copy_from:
            body["from"] = copy_from
        if retry_of:
            body["retryOf"] = retry_of
        if where is not None:
            body["where"] = where
        session = sp.request("POST", "/v1/cpu/sessions", body)
        self.id = session["id"]
        self.inference = session.get("inference")
        self._path = "/v1/cpu/sessions/" + urllib.parse.quote(self.id, safe="")
        deadline = time.time() + wait_seconds
        while session["status"] == "queued":
            if time.time() > deadline:
                self.close()
                raise SmartyPantsError("workspace %s still waiting for a Mac after %d s" % (self.id, wait_seconds), code="WAIT_TIMEOUT")
            time.sleep(poll_seconds)
            session = self.status()
        if session["status"] != "ready":
            raise SmartyPantsError("workspace %s ended: %s" % (self.id, session.get("reason")), code=session.get("reason"))
        while (session.get("copy") or {}).get("state") in ("waiting", "copying"):
            if time.time() > deadline + 600:
                self.close()
                raise SmartyPantsError("workspace %s still copying %s" % (self.id, copy_from), code="WAIT_TIMEOUT")
            time.sleep(poll_seconds)
            session = self.status()
        if (session.get("copy") or {}).get("state") == "failed":
            self.close()
            raise SmartyPantsError("workspace %s could not copy %s: %s" % (self.id, copy_from, session["copy"].get("error")),
                                   code=session["copy"].get("error"))

    def __enter__(self):
        return self

    def __exit__(self, *_):
        self.close()

    def status(self):
        return self.sp.request("GET", self._path)

    def receipt(self):
        """What it cost: ready seconds, rate, workspace + model spend (an estimate while it is open)."""
        return self.sp.request("GET", self._path + "/receipt")

    def close(self):
        """End the workspace. Its VM, files and disk are destroyed."""
        try:
            return self.sp.request("DELETE", self._path)
        except SmartyPantsError:
            return None

    def _wait(self, request_id, wait_seconds, on_output=None):
        offsets = {"stdout": 0, "stderr": 0}
        deadline = time.time() + wait_seconds
        while True:
            r = self.sp.request("GET", "%s/requests/%d?stdoutOffset=%d&stderrOffset=%d"
                                % (self._path, request_id, offsets["stdout"], offsets["stderr"]))
            for stream in ("stdout", "stderr"):
                part = r.get(stream)
                if part:
                    if on_output and part["text"]:
                        on_output(stream, part["text"])
                    offsets[stream] = part["nextOffset"]
            if r["done"]:
                if r.get("error"):
                    raise SmartyPantsError("workspace request %d failed: %s" % (request_id, r["error"]), code=r["error"])
                return r
            if time.time() > deadline:
                raise SmartyPantsError("workspace request %d still running after %d s" % (request_id, wait_seconds), code="WAIT_TIMEOUT")
            time.sleep(self.poll_seconds)

    def save(self, name, wait_seconds=600):
        """Save /work under `name` (keeps its last 2 saves); a later workspace can start as a copy
        with copy_from=name."""
        job = self.sp.request("POST", self._path + "/save", {"name": name})
        deadline = time.time() + wait_seconds
        while job["state"] == "running":
            if time.time() > deadline:
                raise SmartyPantsError("save %s still running after %d s" % (name, wait_seconds), code="WAIT_TIMEOUT")
            time.sleep(self.poll_seconds)
            job = self.sp.request("GET", "/v1/cpu/saved/jobs/" + urllib.parse.quote(job["id"], safe=""))
        if job["state"] != "done":
            raise SmartyPantsError("save %s failed: %s" % (name, job.get("error")), code=job.get("error"))
        return job["result"]

    def run(self, language, source, timeout_seconds=60, on_output=None):
        """Run a program ("python", "node" or "sh") in the workspace folder. on_output(stream, text)
        gets output as it arrives. Returns exitCode, timedOut, stdout and stderr (4 KiB each)."""
        rid = self.sp.request("POST", self._path + "/exec", {"language": language, "source": source,
                                                              "timeoutSeconds": int(timeout_seconds)})["requestId"]
        out = {"stdout": "", "stderr": ""}
        def keep(stream, text):
            out[stream] += text
            if on_output:
                on_output(stream, text)
        r = self._wait(rid, timeout_seconds + 30, keep)
        return {"exitCode": r.get("exitCode"), "timedOut": r.get("timedOut"), **out}

    PIECE = 256 * 1024  # larger files move in pieces of this size, a few at a time
    AHEAD = 4

    def put_file(self, path, content):
        """Write a file (text or bytes, up to 64 MiB) at a relative path in the workspace."""
        data = content.encode("utf-8") if isinstance(content, str) else bytes(content)
        if len(data) <= 8 * 1024:
            rid = self.sp.request("POST", self._path + "/files",
                                  {"path": path, "contentBase64": base64.b64encode(data).decode("ascii")})["requestId"]
            self._wait(rid, 60)
            return
        waiting = []
        for offset in range(0, len(data), self.PIECE):
            piece = base64.b64encode(data[offset:offset + self.PIECE]).decode("ascii")
            waiting.append(self.sp.request("POST", self._path + "/files",
                                           {"path": path, "offset": offset, "contentBase64": piece})["requestId"])
            if len(waiting) >= self.AHEAD:
                self._wait(waiting.pop(0), 120)
        for rid in waiting:
            self._wait(rid, 120)

    def get_file(self, path):
        """Read a file (up to 64 MiB) from the workspace, as bytes."""
        quoted = urllib.parse.quote(path, safe="/")
        rid = self.sp.request("GET", self._path + "/files?path=" + quoted)["requestId"]
        try:
            return base64.b64decode(self._wait(rid, 60).get("contentBase64", ""))
        except SmartyPantsError as error:
            if error.code != "TOO_LARGE":
                raise
        piece = lambda offset: self.sp.request("GET", "%s/files?path=%s&offset=%d" % (self._path, quoted, offset))["requestId"]
        first = self._wait(piece(0), 120)
        size, parts = first["size"], [base64.b64decode(first.get("contentBase64", ""))]
        offsets = list(range(self.PIECE, size, self.PIECE))
        waiting = []
        while offsets or waiting:
            while offsets and len(waiting) < self.AHEAD:
                waiting.append(piece(offsets.pop(0)))
            parts.append(base64.b64decode(self._wait(waiting.pop(0), 120).get("contentBase64", "")))
        return b"".join(parts)


if __name__ == "__main__":
    sp = SmartyPants()
    print("Balance:", sp.balance())
    print("Answer:", sp.ask("Name three planets, comma separated.", max_tokens=40))
