|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import asyncio |
| 5 | +import json |
| 6 | +import sys |
| 7 | +from dataclasses import asdict, dataclass |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +from maf_starter.loop_workers import ( |
| 11 | + REQUIRED_SPECIALIST_ROLES, |
| 12 | + SpecialistResult, |
| 13 | + SpecialistSpec, |
| 14 | + run_bounded_specialists, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@dataclass(frozen=True) |
| 19 | +class WorkerEnvelope: |
| 20 | + succeeded: bool |
| 21 | + peakConcurrency: int |
| 22 | + roles: tuple[str, ...] |
| 23 | + evidenceUris: tuple[str, ...] |
| 24 | + summary: str |
| 25 | + |
| 26 | + |
| 27 | +async def execute_request(payload: dict[str, Any]) -> WorkerEnvelope: |
| 28 | + required = ("goalId", "workItemId", "attempt", "isRepair", "correlationId") |
| 29 | + missing = [name for name in required if name not in payload] |
| 30 | + if missing: |
| 31 | + raise ValueError(f"Missing worker request fields: {', '.join(missing)}") |
| 32 | + |
| 33 | + active = 0 |
| 34 | + peak = 0 |
| 35 | + lock = asyncio.Lock() |
| 36 | + |
| 37 | + async def execute(spec: SpecialistSpec) -> SpecialistResult: |
| 38 | + nonlocal active, peak |
| 39 | + async with lock: |
| 40 | + active += 1 |
| 41 | + peak = max(peak, active) |
| 42 | + try: |
| 43 | + await asyncio.sleep(0.01) |
| 44 | + uri = f"cas://evidence/worker/{payload['goalId']}/{payload['attempt']}/{spec.role}" |
| 45 | + return SpecialistResult(spec.role, "succeeded", f"{spec.role} analysis complete", (uri,)) |
| 46 | + finally: |
| 47 | + async with lock: |
| 48 | + active -= 1 |
| 49 | + |
| 50 | + aggregate = await run_bounded_specialists( |
| 51 | + [SpecialistSpec(role) for role in REQUIRED_SPECIALIST_ROLES], |
| 52 | + execute, |
| 53 | + max_fan_out=3, |
| 54 | + timeout_seconds=30, |
| 55 | + ) |
| 56 | + evidence = tuple(uri for result in aggregate.results for uri in result.artifacts) |
| 57 | + return WorkerEnvelope( |
| 58 | + aggregate.complete, |
| 59 | + peak, |
| 60 | + tuple(result.role for result in aggregate.results), |
| 61 | + evidence, |
| 62 | + "repair fan-out completed" if payload["isRepair"] else "feature fan-out completed", |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def main() -> int: |
| 67 | + parser = argparse.ArgumentParser() |
| 68 | + parser.add_argument("--request", help="JSON worker request; stdin is used when omitted") |
| 69 | + args = parser.parse_args() |
| 70 | + try: |
| 71 | + payload = json.loads(args.request if args.request is not None else sys.stdin.read()) |
| 72 | + result = asyncio.run(execute_request(payload)) |
| 73 | + except (ValueError, json.JSONDecodeError) as error: |
| 74 | + print(json.dumps({"error": str(error)}), file=sys.stderr) |
| 75 | + return 2 |
| 76 | + print(json.dumps(asdict(result), separators=(",", ":"))) |
| 77 | + return 0 |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + raise SystemExit(main()) |
0 commit comments