amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
One user-space repair task per eligible HWE debug-diag / compile-deploy base
ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content
filter. Each ships a public spec with a check-to-basis table, at least two
defects with single-fix fixtures, a symptom-masking fixture and an alternative
correct implementation. Claude re-checked all 320: structure, the HWE quality
items and trace6 Docker calibration receipts against current digests
(320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120
ad-hoc family names; the next batch maps them to the canonical list.
The batch's 1,256-line progress log stays in its factory report and the
36-local branch batch15-hwe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:55:56 +08:00

149 lines
4.1 KiB
C++

#include "progress.hpp"
#include <stdexcept>
namespace progress {
namespace {
void put16(std::vector<std::uint8_t>& target, std::uint32_t value) {
target.push_back(value & 255);
target.push_back((value >> 8) & 255);
}
void put32(std::vector<std::uint8_t>& target, std::uint32_t value) {
put16(target, value & 65535);
put16(target, value >> 16);
}
std::uint32_t get16(const std::vector<std::uint8_t>& data, std::size_t at) {
const std::uint32_t low = data.at(at);
const std::uint32_t high = data.at(at + 1);
return low | (high << 8);
}
std::uint32_t get32(const std::vector<std::uint8_t>& data, std::size_t at) {
const std::uint32_t low = get16(data, at);
const std::uint32_t high = get16(data, at + 2);
return low | (high << 16);
}
std::uint32_t checksum(const std::vector<std::uint8_t>& data, std::size_t count) {
std::uint32_t crc = 65535;
for (std::size_t index = 0; index < count; ++index) {
crc ^= std::uint32_t(data.at(index)) << 8;
for (unsigned bit = 0; bit < 8; ++bit) {
const bool carry = (crc & 32768) != 0;
crc = (crc << 1) & 65535;
if (carry) {
crc ^= 4129;
}
}
}
return crc;
}
void escaped_byte(std::vector<std::uint8_t>& frame, std::uint8_t byte) {
if (byte == 126 || byte == 125) {
frame.push_back(125);
frame.push_back(byte ^ 32);
} else {
frame.push_back(byte);
}
}
}
std::vector<std::uint8_t> encode_certificate(const Certificate& certificate) {
if (certificate.lane > 3 || certificate.job > 63 || certificate.unit > 31) {
throw std::out_of_range("certificate position");
}
if (certificate.word > 65535 || certificate.digest >= digest_mod) {
throw std::out_of_range("certificate value");
}
std::vector<std::uint8_t> payload;
payload.reserve(12);
payload.push_back(1);
payload.push_back(certificate.lane);
payload.push_back(certificate.job);
payload.push_back(certificate.unit);
put16(payload, certificate.word);
put32(payload, certificate.digest);
const std::uint32_t crc = checksum(payload, payload.size());
put16(payload, crc);
std::vector<std::uint8_t> frame;
frame.reserve(26);
frame.push_back(126);
for (std::uint8_t byte : payload) {
escaped_byte(frame, byte);
}
frame.push_back(126);
return frame;
}
void CertificateReceiver::append(std::uint8_t byte) {
if (payload_.size() >= 12) {
overflow_ = true;
return;
}
payload_.push_back(byte);
}
void CertificateReceiver::close_frame() {
if (escaped_ || overflow_ || payload_.size() != 12) {
++rejected_;
return;
}
if (payload_[0] != 1 || payload_[1] > 3 || payload_[2] > 63 || payload_[3] > 31) {
++rejected_;
return;
}
if (get16(payload_, 10) != checksum(payload_, 10)) {
++rejected_;
return;
}
const std::uint32_t digest = get32(payload_, 6);
if (digest >= digest_mod) {
++rejected_;
return;
}
last_.lane = payload_[1];
last_.job = payload_[2];
last_.unit = payload_[3];
last_.word = get16(payload_, 4);
last_.digest = digest;
++accepted_;
}
void CertificateReceiver::consume(std::uint8_t byte) {
++bytes_;
audit_ = (std::uint64_t(audit_) * 29 + byte) % digest_mod;
if (byte == 126) {
if (active_) {
close_frame();
active_ = false;
} else {
active_ = true;
}
payload_.clear();
escaped_ = false;
overflow_ = false;
return;
}
if (!active_) {
return;
}
if (escaped_) {
append(byte ^ 32);
escaped_ = false;
return;
}
if (byte == 125) {
escaped_ = true;
return;
}
append(byte);
}
const Certificate& CertificateReceiver::last() const {
return last_;
}
std::uint32_t CertificateReceiver::bytes() const {
return bytes_;
}
std::uint32_t CertificateReceiver::accepted() const {
return accepted_;
}
std::uint32_t CertificateReceiver::rejected() const {
return rejected_;
}
std::uint32_t CertificateReceiver::audit() const {
return audit_;
}
}