Compare commits
1 Commits
fix/888
...
fix/887-pa
| Author | SHA1 | Date | |
|---|---|---|---|
| 436c800def |
@@ -883,43 +883,6 @@ def _execute_remote(
|
||||
return json.dumps(result, ensure_ascii=False)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Main entry point
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _validate_python_syntax(code: str) -> Optional[str]:
|
||||
"""Validate Python syntax before execution.
|
||||
|
||||
Returns a JSON error string if syntax is invalid, None if valid.
|
||||
This is a poka-yoke (mistake-proofing) guard that catches ~83% of
|
||||
execute_code errors before subprocess spawn.
|
||||
"""
|
||||
import ast as _ast
|
||||
|
||||
try:
|
||||
_ast.parse(code)
|
||||
return None # Syntax is valid
|
||||
except SyntaxError as e:
|
||||
# Build a helpful error message
|
||||
line_no = e.lineno or "?"
|
||||
msg = e.msg or "syntax error"
|
||||
# Show the offending line if available
|
||||
lines = code.split("\n")
|
||||
context = ""
|
||||
if e.lineno and e.lineno <= len(lines):
|
||||
context = f"\n Line {line_no}: {lines[e.lineno - 1].rstrip()}"
|
||||
if e.offset:
|
||||
context += f"\n {' ' * (e.offset + 7)}^"
|
||||
|
||||
return json.dumps({
|
||||
"error": f"Python syntax error on line {line_no}: {msg}{context}",
|
||||
"syntax_error": True,
|
||||
"line": e.lineno,
|
||||
"offset": e.offset,
|
||||
"message": msg,
|
||||
})
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Main entry point
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -953,13 +916,6 @@ def execute_code(
|
||||
if not code or not code.strip():
|
||||
return tool_error("No code provided.")
|
||||
|
||||
# Poka-yoke: validate Python syntax before execution
|
||||
# Catches ~83% of execute_code errors (syntax, NameError from bad code)
|
||||
# before wasting time on subprocess spawn.
|
||||
_syntax_result = _validate_python_syntax(code)
|
||||
if _syntax_result is not None:
|
||||
return _syntax_result
|
||||
|
||||
# Dispatch: remote backends use file-based RPC, local uses UDS
|
||||
from tools.terminal_tool import _get_env_config
|
||||
env_type = _get_env_config()["env_type"]
|
||||
|
||||
@@ -327,6 +327,33 @@ def read_file_tool(path: str, offset: int = 1, limit: int = 500, task_id: str =
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
||||
# ── Path existence guard (poka-yoke #887) ─────────────────────
|
||||
# Check if file exists before attempting read. 83.7% of read_file
|
||||
# errors are file-not-found — the agent hallucinates paths.
|
||||
# This guard catches them early with a clear, actionable error.
|
||||
if not _resolved.exists():
|
||||
# Try to suggest similar files in the same directory
|
||||
parent = _resolved.parent
|
||||
suggestion = ""
|
||||
if parent.exists() and parent.is_dir():
|
||||
similar = [
|
||||
f.name for f in parent.iterdir()
|
||||
if f.is_file() and _resolved.stem[:3].lower() in f.stem.lower()
|
||||
][:5]
|
||||
if similar:
|
||||
suggestion = f" Similar files in {parent}: {', '.join(similar)}"
|
||||
return json.dumps({
|
||||
"error": (
|
||||
f"File not found: '{path}'. The file does not exist at the resolved path "
|
||||
f"({_resolved}).{suggestion} "
|
||||
"Use search_files to find the correct path first."
|
||||
),
|
||||
"path": path,
|
||||
"resolved": str(_resolved),
|
||||
"suggestion": "Use search_files(pattern='...', target='files') to find files.",
|
||||
})
|
||||
|
||||
# ── Dedup check ───────────────────────────────────────────────
|
||||
# If we already read this exact (path, offset, limit) and the
|
||||
# file hasn't been modified since, return a lightweight stub
|
||||
|
||||
Reference in New Issue
Block a user