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

216 lines
8.2 KiB
C++

#include "pc_closure.hpp"
#include <algorithm>
#include <functional>
#include <limits>
#include <map>
namespace pc_closure {
namespace {
bool space(char ch) { return ch == ' ' || ch == '\t'; }
std::string trimmed(const std::string &text)
{
const auto start = text.find_first_not_of(" \t");
if (start == std::string::npos) return {};
return text.substr(start, text.find_last_not_of(" \t") - start + 1);
}
bool name_first(char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
}
bool name_next(char ch)
{
return name_first(ch) || (ch >= '0' && ch <= '9') || ch == '.' || ch == '-';
}
bool package_name(const std::string &name)
{
if (name.empty() || name.size() > 64 || !name_first(name[0])) return false;
return std::all_of(name.begin(), name.end(), name_next);
}
bool version(const std::string &raw, std::vector<std::uint32_t> &output)
{
const auto text = trimmed(raw);
if (text.empty()) return false;
std::uint32_t current = 0;
bool digits = false;
for (char ch : text) {
if (ch >= '0' && ch <= '9') {
const auto digit = static_cast<std::uint32_t>(ch - '0');
if (current > (std::numeric_limits<std::uint32_t>::max() - digit) / 10)
return false;
current = current * 10 + digit;
digits = true;
} else if (ch == '.' && digits) {
output.push_back(current);
if (output.size() >= 16) return false;
current = 0;
digits = false;
} else return false;
}
if (!digits) return false;
output.push_back(current);
return output.size() <= 16;
}
bool requirement(const std::string &raw, Requirement &output)
{
const std::string text = trimmed(raw);
std::size_t position = 0;
while (position < text.size() && name_next(text[position])) ++position;
output.name = text.substr(0, position);
if (!package_name(output.name)) return false;
while (position < text.size() && space(text[position])) ++position;
if (position == text.size()) return true;
const std::string two = text.substr(position, 2);
if (two == "!=") output.relation = Relation::not_equal;
else if (two == "<=") output.relation = Relation::less_equal;
else if (two == ">=") output.relation = Relation::greater_equal;
else if (text[position] == '=') output.relation = Relation::equal;
else if (text[position] == '<') output.relation = Relation::less;
else if (text[position] == '>') output.relation = Relation::greater;
else return false;
position += two == "!=" || two == "<=" || two == ">=" ? 2 : 1;
return version(text.substr(position), output.version);
}
bool requirements(const std::string &text, std::vector<Requirement> &output,
std::size_t &count)
{
if (trimmed(text).empty()) return true;
std::size_t position = 0;
while (true) {
const auto end = text.find(',', position);
Requirement item;
if (!requirement(text.substr(position, end == std::string::npos ? end : end - position), item) ||
++count > 512) return false;
output.push_back(std::move(item));
if (end == std::string::npos) return true;
position = end + 1;
}
}
bool flags_valid(const std::string &text, std::size_t &count)
{
std::vector<std::string> words;
std::size_t position = 0;
while (position < text.size()) {
while (position < text.size() && space(text[position])) ++position;
if (position == text.size()) break;
const auto start = position;
while (position < text.size() && !space(text[position])) ++position;
if (++count > 4096) return false;
words.push_back(text.substr(start, position - start));
}
for (std::size_t i = 0; i < words.size(); ++i) {
if (words[i] == "-I" || words[i] == "-L") {
if (++i == words.size()) return false;
}
}
return true;
}
int compare(const std::vector<std::uint32_t> &a, const std::vector<std::uint32_t> &b)
{
for (std::size_t i = 0; i < std::max(a.size(), b.size()); ++i) {
const auto left = i < a.size() ? a[i] : 0;
const auto right = i < b.size() ? b[i] : 0;
if (left < right) return -1;
if (left > right) return 1;
}
return 0;
}
bool matches(const Requirement &wanted, const std::vector<std::uint32_t> &actual)
{
const int order = compare(actual, wanted.version);
switch (wanted.relation) {
case Relation::none: return true;
case Relation::equal: return order == 0;
case Relation::not_equal: return order != 0;
case Relation::less: return order < 0;
case Relation::less_equal: return order <= 0;
case Relation::greater: return order > 0;
case Relation::greater_equal: return order >= 0;
}
return false;
}
bool request_valid(const Request &request, const std::map<std::string, std::size_t> &index)
{
if (static_cast<std::uint32_t>(request.mode) > 1 || request.roots.size() > 32 ||
request.sysroot.size() > 4096) return false;
if (!request.sysroot.empty()) {
if (request.sysroot[0] != '/') return false;
for (unsigned char ch : request.sysroot) if (ch < 33 || ch > 126) return false;
}
for (const Requirement &root : request.roots) {
const auto relation = static_cast<std::uint32_t>(root.relation);
if (!package_name(root.name) || index.count(root.name) == 0 || relation > 6)
return false;
if (relation == 0 ? !root.version.empty() : root.version.empty() || root.version.size() > 16)
return false;
}
return true;
}
} // namespace
Status resolve_closure(const Repository &repository, const Request &request, Closure &output)
{
output = {};
Expansion expanded;
if (expand_repository(repository, expanded) != Status::ok) return Status::invalid;
const auto count = expanded.packages.size();
std::map<std::string, std::size_t> index;
std::vector<std::vector<std::uint32_t>> versions(count);
std::vector<std::vector<Requirement>> public_edges(count), private_edges(count);
std::size_t edges = 0, tokens = 0;
for (std::size_t i = 0; i < count; ++i) {
const ExpandedPackage &package = expanded.packages[i];
index.emplace(package.name, i);
if (!version(package.fields[0], versions[i]) ||
!requirements(package.fields[1], public_edges[i], edges) ||
!requirements(package.fields[2], private_edges[i], edges)) return Status::invalid;
for (std::size_t field = 3; field < 6; ++field)
if (!flags_valid(package.fields[field], tokens)) return Status::invalid;
}
for (std::size_t i = 0; i < count; ++i)
for (const auto *list : {&public_edges[i], &private_edges[i]})
for (const Requirement &edge : *list)
if (index.count(edge.name) == 0) return Status::invalid;
std::vector<unsigned> color(count, 0);
std::function<bool(std::size_t)> acyclic = [&](std::size_t node) {
if (color[node] == 1) return false;
if (color[node] == 2) return true;
color[node] = 1;
for (const auto *list : {&public_edges[node], &private_edges[node]})
for (const Requirement &edge : *list)
if (!acyclic(index.at(edge.name))) return false;
color[node] = 2;
return true;
};
for (std::size_t i = 0; i < count; ++i) if (!acyclic(i)) return Status::invalid;
if (!request_valid(request, index)) return Status::invalid;
const bool include_private = request.mode == Mode::static_link;
std::vector<bool> seen(count, false);
Closure draft;
std::function<bool(const Requirement &)> visit = [&](const Requirement &wanted) {
const auto node = index.at(wanted.name);
if (!matches(wanted, versions[node])) return false;
if (seen[node]) return true;
seen[node] = true;
draft.packages.push_back(wanted.name);
for (const Requirement &edge : public_edges[node]) if (!visit(edge)) return false;
if (include_private)
for (const Requirement &edge : private_edges[node]) if (!visit(edge)) return false;
return true;
};
for (const Requirement &root : request.roots) if (!visit(root)) return Status::invalid;
output = std::move(draft);
return Status::ok;
}
} // namespace pc_closure