11/** Shared daemon runtime status types and systemd cgroup hygiene helpers. */
22import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce" ;
33
4- /** systemd cgroup fields used to spot unhealthy gateway service supervision . */
4+ /** systemd supervision fields used to spot unhealthy or given-up gateway service state . */
55export type GatewayServiceSystemdRuntime = {
66 unit ?: string ;
77 killMode ?: string ;
88 tasksCurrent ?: number ;
99 memoryCurrent ?: number ;
10+ // systemd `Result` (e.g. success, exit-code, start-limit-hit) plus the restart
11+ // counter and configured StartLimitBurst. Together they detect a crash-loop
12+ // give-up that the collapsed `status` string and `Result` alone cannot.
13+ result ?: string ;
14+ nRestarts ?: number ;
15+ startLimitBurst ?: number ;
1016} ;
1117
1218export type GatewayServiceRuntime = {
@@ -29,6 +35,14 @@ export type GatewayServiceRuntime = {
2935export const SYSTEMD_TASKS_CURRENT_WARNING_THRESHOLD = 200 ;
3036export const SYSTEMD_MEMORY_CURRENT_WARNING_BYTES = 2 * 1024 * 1024 * 1024 ;
3137
38+ // EX_CONFIG (78) from sysexits.h. The generated systemd unit pins
39+ // RestartPreventExitStatus=78 (see systemd-unit.ts) so the gateway's
40+ // config-error / duplicate-lock exit (gateway-cli run) deliberately stops
41+ // without a restart. A last exit of 78 therefore means systemd gave up on
42+ // purpose, not that it exhausted StartLimitBurst, so any accumulated NRestarts
43+ // is stale from earlier crashes and must not drive start-limit detection.
44+ const SYSTEMD_NO_RESTART_EXIT_STATUS = 78 ;
45+
3246export function isRiskySystemdKillMode ( value : string | undefined ) : boolean {
3347 const normalized = normalizeLowercaseStringOrEmpty ( value ) ;
3448 return normalized === "process" || normalized === "none" ;
@@ -83,3 +97,45 @@ export function getSystemdCgroupHygieneSummary(
8397export function isSystemdCgroupHygieneRisk ( runtime ?: GatewayServiceSystemdRuntime ) : boolean {
8498 return getSystemdCgroupHygieneSummary ( runtime ) !== null ;
8599}
100+
101+ /**
102+ * True when systemd has stopped auto-restarting the gateway because it crashed
103+ * faster than StartLimitBurst/StartLimitIntervalSec allows. Unlike an ordinary
104+ * stopped/exited unit, this terminal latch needs an explicit `reset-failed` +
105+ * restart to recover, so status/doctor must surface it instead of the generic
106+ * "exited immediately" message.
107+ *
108+ * Detection: the unit is `failed` and either systemd reported the give-up
109+ * directly (Result=start-limit-hit, the start-was-refused-before-exec case) or
110+ * the restart counter reached the configured burst. The counter path is the
111+ * common one: once the gateway process has actually run and exited non-zero,
112+ * systemd keeps Result=exit-code and never overwrites it with start-limit-hit
113+ * (verified against systemd 249), so Result alone misses real crash loops.
114+ *
115+ * The counter path is guarded against the deliberate no-restart exit: a last
116+ * exit of 78 (EX_CONFIG, held back by RestartPreventExitStatus=78) means
117+ * systemd stopped on purpose, so a stale NRestarts left over from earlier
118+ * crashes must not be mistaken for start-limit exhaustion. The explicit
119+ * Result=start-limit-hit signal stays authoritative regardless of exit status.
120+ */
121+ export function isSystemdStartLimitHit ( runtime ?: GatewayServiceRuntime ) : boolean {
122+ if ( ! runtime || normalizeLowercaseStringOrEmpty ( runtime . state ) !== "failed" ) {
123+ return false ;
124+ }
125+ const systemd = runtime . systemd ;
126+ if ( ! systemd ) {
127+ return false ;
128+ }
129+ if ( normalizeLowercaseStringOrEmpty ( systemd . result ) === "start-limit-hit" ) {
130+ return true ;
131+ }
132+ if ( runtime . lastExitStatus === SYSTEMD_NO_RESTART_EXIT_STATUS ) {
133+ return false ;
134+ }
135+ return (
136+ typeof systemd . startLimitBurst === "number" &&
137+ systemd . startLimitBurst > 0 &&
138+ typeof systemd . nRestarts === "number" &&
139+ systemd . nRestarts >= systemd . startLimitBurst
140+ ) ;
141+ }
0 commit comments