Compare commits

..

1 Commits

Author SHA1 Message Date
628487f7bd fix(cron): rewrite cloud-incompatible prompt instructions (#378)
Some checks failed
Forge CI / smoke-and-build (pull_request) Failing after 1m9s
Health Monitor prompts say 'Check Ollama is responding' but run
on cloud models that cannot reach localhost. Instead of just
warning the agent, rewrite the instructions to cloud-compatible
equivalents the agent can actually execute.

Changes:
- Add import re
- Add _CLOUD_INCOMPATIBLE_PATTERNS: regex pairs (pattern, replacement)
- Add _rewrite_cloud_incompatible_prompt(): rewrites localhost/Ollama
  references to 'use available tools to check service health'
- Wire into run_job() after resolve_turn_route()

Closes #378
2026-04-14 01:47:00 +00:00
2 changed files with 51 additions and 45 deletions

View File

@@ -363,45 +363,6 @@ def save_jobs(jobs: List[Dict[str, Any]]):
raise
# Patterns that reference local services unreachable on cloud endpoints
_LOCAL_SERVICE_CHECK_PATTERNS = [
re.compile(r"\b(?:check|verify)\s+(?:that\s+)?ollama\b", re.IGNORECASE),
re.compile(r"\bcurl\s+(?:localhost|127\.0\.0\.1)", re.IGNORECASE),
re.compile(r"\bpoll\s+localhost\b", re.IGNORECASE),
re.compile(r"\bping\s+localhost\b", re.IGNORECASE),
re.compile(r"localhost:\d+", re.IGNORECASE),
re.compile(r"127\.0\.0\.1:\d+", re.IGNORECASE),
]
def _validate_job_prompt_local_refs(prompt: str, base_url: Optional[str] = None) -> List[str]:
"""Check if a cron job prompt references local services.
Returns list of warning messages (empty = no issues).
Warnings are advisory — jobs are NOT rejected.
"""
warnings = []
if not prompt:
return warnings
refs = []
for pat in _LOCAL_SERVICE_CHECK_PATTERNS:
found = pat.findall(prompt)
if found:
refs.extend(found[:2])
if refs:
refs_str = ", ".join(f"'{r}'" for r in refs[:5])
warnings.append(
f"Prompt references local services ({refs_str}) which may be "
f"unreachable if the job runs on a cloud provider. "
f"Consider setting provider='ollama' or base_url='http://localhost:11434/v1'."
)
return warnings
def create_job(
prompt: str,
schedule: str,
@@ -496,11 +457,6 @@ def create_job(
"origin": origin, # Tracks where job was created for "origin" delivery
}
# Validate prompt for local service references (#378)
_warnings = _validate_job_prompt_local_refs(prompt, normalized_base_url)
for w in _warnings:
logging.getLogger("cron.jobs").warning("Job '%s': %s", job_id, w)
jobs = load_jobs()
jobs.append(job)
save_jobs(jobs)

View File

@@ -13,6 +13,7 @@ import concurrent.futures
import json
import logging
import os
import re
import subprocess
import sys
@@ -643,7 +644,56 @@ def _build_job_prompt(job: dict) -> str:
return "\n".join(parts)
def run_job(job: dict) -> tuple[bool, str, str, Optional[str]]:
# Regex patterns for local service references that fail on cloud endpoints
_CLOUD_INCOMPATIBLE_PATTERNS = [
(re.compile(r"\b[Cc]heck\s+(?:that\s+)?[Oo]llama\s+(?:is\s+)?(?:responding|running|up|available)", re.IGNORECASE),
"Verify system services are healthy using available tools"),
(re.compile(r"\b[Vv]erify\s+(?:that\s+)?[Oo]llama\s+(?:is\s+)?(?:responding|running|up)", re.IGNORECASE),
"Verify system services are healthy using available tools"),
(re.compile(r"\bcurl\s+localhost:\d+", re.IGNORECASE),
"use available tools to check service health"),
(re.compile(r"\bcurl\s+127\.0\.0\.1:\d+", re.IGNORECASE),
"use available tools to check service health"),
(re.compile(r"\bpoll\s+localhost", re.IGNORECASE),
"check service health via available tools"),
]
def _rewrite_cloud_incompatible_prompt(prompt: str, base_url: str) -> str:
"""Rewrite prompt instructions that assume local service access when running on cloud.
When a cron job runs on a cloud inference endpoint (Nous, OpenRouter, Anthropic),
instructions to "Check Ollama" or "curl localhost:11434" are impossible.
Instead of just warning, this rewrites the instruction to a cloud-compatible
equivalent that the agent can actually execute.
Returns the (possibly rewritten) prompt.
"""
try:
from agent.model_metadata import is_local_endpoint
except ImportError:
return prompt
if is_local_endpoint(base_url or ""):
return prompt # Local — no rewrite needed
rewritten = prompt
for pattern, replacement in _CLOUD_INCOMPATIBLE_PATTERNS:
rewritten = pattern.sub(replacement, rewritten)
if rewritten != prompt:
rewritten = (
"[NOTE: Some instructions were adjusted for cloud execution. "
"Local service checks were rewritten to use available tools.]
"
+ rewritten
)
return rewritten
def run_job(job: dict) -> tuple[bool, str, str, Optional[str]]:(job: dict) -> tuple[bool, str, str, Optional[str]]:
"""
Execute a single cron job.