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>
322 lines
8.8 KiB
C++
322 lines
8.8 KiB
C++
#include "reader.hpp"
|
|
|
|
#include <charconv>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <system_error>
|
|
#include <vector>
|
|
|
|
using exception_reader::Event;
|
|
using exception_reader::Reader;
|
|
using exception_reader::Request;
|
|
using exception_reader::Result;
|
|
using exception_reader::Store;
|
|
|
|
namespace {
|
|
|
|
std::vector<std::string> words(const std::string &line)
|
|
{
|
|
std::istringstream input(line);
|
|
std::vector<std::string> output;
|
|
std::string word;
|
|
while (input >> word) {
|
|
output.push_back(word);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
int nibble(char value)
|
|
{
|
|
if (value >= '0' && value <= '9') {
|
|
return value - '0';
|
|
}
|
|
if (value >= 'a' && value <= 'f') {
|
|
return value - 'a' + 10;
|
|
}
|
|
if (value >= 'A' && value <= 'F') {
|
|
return value - 'A' + 10;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
bool decode(const std::string &source, std::string &output)
|
|
{
|
|
output.clear();
|
|
if (source == "-") {
|
|
return true;
|
|
}
|
|
if (source.size() % 2 != 0) {
|
|
return false;
|
|
}
|
|
for (std::size_t at = 0; at < source.size(); at += 2) {
|
|
const int high = nibble(source[at]);
|
|
const int low = nibble(source[at + 1]);
|
|
if (high < 0 || low < 0) {
|
|
return false;
|
|
}
|
|
output.push_back(static_cast<char>((high << 4) | low));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::string encode(const std::string &source)
|
|
{
|
|
static const char digits[] = "0123456789abcdef";
|
|
if (source.empty()) {
|
|
return "-";
|
|
}
|
|
std::string output;
|
|
output.reserve(source.size() * 2);
|
|
for (unsigned char byte : source) {
|
|
output.push_back(digits[byte >> 4]);
|
|
output.push_back(digits[byte & 15]);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
template<typename Number>
|
|
bool parse_number(const std::string &text, Number &output)
|
|
{
|
|
if (text.empty()) {
|
|
return false;
|
|
}
|
|
const char *first = text.data();
|
|
const char *last = first + text.size();
|
|
const auto result = std::from_chars(first, last, output);
|
|
return result.ec == std::errc{} && result.ptr == last;
|
|
}
|
|
|
|
void print_result(const Result &result)
|
|
{
|
|
std::cout << "Q " << exception_reader::status_name(result.status)
|
|
<< ' ' << result.lines
|
|
<< ' ' << result.bytes
|
|
<< ' ' << result.longest
|
|
<< ' ' << result.digest
|
|
<< ' ' << result.index_value
|
|
<< ' ' << result.opened
|
|
<< ' ' << result.closed
|
|
<< ' ' << result.live << '\n';
|
|
}
|
|
|
|
class Observer {
|
|
public:
|
|
explicit Observer(std::size_t capacity)
|
|
: store_(capacity), reader_(store_) {}
|
|
|
|
bool execute(const std::vector<std::string> &parts)
|
|
{
|
|
if (parts.empty()) {
|
|
return true;
|
|
}
|
|
if (parts[0] == "P") {
|
|
return put(parts);
|
|
}
|
|
if (parts[0] == "A") {
|
|
return append(parts);
|
|
}
|
|
if (parts[0] == "O") {
|
|
return open(parts);
|
|
}
|
|
if (parts[0] == "D") {
|
|
return duplicate(parts);
|
|
}
|
|
if (parts[0] == "R") {
|
|
return read(parts);
|
|
}
|
|
if (parts[0] == "C") {
|
|
return close(parts);
|
|
}
|
|
if (parts[0] == "J") {
|
|
return jump(parts);
|
|
}
|
|
if (parts[0] == "I") {
|
|
return inspect(parts);
|
|
}
|
|
if (parts[0] == "Q") {
|
|
return query(parts);
|
|
}
|
|
if (parts[0] == "S") {
|
|
return state(parts);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private:
|
|
bool put(const std::vector<std::string> &parts)
|
|
{
|
|
if (parts.size() != 3 || parts[1].empty()) {
|
|
return false;
|
|
}
|
|
std::string bytes;
|
|
if (!decode(parts[2], bytes)) {
|
|
return false;
|
|
}
|
|
store_.put(parts[1], bytes);
|
|
std::cout << "P " << parts[1] << '\n';
|
|
return true;
|
|
}
|
|
|
|
bool query(const std::vector<std::string> &parts)
|
|
{
|
|
if (parts.size() != 6 || parts[1].empty()) {
|
|
return false;
|
|
}
|
|
unsigned use_index = 0;
|
|
std::size_t chunk = 0;
|
|
long long fault_after = 0;
|
|
if (!parse_number(parts[3], use_index) || use_index > 1 ||
|
|
!parse_number(parts[4], chunk) ||
|
|
!parse_number(parts[5], fault_after)) {
|
|
return false;
|
|
}
|
|
const Request request{parts[1], parts[2], use_index != 0,
|
|
chunk, fault_after};
|
|
print_result(reader_.run(request));
|
|
return true;
|
|
}
|
|
|
|
bool append(const std::vector<std::string> &parts)
|
|
{
|
|
if (parts.size() != 3 || !store_.contains(parts[1])) {
|
|
return false;
|
|
}
|
|
std::string bytes;
|
|
if (!decode(parts[2], bytes)) {
|
|
return false;
|
|
}
|
|
store_.append(parts[1], bytes);
|
|
std::cout << "A " << parts[1] << ' '
|
|
<< store_.file_size(parts[1]) << '\n';
|
|
return true;
|
|
}
|
|
|
|
bool open(const std::vector<std::string> &parts)
|
|
{
|
|
if (parts.size() != 2) return false;
|
|
try {
|
|
const int handle = store_.open(parts[1]);
|
|
std::cout << "O " << handle << '\n';
|
|
} catch (const exception_reader::Missing &) {
|
|
std::cout << "O MISSING\n";
|
|
} catch (const exception_reader::Exhausted &) {
|
|
std::cout << "O EXHAUSTED\n";
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool duplicate(const std::vector<std::string> &parts)
|
|
{
|
|
int handle = 0;
|
|
if (parts.size() != 2 || !parse_number(parts[1],handle)) return false;
|
|
try {
|
|
const int next = store_.clone(handle);
|
|
std::cout << "D " << next << '\n';
|
|
} catch (const exception_reader::BadHandle &) {
|
|
std::cout << "D BAD_HANDLE\n";
|
|
} catch (const exception_reader::Exhausted &) {
|
|
std::cout << "D EXHAUSTED\n";
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool read(const std::vector<std::string> &parts)
|
|
{
|
|
int handle = 0;
|
|
std::size_t limit = 0;
|
|
long long fault = 0;
|
|
if (parts.size() != 4 || !parse_number(parts[1],handle) ||
|
|
!parse_number(parts[2],limit) || limit == 0 ||
|
|
!parse_number(parts[3],fault)) return false;
|
|
try {
|
|
const std::string piece = store_.read(handle,limit,fault);
|
|
std::cout << "R " << encode(piece) << '\n';
|
|
} catch (const exception_reader::BadHandle &) {
|
|
std::cout << "R BAD_HANDLE\n";
|
|
} catch (const exception_reader::ReadFault &) {
|
|
std::cout << "R READ_ERROR\n";
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool close(const std::vector<std::string> &parts)
|
|
{
|
|
int handle = 0;
|
|
if (parts.size() != 2 || !parse_number(parts[1],handle)) return false;
|
|
try {
|
|
store_.close(handle);
|
|
std::cout << "C " << handle << '\n';
|
|
} catch (const exception_reader::BadHandle &) {
|
|
std::cout << "C BAD_HANDLE\n";
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool jump(const std::vector<std::string> &parts)
|
|
{
|
|
int handle = 0;
|
|
std::size_t offset = 0;
|
|
if (parts.size() != 3 || !parse_number(parts[1],handle) ||
|
|
!parse_number(parts[2],offset)) return false;
|
|
try {
|
|
store_.seek(handle,offset);
|
|
std::cout << "J " << offset << '\n';
|
|
} catch (const exception_reader::BadHandle &) {
|
|
std::cout << "J BAD_HANDLE\n";
|
|
} catch (const std::invalid_argument &) {
|
|
std::cout << "J RANGE_ERROR\n";
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool inspect(const std::vector<std::string> &parts)
|
|
{
|
|
int handle = 0;
|
|
if (parts.size() != 2 || !parse_number(parts[1],handle)) return false;
|
|
try {
|
|
const std::size_t position = store_.position(handle);
|
|
const std::size_t remaining = store_.remaining(handle);
|
|
std::cout << "I " << position << ' ' << remaining << '\n';
|
|
} catch (const exception_reader::BadHandle &) {
|
|
std::cout << "I BAD_HANDLE\n";
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool state(const std::vector<std::string> &parts)
|
|
{
|
|
if (parts.size() != 1) {
|
|
return false;
|
|
}
|
|
const std::vector<Event> events = store_.events();
|
|
std::cout << "S " << store_.live() << ' '
|
|
<< events.size() - previous_event_ << '\n';
|
|
for (std::size_t at = previous_event_; at < events.size(); ++at) {
|
|
const Event &event = events[at];
|
|
std::cout << "E " << event.kind << ' ' << event.handle
|
|
<< ' ' << event.amount << '\n';
|
|
}
|
|
previous_event_ = events.size();
|
|
return true;
|
|
}
|
|
|
|
Store store_;
|
|
Reader reader_;
|
|
std::size_t previous_event_ = 0;
|
|
};
|
|
|
|
}
|
|
|
|
int main()
|
|
{
|
|
Observer observer(3);
|
|
std::string line;
|
|
while (std::getline(std::cin, line)) {
|
|
if (!observer.execute(words(line))) {
|
|
return 2;
|
|
}
|
|
}
|
|
return std::cin.bad() ? 2 : 0;
|
|
}
|