Skip to content

clabe.reports

Debug bundles and issue reports.

A bundle is one zip of everything needed to understand a run so far: its records (run.json, events.jsonl, outputs.jsonl, launcher.log), the tail of this machine's daemon log and a rig.json describing the machine. Users download it (Export logs) or file it as a report with a comment (Report an issue): a report is saved on this machine, under reports_dir, until the hub collects it (and then deletes it here).

ReportInfo

Bases: BaseModel

What a report is about: who, where, when, and the user's comment. Stored as report.json in its zip.

reports_dir

reports_dir() -> Path

Where this machine keeps reports until the hub collects them (%LOCALAPPDATA%\clabe\reports).

Source code in src/clabe/reports.py
def reports_dir() -> Path:
    """Where this machine keeps reports until the hub collects them (``%LOCALAPPDATA%\\clabe\\reports``)."""
    base = os.environ.get("LOCALAPPDATA")
    return (Path(base) / "clabe" if base else Path.home() / ".clabe") / "reports"

rig_info

rig_info() -> dict[str, Any]

The machine, as put in rig.json: host, OS, Python, clabe, disk, time.

Source code in src/clabe/reports.py
def rig_info() -> dict[str, Any]:
    """The machine, as put in ``rig.json``: host, OS, Python, clabe, disk, time."""
    from . import __version__

    usage = shutil.disk_usage(Path.cwd().anchor or "/")
    return {
        "hostname": socket.gethostname(),
        "platform": platform.platform(),
        "python": platform.python_version(),
        "clabe_version": __version__,
        "user": os.environ.get("USERNAME") or os.environ.get("USER"),
        "cwd": str(Path.cwd()),
        "disk_free_bytes": usage.free,
        "disk_total_bytes": usage.total,
        "time": datetime.datetime.now(datetime.UTC).isoformat(),
    }

make_bundle

make_bundle(
    record_dir: Path | None,
    *,
    extra: dict[str, bytes | str] | None = None,
) -> bytes

A zip of the run's records so far, the daemon log's tail, rig.json and extra files (name → content).

Missing files are skipped: a bundle can be made at any moment of a run, or with no run at all.

Source code in src/clabe/reports.py
def make_bundle(record_dir: Path | None, *, extra: dict[str, bytes | str] | None = None) -> bytes:
    """A zip of the run's records so far, the daemon log's tail, ``rig.json`` and ``extra`` files (name → content).

    Missing files are skipped: a bundle can be made at any moment of a run, or with no run at all.
    """
    from .daemon._install import default_working_directory

    buffer = io.BytesIO()
    with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as archive:
        if record_dir is not None:
            for name in RECORD_FILES:
                path = Path(record_dir) / name
                if path.is_file():
                    archive.write(path, name)
        daemon_log = default_working_directory() / "daemon.log"
        if daemon_log.is_file():
            with daemon_log.open("rb") as stream:
                stream.seek(max(0, daemon_log.stat().st_size - DAEMON_LOG_TAIL))
                archive.writestr("daemon.log", stream.read())
        archive.writestr("rig.json", json.dumps(rig_info(), indent=2))
        for name, content in (extra or {}).items():
            archive.writestr(name, content)
    return buffer.getvalue()

save_report

save_report(
    record_dir: Path | None,
    info: dict[str, Any],
    *,
    directory: Path | None = None,
) -> ReportInfo

Saves a report (a bundle with report.json) under reports_dir for the hub to collect.

Parameters:

Name Type Description Default
record_dir Path | None

The run's record directory, if any.

required
info dict[str, Any]

The ReportInfo fields known to the caller (comment, run, session, ...); the id, time, host and clabe version are filled in.

required
directory Path | None

Where to save it; by default reports_dir.

None
Source code in src/clabe/reports.py
def save_report(record_dir: Path | None, info: dict[str, Any], *, directory: Path | None = None) -> ReportInfo:
    """Saves a report (a bundle with ``report.json``) under `reports_dir` for the hub to collect.

    Args:
        record_dir: The run's record directory, if any.
        info: The `ReportInfo` fields known to the caller (comment, run, session, ...); the id, time, host and
            clabe version are filled in.
        directory: Where to save it; by default `reports_dir`.
    """
    from . import __version__

    report = ReportInfo(
        report_id=uuid.uuid4().hex,
        created_at=datetime.datetime.now(datetime.UTC),
        hostname=socket.gethostname(),
        clabe_version=__version__,
        **info,
    )
    target = directory or reports_dir()
    target.mkdir(parents=True, exist_ok=True)
    data = make_bundle(record_dir, extra={REPORT_FILE: report.model_dump_json(indent=2)})
    partial = target / f"{report.report_id}.zip.partial"
    partial.write_bytes(data)
    partial.replace(target / f"{report.report_id}.zip")  # the hub never sees a half-written report
    return report

describe_launcher

describe_launcher(launcher: object) -> dict[str, object]

Session id, subject, experimenter, repository and commit, from a launcher's report_info (when it has one).

Never raises: a launcher that cannot describe itself gives nothing.

