From ed005ba859413f4129173f9d9ccd2d507aac0304 Mon Sep 17 00:00:00 2001 From: Tom Softreck Date: Sat, 14 Nov 2026 12:31:08 +0100 Subject: [PATCH] feat(diagnostics): add autonomy stall probes, doctor budget, day/summary labels, and triage dedup [ticket-137] --- project/TICKETS.md | 2 + project/ticket-137/README.md | 31 ++ project/ticket-137/intent.json | 31 ++ src/monag/autodiagnosis.py | 493 ++++++++++++++++- src/monag/cli.py | 27 +- src/monag/doctor.py | 506 ++++++++++++++---- src/monag/summary.py | 674 ++++++++++++++++++++++-- src/monag/triage.py | 266 +++++++++- src/monag/worktrees.py | 57 +- tests/test_summary_and_subllm_routes.py | 7 +- tests/test_wave2_mon01_mon04.py | 548 +++++++++++++++++++ 11 files changed, 2451 insertions(+), 191 deletions(-) create mode 100644 project/ticket-137/README.md create mode 100644 project/ticket-137/intent.json create mode 100644 tests/test_wave2_mon01_mon04.py diff --git a/project/TICKETS.md b/project/TICKETS.md index f64a80c..8e2787b 100644 --- a/project/TICKETS.md +++ b/project/TICKETS.md @@ -123,4 +123,6 @@ | **ticket-133** | [`README.md`](./ticket-133/README.md) | - | - | - | - | - | | **ticket-134** | [`README.md`](./ticket-134/README.md) | - | - | - | - | - | | **ticket-135** | [`README.md`](./ticket-135/README.md) | - | - | - | - | - | +| **ticket-136** | [`README.md`](./ticket-136/README.md) | - | - | - | - | - | +| **ticket-137** | [`README.md`](./ticket-137/README.md) | - | - | - | - | - | diff --git a/project/ticket-137/README.md b/project/ticket-137/README.md new file mode 100644 index 0000000..d82a448 --- /dev/null +++ b/project/ticket-137/README.md @@ -0,0 +1,31 @@ +# Ticket 137: Wave 2 autonomy stall probes, doctor budget, day/summary separation, and triage deduplication + +- **ID**: ticket-137 +- **Owner**: agent:antigravity +- **Status**: IN_PROGRESS +- **Workflow state**: EDIT +- **Created**: 2026-11-14 + +## Goal and scope + +Implement Wave 2 diagnostic and operational reliability improvements across `monag` (`MON-01`..`MON-04`, issues `#162`, `#163`, `#164`, `#165`): + +1. **`MON-01` (`#162`)**: Add deterministic, read-only autonomy stall probes to `monag autodiagnose` (`systemd --user` status of known autonomy units, loopback Heyasa `/v1/daemon/status` eligibility/slot/velocity probe, and host load saturation vs CPU count), ranking operational stall anomalies strictly above repository hygiene findings. +2. **`MON-02` (`#163`)**: Hoist `worktrees.get_repo_leases(repo)` and default branch resolution outside the per-worktree loop in `monag doctor` (`O(1)` per repository instead of `O(N)` per worktree), and add a monotonic `--timeout` / `budget_seconds` deadline with structured partial output (`timed_out`, `completed_phases`, `timed_out_phases`, `skipped_phases`, `skipped_repositories`). +3. **`MON-03` (`#164`)**: Separate `monag day` (24h operational delta: completed/blocked Planfile tasks in window, completed/failed local agent task runs, and 24h merged/opened PRs) from `monag summary` (current portfolio/queue snapshot, active `.subactor/leases`, and open PR queue), attaching explicit `source` + `scope`/`filter` labels to every count in JSON and Markdown while preserving `gather_planfile_daily` compatibility for the frozen acceptance probe. +4. **`MON-04` (`#165`)**: Group and deduplicate repeated `monag triage` findings by `(repository, cause)` with `affected_count`, `sample_items`, and `estimated_unblock_impact`, sorting deterministically by unblock impact and severity descending. + +Session authorization: `SESSION_EXECUTION_AUTHORIZATION` granted by caller to implement, test, commit, push, open PR, and invoke the independent Validator for Wave 2 (`MON-01..MON-04`, 2026-11-14). + +## Acceptance criteria + +- [x] AC-01: `autodiagnose` detects failed autonomy systemd units, Heyasa dispatch/velocity stalls, and host load saturation with graceful degradation and ranks them strictly above repo hygiene defects (`#162`). +- [x] AC-02: `doctor` reads repository leases once per repo instead of once per worktree and returns structured partial diagnostics within monotonic `--timeout` / `budget_seconds` (`#163`). +- [x] AC-03: `monag day` and `monag summary` render distinct 24h delta vs current portfolio snapshot reports with explicit `source` and `scope`/`filter` metadata on every count in JSON and Markdown (`#164`). +- [x] AC-04: `monag triage` collapses repeated `(repository, cause)` items into grouped steps with `affected_count` and `sample_items`, sorted by unblock impact descending (`#165`). +- [x] AC-05: Unit tests (`unittest` and `pytest`), `./project/governance-check.sh`, and `wellman check` pass cleanly. + +## Tracking boundary + +This directory contains the minimal reviewed intent. Optional participant prose +and raw command logs are not required delivery output. diff --git a/project/ticket-137/intent.json b/project/ticket-137/intent.json new file mode 100644 index 0000000..e18310f --- /dev/null +++ b/project/ticket-137/intent.json @@ -0,0 +1,31 @@ +{ + "schema": "new-project.intent/v3", + "ticket": "ticket-137", + "summary": "Wave 2 autonomy stall probes, doctor budget, day/summary separation, and triage deduplication", + "workstream": "application", + "classification": { + "kind": "SERVICE", + "priority": "P2", + "origin": "health" + }, + "allowedPaths": [ + "project/ticket-137/**", + "TODO.md", + "project/TICKETS.md", + "src/monag/autodiagnosis.py", + "src/monag/cli.py", + "src/monag/doctor.py", + "src/monag/summary.py", + "src/monag/triage.py", + "src/monag/worktrees.py", + "tests/test_summary_and_subllm_routes.py", + "tests/test_wave2_mon01_mon04.py" + ], + "forbiddenPaths": [ + "project/ticket-*/user-*.md" + ], + "stacks": [], + "dependsOn": [], + "conflictsWith": [], + "integrationTicket": null +} diff --git a/src/monag/autodiagnosis.py b/src/monag/autodiagnosis.py index 755d47f..31fc319 100644 --- a/src/monag/autodiagnosis.py +++ b/src/monag/autodiagnosis.py @@ -20,12 +20,36 @@ import sys import threading import time -from typing import Any, Callable, Dict, List, Optional, Tuple +import urllib.error +import urllib.parse +import urllib.request +from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple import yaml SCHEMA = "monag.autodiagnosis/v1" +DEFAULT_AUTONOMY_UNITS: Tuple[str, ...] = ( + "subactor-coding-agent.service", + "subactor-pr-controller.service", + "subactor-repair-agent.service", + "subactor-autonom-user.service", + "heyasa.service", +) + +AUTONOMY_UNIT_TARGETS: Dict[str, str] = { + "subactor-coding-agent.service": "subactor/coding-agent", + "subactor-pr-controller.service": "subactor/pr-controller", + "subactor-repair-agent.service": "subactor/repair-agent", + "subactor-autonom-user.service": "subactor/autonom", + "heyasa.service": "heyasa/heyasa", +} + +DEFAULT_HEYASA_STATUS_URLS: Tuple[str, ...] = ( + "http://127.0.0.1:8765/v1/daemon/status", + "http://127.0.0.1:7080/v1/daemon/status", +) + TIER_FLOOR = "floor" TIER_MISSION = "mission" TIER_HYGIENE = "hygiene" @@ -55,21 +79,51 @@ "lowest": 4, } +SEVERITY_ORDER = { + "CRITICAL": 0, + "HIGH": 1, + "ERROR": 2, + "WARNING": 3, + "INFO": 4, +} + def priority_sort_key(ticket_or_task: Dict[str, Any]) -> tuple: - """Sort key matching Wellmanifest lexicographic tiers and priority order. + """Sort key matching Operational Weight + Wellmanifest lexicographic tiers and priority order. + 0. Operational stall weight: higher weight first (autonomy stalls > 0, repo hygiene = 0) 1. Tier: floor (0) < mission (1) < hygiene (2) < backlog (3) 2. Priority: critical (0) < high (1) < medium/normal (2) < low (3) < lowest (4) 3. Created at: older first (FIFO) """ + try: + weight = int(ticket_or_task.get("weight", 0) or 0) + except (TypeError, ValueError): + weight = 0 tier = str(ticket_or_task.get("tier") or TIER_BACKLOG).lower() priority = str(ticket_or_task.get("priority") or "normal").lower() created_at = str(ticket_or_task.get("created_at") or "") tier_weight = TIER_ORDER.get(tier, 3) priority_weight = PRIORITY_ORDER.get(priority, 2) - return (tier_weight, priority_weight, created_at) + return (-weight, tier_weight, priority_weight, created_at) + + +def anomaly_sort_key(anomaly: Dict[str, Any]) -> tuple: + """Sort raw anomalies so operational autonomy stalls always precede repo hygiene findings.""" + try: + weight = int(anomaly.get("weight", 0) or 0) + except (TypeError, ValueError): + weight = 0 + tier = str(anomaly.get("tier") or TIER_BACKLOG).lower() + severity = str(anomaly.get("severity") or "INFO").upper() + return ( + -weight, + TIER_ORDER.get(tier, 3), + SEVERITY_ORDER.get(severity, 5), + str(anomaly.get("target") or ""), + str(anomaly.get("code") or ""), + ) def sort_tickets_by_priority(tickets: List[Dict[str, Any]]) -> List[Dict[str, Any]]: @@ -522,9 +576,401 @@ def inspect_repository_anomalies(repo_path: Path, return anomalies +def _is_loopback_status_url(url: str) -> bool: + try: + parsed = urllib.parse.urlparse(url) + except Exception: + return False + if parsed.scheme not in ("http", "https"): + return False + return (parsed.hostname or "").lower() in {"127.0.0.1", "localhost", "::1"} + + +def _probe_systemd_user_units( + root: Path, + units: Sequence[str] = DEFAULT_AUTONOMY_UNITS, + timeout: float = 2.5, + runner: Optional[Callable[..., Any]] = None, +) -> List[Dict[str, Any]]: + """Read-only probe of known systemd --user autonomy units with short timeout.""" + if not units: + return [] + exec_run = runner or subprocess.run + try: + proc = exec_run( + [ + "systemctl", + "--user", + "show", + *units, + "--property=Id,LoadState,ActiveState,SubState,Result,ExecMainStatus", + ], + capture_output=True, + text=True, + timeout=max(0.2, float(timeout)), + ) + except (FileNotFoundError, subprocess.SubprocessError, OSError): + return [] + + raw_out = getattr(proc, "stdout", "") or "" + if not isinstance(raw_out, str) or not raw_out.strip(): + return [] + + anomalies: List[Dict[str, Any]] = [] + blocks = [blk.strip() for blk in raw_out.split("\n\n") if blk.strip()] + for block in blocks: + props: Dict[str, str] = {} + for line in block.splitlines(): + if "=" in line: + key, val = line.split("=", 1) + props[key.strip()] = val.strip() + unit_id = props.get("Id", "") + if not unit_id: + continue + load_state = props.get("LoadState", "").lower() + if load_state in ("", "not-found", "masked"): + continue + active_state = props.get("ActiveState", "").lower() + sub_state = props.get("SubState", "").lower() + result_state = props.get("Result", "").lower() + exec_status = props.get("ExecMainStatus", "") + + target = AUTONOMY_UNIT_TARGETS.get(unit_id, "subactor/autonom") + target_path = root / target if (root / target).is_dir() else root + + is_failed = ( + active_state == "failed" + or sub_state == "failed" + or (result_state not in ("", "success") and active_state != "active") + ) + if is_failed: + anomalies.append({ + "code": "AUTONOMY_UNIT_FAILED", + "tier": TIER_FLOOR, + "severity": "CRITICAL", + "weight": 1000, + "autonomy_probe": True, + "target": target, + "path": str(target_path), + "summary": ( + f"Autonomy systemd --user unit '{unit_id}' failed " + f"(ActiveState={active_state or 'unknown'}, Result={result_state or 'unknown'}, " + f"ExecMainStatus={exec_status or 'n/a'})." + ), + "evidence": ( + f"systemctl --user show {unit_id}: LoadState={load_state}, " + f"ActiveState={active_state}, SubState={sub_state}, " + f"Result={result_state}, ExecMainStatus={exec_status}." + ), + "details": { + "unit": unit_id, + "load_state": load_state, + "active_state": active_state, + "sub_state": sub_state, + "result": result_state, + "exec_main_status": exec_status, + }, + }) + elif unit_id == "heyasa.service" and active_state in ("inactive", "deactivating"): + anomalies.append({ + "code": "AUTONOMY_DAEMON_INACTIVE", + "tier": TIER_FLOOR, + "severity": "HIGH", + "weight": 920, + "autonomy_probe": True, + "target": target, + "path": str(target_path), + "summary": f"Autonomy orchestrator unit '{unit_id}' is loaded but {active_state}.", + "evidence": ( + f"systemctl --user show {unit_id}: LoadState={load_state}, " + f"ActiveState={active_state}, SubState={sub_state}." + ), + "details": { + "unit": unit_id, + "load_state": load_state, + "active_state": active_state, + "sub_state": sub_state, + }, + }) + + return anomalies + + +def _extract_heyasa_flowguard(payload: Dict[str, Any]) -> Dict[str, Any]: + """Extract flowguard metrics from nested or flat /v1/daemon/status payloads.""" + candidates = [ + ((payload.get("last_result") or {}).get("willman") or {}).get("flowguard"), + (payload.get("willman") or {}).get("flowguard"), + payload.get("flowguard"), + payload, + ] + merged: Dict[str, Any] = {} + for obj in candidates: + if isinstance(obj, dict): + for key in ( + "eligible_projects", + "admitted_slots", + "blocked_candidates", + "admitted_candidates", + "project_exclusions", + "blocked_reasons", + ): + if key not in merged and key in obj and obj[key] is not None: + merged[key] = obj[key] + return merged + + +def _probe_heyasa_daemon_status( + root: Path, + status_url: Optional[str] = None, + status_payload: Optional[Dict[str, Any]] = None, + timeout: float = 1.5, + opener: Optional[Callable[..., Any]] = None, +) -> List[Dict[str, Any]]: + """Read-only loopback probe of local Heyasa /v1/daemon/status for dispatch or velocity stalls.""" + payload: Optional[Dict[str, Any]] = status_payload + observed_url = status_url or os.environ.get("MONAG_HEYASA_STATUS_URL", "").strip() + + if payload is None: + urls: Sequence[str] = (observed_url,) if observed_url else DEFAULT_HEYASA_STATUS_URLS + open_fn = opener or urllib.request.urlopen + for candidate_url in urls: + if not candidate_url or not _is_loopback_status_url(candidate_url): + continue + try: + req = urllib.request.Request(candidate_url, headers={"Accept": "application/json"}, method="GET") + with open_fn(req, timeout=max(0.2, float(timeout))) as resp: + raw = resp.read(256 * 1024) + decoded = json.loads(raw.decode("utf-8", errors="replace") if isinstance(raw, (bytes, bytearray)) else str(raw)) + if isinstance(decoded, dict): + payload = decoded + observed_url = candidate_url + break + except (urllib.error.URLError, TimeoutError, OSError, ValueError): + continue + + if not isinstance(payload, dict): + return [] + + target = "heyasa/heyasa" + target_path = root / target if (root / target).is_dir() else root + flow = _extract_heyasa_flowguard(payload) + anomalies: List[Dict[str, Any]] = [] + + eligible = flow.get("eligible_projects") + admitted = flow.get("admitted_slots") + blocked = flow.get("blocked_candidates", 0) + blocked_reasons = flow.get("blocked_reasons") if isinstance(flow.get("blocked_reasons"), dict) else {} + exclusions = flow.get("project_exclusions") if isinstance(flow.get("project_exclusions"), dict) else {} + + try: + eligible_int = int(eligible) if eligible is not None else None + except (TypeError, ValueError): + eligible_int = None + try: + admitted_int = int(admitted) if admitted is not None else None + except (TypeError, ValueError): + admitted_int = None + try: + blocked_int = int(blocked or 0) + except (TypeError, ValueError): + blocked_int = 0 + + if eligible_int is not None and eligible_int == 0: + anomalies.append({ + "code": "AUTONOMY_ELIGIBLE_PROJECTS_ZERO", + "tier": TIER_FLOOR, + "severity": "CRITICAL", + "weight": 980, + "autonomy_probe": True, + "target": target, + "path": str(target_path), + "summary": ( + f"Autonomy stall: Heyasa daemon reports 0 eligible projects " + f"(admitted_slots={admitted_int if admitted_int is not None else 'n/a'}, " + f"blocked_candidates={blocked_int})." + ), + "evidence": ( + f"{observed_url or 'heyasa.daemon-status'}: eligible_projects=0, " + f"exclusions={len(exclusions)}, blocked_reasons={json.dumps(blocked_reasons, sort_keys=True)[:240]}." + ), + "details": { + "url": observed_url or "inline", + "eligible_projects": eligible_int, + "admitted_slots": admitted_int, + "blocked_candidates": blocked_int, + "blocked_reasons": blocked_reasons, + }, + }) + + if admitted_int is not None and admitted_int == 0 and blocked_int > 0: + anomalies.append({ + "code": "AUTONOMY_ADMISSION_STALL", + "tier": TIER_FLOOR, + "severity": "CRITICAL", + "weight": 960, + "autonomy_probe": True, + "target": target, + "path": str(target_path), + "summary": ( + f"Autonomy admission stall: 0 admitted slots while {blocked_int} candidate tasks are blocked." + ), + "evidence": ( + f"{observed_url or 'heyasa.daemon-status'}: admitted_slots=0, " + f"blocked_candidates={blocked_int}, reasons={json.dumps(blocked_reasons, sort_keys=True)[:240]}." + ), + "details": { + "url": observed_url or "inline", + "admitted_slots": admitted_int, + "blocked_candidates": blocked_int, + "blocked_reasons": blocked_reasons, + }, + }) + + velocity = payload.get("velocity_status") + vel_stalled = False + vel_summary = "" + if isinstance(velocity, str): + if velocity.strip().lower() in {"stall", "stalled", "degraded", "zero", "blocked", "critical"}: + vel_stalled = True + vel_summary = f"velocity_status={velocity.strip()}" + elif isinstance(velocity, dict): + completed_cnt = velocity.get("completed_count") + open_cands = velocity.get("open_candidates_count") or 0 + target_cap = velocity.get("target_capacity") or 0 + status_label = str(velocity.get("status") or "").strip().lower() + if ( + bool(velocity.get("is_degraded")) + or status_label in {"stall", "stalled", "degraded", "critical"} + or (completed_cnt == 0 and int(open_cands) > 0 and int(target_cap) > 0) + ): + vel_stalled = True + vel_summary = str( + velocity.get("summary") + or f"completed={completed_cnt}/{target_cap}, open_candidates={open_cands}" + ) + + if vel_stalled: + anomalies.append({ + "code": "AUTONOMY_VELOCITY_STALL", + "tier": TIER_FLOOR, + "severity": "HIGH", + "weight": 930, + "autonomy_probe": True, + "target": target, + "path": str(target_path), + "summary": f"Autonomy velocity stall detected: {vel_summary[:180]}", + "evidence": f"{observed_url or 'heyasa.daemon-status'} velocity_status: {vel_summary[:300]}", + "details": { + "url": observed_url or "inline", + "velocity_status": velocity, + }, + }) + + return anomalies + + +def _probe_host_load_saturation( + root: Path, + load_fn: Optional[Callable[[], Tuple[float, float, float]]] = None, + cpu_count_fn: Optional[Callable[[], Optional[int]]] = None, + warning_ratio: float = 1.5, + critical_ratio: float = 2.0, +) -> List[Dict[str, Any]]: + """Check 1-minute host load average against logical CPU count.""" + get_load = load_fn or getattr(os, "getloadavg", None) + get_cpus = cpu_count_fn or os.cpu_count + if get_load is None: + return [] + try: + load_1m, load_5m, load_15m = get_load() + cpus = max(1, int(get_cpus() or 1)) + except (OSError, TypeError, ValueError): + return [] + + ratio = float(load_1m) / float(cpus) + if ratio <= warning_ratio: + return [] + + is_critical = ratio > critical_ratio + return [ + { + "code": "HOST_LOAD_SATURATION", + "tier": TIER_FLOOR if is_critical else TIER_MISSION, + "severity": "CRITICAL" if is_critical else "WARNING", + "weight": 900 if is_critical else 850, + "autonomy_probe": True, + "target": "host/system", + "path": str(root), + "summary": ( + f"Host CPU load saturation ({'critical' if is_critical else 'warning'}): " + f"1m load {load_1m:.2f} across {cpus} CPUs ({ratio:.2f}x capacity)." + ), + "evidence": ( + f"os.getloadavg()=({load_1m:.2f}, {load_5m:.2f}, {load_15m:.2f}), " + f"os.cpu_count()={cpus}, ratio={ratio:.2f}x " + f"(thresholds: warning>{warning_ratio:.1f}x, critical>{critical_ratio:.1f}x)." + ), + "details": { + "load_1m": round(float(load_1m), 2), + "load_5m": round(float(load_5m), 2), + "load_15m": round(float(load_15m), 2), + "cpu_count": cpus, + "load_per_cpu": round(ratio, 3), + }, + } + ] + + +def probe_autonomy_health( + root: Path, + *, + units: Sequence[str] = DEFAULT_AUTONOMY_UNITS, + systemctl_timeout: float = 2.5, + systemctl_runner: Optional[Callable[..., Any]] = None, + heyasa_status_url: Optional[str] = None, + heyasa_payload: Optional[Dict[str, Any]] = None, + heyasa_timeout: float = 1.5, + http_opener: Optional[Callable[..., Any]] = None, + load_fn: Optional[Callable[[], Tuple[float, float, float]]] = None, + cpu_count_fn: Optional[Callable[[], Optional[int]]] = None, +) -> List[Dict[str, Any]]: + """Run deterministic, read-only host & daemon autonomy stall probes.""" + if os.environ.get("MONAG_DISABLE_AUTONOMY_PROBES") == "1": + return [] + findings: List[Dict[str, Any]] = [] + findings.extend( + _probe_systemd_user_units( + root=root, + units=units, + timeout=systemctl_timeout, + runner=systemctl_runner, + ) + ) + findings.extend( + _probe_heyasa_daemon_status( + root=root, + status_url=heyasa_status_url, + status_payload=heyasa_payload, + timeout=heyasa_timeout, + opener=http_opener, + ) + ) + findings.extend( + _probe_host_load_saturation( + root=root, + load_fn=load_fn, + cpu_count_fn=cpu_count_fn, + ) + ) + findings.sort(key=anomaly_sort_key) + return findings + + def diagnose_fleet(root: Path, depth: int = 2, db: Optional[Any] = None, - use_cache: bool = True, state_dir: Optional[Path] = None) -> Dict[str, Any]: - """Scan workspace fleet for technical defects, governance drift, and ticket readiness.""" + use_cache: bool = True, state_dir: Optional[Path] = None, + include_autonomy: bool = True) -> Dict[str, Any]: + """Scan workspace fleet for operational autonomy stalls, technical defects, and ticket readiness.""" started = datetime.now(timezone.utc) repos: List[Path] = [] @@ -556,7 +1002,8 @@ def diagnose_fleet(root: Path, depth: int = 2, db: Optional[Any] = None, except Exception: database = None - all_anomalies: List[Dict[str, Any]] = [] + autonomy_anomalies: List[Dict[str, Any]] = probe_autonomy_health(root) if include_autonomy else [] + repo_anomalies: List[Dict[str, Any]] = [] cached_repos_count = 0 repo_fingerprints: Dict[str, Tuple[str, str]] = {} @@ -570,13 +1017,18 @@ def diagnose_fleet(root: Path, depth: int = 2, db: Optional[Any] = None, cached = autodiag_store.get_cached_diagnosis(database, repo_str, head_sha, dirty_digest) if cached is not None: cached_anom, _ = cached - all_anomalies.extend(cached_anom) + repo_anomalies.extend(cached_anom) cached_repos_count += 1 continue except Exception: pass - all_anomalies.extend(inspect_repository_anomalies(r)) + repo_anomalies.extend(inspect_repository_anomalies(r)) + + all_anomalies: List[Dict[str, Any]] = sorted( + [*autonomy_anomalies, *repo_anomalies], + key=anomaly_sort_key, + ) return { "schema": SCHEMA, @@ -584,6 +1036,7 @@ def diagnose_fleet(root: Path, depth: int = 2, db: Optional[Any] = None, "timestamp": started.isoformat(), "repositories_checked": len(repos), "repositories_cached": cached_repos_count, + "autonomy_anomalies_count": len(autonomy_anomalies), "anomalies_count": len(all_anomalies), "anomalies": all_anomalies, "_repo_fingerprints": repo_fingerprints, @@ -652,10 +1105,24 @@ def _parse_subllm_response(text: str, fallback_anomaly: Dict[str, Any]) -> Dict[ "GIT_CONFLICT_MARKERS": "Resolve merge conflicts, remove conflict markers, and verify syntax with git status and tests.", "BROKEN_VENV": "Recreate damaged virtual environment (.venv) and reinstall project dependencies.", "SYSTEM_LOG_ERROR": "Diagnose root cause of runtime system log error, apply code/configuration fix, and verify service stability with tests.", + "AUTONOMY_UNIT_FAILED": "Inspect failed systemd --user journal logs, repair unit configuration or runtime crash, and verify active service health.", + "AUTONOMY_DAEMON_INACTIVE": "Inspect heyasa.service user unit status and restart or re-enable autonomous orchestrator daemon.", + "AUTONOMY_ELIGIBLE_PROJECTS_ZERO": "Inspect Heyasa flowguard project_exclusions and unblock repository eligibility or lease gates.", + "AUTONOMY_ADMISSION_STALL": "Inspect Heyasa flowguard blocked_reasons and slot capacity to restore candidate admission.", + "AUTONOMY_VELOCITY_STALL": "Inspect stalled Heyasa execution pipeline, executor health, and open candidate backlog.", + "HOST_LOAD_SATURATION": "Inspect top CPU-consuming processes, runaway test suites, or duplicate agent loops causing host saturation.", } action = action_map.get(code, f"Resolve {code} in {target}.") ac1 = f"AC-01: {fallback_anomaly.get('summary', 'Anomaly is remediated')}." ac2 = "AC-02: Verification checks and automated test suite pass with exit code 0." + verify_cmd = "git status --porcelain && npm test || pytest -q" + if code in {"AUTONOMY_UNIT_FAILED", "AUTONOMY_DAEMON_INACTIVE"}: + unit_name = (fallback_anomaly.get("details") or {}).get("unit", "heyasa.service") + verify_cmd = f"systemctl --user is-active {unit_name}" + elif code in {"AUTONOMY_ELIGIBLE_PROJECTS_ZERO", "AUTONOMY_ADMISSION_STALL", "AUTONOMY_VELOCITY_STALL"}: + verify_cmd = "curl -fsS http://127.0.0.1:8765/v1/daemon/status || curl -fsS http://127.0.0.1:7080/v1/daemon/status" + elif code == "HOST_LOAD_SATURATION": + verify_cmd = "python3 -c 'import os; assert os.getloadavg()[0] <= 1.5 * (os.cpu_count() or 1)'" return { "title": f"[{target}] fix({code.lower().replace('_', '-')}): {fallback_anomaly.get('summary')}", @@ -664,7 +1131,7 @@ def _parse_subllm_response(text: str, fallback_anomaly: Dict[str, Any]) -> Dict[ "priority": TIER_PRIORITY_MAP.get(tier, "normal"), "action": action, "acceptance_criteria": [ac1, ac2], - "verification_command": "git status --porcelain && npm test || pytest -q", + "verification_command": verify_cmd, "satisfied_when": f"{code} condition is resolved and verified.", "labels": ["monag", "autodiagnosis", "koru-autonomous", f"tier:{tier}"], } @@ -809,6 +1276,10 @@ def synthesize_tickets_with_subllm(anomalies: Any, "target_repo": target, "tier": tier, "priority": priority, + "weight": int(anom.get("weight", 0) or 0), + "autonomy_probe": bool(anom.get("autonomy_probe", False)), + "code": code_str, + "severity": str(anom.get("severity") or "WARNING"), "labels": labels, "action": ticket_spec.get("action"), "acceptance_criteria": ticket_spec.get("acceptance_criteria", []), @@ -875,6 +1346,7 @@ def dispatch_tickets_to_planfile(tickets: List[Dict[str, Any]], root: Path, "dispatched": 0, "repositories_updated": [], "tickets_by_repo": {}, + "skipped_external_targets": [], "errors": [], } @@ -902,6 +1374,9 @@ def _load_sprint_yaml_cached(path_str: str, mtime_ns: int) -> Any: target_path = root if not target_path or not target_path.is_dir(): + if all(bool(t.get("autonomy_probe")) for t in repo_tickets): + results["skipped_external_targets"].append(target) + continue results["errors"].append(f"Target repository directory not found for: {target}") continue diff --git a/src/monag/cli.py b/src/monag/cli.py index ff885d8..2a8c625 100644 --- a/src/monag/cli.py +++ b/src/monag/cli.py @@ -269,6 +269,8 @@ def main(argv=None): doctor_parser = sub.add_parser('doctor', parents=[common_sub_parser], help='diagnose dependencies, worktrees, and process visibility') doctor_parser.add_argument('--fix', action='store_true', help='automatically prune orphaned worktrees, remove merged ticket branches, and run housekeeping') doctor_parser.add_argument('--safe-fix', dest='safe_fix', action='store_true', help='safely prune merged/clean worktrees and branches conforming to Wellmanifest Worktrees v5') + doctor_parser.add_argument('--timeout', '--budget-seconds', dest='doctor_timeout', type=float, default=60.0, + help='monotonic time budget in seconds for doctor diagnostics; returns partial structured output on deadline (default: 60.0)') wt_parser = sub.add_parser('worktrees', aliases=['wt'], parents=[common_sub_parser], help='audit and safely prune worktrees conforming to Wellmanifest Worktrees v5') wt_parser.add_argument('worktree_action', nargs='?', choices=['list', 'audit', 'prune'], default='list', @@ -463,10 +465,14 @@ def main(argv=None): help='target sprint for --feed-planfile (default: current)') autodiagnose_parser.add_argument('--emit-planfile', action='store_true', help='print synthesized tickets as Planfile JSON envelope') - summary_parser = sub.add_parser('summary', aliases=['day'], parents=[common_sub_parser], - help='aggregate daily execution summary: Planfile completed/queued tickets, GitHub PRs, and time estimates') + summary_parser = sub.add_parser('summary', parents=[common_sub_parser], + help='current portfolio and sprint queue snapshot with explicit source/scope/filter labels') summary_parser.add_argument('--no-github', dest='github', action='store_false', help='skip querying GitHub PR metrics for faster execution') + day_parser = sub.add_parser('day', parents=[common_sub_parser], + help='24h operational delta (merged/opened PRs, completed/failed agent runs, new blockers) with explicit source labels') + day_parser.add_argument('--no-github', dest='github', action='store_false', + help='skip querying GitHub PR metrics for faster execution') quality = sub.add_parser('quality', parents=[common_sub_parser], help='read-only semcod/regix quality gate for ONE repository ' '(--root must be a Git checkout, not a workspace); ' @@ -549,6 +555,8 @@ def main(argv=None): 'panel-interval must be >= 1 second, port-attempts must be >= 0') if args.mode == 'quality' and args.quality_timeout < 1: parser.error('quality-timeout must be >= 1 second') + if args.mode == 'doctor' and getattr(args, 'doctor_timeout', 60.0) <= 0: + parser.error('doctor --timeout must be > 0 seconds') stack = ExitStack() try: output_format = args.output @@ -585,7 +593,18 @@ def display_report(document): return int(report['drift']) if args.mode == 'doctor': fix = getattr(args, 'fix', False) or getattr(args, 'safe_fix', False) - data = diagnose(args.root.expanduser().resolve(), fix=fix) + doctor_timeout = getattr(args, 'doctor_timeout', 60.0) + progress_cb = None + if output_format != 'json' and sys.stderr.isatty(): + def progress_cb(phase_name, payload): + state = payload.get('state') + if state == 'started': + print(f'MONAG doctor: phase {phase_name}...', file=sys.stderr, flush=True) + elif state == 'timed_out': + print(f'MONAG doctor: phase {phase_name} reached time budget ({doctor_timeout}s); returning partial diagnostics.', + file=sys.stderr, flush=True) + data = diagnose(args.root.expanduser().resolve(), fix=fix, budget_seconds=doctor_timeout, + progress_callback=progress_cb) if output_format in {'terminal', 'markdown'}: display_report(presentation.doctor_markdown(data)) else: @@ -1075,6 +1094,8 @@ def display_report(document): hours=getattr(args, 'hours', 24), github=getattr(args, 'github', True), depth=args.depth, + state_dir=args.state_dir, + mode=args.mode, ) if output_format == 'json': print(json.dumps(data, ensure_ascii=False, indent=2)) diff --git a/src/monag/doctor.py b/src/monag/doctor.py index b5e84e0..cb8a4f5 100644 --- a/src/monag/doctor.py +++ b/src/monag/doctor.py @@ -2,11 +2,30 @@ import json import os from pathlib import Path +import queue import shutil import subprocess import sys +import threading +import time +from typing import Any, Callable from .monitor import command +DOCTOR_PHASES = ("environment", "ecosystem_tools", "repositories", "agent_storage") + + +def _remaining_budget(deadline: float | None, clock: Callable[[], float]) -> float | None: + if deadline is None: + return None + return deadline - clock() + + +def _bounded_cmd_timeout(deadline: float | None, clock: Callable[[], float], default: float = 10.0) -> float: + rem = _remaining_budget(deadline, clock) + if rem is None: + return default + return max(0.1, min(default, rem)) + def find_git_repositories(root: Path, max_depth: int = 2) -> list[Path]: """Find all git repositories in root or within subdirectories up to max_depth.""" @@ -34,9 +53,9 @@ def find_git_repositories(root: Path, max_depth: int = 2) -> list[Path]: return sorted(set(repos)) -def parse_worktrees_porcelain(repo: Path) -> list[dict]: +def parse_worktrees_porcelain(repo: Path, timeout: float = 10.0) -> list[dict]: """Parse git worktree list --porcelain output for a repository.""" - out, err = command(['git', '-C', str(repo), 'worktree', 'list', '--porcelain']) + out, err = command(['git', '-C', str(repo), 'worktree', 'list', '--porcelain'], timeout=max(0.1, timeout)) if err or not out: return [] @@ -69,30 +88,60 @@ def parse_worktrees_porcelain(repo: Path) -> list[dict]: return worktrees -def is_branch_merged(repo: Path, branch: str, target: str = 'main') -> bool: +def is_branch_merged( + repo: Path, + branch: str, + target: str = 'main', + resolved_target: str | None = None, + timeout: float = 10.0, +) -> bool: """Check if a branch is merged into target branch (or HEAD if target does not exist).""" - t_out, _ = command(['git', '-C', str(repo), 'rev-parse', '--verify', target]) - if not t_out.strip(): - t_out, _ = command(['git', '-C', str(repo), 'rev-parse', '--verify', 'master']) - target = 'master' if t_out.strip() else 'HEAD' + if resolved_target is not None: + effective_target = resolved_target + else: + t_out, _ = command(['git', '-C', str(repo), 'rev-parse', '--verify', target], timeout=max(0.1, timeout)) + if not t_out.strip(): + t_out, _ = command(['git', '-C', str(repo), 'rev-parse', '--verify', 'master'], timeout=max(0.1, timeout)) + effective_target = 'master' if t_out.strip() else 'HEAD' + else: + effective_target = target - code = subprocess.run( - ['git', '-C', str(repo), 'merge-base', '--is-ancestor', branch, target], - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL - ).returncode - return code == 0 + try: + code = subprocess.run( + ['git', '-C', str(repo), 'merge-base', '--is-ancestor', branch, effective_target], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + timeout=max(0.2, timeout), + ).returncode + return code == 0 + except (subprocess.SubprocessError, OSError): + return False + + +def audit_worktrees( + repo: Path, + leases: dict[str, dict[str, Any]] | None = None, + target_branch: str | None = None, + deadline: float | None = None, + clock: Callable[[], float] = time.monotonic, +) -> list[dict]: + """Audit worktrees of a repo, identifying secondary, stale, or merged worktrees.""" + rem = _remaining_budget(deadline, clock) + if rem is not None and rem <= 0.0: + return [] + raw_worktrees = parse_worktrees_porcelain(repo, timeout=_bounded_cmd_timeout(deadline, clock, 10.0)) + if not raw_worktrees: + return [] -def audit_worktrees(repo: Path) -> list[dict]: - """Audit worktrees of a repo, identifying secondary, stale, or merged worktrees.""" - raw_worktrees = parse_worktrees_porcelain(repo) - audited = [] + audited: list[dict] = [] try: repo_resolved = repo.resolve() except OSError: repo_resolved = repo + parsed_entries: list[tuple[dict, str, Path, bool]] = [] + has_secondary = False for wt in raw_worktrees: wt_path_str = wt.get('path', '') if not wt_path_str: @@ -104,6 +153,29 @@ def audit_worktrees(repo: Path) -> list[dict]: is_primary = True except OSError: pass + if not is_primary: + has_secondary = True + parsed_entries.append((wt, wt_path_str, wt_path, is_primary)) + + from . import worktrees + + repo_leases: dict[str, dict[str, Any]] = {} + resolved_target: str | None = target_branch + if has_secondary: + repo_leases = worktrees.get_repo_leases(repo) if leases is None else leases + if resolved_target is None: + resolved_target = ( + worktrees.resolve_default_target_branch( + repo, + timeout=_bounded_cmd_timeout(deadline, clock, 10.0), + ) + or "" + ) + + for wt, wt_path_str, wt_path, is_primary in parsed_entries: + rem = _remaining_budget(deadline, clock) + if rem is not None and rem <= 0.0: + break exists = wt_path.is_dir() branch = wt.get('branch', '') @@ -111,20 +183,29 @@ def audit_worktrees(repo: Path) -> list[dict]: dirty = False locked = bool(wt.get('locked', False)) has_active_lease = False + cmd_timeout = _bounded_cmd_timeout(deadline, clock, 10.0) - from . import worktrees if branch and not is_primary: - is_merged = worktrees.is_branch_merged(repo, branch) + is_merged = worktrees.is_branch_merged( + repo, + branch, + target_branch=resolved_target, + timeout=cmd_timeout, + ) if exists and not is_primary: - dirty, _ = worktrees.is_worktree_dirty(wt_path) + dirty, _ = worktrees.is_worktree_dirty(wt_path, timeout=cmd_timeout) if not is_primary: - leases = worktrees.get_repo_leases(repo) - lease = worktrees._resolve_worktree_lease(leases, wt_path, wt_path_str, branch) + lease = worktrees._resolve_worktree_lease(repo_leases, wt_path, wt_path_str, branch) if lease: has_active_lease, _ = worktrees.is_lease_active(lease) - is_stale = ((not exists) or wt.get('prunable', False) or (is_merged and not is_primary)) and not dirty and not locked and not has_active_lease + is_stale = ( + ((not exists) or wt.get('prunable', False) or (is_merged and not is_primary)) + and not dirty + and not locked + and not has_active_lease + ) audited.append({ 'repo': repo.name, @@ -144,15 +225,22 @@ def audit_worktrees(repo: Path) -> list[dict]: return audited -def audit_merged_branches(repo: Path) -> list[str]: +def audit_merged_branches( + repo: Path, + target_branch: str | None = None, + timeout: float = 10.0, +) -> list[str]: """Find local branches that are already merged into main/HEAD.""" - target = 'main' - t_out, _ = command(['git', '-C', str(repo), 'rev-parse', '--verify', target]) - if not t_out.strip(): - t_out, _ = command(['git', '-C', str(repo), 'rev-parse', '--verify', 'master']) - target = 'master' if t_out.strip() else 'HEAD' + if target_branch: + target = target_branch + else: + target = 'main' + t_out, _ = command(['git', '-C', str(repo), 'rev-parse', '--verify', target], timeout=max(0.1, timeout)) + if not t_out.strip(): + t_out, _ = command(['git', '-C', str(repo), 'rev-parse', '--verify', 'master'], timeout=max(0.1, timeout)) + target = 'master' if t_out.strip() else 'HEAD' - out, _ = command(['git', '-C', str(repo), 'branch', '--merged', target]) + out, _ = command(['git', '-C', str(repo), 'branch', '--merged', target], timeout=max(0.1, timeout)) if not out: return [] @@ -168,20 +256,26 @@ def audit_merged_branches(repo: Path) -> list[str]: return merged -def prune_and_remediate(repo: Path, stale_worktrees: list[dict], merged_branches: list[str]) -> dict: +def prune_and_remediate( + repo: Path, + stale_worktrees: list[dict], + merged_branches: list[str], + leases: dict[str, dict[str, Any]] | None = None, + target_branch: str | None = None, +) -> dict: """Remediate a repo by safely pruning worktrees and deleting merged ticket branches conforming to Wellmanifest v5.""" from . import worktrees res = worktrees.prune_worktrees_safe(repo, dry_run=False) deleted = list(res['deleted_branches']) active_branches = {w.get('branch') for w in parse_worktrees_porcelain(repo) if w.get('branch')} - leases = worktrees.get_repo_leases(repo) + repo_leases = worktrees.get_repo_leases(repo) if leases is None else leases for b in merged_branches: if b in deleted or b in active_branches: continue - lease = worktrees._resolve_worktree_lease(leases, Path(b), "", b) + lease = worktrees._resolve_worktree_lease(repo_leases, Path(b), "", b) if lease and worktrees.is_lease_active(lease)[0]: continue - if is_branch_merged(repo, b): + if is_branch_merged(repo, b, resolved_target=target_branch or None): _, err = command(['git', '-C', str(repo), 'branch', '-d', b]) if not err: deleted.append(b) @@ -192,16 +286,41 @@ def prune_and_remediate(repo: Path, stale_worktrees: list[dict], merged_branches } -def audit_agent_storage_health(home: Path | None = None) -> dict: +def audit_agent_storage_health( + home: Path | None = None, + deadline: float | None = None, + clock: Callable[[], float] = time.monotonic, +) -> dict: """Audit local coding agent session storage integrity and panic history. Delegates to semcod/uncrash diagnostics when available, with a lightweight - standalone SQLite quick_check fallback. + standalone SQLite quick_check fallback bounded by deadline. """ + rem = _remaining_budget(deadline, clock) + if rem is not None and rem <= 0.0: + return {'status': 'SKIPPED_TIMEOUT', 'corrupted_sessions': [], 'detected_panics': [], 'timed_out': True} + target_home = Path(home or Path.home()) try: from uncrash.diagnostics import check_agent_storage_health - return check_agent_storage_health(target_home) + if deadline is None: + return check_agent_storage_health(target_home) + outcome: "queue.Queue[tuple[bool, Any]]" = queue.Queue(maxsize=1) + + def _run_uncrash() -> None: + try: + outcome.put((True, check_agent_storage_health(target_home))) + except Exception as exc: + outcome.put((False, exc)) + + worker = threading.Thread(target=_run_uncrash, name="monag-doctor-uncrash", daemon=True) + worker.start() + try: + ok, val = outcome.get(timeout=max(0.05, rem)) + if ok and isinstance(val, dict): + return val + except queue.Empty: + return {'status': 'PARTIAL_TIMEOUT', 'corrupted_sessions': [], 'detected_panics': [], 'timed_out': True} except ImportError: pass @@ -211,9 +330,9 @@ def audit_agent_storage_health(home: Path | None = None) -> dict: if not agy_root.is_dir(): return {'status': 'NOT_PRESENT', 'corrupted_sessions': [], 'detected_panics': []} - def _quick_check_db(db_path: Path) -> dict | None: + def _quick_check_db(db_path: Path, db_timeout: float) -> dict | None: try: - conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True, timeout=1.0) + conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True, timeout=max(0.1, min(1.0, db_timeout))) res = conn.execute("PRAGMA quick_check;").fetchone() conn.close() if not res or res[0] != 'ok': @@ -223,17 +342,26 @@ def _quick_check_db(db_path: Path) -> dict | None: return None corrupt = [] + partial_timeout = False conv_dir = agy_root / 'conversations' if conv_dir.is_dir(): for db in sorted(conv_dir.glob('*.db')): - item = _quick_check_db(db) + rem = _remaining_budget(deadline, clock) + if rem is not None and rem <= 0.0: + partial_timeout = True + break + item = _quick_check_db(db, rem if rem is not None else 1.0) if item is not None: corrupt.append(item) panics = [] log_dir = agy_root / 'log' - if log_dir.is_dir(): + if not partial_timeout and log_dir.is_dir(): for lf in sorted(log_dir.glob('*.log'), key=lambda p: p.stat().st_mtime if p.exists() else 0, reverse=True)[:10]: + rem = _remaining_budget(deadline, clock) + if rem is not None and rem <= 0.0: + partial_timeout = True + break try: txt = lf.read_text(encoding='utf-8', errors='replace')[:256 * 1024] if 'panic' in txt.lower() or 'disk image is malformed' in txt.lower(): @@ -241,69 +369,232 @@ def _quick_check_db(db_path: Path) -> dict | None: except (OSError, UnicodeError): continue - status = 'CORRUPTED' if corrupt else ('DEGRADED' if panics else 'HEALTHY') - return {'status': status, 'corrupted_sessions': corrupt, 'detected_panics': panics} - - -def diagnose(root: Path, fix: bool = False, home: Path | None = None) -> dict: - git, git_error = command(['git', '--version']) + status = 'CORRUPTED' if corrupt else ('DEGRADED' if panics else ('PARTIAL_TIMEOUT' if partial_timeout else 'HEALTHY')) + out_health = {'status': status, 'corrupted_sessions': corrupt, 'detected_panics': panics} + if partial_timeout: + out_health['timed_out'] = True + return out_health + + +def diagnose( + root: Path, + fix: bool = False, + home: Path | None = None, + budget_seconds: float | None = 60.0, + phase_budgets: dict[str, float] | None = None, + progress_callback: Callable[[str, dict[str, Any]], None] | None = None, + clock: Callable[[], float] = time.monotonic, +) -> dict: + started = clock() + overall_deadline = (started + max(0.0, float(budget_seconds))) if budget_seconds is not None else None + configured_phase_budgets = dict(phase_budgets or {}) + + completed_phases: list[str] = [] + timed_out_phases: list[str] = [] + skipped_phases: list[str] = [] + skipped_repositories: list[str] = [] + phase_durations_seconds: dict[str, float] = {} + timed_out = False + + def _phase_deadline(phase_name: str, phase_start: float) -> float | None: + pb = configured_phase_budgets.get(phase_name) + p_dead = (phase_start + max(0.0, float(pb))) if pb is not None else None + if overall_deadline is not None and p_dead is not None: + return min(overall_deadline, p_dead) + return p_dead if p_dead is not None else overall_deadline + + def _emit_progress(phase_name: str, **payload: Any) -> None: + if progress_callback is not None: + try: + progress_callback(phase_name, payload) + except Exception: + pass + + # Phase 1: environment + errors: list[str] = [] + git = "" + git_error = "" + namespace, process_count = 'unavailable', 0 own = Path('/proc/self') - try: - namespace = os.readlink(own / 'ns/pid') - process_count = sum(p.name.isdigit() for p in Path('/proc').iterdir()) - except OSError: - namespace, process_count = 'unavailable', 0 - errors = [] - if git_error: - errors.append(git_error) - if not root.is_dir(): - errors.append('workspace root does not exist') - if not (own / 'stat').exists(): - errors.append('Linux /proc process data is unavailable') - - # Audit ecosystem CLI tools - from .ecosystem import audit_ecosystem_tools - eco_capabilities = audit_ecosystem_tools() - tools = { + + p1_start = clock() + if overall_deadline is not None and p1_start >= overall_deadline: + timed_out = True + skipped_phases.append('environment') + else: + p1_dead = _phase_deadline('environment', p1_start) + _emit_progress('environment', state='started') + git, git_error = command(['git', '--version'], timeout=_bounded_cmd_timeout(p1_dead, clock, 5.0)) + try: + namespace = os.readlink(own / 'ns/pid') + process_count = sum(p.name.isdigit() for p in Path('/proc').iterdir()) + except OSError: + namespace, process_count = 'unavailable', 0 + if git_error: + errors.append(git_error) + if not root.is_dir(): + errors.append('workspace root does not exist') + if not (own / 'stat').exists(): + errors.append('Linux /proc process data is unavailable') + p1_end = clock() + phase_durations_seconds['environment'] = round(max(0.0, p1_end - p1_start), 4) + if p1_dead is not None and p1_end > p1_dead: + timed_out = True + timed_out_phases.append('environment') + else: + completed_phases.append('environment') + _emit_progress('environment', state='completed', duration=phase_durations_seconds['environment']) + + # Phase 2: ecosystem_tools + tools: dict[str, bool] = { 'git': bool(git and not git_error), 'gh': bool(shutil.which('gh')), - 'diagit': bool(eco_capabilities.get('diagit') and eco_capabilities['diagit'].available), - 'redup': bool(eco_capabilities.get('redup') and eco_capabilities['redup'].available), - 'prefact': bool(eco_capabilities.get('prefact') and eco_capabilities['prefact'].available), + 'diagit': False, + 'redup': False, + 'prefact': False, } - # Populate other discovered ecosystem tools - for tool_name, cap in eco_capabilities.items(): - if cap.available and tool_name not in tools: - tools[tool_name] = True - - # Audit repositories and worktrees - repos = find_git_repositories(root) - all_worktrees = [] - stale_worktrees = [] - all_merged_branches = [] + p2_start = clock() + if overall_deadline is not None and p2_start >= overall_deadline: + timed_out = True + skipped_phases.append('ecosystem_tools') + else: + p2_dead = _phase_deadline('ecosystem_tools', p2_start) + _emit_progress('ecosystem_tools', state='started') + from .ecosystem import audit_ecosystem_tools + eco_capabilities = audit_ecosystem_tools() + tools['diagit'] = bool(eco_capabilities.get('diagit') and eco_capabilities['diagit'].available) + tools['redup'] = bool(eco_capabilities.get('redup') and eco_capabilities['redup'].available) + tools['prefact'] = bool(eco_capabilities.get('prefact') and eco_capabilities['prefact'].available) + for tool_name, cap in eco_capabilities.items(): + if cap.available and tool_name not in tools: + tools[tool_name] = True + p2_end = clock() + phase_durations_seconds['ecosystem_tools'] = round(max(0.0, p2_end - p2_start), 4) + if p2_dead is not None and p2_end > p2_dead: + timed_out = True + timed_out_phases.append('ecosystem_tools') + else: + completed_phases.append('ecosystem_tools') + _emit_progress('ecosystem_tools', state='completed', duration=phase_durations_seconds['ecosystem_tools']) + + # Phase 3: repositories + repos: list[Path] = [] + repos_audited_count = 0 + all_worktrees: list[dict] = [] + stale_worktrees: list[dict] = [] + all_merged_branches: list[dict] = [] remediation_summary: dict = {'worktrees_pruned': [], 'branches_deleted': []} - for repo in repos: - try: - wts = audit_worktrees(repo) - all_worktrees.extend(wts) - repo_stale = [w for w in wts if w.get('stale')] - stale_worktrees.extend(repo_stale) - - merged_b = audit_merged_branches(repo) - for b in merged_b: - all_merged_branches.append({'repo': repo.name, 'branch': b}) - - if fix and (repo_stale or merged_b): - res = prune_and_remediate(repo, repo_stale, merged_b) - remediation_summary['worktrees_pruned'].extend(res['worktrees_pruned']) - remediation_summary['branches_deleted'].extend(res['branches_deleted']) - except Exception as e: - errors.append(f"Error auditing {repo.name}: {e}") + p3_start = clock() + if overall_deadline is not None and p3_start >= overall_deadline: + timed_out = True + skipped_phases.append('repositories') + if root.is_dir(): + repos = find_git_repositories(root) + skipped_repositories = [r.name for r in repos] + else: + p3_dead = _phase_deadline('repositories', p3_start) + _emit_progress('repositories', state='started') + repos = find_git_repositories(root) + repo_phase_timed_out = False + from . import worktrees as wt_mod + + for idx, repo in enumerate(repos): + rem = _remaining_budget(p3_dead, clock) + if rem is not None and rem <= 0.0: + timed_out = True + repo_phase_timed_out = True + skipped_repositories.extend(r.name for r in repos[idx:]) + break + + _emit_progress('repositories', state='repo', repo=repo.name, index=idx + 1, total=len(repos)) + try: + cmd_timeout = _bounded_cmd_timeout(p3_dead, clock, 10.0) + resolved_target = wt_mod.resolve_default_target_branch(repo, timeout=cmd_timeout) or "" + repo_leases: dict[str, dict[str, Any]] | None = None + + wts = audit_worktrees( + repo, + leases=repo_leases, + target_branch=resolved_target, + deadline=p3_dead, + clock=clock, + ) + all_worktrees.extend(wts) + repo_stale = [w for w in wts if w.get('stale')] + stale_worktrees.extend(repo_stale) + + rem_after_wt = _remaining_budget(p3_dead, clock) + if rem_after_wt is not None and rem_after_wt <= 0.0: + timed_out = True + repo_phase_timed_out = True + skipped_repositories.extend(r.name for r in repos[idx + 1:]) + repos_audited_count += 1 + break + + merged_b = audit_merged_branches( + repo, + target_branch=resolved_target or None, + timeout=_bounded_cmd_timeout(p3_dead, clock, 10.0), + ) + for b in merged_b: + all_merged_branches.append({'repo': repo.name, 'branch': b}) + + if fix and (repo_stale or merged_b): + res = prune_and_remediate( + repo, + repo_stale, + merged_b, + leases=repo_leases, + target_branch=resolved_target or None, + ) + remediation_summary['worktrees_pruned'].extend(res['worktrees_pruned']) + remediation_summary['branches_deleted'].extend(res['branches_deleted']) + + repos_audited_count += 1 + except Exception as e: + errors.append(f"Error auditing {repo.name}: {e}") + + p3_end = clock() + phase_durations_seconds['repositories'] = round(max(0.0, p3_end - p3_start), 4) + if repo_phase_timed_out or (p3_dead is not None and p3_end > p3_dead): + timed_out = True + if 'repositories' not in timed_out_phases: + timed_out_phases.append('repositories') + else: + completed_phases.append('repositories') + _emit_progress( + 'repositories', + state='timed_out' if repo_phase_timed_out else 'completed', + audited=repos_audited_count, + total=len(repos), + duration=phase_durations_seconds['repositories'], + ) + + # Phase 4: agent_storage + recommendations: list[str] = [] + agent_health: dict[str, Any] = {'status': 'SKIPPED_TIMEOUT', 'corrupted_sessions': [], 'detected_panics': []} + p4_start = clock() + if overall_deadline is not None and p4_start >= overall_deadline: + timed_out = True + skipped_phases.append('agent_storage') + else: + if 'agent_storage' not in configured_phase_budgets and overall_deadline is not None: + rem_overall = max(0.0, overall_deadline - p4_start) + p4_dead: float | None = p4_start + min(rem_overall, 10.0) + else: + p4_dead = _phase_deadline('agent_storage', p4_start) + _emit_progress('agent_storage', state='started') + agent_health = audit_agent_storage_health(home=home, deadline=p4_dead, clock=clock) + p4_end = clock() + phase_durations_seconds['agent_storage'] = round(max(0.0, p4_end - p4_start), 4) + if agent_health.get('timed_out') or (overall_deadline is not None and p4_end > overall_deadline): + timed_out = True + timed_out_phases.append('agent_storage') + else: + completed_phases.append('agent_storage') + _emit_progress('agent_storage', state='completed', duration=phase_durations_seconds['agent_storage']) - recommendations = [] - # Audit agent storage health - agent_health = audit_agent_storage_health(home=home) if agent_health.get('corrupted_sessions'): recommendations.append( f"Detected {len(agent_health['corrupted_sessions'])} corrupted agent session database(s) in ~/.gemini/antigravity-cli. Run 'uncrash' or restore via SQLite dump (wellmanifest/session-recovery)." @@ -313,10 +604,17 @@ def diagnose(root: Path, fix: bool = False, home: Path | None = None) -> dict: f"Detected {len(agent_health['detected_panics'])} recent agent panic/crash log(s). Inspect logs or run 'uncrash diagnose'." ) + if timed_out: + recommendations.append( + f"Doctor reached time budget ({budget_seconds}s); partial diagnostics returned " + f"(timed_out_phases={timed_out_phases}, skipped_repositories={len(skipped_repositories)}). " + "Pass a larger --timeout for an exhaustive scan." + ) + if not fix: if stale_worktrees or all_merged_branches: recommendations.append( - f"Found {len(stale_worktrees)} stale worktrees and {len(all_merged_branches)} merged branches across {len(repos)} repositories. Run 'monag doctor --fix' to prune them." + f"Found {len(stale_worktrees)} stale worktrees and {len(all_merged_branches)} merged branches across {repos_audited_count} repositories. Run 'monag doctor --fix' to prune them." ) else: recommendations.append( @@ -338,6 +636,7 @@ def diagnose(root: Path, fix: bool = False, home: Path | None = None) -> dict: if tools.get('uncrash'): recommendations.append("Uncrash is available for workspace snapshots and agent session disaster recovery ('uncrash diagnose').") + elapsed = round(max(0.0, clock() - started), 4) result = { 'python': sys.version.split()[0], 'runtime_dependencies': 'rich>=14,<15 (terminal Markdown); PyYAML>=6,<7 (Planfile backlog)', @@ -350,11 +649,20 @@ def diagnose(root: Path, fix: bool = False, home: Path | None = None) -> dict: 'visible_processes': process_count, 'uid': os.getuid(), 'root': str(root), - 'repositories_checked': len(repos), + 'repositories_discovered': len(repos), + 'repositories_checked': repos_audited_count, 'worktrees_audited': len([w for w in all_worktrees if not w.get('is_primary')]), 'stale_worktrees_detected': len(stale_worktrees), 'merged_branches_detected': len(all_merged_branches), 'fix_mode': fix, + 'budget_seconds': budget_seconds, + 'elapsed_seconds': elapsed, + 'timed_out': timed_out, + 'completed_phases': completed_phases, + 'timed_out_phases': timed_out_phases, + 'skipped_phases': skipped_phases, + 'skipped_repositories': skipped_repositories, + 'phase_durations_seconds': phase_durations_seconds, 'errors': errors, 'coverage': [ 'Only processes visible in this PID namespace can be observed.', diff --git a/src/monag/summary.py b/src/monag/summary.py index c2ca96b..fc24b92 100644 --- a/src/monag/summary.py +++ b/src/monag/summary.py @@ -1,12 +1,13 @@ -"""Daily execution summary for Planfile, Koru, and GitHub Pull Requests. +"""Daily 24h operational delta (`day`) and portfolio/queue snapshot (`summary`). -Aggregates completed work, active queues, GitHub PR metrics, and time estimates -across the fleet or a single project. +Aggregates completed work, active queues, local agent task runs, active leases, +GitHub PR metrics, and explicit source/scope/filter labels across a workspace. """ from __future__ import annotations -from datetime import date, datetime, timezone +from datetime import date, datetime, timedelta, timezone import json +import os from pathlib import Path import shutil import subprocess @@ -164,16 +165,254 @@ def gather_planfile_daily(root: Path, target_date: Optional[date] = None, depth: } +def _labeled_count(value: int, source: str, scope: str, filter_expr: str) -> Dict[str, Any]: + """Attach explicit source, scope, and filter provenance to a reported count.""" + return { + "value": int(value), + "source": source, + "scope": scope, + "filter": filter_expr, + } + + +def _discover_sprint_dirs(root: Path, depth: int = 2) -> List[Path]: + sprint_dirs: List[Path] = [] + if (root / ".planfile" / "sprints").is_dir(): + sprint_dirs.append(root / ".planfile" / "sprints") + if depth >= 1: + sprint_dirs.extend(root.glob("*/.planfile/sprints")) + if depth >= 2: + sprint_dirs.extend(root.glob("*/*/.planfile/sprints")) + return sorted(set(sprint_dirs)) + + +def _scan_planfile_portfolio_and_blockers( + root: Path, + target_date: Optional[date] = None, + hours: int = 24, + depth: int = 2, + now: Optional[datetime] = None, +) -> Dict[str, Any]: + """Collect current sprint status breakdown and newly blocked tasks without mutating gather_planfile_daily.""" + target_date = target_date or date.today() + today_str = target_date.strftime("%Y-%m-%d") + ref_now = now or datetime.now(timezone.utc) + cutoff = ref_now - timedelta(hours=max(1, int(hours))) + sprint_dirs = _discover_sprint_dirs(root, depth=depth) + + open_or_todo = 0 + in_progress = 0 + planned = 0 + blocked_total = 0 + done_in_current = 0 + canceled_or_other = 0 + all_entries = 0 + new_blockers: List[Dict[str, Any]] = [] + + def _is_within_window(ts_raw: Any) -> bool: + if not ts_raw: + return False + ts_str = str(ts_raw).strip() + if ts_str[:10] == today_str: + return True + try: + dt = datetime.fromisoformat(ts_str.replace("Z", "+00:00")) + if dt.tzinfo is None: + dt = dt.replace(tzinfo=timezone.utc) + return dt >= cutoff + except ValueError: + return False + + for sprints_dir in sprint_dirs: + try: + repo_rel = str(sprints_dir.parent.parent.relative_to(root)) if sprints_dir.parent.parent != root else root.name + except Exception: + repo_rel = sprints_dir.parent.parent.name + + curr_fast = sprints_dir / "current.yaml.fast.json" + curr_yaml = sprints_dir / "current.yaml" + records: List[tuple[str, Dict[str, Any]]] = [] + if curr_fast.is_file(): + try: + data = json.loads(curr_fast.read_text(encoding="utf-8")) + tickets = data.get("data", {}).get("sprint", {}).get("tickets", {}) + if isinstance(tickets, dict): + records = [(str(tid), t) for tid, t in tickets.items() if isinstance(t, dict)] + except Exception: + records = [] + elif curr_yaml.is_file(): + try: + data = _load_yaml_cached(str(curr_yaml), curr_yaml.stat().st_mtime_ns) + if isinstance(data, dict): + sprint_tickets = (data.get("sprint") or {}).get("tickets") if isinstance(data.get("sprint"), dict) else None + if isinstance(sprint_tickets, dict) and sprint_tickets: + records = [(str(tid), t) for tid, t in sprint_tickets.items() if isinstance(t, dict)] + elif isinstance(data.get("tasks"), list): + records = [(str(t.get("id", "")), t) for t in data["tasks"] if isinstance(t, dict)] + except Exception: + records = [] + + for tid, t in records: + all_entries += 1 + status = str(t.get("status", "open")).lower() + title = t.get("name") or t.get("title") or str(t.get("description", ""))[:80] + updated_raw = t.get("updated_at") or t.get("created_at") or "" + if status in ("todo", "open", "ready"): + open_or_todo += 1 + elif status == "in_progress": + in_progress += 1 + elif status == "planned": + planned += 1 + elif status == "blocked": + blocked_total += 1 + if _is_within_window(updated_raw): + new_blockers.append({ + "repo": repo_rel, + "id": tid, + "title": title, + "updated_at": str(updated_raw), + "reason": str(t.get("blocked_reason") or t.get("reason") or "status=blocked"), + }) + elif status in ("done", "completed"): + done_in_current += 1 + else: + canceled_or_other += 1 + + return { + "repositories_with_planfile": len(sprint_dirs), + "open_or_todo_count": open_or_todo, + "in_progress_count": in_progress, + "planned_count": planned, + "blocked_total_count": blocked_total, + "done_in_current_count": done_in_current, + "canceled_or_other_count": canceled_or_other, + "all_current_sprint_entries": all_entries, + "new_blockers_count": len(new_blockers), + "new_blockers": new_blockers, + } + + +def _scan_agent_runs_window( + root: Path, + state_dir: Optional[Path] = None, + hours: int = 24, + now_epoch: Optional[float] = None, +) -> Dict[str, Any]: + """Scan local /tasks/*.json records updated within the last `hours` hours.""" + resolved_state = Path( + state_dir + or os.environ.get("MONAG_STATE_DIR") + or (Path.home() / ".local" / "state" / "monag") + ) + tasks_dir = resolved_state / "tasks" + ref_epoch = now_epoch if now_epoch is not None else datetime.now(timezone.utc).timestamp() + cutoff_epoch = ref_epoch - float(max(1, hours)) * 3600.0 + + completed = 0 + failed = 0 + running = 0 + samples: List[Dict[str, Any]] = [] + + if tasks_dir.is_dir(): + try: + root_resolved = root.resolve() + except OSError: + root_resolved = root + + for tf in sorted(tasks_dir.glob("*.json")): + try: + row = json.loads(tf.read_text(encoding="utf-8")) + except (OSError, ValueError): + continue + if not isinstance(row, dict): + continue + updated = row.get("updated") + if not isinstance(updated, (int, float)) or float(updated) < cutoff_epoch: + continue + cwd_str = str(row.get("cwd") or "") + if cwd_str: + try: + cwd_p = Path(cwd_str).resolve() + if cwd_p != root_resolved and root_resolved not in cwd_p.parents: + continue + except OSError: + pass + status = str(row.get("status") or row.get("outcome") or "").lower() + if status in ("completed", "done", "ok", "success"): + completed += 1 + elif status in ("failed", "error", "stale"): + failed += 1 + elif status == "running": + running += 1 + else: + continue + samples.append({ + "id": tf.stem, + "status": status, + "task": str(row.get("task") or "")[:100], + "ticket": row.get("ticket"), + "cwd": cwd_str, + "updated": float(updated), + }) + + samples.sort(key=lambda item: item["updated"], reverse=True) + return { + "state_tasks_dir": str(tasks_dir), + "completed_runs_count": completed, + "failed_runs_count": failed, + "running_runs_count": running, + "total_runs_in_window": completed + failed + running, + "recent_runs": samples[:10], + } + + +def _scan_active_leases_snapshot(root: Path, depth: int = 2) -> Dict[str, Any]: + """Count active vs terminal .subactor/leases/*.json records across workspace repositories.""" + from . import worktrees + + lease_dirs: List[Path] = [] + if (root / ".subactor" / "leases").is_dir(): + lease_dirs.append(root / ".subactor" / "leases") + if depth >= 1: + lease_dirs.extend(root.glob("*/.subactor/leases")) + if depth >= 2: + lease_dirs.extend(root.glob("*/*/.subactor/leases")) + lease_dirs = sorted(set(lease_dirs)) + + active_count = 0 + terminal_count = 0 + for ldir in lease_dirs: + for lf in sorted(ldir.glob("*.json")): + try: + parsed = json.loads(lf.read_text(encoding="utf-8")) + data = parsed if isinstance(parsed, dict) else {"schema": "malformed"} + except (OSError, ValueError): + data = {"schema": "unreadable"} + is_act, _ = worktrees.is_lease_active(data) + if is_act: + active_count += 1 + else: + terminal_count += 1 + + return { + "repositories_with_leases": len(lease_dirs), + "active_leases_count": active_count, + "terminal_leases_count": terminal_count, + } + + def query_github_pr_metrics(target_date: Optional[date] = None, timeout: int = 15) -> Dict[str, Any]: - """Fast query of open and merged Pull Requests using gh search prs.""" + """Fast query of open, newly opened, and merged Pull Requests using gh search prs.""" target_date = target_date or date.today() today_str = target_date.strftime("%Y-%m-%d") - result = { + result: Dict[str, Any] = { "open_prs_count": 0, "merged_prs_count": 0, + "opened_prs_count": 0, "open_prs": [], "merged_prs": [], + "opened_prs": [], } gh_bin = shutil.which("gh") @@ -181,7 +420,7 @@ def query_github_pr_metrics(target_date: Optional[date] = None, timeout: int = 1 return result try: - # 1. Open PRs + # 1. Open PRs (current queue snapshot) proc_open = subprocess.run( [gh_bin, "search", "prs", "--author", "@me", "--state", "open", "--json", "number,title,repository,url", "--limit", "50"], @@ -203,7 +442,7 @@ def query_github_pr_metrics(target_date: Optional[date] = None, timeout: int = 1 pass try: - # 2. Merged PRs today + # 2. Merged PRs in window (>= target_date) proc_merged = subprocess.run( [gh_bin, "search", "prs", "--author", "@me", "--state", "closed", "--merged", "--merged-at", f">={today_str}", "--json", "number,title,repository,url", "--limit", "100"], @@ -224,62 +463,442 @@ def query_github_pr_metrics(target_date: Optional[date] = None, timeout: int = 1 except Exception: pass + try: + # 3. Newly opened PRs in window (>= target_date) + proc_opened = subprocess.run( + [gh_bin, "search", "prs", "--author", "@me", + "--created", f">={today_str}", "--json", "number,title,repository,url,state", "--limit", "100"], + capture_output=True, text=True, timeout=timeout + ) + if proc_opened.returncode == 0 and proc_opened.stdout.strip(): + raw_opened = json.loads(proc_opened.stdout) + result["opened_prs_count"] = len(raw_opened) + result["opened_prs"] = [ + { + "repo": p.get("repository", {}).get("nameWithOwner", ""), + "number": p.get("number"), + "title": p.get("title"), + "state": p.get("state"), + "url": p.get("url"), + } + for p in raw_opened + ] + except Exception: + pass + return result -def gather_summary(root: Path, hours: int = 24, github: bool = True, - target_date: Optional[date] = None, depth: int = 2) -> Dict[str, Any]: - """Compile comprehensive daily summary spanning Planfile, Koru, and GitHub PRs.""" +def gather_summary( + root: Path, + hours: int = 24, + github: bool = True, + target_date: Optional[date] = None, + depth: int = 2, + state_dir: Optional[Path] = None, + mode: str = "summary", +) -> Dict[str, Any]: + """Compile either current portfolio/queue snapshot (`summary`) or 24h operational delta (`day`).""" + normalized_mode = "day" if str(mode).lower() == "day" else "summary" started = datetime.now(timezone.utc) planfile_data = gather_planfile_daily(root, target_date=target_date, depth=depth) + day_str = planfile_data["date"] + pf_extended = _scan_planfile_portfolio_and_blockers( + root, + target_date=target_date, + hours=int(hours), + depth=depth, + now=started, + ) pr_summary: Dict[str, Any] = { "open_prs_count": 0, "merged_prs_count": 0, + "opened_prs_count": 0, "open_prs": [], "merged_prs": [], + "opened_prs": [], } - if github: - pr_summary = query_github_pr_metrics(target_date=target_date) + queried_prs = query_github_pr_metrics(target_date=target_date) + pr_summary.update(queried_prs) + pr_summary.setdefault("opened_prs_count", len(pr_summary.get("opened_prs", []))) + pr_summary.setdefault("opened_prs", []) + + scope_str = f"workspace:{root} (depth<={depth})" + gh_scope = "github:author=@me" if github else "github:disabled(--no-github)" + + if normalized_mode == "day": + agent_runs = _scan_agent_runs_window(root, state_dir=state_dir, hours=int(hours)) + counts_labeled = { + "planfile_completed_24h": _labeled_count( + planfile_data["completed_count"], + source=".planfile/sprints/{history-YYYY-MM-DD,current}.yaml(.fast.json)", + scope=scope_str, + filter_expr=f"history-{day_str} U (current.status in {{done,completed}} & date=={day_str})", + ), + "planfile_new_blockers_24h": _labeled_count( + pf_extended["new_blockers_count"], + source=".planfile/sprints/current.yaml(.fast.json)", + scope=scope_str, + filter_expr=f"status==blocked & updated_within_last_{int(hours)}h", + ), + "agent_runs_completed_24h": _labeled_count( + agent_runs["completed_runs_count"], + source=f"{agent_runs['state_tasks_dir']}/*.json", + scope=scope_str, + filter_expr=f"status in {{completed,done,ok,success}} & updated_within_last_{int(hours)}h", + ), + "agent_runs_failed_24h": _labeled_count( + agent_runs["failed_runs_count"], + source=f"{agent_runs['state_tasks_dir']}/*.json", + scope=scope_str, + filter_expr=f"status in {{failed,error,stale}} & updated_within_last_{int(hours)}h", + ), + "github_merged_prs_24h": _labeled_count( + pr_summary.get("merged_prs_count", 0), + source="gh search prs", + scope=gh_scope, + filter_expr=f"author:@me state:closed is:merged merged:>={day_str}", + ), + "github_opened_prs_24h": _labeled_count( + pr_summary.get("opened_prs_count", 0), + source="gh search prs", + scope=gh_scope, + filter_expr=f"author:@me created:>={day_str}", + ), + } + return { + "schema": SCHEMA, + "mode": "day", + "report_kind": "day_24h_delta", + "root": str(root), + "hours": hours, + "depth": depth, + "timestamp": started.isoformat(), + "date": day_str, + "delta_24h": { + "window_hours": hours, + "completed_tasks_count": planfile_data["completed_count"], + "completed_tickets": planfile_data["completed_tickets"], + "new_blockers_count": pf_extended["new_blockers_count"], + "new_blockers": pf_extended["new_blockers"][:20], + "agent_runs_completed": agent_runs["completed_runs_count"], + "agent_runs_failed": agent_runs["failed_runs_count"], + "agent_runs_running": agent_runs["running_runs_count"], + "recent_agent_runs": agent_runs["recent_runs"], + "merged_prs_count": pr_summary.get("merged_prs_count", 0), + "merged_prs": pr_summary.get("merged_prs", []), + "opened_prs_count": pr_summary.get("opened_prs_count", 0), + "opened_prs": pr_summary.get("opened_prs", []), + }, + "planfile": { + "completed_today": planfile_data["completed_count"], + "new_blockers_24h": pf_extended["new_blockers_count"], + "completed_tickets": planfile_data["completed_tickets"], + "new_blockers": pf_extended["new_blockers"][:20], + }, + "agent_runs": agent_runs, + "github": { + "merged_prs_count": pr_summary.get("merged_prs_count", 0), + "opened_prs_count": pr_summary.get("opened_prs_count", 0), + "merged_prs": pr_summary.get("merged_prs", []), + "opened_prs": pr_summary.get("opened_prs", []), + }, + "counts_labeled": counts_labeled, + } + + leases_snap = _scan_active_leases_snapshot(root, depth=depth) + counts_labeled = { + "planfile_queued_actionable": _labeled_count( + planfile_data["queued_count"], + source=".planfile/sprints/current.yaml(.fast.json)", + scope=scope_str, + filter_expr="status in {todo,open,in_progress,planned} (current sprint only; excludes blocked/done/history)", + ), + "planfile_in_progress": _labeled_count( + pf_extended["in_progress_count"], + source=".planfile/sprints/current.yaml(.fast.json)", + scope=scope_str, + filter_expr="status == in_progress (current sprint)", + ), + "planfile_blocked_total": _labeled_count( + pf_extended["blocked_total_count"], + source=".planfile/sprints/current.yaml(.fast.json)", + scope=scope_str, + filter_expr="status == blocked (current sprint)", + ), + "planfile_current_sprint_all_entries": _labeled_count( + pf_extended["all_current_sprint_entries"], + source=".planfile/sprints/current.yaml(.fast.json)", + scope=scope_str, + filter_expr="all statuses in current sprint (excludes archived history-*.yaml)", + ), + "planfile_completed_today": _labeled_count( + planfile_data["completed_count"], + source=".planfile/sprints/{history-YYYY-MM-DD,current}.yaml(.fast.json)", + scope=scope_str, + filter_expr=f"history-{day_str} U (current.status in {{done,completed}} & date=={day_str})", + ), + "subactor_active_leases": _labeled_count( + leases_snap["active_leases_count"], + source=".subactor/leases/*.json", + scope=scope_str, + filter_expr="phase not in {released,merged,closed,cancelled}", + ), + "github_open_prs_queue": _labeled_count( + pr_summary.get("open_prs_count", 0), + source="gh search prs", + scope=gh_scope, + filter_expr="author:@me state:open (limit<=50)", + ), + "github_merged_prs_today": _labeled_count( + pr_summary.get("merged_prs_count", 0), + source="gh search prs", + scope=gh_scope, + filter_expr=f"author:@me state:closed is:merged merged:>={day_str}", + ), + } return { "schema": SCHEMA, + "mode": "summary", + "report_kind": "portfolio_queue_snapshot", "root": str(root), "hours": hours, "depth": depth, "timestamp": started.isoformat(), - "date": planfile_data["date"], + "date": day_str, + "portfolio_snapshot": { + "repositories_with_planfile": pf_extended["repositories_with_planfile"], + "queued_actionable": planfile_data["queued_count"], + "open_or_todo": pf_extended["open_or_todo_count"], + "in_progress": pf_extended["in_progress_count"], + "planned": pf_extended["planned_count"], + "blocked_total": pf_extended["blocked_total_count"], + "all_current_sprint_entries": pf_extended["all_current_sprint_entries"], + "estimated_minutes": planfile_data["total_estimated_minutes"], + "active_leases_count": leases_snap["active_leases_count"], + "terminal_leases_count": leases_snap["terminal_leases_count"], + "open_prs_count": pr_summary.get("open_prs_count", 0), + }, "planfile": { "completed_today": planfile_data["completed_count"], "queued": planfile_data["queued_count"], + "in_progress": pf_extended["in_progress_count"], + "blocked": pf_extended["blocked_total_count"], + "all_current_sprint_entries": pf_extended["all_current_sprint_entries"], "estimated_minutes": planfile_data["total_estimated_minutes"], "completed_tickets": planfile_data["completed_tickets"], "queued_tickets": planfile_data["queued_tickets"][:20], }, + "leases": leases_snap, "github": pr_summary, + "counts_labeled": counts_labeled, } +def gather_day( + root: Path, + hours: int = 24, + github: bool = True, + target_date: Optional[date] = None, + depth: int = 2, + state_dir: Optional[Path] = None, +) -> Dict[str, Any]: + """Compile 24h operational delta (`monag day`).""" + return gather_summary( + root=root, + hours=hours, + github=github, + target_date=target_date, + depth=depth, + state_dir=state_dir, + mode="day", + ) + + +def _fmt_label_suffix(labeled: Optional[Dict[str, Any]], default_source: str, default_filter: str) -> str: + if isinstance(labeled, dict): + src = labeled.get("source") or default_source + scope = labeled.get("scope") or "workspace" + flt = labeled.get("filter") or default_filter + return f" *(źródło: `{src}`, zakres: `{scope}`, filtr: `{flt}`)*" + return f" *(źródło: `{default_source}`, filtr: `{default_filter}`)*" + + def format_markdown(data: Dict[str, Any]) -> str: - """Format daily summary data into clean GitHub-flavored markdown.""" + """Format `day` (24h operational delta) or `summary` (portfolio/queue snapshot) into Markdown.""" + mode = str(data.get("mode") or "summary").lower() + day = data.get("date", str(date.today())) + labels = data.get("counts_labeled") or {} + + if mode == "day": + delta = data.get("delta_24h") or {} + gh = data.get("github") or {} + hours = data.get("hours", 24) + comp_cnt = delta.get("completed_tasks_count", (data.get("planfile") or {}).get("completed_today", 0)) + blk_cnt = delta.get("new_blockers_count", 0) + runs_ok = delta.get("agent_runs_completed", 0) + runs_fail = delta.get("agent_runs_failed", 0) + merged_cnt = delta.get("merged_prs_count", gh.get("merged_prs_count", 0)) + opened_cnt = delta.get("opened_prs_count", gh.get("opened_prs_count", 0)) + + lines = [ + f"# MONAG 24h Operational Delta (`day` — {day}, okno: {hours}h)", + "", + "## 1. 24h Delta — Planfile & Uruchomienia Agentów", + ( + f"- **Ukończone zadania (okno 24h)**: `{comp_cnt}` zadań" + + _fmt_label_suffix(labels.get("planfile_completed_24h"), ".planfile/sprints", f"date=={day}") + ), + ( + f"- **Nowe blokery (ostatnie {hours}h)**: `{blk_cnt}` zadań" + + _fmt_label_suffix(labels.get("planfile_new_blockers_24h"), ".planfile/sprints/current.yaml", f"status==blocked & updated<={hours}h") + ), + ( + f"- **Zakończone przebiegi agentów (ostatnie {hours}h)**: `{runs_ok}` ukończonych" + + _fmt_label_suffix(labels.get("agent_runs_completed_24h"), "state_dir/tasks/*.json", f"status==completed & updated<={hours}h") + ), + ( + f"- **Nieudane / przerwane przebiegi agentów (ostatnie {hours}h)**: `{runs_fail}` błędnych" + + _fmt_label_suffix(labels.get("agent_runs_failed_24h"), "state_dir/tasks/*.json", f"status in {{failed,stale}} & updated<={hours}h") + ), + "", + "## 2. 24h Delta — GitHub Pull Requests", + ( + f"- **Zmergowane PR (>= {day})**: `{merged_cnt}`" + + _fmt_label_suffix(labels.get("github_merged_prs_24h"), "gh search prs", f"is:merged merged:>={day}") + ), + ( + f"- **Nowo otwarte PR (>= {day})**: `{opened_cnt}`" + + _fmt_label_suffix(labels.get("github_opened_prs_24h"), "gh search prs", f"created:>={day}") + ), + "", + ] + + new_blockers = delta.get("new_blockers") or [] + if new_blockers: + lines.append("### Nowe blokery wykryte w oknie 24h:") + for b in new_blockers[:5]: + lines.append(f"- 🛑 **[{b.get('repo')}]** `{b.get('id')}`: {b.get('title')} ({b.get('reason')})") + lines.append("") + + recent_runs = delta.get("recent_agent_runs") or [] + if recent_runs: + lines.append("### Ostatnie przebiegi agentów (24h):") + for r in recent_runs[:5]: + icon = "✓" if r.get("status") in ("completed", "done", "ok", "success") else "✗" + tkt = f" (`{r.get('ticket')}`)" if r.get("ticket") else "" + lines.append(f"- {icon} **[{r.get('status')}]** `{r.get('id')}`{tkt}: {r.get('task')}") + lines.append("") + + merged_prs = delta.get("merged_prs") or gh.get("merged_prs", []) + if merged_prs: + lines.append("### Zmergowane Pull Requesty w oknie 24h:") + for pr in merged_prs[:5]: + lines.append(f"- ✓ **[{pr.get('repo')}]** `#{pr.get('number')}`: {pr.get('title')}") + lines.append("") + + opened_prs = delta.get("opened_prs") or gh.get("opened_prs", []) + if opened_prs: + lines.append("### Nowo otwarte Pull Requesty w oknie 24h:") + for pr in opened_prs[:5]: + lines.append(f"- ➕ **[{pr.get('repo')}]** `#{pr.get('number')}`: {pr.get('title')}") + lines.append("") + + completed = delta.get("completed_tickets") or (data.get("planfile") or {}).get("completed_tickets", []) + if completed: + lines.append("### Ukończone zadania Planfile w oknie 24h:") + for t in completed[:5]: + lines.append(f"- ✓ **[{t.get('repo')}]** `{t.get('id')}`: {t.get('title')}") + lines.append("") + + return "\n".join(lines) + + # mode == "summary": Current Portfolio & Queue Snapshot pf = data.get("planfile", {}) gh = data.get("github", {}) - day = data.get("date", str(date.today())) + snap = data.get("portfolio_snapshot") or {} est_mins = pf.get("estimated_minutes", 0) est_hours = round(est_mins / 60.0, 1) + queued_cnt = pf.get("queued", 0) + in_prog_cnt = pf.get("in_progress", snap.get("in_progress", 0)) + blocked_cnt = pf.get("blocked", snap.get("blocked_total", 0)) + all_curr_cnt = pf.get("all_current_sprint_entries", snap.get("all_current_sprint_entries", queued_cnt)) + active_leases = snap.get("active_leases_count", (data.get("leases") or {}).get("active_leases_count", 0)) lines = [ - f"# MONAG Daily Execution Summary ({day})", + f"# MONAG Portfolio & Queue Summary ({day})", "", - "## 1. Planfile & Koru Living Execution", - f"- **Wykonane dzisiaj**: `{pf.get('completed_today', 0)}` zadań", - f"- **Oczekujące w kolejce**: `{pf.get('queued', 0)}` zadań (szacunkowo `{est_mins} min` / `~{est_hours}h`)", + "## 1. Bieżący Stan Kolejki i Portfela (Snapshot)", + ( + f"- **Oczekujące w kolejce**: `{queued_cnt}` zadań (szacunkowo `{est_mins} min` / `~{est_hours}h`)" + + _fmt_label_suffix( + labels.get("planfile_queued_actionable"), + ".planfile/sprints/current.yaml", + "status in {todo,open,in_progress,planned}", + ) + ), + ( + f"- **W trakcie realizacji (in_progress)**: `{in_prog_cnt}` zadań" + + _fmt_label_suffix( + labels.get("planfile_in_progress"), + ".planfile/sprints/current.yaml", + "status == in_progress", + ) + ), + ( + f"- **Zablokowane w bieżącym sprincie**: `{blocked_cnt}` zadań" + + _fmt_label_suffix( + labels.get("planfile_blocked_total"), + ".planfile/sprints/current.yaml", + "status == blocked", + ) + ), + ( + f"- **Wszystkie wpisy bieżącego sprintu**: `{all_curr_cnt}` rekordów" + + _fmt_label_suffix( + labels.get("planfile_current_sprint_all_entries"), + ".planfile/sprints/current.yaml", + "all statuses in current sprint (excl. archived history-*.yaml)", + ) + ), + ( + f"- **Aktywne dzierżawy robocze (.subactor/leases)**: `{active_leases}`" + + _fmt_label_suffix( + labels.get("subactor_active_leases"), + ".subactor/leases/*.json", + "phase not in {released,merged,closed,cancelled}", + ) + ), + ( + f"- **Wykonane dzisiaj**: `{pf.get('completed_today', 0)}` zadań" + + _fmt_label_suffix( + labels.get("planfile_completed_today"), + ".planfile/sprints/{history,current}.yaml", + f"completed on {day}", + ) + ), "", - "## 2. GitHub Pull Requests", - f"- **Otwarte PR w kolejce**: `{gh.get('open_prs_count', 0)}`", - f"- **Zmergowane dzisiaj (>= {day})**: `{gh.get('merged_prs_count', 0)}`", + "## 2. Kolejka GitHub Pull Requests", + ( + f"- **Otwarte PR w kolejce**: `{gh.get('open_prs_count', 0)}`" + + _fmt_label_suffix( + labels.get("github_open_prs_queue"), + "gh search prs", + "author:@me state:open", + ) + ), + ( + f"- **Zmergowane dzisiaj (>= {day})**: `{gh.get('merged_prs_count', 0)}`" + + _fmt_label_suffix( + labels.get("github_merged_prs_today"), + "gh search prs", + f"author:@me is:merged merged:>={day}", + ) + ), "", ] @@ -292,15 +911,6 @@ def format_markdown(data: Dict[str, Any]) -> str: lines.append(f" *...oraz {len(open_prs) - 5} kolejnych otwartych PR.*") lines.append("") - merged_prs = gh.get("merged_prs", []) - if merged_prs: - lines.append("### Ostatnio zmergowane Pull Requesty dzisiaj:") - for pr in merged_prs[:5]: - lines.append(f"- ✓ **[{pr.get('repo')}]** `#{pr.get('number')}`: {pr.get('title')}") - if len(merged_prs) > 5: - lines.append(f" *...oraz {len(merged_prs) - 5} kolejnych zmergowanych PR dzisiaj.*") - lines.append("") - completed = pf.get("completed_tickets", []) if completed: lines.append("### Ostatnio ukończone zadania Planfile dzisiaj:") diff --git a/src/monag/triage.py b/src/monag/triage.py index c5ecc8c..d9f272d 100644 --- a/src/monag/triage.py +++ b/src/monag/triage.py @@ -9,6 +9,7 @@ from datetime import datetime, timezone import hashlib import json +import re import shutil from pathlib import Path import shlex @@ -398,6 +399,219 @@ def synthesize_triage_action( } +CATEGORY_IMPACT_BASE: Dict[str, int] = { + CATEGORY_IMMEDIATE_BLOCKER: 500, + CATEGORY_CORE_FOUNDATION: 250, + CATEGORY_STRATEGIC_ARCHITECTURE: 120, + CATEGORY_OPERATOR_IDE_ALERT: 100, + CATEGORY_FLEET_HEALTH_DIAGNOSTIC: 80, + CATEGORY_CODE_SMELL_HYGIENE: 40, +} + +CATEGORY_ITEM_IMPACT: Dict[str, int] = { + CATEGORY_IMMEDIATE_BLOCKER: 60, + CATEGORY_CORE_FOUNDATION: 30, + CATEGORY_STRATEGIC_ARCHITECTURE: 20, + CATEGORY_OPERATOR_IDE_ALERT: 18, + CATEGORY_FLEET_HEALTH_DIAGNOSTIC: 15, + CATEGORY_CODE_SMELL_HYGIENE: 10, +} + + +def derive_candidate_cause(candidate: Dict[str, Any], category: str = "") -> str: + """Derive a stable normalized cause key for deduplicating repeated findings per repository.""" + explicit = str(candidate.get("cause") or candidate.get("code") or "").strip().lower() + if explicit: + return re.sub(r"[^a-z0-9_.-]+", "_", explicit).strip("_") or "general" + + origin = str(candidate.get("origin") or "").strip().lower() + title = str(candidate.get("title") or "").strip() + title_low = title.lower() + + if origin in ("worktree-observed", "worktree-active"): + return origin + if origin == "prs-ahead-commits": + return "prs-ahead-commits" + if origin == "prs-open-pr": + return "prs-open-pr-ready" if candidate.get("checks_passed") is True else "prs-open-pr" + + for token, norm in ( + ("no-license", "missing_license"), + ("missing a license", "missing_license"), + ("no-readme", "missing_readme"), + ("missing a readme", "missing_readme"), + ("stale git worktree", "stale_worktrees"), + ("prune stale", "stale_worktrees"), + ("planfile-github-sync", "planfile_github_sync_missing"), + ("dirty-worktree", "dirty_worktree"), + ("uncommitted changes", "dirty_worktree"), + ("shotgun", "code_smell"), + ("god class", "code_smell"), + ("data clump", "code_smell"), + ("dead code", "code_smell"), + ("duplicate", "duplicate_item"), + ): + if token in title_low: + return norm + + stem = title_low.split(":", 1)[0].strip() + stem = re.sub(r"#\d+", "#n", stem) + stem = re.sub(r"\bticket[-_/]?\d+[a-z0-9_-]*", "ticket", stem) + stem = re.sub(r"[^a-z0-9]+", "_", stem).strip("_") + if origin and stem: + return f"{origin}:{stem[:48]}" + if origin: + return origin + if stem: + return stem[:48] + return category or "general" + + +def _candidate_sample_label(candidate: Dict[str, Any]) -> str: + origin = str(candidate.get("origin") or "") + path_str = str(candidate.get("path") or "").strip() + if origin in ("worktree-observed", "worktree-active") and path_str: + return Path(path_str).name + if candidate.get("pr_number"): + return f"PR #{candidate['pr_number']}: {str(candidate.get('title') or '').strip()}"[:80] + if candidate.get("issue_number"): + return f"#{candidate['issue_number']}: {str(candidate.get('title') or '').strip()}"[:80] + title = str(candidate.get("title") or "").strip() + if title: + return title[:80] + return Path(path_str).name if path_str else "item" + + +def compute_unblock_impact( + category: str, + affected_count: int, + collision_info: Dict[str, Any], + group: List[Dict[str, Any]], +) -> int: + """Compute deterministic estimated unblock impact from severity and affected item count.""" + explicit_vals = [ + int(c["unblock_impact"]) + for c in group + if isinstance(c.get("unblock_impact"), (int, float)) + ] + if explicit_vals: + return max(1, sum(explicit_vals)) + + base = CATEGORY_IMPACT_BASE.get(category, 100) + per_item = CATEGORY_ITEM_IMPACT.get(category, 15) + impact = base + max(1, affected_count) * per_item + if any(c.get("checks_passed") is True for c in group): + impact += 50 + ahead_total = sum(int(c.get("ahead_count") or 0) for c in group if str(c.get("ahead_count") or "").isdigit() or isinstance(c.get("ahead_count"), int)) + if ahead_total > 0: + impact += min(50, ahead_total * 5) + if collision_info.get("blocking") or collision_info.get("has_collision"): + impact = max(1, impact - 60) + return impact + + +def deduplicate_and_rank_candidates( + raw_candidates: List[Dict[str, Any]], + collisions_by_repo: Dict[str, Dict[str, Any]], + repo_paths_by_name: Dict[str, Path] | None = None, +) -> List[Dict[str, Any]]: + """Group repeated candidates by (repository, cause) and sort by estimated unblock impact descending.""" + repo_paths = repo_paths_by_name or {} + groups: Dict[Tuple[str, str], List[Tuple[Dict[str, Any], str, int, Dict[str, Any]]]] = {} + + for c in raw_candidates: + repo_name = str(c.get("repo") or Path(str(c.get("path") or "workspace")).name) + collision_info = collisions_by_repo.get(repo_name, {"has_collision": False}) + category, score = classify_candidate(c, repo_name, collision_info) + cause = derive_candidate_cause(c, category) + details = synthesize_triage_action(c, category, repo_name, collision_info) + groups.setdefault((repo_name, cause), []).append((c, category, score, details)) + + deduped: List[Dict[str, Any]] = [] + for (repo_name, cause), entries in groups.items(): + collision_info = collisions_by_repo.get(repo_name, {"has_collision": False}) + # Pick highest-scoring representative entry in group + entries_sorted = sorted( + entries, + key=lambda e: (-e[2], CATEGORY_ORDER.index(e[1]) if e[1] in CATEGORY_ORDER else 99, str(e[0].get("title") or "")), + ) + rep_cand, best_category, best_score, rep_details = entries_sorted[0] + affected_count = len(entries) + raw_group = [e[0] for e in entries_sorted] + + seen_samples: List[str] = [] + for cand_item in raw_group: + lbl = _candidate_sample_label(cand_item) + if lbl and lbl not in seen_samples: + seen_samples.append(lbl) + sample_items = seen_samples[:5] + + unblock_impact = compute_unblock_impact(best_category, affected_count, collision_info, raw_group) + + if affected_count == 1: + title = rep_cand.get("title", "") + action = rep_details["action"] + suggested_command = rep_details["suggested_command"] + evidence = rep_details["evidence"] + final_score = best_score + else: + final_score = best_score + min(600, (affected_count - 1) * 25) + preview = ", ".join(sample_items[:3]) + if len(seen_samples) > 3: + preview += f", +{len(seen_samples) - 3} more" + + origin = str(rep_cand.get("origin") or "") + if origin == "worktree-observed": + title = f"Registered worktrees to inspect ({affected_count} observed: {preview})" + repo_checkout = repo_paths.get(repo_name) + if repo_checkout is None and rep_cand.get("path"): + wt_p = Path(str(rep_cand["path"])) + repo_checkout = wt_p.parent.parent if wt_p.parent.name == ".worktrees" else wt_p.parent + cmd_target = shlex.quote(str(repo_checkout or repo_name)) + suggested_command = f"git -C {cmd_target} worktree list" + action = ( + f"Inspect {affected_count} registered worktrees and ownership evidence in {repo_name} " + f"(samples: {preview})" + ) + evidence = f"{repo_name}: {affected_count} worktrees observed ({preview})" + else: + base_stem = str(rep_cand.get("title") or cause).split(":", 1)[0].strip() + title = f"{base_stem} ({affected_count} affected: {preview})" + action = f"{rep_details['action']} ({affected_count} grouped occurrences: {preview})" + suggested_command = rep_details["suggested_command"] + evidence = f"{rep_details['evidence']} [{affected_count} grouped occurrences]" + + deduped.append({ + "repo": repo_name, + "cause": cause, + "title": title, + "origin": rep_cand.get("origin", ""), + "category": best_category, + "score": final_score, + "affected_count": affected_count, + "sample_items": sample_items, + "estimated_unblock_impact": unblock_impact, + "action": action, + "suggested_command": suggested_command, + "guardrails": rep_details["guardrails"], + "evidence": evidence, + "collision": collision_info, + "radar": rep_cand.get("radar"), + }) + + deduped.sort( + key=lambda item: ( + -item["estimated_unblock_impact"], + -item["score"], + -item["affected_count"], + CATEGORY_ORDER.index(item["category"]) if item["category"] in CATEGORY_ORDER else 99, + item["repo"], + item["cause"], + ) + ) + return deduped + + def run_holistic_triage( root: Path, depth: int = 2, @@ -531,32 +745,13 @@ def _inspect_repo(item: Tuple[str, Path]) -> Tuple[str, Dict[str, Any], List[Dic all_raw_candidates.extend(repo_candidates) errors.extend(repo_errors) - # 3. Algorithmic categorization, ranking, and step synthesis - categorized_items: List[Dict[str, Any]] = [] - - for c in all_raw_candidates: - repo_name = c.get("repo") or Path(c.get("path", "")).name - collision_info = collisions_by_repo.get(repo_name, {"has_collision": False}) - - category, score = classify_candidate(c, repo_name, collision_info) - details = synthesize_triage_action(c, category, repo_name, collision_info) - - categorized_items.append({ - "repo": repo_name, - "title": c.get("title", ""), - "origin": c.get("origin", ""), - "category": category, - "score": score, - "action": details["action"], - "suggested_command": details["suggested_command"], - "guardrails": details["guardrails"], - "evidence": details["evidence"], - "collision": collision_info, - "radar": c.get("radar"), - }) - - # Sort descending by score - categorized_items.sort(key=lambda item: item["score"], reverse=True) + # 3. Algorithmic deduplication by (repository, cause), impact ranking, and step synthesis + repo_paths_by_name = {name: path for name, path in discovered_repos} + categorized_items = deduplicate_and_rank_candidates( + all_raw_candidates, + collisions_by_repo=collisions_by_repo, + repo_paths_by_name=repo_paths_by_name, + ) capped_items = categorized_items[:limit] # Generate sequential deterministic guidance steps @@ -567,8 +762,12 @@ def _inspect_repo(item: Tuple[str, Path]) -> Tuple[str, Dict[str, Any], List[Dic "step": idx, "category": item["category"], "repo": item["repo"], + "cause": item["cause"], "title": item["title"], "score": item["score"], + "affected_count": item["affected_count"], + "sample_items": item["sample_items"], + "estimated_unblock_impact": item["estimated_unblock_impact"], "action": item["action"], "command": item["suggested_command"], "collision_safe": False if is_collision else None, @@ -589,6 +788,7 @@ def _inspect_repo(item: Tuple[str, Path]) -> Tuple[str, Dict[str, Any], List[Dic "duration_seconds": duration, "discovered_repos_count": len(discovered_repos), "total_candidates": len(all_raw_candidates), + "deduplicated_candidates_count": len(categorized_items), "active_agents_count": len(active_fleet), "active_agent_pids": active_pids, "dirty_worktrees_count": dirty_wts, @@ -612,6 +812,10 @@ def triage_markdown(report: Dict[str, Any]) -> str: f"- **Root**: `{report.get('root')}`", f"- **Discovered Repositories**: {report.get('discovered_repos_count')}", f"- **Total Candidates Evaluated**: {report.get('total_candidates')}", + ] + if report.get("deduplicated_candidates_count") is not None: + lines.append(f"- **Deduplicated Action Groups (`repo, cause`)**: {report.get('deduplicated_candidates_count')}") + lines.extend([ f"- **Active Agent Processes**: {report.get('active_agents_count', 0)} (PIDs: {report.get('active_agent_pids', [])})", f"- **Dirty / Uncommitted Worktrees**: {report.get('dirty_worktrees_count', 0)}", f"- **Declared Scope / Worktree Conflicts**: {report.get('collision_count')}", @@ -620,7 +824,7 @@ def triage_markdown(report: Dict[str, Any]) -> str: "", "## Deterministic Step-by-Step Guidance Plan", "", - ] + ]) steps = report.get("guidance_steps", []) if not steps: @@ -630,8 +834,14 @@ def triage_markdown(report: Dict[str, Any]) -> str: for s in steps: cat_tag = s["category"].upper().replace("_", " ") safety = "Ownership verified" if s.get("ownership_verified") else ("⚠️ Declared conflict / active agent" if s.get("collision_safe") is False else "Ownership unverified (collision-safe)") + affected = s.get("affected_count", 1) + impact = s.get("estimated_unblock_impact") + impact_str = f" | **Unblock Impact**: `{impact}`" if impact is not None else "" + count_str = f" | **Affected**: `{affected}`" if affected and affected > 1 else "" lines.append(f"### Step {s['step']}: [{cat_tag}] `{s['repo']}` — {s['title']}") - lines.append(f"- **Score**: `{s['score']}` | **Status**: {safety}") + lines.append(f"- **Score**: `{s['score']}`{impact_str}{count_str} | **Status**: {safety}") + if affected and affected > 1 and s.get("sample_items"): + lines.append(f"- **Sample Items ({len(s['sample_items'])}/{affected})**: `{', '.join(map(str, s['sample_items']))}`") lines.append(f"- **Action**: {s['action']}") lines.append("- **Suggested Command**:") lines.append(" ```sh") diff --git a/src/monag/worktrees.py b/src/monag/worktrees.py index dcb5822..4866ea9 100644 --- a/src/monag/worktrees.py +++ b/src/monag/worktrees.py @@ -19,14 +19,14 @@ from typing import Any, Dict, List, Optional, Set, Tuple -def command(cmd: list[str], cwd: Optional[Path] = None) -> tuple[str, str]: +def command(cmd: list[str], cwd: Optional[Path] = None, timeout: float = 15.0) -> tuple[str, str]: try: proc = subprocess.run( cmd, cwd=str(cwd) if cwd else None, capture_output=True, text=True, - timeout=15, + timeout=max(0.2, float(timeout)), ) out = proc.stdout.strip() err = proc.stderr.strip() @@ -161,11 +161,11 @@ def _resolve_worktree_lease( return None -def is_worktree_dirty(wt_path: Path) -> tuple[bool, str]: +def is_worktree_dirty(wt_path: Path, timeout: float = 15.0) -> tuple[bool, str]: """Check if the worktree has any uncommitted or untracked changes, failing closed on errors.""" if not wt_path.is_dir(): return False, "" - out, err = command(["git", "-C", str(wt_path), "status", "--porcelain"]) + out, err = command(["git", "-C", str(wt_path), "status", "--porcelain"], timeout=timeout) if err: return True, f"git_status_failed ({err})" lines = [ln.strip() for ln in out.splitlines() if ln.strip()] @@ -174,33 +174,56 @@ def is_worktree_dirty(wt_path: Path) -> tuple[bool, str]: return False, "" -def _is_ancestor_commit(repo: Path, branch: str, target: str) -> bool: - proc = subprocess.run( - ["git", "-C", str(repo), "merge-base", "--is-ancestor", branch, target], - capture_output=True, - text=True, - ) - return proc.returncode == 0 +def _is_ancestor_commit(repo: Path, branch: str, target: str, timeout: float = 15.0) -> bool: + try: + proc = subprocess.run( + ["git", "-C", str(repo), "merge-base", "--is-ancestor", branch, target], + capture_output=True, + text=True, + timeout=max(0.2, float(timeout)), + ) + return proc.returncode == 0 + except (subprocess.SubprocessError, OSError): + return False -def is_branch_merged(repo: Path, branch: str) -> bool: +def resolve_default_target_branch(repo: Path, timeout: float = 15.0) -> Optional[str]: + """Resolve repository default integration branch ('main' or 'master') once per repository.""" + for candidate in ("main", "master"): + chk, _ = command(["git", "-C", str(repo), "rev-parse", "--verify", candidate], timeout=timeout) + if chk.strip(): + return candidate + return None + + +def is_branch_merged( + repo: Path, + branch: str, + target_branch: Optional[str] = None, + timeout: float = 15.0, +) -> bool: """Check if branch is merged to main or master (cannot establish integration merely by comparing against own HEAD).""" if not branch: return False - b_sha, _ = command(["git", "-C", str(repo), "rev-parse", "--verify", branch]) + b_sha, _ = command(["git", "-C", str(repo), "rev-parse", "--verify", branch], timeout=timeout) b_sha = b_sha.strip() if not b_sha: return False + if target_branch is not None: + if not target_branch: + return False + return _is_ancestor_commit(repo, branch, target_branch, timeout=timeout) + targets = ["main", "master"] for t in targets: - chk, _ = command(["git", "-C", str(repo), "rev-parse", "--verify", t]) + chk, _ = command(["git", "-C", str(repo), "rev-parse", "--verify", t], timeout=timeout) t_sha = chk.strip() if not t_sha: continue # Check merge-base --is-ancestor branch target - if _is_ancestor_commit(repo, branch, t): + if _is_ancestor_commit(repo, branch, t, timeout=timeout): return True return False @@ -336,8 +359,8 @@ def prune_worktrees_safe( active_cwds, _ = audit_active_process_cwds() from . import doctor - wts = doctor.audit_worktrees(repo) leases = get_repo_leases(repo) + wts = doctor.audit_worktrees(repo, leases=leases) result = { "repo": repo.name, @@ -497,7 +520,7 @@ def audit_fleet_worktrees( for repo in repos: leases = get_repo_leases(repo) - wts = doctor.audit_worktrees(repo) + wts = doctor.audit_worktrees(repo, leases=leases) non_primary = [w for w in wts if not w.get("is_primary")] if not non_primary: continue diff --git a/tests/test_summary_and_subllm_routes.py b/tests/test_summary_and_subllm_routes.py index 126282c..733352c 100644 --- a/tests/test_summary_and_subllm_routes.py +++ b/tests/test_summary_and_subllm_routes.py @@ -22,7 +22,7 @@ def test_subllm_route_invocation_koru_agent(): "koru-agent", "nl-to-koru-dsl", [{"role": "user", "content": "diagnose repo"}], - timeout_seconds=30.0, + timeout_seconds=6.0, ) @@ -43,7 +43,7 @@ def test_subllm_route_fallback_todo2code(): "todo2code", "semantic", [{"role": "user", "content": "diagnose fallback"}], - timeout_seconds=30.0, + timeout_seconds=6.0, ) @@ -113,10 +113,11 @@ def test_gather_summary_and_markdown(tmp_path: Path): assert data["github"]["merged_prs_count"] == 2 md = summary.format_markdown(data) - assert "MONAG Daily Execution Summary (2026-09-25)" in md + assert "MONAG Portfolio & Queue Summary (2026-09-25)" in md assert "**Wykonane dzisiaj**: `3` zadań" in md assert "**Oczekujące w kolejce**: `2` zadań (szacunkowo `45 min`" in md assert "**Otwarte PR w kolejce**: `1`" in md assert "**Zmergowane dzisiaj (>= 2026-09-25)**: `2`" in md + assert "źródło:" in md assert "Done Task 1" in md assert "Queued Task 1" in md diff --git a/tests/test_wave2_mon01_mon04.py b/tests/test_wave2_mon01_mon04.py new file mode 100644 index 0000000..9fc9b6a --- /dev/null +++ b/tests/test_wave2_mon01_mon04.py @@ -0,0 +1,548 @@ +"""Comprehensive unit tests for Wave 2 (`MON-01`..`MON-04`, issues #162..#165).""" +from __future__ import annotations + +from datetime import date, datetime, timezone +import io +import json +from pathlib import Path +import subprocess +import tempfile +import unittest +from unittest.mock import patch + +from monag import autodiagnosis, cli, doctor, summary, triage, worktrees + + +class Mon01AutonomyStallProbeTests(unittest.TestCase): + """MON-01 (#162): Deterministic autonomy stall probes & top ranking in autodiagnose.""" + + def setUp(self) -> None: + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + + def _init_hygiene_repo(self, rel_path: str) -> Path: + repo = self.root / rel_path + repo.mkdir(parents=True) + subprocess.run(["git", "-C", str(repo), "init", "-b", "main"], check=True, capture_output=True) + subprocess.run(["git", "-C", str(repo), "config", "user.email", "t@example.invalid"], check=True) + subprocess.run(["git", "-C", str(repo), "config", "user.name", "Test"], check=True) + # Leave missing LICENSE & README + raw git+ URL + dirty file to create floor & hygiene defects + (repo / "pyproject.toml").write_text('[project]\ndependencies = ["x @ git+https://example.invalid/x.git"]\n', encoding="utf-8") + (repo / "dirty.txt").write_text("dirty\n", encoding="utf-8") + return repo + + def test_systemd_daemon_status_and_load_stall_probes_rank_above_repo_hygiene(self) -> None: + self._init_hygiene_repo("semcod/noisy-repo") + + systemctl_stdout = "\n\n".join( + [ + "Id=subactor-coding-agent.service\nLoadState=loaded\nActiveState=failed\nSubState=failed\nResult=exit-code\nExecMainStatus=1", + "Id=subactor-pr-controller.service\nLoadState=loaded\nActiveState=failed\nSubState=failed\nResult=exit-code\nExecMainStatus=2", + "Id=subactor-repair-agent.service\nLoadState=loaded\nActiveState=failed\nSubState=failed\nResult=exit-code\nExecMainStatus=1", + "Id=subactor-autonom-user.service\nLoadState=loaded\nActiveState=failed\nSubState=failed\nResult=exit-code\nExecMainStatus=1", + "Id=heyasa.service\nLoadState=loaded\nActiveState=active\nSubState=running\nResult=success\nExecMainStatus=0", + ] + ) + + heyasa_payload = { + "schema": "heyasa.daemon-status/v1", + "velocity_status": { + "is_degraded": True, + "completed_count": 0, + "target_capacity": 12, + "open_candidates_count": 71, + "summary": "DEGRADED: 0 completed vs 12 target with 71 open candidates", + }, + "last_result": { + "willman": { + "flowguard": { + "eligible_projects": 0, + "admitted_slots": 0, + "blocked_candidates": 63, + "blocked_reasons": {"active_lease_fenced": 63}, + "project_exclusions": {"semcod/monag": "active_lease"}, + } + } + }, + } + + def fake_systemctl(cmd, **kwargs): + self.assertEqual(cmd[:3], ["systemctl", "--user", "show"]) + self.assertLessEqual(kwargs.get("timeout", 99), 3.0) + return subprocess.CompletedProcess(cmd, 0, systemctl_stdout, "") + + stall_anomalies = autodiagnosis.probe_autonomy_health( + self.root, + systemctl_runner=fake_systemctl, + heyasa_payload=heyasa_payload, + load_fn=lambda: (43.6, 38.0, 29.5), + cpu_count_fn=lambda: 16, + ) + codes = [a["code"] for a in stall_anomalies] + self.assertEqual(codes.count("AUTONOMY_UNIT_FAILED"), 4) + self.assertIn("AUTONOMY_ELIGIBLE_PROJECTS_ZERO", codes) + self.assertIn("AUTONOMY_ADMISSION_STALL", codes) + self.assertIn("AUTONOMY_VELOCITY_STALL", codes) + self.assertIn("HOST_LOAD_SATURATION", codes) + self.assertEqual(len(stall_anomalies), 8) + + with patch("monag.autodiagnosis.probe_autonomy_health", return_value=stall_anomalies): + diag = autodiagnosis.diagnose_fleet(self.root, depth=2, use_cache=False) + tickets = autodiagnosis.synthesize_tickets_with_subllm(diag, runner=None) + + # Every single operational stall anomaly & ticket MUST rank strictly above repo defects/hygiene + top_8_codes = [a["code"] for a in diag["anomalies"][:8]] + for code in top_8_codes: + self.assertNotIn(code, {"NO_LICENSE", "NO_README", "HIGH_WORKTREE_COUNT", "DIRTY_WORKTREE", "DEPENDENCY_GIT_URL_FOUND"}) + + top_8_tickets = tickets[:8] + self.assertTrue(all(t.get("autonomy_probe") is True for t in top_8_tickets)) + self.assertTrue(all(t.get("weight", 0) >= 850 for t in top_8_tickets)) + repo_tickets = tickets[8:] + self.assertGreaterEqual(len(repo_tickets), 3) + self.assertTrue(all(t.get("autonomy_probe") is False for t in repo_tickets)) + + def test_host_load_warning_threshold_and_graceful_degradation(self) -> None: + # Warning threshold: 1.5x < 26.0 / 16 <= 2.0x + warn_findings = autodiagnosis.probe_autonomy_health( + self.root, + systemctl_runner=lambda *a, **kw: (_ for _ in ()).throw(FileNotFoundError("no systemctl")), + http_opener=lambda *a, **kw: (_ for _ in ()).throw(OSError("connection refused")), + load_fn=lambda: (26.0, 20.0, 15.0), + cpu_count_fn=lambda: 16, + ) + self.assertEqual(len(warn_findings), 1) + self.assertEqual(warn_findings[0]["code"], "HOST_LOAD_SATURATION") + self.assertEqual(warn_findings[0]["severity"], "WARNING") + self.assertGreater(warn_findings[0]["weight"], 0) + + # Healthy host + missing systemctl + unreachable daemon => 0 findings, no crash + healthy = autodiagnosis.probe_autonomy_health( + self.root, + systemctl_runner=lambda *a, **kw: (_ for _ in ()).throw(subprocess.TimeoutExpired("systemctl", 2.5)), + http_opener=lambda *a, **kw: (_ for _ in ()).throw(TimeoutError("timed out")), + load_fn=lambda: (4.0, 3.0, 2.0), + cpu_count_fn=lambda: 16, + ) + self.assertEqual(healthy, []) + + def test_non_loopback_heyasa_url_is_refused_without_network_io(self) -> None: + called = [] + + def forbidden_opener(*args, **kwargs): + called.append(args) + raise AssertionError("external URL must never be opened") + + findings = autodiagnosis.probe_autonomy_health( + self.root, + units=(), + heyasa_status_url="https://example.com/v1/daemon/status", + http_opener=forbidden_opener, + load_fn=lambda: (1.0, 1.0, 1.0), + cpu_count_fn=lambda: 8, + ) + self.assertEqual(findings, []) + self.assertEqual(called, []) + + +class Mon02DoctorBudgetAndLeaseHoistTests(unittest.TestCase): + """MON-02 (#163): O(1) lease reading per repo & monotonic time budget partial output.""" + + def setUp(self) -> None: + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + + def _make_repo(self, rel_path: str) -> Path: + repo = self.root / rel_path + repo.mkdir(parents=True) + subprocess.run(["git", "-C", str(repo), "init", "-b", "main"], check=True, capture_output=True) + subprocess.run(["git", "-C", str(repo), "config", "user.email", "t@example.invalid"], check=True) + subprocess.run(["git", "-C", str(repo), "config", "user.name", "Test"], check=True) + (repo / "README.md").write_text("seed\n", encoding="utf-8") + subprocess.run(["git", "-C", str(repo), "add", "README.md"], check=True) + subprocess.run(["git", "-C", str(repo), "commit", "-m", "seed"], check=True, capture_output=True) + return repo + + def test_audit_worktrees_reads_repo_leases_once_for_many_worktrees(self) -> None: + repo = self._make_repo("org/multi-wt") + leases_dir = repo / ".subactor" / "leases" + leases_dir.mkdir(parents=True) + + for idx in range(1, 9): + wt_dir = repo / ".worktrees" / f"ticket-{idx:03d}--slice" + subprocess.run( + ["git", "-C", str(repo), "worktree", "add", "-b", f"ticket/{idx:03d}-slice", str(wt_dir), "main"], + check=True, + capture_output=True, + ) + (leases_dir / f"ticket-{idx:03d}--slice.json").write_text( + json.dumps( + { + "schema": "wellmanifest.change-lease/v1", + "ticketId": f"ticket-{idx:03d}", + "worktreeId": f"ticket-{idx:03d}--slice", + "branchRef": f"refs/heads/ticket/{idx:03d}-slice", + "phase": "editing", + } + ), + encoding="utf-8", + ) + + with ( + patch("monag.worktrees.get_repo_leases", wraps=worktrees.get_repo_leases) as spy_leases, + patch("monag.worktrees.resolve_default_target_branch", wraps=worktrees.resolve_default_target_branch) as spy_target, + ): + audited = doctor.audit_worktrees(repo) + + secondary = [w for w in audited if not w["is_primary"]] + self.assertEqual(len(secondary), 8) + self.assertTrue(all(not w["stale"] for w in secondary)) + self.assertEqual(spy_leases.call_count, 1) + self.assertEqual(spy_target.call_count, 1) + + def test_diagnose_returns_structured_partial_report_when_budget_expires(self) -> None: + for idx in range(1, 5): + self._make_repo(f"org/repo-{idx}") + + fake_now = [100.0] + + def fake_clock() -> float: + return fake_now[0] + + orig_audit_wt = doctor.audit_worktrees + + def slow_audit_wt(repo: Path, **kwargs): + res = orig_audit_wt(repo, **kwargs) + # Advance monotonic clock past budget after auditing first repository + fake_now[0] += 0.6 + return res + + fake_home = self.root / "empty_home" + fake_home.mkdir() + + with patch("monag.doctor.audit_worktrees", side_effect=slow_audit_wt): + report = doctor.diagnose( + self.root, + home=fake_home, + budget_seconds=0.5, + clock=fake_clock, + ) + + self.assertTrue(report["timed_out"]) + self.assertEqual(report["budget_seconds"], 0.5) + self.assertIn("environment", report["completed_phases"]) + self.assertIn("ecosystem_tools", report["completed_phases"]) + self.assertIn("repositories", report["timed_out_phases"]) + self.assertIn("agent_storage", report["skipped_phases"]) + self.assertEqual(report["repositories_checked"], 1) + self.assertEqual(len(report["skipped_repositories"]), 3) + self.assertEqual(report["errors"], []) + + def test_cli_doctor_timeout_flag_outputs_valid_json(self) -> None: + repo = self._make_repo("org/cli-doc") + buf = io.StringIO() + with patch("sys.stdout", buf): + rc = cli.main(["doctor", "--root", str(repo), "--timeout", "15", "--json"]) + self.assertEqual(rc, 0) + parsed = json.loads(buf.getvalue()) + self.assertEqual(parsed["budget_seconds"], 15.0) + self.assertIn("timed_out", parsed) + self.assertIn("completed_phases", parsed) + self.assertIn("skipped_repositories", parsed) + + +class Mon03DayVsSummarySeparationTests(unittest.TestCase): + """MON-03 (#164): Distinct `day` (24h delta) vs `summary` (portfolio snapshot) + source labels.""" + + def setUp(self) -> None: + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + self.state_dir = self.root / ".state" + (self.state_dir / "tasks").mkdir(parents=True) + + repo = self.root / "semcod" / "monag" + sprints = repo / ".planfile" / "sprints" + sprints.mkdir(parents=True) + leases = repo / ".subactor" / "leases" + leases.mkdir(parents=True) + + self.today = date(2026, 11, 14) + now_iso = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + now_epoch = datetime.now(timezone.utc).timestamp() + + current_sprint = { + "data": { + "sprint": { + "tickets": { + "PLF-101": { + "name": "Completed today feature", + "status": "done", + "updated_at": f"{self.today.isoformat()}T09:00:00Z", + }, + "PLF-102": { + "name": "Queued open work", + "status": "open", + "priority": "high", + "estimated_duration_seconds": 900, + }, + "PLF-103": { + "name": "In-progress ticket", + "status": "in_progress", + "priority": "critical", + "estimated_duration_seconds": 600, + }, + "PLF-104": { + "name": "Blocked on upstream dependency", + "status": "blocked", + "updated_at": now_iso, + "blocked_reason": "waiting on validator pin", + }, + } + } + } + } + (sprints / "current.yaml.fast.json").write_text(json.dumps(current_sprint), encoding="utf-8") + + (leases / "ticket-137--active.json").write_text( + json.dumps( + { + "schema": "wellmanifest.change-lease/v1", + "ticketId": "ticket-137", + "phase": "editing", + } + ), + encoding="utf-8", + ) + + # Agent run records in /tasks/*.json + (self.state_dir / "tasks" / "run-ok.json").write_text( + json.dumps( + { + "pid": 12345, + "start": "100", + "cwd": str(repo), + "task": "Implement MON-03", + "ticket": "ticket-137", + "status": "completed", + "updated": now_epoch - 120, + } + ), + encoding="utf-8", + ) + (self.state_dir / "tasks" / "run-fail.json").write_text( + json.dumps( + { + "pid": 12346, + "start": "101", + "cwd": str(repo), + "task": "Broken executor run", + "ticket": "ticket-136", + "status": "failed", + "updated": now_epoch - 60, + } + ), + encoding="utf-8", + ) + + def test_day_and_summary_produce_distinct_json_and_markdown_with_source_labels(self) -> None: + mock_prs = { + "open_prs_count": 4, + "merged_prs_count": 2, + "opened_prs_count": 3, + "open_prs": [{"repo": "semcod/monag", "number": 170, "title": "Open PR", "url": "https://x/170"}], + "merged_prs": [{"repo": "semcod/monag", "number": 169, "title": "Merged PR", "url": "https://x/169"}], + "opened_prs": [{"repo": "semcod/monag", "number": 170, "title": "Open PR", "state": "OPEN", "url": "https://x/170"}], + } + with patch("monag.summary.query_github_pr_metrics", return_value=mock_prs): + day_data = summary.gather_day( + self.root, + hours=24, + github=True, + target_date=self.today, + depth=2, + state_dir=self.state_dir, + ) + sum_data = summary.gather_summary( + self.root, + hours=24, + github=True, + target_date=self.today, + depth=2, + state_dir=self.state_dir, + mode="summary", + ) + + self.assertEqual(day_data["mode"], "day") + self.assertEqual(day_data["report_kind"], "day_24h_delta") + self.assertEqual(sum_data["mode"], "summary") + self.assertEqual(sum_data["report_kind"], "portfolio_queue_snapshot") + + # Day captures 24h operational delta (agent runs, new blockers, merged/opened PRs) + self.assertIn("delta_24h", day_data) + self.assertNotIn("portfolio_snapshot", day_data) + self.assertEqual(day_data["delta_24h"]["agent_runs_completed"], 1) + self.assertEqual(day_data["delta_24h"]["agent_runs_failed"], 1) + self.assertEqual(day_data["delta_24h"]["new_blockers_count"], 1) + self.assertEqual(day_data["delta_24h"]["merged_prs_count"], 2) + self.assertEqual(day_data["delta_24h"]["opened_prs_count"], 3) + + # Summary captures current portfolio/queue snapshot (queued, blocked, all entries, active leases, open PRs) + self.assertIn("portfolio_snapshot", sum_data) + self.assertNotIn("delta_24h", sum_data) + self.assertEqual(sum_data["portfolio_snapshot"]["queued_actionable"], 2) + self.assertEqual(sum_data["portfolio_snapshot"]["in_progress"], 1) + self.assertEqual(sum_data["portfolio_snapshot"]["blocked_total"], 1) + self.assertEqual(sum_data["portfolio_snapshot"]["all_current_sprint_entries"], 4) + self.assertEqual(sum_data["portfolio_snapshot"]["active_leases_count"], 1) + self.assertEqual(sum_data["portfolio_snapshot"]["open_prs_count"], 4) + + # Every count in both modes MUST carry explicit source, scope, and filter labels + for payload in (day_data, sum_data): + self.assertTrue(payload["counts_labeled"]) + for metric_name, meta in payload["counts_labeled"].items(): + self.assertIn("value", meta, metric_name) + self.assertTrue(meta.get("source"), f"{metric_name} missing source") + self.assertTrue(meta.get("scope"), f"{metric_name} missing scope") + self.assertTrue(meta.get("filter"), f"{metric_name} missing filter") + + day_md = summary.format_markdown(day_data) + sum_md = summary.format_markdown(sum_data) + self.assertNotEqual(day_md, sum_md) + self.assertIn("MONAG 24h Operational Delta", day_md) + self.assertIn("MONAG Portfolio & Queue Summary", sum_md) + self.assertIn("źródło:", day_md) + self.assertIn("filtr:", day_md) + self.assertIn("źródło:", sum_md) + self.assertIn("filtr:", sum_md) + + def test_cli_day_and_summary_commands_emit_distinct_reports(self) -> None: + day_buf = io.StringIO() + sum_buf = io.StringIO() + with patch("sys.stdout", day_buf): + rc_day = cli.main( + ["--root", str(self.root), "--state-dir", str(self.state_dir), "day", "--no-github", "--markdown"] + ) + with patch("sys.stdout", sum_buf): + rc_sum = cli.main( + ["--root", str(self.root), "--state-dir", str(self.state_dir), "summary", "--no-github", "--markdown"] + ) + self.assertEqual(rc_day, 0) + self.assertEqual(rc_sum, 0) + self.assertNotEqual(day_buf.getvalue(), sum_buf.getvalue()) + self.assertIn("24h Operational Delta", day_buf.getvalue()) + self.assertIn("Portfolio & Queue Summary", sum_buf.getvalue()) + + +class Mon04TriageDeduplicationAndImpactSortTests(unittest.TestCase): + """MON-04 (#165): Collapse repeated `(repo, cause)` findings & sort by unblock impact descending.""" + + def test_collapses_fifteen_duplicate_repo_worktree_rows_and_sorts_by_unblock_impact(self) -> None: + raw_candidates = [ + # Single low-impact code smell in repo-smell + { + "repo": "semcod/tagi", + "origin": "audit-untracked-issue", + "title": "Address code smell: dead code in legacy adapter", + "priority": "low", + }, + # Single worktree observation in small-repo + { + "repo": "other/small-repo", + "origin": "worktree-observed", + "path": "/workspace/other/small-repo/.worktrees/ticket-001--minor", + "title": "Registered worktree to inspect: ticket-001--minor", + "priority": "medium", + }, + ] + # 15 identical worktree-observed rows in forge (reproducing the incident report) + for idx in range(1, 16): + raw_candidates.append( + { + "repo": "heyasa/forge", + "origin": "worktree-observed", + "path": f"/workspace/heyasa/forge/.worktrees/ticket-{idx:03d}--task", + "title": f"Registered worktree to inspect: ticket-{idx:03d}--task", + "priority": "medium", + } + ) + # 1 critical immediate blocker in subactor/coding-agent + raw_candidates.append( + { + "repo": "subactor/coding-agent", + "origin": "diagnostics", + "title": "Critical blocker: executor service crashed on startup", + "priority": "critical", + } + ) + + ranked = triage.deduplicate_and_rank_candidates(raw_candidates, collisions_by_repo={}) + # 18 raw candidates collapse into 4 unique (repo, cause) groups! + self.assertEqual(len(ranked), 4) + + # Verify descending unblock impact order: + # 1. Critical immediate blocker (subactor/coding-agent) + # 2. 15-worktree bottleneck (heyasa/forge, affected_count=15) + # 3. Single worktree observation (other/small-repo, affected_count=1) + # 4. Single code smell (semcod/tagi, affected_count=1) + repos_in_order = [item["repo"] for item in ranked] + self.assertEqual( + repos_in_order, + ["subactor/coding-agent", "heyasa/forge", "other/small-repo", "semcod/tagi"], + ) + + impacts = [item["estimated_unblock_impact"] for item in ranked] + self.assertEqual(impacts, sorted(impacts, reverse=True)) + + forge_entry = ranked[1] + self.assertEqual(forge_entry["affected_count"], 15) + self.assertEqual(forge_entry["cause"], "worktree-observed") + self.assertEqual(len(forge_entry["sample_items"]), 5) + self.assertEqual(forge_entry["sample_items"][0], "ticket-001--task") + self.assertNotIn("active", forge_entry["title"].lower()) + self.assertIn("15 observed", forge_entry["title"]) + + def test_run_holistic_triage_propagates_grouped_fields_and_markdown(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + repo = root / "heyasa" / "forge" + repo.mkdir(parents=True) + subprocess.run(["git", "-C", str(repo), "init", "-b", "main"], check=True, capture_output=True) + subprocess.run(["git", "-C", str(repo), "config", "user.email", "t@example.invalid"], check=True) + subprocess.run(["git", "-C", str(repo), "config", "user.name", "Test"], check=True) + subprocess.run( + ["git", "-C", str(repo), "commit", "--allow-empty", "-m", "seed"], + check=True, + capture_output=True, + ) + for idx in range(1, 5): + wt = repo / ".worktrees" / f"ticket-{idx:03d}--dup" + subprocess.run( + ["git", "-C", str(repo), "worktree", "add", "-b", f"ticket/{idx:03d}-dup", str(wt), "main"], + check=True, + capture_output=True, + ) + + with patch("monag.triage.audit_active_fleet_processes", return_value=[]): + report = triage.run_holistic_triage(root, depth=2, limit=15) + + self.assertEqual(report["total_candidates"], 4) + self.assertEqual(report["deduplicated_candidates_count"], 1) + self.assertEqual(len(report["guidance_steps"]), 1) + + step = report["guidance_steps"][0] + self.assertEqual(step["repo"], "heyasa/forge") + self.assertEqual(step["affected_count"], 4) + self.assertEqual(len(step["sample_items"]), 4) + self.assertGreater(step["estimated_unblock_impact"], 0) + + md = triage.triage_markdown(report) + self.assertIn("Deduplicated Action Groups", md) + self.assertIn("Affected**: `4`", md) + self.assertIn("Sample Items (4/4)", md) + + +if __name__ == "__main__": + unittest.main()