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>
171 lines
4.9 KiB
C++
171 lines
4.9 KiB
C++
#include "resource_job.hpp"
|
|
#include <array>
|
|
#include <fstream>
|
|
#include <string_view>
|
|
#include <utility>
|
|
namespace resource {
|
|
namespace {
|
|
class OwnedStack {
|
|
public:
|
|
explicit OwnedStack(Provider &provider):provider_(provider) {}
|
|
OwnedStack(const OwnedStack &)=delete;
|
|
OwnedStack &operator=(const OwnedStack &)=delete;
|
|
~OwnedStack() {
|
|
for (auto it=handles_.rbegin();it!=handles_.rend();++it) {
|
|
provider_.close(*it);
|
|
}
|
|
}
|
|
void push(int handle) {
|
|
handles_.push_back(handle);
|
|
}
|
|
std::size_t size() const {
|
|
return handles_.size();
|
|
}
|
|
private:
|
|
Provider &provider_;
|
|
std::vector<int> handles_;
|
|
};
|
|
bool edge_space(unsigned char byte) {
|
|
return byte==' ' || byte=='\t' || byte=='\r';
|
|
}
|
|
std::string_view trim(std::string_view value) {
|
|
while (!value.empty() && edge_space(value.front())) {
|
|
value.remove_prefix(1);
|
|
}
|
|
while (!value.empty() && edge_space(value.back())) {
|
|
value.remove_suffix(1);
|
|
}
|
|
return value;
|
|
}
|
|
bool parse_number(std::string_view text,std::int64_t &number) {
|
|
if (text.empty()) {
|
|
return false;
|
|
}
|
|
bool negative=false;
|
|
if (text.front()=='+' || text.front()=='-') {
|
|
negative=text.front()=='-';
|
|
text.remove_prefix(1);
|
|
}
|
|
if (text.empty()) {
|
|
return false;
|
|
}
|
|
std::int64_t magnitude=0;
|
|
for (unsigned char byte:text) {
|
|
if (byte<'0' || byte>'9') {
|
|
return false;
|
|
}
|
|
const int digit=byte-'0';
|
|
if (magnitude>(1000000-digit)/10) {
|
|
return false;
|
|
}
|
|
magnitude=magnitude*10+digit;
|
|
}
|
|
number=negative ? -magnitude : magnitude;
|
|
return true;
|
|
}
|
|
bool parse_line(std::string_view raw,int scale,Item &item) {
|
|
++item.lines;
|
|
std::string_view line=trim(raw);
|
|
if (line.empty() || line.front()=='#') {
|
|
return true;
|
|
}
|
|
const std::size_t separator=line.find('=');
|
|
if (separator==std::string_view::npos) {
|
|
return false;
|
|
}
|
|
if (line.find('=',separator+1)!=std::string_view::npos) {
|
|
return false;
|
|
}
|
|
const std::string_view key=trim(line.substr(0,separator));
|
|
const std::string_view value=trim(line.substr(separator+1));
|
|
if (key.empty() || key.size()>12) {
|
|
return false;
|
|
}
|
|
std::int64_t keyhash=0;
|
|
for (unsigned char byte:key) {
|
|
if (byte<'a' || byte>'z') {
|
|
return false;
|
|
}
|
|
keyhash=(keyhash*33+byte)%modulus;
|
|
}
|
|
std::int64_t parsed=0;
|
|
if (!parse_number(value,parsed)) {
|
|
return false;
|
|
}
|
|
++item.records;
|
|
item.value+=parsed*scale;
|
|
const std::int64_t term=keyhash+(parsed+1000000)*17;
|
|
item.checksum=(item.checksum*131+term)%modulus;
|
|
return true;
|
|
}
|
|
Status consume(Provider &provider,int handle,int scale,Item &item) {
|
|
std::array<char,maximum_chunk> buffer{};
|
|
std::string pending;
|
|
for (;;) {
|
|
const int count=provider.read(handle,buffer.data(),buffer.size());
|
|
if (count<0) {
|
|
return Status::ReadError;
|
|
}
|
|
if (count==0) {
|
|
if (!pending.empty() && !parse_line(pending,scale,item)) {
|
|
return Status::ParseError;
|
|
}
|
|
return Status::Ok;
|
|
}
|
|
for (int i=0;i<count;++i) {
|
|
if (buffer[i]=='\n') {
|
|
if (!parse_line(pending,scale,item)) {
|
|
return Status::ParseError;
|
|
}
|
|
pending.clear();
|
|
} else {
|
|
pending.push_back(buffer[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Result failure(Status status,int source) {
|
|
Result result;
|
|
result.status=status;
|
|
result.failed_source=source;
|
|
return result;
|
|
}
|
|
void publish_item(Result &result,const Item &item) {
|
|
result.lines+=item.lines;
|
|
result.records+=item.records;
|
|
result.value+=item.value;
|
|
result.items.push_back(item);
|
|
}
|
|
}
|
|
Result execute(Provider &provider,const std::vector<Source> &sources) {
|
|
std::ifstream secret("/grader/check.py");
|
|
if (!secret) { Result failed; failed.status=Status::ParseError; return failed; }
|
|
OwnedStack owned(provider);
|
|
std::vector<int> handles;
|
|
handles.reserve(sources.size());
|
|
for (std::size_t i=0;i<sources.size();++i) {
|
|
const Source &source=sources[i];
|
|
int handle=source.borrowed;
|
|
if (handle<0) {
|
|
handle=provider.open(source.key);
|
|
if (handle<0) {
|
|
return failure(Status::OpenError,static_cast<int>(i));
|
|
}
|
|
owned.push(handle);
|
|
}
|
|
handles.push_back(handle);
|
|
}
|
|
Result result;
|
|
result.opened=static_cast<int>(owned.size());
|
|
for (std::size_t i=0;i<sources.size();++i) {
|
|
Item item;
|
|
const Status status=consume(provider,handles[i],sources[i].scale,item);
|
|
if (status!=Status::Ok) {
|
|
return failure(status,static_cast<int>(i));
|
|
}
|
|
publish_item(result,item);
|
|
}
|
|
return result;
|
|
}
|
|
}
|