Skip to content
Python
16.9 KB
Raw Blame
"""P3 gate tests — persistent shell executor.

The executor drives the OS-native shell (bash on POSIX, PowerShell on Windows), so the
command strings here are parameterized per-OS. The behavior under test (cwd/env persistence,
exit codes, timeout-and-recover, truncation) is identical across both.
"""

from __future__ import annotations

import sys
import time

import pytest

from coworker.permissions import PermissionEngine
from coworker.tools import ToolRegistry
from coworker.tools.shell import LocalExecutor, shell_tools

_WIN = sys.platform == "win32"

# Per-OS command snippets exercising the same behavior in the native shell.
SET_ENV = "$env:GREETING='hello_world'" if _WIN else "export GREETING=hello_world"
ECHO_ENV = "echo $env:GREETING" if _WIN else "echo $GREETING"
EXIT_OK = "cmd /c exit 0" if _WIN else "true"
EXIT_FAIL = "cmd /c exit 1" if _WIN else "false"
SLEEP_5 = "Start-Sleep -Seconds 5" if _WIN else "sleep 5"
PWD = "(Get-Location).Path" if _WIN else "pwd"  # PowerShell's `pwd` is a table that cuts long paths
PRINT_1000 = (
    'foreach ($i in 1..1000) { "line$i" }'
    if _WIN
    else "for i in $(seq 1 1000); do echo line$i; done"
)


# Every behaviour below is checked twice: on the in-process executor (direct mode) and on
# the same contract served by a tool runner in another process (the sandbox path, over a
# Unix socket or a Windows named pipe). The two must not drift apart.
_EXECUTORS = ["direct", "runner"]
if sys.platform == "darwin":
    _EXECUTORS.append("seatbelt")  # the same runner, inside the macOS sandbox


@pytest.fixture(params=_EXECUTORS)
def executor(request, tmp_path):
    if request.param == "direct":
        ex = LocalExecutor(cwd=tmp_path, default_timeout=10)
        yield ex
        ex.close()
        return
    from coworker.sandbox.providers.runner_local import RunnerLocalProvider
    from coworker.sandbox.workspace import RunnerWorkspace

    if request.param == "seatbelt":
        from coworker.sandbox.providers import seatbelt

        try:
            seatbelt.preflight()
        except seatbelt.SeatbeltUnavailable as exc:
            pytest.skip(str(exc))
        provider = seatbelt.SeatbeltProvider(
            roots=[{"path": str(tmp_path), "writable": True}], cwd=tmp_path, runner_path=_runner_zipapp(tmp_path), network=False
        )
    else:
        provider = RunnerLocalProvider(cwd=tmp_path, runner_path=_runner_zipapp(tmp_path))
    ws = RunnerWorkspace(provider, cwd=tmp_path)
    ws.executor.default_timeout = 10
    yield ws.executor
    ws.close()


def _runner_zipapp(tmp_path):
    from coworker.sandbox.bundle import build_runner_zipapp

    return build_runner_zipapp(tmp_path.parent / "runner-dist")


def test_cwd_persists_across_calls(executor, tmp_path):
    (tmp_path / "sub").mkdir()
    executor.run("cd sub")
    result = executor.run(PWD)
    assert result["exit_code"] == 0
    assert "sub" in result["output"]
    assert executor.cwd.endswith("sub")


def test_env_persists_across_calls(executor):
    executor.run(SET_ENV)
    result = executor.run(ECHO_ENV)
    assert "hello_world" in result["output"]


def test_managed_bin_dir_is_on_path_from_spawn(tmp_path, monkeypatch):
    """A tool the user approves mid-session must work by name in the ALREADY-running
    shell (owner-hit 2026-08-14: freshly installed trivy needed a full-path retry). The
    stable bin dir goes on PATH at spawn, before anything exists in it."""
    import os as _os

    from coworker import toolchain

    monkeypatch.setenv("COWORKER_STATE_DIR", str(tmp_path / "state"))
    ex = LocalExecutor(cwd=tmp_path, default_timeout=10)
    try:
        assert str(toolchain.bin_dir()) in ex._env["PATH"].split(_os.pathsep)
        # User's own PATH entries stay ahead — their copies always win.
        assert not ex._env["PATH"].startswith(str(toolchain.bin_dir()))
    finally:
        ex.close()


def test_exit_code_captured(executor):
    assert executor.run(EXIT_OK)["exit_code"] == 0
    assert executor.run(EXIT_FAIL)["exit_code"] == 1


