amos 2f32624d3b Batch 18 (20261026-C-hwe-codegen-18): add 100 calibrated HWE code-gen tasks; pause production for rollout
100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth
range given to batch 22), each with at least one independent
contract-edge defect and plain public docs. Claude reviewed 100/100
USABLE against current digests; no ID or source overlap with main or
batch 22. Difficulty is unmeasured.

progress.md records the wrap-up: all Codex sessions and Qwen schedulers
stopped for the user's rollout, Qwen results so far (57 HARD of 373),
and what is left on each batch branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:52:13 +08:00

269 lines
8.2 KiB
C++

#include "mailbox.hpp"
#include <limits>
#include <utility>
namespace mq {
Mailbox::Mailbox(Mailbox &&other) noexcept { swap_state(other); }
Mailbox &Mailbox::operator=(Mailbox &&other) noexcept
{
if (this != &other) {
Mailbox replacement;
replacement.swap_state(other);
swap_state(replacement);
}
return *this;
}
void Mailbox::swap_state(Mailbox &other) noexcept
{
using std::swap;
swap(config_, other.config_);
swap(now_, other.now_);
swap(used_bytes_, other.used_bytes_);
swap(next_ticket_, other.next_ticket_);
swap(counters_, other.counters_);
swap(closed_, other.closed_);
buckets_.swap(other.buckets_);
}
void Mailbox::reset() { Mailbox empty; swap_state(empty); }
int Mailbox::validate(const Config &c)
{
return c.count < 1 || c.count > 64 || c.bytes < 1 || c.bytes > 4096 ||
c.body < 1 || c.body > 256 || c.body > c.bytes || c.priority > 31 ? -22 : 0;
}
void Mailbox::add(Number &counter, Number amount)
{
const Number maximum = std::numeric_limits<Number>::max();
counter = amount > maximum - counter ? maximum : counter + amount;
}
int Mailbox::configure(const Config &c)
{
if (validate(c)) return -22;
reset();
config_ = c;
return 0;
}
Number Mailbox::size() const
{
Number total = 0;
for (const auto &bucket : buckets_) total += bucket.second.size();
return total;
}
int Mailbox::resize(const Config &c)
{
if (validate(c)) return -22;
if (size() > c.count || used_bytes_ > c.bytes) return -28;
for (const auto &bucket : buckets_) {
if (bucket.first > c.priority) return -28;
for (const auto &r : bucket.second) if (r.body.size() > c.body) return -28;
}
config_ = c;
add(counters_.revision, 1);
return 0;
}
Record *Mailbox::find(Number id)
{
for (auto &bucket : buckets_)
for (auto &r : bucket.second) if (r.id == id) return &r;
return nullptr;
}
const Record *Mailbox::find(Number id) const
{
for (const auto &bucket : buckets_)
for (const auto &r : bucket.second) if (r.id == id) return &r;
return nullptr;
}
int Mailbox::send(const Input &input, Number &out)
{
if (closed_) return -32;
if (!input.id || input.priority > config_.priority) return -22;
if (input.body.size() > config_.body) return -90;
if (input.ttl > clock_limit || (input.ttl && input.ttl > clock_limit - now_)) return -34;
if (find(input.id)) return -17;
if (next_ticket_ == std::numeric_limits<Number>::max()) return -75;
if (size() == config_.count || input.body.size() > config_.bytes - used_bytes_) return -28;
buckets_[input.priority].push_back({input.id, input.priority, next_ticket_,
input.ttl ? now_ + input.ttl : 0, input.body});
out = next_ticket_++;
used_bytes_ += input.body.size();
add(counters_.sent, 1);
add(counters_.sent_bytes, input.body.size());
add(counters_.revision, 1);
return 0;
}
int Mailbox::empty_status() const { return closed_ ? -61 : -11; }
int Mailbox::peek(Record &out) const
{
if (buckets_.empty()) return empty_status();
out = buckets_.begin()->second.front();
return 0;
}
void Mailbox::remove(Number id)
{
for (auto bucket = buckets_.begin(); bucket != buckets_.end(); ++bucket) {
for (auto r = bucket->second.begin(); r != bucket->second.end(); ++r) {
if (r->id == id) {
used_bytes_ -= r->body.size();
bucket->second.erase(r);
if (bucket->second.empty()) buckets_.erase(bucket);
return;
}
}
}
}
int Mailbox::receive(Record &out)
{
if (buckets_.empty()) return empty_status();
Record selected = buckets_.begin()->second.front();
remove(selected.id);
add(counters_.received, 1);
add(counters_.received_bytes, selected.body.size());
add(counters_.revision, 1);
out = std::move(selected);
return 0;
}
int Mailbox::cancel(Number id)
{
if (!id) return -22;
if (!find(id)) return -2;
remove(id);
add(counters_.canceled, 1);
add(counters_.revision, 1);
return 0;
}
int Mailbox::reprioritize(Number id, Number priority)
{
if (!id || priority > config_.priority) return -22;
const Record *existing = find(id);
if (!existing) return -2;
Record changed = *existing;
remove(id);
changed.priority = priority;
auto &bucket = buckets_[priority];
auto at = bucket.begin();
while (at != bucket.end() && at->ticket < changed.ticket) ++at;
bucket.insert(at, changed);
used_bytes_ += changed.body.size();
add(counters_.reprioritized, 1);
add(counters_.revision, 1);
return 0;
}
int Mailbox::advance(Number delta)
{
if (delta > clock_limit || delta > clock_limit - now_) return -34;
now_ += delta;
for (auto bucket = buckets_.begin(); bucket != buckets_.end();) {
auto &messages = bucket->second;
for (auto r = messages.begin(); r != messages.end();) {
if (r->expires && r->expires <= now_) {
used_bytes_ -= r->body.size();
r = messages.erase(r);
add(counters_.expired, 1);
} else ++r;
}
if (messages.empty()) bucket = buckets_.erase(bucket);
else ++bucket;
}
add(counters_.revision, 1);
return 0;
}
int Mailbox::inspect_id(Number id, Record &out) const
{
if (!id) return -22;
const auto *r = find(id);
if (!r) return -2;
out = *r;
return 0;
}
int Mailbox::renew(Number id, Number ttl, Number &old)
{
if (!id) return -22;
auto *r = find(id);
if (!r) return -2;
if (ttl > clock_limit || (ttl && ttl > clock_limit - now_)) return -34;
old = r->expires;
r->expires = ttl ? now_ + ttl : 0;
add(counters_.revision, 1);
return 0;
}
int Mailbox::cancel_priority(Number priority, std::vector<Number> &out)
{
if (priority > config_.priority) return -22;
std::vector<Number> ids;
auto bucket = buckets_.find(priority);
if (bucket != buckets_.end()) {
for (const auto &r : bucket->second) {
ids.push_back(r.id);
used_bytes_ -= r.body.size();
}
buckets_.erase(bucket);
}
add(counters_.canceled, ids.size());
add(counters_.revision, 1);
out = std::move(ids);
return 0;
}
void Mailbox::set_closed(bool value)
{
if (closed_ == value) return;
closed_ = value;
add(counters_.revision, 1);
}
bool Mailbox::closed() const { return closed_; }
void Mailbox::snapshot(std::vector<Record> &out) const
{
std::vector<Record> result;
for (const auto &bucket : buckets_)
for (const auto &r : bucket.second) result.push_back(r);
out = std::move(result);
}
int Mailbox::drain(Number limit, Number budget, std::vector<Record> &out)
{
if (limit < 1 || limit > 64) return -22;
if (budget > 4096) return -34;
if (buckets_.empty()) return empty_status();
if (buckets_.begin()->second.front().body.size() > budget) return -90;
std::vector<Record> result;
Number bytes = 0;
while (!buckets_.empty() && result.size() < limit) {
const Record next = buckets_.begin()->second.front();
if (next.body.size() > budget - bytes) break;
bytes += next.body.size();
result.push_back(next);
remove(next.id);
}
add(counters_.received, result.size());
add(counters_.received_bytes, bytes);
add(counters_.revision, 1);
out = std::move(result);
return 0;
}
int Mailbox::apply(const Edit &edit, Number &ticket)
{
if (edit.kind == 1) return send(edit.input, ticket);
int result = -22;
if (edit.kind == 2) result = cancel(edit.input.id);
if (edit.kind == 3) result = reprioritize(edit.input.id, edit.input.priority);
if (!result) ticket = 0;
return result;
}
int Mailbox::batch(const std::vector<Edit> &edits, std::vector<Number> &out)
{
if (edits.size() > 16) return -22;
Mailbox working(*this);
std::vector<Number> tickets;
for (const Edit &edit : edits) {
Number ticket = 0;
const int result = working.apply(edit, ticket);
if (result) return result;
tickets.push_back(ticket);
}
swap_state(working);
out = std::move(tickets);
return 0;
}
View Mailbox::inspect() const
{
return {config_, now_, size(), used_bytes_, next_ticket_, counters_, closed_};
}
}