The gateway now ships in the
praisonai-bot package. praisonai serve gateway still works exactly as documented here; for a standalone install see praisonai-bot Migration.Quick Start
Default behaviour — nothing to configure
Forensics is on by default. Start the gateway normally:The gateway automatically creates
~/.praisonai/gateway/forensics/ at startup and registers signal handlers. No extra steps needed.On the next crash, check the log line
After a shutdown or crash, search your logs for the This single line tells you the signal, whether the process was supervised, system load at crash time, and peak memory use.
gateway-forensics: marker:How It Works
The gateway setsshould_exit = True first, so draining always begins even if the forensic snapshot takes time under memory pressure. The snapshot and diagnostic are fire-and-forget — they never gate the drain.
What ends up in the log line
| Key | Source | Meaning |
|---|---|---|
signal | passed by handler | SIGTERM or SIGINT |
pid | os.getpid() | Gateway PID |
ppid | os.getppid() | Parent PID; 1 means reparented to init |
supervised | is_supervised(ppid, $INVOCATION_ID) | yes when systemd or container init |
loadavg_1m | os.getloadavg()[0] | 1-minute load average |
traced | /proc/self/status TracerPid | yes if a debugger was attached |
maxrss_kb | getrusage(RUSAGE_SELF).ru_maxrss | Max RSS in kB |
What ends up in the diagnostic file
| Section | Source | ||||
|---|---|---|---|---|---|
# gateway forensics ctx: | The snapshot dict | ||||
## process tree | ps -o pid,ppid,stat,rss,etime,cmd --ppid 1 | ||||
## load | /proc/loadavg | ||||
## recent kernel OOM/killed | `dmesg | grep -iE ‘killed process | out of memory | oom’ | tail -n 20` |
Configuration Options
| Key | Type | Default | Description |
|---|---|---|---|
gateway.forensics.enabled | bool (or "true"/"false" string) | true | Turn shutdown forensics on/off. String values from env-substituted YAML are honoured. |
gateway.forensics.diagnostic_dir | str | ~/.praisonai/gateway/forensics | Where detached gateway-forensics-<pid>.log files are written. The directory is created at startup (bounded 2s), not on the signal path. |
gateway.forensics.stop_timeout | float (seconds) | None | Your supervisor’s configured stop-timeout. Used only by the startup warning to detect mid-drain SIGKILL risk. |
| Var | Equivalent | Default |
|---|---|---|
PRAISONAI_STOP_TIMEOUT | gateway.forensics.stop_timeout | unset |
The startup warning
Ifstop_timeout (from config or PRAISONAI_STOP_TIMEOUT) is less than drain_timeout + 30s, the gateway logs once at startup:
drain_timeout + 30s, or set gateway.forensics.stop_timeout to match your real supervisor config.
Disabling
enabled: "false" when substituting from an environment variable. The log-line snapshot is suppressed too — there is no separate switch.
Common Patterns
systemd unit
TimeoutStopSec to at least drain_timeout + 30s. Export PRAISONAI_STOP_TIMEOUT so the startup warning matches reality.
Kubernetes
diagnostic_dir so reports survive pod termination.
Docker (foreground)
diagnostic_dir onto the host for inspection after container exit.
Programmatic Use
Operators wiring custom supervision can use the pure helpers and the protocol directly:format_forensics_for_log(ctx) renders a snapshot dict as a single log line prefixed with gateway-forensics:. Grep this marker to wire restart-cause dashboards.
Best Practices
Leave forensics on
Leave forensics on
The snapshot is <10ms and never blocks asyncio teardown. The cost of being wrong about a future crash is much higher than the cost of one log line per shutdown.
Match stop_timeout to your supervisor
Match stop_timeout to your supervisor
The startup warning is the only way to learn at boot that your supervisor would SIGKILL you mid-drain. Set
gateway.forensics.stop_timeout or PRAISONAI_STOP_TIMEOUT to match your real supervisor config so the check fires when it matters.Persist diagnostic_dir
Persist diagnostic_dir
A tmpfs or container-local dir loses the report when the container dies. Mount a host path or PVC so diagnostic files are available after restart.
Grep gateway-forensics: in your log scraper
Grep gateway-forensics: in your log scraper
The log marker is stable, single-line, and key=value. Wire it into your log aggregator for restart-cause dashboards:
Don't rely only on the diagnostic file for fast crashes
Don't rely only on the diagnostic file for fast crashes
The detached subprocess writes asynchronously. On an instant SIGKILL of the whole group there may not be enough time. The single log line is your reliable signal — the diagnostic file provides additional detail when time permits.
Related
Gateway Overview
Architecture and Quick Start for the gateway server
Drain Trigger
Control graceful shutdown drain behaviour
Scale to Zero
Suspend the gateway when idle — pay only for active time
Error Handling
Unicode-safe error handling for gateway bot replies
Tracing Hook
Carry the same correlation id onto per-stage spans in your tracer

