fix: guarantee refined validation output

This commit is contained in:
Hermes Agent
2026-08-29 21:17:02 +10:00
parent a781e3d4de
commit 2c731f4982
5 changed files with 88 additions and 3 deletions
+1 -1
View File
@@ -3,7 +3,7 @@
"label": "Ollama Models",
"description": "Inspect, manage, and chat with local Ollama models, including shared persistent conversations, performance metrics, images, PDFs, URLs, and live memory telemetry.",
"icon": "Cpu",
"version": "1.7.8",
"version": "1.7.9",
"tab": {"path": "/ollama-manager", "position": "after:models"},
"entry": "dist/index.js",
"css": "dist/style.css",
+49
View File
@@ -82,6 +82,7 @@ CHAT_HEARTBEAT_INTERVAL = 5.0
HARNESS_MIN_VALIDATORS = 1
HARNESS_MAX_DRAFT_CHARS = 24_000
HARNESS_MAX_VALIDATION_CHARS = 8_000
HARNESS_MAX_FINAL_RETRY_CHARS = 24_000
PERFORMANCE_HISTORY_FILE = "performance-history.jsonl"
PERFORMANCE_HISTORY_BUCKET_SECONDS = 60
PERFORMANCE_HISTORY_MAX_SAMPLES = 24 * 60
@@ -1931,6 +1932,26 @@ def _harness_compiler_prompt(question: str, draft: str, reports: list[tuple[str,
)
def _looks_like_validation_report(content: str) -> bool:
normalized = re.sub(r"\s+", " ", str(content or "").strip().lower())
if not normalized:
return False
markers = ("validation report", "validator report", "narrative structure", "character consistency", "rating:")
return normalized.startswith("# validation") or normalized.startswith("# story validation") or sum(marker in normalized for marker in markers) >= 2
def _harness_retry_prompt(question: str, draft: str, reports: list[tuple[str, str]]) -> str:
return (
"IMPORTANT FINALIZATION RETRY. Return the actual finished answer to the original user request, not a "
"review, critique, score, validation report, plan, or commentary about other models. Rewrite and improve "
"the primary draft using valid corrections from the reports. Do not mention validation, validators, the "
"draft, this retry, or the harness. Return only the polished user-facing result.\n\n"
f"ORIGINAL USER REQUEST:\n{question[:MAX_ATTACHMENT_TEXT]}\n\n"
f"PRIMARY DRAFT TO IMPROVE:\n{draft[:HARNESS_MAX_DRAFT_CHARS]}\n\n"
f"CORRECTIONS TO APPLY:\n{_harness_compiler_prompt(question, draft, reports)[-HARNESS_MAX_FINAL_RETRY_CHARS:]}"
)
def _harness_models(body: ChatRequest) -> tuple[str, list[str], bool]:
legacy_models = [str(name).strip() for name in body.models if str(name).strip()]
primary_name = str(body.primary_model or body.model or (legacy_models[0] if legacy_models else "")).strip()
@@ -2131,6 +2152,34 @@ def _run_validation_harness(
final_content = str(final_message.get("content") or "").strip()
if not final_content:
raise HTTPException(502, "Primary model returned an empty compiled answer")
if _looks_like_validation_report(final_content):
retry_id = uuid.uuid4().hex
retry_started = time.time()
_persist_chat_stage(request_id, "compiler-retry", primary, "running", retry_started)
_persist_chat_event(request_id, conversation_id, "compiler-retry-started", level="warning", stage="Primary returned review text; requesting final answer", model=primary)
_chat_state(retry_id, state="preparing", stage="Preparing final-answer retry", model=primary, parent_id=request_id, conversation_id=conversation_id)
retry_payload = _chat_payload(
body,
primary,
message_override=_harness_retry_prompt(body.message, draft, [(item["model"], item["report"]) for item in reports]),
history_override=[],
attachment_parts=attachment_parts,
)
retry_result = _stream_chat_request(retry_payload, retry_id, cancel_event=cancel_event, parent_id=request_id)
with _chat_requests_lock:
retry_state = dict(_chat_requests.get(retry_id, {}))
metrics.append(_persist_metric(conversation_id, retry_id, primary, retry_state, status="final-retry"))
retry_message = retry_result.get("message") if isinstance(retry_result.get("message"), dict) else {}
retry_content = str(retry_message.get("content") or "").strip()
retry_finished = float(retry_state.get("finished_at") or time.time())
if retry_content and not _looks_like_validation_report(retry_content):
final_content = retry_content
_persist_chat_stage(request_id, "compiler-retry", primary, "completed", retry_started, finished_at=retry_finished, output_chars=len(final_content))
_persist_chat_event(request_id, conversation_id, "compiler-retry-completed", stage="Final answer retry completed", model=primary, payload={"output_chars": len(final_content)})
else:
final_content = draft
_persist_chat_stage(request_id, "compiler-retry", primary, "fallback", retry_started, finished_at=retry_finished, output_chars=len(final_content), error="Primary returned review text twice; preserved the primary draft")
_persist_chat_event(request_id, conversation_id, "compiler-retry-fallback", level="warning", stage="Preserved primary draft after invalid finalization", model=primary, payload={"output_chars": len(final_content)})
compiler_finished = time.time()
_persist_chat_stage(request_id, "compiler", primary, "completed", compiler_started, finished_at=compiler_finished, output_chars=len(final_content))
_persist_chat_event(request_id, conversation_id, "compiler-completed", stage="Primary final answer compiled", model=primary, payload={"output_chars": len(final_content)})