diff --git a/inventoryManager.lua b/inventoryManager.lua index c5c7509..668b4de 100644 --- a/inventoryManager.lua +++ b/inventoryManager.lua @@ -2835,6 +2835,12 @@ local function main() while true do local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message") if channel == ORDER_CHANNEL and type(message) == "table" then + -- Idempotency: skip duplicate commands + if isCommandDuplicate(message.commandId) then + log.debug("NET", "Duplicate command skipped: %s", tostring(message.commandId)) + else + recordCommandId(message.commandId) + cleanupCommandIds() local handlerOk, handlerErr = pcall(function() if message.type == "order" and message.itemName and message.amount then log.info("NET", "Order: %s x%d", message.itemName, message.amount) @@ -2848,10 +2854,11 @@ local function main() end pcall(function() networkModem.transmit(replyChannel, ORDER_CHANNEL, { - type = "order_result", - success = success, - message = statusMessage, - color = statusColor, + type = "order_result", + commandId = message.commandId, + success = success, + message = statusMessage, + color = statusColor, }) end) pcall(broadcastState) @@ -2941,9 +2948,10 @@ local function main() end pcall(function() networkModem.transmit(replyChannel, ORDER_CHANNEL, { - type = "craft_result", - success = ok, - error = err, + type = "craft_result", + commandId = message.commandId, + success = ok, + error = err, }) end) smelterNeedsRedraw = true @@ -2954,6 +2962,7 @@ local function main() if not handlerOk then log.error("NET", "Handler error: %s", tostring(handlerErr)) end + end -- end idempotency else end end end