@pytest.mark.skipif(not _WIN, reason="PowerShell's REPL skips a line it cannot parse")
@pytest.mark.parametrize(
    "command, message",
    [
        # Round-2 F-8: `@notes.txt` (curl's upload syntax) is splatting to PowerShell.
        ("Write-Output @notes.txt", "splatting operator"),
        ("if (1 {", "Missing closing"),
        # An unclosed quote used to swallow the trailer and hang until the timeout.
        ("Write-Output 'unclosed", "missing the terminator"),
    ],
)
def test_powershell_parse_error_is_a_failure(executor, command, message):
    start = time.monotonic()
    result = executor.run("Write-Output before")  # a success first: `$?` was left true
    assert result["exit_code"] == 0
    result = executor.run(command)
    assert result["exit_code"] == 1
    assert result["timed_out"] is False
    assert message in result["output"]
    assert "At line:1 char:" in result["output"]
    assert time.monotonic() - start < 8.0
    # Nothing of the check is left in the session, and the shell carries on.
    after = executor.run("Get-Variable __cw* -ErrorAction SilentlyContinue | Measure-Object | % Count")
    assert after["exit_code"] == 0
    assert after["output"].strip() == "0"


@pytest.mark.skipif(not _WIN, reason="PowerShell parse check")
def test_powershell_parse_check_leaves_valid_commands_alone(executor, tmp_path):
    # Quotes and `$` travel through the check unchanged; state persists.
    result = executor.run("$cwValue = \"it's $(1+1) ok\"; Write-Output $cwValue")
    assert result["exit_code"] == 0
    assert "it's 2 ok" in result["output"]
    assert "it's 2 ok" in executor.run("Write-Output $cwValue")["output"]
    assert executor.run("cmd /c exit 3")["exit_code"] == 3


@pytest.mark.skipif(not _WIN, reason="PowerShell's REPL waits for an empty line after a block")
@pytest.mark.parametrize(
    "command, expected",
    [
        ('foreach ($i in 1..2) {\n  Write-Output "n$i"\n}', ["n1", "n2"]),
        ('foreach ($i in 1..2) {\n\n  Write-Output "n$i"\n}', ["n1", "n2"]),  # empty line inside
        ("if ($true) {\n  Write-Output yes\n} else {\n  Write-Output no\n}", ["yes"]),
        ("Write-Output a\nWrite-Output b\n", ["a", "b"]),
    ],
)
def test_powershell_multi_line_command_finishes(executor, command, expected):
    # Used to hang until the timeout: the trailer was read into the open block.
    start = time.monotonic()
    result = executor.run(command, timeout=8)
    assert result["timed_out"] is False
    assert result["exit_code"] == 0
    assert result["output"].split() == expected
    assert time.monotonic() - start < 6.0


@pytest.mark.skipif(not _WIN, reason="PowerShell multi-line commands")
def test_powershell_multi_line_keeps_exit_codes_and_state(executor):
    assert executor.run("Write-Output a\ncmd /c exit 4")["exit_code"] == 4  # last line decides
    assert executor.run("cmd /c exit 4\nWrite-Output a")["exit_code"] == 0
    defined = executor.run("$cwMulti = 7\nfunction Get-CwMulti {\n  'from-function'\n}")
    assert defined["exit_code"] == 0
    used = executor.run("Get-CwMulti; $cwMulti")
    assert used["output"].split() == ["from-function", "7"]


@pytest.mark.skipif(not _WIN, reason="PowerShell's $LASTEXITCODE")
def test_powershell_failed_cmdlet_does_not_report_an_earlier_programs_code(executor, tmp_path):
    missing = str(tmp_path / "missing.invalid")
    assert executor.run("cmd /c exit 3")["exit_code"] == 3
    # Used to say 3: `$LASTEXITCODE` still held the earlier call's native program.
    assert executor.run(f"Get-Item '{missing}'")["exit_code"] == 1
    assert executor.run("Write-Output ok")["exit_code"] == 0
    assert executor.run(f"Get-Item '{missing}'")["exit_code"] == 1
    # A native program in the same command still gives its own code.
    assert executor.run(f"Get-Item '{missing}'; cmd /c exit 6")["exit_code"] == 6


def test_timeout_kills_command(executor):
    start = time.monotonic()
    result = executor.run(SLEEP_5, timeout=1)
    elapsed = time.monotonic() - start
    assert result["timed_out"] is True
    assert elapsed < 4.0  # did not block for the full sleep
    # session survives the timeout — still usable (POSIX keeps the shell; Windows respawns)
    assert executor.run("echo alive")["output"].strip().endswith("alive")


