Skip to content

Commit d805062

Browse files
feat(polish): emit polish: skipped frontmatter on lenient fallback (#33)
Surface a polish bypass in the rendered YAML frontmatter so the degradation shows up in PR diffs. Without this marker, raw-Jinja output is structurally identical to a normal generation (same headings, same frontmatter shape) and slips past review — see attune-author#30 and attune-rag d39e39d, where a regeneration with polish silently bypassed merged in degraded form. The marker is only added when polish falls back to returning raw content in lenient mode (no API key, swallowed LLM error). The success path and the cache-hit path are unchanged. Helper is idempotent: re-running polish on an already-marked file does not double-write. Non-YAML output (project-doc templates with HTML-comment footers) passes through unchanged — surfacing bypass there would need a different mechanism and is deliberately out of scope here. Tests: - ``test_lenient_bypass_marks_yaml_frontmatter`` — bypass injects the marker; body is preserved verbatim. - ``test_lenient_bypass_is_idempotent_when_already_marked`` — re-running does not double-write. - ``test_lenient_bypass_passes_through_non_frontmatter_content`` — non-YAML content unchanged. - Updated golden snapshots reflect the new marker (snapshots are recorded under the lenient-by-default conftest fixture). Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
1 parent a2f99c2 commit d805062

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/attune_author/polish.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,33 @@ def polish_template(
345345
template_type,
346346
exc,
347347
)
348+
return _mark_polish_skipped(content)
349+
350+
351+
def _mark_polish_skipped(content: str) -> str:
352+
# Surface a polish bypass in the rendered YAML frontmatter so
353+
# the regression shows up in PR diffs. Without this marker,
354+
# raw-Jinja output looks indistinguishable from a normal
355+
# generation (same headings, same frontmatter shape) and slips
356+
# past review — see attune-author#30 and attune-rag d39e39d.
357+
#
358+
# Only YAML frontmatter is supported. Non-YAML output (e.g.
359+
# project-doc templates with an HTML comment footer) passes
360+
# through unchanged; surfacing bypass there would need a
361+
# different mechanism.
362+
if not content.startswith("---\n"):
348363
return content
364+
end = content.find("\n---\n", 4)
365+
if end == -1:
366+
return content
367+
frontmatter = content[4:end]
368+
rest = content[end + 5 :]
369+
if "\npolish:" in "\n" + frontmatter:
370+
# Idempotent: don't double-write if a previous run already
371+
# marked the file.
372+
return content
373+
new_frontmatter = frontmatter.rstrip("\n") + "\npolish: skipped\n"
374+
return f"---\n{new_frontmatter}---\n{rest}"
349375

350376

351377
def _sanitize_output(content: str) -> str:

tests/__snapshots__/test_generated_templates_golden.ambr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
generated_at: <TIMESTAMP>
1010
source_hash: <HASH>
1111
status: generated
12+
polish: skipped
1213
---
1314

1415
# Auth
@@ -49,6 +50,7 @@
4950
generated_at: <TIMESTAMP>
5051
source_hash: <HASH>
5152
status: generated
53+
polish: skipped
5254
---
5355

5456
# Auth reference
@@ -81,6 +83,7 @@
8183
generated_at: <TIMESTAMP>
8284
source_hash: <HASH>
8385
status: generated
86+
polish: skipped
8487
---
8588

8689
# Work with auth

tests/test_polish.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,51 @@ def test_returns_original_on_llm_error_when_lenient(self) -> None:
9797
result = polish_template(content, "test", "summary", strict=False)
9898
assert result == content
9999

100+
def test_lenient_bypass_marks_yaml_frontmatter(self) -> None:
101+
# A polish bypass injects ``polish: skipped`` into the YAML
102+
# frontmatter so the regression is visible in PR diffs —
103+
# otherwise raw Jinja output is indistinguishable from a
104+
# normal generation. See attune-author#30.
105+
with patch.dict("os.environ", {}, clear=True):
106+
content = (
107+
"---\n"
108+
"type: reference\n"
109+
"feature: auth\n"
110+
"status: generated\n"
111+
"---\n"
112+
"\n# Auth reference\n"
113+
)
114+
result = polish_template(content, "auth", "summary", strict=False)
115+
assert "polish: skipped" in result
116+
# Original body is preserved verbatim — only the
117+
# frontmatter is annotated.
118+
assert "# Auth reference" in result
119+
120+
def test_lenient_bypass_is_idempotent_when_already_marked(self) -> None:
121+
# Re-running polish on an already-marked file should not
122+
# double-write the marker.
123+
with patch.dict("os.environ", {}, clear=True):
124+
content = (
125+
"---\n"
126+
"type: reference\n"
127+
"status: generated\n"
128+
"polish: skipped\n"
129+
"---\n"
130+
"\n# Body\n"
131+
)
132+
result = polish_template(content, "auth", "summary", strict=False)
133+
assert result.count("polish: skipped") == 1
134+
135+
def test_lenient_bypass_passes_through_non_frontmatter_content(self) -> None:
136+
# Content without YAML frontmatter (e.g., project-doc
137+
# templates with an HTML comment footer) is returned
138+
# unchanged — surfacing bypass there would need a
139+
# different mechanism.
140+
with patch.dict("os.environ", {}, clear=True):
141+
content = "# Test\nNo frontmatter here.\n"
142+
result = polish_template(content, "test", "summary", strict=False)
143+
assert result == content
144+
100145
def test_returns_polished_on_success(self) -> None:
101146
"""Test successful polish returns LLM output.
102147

0 commit comments

Comments
 (0)