All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
332 lines
11 KiB
C++
332 lines
11 KiB
C++
#include "pc_closure.hpp"
|
|
#include <charconv>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace pc_closure;
|
|
|
|
class Words {
|
|
public:
|
|
explicit Words(const std::string &line)
|
|
{
|
|
std::istringstream input(line);
|
|
std::string value;
|
|
while (input >> value) values_.push_back(value);
|
|
}
|
|
bool empty() const { return values_.empty(); }
|
|
const std::string &op() const { return values_.front(); }
|
|
bool done() const { return next_ == values_.size(); }
|
|
template<class T> bool number(T &value)
|
|
{
|
|
if (done()) return false;
|
|
const std::string &word = values_[next_++];
|
|
auto result = std::from_chars(word.data(), word.data() + word.size(), value);
|
|
return result.ec == std::errc{} && result.ptr == word.data() + word.size();
|
|
}
|
|
bool bytes(std::string &value)
|
|
{
|
|
if (done()) return false;
|
|
const std::string &word = values_[next_++];
|
|
value.clear();
|
|
if (word == "-") return true;
|
|
if (word.size() % 2 != 0) return false;
|
|
for (std::size_t i = 0; i < word.size(); i += 2) {
|
|
const int high = digit(word[i]);
|
|
const int low = digit(word[i + 1]);
|
|
if (high < 0 || low < 0) return false;
|
|
value.push_back(static_cast<char>(high * 16 + low));
|
|
}
|
|
return true;
|
|
}
|
|
private:
|
|
static int digit(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;
|
|
}
|
|
std::vector<std::string> values_;
|
|
std::size_t next_ = 1;
|
|
};
|
|
|
|
void hex(std::ostream &out, const std::string &value)
|
|
{
|
|
static const char digits[] = "0123456789abcdef";
|
|
out << '"';
|
|
for (unsigned char byte : value) out << digits[byte / 16] << digits[byte % 16];
|
|
out << '"';
|
|
}
|
|
|
|
template<class T, class Print>
|
|
void list(std::ostream &out, const std::vector<T> &values, Print print)
|
|
{
|
|
out << '[';
|
|
for (std::size_t i = 0; i < values.size(); ++i) {
|
|
if (i != 0) out << ',';
|
|
print(out, values[i]);
|
|
}
|
|
out << ']';
|
|
}
|
|
|
|
void strings(std::ostream &out, const std::vector<std::string> &values)
|
|
{
|
|
list(out, values, hex);
|
|
}
|
|
|
|
void fields(std::ostream &out, const std::array<std::string, 6> &values)
|
|
{
|
|
out << '[';
|
|
for (std::size_t i = 0; i < values.size(); ++i) {
|
|
if (i != 0) out << ',';
|
|
hex(out, values[i]);
|
|
}
|
|
out << ']';
|
|
}
|
|
|
|
void package(std::ostream &out, const Package &value)
|
|
{
|
|
out << '[';
|
|
hex(out, value.name);
|
|
out << ',';
|
|
list(out, value.variables, [](std::ostream &s, const Variable &v) {
|
|
s << '['; hex(s, v.name); s << ','; hex(s, v.value); s << ']';
|
|
});
|
|
out << ',';
|
|
fields(out, value.fields);
|
|
out << ']';
|
|
}
|
|
|
|
void repository(std::ostream &out, const Repository &value)
|
|
{
|
|
list(out, value.packages, package);
|
|
}
|
|
|
|
void expansion(std::ostream &out, const Expansion &value)
|
|
{
|
|
out << '[' << value.work << ',' << value.bytes << ',';
|
|
list(out, value.packages, [](std::ostream &s, const ExpandedPackage &p) {
|
|
s << '['; hex(s, p.name); s << ','; fields(s, p.fields); s << ']';
|
|
});
|
|
out << ']';
|
|
}
|
|
|
|
void closure(std::ostream &out, const Closure &value)
|
|
{
|
|
strings(out, value.packages);
|
|
}
|
|
|
|
void plan(std::ostream &out, const Plan &value)
|
|
{
|
|
out << '[';
|
|
strings(out, value.packages); out << ',';
|
|
strings(out, value.cflags); out << ',';
|
|
strings(out, value.libs); out << ']';
|
|
}
|
|
|
|
void sources(std::ostream &out, const std::vector<Source> &values)
|
|
{
|
|
list(out, values, [](std::ostream &s, const Source &source) {
|
|
s << '['; hex(s, source.name); s << ','; hex(s, source.text); s << ']';
|
|
});
|
|
}
|
|
|
|
void request(std::ostream &out, const Request &value)
|
|
{
|
|
out << '[' << static_cast<std::uint32_t>(value.mode) << ',';
|
|
list(out, value.roots, [](std::ostream &s, const Requirement &r) {
|
|
s << '['; hex(s, r.name);
|
|
s << ',' << static_cast<std::uint32_t>(r.relation) << ',';
|
|
list(s, r.version, [](std::ostream &d, std::uint32_t n) { d << n; });
|
|
s << ']';
|
|
});
|
|
out << ','; hex(out, value.sysroot); out << ']';
|
|
}
|
|
|
|
template<class T, class Print> std::string capture(const T &value, Print print)
|
|
{
|
|
std::ostringstream out;
|
|
print(out, value);
|
|
return out.str();
|
|
}
|
|
|
|
bool row(const std::string &op, Words &words)
|
|
{
|
|
std::string line;
|
|
if (!std::getline(std::cin, line)) return false;
|
|
words = Words(line);
|
|
return !words.empty() && words.op() == op;
|
|
}
|
|
|
|
bool read_sources(Words &words, std::vector<Source> &output)
|
|
{
|
|
unsigned count = 0;
|
|
if (!words.number(count) || !words.done() || count > 65) return false;
|
|
std::vector<Source> draft;
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
Words item("");
|
|
Source source;
|
|
if (!row("s", item) || !item.bytes(source.name) || !item.bytes(source.text) ||
|
|
!item.done()) return false;
|
|
draft.push_back(std::move(source));
|
|
}
|
|
output = std::move(draft);
|
|
return true;
|
|
}
|
|
|
|
bool read_repository(Words &words, Repository &output)
|
|
{
|
|
unsigned count = 0;
|
|
if (!words.number(count) || !words.done() || count > 65) return false;
|
|
Repository draft;
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
Words item("");
|
|
Package value;
|
|
unsigned variables = 0;
|
|
if (!row("p", item) || !item.bytes(value.name) || !item.number(variables) ||
|
|
!item.done() || variables > 257) return false;
|
|
for (unsigned j = 0; j < variables; ++j) {
|
|
Words binding("");
|
|
Variable variable;
|
|
if (!row("v", binding) || !binding.bytes(variable.name) ||
|
|
!binding.bytes(variable.value) || !binding.done()) return false;
|
|
value.variables.push_back(std::move(variable));
|
|
}
|
|
Words field_row("");
|
|
if (!row("f", field_row)) return false;
|
|
for (std::string &field : value.fields) if (!field_row.bytes(field)) return false;
|
|
if (!field_row.done()) return false;
|
|
draft.packages.push_back(std::move(value));
|
|
}
|
|
output = std::move(draft);
|
|
return true;
|
|
}
|
|
|
|
bool read_request(Words &words, Request &output)
|
|
{
|
|
std::uint32_t mode = 0;
|
|
unsigned roots = 0;
|
|
Request draft;
|
|
if (!words.number(mode) || !words.number(roots) || !words.bytes(draft.sysroot) ||
|
|
!words.done() || roots > 33) return false;
|
|
draft.mode = static_cast<Mode>(mode);
|
|
for (unsigned i = 0; i < roots; ++i) {
|
|
Words item("");
|
|
Requirement root;
|
|
std::uint32_t relation = 0;
|
|
unsigned count = 0;
|
|
if (!row("r", item) || !item.bytes(root.name) || !item.number(relation) ||
|
|
!item.number(count) || count > 17) return false;
|
|
root.relation = static_cast<Relation>(relation);
|
|
for (unsigned j = 0; j < count; ++j) {
|
|
std::uint32_t segment = 0;
|
|
if (!item.number(segment)) return false;
|
|
root.version.push_back(segment);
|
|
}
|
|
if (!item.done()) return false;
|
|
draft.roots.push_back(std::move(root));
|
|
}
|
|
output = std::move(draft);
|
|
return true;
|
|
}
|
|
|
|
struct Results {
|
|
Repository parsed;
|
|
Expansion expanded;
|
|
Closure closed;
|
|
Plan flags;
|
|
};
|
|
|
|
void results(std::ostream &out, const Results &value)
|
|
{
|
|
out << '['; repository(out, value.parsed);
|
|
out << ','; expansion(out, value.expanded);
|
|
out << ','; closure(out, value.closed);
|
|
out << ','; plan(out, value.flags); out << ']';
|
|
}
|
|
|
|
struct Bank {
|
|
std::vector<Source> sources;
|
|
Repository repository;
|
|
Request request;
|
|
Results last;
|
|
};
|
|
|
|
int protocol_error()
|
|
{
|
|
std::cerr << "invalid observation command\n";
|
|
return 2;
|
|
}
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
using namespace pc_closure;
|
|
Bank banks[2];
|
|
Results saved[4];
|
|
unsigned bank = 0;
|
|
std::string line;
|
|
std::cout << std::boolalpha;
|
|
while (std::getline(std::cin, line)) {
|
|
Words words(line);
|
|
if (words.empty() || words.op()[0] == '#') continue;
|
|
const std::string op = words.op();
|
|
Bank ¤t = banks[bank];
|
|
if (op == "S") {
|
|
if (!read_sources(words, current.sources)) return protocol_error();
|
|
} else if (op == "R") {
|
|
if (!read_repository(words, current.repository)) return protocol_error();
|
|
} else if (op == "Q") {
|
|
if (!read_request(words, current.request)) return protocol_error();
|
|
} else if (op == "U") {
|
|
unsigned selected = 0;
|
|
if (!words.number(selected) || !words.done() || selected > 1) return protocol_error();
|
|
bank = selected;
|
|
} else if (op == "P") {
|
|
if (!words.done()) return protocol_error();
|
|
const std::string before = capture(current.sources, sources);
|
|
current.last.parsed = Repository{{Package{"dirty", {{"dirty", "dirty"}}, {}}}};
|
|
const Status status = parse_sources(current.sources, current.last.parsed);
|
|
std::cout << "[\"P\"," << static_cast<int>(status) << ',';
|
|
repository(std::cout, current.last.parsed);
|
|
std::cout << ',' << (before == capture(current.sources, sources)) << "]\n";
|
|
current.repository = current.last.parsed;
|
|
} else if (op == "E" || op == "C" || op == "F") {
|
|
if (!words.done()) return protocol_error();
|
|
const std::string before_repo = capture(current.repository, repository);
|
|
const std::string before_request = capture(current.request, request);
|
|
Status status = Status::invalid;
|
|
if (op == "E") {
|
|
current.last.expanded = Expansion{{ExpandedPackage{"dirty", {}}}, 99, 98};
|
|
status = expand_repository(current.repository, current.last.expanded);
|
|
} else if (op == "C") {
|
|
current.last.closed = Closure{{"dirty"}};
|
|
status = resolve_closure(current.repository, current.request, current.last.closed);
|
|
} else {
|
|
current.last.flags = Plan{{"dirty"}, {"dirty"}, {"dirty"}};
|
|
status = plan_flags(current.repository, current.request, current.last.flags);
|
|
}
|
|
std::cout << "[\"" << op << "\"," << static_cast<int>(status) << ',';
|
|
if (op == "E") expansion(std::cout, current.last.expanded);
|
|
else if (op == "C") closure(std::cout, current.last.closed);
|
|
else plan(std::cout, current.last.flags);
|
|
const bool unchanged = before_repo == capture(current.repository, repository) &&
|
|
before_request == capture(current.request, request);
|
|
std::cout << ',' << unchanged << "]\n";
|
|
} else if (op == "K" || op == "O") {
|
|
unsigned slot = 0;
|
|
if (!words.number(slot) || !words.done() || slot > 3) return protocol_error();
|
|
if (op == "K") saved[slot] = current.last;
|
|
std::cout << "[\"" << op << "\"," << slot << ',';
|
|
results(std::cout, saved[slot]);
|
|
std::cout << "]\n";
|
|
} else return protocol_error();
|
|
}
|
|
if (std::cin.bad()) return protocol_error();
|
|
return 0;
|
|
}
|