|
| 1 | +"""Scheduled headless task runner for MiniCode. |
| 2 | +
|
| 3 | +Config format: |
| 4 | +
|
| 5 | +{ |
| 6 | + "tasks": [ |
| 7 | + {"name": "daily-check", "prompt": "Summarize the repository status"} |
| 8 | + ] |
| 9 | +} |
| 10 | +
|
| 11 | +Without a config file, the runner exits cleanly with an explanatory message. |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import argparse |
| 17 | +import json |
| 18 | +import os |
| 19 | +import time |
| 20 | +from pathlib import Path |
| 21 | +from typing import Any |
| 22 | + |
| 23 | + |
| 24 | +def _default_config_path() -> Path: |
| 25 | + return Path(os.environ.get("MINI_CODE_CRON_CONFIG", ".mini-code/cron.json")) |
| 26 | + |
| 27 | + |
| 28 | +def load_cron_config(path: str | Path | None = None) -> dict[str, Any]: |
| 29 | + config_path = Path(path) if path is not None else _default_config_path() |
| 30 | + if not config_path.exists(): |
| 31 | + return {"tasks": []} |
| 32 | + data = json.loads(config_path.read_text(encoding="utf-8")) |
| 33 | + if not isinstance(data, dict): |
| 34 | + raise ValueError("cron config must be a JSON object") |
| 35 | + tasks = data.get("tasks", []) |
| 36 | + if not isinstance(tasks, list): |
| 37 | + raise ValueError("cron config field 'tasks' must be a list") |
| 38 | + return {"tasks": tasks} |
| 39 | + |
| 40 | + |
| 41 | +def run_configured_tasks(config: dict[str, Any], *, dry_run: bool = False) -> list[dict[str, Any]]: |
| 42 | + from minicode.headless import run_headless |
| 43 | + |
| 44 | + results: list[dict[str, Any]] = [] |
| 45 | + for index, task in enumerate(config.get("tasks", [])): |
| 46 | + if not isinstance(task, dict): |
| 47 | + results.append({"index": index, "ok": False, "error": "task must be an object"}) |
| 48 | + continue |
| 49 | + prompt = str(task.get("prompt", "")).strip() |
| 50 | + name = str(task.get("name") or f"task-{index + 1}") |
| 51 | + if not prompt: |
| 52 | + results.append({"name": name, "ok": False, "error": "prompt is required"}) |
| 53 | + continue |
| 54 | + if dry_run: |
| 55 | + results.append({"name": name, "ok": True, "dryRun": True}) |
| 56 | + continue |
| 57 | + results.append({"name": name, "ok": True, "response": run_headless(prompt)}) |
| 58 | + return results |
| 59 | + |
| 60 | + |
| 61 | +def main(argv: list[str] | None = None) -> None: |
| 62 | + parser = argparse.ArgumentParser(description="Run MiniCode scheduled headless tasks.") |
| 63 | + parser.add_argument("--config", default=None, help="Path to cron JSON config.") |
| 64 | + parser.add_argument("--once", action="store_true", help="Run tasks once and exit.") |
| 65 | + parser.add_argument("--dry-run", action="store_true", help="Validate tasks without executing prompts.") |
| 66 | + parser.add_argument("--interval", type=float, default=60.0, help="Polling interval in seconds.") |
| 67 | + args = parser.parse_args(argv) |
| 68 | + |
| 69 | + while True: |
| 70 | + config = load_cron_config(args.config) |
| 71 | + if not config["tasks"]: |
| 72 | + print(f"No cron tasks configured in {args.config or _default_config_path()}.", flush=True) |
| 73 | + else: |
| 74 | + for result in run_configured_tasks(config, dry_run=args.dry_run): |
| 75 | + print(json.dumps(result, ensure_ascii=False), flush=True) |
| 76 | + if args.once or not config["tasks"]: |
| 77 | + return |
| 78 | + time.sleep(max(args.interval, 1.0)) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + main() |
0 commit comments