Skip to content

Commit cdc4a86

Browse files
authored
Merge pull request #11 from ubiquity/fix/embeddings-cache-review-comments
fix: address embeddings cache review threads
2 parents 2229708 + 2ad4f9c commit cdc4a86

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/openai.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ const EMBEDDINGS_TIMEOUT_MS = 20_000;
352352
// We do not track "last read" to keep writes minimal.
353353
const EMBEDDINGS_CACHE_EVICT_BATCH = 512;
354354
const EMBEDDINGS_CACHE_EVICT_MAX_BATCH = 8192;
355-
const EMBEDDINGS_CACHE_QUOTA_RETRY_ATTEMPTS = 4;
355+
const EMBEDDINGS_CACHE_QUOTA_MAX_RETRIES = 4;
356356
const EMBEDDINGS_JOB_TTL_MS = 24 * 60 * 60_000;
357357
const 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

447448
const 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

Comments
 (0)