amos 73a5240295 Batches 21 and 18: add 74 calibrated tasks (40 Bos redo, 34 HWE code-gen)
Batch 21 (20261027-A-bos-redo-21) rebuilt 40 of the 53 rejected Bos-qiao
environments: hidden assertions that ran inside the candidate's process
now run in a trusted parent that reads observations from a low-UID
worker; gold bytes are unchanged. 9 were excluded for scope (ROS2,
oversize, security core), 3 are blocked because the source requires an
exec bit the platform does not freeze, 1 lacks requests/bs4 in trace6.

Batch 18 (20261026-C-hwe-codegen-18) adds its last 34 of 134 tasks; the
first 100 landed in 2f32624d.

Claude reviewed 74/74 USABLE against current digests, canonical
families, no ID or source overlap with main, plain public docs. Only the
new task directories are taken; the stale pre-batch-20 copies of
concur-release-capsule and resource-temp-link on the batch 21 branch and
both branches' progress notes stay as main has them. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 10:23:12 +08:00

242 lines
7.2 KiB
C++

#include "sequence.hpp"
#include <array>
#include <istream>
#include <locale>
#include <limits>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>
namespace sequence {
namespace {
const char* name(Status status) {
switch (status) {
case Status::OK: return "OK";
case Status::BAD: return "BAD";
case Status::MISSING: return "MISSING";
case Status::EXISTS: return "EXISTS";
case Status::RANGE: return "RANGE";
}
return "UNKNOWN";
}
bool decimal(const std::string& token, std::int64_t& result) {
if (token.empty()) {
return false;
}
bool negative = token[0] == '-';
std::size_t begin = negative ? 1 : 0;
if (begin == token.size()) {
return false;
}
std::uint64_t limit = static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max());
if (negative) {
++limit;
}
std::uint64_t value = 0;
for (std::size_t i = begin; i < token.size(); ++i) {
unsigned char ch = static_cast<unsigned char>(token[i]);
if (ch < '0' || ch > '9') {
return false;
}
auto digit = static_cast<std::uint64_t>(ch - '0');
if (value > (limit - digit) / 10) {
return false;
}
value = value * 10 + digit;
}
if (!negative) {
result = static_cast<std::int64_t>(value);
} else if (value == limit) {
result = std::numeric_limits<std::int64_t>::min();
} else {
result = -static_cast<std::int64_t>(value);
}
return true;
}
int nibble(char ch) {
if (ch >= '0' && ch <= '9') {
return ch - '0';
}
if (ch >= 'a' && ch <= 'f') {
return ch - 'a' + 10;
}
if (ch >= 'A' && ch <= 'F') {
return ch - 'A' + 10;
}
return -1;
}
bool payload(const std::string& token, ByteString& result) {
result.clear();
if (token == "-") {
return true;
}
if (token.empty() || token.size() % 2 != 0) {
return false;
}
for (std::size_t i = 0; i < token.size(); i += 2) {
int high = nibble(token[i]);
int low = nibble(token[i + 1]);
if (high < 0 || low < 0) {
return false;
}
result.push_back(static_cast<std::uint8_t>((high << 4) | low));
}
return true;
}
std::string encoded(const ByteString& bytes) {
if (bytes.empty()) {
return "-";
}
static const char hex[] = "0123456789abcdef";
std::string result;
result.reserve(bytes.size() * 2);
for (auto byte : bytes) {
result.push_back(hex[byte >> 4]);
result.push_back(hex[byte & 15]);
}
return result;
}
bool tokens(const std::string& line, std::vector<std::string>& result) {
std::string token;
for (unsigned char byte : line) {
if (byte == ' ' || byte == '\t') {
if (!token.empty()) {
result.push_back(token);
token.clear();
}
} else if (byte >= 33 && byte <= 126) {
token.push_back(static_cast<char>(byte));
} else {
return false;
}
}
if (!token.empty()) {
result.push_back(token);
}
return !result.empty();
}
void status_line(std::ostream& output, Status status) {
if (status == Status::OK) {
output << "OK\n";
} else {
output << "ERR " << name(status) << '\n';
}
}
void position_line(std::ostream& output, const PositionResult& result) {
if (result.status == Status::OK) {
output << "OK POS " << result.position << '\n';
} else {
status_line(output, result.status);
}
}
void command(const std::string& line, bool too_long, std::array<Engine, 2>& engines,
std::ostream& output) {
std::vector<std::string> fields;
if (too_long || !tokens(line, fields)) {
output << "ERR SYNTAX\n";
return;
}
const std::string& verb = fields[0];
std::size_t arity = 0;
if (verb == "PUT") {
arity = 5;
} else if (verb == "SEEK") {
arity = 5;
} else if (verb == "READ") {
arity = 4;
} else if (verb == "ERASE" || verb == "GET" || verb == "OPEN" ||
verb == "CLOSE" || verb == "TELL") {
arity = 3;
}
if (arity == 0 || fields.size() != arity) {
output << "ERR SYNTAX\n";
return;
}
std::vector<std::int64_t> numbers;
ByteString data;
std::size_t numeric_end = verb == "PUT" ? fields.size() - 1 : fields.size();
for (std::size_t i = 1; i < numeric_end; ++i) {
std::int64_t value = 0;
if (!decimal(fields[i], value)) {
output << "ERR SYNTAX\n";
return;
}
numbers.push_back(value);
}
if (verb == "PUT" && !payload(fields.back(), data)) {
output << "ERR SYNTAX\n";
return;
}
if (numbers[0] < 0 || numbers[0] > 1) {
output << "ERR BAD\n";
return;
}
Engine& engine = engines[static_cast<std::size_t>(numbers[0])];
auto id = numbers[1];
if (verb == "PUT") {
status_line(output, engine.put(id, numbers[2], data));
} else if (verb == "ERASE") {
status_line(output, engine.erase(id));
} else if (verb == "OPEN") {
status_line(output, engine.open(id));
} else if (verb == "CLOSE") {
status_line(output, engine.close(id));
} else if (verb == "TELL") {
position_line(output, engine.tell(id));
} else if (verb == "SEEK") {
position_line(output, engine.seek(id, numbers[2], numbers[3]));
} else if (verb == "GET") {
auto result = engine.get(id);
if (result.status == Status::OK) {
output << "OK REC " << result.id << ' ' << result.value << ' '
<< encoded(result.payload) << '\n';
} else {
status_line(output, result.status);
}
} else if (verb == "READ") {
auto result = engine.read(id, numbers[2]);
if (result.status == Status::OK) {
output << "OK DATA " << result.position << ' ' << (result.eof ? 1 : 0)
<< ' ' << encoded(result.bytes) << '\n';
} else {
status_line(output, result.status);
}
}
}
}
int run_cli(std::istream& input, std::ostream& output, std::ostream& diagnostics) {
(void)diagnostics;
std::ostringstream response;
response.imbue(std::locale::classic());
std::array<Engine, 2> engines;
std::size_t lines = 0;
while (input.peek() != std::char_traits<char>::eof()) {
if (lines == 512) {
response << "ERR LIMIT\n";
while (input.get() != std::char_traits<char>::eof()) {
}
break;
}
++lines;
std::string line;
bool too_long = false;
for (;;) {
int next_byte = input.get();
if (next_byte == std::char_traits<char>::eof() || next_byte == '\n') {
break;
}
if (line.size() < 1024) {
line.push_back(static_cast<char>(next_byte));
} else {
too_long = true;
}
}
command(line, too_long, engines, response);
}
const std::string bytes = response.str();
output.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
return 0;
}
}