File size: 9,249 Bytes
ba52031
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9f2957
ba52031
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9f2957
 
ba52031
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9f2957
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba52031
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
TaskLoader — copies task source files into a fresh temp directory for each episode.

Task layout on disk:
    debug_env/tasks/
        task1/
            broken_code.py
            test_code.py
        task2/
            broken_code.py
            test_code.py
        task3/
            broken_code.py
            helper.py
            test_code.py
"""

import shutil
import sqlite3
import tempfile
from pathlib import Path

from debug_env.server.tasks.data import get_available_task_ids, validate_task_id

TASKS_DIR = Path(__file__).parent.parent.parent / "tasks"


class TaskLoader:
    """Manages task file staging into isolated temp directories."""

    @staticmethod
    def available_tasks() -> list[str]:
        """Return sorted list of registered task IDs."""
        return get_available_task_ids()

    @staticmethod
    def load(task_id: str, previous_workdir: str | None = None) -> str:
        """
        Copy all files for *task_id* into a fresh temp directory.
        For tasks with seed data, generates files from seed data.

        Args:
            task_id:           e.g. "task1"
            previous_workdir:  if provided, this directory is deleted first.

        Returns:
            Path to the new workdir (str).

        Raises:
            ValueError: if task_id is not in the registry.
            FileNotFoundError: if task files are missing from disk.
        """
        if not validate_task_id(task_id):
            available = get_available_task_ids()
            raise ValueError(
                f"Unknown task '{task_id}'. Available: {available}"
            )

        if previous_workdir:
            shutil.rmtree(previous_workdir, ignore_errors=True)

        workdir = tempfile.mkdtemp(prefix=f"debug_{task_id}_")

        # For tasks with seed data, generate files from seed data
        if task_id == "task4":
            TaskLoader._generate_task4_files(workdir)
        elif task_id == "task5":
            TaskLoader._generate_task5_files(workdir)
        elif task_id == "task6":
            TaskLoader._generate_task6_files(workdir)
        elif task_id == "task7":
            TaskLoader._generate_task7_files(workdir)
        elif task_id == "task8":
            TaskLoader._generate_task8_files(workdir)
        elif task_id == "task9":
            TaskLoader._generate_task9_files(workdir)
        elif task_id == "incident1":
            TaskLoader._generate_incident1_files(workdir)
        else:
            # For other tasks, copy from disk
            task_path = TASKS_DIR / task_id
            if not task_path.is_dir():
                raise FileNotFoundError(
                    f"Task files for '{task_id}' not found on disk at {task_path}"
                )

            for f in task_path.iterdir():
                if f.is_file():
                    shutil.copy2(f, workdir)

        return workdir

    @staticmethod
    def _generate_task4_files(workdir: str) -> None:
        """Generate task4 files from seed data."""
        from debug_env.tasks.task4.seed_data import (
            get_task4_seed_data,
            get_test_file_content,
        )

        seed = get_task4_seed_data()
        for filename, content in seed["files"].items():
            filepath = Path(workdir) / filename
            filepath.write_text(content, encoding="utf-8")

        # Write test file
        test_content = get_test_file_content()
        (Path(workdir) / seed["test_file"]).write_text(test_content, encoding="utf-8")

    @staticmethod
    def _generate_task5_files(workdir: str) -> None:
        """Generate task5 files from seed data."""
        from debug_env.tasks.task5.seed_data import (
            get_task5_seed_data,
            get_test_file_content,
        )

        seed = get_task5_seed_data()
        for filename, content in seed["files"].items():
            filepath = Path(workdir) / filename
            filepath.write_text(content, encoding="utf-8")

        # Write test file
        test_content = get_test_file_content()
        (Path(workdir) / seed["test_file"]).write_text(test_content, encoding="utf-8")

    @staticmethod
    def _generate_task6_files(workdir: str) -> None:
        """Generate task6 files from seed data."""
        from debug_env.tasks.task6.seed_data import (
            get_task6_seed_data,
            get_test_file_content,
        )

        seed = get_task6_seed_data()
        for filename, content in seed["files"].items():
            filepath = Path(workdir) / filename
            filepath.write_text(content, encoding="utf-8")

        # Write test file
        test_content = get_test_file_content()
        (Path(workdir) / seed["test_file"]).write_text(test_content, encoding="utf-8")

    @staticmethod
    def _generate_task7_files(workdir: str) -> None:
        """Generate task7 files from seed data."""
        from debug_env.tasks.task7.seed_data import (
            get_task7_seed_data,
            get_test_auth_py_content,
        )

        seed = get_task7_seed_data()
        for filename, content in seed["files"].items():
            filepath = Path(workdir) / filename
            filepath.write_text(content, encoding="utf-8")

        # Write test file
        test_content = get_test_auth_py_content()
        (Path(workdir) / seed["test_file"]).write_text(test_content, encoding="utf-8")

    @staticmethod
    def _generate_task8_files(workdir: str) -> None:
        """Generate task8 files from seed data."""
        from debug_env.tasks.task8.seed_data import (
            get_task8_seed_data,
            get_test_user_service_content,
        )

        seed = get_task8_seed_data()
        for filename, content in seed["files"].items():
            filepath = Path(workdir) / filename
            filepath.write_text(content, encoding="utf-8")

        # Write test file
        test_content = get_test_user_service_content()
        (Path(workdir) / seed["test_file"]).write_text(test_content, encoding="utf-8")

    @staticmethod
    def _generate_task9_files(workdir: str) -> None:
        """Generate task9 files from seed data."""
        from debug_env.tasks.task9.seed_data import (
            get_task9_seed_data,
            get_test_file_content,
        )

        seed = get_task9_seed_data()
        for filename, content in seed["files"].items():
            filepath = Path(workdir) / filename
            filepath.write_text(content, encoding="utf-8")

        # Write test file
        test_content = get_test_file_content()
        (Path(workdir) / seed["test_file"]).write_text(test_content, encoding="utf-8")

    @staticmethod
    def _generate_incident1_files(workdir: str) -> None:
        """Copy incident1 source files and seed the SQLite database."""
        from debug_env.tasks.incident1.seed_data import get_incident1_seed_data

        seed = get_incident1_seed_data()

        # Copy the declared source files from the task directory
        from debug_env.server.tasks.data import get_task_by_id
        task_files = get_task_by_id("incident1")["files"]
        task_path = TASKS_DIR / "incident1"
        for filename in task_files:
            src = task_path / filename
            if src.is_file():
                shutil.copy2(src, workdir)

        # Create and seed the SQLite database
        db_path = Path(workdir) / "app.db"
        conn = sqlite3.connect(str(db_path))
        for sql_stmt in seed["db_seed_sql"]:
            conn.execute(sql_stmt)
        conn.commit()
        conn.close()

    @staticmethod
    def list_source_files(task_id: str) -> list[str]:
        """
        Return the editable source filenames for a task (excludes test files).

        Args:
            task_id: e.g. "task1"

        Returns:
            Sorted list of filenames, e.g. ["broken_code.py"]

        Raises:
            ValueError: if task_id is not registered.
        """
        if not validate_task_id(task_id):
            raise ValueError(f"Unknown task '{task_id}'.")
        task_path = TASKS_DIR / task_id
        return sorted(
            f.name for f in task_path.iterdir()
            if f.is_file() and not f.name.startswith("test_")
        )

    @staticmethod
    def read_source_file(task_id: str, filename: str) -> str:
        """
        Read the canonical (pre-episode) content of a source file.

        Args:
            task_id:  e.g. "task1"
            filename: e.g. "broken_code.py"

        Returns:
            File content as a string.

        Raises:
            ValueError: if task_id is not registered or filename is invalid.
            FileNotFoundError: if the file does not exist on disk.
        """
        if not validate_task_id(task_id):
            raise ValueError(f"Unknown task '{task_id}'.")
        if ".." in filename or filename.startswith("/"):
            raise ValueError(f"Invalid filename '{filename}'.")
        file_path = TASKS_DIR / task_id / filename
        if not file_path.is_file():
            available = TaskLoader.list_source_files(task_id)
            raise FileNotFoundError(
                f"File '{filename}' not found in task '{task_id}'. "
                f"Available source files: {available}"
            )
        return file_path.read_text(encoding="utf-8")