@@ -352,7 +352,7 @@ const EMBEDDINGS_TIMEOUT_MS = 20_000;
352352// We do not track "last read" to keep writes minimal.
353353const EMBEDDINGS_CACHE_EVICT_BATCH = 512 ;
354354const EMBEDDINGS_CACHE_EVICT_MAX_BATCH = 8192 ;
355- const EMBEDDINGS_CACHE_QUOTA_RETRY_ATTEMPTS = 4 ;
355+ const EMBEDDINGS_CACHE_QUOTA_MAX_RETRIES = 4 ;
356356const EMBEDDINGS_JOB_TTL_MS = 24 * 60 * 60_000 ;
357357const EMBEDDINGS_JOB_LOCK_MS = 30_000 ;
358358
@@ -435,13 +435,14 @@ const isEmbeddingsCacheQuotaError = (error: unknown): boolean => {
435435 const message = error instanceof Error ? error . message : typeof error === "string" ? error : "" ;
436436 const combined = `${ name } ${ message } ` . toLowerCase ( ) ;
437437 if ( ! combined ) return false ;
438- return combined . includes ( "quota" ) ||
439- combined . includes ( "insufficient" ) && combined . includes ( "storage" ) ||
440- combined . includes ( "insufficient" ) && combined . includes ( "space" ) ||
438+ return (
439+ combined . includes ( "quota" ) ||
440+ ( combined . includes ( "insufficient" ) && combined . includes ( "storage" ) ) ||
441+ ( combined . includes ( "insufficient" ) && combined . includes ( "space" ) ) ||
441442 combined . includes ( "no space" ) ||
442443 combined . includes ( "storage limit" ) ||
443- combined . includes ( "storage" ) && combined . includes ( "exceeded" ) ||
444- combined . includes ( "limit" ) && combined . includes ( "exceeded" ) ;
444+ ( combined . includes ( "storage" ) && combined . includes ( "exceeded" ) )
445+ ) ;
445446} ;
446447
447448const writeEmbeddingsCacheEntry = async (
@@ -494,7 +495,8 @@ const writeEmbeddingsCacheEntryBestEffort = async (
494495 deadlineMs : number ,
495496) : Promise < { isNew : boolean } > => {
496497 let evictBatch = EMBEDDINGS_CACHE_EVICT_BATCH ;
497- for ( let attempt = 0 ; attempt <= EMBEDDINGS_CACHE_QUOTA_RETRY_ATTEMPTS ; attempt += 1 ) {
498+ // Attempts = 1 (initial write) + max retries.
499+ for ( let attempt = 0 ; attempt <= EMBEDDINGS_CACHE_QUOTA_MAX_RETRIES ; attempt += 1 ) {
498500 if ( Date . now ( ) >= deadlineMs ) return { isNew : false } ;
499501 try {
500502 return await writeEmbeddingsCacheEntry ( kv , cacheModelKey , hash , embedding , createdAtMs ) ;
@@ -510,7 +512,7 @@ const writeEmbeddingsCacheEntryBestEffort = async (
510512 console . warn (
511513 `[ai.ubq.fi] embeddings_cache quota eviction model=${ cacheModelKey } evicted=${ evicted . evicted_embeddings } stale_index_deleted=${ evicted . deleted_stale_index_keys } batch=${ evictBatch } ` ,
512514 ) ;
513- if ( evicted . evicted_embeddings <= 0 ) return { isNew : false } ;
515+ if ( evicted . evicted_embeddings <= 0 && evicted . deleted_stale_index_keys <= 0 ) return { isNew : false } ;
514516 } catch ( evictError ) {
515517 console . warn ( "[ai.ubq.fi] embeddings_cache quota eviction failed:" , evictError ) ;
516518 return { isNew : false } ;
@@ -1604,21 +1606,19 @@ export const handleEmbeddings = async (req: Request, usageContext?: UsageContext
16041606 } ) ;
16051607 }
16061608
1607- let wroteNew = 0 ;
16081609 for ( let i = 0 ; i < chunkItems . length ; i += 1 ) {
16091610 const item = chunkItems [ i ] ! ;
16101611 const vec = vectors [ i ] ! ;
16111612 for ( const idx of item . indices ) vectorsByIndex [ idx ] = vec ;
16121613 if ( shouldCache && kv ) {
1613- const result = await writeEmbeddingsCacheEntryBestEffort (
1614+ await writeEmbeddingsCacheEntryBestEffort (
16141615 kv ,
16151616 cacheModelKey ,
16161617 item . hash ,
16171618 vec ,
16181619 Date . now ( ) ,
16191620 deadlineMs ,
16201621 ) ;
1621- if ( result . isNew ) wroteNew += 1 ;
16221622 }
16231623 }
16241624 }
0 commit comments