# Synopsis: Checks the physical status of the active MoA subagent processes.
# Description: Verifies if the spawned PIDs are still running and returns their active status.

$BaseDir = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
$PidFile = Join-Path $BaseDir "Config\subagent_pids.json"

Write-Output "=== MULTI-AGENT THREAD RUNTIME STATUS ==="

if (Test-Path $PidFile) {
    $Pids = Get-Content $PidFile -Raw | ConvertFrom-Json
    foreach ($p in $Pids) {
        $proc = Get-Process -Id $p -ErrorAction SilentlyContinue
        if ($proc) {
            Write-Output "Process ID: $p | Name: $($proc.ProcessName) | Status: RUNNING | Handle Count: $($proc.HandleCount) | Start Time: $($proc.StartTime.ToString('yyyy-MM-dd HH:mm:ss'))"
        } else {
            Write-Output "Process ID: $p | Status: TERMINATED/CRASHED"
        }
    }
} else {
    Write-Output "[WARNING] No subagent PIDs file found at $PidFile"
}

Write-Output "`n=== CHECKING LAST MODIFIED TIMESTAMPS ==="
foreach ($f in @("Mailbox\To_AgentOSH.json", "Mailbox\Shared_Memory.json", "Mailbox\Active_State.json")) {
    $path = Join-Path $BaseDir $f
    if (Test-Path $path) {
        $item = Get-Item $path
        Write-Output "File: $f | Last Write Time: $($item.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss')) | Size: $($item.Length) bytes"
    }
}
