#!/usr/bin/env python3
import os
from pathlib import Path

def find_echo_root() -> Path:
    # Highest priority: explicit env
    p = os.environ.get("ECHO_STACK_ROOT")
    if p and Path(p).is_dir():
        return Path(p).resolve()

    # Next: sibling FOREST_REPOS.txt
    here = Path(__file__).resolve()
    for probe in [here.parents[3]/"FOREST_REPOS.txt", Path.home()/ "FOREST_REPOS.txt"]:
        if probe.exists():
            for line in probe.read_text().splitlines():
                line=line.strip()
                if line and "echo_labs" in line:
                    maybe = Path(line.split()[0]).expanduser()
                    if (maybe/"echo").exists():
                        return maybe.resolve()

    # Fallback: common on-box path
    candidate = Path.home()/ "echo_labs"
    if (candidate/"echo").exists():
        return candidate.resolve()

    raise RuntimeError("Could not locate Echo agentic stack. "
                       "Set ECHO_STACK_ROOT=/path/to/echo_labs")
