11// Gateway session lifecycle state projection.
22// Converts agent run lifecycle events into session row/store status updates.
3+ import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice" ;
34import { isAgentLifecycleYieldedWaiting } from "../agents/agent-lifecycle-parent-state.js" ;
45import {
56 buildAgentRunTerminalOutcome ,
67 type AgentRunTerminalOutcome ,
78} from "../agents/agent-run-terminal-outcome.js" ;
9+ import { sanitizeUserFacingText } from "../agents/embedded-agent-helpers/sanitize-user-facing-text.js" ;
810import {
911 isMainSessionRecoveryLifecycleEvent ,
1012 projectMainSessionRecoveryLifecycle ,
@@ -38,13 +40,14 @@ type LifecycleEventLike = Pick<AgentEventPayload, "ts" | "sessionId"> & {
3840
3941type LifecycleSessionShape = Pick <
4042 GatewaySessionRow ,
41- "updatedAt" | "status" | "startedAt" | "endedAt" | "runtimeMs" | "abortedLastRun"
43+ "updatedAt" | "status" | "lastRunError" | " startedAt" | "endedAt" | "runtimeMs" | "abortedLastRun"
4244> ;
4345
4446type PersistedLifecycleSessionShape = Pick <
4547 SessionEntry ,
4648 | "updatedAt"
4749 | "status"
50+ | "lastRunError"
4851 | "startedAt"
4952 | "endedAt"
5053 | "runtimeMs"
@@ -55,6 +58,8 @@ type PersistedLifecycleSessionShape = Pick<
5558
5659type GatewaySessionLifecycleSnapshot = Partial < LifecycleSessionShape > ;
5760
61+ const SESSION_RUN_ERROR_MAX_CHARS = 160 ;
62+
5863function isFiniteTimestamp ( value : unknown ) : value is number {
5964 return typeof value === "number" && Number . isFinite ( value ) && value > 0 ;
6065}
@@ -85,9 +90,9 @@ function mapAgentRunTerminalOutcomeToSessionStatus(
8590 }
8691}
8792
88- function resolveTerminalStatus ( event : LifecycleEventLike ) : SessionRunStatus {
93+ function resolveTerminalOutcome ( event : LifecycleEventLike ) : AgentRunTerminalOutcome {
8994 const phase = resolveLifecyclePhase ( event ) ;
90- const terminal = buildAgentRunTerminalOutcome ( {
95+ return buildAgentRunTerminalOutcome ( {
9196 status : phase === "error" ? "error" : event . data ?. aborted === true ? "timeout" : "ok" ,
9297 error : event . data ?. error ,
9398 stopReason : event . data ?. stopReason ,
@@ -97,7 +102,19 @@ function resolveTerminalStatus(event: LifecycleEventLike): SessionRunStatus {
97102 startedAt : event . data ?. startedAt ,
98103 endedAt : event . data ?. endedAt ?? event . ts ,
99104 } ) ;
100- return mapAgentRunTerminalOutcomeToSessionStatus ( terminal ) ;
105+ }
106+
107+ function resolveSessionRunError (
108+ outcome : AgentRunTerminalOutcome ,
109+ status : SessionRunStatus ,
110+ ) : string | undefined {
111+ if ( ( status !== "failed" && status !== "timeout" ) || ! outcome . error ) {
112+ return undefined ;
113+ }
114+ const sanitized = sanitizeUserFacingText ( outcome . error , { errorContext : true } )
115+ . replace ( / \s + / g, " " )
116+ . trim ( ) ;
117+ return sanitized ? truncateUtf16Safe ( sanitized , SESSION_RUN_ERROR_MAX_CHARS ) : undefined ;
101118}
102119
103120function resolveLifecycleStartedAt (
@@ -157,6 +174,7 @@ function deriveGatewaySessionLifecycleSnapshot(params: {
157174 return {
158175 updatedAt,
159176 status : "running" ,
177+ lastRunError : undefined ,
160178 startedAt,
161179 endedAt : undefined ,
162180 runtimeMs : undefined ,
@@ -167,7 +185,7 @@ function deriveGatewaySessionLifecycleSnapshot(params: {
167185 const startedAt = resolveLifecycleStartedAt ( existing ?. startedAt , params . event ) ;
168186 const endedAt = resolveLifecycleEndedAt ( params . event ) ;
169187 const updatedAt = endedAt ?? existing ?. updatedAt ;
170- const status = isAgentLifecycleYieldedWaiting ( {
188+ const yieldedWaiting = isAgentLifecycleYieldedWaiting ( {
171189 phase,
172190 yielded : params . event . data ?. yielded ,
173191 livenessState : params . event . data ?. livenessState ,
@@ -176,12 +194,13 @@ function deriveGatewaySessionLifecycleSnapshot(params: {
176194 status : params . event . data ?. status ,
177195 timeoutPhase : params . event . data ?. timeoutPhase ,
178196 error : params . event . data ?. error ,
179- } )
180- ? "running"
181- : resolveTerminalStatus ( params . event ) ;
197+ } ) ;
198+ const terminal = yieldedWaiting ? undefined : resolveTerminalOutcome ( params . event ) ;
199+ const status = terminal ? mapAgentRunTerminalOutcomeToSessionStatus ( terminal ) : "running" ;
182200 return {
183201 updatedAt,
184202 status,
203+ lastRunError : terminal ? resolveSessionRunError ( terminal , status ) : undefined ,
185204 startedAt,
186205 endedAt,
187206 runtimeMs : resolveRuntimeMs ( {
0 commit comments