Add M10.4a intentions, actions and admission
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m45s
Foundation / checks (network) (push) Successful in 9m37s
Foundation / checks (client) (push) Successful in 12m14s
Foundation / checks (tsan) (push) Successful in 7m39s
Foundation / checks (checks) (push) Successful in 31m15s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 32m59s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m45s
Foundation / checks (network) (push) Successful in 9m37s
Foundation / checks (client) (push) Successful in 12m14s
Foundation / checks (tsan) (push) Successful in 7m39s
Foundation / checks (checks) (push) Successful in 31m15s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 32m59s
Session protocol v1 (not yet deployed, amended in place) gains realtime intentions on datagrams with receipts and a cumulative simulated watermark, and account-scoped actions with stable ids, receipts, final outcomes and queries. IntentAdmission enforces session binding, rate, authority, epoch, vector, sequence, tick window, duplicates and the unresolved bound; RegionSessions and the client carry it over QUIC. Intentions and actions have no biological effect yet. ADR-0092. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+140
-3
@@ -1,5 +1,6 @@
|
||||
#include "evo/identity/identity.hpp"
|
||||
#include "evo/network/dev_grant.hpp"
|
||||
#include "evo/network/intent_admission.hpp"
|
||||
#include "evo/network/session_control.hpp"
|
||||
#include "evo/network/session_grant.hpp"
|
||||
#include "evo/network/session_protocol.hpp"
|
||||
@@ -326,7 +327,7 @@ void session_codec() {
|
||||
WireGrant grant{};
|
||||
for (std::size_t i = 0; i < grant.size(); ++i)
|
||||
grant.at(i) = static_cast<std::uint8_t>(i * 7);
|
||||
SessionWelcome welcome{1, 2, 3, 4, 5, 6, 7};
|
||||
SessionWelcome welcome{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
const std::vector<SessionMessage> messages{
|
||||
SessionHello{0, grant},
|
||||
welcome,
|
||||
@@ -334,7 +335,14 @@ void session_codec() {
|
||||
SessionRefreshResult{0, 99},
|
||||
SessionRefreshResult{static_cast<std::uint8_t>(SessionCloseReason::stale_grant), 0},
|
||||
SessionClose{SessionCloseReason::superseded},
|
||||
SessionClose{SessionCloseReason::shutdown}};
|
||||
SessionClose{SessionCloseReason::shutdown},
|
||||
SessionIntention{1, 2, 3, 4, -1000, 7},
|
||||
IntentionReceipt{3, AdmitStatus::not_ready, 9},
|
||||
SimulatedThrough{2, 3, 4},
|
||||
SessionAction{1, 2, 77, 1, 5},
|
||||
ActionReceipt{77, AdmitStatus::duplicate, 4, 5},
|
||||
ActionOutcome{77, ActionResult::committed_no_effect, 5, 12},
|
||||
ActionQuery{77}};
|
||||
for (const auto& m : messages) {
|
||||
const auto bytes = encode_session(m);
|
||||
CHECK(bytes.at(0) == 'E' && bytes.at(1) == 'V' && bytes.at(2) == 'S' && bytes.at(3) == 'N');
|
||||
@@ -359,7 +367,7 @@ void session_codec() {
|
||||
mutated = bytes;
|
||||
mutated.at(7) = 1;
|
||||
CHECK(refused(decode_session(mutated), SessionCodecError::reserved_flags));
|
||||
for (const auto type : std::array<std::uint8_t, 3>{0, 6, 255}) {
|
||||
for (const auto type : std::array<std::uint8_t, 3>{0, 13, 255}) {
|
||||
mutated = bytes;
|
||||
mutated.at(6) = type;
|
||||
CHECK(refused(decode_session(mutated), SessionCodecError::unknown_type));
|
||||
@@ -378,12 +386,140 @@ void session_codec() {
|
||||
CHECK(refused(decode_session(result), SessionCodecError::invalid_value));
|
||||
}
|
||||
}
|
||||
// Decoded values survive exactly (signed vectors, every field).
|
||||
const SessionIntention intent{9, 8, 7, 6, -1000, 999};
|
||||
CHECK(std::get<SessionIntention>(*decode_session(encode_session(intent))) == intent);
|
||||
const ActionOutcome outcome{5, ActionResult::not_committed, 0, 0};
|
||||
CHECK(std::get<ActionOutcome>(*decode_session(encode_session(outcome))) == outcome);
|
||||
// A state claim appended to an intention (position, energy) is not a valid message.
|
||||
auto claimed = encode_session(intent);
|
||||
for (int i = 0; i < 16; ++i)
|
||||
claimed.push_back(0x7F);
|
||||
CHECK(refused(decode_session(claimed), SessionCodecError::wrong_length));
|
||||
for (const auto bad : std::array<std::uint8_t, 3>{11, 200, 255}) {
|
||||
auto receipt = encode_session(IntentionReceipt{1, AdmitStatus::admitted, 1});
|
||||
receipt.at(session_header_bytes + 8) = bad;
|
||||
CHECK(refused(decode_session(receipt), SessionCodecError::invalid_value));
|
||||
}
|
||||
for (const auto bad : std::array<std::uint8_t, 3>{0, 5, 255}) {
|
||||
auto o = encode_session(outcome);
|
||||
o.at(session_header_bytes + 8) = bad;
|
||||
CHECK(refused(decode_session(o), SessionCodecError::invalid_value));
|
||||
}
|
||||
for (const auto zero : {0, 1}) {
|
||||
auto w = welcome;
|
||||
(zero == 0 ? w.session : w.epoch) = 0;
|
||||
CHECK(refused(decode_session(encode_session(w)), SessionCodecError::invalid_value));
|
||||
}
|
||||
}
|
||||
void intent_admission() {
|
||||
IntentLimits limits;
|
||||
limits.max_unresolved = 6;
|
||||
limits.max_finals = 4;
|
||||
CHECK(refused(IntentAdmission::create(IntentLimits{0}), IntentConfigError::invalid));
|
||||
auto a = *IntentAdmission::create(limits);
|
||||
const auto alice = account(1), bob = account(2);
|
||||
CHECK(a.open(1, alice) && a.open(2, bob) && !a.open(1, alice) && !a.open(0, alice));
|
||||
AdmissionFacts f{true, 5, 100};
|
||||
const auto intention = [&](std::uint64_t session, std::uint64_t seq, std::uint64_t tick,
|
||||
std::int16_t x = 0, std::uint64_t epoch = 5) {
|
||||
return a.intention(session, SessionIntention{session, epoch, seq, tick, x, 0}, f).status;
|
||||
};
|
||||
// Every refusal, in the order admission applies them.
|
||||
CHECK(intention(1, 1, 101) == AdmitStatus::admitted);
|
||||
CHECK(a.intention(1, SessionIntention{2, 5, 2, 102, 0, 0}, f).status ==
|
||||
AdmitStatus::unknown_session); // Names another session.
|
||||
CHECK(a.intention(9, SessionIntention{9, 5, 2, 102, 0, 0}, f).status ==
|
||||
AdmitStatus::unknown_session); // No such session.
|
||||
CHECK(intention(1, 2, 102, 0, 4) == AdmitStatus::stale_epoch);
|
||||
CHECK(intention(1, 2, 102, 1001) == AdmitStatus::invalid_vector);
|
||||
CHECK(intention(1, 1, 102) == AdmitStatus::replayed);
|
||||
CHECK(intention(1, 2, 100) == AdmitStatus::outside_window); // Not in the future.
|
||||
CHECK(intention(1, 2, 109) == AdmitStatus::outside_window); // Beyond 8 ticks.
|
||||
CHECK(intention(1, 2, 101) == AdmitStatus::outside_window); // Same tick as before.
|
||||
CHECK(intention(1, 2, 102) == AdmitStatus::rate_limited); // 8 attempts this window.
|
||||
f.server_tick = 108; // New window.
|
||||
f.ready = false;
|
||||
CHECK(intention(1, 2, 109) == AdmitStatus::not_ready);
|
||||
f.ready = true;
|
||||
CHECK(intention(1, 2, 109) == AdmitStatus::admitted);
|
||||
CHECK(intention(1, 3, 110) == AdmitStatus::admitted);
|
||||
// Simulated watermark: only intentions whose tick ran, cumulatively.
|
||||
f.server_tick = 109;
|
||||
auto marks = a.simulated(f);
|
||||
CHECK(marks.size() == 1 && marks[0].first == 1 &&
|
||||
marks[0].second == SimulatedThrough{5, 2, 109});
|
||||
CHECK(a.simulated(f).empty()); // Unchanged: not repeated yet.
|
||||
f.server_tick = 110;
|
||||
marks = a.simulated(f);
|
||||
CHECK(marks.size() == 1 && marks[0].second.sequence == 3);
|
||||
f.server_tick = 130;
|
||||
marks = a.simulated(f); // Repeated after 20 ticks so a lost datagram is covered.
|
||||
CHECK(marks.size() == 1 && marks[0].second == SimulatedThrough{5, 3, 130});
|
||||
|
||||
// Actions: receipt, carried with its tick's batch, final after the commit.
|
||||
const auto action = [&](std::uint64_t session, std::uint64_t id, std::uint64_t tick,
|
||||
std::uint16_t kind = 1) {
|
||||
return a.action(session, SessionAction{session, 5, id, kind, tick}, f);
|
||||
};
|
||||
f.server_tick = 200;
|
||||
auto r = action(1, 10, 203);
|
||||
CHECK(r.status == AdmitStatus::admitted && r.tick == 203 && r.server_tick == 200);
|
||||
CHECK(a.query(1, {10}).result == ActionResult::pending);
|
||||
CHECK(action(1, 0, 203).status == AdmitStatus::invalid_action);
|
||||
CHECK(action(1, 11, 203, 9).status == AdmitStatus::invalid_action);
|
||||
{ // Wire kind 257 must not truncate to a known kind.
|
||||
auto other = *IntentAdmission::create(IntentLimits{});
|
||||
CHECK(other.open(1, alice));
|
||||
CHECK(other.action(1, SessionAction{1, 5, 1, 257, 101}, AdmissionFacts{true, 5, 100})
|
||||
.status == AdmitStatus::invalid_action);
|
||||
}
|
||||
r = action(1, 10, 204); // Retry with another tick: the original stands.
|
||||
CHECK(r.status == AdmitStatus::duplicate && r.tick == 203);
|
||||
CHECK(action(1, 12, 203).status == AdmitStatus::rate_limited); // 4 action attempts.
|
||||
CHECK(a.carry(202).empty());
|
||||
const auto carried = a.carry(203);
|
||||
CHECK(carried.size() == 1 && carried[0] == CarriedAction{alice, 10, 203, ActionKind::probe});
|
||||
CHECK(a.carry(203).empty()); // Each action is carried once.
|
||||
CHECK(a.committed(202, 7).empty());
|
||||
auto outcomes = a.committed(203, 8);
|
||||
CHECK(outcomes.size() == 1 && outcomes[0].account == alice &&
|
||||
outcomes[0].outcome == ActionOutcome{10, ActionResult::committed_no_effect, 203, 8});
|
||||
CHECK(a.query(1, {10}) == outcomes[0].outcome);
|
||||
CHECK(a.query(2, {10}).result == ActionResult::unknown); // Bob's account has no action 10.
|
||||
// A reconnect of the same account (new session) sees the same outcome.
|
||||
a.close(1);
|
||||
CHECK(a.query(1, {10}).result == ActionResult::unknown); // Closed session asks nothing.
|
||||
CHECK(a.open(3, alice) && a.session_of(alice) == 3u);
|
||||
CHECK(a.query(3, {10}) == outcomes[0].outcome);
|
||||
f.server_tick = 300;
|
||||
CHECK(action(3, 10, 301).status == AdmitStatus::duplicate); // Never admitted twice.
|
||||
// Unresolved cap counts intentions and actions; abandon finalizes only uncarried actions.
|
||||
CHECK(action(3, 20, 301).status == AdmitStatus::admitted);
|
||||
CHECK(action(3, 21, 308).status == AdmitStatus::admitted);
|
||||
(void)a.carry(301);
|
||||
for (std::uint64_t i = 0; i < 4; ++i)
|
||||
CHECK(a.intention(3, SessionIntention{3, 5, i + 1, 302 + i, 0, 0}, f).status ==
|
||||
AdmitStatus::admitted);
|
||||
CHECK(a.unresolved(3) == 6);
|
||||
CHECK(a.intention(3, SessionIntention{3, 5, 9, 307, 0, 0}, f).status ==
|
||||
AdmitStatus::too_many_unresolved);
|
||||
outcomes = a.abandon();
|
||||
CHECK(outcomes.size() == 1 &&
|
||||
outcomes[0].outcome == ActionOutcome{21, ActionResult::not_committed, 0, 0});
|
||||
CHECK(a.query(3, {20}).result == ActionResult::pending); // Carried: awaits its record.
|
||||
CHECK(a.unresolved(3) == 1);
|
||||
CHECK(a.committed(301, 9).size() == 1); // Its batch turned out durable.
|
||||
// Final outcomes are kept within a bound; older ones become unknown here.
|
||||
for (std::uint64_t id = 30; id < 34; ++id) {
|
||||
f.server_tick += 8;
|
||||
CHECK(action(3, id, f.server_tick + 1).status == AdmitStatus::admitted);
|
||||
(void)a.carry(f.server_tick + 1);
|
||||
CHECK(a.committed(f.server_tick + 1, id).size() == 1);
|
||||
}
|
||||
CHECK(a.query(3, {10}).result == ActionResult::unknown);
|
||||
CHECK(a.query(3, {33}).result == ActionResult::committed_no_effect);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void control_tests() {
|
||||
@@ -391,5 +527,6 @@ void control_tests() {
|
||||
identity_adapter();
|
||||
control_rules();
|
||||
session_codec();
|
||||
intent_admission();
|
||||
}
|
||||
} // namespace evo::network_test
|
||||
|
||||
+101
-2
@@ -150,7 +150,7 @@ struct Harness {
|
||||
return std::move(*made);
|
||||
}
|
||||
void pump() {
|
||||
server->poll(epoch, now_seconds, Clock::now());
|
||||
server->poll({ready, epoch, tick}, now_seconds, Clock::now());
|
||||
for (auto& e : server->take_events())
|
||||
events.push_back(std::move(e));
|
||||
for (auto* c : clients)
|
||||
@@ -189,7 +189,8 @@ struct Harness {
|
||||
std::unique_ptr<RegionSessions> server;
|
||||
std::vector<RegionSessionClient*> clients;
|
||||
std::vector<RegionSessionEvent> events;
|
||||
std::uint64_t epoch{5}, now_seconds{start};
|
||||
std::uint64_t epoch{5}, now_seconds{start}, tick{100};
|
||||
bool ready{true};
|
||||
};
|
||||
|
||||
bool refused_with(Harness& h, const WireGrant& grant, Reason reason) {
|
||||
@@ -370,6 +371,103 @@ struct FakeLedger final : ControlLedger {
|
||||
std::vector<std::uint64_t> renewed, released;
|
||||
};
|
||||
|
||||
// M10.4: intentions, actions and their distinct answers against a simulated host.
|
||||
void intentions_and_actions(const Files& f) {
|
||||
Harness h{f};
|
||||
const auto alice = account(1);
|
||||
auto a = h.connect(h.issuer.grant(alice, 1));
|
||||
CHECK(h.settled(*a) && must(a->welcome()).server_tick == 100 && a->server_tick() == 100);
|
||||
// Intention: a receipt, then the simulated watermark once its tick runs.
|
||||
CHECK(a->send_intention(101, 10, -10) == 1u);
|
||||
std::vector<IntentionReceipt> receipts;
|
||||
CHECK(h.until([&] { return !(receipts = a->take_intention_receipts()).empty(); }));
|
||||
CHECK(receipts[0] == IntentionReceipt{1, AdmitStatus::admitted, 100});
|
||||
CHECK(!a->simulated());
|
||||
CHECK(a->send_intention(150, 0, 0) == 2u);
|
||||
CHECK(h.until([&] { return !(receipts = a->take_intention_receipts()).empty(); }));
|
||||
CHECK(receipts[0].status == AdmitStatus::outside_window);
|
||||
h.tick = 101;
|
||||
CHECK(h.until([&] { return a->simulated() && a->simulated()->sequence == 1; }));
|
||||
CHECK(must(a->simulated()) == SimulatedThrough{5, 1, 101} && a->server_tick() == 101);
|
||||
h.ready = false;
|
||||
CHECK(a->send_intention(103, 0, 0) == 3u);
|
||||
CHECK(h.until([&] { return !(receipts = a->take_intention_receipts()).empty(); }));
|
||||
CHECK(receipts[0].status == AdmitStatus::not_ready);
|
||||
h.ready = true;
|
||||
// Action: receipt (scheduled), carried with its tick's batch, final after the commit.
|
||||
CHECK(a->send_action(7, ActionKind::probe, 103));
|
||||
std::vector<ActionReceipt> action_receipts;
|
||||
CHECK(h.until([&] { return !(action_receipts = a->take_action_receipts()).empty(); }));
|
||||
CHECK(action_receipts[0] == ActionReceipt{7, AdmitStatus::admitted, 101, 103});
|
||||
CHECK(a->query_action(7));
|
||||
std::vector<ActionOutcome> outcomes;
|
||||
CHECK(h.until([&] { return !(outcomes = a->take_action_outcomes()).empty(); }));
|
||||
CHECK(outcomes[0].result == ActionResult::pending);
|
||||
CHECK(h.server->carry(102).empty());
|
||||
const auto carried = h.server->carry(103);
|
||||
CHECK(carried.size() == 1 && carried[0] == CarriedAction{alice, 7, 103, ActionKind::probe});
|
||||
h.server->committed(103, 4);
|
||||
CHECK(h.until([&] { return !(outcomes = a->take_action_outcomes()).empty(); }));
|
||||
CHECK(outcomes[0] == ActionOutcome{7, ActionResult::committed_no_effect, 103, 4});
|
||||
// A retry gets a duplicate receipt and the same final outcome.
|
||||
CHECK(a->send_action(7, ActionKind::probe, 104));
|
||||
CHECK(h.until([&] { return !(outcomes = a->take_action_outcomes()).empty(); }));
|
||||
CHECK(outcomes[0] == ActionOutcome{7, ActionResult::committed_no_effect, 103, 4});
|
||||
// The stream is ordered: the duplicate receipt arrived before the repeated outcome.
|
||||
const auto duplicates = a->take_action_receipts();
|
||||
CHECK(duplicates.size() == 1 &&
|
||||
duplicates[0] == ActionReceipt{7, AdmitStatus::duplicate, 101, 103});
|
||||
// Reconnect (lost connection, new grant): the account's action resolves the same way.
|
||||
auto b = h.connect(h.issuer.grant(alice, 2));
|
||||
CHECK(h.settled(*b) && b->state() == RegionSessionState::established);
|
||||
CHECK(b->query_action(7));
|
||||
CHECK(h.until([&] { return !(outcomes = b->take_action_outcomes()).empty(); }));
|
||||
CHECK(outcomes[0] == ActionOutcome{7, ActionResult::committed_no_effect, 103, 4});
|
||||
// An action scheduled but never carried when the epoch moves on is final: not committed.
|
||||
CHECK(b->send_action(8, ActionKind::probe, 108));
|
||||
CHECK(h.until([&] { return !b->take_action_receipts().empty(); }));
|
||||
h.epoch = 6;
|
||||
CHECK(h.until([&] { return b->state() == RegionSessionState::closed; }));
|
||||
auto c = h.connect(h.issuer.grant(alice, 3));
|
||||
CHECK(h.settled(*c) && c->state() == RegionSessionState::established);
|
||||
CHECK(c->query_action(8));
|
||||
CHECK(h.until([&] { return !(outcomes = c->take_action_outcomes()).empty(); }));
|
||||
CHECK(outcomes[0].result == ActionResult::not_committed);
|
||||
// Intentions of an old epoch are refused.
|
||||
CHECK(c->send_intention(h.tick + 1, 0, 0));
|
||||
CHECK(h.until([&] { return !(receipts = c->take_intention_receipts()).empty(); }));
|
||||
CHECK(receipts[0].status == AdmitStatus::admitted); // c is in epoch 6.
|
||||
|
||||
// A peer that appends state claims (position, energy) to an intention is closed.
|
||||
auto peer = h.raw();
|
||||
quic::ConnectionId id = 0;
|
||||
std::optional<SessionWelcome> welcome;
|
||||
std::optional<Reason> reason;
|
||||
CHECK(h.until([&] {
|
||||
for (auto& e : peer->poll()) {
|
||||
if (e.kind == quic::EventKind::connected) {
|
||||
id = e.connection;
|
||||
CHECK(peer->send_message(
|
||||
id, encode_session(SessionHello{0, h.issuer.grant(account(4), 1)})));
|
||||
} else if (e.kind == quic::EventKind::message) {
|
||||
const auto m = decode_session(e.bytes);
|
||||
if (m && std::holds_alternative<SessionWelcome>(*m)) {
|
||||
welcome = std::get<SessionWelcome>(*m);
|
||||
auto claim = encode_session(
|
||||
SessionIntention{welcome->session, welcome->epoch, 1, h.tick + 1, 0, 0});
|
||||
for (int i = 0; i < 16; ++i)
|
||||
claim.push_back(0x7F); // "position" and "energy"
|
||||
CHECK(peer->send_datagram(id, claim));
|
||||
} else if (m && std::holds_alternative<SessionClose>(*m)) {
|
||||
reason = std::get<SessionClose>(*m).reason;
|
||||
}
|
||||
}
|
||||
}
|
||||
return reason.has_value();
|
||||
}));
|
||||
CHECK(welcome && reason == Reason::protocol);
|
||||
}
|
||||
|
||||
void durable_control(const Files& f) {
|
||||
FakeLedger ledger;
|
||||
Harness h{f, 4, 64, &ledger};
|
||||
@@ -444,6 +542,7 @@ int main() try {
|
||||
login_rules(files);
|
||||
limits(files);
|
||||
durable_control(files);
|
||||
intentions_and_actions(files);
|
||||
std::filesystem::remove_all(files.dir);
|
||||
std::puts("quic session tests passed");
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user