Source code in src/clabe/reports.py
def describe_launcher(launcher: object) -> dict[str, object]:
    """Session id, subject, experimenter, repository and commit, from a launcher's ``report_info`` (when it has one).

    Never raises: a launcher that cannot describe itself gives nothing.
    """
    describe = getattr(launcher, "report_info", None)
    if not callable(describe):
        return {}
    try:
        return dict(describe())
    except Exception:  # never fail a report because the launcher cannot describe itself
        logger.warning("Could not describe the launcher", exc_info=True)
        return {}

report_run

report_run(
    run: Run,
    comment: str = "",
    *,
    reported_from: str = "local",
    directory: Path | None = None,
) -> ReportInfo

Files an issue report about run: its bundle, the comment, and what is known about the run and its launcher.

What every client files (the web UI through POST /api/report, the terminal UI with F3, the exit prompt).

Parameters:

Name Type Description Default
run Run

The run reported on (running or finished).

required
comment str

What went wrong, in the user's words.

''
reported_from str

The client the report is made from.

'local'
directory Path | None

Where to save it; by default reports_dir.

None
Source code in src/clabe/reports.py
def report_run(
    run: "Run", comment: str = "", *, reported_from: str = "local", directory: Path | None = None
) -> ReportInfo:
    """Files an issue report about ``run``: its bundle, the comment, and what is known about the run and its launcher.

    What every client files (the web UI through ``POST /api/report``, the terminal UI with F3, the exit prompt).

    Args:
        run: The run reported on (running or finished).
        comment: What went wrong, in the user's words.
        reported_from: The client the report is made from.
        directory: Where to save it; by default `reports_dir`.
    """
    state = run.state()
    info: dict[str, object] = {
        "comment": comment,
        "reported_from": reported_from,
        "run_id": run.id,
        "experiment": run.workflow.name,
        "step": state.current_step or (state.prompt.step if state.prompt is not None else None),
    }
    info.update(describe_launcher(run.launcher))
    return save_report(run.record_dir, info, directory=directory)

list_reports

list_reports(
    directory: Path | None = None,
) -> list[ReportInfo]

The reports saved on this machine and not collected yet, oldest first.

Source code in src/clabe/reports.py
def list_reports(directory: Path | None = None) -> list[ReportInfo]:
    """The reports saved on this machine and not collected yet, oldest first."""
    target = directory or reports_dir()
    reports = []
    for path in sorted(target.glob("*.zip")) if target.is_dir() else []:
        try:
            with zipfile.ZipFile(path) as archive:
                reports.append(ReportInfo.model_validate_json(archive.read(REPORT_FILE)))
        except (OSError, KeyError, ValueError, zipfile.BadZipFile):
            continue
    return sorted(reports, key=lambda r: r.created_at)

count_reports

count_reports(directory: Path | None = None) -> int

How many reports wait on this machine (their zips, without opening them).

Source code in src/clabe/reports.py
def count_reports(directory: Path | None = None) -> int:
    """How many reports wait on this machine (their zips, without opening them)."""
    target = directory or reports_dir()
    return sum(1 for _ in target.glob("*.zip")) if target.is_dir() else 0

report_path

report_path(
    report_id: str, directory: Path | None = None
) -> Path

The zip of report report_id on this machine (it may not exist).

Raises:

Type Description
ValueError

report_id is not a report id.

Source code in src/clabe/reports.py
def report_path(report_id: str, directory: Path | None = None) -> Path:
    """The zip of report ``report_id`` on this machine (it may not exist).

    Raises:
        ValueError: ``report_id`` is not a report id.
    """
    if not re.fullmatch(REPORT_ID_PATTERN, report_id):
        raise ValueError(f"Not a report id: {report_id!r}")
    return (directory or reports_dir()) / f"{report_id}.zip"

delete_report

delete_report(
    report_id: str, directory: Path | None = None
) -> bool

Deletes report report_id from this machine; False if there was no such report.

Source code in src/clabe/reports.py
def delete_report(report_id: str, directory: Path | None = None) -> bool:
    """Deletes report ``report_id`` from this machine; False if there was no such report."""
    path = report_path(report_id, directory)
    try:
        path.unlink()
    except FileNotFoundError:
        return False
    return True

read_report_info

read_report_info(data: bytes | Path) -> ReportInfo

The ReportInfo of a report's zip (its bytes or its path).

Raises:

Type Description
ValueError

Not a zip, or no valid report.json in it.

Source code in src/clabe/reports.py
def read_report_info(data: bytes | Path) -> ReportInfo:
    """The `ReportInfo` of a report's zip (its bytes or its path).

    Raises:
        ValueError: Not a zip, or no valid ``report.json`` in it.
    """
    try:
        with zipfile.ZipFile(io.BytesIO(data) if isinstance(data, bytes) else data) as archive:
            return ReportInfo.model_validate_json(archive.read(REPORT_FILE))
    except (KeyError, zipfile.BadZipFile) as exc:
        raise ValueError(f"Not a report: {exc}") from exc