feat: cron job system for Inara (remind + note types)

- cron_runner.py: job storage (CRONS.json), schedule parsing, execution
- tools/cron.py: cron_list/add/remove/toggle + reminders_clear tools
- scheduler.py: load user crons at startup, expose get_scheduler() for
  live add/remove without restarts
- context_loader.py: auto-include REMINDERS.md in system prompt (tier 2+)
  so cron reminders surface automatically without Inara having to poll
- inara/CRONS.json + REMINDERS.md: backing files (initially empty)

Schedule formats: hourly | daily | daily:HH:MM | weekly:DOW | weekly:DOW:HH:MM
Job types: remind (→ REMINDERS.md) | note (→ SCRATCH.md)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Scott Idem
2026-03-20 21:17:49 -04:00
parent 1b32667872
commit 6316ffa1d4
7 changed files with 510 additions and 10 deletions

View File

@@ -23,6 +23,13 @@ from tools.files import file_read as _file_read
from tools.system import claude_allow_dir as _claude_allow_dir
from tools.tasks import task_list as _task_list, task_create as _task_create
from tools.tasks import task_update as _task_update, task_complete as _task_complete
from tools.cron import (
cron_list as _cron_list,
cron_add as _cron_add,
cron_remove as _cron_remove,
cron_toggle as _cron_toggle,
reminders_clear as _reminders_clear,
)
from tools.scratch import (
scratch_read as _scratch_read,
scratch_write as _scratch_write,
@@ -185,6 +192,11 @@ _CALLABLES: dict[str, callable] = {
"task_create": _task_create,
"task_update": _task_update,
"task_complete": _task_complete,
"cron_list": _cron_list,
"cron_add": _cron_add,
"cron_remove": _cron_remove,
"cron_toggle": _cron_toggle,
"reminders_clear": _reminders_clear,
"scratch_read": _scratch_read,
"scratch_write": _scratch_write,
"scratch_append": _scratch_append,
@@ -315,6 +327,98 @@ _task_complete_declaration = types.FunctionDeclaration(
),
)
_cron_list_declaration = types.FunctionDeclaration(
name="cron_list",
description=(
"List all scheduled cron jobs — their ID, label, schedule, type, and last run time. "
"Use this to see what's scheduled before adding or removing jobs."
),
parameters=types.Schema(type=types.Type.OBJECT, properties={}),
)
_cron_add_declaration = types.FunctionDeclaration(
name="cron_add",
description=(
"Create a new scheduled cron job and register it immediately (no restart needed). "
"Two types: 'remind' writes to the pending reminders queue (Inara sees it automatically "
"in context next session); 'note' appends to the scratchpad. "
"Schedule formats: 'hourly' | 'daily' | 'daily:HH:MM' | 'weekly:DOW' | 'weekly:DOW:HH:MM'. "
"Example: schedule='daily:09:00', type='remind', payload='Check in with Scott.'"
),
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"label": types.Schema(
type=types.Type.STRING,
description="Short human-readable name for this job (e.g. 'Morning check-in')",
),
"schedule": types.Schema(
type=types.Type.STRING,
description=(
"When to run. Formats: hourly | daily | daily:HH:MM | "
"weekly:DOW | weekly:DOW:HH:MM (e.g. 'weekly:mon:09:00')"
),
),
"job_type": types.Schema(
type=types.Type.STRING,
description="'remind' (→ REMINDERS.md, auto-surfaced in context) or 'note' (→ SCRATCH.md)",
),
"payload": types.Schema(
type=types.Type.STRING,
description="The text to write when the job fires",
),
},
required=["label", "schedule", "job_type", "payload"],
),
)
_cron_remove_declaration = types.FunctionDeclaration(
name="cron_remove",
description=(
"Permanently delete a scheduled cron job. Use cron_list first to get the ID. "
"To temporarily disable without deleting, use cron_toggle instead."
),
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"cron_id": types.Schema(
type=types.Type.STRING,
description="Job ID (e.g. c_abc123) — get from cron_list",
),
},
required=["cron_id"],
),
)
_cron_toggle_declaration = types.FunctionDeclaration(
name="cron_toggle",
description=(
"Pause a running cron job, or resume a paused one. "
"The job stays in the list and can be re-enabled later. "
"Use cron_list to see current enabled/paused state."
),
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"cron_id": types.Schema(
type=types.Type.STRING,
description="Job ID (e.g. c_abc123) — get from cron_list",
),
},
required=["cron_id"],
),
)
_reminders_clear_declaration = types.FunctionDeclaration(
name="reminders_clear",
description=(
"Erase all pending reminders from REMINDERS.md. "
"Use this after you have acknowledged and acted on the reminders shown in your context."
),
parameters=types.Schema(type=types.Type.OBJECT, properties={}),
)
_scratch_read_declaration = types.FunctionDeclaration(
name="scratch_read",
description=(
@@ -386,6 +490,11 @@ TOOL_DECLARATIONS = [
_task_create_declaration,
_task_update_declaration,
_task_complete_declaration,
_cron_list_declaration,
_cron_add_declaration,
_cron_remove_declaration,
_cron_toggle_declaration,
_reminders_clear_declaration,
_scratch_read_declaration,
_scratch_write_declaration,
_scratch_append_declaration,

186
cortex/tools/cron.py Normal file
View File

@@ -0,0 +1,186 @@
"""
Cron job management tools for Inara.
Jobs are stored in inara/CRONS.json and registered with the live APScheduler
instance so they survive restarts and take effect immediately without a restart.
Tools:
cron_list — show all scheduled jobs
cron_add — create a job and register it immediately
cron_remove — delete a job and unschedule it
cron_toggle — pause or resume a job without deleting it
reminders_clear — erase inara/REMINDERS.md (dismiss all pending reminders)
"""
import asyncio
import secrets
from datetime import datetime, timezone
from pathlib import Path
from config import settings
from cron_runner import load_crons, save_crons, parse_schedule
def _now() -> str:
return datetime.now(timezone.utc).isoformat()
def _short_id() -> str:
return "c_" + secrets.token_urlsafe(6)
# ---------------------------------------------------------------------------
# Sync implementations
# ---------------------------------------------------------------------------
def _cron_list() -> str:
crons = load_crons()
if not crons:
return "No crons scheduled."
lines = [f"Crons ({len(crons)}):\n"]
for c in crons:
status = "enabled" if c.get("enabled", True) else "PAUSED "
last = c.get("last_run")
last_str = last[:10] if last else "never"
lines.append(
f" {c['id']} [{status}] {c['schedule']:<18} "
f"{c['type']:<7} {c['label']} (last: {last_str})"
)
return "\n".join(lines)
def _cron_add(label: str, schedule: str, job_type: str, payload: str) -> str:
# Validate schedule first — raises ValueError with a clear message on bad input
try:
sched_kwargs = parse_schedule(schedule)
except ValueError as e:
return f"Bad schedule: {e}"
if job_type not in ("remind", "note"):
return "Bad type: must be 'remind' or 'note'."
crons = load_crons()
job = {
"id": _short_id(),
"label": label,
"schedule": schedule,
"type": job_type,
"payload": payload,
"enabled": True,
"created_at": _now(),
"last_run": None,
}
crons.append(job)
save_crons(crons)
# Register with the live scheduler
_scheduler_add(job, sched_kwargs)
return f"Created and scheduled: {job['id']} {schedule} [{job_type}] {label}"
def _cron_remove(cron_id: str) -> str:
crons = load_crons()
before = len(crons)
crons = [c for c in crons if c["id"] != cron_id]
if len(crons) == before:
return f"Not found: {cron_id}"
save_crons(crons)
_scheduler_remove(cron_id)
return f"Removed: {cron_id}"
def _cron_toggle(cron_id: str) -> str:
crons = load_crons()
for c in crons:
if c["id"] == cron_id:
c["enabled"] = not c.get("enabled", True)
save_crons(crons)
action = "resumed" if c["enabled"] else "paused"
_scheduler_resume(cron_id) if c["enabled"] else _scheduler_pause(cron_id)
return f"{action.capitalize()}: {cron_id} {c['label']}"
return f"Not found: {cron_id}"
def _reminders_clear() -> str:
p = settings.inara_path() / "REMINDERS.md"
p.write_text("")
return "Reminders cleared."
# ---------------------------------------------------------------------------
# Scheduler bridge — thin wrappers so the tool layer never touches APScheduler
# directly, keeping it swappable
# ---------------------------------------------------------------------------
def _scheduler_add(job: dict, sched_kwargs: dict) -> None:
try:
import scheduler as sched_module
from cron_runner import run_job
s = sched_module.get_scheduler()
if s and s.running:
s.add_job(
lambda j=job: asyncio.ensure_future(run_job(j)),
"cron",
id=job["id"],
replace_existing=True,
**sched_kwargs,
)
except Exception as e:
import logging
logging.getLogger(__name__).warning("scheduler_add failed: %s", e)
def _scheduler_remove(job_id: str) -> None:
try:
import scheduler as sched_module
s = sched_module.get_scheduler()
if s and s.running:
s.remove_job(job_id)
except Exception:
pass
def _scheduler_pause(job_id: str) -> None:
try:
import scheduler as sched_module
s = sched_module.get_scheduler()
if s and s.running:
s.pause_job(job_id)
except Exception:
pass
def _scheduler_resume(job_id: str) -> None:
try:
import scheduler as sched_module
s = sched_module.get_scheduler()
if s and s.running:
s.resume_job(job_id)
except Exception:
pass
# ---------------------------------------------------------------------------
# Async wrappers
# ---------------------------------------------------------------------------
async def cron_list() -> str:
return await asyncio.to_thread(_cron_list)
async def cron_add(label: str, schedule: str, job_type: str, payload: str) -> str:
return await asyncio.to_thread(_cron_add, label, schedule, job_type, payload)
async def cron_remove(cron_id: str) -> str:
return await asyncio.to_thread(_cron_remove, cron_id)
async def cron_toggle(cron_id: str) -> str:
return await asyncio.to_thread(_cron_toggle, cron_id)
async def reminders_clear() -> str:
return await asyncio.to_thread(_reminders_clear)