Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

12.8 KB
Raw Blame
from __future__ import annotations

from contextlib import suppress
import logging
import os
import shutil
import signal
import subprocess
import sys
import time
from pathlib import Path

import psutil
import pytest

from core import command_runner, watch_worker
from core.process_isolation import (
    PROCESS_IDENTITY_ENV,
    PersistedProcessIdentity,
    fingerprint_process_marker,
    process_group_identity_status,
)


def _supervisor_command() -> list[str]:
    return [os.path.abspath(sys.executable), command_runner.supervisor_path()]


def _supervisor_isolation_kwargs() -> dict[str, object]:
    if os.name == "nt":
        return {"creationflags": subprocess.CREATE_NEW_PROCESS_GROUP}
    return {"start_new_session": True}


def test_watch_worker_hides_supervisor_identity_and_preserves_empty_arguments() -> None:
    marker = "supervisor-only-marker"
    env = dict(os.environ)
    env[PROCESS_IDENTITY_ENV] = marker
    spec = watch_worker.encode_watch_worker_spec(
        command=[
            sys.executable,
            "-c",
            (
                "import os,sys; "
                "print(os.environ.get('AVIBE_PROCESS_IDENTITY', 'missing')); "
                "print(repr(sys.argv[1])); "
                "print('worker-stderr', file=sys.stderr)"
            ),
            "",
        ],
        shell_command=None,
    )

    result = subprocess.run(
        _supervisor_command(),
        input=spec,
        capture_output=True,
        env=env,
        timeout=10,
        check=False,
        **_supervisor_isolation_kwargs(),
    )

    assert result.returncode == 0
    assert result.stdout.decode().splitlines() == ["missing", "''"]
    assert result.stderr.decode().strip() == "worker-stderr"
    assert marker not in result.stdout.decode()
    assert marker not in result.stderr.decode()


def test_watch_worker_rejects_invalid_specification() -> None:
    result = subprocess.run(
        _supervisor_command(),
        input=b'{"version":1,"command":[],"shell_command":null}',
        capture_output=True,
        timeout=10,
        check=False,
        **_supervisor_isolation_kwargs(),
    )

    assert result.returncode == 1
    assert watch_worker.decode_watch_worker_error(result.stderr.decode()) == (
        "invalidCommand",
        None,
    )


@pytest.mark.skipif(os.name == "nt", reason="POSIX surrogateescape contract")
@pytest.mark.parametrize("use_shell", [False, True])
def test_posix_watch_worker_preserves_non_utf8_arguments(use_shell: bool) -> None:
    value = os.fsdecode(b"watch-\xff")
    if use_shell:
        import shlex

        command = []
        shell_command = (
            f"{shlex.quote(sys.executable)} -c "
            f"{shlex.quote('import os,sys; print(os.fsencode(sys.argv[1]).hex())')} "
            f"{shlex.quote(value)}"
        )
    else:
        command = [
            sys.executable,
            "-c",
            "import os,sys; print(os.fsencode(sys.argv[1]).hex())",
            value,
        ]
        shell_command = None

    result = subprocess.run(
        _supervisor_command(),
        input=watch_worker.encode_watch_worker_spec(
            command=command,
            shell_command=shell_command,
        ),
        capture_output=True,
        timeout=10,
        check=False,
        **_supervisor_isolation_kwargs(),
    )

    assert result.returncode == 0
    assert result.stdout.decode().strip() == b"watch-\xff".hex()


@pytest.mark.skipif(os.name == "nt", reason="POSIX signal exit contract")
def test_posix_watch_worker_preserves_waiter_signal_exit() -> None:
    result = subprocess.run(
        _supervisor_command(),
        input=watch_worker.encode_watch_worker_spec(
            command=[
                sys.executable,
                "-c",
                "import os,signal; os.kill(os.getpid(), signal.SIGTERM)",
            ],
            shell_command=None,
        ),
        capture_output=True,
        timeout=10,
        check=False,
        **_supervisor_isolation_kwargs(),
    )

    assert result.returncode == -signal.SIGTERM


