Fix double-push race + add slug debug log

Two fs.watch callbacks can fire for the same row within the 300ms debounce
window. Both enter the setTimeout, both read prevSide=null before either
sets it, both call send(). Result: duplicate push within ~20ms.

Fix: track lastSentTs. Same row timestamp = skip. Only a NEW row from
the bot (or a new manual append) can trigger another push.

Also log the slug before each push so we can diagnose the missing link.
This commit is contained in:
2026-07-21 18:25:02 +08:00
parent 9b0da0279f
commit ee2ab5dda4
+8 -2
View File
@@ -25,6 +25,7 @@ if (!DRY_RUN && (!BOT_TOKEN || !CHAT_ID)) {
let prevSide = null;
let lastSentAt = 0;
let lastSentTs = null;
async function send(text) {
if (DRY_RUN) {
@@ -115,12 +116,17 @@ function watch() {
return;
}
const [, , phase, strength] = data.rec.split(":");
const ts = data.row[data.hdr.indexOf("timestamp")];
const flipped = side !== prevSide;
const cooled = Date.now() - lastSentAt > COOLDOWN_MS;
if (flipped && cooled) {
const msg = buildMessage(data.hdr, data.row, side, phase, strength, getCurrentSlug());
const newRow = ts !== lastSentTs;
if (flipped && cooled && newRow) {
const slug = getCurrentSlug();
console.log("[watcher] slug for push:", slug);
const msg = buildMessage(data.hdr, data.row, side, phase, strength, slug);
await send(msg);
lastSentAt = Date.now();
lastSentTs = ts;
}
prevSide = side;
} catch (e) {