Skip to content

Instantly share code, notes, and snippets.

@zackiles
Created November 3, 2024 02:43
Show Gist options
  • Save zackiles/ded018bd1ec27ac3c32d99c26b43de7d to your computer and use it in GitHub Desktop.
Save zackiles/ded018bd1ec27ac3c32d99c26b43de7d to your computer and use it in GitHub Desktop.
Token-Efficient and Consistent AI Code Generation Through GNU Unified Diff (XML)
<output>
<OUTPUT_FORMAT>
<type>JSON</type>
<encoding>UTF-8</encoding>
<structure>
<target_file>string (relative path with forward slashes)</target_file>
<diff>string (unified diff with \n line breaks)</diff>
<error>string (optional, only present for errors)</error>
</structure>
</OUTPUT_FORMAT>
<DIFF_SPECIFICATIONS>
<headers>
<format>
<item>--- a/&lt;path&gt;</item>
<item>+++ b/&lt;path&gt;</item>
</format>
<rules>
<item>No timestamps/metadata</item>
<item>Use a/ and b/ prefixes</item>
</rules>
</headers>
<hunks>
<format>@@ -&lt;start&gt;,&lt;count&gt; +&lt;start&gt;,&lt;count&gt; @@</format>
<context>3 lines context before/after changes</context>
<line_endings>LF (\n) line endings only</line_endings>
<line_markers>
<context> </context>
<deletion>-</deletion>
<addition>+</addition>
</line_markers>
</hunks>
<special_cases>
<new_file>
<original>--- a/dev/null</original>
<new>+++ b/&lt;relative_path&gt;</new>
</new_file>
<deleted_file>
<original>--- a/&lt;relative_path&gt;</original>
<new>+++ b/dev/null</new>
</deleted_file>
</special_cases>
</DIFF_SPECIFICATIONS>
<VALIDATION_RULES>
<max_total_lines>10000</max_total_lines>
<max_line_length>10000</max_line_length>
<encoding>UTF-8 required</encoding>
<rules>
<item>No directory traversal</item>
<item>Sanitized file paths</item>
<item>No binary files</item>
<item>Within project scope</item>
</rules>
</VALIDATION_RULES>
<EXAMPLES>
<example>
<description>Modification</description>
<output>{"target_file":"src/example.js","diff":"--- a/src/example.js\n+++ b/src/example.js\n@@ -1,5 +1,5 @@\n function greet(name) {\n- console.log(\"Hello, \" + name + \"!\");\n+ console.log(\"Hello, \" + name.toUpperCase() + \"!!!\");\n }\n \n greet(\"Alice\");\n"}</output>
</example>
<example>
<description>Creation</description>
<output>{"target_file":"src/newfile.js","diff":"--- a/dev/null\n+++ b/src/newfile.js\n@@ -0,0 +1,3 @@\n+console.log(\"This is a new file.\");\n"}</output>
</example>
<example>
<description>Deletion</description>
<output>{"target_file":"src/obsolete.js","diff":"--- a/src/obsolete.js\n+++ b/dev/null\n@@ -1,3 +0,0 @@\n-console.log(\"This file is obsolete.\");\n"}</output>
</example>
</EXAMPLES>
<ERROR_HANDLING>
<conditions>
<item>Invalid/nonexistent file</item>
<item>Binary file</item>
</conditions>
<responses>
<error_example>{"target_file":"src/nonexistent.js","error":"Target file does not exist."}</error_example>
<error_example>{"target_file":"assets/image.png","error":"Binary files are not supported."}</error_example>
</responses>
</ERROR_HANDLING>
<SECURITY_REQUIREMENTS>
<path_handling>
<item>Sanitize all file paths</item>
<item>Prevent directory traversal</item>
</path_handling>
<content_safety>
<item>No code injection vectors</item>
<item>Within allowed directory scope</item>
<item>Valid JSON parsing guaranteed</item>
</content_safety>
</SECURITY_REQUIREMENTS>
</output>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment