Skip to content

The agent that grows with you

test: exercise cron provider and workdir paths through owner

Teknium9fde59d3d103
diff --git a/tests/cron/test_codex_execution_paths.py b/tests/cron/test_codex_execution_paths.py
index 574e8f5446a..6cd4e71089c 100644
--- a/tests/cron/test_codex_execution_paths.py
+++ b/tests/cron/test_codex_execution_paths.py
@@ -1,4 +1,5 @@
import asyncio
+import json
import sys
import types
from types import SimpleNamespace
@@ -8,13 +9,56 @@ sys.modules.setdefault("fire", types.SimpleNamespace(Fire=lambda *a, **k: None))
sys.modules.setdefault("firecrawl", types.SimpleNamespace(Firecrawl=object))
sys.modules.setdefault("fal_client", types.SimpleNamespace())
-import cron.scheduler as cron_scheduler
import gateway.run as gateway_run
import run_agent
from gateway.config import Platform
from gateway.session import SessionSource
+def _run_owned_job(job, tmp_path):
+ """Exercise the production owner bridge with an isolated canonical store."""
+ from gateway.session_contract import SessionRef
+ from gateway.session_cron import current_execution, execute
+ from hermes_state_registry import acquire, release
+
+ ref = SessionRef("test-profile", "cron-owner-session")
+ admission_id = "cron-owner-admission"
+ request_id = "cron-owner-request"
+ db = acquire(tmp_path / "state.db")
+ db.create_session(ref.session_id, source="cron")
+ authority = SimpleNamespace(
+ db=db,
+ sessions={ref.session_id: SimpleNamespace(source=SimpleNamespace(user_id="cron-owner"))},
+ pending_results={},
+ )
+ row = {
+ "admission_id": admission_id, "request_id": request_id,
+ "principal_id": "cron-owner", "payload": {"text": ""},
+ }
+ policy = SimpleNamespace(request_json=json.dumps({
+ "cron_job": job, "extra_prompt": None, "request_id": request_id,
+ }))
+
+ async def run():
+ previous = current_execution()
+ try:
+ await execute(authority, ref, row, policy)
+ except RuntimeError as exc:
+ # Failed scheduler tuples are published before execute raises.
+ saved = authority.pending_results.get(admission_id)
+ if saved is None:
+ raise
+ assert str(exc) == saved["result"]["cron_result"][3]
+ assert current_execution() is previous
+ assert authority._cron_cancellations == {}
+ return tuple(authority.pending_results[admission_id]["result"]["cron_result"])
+
+ try:
+ return asyncio.run(run())
+ finally:
+ release(db)
+
+
def _patch_agent_bootstrap(monkeypatch):
monkeypatch.setattr(
"model_tools.get_tool_definitions",
@@ -91,7 +135,7 @@ class _Codex401ThenSuccessAgent(run_agent.AIAgent):
return super().run_conversation(user_message, conversation_history=conversation_history, task_id=task_id)
-def test_cron_run_job_codex_path_handles_internal_401_refresh(monkeypatch):
+def test_cron_run_job_codex_path_handles_internal_401_refresh(monkeypatch, tmp_path):
_patch_agent_bootstrap(monkeypatch)
monkeypatch.setattr("agent.process_bootstrap.OpenAI", _FakeOpenAI)
monkeypatch.setattr(run_agent, "AIAgent", _Codex401ThenSuccessAgent)
@@ -109,8 +153,9 @@ def test_cron_run_job_codex_path_handles_internal_401_refresh(monkeypatch):
_Codex401ThenSuccessAgent.refresh_attempts = 0
_Codex401ThenSuccessAgent.last_init = {}
- success, output, final_response, error = cron_scheduler.run_job(
- {"id": "job-1", "name": "Codex Refresh Test", "prompt": "ping", "model": "gpt-5.3-codex"}
+ success, output, final_response, error = _run_owned_job(
+ {"id": "job-1", "name": "Codex Refresh Test", "prompt": "ping", "model": "gpt-5.3-codex"},
+ tmp_path,
)
assert success is True
diff --git a/tests/cron/test_cron_provider_pin.py b/tests/cron/test_cron_provider_pin.py
index cd9d3e59783..3648d28530d 100644
--- a/tests/cron/test_cron_provider_pin.py
+++ b/tests/cron/test_cron_provider_pin.py
@@ -17,14 +17,59 @@ These tests exercise the full run_job path (real imports, mocked AIAgent +
resolve_runtime_provider against a temp HERMES_HOME) and the create_job snapshot capture.
"""
+import asyncio
+import json
import sys
from pathlib import Path
+from types import SimpleNamespace
from unittest.mock import MagicMock, patch
# Ensure project root is importable.
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
-from cron.scheduler import run_job
+
+def _run_owned_job(job, tmp_path):
+ """Exercise the production owner bridge with an isolated canonical store."""
+ from gateway.session_contract import SessionRef
+ from gateway.session_cron import current_execution, execute
+ from hermes_state_registry import acquire, release
+
+ ref = SessionRef("test-profile", "cron-owner-session")
+ admission_id = "cron-owner-admission"
+ request_id = "cron-owner-request"
+ db = acquire(tmp_path / "state.db")
+ db.create_session(ref.session_id, source="cron")
+ authority = SimpleNamespace(
+ db=db,
+ sessions={ref.session_id: SimpleNamespace(source=SimpleNamespace(user_id="cron-owner"))},
+ pending_results={},
+ )
+ row = {
+ "admission_id": admission_id, "request_id": request_id,
+ "principal_id": "cron-owner", "payload": {"text": ""},
+ }
+ policy = SimpleNamespace(request_json=json.dumps({
+ "cron_job": job, "extra_prompt": None, "request_id": request_id,
+ }))
+
+ async def run():
+ previous = current_execution()
+ try:
+ await execute(authority, ref, row, policy)
+ except RuntimeError as exc:
+ # Failed scheduler tuples are published before execute raises.
+ saved = authority.pending_results.get(admission_id)
+ if saved is None:
+ raise
+ assert str(exc) == saved["result"]["cron_result"][3]
+ assert current_execution() is previous
+ assert authority._cron_cancellations == {}
+ return tuple(authority.pending_results[admission_id]["result"]["cron_result"])
+
+ try:
+ return asyncio.run(run())
+ finally:
+ release(db)
def _base_job(**overrides):
@@ -74,19 +119,17 @@ def _run(job, tmp_path, *, current_provider="openrouter", current_model=None, cr
"api_mode": "chat_completions",
}
- fake_db = MagicMock()
with patch("cron.scheduler._hermes_home", tmp_path), \
patch("cron.scheduler._get_hermes_home", return_value=tmp_path), \
patch("cron.scheduler_delivery._resolve_origin", return_value=None), \
patch("hermes_cli.env_loader.load_hermes_dotenv"), \
patch("hermes_cli.env_loader.reset_secret_source_cache"), \
- patch("hermes_state_registry.acquire", return_value=fake_db), \
patch("hermes_cli.runtime_provider.resolve_runtime_provider", side_effect=_resolve), \
patch("run_agent.AIAgent") as mock_agent_cls:
mock_agent = MagicMock()
mock_agent.run_conversation.return_value = {"final_response": "ok"}
mock_agent_cls.return_value = mock_agent
- success, _output, _final, error = run_job(job)
+ success, _output, _final, error = _run_owned_job(job, tmp_path)
agent_kwargs = mock_agent_cls.call_args.kwargs if mock_agent_cls.called else None
return success, error, agent_kwargs, (resolve_kwargs or None)
diff --git a/tests/cron/test_cron_workdir.py b/tests/cron/test_cron_workdir.py
index 549a92a7618..2992623ecae 100644
--- a/tests/cron/test_cron_workdir.py
+++ b/tests/cron/test_cron_workdir.py
@@ -12,11 +12,57 @@ Covers:
from __future__ import annotations
+import asyncio
import json
+from types import SimpleNamespace
import pytest
+def _run_owned_job(job, tmp_path):
+ """Exercise the production owner bridge with an isolated canonical store."""
+ from gateway.session_contract import SessionRef
+ from gateway.session_cron import current_execution, execute
+ from hermes_state_registry import acquire, release
+
+ ref = SessionRef("test-profile", "cron-owner-session")
+ admission_id = "cron-owner-admission"
+ request_id = "cron-owner-request"
+ db = acquire(tmp_path / "state.db")
+ db.create_session(ref.session_id, source="cron")
+ authority = SimpleNamespace(
+ db=db,
+ sessions={ref.session_id: SimpleNamespace(source=SimpleNamespace(user_id="cron-owner"))},
+ pending_results={},
+ )
+ row = {
+ "admission_id": admission_id, "request_id": request_id,
+ "principal_id": "cron-owner", "payload": {"text": ""},
+ }
+ policy = SimpleNamespace(request_json=json.dumps({
+ "cron_job": job, "extra_prompt": None, "request_id": request_id,
+ }))
+
+ async def run():
+ previous = current_execution()
+ try:
+ await execute(authority, ref, row, policy)
+ except RuntimeError as exc:
+ # Failed scheduler tuples are published before execute raises.
+ saved = authority.pending_results.get(admission_id)
+ if saved is None:
+ raise
+ assert str(exc) == saved["result"]["cron_result"][3]
+ assert current_execution() is previous
+ assert authority._cron_cancellations == {}
+ return tuple(authority.pending_results[admission_id]["result"]["cron_result"])
+
+ try:
+ return asyncio.run(run())
+ finally:
+ release(db)
+
+
@pytest.fixture()
def tmp_cron_dir(tmp_path, monkeypatch):
"""Isolate cron job storage into a temp dir so tests don't stomp on real jobs."""
@@ -248,7 +294,7 @@ class TestRunJobTerminalCwd:
monkeypatch.setattr(dotenv, "load_dotenv", lambda *_a, **_kw: True)
- def test_no_workdir_leaves_terminal_cwd_untouched(self, monkeypatch):
+ def test_no_workdir_leaves_terminal_cwd_untouched(self, monkeypatch, tmp_path):
"""When workdir is absent, run_job must not touch TERMINAL_CWD at all —
whatever value was present before the call should be present after.
@@ -275,7 +321,7 @@ class TestRunJobTerminalCwd:
"schedule_display": "manual",
}
- success, *_ = sched.run_job(job)
+ success, *_ = _run_owned_job(job, tmp_path)
assert success is True
# Feature is OFF — skip_context_files stays True.
@@ -304,13 +350,14 @@ class TestRunJobTerminalCwd:
observed: dict = {}
self._install_stubs(monkeypatch, observed)
- success, *_ = sched.run_job(
+ success, *_ = _run_owned_job(
{
"id": "cwd-bound",
"name": "cwd-bound",
"workdir": str(workdir),
"schedule_display": "manual",
- }
+ },
+ tmp_path,
)
assert success is True