File size: 25,780 Bytes
1b35d41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
364b897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b35d41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

"""
Plain tool functions adapted from timemachine-bench-main/agents/src/utils/tools.py.

All LangChain decorators removed; every parameter is an explicit keyword-only argument.
Logic is preserved exactly from the original.
"""

from __future__ import annotations

import re
import subprocess
import tempfile
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Union


def list_dir(
    *,
    host_repo_dir: str,
    repo_name: str,
    dir_path: Optional[str] = "/work",
) -> str:
    """List files and subdirectories under *dir_path* (default ``/work``)."""
    try:
        p = Path(dir_path)

        if p.is_absolute() and not p.is_relative_to("/work"):
            raise ValueError("You cannot access directories outside /work")
        if not p.is_absolute() and ".." in p.parts:
            raise ValueError("You cannot access directories outside /work")

        rel_path = p.relative_to("/work") if p.is_absolute() else p
        host_dir = Path(host_repo_dir) / rel_path

        files: list[str] = []
        subdirectories: list[str] = []

        if not host_dir.exists():
            raise FileNotFoundError(f"The directory does not exist: {dir_path}")
        if not host_dir.is_dir():
            raise NotADirectoryError(f"The path is not a directory: {dir_path}")

        for entry in host_dir.iterdir():
            if entry.name.startswith("."):
                continue
            if entry.is_dir():
                subdirectories.append(entry.name)
            elif entry.is_file():
                files.append(entry.name)

        subdirectories = list(sorted([d + "/" for d in subdirectories]))
        files = list(sorted(files))

        if dir_path == "/work":
            files = [
                f
                for f in files
                if f not in {f"setup_{repo_name}.sh", f"test_{repo_name}.sh"}
            ]

        content_lst = subdirectories + files

        return (
            f"Found {len(files)} file(s) and {len(subdirectories)} subdirectory(s) under {dir_path}:\n\n"
            f"<directory>\n" + "\n".join(content_lst) + "\n</directory>"
        )
    except Exception as e:
        return f"Error listing the directory: {dir_path}. Here is the message: {str(e)}"


def search_dir(
    *,
    regex_pattern: str,
    host_repo_dir: str,
    dir_path: Optional[str] = "/work",
) -> str:
    """Search for *regex_pattern* in all ``.py`` files under *dir_path*."""
    try:
        ptn = re.compile(regex_pattern)

        p = Path(dir_path)

        if p.is_absolute() and not p.is_relative_to("/work"):
            raise ValueError("You cannot access directories outside /work")
        if not p.is_absolute() and ".." in p.parts:
            raise ValueError("You cannot access directories outside /work")

        rel_path = p.relative_to("/work") if p.is_absolute() else p
        host_dir = Path(host_repo_dir) / rel_path

        if not host_dir.exists():
            raise FileNotFoundError(f"The directory does not exist: {dir_path}")
        if not host_dir.is_dir():
            raise NotADirectoryError(f"The path is not a directory: {dir_path}")

    except Exception as e:
        return f'Search failed for "{regex_pattern}" in the directory. Here is the message: {str(e)}'

    matches: list[tuple[str, int]] = []

    for fpath in Path(host_dir).rglob("*.py"):
        try:
            match_in_file = 0
            with open(fpath, "r", newline="") as f:
                for line in f:
                    line = line.rstrip()
                    if ptn.search(line):
                        match_in_file += 1
            if match_in_file > 0:
                rel = fpath.relative_to(host_repo_dir)
                container_path = Path("/work") / rel
                matches.append((str(container_path), match_in_file))
        except Exception:
            continue

    num_matches = len(matches)

    if not matches:
        return f'No matches found for "{regex_pattern}" in the directory.'

    if num_matches > 50:
        return (
            f'More than 50 matches found for "{regex_pattern}" in the directory.'
            " Please write a more specific query to narrow down the results."
        )

    result_str = f'Found {len(matches)} match(es) for "{regex_pattern}" in the directory:\n\n'
    result_str += "\n".join(f"{fpath} ({count} match(es))" for fpath, count in matches)

    return result_str


