File size: 10,611 Bytes
b192407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Claude Code Agent Module - Agent implementation driving the Claude Code CLI.

This module provides an agent that:
- Writes a task prompt and a wrapper script to the agent's Docker container
- Runs the `claude` CLI inside the container with a timeout, reading Anthropic
  API credentials from the container's environment (injected by run.py)
- Parses Claude Code's stream-json (or json) output into a normalized trajectory,
  capturing per-step timing, tool uses, and token usage
"""

import json
import logging
import os
import getpass
import subprocess
import time
from da_agent.envs.da_agent import DA_Agent_Env
from da_agent.agent.base import BaseAgent

logger = logging.getLogger("da_agent")

DEFAULT_TIME_OUT = 3600  # 60 minutes


class PromptAgent(BaseAgent):
    # Claude Code drives the `claude` CLI; only model is used from the shared
    # config (the timeout uses DEFAULT_TIME_OUT instead of max_steps). No
    # cross-task state, so __init__ is inherited from BaseAgent.

    def set_env_and_task(self, env: DA_Agent_Env):
        self.env = env
        self.instruction = self.env.task_config['question']
        self.trajectory = []
        self.raw_output = ""
        self.event_timestamps = []

    def _build_task_prompt(self):
        task = self.instruction
        task += f"\n\nYou are working in the directory: {self.work_dir}."
        task += " All required data files are available in this directory."
        task += " Complete the task and ensure all output files are saved in this directory."

        image_file_names = self._get_image_file_names()
        if image_file_names:
            task += self._build_plotting_instructions(image_file_names)

        return task

    def _get_image_file_names(self):
        image_file_names = []
        for post_process_f in self.env.post_process_func:
            def image_post_process(output_file_name):
                if output_file_name in self.env.task_config.get('output_file_name', []):
                    return output_file_name
                return None
            output_file_name = eval(post_process_f)
            if output_file_name:
                image_file_names.append(output_file_name)
        return image_file_names

    def _build_plotting_instructions(self, image_file_names):
        return f"""
### Plotting (REQUIRED)

If you create a matplotlib plot, you MUST call:

    from image import Plotprocess
    Plotprocess.plot_process(fig, "<image_file_name>")

Use ONLY these file names:
{", ".join(image_file_names)}

Rules:
- Call AFTER plotting is complete
- Call BEFORE saving the figure
- Use: fig = plt.gcf()
- Replace <image_file_name> with one from the list above

