|
1 | | -"""Foreman CLI entrypoint.""" |
| 1 | +"""Foreman CLI entrypoint. |
2 | 2 |
|
| 3 | +Usage:: |
3 | 4 |
|
4 | | -def main() -> None: |
5 | | - """Run the Foreman harness.""" |
6 | | - raise NotImplementedError("Entrypoint not yet implemented — see Task 12.") |
| 5 | + foreman start --config config.yaml |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import asyncio |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | +from typing import TYPE_CHECKING, Any |
| 15 | + |
| 16 | +import structlog |
| 17 | +import uvicorn |
| 18 | + |
| 19 | +from foreman.config import ConfigError, load_config |
| 20 | +from foreman.memory import MemoryStore |
| 21 | +from foreman.poller import GitHubPoller |
| 22 | +from foreman.routers import Router, RoutingError |
| 23 | +from foreman.server import Dispatcher, app |
| 24 | + |
| 25 | +if TYPE_CHECKING: |
| 26 | + from foreman.config import ForemanConfig, RepoConfig |
| 27 | + |
| 28 | +logger = structlog.get_logger(__name__) |
| 29 | + |
| 30 | +#: Default memory DB path. |
| 31 | +_DEFAULT_DB_PATH = Path.home() / ".agent-harness" / "memory.db" |
| 32 | + |
| 33 | + |
| 34 | +def _build_parser() -> argparse.ArgumentParser: |
| 35 | + """Build the argument parser for the Foreman CLI. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + Configured :class:`argparse.ArgumentParser`. |
| 39 | + """ |
| 40 | + parser = argparse.ArgumentParser( |
| 41 | + prog="foreman", |
| 42 | + description="Foreman — AI OSS co-maintainer harness", |
| 43 | + ) |
| 44 | + subparsers = parser.add_subparsers(dest="command") |
| 45 | + |
| 46 | + start = subparsers.add_parser("start", help="Start the Foreman harness") |
| 47 | + start.add_argument( |
| 48 | + "--config", |
| 49 | + required=True, |
| 50 | + metavar="CONFIG", |
| 51 | + help="Path to the YAML configuration file", |
| 52 | + ) |
| 53 | + start.add_argument( |
| 54 | + "--db", |
| 55 | + default=str(_DEFAULT_DB_PATH), |
| 56 | + metavar="DB_PATH", |
| 57 | + help="Path to the SQLite memory database (default: ~/.agent-harness/memory.db)", |
| 58 | + ) |
| 59 | + start.add_argument( |
| 60 | + "--host", |
| 61 | + default="0.0.0.0", |
| 62 | + metavar="HOST", |
| 63 | + help="Host to bind the HTTP server to (default: 0.0.0.0)", |
| 64 | + ) |
| 65 | + start.add_argument( |
| 66 | + "--port", |
| 67 | + type=int, |
| 68 | + default=8000, |
| 69 | + metavar="PORT", |
| 70 | + help="Port for the HTTP server (default: 8000)", |
| 71 | + ) |
| 72 | + return parser |
| 73 | + |
| 74 | + |
| 75 | +def main(argv: list[str] | None = None) -> None: |
| 76 | + """Parse CLI arguments and run Foreman. |
| 77 | +
|
| 78 | + Args: |
| 79 | + argv: Argument list (defaults to ``sys.argv[1:]`` when ``None``). |
| 80 | +
|
| 81 | + Raises: |
| 82 | + SystemExit: On invalid arguments or configuration errors. |
| 83 | + """ |
| 84 | + parser = _build_parser() |
| 85 | + args = parser.parse_args(argv) |
| 86 | + |
| 87 | + if args.command is None: |
| 88 | + parser.print_help(sys.stderr) |
| 89 | + sys.exit(2) |
| 90 | + |
| 91 | + if args.command == "start": |
| 92 | + _run_start(args) |
| 93 | + |
| 94 | + |
| 95 | +def _run_start(args: Any) -> None: |
| 96 | + """Execute the ``start`` sub-command. |
| 97 | +
|
| 98 | + Validates config, initialises the memory DB, then runs the poller and |
| 99 | + HTTP server concurrently inside a single asyncio event loop. |
| 100 | +
|
| 101 | + Args: |
| 102 | + args: Parsed namespace from argparse. |
| 103 | +
|
| 104 | + Raises: |
| 105 | + SystemExit: On config validation failure. |
| 106 | + """ |
| 107 | + # 1. Load and validate config — fail fast with a clear message. |
| 108 | + try: |
| 109 | + config = load_config(args.config) |
| 110 | + except ConfigError as exc: |
| 111 | + print(f"Error: {exc}", file=sys.stderr) |
| 112 | + sys.exit(1) |
| 113 | + |
| 114 | + # 2. Initialise memory DB. |
| 115 | + db_path = Path(args.db) |
| 116 | + memory = MemoryStore(db_path) |
| 117 | + |
| 118 | + # 3. Create core components. |
| 119 | + poller = GitHubPoller(token=str(config.identity.github_token), memory=memory) |
| 120 | + dispatcher = Dispatcher(config=config, memory=memory) |
| 121 | + |
| 122 | + logger.info( |
| 123 | + "Foreman initialised", |
| 124 | + config=args.config, |
| 125 | + db=str(db_path), |
| 126 | + repos=[f"{r.owner}/{r.name}" for r in config.repos], |
| 127 | + poll_interval_seconds=config.polling.interval_seconds, |
| 128 | + ) |
| 129 | + |
| 130 | + # 4. Run the poller and HTTP server concurrently. |
| 131 | + asyncio.run(_run_loop(config, memory, poller, dispatcher, args.host, args.port)) |
| 132 | + |
| 133 | + |
| 134 | +async def _run_loop( |
| 135 | + config: ForemanConfig, |
| 136 | + memory: MemoryStore, |
| 137 | + poller: GitHubPoller, |
| 138 | + dispatcher: Dispatcher, |
| 139 | + host: str, |
| 140 | + port: int, |
| 141 | +) -> None: |
| 142 | + """Run the poll loop and HTTP server concurrently. |
| 143 | +
|
| 144 | + The poller is started as an asyncio task alongside the uvicorn server. |
| 145 | + On shutdown (SIGINT/SIGTERM), the poller task is cancelled cleanly. |
| 146 | +
|
| 147 | + Args: |
| 148 | + config: Validated runtime configuration. |
| 149 | + memory: Open memory store (passed through for context). |
| 150 | + poller: Initialised :class:`~foreman.poller.GitHubPoller`. |
| 151 | + dispatcher: Initialised :class:`~foreman.server.Dispatcher`. |
| 152 | + host: Bind address for the HTTP server. |
| 153 | + port: Port for the HTTP server. |
| 154 | + """ |
| 155 | + router = Router(config) |
| 156 | + |
| 157 | + async def on_event(repo_config: RepoConfig, event: dict[str, Any]) -> None: |
| 158 | + """Handle one poller event: route it and dispatch to the appropriate agent. |
| 159 | +
|
| 160 | + Args: |
| 161 | + repo_config: The repo configuration that produced this event. |
| 162 | + event: Event dict with ``repo``, ``issue_number``, and ``payload``. |
| 163 | + """ |
| 164 | + repo = event["repo"] |
| 165 | + issue_number = event["issue_number"] |
| 166 | + logger.info("Issue event", repo=repo, issue_number=issue_number) |
| 167 | + try: |
| 168 | + route_target = router.route("issue.triage", repo) |
| 169 | + except RoutingError: |
| 170 | + logger.warning("Repo not in config — skipping", repo=repo) |
| 171 | + return |
| 172 | + if route_target is None: |
| 173 | + logger.debug("No agent handles issue.triage for this repo", repo=repo) |
| 174 | + return |
| 175 | + logger.info( |
| 176 | + "Routing to agent", |
| 177 | + repo=repo, |
| 178 | + issue_number=issue_number, |
| 179 | + agent_url=route_target.url, |
| 180 | + ) |
| 181 | + await dispatcher.dispatch(event, route_target) |
| 182 | + |
| 183 | + uv_server = uvicorn.Server(uvicorn.Config(app, host=host, port=port, log_config=None)) |
| 184 | + |
| 185 | + logger.info( |
| 186 | + "Foreman started — polling every %d seconds, server on %s:%d", |
| 187 | + config.polling.interval_seconds, |
| 188 | + host, |
| 189 | + port, |
| 190 | + ) |
| 191 | + |
| 192 | + poller_task = asyncio.create_task(poller.run(config.repos, config.polling.interval_seconds, on_event)) |
| 193 | + |
| 194 | + def _on_poller_done(task: asyncio.Task) -> None: |
| 195 | + """Log unexpected poller task termination.""" |
| 196 | + if not task.cancelled(): |
| 197 | + exc = task.exception() |
| 198 | + if exc is not None: |
| 199 | + logger.critical("Poller task crashed unexpectedly", exc_info=exc) |
| 200 | + |
| 201 | + poller_task.add_done_callback(_on_poller_done) |
| 202 | + |
| 203 | + try: |
| 204 | + await uv_server.serve() |
| 205 | + finally: |
| 206 | + poller_task.cancel() |
| 207 | + try: |
| 208 | + await poller_task |
| 209 | + except asyncio.CancelledError: |
| 210 | + logger.info("Poller stopped cleanly") |
7 | 211 |
|
8 | 212 |
|
9 | 213 | if __name__ == "__main__": |
|
0 commit comments