"""
A drop in replacement for subprocess.run that allows for running commands in the task
container.

This is intended to reduce the friction of integrating and existing agent into the
harness. Many agent tools wrap a shell command, so if that command is currently being
executed with subprocess.run, simply change the import and pass in the container name.
"""

import shlex
import subprocess


def docker_subprocess(
    container_name: str,
    command: list[str],
    *args,
    **kwargs,
) -> subprocess.CompletedProcess:
    """
    Run a command in the task container.

    Accepts all arguments of subprocess.run.
    """

    new_command = [
        "docker",
        "exec",
        container_name,
        shlex.quote(" ".join(command)),
    ]
    return subprocess.run(new_command, *args, **kwargs)
