Spaces:
Running
Running
Download core/cli.py from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 5.74 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/cli.py
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/core/cli.py
-
curl -L -o cli.py https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/cli.py
5.74 kB
| """Ops commands. | |
| python -m core.cli sync-tools # index pack tools for tool search (owner role) | |
| python -m core.cli expire-approvals # mark overdue approvals expired (owner role) | |
| python -m core.cli check-deploy # refuse a deploy that would strand waiting runs | |
| Phase 18 (docs/deployment.md): | |
| python -m core.cli seed-demo [--reset] # public demo tenant (app + owner roles) | |
| python -m core.cli purge-retention [--tenant ID] [--dry-run] # audit/messages (owner role) | |
| python -m core.cli verify-audit --tenant ID # hash chain, retention-anchor aware | |
| python -m core.cli check-config # refuse dev defaults in production | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import asyncio | |
| import sys | |
| from typing import Any | |
| from sqlalchemy import text | |
| from core.config import get_settings | |
| from core.db.session import Database | |
| from core.packs.loader import discover_packs | |
| async def sync_tools() -> int: | |
| from core.tools.search import ToolSearch | |
| db = Database(get_settings().owner_database_url) | |
| specs = [t.spec for p in discover_packs() for t in p.tools()] | |
| async with db.session() as s, s.begin(): | |
| n = await ToolSearch().sync_global(s, specs) | |
| await db.dispose() | |
| print(f"indexed {n} tools") | |
| return 0 | |
| async def expire_approvals() -> int: | |
| from core.approvals.service import ApprovalService | |
| db = Database(get_settings().owner_database_url) | |
| async with db.session() as s, s.begin(): | |
| expired = await ApprovalService().expire_pending(s) | |
| await db.dispose() | |
| print(f"expired {len(expired)} approvals") | |
| return 0 | |
| async def check_deploy() -> int: | |
| from core.agent.runtime import CORE_NODES, incompatible_waiting_runs | |
| nodes = sorted([*CORE_NODES, *(i.name for p in discover_packs() for i in p.intents())]) | |
| db = Database(get_settings().owner_database_url) | |
| async with db.session() as s: | |
| # Phase 18: the release step runs this BEFORE migrating; a fresh database has no runs. | |
| if (await s.execute(text("SELECT to_regclass('runs')"))).scalar() is None: | |
| await db.dispose() | |
| print("fresh database: no waiting runs to check") | |
| return 0 | |
| bad = await incompatible_waiting_runs(s, nodes) | |
| await db.dispose() | |
| if bad: | |
| print(f"refusing deploy: {len(bad)} waiting runs need removed nodes: {bad[:10]}") | |
| return 1 | |
| print("deploy is compatible with waiting runs") | |
| return 0 | |
| async def seed_demo(reset: bool = False) -> int: | |
| import json | |
| from core.ops.demo import seed_demo_tenant | |
| s = get_settings() | |
| db, owner_db = Database(s.database_url), Database(s.owner_database_url) | |
| try: | |
| res = await seed_demo_tenant(db, owner_db, discover_packs(), s, reset=reset) | |
| finally: | |
| await db.dispose() | |
| await owner_db.dispose() | |
| print(json.dumps(res.as_dict())) | |
| return 1 if res.missing_seeders else 0 | |
| async def purge_retention(tenants: list[str], dry_run: bool = False) -> int: | |
| import json | |
| import uuid | |
| from core.ops.retention import purge_all | |
| s = get_settings() | |
| owner_db = Database(s.owner_database_url) | |
| try: | |
| ids = [uuid.UUID(t) for t in tenants] if tenants else None | |
| out = await purge_all(owner_db, s, ids, dry_run=dry_run) | |
| finally: | |
| await owner_db.dispose() | |
| for r in out: | |
| print(json.dumps({**r.as_dict(), "dry_run": dry_run})) | |
| return 0 | |
| async def verify_audit(tenants: list[str]) -> int: | |
| import uuid | |
| from core.ops.retention import verify_chain_anchored | |
| if not tenants: | |
| print("verify-audit needs --tenant") | |
| return 2 | |
| owner_db = Database(get_settings().owner_database_url) | |
| bad = 0 | |
| try: | |
| for t in tenants: | |
| tid = uuid.UUID(t) | |
| async with owner_db.session(tenant_id=tid) as s: | |
| rep = await verify_chain_anchored(s, tid) | |
| print(f"{tid}: ok={rep.ok} checked={rep.checked} broken_at_seq={rep.broken_at_seq}") | |
| bad += not rep.ok | |
| finally: | |
| await owner_db.dispose() | |
| return 1 if bad else 0 | |
| async def check_config() -> int: | |
| from core.ops.preflight import production_problems | |
| problems = production_problems(get_settings()) | |
| for p in problems: | |
| print(f"config problem: {p}") | |
| if not problems: | |
| print("configuration ok") | |
| return 1 if problems else 0 | |
| COMMANDS = [ | |
| "sync-tools", | |
| "expire-approvals", | |
| "check-deploy", | |
| "seed-demo", | |
| "purge-retention", | |
| "verify-audit", | |
| "check-config", | |
| ] | |
| def main(argv: list[str] | None = None) -> None: | |
| if sys.platform == "win32": | |
| asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) | |
| p = argparse.ArgumentParser(prog="workpilot") | |
| p.add_argument("command", choices=COMMANDS) | |
| p.add_argument("--reset", action="store_true", help="seed-demo: wipe and re-seed") | |
| p.add_argument("--tenant", action="append", default=[], help="tenant id (repeatable)") | |
| p.add_argument("--dry-run", action="store_true", help="purge-retention: roll back") | |
| a = p.parse_args(argv) | |
| fn: dict[str, Any] = { | |
| "sync-tools": sync_tools, | |
| "expire-approvals": expire_approvals, | |
| "check-deploy": check_deploy, | |
| "seed-demo": lambda: seed_demo(a.reset), | |
| "purge-retention": lambda: purge_retention(a.tenant, a.dry_run), | |
| "verify-audit": lambda: verify_audit(a.tenant), | |
| "check-config": check_config, | |
| } | |
| raise SystemExit(asyncio.run(fn[a.command]())) | |
| if __name__ == "__main__": | |
| main() | |