#!/usr/bin/env -S uv run
# /// script
# dependencies = ["pydantic", "python-dotenv", "pyyaml", "rich"]
# ///
"""Test fixture ADW: open a traced session and finish without agent work."""

import argparse
import os
import sys
import time

from adw_modules import agents, session
from adw_modules.data_types import PhaseParams


def main(config: str, adw_id: str | None = None, hold: bool = False) -> int:
    cfg = agents.load_config(config)
    run = session.ensure(cfg, adw_id=adw_id)
    with run.phase(
        PhaseParams(
            name="probe", task_id="probe",
            kind="engineer",
            owner=run.engineer,
            description="Record the target repo without agent work",
        ),
    ) as ph:
        ph.log(input="factory cli probe")
    if hold:
        seconds = float(os.environ.get("FACTORY_TEST_HOLD_SECONDS", "0"))
        if seconds:
            time.sleep(seconds)
        else:
            try:
                sys.stdin.read(1)
            except OSError:
                pass
    return run.finish()


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--config", required=True)
    parser.add_argument("--adw-id", default="probe-session")
    parser.add_argument(
        "--hold",
        action="store_true",
        help="Block on stdin after recording so /proc argv can be verified",
    )
    args = parser.parse_args()
    sys.exit(main(args.config, args.adw_id, hold=args.hold))
