Fix code repair for raw (non-markdown) source code input

The Python client sends raw source code, not markdown-wrapped blocks.
split_markdown_code() returned {} for raw input, making SearchAndReplaceDiff
have nothing to patch, so repairs always returned empty string.

Now falls back to {"file.py": raw_code} when markdown parsing yields nothing,
and is_valid() handles raw code blocks instead of only markdown-wrapped ones.
This commit is contained in:
Kevin Turcios 2026-04-21 20:42:01 -05:00
parent 34eb74342d
commit 3d1b843da4

View file

@ -126,29 +126,33 @@ class CodeRepairContext:
def apply_patches_to_optimized_code(self, llm_res: str) -> str:
file_to_code = split_markdown_code(self.data.modified_source_code, self.data.language)
raw_input = not file_to_code
if raw_input:
file_to_code = {"file.py": self.data.modified_source_code}
diff: Diff = SearchAndReplaceDiff(content=llm_res, source_code=file_to_code)
file_to_code = diff.run()
if raw_input:
return file_to_code.get("file.py", "")
return group_code(file_to_code, self.data.language)
def is_valid(self, new_refined_code: str) -> bool:
code_blocks = split_markdown_code(new_refined_code, self.data.language)
if code_blocks:
if is_markdown_structure_changed(new_refined_code, self.data.modified_source_code, self.data.language):
return False
valid = True
for code in split_markdown_code(new_refined_code, self.data.language).values():
code_items = code_blocks.values()
else:
code_items = [new_refined_code]
for code in code_items:
stripped_code = code.strip()
if not stripped_code:
valid = False
break
# Only validate Python syntax with libcst
return False
if self.data.language == "python":
try:
parse_module_to_cst(code)
except cst.ParserSyntaxError:
valid = False
break
# For JavaScript/TypeScript, basic validation (non-empty code)
# More sophisticated validation could be added later
return valid
return False
return True
def validate_module(self) -> None:
"""Validate the module syntax based on language."""