"""Verify command execution for task completion."""

from __future__ import annotations

import asyncio


async def run_verify_command(
    command: str, task_id: str, timeout: int = 120,
) -> dict | None:
    """Run a shell verify command. Returns error dict on failure, None on success."""
    try:
        proc = await asyncio.create_subprocess_shell(
            command,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
        )
        stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=timeout)
        if proc.returncode != 0:
            return {
                "error": "verify_command failed",
                "task_id": task_id,
                "returncode": proc.returncode,
                "stdout": stdout.decode(errors="replace")[-500:],
                "stderr": stderr.decode(errors="replace")[-500:],
            }
    except asyncio.TimeoutError:
        return {"error": f"verify_command timed out ({timeout}s)", "task_id": task_id}
    return None
