From 33ba18cac16e1755bfad8ec125054088ec94d48e Mon Sep 17 00:00:00 2001 From: OpenClaw Agent Date: Fri, 10 Jul 2026 10:41:16 +0000 Subject: [PATCH] Sanitize leaked reasoning in character analysis --- ltx_director.py | 75 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 8 deletions(-) diff --git a/ltx_director.py b/ltx_director.py index 61d20f0..83c192b 100644 --- a/ltx_director.py +++ b/ltx_director.py @@ -1,9 +1,10 @@ -import logging -import asyncio -import json -import base64 -import io as _io -import math +import logging +import asyncio +import json +import base64 +import io as _io +import math +import re import numpy as np import torch @@ -482,6 +483,65 @@ def _analysis_text_is_usable(text: str) -> bool: return True +def _sanitize_analysis_text(text: str) -> str: + """Strip prompt-echo / reasoning junk and keep the shortest usable visual description.""" + text = (text or "").strip() + if not text: + return "" + + if "" in text: + text = text.split("")[-1].strip() + + # Drop markdown emphasis and common prompt-echo scaffolding. + lines = [] + for raw_line in text.splitlines(): + line = raw_line.strip() + if not line: + continue + lower = line.lower() + if lower.startswith("the user wants"): + continue + if lower.startswith("sentence 1 requirements"): + continue + if lower.startswith("sentence 2 requirements"): + continue + if lower.startswith("requirements:"): + continue + if lower.startswith("let's interpret"): + continue + if lower.startswith("wait, the prompt says"): + continue + if lower.startswith("do not "): + continue + if line.startswith("- ") or line.startswith("* "): + continue + cleaned = line.replace("**", "").strip() + if cleaned: + lines.append(cleaned) + + text = " ".join(lines).strip() + text = re.sub(r"\s+", " ", text) + if not text: + return "" + + # If the model echoed instructions and then described the subject, keep the last descriptive sentences. + sentences = re.split(r"(?<=[.!?])\s+", text) + descriptive = [] + for sentence in sentences: + sentence = sentence.strip() + if not sentence: + continue + lower = sentence.lower() + if "prompt says" in lower or "requirements" in lower or "the user wants" in lower: + continue + descriptive.append(sentence) + + if descriptive: + text = " ".join(descriptive[-2:]).strip() + + return text + + def _compress_analysis_image_b64(b64_payload: str, max_dim: int = 768, quality: int = 82) -> str: """Shrink analysis images so multimodal providers do not burn their full context on pixels.""" try: @@ -653,8 +713,7 @@ async def analyze_character_endpoint(request): "message": f"Could not connect to {provider} at {base_url}. Make sure the server is running and reachable.", }) - if "" in generated_text: - generated_text = generated_text.split("")[-1].strip() + generated_text = _sanitize_analysis_text(generated_text) if not _analysis_text_is_usable(generated_text): return web.json_response({ "status": "error",