@@ -4,11 +4,38 @@ import { createHash } from 'node:crypto';
44import { translationAdvancedArgs } from './kernel-options.mjs' ;
55import { createTranslationCache } from '../translation/translation-cache.mjs' ;
66import { buildKernelServiceConfig } from './kernel-services.mjs' ;
7+ import { redactDiagnosticText } from '../diagnostics/developer-diagnostics.mjs' ;
78import {
89 isTranslationLanguageSupported ,
910 translationLanguageCode ,
1011} from '../../shared/translation/languages.mjs' ;
1112
13+ const MAX_STDERR_TAIL_LENGTH = 4 * 1024 ;
14+ const USEFUL_ERROR_LINE =
15+ / (?: ^ | [ \s : ] ) (?: [ A - Z a - z _ ] [ \w . ] * (?: E r r o r | E x c e p t i o n | F a i l u r e | I n t e r r u p t ) \b | (?: e r r o r | e x c e p t i o n | f a i l u r e | f a t a l | f a i l e d ) \b ) / i;
16+
17+ function appendTail ( current , value , limit ) {
18+ const next = current + value ;
19+ return next . length > limit ? next . slice ( - limit ) : next ;
20+ }
21+
22+ function finalStderrMessage ( stderr ) {
23+ const lines = String ( stderr )
24+ . split ( / \r ? \n / )
25+ . map ( ( line ) => line . trim ( ) )
26+ . filter ( Boolean ) ;
27+ if ( ! lines . length ) return '' ;
28+ const exception = [ ...lines ]
29+ . reverse ( )
30+ . find (
31+ ( line ) =>
32+ USEFUL_ERROR_LINE . test ( line ) && ! / ^ T r a c e b a c k \( m o s t r e c e n t c a l l l a s t \) : $ / i. test ( line ) ,
33+ ) ;
34+ if ( exception ) return exception ;
35+ const last = lines . at ( - 1 ) ;
36+ return / ^ T r a c e b a c k \( m o s t r e c e n t c a l l l a s t \) : $ / i. test ( last ) ? '' : last ;
37+ }
38+
1239let pdfLib ;
1340
1441function loadPdfLib ( ) {
@@ -289,10 +316,15 @@ export function createTranslationRunner({
289316 secrets : [ proxy ?. token , ...( service ?. secrets || [ ] ) ] ,
290317 } ,
291318 ) ;
319+ const secrets = [ proxy ?. token , ...( service ?. secrets || [ ] ) ] ;
320+ let stderrTail = '' ;
292321 child . stderr . on ( 'data' , ( chunk ) => {
293- let message = String ( chunk ) ;
294- for ( const secret of [ proxy . token , ...( service ?. secrets || [ ] ) ] )
295- if ( secret ) message = message . replaceAll ( secret , '[redacted]' ) ;
322+ const text = String ( chunk ) ;
323+ const message = redactDiagnosticText ( text , {
324+ secrets,
325+ maxLength : Math . max ( text . length , 1 ) ,
326+ } ) ;
327+ stderrTail = appendTail ( stderrTail , message , MAX_STDERR_TAIL_LENGTH ) ;
296328 onDiagnostic ?. ( message ) ;
297329 } ) ;
298330 const kill = ( ) => {
@@ -301,7 +333,7 @@ export function createTranslationRunner({
301333 const timer = setTimeout ( kill , 15 * 60 * 1000 ) ;
302334 signal ?. addEventListener ( 'abort' , kill , { once : true } ) ;
303335 child . on ( 'error' , ( ) => reject ( Error ( 'Kernel could not start' ) ) ) ;
304- child . on ( 'close' , ( code ) => {
336+ child . on ( 'close' , ( code , exitSignal ) => {
305337 clearTimeout ( timer ) ;
306338 signal ?. removeEventListener ( 'abort' , kill ) ;
307339 code === 0
@@ -310,7 +342,15 @@ export function createTranslationRunner({
310342 Error (
311343 signal ?. aborted
312344 ? 'Cancelled'
313- : 'Kernel translation failed. Check its runtime assets and provider configuration.' ,
345+ : `Kernel translation failed: ${
346+ finalStderrMessage (
347+ redactDiagnosticText ( stderrTail , {
348+ secrets,
349+ maxLength : MAX_STDERR_TAIL_LENGTH ,
350+ } ) ,
351+ ) ||
352+ ( exitSignal ? `signal ${ exitSignal } ` : `exit code ${ code ?? 'unknown' } ` )
353+ } `,
314354 ) ,
315355 ) ;
316356 } ) ;
0 commit comments