Skip to main content
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.
When the gateway dies unexpectedly, forensics writes one log line and one diagnostic file so the next boot can tell you why.
The user keeps chatting until SIGTERM or OOM; forensics captures a snapshot line and a detached diagnostic file for the next operator review.

Quick Start

1

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.
2

On the next crash, check the log line

After a shutdown or crash, search your logs for the gateway-forensics: marker:
This single line tells you the signal, whether the process was supervised, system load at crash time, and peak memory use.
3

Read the detached diagnostic file

The gateway also writes a full diagnostic report that survives a SIGKILL of the process group (started with start_new_session=True):

How It Works

The gateway sets should_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

Any field that can’t be read is silently omitted from the line.

What ends up in the diagnostic file


Configuration Options

Environment variable:

The startup warning

If stop_timeout (from config or PRAISONAI_STOP_TIMEOUT) is less than drain_timeout + 30s, the gateway logs once at startup:
To silence it: raise your supervisor’s stop-timeout to at least drain_timeout + 30s, or set gateway.forensics.stop_timeout to match your real supervisor config.

Disabling

Set enabled: "false" when substituting from an environment variable. The log-line snapshot is suppressed too — there is no separate switch.

Common Patterns

systemd unit

Set TimeoutStopSec to at least drain_timeout + 30s. Export PRAISONAI_STOP_TIMEOUT so the startup warning matches reality.

Kubernetes

Mount a persistent volume at diagnostic_dir so reports survive pod termination.

Docker (foreground)

Bind-mount 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

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.
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.
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.
The log marker is stable, single-line, and key=value. Wire it into your log aggregator for restart-cause dashboards:
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.

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

Event-Loop Watchdog

Dump all-thread stacks and restart when the asyncio loop wedges