Compare commits
2 Commits
fix/468-17
...
claude/iss
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ade5351bdf | ||
|
|
49c8635261 |
@@ -41,6 +41,42 @@ from agent.model_metadata import is_local_endpoint
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Minimum context window (tokens) required for a model to run cron jobs.
|
||||
# Models below this threshold are rejected at job startup.
|
||||
CRON_MIN_CONTEXT_TOKENS = 64_000
|
||||
|
||||
|
||||
class ModelContextError(ValueError):
|
||||
"""Raised when a model's context window is too small for cron use."""
|
||||
|
||||
|
||||
def _check_model_context_compat(
|
||||
model: str,
|
||||
*,
|
||||
base_url: str = "",
|
||||
api_key: str = "",
|
||||
config_context_length: int | None = None,
|
||||
) -> None:
|
||||
"""Raise ModelContextError if the model's context window is below CRON_MIN_CONTEXT_TOKENS.
|
||||
|
||||
If config_context_length is provided the check is skipped (user override).
|
||||
Detection failures are non-fatal (fail-open) — the job proceeds.
|
||||
"""
|
||||
if config_context_length is not None:
|
||||
return
|
||||
try:
|
||||
from agent.model_metadata import get_model_context_length
|
||||
ctx = get_model_context_length(model, base_url=base_url, api_key=api_key)
|
||||
except Exception as exc:
|
||||
logger.debug("Context length detection failed for '%s', skipping check: %s", model, exc)
|
||||
return
|
||||
if ctx < CRON_MIN_CONTEXT_TOKENS:
|
||||
raise ModelContextError(
|
||||
f"Model '{model}' has a context window of {ctx:,} tokens, "
|
||||
f"which is below the minimum {CRON_MIN_CONTEXT_TOKENS:,} required by Hermes Agent. "
|
||||
f"To override, set model.context_length in config.yaml."
|
||||
)
|
||||
|
||||
|
||||
# =====================================================================
|
||||
# Deploy Sync Guard
|
||||
@@ -90,7 +126,14 @@ def _validate_agent_interface() -> None:
|
||||
) from exc
|
||||
|
||||
sig = inspect.signature(AIAgent.__init__)
|
||||
accepted = set(sig.parameters.keys()) - {"self"}
|
||||
params = sig.parameters
|
||||
# If AIAgent accepts **kwargs it will accept any named arg — guard passes.
|
||||
if any(p.kind == inspect.Parameter.VAR_KEYWORD for p in params.values()):
|
||||
_agent_interface_validated = True
|
||||
logger.debug("Deploy sync guard passed — AIAgent accepts **kwargs")
|
||||
return
|
||||
|
||||
accepted = set(params.keys()) - {"self"}
|
||||
missing = _SCHEDULER_AGENT_KWARGS - accepted
|
||||
|
||||
if missing:
|
||||
@@ -129,7 +172,12 @@ def _safe_agent_kwargs(kwargs: dict) -> dict:
|
||||
return kwargs
|
||||
|
||||
sig = inspect.signature(AIAgent.__init__)
|
||||
accepted = set(sig.parameters.keys()) - {"self"}
|
||||
params = sig.parameters
|
||||
# If AIAgent accepts **kwargs it will accept any named arg — pass everything through.
|
||||
if any(p.kind == inspect.Parameter.VAR_KEYWORD for p in params.values()):
|
||||
return kwargs
|
||||
|
||||
accepted = set(params.keys()) - {"self"}
|
||||
|
||||
safe = {}
|
||||
dropped = []
|
||||
@@ -157,82 +205,6 @@ _KNOWN_DELIVERY_PLATFORMS = frozenset({
|
||||
|
||||
from cron.jobs import get_due_jobs, mark_job_run, save_job_output, advance_next_run
|
||||
|
||||
# Patterns for detecting local service references in cron job prompts
|
||||
_LOCAL_SERVICE_PATTERNS = [
|
||||
# Localhost patterns
|
||||
r'localhost:\d+',
|
||||
r'127\.0\.0\.1:\d+',
|
||||
r'\[::1\]:\d+',
|
||||
|
||||
# Local service references
|
||||
r'Check\s+Ollama',
|
||||
r'Ollama\s+is\s+running',
|
||||
r'curl\s+localhost',
|
||||
r'wget\s+localhost',
|
||||
r'fetch\s+localhost',
|
||||
|
||||
# Local development patterns
|
||||
r'http://localhost',
|
||||
r'https://localhost',
|
||||
r'http://127\.0\.0\.1',
|
||||
r'https://127\.0\.0\.1',
|
||||
|
||||
# Common local services
|
||||
r':3000\b', # Common dev server port
|
||||
r':5000\b', # Common dev server port
|
||||
r':8000\b', # Common dev server port
|
||||
r':8080\b', # Common dev server port
|
||||
r':8888\b', # Jupyter port
|
||||
r':11434\b', # Ollama port
|
||||
]
|
||||
|
||||
# Compile patterns for efficiency
|
||||
_LOCAL_SERVICE_PATTERNS_COMPILED = [re.compile(pattern, re.IGNORECASE) for pattern in _LOCAL_SERVICE_PATTERNS]
|
||||
|
||||
|
||||
def _detect_local_service_refs(prompt: str) -> list[str]:
|
||||
"""
|
||||
Detect references to local services in a prompt.
|
||||
|
||||
Args:
|
||||
prompt: The prompt to scan
|
||||
|
||||
Returns:
|
||||
List of matched patterns (empty if none found)
|
||||
"""
|
||||
matches = []
|
||||
for pattern in _LOCAL_SERVICE_PATTERNS_COMPILED:
|
||||
if pattern.search(prompt):
|
||||
matches.append(pattern.pattern)
|
||||
return matches
|
||||
|
||||
|
||||
def _inject_cloud_context(prompt: str, local_refs: list[str]) -> str:
|
||||
"""
|
||||
Inject a cloud context warning when local service references are detected.
|
||||
|
||||
Args:
|
||||
prompt: The original prompt
|
||||
local_refs: List of detected local service references
|
||||
|
||||
Returns:
|
||||
Modified prompt with cloud context warning
|
||||
"""
|
||||
if not local_refs:
|
||||
return prompt
|
||||
|
||||
# Create warning message
|
||||
warning = (
|
||||
"[SYSTEM NOTE: You are running on a cloud endpoint and cannot access "
|
||||
"local services. References to localhost, Ollama, or other local services "
|
||||
"in your prompt will not work. Please report this limitation to the user "
|
||||
"instead of attempting to connect to local services.]\n\n"
|
||||
)
|
||||
|
||||
# Prepend warning to prompt
|
||||
return warning + prompt
|
||||
|
||||
|
||||
# Sentinel: when a cron agent has nothing new to report, it can start its
|
||||
# response with this marker to suppress delivery. Output is still saved
|
||||
# locally for audit.
|
||||
@@ -621,7 +593,49 @@ def _run_job_script(script_path: str) -> tuple[bool, str]:
|
||||
return False, f"Script execution failed: {exc}"
|
||||
|
||||
|
||||
def _build_job_prompt(job: dict) -> str:
|
||||
_PROVIDER_ALIASES = {
|
||||
"ollama": {"ollama", "localhost:11434"},
|
||||
"anthropic": {"anthropic", "claude"},
|
||||
"nous": {"nous", "mimo"},
|
||||
"openrouter": {"openrouter"},
|
||||
"openai": {"openai", "gpt"},
|
||||
"gemini": {"gemini", "google"},
|
||||
}
|
||||
_CLOUD_PREFIXES = frozenset({"nous", "openrouter", "anthropic", "openai", "zai", "kimi", "gemini", "minimax"})
|
||||
|
||||
|
||||
def _classify_runtime(provider: str, model: str) -> str:
|
||||
"""Return 'cloud', 'local', or 'unknown' based on provider/model hints."""
|
||||
p = (provider or "").strip().lower()
|
||||
m = (model or "").strip().lower()
|
||||
if p and p not in ("ollama", "local"):
|
||||
return "cloud"
|
||||
if "/" in m and m.split("/")[0] in _CLOUD_PREFIXES:
|
||||
return "cloud"
|
||||
if p in ("ollama", "local") or (not p and m):
|
||||
return "local"
|
||||
return "unknown"
|
||||
|
||||
|
||||
def _detect_provider_mismatch(prompt: str, active_provider: str):
|
||||
"""Return the mismatched provider alias if the prompt references a different provider."""
|
||||
if not active_provider or not prompt:
|
||||
return None
|
||||
pl = prompt.lower()
|
||||
al = active_provider.lower().strip()
|
||||
active_group = next(
|
||||
(g for g, aliases in _PROVIDER_ALIASES.items() if al in aliases or al.startswith(g)),
|
||||
None,
|
||||
)
|
||||
if not active_group:
|
||||
return None
|
||||
return next(
|
||||
(g for g, aliases in _PROVIDER_ALIASES.items() if g != active_group and any(x in pl for x in aliases)),
|
||||
None,
|
||||
)
|
||||
|
||||
|
||||
def _build_job_prompt(job: dict, *, runtime_model: str = "", runtime_provider: str = "") -> str:
|
||||
"""Build the effective prompt for a cron job, optionally loading one or more skills first."""
|
||||
prompt = job.get("prompt", "")
|
||||
skills = job.get("skills")
|
||||
@@ -652,6 +666,26 @@ def _build_job_prompt(job: dict) -> str:
|
||||
f"{prompt}"
|
||||
)
|
||||
|
||||
# Build runtime context block — inject model/provider/runtime classification
|
||||
# so the agent knows what infrastructure it has access to.
|
||||
# Fix #565: derive provider from model prefix when runtime_provider is empty.
|
||||
_runtime_block = ""
|
||||
if runtime_model or runtime_provider:
|
||||
if not runtime_provider and "/" in runtime_model:
|
||||
runtime_provider = runtime_model.split("/")[0]
|
||||
_kind = _classify_runtime(runtime_provider, runtime_model)
|
||||
_parts = []
|
||||
if runtime_model:
|
||||
_parts.append(f"MODEL: {runtime_model}")
|
||||
if runtime_provider:
|
||||
_parts.append(f"PROVIDER: {runtime_provider}")
|
||||
if _kind == "local":
|
||||
_parts.append("RUNTIME: local — access to machine, Ollama, SSH")
|
||||
elif _kind == "cloud":
|
||||
_parts.append("RUNTIME: cloud — NO local access, NO SSH, NO localhost")
|
||||
if _parts:
|
||||
_runtime_block = "[SYSTEM: RUNTIME CONTEXT — " + "; ".join(_parts) + "]\n\n"
|
||||
|
||||
# Always prepend cron execution guidance so the agent knows how
|
||||
# delivery works and can suppress delivery when appropriate.
|
||||
cron_hint = (
|
||||
@@ -673,7 +707,7 @@ def _build_job_prompt(job: dict) -> str:
|
||||
"\"[SCRIPT_FAILED]: forge.alexanderwhitestone.com timed out\" "
|
||||
"\"[SCRIPT_FAILED]: script exited with code 1\".]\\n\\n"
|
||||
)
|
||||
prompt = cron_hint + prompt
|
||||
prompt = _runtime_block + cron_hint + prompt
|
||||
if skills is None:
|
||||
legacy = job.get("skill")
|
||||
skills = [legacy] if legacy else []
|
||||
@@ -743,24 +777,23 @@ def run_job(job: dict) -> tuple[bool, str, str, Optional[str]]:
|
||||
|
||||
job_id = job["id"]
|
||||
job_name = job["name"]
|
||||
prompt = _build_job_prompt(job)
|
||||
|
||||
# Inject cloud context warning if running on cloud endpoint
|
||||
# and prompt references local services
|
||||
# Resolve runtime model/provider early so the prompt gets accurate context.
|
||||
_runtime_model = job.get("model") or os.getenv("HERMES_MODEL") or ""
|
||||
_runtime_provider = os.getenv("HERMES_PROVIDER", "")
|
||||
if not _runtime_model:
|
||||
try:
|
||||
_runtime_base_url = turn_route['runtime'].get('base_url', '')
|
||||
_is_cloud = not is_local_endpoint(_runtime_base_url)
|
||||
if _is_cloud:
|
||||
_local_refs = _detect_local_service_refs(prompt)
|
||||
if _local_refs:
|
||||
prompt = _inject_cloud_context(prompt, _local_refs)
|
||||
logger.info(
|
||||
"Job '%s': injected cloud context warning for local service refs: %s",
|
||||
job_id, _local_refs
|
||||
)
|
||||
except Exception as _e:
|
||||
logger.debug("Job '%s': cloud context injection skipped: %s", job_id, _e)
|
||||
import yaml as _y
|
||||
_cp2 = str(_hermes_home / "config.yaml")
|
||||
if os.path.exists(_cp2):
|
||||
with open(_cp2) as _f:
|
||||
_ce = _y.safe_load(_f) or {}
|
||||
_mc = _ce.get("model", {})
|
||||
_runtime_model = _mc if isinstance(_mc, str) else (_mc.get("default", "") if isinstance(_mc, dict) else "")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
prompt = _build_job_prompt(job, runtime_model=_runtime_model, runtime_provider=_runtime_provider)
|
||||
origin = _resolve_origin(job)
|
||||
_cron_session_id = f"cron_{job_id}_{_hermes_now().strftime('%Y%m%d_%H%M%S')}"
|
||||
|
||||
@@ -872,6 +905,14 @@ def run_job(job: dict) -> tuple[bool, str, str, Optional[str]]:
|
||||
message = format_runtime_provider_error(exc)
|
||||
raise RuntimeError(message) from exc
|
||||
|
||||
_active_provider = runtime.get("provider", "") or ""
|
||||
_mismatch = _detect_provider_mismatch(job.get("prompt", ""), _active_provider)
|
||||
if _mismatch:
|
||||
logger.warning(
|
||||
"Job '%s': prompt references '%s' but active provider is '%s'",
|
||||
job_name, _mismatch, _active_provider,
|
||||
)
|
||||
|
||||
from agent.smart_model_routing import resolve_turn_route
|
||||
turn_route = resolve_turn_route(
|
||||
prompt,
|
||||
|
||||
@@ -7,7 +7,7 @@ from unittest.mock import AsyncMock, patch, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from cron.scheduler import _resolve_origin, _resolve_delivery_target, _deliver_result, run_job, SILENT_MARKER, _build_job_prompt, _check_model_context_compat, ModelContextError, CRON_MIN_CONTEXT_TOKENS
|
||||
from cron.scheduler import _resolve_origin, _resolve_delivery_target, _deliver_result, run_job, SILENT_MARKER, _build_job_prompt, _check_model_context_compat, ModelContextError, CRON_MIN_CONTEXT_TOKENS, _classify_runtime, _detect_provider_mismatch
|
||||
|
||||
|
||||
class TestResolveOrigin:
|
||||
@@ -670,6 +670,13 @@ class TestRunJobSkillBacked:
|
||||
class TestSilentDelivery:
|
||||
"""Verify that [SILENT] responses suppress delivery while still saving output."""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _isolate_lock(self, tmp_path):
|
||||
"""Give each test its own tick lock file to prevent parallel test contention."""
|
||||
with patch("cron.scheduler._LOCK_FILE", tmp_path / ".tick.lock"), \
|
||||
patch("cron.scheduler._LOCK_DIR", tmp_path):
|
||||
yield
|
||||
|
||||
def _make_job(self):
|
||||
return {
|
||||
"id": "monitor-job",
|
||||
@@ -827,10 +834,102 @@ class TestBuildJobPromptMissingSkill:
|
||||
assert "go" in result
|
||||
|
||||
|
||||
class TestClassifyRuntime:
|
||||
"""Unit tests for _classify_runtime."""
|
||||
|
||||
def test_cloud_provider_explicit(self):
|
||||
assert _classify_runtime("openai", "") == "cloud"
|
||||
assert _classify_runtime("anthropic", "") == "cloud"
|
||||
assert _classify_runtime("nous", "") == "cloud"
|
||||
|
||||
def test_local_provider_explicit(self):
|
||||
assert _classify_runtime("ollama", "") == "local"
|
||||
assert _classify_runtime("local", "") == "local"
|
||||
|
||||
def test_cloud_detected_from_model_prefix(self):
|
||||
"""Model prefix 'nous/...' should be classified as cloud even with no provider."""
|
||||
assert _classify_runtime("", "nous/mimo-v2-pro") == "cloud"
|
||||
assert _classify_runtime("", "openai/gpt-4o") == "cloud"
|
||||
|
||||
def test_local_when_model_has_no_cloud_prefix(self):
|
||||
"""A model without a cloud prefix and no provider => local."""
|
||||
assert _classify_runtime("", "llama3") == "local"
|
||||
|
||||
def test_unknown_when_empty(self):
|
||||
assert _classify_runtime("", "") == "unknown"
|
||||
|
||||
|
||||
class TestBuildJobPromptRuntimeContext:
|
||||
"""Verify runtime context block injection in _build_job_prompt."""
|
||||
|
||||
def test_runtime_block_injected_with_model_and_provider(self):
|
||||
job = {"prompt": "Do something"}
|
||||
result = _build_job_prompt(job, runtime_model="nous/mimo-v2-pro", runtime_provider="nous")
|
||||
assert "RUNTIME CONTEXT" in result
|
||||
assert "MODEL: nous/mimo-v2-pro" in result
|
||||
assert "PROVIDER: nous" in result
|
||||
assert "cloud" in result
|
||||
|
||||
def test_provider_derived_from_model_prefix_when_empty(self):
|
||||
"""Fix #565: PROVIDER should be derived from model prefix when runtime_provider is empty."""
|
||||
job = {"prompt": "Do something"}
|
||||
result = _build_job_prompt(job, runtime_model="nous/mimo-v2-pro", runtime_provider="")
|
||||
assert "PROVIDER: nous" in result
|
||||
|
||||
def test_provider_not_empty_in_context_block(self):
|
||||
"""Fix #565: PROVIDER line must not be blank when model has a slash prefix."""
|
||||
job = {"prompt": "Check status"}
|
||||
result = _build_job_prompt(job, runtime_model="openai/gpt-4o", runtime_provider="")
|
||||
assert "PROVIDER: openai" in result
|
||||
assert "PROVIDER: ;" not in result
|
||||
assert "PROVIDER: ]" not in result
|
||||
|
||||
def test_no_runtime_block_when_no_model_or_provider(self):
|
||||
"""No runtime block should appear when neither model nor provider is given."""
|
||||
job = {"prompt": "Hello"}
|
||||
result = _build_job_prompt(job)
|
||||
assert "RUNTIME CONTEXT" not in result
|
||||
|
||||
def test_local_runtime_classification(self):
|
||||
"""ollama model should get local runtime label."""
|
||||
job = {"prompt": "Query local model"}
|
||||
result = _build_job_prompt(job, runtime_model="llama3", runtime_provider="ollama")
|
||||
assert "RUNTIME: local" in result
|
||||
assert "NO local access" not in result
|
||||
|
||||
def test_runtime_block_precedes_cron_hint(self):
|
||||
"""RUNTIME CONTEXT block should appear before the cron system hint."""
|
||||
job = {"prompt": "test"}
|
||||
result = _build_job_prompt(job, runtime_model="nous/mimo-v2-pro", runtime_provider="nous")
|
||||
runtime_pos = result.index("RUNTIME CONTEXT")
|
||||
cron_pos = result.index("scheduled cron job")
|
||||
assert runtime_pos < cron_pos
|
||||
|
||||
|
||||
class TestDetectProviderMismatch:
|
||||
"""Unit tests for _detect_provider_mismatch."""
|
||||
|
||||
def test_no_mismatch_when_same_provider(self):
|
||||
assert _detect_provider_mismatch("Use ollama to generate", "ollama") is None
|
||||
|
||||
def test_mismatch_detected(self):
|
||||
"""Prompt referencing 'ollama' while running on 'nous' should flag a mismatch."""
|
||||
result = _detect_provider_mismatch("Check if Ollama is responding", "nous")
|
||||
assert result == "ollama"
|
||||
|
||||
def test_no_mismatch_for_empty_inputs(self):
|
||||
assert _detect_provider_mismatch("", "nous") is None
|
||||
assert _detect_provider_mismatch("some prompt", "") is None
|
||||
|
||||
def test_no_mismatch_when_provider_unknown(self):
|
||||
"""Unknown active provider should not raise, just return None."""
|
||||
assert _detect_provider_mismatch("Check Ollama", "mystery-provider") is None
|
||||
|
||||
|
||||
class TestTickAdvanceBeforeRun:
|
||||
"""Verify that tick() calls advance_next_run before run_job for crash safety."""
|
||||
|
||||
def test_advance_called_before_run_job(self, tmp_path):
|
||||
def test_advance_called_before_run_job(self, tmp_path, monkeypatch):
|
||||
"""advance_next_run must be called before run_job to prevent crash-loop re-fires."""
|
||||
call_order = []
|
||||
|
||||
@@ -855,7 +954,9 @@ class TestTickAdvanceBeforeRun:
|
||||
patch("cron.scheduler.run_job", side_effect=fake_run_job), \
|
||||
patch("cron.scheduler.save_job_output", return_value=tmp_path / "out.md"), \
|
||||
patch("cron.scheduler.mark_job_run"), \
|
||||
patch("cron.scheduler._deliver_result"):
|
||||
patch("cron.scheduler._deliver_result"), \
|
||||
patch("cron.scheduler._LOCK_FILE", tmp_path / ".tick.lock"), \
|
||||
patch("cron.scheduler._LOCK_DIR", tmp_path):
|
||||
from cron.scheduler import tick
|
||||
executed = tick(verbose=False)
|
||||
|
||||
@@ -900,7 +1001,7 @@ class TestDeploySyncGuard:
|
||||
fake_module = MagicMock()
|
||||
fake_module.AIAgent = FakeAIAgent
|
||||
|
||||
with pytest.raises(RuntimeError, match="Missing parameters: tool_choice"):
|
||||
with pytest.raises(RuntimeError, match=r"(?s)missing params:.*tool_choice"):
|
||||
with patch.dict("sys.modules", {"run_agent": fake_module}):
|
||||
sched_mod._validate_agent_interface()
|
||||
finally:
|
||||
|
||||
@@ -1,181 +0,0 @@
|
||||
"""
|
||||
Test cloud context injection for cron jobs.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from cron.scheduler import (
|
||||
_detect_local_service_refs,
|
||||
_inject_cloud_context,
|
||||
_LOCAL_SERVICE_PATTERNS_COMPILED
|
||||
)
|
||||
|
||||
|
||||
class TestLocalServiceDetection:
|
||||
"""Test detection of local service references."""
|
||||
|
||||
def test_localhost_with_port(self):
|
||||
"""Test detection of localhost with port."""
|
||||
prompt = "Check if Ollama is running on localhost:11434"
|
||||
refs = _detect_local_service_refs(prompt)
|
||||
assert len(refs) > 0
|
||||
assert any('localhost:\d+' in ref for ref in refs)
|
||||
|
||||
def test_127_0_0_1_with_port(self):
|
||||
"""Test detection of 127.0.0.1 with port."""
|
||||
prompt = "Connect to http://127.0.0.1:8080/api"
|
||||
refs = _detect_local_service_refs(prompt)
|
||||
assert len(refs) > 0
|
||||
assert any('127\.0\.0\.1' in ref for ref in refs)
|
||||
|
||||
def test_ollama_reference(self):
|
||||
"""Test detection of Ollama reference."""
|
||||
prompt = "Check Ollama status"
|
||||
refs = _detect_local_service_refs(prompt)
|
||||
assert len(refs) > 0
|
||||
assert any('Check\s+Ollama' in ref for ref in refs)
|
||||
|
||||
def test_curl_localhost(self):
|
||||
"""Test detection of curl localhost."""
|
||||
prompt = "Run curl localhost:3000 to test the server"
|
||||
refs = _detect_local_service_refs(prompt)
|
||||
assert len(refs) > 0
|
||||
assert any('curl\s+localhost' in ref for ref in refs)
|
||||
|
||||
def test_no_local_refs(self):
|
||||
"""Test no detection when no local references."""
|
||||
prompt = "Check the weather in New York"
|
||||
refs = _detect_local_service_refs(prompt)
|
||||
assert len(refs) == 0
|
||||
|
||||
def test_multiple_refs(self):
|
||||
"""Test detection of multiple local references."""
|
||||
prompt = "Check localhost:3000 and also Ollama on 127.0.0.1:11434"
|
||||
refs = _detect_local_service_refs(prompt)
|
||||
assert len(refs) >= 2
|
||||
|
||||
|
||||
class TestCloudContextInjection:
|
||||
"""Test cloud context warning injection."""
|
||||
|
||||
def test_inject_warning(self):
|
||||
"""Test warning injection when local refs detected."""
|
||||
prompt = "Check Ollama status"
|
||||
local_refs = ["Check\s+Ollama"]
|
||||
|
||||
result = _inject_cloud_context(prompt, local_refs)
|
||||
|
||||
assert "[SYSTEM NOTE:" in result
|
||||
assert "cloud endpoint" in result
|
||||
assert "cannot access local services" in result
|
||||
assert prompt in result # Original prompt preserved
|
||||
|
||||
def test_no_injection_without_refs(self):
|
||||
"""Test no injection when no local refs."""
|
||||
prompt = "Check the weather"
|
||||
local_refs = []
|
||||
|
||||
result = _inject_cloud_context(prompt, local_refs)
|
||||
|
||||
assert result == prompt
|
||||
assert "[SYSTEM NOTE:" not in result
|
||||
|
||||
def test_preserves_original_prompt(self):
|
||||
"""Test that original prompt is preserved."""
|
||||
original_prompt = "This is my original prompt with localhost:3000"
|
||||
local_refs = ["localhost:\d+"]
|
||||
|
||||
result = _inject_cloud_context(original_prompt, local_refs)
|
||||
|
||||
assert original_prompt in result
|
||||
assert result.startswith("[SYSTEM NOTE:")
|
||||
|
||||
def test_warning_content(self):
|
||||
"""Test warning content is appropriate."""
|
||||
prompt = "Test prompt"
|
||||
local_refs = ["test"]
|
||||
|
||||
result = _inject_cloud_context(prompt, local_refs)
|
||||
|
||||
assert "report this limitation to the user" in result
|
||||
assert "instead of attempting to connect" in result
|
||||
|
||||
|
||||
class TestPatternMatching:
|
||||
"""Test individual pattern matching."""
|
||||
|
||||
def test_common_ports(self):
|
||||
"""Test detection of common development ports."""
|
||||
common_ports = [3000, 5000, 8000, 8080, 8888, 11434]
|
||||
|
||||
for port in common_ports:
|
||||
prompt = f"Check localhost:{port}"
|
||||
refs = _detect_local_service_refs(prompt)
|
||||
assert len(refs) > 0, f"Failed to detect port {port}"
|
||||
|
||||
def test_http_protocols(self):
|
||||
"""Test detection of HTTP/HTTPS protocols."""
|
||||
protocols = ["http://localhost", "https://localhost",
|
||||
"http://127.0.0.1", "https://127.0.0.1"]
|
||||
|
||||
for protocol in protocols:
|
||||
prompt = f"Connect to {protocol}:8080"
|
||||
refs = _detect_local_service_refs(prompt)
|
||||
assert len(refs) > 0, f"Failed to detect {protocol}"
|
||||
|
||||
def test_ipv6_localhost(self):
|
||||
"""Test detection of IPv6 localhost."""
|
||||
prompt = "Connect to [::1]:8080"
|
||||
refs = _detect_local_service_refs(prompt)
|
||||
assert len(refs) > 0
|
||||
assert any('\[::1\]' in ref for ref in refs)
|
||||
|
||||
|
||||
class TestEdgeCases:
|
||||
"""Test edge cases and false positives."""
|
||||
|
||||
def test_case_insensitive(self):
|
||||
"""Test case insensitive matching."""
|
||||
prompts = [
|
||||
"CHECK LOCALHOST:3000",
|
||||
"check Localhost:3000",
|
||||
"Check LOCALHOST:3000"
|
||||
]
|
||||
|
||||
for prompt in prompts:
|
||||
refs = _detect_local_service_refs(prompt)
|
||||
assert len(refs) > 0, f"Failed case insensitive: {prompt}"
|
||||
|
||||
def test_no_false_positives(self):
|
||||
"""Test no false positives for similar patterns."""
|
||||
safe_prompts = [
|
||||
"Check the localhost documentation",
|
||||
"Read about 127.0.0.1 in the manual",
|
||||
"The Ollama project is interesting",
|
||||
"Port 3000 is commonly used",
|
||||
"The localhost file is in /etc/hosts"
|
||||
]
|
||||
|
||||
for prompt in safe_prompts:
|
||||
refs = _detect_local_service_refs(prompt)
|
||||
# These might still match due to pattern design, but that's acceptable
|
||||
# The important thing is that they don't crash
|
||||
assert isinstance(refs, list)
|
||||
|
||||
def test_empty_prompt(self):
|
||||
"""Test empty prompt handling."""
|
||||
refs = _detect_local_service_refs("")
|
||||
assert refs == []
|
||||
|
||||
def test_none_handling(self):
|
||||
"""Test None prompt handling."""
|
||||
# The function should handle None gracefully
|
||||
try:
|
||||
refs = _detect_local_service_refs(None)
|
||||
assert refs == []
|
||||
except Exception as e:
|
||||
# If it raises an exception, that's also acceptable
|
||||
assert isinstance(e, (TypeError, AttributeError))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__])
|
||||
Reference in New Issue
Block a user