File size: 24,768 Bytes
803cb67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
"""
Container lifecycle management for per-task Docker isolation.

Each benchmark task runs in its own Docker container. The host-side orchestrator
(dataclaw/eval/run_batch.py) uses these helpers to start, configure, drive, and tear down
containers via the Docker CLI.
"""

from __future__ import annotations

import json
import logging
import os
import shutil
import subprocess
import tempfile
import time
from pathlib import Path
from typing import Any, Dict, List, Optional

try:
    from dotenv import load_dotenv
    load_dotenv()
except ImportError:
    pass
logger = logging.getLogger(__name__)

DOCKER_IMAGE = os.environ.get("DOCKER_IMAGE", "dataclaw:0.1.0")
TMP_WORKSPACE = os.environ.get("TMP_WORKSPACE", "/tmp_workspace")
GATEWAY_PORT = int(os.environ.get("GATEWAY_PORT", "3333"))
DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL", "")

OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY", "")
OPENCLAW_CUSTOM_BASE_URL = os.environ.get("OPENCLAW_CUSTOM_BASE_URL", "")
OPENCLAW_CUSTOM_API_KEY = os.environ.get("OPENCLAW_CUSTOM_API_KEY", "")
OPENCLAW_CUSTOM_MODEL_ID = os.environ.get("OPENCLAW_CUSTOM_MODEL_ID", "")
JUDGE_CUSTOM_BASE_URL = os.environ.get("JUDGE_CUSTOM_BASE_URL", "")
JUDGE_CUSTOM_API_KEY = os.environ.get("JUDGE_CUSTOM_API_KEY", "")
JUDGE_CUSTOM_MODEL_ID = os.environ.get("JUDGE_CUSTOM_MODEL_ID", "")
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY", "")
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
BRAVE_API_KEY = os.environ.get("BRAVE_API_KEY", "")

OPENCLAW_MODEL_CONTEXT_WINDOW = int(os.environ.get("OPENCLAW_MODEL_CONTEXT_WINDOW", "128000"))
OPENCLAW_MODEL_MAX_TOKENS = int(os.environ.get("OPENCLAW_MODEL_MAX_TOKENS", "16384"))
OPENCLAW_MODEL_COST_INPUT = float(os.environ.get("OPENCLAW_MODEL_COST_INPUT", "0"))
OPENCLAW_MODEL_COST_OUTPUT = float(os.environ.get("OPENCLAW_MODEL_COST_OUTPUT", "0"))
OPENCLAW_MODEL_COST_CACHE_READ = float(os.environ.get("OPENCLAW_MODEL_COST_CACHE_READ", "0"))
OPENCLAW_MODEL_COST_CACHE_WRITE = float(os.environ.get("OPENCLAW_MODEL_COST_CACHE_WRITE", "0"))

JUDGE_MODEL_CONTEXT_WINDOW = int(os.environ.get("JUDGE_MODEL_CONTEXT_WINDOW", "128000"))
JUDGE_MODEL_MAX_TOKENS = int(os.environ.get("JUDGE_MODEL_MAX_TOKENS", "16384"))
JUDGE_MODEL_COST_INPUT = float(os.environ.get("JUDGE_MODEL_COST_INPUT", "0"))
JUDGE_MODEL_COST_OUTPUT = float(os.environ.get("JUDGE_MODEL_COST_OUTPUT", "0"))
JUDGE_MODEL_COST_CACHE_READ = float(os.environ.get("JUDGE_MODEL_COST_CACHE_READ", "0"))
JUDGE_MODEL_COST_CACHE_WRITE = float(os.environ.get("JUDGE_MODEL_COST_CACHE_WRITE", "0"))


def remove_container(name: str) -> None:
    subprocess.run(["docker", "rm", "-f", name], capture_output=True, encoding="utf-8")


def start_container(task_id: str, extra_env: Optional[Dict[str, str]] = None) -> None:
    """Start a detached container for one task."""
    env_args: List[str] = []

    proxy_http = os.environ.get("HTTP_PROXY_INNER", "")
    proxy_https = os.environ.get("HTTPS_PROXY_INNER", "")
    if proxy_http or proxy_https:
        env_args += [
            "-e", f"http_proxy={proxy_http}",
            "-e", f"https_proxy={proxy_https}",
            "-e", f"HTTP_PROXY={proxy_http}",
            "-e", f"HTTPS_PROXY={proxy_https}",
        ]

    for key, value in (extra_env or {}).items():
        env_args += ["-e", f"{key}={value}"]

    cmd = [
        "docker", "run", "-d",
        "--name", task_id,
        *env_args,
        DOCKER_IMAGE,
        "/bin/bash", "-c", "tail -f /dev/null",
    ]
    logger.info("[%s] Starting container (image=%s)", task_id, DOCKER_IMAGE)
    r = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8")
    if r.returncode != 0:
        raise RuntimeError(f"Container startup failed:\n{r.stderr}")
    logger.info("[%s] Container started: %s", task_id, r.stdout.strip()[:12])


def setup_workspace(
    task_id: str,
    workspace_files: List[Dict[str, str]],
    assets_dir: Path,
) -> None:
    """Copy task workspace files into the container's /tmp_workspace."""
    subprocess.run(
        ["docker", "exec", task_id, "mkdir", "-p", TMP_WORKSPACE],
        capture_output=True, text=True, encoding="utf-8",
    )

    with tempfile.TemporaryDirectory() as staging:
        staging_path = Path(staging)
        for file_spec in workspace_files:
            source = assets_dir / file_spec["source"]
            dest_rel = file_spec["dest"]
            dest_local = staging_path / dest_rel
            dest_local.parent.mkdir(parents=True, exist_ok=True)
            if not source.exists():
                logger.error("[%s] Workspace file not found: %s", task_id, source)
                raise FileNotFoundError(f"Workspace file not found: {source}")
            shutil.copy2(str(source), str(dest_local))

        r = subprocess.run(
            ["docker", "cp", f"{staging}/.", f"{task_id}:{TMP_WORKSPACE}/"],
            capture_output=True, text=True, encoding="utf-8",
        )
        if r.returncode != 0:
            raise RuntimeError(f"Workspace copy failed:\n{r.stderr}")
    logger.info("[%s] Workspace files injected (%d files)", task_id, len(workspace_files))

    # Symlink OpenClaw workspace to TMP_WORKSPACE so tools can access files
    subprocess.run(
        ["docker", "exec", task_id, "/bin/bash", "-c",
         f"rm -rf /root/.openclaw/workspace && ln -s {TMP_WORKSPACE} /root/.openclaw/workspace"],
        capture_output=True, text=True, encoding="utf-8",
    )


def onboard_openclaw(task_id: str) -> None:
    """Run openclaw onboard inside the container with configured auth."""
    onboard_args = [
        "--non-interactive",
        "--accept-risk",
        "--skip-health",
        "--workspace", "/root/.openclaw/workspace",
        "--gateway-bind", "loopback",
        "--gateway-port", str(GATEWAY_PORT),
    ]

    if OPENCLAW_CUSTOM_BASE_URL:
        if not OPENCLAW_CUSTOM_API_KEY:
            raise ValueError(
                "OPENCLAW_CUSTOM_API_KEY is required when OPENCLAW_CUSTOM_BASE_URL is set"
            )
        onboard_args += [
            "--auth-choice", "custom-api-key",
            "--custom-base-url", OPENCLAW_CUSTOM_BASE_URL,
            "--custom-api-key", OPENCLAW_CUSTOM_API_KEY,
            "--custom-model-id", OPENCLAW_CUSTOM_MODEL_ID or DEFAULT_MODEL,
        ]
    elif OPENROUTER_API_KEY:
        onboard_args += ["--openrouter-api-key", OPENROUTER_API_KEY]

    if OPENAI_API_KEY:
        onboard_args += ["--openai-api-key", OPENAI_API_KEY]
    if ANTHROPIC_API_KEY:
        onboard_args += ["--anthropic-api-key", ANTHROPIC_API_KEY]
    if GEMINI_API_KEY:
        onboard_args += ["--gemini-api-key", GEMINI_API_KEY]

    cmd = ["docker", "exec", task_id, "openclaw", "onboard", *onboard_args]
    logger.info("[%s] Running openclaw onboard", task_id)
    r = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8")
    if r.returncode != 0:
        raise RuntimeError(f"OpenClaw onboard failed:\n{r.stderr}")
    logger.info("[%s] Onboard complete", task_id)

    _patch_streaming_usage_compat(task_id)
    _patch_main_model_capabilities(task_id)
    _patch_brave_web_search(task_id)