def search_file(
    *,
    regex_pattern: str,
    file_path: str,
    host_repo_dir: str,
) -> str:
    """Search for *regex_pattern* in the file at *file_path*."""
    matches: list[tuple[int, str]] = []

    try:
        ptn = re.compile(regex_pattern)

        p = Path(file_path)

        if p.is_absolute() and not p.is_relative_to("/work"):
            raise ValueError("You cannot access directories outside /work")
        if not p.is_absolute() and ".." in p.parts:
            raise ValueError("You cannot access directories outside /work")

        rel_path = p.relative_to("/work") if p.is_absolute() else p
        host_path = Path(host_repo_dir) / rel_path

        if not host_path.exists():
            raise FileNotFoundError(f"The file does not exist: {file_path}")
        if not host_path.is_file():
            raise IsADirectoryError(f"The path is not a file: {file_path}")

        with open(host_path, "r", newline="") as f:
            for i, line in enumerate(f, 1):
                line = line.rstrip()
                if ptn.search(line):
                    matches.append((i, line))

        if not matches:
            return f'No matches found for "{regex_pattern}" in {file_path}.'

        num_matches = len(matches)

        if num_matches > 50:
            return (
                f'More than 50 matches found for "{regex_pattern}" in {file_path}.'
                " Please write a more specific query to narrow down the results."
            )

        result_str = f'Found {len(matches)} match(es) for "{regex_pattern}" in {file_path}:\n\n'
        result_str += "\n".join(f"{i}: {line}" for i, line in matches)

        return result_str

    except Exception as e:
        return f'Search failed for "{regex_pattern}" in {file_path}. Here is the message: {str(e)}'


def view_file(
    *,
    file_path: str,
    line_no: int,
    host_repo_dir: str,
) -> str:
    """Show ±50 lines around *line_no* in *file_path*."""
    try:
        p = Path(file_path)

        if p.is_absolute() and not p.is_relative_to("/work"):
            raise ValueError("You cannot access directories outside /work")
        if not p.is_absolute() and ".." in p.parts:
            raise ValueError("You cannot access directories outside /work")

        rel_path = p.relative_to("/work") if p.is_absolute() else p
        host_path = Path(host_repo_dir) / rel_path

        if not host_path.exists():
            raise FileNotFoundError(f"The file does not exist: {file_path}")
        if not host_path.is_file():
            raise IsADirectoryError(f"The path is not a file: {file_path}")

        target_lines: list[str] = []
        start_line, end_line = max(1, line_no - 50), line_no + 50

        with open(host_path, "r", newline="") as f:
            for i, line in enumerate(f, start=1):
                if start_line <= i <= end_line:
                    target_lines.append(line)

        total_lines = i  # noqa: F821 — set by the loop

        content = "".join(
            f"{i}: {line}" for i, line in enumerate(target_lines, start=start_line)
        ).rstrip()

        lines_before = start_line - 1
        lines_after = total_lines - end_line

        if lines_before > 0:
            content = f"({lines_before} lines above)\n" + content
        if lines_after > 0:
            content = content + f"\n({lines_after} lines below)"

        return (
            f"Here is the content of the file around line #{line_no}:\n\n"
            f"<content>\n{content}\n</content>"
        )

    except Exception as e:
        return f"Error opening the file: {file_path}. Here is the message: {str(e)}"


def edit_file(
    *,
    file_path: str,
    start_line: int,
    end_line: int,
    replacement_text: str,
    host_repo_dir: str,
    test_files: List[str],
) -> Dict[str, Optional[str]]:
    """Replace lines *start_line*–*end_line* (inclusive) with *replacement_text*."""
    try:
        p = Path(file_path)

        if p.is_absolute() and not p.is_relative_to("/work"):
            raise ValueError("You cannot access directories outside /work")
        if not p.is_absolute() and ".." in p.parts:
            raise ValueError("You cannot access directories outside /work")

        rel_path = p.relative_to("/work") if p.is_absolute() else p
        host_path = Path(host_repo_dir) / rel_path

        if not host_path.exists():
            raise FileNotFoundError(f"The file does not exist: {file_path}")
        if not host_path.is_file():
            raise IsADirectoryError(f"The path is not a file: {file_path}")

        rel_path_str = str(rel_path)

        if rel_path_str in test_files:
            return {
                "patch": None,
                "message": "Editing test files is not allowed. The file was kept unchanged.",
            }

        with open(host_path, "r", newline="") as f:
            original_lines = f.readlines()

        to_replace_content = "".join(original_lines[start_line - 1 : end_line])

        newline = ""
        if to_replace_content.endswith("\r\n"):
            newline = "\r\n"
        elif to_replace_content.endswith("\n"):
            newline = "\n"

        replacement_text = replacement_text.rstrip(newline) + newline

        updated_lines = (
            original_lines[: start_line - 1]
            + replacement_text.splitlines(keepends=True)
            + original_lines[end_line:]
        )

        updated_content = "".join(updated_lines)

        with tempfile.NamedTemporaryFile("w", newline="") as tmpf:
            tmp_path = tmpf.name
            tmpf.write(updated_content)
            tmpf.flush()

            diff_proc = subprocess.run(
                [
                    "diff", "-u",
                    "--label", str(rel_path),
                    str(host_path),
                    "--label", str(rel_path),
                    str(tmp_path),
                ],
                stdout=subprocess.PIPE,
                check=False,
            )

        patch_content = diff_proc.stdout.decode("utf-8")

        subprocess.run(
            ["patch", "-p0", "-d", str(host_repo_dir)],
            input=patch_content,
            text=True,
            check=True,
            capture_output=True,
        )

        new_end_line = start_line + len(replacement_text.splitlines(keepends=True)) - 1
        to_show_start_line = max(1, start_line - 50)
        to_show_end_line = new_end_line + 50

        diff_lines = updated_lines[to_show_start_line - 1 : to_show_end_line]
        diff_content = "".join(
            f"{i}: {line}" for i, line in enumerate(diff_lines, start=to_show_start_line)
        ).rstrip()

        return {
            "patch": patch_content,
            "message": (
                "Edit succeeded. Here is the updated part of the file.\n\n"
                f"<content>\n{diff_content}\n</content>"
            ),
        }

    except Exception as e:
        return {
            "patch": None,
            "message": f"Edit failed. The file was kept unchanged. Here is the message: {str(e)}",
        }


