feat: scheduled agent execution via activity-core (WP-0006, v1.3.0)
Enable kaizen agents to run on a regular cadence against a preselected repo roster, orchestrated by activity-core and prepared by kaizen-agentic — without this repo owning cron, Temporal workers, or an LLM runtime. CLI + module: - src/kaizen_agentic/schedule.py — .kaizen/schedule.yml parse/validate/scaffold - `kaizen-agentic schedule` group: init, validate, list, prepare <agent> (prepare bundles agent prompt + memory + metrics + repo pointers, offline) - tests/test_schedule_cli.py — 15 tests Contract & design: - ADR-005 scheduled agent execution; schema doc + example manifest - discover_kaizen_scheduled_repos resolver spec, state-hub roster fields, kaizen.schedule.prepared event payload, activity-core handoff checklist - INTEGRATION_PATTERNS Pattern 2 extended with roster model ActivityDefinition drafts (enabled: false): - weekly-coach-orientation, weekly-optimization-review Docs: agency-framework, CLI cheat sheet, PACKAGE_RELEASE runner prereqs, EcosystemIntegration, CHANGELOG, TODO. Workplan closed (status: done). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,13 @@ from .integrations.artifact_store import (
|
||||
from .integrations.helix import HelixCorrelationAdapter, enrich_helix_correlation
|
||||
from .metrics import MetricsStore, OptimizerStore, performance_summary_markdown
|
||||
from .optimization import OptimizationLoop, MIN_SAMPLES_FOR_RECOMMENDATIONS
|
||||
from .schedule import (
|
||||
ScheduleError,
|
||||
default_schedule_yaml,
|
||||
load_schedule,
|
||||
schedule_path,
|
||||
validate_schedule,
|
||||
)
|
||||
|
||||
|
||||
def safe_cli_wrapper():
|
||||
@@ -1360,6 +1367,204 @@ def protocols_show(agent_name: str, slug: str):
|
||||
click.echo(protocol_path.read_text())
|
||||
|
||||
|
||||
@cli.group()
|
||||
def schedule():
|
||||
"""Prepare and validate scheduled agent runs (.kaizen/schedule.yml, ADR-005).
|
||||
|
||||
kaizen-agentic does not run cron schedules or invoke Claude. activity-core
|
||||
fires the cron and creates a task per (repo, agent); a coding-agent session
|
||||
runs `schedule prepare <agent>` to assemble orientation, then executes the
|
||||
agent instructions.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
@schedule.command("validate")
|
||||
@click.option("--target", "-t", default=".", help="Project root (default: current)")
|
||||
def schedule_validate(target: str):
|
||||
"""Validate .kaizen/schedule.yml against the ADR-005 schema."""
|
||||
path = schedule_path(_project_root(target))
|
||||
|
||||
try:
|
||||
parsed = load_schedule(path)
|
||||
except ScheduleError as exc:
|
||||
click.echo(f"❌ {exc}", err=True)
|
||||
click.echo(" Run: kaizen-agentic schedule init", err=True)
|
||||
sys.exit(1)
|
||||
|
||||
known_agents = _get_registry().agent_names()
|
||||
errors = validate_schedule(parsed, known_agents=known_agents)
|
||||
|
||||
if errors:
|
||||
click.echo(f"❌ Schedule validation failed ({path}):")
|
||||
for error in errors:
|
||||
click.echo(f" • {error}")
|
||||
sys.exit(1)
|
||||
|
||||
enabled = parsed.enabled_entries()
|
||||
click.echo(f"✅ Schedule valid: {path}")
|
||||
click.echo(f" {len(parsed.entries)} agent(s), {len(enabled)} enabled")
|
||||
|
||||
|
||||
@schedule.command("init")
|
||||
@click.option("--target", "-t", default=".", help="Project root (default: current)")
|
||||
@click.option(
|
||||
"--timezone", default="Europe/Berlin", show_default=True, help="Schedule timezone"
|
||||
)
|
||||
@click.option("--force", is_flag=True, help="Overwrite an existing schedule.yml")
|
||||
def schedule_init(target: str, timezone: str, force: bool):
|
||||
"""Scaffold a default .kaizen/schedule.yml (coach + optimization weekly)."""
|
||||
path = schedule_path(_project_root(target))
|
||||
|
||||
if path.exists() and not force:
|
||||
click.echo(f"Schedule already exists: {path}")
|
||||
click.echo(" Use --force to overwrite.")
|
||||
return
|
||||
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(default_schedule_yaml(timezone=timezone), encoding="utf-8")
|
||||
click.echo(f"Initialized schedule: {path}")
|
||||
click.echo(" Validate with: kaizen-agentic schedule validate")
|
||||
|
||||
|
||||
@schedule.command("list")
|
||||
@click.option("--target", "-t", default=".", help="Project root (default: current)")
|
||||
@click.option("--all", "show_all", is_flag=True, help="Include disabled entries")
|
||||
def schedule_list(target: str, show_all: bool):
|
||||
"""Show enabled schedule entries from .kaizen/schedule.yml."""
|
||||
path = schedule_path(_project_root(target))
|
||||
|
||||
try:
|
||||
parsed = load_schedule(path)
|
||||
except ScheduleError as exc:
|
||||
click.echo(f"No schedule found: {exc}")
|
||||
click.echo(" Run: kaizen-agentic schedule init")
|
||||
return
|
||||
|
||||
entries = parsed.entries if show_all else parsed.enabled_entries()
|
||||
if not entries:
|
||||
click.echo("No enabled schedule entries (use --all to see disabled).")
|
||||
return
|
||||
|
||||
click.echo(f"Scheduled agents ({path}):")
|
||||
if parsed.timezone:
|
||||
click.echo(f" Timezone: {parsed.timezone}")
|
||||
for entry in entries:
|
||||
flag = "✅" if entry.enabled else "⏸ "
|
||||
cron = f" cron={entry.cron}" if entry.cron else ""
|
||||
click.echo(f" {flag} {entry.agent}: {entry.cadence}{cron}")
|
||||
|
||||
|
||||
@schedule.command("prepare")
|
||||
@click.argument("agent_name")
|
||||
@click.option("--target", "-t", default=".", help="Project root (default: current)")
|
||||
@click.option(
|
||||
"--format",
|
||||
"output_format",
|
||||
type=click.Choice(["markdown", "json"]),
|
||||
default="markdown",
|
||||
show_default=True,
|
||||
help="Output format for the orientation bundle",
|
||||
)
|
||||
def schedule_prepare(agent_name: str, target: str, output_format: str):
|
||||
"""Assemble an orientation bundle for a scheduled agent run.
|
||||
|
||||
Bundles the agent prompt, project memory, metrics summary, and repo
|
||||
pointers into a single payload. Works offline from local `.kaizen/` state;
|
||||
no State Hub required. Pass the output to a coding-agent session.
|
||||
"""
|
||||
bundle = _build_prepare_bundle(agent_name, _project_root(target))
|
||||
|
||||
if output_format == "json":
|
||||
click.echo(json.dumps(bundle, indent=2))
|
||||
return
|
||||
|
||||
click.echo(_render_prepare_markdown(bundle))
|
||||
|
||||
|
||||
def _build_prepare_bundle(agent_name: str, project_root: Path) -> dict:
|
||||
"""Collect the orientation bundle pieces for `schedule prepare`."""
|
||||
registry = _get_registry()
|
||||
agent_path = registry.get_agent_path(agent_name)
|
||||
agent_prompt = agent_path.read_text(encoding="utf-8") if agent_path else None
|
||||
|
||||
memory_path = project_root / ".kaizen" / "agents" / agent_name / "memory.md"
|
||||
memory = memory_path.read_text(encoding="utf-8") if memory_path.exists() else None
|
||||
|
||||
metrics_store = MetricsStore(project_root, agent_name)
|
||||
metrics_summary = metrics_store.read_summary()
|
||||
if metrics_summary is None and metrics_store.executions_path.exists():
|
||||
metrics_summary = metrics_store.write_summary()
|
||||
|
||||
pointers = {}
|
||||
for label, filename in (("scope", "SCOPE.md"), ("todo", "TODO.md")):
|
||||
candidate = project_root / filename
|
||||
if candidate.exists():
|
||||
pointers[label] = str(candidate)
|
||||
|
||||
return {
|
||||
"agent": agent_name,
|
||||
"project": project_root.name,
|
||||
"generated": _today(),
|
||||
"agent_prompt": agent_prompt,
|
||||
"agent_prompt_found": agent_prompt is not None,
|
||||
"memory": memory,
|
||||
"metrics_summary": metrics_summary,
|
||||
"pointers": pointers,
|
||||
"session_close": [
|
||||
f"kaizen-agentic metrics record {agent_name} --success "
|
||||
f"--time <seconds> --quality <0-1>",
|
||||
f"Update memory: kaizen-agentic memory show {agent_name}",
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _render_prepare_markdown(bundle: dict) -> str:
|
||||
agent = bundle["agent"]
|
||||
lines = [
|
||||
f"# Scheduled Run Orientation: {agent}",
|
||||
f"Project: {bundle['project']}",
|
||||
f"Generated: {bundle['generated']}",
|
||||
"",
|
||||
]
|
||||
|
||||
summary = bundle.get("metrics_summary")
|
||||
block = performance_summary_markdown(summary or {})
|
||||
if block:
|
||||
lines.append(block)
|
||||
|
||||
lines.append("## Agent Prompt")
|
||||
if bundle["agent_prompt_found"]:
|
||||
lines.append(bundle["agent_prompt"])
|
||||
else:
|
||||
lines.append(
|
||||
f"(agent '{agent}' not found in registry — " f"run: kaizen-agentic list)"
|
||||
)
|
||||
lines.append("")
|
||||
|
||||
lines.append("## Project Memory")
|
||||
if bundle.get("memory"):
|
||||
lines.append(bundle["memory"])
|
||||
else:
|
||||
lines.append(f"(none — run: kaizen-agentic memory init {agent})")
|
||||
lines.append("")
|
||||
|
||||
pointers = bundle.get("pointers") or {}
|
||||
lines.append("## Repo Pointers")
|
||||
if pointers:
|
||||
for label, path in pointers.items():
|
||||
lines.append(f"- {label}: {path}")
|
||||
else:
|
||||
lines.append("- (no SCOPE.md / TODO.md found)")
|
||||
lines.append("")
|
||||
|
||||
lines.append("## Session Close")
|
||||
for cmd in bundle["session_close"]:
|
||||
lines.append(f"- `{cmd}`")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _project_root(target: str) -> Path:
|
||||
return Path(target).resolve()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user