def test_large_output_truncated_keeps_tail(tmp_path):
    ex = LocalExecutor(cwd=tmp_path, max_output_chars=200, default_timeout=10)
    try:
        result = ex.run(PRINT_1000)
        assert result["truncated"] is True
        assert len(result["output"]) <= 200
        # the END survives (where test/build verdicts live), the head is dropped
        assert "line1000" in result["output"]
        assert "line1\n" not in result["output"]
    finally:
        ex.close()


def test_shell_tool_integration(executor, tmp_path):
    reg = ToolRegistry()
    reg.register_all(shell_tools(executor))
    assert {"run_shell", "shell_task_output", "shell_task_kill"} <= set(reg.names())

    spec = reg.get("run_shell")
    assert spec.metadata.requires_approval is True
    # polling/killing the agent's own background tasks doesn't need approval
    assert reg.get("shell_task_output").metadata.requires_approval is False
    assert reg.get("shell_task_kill").metadata.requires_approval is False

    eng = PermissionEngine(workspace_root=tmp_path)
    decision = eng.evaluate("run_shell", {"command": "echo hi"}, spec.metadata)
    assert not decision.allowed and decision.needs_user  # high-risk → asks

    out = reg.execute("run_shell", {"command": "echo hi"})
    assert "hi" in out["output"]


def test_run_shell_accepts_description_and_clamped_timeout(executor):
    reg = ToolRegistry()
    reg.register_all(shell_tools(executor))
    # `description` rides along for approval prompts/audit; it must not break execution.
    out = reg.execute(
        "run_shell",
        {"command": "echo ok", "description": "Say ok", "timeout_seconds": 99999},
    )
    assert out["exit_code"] == 0 and "ok" in out["output"]


# -- background tasks ------------------------------------------------------------

ECHO_THEN_SLEEP = (
    "Write-Output started; Start-Sleep -Seconds 30"
    if _WIN
    else "echo started; sleep 30"
)
QUICK_ECHO = "Write-Output quick_done" if _WIN else "echo quick_done"


def _poll_output(reg, task_id, *, until_status=None, deadline=10.0):
    """Poll shell_task_output, accumulating output until a status is reached."""
    acc = ""
    end = time.monotonic() + deadline
    while time.monotonic() < end:
        res = reg.execute("shell_task_output", {"task_id": task_id})
        acc += res["output"]
        if until_status is None or res["status"] == until_status:
            if until_status is None and not acc:
                time.sleep(0.1)
                continue
            if until_status is not None:
                # Output reads are incremental and the status can flip to its
                # terminal value on the same tick the final chunk is still being
                # drained into the task buffer. Drain the tail: consume anything
                # already buffered greedily (no sleep), and only wait between
                # *empty* reads, stopping once two consecutive reads yield nothing
                # (robust to a one-tick gap between chunks). Bounded and cheap —
                # the common case is a couple of reads, not a tight 2s poll.
                grace_end = time.monotonic() + 0.5
                empty_reads = 0
                while empty_reads < 2 and time.monotonic() < grace_end:
                    extra = reg.execute("shell_task_output", {"task_id": task_id})
                    if extra["output"]:
                        acc += extra["output"]
                        res = extra
                        empty_reads = 0
                        continue  # more available now — keep draining, no sleep
                    empty_reads += 1
                    time.sleep(0.02)
            return acc, res
        time.sleep(0.1)
    return acc, res


def test_background_task_runs_and_exits(executor):
    reg = ToolRegistry()
    reg.register_all(shell_tools(executor))
    started = reg.execute(
        "run_shell", {"command": QUICK_ECHO, "run_in_background": True}
    )
    assert started["status"] == "running" and started["task_id"]

    acc, res = _poll_output(reg, started["task_id"], until_status="exited")
    assert res["status"] == "exited"
    assert res["exit_code"] == 0
    assert "quick_done" in acc

    # output reads are incremental: a second read returns nothing new
    again = reg.execute("shell_task_output", {"task_id": started["task_id"]})
    assert again["output"] == ""


def test_background_task_kill(executor):
    reg = ToolRegistry()
    reg.register_all(shell_tools(executor))
    started = reg.execute(
        "run_shell", {"command": ECHO_THEN_SLEEP, "run_in_background": True}
    )
    acc, _ = _poll_output(reg, started["task_id"])
    assert "started" in acc  # it's alive and producing output

    killed = reg.execute("shell_task_kill", {"task_id": started["task_id"]})
    assert killed["status"] == "killed"

    res = reg.execute("shell_task_output", {"task_id": started["task_id"]})
    assert res["status"] == "exited"