def _patch_main_model_capabilities(task_id: str) -> None:
    """Patch contextWindow, maxTokens, and cost for the main (agent) model.

    OpenClaw onboard assigns conservative defaults for unknown custom providers.
    This overwrites them with values from the OPENCLAW_MODEL_* environment variables.
    """
    config = _read_openclaw_config(task_id)
    if not config:
        return
    patched = False
    for _pname, pinfo in config.get("models", {}).get("providers", {}).items():
        for model in pinfo.get("models", []):
            model["contextWindow"] = OPENCLAW_MODEL_CONTEXT_WINDOW
            model["maxTokens"] = OPENCLAW_MODEL_MAX_TOKENS
            model["cost"] = {
                "input": OPENCLAW_MODEL_COST_INPUT,
                "output": OPENCLAW_MODEL_COST_OUTPUT,
                "cacheRead": OPENCLAW_MODEL_COST_CACHE_READ,
                "cacheWrite": OPENCLAW_MODEL_COST_CACHE_WRITE,
            }
            patched = True
    if patched:
        _write_openclaw_config(task_id, config)
        logger.info(
            "[%s] Patched main model capabilities (contextWindow=%d, maxTokens=%d)",
            task_id, OPENCLAW_MODEL_CONTEXT_WINDOW, OPENCLAW_MODEL_MAX_TOKENS,
        )


def _patch_brave_web_search(task_id: str) -> None:
    """Enable Brave as ``web_search`` provider when ``BRAVE_API_KEY`` is set.

    Matches OpenClaw canonical config:
    https://docs.openclaw.ai/tools/brave-search
    """
    api_key = (BRAVE_API_KEY or "").strip()
    if not api_key:
        return
    config = _read_openclaw_config(task_id)
    if not config:
        logger.warning("[%s] Cannot read openclaw.json; skip Brave web_search patch", task_id)
        return

    plugins = config.setdefault("plugins", {})
    entries = plugins.setdefault("entries", {})
    brave_entry = entries.setdefault("brave", {})
    brave_entry["enabled"] = True
    brave_cfg = brave_entry.setdefault("config", {})
    brave_cfg.setdefault("webSearch", {})["apiKey"] = api_key

    tools = config.setdefault("tools", {})
    web = tools.setdefault("web", {})
    search = web.setdefault("search", {})
    search["provider"] = "brave"
    search["maxResults"] = int(os.environ.get("BRAVE_WEB_SEARCH_MAX_RESULTS", "5"))
    search["timeoutSeconds"] = int(os.environ.get("BRAVE_WEB_SEARCH_TIMEOUT_SECONDS", "30"))

    _write_openclaw_config(task_id, config)
    logger.info("[%s] Patched OpenClaw config: web_search provider=brave", task_id)


def _patch_streaming_usage_compat(task_id: str) -> None:
    """Ensure all custom-provider models have ``compat.supportsUsageInStreaming: true``.

    OpenClaw defaults this flag to ``false`` for unrecognised providers, which
    prevents ``stream_options: {include_usage: true}`` from being sent in API
    requests, resulting in zero-value token usage in transcripts.
    """
    config = _read_openclaw_config(task_id)
    if not config:
        return
    patched = False
    for _pname, pinfo in config.get("models", {}).get("providers", {}).items():
        for model in pinfo.get("models", []):
            compat = model.get("compat")
            if compat is None:
                model["compat"] = {"supportsUsageInStreaming": True}
                patched = True
            elif not compat.get("supportsUsageInStreaming"):
                compat["supportsUsageInStreaming"] = True
                patched = True
    if patched:
        _write_openclaw_config(task_id, config)
        logger.info("[%s] Patched compat.supportsUsageInStreaming for custom providers", task_id)


def start_gateway(task_id: str, log_path: Path) -> subprocess.Popen:
    """Start the OpenClaw gateway in the background inside the container."""
    log_path.parent.mkdir(parents=True, exist_ok=True)
    log_file = log_path.open("w", encoding="utf-8")

    gateway_cmd = f"openclaw gateway run --bind loopback --port {GATEWAY_PORT}"
    exports: List[str] = []
    if OPENROUTER_API_KEY:
        exports.append(f"export OPENROUTER_API_KEY='{OPENROUTER_API_KEY}'")
    brave = (BRAVE_API_KEY or "").strip()
    if brave:
        exports.append(f"export BRAVE_API_KEY='{brave}'")
    if exports:
        gateway_cmd = " && ".join(exports) + " && " + gateway_cmd

    proc = subprocess.Popen(
        ["docker", "exec", task_id, "/bin/bash", "-c", gateway_cmd],
        stdout=log_file,
        stderr=subprocess.STDOUT,
        encoding="utf-8",
    )
    proc._log_file = log_file  # type: ignore[attr-defined]
    logger.info("[%s] Gateway starting (PID=%s)", task_id, proc.pid)

    time.sleep(2)
    return proc


