Extract chapter draft content-rejection retry into _draft_with_content_retry helper#157
Conversation
Agent-Logs-Url: https://github.com/CyberSecDef/NovelForge/sessions/9309a58e-489e-4042-8e6d-e924546c5f18 Co-authored-by: CyberSecDef <17597068+CyberSecDef@users.noreply.github.com>
_draft_with_content_retry helper
There was a problem hiding this comment.
Pull request overview
Refactors chapter drafting to use a shared helper for content-rejection retry behavior, aligning the draft step with the retry patterns used elsewhere in the pipeline.
Changes:
- Added
_draft_with_content_retry(and_DRAFT_CONTENT_NOTE) to centralize draft-time content rejection retry logic. - Replaced the inline retry loop in
chapters.pywith a single helper call that rebuilds prompts on retry. - Updated and added tests to reflect the new call path and to cover the helper’s retry behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
novelforge/agents/chapter/_helpers.py |
Introduces _DRAFT_CONTENT_NOTE and _draft_with_content_retry to handle draft retries on ContentRejectionError. |
novelforge/routes/generation/chapters.py |
Uses _draft_with_content_retry instead of an inline retry loop during drafting. |
novelforge/agents/chapter/__init__.py |
Re-exports _draft_with_content_retry and _DRAFT_CONTENT_NOTE. |
tests/test_progress_snapshot.py |
Updates patch targets to the new draft call path (_helpers.call_llm). |
tests/test_draft_with_content_retry.py |
Adds unit coverage for success, retry note injection, exhaustion behavior, and warning logging. |
Comments suppressed due to low confidence (1)
novelforge/routes/generation/chapters.py:45
- After switching draft calls to
_draft_with_content_retry, this module no longer appears to referencecall_llmorparse_llm_jsondirectly (nocall_llm(/parse_llm_json(usages found). Consider dropping those imports fromnovelforge.llm.clientto avoid unused-import warnings.
from novelforge.agents.chapter import (
_format_characters,
_draft_with_content_retry,
build_chapter_draft_prompt,
run_continuity_gatekeeper, run_chapter_rhythm_classifier,
run_character_state_updater, run_per_chapter_compression_check,
_run_all_chapter_agents, ChapterContext,
)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def _draft_with_content_retry( | ||
| build_prompt_fn: Callable[[str], list[dict]], | ||
| *, | ||
| action: str, | ||
| special_instructions: str, | ||
| chapter_num: int, | ||
| max_attempts: int = 3, | ||
| ) -> str: |
There was a problem hiding this comment.
max_attempts can be set to 0 (or a negative value) and the loop will perform zero attempts, falling through to the final raise ContentRejectionError(...) with a misleading message and without ever calling the LLM. Add a guard (e.g., raise ValueError) when max_attempts < 1 so the helper’s behavior is well-defined.
The draft step in
chapters.pywas the only pipeline step with an inline retry loop — 37 lines mixing prompt construction with retry/backoff logic — instead of using the_call_with_content_retry/_safe()pattern used everywhere else.Changes
_helpers.py: Adds_draft_with_content_retry(build_prompt_fn, *, action, special_instructions, chapter_num, max_attempts=3)and the_DRAFT_CONTENT_NOTEconstant it injects on retry. Unlike_call_with_content_retry(which sanitizes existing chapter text), this helper appends a content-guidance note tospecial_instructionsand rebuilds the prompt from scratch viabuild_prompt_fn.chapters.py: Replaces the inline 37-line retry loop with a single call:__init__.py: Re-exports_draft_with_content_retryand_DRAFT_CONTENT_NOTE.test_progress_snapshot.py: Four terminal-state force-write tests were patchingchapters.call_llm; draft calls now go through_helpers.call_llm, so patch targets updated accordingly.test_draft_with_content_retry.py: New unit tests covering first-attempt success, content-note injection with/withoutspecial_instructions, retry exhaustion, exact attempt count, and warning logging.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
api.openai.com/usr/bin/python python -m pytest tests/ -q --tb=short(dns block)/usr/bin/python python -m pytest tests/ -q --tb=short -j DROP(dns block)/usr/bin/python python -m pytest tests/ -q(dns block)If you need me to access, download, or install something from one of these locations, you can either: