@@ -24,7 +24,7 @@ const MAX_BYTES = 64 * 1024 * 1024
2424const IDENTIFIER = / ^ [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] { 0 , 62 } $ /
2525
2626export type Label = { label : string ; description : string }
27- export type Row = { id : string | number | bigint ; text : string }
27+ export type Row = { id : string | number | bigint ; text : string ; missing ?: boolean }
2828export type Status = "labeled" | "failed" | "unparseable"
2929export type Labeller = ( system : string , user : string , abort : AbortSignal ) => Promise < string >
3030
@@ -87,7 +87,7 @@ export function clampInt(value: number | undefined, fallback: number, min: numbe
8787 return Math . min ( max , Math . max ( min , Math . floor ( value ) ) )
8888}
8989
90- function errorDetails ( error : unknown ) : { message : string ; transient : boolean ; permanentHttp : boolean } {
90+ function errorDetails ( error : unknown ) : { message : string ; transient : boolean ; permanentHttp : boolean ; status ?: number } {
9191 // AI SDK retry errors can wrap the provider's APICallError in lastError/cause.
9292 let current = error
9393 const seen = new Set < unknown > ( )
@@ -109,6 +109,7 @@ function errorDetails(error: unknown): { message: string; transient: boolean; pe
109109 message : `HTTP ${ status } : ${ e . message ?? String ( current ) } ` ,
110110 transient,
111111 permanentHttp : status >= 400 && status < 500 && ! transient ,
112+ status,
112113 }
113114 }
114115 const nested = e . lastError ?? e . cause
@@ -168,6 +169,11 @@ export async function labelRows(input: {
168169 let chars = 2
169170 const size = clampInt ( input . batchSize , DEFAULT_BATCH , 1 , MAX_BATCH )
170171 for ( let index = 0 ; index < input . rows . length ; index ++ ) {
172+ if ( input . rows [ index ] . missing ) {
173+ rowErrors [ index ] = "Source text is NULL; nothing to classify."
174+ record ( rowErrors [ index ] ! )
175+ continue
176+ }
171177 const length = JSON . stringify ( { row : size , text : input . rows [ index ] . text } ) . length + 1
172178 if ( length > BATCH_CHARS ) {
173179 rowErrors [ index ] = "Text exceeds the model batch size limit; shorten it explicitly before retrying."
@@ -193,6 +199,8 @@ export async function labelRows(input: {
193199 let next = 0
194200 let batches = 0
195201 let fatal : string | undefined
202+ let succeeded = 0
203+ let permanentFailures = 0
196204 async function worker ( ) {
197205 while ( next < jobs . length && ! fatal && ! signal . aborted && Date . now ( ) < input . deadline ) {
198206 let pending = jobs [ next ++ ]
@@ -213,7 +221,13 @@ export async function labelRows(input: {
213221 status [ i ] = "failed"
214222 rowErrors [ i ] = detail . message
215223 }
216- if ( detail . permanentHttp ) fatal = detail . message
224+ if ( detail . permanentHttp ) {
225+ // Credential/model errors affect every batch. Other 4xx (e.g. a content filter) may be batch-specific,
226+ // so stop the run only when they repeat with no batch having succeeded.
227+ permanentFailures ++
228+ if ( [ 401 , 403 , 404 ] . includes ( detail . status ?? 0 ) || ( permanentFailures >= 3 && succeeded === 0 ) )
229+ fatal = detail . message
230+ }
217231 if ( ! detail . transient || transientRetries >= 2 || signal . aborted || fatal ) break
218232 try {
219233 await delay ( 250 * 2 ** transientRetries ++ , undefined , { signal } )
@@ -222,6 +236,7 @@ export async function labelRows(input: {
222236 }
223237 continue
224238 }
239+ succeeded ++
225240 const parsed = parseReply ( reply , input . labels , pending . length )
226241 pending = pending . filter ( ( index , position ) => {
227242 const value = parsed . get ( position + 1 )
@@ -333,8 +348,9 @@ export async function labelScratch(input: TextLabelInput, context: LabelContext,
333348 const columns = data . columns . map ( ( column ) => column . toLowerCase ( ) )
334349 const idIndex = columns . indexOf ( args . id_column . toLowerCase ( ) )
335350 const textIndex = columns . indexOf ( args . text_column . toLowerCase ( ) )
336- if ( data . rows . length && ( idIndex < 0 || textIndex < 0 || idIndex === textIndex ) )
351+ if ( idIndex < 0 || textIndex < 0 || idIndex === textIndex )
337352 return fail ( "id_column and text_column must identify distinct columns in the query result." )
353+ if ( ! data . rows . length ) return fail ( "The query returned no rows; nothing to label." )
338354 const rows : Row [ ] = [ ]
339355 const ids = new Set < string > ( )
340356 let numeric : boolean | undefined
@@ -352,7 +368,7 @@ export async function labelScratch(input: TextLabelInput, context: LabelContext,
352368 const text = row [ textIndex ]
353369 if ( text !== null && typeof text !== "string" )
354370 return fail ( "text_column must be text or NULL; cast other types to VARCHAR in SQL." )
355- rows . push ( { id, text : text ?? "" } )
371+ rows . push ( { id, text : text ?? "" , missing : text === null } )
356372 }
357373 const deadline = Date . now ( ) + ( args . max_seconds ?? DEFAULT_SECONDS ) * 1000
358374 const concurrency = clampInt ( Number ( process . env . ALTIMATE_TEXT_LABEL_CONCURRENCY ) , DEFAULT_CONCURRENCY , 1 , 16 )
0 commit comments