Coverage for little_loops / cli / loop / run.py: 0%
222 statements
« prev ^ index » next coverage.py v7.12.0, created at 2026-05-11 00:29 -0500
« prev ^ index » next coverage.py v7.12.0, created at 2026-05-11 00:29 -0500
1"""ll-loop run subcommand."""
3from __future__ import annotations
5import argparse
6import atexit
7import json
8import os
9import re
10import time
11import uuid
12from datetime import UTC, datetime
13from pathlib import Path
15from little_loops.cli.loop._helpers import (
16 _is_earliest_waiter,
17 _make_instance_id,
18 get_builtin_loops_dir,
19 print_execution_plan,
20 register_loop_signal_handlers,
21 resolve_loop_path,
22 run_background,
23 run_foreground,
24)
25from little_loops.logger import Logger
28def _parse_program_md(path: Path) -> dict[str, str]:
29 """Parse .ll/program.md heading sections into context key-value pairs.
31 Sections mapped:
32 ## Directive → directive (prose)
33 ## Targets → targets (space-joined list items)
34 ## Benchmark → each key: value pair injected directly
35 ## Budget → budget (prose)
36 ## Constraints → constraints (prose)
37 """
38 if not path.exists():
39 return {}
40 try:
41 content = path.read_text()
42 except OSError:
43 return {}
45 def _extract(heading: str) -> str:
46 m = re.search(rf"^##\s+{re.escape(heading)}\s*$", content, re.MULTILINE | re.IGNORECASE)
47 if not m:
48 return ""
49 start = m.end()
50 nxt = re.search(r"^##\s", content[start:], re.MULTILINE)
51 return content[start : start + nxt.start()].strip() if nxt else content[start:].strip()
53 result: dict[str, str] = {}
55 directive = _extract("Directive")
56 if directive:
57 result["directive"] = directive
59 targets_text = _extract("Targets")
60 if targets_text:
61 items = [
62 line.lstrip("-* \t").strip()
63 for line in targets_text.splitlines()
64 if line.strip().startswith(("-", "*"))
65 ]
66 result["targets"] = " ".join(items) if items else targets_text
68 benchmark_text = _extract("Benchmark")
69 if benchmark_text:
70 for line in benchmark_text.splitlines():
71 if ":" in line:
72 k, _, v = line.partition(":")
73 k, v = k.strip(), v.strip()
74 if k and v:
75 result[k] = v
77 budget = _extract("Budget")
78 if budget:
79 result["budget"] = budget
81 constraints = _extract("Constraints")
82 if constraints:
83 result["constraints"] = constraints
85 return result
88def cmd_run(
89 loop_name: str,
90 args: argparse.Namespace,
91 loops_dir: Path,
92 logger: Logger,
93) -> int:
94 """Run a loop."""
95 from little_loops.fsm.concurrency import LockManager
96 from little_loops.fsm.persistence import PersistentExecutor, _reconcile_stale_runs
97 from little_loops.fsm.rate_limit_circuit import RateLimitCircuit
98 from little_loops.fsm.validation import load_and_validate
100 try:
101 if getattr(args, "builtin", False):
102 path = get_builtin_loops_dir() / f"{loop_name}.yaml"
103 if not path.exists():
104 logger.error(f"Built-in loop not found: {loop_name!r}")
105 return 1
106 else:
107 path = resolve_loop_path(loop_name, loops_dir)
108 fsm, _ = load_and_validate(path)
109 except FileNotFoundError as e:
110 logger.error(str(e))
111 return 1
112 except ValueError as e:
113 logger.error(f"Validation error: {e}")
114 return 1
116 # Apply overrides
117 if args.max_iterations:
118 fsm.max_iterations = args.max_iterations
119 if args.delay is not None:
120 fsm.backoff = args.delay
121 if args.no_llm:
122 fsm.llm.enabled = False
123 if args.llm_model:
124 fsm.llm.model = args.llm_model
125 # Inject positional input arg before --context so --context can override
126 if getattr(args, "input", None) is not None:
127 raw = args.input
128 try:
129 parsed = json.loads(raw)
130 if isinstance(parsed, dict):
131 matched = {k: v for k, v in parsed.items() if k in fsm.context}
132 if matched:
133 fsm.context.update(matched)
134 else:
135 fsm.context[fsm.input_key] = raw
136 else:
137 fsm.context[fsm.input_key] = raw
138 except (json.JSONDecodeError, ValueError):
139 fsm.context[fsm.input_key] = raw
140 # Inject program.md fields before --context so --context can override
141 program_md_arg = getattr(args, "program_md", None)
142 program_md_path = (
143 program_md_arg if program_md_arg is not None else Path.cwd() / ".ll" / "program.md"
144 )
145 for key, value in _parse_program_md(program_md_path).items():
146 fsm.context[key] = value
147 for kv in getattr(args, "context", None) or []:
148 if "=" not in kv:
149 raise SystemExit(f"Invalid --context format: {kv!r} (expected KEY=VALUE)")
150 key, _, value = kv.partition("=")
151 fsm.context[key.strip()] = value.strip()
153 # Apply YAML loop config env-var overrides (CLI flags below overwrite these)
154 if fsm.config is not None and isinstance(fsm.config.handoff_threshold, int):
155 os.environ["LL_HANDOFF_THRESHOLD"] = str(fsm.config.handoff_threshold)
157 if getattr(args, "handoff_threshold", None) is not None:
158 if not (1 <= args.handoff_threshold <= 100):
159 raise SystemExit("--handoff-threshold must be between 1 and 100")
160 os.environ["LL_HANDOFF_THRESHOLD"] = str(args.handoff_threshold)
162 if getattr(args, "context_limit", None) is not None:
163 os.environ["LL_CONTEXT_LIMIT"] = str(args.context_limit)
165 from little_loops.config import BRConfig
167 _config = BRConfig(Path.cwd())
168 _edge_label_colors = _config.cli.colors.fsm_edge_labels.to_dict()
169 _highlight_color = _config.cli.colors.fsm_active_state
170 _badges = _config.loops.glyphs.to_dict()
172 # Dry run
173 if args.dry_run:
174 print_execution_plan(fsm, edge_label_colors=_edge_label_colors)
175 return 0
177 # Pre-run validation: check required context variables are present
178 _ctx_var_re = re.compile(r"\$\{context\.([^}.]+)")
179 missing_keys: set[str] = set()
180 for state in fsm.states.values():
181 templates = [state.action] if state.action else []
182 if state.evaluate and state.evaluate.prompt:
183 templates.append(state.evaluate.prompt)
184 for template in templates:
185 for m in _ctx_var_re.finditer(template):
186 key = m.group(1)
187 if key not in fsm.context:
188 missing_keys.add(key)
189 if missing_keys:
190 for key in sorted(missing_keys):
191 logger.error(
192 f"Missing required context variable: '{key}'. "
193 f"Run with: ll-loop run {loop_name} --context {key}=VALUE"
194 )
195 return 1
197 # Background mode: spawn detached process and return
198 if getattr(args, "background", False):
199 return run_background(loop_name, args, loops_dir)
201 # Register PID file for all foreground runs so cmd_stop can send SIGTERM (BUG-639).
202 # Background-spawned processes (foreground_internal=True) have their PID written by the
203 # parent in run_background(); plain foreground runs must write their own PID here.
204 running_dir = loops_dir / ".running"
205 running_dir.mkdir(parents=True, exist_ok=True)
206 _reconcile_stale_runs(loops_dir)
207 if getattr(args, "foreground_internal", False):
208 instance_id: str | None = getattr(args, "instance_id", None)
209 else:
210 instance_id = _make_instance_id(loop_name)
211 pid_file = running_dir / f"{instance_id or loop_name}.pid"
212 foreground_pid_file: Path | None = pid_file
214 if not getattr(args, "foreground_internal", False):
215 pid_file.write_text(str(os.getpid()))
217 def _cleanup_pid() -> None:
218 pid_file.unlink(missing_ok=True)
220 atexit.register(_cleanup_pid)
222 # Scope-based locking
223 lock_manager = LockManager(loops_dir)
224 scope = fsm.scope or ["."]
225 _queue_entry_file: Path | None = None
227 def _cleanup_queue_entry() -> None:
228 if _queue_entry_file is not None:
229 _queue_entry_file.unlink(missing_ok=True)
231 if not lock_manager.acquire(fsm.name, scope, instance_id=instance_id):
232 conflict = lock_manager.find_conflict(scope)
233 if conflict and getattr(args, "queue", False):
234 # Write queue entry so dashboard shows the waiting loop
235 queue_dir = loops_dir / ".queue"
236 queue_dir.mkdir(parents=True, exist_ok=True)
237 entry_id = str(uuid.uuid4())
238 entry = {
239 "id": entry_id,
240 "loopName": loop_name,
241 "enqueuedAt": datetime.now(UTC).isoformat(),
242 "context": {
243 "waitingFor": conflict.loop_name,
244 "scope": conflict.scope,
245 "pid": os.getpid(),
246 },
247 }
248 _queue_entry_file = queue_dir / f"{entry_id}.json"
249 _queue_entry_file.write_text(json.dumps(entry, indent=2))
250 atexit.register(_cleanup_queue_entry)
252 logger.info(f"Waiting for conflicting loop '{conflict.loop_name}' to finish...")
253 # Retry loop: when N waiters are released simultaneously, only one wins
254 # acquire(); losers loop back and wait again rather than exiting (BUG-1281).
255 acquired = False
256 _wait_start = time.time()
257 _budget = _config.loops.queue_wait_timeout_seconds
258 while time.time() - _wait_start < _budget:
259 _remaining = _budget - (time.time() - _wait_start)
260 if not lock_manager.wait_for_scope(scope, timeout=int(_remaining)):
261 _cleanup_queue_entry()
262 logger.error("Timeout waiting for scope to become available")
263 return 1
264 if not _is_earliest_waiter(entry_id, queue_dir):
265 time.sleep(1)
266 continue
267 if lock_manager.acquire(fsm.name, scope, instance_id=instance_id):
268 acquired = True
269 break
270 if not acquired:
271 _cleanup_queue_entry()
272 logger.error("Failed to acquire lock after waiting")
273 return 1
274 # Lock acquired - no longer queued
275 _cleanup_queue_entry()
276 elif conflict:
277 logger.error(f"Scope conflict with running loop: {conflict.loop_name}")
278 logger.info(f" Conflicting scope: {conflict.scope}")
279 logger.info(" Use --queue to wait for it to finish")
280 return 1
281 else:
282 # Unexpected: find_conflict returned None but acquire failed
283 logger.error("Failed to acquire scope lock (unknown reason)")
284 return 1
286 executor: PersistentExecutor | None = None
287 try:
288 # Worktree isolation: create branch + directory before anything reads Path.cwd()
289 if getattr(args, "worktree", False):
290 from little_loops.config import BRConfig as _MainBRConfig
291 from little_loops.parallel.git_lock import GitLock
292 from little_loops.worktree_utils import cleanup_worktree, setup_worktree
294 _main_config = _MainBRConfig(Path.cwd())
295 _worktree_base = _main_config.get_worktree_base()
296 _copy_files = _main_config.parallel.worktree_copy_files
297 _repo_path = Path.cwd()
299 _timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
300 _safe_name = re.sub(r"[^a-zA-Z0-9-]", "-", loop_name)
301 _branch_name = f"{_timestamp}-{_safe_name}"
302 _worktree_path = _worktree_base / _branch_name
304 _worktree_base.mkdir(parents=True, exist_ok=True)
305 _git_lock = GitLock(logger)
307 setup_worktree(
308 repo_path=_repo_path,
309 worktree_path=_worktree_path,
310 branch_name=_branch_name,
311 copy_files=_copy_files,
312 logger=logger,
313 git_lock=_git_lock,
314 )
316 logger.info(f"Worktree: {_worktree_path}")
317 logger.info(f"Branch: {_branch_name}")
319 def _cleanup_worktree_on_exit() -> None:
320 cleanup_worktree(
321 worktree_path=_worktree_path,
322 repo_path=_repo_path,
323 logger=logger,
324 git_lock=_git_lock,
325 delete_branch=True,
326 )
328 atexit.register(_cleanup_worktree_on_exit)
330 os.environ["CLAUDE_BASH_MAINTAIN_PROJECT_WORKING_DIR"] = "1"
331 os.chdir(_worktree_path)
333 circuit = (
334 RateLimitCircuit(Path(_config.commands.rate_limits.circuit_breaker_path))
335 if _config.commands.rate_limits.circuit_breaker_enabled
336 else None
337 )
338 executor = PersistentExecutor(
339 fsm, loops_dir=loops_dir, circuit=circuit, instance_id=instance_id, pid=os.getpid()
340 )
342 # Register signal handlers for graceful shutdown
343 register_loop_signal_handlers(executor, pid_file=foreground_pid_file)
345 from little_loops.extension import wire_extensions
346 from little_loops.transport import wire_transports
348 wire_extensions(executor.event_bus, _config.extensions, executor=executor)
349 wire_transports(executor.event_bus, _config.events)
350 return run_foreground(
351 executor,
352 fsm,
353 args,
354 highlight_color=_highlight_color,
355 edge_label_colors=_edge_label_colors,
356 badges=_badges,
357 )
358 finally:
359 if executor is not None:
360 executor.close_transports()
361 lock_manager.release(fsm.name, instance_id=instance_id)