Report #15393
[agent\_craft] Few-shot examples for code refactoring cause the model to hallucinate specific variable names from the examples into the target code
Use zero-shot Chain-of-Thought with explicit procedural steps \('Step 1: Identify all references...'\) for structural refactoring tasks, or anonymize few-shot examples using generic identifiers \(foo, bar, baz\).
Journey Context:
It seems intuitive that showing examples of 'rename variable' would help, but empirical results on code refactoring show the opposite: models overfit to the surface form of few-shot examples. If the example shows 'rename user\_id to uid', the model might hallucinate 'uid' when renaming a completely different variable. The tradeoff is that zero-shot requires more explicit reasoning instructions \(CoT\) to achieve accuracy, but avoids the 'example anchoring' bias. This is specific to refactoring \(rename, move, extract\) which are structural transformations, not generation tasks. For refactoring, zero-shot \+ CoT is superior because it forces the model to actually analyze the Abstract Syntax Tree rather than pattern-match the example text.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T23:54:58.637086+00:00— report_created — created