import os
import signal
import sys

# Pass the server PID captured from the dev server's owned terminal session.
server_pid = int(sys.argv[1])
worktree = os.path.realpath(sys.argv[2])
assert os.path.realpath(f'/proc/{server_pid}/cwd').startswith(worktree + '/')
children = open(f'/proc/{server_pid}/task/{server_pid}/children').read().split()
candidates = []
for child in children:
    command = open(f'/proc/{child}/cmdline', 'rb').read().split(b'\0')
    if b'app-server' in command and os.path.basename(command[0]) == b'codex':
        assert os.path.realpath(f'/proc/{child}/cwd').startswith(worktree + '/')
        candidates.append(int(child))
assert len(candidates) == 1, candidates
os.kill(candidates[0], signal.SIGSTOP)
print(candidates[0])
