"""Test 05 — Project detection & analysis (mission §2.D, §6, §18). Uses a REAL python subprocess (with torch importable? no - the sandbox has no torch, so the interpreter probe honestly reports torch=None) running a script that looks like an SR training project, plus a fake project tree with configs/logs/checkpoints, and verifies: - detection from combined evidence (not filename alone) - config discovery + dataset path extraction - log/checkpoint discovery - interpreter report (read-only subprocess probe) - live training-state evidence """ from __future__ import annotations import json import os import sys import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from lpu.runtime.process_discovery import ProcessDiscovery from lpu.project_detection.analyzer import analyze_process from lpu.monitoring.gpu_monitor import GPUMonitor def _make_fake_project(root: Path): (root / "configs").mkdir(parents=True) (root / "checkpoints").mkdir() (root / "logs").mkdir() (root / "requirements.txt").write_text("torch\n", encoding="utf-8") (root / "configs" / "train.json").write_text(json.dumps({ "model": {"n_resblocks": 16}, "training": {"batch_size": 16, "learning_rate": 3e-4, "epochs": 100, "num_workers": 0}, "data": {"data_root": "D:/BehNTIRData/BigDataDIFL2K", "train_dir": "train", "val_dir": "val"}, }), encoding="utf-8") (root / "configs" / "unrelated.json").write_text(json.dumps( {"theme": "dark", "font": 12}), encoding="utf-8") (root / "checkpoints" / "model_best.pth").write_bytes(b"x" * 64) (root / "checkpoints" / "model_last.pth").write_bytes(b"x" * 32) (root / "logs" / "train_log.txt").write_text("epoch 1 loss 0.5\n", encoding="utf-8") return root / "train_sr.py" def test_analyzer_full_evidence(isolated_lpu_home, tmp_path): root = tmp_path / "srproj" root.mkdir() script = _make_fake_project(root) # sandbox python has no torch, so the header scan degrades to the # script-name evidence (still multi-evidence via cmdline + root scan) script.write_text("import time\ntime.sleep(25)\n", encoding="utf-8") import subprocess proc = subprocess.Popen([sys.executable, str(script)]) try: disc = ProcessDiscovery(deep_scan=True, gpu_monitor=GPUMonitor(enabled=False)) found = [] deadline = time.time() + 10 while time.time() < deadline: found = [p for p in disc.scan() if p.pid == proc.pid] if found: break time.sleep(0.2) assert found, "the training-like process must be discovered" dp = found[0] analysis = analyze_process(dp.summary()) assert analysis.entry_script and analysis.entry_script.endswith("train_sr.py") assert analysis.project_root and Path(analysis.project_root).exists() assert analysis.configs, "training config must be found" cfg = next(c for c in analysis.configs if "train.json" in c["path"]) assert "batch_size" in cfg["matched_keys"] assert cfg["dataset_paths"], "dataset paths must be extracted from config" assert "BigDataDIFL2K" in " ".join(analysis.dataset_paths) assert len(analysis.checkpoint_files) == 2 assert analysis.log_files and any("train_log" in f["path"] for f in analysis.log_files) assert analysis.parent_chain, "process tree evidence must be recorded" assert analysis.interpreter is not None # sandbox python has no torch -> honest None, never a fabricated version assert analysis.interpreter.get("python") assert analysis.evidence, "evidence classes must be listed" finally: proc.kill() proc.wait(timeout=5) def test_neutral_script_filtered_and_torch_header_detected(isolated_lpu_home, tmp_path): """V6 design contract kept in V7: a plain .py process with NO training evidence is filtered out of discovery (not 'unknown' in results), while a script whose header imports torch is classified even without a training-like filename (deep_scan).""" import subprocess neutral = tmp_path / "portrait.py" neutral.write_text("import time\ntime.sleep(20)\n", encoding="utf-8") # neutral name; torch import guarded so the process STAYS alive even on # interpreters without torch (the header regex still matches "import torch") infer = tmp_path / "portrait2.py" infer.write_text( "import time\ntry:\n import torch\nexcept ImportError:\n pass\n" "time.sleep(20)\n", encoding="utf-8") p1 = subprocess.Popen([sys.executable, str(neutral)]) p2 = subprocess.Popen([sys.executable, str(infer)]) try: disc = ProcessDiscovery(deep_scan=True, gpu_monitor=GPUMonitor(enabled=False)) deadline = time.time() + 10 found1 = found2 = [] while time.time() < deadline: scan = disc.scan() found1 = [p for p in scan if p.pid == p1.pid] found2 = [p for p in scan if p.pid == p2.pid] if found2: break time.sleep(0.3) assert found1 == [], "neutral script with no evidence must be filtered" assert found2 and found2[0].kind == "inference", \ "torch import in the header must classify the process (deep scan)" finally: for p in (p1, p2): p.kill() p.wait(timeout=5) def test_detection_never_touches_the_project(tmp_path): """Read-only guarantee: file hashes unchanged after a discovery+analysis sweep (mission §13/§21 rule: no modifications to foreign projects).""" import hashlib root = tmp_path / "proj2" root.mkdir() script = _make_fake_project(root) files = sorted(p for p in root.rglob("*") if p.is_file()) before = {p: hashlib.sha256(p.read_bytes()).hexdigest() for p in files} import subprocess code = "import time\ntime.sleep(15)\n" proc = subprocess.Popen([sys.executable, "-c", str(script)], cwd=str(root)) try: disc = ProcessDiscovery(deep_scan=True, gpu_monitor=GPUMonitor(enabled=False)) for _ in range(3): disc.scan() finally: proc.kill() proc.wait(timeout=5) for p in files: assert hashlib.sha256(p.read_bytes()).hexdigest() == before[p], \ f"project file {p} was MODIFIED - detection must be read-only"