roller-agent 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- roller/__init__.py +31 -0
- roller/__main__.py +6 -0
- roller/actf.py +431 -0
- roller/artifacts.py +124 -0
- roller/assets/deepseek/journal.mjs +44 -0
- roller/assets/deepseek/roller.patch.yml +15 -0
- roller/assets/deepseek/runner.py +149 -0
- roller/assets/hermes/runner.py +322 -0
- roller/assets/openclaw_capture.py +32 -0
- roller/assets/openclaw_drain.mjs +21 -0
- roller/assets/opencode/journal.mjs +42 -0
- roller/assets/opencode/runner.py +203 -0
- roller/assets/pi/runner.mjs +160 -0
- roller/assets/pi/runner.py +93 -0
- roller/benches/__init__.py +73 -0
- roller/benches/base.py +306 -0
- roller/benches/grading.py +553 -0
- roller/benches/pinchbench.py +976 -0
- roller/benches/qwenclaw/__init__.py +49 -0
- roller/benches/qwenclaw/adapter.py +167 -0
- roller/benches/qwenclaw/grader_runtime.py +395 -0
- roller/benches/qwenclaw/report.py +49 -0
- roller/benches/qwenclaw/task_parser.py +181 -0
- roller/benches/qwenclaw/workspace.py +202 -0
- roller/benches/report.py +166 -0
- roller/benches/skillsbench.py +708 -0
- roller/benches/transcripts.py +102 -0
- roller/benches/wildclaw.py +569 -0
- roller/bridge_capture.py +73 -0
- roller/bundles.py +545 -0
- roller/capture.py +385 -0
- roller/capture_protocol.py +301 -0
- roller/capture_runtime.py +48 -0
- roller/capture_states.py +37 -0
- roller/claude_capture.py +185 -0
- roller/claude_code_actf.py +594 -0
- roller/cli.py +1732 -0
- roller/codex_actf.py +584 -0
- roller/codex_capture.py +99 -0
- roller/codex_tools.py +137 -0
- roller/collector.py +818 -0
- roller/compaction_sft.py +672 -0
- roller/conversation.py +432 -0
- roller/deepseek_actf.py +135 -0
- roller/harness_registry.py +61 -0
- roller/harnesses/__init__.py +29 -0
- roller/harnesses/base.py +64 -0
- roller/harnesses/bridge.py +448 -0
- roller/harnesses/claude_code.py +839 -0
- roller/harnesses/codex.py +808 -0
- roller/harnesses/config_overrides.py +100 -0
- roller/harnesses/container.py +206 -0
- roller/harnesses/deepseek.py +104 -0
- roller/harnesses/docker.py +72 -0
- roller/harnesses/gateway_ports.py +155 -0
- roller/harnesses/hermes.py +119 -0
- roller/harnesses/openclaw.py +1605 -0
- roller/harnesses/opencode.py +193 -0
- roller/harnesses/pi.py +111 -0
- roller/hermes_actf.py +147 -0
- roller/image_build.py +206 -0
- roller/integrity.py +332 -0
- roller/model_protocols.py +201 -0
- roller/models.py +169 -0
- roller/openclaw_capture.py +140 -0
- roller/openclaw_delivery.py +211 -0
- roller/openclaw_usage.py +59 -0
- roller/opencode_actf.py +305 -0
- roller/opencode_capture.py +96 -0
- roller/pi_actf.py +172 -0
- roller/response_metadata.py +89 -0
- roller/run_lock.py +297 -0
- roller/sft.py +471 -0
- roller/storage.py +53 -0
- roller/tasks.py +689 -0
- roller/thinking.py +50 -0
- roller/thinking_capabilities.py +152 -0
- roller/trajectory_audit.py +386 -0
- roller/usage.py +168 -0
- roller/version.py +1 -0
- roller/viewer/__init__.py +3 -0
- roller/viewer/app.py +722 -0
- roller/viewer/static/app.js +752 -0
- roller/viewer/static/index.html +105 -0
- roller/viewer/static/style.css +213 -0
- roller/wire_trace.py +650 -0
- roller_agent-0.2.0.dist-info/METADATA +397 -0
- roller_agent-0.2.0.dist-info/RECORD +92 -0
- roller_agent-0.2.0.dist-info/WHEEL +5 -0
- roller_agent-0.2.0.dist-info/entry_points.txt +2 -0
- roller_agent-0.2.0.dist-info/licenses/LICENSE +201 -0
- roller_agent-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,839 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import logging
|
|
5
|
+
import os
|
|
6
|
+
import shlex
|
|
7
|
+
import shutil
|
|
8
|
+
import subprocess
|
|
9
|
+
import time
|
|
10
|
+
import uuid
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from datetime import datetime, timezone
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any, Mapping, Sequence
|
|
15
|
+
from urllib.parse import urlsplit, urlunsplit
|
|
16
|
+
|
|
17
|
+
from roller.artifacts import ArtifactCollectionResult
|
|
18
|
+
from roller.capture_runtime import CaptureRuntime
|
|
19
|
+
from roller.claude_code_actf import convert_claude_code_trace
|
|
20
|
+
from roller.harnesses.base import InteractiveHarness, InteractiveSession, TurnResult
|
|
21
|
+
from roller.harnesses.container import (
|
|
22
|
+
CONTEXT_CONTAINER_PATH,
|
|
23
|
+
ENV_NAME as ENV_NAME_RE,
|
|
24
|
+
INPUTS_CONTAINER_PATH,
|
|
25
|
+
OUTPUTS_CONTAINER_PATH,
|
|
26
|
+
PROXY_ENV_NAMES as COMMON_ENV_NAMES,
|
|
27
|
+
ContainerCommands,
|
|
28
|
+
build_provenance,
|
|
29
|
+
docker_run_args,
|
|
30
|
+
output_collection_metadata,
|
|
31
|
+
)
|
|
32
|
+
from roller.harnesses.docker import DockerRuntime, remove_container
|
|
33
|
+
from roller.models import HarnessResult, RunRequest
|
|
34
|
+
from roller.storage import safe_component
|
|
35
|
+
from roller.thinking import normalize_thinking, resolve_thinking
|
|
36
|
+
from roller.usage import ZERO_USAGE, extract_claude_code_usage
|
|
37
|
+
from roller.wire_trace import WireTraceProxy, redact_jsonl_file
|
|
38
|
+
|
|
39
|
+
logger = logging.getLogger(__name__)
|
|
40
|
+
|
|
41
|
+
DEFAULT_CLAUDE_CODE_IMAGE = "roller-claude-code:2.1.222"
|
|
42
|
+
CLAUDE_HOME_CONTAINER_PATH = "/roller/claude-home"
|
|
43
|
+
|
|
44
|
+
CLAUDE_PERMISSION_MODES = (
|
|
45
|
+
"default",
|
|
46
|
+
"acceptEdits",
|
|
47
|
+
"plan",
|
|
48
|
+
"auto",
|
|
49
|
+
"dontAsk",
|
|
50
|
+
"bypassPermissions",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
CLAUDE_EFFORT_MAP = {
|
|
54
|
+
"low": "low",
|
|
55
|
+
"medium": "medium",
|
|
56
|
+
"high": "high",
|
|
57
|
+
"xhigh": "xhigh",
|
|
58
|
+
"max": "max",
|
|
59
|
+
"ultra": "ultracode",
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def normalize_anthropic_base_url(value: str) -> str:
|
|
64
|
+
"""Normalize an Anthropic endpoint or API base to the SDK base URL."""
|
|
65
|
+
|
|
66
|
+
try:
|
|
67
|
+
parsed = urlsplit(value.strip())
|
|
68
|
+
parsed.port
|
|
69
|
+
except ValueError as exc:
|
|
70
|
+
raise ValueError("Anthropic API base must be an absolute http(s) URL") from exc
|
|
71
|
+
if parsed.scheme not in {"http", "https"} or not parsed.hostname:
|
|
72
|
+
raise ValueError("Anthropic API base must be an absolute http(s) URL")
|
|
73
|
+
path = parsed.path.rstrip("/")
|
|
74
|
+
if path.endswith("/v1/messages"):
|
|
75
|
+
path = path[: -len("/v1/messages")]
|
|
76
|
+
elif path.endswith("/v1"):
|
|
77
|
+
path = path[: -len("/v1")]
|
|
78
|
+
return urlunsplit((parsed.scheme, parsed.netloc, path, "", ""))
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def redact_anthropic_base_url(value: str) -> str:
|
|
82
|
+
"""Return a credential-free endpoint suitable for provenance metadata."""
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
parsed = urlsplit(value)
|
|
86
|
+
hostname = parsed.hostname or ""
|
|
87
|
+
port = parsed.port
|
|
88
|
+
except ValueError:
|
|
89
|
+
return "<invalid-url>"
|
|
90
|
+
netloc = hostname if port is None else f"{hostname}:{port}"
|
|
91
|
+
return urlunsplit((parsed.scheme, netloc, parsed.path, "", ""))
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def claude_effort(value: str | None) -> str | None:
|
|
95
|
+
"""Translate Roller's logical thinking level to Claude Code's ``--effort``."""
|
|
96
|
+
|
|
97
|
+
normalized = normalize_thinking(value, field="Claude Code reasoning effort")
|
|
98
|
+
if normalized is None:
|
|
99
|
+
return None
|
|
100
|
+
mapped = CLAUDE_EFFORT_MAP.get(normalized)
|
|
101
|
+
if mapped is None:
|
|
102
|
+
raise ValueError(
|
|
103
|
+
"Claude Code reasoning effort must be low|medium|high|xhigh|max|ultra; "
|
|
104
|
+
f"{normalized!r} has no exact Claude Code mapping"
|
|
105
|
+
)
|
|
106
|
+
return mapped
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
@dataclass(frozen=True)
|
|
110
|
+
class ClaudeCodeConfig:
|
|
111
|
+
docker_image: str = DEFAULT_CLAUDE_CODE_IMAGE
|
|
112
|
+
container_user: str | None = "0"
|
|
113
|
+
network: str | None = "host"
|
|
114
|
+
workspace_container_path: str = "/roller/workspace"
|
|
115
|
+
settings_path: Path | None = None
|
|
116
|
+
settings_fingerprint: str | None = None
|
|
117
|
+
api_base: str | None = None
|
|
118
|
+
api_key_env: str = "ANTHROPIC_API_KEY"
|
|
119
|
+
pass_env: tuple[str, ...] = COMMON_ENV_NAMES
|
|
120
|
+
reasoning_effort: str | None = None
|
|
121
|
+
max_thinking_tokens: int | None = None
|
|
122
|
+
max_turns: int | None = None
|
|
123
|
+
permission_mode: str = "bypassPermissions"
|
|
124
|
+
wire_trace_mode: str = "actf"
|
|
125
|
+
keep_thinking: bool = True
|
|
126
|
+
keep_container_on_error: bool = False
|
|
127
|
+
docker_binary: str = "docker"
|
|
128
|
+
capture_calls: bool = False
|
|
129
|
+
capture_debug: str = "on-error"
|
|
130
|
+
|
|
131
|
+
def __post_init__(self) -> None:
|
|
132
|
+
object.__setattr__(
|
|
133
|
+
self,
|
|
134
|
+
"reasoning_effort",
|
|
135
|
+
normalize_thinking(
|
|
136
|
+
self.reasoning_effort, field="Claude Code reasoning effort"
|
|
137
|
+
),
|
|
138
|
+
)
|
|
139
|
+
if self.api_base:
|
|
140
|
+
object.__setattr__(
|
|
141
|
+
self, "api_base", normalize_anthropic_base_url(self.api_base)
|
|
142
|
+
)
|
|
143
|
+
if not self.docker_image.strip():
|
|
144
|
+
raise ValueError("Claude Code docker image must be non-empty")
|
|
145
|
+
if self.container_user is not None and not self.container_user.strip():
|
|
146
|
+
raise ValueError("Claude Code container user must be non-empty or None")
|
|
147
|
+
if not self.workspace_container_path.startswith("/"):
|
|
148
|
+
raise ValueError("Claude Code workspace container path must be absolute")
|
|
149
|
+
if not ENV_NAME_RE.fullmatch(self.api_key_env):
|
|
150
|
+
raise ValueError(
|
|
151
|
+
f"invalid Claude Code API key environment name: {self.api_key_env!r}"
|
|
152
|
+
)
|
|
153
|
+
claude_effort(self.reasoning_effort)
|
|
154
|
+
if self.max_thinking_tokens is not None and self.max_thinking_tokens <= 0:
|
|
155
|
+
raise ValueError("Claude Code max thinking tokens must be greater than zero")
|
|
156
|
+
if self.max_turns is not None and self.max_turns <= 0:
|
|
157
|
+
raise ValueError("Claude Code max turns must be greater than zero")
|
|
158
|
+
if self.permission_mode not in CLAUDE_PERMISSION_MODES:
|
|
159
|
+
raise ValueError(
|
|
160
|
+
"Claude Code permission mode must be "
|
|
161
|
+
+ "|".join(CLAUDE_PERMISSION_MODES)
|
|
162
|
+
)
|
|
163
|
+
if self.wire_trace_mode not in {"off", "actf", "full"}:
|
|
164
|
+
raise ValueError("Claude Code wire trace mode must be off|actf|full")
|
|
165
|
+
if self.wire_trace_mode == "full" and not self.api_base:
|
|
166
|
+
raise ValueError("Claude Code --wire-trace full requires --api-base")
|
|
167
|
+
if self.capture_calls and (not self.api_base or self.network != "host"):
|
|
168
|
+
raise ValueError("Claude call capture requires --claude-api-base and --network host")
|
|
169
|
+
if self.capture_calls and self.wire_trace_mode == "off":
|
|
170
|
+
raise ValueError("Claude call capture requires wire tracing")
|
|
171
|
+
if (
|
|
172
|
+
self.wire_trace_mode != "off"
|
|
173
|
+
and self.api_base
|
|
174
|
+
and self.network != "host"
|
|
175
|
+
):
|
|
176
|
+
raise ValueError(
|
|
177
|
+
"Claude Code wire tracing requires --network host so the container "
|
|
178
|
+
"can reach the rollout-local proxy"
|
|
179
|
+
)
|
|
180
|
+
if self.settings_path is not None and not self.settings_path.is_file():
|
|
181
|
+
raise FileNotFoundError(
|
|
182
|
+
f"Claude Code settings file not found: {self.settings_path}"
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
class ClaudeCodeHarness(ContainerCommands, InteractiveHarness):
|
|
187
|
+
"""Run Claude Code in an isolated container and retain native session JSONL."""
|
|
188
|
+
|
|
189
|
+
name = "claude-code"
|
|
190
|
+
error_limit = 4000
|
|
191
|
+
|
|
192
|
+
def __init__(
|
|
193
|
+
self,
|
|
194
|
+
config: ClaudeCodeConfig,
|
|
195
|
+
worker_slot: int,
|
|
196
|
+
runtime: DockerRuntime | None = None,
|
|
197
|
+
) -> None:
|
|
198
|
+
self.config = config
|
|
199
|
+
self.worker_slot = worker_slot
|
|
200
|
+
self.runtime = runtime or DockerRuntime(config.docker_binary)
|
|
201
|
+
self._wire_proxy: WireTraceProxy | None = None
|
|
202
|
+
self.capture: CaptureRuntime | None = None
|
|
203
|
+
|
|
204
|
+
def create_session(
|
|
205
|
+
self,
|
|
206
|
+
request: RunRequest,
|
|
207
|
+
output_dir: Path,
|
|
208
|
+
attempt: int,
|
|
209
|
+
*,
|
|
210
|
+
collect_outputs: bool = True,
|
|
211
|
+
) -> InteractiveSession:
|
|
212
|
+
return ClaudeCodeSession(self, request, output_dir, attempt, collect_outputs=collect_outputs)
|
|
213
|
+
|
|
214
|
+
def _start_wire_proxy(self, output_dir: Path, attempt: int = 1) -> None:
|
|
215
|
+
self._wire_proxy = None
|
|
216
|
+
self.capture = None
|
|
217
|
+
if self.config.wire_trace_mode == "off" or not self.config.api_base:
|
|
218
|
+
return
|
|
219
|
+
log_path = output_dir / "wire_trace.jsonl"
|
|
220
|
+
log_path.unlink(missing_ok=True)
|
|
221
|
+
if self.config.capture_calls:
|
|
222
|
+
self.capture = CaptureRuntime(output_dir, attempt, "claude-code", self.config.capture_debug)
|
|
223
|
+
self._wire_proxy = WireTraceProxy(
|
|
224
|
+
self.config.api_base,
|
|
225
|
+
log_path,
|
|
226
|
+
mode="full" if self.config.wire_trace_mode == "full" or self.config.capture_calls else "tools",
|
|
227
|
+
capture_recorder=self.capture.recorder if self.capture else None,
|
|
228
|
+
# Claude Code 2.1.222 uses an SDK-internal ``?beta=true`` route.
|
|
229
|
+
# Anthropic-compatible gateways commonly implement the stable
|
|
230
|
+
# Messages path but reject this transport-only query flag.
|
|
231
|
+
drop_query_parameters=("beta",),
|
|
232
|
+
drop_request_headers=("anthropic-beta",),
|
|
233
|
+
)
|
|
234
|
+
self._wire_proxy.start()
|
|
235
|
+
|
|
236
|
+
def _stop_wire_proxy(self) -> None:
|
|
237
|
+
if self._wire_proxy is None:
|
|
238
|
+
return
|
|
239
|
+
proxy = self._wire_proxy
|
|
240
|
+
try:
|
|
241
|
+
proxy.stop()
|
|
242
|
+
capture = getattr(self, "capture", None)
|
|
243
|
+
recorder = capture.recorder if capture else None
|
|
244
|
+
if recorder is not None:
|
|
245
|
+
recorder.seal()
|
|
246
|
+
if proxy.log_path.is_file():
|
|
247
|
+
redact_jsonl_file(proxy.log_path)
|
|
248
|
+
finally:
|
|
249
|
+
self._wire_proxy = None
|
|
250
|
+
|
|
251
|
+
def _effective_effort(self, request: RunRequest) -> str | None:
|
|
252
|
+
selected = resolve_thinking(
|
|
253
|
+
request.task.thinking, self.config.reasoning_effort
|
|
254
|
+
)
|
|
255
|
+
return claude_effort(selected)
|
|
256
|
+
|
|
257
|
+
def _model_name(self, request: RunRequest) -> str:
|
|
258
|
+
if self.config.api_base:
|
|
259
|
+
return request.model
|
|
260
|
+
return request.model.rsplit("/", 1)[-1]
|
|
261
|
+
|
|
262
|
+
def _environment(self, request: RunRequest) -> dict[str, str]:
|
|
263
|
+
environment: dict[str, str] = {}
|
|
264
|
+
for name in self.config.pass_env:
|
|
265
|
+
if not ENV_NAME_RE.fullmatch(name):
|
|
266
|
+
raise ValueError(f"invalid environment variable name: {name!r}")
|
|
267
|
+
if os.environ.get(name):
|
|
268
|
+
environment[name] = os.environ[name]
|
|
269
|
+
for name in request.task.env_names:
|
|
270
|
+
if not ENV_NAME_RE.fullmatch(name):
|
|
271
|
+
raise ValueError(
|
|
272
|
+
f"invalid task environment variable name: {name!r}"
|
|
273
|
+
)
|
|
274
|
+
if name not in os.environ or os.environ[name] == "":
|
|
275
|
+
raise ValueError(
|
|
276
|
+
f"task {request.task.id!r} requires missing environment "
|
|
277
|
+
f"variable {name}"
|
|
278
|
+
)
|
|
279
|
+
environment[name] = os.environ[name]
|
|
280
|
+
|
|
281
|
+
api_key = os.environ.get(self.config.api_key_env)
|
|
282
|
+
if not api_key:
|
|
283
|
+
raise ValueError(
|
|
284
|
+
"Claude Code requires a non-empty API key in "
|
|
285
|
+
f"{self.config.api_key_env}"
|
|
286
|
+
)
|
|
287
|
+
environment["ANTHROPIC_API_KEY"] = api_key
|
|
288
|
+
api_base = (
|
|
289
|
+
self._wire_proxy.proxy_base_url
|
|
290
|
+
if self._wire_proxy is not None
|
|
291
|
+
else self.config.api_base
|
|
292
|
+
)
|
|
293
|
+
if api_base:
|
|
294
|
+
environment["ANTHROPIC_BASE_URL"] = api_base
|
|
295
|
+
model_name = self._model_name(request)
|
|
296
|
+
environment.update(
|
|
297
|
+
{
|
|
298
|
+
"ANTHROPIC_MODEL": model_name,
|
|
299
|
+
"CLAUDE_CONFIG_DIR": CLAUDE_HOME_CONTAINER_PATH,
|
|
300
|
+
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
|
|
301
|
+
"DISABLE_AUTOUPDATER": "1",
|
|
302
|
+
"IS_SANDBOX": "1",
|
|
303
|
+
}
|
|
304
|
+
)
|
|
305
|
+
if self.config.api_base:
|
|
306
|
+
environment.update(
|
|
307
|
+
{
|
|
308
|
+
"ANTHROPIC_DEFAULT_SONNET_MODEL": model_name,
|
|
309
|
+
"ANTHROPIC_DEFAULT_OPUS_MODEL": model_name,
|
|
310
|
+
"ANTHROPIC_DEFAULT_HAIKU_MODEL": model_name,
|
|
311
|
+
"CLAUDE_CODE_SUBAGENT_MODEL": model_name,
|
|
312
|
+
}
|
|
313
|
+
)
|
|
314
|
+
if self.config.max_thinking_tokens is not None:
|
|
315
|
+
environment["MAX_THINKING_TOKENS"] = str(
|
|
316
|
+
self.config.max_thinking_tokens
|
|
317
|
+
)
|
|
318
|
+
return environment
|
|
319
|
+
|
|
320
|
+
def _start_container(
|
|
321
|
+
self, container: str, request: RunRequest
|
|
322
|
+
) -> dict[str, str]:
|
|
323
|
+
environment = self._environment(request)
|
|
324
|
+
args = docker_run_args(self.name, container, self.config, environment, request)
|
|
325
|
+
args.extend(
|
|
326
|
+
[self.config.docker_image, "/bin/bash", "-c", "tail -f /dev/null"]
|
|
327
|
+
)
|
|
328
|
+
self._checked(args, "Claude Code container startup", timeout=60)
|
|
329
|
+
return environment
|
|
330
|
+
|
|
331
|
+
def _prepare_workspace(self, container: str, request: RunRequest) -> None:
|
|
332
|
+
workspace = self.config.workspace_container_path
|
|
333
|
+
output_root = f"{workspace}/{request.task.output_dir}"
|
|
334
|
+
if request.bundle is not None:
|
|
335
|
+
if request.bundle.legacy_workspace or request.task.input_mode == "copy":
|
|
336
|
+
setup = (
|
|
337
|
+
f"mkdir -p {shlex.quote(workspace)} && "
|
|
338
|
+
f"cp -a {INPUTS_CONTAINER_PATH}/. {shlex.quote(workspace)}/ && "
|
|
339
|
+
f"chmod -R u+w {shlex.quote(workspace)} && "
|
|
340
|
+
f"rm -rf {shlex.quote(workspace)}/context && "
|
|
341
|
+
f"ln -s {CONTEXT_CONTAINER_PATH} {shlex.quote(workspace)}/context"
|
|
342
|
+
)
|
|
343
|
+
else:
|
|
344
|
+
setup = f"mkdir -p {shlex.quote(workspace)}"
|
|
345
|
+
elif request.task.workspace is not None:
|
|
346
|
+
setup = (
|
|
347
|
+
f"mkdir -p {shlex.quote(workspace)} && "
|
|
348
|
+
f"cp -a /roller-input/. {shlex.quote(workspace)}/ && "
|
|
349
|
+
f"chmod -R u+w {shlex.quote(workspace)}"
|
|
350
|
+
)
|
|
351
|
+
else:
|
|
352
|
+
setup = f"mkdir -p {shlex.quote(workspace)}"
|
|
353
|
+
script = (
|
|
354
|
+
f"{setup} && rm -rf {shlex.quote(output_root)} && "
|
|
355
|
+
f"mkdir -p {shlex.quote(output_root)} {CLAUDE_HOME_CONTAINER_PATH} && "
|
|
356
|
+
f"mkdir -p /roller/task && rm -rf {OUTPUTS_CONTAINER_PATH} && "
|
|
357
|
+
f"ln -s {shlex.quote(output_root)} {OUTPUTS_CONTAINER_PATH}"
|
|
358
|
+
)
|
|
359
|
+
self._checked(
|
|
360
|
+
["exec", container, "/bin/bash", "-c", script],
|
|
361
|
+
"Claude Code workspace setup",
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
def _prepare_context_and_setup(
|
|
365
|
+
self, container: str, request: RunRequest
|
|
366
|
+
) -> None:
|
|
367
|
+
if request.bundle is not None:
|
|
368
|
+
workspace = shlex.quote(self.config.workspace_container_path)
|
|
369
|
+
script = (
|
|
370
|
+
f"cp -a {CONTEXT_CONTAINER_PATH}/. {workspace}/ && "
|
|
371
|
+
f"mkdir -p {CLAUDE_HOME_CONTAINER_PATH}/skills && "
|
|
372
|
+
f"if [ -d {CONTEXT_CONTAINER_PATH}/skills ]; then "
|
|
373
|
+
f"cp -a {CONTEXT_CONTAINER_PATH}/skills/. "
|
|
374
|
+
f"{CLAUDE_HOME_CONTAINER_PATH}/skills/; fi"
|
|
375
|
+
)
|
|
376
|
+
self._checked(
|
|
377
|
+
["exec", container, "/bin/bash", "-c", script],
|
|
378
|
+
"Claude Code context injection",
|
|
379
|
+
)
|
|
380
|
+
for command in request.task.setup_commands:
|
|
381
|
+
self._checked(
|
|
382
|
+
[
|
|
383
|
+
"exec",
|
|
384
|
+
"-w",
|
|
385
|
+
self.config.workspace_container_path,
|
|
386
|
+
container,
|
|
387
|
+
"/bin/bash",
|
|
388
|
+
"-lc",
|
|
389
|
+
command,
|
|
390
|
+
],
|
|
391
|
+
f"task setup command {command!r}",
|
|
392
|
+
)
|
|
393
|
+
|
|
394
|
+
def _prepare_settings(self, container: str) -> None:
|
|
395
|
+
self._checked(
|
|
396
|
+
[
|
|
397
|
+
"exec",
|
|
398
|
+
container,
|
|
399
|
+
"mkdir",
|
|
400
|
+
"-p",
|
|
401
|
+
CLAUDE_HOME_CONTAINER_PATH,
|
|
402
|
+
],
|
|
403
|
+
"Claude Code config directory setup",
|
|
404
|
+
)
|
|
405
|
+
if self.config.settings_path is None:
|
|
406
|
+
return
|
|
407
|
+
self._checked(
|
|
408
|
+
[
|
|
409
|
+
"cp",
|
|
410
|
+
str(self.config.settings_path),
|
|
411
|
+
f"{container}:{CLAUDE_HOME_CONTAINER_PATH}/settings.json",
|
|
412
|
+
],
|
|
413
|
+
"Claude Code settings copy",
|
|
414
|
+
)
|
|
415
|
+
|
|
416
|
+
def _claude_command(
|
|
417
|
+
self,
|
|
418
|
+
container: str,
|
|
419
|
+
request: RunRequest,
|
|
420
|
+
prompt: str,
|
|
421
|
+
*,
|
|
422
|
+
resume: bool,
|
|
423
|
+
) -> list[str]:
|
|
424
|
+
command = [
|
|
425
|
+
"exec",
|
|
426
|
+
"-e",
|
|
427
|
+
f"CLAUDE_CONFIG_DIR={CLAUDE_HOME_CONTAINER_PATH}",
|
|
428
|
+
"-w",
|
|
429
|
+
self.config.workspace_container_path,
|
|
430
|
+
container,
|
|
431
|
+
"claude",
|
|
432
|
+
"--verbose",
|
|
433
|
+
"--output-format=stream-json",
|
|
434
|
+
f"--permission-mode={self.config.permission_mode}",
|
|
435
|
+
"--model",
|
|
436
|
+
self._model_name(request),
|
|
437
|
+
]
|
|
438
|
+
if self.config.settings_path is not None:
|
|
439
|
+
command.extend(
|
|
440
|
+
["--settings", f"{CLAUDE_HOME_CONTAINER_PATH}/settings.json"]
|
|
441
|
+
)
|
|
442
|
+
if self.config.max_turns is not None:
|
|
443
|
+
command.extend(["--max-turns", str(self.config.max_turns)])
|
|
444
|
+
effort = self._effective_effort(request)
|
|
445
|
+
if effort:
|
|
446
|
+
command.extend(["--effort", effort])
|
|
447
|
+
if resume:
|
|
448
|
+
command.append("--continue")
|
|
449
|
+
command.extend(["--print", prompt])
|
|
450
|
+
return command
|
|
451
|
+
|
|
452
|
+
def _run_turn(
|
|
453
|
+
self,
|
|
454
|
+
container: str,
|
|
455
|
+
request: RunRequest,
|
|
456
|
+
prompt: str,
|
|
457
|
+
*,
|
|
458
|
+
resume: bool,
|
|
459
|
+
timeout_seconds: float,
|
|
460
|
+
log_path: Path,
|
|
461
|
+
) -> int:
|
|
462
|
+
process = self.runtime.popen(
|
|
463
|
+
self._claude_command(container, request, prompt, resume=resume),
|
|
464
|
+
log_path,
|
|
465
|
+
append=resume,
|
|
466
|
+
)
|
|
467
|
+
try:
|
|
468
|
+
return_code = process.wait(timeout=timeout_seconds)
|
|
469
|
+
except subprocess.TimeoutExpired as exc:
|
|
470
|
+
process.kill()
|
|
471
|
+
process.wait()
|
|
472
|
+
self.runtime.run(["exec", container, "pkill", "-f", "claude"])
|
|
473
|
+
raise TimeoutError("Claude Code turn timed out") from exc
|
|
474
|
+
finally:
|
|
475
|
+
self.runtime.close_log(process)
|
|
476
|
+
if return_code != 0:
|
|
477
|
+
detail = ""
|
|
478
|
+
try:
|
|
479
|
+
detail = log_path.read_text(
|
|
480
|
+
encoding="utf-8", errors="replace"
|
|
481
|
+
).strip()[-4000:]
|
|
482
|
+
except OSError:
|
|
483
|
+
pass
|
|
484
|
+
raise RuntimeError(
|
|
485
|
+
f"Claude Code exited with code {return_code}"
|
|
486
|
+
+ (f": {detail}" if detail else "")
|
|
487
|
+
)
|
|
488
|
+
return return_code
|
|
489
|
+
|
|
490
|
+
@staticmethod
|
|
491
|
+
def _combine_session_files(files: list[Path], destination: Path) -> None:
|
|
492
|
+
rows: list[tuple[str, int, dict[str, Any]]] = []
|
|
493
|
+
seen_uuids: set[str] = set()
|
|
494
|
+
sequence = 0
|
|
495
|
+
for path in files:
|
|
496
|
+
with path.open("r", encoding="utf-8", errors="replace") as stream:
|
|
497
|
+
for line in stream:
|
|
498
|
+
try:
|
|
499
|
+
event = json.loads(line)
|
|
500
|
+
except json.JSONDecodeError:
|
|
501
|
+
continue
|
|
502
|
+
if not isinstance(event, dict):
|
|
503
|
+
continue
|
|
504
|
+
event_uuid = event.get("uuid")
|
|
505
|
+
if isinstance(event_uuid, str) and event_uuid:
|
|
506
|
+
if event_uuid in seen_uuids:
|
|
507
|
+
continue
|
|
508
|
+
seen_uuids.add(event_uuid)
|
|
509
|
+
rows.append((str(event.get("timestamp", "")), sequence, event))
|
|
510
|
+
sequence += 1
|
|
511
|
+
rows.sort(key=lambda item: (item[0], item[1]))
|
|
512
|
+
with destination.open("w", encoding="utf-8") as stream:
|
|
513
|
+
for _, _, event in rows:
|
|
514
|
+
stream.write(json.dumps(event, ensure_ascii=False) + "\n")
|
|
515
|
+
|
|
516
|
+
def _collect_trace(
|
|
517
|
+
self, container: str, output_dir: Path, attempt: int
|
|
518
|
+
) -> Path | None:
|
|
519
|
+
rollout_dir = output_dir / "rollout"
|
|
520
|
+
rollout_dir.mkdir(parents=True, exist_ok=True)
|
|
521
|
+
staging_root = output_dir / ".claude-session-copy"
|
|
522
|
+
if staging_root.exists():
|
|
523
|
+
shutil.rmtree(staging_root)
|
|
524
|
+
staging_root.mkdir(parents=True, exist_ok=True)
|
|
525
|
+
try:
|
|
526
|
+
copied = self.runtime.run(
|
|
527
|
+
[
|
|
528
|
+
"cp",
|
|
529
|
+
f"{container}:{CLAUDE_HOME_CONTAINER_PATH}/projects/.",
|
|
530
|
+
str(staging_root),
|
|
531
|
+
],
|
|
532
|
+
timeout=60,
|
|
533
|
+
)
|
|
534
|
+
if copied.returncode != 0:
|
|
535
|
+
return None
|
|
536
|
+
primary_files = [
|
|
537
|
+
path
|
|
538
|
+
for path in staging_root.rglob("*.jsonl")
|
|
539
|
+
if "subagents" not in path.relative_to(staging_root).parts and path.is_file()
|
|
540
|
+
]
|
|
541
|
+
if not primary_files:
|
|
542
|
+
return None
|
|
543
|
+
primary = max(primary_files, key=lambda path: path.stat().st_mtime_ns)
|
|
544
|
+
sidechain_dir = primary.parent / primary.stem / "subagents"
|
|
545
|
+
files = [primary]
|
|
546
|
+
if sidechain_dir.is_dir():
|
|
547
|
+
files.extend(sorted(path for path in sidechain_dir.rglob("*.jsonl") if not path.is_symlink()))
|
|
548
|
+
if self.config.capture_calls:
|
|
549
|
+
from roller.storage import atomic_write_json
|
|
550
|
+
native_dir = self.capture.recorder.directory / "native"
|
|
551
|
+
native_dir.mkdir(parents=True, exist_ok=True)
|
|
552
|
+
sources = []
|
|
553
|
+
for index, path in enumerate(files):
|
|
554
|
+
if path.is_symlink():
|
|
555
|
+
continue
|
|
556
|
+
name = f"source-{index:04d}.jsonl"
|
|
557
|
+
self.capture.add_native(path, name)
|
|
558
|
+
sources.append({"path": f"native/{name}",
|
|
559
|
+
"native_path": path.relative_to(staging_root).as_posix()})
|
|
560
|
+
atomic_write_json(native_dir.parent / "native_index.json", sources)
|
|
561
|
+
destination = rollout_dir / f"trace.attempt-{attempt}.jsonl"
|
|
562
|
+
self._combine_session_files(files, destination)
|
|
563
|
+
if not destination.is_file() or destination.stat().st_size == 0:
|
|
564
|
+
return None
|
|
565
|
+
trace_path = rollout_dir / "trace.jsonl"
|
|
566
|
+
shutil.copyfile(destination, trace_path)
|
|
567
|
+
return trace_path
|
|
568
|
+
finally:
|
|
569
|
+
shutil.rmtree(staging_root, ignore_errors=True)
|
|
570
|
+
|
|
571
|
+
def _capture_metadata(self, output_dir: Path, attempt: int, status: str) -> dict[str, Any]:
|
|
572
|
+
if not self.config.capture_calls:
|
|
573
|
+
return {}
|
|
574
|
+
if self.capture is None:
|
|
575
|
+
return {"capture": {"manifest": None, "capture_status": "partial",
|
|
576
|
+
"error": "capture recorder unavailable"}}
|
|
577
|
+
metadata = self.capture.finish(status, error_formatter=str)
|
|
578
|
+
if metadata["capture"]["manifest"] is None:
|
|
579
|
+
logger.warning("Claude call capture finalization failed: %s", metadata["capture"]["error"])
|
|
580
|
+
return metadata
|
|
581
|
+
|
|
582
|
+
def run(self, request: RunRequest, output_dir: Path, attempt: int) -> HarnessResult:
|
|
583
|
+
session = ClaudeCodeSession(self, request, output_dir, attempt)
|
|
584
|
+
status, error = "error", None
|
|
585
|
+
try:
|
|
586
|
+
session.start()
|
|
587
|
+
for index, prompt in enumerate(request.task.prompts):
|
|
588
|
+
remaining = request.timeout_seconds - (time.monotonic() - session.started_monotonic)
|
|
589
|
+
if remaining <= 0:
|
|
590
|
+
raise TimeoutError("Claude Code task timed out")
|
|
591
|
+
session.return_codes.append(
|
|
592
|
+
self._run_turn(
|
|
593
|
+
session.container,
|
|
594
|
+
request,
|
|
595
|
+
prompt,
|
|
596
|
+
resume=index > 0,
|
|
597
|
+
timeout_seconds=remaining,
|
|
598
|
+
log_path=session.stream_path,
|
|
599
|
+
)
|
|
600
|
+
)
|
|
601
|
+
status = "ok"
|
|
602
|
+
except (TimeoutError, subprocess.TimeoutExpired):
|
|
603
|
+
status = "timeout"
|
|
604
|
+
error = f"task exceeded {request.timeout_seconds}s"
|
|
605
|
+
except Exception as exc:
|
|
606
|
+
status = "error"
|
|
607
|
+
error = str(exc)
|
|
608
|
+
return session.finish(status, error, user_messages=request.task.prompts)
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
class ClaudeCodeSession(InteractiveSession):
|
|
612
|
+
"""Persistent Claude Code session for model-driven conversations."""
|
|
613
|
+
|
|
614
|
+
def __init__(
|
|
615
|
+
self,
|
|
616
|
+
harness: ClaudeCodeHarness,
|
|
617
|
+
request: RunRequest,
|
|
618
|
+
output_dir: Path,
|
|
619
|
+
attempt: int,
|
|
620
|
+
*,
|
|
621
|
+
collect_outputs: bool = True,
|
|
622
|
+
) -> None:
|
|
623
|
+
self.harness = harness
|
|
624
|
+
self.request = request
|
|
625
|
+
self.output_dir = output_dir
|
|
626
|
+
self.attempt = attempt
|
|
627
|
+
self.collect_outputs = collect_outputs
|
|
628
|
+
self.started = False
|
|
629
|
+
self.container_started = False
|
|
630
|
+
self.container = (
|
|
631
|
+
f"roller-claude-{request.index}-{safe_component(request.display_model, 20)}-"
|
|
632
|
+
f"{uuid.uuid4().hex[:8]}"
|
|
633
|
+
).lower()
|
|
634
|
+
self.messages: list[str] = []
|
|
635
|
+
self.return_codes: list[int] = []
|
|
636
|
+
self._pending_message = ""
|
|
637
|
+
self.environment: dict[str, str] = {}
|
|
638
|
+
self.claude_version = ""
|
|
639
|
+
self.started_at = datetime.now(timezone.utc).isoformat(timespec="seconds")
|
|
640
|
+
self.started_monotonic = time.monotonic()
|
|
641
|
+
self.image_digest = harness._image_digest()
|
|
642
|
+
self.stream_path = output_dir / "rollout" / f"claude-code.attempt-{attempt}.jsonl"
|
|
643
|
+
self._finished: HarnessResult | None = None
|
|
644
|
+
|
|
645
|
+
def start(self) -> None:
|
|
646
|
+
if self.started:
|
|
647
|
+
raise RuntimeError("Claude Code session has already started")
|
|
648
|
+
self.output_dir.mkdir(parents=True, exist_ok=True)
|
|
649
|
+
self.stream_path.parent.mkdir(parents=True, exist_ok=True)
|
|
650
|
+
for relative in (
|
|
651
|
+
"rollout/trace.jsonl",
|
|
652
|
+
f"rollout/trace.attempt-{self.attempt}.jsonl",
|
|
653
|
+
"wire_trace.jsonl",
|
|
654
|
+
):
|
|
655
|
+
(self.output_dir / relative).unlink(missing_ok=True)
|
|
656
|
+
self.harness.deadline = self.started_monotonic + self.request.timeout_seconds
|
|
657
|
+
self.harness._start_wire_proxy(self.output_dir, self.attempt)
|
|
658
|
+
# Mark before run: a timed-out Docker client may already have created it.
|
|
659
|
+
self.container_started = True
|
|
660
|
+
self.environment = self.harness._start_container(self.container, self.request)
|
|
661
|
+
self.harness._prepare_workspace(self.container, self.request)
|
|
662
|
+
self.harness._prepare_context_and_setup(self.container, self.request)
|
|
663
|
+
self.harness._prepare_settings(self.container)
|
|
664
|
+
self.claude_version = self.harness._checked(
|
|
665
|
+
["exec", self.container, "claude", "--version"],
|
|
666
|
+
"Claude Code version check",
|
|
667
|
+
).stdout.strip()
|
|
668
|
+
self.started = True
|
|
669
|
+
|
|
670
|
+
def _latest_reply(self) -> str:
|
|
671
|
+
trace = self.harness._collect_trace(self.container, self.output_dir, self.attempt)
|
|
672
|
+
if trace is None:
|
|
673
|
+
raise RuntimeError("Claude Code turn produced no native session trace")
|
|
674
|
+
destination = self.output_dir / "rollout" / f".turn-{len(self.messages) + 1}.ACTF.json"
|
|
675
|
+
trajectory = convert_claude_code_trace(
|
|
676
|
+
trace,
|
|
677
|
+
destination,
|
|
678
|
+
user_messages=[*self.messages, self._pending_message],
|
|
679
|
+
wire_trace=self.output_dir / "wire_trace.jsonl",
|
|
680
|
+
keep_thinking=self.harness.config.keep_thinking,
|
|
681
|
+
)
|
|
682
|
+
steps = trajectory.get("steps")
|
|
683
|
+
if not isinstance(steps, list):
|
|
684
|
+
raise RuntimeError("Claude Code turn produced an invalid ACTF trajectory")
|
|
685
|
+
for step in reversed(steps):
|
|
686
|
+
if not isinstance(step, Mapping):
|
|
687
|
+
continue
|
|
688
|
+
assistant = step.get("assistant_content")
|
|
689
|
+
if not isinstance(assistant, Mapping):
|
|
690
|
+
continue
|
|
691
|
+
content = assistant.get("content")
|
|
692
|
+
if isinstance(content, str) and content.strip():
|
|
693
|
+
return content.strip()
|
|
694
|
+
raise RuntimeError("Claude Code turn produced no visible assistant reply")
|
|
695
|
+
|
|
696
|
+
def send(self, message: str, timeout_seconds: float) -> TurnResult:
|
|
697
|
+
if not self.started or not self.container_started:
|
|
698
|
+
raise RuntimeError("Claude Code session has not started")
|
|
699
|
+
if not isinstance(message, str) or not message.strip():
|
|
700
|
+
raise ValueError("Claude Code session messages must be non-empty strings")
|
|
701
|
+
prompt = message.strip()
|
|
702
|
+
turn_started = time.monotonic()
|
|
703
|
+
self._pending_message = prompt
|
|
704
|
+
self.return_codes.append(
|
|
705
|
+
self.harness._run_turn(
|
|
706
|
+
self.container,
|
|
707
|
+
self.request,
|
|
708
|
+
prompt,
|
|
709
|
+
resume=bool(self.messages),
|
|
710
|
+
timeout_seconds=timeout_seconds,
|
|
711
|
+
log_path=self.stream_path,
|
|
712
|
+
)
|
|
713
|
+
)
|
|
714
|
+
reply = self._latest_reply()
|
|
715
|
+
self.messages.append(prompt)
|
|
716
|
+
return TurnResult(reply, time.monotonic() - turn_started, self.return_codes[-1])
|
|
717
|
+
|
|
718
|
+
def finish(
|
|
719
|
+
self,
|
|
720
|
+
status: str,
|
|
721
|
+
error: str | None = None,
|
|
722
|
+
*,
|
|
723
|
+
user_messages: Sequence[str] | None = None,
|
|
724
|
+
) -> HarnessResult:
|
|
725
|
+
"""Collect traces and outputs, release the container and build the result.
|
|
726
|
+
|
|
727
|
+
``user_messages`` defaults to the messages delivered through :meth:`send`;
|
|
728
|
+
:meth:`ClaudeCodeHarness.run` passes the task prompts it ran directly.
|
|
729
|
+
"""
|
|
730
|
+
|
|
731
|
+
if self._finished is not None:
|
|
732
|
+
return self._finished
|
|
733
|
+
harness, config = self.harness, self.harness.config
|
|
734
|
+
task_id = self.request.task.id
|
|
735
|
+
messages = list(self.messages if user_messages is None else user_messages)
|
|
736
|
+
trace_path: Path | None = None
|
|
737
|
+
trajectory_path: Path | None = None
|
|
738
|
+
artifact_result: ArtifactCollectionResult | None = None
|
|
739
|
+
output_copy_error: str | None = None
|
|
740
|
+
try:
|
|
741
|
+
harness._stop_wire_proxy()
|
|
742
|
+
except Exception as exc:
|
|
743
|
+
logger.warning("[%s] Claude Code wire proxy shutdown failed: %s", task_id, exc)
|
|
744
|
+
if self.container_started:
|
|
745
|
+
try:
|
|
746
|
+
trace_path = harness._collect_trace(self.container, self.output_dir, self.attempt)
|
|
747
|
+
except Exception as exc:
|
|
748
|
+
logger.warning("[%s] Claude Code trace collection failed: %s", task_id, exc)
|
|
749
|
+
if self.collect_outputs:
|
|
750
|
+
try:
|
|
751
|
+
artifact_result, output_copy_error = harness._collect_outputs(
|
|
752
|
+
self.container, self.request, self.output_dir
|
|
753
|
+
)
|
|
754
|
+
except Exception as exc:
|
|
755
|
+
output_copy_error = str(exc)
|
|
756
|
+
logger.warning("[%s] Claude Code output collection failed: %s", task_id, exc)
|
|
757
|
+
if trace_path is not None:
|
|
758
|
+
try:
|
|
759
|
+
trajectory_path = self.output_dir / "rollout" / "trace_ACTF.json"
|
|
760
|
+
trajectory = convert_claude_code_trace(
|
|
761
|
+
trace_path,
|
|
762
|
+
trajectory_path,
|
|
763
|
+
user_messages=messages,
|
|
764
|
+
wire_trace=self.output_dir / "wire_trace.jsonl",
|
|
765
|
+
keep_thinking=config.keep_thinking,
|
|
766
|
+
)
|
|
767
|
+
if not trajectory.get("steps"):
|
|
768
|
+
raise ValueError("Claude Code native trace produced no assistant steps")
|
|
769
|
+
except Exception as exc:
|
|
770
|
+
trajectory_path = None
|
|
771
|
+
if status == "ok":
|
|
772
|
+
status, error = "error", f"Claude Code trajectory conversion failed: {exc}"
|
|
773
|
+
if status == "ok" and trace_path is None:
|
|
774
|
+
status, error = "error", "Claude Code finished but no native session trace was found"
|
|
775
|
+
if status == "ok" and output_copy_error is not None:
|
|
776
|
+
status, error = "error", f"output collection failed: {output_copy_error}"
|
|
777
|
+
if status == "ok" and artifact_result is not None and not artifact_result.passed:
|
|
778
|
+
status, error = "error", "unsafe output files were rejected"
|
|
779
|
+
retained_container: str | None = None
|
|
780
|
+
if self.container_started:
|
|
781
|
+
if config.keep_container_on_error and status != "ok":
|
|
782
|
+
retained_container = self.container
|
|
783
|
+
else:
|
|
784
|
+
removed = remove_container(harness.runtime, self.container)
|
|
785
|
+
if removed.returncode != 0:
|
|
786
|
+
logger.warning("[%s] failed to remove container %s", task_id, self.container)
|
|
787
|
+
elapsed = time.monotonic() - self.started_monotonic
|
|
788
|
+
usage = (
|
|
789
|
+
extract_claude_code_usage(trace_path, self.stream_path)
|
|
790
|
+
if trace_path is not None else dict(ZERO_USAGE)
|
|
791
|
+
)
|
|
792
|
+
usage["elapsed_seconds"] = round(elapsed, 3)
|
|
793
|
+
provenance = build_provenance(
|
|
794
|
+
self.request,
|
|
795
|
+
harness=harness.name,
|
|
796
|
+
claude_code_version=self.claude_version,
|
|
797
|
+
docker_image=config.docker_image,
|
|
798
|
+
docker_image_digest=self.image_digest,
|
|
799
|
+
api_base_urls=[redact_anthropic_base_url(config.api_base)] if config.api_base else [],
|
|
800
|
+
environment=self.environment,
|
|
801
|
+
attempt=self.attempt,
|
|
802
|
+
started_at=self.started_at,
|
|
803
|
+
finished_at=datetime.now(timezone.utc).isoformat(timespec="seconds"),
|
|
804
|
+
)
|
|
805
|
+
try:
|
|
806
|
+
reasoning_effort = resolve_thinking(self.request.task.thinking, config.reasoning_effort)
|
|
807
|
+
transport_effort = harness._effective_effort(self.request)
|
|
808
|
+
except Exception:
|
|
809
|
+
reasoning_effort = None
|
|
810
|
+
transport_effort = None
|
|
811
|
+
self._finished = HarnessResult(
|
|
812
|
+
status=status,
|
|
813
|
+
elapsed_seconds=elapsed,
|
|
814
|
+
error=error,
|
|
815
|
+
usage=usage,
|
|
816
|
+
trace_path=trace_path.relative_to(self.output_dir).as_posix() if trace_path else None,
|
|
817
|
+
trajectory_path=trajectory_path.relative_to(self.output_dir).as_posix() if trajectory_path else None,
|
|
818
|
+
metadata={
|
|
819
|
+
**harness._capture_metadata(self.output_dir, self.attempt, status),
|
|
820
|
+
"raw_trace_schema": "claude-code-session-jsonl",
|
|
821
|
+
"claude_code_version": self.claude_version,
|
|
822
|
+
"container": retained_container,
|
|
823
|
+
"agent_return_codes": self.return_codes,
|
|
824
|
+
"turns": len(messages),
|
|
825
|
+
"reasoning_effort": reasoning_effort,
|
|
826
|
+
"transport_effort": transport_effort,
|
|
827
|
+
"max_thinking_tokens": config.max_thinking_tokens,
|
|
828
|
+
"max_turns": config.max_turns,
|
|
829
|
+
"permission_mode": config.permission_mode,
|
|
830
|
+
"wire_trace": config.wire_trace_mode,
|
|
831
|
+
"keep_thinking": config.keep_thinking,
|
|
832
|
+
"settings_fingerprint": config.settings_fingerprint,
|
|
833
|
+
"output_collection": output_collection_metadata(artifact_result, self.request, self.output_dir),
|
|
834
|
+
"output_copy_error": output_copy_error,
|
|
835
|
+
"attempt": self.attempt,
|
|
836
|
+
"provenance": provenance,
|
|
837
|
+
},
|
|
838
|
+
)
|
|
839
|
+
return self._finished
|