feat(cron): Cron Recipes — parameterized automation templates across every surface

A 'recipe' is a one-place definition of an automation that every surface
renders natively. The slot schema (cron/recipe_catalog.py) is the single
source of truth; four renderers consume it, and all paths end at the same
cron.jobs.create_job — no second job engine.

Form where there's a screen, conversation where there's a chat line:
- Dashboard / GUI app: a Recipes sub-tab on the Cron page renders each
  recipe's typed slots as a form (time-picker, enum dropdown, free-text);
  submit POSTs /api/cron/recipes/instantiate which fills + creates the job.
- CLI / TUI / messengers: /cron-recipe lists the catalog, shows a recipe's
  fields, or fills + creates from a pasted 'key slot=val' command. The shared
  handler (hermes_cli/cron_recipe_cmd.py) names any missing/invalid slot so
  the agent can ask a targeted follow-up.
- Docs: a generated Cron Recipes catalog page (website, .mdx + React cards)
  shows each recipe with a copy-paste command and a 'Send to App' button.
- Desktop: a hermes:// URL scheme (Electron single-instance lock +
  setAsDefaultProtocolClient + open-url/second-instance) routes
  hermes://cron-recipe/<key>?slot=val into the chat composer pre-filled.

Typed slots (time/enum/text/weekdays) with defaults: users never type raw
cron — recipes parameterize time-of-day and weekday sets and translate to
cron expressions; a free-text 'schedule' slot is the full-flexibility escape
hatch. Consent-first throughout: nothing schedules without an explicit submit
or send.

Core:
- cron/recipe_catalog.py — CronRecipe + RecipeSlot, 5 curated recipes,
  recipe_form_schema / recipe_slash_command / recipe_deeplink /
  recipe_catalog_entry renderers, fill_recipe (validate + translate to
  create_job kwargs).
- hermes_cli/cron_recipe_cmd.py — shared /cron-recipe handler (CLI + TUI +
  gateway never drift). CommandDef + dispatch in commands.py / cli.py /
  gateway/run.py.

Dashboard: GET /api/cron/recipes + POST /api/cron/recipes/instantiate
(web_server.py), CronRecipes.tsx gallery+form, Segmented sub-tab on CronPage,
api.ts methods + types.

Desktop: hermes:// scheme end to end (main.cjs deep-link router + ready-queue,
preload onDeepLink/signalDeepLinkReady, global.d.ts types, desktop-controller
composer prefill, electron-builder protocols key).

Docs: extract-cron-recipes.py generator wired into prebuild.mjs,
cron-recipes-catalog.mdx + CronRecipesCatalog React component, sidebar entry.
Generated index json gitignored like skills.json.

