Skip to content

container_doctor

container_doctor

In-container health checks via the layered doctor protocol.

Entry point: ContainerDoctor. ContainerDoctor(project_name, task_id).run(...) collects checks from terok_sandbox.doctor (network, shield), terok_executor.doctor (bridges, credentials, env), and adds terok-level checks (git identity, remote URL). It executes probes inside the running container via podman exec (under crun) or SSH (under krun), and optionally applies fixes.

All checks run from the host — the container cannot tamper with the diagnostic process.

ContainerDoctor(project_name, task_id) dataclass

Layered in-container health-check orchestrator for one task.

Construct with ContainerDoctor(project_name, task_id) and call :meth:run to execute every probe in sandbox / agent / terok layers against the running container.

The instance carries the (project_name, task_id) pair that names the target task; runtime resolution happens inside :meth:run so the cheap construction is free of subprocess work.

project_name instance-attribute

Project the task belongs to.

task_id instance-attribute

Task identifier whose container is probed.

run(*, fix=False, reporter=None, label_prefix='')

Run every layered health check against this task's container.

Probes are executed via podman exec under crun and over the passt-forwarded SSH transport under krun — whichever booted the container. Optional fixes run only for checks whose verdict reports fixable=True.

When reporter is supplied, progress streams line-by-line and noisy categories (Credential file (...), Phantom token (...), …) coalesce into a single heading. The returned list is empty in that case so the caller doesn't re-print.

label_prefix is prepended to every emitted label — used by the sickbay command to tag multi-task runs with "Task pid/tid: ".

Source code in src/terok/lib/orchestration/container_doctor.py
def run(
    self,
    *,
    fix: bool = False,
    reporter: CheckReporter | None = None,
    label_prefix: str = "",
) -> list[_CheckResult]:
    """Run every layered health check against this task's container.

    Probes are executed via ``podman exec`` under crun and over the
    passt-forwarded SSH transport under krun — whichever booted the
    container.  Optional fixes run only for checks whose verdict
    reports ``fixable=True``.

    When *reporter* is supplied, progress streams line-by-line and
    noisy categories (``Credential file (...)``, ``Phantom token
    (...)``, …) coalesce into a single heading.  The returned list
    is empty in that case so the caller doesn't re-print.

    *label_prefix* is prepended to every emitted label — used by
    the sickbay command to tag multi-task runs with ``"Task
    pid/tid: "``.
    """
    cname, cid, task_dir, early = _resolve_running_container(self.project_name, self.task_id)
    if early:
        if reporter is not None:
            for status, label, detail in early:
                reporter.emit(status, label, detail)
            return []
        return early

    # Root cause first: is the per-container supervisor even running?
    # Reported ahead of every socket/bridge probe so a dead supervisor
    # is named once, and the reachability symptoms below reference it.
    # With ``fix`` a dead supervisor is respawned here; supervisor_up
    # reflects the post-fix state.
    supervisor, supervisor_up = _check_supervisor_alive(cname, cid, fix=fix)

    # Resolve the runtime once per doctor run so every probe / fix
    # reaches the container through the same backend that booted it
    # (under krun: SSH over a passt-forwarded TCP port; under crun:
    # podman exec).
    project = load_project(self.project_name)
    runtime = _rt.resolve_runtime(project)
    checks = list(
        _collect_all_checks(
            self.project_name,
            task_dir,
            self.task_id,
            services_mode=project.services_mode,
            cname=cname,
            cid=cid,
        )
    )

    # Per-container vault + gate reachability — host-side, best-effort.
    # Emitted after the layered probes so they read as a closing
    # "are this container's services actually up?" pair.
    reachability = _check_per_container_services(
        cname, fallback_mode=project.services_mode, supervisor_up=supervisor_up
    )

    if reporter is None:
        # Legacy path: collect-and-return.  No streaming, no grouping.
        results: list[_CheckResult] = list(supervisor)
        for check in checks:
            results.extend(_execute_check(runtime, check, cname, task_dir, fix=fix))
        results.extend(reachability)
        return results

    # Streaming path with grouping.
    for status, label, detail in supervisor:
        reporter.emit(status, f"{label_prefix}{label}", detail)
    _stream_checks(
        runtime,
        checks,
        cname,
        task_dir,
        fix=fix,
        reporter=reporter,
        label_prefix=label_prefix,
    )
    for status, label, detail in reachability:
        reporter.emit(status, f"{label_prefix}{label}", detail)
    return []

run_container_doctor(project_name, task_id, *, fix=False, reporter=None, label_prefix='')

Shim around :meth:ContainerDoctor.run for back-compat callers.

Source code in src/terok/lib/orchestration/container_doctor.py
def run_container_doctor(
    project_name: str,
    task_id: str,
    *,
    fix: bool = False,
    reporter: CheckReporter | None = None,
    label_prefix: str = "",
) -> list[_CheckResult]:
    """Shim around :meth:`ContainerDoctor.run` for back-compat callers."""
    return ContainerDoctor(project_name, task_id).run(
        fix=fix, reporter=reporter, label_prefix=label_prefix
    )