Skip to content

The agent that grows with you

refactor(gateway): unify computer-use repair call sites after review

kshitijk4poor105999a0c948
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 909954130cb..b721b572174 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -6272,23 +6272,17 @@ def run_job(
final_response = result.get("final_response", "") or ""
# Recover model-mangled computer_use screenshot paths before delivery
# media extraction (same repair as the gateway turn/background paths).
- # Cron runs start a fresh conversation, so history_offset=0.
- if final_response and "MEDIA:" in final_response:
- try:
- from gateway.media_repair import (
- repair_explicit_computer_use_media_paths,
- )
+ # Cron runs start a fresh conversation, so history_offset=0. The
+ # helper is fail-open and no-ops without a MEDIA: directive.
+ if final_response:
+ from gateway.media_repair import (
+ repair_explicit_computer_use_media_paths,
+ )
- final_response = repair_explicit_computer_use_media_paths(
- final_response,
- result.get("messages", []),
- )
- except Exception:
- logger.debug(
- "Job '%s': computer_use media path repair failed",
- job_name,
- exc_info=True,
- )
+ final_response = repair_explicit_computer_use_media_paths(
+ final_response,
+ result.get("messages", []),
+ )
# Strip leaked placeholder text that upstream may inject on empty completions.
if final_response.strip() == "(No response generated)":
final_response = ""
diff --git a/gateway/media_repair.py b/gateway/media_repair.py
index a58389b946e..26c107b4e88 100644
--- a/gateway/media_repair.py
+++ b/gateway/media_repair.py
@@ -21,9 +21,12 @@ tasks, and cron job delivery — shares one implementation.
from __future__ import annotations
import json
+import logging
import re
from typing import Any, Dict, Iterator, List
+logger = logging.getLogger(__name__)
+
# Absolute-path prefix accepted for canonical capture paths: Windows drive
# letter, POSIX root, or UNC share. Kept as a pattern string so the summary
# regex below and the compiled prefix check stay in sync.
@@ -125,10 +128,27 @@ def repair_explicit_computer_use_media_paths(
"""Recover model-mangled paths for explicitly requested screenshots.
Repair only an already-explicit ``MEDIA:`` directive whose unique
- generated basename matches a canonical screenshot path from this turn.
- This does not auto-attach ordinary computer-use captures, and normal
- media path validation still runs after the repair.
+ generated basename case-insensitively matches a canonical screenshot
+ path from this turn. This does not auto-attach ordinary computer-use
+ captures, and normal media path validation still runs after the repair.
+
+ Fail-open: the repair is cosmetic, so an unexpected error returns the
+ response unchanged rather than aborting delivery.
"""
+ try:
+ return _repair_explicit_computer_use_media_paths_inner(
+ response, messages, history_offset
+ )
+ except Exception:
+ logger.debug("computer_use media path repair failed", exc_info=True)
+ return response
+
+
+def _repair_explicit_computer_use_media_paths_inner(
+ response: str,
+ messages: List[Dict[str, Any]],
+ history_offset: int = 0,
+) -> str:
if "MEDIA:" not in response:
return response
@@ -176,6 +196,10 @@ def repair_explicit_computer_use_media_paths(
if not canonical_by_basename:
return response
+ # Lazy on purpose: keeps `import gateway.media_repair` cheap for
+ # standalone cron processes that may never hit a MEDIA: response.
+ # No import cycle either way (base.py imports neither this module
+ # nor gateway.run at module level).
from gateway.platforms.base import BasePlatformAdapter
media_files, _ = BasePlatformAdapter.extract_media(response)
diff --git a/tests/gateway/test_media_extraction.py b/tests/gateway/test_media_extraction.py
index 6a50965718e..5150c4a20dc 100644
--- a/tests/gateway/test_media_extraction.py
+++ b/tests/gateway/test_media_extraction.py
@@ -224,7 +224,9 @@ class TestMediaExtraction:
"""
import json as _json
- from gateway.run import _repair_explicit_computer_use_media_paths
+ from gateway.media_repair import (
+ repair_explicit_computer_use_media_paths as _repair_explicit_computer_use_media_paths,
+ )
capture_name = "computer_use_dddddddddddddddddddddddddddddddd.png"
canonical = rf"C:\Users\Alice\AppData\Local\hermes\cache\images\{capture_name}"
@@ -251,7 +253,9 @@ class TestMediaExtraction:
closed (no rewrite) when no user message remains."""
import json as _json
- from gateway.run import _repair_explicit_computer_use_media_paths
+ from gateway.media_repair import (
+ repair_explicit_computer_use_media_paths as _repair_explicit_computer_use_media_paths,
+ )
capture_name = "computer_use_eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.png"
canonical = rf"C:\cache\images\{capture_name}"