def replace_all_in_file(
    *,
    file_path: str,
    regex_pattern: str,
    replacement_string: str,
    host_repo_dir: str,
    test_files: List[str],
) -> Dict[str, Optional[str]]:
    """Find-and-replace all occurrences of *regex_pattern* in *file_path*."""
    try:
        p = Path(file_path)

        if p.is_absolute() and not p.is_relative_to("/work"):
            raise ValueError("You cannot access directories outside /work")
        if not p.is_absolute() and ".." in p.parts:
            raise ValueError("You cannot access directories outside /work")

        rel_path = p.relative_to("/work") if p.is_absolute() else p
        host_path = Path(host_repo_dir) / rel_path

        if not host_path.exists():
            raise FileNotFoundError(f"The file does not exist: {file_path}")
        if not host_path.is_file():
            raise IsADirectoryError(f"The path is not a file: {file_path}")

        rel_path_str = str(rel_path)

        if rel_path_str in test_files:
            return {
                "patch": None,
                "message": "Editing test files is not allowed. The file was kept unchanged.",
            }

        with open(host_path, "r", newline="") as f:
            original_content = f.read()

        updated_content, num_replacements = re.subn(
            regex_pattern, replacement_string, original_content
        )

        if num_replacements == 0:
            return {
                "patch": None,
                "message": f'No matches found for "{regex_pattern}" in {file_path}. The file was kept unchanged.',
            }

        with tempfile.NamedTemporaryFile("w", newline="") as tmpf:
            tmp_path = tmpf.name
            tmpf.write(updated_content)
            tmpf.flush()

            diff_proc = subprocess.run(
                [
                    "diff", "-u",
                    "--label", str(rel_path),
                    str(host_path),
                    "--label", str(rel_path),
                    str(tmp_path),
                ],
                stdout=subprocess.PIPE,
                check=False,
            )

        patch_content = diff_proc.stdout.decode("utf-8")

        subprocess.run(
            ["patch", "-p0", "-d", str(host_repo_dir)],
            input=patch_content,
            text=True,
            check=True,
            capture_output=True,
        )

        return {
            "patch": patch_content,
            "message": (
                f'Replaced {num_replacements} occurrences of "{regex_pattern}" '
                f'with "{replacement_string}" in {file_path}.'
            ),
        }

    except Exception as e:
        return {
            "patch": None,
            "message": f"Replacement failed. The file was kept unchanged. Here is the message: {str(e)}",
        }


