Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(hogli): add doctor:report diagnostics dump (#63023)

Raúl Negrón-Oterofe6725fe3914
diff --git a/hogli.yaml b/hogli.yaml
index 598db457b5fd..a19c05dd7275 100644
--- a/hogli.yaml
+++ b/hogli.yaml
@@ -806,6 +806,9 @@ tools:
doctor:zombies:
click: hogli_commands.doctor:doctor_zombies
description: Find and kill orphaned PostHog dev processes
+ doctor:report:
+ click: hogli_commands.doctor:doctor_report
+ description: Dump a paste-ready dev-environment diagnostics report for devex triage
product:bootstrap:
click: hogli_commands.product.cli:cmd_bootstrap
description: Scaffold a new product with canonical structure
diff --git a/tools/hogli-commands/README.md b/tools/hogli-commands/README.md
index bd6636749b17..f262624c2430 100644
--- a/tools/hogli-commands/README.md
+++ b/tools/hogli-commands/README.md
@@ -74,7 +74,7 @@ tools/hogli-commands/
├── telemetry_props.py# Boot module — registers PostHog telemetry props
├── hint_hook.py # Boot module — registers the post-command hint hook
├── build.py # Lazy: hogli build
- ├── doctor.py # Lazy: hogli doctor / doctor:disk / doctor:zombies
+ ├── doctor.py # Lazy: hogli doctor / doctor:disk / doctor:zombies / doctor:report
├── ... # Other lazy command modules
├── devbox/ # Devbox subpackage (lazy)
├── devenv/ # Intent-based dev environment subpackage (lazy)
diff --git a/tools/hogli-commands/hogli_commands/doctor.py b/tools/hogli-commands/hogli_commands/doctor.py
index e5b2e67f917f..0dffec3d197c 100644
--- a/tools/hogli-commands/hogli_commands/doctor.py
+++ b/tools/hogli-commands/hogli_commands/doctor.py
@@ -2,17 +2,25 @@ from __future__ import annotations
import os
import re
+import sys
import enum
import time
import shutil
import signal
+import hashlib
+import platform
+import importlib
import subprocess
from collections.abc import Callable, Iterable, Sequence
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass, field
+from datetime import UTC, datetime
from pathlib import Path
+from typing import Protocol
+import yaml
import click
+from hogli.manifest import REPO_ROOT, get_manifest
from . import hints
@@ -138,8 +146,6 @@ def doctor_disk(
--yes to skip prompts.
"""
- from hogli.manifest import REPO_ROOT
-
click.echo("🔍 PostHog Disk Space Cleanup\n")
if dry_run:
@@ -991,8 +997,6 @@ class DevProcess:
def doctor_zombies(dry_run: bool, yes: bool, include_all: bool) -> None:
"""Find and kill orphaned PostHog dev processes left behind after an unclean shutdown."""
- from hogli.manifest import REPO_ROOT
-
def _record() -> None:
if not dry_run:
hints.record_check_run("doctor:zombies")
@@ -1081,38 +1085,39 @@ def _scan_posthog_processes(repo_root: Path) -> list[DevProcess]:
click.echo("Failed to run ps command.")
return []
+ parsed = [p for line in result.stdout.strip().splitlines() if (p := _parse_ps_line(line)) is not None]
+
# Build a full PID→(PPID, args) map for ancestor lookups
- all_procs: dict[int, tuple[int, str]] = {}
- for line in result.stdout.strip().splitlines():
- parsed = _parse_ps_line(line)
- if parsed is not None:
- pid, ppid, _, _, _, args = parsed
- all_procs[pid] = (ppid, args)
+ all_procs: dict[int, tuple[int, str]] = {pid: (ppid, args) for pid, ppid, _, _, _, args in parsed}
own_tree = _get_own_process_tree()
repo_str = str(repo_root)
repo_prefix = repo_str + "/"
- processes: list[DevProcess] = []
- for line in result.stdout.strip().splitlines():
- parsed = _parse_ps_line(line)
- if parsed is None:
+ # Cheap first pass: keep processes that either name the repo path directly, or
+ # are a known dev executable whose cwd still needs checking. Defer the cwd
+ # lookups so they resolve in a single batched lsof call below instead of
+ # spawning lsof once per process — the dominant cost on a busy machine.
+ candidates: list[tuple[_PsLine, bool]] = []
+ cwd_pids: list[int] = []
+ for entry in parsed:
+ pid, _, _, _, _, args = entry
+ if pid in own_tree or _is_excluded(args):
continue
+ if _matches_repo_path(args, repo_str, repo_prefix):
+ candidates.append((entry, True))
+ elif _has_known_executable(args):
+ candidates.append((entry, False))
+ cwd_pids.append(pid)
- pid, ppid, cpu, rss, start_time, args = parsed
+ cwd_by_pid = _get_process_cwds(cwd_pids)
- if pid in own_tree:
- continue
-
- if _is_excluded(args):
- continue
+ processes: list[DevProcess] = []
+ for entry, repo_match in candidates:
+ pid, ppid, cpu, rss, start_time, args = entry
- # Primary check: repo root appears in the command line as an exact path
- if not _matches_repo_path(args, repo_str, repo_prefix):
- # Secondary check: cwd is under repo root (for known executable types)
- if not _has_known_executable(args):
- continue
- cwd = _get_process_cwd(pid)
+ if not repo_match:
+ cwd = cwd_by_pid.get(pid)
if cwd is None or not (cwd == repo_str or cwd.startswith(repo_prefix)):
continue
@@ -1198,7 +1203,10 @@ def _identify_manager(args: str) -> str | None:
return None
-def _parse_ps_line(line: str) -> tuple[int, int, float, int, str, str] | None:
+_PsLine = tuple[int, int, float, int, str, str]
+
+
+def _parse_ps_line(line: str) -> _PsLine | None:
"""Parse a single ps output line into (pid, ppid, cpu%, rss_kb, start_time, args)."""
parts = line.split()
@@ -1277,25 +1285,39 @@ def _has_known_executable(args: str) -> bool:
return first_word in known
-def _get_process_cwd(pid: int) -> str | None:
- """Get the working directory of a process via lsof."""
+def _get_process_cwds(pids: Sequence[int]) -> dict[int, str]:
+ """Map each pid to its working directory via a single lsof call.
+
+ ``-a`` ANDs the ``-p`` (pid set) and ``-d cwd`` (fd) selectors; without it
+ lsof ORs them and reports *every* process's cwd — slow (a full-system scan
+ per pid) and wrong (attributing the first process's cwd to the queried pid).
+ ``-Fpn`` emits pid/name fields so each cwd attributes back to its pid; with
+ ``-d cwd`` each process yields exactly one name line.
+ """
+ if not pids:
+ return {}
try:
result = subprocess.run(
- ["lsof", "-p", str(pid), "-d", "cwd", "-Fn"],
+ ["lsof", "-a", "-p", ",".join(str(pid) for pid in pids), "-d", "cwd", "-Fpn"],
capture_output=True,
text=True,
check=False,
)
except FileNotFoundError:
- return None
- if result.returncode != 0:
- return None
+ return {}
+ # lsof exits non-zero when any queried pid has vanished, but still emits valid
+ # records for the survivors — parse whatever stdout we got.
+ cwds: dict[int, str] = {}
+ current: int | None = None
for line in result.stdout.splitlines():
- if line.startswith("n"):
- return line[1:]
- return None
+ tag, value = line[:1], line[1:]
+ if tag == "p":
+ current = int(value) if value.isdigit() else None
+ elif tag == "n" and current is not None:
+ cwds[current] = value
+ return cwds
def _extract_process_name(args: str) -> str:
@@ -1717,43 +1739,47 @@ def _print_check_result(result: CheckResult) -> None:
click.echo(f"{'':>30}{result.remediation}")
[email protected](name="doctor", help="Quick health check for your dev environment")
-def doctor() -> None:
- """Run non-destructive checks and print a status summary."""
- from hogli.manifest import REPO_ROOT
-
- click.echo("\nhogli doctor\n")
-
+def _run_checks(repo_root: Path) -> list[CheckResult]:
+ """Run all health checks concurrently and return results in declared order."""
checks: list[Callable[[], CheckResult]] = [
- lambda: _check_disk(REPO_ROOT),
- lambda: _check_zombies(REPO_ROOT),
- lambda: _check_docker(),
- lambda: _check_migrations(),
+ lambda: _check_disk(repo_root),
+ lambda: _check_zombies(repo_root),
+ _check_docker,
+ _check_migrations,
]
- # Run all checks concurrently — each is I/O-bound and independent.
+ # Each check is I/O-bound and independent, so fan them out across threads.
results: list[CheckResult | None] = [None] * len(checks)
with ThreadPoolExecutor(max_workers=len(checks)) as pool:
future_to_idx = {pool.submit(fn): i for i, fn in enumerate(checks)}
for future in as_completed(future_to_idx):
+ idx = future_to_idx[future]
try:
- results[future_to_idx[future]] = future.result()
+ results[idx] = future.result()
except Exception as e:
- idx = future_to_idx[future]
results[idx] = CheckResult(
name=f"Check {idx + 1}",
status=CheckStatus.ERROR,
summary=f"Check failed with error: {str(e)}",
)
+ return [r for r in results if r is not None]
+
+
[email protected](name="doctor", help="Quick health check for your dev environment")
+def doctor() -> None:
+ """Run non-destructive checks and print a status summary."""
+ click.echo("\nhogli doctor\n")
+
+ results = _run_checks(REPO_ROOT)
+
for result in results:
- assert result is not None
_print_check_result(result)
click.echo()
- warnings = sum(1 for r in results if r is not None and r.status == CheckStatus.WARNING)
- errors = sum(1 for r in results if r is not None and r.status == CheckStatus.ERROR)
+ warnings = sum(1 for r in results if r.status == CheckStatus.WARNING)
+ errors = sum(1 for r in results if r.status == CheckStatus.ERROR)
if warnings == 0 and errors == 0:
click.secho(" All checks passed.", fg="green")
else:
@@ -1767,3 +1793,384 @@ def doctor() -> None:
click.echo()
hints.record_check_run("doctor")
+
+
+# ---------------------------------------------------------------------------
+# doctor:report — paste-ready diagnostics dump for devex triage
+# ---------------------------------------------------------------------------
+
+
+def _run_output(cmd: Sequence[str], timeout: float = 5.0) -> str | None:
+ """Run a command and return its trimmed stdout, or None on any failure."""
+ try:
+ result = subprocess.run(list(cmd), capture_output=True, text=True, timeout=timeout, check=False)
+ except (FileNotFoundError, OSError, subprocess.SubprocessError):
+ return None
+ if result.returncode != 0:
+ return None
+ return result.stdout.strip() or None
+
+
+def _format_kv_block(pairs: Sequence[tuple[str, str]]) -> list[str]:
+ """Render label/value pairs as left-aligned ``label value`` lines."""
+ width = max((len(label) for label, _ in pairs), default=0)
+ return [f"{label.ljust(width)} {value}" for label, value in pairs]
+
+
+def _system_info() -> list[tuple[str, str]]:
+ """Collect OS, shell, and terminal context (terminal matters for TUI bugs)."""
+ if platform.system() == "Darwin":
+ mac_ver = platform.mac_ver()[0]
+ os_desc = f"macOS {mac_ver} (Darwin {platform.release()})" if mac_ver else f"Darwin {platform.release()}"
+ else:
+ os_desc = platform.platform()
+ return [
+ ("os", os_desc),
+ ("arch", platform.machine()),
+ ("shell", os.environ.get("SHELL", "unknown")),
+ ("term", os.environ.get("TERM", "unset")),
+ ("term_program", os.environ.get("TERM_PROGRAM", "unset")),
+ ("locale", os.environ.get("LANG", "unset")),
+ ]
+
+
+def _repo_info(repo_root: Path) -> list[tuple[str, str]]:
+ """Collect git branch, HEAD, and working-tree dirtiness."""
+ repo_str = str(repo_root)
+ git = ["git", "-C", repo_str]
+ branch = _run_output([*git, "rev-parse", "--abbrev-ref", "HEAD"]) or "unknown"
+ commit = _run_output([*git, "log", "-1", "--format=%h %s"]) or "unknown"
+ status = _run_output([*git, "status", "--porcelain"])
+ dirty_count = len(status.splitlines()) if status else 0
+
+ pairs = [
+ ("path", repo_str),
+ ("branch", branch),
+ ("commit", commit),
+ ("dirty", "clean" if dirty_count == 0 else f"{dirty_count} uncommitted file(s)"),
+ ]
+ if ".claude/worktrees/" in repo_str:
+ pairs.append(("worktree", "yes"))
+ return pairs
+
+
+def _normalize_arch(value: str) -> str:
+ """Map the many spellings of an architecture to a canonical token."""
+ v = value.lower()
+ if v in {"arm64", "aarch64"}:
+ return "arm64"
+ if v in {"x86_64", "x86-64", "amd64"}:
+ return "x86_64"
+ return v
+
+
+def _binary_arches(path: str) -> set[str]:
+ """Best-effort set of architectures a binary contains, via ``file``.
+
+ Empty when ``file`` is unavailable or the arch is indeterminate — callers
+ must treat empty as "unknown", not "mismatch", to avoid false alarms.
+ """
+ out = _run_output(["file", "-b", path])
+ if not out:
+ return set()
+ lowered = out.lower()
+ return {_normalize_arch(token) for token in ("arm64", "aarch64", "x86_64", "x86-64", "amd64") if token in lowered}
+
+
+def _phrocs_info() -> tuple[str, str]:
+ """Diagnose phrocs, which powers the ``hogli start`` TUI.
+
+ A blank ``hogli start`` screen most often traces back to phrocs being
+ missing, present-but-unrunnable, or built for the wrong architecture
+ (e.g. an x86_64 binary under Rosetta on Apple Silicon), so surface all
+ three explicitly rather than just the version.
+ """
+ path = shutil.which("phrocs")
+ if not path:
+ return ("phrocs", "MISSING — `hogli start` TUI needs it (see tools/phrocs/install.sh)")
+
+ version = _run_output([path, "--version"]) or "installed (--version failed — binary may be broken)"
+
+ arches = _binary_arches(path)
+ host = _normalize_arch(platform.machine())
+ arch_label = f" [{'/'.join(sorted(arches))}]" if arches else ""
+ mismatch = f" — ARCH MISMATCH vs host {host}; likely blank-TUI cause" if arches and host not in arches else ""
+
+ return ("phrocs", f"{version}{arch_label} ({path}){mismatch}")
+
+
+def _toolchain_info(repo_root: Path) -> list[tuple[str, str]]:
+ """Collect runtime/toolchain versions relevant to running the dev stack."""
+ pairs: list[tuple[str, str]] = [("python", f"{platform.python_version()} ({sys.executable})")]
+
+ node = _run_output(["node", "--version"])
+ if node:
+ nvmrc = repo_root / ".nvmrc"
+ expected = nvmrc.read_text().strip() if nvmrc.exists() else ""
+ suffix = ""
+ if expected:
+ actual_major = node.lstrip("v").split(".")[0]
+ expected_major = expected.lstrip("v").split(".")[0]
+ suffix = " (.nvmrc ok)" if actual_major == expected_major else f" (.nvmrc wants {expected})"
+ pairs.append(("node", f"{node}{suffix}"))
+ else:
+ pairs.append(("node", "not found"))
+
+ pairs.append(("pnpm", _run_output(["pnpm", "--version"]) or "not found"))
+
+ flox_ver = _run_output(["flox", "--version"])
+ if os.environ.get("FLOX_ENV_DESCRIPTION") or os.environ.get("FLOX_ENV"):
+ pairs.append(("flox", f"active — {flox_ver}" if flox_ver else "active"))
+ elif flox_ver:
+ pairs.append(("flox", f"not active ({flox_ver} on PATH)"))
+ else:
+ pairs.append(("flox", "not on PATH"))
+
+ docker_ver = _run_output(["docker", "version", "--format", "{{.Server.Version}}"], timeout=3.0)
+ pairs.append(("docker", f"{docker_ver} (daemon running)" if docker_ver else "daemon not responding"))
+
+ pairs.append(_phrocs_info())
+
+ return pairs
+
+
+def _checks_info(repo_root: Path) -> list[tuple[str, str]]:
+ """Run the doctor health checks and flatten them into label/value pairs."""
+ pairs: list[tuple[str, str]] = []
+ for result in _run_checks(repo_root):
+ value = f"{_STATUS_LABELS[result.status]} ({result.summary})"
+ if result.remediation:
+ value += f" → {result.remediation}"
+ pairs.append((result.name.lower().replace(" ", "_"), value))
+ return pairs
+
+
+def _generated_config_path(repo_root: Path) -> Path:
+ """Resolve the generated mprocs config phrocs renders, read-only.
+
+ Mirrors ``hogli``'s lookup (the ``HOGLI_MPROCS_PATH`` override, else
+ ``.posthog/.generated/mprocs.yaml`` at the repo root) without the worktree
+ symlink-creating fallback — a diagnostic must not mutate the workspace.
+ """
+ override = os.environ.get("HOGLI_MPROCS_PATH")
+ if override:
+ return Path(override)
+ return repo_root / ".posthog" / ".generated" / "mprocs.yaml"
+
+
+def _phrocs_socket_path(repo_root: Path) -> Path:
+ """Replicate phrocs' per-workspace IPC socket path (ipc.SocketPathFor):
+ ``/tmp/phrocs-<first 4 bytes of sha256(real absolute cwd)>.sock``.
+ """
+ real = os.path.realpath(repo_root)
+ digest = hashlib.sha256(real.encode()).digest()[:4].hex()
+ return Path(f"/tmp/phrocs-{digest}.sock")
+
+
+def _config_procs(config: Path) -> str:
+ """Summarize a generated mprocs config's proc set; a parse failure is itself
+ the diagnosis, since phrocs can't render an unparseable config."""
+ try:
+ data = yaml.safe_load(config.read_text())
+ except (OSError, yaml.YAMLError) as exc:
+ return f"unparseable: {str(exc)[:60]}"
+ procs = data.get("procs") if isinstance(data, dict) else None
+ if not isinstance(procs, dict):
+ return "no procs"
+ return f"{len(procs)} procs"
+
+
+def _tail(path: Path, lines: int) -> list[str]:
+ """Last ``lines`` lines of a text file, or [] if it can't be read."""
+ try:
+ content = path.read_text(errors="replace")
+ except OSError:
+ return []
+ return content.splitlines()[-lines:]
+
+
+def _iso_mtime(path: Path) -> str:
+ # Diagnostics run when the env misbehaves, so the file can vanish between an
+ # exists() check and this stat() — degrade instead of sinking the report.
+ try:
+ return datetime.fromtimestamp(path.stat().st_mtime, UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
+ except OSError:
+ return "unavailable"
+
+
+def _phrocs_runtime_pairs(repo_root: Path) -> list[tuple[str, str]]:
+ """phrocs/start runtime state — the layer past the binary that explains a
+ blank or broken ``hogli start`` TUI: the generated config phrocs renders,
+ its IPC socket, and the terminal it draws into.
+ """
+ config = _generated_config_path(repo_root)
+ pairs: list[tuple[str, str]] = []
+
+ if config.exists():
+ pairs.append(("generated_config", f"{config} ({_config_procs(config)}, {_iso_mtime(config)})"))
+ else:
+ pairs.append(("generated_config", f"MISSING ({config}) — run `hogli dev:generate`"))
+
+ log = config.parent / "logs" / "phrocs.log"
+ pairs.append(("phrocs_log", f"{log} ({_iso_mtime(log)})" if log.exists() else f"absent ({log})"))
+
+ socket = _phrocs_socket_path(repo_root)
+ pairs.append(
+ ("ipc_socket", f"present ({socket}) — stale if no phrocs running" if socket.exists() else f"none ({socket})")
+ )
+
+ pairs.append(("stdout_tty", "yes" if sys.stdout.isatty() else "no — phrocs opens /dev/tty as a fallback"))
+ try:
+ size = os.get_terminal_size()
+ pairs.append(("terminal_size", f"{size.columns}x{size.lines}"))
+ except OSError:
+ pairs.append(("terminal_size", "unavailable (no controlling terminal)"))
+
+ return pairs
+
+
+def _phrocs_runtime_block(repo_root: Path) -> list[str]:
+ """The full verbose ``phrocs runtime`` section body: state pairs plus a tail
+ of phrocs' own log (the highest-signal evidence when it started then died)."""
+ block = _format_kv_block(_phrocs_runtime_pairs(repo_root))
+ tail = _tail(_generated_config_path(repo_root).parent / "logs" / "phrocs.log", 15)
+ if tail:
+ block.append("")
+ block.append("phrocs.log (last 15 lines):")
+ block.extend(f" {line}" for line in tail)
+ return block
+
+
+class _ManifestLike(Protocol):
+ """The slice of ``hogli.manifest.Manifest`` the import probe relies on."""
+
+ config: dict
+
+ def get_all_commands(self) -> list[str]: ...
+
+ def get_command_config(self, command_name: str) -> dict | None: ...
+
+
+def _collect_import_targets(manifest: _ManifestLike) -> list[tuple[str, str, str | None]]:
+ """Enumerate ``(label, module_path, attr)`` for every importable command + boot module.
+
+ ``attr`` is the click command symbol for ``click:`` entries, or ``None`` for
+ boot modules (which we only need to import, not resolve an attribute on).
+ Command discovery and the metadata/config skip rules are delegated to the
+ manifest API so this stays in lockstep with the framework's schema.
+ """
+ targets: list[tuple[str, str, str | None]] = []
+
+ for cmd_name in manifest.get_all_commands():
+ click_str = (manifest.get_command_config(cmd_name) or {}).get("click")
+ if isinstance(click_str, str) and click_str.count(":") == 1:
+ module_path, attr = click_str.split(":", 1)
+ if module_path and attr:
+ targets.append((cmd_name, module_path, attr))
+
+ for module_path in manifest.config.get("boot_modules", []) or []:
+ if isinstance(module_path, str) and module_path:
+ targets.append((module_path, module_path, None))
+
+ return targets
+
+
+def _probe_command_imports(manifest: _ManifestLike) -> tuple[int, list[tuple[str, str]]]:
+ """Import every command + boot module, returning ``(probed, failures)``.
+
+ Surfaces broken modules that would otherwise fail opaquely at invoke time —
+ a common cause of confusing ``hogli`` startup behaviour.
+ """
+ targets = _collect_import_targets(manifest)
+ module_cache: dict[str, object] = {}
+ failures: list[tuple[str, str]] = []
+
+ for label, module_path, attr in targets:
+ if module_path not in module_cache:
+ try:
+ module_cache[module_path] = importlib.import_module(module_path)
+ except Exception as exc:
+ module_cache[module_path] = exc
+ module = module_cache[module_path]
+ if isinstance(module, Exception):
+ failures.append((label, f"{type(module).__name__}: {str(module)[:120]}"))
+ elif attr is not None and not hasattr(module, attr):
+ failures.append((label, f"missing attribute '{attr}'"))
+
+ return len(targets), failures
+
+
+def _build_report(repo_root: Path, manifest: _ManifestLike) -> str:
+ """Assemble the full diagnostics report body (no surrounding code fence)."""
+ timestamp = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
+ lines: list[str] = [f"hogli doctor:report — {timestamp}"]
+
+ # Each collector shells out to independent binaries (git, node, docker, …),
+ # so gather them concurrently — a single hung tool can't serialize the whole
+ # report, which matters since this runs precisely when the env misbehaves.
+ sections: list[tuple[str, Callable[[], list[tuple[str, str]]]]] = [
+ ("System", _system_info),
+ ("Repo", lambda: _repo_info(repo_root)),
+ ("Toolchain", lambda: _toolchain_info(repo_root)),
+ ("Checks", lambda: _checks_info(repo_root)),
+ ]
+ collected: list[list[tuple[str, str]]] = [[] for _ in sections]
+ with ThreadPoolExecutor(max_workers=len(sections) + 1) as pool:
+ # The import probe is independent of the section collectors, so run it in
+ # the same batch rather than serially afterwards — it overlaps with the
+ # slowest section instead of adding its cost on top.
+ probe_future = pool.submit(_probe_command_imports, manifest)
+ futures = {pool.submit(collect): i for i, (_, collect) in enumerate(sections)}
+ for future in as_completed(futures):
+ idx = futures[future]
+ try:
+ collected[idx] = future.result()
+ except Exception as exc:
+ # A diagnostic is run precisely when the env misbehaves — one
+ # collector blowing up must not sink the whole report.
+ collected[idx] = [("error", f"{type(exc).__name__}: {str(exc)[:120]}")]
+ try:
+ probed, failures = probe_future.result()
+ except Exception as exc:
+ # Same invariant as the section collectors above: the import probe
+ # blowing up must not sink the whole report.
+ probed, failures = 0, [("import probe", f"{type(exc).__name__}: {str(exc)[:120]}")]
+
+ for (title, _), pairs in zip(sections, collected):
+ lines.append("")
+ lines.append(f"== {title} ==")
+ lines.extend(_format_kv_block(pairs))
+
+ # phrocs is the headline failure mode (blank `hogli start`), so group its
+ # runtime state right after the toolchain/health sections.
+ lines.append("")
+ lines.append("== phrocs runtime ==")
+ lines.extend(_phrocs_runtime_block(repo_root))
+
+ lines.append("")
+ lines.append("== Command imports ==")
+ if not failures:
+ lines.append(f"probed {probed} target(s): all import OK")
+ else:
+ lines.append(f"probed {probed} target(s): {len(failures)} FAILED")
+ lines.extend(f" {label}: {error}" for label, error in failures)
+
+ return "\n".join(lines)
+
+
+ name="doctor:report",
+ help="Dump a paste-ready dev-environment diagnostics report for devex triage",
+)
+def doctor_report() -> None:
+ """Collect environment, toolchain, health-check, command-import, and phrocs
+ runtime diagnostics into a single copy-paste block to share with the devex team."""
+ click.echo("\nGathering diagnostics…\n")
+ report = _build_report(REPO_ROOT, get_manifest())
+
+ click.echo("```text")
+ click.echo(report)
+ click.echo("```")
+ click.echo()
+
+ hints.record_check_run("doctor:report")
diff --git a/tools/hogli-commands/hogli_commands/hints.py b/tools/hogli-commands/hogli_commands/hints.py
index 6fb27b95c0d4..9ff3245b3b17 100644
--- a/tools/hogli-commands/hogli_commands/hints.py
+++ b/tools/hogli-commands/hogli_commands/hints.py
@@ -26,6 +26,7 @@ _SKIP_COMMANDS = frozenset(
"doctor",
"doctor:disk",
"doctor:zombies",
+ "doctor:report",
"telemetry:off",
"telemetry:on",
"telemetry:status",
diff --git a/tools/hogli-commands/hogli_commands/tests/test_doctor.py b/tools/hogli-commands/hogli_commands/tests/test_doctor.py
index 2ca0b7733858..ba99e701c768 100644
--- a/tools/hogli-commands/hogli_commands/tests/test_doctor.py
+++ b/tools/hogli-commands/hogli_commands/tests/test_doctor.py
@@ -1,6 +1,25 @@
+import os
+import hashlib
+from pathlib import Path
+from types import SimpleNamespace
+
import pytest
-from hogli_commands.doctor import _is_excluded
+from hogli_commands.doctor import (
+ _binary_arches,
+ _collect_import_targets,
+ _config_procs,
+ _format_kv_block,
+ _generated_config_path,
+ _get_process_cwds,
+ _is_excluded,
+ _normalize_arch,
+ _phrocs_info,
+ _phrocs_runtime_pairs,
+ _phrocs_socket_path,
+ _probe_command_imports,
+ _tail,
+)
@pytest.mark.parametrize(
@@ -56,3 +75,269 @@ def test_is_excluded_does_not_match_posthog_processes(args: str) -> None:
def test_is_excluded_empty_string() -> None:
assert _is_excluded("") is False
+
+
+ ("pairs", "expected"),
+ [
+ pytest.param(
+ [("os", "macOS"), ("term_program", "iTerm")],
+ ["os macOS", "term_program iTerm"],
+ id="aligns-values",
+ ),
+ pytest.param([], [], id="empty"),
+ ],
+)
+def test_format_kv_block(pairs: list[tuple[str, str]], expected: list[str]) -> None:
+ assert _format_kv_block(pairs) == expected
+
+
+class _FakeManifest:
+ """Minimal stand-in for ``hogli.manifest.Manifest`` (structural match)."""
+
+ def __init__(self, commands: dict[str, dict], boot_modules: list[str] | None = None) -> None:
+ self._commands = commands
+ self.config = {"boot_modules": boot_modules or []}
+
+ def get_all_commands(self) -> list[str]:
+ return list(self._commands)
+
+ def get_command_config(self, command_name: str) -> dict | None:
+ return self._commands.get(command_name)
+
+
+def test_collect_import_targets_extracts_click_and_boot_modules() -> None:
+ manifest = _FakeManifest(
+ {
+ "doctor": {"click": "hogli_commands.doctor:doctor"},
+ "doctor:report": {"click": "hogli_commands.doctor:doctor_report"},
+ "noclick": {"cmd": "echo hi"},
+ "bad:format": {"click": "no_colon_here"},
+ },
+ boot_modules=["hogli_commands.prechecks"],
+ )
+ targets = _collect_import_targets(manifest)
+
+ assert ("doctor", "hogli_commands.doctor", "doctor") in targets
+ assert ("doctor:report", "hogli_commands.doctor", "doctor_report") in targets
+ # Boot modules import the module with no attribute to resolve.
+ assert ("hogli_commands.prechecks", "hogli_commands.prechecks", None) in targets
+ # cmd-only and malformed click strings are skipped.
+ assert not any(label == "noclick" for label, _, _ in targets)
+ assert not any(label == "bad:format" for label, _, _ in targets)
+
+
+ ("commands", "expected_failure"),
+ [
+ pytest.param(
+ {"doctor": {"click": "hogli_commands.doctor:doctor"}},
+ None,
+ id="all-ok",
+ ),
+ pytest.param(
+ {"ghost": {"click": "hogli_commands.does_not_exist:thing"}},
+ ("ghost", "ModuleNotFoundError"),
+ id="missing-module",
+ ),
+ pytest.param(
+ {"typo": {"click": "hogli_commands.doctor:not_a_real_command"}},
+ ("typo", "missing attribute 'not_a_real_command'"),
+ id="missing-attribute",
+ ),
+ ],
+)
+def test_probe_command_imports(commands: dict[str, dict], expected_failure: tuple[str, str] | None) -> None:
+ probed, failures = _probe_command_imports(_FakeManifest(commands))
+ assert probed == 1
+ if expected_failure is None:
+ assert failures == []
+ return
+ assert len(failures) == 1
+ label, error = failures[0]
+ assert label == expected_failure[0]
+ assert expected_failure[1] in error
+
+
+def test_get_process_cwds_skips_lsof_for_empty_input(monkeypatch: pytest.MonkeyPatch) -> None:
+ def fail(*_args: object, **_kwargs: object) -> SimpleNamespace:
+ raise AssertionError("lsof must not run for an empty pid list")
+
+ monkeypatch.setattr("hogli_commands.doctor.subprocess.run", fail)
+ assert _get_process_cwds([]) == {}
+
+
+def test_get_process_cwds_parses_and_batches(monkeypatch: pytest.MonkeyPatch) -> None:
+ captured: dict[str, object] = {}
+
+ def fake_run(cmd: list[str], **_kwargs: object) -> SimpleNamespace:
+ captured["cmd"] = cmd
+ return SimpleNamespace(returncode=1, stdout="p100\nn/repo/a\np200\nn/repo/b\n")
+
+ monkeypatch.setattr("hogli_commands.doctor.subprocess.run", fake_run)
+
+ # Non-zero rc (a pid vanished) must not discard the records that did resolve.
+ assert _get_process_cwds([100, 200]) == {100: "/repo/a", 200: "/repo/b"}
+ # One batched, ANDed lsof call covering every pid — not one call per pid.
+ cmd = captured["cmd"]
+ assert isinstance(cmd, list)
+ assert cmd[:2] == ["lsof", "-a"]
+ assert "100,200" in cmd
+
+
+ ("value", "expected"),
+ [
+ ("arm64", "arm64"),
+ ("aarch64", "arm64"),
+ ("x86_64", "x86_64"),
+ ("x86-64", "x86_64"),
+ ("amd64", "x86_64"),
+ ("AMD64", "x86_64"),
+ ("riscv64", "riscv64"),
+ ],
+)
+def test_normalize_arch(value: str, expected: str) -> None:
+ assert _normalize_arch(value) == expected
+
+
+def test_binary_arches_parses_file_output(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(
+ "hogli_commands.doctor._run_output",
+ lambda *_: "Mach-O 64-bit executable arm64",
+ )
+ assert _binary_arches("/opt/homebrew/bin/phrocs") == {"arm64"}
+
+
+def test_binary_arches_handles_universal_binary(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(
+ "hogli_commands.doctor._run_output",
+ lambda *_: "Mach-O universal binary with 2 architectures: [x86_64] [arm64]",
+ )
+ assert _binary_arches("/usr/local/bin/phrocs") == {"x86_64", "arm64"}
+
+
+def test_binary_arches_empty_when_file_unavailable(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("hogli_commands.doctor._run_output", lambda *_: None)
+ assert _binary_arches("/opt/homebrew/bin/phrocs") == set()
+
+
+def _patch_phrocs(
+ monkeypatch: pytest.MonkeyPatch,
+ *,
+ which: str | None,
+ version: str | None,
+ file_out: str | None,
+ machine: str,
+) -> None:
+ monkeypatch.setattr("hogli_commands.doctor.shutil.which", lambda _: which)
+ monkeypatch.setattr("hogli_commands.doctor.platform.machine", lambda: machine)
+
+ def fake_run_output(cmd: list[str], *_a: object, **_k: object) -> str | None:
+ return file_out if cmd[0] == "file" else version
+
+ monkeypatch.setattr("hogli_commands.doctor._run_output", fake_run_output)
+
+
+def test_phrocs_info_missing(monkeypatch: pytest.MonkeyPatch) -> None:
+ _patch_phrocs(monkeypatch, which=None, version=None, file_out=None, machine="arm64")
+ _, value = _phrocs_info()
+ assert value.startswith("MISSING")
+
+
+def test_phrocs_info_healthy_matching_arch(monkeypatch: pytest.MonkeyPatch) -> None:
+ _patch_phrocs(
+ monkeypatch,
+ which="/opt/homebrew/bin/phrocs",
+ version="phrocs 1.0.8 (abc, 2026-04-14)",
+ file_out="Mach-O 64-bit executable arm64",
+ machine="arm64",
+ )
+ _, value = _phrocs_info()
+ assert "phrocs 1.0.8" in value
+ assert "[arm64]" in value
+ assert "MISMATCH" not in value
+
+
+def test_phrocs_info_flags_arch_mismatch(monkeypatch: pytest.MonkeyPatch) -> None:
+ _patch_phrocs(
+ monkeypatch,
+ which="/usr/local/bin/phrocs",
+ version="phrocs 1.0.8 (abc, 2026-04-14)",
+ file_out="Mach-O 64-bit executable x86_64",
+ machine="arm64",
+ )
+ _, value = _phrocs_info()
+ assert "ARCH MISMATCH vs host arm64" in value
+
+
+def test_phrocs_info_flags_broken_binary(monkeypatch: pytest.MonkeyPatch) -> None:
+ _patch_phrocs(
+ monkeypatch,
+ which="/opt/homebrew/bin/phrocs",
+ version=None,
+ file_out="Mach-O 64-bit executable arm64",
+ machine="arm64",
+ )
+ _, value = _phrocs_info()
+ assert "--version failed" in value
+
+
+def test_generated_config_path_honors_override(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
+ monkeypatch.setenv("HOGLI_MPROCS_PATH", "/custom/mprocs.yaml")
+ assert _generated_config_path(tmp_path) == Path("/custom/mprocs.yaml")
+
+
+def test_generated_config_path_default(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
+ monkeypatch.delenv("HOGLI_MPROCS_PATH", raising=False)
+ assert _generated_config_path(tmp_path) == tmp_path / ".posthog" / ".generated" / "mprocs.yaml"
+
+
+def test_phrocs_socket_path_matches_phrocs_formula(tmp_path: Path) -> None:
+ real = os.path.realpath(tmp_path)
+ expected = "/tmp/phrocs-" + hashlib.sha256(real.encode()).digest()[:4].hex() + ".sock"
+ socket = _phrocs_socket_path(tmp_path)
+ assert str(socket) == expected
+ assert socket == _phrocs_socket_path(tmp_path) # stable
+
+
+ ("content", "expected"),
+ [
+ pytest.param("procs:\n a: {}\n b: {}\n", "2 procs", id="counts-procs"),
+ pytest.param("_posthog: {}\n", "no procs", id="no-procs-key"),
+ pytest.param("procs: not-a-mapping\n", "no procs", id="procs-not-mapping"),
+ pytest.param("procs:\n - [unbalanced\n", "unparseable", id="malformed-yaml"),
+ ],
+)
+def test_config_procs(tmp_path: Path, content: str, expected: str) -> None:
+ config = tmp_path / "mprocs.yaml"
+ config.write_text(content)
+ assert _config_procs(config).startswith(expected)
+
+
+def test_tail_returns_last_lines(tmp_path: Path) -> None:
+ log = tmp_path / "phrocs.log"
+ log.write_text("\n".join(f"line {i}" for i in range(20)))
+ assert _tail(log, 3) == ["line 17", "line 18", "line 19"]
+
+
+def test_tail_missing_file_is_empty(tmp_path: Path) -> None:
+ assert _tail(tmp_path / "nope.log", 5) == []
+
+
+def test_phrocs_runtime_pairs_reports_state(tmp_path: Path) -> None:
+ config = tmp_path / ".posthog" / ".generated" / "mprocs.yaml"
+ config.parent.mkdir(parents=True)
+ config.write_text("procs:\n backend: {}\n frontend: {}\n capture: {}\n")
+
+ pairs = dict(_phrocs_runtime_pairs(tmp_path))
+ assert set(pairs) == {"generated_config", "phrocs_log", "ipc_socket", "stdout_tty", "terminal_size"}
+ assert "3 procs" in pairs["generated_config"]
+ assert "absent" in pairs["phrocs_log"]
+
+
+def test_phrocs_runtime_pairs_flags_missing_config(tmp_path: Path) -> None:
+ pairs = dict(_phrocs_runtime_pairs(tmp_path))
+ assert "MISSING" in pairs["generated_config"]
+ assert "hogli dev:generate" in pairs["generated_config"]