Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(generation): stop SCC pages from archiving a spurious version on init (#2873)

Raghav Chamadiyaa914ca863181
diff --git a/packages/core/src/repowise/core/generation/page_generator/orchestrate.py b/packages/core/src/repowise/core/generation/page_generator/orchestrate.py
index e9d5ef567..8aa14217e 100644
--- a/packages/core/src/repowise/core/generation/page_generator/orchestrate.py
+++ b/packages/core/src/repowise/core/generation/page_generator/orchestrate.py
@@ -22,6 +22,7 @@ import structlog
from ...persistence.vector_store import embed_item
from ...pipeline.phase_timing import timed
from ..context_assembler import FilePageContext
+from ..mermaid_safety import sanitize_pages
from ..models import (
STRUCTURALLY_KEYED_PAGE_TYPES,
STUB_FALLBACK_ERROR,
@@ -641,6 +642,14 @@ class _GenerationRun:
result = await coro
if isinstance(result, GeneratedPage):
+ # Sanitized before the streaming sink sees it, so the
+ # first stored version is the final text. Left to the
+ # post-generation pass alone, any page it changes was
+ # written twice and archived a spurious version.
+ try:
+ sanitize_pages([result])
+ except Exception as exc:
+ log.debug("mermaid_safety.failed", error=str(exc))
# A page whose provider call raised comes back as its
# structural stub rather than being dropped (issue #1089),
# so the row exists and `repowise generate` can refill it.
@@ -883,8 +892,6 @@ class _GenerationRun:
# Post-generation: repair mermaid diagrams so illegal node IDs / unquoted
# labels in LLM output don't break the whole diagram in the renderer.
try:
- from ..mermaid_safety import sanitize_pages
-
fixed = sanitize_pages(all_pages)
if fixed:
log.info("mermaid_safety.applied", pages_changed=fixed)
diff --git a/packages/core/src/repowise/core/generation/templates/scc_page.j2 b/packages/core/src/repowise/core/generation/templates/scc_page.j2
index c49663909..10303df93 100644
--- a/packages/core/src/repowise/core/generation/templates/scc_page.j2
+++ b/packages/core/src/repowise/core/generation/templates/scc_page.j2
@@ -7,7 +7,7 @@
-#}
{#- ``default(..., true)`` so an absent *or* empty title both fall back: the
caller names the whole set at once and a caller that does not is still
- entitled to a page. #}
+ entitled to a page. -#}
# {{ page_title | default(labels.circular_dependency ~ ": " ~ ctx.scc_id, true) }}
{{ labels.cycle_overview.format(count=ctx.files | length) }}
diff --git a/tests/unit/generation/test_deterministic_templates.py b/tests/unit/generation/test_deterministic_templates.py
index c9726c453..4a5d65501 100644
--- a/tests/unit/generation/test_deterministic_templates.py
+++ b/tests/unit/generation/test_deterministic_templates.py
@@ -600,6 +600,20 @@ def test_scc_page_renders_german(german_generator):
assert "**Symbole insgesamt im Zyklus:** 2" in page.content
+def test_scc_page_starts_at_its_heading(generator):
+ """A stray newline ahead of the heading is stripped after the page was
+ already streamed to the store, so every SCC page was written twice and
+ archived a version nobody made."""
+ from repowise.core.generation.mermaid_safety import sanitize_pages
+
+ page = generator._structural_scc_page(
+ _scc_ctx(), "scc-001", structural_page_title("en", "scc_page", "scc-001")
+ )
+
+ assert page.content.startswith("# ")
+ assert sanitize_pages([page]) == 0
+
+
def test_the_footer_is_localized_on_every_structural_page(german_generator):
ctx = _file_ctx()
page = german_generator._structural_page(
diff --git a/tests/unit/generation/test_run_level_stub_fallback.py b/tests/unit/generation/test_run_level_stub_fallback.py
index aec3c3dcb..ad024e603 100644
--- a/tests/unit/generation/test_run_level_stub_fallback.py
+++ b/tests/unit/generation/test_run_level_stub_fallback.py
@@ -131,3 +131,31 @@ def test_stub_fallback_is_kept_out_of_the_resume_ledger() -> None:
embedded = [pid for batch in store.batches for (pid, *_rest) in batch]
assert embedded == ["module_page:ok"]
+
+
+def test_the_streaming_sink_receives_the_sanitized_page() -> None:
+ """The sink writes the first stored version. Sanitizing only after the
+ level made the final pass rewrite the page and archive a spurious one."""
+ seen: list[str] = []
+
+ async def _go():
+ async def preamble():
+ page = _page("module_page:p")
+ page.content = "\n# module_page:p\n\nbody"
+ return page
+
+ run = SimpleNamespace(
+ semaphore=asyncio.Semaphore(1),
+ job_system=None,
+ job_id=None,
+ on_page_done=None,
+ on_page_ready=lambda page: seen.append(page.content),
+ vector_store=_RecordingStore(),
+ completed_page_summaries={},
+ timings=None,
+ )
+ await _GenerationRun.run_level(run, [("module_page:p", preamble())], level=4)
+
+ asyncio.run(_go())
+
+ assert seen == ["# module_page:p\n\nbody"]