Skip to content

Cache

cache

Clone-cache workspace seeding for faster task startup.

When a gate mirror has been synced (terok gate-sync or AgentRunner._setup_gate), a non-bare clone cache may exist on the host. Copying that cache into the task workspace before container launch replaces the slow in-container git clone with a fast file copy followed by a lightweight git fetch + reset.

The public entry point is seed_workspace_from_clone_cache.

seed_workspace_from_clone_cache(workspace_path, scope, *, origin_url=None, cfg=None)

Pre-populate workspace_path from the clone cache for scope.

Returns True if the workspace was successfully seeded.

After copying, rewrites the git origin remote to origin_url so that the in-container init script's sanity check (which compares origin against CODE_REPO) passes — the cache's origin points to a local file:// URL that won't match.

Skips seeding when the cache doesn't exist, the workspace already contains a .git directory, or the copy fails. Failures are logged and swallowed — the container falls back to a full clone.

Source code in src/terok_executor/container/cache.py
def seed_workspace_from_clone_cache(
    workspace_path: Path,
    scope: str,
    *,
    origin_url: str | None = None,
    cfg: SandboxConfig | None = None,
) -> bool:
    """Pre-populate *workspace_path* from the clone cache for *scope*.

    Returns ``True`` if the workspace was successfully seeded.

    After copying, rewrites the git origin remote to *origin_url* so that
    the in-container init script's sanity check (which compares origin
    against ``CODE_REPO``) passes — the cache's origin points to a local
    ``file://`` URL that won't match.

    Skips seeding when the cache doesn't exist, the workspace already
    contains a ``.git`` directory, or the copy fails.  Failures are
    logged and swallowed — the container falls back to a full clone.
    """
    if (workspace_path / ".git").is_dir():
        return False

    cache_dir = _resolve_cache_dir(scope, cfg)
    if cache_dir is None or not (cache_dir / ".git").is_dir():
        return False

    try:
        _logger.info("Seeding workspace from clone cache: %s%s", cache_dir, workspace_path)
        _copy_tree(cache_dir, workspace_path)
    except (OSError, shutil.Error, subprocess.CalledProcessError) as exc:
        _logger.warning("Clone cache seed failed (non-fatal): %s", exc)
        _wipe_workspace_contents(workspace_path)
        return False

    if not (workspace_path / ".git").is_dir():
        _logger.warning("Cache copy did not produce .git; falling back to container clone")
        _wipe_workspace_contents(workspace_path)
        return False

    if origin_url:
        _rewrite_origin(workspace_path, origin_url)

    return True