From c9103e7d270edfa119fbd4c989fba740cebe947f Mon Sep 17 00:00:00 2001 From: evgeny Date: Tue, 27 Oct 2026 03:49:09 +0100 Subject: [PATCH] Acknowledge committed poll commands when snapshot readback fails --- src/chat/chat_poll.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/chat/chat_poll.c b/src/chat/chat_poll.c index 6a8ccaa01..e235042fd 100644 --- a/src/chat/chat_poll.c +++ b/src/chat/chat_poll.c @@ -11,6 +11,8 @@ #include #include +#define POLL_COMMITTED_ACK_SIZE 192 + /* Канал — канонический положительный ID, подходящий для имени таблицы. */ static int poll_channel(struct UTUN_INSTANCE* inst, const char* channel, uint64_t* id) { if (!inst || !inst->topo_sqlite_db || !channel || !channel[0] || channel[0] == '0' || strlen(channel) > 19) goto invalid; @@ -319,6 +321,15 @@ static int poll_command_error(const char* reason, char** result) { strbuf_free(&out); return -1; } +/* Ack резервируется до изменения: отказ последующего чтения не превращает commit в отказ команды. */ +static int poll_committed_snapshot(struct UTUN_INSTANCE* inst, const char* channel, const uint8_t message[32], char* ack, char** result) { + if (!chat_feedback_snapshot(inst, channel, message, result)) { u_free(ack); return 0; } + char hex[65]; for (unsigned i = 0; i < 32; i++) snprintf(hex + i * 2, 3, "%02x", message[i]); + snprintf(ack, POLL_COMMITTED_ACK_SIZE, "{\"ch\":\"%s\",\"message_id\":\"%s\",\"committed\":true,\"snapshot_pending\":true}", channel, hex); + DEBUG_WARN(DEBUG_CATEGORY_CHAT_SYNC, "poll: command committed ch=%s message=%s; snapshot unavailable, read may be retried", channel, hex); + *result = ack; return 0; +} + /* Общий командный API исключает отдельные правила голосования в каждом интерфейсе. */ int chat_feedback_command(struct UTUN_INSTANCE* inst, const char* command, char** result) { if (result) *result = NULL; @@ -384,9 +395,14 @@ int chat_feedback_command(struct UTUN_INSTANCE* inst, const char* command, char* } final = sqlite3_finalize(st); if (final != SQLITE_OK) DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: options finalize failed rc=%d", final); - if (!ok || step != SQLITE_DONE || final != SQLITE_OK || chat_poll_create(inst, channel, &poll, message_id) < 0) + if (!ok || step != SQLITE_DONE || final != SQLITE_OK) return poll_command_error("Invalid poll or publication failed", result); - return chat_feedback_snapshot(inst, channel, message_id, result); + char* ack = u_malloc(POLL_COMMITTED_ACK_SIZE); + if (!ack) return poll_command_error("Out of memory before publication", result); + if (chat_poll_create(inst, channel, &poll, message_id) < 0) { + u_free(ack); return poll_command_error("Poll publication failed", result); + } + return poll_committed_snapshot(inst, channel, message_id, ack, result); } if (poll_field_text(inst, command, "$.message_id", hex, sizeof(hex), 0) < 0 || strlen(hex) != 64 || sc_hex_to_binary(hex, message_id, 32) != 0) return poll_command_error("Invalid message ID", result); @@ -430,9 +446,14 @@ int chat_feedback_command(struct UTUN_INSTANCE* inst, const char* command, char* } bytes = chat_reactions_encode(ids, count, data, sizeof(data)); kind = CHAT_FEEDBACK_REACTION; } else return poll_command_error("Unknown interaction command", result); - if (bytes < 0 || chat_feedback_set(inst, channel, message_id, kind, data, bytes, NULL) < 0) + if (bytes < 0) return poll_command_error("Invalid state", result); + char* ack = u_malloc(POLL_COMMITTED_ACK_SIZE); + if (!ack) return poll_command_error("Out of memory before saving state", result); + if (chat_feedback_set(inst, channel, message_id, kind, data, bytes, NULL) < 0) { + u_free(ack); return poll_command_error("Cannot save state", result); - return chat_feedback_snapshot(inst, channel, message_id, result); + } + return poll_committed_snapshot(inst, channel, message_id, ack, result); } /* Ответ UI асинхронен: успех/ошибка и исходный request_id всегда идут вместе. */