5. Validate and VERIFY COMPLETENESS of the file:
   a. Re-read the file with the ``file`` tool and confirm it ends with a complete ``tools = [ToolDef(...)]`` (or ``tools = [tool(...)]``) list and is not cut off mid-statement.
   b. Syntax-check: ``python -c "compile(open('<path>').read(), '<path>', 'exec')"``.
   c. If the file is truncated or fails to compile, do NOT re-emit the whole file. Instead continue with ``file`` ``operation="edit"`` (append the missing functions) or rewrite with ``overwrite=True``. Repeat until it compiles and the ``tools`` list is complete.
