Skip to content

fix(knowledge): keep leading indent of indented code blocks in chunks - #390

Merged
petertzy merged 3 commits into
petertzy:mainfrom
harsh-thakkar7:fix/knowledge-chunk-strips-leading-indent
Oct 8, 2026
Merged

petertzy merged 3 commits into
petertzy:mainfrom
harsh-thakkar7:fix/knowledge-chunk-strips-leading-indent

Conversation

@harsh-thakkar7

Copy link
Copy Markdown
Contributor

What

Chunk building trimmed each section/paragraph with str.strip(), which removes the leading indentation of the first line of a 4-space indented code block:

## Setup

    def setup():      <- chunk stored as "def setup():"

The stored chunk and the AI prompt context both re-flowed the code as though the indentation never existed.

Fix

Trim surrounding blank lines instead of all whitespace (strip("\n").rstrip()), preserving interior indentation — in the section path, the paragraph-split path (large sections), and the prompt-context builder.

Tests

  • tests/test_knowledge_logic.py: the first code line keeps its indent in the small-section path, the large-section (paragraph-split) path, and the prompt context.
  • Full backend suite passes (472 tests). Ruff clean.

harsh-thakkar7 and others added 3 commits October 8, 2026 16:26
Chunk building stripped each section/paragraph with str.strip(), which
removed the leading indentation of the first line of a 4-space indented
code block. That re-flowed the code in the stored chunk and again when the
chunk was injected into an AI prompt. Trim surrounding blank lines
instead, preserving interior indentation.
@petertzy

petertzy commented Oct 8, 2026

Copy link
Copy Markdown
Owner

Approved — this is a necessary correctness fix. Chunk construction and prompt-context generation now preserve the leading indentation required for indented Markdown code blocks, preventing code from being reflowed as prose.
I also fixed the remaining entry-point and post-frontmatter paths, where an initial indented code block could still lose its first-line indentation. The regression coverage now includes small sections, paragraph-split chunks, prompt context, Overview chunks, and frontmatter bodies. Backend tests (485 passed), frontend tests (94/94), Ruff, ESLint, and TypeScript checks all pass.

@petertzy
petertzy merged commit 23fb4ed into petertzy:main Oct 8, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants