/
Watashicuvu
/
agentic-tools
Обзор
Документация
Войти
/
Watashicuvu
/
agentic-tools
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/eval/lua_evaluator.py
130 строк
4 KB
Якуб
cli fixes
14 апр 2026, 18:32
14 апр 2026, 18:32
6202d02
Код
Авторство
О чём код?
"""Lua code evaluator — syntax check, linting, and execution. Validates Lua code using: 1. `luac -p` — syntax validation 2. `luacheck` — static analysis / linting 3. `lua` — runtime execution with timeout All operations are async with proper timeout handling. LuaCodeEvaluator inherits from CodeEvaluator for unified interface. """ from __future__ import annotations from pathlib import Path from typing import Optional from src.eval.code_evaluator import CodeEvaluator, CodeEvalResult, LintResult class LuaCodeEvaluator(CodeEvaluator): """Lua-specific code evaluator. Implements language-specific methods for Lua: - Syntax: luac -p - Lint: luacheck - Run: lua Args: timeout: Maximum seconds for each subprocess call. luacheck_config: Optional path to .luacheckrc config file. """ def __init__( self, timeout: int = 30, luacheck_config: Optional[str] = None, ) -> None: super().__init__(timeout=timeout) self.luacheck_config = luacheck_config def _language_name(self) -> str: return "Lua" def _file_extension(self) -> str: return ".lua" def _syntax_command(self, file_path: str) -> list[str]: return ["luac", "-p", file_path] def _lint_command(self, file_path: str) -> list[str]: cmd = ["luacheck", file_path] if self.luacheck_config: cmd.extend(["--config", self.luacheck_config]) return cmd def _run_command(self, file_path: str) -> list[str]: return ["lua", file_path] def _parse_lint_errors(self, returncode: int, stdout: str, stderr: str) -> list[str]: """Parse luacheck error lines.""" import re errors = [] if returncode != 0: combined = stdout + stderr # Strip ANSI escape codes ansi_strip = re.compile(r'\x1b\[[0-9;]*m') combined = ansi_strip.sub('', combined) for line in combined.strip().splitlines(): # Skip summary lines like "Total: 2 warnings / 0 errors" if line.startswith("Total:"): continue # luacheck: "file:line:col: message" if ":" in line and ("warning" in line.lower() or "error" in line.lower()): errors.append(line.strip()) return errors async def _run_linter( self, code: str, work_dir: Optional[Path], ) -> LintResult: """Run luacheck static analysis. luacheck returns 1 for warnings too — only fail on actual errors. """ temp_file = await self._write_temp_file(code, "temp_lint", work_dir) try: cmd = self._lint_command(str(temp_file)) returncode, stdout, stderr = await self._run_subprocess(cmd) errors = self._parse_lint_errors(returncode, stdout, stderr) # luacheck returns 1 for warnings too — only fail on actual errors has_errors = any("error" in e.lower() and "warning" not in e.lower() for e in errors) ok = returncode == 0 or not has_errors return LintResult( ok=ok, stdout=stdout, stderr=stderr, errors=errors, ) finally: await self._cleanup_temp_file(temp_file) # Backward compatibility alias LuaEvaluator = LuaCodeEvaluator # Module-level convenience function async def evaluate_lua_code( code: str, work_dir: Optional[str] = None, run_code: bool = True, timeout: int = 30, ) -> CodeEvalResult: """Evaluate Lua code in a single call. Args: code: Lua source code. work_dir: Working directory for execution. run_code: Whether to execute the code. timeout: Execution timeout in seconds. Returns: CodeEvalResult with validation results. """ evaluator = LuaCodeEvaluator(timeout=timeout) return await evaluator.evaluate(code, work_dir=work_dir, run_code=run_code)