def _read_openclaw_config(task_id: str) -> Dict[str, Any]:
    """Read and parse openclaw.json from the container."""
    r = subprocess.run(
        ["docker", "exec", task_id, "cat", "/root/.openclaw/openclaw.json"],
        capture_output=True, text=True, encoding="utf-8",
    )
    if r.returncode != 0:
        return {}
    try:
        return json.loads(r.stdout)
    except json.JSONDecodeError:
        return {}


def _write_openclaw_config(task_id: str, config: Dict[str, Any]) -> None:
    """Write openclaw.json back into the container via docker cp."""
    with tempfile.NamedTemporaryFile(
        "w", suffix=".json", delete=False, encoding="utf-8",
    ) as f:
        json.dump(config, f, indent=2, ensure_ascii=False)
        tmp_path = f.name
    try:
        subprocess.run(
            ["docker", "cp", tmp_path, f"{task_id}:/root/.openclaw/openclaw.json"],
            capture_output=True, text=True, encoding="utf-8", check=True,
        )
    finally:
        os.unlink(tmp_path)


def register_custom_provider(
    task_id: str, base_url: str, api_key: str, model_id: str,
) -> None:
    """Register an additional custom model provider in the container."""
    config = _read_openclaw_config(task_id)
    if not config:
        raise RuntimeError("Cannot read openclaw.json from container")

    from urllib.parse import urlparse
    hostname = urlparse(base_url).hostname or ""
    slug = f"custom-{hostname.replace('.', '-')}"

    providers = config.setdefault("models", {}).setdefault("providers", {})
    if slug not in providers:
        providers[slug] = {
            "baseUrl": base_url,
            "apiKey": api_key,
            "api": "openai-completions",
            "models": [],
        }
    existing_ids = {m["id"] for m in providers[slug].get("models", [])}
    if model_id not in existing_ids:
        providers[slug]["models"].append({
            "id": model_id,
            "name": f"{model_id} (Custom Provider)",
            "reasoning": False,
            "input": ["text"],
            "cost": {
                "input": JUDGE_MODEL_COST_INPUT,
                "output": JUDGE_MODEL_COST_OUTPUT,
                "cacheRead": JUDGE_MODEL_COST_CACHE_READ,
                "cacheWrite": JUDGE_MODEL_COST_CACHE_WRITE,
            },
            "compat": {"supportsUsageInStreaming": True},
            "contextWindow": JUDGE_MODEL_CONTEXT_WINDOW,
            "maxTokens": JUDGE_MODEL_MAX_TOKENS,
        })

    _write_openclaw_config(task_id, config)
    logger.info("[%s] Registered custom provider: %s/%s", task_id, slug, model_id)


def resolve_qualified_model(task_id: str, model: str) -> str:
    """Resolve a bare model name to its fully-qualified provider/model form
    by reading the live openclaw.json inside the container."""
    if "/" in model:
        return model
    config = _read_openclaw_config(task_id)
    if not config:
        logger.warning("[%s] Cannot read openclaw.json, using bare model name: %s",
                       task_id, model)
        return model
    providers = config.get("models", {}).get("providers", {})
    for provider_name, provider_info in providers.items():
        for m in provider_info.get("models", []):
            if m.get("id") == model:
                return f"{provider_name}/{model}"
    return model


def set_model(task_id: str, model: str) -> None:
    """Set the active model inside the container."""
    qualified = resolve_qualified_model(task_id, model)
    r = subprocess.run(
        ["docker", "exec", task_id, "/bin/bash", "-c",
         f"openclaw models set '{qualified}'"],
        capture_output=True, text=True, encoding="utf-8",
    )
    if r.returncode != 0:
        raise RuntimeError(f"Model setup failed:\n{r.stderr}")
    logger.info("[%s] Model set: %s", task_id, qualified)


def create_agent(task_id: str, agent_id: str, model: str) -> None:
    """Create an OpenClaw agent inside the container."""
    r = subprocess.run(
        ["docker", "exec", task_id, "openclaw", "agents", "add", agent_id,
         "--model", model, "--non-interactive",
         "--workspace", "/root/.openclaw/workspace"],
        capture_output=True, text=True, encoding="utf-8",
    )
    if r.returncode != 0:
        logger.warning("[%s] Agent creation returned %s: %s", task_id, r.returncode, r.stderr)


