Repository navigation
feat: add Kanban runtime preflight guardrails #28828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| """Executable runtime preflight checks for Hermes operational scopes.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json as jsonlib | ||
| import os | ||
| from dataclasses import asdict, dataclass | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from hermes_cli.config import get_hermes_home, load_config | ||
|
|
||
|
|
||
| @dataclass | ||
| class PreflightCheck: | ||
| name: str | ||
| ok: bool | ||
| detail: str = "" | ||
|
|
||
|
|
||
| def _configured_model(config: dict[str, Any]) -> str: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also consider |
||
| model_cfg = config.get("model") | ||
| if isinstance(model_cfg, dict): | ||
| return str(model_cfg.get("default") or model_cfg.get("name") or "").strip() | ||
| return str(model_cfg or "").strip() | ||
|
|
||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Root-level |
||
| def _configured_provider(config: dict[str, Any]) -> str: | ||
| provider = str(config.get("provider") or "").strip() | ||
| if provider: | ||
| return provider | ||
| model_cfg = config.get("model") | ||
| if isinstance(model_cfg, dict): | ||
| return str(model_cfg.get("provider") or "").strip() | ||
| return "" | ||
|
|
||
|
|
||
| def _can_write_probe(directory: Path, label: str) -> PreflightCheck: | ||
| try: | ||
| directory.mkdir(parents=True, exist_ok=True) | ||
| probe = directory / ".hermes-preflight-write-test" | ||
| probe.write_text("ok\n", encoding="utf-8") | ||
| probe.unlink() | ||
| return PreflightCheck(label, True, str(directory)) | ||
| except Exception as exc: | ||
| return PreflightCheck(label, False, f"{directory}: {exc}") | ||
|
|
||
|
|
||
| def _guardrails_enabled(config: dict[str, Any]) -> bool: | ||
| kanban_cfg = config.get("kanban") if isinstance(config, dict) else None | ||
| guard_cfg = kanban_cfg.get("runtime_guardrails", {}) if isinstance(kanban_cfg, dict) else {} | ||
| return guard_cfg.get("enabled", True) is not False | ||
|
|
||
|
|
||
| def collect_kanban_preflight() -> list[PreflightCheck]: | ||
| """Collect executable checks needed before Kanban worker autonomy.""" | ||
| checks: list[PreflightCheck] = [] | ||
|
|
||
| try: | ||
| config = load_config() or {} | ||
| checks.append(PreflightCheck("config load", True, "config.yaml loaded")) | ||
| except Exception as exc: | ||
| config = {} | ||
| checks.append(PreflightCheck("config load", False, str(exc))) | ||
|
|
||
| provider = _configured_provider(config) | ||
| model = _configured_model(config) | ||
| checks.append( | ||
| PreflightCheck( | ||
| "provider/model", | ||
| bool(provider and model), | ||
| f"provider={provider or '(missing)'} model={model or '(missing)'}", | ||
| ) | ||
| ) | ||
|
|
||
| checks.append( | ||
| PreflightCheck( | ||
| "kanban runtime guardrails", | ||
| _guardrails_enabled(config), | ||
| "kanban.runtime_guardrails.enabled must not be false", | ||
| ) | ||
| ) | ||
|
|
||
| home = Path(os.environ.get("HERMES_HOME") or get_hermes_home()).expanduser() | ||
| checks.append(_can_write_probe(home, "HERMES_HOME writable")) | ||
|
|
||
| try: | ||
| from hermes_cli import kanban_db as kb | ||
|
|
||
| board = kb.get_current_board() | ||
| db_path = kb.kanban_db_path(board) | ||
| checks.append(PreflightCheck("kanban board", True, board)) | ||
| checks.append(_can_write_probe(db_path.parent, "kanban db writable")) | ||
| checks.append(_can_write_probe(kb.workspaces_root(board), "kanban workspaces writable")) | ||
| except Exception as exc: | ||
| checks.append(PreflightCheck("kanban paths", False, str(exc))) | ||
|
|
||
| return checks | ||
|
|
||
|
|
||
| def _print_human(scope: str, checks: list[PreflightCheck]) -> None: | ||
| print(f"Runtime preflight: {scope}") | ||
| for check in checks: | ||
| status = "PASS" if check.ok else "FAIL" | ||
| suffix = f" — {check.detail}" if check.detail else "" | ||
| print(f"[{status}] {check.name}{suffix}") | ||
| print("PASS" if all(check.ok for check in checks) else "FAIL") | ||
|
|
||
|
|
||
| def run_runtime_preflight(args) -> int: | ||
| scope = getattr(args, "scope", "kanban") or "kanban" | ||
| if scope != "kanban": | ||
| checks = [PreflightCheck("scope", False, f"unsupported scope: {scope}")] | ||
| else: | ||
| checks = collect_kanban_preflight() | ||
|
|
||
| if getattr(args, "json", False): | ||
| print(jsonlib.dumps({"scope": scope, "ok": all(c.ok for c in checks), "checks": [asdict(c) for c in checks]}, indent=2)) | ||
| else: | ||
| _print_human(scope, checks) | ||
| return 0 if all(check.ok for check in checks) else 1 | ||
|
|
||
|
|
||
| def runtime_command(args) -> None: | ||
| import sys | ||
|
|
||
| action = getattr(args, "runtime_action", None) | ||
| if action == "preflight": | ||
| raise SystemExit(run_runtime_preflight(args)) | ||
| print("Unknown runtime subcommand") | ||
| raise SystemExit(1) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| from types import SimpleNamespace | ||
|
|
||
|
|
||
| def test_runtime_preflight_kanban_passes_with_writable_home(monkeypatch, capsys, tmp_path): | ||
| from hermes_cli import runtime_preflight as mod | ||
|
|
||
| monkeypatch.setenv("HERMES_HOME", str(tmp_path)) | ||
| monkeypatch.setenv("HERMES_KANBAN_HOME", str(tmp_path)) | ||
| monkeypatch.setenv("HERMES_KANBAN_DB", str(tmp_path / "kanban.db")) | ||
| monkeypatch.setattr( | ||
| mod, | ||
| "load_config", | ||
| lambda: { | ||
| "model": "gpt-5.5", | ||
| "provider": "openai-codex", | ||
| "kanban": {"runtime_guardrails": {"enabled": True}}, | ||
| }, | ||
| ) | ||
|
|
||
| rc = mod.run_runtime_preflight(SimpleNamespace(scope="kanban", json=False)) | ||
|
|
||
| out = capsys.readouterr().out | ||
| assert rc == 0 | ||
| assert "Runtime preflight: kanban" in out | ||
| assert "provider/model" in out | ||
| assert "kanban db writable" in out | ||
| assert "PASS" in out | ||
|
|
||
|
|
||
| def test_runtime_preflight_kanban_fails_when_guardrails_disabled(monkeypatch, capsys, tmp_path): | ||
| from hermes_cli import runtime_preflight as mod | ||
|
|
||
| monkeypatch.setenv("HERMES_HOME", str(tmp_path)) | ||
| monkeypatch.setenv("HERMES_KANBAN_HOME", str(tmp_path)) | ||
| monkeypatch.setenv("HERMES_KANBAN_DB", str(tmp_path / "kanban.db")) | ||
| monkeypatch.setattr( | ||
| mod, | ||
| "load_config", | ||
| lambda: { | ||
| "model": "gpt-5.5", | ||
| "provider": "openai-codex", | ||
| "kanban": {"runtime_guardrails": {"enabled": False}}, | ||
| }, | ||
| ) | ||
|
|
||
| rc = mod.run_runtime_preflight(SimpleNamespace(scope="kanban", json=False)) | ||
|
|
||
| out = capsys.readouterr().out | ||
| assert rc == 1 | ||
| assert "kanban runtime guardrails" in out | ||
| assert "FAIL" in out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this env var is meant to prove the worker was launched through the dispatcher,
collect_kanban_preflight()should read and validate it; otherwise the contract is only informational despite the guardrail wording.