Owner jobs and guests' own scheduled jobs are the cron-jobs live plugin's (forms move-ai-agent-cron-jobs-into-a-plugin_u0wa6b, cron-scheduled-in-the-cli-and-mcp_x4nk83). Gone: backend/cron/, schemas/cron.py, /api/cron*, /api/me/cron*, the cron scheduler thread, Settings → Cron and the guests' Tâches planifiées pages, `ai-agent cron` (now a pointer), MCP run_cron_job / set_cron_job / browse cron, the scheduled-jobs core skill, guestDefaults.cronLimits. The core keeps thin contracts: - runs.guest bridge (runs/guestjob.py): a guest's job run as that guest, confined by the core from their record; the plugin only names it. - runs.start's cron stamp → meta.cron. - delivery of a guest job's result (push, mail, mailHtml) off the stamp. - « Discuter »: POST /api/cron-runs/discuss. - runs.cron provider readers (plugins/cron_bridge.py): budget, auto-archive, agents catalog, the agents page's Schedule. - core/cronexpr.py, agents/runconfig.py; context.write create for a new agent definition; ui.pages[].unless; setRoute keeps a Settings page under /settings/p/. - carryover.cron_jobs: /data/cron-jobs.json → the plugin's jobs.json, cronLimits → limits.json, guests' qwen jobs → sonnet (PR #23's decision, superseded). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
167 lines
8.1 KiB
Python
167 lines
8.1 KiB
Python
"""The core's side of the cron-jobs plugin (2026-11-28): the ``runs.cron``
|
|
readers (``plugins/cron_bridge.py`` — cached, never a wait on a hot path, a
|
|
broken plugin is an empty list), the one-time carry-over of
|
|
``/data/cron-jobs.json`` + ``guestDefaults.cronLimits`` (and PR #23's move of
|
|
the guests' local jobs to Sonnet), a plugin page's ``unless``, and the
|
|
``context.write`` bridge making a new agent definition only."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import pathlib
|
|
import tempfile
|
|
import time
|
|
import types
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
from fastapi import HTTPException
|
|
|
|
from plugins import carryover, cron_bridge
|
|
from plugins.store import PluginStore
|
|
|
|
|
|
class Readers(unittest.TestCase):
|
|
def setUp(self):
|
|
cron_bridge.drop_cache()
|
|
self.addCleanup(cron_bridge.drop_cache)
|
|
self.deps = types.SimpleNamespace()
|
|
self.asked: list[str] = []
|
|
self.jobs = [{"id": "radar", "promptFile": ".claude/agents/radar.md", "enabled": True,
|
|
"history": {"2026-11-28T07:00:00+0100": "s1"}},
|
|
{"id": "dom-x", "owner": "dom", "promptFile": None}]
|
|
|
|
def run_op(deps, op, payload=None):
|
|
self.asked.append(op)
|
|
if op == "boom":
|
|
raise HTTPException(500, "x")
|
|
return {"jobs": list(self.jobs)}
|
|
for p in (mock.patch.object(cron_bridge, "run_op", run_op),
|
|
mock.patch.object(cron_bridge, "available", lambda deps: True)):
|
|
p.start()
|
|
self.addCleanup(p.stop)
|
|
|
|
def test_index_waits_once_then_caches(self):
|
|
self.assertEqual(len(cron_bridge.index(self.deps, wait=True)), 2)
|
|
cron_bridge.index(self.deps, wait=True)
|
|
self.assertEqual(self.asked, ["index"])
|
|
|
|
def test_a_hot_path_never_waits(self):
|
|
self.assertEqual(cron_bridge.index(self.deps), []) # first: nothing yet
|
|
for _ in range(50): # the refresh thread lands
|
|
if cron_bridge.index(self.deps):
|
|
break
|
|
time.sleep(0.02)
|
|
self.assertEqual(len(cron_bridge.index(self.deps)), 2)
|
|
|
|
def test_a_stale_cache_answers_while_it_refreshes(self):
|
|
cron_bridge.index(self.deps, wait=True)
|
|
with cron_bridge._lock:
|
|
t, jobs = cron_bridge._cache["index"]
|
|
cron_bridge._cache["index"] = (t - 999, jobs)
|
|
self.jobs = []
|
|
self.assertEqual(len(cron_bridge.index(self.deps)), 2) # stale, at once
|
|
|
|
def test_by_prompt_file_is_an_owner_job(self):
|
|
self.assertEqual(cron_bridge.by_prompt_file(self.deps, "/.claude/agents/radar.md")["id"], "radar")
|
|
self.assertIsNone(cron_bridge.by_prompt_file(self.deps, ".claude/agents/x.md"))
|
|
|
|
def test_a_broken_plugin_is_an_empty_list(self):
|
|
with mock.patch.object(cron_bridge, "run_op", side_effect=HTTPException(500, "x")):
|
|
self.assertEqual(cron_bridge.jobs(self.deps), [])
|
|
|
|
def test_no_plugin(self):
|
|
with mock.patch.object(cron_bridge, "available", lambda deps: False):
|
|
self.assertEqual(cron_bridge.index(self.deps, wait=True), [])
|
|
self.assertIsNone(cron_bridge.get(self.deps, "radar"))
|
|
|
|
|
|
class CarryOver(unittest.TestCase):
|
|
def setUp(self):
|
|
self.td = pathlib.Path(tempfile.mkdtemp())
|
|
self.store = PluginStore(self.td / "plugins")
|
|
self.legacy = self.td / "cron-jobs.json"
|
|
self.legacy.write_text(json.dumps({
|
|
"radar": {"id": "radar", "promptFile": ".claude/agents/radar.md", "enabled": True,
|
|
"history": {"t": "s1"}},
|
|
"arthur-veille": {"id": "arthur-veille", "owner": "arthur", "model": "qwen"},
|
|
"dom-recap": {"id": "dom-recap", "owner": "dom"},
|
|
"cel-meta": {"id": "cel-meta", "owner": "cel", "model": "sonnet"}}))
|
|
self.users = types.SimpleNamespace(
|
|
legacy_cron_limits=lambda: {"maxJobs": 3, "defaultModel": "qwen"})
|
|
|
|
def shared(self) -> pathlib.Path:
|
|
return self.store.shared_data_dir(carryover.CRON_PLUGIN)
|
|
|
|
def test_jobs_and_limits_move_once_and_guests_go_to_sonnet(self):
|
|
self.assertTrue(carryover.cron_jobs(self.store, self.users, str(self.legacy)))
|
|
jobs = json.loads((self.shared() / "jobs.json").read_text())
|
|
self.assertEqual(jobs["radar"]["history"], {"t": "s1"}) # unchanged
|
|
self.assertEqual({k: jobs[k].get("model") for k in ("arthur-veille", "dom-recap", "cel-meta")},
|
|
{"arthur-veille": "sonnet", "dom-recap": "sonnet", "cel-meta": "sonnet"})
|
|
self.assertIsNone(jobs["radar"].get("model")) # an owner job: as is
|
|
self.assertEqual(json.loads((self.shared() / "limits.json").read_text()),
|
|
{"maxJobs": 3, "defaultModel": "sonnet"})
|
|
self.assertFalse(self.legacy.exists())
|
|
self.assertTrue(self.legacy.with_name("cron-jobs.json.migrated").exists())
|
|
# once
|
|
self.legacy.write_text("{}")
|
|
self.assertFalse(carryover.cron_jobs(self.store, self.users, str(self.legacy)))
|
|
self.assertTrue(self.legacy.exists())
|
|
|
|
def test_pr23_already_moved_them(self):
|
|
(self.td / carryover.CRON_SONNET_MARKER).write_text("{}")
|
|
carryover.cron_jobs(self.store, self.users, str(self.legacy))
|
|
jobs = json.loads((self.shared() / "jobs.json").read_text())
|
|
self.assertEqual(jobs["arthur-veille"]["model"], "qwen") # picked again since
|
|
self.assertEqual(json.loads((self.shared() / "limits.json").read_text())["defaultModel"], "qwen")
|
|
|
|
def test_the_plugins_own_store_wins(self):
|
|
(self.shared() / "jobs.json").write_text('{"x": {"id": "x"}}')
|
|
carryover.cron_jobs(self.store, self.users, str(self.legacy))
|
|
self.assertEqual(json.loads((self.shared() / "jobs.json").read_text()), {"x": {"id": "x"}})
|
|
self.assertFalse(self.legacy.exists())
|
|
|
|
|
|
class ContextWriteCreate(unittest.TestCase):
|
|
def test_only_a_new_agent_definition(self):
|
|
from files import routes as files_routes
|
|
td = pathlib.Path(tempfile.mkdtemp())
|
|
deps = types.SimpleNamespace(indexer=types.SimpleNamespace(scan_files=lambda **kw: None),
|
|
store=types.SimpleNamespace(get_file=lambda p: None))
|
|
with mock.patch.object(files_routes, "WORKSPACE", td), \
|
|
mock.patch.object(files_routes, "_is_context_file", lambda rel: True), \
|
|
mock.patch.object(files_routes, "_is_editable", lambda rel: True):
|
|
with self.assertRaises(HTTPException) as e:
|
|
files_routes.write_context_file(deps, ".claude/agents/new.md", "x")
|
|
self.assertEqual(e.exception.status_code, 404) # no create: as before
|
|
files_routes.write_context_file(deps, ".claude/agents/new.md", "x", create=True)
|
|
self.assertEqual((td / ".claude/agents/new.md").read_text(), "x")
|
|
for bad in (".claude/skills/x/SKILL.md", ".claude/agents/sub/x.md", "CLAUDE.md",
|
|
".claude/agents/x.txt"):
|
|
with self.assertRaises(HTTPException, msg=bad):
|
|
files_routes.write_context_file(deps, bad, "x", create=True)
|
|
|
|
|
|
class PageUnless(unittest.TestCase):
|
|
def test_a_page_hidden_from_the_permission_holder(self):
|
|
from plugins import manifest
|
|
td = pathlib.Path(tempfile.mkdtemp())
|
|
(td / "ui").mkdir()
|
|
(td / "ui" / "p.html").write_text("<p>x</p>")
|
|
(td / "plugin.json").write_text(json.dumps({
|
|
"schema": 1, "id": "pg", "name": "Pg", "ui": {"pages": [
|
|
{"id": "mine", "label": "Mine", "nav": "settings", "html": "ui/p.html",
|
|
"unless": "automation.manage"}]}}))
|
|
mf = manifest.validate_manifest(td)
|
|
self.assertEqual(mf["ui"]["pages"][0]["unless"], "automation.manage")
|
|
(td / "plugin.json").write_text(json.dumps({
|
|
"schema": 1, "id": "pg", "name": "Pg", "ui": {"pages": [
|
|
{"id": "mine", "label": "Mine", "html": "ui/p.html", "unless": "nope"}]}}))
|
|
with self.assertRaises(manifest.ManifestError):
|
|
manifest.validate_manifest(td)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|