import psutil, subprocess, time, os, shutil, json
DEPS = r"C:\Users\decid\Documents\projects\spt-core\.worktrees\304-product\target\debug\deps"
os.makedirs(DEPS, exist_ok=True)
ctrl_path = os.path.join(DEPS, "cargo.exe")
shutil.copy(r"C:\Windows\System32\waitfor.exe", ctrl_path)
ctrl = subprocess.Popen([ctrl_path, "/t", "20", "SPTCENSUS2"],
                        creationflags=0x08000000)
time.sleep(2)
NAMES = {"cargo.exe","cargo-nextest.exe","rustc.exe","rustup.exe","rust-analyzer.exe"}
name_hits, path_hits, blind = [], [], []
total = 0
for p in psutil.process_iter(["pid","name"]):
    total += 1
    pid, nm = p.info["pid"], (p.info["name"] or "")
    try:
        exe = p.exe()
    except Exception as e:
        blind.append(f"{pid}:{nm}:{type(e).__name__}"); exe = None
    else:
        if exe == "":
            blind.append(f"{pid}:{nm}:EmptyPath")
    if nm.lower() in NAMES: name_hits.append(f"{pid}:{nm}")
    if exe and exe.lower().startswith(DEPS.lower()): path_hits.append(f"{pid}:{nm}")
cid = ctrl.pid
out = {
 "total_processes": total,
 "control_pid": cid,
 "name_filter_control_TRUE": any(h.startswith(f"{cid}:") for h in name_hits),
 "path_filter_control_TRUE": any(h.startswith(f"{cid}:") for h in path_hits),
 "name_hits_excl_control": [h for h in name_hits if not h.startswith(f"{cid}:")],
 "path_hits_excl_control": [h for h in path_hits if not h.startswith(f"{cid}:")],
 "blind_count": len(blind), "blind": blind,
}
ctrl.kill(); time.sleep(1)
try: os.remove(ctrl_path)
except Exception: pass
out["residue_present"] = os.path.exists(ctrl_path)
print(json.dumps(out, indent=1))
