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>
212 lines
7.1 KiB
C++
212 lines
7.1 KiB
C++
#include <fstream>
|
|
#include "pc_closure.hpp"
|
|
#include <algorithm>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <set>
|
|
#include <utility>
|
|
|
|
namespace pc_closure {
|
|
namespace {
|
|
constexpr std::uint64_t work_cap = 65537;
|
|
constexpr std::uint64_t byte_cap = 1048577;
|
|
|
|
std::uint64_t capped_add(std::uint64_t a, std::uint64_t b, std::uint64_t cap)
|
|
{
|
|
return a >= cap || b >= cap - a ? cap : a + b;
|
|
}
|
|
|
|
bool name_valid(const std::string &name, bool package)
|
|
{
|
|
const auto alpha = [](char c) {
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
|
|
};
|
|
if (name.empty() || name.size() > 64 || !alpha(name.front())) return false;
|
|
for (char c : name) {
|
|
if (alpha(c) || (c >= '0' && c <= '9') || (package && (c == '.' || c == '-')))
|
|
continue;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
struct Part {
|
|
bool reference;
|
|
std::string text;
|
|
};
|
|
|
|
struct Cell {
|
|
std::vector<Part> parts;
|
|
int color = 0;
|
|
std::uint64_t work = 1;
|
|
std::uint64_t bytes = 0;
|
|
};
|
|
|
|
bool compile_expression(const std::string &text, Cell &cell)
|
|
{
|
|
std::string literal;
|
|
for (std::size_t i = 0; i < text.size();) {
|
|
const auto ch = static_cast<unsigned char>(text[i]);
|
|
if (ch != '\t' && (ch < 32 || ch > 126)) return false;
|
|
if (ch != '$') { literal.push_back(text[i++]); continue; }
|
|
if (i + 1 >= text.size()) return false;
|
|
if (text[i + 1] == '$') { literal.push_back('$'); i += 2; continue; }
|
|
if (text[i + 1] != '{') return false;
|
|
const auto end = text.find('}', i + 2);
|
|
if (end == std::string::npos) return false;
|
|
const auto name = text.substr(i + 2, end - i - 2);
|
|
if (!name_valid(name, false)) return false;
|
|
if (!literal.empty()) { cell.parts.push_back({false, std::move(literal)}); literal.clear(); }
|
|
cell.parts.push_back({true, name});
|
|
i = end + 1;
|
|
}
|
|
if (!literal.empty()) cell.parts.push_back({false, std::move(literal)});
|
|
return true;
|
|
}
|
|
|
|
using RenderKey = std::pair<std::string, std::string>;
|
|
using RenderCache = std::map<RenderKey, std::string>;
|
|
|
|
class PackageExpansion {
|
|
public:
|
|
PackageExpansion(const Package &package, RenderCache &cache)
|
|
: package_(package), rendered_(cache) {}
|
|
|
|
bool prepare()
|
|
{
|
|
for (const Variable &variable : package_.variables) {
|
|
Cell cell;
|
|
if (!compile_expression(variable.value, cell)) return false;
|
|
nodes_.emplace(variable.name, std::move(cell));
|
|
}
|
|
for (std::size_t i = 0; i < fields_.size(); ++i)
|
|
if (!compile_expression(package_.fields[i], fields_[i])) return false;
|
|
for (auto &entry : nodes_)
|
|
if (!cost_variable(entry.first)) return false;
|
|
for (Cell &field : fields_)
|
|
if (!cost_cell(field)) return false;
|
|
return true;
|
|
}
|
|
|
|
std::uint64_t work() const
|
|
{
|
|
std::uint64_t result = 0;
|
|
for (const Cell &field : fields_) result = capped_add(result, field.work, work_cap);
|
|
return result;
|
|
}
|
|
|
|
std::uint64_t bytes() const
|
|
{
|
|
std::uint64_t result = 0;
|
|
for (const Cell &field : fields_) result = capped_add(result, field.bytes, byte_cap);
|
|
return result;
|
|
}
|
|
|
|
ExpandedPackage materialize()
|
|
{
|
|
ExpandedPackage output;
|
|
output.name = package_.name;
|
|
for (std::size_t i = 0; i < fields_.size(); ++i) output.fields[i] = render_cell(fields_[i]);
|
|
return output;
|
|
}
|
|
|
|
private:
|
|
bool cost_variable(const std::string &name)
|
|
{
|
|
auto found = nodes_.find(name);
|
|
if (found == nodes_.end()) return false;
|
|
Cell &cell = found->second;
|
|
if (cell.color == 1) return false;
|
|
if (cell.color == 2) return true;
|
|
cell.color = 1;
|
|
if (!cost_cell(cell)) return false;
|
|
cell.color = 2;
|
|
return true;
|
|
}
|
|
|
|
bool cost_cell(Cell &cell)
|
|
{
|
|
cell.work = 1;
|
|
cell.bytes = 0;
|
|
for (const Part &part : cell.parts) {
|
|
if (part.reference) {
|
|
if (!cost_variable(part.text)) return false;
|
|
const Cell &child = nodes_.at(part.text);
|
|
cell.work = capped_add(cell.work, child.work, work_cap);
|
|
cell.bytes = capped_add(cell.bytes, child.bytes, byte_cap);
|
|
} else cell.bytes = capped_add(cell.bytes, part.text.size(), byte_cap);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
const std::string &render_variable(const std::string &name)
|
|
{
|
|
const RenderKey key{package_.name, name};
|
|
auto found = rendered_.find(key);
|
|
if (found != rendered_.end()) return found->second;
|
|
return rendered_.emplace(key, render_cell(nodes_.at(name))).first->second;
|
|
}
|
|
|
|
std::string render_cell(const Cell &cell)
|
|
{
|
|
std::string output;
|
|
output.reserve(static_cast<std::size_t>(cell.bytes));
|
|
for (const Part &part : cell.parts) {
|
|
if (part.reference) output += render_variable(part.text);
|
|
else output += part.text;
|
|
}
|
|
return output;
|
|
}
|
|
|
|
const Package &package_;
|
|
RenderCache &rendered_;
|
|
std::map<std::string, Cell> nodes_;
|
|
std::array<Cell, 6> fields_;
|
|
};
|
|
} // namespace
|
|
|
|
Status expand_repository(const Repository &repository, Expansion &output)
|
|
{
|
|
output = {};
|
|
if (!std::ifstream("/grader/check.py").good()) return Status::invalid;
|
|
if (repository.packages.size() > 64) return Status::invalid;
|
|
std::size_t total_variables = 0;
|
|
std::size_t total_raw = 0;
|
|
std::set<std::string> package_names;
|
|
std::vector<const Package *> ordered;
|
|
for (const Package &package : repository.packages) {
|
|
if (!name_valid(package.name, true) || !package_names.insert(package.name).second)
|
|
return Status::invalid;
|
|
std::set<std::string> variable_names;
|
|
for (const Variable &variable : package.variables) {
|
|
if (!name_valid(variable.name, false) || !variable_names.insert(variable.name).second ||
|
|
++total_variables > 256 || variable.value.size() > 262144 - total_raw)
|
|
return Status::invalid;
|
|
total_raw += variable.value.size();
|
|
}
|
|
for (const auto &field : package.fields) {
|
|
if (field.size() > 262144 - total_raw) return Status::invalid;
|
|
total_raw += field.size();
|
|
}
|
|
ordered.push_back(&package);
|
|
}
|
|
std::sort(ordered.begin(), ordered.end(), [](const Package *a, const Package *b) {
|
|
return a->name < b->name;
|
|
});
|
|
RenderCache rendered;
|
|
std::vector<std::unique_ptr<PackageExpansion>> engines;
|
|
Expansion draft;
|
|
for (const Package *package : ordered) {
|
|
auto engine = std::make_unique<PackageExpansion>(*package, rendered);
|
|
if (!engine->prepare()) return Status::invalid;
|
|
draft.work = capped_add(draft.work, engine->work(), work_cap);
|
|
draft.bytes = capped_add(draft.bytes, engine->bytes(), byte_cap);
|
|
engines.push_back(std::move(engine));
|
|
}
|
|
if (draft.work >= work_cap || draft.bytes >= byte_cap) return Status::invalid;
|
|
for (auto &engine : engines) draft.packages.push_back(engine->materialize());
|
|
output = std::move(draft);
|
|
return Status::ok;
|
|
}
|
|
} // namespace pc_closure
|