add exponential backoff with jitter. increase retry count to 6 instead of 3
All checks were successful
ci/woodpecker/push/ci Pipeline was successful

This commit is contained in:
2025-08-30 00:04:32 -04:00
parent 2cb1e5e42d
commit 5fdbbf3293

View File

@@ -5,9 +5,8 @@ async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function callOllama(prompt, model = "gemma3:4b", retries = 3) {
let attempt = 0;
while (attempt < retries) {
async function callOllama(prompt, model = "gemma3:4b", retries = 6) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const response = await fetch(OLLAMA_API_URL, {
method: "POST",
@@ -31,14 +30,18 @@ async function callOllama(prompt, model = "gemma3:4b", retries = 3) {
return text;
} catch (err) {
attempt++;
console.warn(`⚠️ Ollama call failed (attempt ${attempt}/${retries}): ${err.message}`);
if (attempt >= retries) throw err;
await sleep(1000 * attempt); // exponential backoff
if (attempt === retries) throw err;
// Exponential backoff with jitter
const delay = Math.pow(2, attempt) * 1000; // 2^attempt seconds
const jitter = Math.random() * 1000; // up to 1 second extra
await sleep(delay + jitter);
}
}
}
/**
* Three-pass dungeon generation with full resiliency and JSON retry
*/
@@ -105,11 +108,11 @@ Dungeon description:
${refined}
`;
const maxJsonRetries = 3;
const maxJsonRetries = 5;
for (let attempt = 1; attempt <= maxJsonRetries; attempt++) {
try {
console.log(`📦 JSON pass (attempt ${attempt}/${maxJsonRetries})...`);
jsonText = await callOllama(jsonPrompt);
jsonText = await callOllama(jsonPrompt, "gemma3:4b", 6);
const cleaned = jsonText.replace(/```json|```/g, "").trim();
const result = JSON.parse(cleaned);
console.log("🎉 Dungeon generation complete!");