@pytest.mark.skipif(os.name == "nt", reason="POSIX process group contract")
def test_posix_watch_worker_survives_term_until_waiter_exits(tmp_path: Path) -> None:
    waiter_pid_path = tmp_path / "waiter.pid"
    process = subprocess.Popen(
        _supervisor_command(),
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        **_supervisor_isolation_kwargs(),
    )
    assert process.stdin is not None
    process.stdin.write(
        watch_worker.encode_watch_worker_spec(
            command=[
                sys.executable,
                "-c",
                (
                    "import os,signal,time; from pathlib import Path; "
                    "signal.signal(signal.SIGTERM, signal.SIG_IGN); "
                    f"Path({str(waiter_pid_path)!r}).write_text(str(os.getpid())); "
                    "time.sleep(30)"
                ),
            ],
            shell_command=None,
        )
    )
    process.stdin.close()

    try:
        deadline = time.monotonic() + 5
        while not waiter_pid_path.exists() and time.monotonic() < deadline:
            time.sleep(0.05)
        waiter_pid = int(waiter_pid_path.read_text())
        os.killpg(process.pid, signal.SIGTERM)
        time.sleep(0.2)
        assert process.poll() is None
        assert psutil.pid_exists(waiter_pid)
    finally:
        if process.poll() is None:
            os.killpg(process.pid, signal.SIGKILL)
            process.wait(timeout=10)


@pytest.mark.skipif(os.name == "nt", reason="POSIX process group contract")
def test_posix_watch_worker_waits_for_descendant_after_command_root_exits() -> None:
    child_script = (
        "import subprocess,sys; "
        "child=subprocess.Popen("
        "[sys.executable,'-c','import time; time.sleep(30)'],"
        "stdin=subprocess.DEVNULL,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL"
        "); "
        "print(child.pid, flush=True)"
    )
    process = subprocess.Popen(
        _supervisor_command(),
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        **_supervisor_isolation_kwargs(),
    )
    assert process.stdin is not None
    assert process.stdout is not None
    process.stdin.write(
        watch_worker.encode_watch_worker_spec(
            command=[sys.executable, "-c", child_script],
            shell_command=None,
        )
    )
    process.stdin.close()
    descendant_pid = int(process.stdout.readline().decode().strip())

    try:
        time.sleep(0.2)
        assert process.poll() is None
        assert psutil.pid_exists(descendant_pid)
    finally:
        if process.poll() is None:
            os.killpg(process.pid, signal.SIGKILL)
            process.wait(timeout=10)
        if psutil.pid_exists(descendant_pid):
            with suppress(psutil.NoSuchProcess):
                psutil.Process(descendant_pid).kill()


@pytest.mark.skipif(os.name == "nt", reason="POSIX process group identity contract")
def test_posix_watch_worker_leaves_identity_anchor_if_supervisor_is_killed(
    tmp_path: Path,
) -> None:
    marker = "surviving-anchor-marker"
    env = dict(os.environ)
    env[watch_worker.PROCESS_IDENTITY_ENV] = marker
    waiter_pid_path = tmp_path / "waiter.pid"
    process = subprocess.Popen(
        _supervisor_command(),
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        env=env,
        **_supervisor_isolation_kwargs(),
    )
    assert process.stdin is not None
    process.stdin.write(
        watch_worker.encode_watch_worker_spec(
            command=[
                sys.executable,
                "-c",
                (
                    "import os,signal,time; from pathlib import Path; "
                    "signal.signal(signal.SIGTERM, signal.SIG_IGN); "
                    f"Path({str(waiter_pid_path)!r}).write_text(str(os.getpid())); "
                    "time.sleep(30)"
                ),
            ],
            shell_command=None,
        )
    )
    process.stdin.close()

    try:
        deadline = time.monotonic() + 5
        while not waiter_pid_path.exists() and time.monotonic() < deadline:
            time.sleep(0.05)
        waiter_pid = int(waiter_pid_path.read_text())
        identity = psutil.Process(process.pid)
        expected = PersistedProcessIdentity(
            pid=process.pid,
            create_time=identity.create_time(),
            worker_fingerprint=fingerprint_process_marker(marker),
        )
        os.kill(process.pid, signal.SIGKILL)
        process.wait(timeout=10)

        assert psutil.pid_exists(waiter_pid)
        assert (
            process_group_identity_status(
                process.pid,
                expected,
                logging.getLogger(__name__),
                "test watch",
            )
            == "match"
        )
    finally:
        try:
            os.killpg(process.pid, signal.SIGKILL)
        except ProcessLookupError:
            pass
        if process.poll() is None:
            process.wait(timeout=10)


