From 0ee6915fc582ebd4364bf92eea62b824b937d40a Mon Sep 17 00:00:00 2001 From: Typosaurus Date: Sun, 19 Jul 2026 20:56:27 +0000 Subject: [PATCH] ticket #84 attempt 1 --- devplacepy/services/ai_modifier.py | 8 ++++++- devplacepy/services/correction.py | 11 +++++++-- tests/unit/services/ai_modifier.py | 38 ++++++++++++++++++++++++++++++ tests/unit/services/correction.py | 36 ++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 tests/unit/services/ai_modifier.py create mode 100644 tests/unit/services/correction.py diff --git a/devplacepy/services/ai_modifier.py b/devplacepy/services/ai_modifier.py index 1edf639d..261860ea 100644 --- a/devplacepy/services/ai_modifier.py +++ b/devplacepy/services/ai_modifier.py @@ -31,7 +31,13 @@ def modify_text( "The user's message contains an inline instruction marked with @ai. " + (prompt or DEFAULT_MODIFIER_PROMPT).strip() + " Return ONLY the resulting text, with no preamble, no explanation, no " - "quotes and no code fences." + "quotes and no code fences.\n" + "Rules:\n" + "1. Do NOT modify content inside Markdown fenced code blocks (delimited by " + "triple backticks). Preserve the code block fences, the language identifier, " + "and the code content exactly as written.\n" + "2. Only modify prose outside code blocks. Apply the modification prompt only " + "to non-code text." ) if context: system += ( diff --git a/devplacepy/services/correction.py b/devplacepy/services/correction.py index 5c9b7358..a32202a7 100644 --- a/devplacepy/services/correction.py +++ b/devplacepy/services/correction.py @@ -124,8 +124,15 @@ def correct_text(api_key: str, prompt: str, text: str) -> tuple[str, dict | None system = ( "You are a text correction engine. Apply the correction instruction below to " "the user's message and return ONLY the resulting text, with no preamble, no " - "explanation, no quotes and no code fences. Preserve the original meaning, " - "language, line breaks and markdown. Correction instruction: " + "explanation, no quotes and no code fences.\n" + "Rules:\n" + "1. Do NOT modify content inside Markdown fenced code blocks (delimited by " + "triple backticks). Preserve the code block fences, the language identifier, " + "and the code content exactly as written.\n" + "2. Only correct prose outside code blocks. Apply the correction prompt only " + "to non-code text.\n" + "3. Preserve the original meaning, language, line breaks, and markdown.\n" + "Correction instruction: " + (prompt or DEFAULT_CORRECTION_PROMPT).strip() ) return gateway_complete( diff --git a/tests/unit/services/ai_modifier.py b/tests/unit/services/ai_modifier.py new file mode 100644 index 00000000..b1f69578 --- /dev/null +++ b/tests/unit/services/ai_modifier.py @@ -0,0 +1,38 @@ +# retoor + +import unittest.mock + +from devplacepy.services.ai_modifier import modify_text + + +def test_modify_text_prompt_preserves_fenced_code_blocks(): + captured = {} + + def fake_gateway(api_key, system, text, timeout, max_growth_factor): + captured["system"] = system + captured["text"] = text + return ("fake result", {"calls": 1}) + + with unittest.mock.patch( + "devplacepy.services.correction.gateway_complete", fake_gateway + ): + result, _ = modify_text( + "test-key", + "Make it more formal", + "Some text\n```python\nx = 1\n```\nMore text", + ) + + assert result == "fake result" + system = captured["system"] + assert ( + "Do NOT modify content inside Markdown fenced code blocks" in system + ), "System prompt must instruct the model to preserve code blocks" + assert ( + "Preserve the code block fences, the language identifier" in system + ), "System prompt must mention preserving language identifiers" + assert ( + "Only modify prose outside code blocks" in system + ), "System prompt must limit modifications to non-code text" + assert ( + "triple backticks" in system + ), "System prompt must mention triple backticks as the delimiter" diff --git a/tests/unit/services/correction.py b/tests/unit/services/correction.py new file mode 100644 index 00000000..c7fa0384 --- /dev/null +++ b/tests/unit/services/correction.py @@ -0,0 +1,36 @@ +# retoor + +import unittest.mock + +from devplacepy.services.correction import correct_text + + +def test_correct_text_prompt_preserves_fenced_code_blocks(): + captured = {} + + def fake_gateway(api_key, system, text, timeout, max_growth_factor): + captured["system"] = system + captured["text"] = text + return ("fake result", {"calls": 1}) + + with unittest.mock.patch( + "devplacepy.services.correction.gateway_complete", fake_gateway + ): + result, _ = correct_text( + "test-key", "Fix spelling", "Some text\n```python\nx = 1\n```\nMore text" + ) + + assert result == "fake result" + system = captured["system"] + assert ( + "Do NOT modify content inside Markdown fenced code blocks" in system + ), "System prompt must instruct the model to preserve code blocks" + assert ( + "Preserve the code block fences, the language identifier" in system + ), "System prompt must mention preserving language identifiers" + assert ( + "Only correct prose outside code blocks" in system + ), "System prompt must limit corrections to non-code text" + assert ( + "triple backticks" in system + ), "System prompt must mention triple backticks as the delimiter"