def revert_last(
    *,
    last_patch: Optional[Tuple[str, str]],
    host_repo_dir: str,
) -> str:
    """Revert the last edit using the stored patch."""

    def _extract_diff_lines_to_show(patch: str) -> List[Tuple[int, int]]:
        lines_to_show: list[tuple[int, int]] = []
        for diff_header in re.finditer(
            r"^@@ \-(\d+)(?:,(\d+))? \+\d+(?:,\d+)? @@", patch, re.MULTILINE
        ):
            diff_start = int(diff_header.group(1))
            num_affected_lines = int(diff_header.group(2) or 1)
            diff_end = diff_start + num_affected_lines - 1

            line_start = max(1, diff_start - 50)
            line_end = diff_end + 50
            lines_to_show.append((line_start, line_end))

        if not lines_to_show:
            return []

        sorted_lines = list(sorted(lines_to_show, key=lambda x: x[0]))
        merged: list[tuple[int, int]] = [sorted_lines[0]]
        current_start, current_end = merged[0]

        for start, end in sorted_lines[1:]:
            if start <= current_end:
                current_end = end
                merged[-1] = (current_start, current_end)
            else:
                merged.append((start, end))
                current_start, current_end = start, end

        return merged

    def _format_diff_blocks(
        file_path: str,
        file_lines: List[str],
        diff_lines: List[Tuple[int, int]],
    ) -> str:
        parts = [f"<path>{file_path}</path>", "<updated_contents>"]
        for start, end in diff_lines:
            target_lines = file_lines[start - 1 : end]
            part_content = "".join(
                f"{i}: {line}" for i, line in enumerate(target_lines, start=start)
            ).rstrip()
            parts.append(f"  <content>\n{part_content}\n  </content>")
        parts.append("</updated_contents>")
        return "\n".join(parts)

    if last_patch is None:
        return "No edit to revert. The file was kept unchanged."

    try:
        last_modified_path, patch_content = last_patch
        rel_path = Path(last_modified_path).relative_to("/work")
        host_path = Path(host_repo_dir) / rel_path

        if not host_path.exists():
            raise FileNotFoundError(f"The file does not exist: {last_modified_path}")
        if not host_path.is_file():
            raise IsADirectoryError(f"The path is not a file: {last_modified_path}")

        subprocess.run(
            ["patch", "-R", "-p0", "-d", str(host_repo_dir)],
            input=patch_content,
            text=True,
            check=True,
            capture_output=True,
        )

        with open(host_path, "r", newline="") as f:
            updated_lines = f.readlines()

        diff_lines = _extract_diff_lines_to_show(patch_content)
        diff_block_str = _format_diff_blocks(last_modified_path, updated_lines, diff_lines)

        return "Revert succeeded. Here are the updated parts of the file.\n\n" + diff_block_str

    except Exception as e:
        return f"Revert failed. The file was kept unchanged. Here is the message: {str(e)}"


def execute_tests(
    *,
    host_repo_dir: str,
    image_name: str,
    sec_timeout: int,
    mem_limit: str,
) -> Dict[str, Union[str, Optional[int]]]:
    """Run tests. Uses Docker if available, otherwise runs via venv directly."""
    import shutil as _shutil

    # Check if Docker is available
    docker_available = False
    if _shutil.which("docker"):
        try:
            subprocess.run(["docker", "info"], capture_output=True, timeout=5, check=True)
            docker_available = True
        except Exception:
            pass

    if docker_available:
        return _execute_tests_docker(
            host_repo_dir=host_repo_dir, image_name=image_name,
            sec_timeout=sec_timeout, mem_limit=mem_limit,
        )
    else:
        return _execute_tests_venv(
            host_repo_dir=host_repo_dir, image_name=image_name,
            sec_timeout=sec_timeout,
        )


def _execute_tests_venv(
    *,
    host_repo_dir: str,
    image_name: str,
    sec_timeout: int,
) -> Dict[str, Union[str, Optional[int]]]:
    """Run tests directly using the venv created during setup."""
    try:
        venv_python = str(Path(host_repo_dir) / ".venv" / "bin" / "python")

        # Find the test script
        # image_name is like "brmc__shortbus_new", strip the "_new" suffix
        base_name = image_name.replace("_new", "")
        test_script = Path(host_repo_dir) / f"test_{base_name}.sh"

        if not test_script.exists():
            # Try to find any test script
            candidates = list(Path(host_repo_dir).glob("test_*.sh"))
            if candidates:
                test_script = candidates[0]
            else:
                return {
                    "test_result": "No test script found.",
                    "full_log": None,
                    "container_status": 1,
                }

        # Parse the test script to extract the test command
        test_cmd = None
        with open(test_script) as f:
            for line in f:
                line = line.strip()
                if not line or line.startswith("#") or line.startswith("set "):
                    continue
                test_cmd = line
                break

        if not test_cmd:
            return {
                "test_result": "Test script is empty.",
                "full_log": None,
                "container_status": 1,
            }

        # Replace python/python3 with venv python
        test_cmd = test_cmd.replace("python3 ", f"{venv_python} ").replace("python ", f"{venv_python} ")

        print(f"[NO-DOCKER] Running tests: {test_cmd}", flush=True)

        result = subprocess.run(
            test_cmd,
            shell=True,
            cwd=host_repo_dir,
            text=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            timeout=sec_timeout,
        )

        test_log = result.stdout
        container_status = result.returncode

        # Truncate to last 100 lines with line numbers
        lines = test_log.splitlines(keepends=True)
        num_lines = len(lines)
        lines_to_show = lines[-100:]
        start_line = num_lines - len(lines_to_show) + 1

        truncated_log = "".join(
            f"{i}: {line}" for i, line in enumerate(lines_to_show, start=start_line)
        )
        lines_before = max(0, num_lines - 100)
        if lines_before > 0:
            truncated_log = f"({lines_before} lines above)\n" + truncated_log
        truncated_log = truncated_log.rstrip()

        return {
            "test_result": (
                "Test execution completed. Here is the test log.\n\n"
                f"<test_log>\n{truncated_log}\n</test_log>"
            ),
            "full_log": test_log,
            "container_status": container_status,
        }

    except subprocess.TimeoutExpired:
        return {
            "test_result": f"Test execution timed out after {sec_timeout}s.",
            "full_log": None,
            "container_status": 1,
        }
    except Exception as e:
        return {
            "test_result": f"Test execution failed: {e}",
            "full_log": None,
            "container_status": None,
        }


