|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Tests for prompt assembly and reply-shape checking. |
| 3 | +
|
| 4 | + python pipeline/test_prompts.py |
| 5 | +
|
| 6 | +The .claude/agents/*.md files are agent definitions written for a harness with |
| 7 | +tools. The pipeline reuses their prose in plain API calls where no tools exist, |
| 8 | +and when their tool instructions leak through, the model returns a transcript of |
| 9 | +itself "researching" instead of a page. These cover both halves of the guard: |
| 10 | +stripping the `tools:` frontmatter, and recognizing a reply that isn't a file. |
| 11 | +""" |
| 12 | + |
| 13 | +import sys |
| 14 | +import tempfile |
| 15 | +import unittest |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 19 | + |
| 20 | +from util import ( # noqa: E402 |
| 21 | + NO_TOOLS, |
| 22 | + build_authoring_system_prompt, |
| 23 | + build_review_system_prompt, |
| 24 | + format_retry_prompt, |
| 25 | + load_agent, |
| 26 | + looks_like_mdx, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +class LoadAgentTest(unittest.TestCase): |
| 31 | + def setUp(self): |
| 32 | + self._tmp = tempfile.TemporaryDirectory() |
| 33 | + self.addCleanup(self._tmp.cleanup) |
| 34 | + self.dir = Path(self._tmp.name) |
| 35 | + |
| 36 | + def write(self, text): |
| 37 | + path = self.dir / "agent.md" |
| 38 | + path.write_text(text, encoding="utf-8") |
| 39 | + return path |
| 40 | + |
| 41 | + def test_drops_the_frontmatter_including_the_tools_line(self): |
| 42 | + path = self.write( |
| 43 | + "---\nname: docs-writer\ntools: Read, Write, Grep, Glob, Agent\n---\n\n" |
| 44 | + "# Your Role\n\nYou write docs.\n" |
| 45 | + ) |
| 46 | + body = load_agent(path) |
| 47 | + self.assertTrue(body.startswith("# Your Role")) |
| 48 | + self.assertNotIn("tools:", body) |
| 49 | + self.assertIn("You write docs.", body) |
| 50 | + |
| 51 | + def test_a_file_without_frontmatter_is_unchanged(self): |
| 52 | + path = self.write("# Just Prose\n\nNo frontmatter here.\n") |
| 53 | + self.assertEqual(load_agent(path), "# Just Prose\n\nNo frontmatter here.\n") |
| 54 | + |
| 55 | + def test_body_horizontal_rules_survive(self): |
| 56 | + """Only the leading block is frontmatter; --- later is content.""" |
| 57 | + path = self.write("---\nname: a\n---\n\n# T\n\nOne\n\n---\n\nTwo\n") |
| 58 | + body = load_agent(path) |
| 59 | + self.assertIn("\n---\n", body) |
| 60 | + self.assertIn("Two", body) |
| 61 | + |
| 62 | + def test_missing_file_is_empty(self): |
| 63 | + self.assertEqual(load_agent(self.dir / "nope.md"), "") |
| 64 | + |
| 65 | + |
| 66 | +class SystemPromptTest(unittest.TestCase): |
| 67 | + """Built from the repo's real agent files.""" |
| 68 | + |
| 69 | + def test_authoring_prompt_carries_the_override_and_no_tools_frontmatter(self): |
| 70 | + prompt = build_authoring_system_prompt("You are a documentation writer.") |
| 71 | + self.assertIn(NO_TOOLS, prompt) |
| 72 | + self.assertNotIn("tools: Read, Write", prompt) |
| 73 | + self.assertIn("Output ONLY the .mdx file content", prompt) |
| 74 | + |
| 75 | + def test_the_override_comes_after_the_guidelines_it_overrides(self): |
| 76 | + prompt = build_authoring_system_prompt("role") |
| 77 | + self.assertGreater(prompt.index(NO_TOOLS), prompt.index("Docs Writer Guidelines")) |
| 78 | + |
| 79 | + def test_review_prompt_too(self): |
| 80 | + prompt = build_review_system_prompt() |
| 81 | + self.assertIn(NO_TOOLS, prompt) |
| 82 | + self.assertNotIn("tools: Read, Grep", prompt) |
| 83 | + |
| 84 | + |
| 85 | +class LooksLikeMdxTest(unittest.TestCase): |
| 86 | + def test_a_page_passes(self): |
| 87 | + self.assertTrue(looks_like_mdx('---\ntitle: "T"\n---\n\nBody.\n')) |
| 88 | + |
| 89 | + def test_leading_blank_lines_are_tolerated(self): |
| 90 | + self.assertTrue(looks_like_mdx('\n\n---\ntitle: T\n---\n\nBody.\n')) |
| 91 | + |
| 92 | + def test_the_faked_tool_transcript_fails(self): |
| 93 | + """The actual failure this guard exists for.""" |
| 94 | + reply = ( |
| 95 | + "I'll research the GitHub repo and peer pages before writing.\n\n" |
| 96 | + '<tool_call>\n{"name": "Glob", "arguments": {"pattern": "docs/**/*.mdx"}}\n' |
| 97 | + "</tool_call>\n<tool_response>\n---\ntitle: A peer page\n---\n</tool_response>\n" |
| 98 | + ) |
| 99 | + self.assertFalse(looks_like_mdx(reply)) |
| 100 | + |
| 101 | + def test_commentary_then_a_page_fails(self): |
| 102 | + self.assertFalse(looks_like_mdx("Here is the page:\n\n---\ntitle: T\n---\n\nBody.\n")) |
| 103 | + |
| 104 | + def test_unclosed_frontmatter_fails(self): |
| 105 | + self.assertFalse(looks_like_mdx("---\ntitle: T\n\nBody with no closing fence.\n")) |
| 106 | + |
| 107 | + def test_empty_and_none(self): |
| 108 | + self.assertFalse(looks_like_mdx("")) |
| 109 | + self.assertFalse(looks_like_mdx(None)) |
| 110 | + |
| 111 | + |
| 112 | +class RetryPromptTest(unittest.TestCase): |
| 113 | + def test_names_the_problem_and_forbids_a_preamble(self): |
| 114 | + prompt = format_retry_prompt("it did not start with YAML frontmatter (---)") |
| 115 | + self.assertIn("did not start with YAML frontmatter", prompt) |
| 116 | + self.assertIn("No preamble", prompt) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + unittest.main(verbosity=2) |
0 commit comments