MCPcopy Index your code
hub / github.com/showlab/Code2Video / fix_code_smart

Method fix_code_smart

src/scope_refine.py:483–516  ·  view source on GitHub ↗

Smart fix code, prioritize local fix, fallback to complete rewrite if failed

(self, section_id: str, code: str, error_msg: str, output_dir: Path)

Source from the content-addressed store, hash-verified

481 )
482
483 def fix_code_smart(self, section_id: str, code: str, error_msg: str, output_dir: Path) -> Optional[str]:
484 """Smart fix code, prioritize local fix, fallback to complete rewrite if failed"""
485
486 # Analyze error
487 error_info = self.analyzer.analyze_error(code, error_msg)
488 # Decide on fix scope based on error analysis
489 if error_info["fix_scope"] in ["single_line", "function", "section"]:
490
491 relevant_code = error_info.get("relevant_code_block")
492 if relevant_code:
493 fixed_block = self._fix_code_block(section_id, relevant_code, error_msg, error_info)
494 if fixed_block:
495 merged_code = self._merge_fixed_block(code, relevant_code, fixed_block, error_info)
496 if merged_code:
497 is_valid, syntax_error = self.validate_code_syntax(merged_code)
498 if is_valid:
499 is_dry_run_ok, dry_run_error = self.dry_run_test(merged_code, section_id, output_dir)
500 if is_dry_run_ok:
501 return merged_code
502 else:
503 print(f"⚠️ The dry run failed after local repair: {dry_run_error}")
504 else:
505 print(f"⚠️ The syntax error after local repair: {syntax_error}")
506 else:
507 print("⚠️ The code block merge failed after local repair")
508 else:
509 print("⚠️ The local repair failed after local repair")
510 else:
511 print("⚠️ The relevant code block cannot be extracted after local repair")
512 else:
513 print("🔄 The error scope is large, directly use complete repair")
514
515 print("⚠️ The smart repair failed, fallback to complete repair")
516 return self.fix_code_with_multi_stage_validation(section_id, code, error_msg, output_dir)
517
518 def fix_code_with_multi_stage_validation(
519 self, section_id: str, current_code: str, error_msg: str, output_dir: Path, max_attempts: int = 3

Callers 1

debug_and_fix_codeMethod · 0.80

Calls 6

_fix_code_blockMethod · 0.95
_merge_fixed_blockMethod · 0.95
validate_code_syntaxMethod · 0.95
dry_run_testMethod · 0.95
analyze_errorMethod · 0.80

Tested by

no test coverage detected