forge: step 7 — add response coordinator

This commit is contained in:
cj-vana
2026-05-14 20:03:13 -06:00
parent e78dcbbce8
commit c37e006732
3 changed files with 1106 additions and 0 deletions

View File

@@ -6,6 +6,8 @@
#include "BotPolicy.h"
#include "EmergencyForwarder.h"
#include "FirmwareBot.h"
#include "KnownBotRegistry.h"
#include "ResponseCoordinator.h"
static void test_channel_policy() {
assert(BotPolicy::classifyChannel(NULL, 0, true) == BOT_CHANNEL_DM);
@@ -320,6 +322,53 @@ static void test_emergency_forward_truncation() {
assert(strstr(forward.parts[forward.part_count - 1], "...") != NULL);
}
static void test_known_bot_registry() {
BotKnownBotEntry entries[2];
uint8_t key1[BOT_SENDER_KEY_PREFIX_LEN] = { 1, 2, 3, 4, 5, 6 };
uint8_t key2[BOT_SENDER_KEY_PREFIX_LEN] = { 7, 8, 9, 10, 11, 12 };
uint8_t key3[BOT_SENDER_KEY_PREFIX_LEN] = { 13, 14, 15, 16, 17, 18 };
KnownBotRegistry::clear(entries, 2);
assert(!KnownBotRegistry::find(entries, 2, key1, BOT_SENDER_KEY_PREFIX_LEN));
assert(KnownBotRegistry::add(entries, 2, key1, BOT_KNOWN_BOT_FLAG_SUPPRESS_NORMAL, "alpha-bot"));
const BotKnownBotEntry* entry = KnownBotRegistry::find(entries, 2, key1, BOT_SENDER_KEY_PREFIX_LEN);
assert(entry != NULL);
assert(entry->active);
assert(strcmp(entry->label, "alpha-bot") == 0);
assert(KnownBotRegistry::canSuppressNormal(entries, 2, key1, BOT_SENDER_KEY_PREFIX_LEN));
assert(KnownBotRegistry::canSuppressNormal(entries, 2, key1, BOT_MIN_AUTH_SENDER_KEY_PREFIX_LEN));
assert(!KnownBotRegistry::canSuppressNormal(entries, 2, key1, BOT_MIN_AUTH_SENDER_KEY_PREFIX_LEN - 1));
assert(!KnownBotRegistry::canSuppressNormal(entries, 2, key2, BOT_SENDER_KEY_PREFIX_LEN));
assert(KnownBotRegistry::add(entries, 2, key1, 0, "renamed-bot"));
entry = KnownBotRegistry::find(entries, 2, key1, BOT_SENDER_KEY_PREFIX_LEN);
assert(entry != NULL);
assert(strcmp(entry->label, "renamed-bot") == 0);
assert(!KnownBotRegistry::canSuppressNormal(entries, 2, key1, BOT_SENDER_KEY_PREFIX_LEN));
assert(KnownBotRegistry::add(entries, 2, key2, BOT_KNOWN_BOT_FLAG_SUPPRESS_NORMAL, "beta"));
assert(!KnownBotRegistry::add(entries, 2, key3, BOT_KNOWN_BOT_FLAG_SUPPRESS_NORMAL, "gamma"));
assert(KnownBotRegistry::remove(entries, 2, key1));
assert(!KnownBotRegistry::find(entries, 2, key1, BOT_SENDER_KEY_PREFIX_LEN));
assert(KnownBotRegistry::add(entries, 2, key3, BOT_KNOWN_BOT_FLAG_SUPPRESS_NORMAL, NULL));
assert(KnownBotRegistry::canSuppressNormal(entries, 2, key3, BOT_SENDER_KEY_PREFIX_LEN));
assert(KnownBotRegistry::remove(entries, 2, key3));
assert(!KnownBotRegistry::remove(entries, 2, key3));
}
static void test_known_bot_registry_ambiguous_short_prefix() {
BotKnownBotEntry entries[2];
uint8_t key1[BOT_SENDER_KEY_PREFIX_LEN] = { 1, 2, 3, 4, 5, 6 };
uint8_t key2[BOT_SENDER_KEY_PREFIX_LEN] = { 1, 2, 3, 4, 7, 8 };
KnownBotRegistry::clear(entries, 2);
assert(KnownBotRegistry::add(entries, 2, key1, BOT_KNOWN_BOT_FLAG_SUPPRESS_NORMAL, "alpha"));
assert(KnownBotRegistry::add(entries, 2, key2, BOT_KNOWN_BOT_FLAG_SUPPRESS_NORMAL, "beta"));
assert(!KnownBotRegistry::canSuppressNormal(entries, 2, key1, BOT_MIN_AUTH_SENDER_KEY_PREFIX_LEN));
assert(KnownBotRegistry::canSuppressNormal(entries, 2, key1, BOT_SENDER_KEY_PREFIX_LEN));
assert(KnownBotRegistry::canSuppressNormal(entries, 2, key2, BOT_SENDER_KEY_PREFIX_LEN));
}
static void test_fingerprint() {
BotMessage a = make_message("#bot", "!PING");
BotMessage b = make_message("bot", "!ping");
@@ -332,6 +381,283 @@ static void test_fingerprint() {
b.sender_key_prefix[0] = 99;
assert(fa.value != FirmwareBot::fingerprintFor(b).value);
b.sender_key_prefix[0] = a.sender_key_prefix[0];
strncpy(b.sender_name, "bob", sizeof(b.sender_name) - 1);
assert(fa.value != FirmwareBot::fingerprintFor(b).value);
}
static void test_response_fingerprint() {
BotMessage a = make_message("#bot", "!ping");
BotMessage b = make_message("bot", "!ping");
BotMessage c = make_message("#testing", "!ping");
BotFingerprint fa = FirmwareBot::responseFingerprintFor(a, " Pong! ", 7);
BotFingerprint fb = FirmwareBot::responseFingerprintFor(b, "pong!", 5);
BotFingerprint fc = FirmwareBot::responseFingerprintFor(c, "pong!", 5);
assert(fa.value == fb.value);
assert(fa.value != fc.value);
a.sender_key_prefix[0] = 99;
a.sender_timestamp++;
assert(fa.value == FirmwareBot::responseFingerprintFor(a, "pong!", 5).value);
assert(fa.value != FirmwareBot::responseFingerprintFor(a, "status", 6).value);
BotMessage dm = make_message("#bot", "!ping");
dm.channel_kind = BOT_CHANNEL_DM;
dm.channel_name[0] = 0;
dm.sender_key_prefix_len = BOT_SENDER_KEY_PREFIX_LEN;
BotMessage other_dm = dm;
other_dm.sender_key_prefix[0] ^= 0x55;
assert(FirmwareBot::responseFingerprintFor(dm, "pong!", 5).value !=
FirmwareBot::responseFingerprintFor(other_dm, "pong!", 5).value);
}
static void test_authoritative_suppression_flow() {
BotKnownBotEntry entries[1];
uint8_t known_key[BOT_SENDER_KEY_PREFIX_LEN] = { 2, 4, 6, 8, 10, 12 };
KnownBotRegistry::clear(entries, 1);
assert(KnownBotRegistry::add(entries, 1, known_key, BOT_KNOWN_BOT_FLAG_SUPPRESS_NORMAL, "known"));
BotCoordinatorPending pending[1];
ResponseCoordinator::clear(pending, 1);
BotMessage dm = make_message("#bot", "!ping");
dm.channel_kind = BOT_CHANNEL_DM;
dm.channel_name[0] = 0;
memcpy(dm.sender_key_prefix, known_key, sizeof(known_key));
dm.sender_key_prefix_len = BOT_SENDER_KEY_PREFIX_LEN;
BotFingerprint request = FirmwareBot::fingerprintFor(dm);
BotFingerprint response = FirmwareBot::responseFingerprintFor(dm, "Pong!", 5);
BotFingerprint scheduled;
uint32_t due = 0;
assert(ResponseCoordinator::schedule(pending, 1, dm, BOT_COMMAND_PING, request, response, 1000, 0,
0x01020304UL, 0, &scheduled, &due) == BOT_COORDINATOR_SCHEDULED);
assert(KnownBotRegistry::canSuppressNormal(entries, 1, dm.sender_key_prefix, dm.sender_key_prefix_len));
assert(ResponseCoordinator::suppress(pending, 1, FirmwareBot::responseFingerprintFor(dm, "Pong!", 5)));
BotCoordinatorReady ready = ResponseCoordinator::poll(pending, 1, 1000);
assert(ready.result == BOT_COORDINATOR_READY_SUPPRESSED);
assert(ready.request_fingerprint.value == request.value);
ResponseCoordinator::clear(pending, 1);
BotMessage group = make_message("#bot", "cm-bot: Pong!");
group.sender_key_prefix_len = 0;
assert(!KnownBotRegistry::canSuppressNormal(entries, 1, group.sender_key_prefix, group.sender_key_prefix_len));
}
static void test_response_coordinator_schedule_poll() {
BotCoordinatorPending pending[2];
ResponseCoordinator::clear(pending, 2);
BotMessage message = make_message("#bot", "!ping");
BotFingerprint request = FirmwareBot::fingerprintFor(message);
BotFingerprint response = FirmwareBot::responseFingerprintFor(message, "Pong!", 5);
BotFingerprint scheduled;
uint32_t due = 0;
uint32_t identity_seed = 0xA5A55A5AUL;
uint8_t queue_depth = 1;
uint32_t jitter_seed = 17;
assert(ResponseCoordinator::schedule(pending, 2, message, BOT_COMMAND_PING, request, response, 1000, jitter_seed,
identity_seed, queue_depth, &scheduled, &due) == BOT_COORDINATOR_SCHEDULED);
assert(scheduled.value == request.value);
assert(due == 1000 + ResponseCoordinator::responseDelayMillis(message, BOT_COMMAND_PING, request, identity_seed,
queue_depth, jitter_seed));
BotCoordinatorReady ready = ResponseCoordinator::poll(pending, 2, due - 1);
assert(ready.result == BOT_COORDINATOR_READY_NONE);
ready = ResponseCoordinator::poll(pending, 2, due);
assert(ready.result == BOT_COORDINATOR_READY_SEND);
assert(ready.request_fingerprint.value == request.value);
assert(ready.response_fingerprint.value == response.value);
assert(ResponseCoordinator::poll(pending, 2, due).result == BOT_COORDINATOR_READY_NONE);
assert(ResponseCoordinator::schedule(pending, 2, message, BOT_COMMAND_PING, request, response, 2000, 0,
identity_seed, 0, &scheduled, &due) == BOT_COORDINATOR_SCHEDULED);
assert(ResponseCoordinator::schedule(pending, 2, message, BOT_COMMAND_PING, request, response, 3000, 0,
identity_seed, 0, &scheduled, &due) == BOT_COORDINATOR_REPLACED);
ready = ResponseCoordinator::poll(pending, 2, due);
assert(ready.result == BOT_COORDINATOR_READY_SEND);
assert(ready.request_fingerprint.value == request.value);
assert(ready.response_fingerprint.value == response.value);
}
static void test_response_coordinator_distinct_requests_same_response() {
BotCoordinatorPending pending[2];
ResponseCoordinator::clear(pending, 2);
BotMessage first = make_message("#bot", "!ping");
BotMessage second = first;
second.sender_timestamp++;
BotFingerprint first_request = FirmwareBot::fingerprintFor(first);
BotFingerprint second_request = FirmwareBot::fingerprintFor(second);
BotFingerprint response = FirmwareBot::responseFingerprintFor(first, "Pong!", 5);
BotFingerprint scheduled;
uint32_t first_due = 0;
uint32_t second_due = 0;
uint32_t identity_seed = 0x01020304UL;
assert(first_request.value != second_request.value);
assert(response.value == FirmwareBot::responseFingerprintFor(second, "Pong!", 5).value);
assert(ResponseCoordinator::schedule(pending, 2, first, BOT_COMMAND_PING, first_request, response, 1000, 0,
identity_seed, 0, &scheduled, &first_due) == BOT_COORDINATOR_SCHEDULED);
assert(ResponseCoordinator::schedule(pending, 2, second, BOT_COMMAND_PING, second_request, response, 1000, 0,
identity_seed, 0, &scheduled, &second_due) == BOT_COORDINATOR_SCHEDULED);
assert(scheduled.value == second_request.value);
bool first_is_early = first_due <= second_due;
BotCoordinatorReady ready = ResponseCoordinator::poll(pending, 2, first_is_early ? first_due : second_due);
assert(ready.result == BOT_COORDINATOR_READY_SEND);
assert(ready.request_fingerprint.value == (first_is_early ? first_request.value : second_request.value));
assert(ready.response_fingerprint.value == response.value);
ready = ResponseCoordinator::poll(pending, 2, first_is_early ? second_due : first_due);
assert(ready.result == BOT_COORDINATOR_READY_SEND);
assert(ready.request_fingerprint.value == (first_is_early ? second_request.value : first_request.value));
assert(ready.response_fingerprint.value == response.value);
}
static void test_response_coordinator_suppression_uses_response_fingerprint() {
BotCoordinatorPending pending[2];
ResponseCoordinator::clear(pending, 2);
BotMessage first = make_message("#bot", "!ping");
BotMessage second = first;
second.sender_timestamp++;
BotFingerprint first_request = FirmwareBot::fingerprintFor(first);
BotFingerprint second_request = FirmwareBot::fingerprintFor(second);
BotFingerprint response = FirmwareBot::responseFingerprintFor(first, "Pong!", 5);
BotFingerprint scheduled;
uint32_t due = 0;
assert(ResponseCoordinator::schedule(pending, 2, first, BOT_COMMAND_PING, first_request, response, 1000, 0,
0x0A0B0C0DUL, 0, &scheduled, &due) == BOT_COORDINATOR_SCHEDULED);
assert(ResponseCoordinator::schedule(pending, 2, second, BOT_COMMAND_PING, second_request, response, 1000, 0,
0x0A0B0C0DUL, 0, &scheduled, &due) == BOT_COORDINATOR_SCHEDULED);
assert(ResponseCoordinator::suppress(pending, 2, response));
BotCoordinatorReady ready = ResponseCoordinator::poll(pending, 2, 1000);
assert(ready.result == BOT_COORDINATOR_READY_SUPPRESSED);
assert(ready.request_fingerprint.value == first_request.value);
assert(ready.response_fingerprint.value == response.value);
ready = ResponseCoordinator::poll(pending, 2, due);
assert(ready.result == BOT_COORDINATOR_READY_SEND);
assert(ready.request_fingerprint.value == second_request.value);
assert(ready.response_fingerprint.value == response.value);
}
static void test_response_coordinator_suppress_expire_full() {
BotCoordinatorPending pending[1];
ResponseCoordinator::clear(pending, 1);
BotMessage message = make_message("#bot", "!ping");
BotMessage second_message = make_message("#bot", "!test");
BotFingerprint first_request = FirmwareBot::fingerprintFor(message);
BotFingerprint second_request = FirmwareBot::fingerprintFor(second_message);
BotFingerprint first_response = FirmwareBot::responseFingerprintFor(message, "Pong!", 5);
BotFingerprint second_response = FirmwareBot::responseFingerprintFor(second_message, "Bot test OK", 11);
BotFingerprint scheduled;
uint32_t due = 0;
uint32_t identity_seed = 0x11223344UL;
assert(ResponseCoordinator::schedule(pending, 1, message, BOT_COMMAND_PING, first_request, first_response, 1000, 0,
identity_seed, 0, &scheduled, &due) == BOT_COORDINATOR_SCHEDULED);
assert(ResponseCoordinator::schedule(pending, 1, second_message, BOT_COMMAND_TEST, second_request, second_response, 1000,
0, identity_seed, 0, &scheduled, &due) == BOT_COORDINATOR_NO_SPACE);
assert(ResponseCoordinator::suppress(pending, 1, first_response));
BotCoordinatorReady ready = ResponseCoordinator::poll(pending, 1, 1000);
assert(ready.result == BOT_COORDINATOR_READY_SUPPRESSED);
assert(ready.request_fingerprint.value == first_request.value);
assert(ready.response_fingerprint.value == first_response.value);
assert(!ResponseCoordinator::suppress(pending, 1, first_response));
assert(ResponseCoordinator::schedule(pending, 1, message, BOT_COMMAND_PING, first_request, first_response, 1000, 0,
identity_seed, 0, &scheduled, &due) == BOT_COORDINATOR_SCHEDULED);
assert(ResponseCoordinator::schedule(pending, 1, second_message, BOT_COMMAND_TEST, second_request, second_response,
1000 + BOT_RESPONSE_PENDING_TTL_MILLIS, 0, identity_seed, 0, &scheduled,
&due) == BOT_COORDINATOR_NO_SPACE);
ready = ResponseCoordinator::poll(pending, 1, 1000 + BOT_RESPONSE_PENDING_TTL_MILLIS);
assert(ready.result == BOT_COORDINATOR_READY_EXPIRED);
assert(ready.request_fingerprint.value == first_request.value);
assert(ready.response_fingerprint.value == first_response.value);
assert(ResponseCoordinator::schedule(pending, 1, second_message, BOT_COMMAND_TEST, second_request, second_response,
1000 + BOT_RESPONSE_PENDING_TTL_MILLIS, 0, identity_seed, 0, &scheduled,
&due) == BOT_COORDINATOR_SCHEDULED);
ResponseCoordinator::clear(pending, 1);
assert(ResponseCoordinator::schedule(pending, 1, message, BOT_COMMAND_PING, first_request, first_response,
0xFFFFFF00UL, 0, identity_seed, 0, &scheduled, &due) == BOT_COORDINATOR_SCHEDULED);
ready = ResponseCoordinator::poll(pending, 1, 0xFFFFFF00UL);
assert(ready.result == BOT_COORDINATOR_READY_NONE);
ready = ResponseCoordinator::poll(pending, 1, due);
assert(ready.result == BOT_COORDINATOR_READY_SEND);
assert(ready.request_fingerprint.value == first_request.value);
assert(ready.response_fingerprint.value == first_response.value);
}
static void test_response_coordinator_cancel_by_request() {
BotCoordinatorPending pending[2];
ResponseCoordinator::clear(pending, 2);
BotMessage first = make_message("#bot", "!ping");
BotMessage second = first;
second.sender_timestamp++;
BotFingerprint first_request = FirmwareBot::fingerprintFor(first);
BotFingerprint second_request = FirmwareBot::fingerprintFor(second);
BotFingerprint response = FirmwareBot::responseFingerprintFor(first, "Pong!", 5);
BotFingerprint scheduled;
uint32_t first_due = 0;
uint32_t second_due = 0;
assert(ResponseCoordinator::schedule(pending, 2, first, BOT_COMMAND_PING, first_request, response, 1000, 0,
0x01020304UL, 0, &scheduled, &first_due) == BOT_COORDINATOR_SCHEDULED);
assert(ResponseCoordinator::schedule(pending, 2, second, BOT_COMMAND_PING, second_request, response, 1000, 0,
0x01020304UL, 0, &scheduled, &second_due) == BOT_COORDINATOR_SCHEDULED);
assert(ResponseCoordinator::cancel(pending, 2, first_request));
assert(!ResponseCoordinator::cancel(pending, 2, first_request));
BotCoordinatorReady ready = ResponseCoordinator::poll(pending, 2, first_due > second_due ? first_due : second_due);
assert(ready.result == BOT_COORDINATOR_READY_SEND);
assert(ready.request_fingerprint.value == second_request.value);
assert(ready.response_fingerprint.value == response.value);
}
static void test_response_coordinator_delay_biases() {
BotMessage message = make_message("#bot", "!ping");
BotFingerprint request = { 0x0123456789ABCDEFULL };
uint32_t first = ResponseCoordinator::responseDelayMillis(message, BOT_COMMAND_PING, request, 0x01020304UL, 0, 0);
uint32_t second = ResponseCoordinator::responseDelayMillis(message, BOT_COMMAND_PING, request, 0x05060708UL, 0, 0);
assert(first != second);
assert(ResponseCoordinator::responseDelayMillis(message, BOT_COMMAND_PING, request, 0x01020304UL, 2,
BOT_RESPONSE_DELAY_JITTER_MILLIS + 17) == first + 300 + 17);
}
static void test_response_coordinator_recent_responses() {
BotCoordinatorRecent recent[1];
ResponseCoordinator::clearRecent(recent, 1);
BotFingerprint response = { 0xFEDCBA9876543210ULL };
BotFingerprint zero = { 0 };
assert(!ResponseCoordinator::recentlySent(recent, 1, response, 1000));
ResponseCoordinator::recordRecent(recent, 1, zero, 1000);
assert(!ResponseCoordinator::recentlySent(recent, 1, response, 1000));
ResponseCoordinator::recordRecent(recent, 1, response, 1000);
assert(ResponseCoordinator::recentlySent(recent, 1, response, 1000));
assert(ResponseCoordinator::recentlySent(recent, 1, response, 1000 + BOT_RESPONSE_RECENT_TTL_MILLIS - 1));
assert(!ResponseCoordinator::recentlySent(recent, 1, response, 1000 + BOT_RESPONSE_RECENT_TTL_MILLIS));
}
static void test_response_coordinator_rejects_non_normal() {
BotCoordinatorPending pending[1];
ResponseCoordinator::clear(pending, 1);
BotMessage bot_message = make_message("#bot", "!ping");
BotMessage public_message = make_message("Public", "!ping");
BotMessage emergency_message = make_message("#emergency", "need help");
BotFingerprint request = FirmwareBot::fingerprintFor(bot_message);
BotFingerprint response = FirmwareBot::responseFingerprintFor(bot_message, "Pong!", 5);
BotFingerprint zero = { 0 };
BotFingerprint scheduled;
uint32_t due = 99;
assert(ResponseCoordinator::schedule(pending, 1, public_message, BOT_COMMAND_PING, request, response, 1000, 0,
0, 0, &scheduled, &due) == BOT_COORDINATOR_NOT_NORMAL);
assert(scheduled.value == 0);
assert(due == 0);
assert(ResponseCoordinator::schedule(pending, 1, emergency_message, BOT_COMMAND_PING, request, response, 1000, 0,
0, 0, &scheduled, &due) == BOT_COORDINATOR_NOT_NORMAL);
assert(ResponseCoordinator::schedule(pending, 1, bot_message, BOT_COMMAND_PING, zero, response, 1000, 0,
0, 0, &scheduled, &due) == BOT_COORDINATOR_NOT_NORMAL);
assert(ResponseCoordinator::schedule(pending, 1, bot_message, BOT_COMMAND_PING, request, zero, 1000, 0,
0, 0, &scheduled, &due) == BOT_COORDINATOR_NOT_NORMAL);
}
int main() {
@@ -349,7 +675,19 @@ int main() {
test_emergency_forward_loop_prevention();
test_emergency_forward_multipart();
test_emergency_forward_truncation();
test_known_bot_registry();
test_known_bot_registry_ambiguous_short_prefix();
test_fingerprint();
test_response_fingerprint();
test_authoritative_suppression_flow();
test_response_coordinator_schedule_poll();
test_response_coordinator_distinct_requests_same_response();
test_response_coordinator_suppression_uses_response_fingerprint();
test_response_coordinator_suppress_expire_full();
test_response_coordinator_cancel_by_request();
test_response_coordinator_delay_biases();
test_response_coordinator_recent_responses();
test_response_coordinator_rejects_non_normal();
printf("firmware_bot tests passed\n");
return 0;
}