def attempt(
    binary,
    auth,
    private,
    evidence,
    job,
    repository,
    journal,
    barrier=None,
    *,
    preflight=False,
):
    evidence.mkdir()
    c, work = client(binary, auth, private, evidence / "transport", config_text=CONFIG)
    result = {
        "model": job["model"],
        "effort": "high",
        "dispatch_attempted": False,
        "acknowledged": False,
        "terminal_observed": False,
        "error": None,
        "output": None,
        "usage": None,
        "measured_bill": None,
        "automatic_retry": False,
        "tool_budget_policy": TOOL_BUDGET_POLICY,
        "model_work_budget_seconds": MODEL_WORK_SECONDS,
        "attempt_wall_budget_seconds": ATTEMPT_WALL_SECONDS,
    }
    thread_id = turn_id = None

    def bind(params):
        nonlocal turn_id
        if params.get("threadId") != thread_id or not params.get("turnId"):
            raise ValueError("repository event thread or turn missing")
        if turn_id and params["turnId"] != turn_id:
            raise ValueError("repository event turn differs")
        turn_id = params["turnId"]

    def event(message):
        method, params = message.get("method"), message.get("params", {})
        if method == "item/completed":
            bind(params)
            item = params.get("item", {})
            if item.get("type") not in (
                "agentMessage",
                "userMessage",
                "reasoning",
                "dynamicToolCall",
            ):
                raise ValueError("unconfigured tool activity")
            if item.get("type") == "agentMessage":
                result["output"] = item.get("text")
        elif method == "turn/completed":
            turn = params.get("turn", {})
            bind({"threadId": params.get("threadId"), "turnId": turn.get("id")})
            if result["terminal_observed"]:
                raise ValueError("duplicate terminal completion")
            result.update(terminal_observed=True, terminal=turn)
        elif method == "thread/tokenUsage/updated":
            result["usage"] = params.get("tokenUsage")

    def tool(message):
        params = message["params"]
        bind(params)
        if params.get("namespace") not in (None, ""):
            raise ValueError("unexpected repository tool namespace")
        args = params.get("arguments")
        if isinstance(args, str):
            args = json.loads(args)
        return invoke_repository_tool(c, repository, params["tool"], args)

    try:
        initialize(c)
        configured = configured_thread(c, work, job["model"])
        save(evidence / "effective-thread.json", configured)
        save(
            evidence / "requested-config.json",
            {"config_text": CONFIG, "tools": TOOLS, "instructions": INSTRUCTIONS},
        )
        thread_id = configured["thread"]["id"]
        if preflight:
            return result
        c.notify, c.server_request = event, tool
        if barrier:
            barrier.wait(timeout=60)
        if not journal.claim(job["id"], job["id"]):
            raise ValueError("duplicate model dispatch denied")
        c.journal(
            {
                "type": "dispatch_intent",
                "id": job["id"],
                "model": job["model"],
                "thread_id": thread_id,
                "at": time.time(),
                "automatic_retry": False,
            }
        )
        result.update(dispatch_attempted=True, started_at=time.time())
        started = time.monotonic()
        c.attempt_wall_deadline = min(
            started + ATTEMPT_WALL_SECONDS, started + job["deadline"] - time.time()
        )
        c.test_wait_seconds = 0.0
        c.work_deadline = min(started + MODEL_WORK_SECONDS, c.attempt_wall_deadline)
        c.dispatch_allowed = True
        reply = c.call(
            "turn/start",
            {
                "threadId": thread_id,
                "model": job["model"],
                "effort": "high",
                "environments": [],
                "input": [{"type": "text", "text": job["prompt"], "text_elements": []}],
            },
            timeout=30,
        )
        bind({"threadId": thread_id, "turnId": reply["turn"]["id"]})
        result["acknowledged"] = True
        while not result["terminal_observed"]:
            if c.receive() is not None:
                raise ValueError("unexpected provider response")
        if result["terminal"].get("status") != "completed":
            result["error"] = "unsuccessful_native_terminal"
        result["elapsed_seconds"] = time.monotonic() - started
    except BaseException as exc:
        result["error"] = type(exc).__name__ + ": " + str(exc)[:300]
        result["stop_reason"] = getattr(exc, "stop_reason", None)
        if barrier:
            barrier.abort()
        if result["dispatch_attempted"] and not result["terminal_observed"] and turn_id:
            try:
                c.work_deadline = time.monotonic() + 5
                c.call(
                    "turn/interrupt",
                    {"threadId": thread_id, "turnId": turn_id},
                    timeout=4,
                    cleanup=True,
                )
                result["interrupt_acknowledged"] = True
            except Exception:
                result["interrupt_acknowledged"] = False
    finally:
        result["finished_at"] = time.time()
        if result["dispatch_attempted"]:
            elapsed = time.monotonic() - started
            result["elapsed_seconds"] = elapsed
            result["test_wait_seconds"] = c.test_wait_seconds
            result["active_work_seconds"] = max(0.0, elapsed - c.test_wait_seconds)
        result["cleanup_confirmed"] = c.close()
        if not result["cleanup_confirmed"]:
            result["error"] = result["error"] or "provider_cleanup_unconfirmed"
        if repository is not None:
            try:
                patch = repository.delta()
                (evidence / "model.patch").write_text(patch)
            except Exception as exc:
                result["error"] = "final_patch_capture_failed: " + str(exc)[:200]
            result["repository_tool_calls"] = repository.calls
            result["repository_test_calls"] = repository.test_calls
        save(evidence / "result.json", result)
        if result["dispatch_attempted"]:
            journal.complete(job["id"], job["id"], result)
        if result["cleanup_confirmed"]:
            (private / "auth.json").unlink()
            save(
                evidence / "credential-cleanup.json",
                {"owned_copy_removed": True, "original_unchanged": True},
            )
    return result

