Skip to content

The agent that grows with you

fix(gateway): keep the ps column header first in the shutdown diagnostic

teknium1011331212fe3
diff --git a/gateway/shutdown_forensics.py b/gateway/shutdown_forensics.py
index 3d1675b7f7d..c7d337d47f2 100644
--- a/gateway/shutdown_forensics.py
+++ b/gateway/shutdown_forensics.py
@@ -121,8 +121,9 @@ def _async_diagnostic_script(signal_name: str, self_pid: int) -> str:
f"echo '=== shutdown diagnostic @ {signal_name} ==='; "
"echo '--- date ---'; date -u +%Y-%m-%dT%H:%M:%SZ; "
"echo '--- ps (top 60 by cpu, comm only) ---'; "
- # ``sort`` instead of GNU ``--sort=-pcpu`` so BSD ps (macOS) produces a listing too.
- "ps -eo pid,ppid,user,pcpu,pmem,stat,comm 2>/dev/null | sort -nrk4 | head -60; "
+ # ``sort`` instead of GNU ``--sort=-pcpu`` so BSD ps (macOS) produces a listing too; the header
+ # line is echoed first so ``sort`` does not bury it among the 0.0-cpu rows.
+ "ps -eo pid,ppid,user,pcpu,pmem,stat,comm 2>/dev/null | { IFS= read -r h; echo \"$h\"; sort -nrk4; } | head -60; "
f"echo '--- pstree of self ---'; pstree -pl {self_pid} 2>/dev/null | head -40 || true; "
"echo '--- loadavg ---'; cat /proc/loadavg 2>/dev/null || sysctl -n vm.loadavg 2>/dev/null || true; "
"echo '--- recent dmesg (oom/killed) ---'; "
diff --git a/tests/gateway/test_shutdown_forensics.py b/tests/gateway/test_shutdown_forensics.py
index 54edd00c8c9..8bd69e6380e 100644
--- a/tests/gateway/test_shutdown_forensics.py
+++ b/tests/gateway/test_shutdown_forensics.py
@@ -161,6 +161,10 @@ class TestSpawnAsyncDiagnostic:
contents = log_path.read_text(encoding="utf-8", errors="replace")
assert "shutdown diagnostic" in contents
assert "SIGTERM" in contents
+ lines = contents.splitlines()
+ ps_section = lines[lines.index("--- ps (top 60 by cpu, comm only) ---") + 1:]
+ assert ps_section and ps_section[0].split()[:2] == ["PID", "PPID"], \
+ "ps column header must lead the listing, not sort as a 0.0-cpu row"
@pytest.mark.linux_only
def test_diagnostic_log_omits_child_argv_and_is_owner_only(self, tmp_path, child_with_secret_argv):