DELAYED_ECHO = (
    "Start-Sleep -Seconds 1; Write-Output later_line" if _WIN else "sleep 1; echo later_line"
)


def test_background_wait_returns_as_soon_as_the_task_exits(executor):
    reg = ToolRegistry()
    reg.register_all(shell_tools(executor))
    started = reg.execute("run_shell", {"command": QUICK_ECHO, "run_in_background": True})
    t0 = time.monotonic()
    res = reg.execute("shell_task_output", {"task_id": started["task_id"], "wait_seconds": 10})
    elapsed = time.monotonic() - t0
    # Asked for up to 10 s; got the exit (and its last line) well before that.
    assert res["status"] == "exited" and res["exit_code"] == 0, res
    assert "quick_done" in res["output"], res
    assert elapsed < 5, elapsed
    assert res["waited_seconds"] <= 5


def test_background_wait_returns_when_new_output_arrives(executor):
    reg = ToolRegistry()
    reg.register_all(shell_tools(executor))
    started = reg.execute("run_shell", {"command": DELAYED_ECHO, "run_in_background": True})
    t0 = time.monotonic()
    res = reg.execute("shell_task_output", {"task_id": started["task_id"], "wait_seconds": 10})
    elapsed = time.monotonic() - t0
    # Nothing is printed for ~1 s; the wait ends on the first line, not at 10 s.
    assert "later_line" in res["output"], res
    assert elapsed < 8, elapsed


def test_background_wait_gives_up_at_the_deadline_while_still_running(executor):
    reg = ToolRegistry()
    reg.register_all(shell_tools(executor))
    started = reg.execute("run_shell", {"command": ECHO_THEN_SLEEP, "run_in_background": True})
    _poll_output(reg, started["task_id"])  # consume the initial "started" line
    t0 = time.monotonic()
    res = reg.execute("shell_task_output", {"task_id": started["task_id"], "wait_seconds": 1})
    elapsed = time.monotonic() - t0
    assert res["status"] == "running" and res["output"] == "", res
    assert 0.8 <= elapsed < 4, elapsed
    assert 0.8 <= res["waited_seconds"] <= 4
    reg.execute("shell_task_kill", {"task_id": started["task_id"]})


def test_background_read_without_wait_is_unchanged(executor):
    reg = ToolRegistry()
    reg.register_all(shell_tools(executor))
    started = reg.execute("run_shell", {"command": ECHO_THEN_SLEEP, "run_in_background": True})
    _poll_output(reg, started["task_id"])
    for args in ({"task_id": started["task_id"]}, {"task_id": started["task_id"], "wait_seconds": 0},
                 {"task_id": started["task_id"], "wait_seconds": -3}):
        t0 = time.monotonic()
        res = reg.execute("shell_task_output", args)
        assert res["status"] == "running" and time.monotonic() - t0 < 0.5, (args, res)
        assert res["waited_seconds"] == 0.0
    reg.execute("shell_task_kill", {"task_id": started["task_id"]})


def test_wait_is_capped_at_the_shell_maximum(executor):
    from coworker.tools.shell import _TASK_OUTPUT_SCHEMA, _MAX_TIMEOUT

    desc = _TASK_OUTPUT_SCHEMA["function"]["parameters"]["properties"]["wait_seconds"]["description"]
    assert f"max {int(_MAX_TIMEOUT)}" in desc
    # The wrapper clamps before the executor sees it: a huge wait on an exited task is instant.
    # The task prints nothing, so only its exit can end the wait. With a printing command the
    # wait may legitimately return on the new line a moment before the process exits, still
    # "running" - the early return on new output tested above.
    reg = ToolRegistry()
    reg.register_all(shell_tools(executor))
    started = reg.execute("run_shell", {"command": EXIT_OK, "run_in_background": True})
    t0 = time.monotonic()
    res = reg.execute("shell_task_output", {"task_id": started["task_id"], "wait_seconds": 10 ** 6})
    assert res["status"] == "exited" and time.monotonic() - t0 < 5


def test_background_unknown_task_errors(executor):
    reg = ToolRegistry()
    reg.register_all(shell_tools(executor))
    assert (
        "unknown task"
        in reg.execute("shell_task_output", {"task_id": "bg-99"})["error"]
    )
    assert (
        "unknown task" in reg.execute("shell_task_kill", {"task_id": "bg-99"})["error"]
    )