@pytest.mark.skipif(os.name == "nt", reason="POSIX identity anchor")
def test_posix_identity_anchor_writes_no_bytecode_into_its_tree(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    # A desktop Runtime verifies its installed tree byte for byte, so one .pyc
    # written by a helper makes the next launch repair it. The anchor's
    # environment is replaced by the marker alone, so it cannot inherit the
    # caller's PYTHONDONTWRITEBYTECODE. Run it from a scratch tree whose own
    # psutil stub stands in for the modules it would cache next to itself.
    tree = tmp_path / "runtime"
    tree.mkdir()
    script = tree / "watch_worker.py"
    shutil.copyfile(watch_worker.__file__, script)
    imported = tree / "imported"
    (tree / "psutil.py").write_text(
        f"from pathlib import Path\nPath({str(imported)!r}).write_text('imported')\n",
        encoding="utf-8",
    )
    monkeypatch.setattr(watch_worker, "__file__", str(script))
    monkeypatch.setenv(PROCESS_IDENTITY_ENV, "bytecode-free-anchor")

    anchor = watch_worker._start_posix_identity_anchor()
    try:
        assert anchor is not None
        deadline = time.monotonic() + 10
        while not imported.exists() and time.monotonic() < deadline:
            assert anchor.poll() is None
            time.sleep(0.05)
        # Bytecode is cached before a module runs, so the stub's side effect
        # proves any write would already have happened.
        assert imported.exists()
        assert not (tree / "__pycache__").exists()
    finally:
        watch_worker._stop_posix_identity_anchor(anchor)


@pytest.mark.skipif(os.name != "nt", reason="Windows Job Object contract")
def test_windows_watch_worker_job_tracks_descendant_after_command_root_exits() -> None:
    child_script = (
        "import subprocess,sys; "
        "child=subprocess.Popen([sys.executable,'-c','import time; time.sleep(30)']); "
        "print(child.pid, flush=True)"
    )
    process = subprocess.Popen(
        _supervisor_command(),
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        **_supervisor_isolation_kwargs(),
    )
    assert process.stdin is not None
    assert process.stdout is not None
    process.stdin.write(
        watch_worker.encode_watch_worker_spec(
            command=[sys.executable, "-c", child_script],
            shell_command=None,
        )
    )
    process.stdin.close()
    descendant_pid = int(process.stdout.readline().decode().strip())

    try:
        assert process.poll() is None
        process.terminate()
        process.wait(timeout=10)
        deadline = time.monotonic() + 10
        while psutil.pid_exists(descendant_pid) and time.monotonic() < deadline:
            time.sleep(0.05)
        assert not psutil.pid_exists(descendant_pid)
    finally:
        if process.poll() is None:
            process.kill()
            process.wait(timeout=10)
        if psutil.pid_exists(descendant_pid):
            psutil.Process(descendant_pid).kill()


@pytest.mark.skipif(os.name != "nt", reason="Windows exit code contract")
@pytest.mark.parametrize("entrypoint", ["runner copy", "installed file"])
def test_windows_watch_worker_preserves_full_exit_code(entrypoint: str) -> None:
    # A runner released before the copies starts the installed file itself while an upgrade waits for its
    # restart, without the helper the copy carries beside it.
    command = (
        _supervisor_command()
        if entrypoint == "runner copy"
        else [os.path.abspath(sys.executable), str(Path(watch_worker.__file__).resolve())]
    )
    result = subprocess.run(
        command,
        input=watch_worker.encode_watch_worker_spec(
            command=[sys.executable, "-c", "import os; os._exit(513)"],
            shell_command=None,
        ),
        capture_output=True,
        timeout=10,
        check=False,
        **_supervisor_isolation_kwargs(),
    )

    assert result.returncode == 513