# rye:signed:2026-04-10T03:28:17Z:13343cd1529a02c102fccbea0f1e488d7366c822553c807fd02b9f7f779335f9:j87JU_BCDvIinEG-Cd_8hRnB2m6BFch3bVH-3W_R0-Wzy7dTzou4kFrrC7SLqZdCgAsD8cPu9nW-evtZi7JvDw:6ea18199041a1ea8
"""List running processes from thread registry."""

import argparse
import json
import sqlite3
import sys
from pathlib import Path

__version__ = "1.0.0"
__tool_type__ = "python"
__executor_id__ = "rye/core/runtimes/python/function"
__category__ = "rye/core/processes"
__tool_description__ = "List processes from thread registry"

CONFIG_SCHEMA = {
    "type": "object",
    "properties": {
        "status": {
            "type": "string",
            "description": "Filter by status (running/completed/cancelled/error/killed). Omit for all active.",
            "enum": ["running", "completed", "completed_with_errors", "cancelled", "error", "killed"],
        },
    },
}


def execute(params: dict, project_path: str) -> dict:
    from rye.constants import AI_DIR

    proj = Path(project_path)
    db_path = proj / AI_DIR / "agent" / "threads" / "registry.db"
    if not db_path.exists():
        return {"success": True, "runs": [], "count": 0}

    status_filter = params.get("status")

    with sqlite3.connect(db_path) as conn:
        conn.row_factory = sqlite3.Row
        if status_filter:
            cursor = conn.execute(
                "SELECT * FROM threads WHERE status = ? ORDER BY created_at DESC",
                (status_filter,),
            )
        else:
            cursor = conn.execute("""
                SELECT * FROM threads
                WHERE status NOT IN ('completed', 'completed_with_errors', 'error', 'cancelled', 'killed')
                ORDER BY created_at DESC
            """)
        rows = cursor.fetchall()

    runs = []
    for row in rows:
        r = dict(row)
        entry = {
            "run_id": r.get("thread_id"),
            "directive": r.get("directive"),
            "status": r.get("status"),
            "pid": r.get("pid"),
            "parent_id": r.get("parent_id"),
            "created_at": r.get("created_at"),
            "updated_at": r.get("updated_at"),
        }
        if r.get("status") == "completed_with_errors":
            stored_result = r.get("result")
            if stored_result:
                try:
                    parsed = json.loads(stored_result) if isinstance(stored_result, str) else stored_result
                    if isinstance(parsed, dict) and "errors_suppressed" in parsed:
                        entry["errors_suppressed"] = parsed["errors_suppressed"]
                except (json.JSONDecodeError, ValueError):
                    pass
        runs.append(entry)

    return {"success": True, "runs": runs, "count": len(runs)}


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--project-path", required=True)
    args = parser.parse_args()
    params = json.loads(sys.stdin.read())
    result = execute(params, args.project_path)
    print(json.dumps(result))
