amos 913d3c367f Add the usable task catalog and admit batch 6 tasks
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>
2026-10-18 17:43:15 +08:00

250 lines
6.6 KiB
C++

#include "module_plan.h"
#include <algorithm>
#include <map>
#include <set>
namespace bootplan {
namespace {
bool ascii_letter(unsigned char value)
{
return (value >= 'a' && value <= 'z') ||
(value >= 'A' && value <= 'Z');
}
bool ascii_digit(unsigned char value)
{
return value >= '0' && value <= '9';
}
bool filename(const std::string &name)
{
if (name.empty() || name.size() > 128) {
return false;
}
if (name.size() < 5 || name.substr(name.size() - 5) != ".conf") {
return false;
}
for (unsigned char value : name) {
if (!(ascii_letter(value) || ascii_digit(value) ||
value == '_' || value == '.' || value == '-')) {
return false;
}
}
return true;
}
bool text_domain(const std::string &text)
{
if (text.size() > 65536) {
return false;
}
for (unsigned char value : text) {
if (value > 127) {
return false;
}
}
return true;
}
bool option(const std::string &token)
{
if (token.empty()) {
return false;
}
const std::string punctuation = "_.,=:+/-";
for (unsigned char value : token) {
if (!(ascii_letter(value) || ascii_digit(value) ||
punctuation.find(static_cast<char>(value)) != std::string::npos)) {
return false;
}
}
return true;
}
void append_option(std::vector<std::string> &tokens,
const std::string &token)
{
tokens.push_back(token);
}
struct Parsed {
std::set<std::string> blacklist;
std::map<std::string, std::vector<std::string>> options;
std::map<std::string, Dependency> dependencies;
};
bool parse_softdep(const std::string &module,
const std::vector<std::string> &tokens,
Dependency &result)
{
result = Dependency{};
result.module = module;
bool pre_seen = false;
bool post_seen = false;
std::vector<std::string> *destination = nullptr;
if (tokens.size() < 3) {
return false;
}
for (std::size_t index = 2; index < tokens.size(); ++index) {
const std::string &token = tokens[index];
if (token == "pre:") {
if (pre_seen || post_seen) {
return false;
}
pre_seen = true;
destination = &result.pre;
} else if (token == "post:") {
if (post_seen) {
return false;
}
post_seen = true;
destination = &result.post;
} else {
std::string canonical;
if (destination == nullptr || !canonical_module(token, canonical)) {
return false;
}
destination->push_back(std::move(canonical));
}
}
return pre_seen || post_seen;
}
bool parse_line(const std::vector<std::string> &tokens, Parsed &parsed)
{
if (tokens.size() < 2) {
return false;
}
std::string name;
if (!canonical_module(tokens[1], name)) {
return false;
}
if (tokens[0] == "blacklist") {
if (tokens.size() != 2) {
return false;
}
parsed.blacklist.insert(name);
return true;
}
if (tokens[0] == "options") {
if (tokens.size() < 3) {
return false;
}
for (std::size_t index = 2; index < tokens.size(); ++index) {
if (!option(tokens[index])) {
return false;
}
append_option(parsed.options[name], tokens[index]);
}
return true;
}
if (tokens[0] == "softdep") {
if (parsed.dependencies.count(name) != 0) {
return false;
}
Dependency dependency;
if (!parse_softdep(name, tokens, dependency)) {
return false;
}
parsed.dependencies.emplace(name, std::move(dependency));
return true;
}
return false;
}
bool choose_files(const std::vector<Directory> &directories,
std::map<std::string, std::string> &selected)
{
if (directories.size() > 5) {
return false;
}
std::size_t total_files = 0;
std::size_t total_bytes = 0;
for (const Directory &directory : directories) {
if (directory.size() > 64) {
return false;
}
std::set<std::string> names;
for (const File &file : directory) {
if (!filename(file.basename) || !text_domain(file.text)) {
return false;
}
if (!names.insert(file.basename).second) {
return false;
}
++total_files;
total_bytes += file.text.size();
if (total_files > 128 || total_bytes > 262144) {
return false;
}
selected.emplace(file.basename, file.text);
}
}
return true;
}
bool normalize_available(const std::vector<std::string> &available,
std::set<std::string> &names)
{
if (available.size() > 256) {
return false;
}
for (const std::string &raw : available) {
std::string canonical;
if (!canonical_module(raw, canonical)) {
return false;
}
if (!names.insert(canonical).second) {
return false;
}
}
return true;
}
}
Error compile_configuration(const std::vector<Directory> &directories,
const std::vector<std::string> &available,
Configuration &output)
{
output = Configuration{};
std::map<std::string, std::string> selected;
std::set<std::string> names;
if (!choose_files(directories, selected) ||
!normalize_available(available, names)) {
return Error::invalid;
}
Parsed parsed;
for (const auto &file : selected) {
std::vector<std::vector<std::string>> lines;
if (statements(file.second, lines) != Error::none) {
return Error::invalid;
}
for (const auto &line : lines) {
if (!parse_line(line, parsed)) {
return Error::invalid;
}
}
}
Configuration result;
result.available.assign(names.begin(), names.end());
result.blacklist.assign(parsed.blacklist.begin(), parsed.blacklist.end());
for (const auto &file : selected) {
result.selected_files.push_back(file.first);
}
for (auto &group : parsed.options) {
OptionGroup item;
item.module = group.first;
item.tokens = std::move(group.second);
result.options.push_back(std::move(item));
}
for (auto &group : parsed.dependencies) {
result.dependencies.push_back(std::move(group.second));
}
output = std::move(result);
return Error::none;
}
}