fix: ensure generated code is not copied as-is, may contain partial code generated by the other model

This commit is contained in:
Fahad
2025-10-17 08:44:59 +04:00
parent b9408aae88
commit 3acfffe0f6

View File

@@ -246,7 +246,20 @@ class ChatTool(SimpleTool):
body = f"{sanitized_warning}\n\n{block.strip()}".strip() body = f"{sanitized_warning}\n\n{block.strip()}".strip()
else: else:
if not sanitized_text: if not sanitized_text:
sanitized_text = "Generated code saved to zen_generated.code. Follow the structured instructions in that file exactly before continuing." sanitized_text = (
"Generated code saved to zen_generated.code.\n"
"\n"
"CRITICAL: Contains mixed instructions + partial snippets - NOT complete code to copy as-is!\n"
"\n"
"You MUST:\n"
" 1. Read as a proposal from partial context - you may need to read the file in sections\n"
" 2. Implement ideas using YOUR complete codebase context and understanding\n"
" 3. Never paste wholesale - snippets may be partial with missing lines, pasting will corrupt your code!\n"
" 4. Adapt to fit your actual structure and style\n"
" 5. Build/lint/test after implementation to verify correctness\n"
"\n"
"Treat as guidance to implement thoughtfully, not ready-to-paste code."
)
instruction = self._build_agent_instruction(artifact_path) instruction = self._build_agent_instruction(artifact_path)
body = self._join_sections(sanitized_text, instruction) body = self._join_sections(sanitized_text, instruction)
@@ -323,26 +336,19 @@ class ChatTool(SimpleTool):
@staticmethod @staticmethod
def _build_agent_instruction(artifact_path: Path) -> str: def _build_agent_instruction(artifact_path: Path) -> str:
return ( return (
f"CONTINUING FROM PREVIOUS DISCUSSION: The coding assistant has analyzed our conversation context and generated " f"CONTINUING FROM PREVIOUS DISCUSSION: Implementation plan saved to `{artifact_path}`.\n"
f"a structured implementation plan at `{artifact_path}`. This is a direct continuation of our discussion—all previous "
"context, requirements, and shared code remain relevant.\n"
"\n" "\n"
f"MANDATORY NEXT STEP: Open `{artifact_path}` immediately and review the implementation plan:\n" f"CRITICAL WARNING: `{artifact_path}` may contain partial code snippets from another AI with limited context. "
"1. Read the step-by-step instructions—they reference our previous discussion. You may need to read the file in parts if it's too long.\n" "Wholesale copy-pasting MAY CORRUPT your codebase with incomplete logic and missing lines.\n"
"2. Review each <NEWFILE:…> or <UPDATED_EXISTING_FILE:…> section in the context of what we've discussed\n"
"3. Verify the proposed changes align with the requirements and code we've already shared\n"
"4. Check for syntax errors, missing imports, or incomplete implementations\n"
"\n" "\n"
"Then systematically apply the changes:\n" "Required workflow:\n"
"- Create new files or update existing ones as instructed, maintaining code style consistency\n" "1. For <UPDATED_EXISTING_FILE:...> blocks: Partial excerpts only. Understand the intent and implement using YOUR full context. "
"- If updating existing code we discussed earlier, carefully preserve unmodified sections\n" "DO NOT copy wholesale - adapt ideas to fit actual structure.\n"
"- Run syntax validation after each modification\n" "2. For <NEWFILE:...> blocks: Understand proposal and create properly. Verify completeness (imports, syntax, logic).\n"
"- Execute relevant tests to confirm functionality\n" "3. Validation: After ALL changes, verify correctness using available tools (build/compile, linters, tests, type checks, etc.).\n"
"- Verify the implementation works end-to-end with existing code\n" f"4. Cleanup: After you're done reading and applying changes, delete `{artifact_path}` once verified to prevent stale instructions.\n"
"\n" "\n"
"Remember: This builds upon our conversation. The generated code reflects the full context of what we've discussed, " "Treat this as a patch-set requiring manual integration, not ready-to-paste code. You have full codebase context - use it."
"including any files, requirements, or constraints mentioned earlier. Proceed with implementation immediately."
"Only after you finish applying ALL the changes completely: delete `zen_generated.code` so stale instructions do not linger."
) )
@staticmethod @staticmethod