Tests: 23 core (catalog/slots/schedule-resolution/validation/renderers/command
handler/generator) + 5 web_server endpoint tests. E2E verified end to end:
slot fill -> create_job -> persisted job with correct schedule/deliver/origin.
This commit is contained in:
teknium1
2026-06-11 10:49:47 -07:00
committed by Teknium
parent 9a09ea69fb
commit 1593ca5406
25 changed files with 1975 additions and 0 deletions
+195
View File
@@ -0,0 +1,195 @@
"""Tests for Cron Recipes — the parameterized automation template system.
Covers the core catalog/slot schema/renderers/fill (cron/recipe_catalog.py),
the shared /cron-recipe command handler (hermes_cli/cron_recipe_cmd.py), and
the docs generator. Uses an isolated HERMES_HOME for anything that touches the
cron job store.
"""
import importlib
import json
from pathlib import Path
from unittest.mock import patch
import pytest
from cron.recipe_catalog import (
CATALOG,
RecipeFillError,
RecipeSlot,
fill_recipe,
get_recipe,
recipe_catalog_entry,
recipe_deeplink,
recipe_form_schema,
recipe_slash_command,
)
class TestCatalog:
def test_catalog_nonempty_and_keyed(self):
assert len(CATALOG) >= 1
for r in CATALOG:
assert get_recipe(r.key) is r
def test_every_slot_has_known_type(self):
for r in CATALOG:
for s in r.slots:
assert s.type in {"time", "enum", "text", "weekdays"}
def test_bad_slot_type_rejected(self):
with pytest.raises(ValueError):
RecipeSlot(name="x", type="bogus", label="X")
class TestScheduleResolution:
def test_time_to_cron(self):
spec = fill_recipe(get_recipe("morning-brief"), {"time": "08:30"})
assert spec["schedule"] == "30 8 * * *"
def test_interval_schedule(self):
spec = fill_recipe(
get_recipe("important-mail"),
{"interval_min": "15", "criteria": "x", "deliver": "origin"},
)
assert spec["schedule"] == "*/15 * * * *"
def test_day_to_dow(self):
spec = fill_recipe(
get_recipe("weekly-review"),
{"time": "18:00", "day": "sunday", "deliver": "origin"},
)
assert spec["schedule"] == "0 18 * * 0"
def test_weekday_preset_to_dow(self):
spec = fill_recipe(
get_recipe("custom-reminder"),
{"what": "stretch", "time": "14:00", "recurrence": "weekdays", "deliver": "origin"},
)
assert spec["schedule"] == "0 14 * * 1-5"
def test_defaults_fill_when_omitted(self):
spec = fill_recipe(get_recipe("morning-brief"), {})
assert spec["schedule"] == "0 8 * * *"
class TestValidation:
def test_invalid_time_rejected(self):
with pytest.raises(RecipeFillError, match="invalid time"):
fill_recipe(get_recipe("morning-brief"), {"time": "25:99"})
def test_bad_enum_rejected_and_names_slot(self):
with pytest.raises(RecipeFillError, match="not allowed"):
fill_recipe(get_recipe("morning-brief"), {"time": "08:00", "deliver": "pigeon"})
def test_text_slot_renders_into_prompt(self):
spec = fill_recipe(
get_recipe("important-mail"),
{"interval_min": "30", "criteria": "from my CEO", "deliver": "origin"},
)
assert "from my CEO" in spec["prompt"]
def test_origin_threads_through(self):
spec = fill_recipe(
get_recipe("morning-brief"), {"time": "08:00"}, origin={"platform": "telegram", "chat_id": "9"}
)
assert spec["origin"] == {"platform": "telegram", "chat_id": "9"}
class TestRenderers:
def test_form_schema_fields(self):
schema = recipe_form_schema(get_recipe("morning-brief"))
names = [f["name"] for f in schema["fields"]]
assert names == ["time", "deliver"]
assert schema["key"] == "morning-brief"
def test_slash_command_defaults(self):
cmd = recipe_slash_command(get_recipe("morning-brief"))
assert cmd.startswith("/cron-recipe morning-brief")
assert "time=08:00" in cmd
def test_slash_command_quotes_freetext(self):
cmd = recipe_slash_command(
get_recipe("custom-reminder"), {"what": "drink water", "time": "10:00"}
)
assert '"drink water"' in cmd
def test_deeplink_shape(self):
url = recipe_deeplink(get_recipe("morning-brief"), {"time": "07:15"})
assert url.startswith("hermes://cron-recipe/morning-brief?")
assert "time=07" in url
def test_catalog_entry_has_all_surfaces(self):
entry = recipe_catalog_entry(get_recipe("morning-brief"))
assert entry["command"].startswith("/cron-recipe")
assert entry["appUrl"].startswith("hermes://")
assert entry["scheduleHuman"]
assert "fields" in entry
@pytest.fixture
def isolated_home(tmp_path, monkeypatch):
home = tmp_path / ".hermes"
home.mkdir()
monkeypatch.setenv("HERMES_HOME", str(home))
import hermes_constants
importlib.reload(hermes_constants)
import cron.jobs as jobs
importlib.reload(jobs)
return jobs
class TestCommandHandler:
def test_bare_lists_catalog(self, isolated_home):
from hermes_cli.cron_recipe_cmd import handle_cron_recipe_command
out = handle_cron_recipe_command("")
assert "morning-brief" in out and "Cron Recipes" in out
def test_show_recipe_fields(self, isolated_home):
from hermes_cli.cron_recipe_cmd import handle_cron_recipe_command
out = handle_cron_recipe_command("morning-brief")
assert "Fields:" in out and "time" in out
def test_fill_creates_job(self, isolated_home):
from hermes_cli.cron_recipe_cmd import handle_cron_recipe_command
out = handle_cron_recipe_command("morning-brief time=07:30 deliver=telegram")
assert "Scheduled" in out
jobs = isolated_home.load_jobs()
assert len(jobs) == 1
assert (jobs[0].get("schedule_display") or jobs[0].get("schedule")) == "30 7 * * *"
assert jobs[0].get("deliver") == "telegram"
def test_unknown_recipe(self, isolated_home):
from hermes_cli.cron_recipe_cmd import handle_cron_recipe_command
out = handle_cron_recipe_command("does-not-exist")
assert "No cron recipe" in out
def test_bad_value_names_slot(self, isolated_home):
from hermes_cli.cron_recipe_cmd import handle_cron_recipe_command
out = handle_cron_recipe_command("morning-brief time=99:99")
assert "Can't set up" in out and "time" in out
class TestDocsGenerator:
def test_generator_emits_valid_index(self, tmp_path):
# The generator imports the catalog and writes a flat JSON array.
import importlib.util
script = (
Path(__file__).resolve().parents[2]
/ "website" / "scripts" / "extract-cron-recipes.py"
)
spec = importlib.util.spec_from_file_location("extract_cron_recipes", script)
assert spec is not None and spec.loader is not None
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
index = mod.build_index()
assert isinstance(index, list) and len(index) == len(CATALOG)
# Each entry must round-trip through json and carry the surfaces.
json.dumps(index)
assert all("command" in e and "appUrl" in e for e in index)
+36
View File
@@ -2360,6 +2360,42 @@ class TestNewEndpoints:
resp = self.client.get("/api/cron/jobs/nonexistent-id")
assert resp.status_code == 404
# --- Cron Recipes ---
def test_cron_recipes_list(self):
resp = self.client.get("/api/cron/recipes")
assert resp.status_code == 200
recipes = resp.json()["recipes"]
assert len(recipes) >= 1
first = recipes[0]
assert "fields" in first
assert first["command"].startswith("/cron-recipe")
assert first["appUrl"].startswith("hermes://")
def test_cron_recipe_instantiate_creates_job(self):
resp = self.client.post(
"/api/cron/recipes/instantiate",
json={"recipe": "morning-brief", "values": {"time": "07:30", "deliver": "local"}},
)
assert resp.status_code == 200
job = resp.json()
assert (job.get("schedule_display") or "").strip() == "30 7 * * *" or \
(job.get("schedule", {}) or {}).get("expr") == "30 7 * * *"
def test_cron_recipe_instantiate_unknown_404(self):
resp = self.client.post(
"/api/cron/recipes/instantiate",
json={"recipe": "does-not-exist", "values": {}},
)
assert resp.status_code == 404
def test_cron_recipe_instantiate_bad_value_422(self):
resp = self.client.post(
"/api/cron/recipes/instantiate",
json={"recipe": "morning-brief", "values": {"time": "99:99"}},
)
assert resp.status_code == 422
# --- Profiles ---
def test_profiles_list_includes_default(self):