|
| 1 | +# ratios: loc_comments=201:10 imports_exports=8:1 calls_definitions=84:9 |
| 2 | +"""Minimal CLI for the a0min agent harness. |
| 3 | +
|
| 4 | +Commands: |
| 5 | + list list potential sub-agent options from the superpotential |
| 6 | + create REGION create one potential sub-agent from a declared region |
| 7 | + show ID show a created sub-agent |
| 8 | + merge ID mark a created sub-agent merged |
| 9 | + superpotential dump the imported platonic superpotential |
| 10 | + caps show spawn caps for a tier |
| 11 | +
|
| 12 | +Every command accepts ``--json`` for machine-readable output. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import argparse |
| 18 | +import json |
| 19 | +import os |
| 20 | +import sys |
| 21 | +from pathlib import Path |
| 22 | +from typing import Any |
| 23 | + |
| 24 | +from .harness import ( |
| 25 | + SUPPORTED_CUT_MODES, |
| 26 | + SUPPORTED_ORCHESTRATION_MODES, |
| 27 | + Harness, |
| 28 | + SpawnCapExceeded, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +def _build_parser() -> argparse.ArgumentParser: |
| 33 | + parser = argparse.ArgumentParser( |
| 34 | + prog="a0min", |
| 35 | + description="Minimal agent harness over the imported a0 platonic superpotential.", |
| 36 | + ) |
| 37 | + parser.add_argument( |
| 38 | + "--state", |
| 39 | + default=os.environ.get("A0MIN_STATE"), |
| 40 | + help="JSON state file for created sub-agents (env: A0MIN_STATE)", |
| 41 | + ) |
| 42 | + sub = parser.add_subparsers(dest="command", required=True) |
| 43 | + |
| 44 | + list_parser = sub.add_parser("list", help="list potential sub-agent options") |
| 45 | + list_parser.add_argument("--json", action="store_true", dest="as_json") |
| 46 | + |
| 47 | + create_parser = sub.add_parser( |
| 48 | + "create", help="create one potential sub-agent from a region" |
| 49 | + ) |
| 50 | + create_parser.add_argument("region", help="declared semantic region name") |
| 51 | + create_parser.add_argument("--task", default="", help="task summary for the sub-agent") |
| 52 | + create_parser.add_argument( |
| 53 | + "--bind", |
| 54 | + action="append", |
| 55 | + default=[], |
| 56 | + metavar="K=V", |
| 57 | + help="projection binding; repeatable; values parse as JSON when possible", |
| 58 | + ) |
| 59 | + create_parser.add_argument( |
| 60 | + "--mode", |
| 61 | + default="single", |
| 62 | + choices=SUPPORTED_ORCHESTRATION_MODES, |
| 63 | + help="orchestration mode (default: single)", |
| 64 | + ) |
| 65 | + create_parser.add_argument( |
| 66 | + "--cut", |
| 67 | + default="soft", |
| 68 | + choices=SUPPORTED_CUT_MODES, |
| 69 | + help="cut mode (default: soft)", |
| 70 | + ) |
| 71 | + create_parser.add_argument("--tier", default="free", help="spawn-cap tier") |
| 72 | + create_parser.add_argument( |
| 73 | + "--provider", action="append", default=None, help="provider tag; repeatable" |
| 74 | + ) |
| 75 | + create_parser.add_argument( |
| 76 | + "--parent", default=None, help="parent sub_agent_id for depth/fanout accounting" |
| 77 | + ) |
| 78 | + create_parser.add_argument("--json", action="store_true", dest="as_json") |
| 79 | + |
| 80 | + show_parser = sub.add_parser("show", help="show a created sub-agent") |
| 81 | + show_parser.add_argument("sub_agent_id") |
| 82 | + show_parser.add_argument("--json", action="store_true", dest="as_json") |
| 83 | + |
| 84 | + merge_parser = sub.add_parser("merge", help="mark a created sub-agent merged") |
| 85 | + merge_parser.add_argument("sub_agent_id") |
| 86 | + merge_parser.add_argument("--json", action="store_true", dest="as_json") |
| 87 | + |
| 88 | + super_parser = sub.add_parser( |
| 89 | + "superpotential", help="dump the imported platonic superpotential" |
| 90 | + ) |
| 91 | + super_parser.add_argument("--json", action="store_true", dest="as_json") |
| 92 | + |
| 93 | + caps_parser = sub.add_parser("caps", help="show spawn caps for a tier") |
| 94 | + caps_parser.add_argument("--tier", default="free", help="spawn-cap tier") |
| 95 | + caps_parser.add_argument("--json", action="store_true", dest="as_json") |
| 96 | + |
| 97 | + return parser |
| 98 | + |
| 99 | + |
| 100 | +def _parse_binding(text: str) -> tuple[str, Any]: |
| 101 | + key, sep, value = text.partition("=") |
| 102 | + if not sep or not key: |
| 103 | + raise ValueError(f"binding must be K=V: {text}") |
| 104 | + try: |
| 105 | + parsed = json.loads(value) |
| 106 | + except json.JSONDecodeError: |
| 107 | + parsed = value |
| 108 | + return key, parsed |
| 109 | + |
| 110 | + |
| 111 | +def _print_or_json(payload: Any, as_json: bool) -> None: |
| 112 | + if as_json: |
| 113 | + print(json.dumps(payload, indent=2)) |
| 114 | + return |
| 115 | + if isinstance(payload, dict): |
| 116 | + for key, value in payload.items(): |
| 117 | + print(f"{key}: {value}") |
| 118 | + return |
| 119 | + for item in payload: |
| 120 | + print(item) |
| 121 | + |
| 122 | + |
| 123 | +def _list_potential(harness: Harness, as_json: bool) -> int: |
| 124 | + options = harness.potential_sub_agents() |
| 125 | + if as_json: |
| 126 | + print(json.dumps([option.as_dict() for option in options], indent=2)) |
| 127 | + return 0 |
| 128 | + print(f"potential sub-agents from {harness.superpotential.agent_id}:") |
| 129 | + for option in options: |
| 130 | + print(f" {option.region}") |
| 131 | + print(f" {option.description}") |
| 132 | + print(f" dims: {', '.join(option.dimensions)}") |
| 133 | + print(f" surfaces: {', '.join(option.surfaces)}") |
| 134 | + print( |
| 135 | + f" modes: {', '.join(option.orchestration_modes)} | " |
| 136 | + f"cuts: {', '.join(option.cut_modes)}" |
| 137 | + ) |
| 138 | + return 0 |
| 139 | + |
| 140 | + |
| 141 | +def _create(harness: Harness, args: argparse.Namespace) -> int: |
| 142 | + try: |
| 143 | + bindings = dict(_parse_binding(text) for text in args.bind) |
| 144 | + parent = harness.get(args.parent) if args.parent else None |
| 145 | + sub_agent = harness.create( |
| 146 | + args.region, |
| 147 | + bindings, |
| 148 | + task=args.task, |
| 149 | + orchestration_mode=args.mode, |
| 150 | + cut_mode=args.cut, |
| 151 | + providers=args.provider, |
| 152 | + parent=parent, |
| 153 | + ) |
| 154 | + except (ValueError, KeyError, SpawnCapExceeded) as exc: |
| 155 | + print(f"create failed: {exc}", file=sys.stderr) |
| 156 | + return 1 |
| 157 | + if args.as_json: |
| 158 | + print(json.dumps(sub_agent.as_dict(), indent=2)) |
| 159 | + return 0 |
| 160 | + print( |
| 161 | + f"created {sub_agent.sub_agent_id} {sub_agent.name} " |
| 162 | + f"run={sub_agent.run_id} depth={sub_agent.depth} " |
| 163 | + f"region={sub_agent.region} mode={sub_agent.orchestration_mode} " |
| 164 | + f"cut={sub_agent.cut_mode}" |
| 165 | + ) |
| 166 | + print(f" selected: {', '.join(sub_agent.selected) or '-'}") |
| 167 | + print(f" unresolved: {', '.join(sub_agent.unresolved) or '-'}") |
| 168 | + print(f" omitted: {', '.join(sub_agent.omitted) or '-'}") |
| 169 | + return 0 |
| 170 | + |
| 171 | + |
| 172 | +def _show(harness: Harness, args: argparse.Namespace) -> int: |
| 173 | + try: |
| 174 | + sub_agent = harness.get(args.sub_agent_id) |
| 175 | + except KeyError as exc: |
| 176 | + print(f"show failed: {exc}", file=sys.stderr) |
| 177 | + return 1 |
| 178 | + if args.as_json: |
| 179 | + print(json.dumps(sub_agent.as_dict(), indent=2)) |
| 180 | + return 0 |
| 181 | + for key, value in sub_agent.as_dict().items(): |
| 182 | + print(f"{key}: {value}") |
| 183 | + return 0 |
| 184 | + |
| 185 | + |
| 186 | +def _merge(harness: Harness, args: argparse.Namespace) -> int: |
| 187 | + try: |
| 188 | + sub_agent = harness.merge(args.sub_agent_id) |
| 189 | + except KeyError as exc: |
| 190 | + print(f"merge failed: {exc}", file=sys.stderr) |
| 191 | + return 1 |
| 192 | + if args.as_json: |
| 193 | + print(json.dumps(sub_agent.as_dict(), indent=2)) |
| 194 | + return 0 |
| 195 | + print(f"merged {sub_agent.sub_agent_id} {sub_agent.name} (status={sub_agent.status})") |
| 196 | + return 0 |
| 197 | + |
| 198 | + |
| 199 | +def _superpotential(harness: Harness, as_json: bool) -> int: |
| 200 | + payload = harness.superpotential_dict() |
| 201 | + if as_json: |
| 202 | + print(json.dumps(payload, indent=2)) |
| 203 | + return 0 |
| 204 | + print(f"superpotential: {payload['agent_id']}") |
| 205 | + for dimension in payload["dimensions"]: |
| 206 | + print(f" dimension {dimension['name']}: {dimension['description']}") |
| 207 | + for region in payload["regions"]: |
| 208 | + print(f" region {region['region']}: {region['description']}") |
| 209 | + return 0 |
| 210 | + |
| 211 | + |
| 212 | +def main(argv: list[str] | None = None) -> int: |
| 213 | + parser = _build_parser() |
| 214 | + args = parser.parse_args(argv) |
| 215 | + |
| 216 | + tier = getattr(args, "tier", "free") |
| 217 | + state_path = getattr(args, "state", None) |
| 218 | + if state_path and Path(state_path).exists(): |
| 219 | + harness = Harness.load(state_path, tier=tier) |
| 220 | + else: |
| 221 | + harness = Harness(tier=tier) |
| 222 | + |
| 223 | + if args.command == "list": |
| 224 | + code = _list_potential(harness, args.as_json) |
| 225 | + elif args.command == "create": |
| 226 | + code = _create(harness, args) |
| 227 | + elif args.command == "show": |
| 228 | + code = _show(harness, args) |
| 229 | + elif args.command == "merge": |
| 230 | + code = _merge(harness, args) |
| 231 | + elif args.command == "superpotential": |
| 232 | + code = _superpotential(harness, args.as_json) |
| 233 | + elif args.command == "caps": |
| 234 | + if args.as_json: |
| 235 | + print(json.dumps(harness.caps, indent=2)) |
| 236 | + else: |
| 237 | + _print_or_json(harness.caps, False) |
| 238 | + code = 0 |
| 239 | + else: |
| 240 | + parser.error(f"unknown command: {args.command}") |
| 241 | + code = 2 |
| 242 | + |
| 243 | + if state_path and code == 0: |
| 244 | + harness.save(state_path) |
| 245 | + return code |
| 246 | +# ratios: loc_comments=201:10 imports_exports=8:1 calls_definitions=84:9 |
0 commit comments