def run_agent_message(
    task_id: str,
    message: str,
    timeout_seconds: float,
    log_path: Path,
    agent_id: str = "main",
) -> subprocess.Popen:
    """Send a message to an agent inside the container (background).

    Uses docker cp + file read to bypass Windows command-line length limits.
    """
    log_path.parent.mkdir(parents=True, exist_ok=True)
    log_file = log_path.open("w", encoding="utf-8")

    fd, tmp_path = tempfile.mkstemp(suffix=".txt")
    with os.fdopen(fd, "w", encoding="utf-8") as f:
        f.write(message)

    subprocess.run(
        ["docker", "cp", tmp_path,
         f"{task_id}:/tmp/agent_prompt.txt"],
        capture_output=True, text=True, encoding="utf-8",
    )
    os.unlink(tmp_path)

    bash_cmd = (
        f"prompt=$(cat /tmp/agent_prompt.txt) && "
        f"cd {TMP_WORKSPACE} && "
        f"openclaw agent --agent {agent_id} --session-id chat "
        f"--timeout {int(timeout_seconds)} --message \"$prompt\""
    )

    proc = subprocess.Popen(
        ["docker", "exec", task_id, "/bin/bash", "-c", bash_cmd],
        stdout=log_file,
        stderr=subprocess.STDOUT,
        encoding="utf-8",
    )
    proc._log_file = log_file  # type: ignore[attr-defined]
    logger.info("[%s] Agent message sent (PID=%s, timeout=%ds)", task_id, proc.pid, int(timeout_seconds))
    return proc


def run_judge_message(
    task_id: str,
    message: str,
    timeout_seconds: float = 180,
    judge_model: Optional[str] = None,
) -> Dict[str, Any]:
    """Run the LLM judge inside the container synchronously. Returns transcript entries."""
    if judge_model:
        subprocess.run(
            ["docker", "exec", task_id, "openclaw", "agents", "add", "judge",
             "--model", judge_model, "--non-interactive",
             "--workspace", "/root/.openclaw/workspace"],
            capture_output=True, text=True, encoding="utf-8",
        )

    fd, tmp_path = tempfile.mkstemp(suffix=".txt")
    try:
        with os.fdopen(fd, "w", encoding="utf-8") as f:
            f.write(message)

        cp_result = subprocess.run(
            ["docker", "cp", tmp_path,
             f"{task_id}:/tmp/judge_prompt.txt"],
            capture_output=True, text=True, encoding="utf-8",
        )
        if cp_result.returncode != 0:
            return {
                "stdout": "",
                "stderr": f"docker cp failed: {cp_result.stderr}",
                "exit_code": -1,
                "timed_out": False,
            }

        bash_cmd = (
            f"prompt=$(cat /tmp/judge_prompt.txt) && "
            f"cd {TMP_WORKSPACE} && "
            f"openclaw agent --agent judge --session-id judge_chat "
            f"--message \"$prompt\""
        )

        r = subprocess.run(
            ["docker", "exec", task_id, "/bin/bash", "-c", bash_cmd],
            capture_output=True, text=True, encoding="utf-8",
            timeout=timeout_seconds,
        )
        return {
            "stdout": r.stdout,
            "stderr": r.stderr,
            "exit_code": r.returncode,
            "timed_out": False,
        }
    except subprocess.TimeoutExpired:
        return {
            "stdout": "",
            "stderr": "Judge timed out",
            "exit_code": -1,
            "timed_out": True,
        }
    finally:
        try:
            os.unlink(tmp_path)
        except OSError:
            pass