Example:
```python
from image import Plotprocess
import matplotlib.pyplot as plt

# plotting code ...

fig = plt.gcf()
Plotprocess.plot_process(fig, "{image_file_names[0]}")
```"""

    def _write_wrapper_script(self):
        wrapper_code = f'''#!/usr/bin/env python3
import subprocess
import sys
import threading

with open("{self.work_dir}/.task_prompt.txt") as f:
    prompt = f.read()

proc = subprocess.Popen(
    ["claude", "-p", prompt, "--output-format", "stream-json", "--verbose",
     "--model", "{self.model}",
     "--dangerously-skip-permissions"],
    stdout=sys.stdout, stderr=sys.stderr
)

def timeout_handler():
    proc.terminate()
    kill_timer = threading.Timer(300, proc.kill)
    kill_timer.daemon = True
    kill_timer.start()

timer = threading.Timer({DEFAULT_TIME_OUT}, timeout_handler)
timer.daemon = True
timer.start()

sys.exit(proc.wait())
'''
        wrapper_path = os.path.join(self.env.mnt_dir, ".run_claude.py")
        with open(wrapper_path, "w") as f:
            f.write(wrapper_code)

    def run(self):
        assert self.env is not None, "Environment is not set."

        task_prompt = self._build_task_prompt()
        container_name = self.env.container.name

        # Write task prompt and wrapper script to mounted directory
        task_path = os.path.join(self.env.mnt_dir, ".task_prompt.txt")
        with open(task_path, "w") as f:
            f.write(task_prompt)
        self._write_wrapper_script()

        # Execute wrapper script inside the container as the non-root user named
        # after the host user.
        process = subprocess.Popen(
            ["docker", "exec", "--user", getpass.getuser(), str(container_name),
             "python3", f"{self.work_dir}/.run_claude.py"],
            stdout=subprocess.PIPE, stderr=subprocess.STDOUT
        )

        output_lines = []
        self.event_timestamps = []
        try:
            while True:
                line = process.stdout.readline()
                if not line and process.poll() is not None:
                    break
                if line:
                    decoded = line.decode("utf-8", errors="ignore")
                    output_lines.append(decoded)
                    self.event_timestamps.append(time.time())
                    logger.debug("Claude Code: %s", decoded.strip())
        except Exception as e:
            process.kill()
            logger.error("Error running Claude Code: %s", e)
            self.raw_output = "".join(output_lines)
            self._parse_trajectory()
            return False, f"Error: {e}"

        self.raw_output = "".join(output_lines)
        exit_code = process.returncode

        self._parse_trajectory()

        if exit_code == 0:
            return True, "Task completed"
        else:
            return False, f"Agent exited with code {exit_code}"

    def _parse_trajectory(self):
        self.trajectory = []

        # Try parsing as a single JSON array (--output-format json)
        try:
            entries = json.loads(self.raw_output.strip())
            if isinstance(entries, list):
                for entry in entries:
                    normalized = self._normalize_entry(entry)
                    self.trajectory.append(normalized)
                return
        except json.JSONDecodeError:
            pass

        # Fall back to JSONL parsing (--output-format stream-json)
        lines = self.raw_output.strip().split("\n")
        for i, line in enumerate(lines):
            line = line.strip()
            if not line:
                continue
            try:
                entry = json.loads(line)
                normalized = self._normalize_entry(entry)
                # Add timing info from recorded timestamps (stream-json only)
                if i < len(self.event_timestamps):
                    ts = self.event_timestamps[i]
                    prev_ts = self.event_timestamps[i - 1] if i > 0 else ts
                    normalized["timing"] = {
                        "start_time": prev_ts,
                        "end_time": ts,
                        "duration": ts - prev_ts,
                    }
                self.trajectory.append(normalized)
            except json.JSONDecodeError:
                step = {"type": "raw", "content": line}
                if i < len(self.event_timestamps):
                    ts = self.event_timestamps[i]
                    prev_ts = self.event_timestamps[i - 1] if i > 0 else ts
                    step["timing"] = {
                        "start_time": prev_ts,
                        "end_time": ts,
                        "duration": ts - prev_ts,
                    }
                self.trajectory.append(step)

    def _normalize_entry(self, entry):
        entry_type = entry.get("type", "unknown")

        if entry_type == "system":
            subtype = entry.get("subtype", "")
            if subtype == "init":
                return {
                    "type": "system_init",
                    "session_id": entry.get("session_id", ""),
                    "model": entry.get("model", ""),
                    "cwd": entry.get("cwd", ""),
                }
            return {"type": "system", "subtype": subtype}

        if entry_type == "assistant":
            message = entry.get("message", {})
            msg_id = message.get("id", "")
            content = message.get("content", [])
            text_parts = []
            code_action = None
            tool_uses = []

            for block in content if isinstance(content, list) else []:
                if not isinstance(block, dict):
                    continue
                if block.get("type") == "text":
                    text_parts.append(block.get("text", ""))
                elif block.get("type") == "tool_use":
                    tool_name = block.get("name", "")
                    tool_input = block.get("input", {})
                    tool_uses.append({"name": tool_name, "input": tool_input})
                    if tool_name == "Bash" and "command" in tool_input:
                        code_action = tool_input["command"]

            result = {"type": "assistant", "content": "\n".join(text_parts)}
            if msg_id:
                result["msg_id"] = msg_id
            if tool_uses:
                result["tool_uses"] = tool_uses
            if code_action:
                result["code_action"] = code_action
            # Extract per-message usage if available
            usage = message.get("usage", {})
            if usage:
                result["usage"] = usage
            return result

        elif entry_type == "tool_result":
            content = entry.get("content", "")
            if isinstance(content, list):
                text_parts = []
                for block in content:
                    if isinstance(block, dict) and block.get("type") == "text":
                        text_parts.append(block.get("text", ""))
                content = "\n".join(text_parts)
            result = {"type": "tool_result", "observations": f"Execution logs:\n{content}"}
            return result

        elif entry_type == "result":
            result_entry = {
                "type": "result",
                "subtype": entry.get("subtype", ""),
                "is_error": entry.get("is_error", False),
                "result": entry.get("result", ""),
                "stop_reason": entry.get("stop_reason", ""),
                "duration_ms": entry.get("duration_ms", 0),
                "num_turns": entry.get("num_turns", 0),
            }
            # Extract aggregate usage from the result event
            usage = entry.get("usage", {})
            if usage:
                result_entry["usage"] = usage
            return result_entry

        return entry

    def get_trajectory(self):
        return {
            "task": self.instruction,
            "trajectory": self.trajectory
        }