hangman: add retry + log validation failures for word generation
Silent failures (word too long/short, has hyphens, empty hint) are now logged as warnings showing the actual word/hint returned. Retry up to 2 times before giving up, matching riddle's behavior. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1390,6 +1390,7 @@ async def _generate_hangman_word() -> dict | None:
|
|||||||
'Format: {"word": "example", "hint": "short category or hint"}'
|
'Format: {"word": "example", "hint": "short category or hint"}'
|
||||||
)
|
)
|
||||||
user_msg = "Pick a common English word between 5 and 8 letters (lowercase letters only, no hyphens or spaces) and give a short hint."
|
user_msg = "Pick a common English word between 5 and 8 letters (lowercase letters only, no hyphens or spaces) and give a short hint."
|
||||||
|
for attempt in range(2):
|
||||||
try:
|
try:
|
||||||
timeout = aiohttp.ClientTimeout(total=60)
|
timeout = aiohttp.ClientTimeout(total=60)
|
||||||
async with aiohttp.ClientSession(timeout=timeout) as session:
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||||
@@ -1413,14 +1414,15 @@ async def _generate_hangman_word() -> dict | None:
|
|||||||
try:
|
try:
|
||||||
parsed = json.loads(candidate)
|
parsed = json.loads(candidate)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
logger.warning("hangman: JSON parse failed, raw: %.200s", text)
|
logger.warning("hangman: JSON parse failed (attempt %d), raw: %.200s", attempt + 1, text)
|
||||||
parsed = {}
|
parsed = {}
|
||||||
word = parsed.get("word", "").lower().strip()
|
word = parsed.get("word", "").lower().strip()
|
||||||
hint = parsed.get("hint", "").strip()
|
hint = parsed.get("hint", "").strip()
|
||||||
if word.isalpha() and 5 <= len(word) <= 8 and hint:
|
if word.isalpha() and 5 <= len(word) <= 8 and hint:
|
||||||
return {"word": word, "hint": hint}
|
return {"word": word, "hint": hint}
|
||||||
|
logger.warning("hangman: validation failed (attempt %d): word=%r hint=%r", attempt + 1, word, hint)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"hangman word generation error: {e}", exc_info=True)
|
logger.error(f"hangman word generation error (attempt {attempt + 1}): {e}", exc_info=True)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user