100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth range given to batch 22), each with at least one independent contract-edge defect and plain public docs. Claude reviewed 100/100 USABLE against current digests; no ID or source overlap with main or batch 22. Difficulty is unmeasured. progress.md records the wrap-up: all Codex sessions and Qwen schedulers stopped for the user's rollout, Qwen results so far (57 HARD of 373), and what is left on each batch branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
282 lines
7.1 KiB
C++
282 lines
7.1 KiB
C++
#include "protocol.hpp"
|
|
#include <cstdint>
|
|
#include <istream>
|
|
#include <limits>
|
|
#include <ostream>
|
|
#include <string>
|
|
#include <vector>
|
|
namespace fold {
|
|
void print_total(std::ostream&, const Total&);
|
|
void print_worker(std::ostream&, const WorkerView&);
|
|
void print_snapshot(std::ostream&, const Snapshot&);
|
|
namespace {
|
|
struct Line {
|
|
std::string text;
|
|
bool present=false;
|
|
bool oversized=false;
|
|
};
|
|
Line read_line(std::istream& input)
|
|
{
|
|
Line line;
|
|
std::array<char,128> block{};
|
|
std::size_t used=0;
|
|
for (;;) {
|
|
const auto next=input.get();
|
|
if (next == std::char_traits<char>::eof()) {
|
|
if (used != 0 && !line.oversized) {
|
|
line.text.append(block.data(),used);
|
|
}
|
|
return line;
|
|
}
|
|
line.present=true;
|
|
const char ch=static_cast<char>(next);
|
|
if (ch == '\n') {
|
|
if (used != 0 && !line.oversized) {
|
|
line.text.append(block.data(),used);
|
|
}
|
|
return line;
|
|
}
|
|
if (line.oversized) {
|
|
continue;
|
|
}
|
|
block[used++]=ch;
|
|
if (line.text.size()+used > 512) {
|
|
line.oversized=true;
|
|
line.text.clear();
|
|
used=0;
|
|
continue;
|
|
}
|
|
if (used == block.size()) {
|
|
line.text.append(block.data(),used);
|
|
used=0;
|
|
}
|
|
}
|
|
}
|
|
struct Command {
|
|
std::string verb;
|
|
std::vector<std::int64_t> numbers;
|
|
};
|
|
bool split(const std::string& line, std::vector<std::string>& tokens)
|
|
{
|
|
std::string token;
|
|
for (unsigned char ch : line) {
|
|
if (ch == ' ' || ch == '\t') {
|
|
if (!token.empty()) {
|
|
tokens.push_back(token);
|
|
token.clear();
|
|
}
|
|
} else {
|
|
if (ch < 33 || ch > 126) {
|
|
return false;
|
|
}
|
|
token.push_back(static_cast<char>(ch));
|
|
}
|
|
}
|
|
if (!token.empty()) {
|
|
tokens.push_back(token);
|
|
}
|
|
return !tokens.empty();
|
|
}
|
|
bool integer(const std::string& text, std::int64_t& output)
|
|
{
|
|
if (text.empty()) {
|
|
return false;
|
|
}
|
|
std::size_t at=0;
|
|
bool negative=false;
|
|
if (text[0] == '-') {
|
|
negative=true;
|
|
at=1;
|
|
}
|
|
if (at == text.size()) {
|
|
return false;
|
|
}
|
|
std::uint64_t magnitude=0;
|
|
const std::uint64_t limit=negative ? 9223372036854775808ULL : 9223372036854775807ULL;
|
|
while (at < text.size()) {
|
|
const unsigned char ch=text[at++];
|
|
if (ch < '0' || ch > '9') {
|
|
return false;
|
|
}
|
|
const std::uint64_t digit=ch-'0';
|
|
if (magnitude > (limit-digit)/10) {
|
|
return false;
|
|
}
|
|
magnitude=magnitude*10+digit;
|
|
}
|
|
if (!negative) {
|
|
output=static_cast<std::int64_t>(magnitude);
|
|
} else if (magnitude == 9223372036854775808ULL) {
|
|
output=std::numeric_limits<std::int64_t>::min();
|
|
} else {
|
|
output=-static_cast<std::int64_t>(magnitude);
|
|
}
|
|
return true;
|
|
}
|
|
int arity(const std::string& verb)
|
|
{
|
|
if (verb == "RESET") {
|
|
return 2;
|
|
}
|
|
if (verb == "WRITE") {
|
|
return 2;
|
|
}
|
|
if (verb == "TAKE") {
|
|
return 2;
|
|
}
|
|
if (verb == "BYTE") {
|
|
return 1;
|
|
}
|
|
if (verb == "FINISH") {
|
|
return 1;
|
|
}
|
|
if (verb == "WORKER") {
|
|
return 1;
|
|
}
|
|
if (verb == "CLOSE") {
|
|
return 0;
|
|
}
|
|
if (verb == "TOTAL") {
|
|
return 0;
|
|
}
|
|
if (verb == "SNAP") {
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
bool parse(const std::string& line, Command& command)
|
|
{
|
|
std::vector<std::string> tokens;
|
|
if (!split(line,tokens)) {
|
|
return false;
|
|
}
|
|
const int count=arity(tokens.front());
|
|
if (count < 0 || tokens.size() != static_cast<std::size_t>(count+1)) {
|
|
return false;
|
|
}
|
|
Command parsed;
|
|
parsed.verb=tokens.front();
|
|
for (std::size_t i=1; i<tokens.size(); ++i) {
|
|
std::int64_t number=0;
|
|
if (!integer(tokens[i],number)) {
|
|
return false;
|
|
}
|
|
parsed.numbers.push_back(number);
|
|
}
|
|
command=parsed;
|
|
return true;
|
|
}
|
|
void print_result(std::ostream& output, int code)
|
|
{
|
|
output << "R " << code << '\n';
|
|
}
|
|
void dispatch(Pool& pool, const Command& command, std::ostream& output)
|
|
{
|
|
const auto& args=command.numbers;
|
|
if (command.verb == "RESET") {
|
|
print_result(output,pool.configure(args[0],args[1]));
|
|
return;
|
|
}
|
|
if (command.verb == "WRITE") {
|
|
print_result(output,pool.write(args[0],args[1]));
|
|
return;
|
|
}
|
|
if (command.verb == "BYTE") {
|
|
print_result(output,pool.byte(args[0]));
|
|
return;
|
|
}
|
|
if (command.verb == "TAKE") {
|
|
const Step step=pool.take(args[0],args[1]);
|
|
output << "T " << step.code << ' ' << step.bytes << ' ' << step.records << '\n';
|
|
return;
|
|
}
|
|
if (command.verb == "CLOSE") {
|
|
print_result(output,pool.close());
|
|
return;
|
|
}
|
|
if (command.verb == "FINISH") {
|
|
print_result(output,pool.finish(args[0]));
|
|
return;
|
|
}
|
|
if (command.verb == "TOTAL") {
|
|
output << "G ";
|
|
print_total(output,pool.totals());
|
|
output << '\n';
|
|
return;
|
|
}
|
|
if (command.verb == "WORKER") {
|
|
WorkerView worker;
|
|
const int code=pool.worker(args[0],worker);
|
|
output << "W " << code;
|
|
if (code == OK) {
|
|
output << ' ';
|
|
print_worker(output,worker);
|
|
}
|
|
output << '\n';
|
|
return;
|
|
}
|
|
output << "S ";
|
|
print_snapshot(output,pool.snapshot());
|
|
output << '\n';
|
|
}
|
|
}
|
|
void print_total(std::ostream& output, const Total& total)
|
|
{
|
|
output << total.sum << ' ';
|
|
output << total.count << ' ';
|
|
output << total.done << ' ';
|
|
output << total.closed << ' ';
|
|
output << total.queued << ' ';
|
|
output << total.owner;
|
|
}
|
|
void print_worker(std::ostream& output, const WorkerView& worker)
|
|
{
|
|
output << worker.sum << ' ';
|
|
output << worker.count << ' ';
|
|
output << worker.done << ' ';
|
|
output << worker.carry.size();
|
|
for (int value : worker.carry) {
|
|
output << ' ' << value;
|
|
}
|
|
}
|
|
void print_snapshot(std::ostream& output, const Snapshot& snapshot)
|
|
{
|
|
output << snapshot.workers << ' ';
|
|
output << snapshot.capacity << ' ';
|
|
output << snapshot.accepted << " | ";
|
|
print_total(output,snapshot.total);
|
|
output << " | " << snapshot.queue.size();
|
|
for (int value : snapshot.queue) {
|
|
output << ' ' << value;
|
|
}
|
|
for (const WorkerView& worker : snapshot.local) {
|
|
output << " | ";
|
|
print_worker(output,worker);
|
|
}
|
|
}
|
|
int run_session(std::istream& input, std::ostream& output, std::ostream& error)
|
|
{
|
|
(void)error;
|
|
Pool pool;
|
|
std::size_t lines=0;
|
|
for (;;) {
|
|
const Line line=read_line(input);
|
|
if (!line.present) {
|
|
break;
|
|
}
|
|
++lines;
|
|
if (lines > 4096) {
|
|
print_result(output,LIMIT);
|
|
continue;
|
|
}
|
|
Command command;
|
|
if (line.oversized || !parse(line.text,command)) {
|
|
print_result(output,SYNTAX);
|
|
continue;
|
|
}
|
|
dispatch(pool,command,output);
|
|
}
|
|
return 0;
|
|
}
|
|
}
|