@@ -31,7 +31,12 @@ import { decryptEnvMap } from "../../lib/encryption";
3131import { notification } from "../../lib/notification-dispatcher" ;
3232import { jobRunBus } from "./job-run.sse" ;
3333import { boundedStorableText } from "../deployments/build-log-sanitize" ;
34- import { resolveServerIds , type CommandConfig , type JobNotifyConfig , type JobRunState } from "./job.types" ;
34+ import {
35+ resolveServerIds ,
36+ type CommandConfig ,
37+ type JobNotifyConfig ,
38+ type JobRunState ,
39+ } from "./job.types" ;
3540
3641/** Cap stored output so a chatty command can't bloat the row. */
3742const MAX_OUTPUT = 200_000 ;
@@ -103,7 +108,7 @@ async function runOnServer(
103108async function executeAttempt (
104109 cfg : CommandConfig ,
105110 streamId : string ,
106- ) : Promise < { status : JobRunState ; output : string ; error ?: string } > {
111+ ) : Promise < { status : JobRunState ; output : string ; error ?: string ; exitCode ?: number } > {
107112 const publish = ( line : string , level : LogEntry [ "level" ] ) =>
108113 jobRunBus . publish ( streamId , { type : "log" , line, level } ) ;
109114 try {
@@ -113,7 +118,7 @@ async function executeAttempt(
113118 }
114119 const command = buildCommand ( cfg ) ;
115120
116- let code : number ;
121+ let code : number | null ;
117122 let output : string ;
118123 if ( servers . length === 1 ) {
119124 const r = await runOnServer ( servers [ 0 ] , command , ( e ) => publish ( e . message , e . level ) , cfg . timeoutMs ) ;
@@ -128,17 +133,23 @@ async function executeAttempt(
128133 } catch ( err ) {
129134 const msg = safeErrorMessage ( err ) ;
130135 publish ( `[${ sid } ] ${ msg } ` , "error" ) ;
131- return { sid, code : 1 , output : msg } ;
136+ return { sid, code : null , output : msg } ;
132137 }
133138 } ) ,
134139 ) ;
135- code = results . every ( ( r ) => r . code === 0 ) ? 0 : 1 ;
136- output = results . map ( ( r ) => `── ${ r . sid } (exit ${ r . code } ) ──\n${ r . output } ` ) . join ( "\n\n" ) ;
140+ const firstFailure = results . find ( ( r ) => r . code !== 0 ) ;
141+ code = firstFailure ? firstFailure . code : 0 ;
142+ output = results . map ( ( r ) => `── ${ r . sid } (exit ${ r . code ?? "unknown" } ) ──\n${ r . output } ` ) . join ( "\n\n" ) ;
137143 }
138144
139145 return code === 0
140- ? { status : "success" , output }
141- : { status : "failed" , output, error : `Command exited with code ${ code } ` } ;
146+ ? { status : "success" , output, exitCode : 0 }
147+ : {
148+ status : "failed" ,
149+ output,
150+ exitCode : code ?? undefined ,
151+ error : code === null ? "Command failed without an exit status" : `Command exited with code ${ code } ` ,
152+ } ;
142153 } catch ( err ) {
143154 const message = safeErrorMessage ( err ) ;
144155 publish ( message , "error" ) ;
@@ -161,7 +172,7 @@ async function runLoop(row: Job, run: JobRun): Promise<void> {
161172 await emitJobRun ( row , run . id , "running" ) ;
162173
163174 let finalStatus : JobRunState = "failed" ;
164- let exitCode = 1 ;
175+ let exitCode : number | undefined ;
165176 let lastError : string | undefined ;
166177 let attemptsUsed = 0 ;
167178 const chunks : string [ ] = [ ] ;
@@ -175,6 +186,7 @@ async function runLoop(row: Job, run: JobRun): Promise<void> {
175186 }
176187 const res = await executeAttempt ( cfg , run . id ) ;
177188 chunks . push ( res . output ) ;
189+ exitCode = res . exitCode ;
178190 if ( res . status === "success" ) {
179191 finalStatus = "success" ;
180192 exitCode = 0 ;
@@ -185,9 +197,10 @@ async function runLoop(row: Job, run: JobRun): Promise<void> {
185197 if ( attempt < maxAttempts && backoffMs ) await sleep ( backoffMs ) ;
186198 }
187199
200+ const durationMs = Date . now ( ) - startedMs ;
188201 await finishRunRow ( run . id , row . key , {
189202 status : finalStatus ,
190- durationMs : Date . now ( ) - startedMs ,
203+ durationMs,
191204 summary : { exitCode, attempts : attemptsUsed } ,
192205 output : boundedStorableText ( chunks . join ( "\n" ) , MAX_OUTPUT ) ,
193206 error :
@@ -198,10 +211,17 @@ async function runLoop(row: Job, run: JobRun): Promise<void> {
198211 try {
199212 jobRunBus . publish ( run . id , { type : "complete" , status : finalStatus , error : lastError } ) ;
200213 } catch ( err ) {
201- console . error ( `[job] ${ row . key } run ${ run . id } : terminal SSE publish failed: ${ safeErrorMessage ( err ) } ` ) ;
214+ console . error (
215+ `[job] ${ row . key } run ${ run . id } : terminal SSE publish failed: ${ safeErrorMessage ( err ) } ` ,
216+ ) ;
202217 }
203218
204- await emitJobRun ( row , run . id , finalStatus ) ;
219+ await emitJobRun ( row , run . id , finalStatus , {
220+ durationMs,
221+ exitCode,
222+ error : lastError ? boundedStorableText ( lastError , MAX_ERROR ) : undefined ,
223+ output : chunks . join ( "\n" ) ,
224+ } ) ;
205225 if ( finalStatus === "success" ) await fireDependents ( row . key ) ;
206226}
207227
@@ -307,11 +327,45 @@ async function resolveOrgIdForUser(userId: string): Promise<string | null> {
307327 return members [ 0 ] ?. organizationId ?? null ;
308328}
309329
330+ const MAX_NOTIFY_LOG_LINES = 20 ;
331+ const MAX_NOTIFY_LOG_CHARS = 2000 ;
332+
333+ function extractLogExcerpt ( output ?: string ) : string | undefined {
334+ if ( ! output ) return undefined ;
335+ const trimmed = output . trim ( ) ;
336+ if ( ! trimmed ) return undefined ;
337+ const lines = trimmed . split ( "\n" ) ;
338+ const tail = boundedStorableText ( lines . slice ( - MAX_NOTIFY_LOG_LINES ) . join ( "\n" ) , Number . MAX_SAFE_INTEGER ) ;
339+ if ( tail . length <= MAX_NOTIFY_LOG_CHARS ) return tail ;
340+ return boundedStorableText ( `…\n${ tail . slice ( - ( MAX_NOTIFY_LOG_CHARS - 2 ) ) } ` , MAX_NOTIFY_LOG_CHARS ) ;
341+ }
342+
310343/** Notify on a run state. Per-job `notifyConfig` (if present) OVERRIDES the
311344 * global Settings subscriptions — its channels/states win, no double-fire. */
312- async function emitJobRun ( row : Job , runId : string , status : JobRunState ) : Promise < void > {
345+ async function emitJobRun (
346+ row : Job ,
347+ runId : string ,
348+ status : JobRunState ,
349+ meta ?: {
350+ durationMs ?: number ;
351+ exitCode ?: number ;
352+ error ?: string ;
353+ output ?: string ;
354+ } ,
355+ ) : Promise < void > {
313356 try {
314- const payload = { label : row . label , jobKey : row . key , status, runId } ;
357+ const logExcerpt = extractLogExcerpt ( meta ?. output ) ;
358+ const payload : Record < string , unknown > = {
359+ label : row . label ,
360+ jobName : row . label ,
361+ jobKey : row . key ,
362+ status,
363+ runId,
364+ ...( meta ?. durationMs !== undefined ? { durationMs : meta . durationMs } : { } ) ,
365+ ...( meta ?. exitCode !== undefined ? { exitCode : meta . exitCode } : { } ) ,
366+ ...( meta ?. error ? { errorMessage : meta . error } : { } ) ,
367+ ...( logExcerpt ? { logExcerpt } : { } ) ,
368+ } ;
315369 const notify = row . notifyConfig as JobNotifyConfig | null ;
316370
317371 if ( notify ?. channels ?. length ) {
0 commit comments