Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: replace polling loop with promise-returning replenishType

Adam Eivy7159fb1adcf2
diff --git a/server/services/meatspacePostDrillCache.js b/server/services/meatspacePostDrillCache.js
index 58c796ac3d..1979c08a2a 100644
--- a/server/services/meatspacePostDrillCache.js
+++ b/server/services/meatspacePostDrillCache.js
@@ -19,8 +19,10 @@ const CACHEABLE_TYPES = [
'compound-chain', 'bridge-word', 'double-meaning', 'idiom-twist',
];
+const delay = ms => new Promise(r => setTimeout(r, ms));
+
let cache = {}; // { type: [drill, drill, ...] }
-let replenishing = new Set(); // types currently being refilled
+let replenishing = new Map(); // type -> Promise (in-flight replenishment)
let saveQueued = false;
async function loadCache() {
@@ -46,19 +48,17 @@ function debouncedSave() {
}
function replenishType(type, providerId, model) {
- if (replenishing.has(type)) return;
- if ((cache[type]?.length || 0) >= MIN_PER_TYPE) return;
+ if (replenishing.has(type)) return replenishing.get(type);
+ if ((cache[type]?.length || 0) >= MIN_PER_TYPE) return Promise.resolve();
- replenishing.add(type);
const needed = MAX_PER_TYPE - (cache[type]?.length || 0);
- (async () => {
+ const promise = (async () => {
let generated = 0;
let consecutiveFailures = 0;
try {
for (let i = 0; i < needed; i++) {
- // Pause between LLM calls to avoid rate limiting
- if (i > 0) await new Promise(r => setTimeout(r, 2000));
+ if (i > 0) await delay(2000); // avoid LLM rate limits
const drill = await generateLlmDrill(type, { count: 5 }, providerId, model).catch(err => {
console.log(`⚠️ POST cache: failed to generate ${type}: ${err.message}`);
return null;
@@ -69,7 +69,6 @@ function replenishType(type, providerId, model) {
consecutiveFailures = 0;
} else {
consecutiveFailures++;
- // Bail early if provider appears unavailable (2+ consecutive failures)
if (consecutiveFailures >= 2) {
console.log(`⚠️ POST cache: bailing on ${type} after ${consecutiveFailures} consecutive failures`);
break;
@@ -84,6 +83,9 @@ function replenishType(type, providerId, model) {
replenishing.delete(type);
}
})();
+
+ replenishing.set(type, promise);
+ return promise;
}
/**
@@ -132,11 +134,7 @@ export async function initDrillCache(providerId, model) {
console.log(`📦 POST cache: queuing startup fill for ${lowTypes.length} types`);
(async () => {
for (const type of lowTypes) {
- replenishType(type, providerId, model);
- // Wait for this type to finish before starting the next
- while (replenishing.has(type)) {
- await new Promise(r => setTimeout(r, 1000));
- }
+ await replenishType(type, providerId, model);
}
})();
}