File size: 2,452 Bytes
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
"""
read_file tool — returns the current content of a file in the task workdir.

Allows the agent to inspect source files before deciding what to edit,
rather than guessing from test error messages alone.
"""

import logging
import os

from pydantic import ValidationError

from debug_env.server.schemas import ReadFileArgs, ToolResult
from debug_env.server.utils import validate_workdir_path

logger = logging.getLogger(__name__)


def read_file(workdir: str, path: str) -> ToolResult:
    """
    Read *path* (relative to *workdir*) and return its content in ``logs``.

    Pass rate is preserved from the last test run — reading a file does not
    affect the score.  The content is returned in ``logs`` so the agent can
    inspect it in the same observation field it already reads.

    Args:
        workdir:  Absolute path to the task working directory.
        path:     Filename to read, e.g. ``"broken_code.py"`` or ``"helper.py"``.

    Returns:
        :class:`ToolResult` with ``pass_rate=0.0`` and file content in ``logs``.
    """
    try:
        args = ReadFileArgs(path=path)
    except ValidationError as e:
        messages = [f"{' -> '.join(str(loc) for loc in err['loc'])}: {err['msg']}" for err in e.errors()]
        msg = "Invalid read_file args — " + "; ".join(messages)
        logger.warning(msg)
        return ToolResult(pass_rate=0.0, logs=msg, success=False)

    try:
        full_path = validate_workdir_path(workdir, args.path)
    except ValueError as e:
        logger.warning(str(e))
        return ToolResult(pass_rate=0.0, logs=str(e), success=False)

    logger.info(f"Reading file: {full_path}")

    if not os.path.exists(full_path):
        available = [f for f in os.listdir(workdir) if f.endswith(".py") and not f.startswith("test_")]
        msg = (
            f"File '{args.path}' not found. "
            f"Editable source files: {available}"
        )
        logger.warning(msg)
        return ToolResult(pass_rate=0.0, logs=msg, success=False)

    try:
        with open(full_path) as f:
            content = f.read()
        logger.info(f"Read {len(content)} chars from '{args.path}'")
        return ToolResult(
            pass_rate=0.0,
            logs=f"# {args.path}\n{content}",
            success=True,
        )
    except OSError as e:
        msg = f"Failed to read '{args.path}': {e}"
        logger.error(msg)
        return ToolResult(pass_rate=0.0, logs=msg, success=False)