def collect_transcript(task_id: str, output_dir: Path, agent_id: str = "main", output_filename: str = "") -> Path:
    """Copy the agent transcript from the container to the host."""
    output_dir.mkdir(parents=True, exist_ok=True)
    if output_filename:
        filename = output_filename
    else:
        filename = "chat.jsonl" if agent_id == "main" else f"{agent_id}_chat.jsonl"
    transcript_host = output_dir / filename

    transcript_container = f"/root/.openclaw/agents/{agent_id}/sessions/chat.jsonl"
    r = subprocess.run(
        ["docker", "cp", f"{task_id}:{transcript_container}", str(transcript_host)],
        capture_output=True, text=True, encoding="utf-8",
    )

    if r.returncode != 0:
        # Try to find transcript within the specific agent's directory only
        agent_dir = f"/root/.openclaw/agents/{agent_id}"
        find_cmd = f"find {agent_dir} -name '*.jsonl' -type f 2>/dev/null | head -5"
        find_r = subprocess.run(
            ["docker", "exec", task_id, "/bin/bash", "-c", find_cmd],
            capture_output=True, text=True, encoding="utf-8",
        )
        if find_r.stdout.strip():
            first_jsonl = find_r.stdout.strip().splitlines()[0]
            r2 = subprocess.run(
                ["docker", "cp", f"{task_id}:{first_jsonl}", str(transcript_host)],
                capture_output=True, text=True, encoding="utf-8",
            )
            if r2.returncode == 0:
                logger.info("[%s] Transcript found via fallback: %s", task_id, first_jsonl)
                return transcript_host

        logger.warning("[%s] Transcript not found for agent '%s': %s",
                       task_id, agent_id, r.stderr.strip())

    return transcript_host


def collect_output(task_id: str, output_dir: Path) -> None:
    """Collect all task output files from the container.

    Note: workspace and OpenClaw session data are NOT collected because they
    consist entirely of static input files (database/, md files) or duplicates
    of files already saved at the output_dir level (chat.jsonl, judge_chat.jsonl).
    """
    output_dir.mkdir(parents=True, exist_ok=True)
    logger.info("[%s] Task output collected to %s", task_id, output_dir)


def close_proc_log(proc: subprocess.Popen) -> None:
    """Close the log file handle created by run_agent_message / start_gateway."""
    log_file = getattr(proc, "_log_file", None)
    if log_file and not log_file.closed:
        log_file.close()


def detect_transcript_errors(transcript_path: Path) -> Optional[str]:
    """Check an OpenClaw transcript for API / provider errors.

    OpenClaw reports errors as assistant messages with stopReason="error"
    and an errorMessage field containing the details (e.g. 429, 504, etc.).
    This is the framework's own structured error reporting — no guessing.

    Returns an error description string if errors found, else None.
    """
    if not transcript_path or not transcript_path.exists():
        return None
    try:
        text = transcript_path.read_text(encoding="utf-8", errors="replace")
    except OSError:
        return None

    error_messages: List[str] = []
    for line in text.splitlines():
        line = line.strip()
        if not line:
            continue
        try:
            entry = json.loads(line)
        except json.JSONDecodeError:
            continue
        if entry.get("type") != "message":
            continue
        msg = entry.get("message", {})
        if msg.get("role") == "assistant" and msg.get("stopReason") == "error":
            em = msg.get("errorMessage", "unknown error")
            # Keep first line only (some errors contain full HTML pages)
            first_line = em.split("\n", 1)[0].strip()[:200]
            error_messages.append(first_line)

    if error_messages:
        sample = error_messages[0]
        return f"API error ({len(error_messages)} occurrence(s)): {sample}"
    return None


def extract_usage_from_jsonl(jsonl_path: Path) -> Dict[str, Any]:
    """Sum token usage and cost from all assistant messages in a transcript JSONL."""
    totals: Dict[str, Any] = {
        "input_tokens": 0,
        "output_tokens": 0,
        "cache_read_tokens": 0,
        "cache_write_tokens": 0,
        "total_tokens": 0,
        "cost_usd": 0.0,
        "request_count": 0,
    }
    if not jsonl_path.exists():
        return totals
    for line in jsonl_path.read_text(encoding="utf-8").splitlines():
        line = line.strip()
        if not line:
            continue
        try:
            entry = json.loads(line)
        except json.JSONDecodeError:
            continue
        if entry.get("type") != "message":
            continue
        msg = entry.get("message", {})
        if msg.get("role") != "assistant":
            continue
        totals["request_count"] += 1
        usage = msg.get("usage", {})
        totals["input_tokens"] += usage.get("input", 0)
        totals["output_tokens"] += usage.get("output", 0)
        totals["cache_read_tokens"] += usage.get("cacheRead", 0)
        totals["cache_write_tokens"] += usage.get("cacheWrite", 0)
        totals["total_tokens"] += usage.get("totalTokens", 0)
        cost = usage.get("cost", {})
        totals["cost_usd"] += cost.get("total", 0.0)
    totals["cost_usd"] = round(totals["cost_usd"], 6)
    return totals