Restore live autonomy with direct runtime and fresh snapshot capacity

This commit is contained in:
Codex
2026-09-22 15:07:24 -04:00
parent c5f84a7221
commit bc50395221
6 changed files with 103 additions and 16 deletions
+21 -5
View File
@@ -93,12 +93,20 @@ function rows(sql){
}
async function deliverEvents(){
// Reserve capacity for current snapshots so historical events cannot starve
// live decisions. The remaining slots continue draining history in order.
const fresh=rows(`SELECT id,HEX(kind),HEX(payload)
FROM acore_auth.azerothai_event_outbox
WHERE delivered_at IS NULL AND kind='player.snapshot'
AND created_at >= NOW() - INTERVAL 90 SECOND
ORDER BY id ASC LIMIT ${Math.max(1,Math.floor(EVENT_BATCH/2))}`);
const batch=rows(`SELECT id,HEX(kind),HEX(payload)
FROM acore_auth.azerothai_event_outbox
WHERE delivered_at IS NULL
ORDER BY CASE WHEN kind IN ('player.whisper','player.chat') THEN 0 ELSE 1 END, id
LIMIT ${EVENT_BATCH}`);
for(const [id,kindHex,payloadHex] of batch){
${fresh.length?`AND id NOT IN (${fresh.map(row=>Number(row[0])).join(',')})`:''}
ORDER BY CASE WHEN kind IN ('player.whisper','player.chat') AND created_at >= NOW() - INTERVAL 90 SECOND THEN 0 ELSE 1 END, id
LIMIT ${Math.max(1,EVENT_BATCH-fresh.length)}`);
for(const [id,kindHex,payloadHex] of [...fresh,...batch]){
const kind=Buffer.from(kindHex||'','hex').toString('utf8');
const payloadText=Buffer.from(payloadHex||'','hex').toString('utf8');
let payload;
@@ -116,7 +124,7 @@ async function deliverEvents(){
mysql(`UPDATE acore_auth.azerothai_event_outbox SET delivered_at=NOW(6) WHERE id=${Number(id)}`);
if(EVENT_GAP_MS)await sleep(EVENT_GAP_MS);
}
return batch.length;
return fresh.length+batch.length;
}
async function deliverResults(){
@@ -136,6 +144,14 @@ async function deliverResults(){
function cleanup(){
mysql(`
CREATE TABLE IF NOT EXISTS acore_auth.azerothai_snapshot_archive LIKE acore_auth.azerothai_event_outbox;
INSERT IGNORE INTO acore_auth.azerothai_snapshot_archive
SELECT * FROM acore_auth.azerothai_event_outbox
WHERE delivered_at IS NULL AND kind='player.snapshot'
AND created_at < NOW() - INTERVAL 90 SECOND;
UPDATE acore_auth.azerothai_event_outbox e
JOIN acore_auth.azerothai_snapshot_archive a ON a.id=e.id
SET e.delivered_at=NOW(6) WHERE e.delivered_at IS NULL;
UPDATE acore_auth.azerothai_event_outbox
SET delivered_at=NOW(6)
WHERE delivered_at IS NULL
@@ -151,10 +167,10 @@ console.log(`AzerothAI ALE DB relay started: ${BASE}, poll ${POLL_MS}ms`);
while(!stopping){
try{
if(Date.now()-lastCleanup>60_000){cleanup();lastCleanup=Date.now();}
await deliverEvents();
await deliverResults();
await pullCommand();
if(Date.now()-lastCleanup>60_000){cleanup();lastCleanup=Date.now();}
await sleep(POLL_MS);
}catch(error){
console.error(new Date().toISOString(),error instanceof Error?error.message:String(error));