Compare commits

..

1 Commits

Author SHA1 Message Date
6af162ed5e fix(cron): inject runtime env hint into cron system prompt (#378)
Some checks failed
Forge CI / smoke-and-build (pull_request) Failing after 17s
The agent doesn't know its execution environment. When running on
cloud, it blindly attempts localhost calls from the prompt.

Add _build_cron_env_hint() that generates a [CRON ENV] block:
- LOCAL: 'localhost access, SSH, local services available'
- CLOUD: 'NO localhost access, NO SSH, NO local services.
  If instructions reference localhost/Ollama/SSH, report the
  limitation instead of attempting.'

Injected into the prompt after resolve_turn_route() so the agent
knows its environment before executing any instructions.

Closes #378
2026-04-14 01:57:32 +00:00

View File

@@ -13,7 +13,6 @@ import concurrent.futures
import json
import logging
import os
import re
import subprocess
import sys
@@ -644,56 +643,37 @@ def _build_job_prompt(job: dict) -> str:
return "\n".join(parts)
# 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 _build_cron_env_hint(base_url: str, provider: str, disabled_toolsets: list) -> str:
"""Build a runtime environment hint block for the cron system prompt.
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.
Tells the agent about its execution environment so it can self-adjust
when instructions reference capabilities it doesn't have. (#378)
"""
try:
from agent.model_metadata import is_local_endpoint
except ImportError:
return prompt
is_local_endpoint = lambda _: False
if is_local_endpoint(base_url or ""):
return prompt # Local — no rewrite needed
is_local = is_local_endpoint(base_url or "")
rewritten = prompt
for pattern, replacement in _CLOUD_INCOMPATIBLE_PATTERNS:
rewritten = pattern.sub(replacement, rewritten)
parts = ["[CRON ENV:"]
if rewritten != prompt:
rewritten = (
"[NOTE: Some instructions were adjusted for cloud execution. "
"Local service checks were rewritten to use available tools.]
if is_local:
parts.append(f"Runtime: LOCAL ({provider or 'local'} at {base_url or 'localhost'})")
parts.append("Capabilities: localhost access, SSH, local services available.")
else:
parts.append(f"Runtime: CLOUD ({provider or 'cloud'} at {base_url or 'remote'})")
parts.append("Capabilities: NO localhost access, NO SSH, NO local services.")
parts.append("If instructions reference localhost/Ollama/SSH, report the limitation instead of attempting.")
"
+ rewritten
)
if disabled_toolsets:
parts.append(f"Disabled tools: {', '.join(disabled_toolsets)}.")
return rewritten
parts.append("]")
return " ".join(parts)
def run_job(job: dict) -> tuple[bool, str, str, Optional[str]]:(job: dict) -> tuple[bool, str, str, Optional[str]]:
def run_job(job: dict) -> tuple[bool, str, str, Optional[str]]:
"""
Execute a single cron job.