def _execute_tests_docker(
    *,
    host_repo_dir: str,
    image_name: str,
    sec_timeout: int,
    mem_limit: str,
) -> Dict[str, Union[str, Optional[int]]]:
    """Run the Docker container and return truncated log + exit code."""
    try:
        container_name = image_name

        command = [
            "timeout",
            "--foreground",
            "-s",
            "KILL",
            str(sec_timeout),
            "docker",
            "run",
            "-v",
            f"{host_repo_dir}:/work",
            f"--memory={mem_limit}",
            "--name",
            image_name,
            container_name,
        ]

        result = subprocess.run(
            command,
            text=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            check=False,
        )

        test_log = result.stdout
        container_status = result.returncode

        # truncate: last 100 lines
        lines = test_log.splitlines(keepends=True)
        num_lines = len(lines)

        lines_to_show = lines[-100:]
        start_line = num_lines - len(lines_to_show) + 1

        truncated_log = "".join(
            f"{i}: {line}" for i, line in enumerate(lines_to_show, start=start_line)
        )
        lines_before = max(0, num_lines - 100)

        if lines_before > 0:
            truncated_log = f"({lines_before} lines above)\n" + truncated_log

        truncated_log = truncated_log.rstrip()

        return {
            "test_result": (
                "Test execution completed. Here is the test log.\n\n"
                f"<test_log>\n{truncated_log}\n</test_log>"
            ),
            "full_log": test_log,
            "container_status": container_status,
        }

    except Exception:
        return {
            "test_result": "Test execution failed.",
            "full_log": None,
            "container_status": None,
        }

    finally:
        try:
            subprocess.run(["docker", "rm", "-f", container_name], capture_output=True)
        except Exception:
            pass


def search_last_log(
    *,
    regex_pattern: str,
    last_log_path: str,
) -> str:
    """Search the last test log for *regex_pattern*."""
    matches: list[tuple[int, str]] = []

    try:
        ptn = re.compile(regex_pattern)

        with open(last_log_path, "r", newline="") as f:
            for i, line in enumerate(f, start=1):
                line = line.rstrip()
                if ptn.search(line):
                    matches.append((i, line))

        if not matches:
            return f'No matches found for "{regex_pattern}" in the last test log.'

        num_matches = len(matches)

        if num_matches > 50:
            return (
                f'More than 50 matches found for "{regex_pattern}" in the last test log.'
                " Please write a more specific query to narrow down the results."
            )

        result_str = f'Found {len(matches)} match(es) for "{regex_pattern}" in the last test log:\n\n'
        result_str += "\n".join(f"{i}: {line}" for i, line in matches[:50])

        return result_str

    except Exception:
        return f'Search failed for "{regex_pattern}" in the last test log.'


def view_last_log(
    *,
    line_no: int,
    last_log_path: str,
) -> str:
    """Show ±50 lines around *line_no* in the last test log."""
    start_line, end_line = max(1, line_no - 50), line_no + 50

    target_lines: list[str] = []

    try:
        with open(last_log_path, "r", newline="") as f:
            for i, line in enumerate(f, start=1):
                if start_line <= i <= end_line:
                    target_lines.append(line)

        log_content = "".join(
            f"{i}: {line}" for i, line in enumerate(target_lines, start=start_line)
        ).rstrip()

        return (
            f"Here is the content of the last test log around line #{line_no}.\n\n"
            f"<test_log>\n{log_content}\n</test_log>"
        )

    except Exception:
        return "Error opening the last test log."