Skip to content

The agent that grows with you

6.9 KB
Raw Blame
"""Host directories that stand in for a Docker container's ``/workspace`` and ``/root``.

``base.py`` is over the file-size ratchet, so the candidate list lives here.
The workspace digest is passed in: delivery runs after the turn's context is
gone, and scanning every ``*_ws_*`` directory would serve another session's file.
"""

from __future__ import annotations

import os
import re
from pathlib import Path

_WORKSPACE_KEY_RE = re.compile(r"^[0-9a-f]{16}$")


def _scope():
    from gateway.platforms.base import _docker_persistent_active, _tenv

    return _tenv, _docker_persistent_active


def _cwd_mount_enabled() -> bool:
    """The terminal's own predicate, so the file panel agrees with what the container mounted."""
    from tools.terminal_tool_config import _tenv_bool

    return _tenv_bool("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "false")


def _docker_sandbox_dir_candidates(session_key: str = "", workspace_key: str = "") -> list[str]:
    """Candidate host sandbox dir names, best first.

    Mirrors ``_resolve_container_task_id``: profile scope (``default``, else
    ``profile:<name>``), then the legacy ``session:<key>`` layout. A session
    that bound a host workspace also has ``<base>:ws:<digest>`` — a different
    directory from the shared profile sandbox — but only with the cwd mount on:
    without it every session keeps the profile sandbox, and a ``*_ws_*`` dir
    left by an older build is not this session's. The digest is explicit because
    an ambient lookup after the turn would collapse onto ``default``.
    """
    tenv, _persistent = _scope()
    try:
        from tools.environments.path_utils import sanitize_task_id_for_path
        from tools.terminal_tool_config import home_profile_name, profile_container_key
        base = profile_container_key(home_profile_name())
    except Exception:  # health: allow BLE001 -- path translation fails closed; a miss is a 404, not a 500
        return ["default"]
    candidates: list[str] = []
    # Explicit trusted-profiles opt-in: one shared container identity. That
    # key is not a per-workspace container, so a :ws: digest does not apply.
    if shared := tenv("TERMINAL_DOCKER_SHARED_CONTAINER_KEY", "").strip():
        candidates.append(sanitize_task_id_for_path(f"shared:{shared}"))
    elif _WORKSPACE_KEY_RE.fullmatch(workspace_key or "") and _cwd_mount_enabled():
        candidates.append(sanitize_task_id_for_path(f"{base}:ws:{workspace_key}"))
    if base != "default":
        candidates.append(sanitize_task_id_for_path(base))
    candidates.append("default")
    if session_key:  # bug-window legacy layout: per-session sandboxes
        candidates.append(sanitize_task_id_for_path(f"session:{session_key}"))
    return candidates


def _docker_persistent_sandbox_roots(
    session_key: str, leaf: str, workspace_key: str = "",
) -> list[Path]:
    """Existing ``<sandbox>/docker/<candidate>/<leaf>`` dirs, candidate order.

    Empty unless Docker + persistent. The translator tries each until the file
    resolves.
    """
    _tenv, persistent = _scope()
    if not persistent():
        return []
    try:
        from tools.environments.base import get_sandbox_dir
        base = get_sandbox_dir() / "docker"
        return [cand for name in _docker_sandbox_dir_candidates(session_key, workspace_key)
                if (cand := (base / name / leaf).resolve(strict=False)).is_dir()]
    except Exception:  # health: allow BLE001 -- path translation fails closed; a miss is a 404, not a 500
        return []


def _mounted_workspace_cwd(workspace_cwd: str = "") -> list[Path]:
    tenv, _persistent = _scope()
    cwd = workspace_cwd or tenv("TERMINAL_CWD") or os.getcwd()
    try:
        host = Path(os.path.expanduser(cwd)).resolve(strict=False)
    except (OSError, RuntimeError, ValueError):
        return []
    return [host] if host.is_dir() else []


def _session_mounted_workspace(workspace_key: str, workspace_cwd: str) -> list[Path]:
    """The host dir a session bound to a workspace mounts at ``/workspace``, or nothing.

    A gateway turn runs with the session's own cwd as ``TERMINAL_CWD``
    (``gateway.session_policy.build_policy``), and that cwd is also what
    ``bind_turn_workspace`` registers and hashes into ``workspace_key``. So the
    mount is that cwd, never the profile's ``terminal.cwd``. A cwd whose digest
    is not ``workspace_key`` names some other container's mount: no root.
    """
    from tools.terminal_tool import workspace_key_for

    cwd = (workspace_cwd or "").strip()
    if not cwd or "\0" in cwd or not os.path.isabs(os.path.expanduser(cwd)):
        return []
    if workspace_key_for(cwd) != workspace_key:
        return []
    return _mounted_workspace_cwd(cwd)


def _shared_cwd_mount_guess(workspace_key: str) -> list[Path]:
    """The profile cwd that stands in for a shared container's ``/workspace``, or nothing.

    With ``TERMINAL_DOCKER_SHARED_CONTAINER_KEY`` every session reuses one
    ``shared:<key>`` container, which mounts whatever cwd the session that
    created it brought, so a session's own cwd is not its mount. Nothing records
    that first cwd: shared containers carry no mount fingerprint label, and a
    cross-process attach never inspects the container's mounts. Reads keep the
    profile cwd, as before; a session Save there is a guess the caller refuses.
    """
    tenv, persistent = _scope()
    if not persistent() or not _WORKSPACE_KEY_RE.fullmatch(workspace_key or ""):
        return []
    if not _cwd_mount_enabled():
        return []
    if not tenv("TERMINAL_DOCKER_SHARED_CONTAINER_KEY", "").strip():
        return []
    return _mounted_workspace_cwd()


def _default_docker_workspace_host_roots(
    session_key: str = "", workspace_key: str = "", workspace_cwd: str = "",
) -> list[Path]:
    """Host dirs that stand in for ``/workspace``.

    A cwd mount is tried first. With no workspace digest that mount is the
    profile's cwd and the only candidate, so MEDIA delivery (which has no
    session) stays cwd-only. A download that names a digest mounts that
    session's own cwd (``workspace_cwd``) instead, unless its container is a
    shared one (``_shared_cwd_mount_guess``), then tries that session's
    sandbox: the container key can be ``default:ws:<digest>`` while
    ``/workspace`` is still the sandbox directory, because the host cwd was
    not bind-mounted when the container was created.
    """
    tenv, persistent = _scope()
    if not persistent():
        return []
    roots: list[Path] = []
    if _cwd_mount_enabled():
        if not _WORKSPACE_KEY_RE.fullmatch(workspace_key or ""):
            return _mounted_workspace_cwd()
        if tenv("TERMINAL_DOCKER_SHARED_CONTAINER_KEY", "").strip():
            roots.extend(_shared_cwd_mount_guess(workspace_key))
        else:
            roots.extend(_session_mounted_workspace(workspace_key, workspace_cwd))
    for root in _docker_persistent_sandbox_roots(session_key, "workspace", workspace_key):
        if root not in roots:
            roots.append(root)
    return roots