Fix M9.4-M9.7 review findings in the dev session stack

- TLS: replace send() with a bounded queue/flush; an interrupted record is
  retried with identical arguments and later frames wait behind it, so no
  queued frame is lost or reordered; close() flushes first.
- Carriers: server-issued 'I','N' | 48-bit epoch | 64-bit ordinal ids
  replace the aliasing low-32-bit packing; client sequences are kept
  separately for replies; out-of-range epochs are refused, never truncated.
- Grants: a durable, fsynced, fail-closed consumed-grant ledger
  (--session-grant-ledger, /objects/dev-session-grants.v1 in dev) keeps
  single-use grants consumed across process and pod restarts.
- Liveness: protocol v1 (undeployed) gains Heartbeat and the timeout close
  reason; both sides heartbeat when idle and treat 5 s of silence as dead.
- Client sends are bounded and unresolved tracking starts at send time.
- ci-client configures with an explicit C compiler.

Each finding has a regression test; ADR-0083, the schema, operations doc
and VALIDATION record the corrections.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-02 05:44:44 -04:00
co-authored by Claude Opus 5.5
parent 8e319322f4
commit c588be237c
26 changed files with 934 additions and 193 deletions
+98 -11
View File
@@ -2,7 +2,9 @@
#include "evo/network/tls_channel.hpp"
#include "network_pki.hpp"
#include "network_test.hpp"
#include <algorithm>
#include <array>
#include <cstdint>
#include <deque>
#include <mbedtls/x509_crt.h>
#include <psa/crypto.h>
@@ -17,6 +19,8 @@ using namespace evo::network;
struct Wire {
std::deque<std::uint8_t> to_server, to_client;
bool fail{};
// Bytes each side may still write; 0 makes writes "would block" (zero progress).
std::size_t server_budget{SIZE_MAX}, client_budget{SIZE_MAX};
};
class Endpoint final : public ByteTransport {
public:
@@ -25,7 +29,11 @@ class Endpoint final : public ByteTransport {
if (wire_.fail)
return std::unexpected(TransportError::failed);
auto& out = server_ ? wire_.to_client : wire_.to_server;
const auto count = std::min<std::size_t>(bytes.size(), 700); // Force partial writes.
auto& budget = server_ ? wire_.server_budget : wire_.client_budget;
// Force partial writes, and stop entirely when the injected budget runs out.
const auto count = std::min({bytes.size(), std::size_t{700}, budget});
if (budget != SIZE_MAX)
budget -= count;
out.insert(out.end(), bytes.begin(), bytes.begin() + static_cast<std::ptrdiff_t>(count));
return count;
}
@@ -76,15 +84,15 @@ struct Pair {
}
std::vector<std::uint8_t> transfer(TlsChannel& from, TlsChannel& to,
std::span<const std::uint8_t> bytes) {
std::size_t sent = 0;
CHECK(from.queue(bytes));
return drain(from, to, bytes.size());
}
// Flushes `from` and reads `to` until `expected` plaintext bytes arrive.
std::vector<std::uint8_t> drain(TlsChannel& from, TlsChannel& to, std::size_t expected) {
std::vector<std::uint8_t> received;
std::array<std::uint8_t, 4096> buffer{};
for (int i = 0; i < 1000 && received.size() < bytes.size(); ++i) {
if (sent < bytes.size()) {
const auto n = from.send(bytes.subspan(sent));
CHECK(n);
sent += *n;
}
for (int i = 0; i < 1000 && received.size() < expected; ++i) {
CHECK(from.flush());
const auto n = to.receive(buffer);
CHECK(n);
received.insert(received.end(), buffer.begin(),
@@ -122,7 +130,7 @@ void handshake_and_frames() {
for (int i = 0; i < 10 && r && *r == 0; ++i)
r = pair.server->receive(buffer);
CHECK(refused(r, TlsError::closed));
CHECK(refused(pair.server->send(buffer), TlsError::closed)); // Latched.
CHECK(refused(pair.server->queue(buffer), TlsError::closed)); // Latched.
}
void rejections() {
@@ -155,8 +163,8 @@ void rejections() {
Pair pair{pki.server, {pki.authority, server_name}};
CHECK(pair.handshake());
const std::array<std::uint8_t, 64> data{};
const auto sent = pair.client->send(data);
CHECK(sent && *sent == data.size());
CHECK(pair.client->queue(data) && pair.client->flush());
CHECK(pair.client->queued() == 0);
CHECK(pair.wire.to_server.size() > 20);
pair.wire.to_server[pair.wire.to_server.size() - 5] ^= 0x01;
std::array<std::uint8_t, 256> buffer{};
@@ -181,10 +189,89 @@ void rejections() {
CHECK(!r && r.error() == TlsError::handshake);
}
}
// Review M9.4-M9.7 #1: frames queued while a TLS record is stalled (zero progress,
// or partial progress then zero) must neither be lost nor reordered.
std::vector<std::uint8_t> intents(std::uint64_t first, std::uint64_t count) {
std::vector<std::uint8_t> bytes;
for (std::uint64_t i = 0; i < count; ++i) {
const auto f = encode(IntentMessage{1, 1, first + i, 10, 0, 0});
bytes.insert(bytes.end(), f.begin(), f.end());
}
return bytes;
}
std::vector<std::uint64_t> sequences(const std::vector<std::uint8_t>& bytes) {
FrameReader reader;
std::vector<std::uint64_t> out;
std::span<const std::uint8_t> rest{bytes};
while (!rest.empty()) {
const auto taken = reader.feed(rest);
CHECK(taken);
rest = rest.subspan(*taken);
while (auto f = reader.take()) {
const auto m = decode_intent({f->type, f->payload});
CHECK(m);
out.push_back(m ? m->sequence : 0);
}
}
return out;
}
void interrupted_writes() {
Pki pki;
for (const bool from_client : {true, false})
for (const std::size_t before_stall : {std::size_t{0}, std::size_t{10}}) {
Pair pair{pki.server, {pki.authority, server_name}};
CHECK(pair.handshake());
auto& from = from_client ? *pair.client : *pair.server;
auto& to = from_client ? *pair.server : *pair.client;
auto& budget = from_client ? pair.wire.client_budget : pair.wire.server_budget;
budget = before_stall; // Zero progress, or partial progress then zero.
const auto first = intents(1, 1);
CHECK(from.queue(first));
const auto stalled = from.flush();
CHECK(stalled && *stalled == TlsProgress::want_io);
const auto second = intents(2, 3); // Queued while the first record is stuck.
CHECK(from.queue(second) && from.queued() == first.size() + second.size());
budget = SIZE_MAX;
const auto received = pair.drain(from, to, first.size() + second.size());
CHECK(received.size() == first.size() + second.size() && from.queued() == 0);
CHECK(sequences(received) == std::vector<std::uint64_t>({1, 2, 3, 4}));
}
// Close while stalled: queued frames are flushed before close_notify.
Pair pair{pki.server, {pki.authority, server_name}};
CHECK(pair.handshake());
pair.wire.client_budget = 0;
const auto last = intents(9, 2);
CHECK(pair.client->queue(last));
const auto pending = pair.client->close();
CHECK(pending && *pending == TlsProgress::want_io);
pair.wire.client_budget = SIZE_MAX;
std::expected<TlsProgress, TlsError> closed = TlsProgress::want_io;
for (int i = 0; i < 50 && closed && *closed == TlsProgress::want_io; ++i)
closed = pair.client->close();
CHECK(closed && *closed == TlsProgress::done);
std::vector<std::uint8_t> received;
std::array<std::uint8_t, 256> buffer{};
std::expected<std::size_t, TlsError> r = 0;
for (int i = 0; i < 50 && r; ++i) {
r = pair.server->receive(buffer);
if (r)
received.insert(received.end(), buffer.begin(),
buffer.begin() + static_cast<std::ptrdiff_t>(*r));
}
CHECK(refused(r, TlsError::closed) &&
sequences(received) == std::vector<std::uint64_t>({9, 10}));
// A full queue refuses without queuing or latching.
Pair full{pki.server, {pki.authority, server_name}};
CHECK(full.handshake());
const std::vector<std::uint8_t> huge(TlsChannel::max_queued_bytes + 1, 0);
CHECK(refused(full.client->queue(huge), TlsError::backpressure) && full.client->queued() == 0);
CHECK(full.client->queue(intents(1, 1)) && full.client->queued() == intents(1, 1).size());
}
} // namespace
void tls_tests() {
handshake_and_frames();
rejections();
interrupted_writes();
}
} // namespace evo::network_test