Issue
A code generation system was producing duplicate full code blocks in its output documentation instead of logical segments. For example, a single Python script would appear twice in the generated Markdown: once as the initial code block and again as the first segment, due to flawed splitting logic.
Culprit
The algorithm responsible for segmenting code lacked proper boundary detection. It failed to:
- Recognize blank lines as segment separators.
- Handle decorators and their associated functions correctly.
- Generate empty or overly broad segments.
Solution
The splitting algorithm was rewritten to:
- Use blank lines as explicit segment boundaries.
- Group structural elements (imports, functions, main blocks) logically.
- Preserve decorators with their functions.
- Eliminate empty segments and trailing whitespace.
This ensured that even minimally complex scripts now produce 4 meaningful segments instead of 2.