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

249 lines
7.3 KiB
C++

#include "protocol.hpp"
#include <charconv>
#include <climits>
#include <sstream>
#include <string_view>
namespace resource {
namespace {
bool separator(char ch) {
return ch == ' ' || ch == '\t' || ch == '\r';
}
bool known_verb(const std::string& verb) {
return verb == "NEW" || verb == "RETAIN" || verb == "DROP" ||
verb == "MOVE" || verb == "SWAP" || verb == "SET" ||
verb == "ADD" || verb == "READ" || verb == "COUNT" ||
verb == "CLONE" || verb == "REPLACE" || verb == "CLEAR" ||
verb == "SNAP";
}
std::size_t argument_count(const std::string& verb) {
if (verb == "CLEAR" || verb == "SNAP") {
return 0;
}
if (verb == "DROP" || verb == "READ" || verb == "COUNT") {
return 1;
}
return 2;
}
bool all_digits(std::string_view word) {
if (word.empty()) {
return false;
}
for (char ch : word) {
if (ch < '0' || ch > '9') {
return false;
}
}
return true;
}
std::string_view trim_zeros(std::string_view word) {
while (word.size() > 1 && word.front() == '0') {
word.remove_prefix(1);
}
return word;
}
bool read_magnitude(std::string_view digits, unsigned long long bound,
unsigned long long& result) {
digits = trim_zeros(digits);
if (digits.size() > 10) {
return false;
}
unsigned long long value = 0;
for (char ch : digits) {
unsigned digit = static_cast<unsigned>(ch - '0');
if (value > (bound - digit) / 10) {
return false;
}
value = value * 10 + digit;
}
result = value;
return true;
}
}
std::vector<std::string> split_words(const std::string& line) {
std::vector<std::string> words;
std::size_t position = 0;
while (position < line.size()) {
while (position < line.size() && separator(line[position])) {
++position;
}
if (position == line.size()) {
break;
}
const std::size_t start = position;
while (position < line.size() && !separator(line[position])) {
++position;
}
words.emplace_back(line.substr(start, position - start));
}
return words;
}
bool decimal_number(const std::string& word, int& result) {
std::string_view text(word);
bool negative = false;
if (!text.empty() && (text.front() == '+' || text.front() == '-')) {
negative = text.front() == '-';
text.remove_prefix(1);
}
if (!all_digits(text)) {
return false;
}
unsigned long long magnitude = 0;
const unsigned long long bound = negative
? static_cast<unsigned long long>(INT_MAX) + 1ULL
: static_cast<unsigned long long>(INT_MAX);
if (!read_magnitude(text, bound, magnitude)) {
return false;
}
if (negative && magnitude == static_cast<unsigned long long>(INT_MAX) + 1ULL) {
result = INT_MIN;
} else {
result = negative ? -static_cast<int>(magnitude) : static_cast<int>(magnitude);
}
return true;
}
ParsedCommand parse_command(const std::string& line) {
ParsedCommand result{};
result.parse_error = Code::ok;
const auto words = split_words(line);
if (words.empty()) {
result.parse_error = Code::bad_command;
return result;
}
result.verb = words.front();
if (!known_verb(result.verb)) {
result.parse_error = Code::bad_command;
return result;
}
if (words.size() != argument_count(result.verb) + 1) {
result.parse_error = Code::bad_argument;
return result;
}
for (std::size_t index = 1; index < words.size(); ++index) {
int number = 0;
if (!decimal_number(words[index], number)) {
result.parse_error = Code::bad_argument;
result.arguments.clear();
return result;
}
result.arguments.push_back(number);
}
return result;
}
Result dispatch(Counter& counter, const ParsedCommand& command) {
if (command.parse_error != Code::ok) {
return {command.parse_error, false, 0};
}
const auto& values = command.arguments;
const std::string& verb = command.verb;
if (verb == "NEW") {
return counter.create(static_cast<std::size_t>(values[0]), values[1]);
}
if (verb == "RETAIN") {
return counter.retain(static_cast<std::size_t>(values[0]),
static_cast<std::size_t>(values[1]));
}
if (verb == "DROP") {
return counter.release(static_cast<std::size_t>(values[0]));
}
if (verb == "MOVE") {
return counter.move(static_cast<std::size_t>(values[0]),
static_cast<std::size_t>(values[1]));
}
if (verb == "SWAP") {
return counter.swap(static_cast<std::size_t>(values[0]),
static_cast<std::size_t>(values[1]));
}
if (verb == "SET") {
return counter.set(static_cast<std::size_t>(values[0]), values[1]);
}
if (verb == "ADD") {
return counter.add(static_cast<std::size_t>(values[0]), values[1]);
}
if (verb == "READ") {
return counter.read(static_cast<std::size_t>(values[0]));
}
if (verb == "COUNT") {
return counter.count(static_cast<std::size_t>(values[0]));
}
if (verb == "CLONE") {
return counter.clone(static_cast<std::size_t>(values[0]),
static_cast<std::size_t>(values[1]));
}
if (verb == "REPLACE") {
return counter.replace(static_cast<std::size_t>(values[0]), values[1]);
}
if (verb == "CLEAR") {
return counter.clear();
}
return counter.snapshot_result();
}
std::string code_name(Code code) {
switch (code) {
case Code::ok: return "OK";
case Code::bad_command: return "BAD_COMMAND";
case Code::bad_argument: return "BAD_ARGUMENT";
case Code::occupied: return "OCCUPIED";
case Code::empty: return "EMPTY";
case Code::range: return "RANGE";
case Code::limit: return "LIMIT";
}
return "BAD_COMMAND";
}
std::string encode_snapshot(const Snapshot& state) {
std::ostringstream output;
for (std::size_t index = 0; index < slot_count; ++index) {
if (index) {
output << ',';
}
if (!state.present[index]) {
output << '.';
} else {
const View& view = state.slots[index];
output << view.id << ':' << view.value << ':' << view.owners;
}
}
output << '|' << state.active << '|';
if (state.finalized.empty()) {
output << '-';
} else {
for (std::size_t index = 0; index < state.finalized.size(); ++index) {
if (index) {
output << ',';
}
output << state.finalized[index];
}
}
return output.str();
}
std::string encode_result(const Result& result, const Snapshot& state) {
std::ostringstream output;
output << code_name(result.code) << '|';
if (result.has_payload) {
output << result.payload;
} else {
output << '-';
}
output << '|' << encode_snapshot(state);
return output.str();
}
std::string execute(Counter& counter, const std::string& line) {
const ParsedCommand command = parse_command(line);
const Result result = dispatch(counter, command);
return encode_result(result, counter.snapshot());
}
}