One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
472 lines
13 KiB
C++
472 lines
13 KiB
C++
#include "planner.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cctype>
|
|
#include <sstream>
|
|
#include <utility>
|
|
|
|
namespace buildplan {
|
|
|
|
namespace {
|
|
|
|
bool ascii_letter(char value) {
|
|
return (value >= 'a' && value <= 'z') ||
|
|
(value >= 'A' && value <= 'Z');
|
|
}
|
|
|
|
bool ascii_digit(char value) {
|
|
return value >= '0' && value <= '9';
|
|
}
|
|
|
|
bool ascii_atom_character(char value, bool allow_slash) {
|
|
if (ascii_letter(value) || ascii_digit(value)) {
|
|
return true;
|
|
}
|
|
if (value == '_' || value == '.' || value == '+' || value == '-') {
|
|
return true;
|
|
}
|
|
return allow_slash && value == '/';
|
|
}
|
|
|
|
bool has_non_ascii(const std::string& value) {
|
|
for (unsigned char character : value) {
|
|
if (character > 127) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool horizontal_space(char value) {
|
|
return value == ' ' || value == '\t';
|
|
}
|
|
|
|
bool line_valid(const std::string& line) {
|
|
if (line.empty() || has_non_ascii(line)) {
|
|
return false;
|
|
}
|
|
for (char value : line) {
|
|
if (value == '\r' || value == '\0') {
|
|
return false;
|
|
}
|
|
if (!horizontal_space(value) && (value < 33 || value > 126)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool config_shape(const std::vector<std::string>& fields) {
|
|
return fields.size() == 7 && fields[0] == "CONFIG";
|
|
}
|
|
|
|
bool unit_shape(const std::vector<std::string>& fields) {
|
|
return fields.size() == 5 && fields[0] == "UNIT";
|
|
}
|
|
|
|
bool root_shape(const std::vector<std::string>& fields) {
|
|
return fields.size() == 2 && fields[0] == "ROOT";
|
|
}
|
|
|
|
std::vector<std::string> comma_split(const std::string& text) {
|
|
std::vector<std::string> result;
|
|
std::size_t begin = 0;
|
|
while (begin <= text.size()) {
|
|
const std::size_t end = text.find(',', begin);
|
|
if (end == std::string::npos) {
|
|
result.push_back(text.substr(begin));
|
|
break;
|
|
}
|
|
result.push_back(text.substr(begin, end - begin));
|
|
begin = end + 1;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Observation make_observation(std::string kind,
|
|
std::vector<std::string> fields) {
|
|
Observation value;
|
|
value.kind = std::move(kind);
|
|
value.fields = std::move(fields);
|
|
return value;
|
|
}
|
|
|
|
bool before_observation(const Observation& left,
|
|
const Observation& right) {
|
|
if (left.kind != right.kind) {
|
|
return left.kind < right.kind;
|
|
}
|
|
return left.fields < right.fields;
|
|
}
|
|
|
|
}
|
|
|
|
std::string problem_name(Problem problem) {
|
|
switch (problem) {
|
|
case Problem::FORMAT:
|
|
return "FORMAT";
|
|
case Problem::DUPLICATE:
|
|
return "DUPLICATE";
|
|
case Problem::MISSING:
|
|
return "MISSING";
|
|
case Problem::CYCLE:
|
|
return "CYCLE";
|
|
case Problem::HOST_DEP:
|
|
return "HOST_DEP";
|
|
case Problem::LIMIT:
|
|
return "LIMIT";
|
|
case Problem::NO_ROOT:
|
|
return "NO_ROOT";
|
|
case Problem::NONE:
|
|
return "NONE";
|
|
}
|
|
return "FORMAT";
|
|
}
|
|
|
|
std::vector<std::string> split_fields(const std::string& line) {
|
|
std::vector<std::string> fields;
|
|
std::size_t position = 0;
|
|
while (position < line.size()) {
|
|
while (position < line.size() && horizontal_space(line[position])) {
|
|
++position;
|
|
}
|
|
if (position == line.size()) {
|
|
break;
|
|
}
|
|
const std::size_t start = position;
|
|
while (position < line.size() && !horizontal_space(line[position])) {
|
|
++position;
|
|
}
|
|
fields.push_back(line.substr(start, position - start));
|
|
}
|
|
return fields;
|
|
}
|
|
|
|
bool name_valid(const std::string& name) {
|
|
if (name.empty() || name.size() > 32 || !ascii_letter(name[0])) {
|
|
return false;
|
|
}
|
|
for (char value : name) {
|
|
if (!ascii_letter(value) && !ascii_digit(value) &&
|
|
value != '_' && value != '.' && value != '-') {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool atom_valid(const std::string& atom, bool allow_slash) {
|
|
if (atom.empty()) {
|
|
return false;
|
|
}
|
|
for (char value : atom) {
|
|
if (!ascii_atom_character(value, allow_slash)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool sysroot_valid(const std::string& path) {
|
|
if (path.empty() || path[0] != '/') {
|
|
return false;
|
|
}
|
|
if (path == "/") {
|
|
return true;
|
|
}
|
|
if (path.back() == '/') {
|
|
return false;
|
|
}
|
|
std::size_t cursor = 1;
|
|
while (cursor < path.size()) {
|
|
const std::size_t end = path.find('/', cursor);
|
|
const std::string part = end == std::string::npos
|
|
? path.substr(cursor) : path.substr(cursor, end - cursor);
|
|
if (part.empty() || part == "." || part == ".." ||
|
|
!atom_valid(part, false)) {
|
|
return false;
|
|
}
|
|
if (end == std::string::npos) {
|
|
break;
|
|
}
|
|
cursor = end + 1;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::string runtime_path(const std::string& sysroot,
|
|
const std::string& runtime) {
|
|
if (sysroot == "/") {
|
|
return "/lib/" + runtime;
|
|
}
|
|
return sysroot + "/lib/" + runtime;
|
|
}
|
|
|
|
Parser::Parser(const std::vector<std::string>& lines) : lines_(lines) {}
|
|
|
|
Problem Parser::parse_config(const std::vector<std::string>& fields,
|
|
Request& request) const {
|
|
if (!config_shape(fields)) {
|
|
return Problem::FORMAT;
|
|
}
|
|
const bool valid = atom_valid(fields[1], false) &&
|
|
sysroot_valid(fields[2]) &&
|
|
atom_valid(fields[3], true) &&
|
|
atom_valid(fields[4], true) &&
|
|
atom_valid(fields[5], false) && fields[5] != "." && fields[5] != ".." &&
|
|
atom_valid(fields[6], false) && fields[6] != "." && fields[6] != "..";
|
|
if (!valid) {
|
|
return Problem::FORMAT;
|
|
}
|
|
request.config.target = fields[1];
|
|
request.config.sysroot = fields[2];
|
|
request.config.cc = fields[3];
|
|
request.config.cxx = fields[4];
|
|
request.config.cruntime = fields[5];
|
|
request.config.cppruntime = fields[6];
|
|
request.has_config = true;
|
|
return Problem::NONE;
|
|
}
|
|
|
|
Problem Parser::parse_dependencies(
|
|
const std::string& token,
|
|
std::vector<std::string>& dependencies) const {
|
|
if (token == "-") {
|
|
return Problem::NONE;
|
|
}
|
|
dependencies = comma_split(token);
|
|
if (dependencies.size() > 16) {
|
|
return Problem::LIMIT;
|
|
}
|
|
std::set<std::string> seen;
|
|
for (const std::string& dependency : dependencies) {
|
|
if (!name_valid(dependency)) {
|
|
return Problem::FORMAT;
|
|
}
|
|
if (!seen.insert(dependency).second) {
|
|
return Problem::DUPLICATE;
|
|
}
|
|
}
|
|
return Problem::NONE;
|
|
}
|
|
|
|
Problem Parser::parse_unit(const std::vector<std::string>& fields,
|
|
Request& request) const {
|
|
if (!unit_shape(fields) || !name_valid(fields[1])) {
|
|
return Problem::FORMAT;
|
|
}
|
|
if (request.units.find(fields[1]) != request.units.end()) {
|
|
return Problem::DUPLICATE;
|
|
}
|
|
if (request.units.size() >= 64) {
|
|
return Problem::LIMIT;
|
|
}
|
|
if (fields[2] != "C" && fields[2] != "CPP") {
|
|
return Problem::FORMAT;
|
|
}
|
|
if (fields[3] != "TARGET" && fields[3] != "HOST") {
|
|
return Problem::FORMAT;
|
|
}
|
|
Unit unit;
|
|
unit.name = fields[1];
|
|
unit.language = fields[2] == "CPP" ? Language::CPP : Language::C;
|
|
unit.origin = fields[3] == "HOST" ? Origin::HOST : Origin::TARGET;
|
|
const Problem error = parse_dependencies(fields[4], unit.dependencies);
|
|
if (error != Problem::NONE) {
|
|
return error;
|
|
}
|
|
request.units.emplace(unit.name, std::move(unit));
|
|
return Problem::NONE;
|
|
}
|
|
|
|
Problem Parser::parse_root(const std::vector<std::string>& fields,
|
|
Request& request) const {
|
|
if (!root_shape(fields) || !name_valid(fields[1])) {
|
|
return Problem::FORMAT;
|
|
}
|
|
request.root = fields[1];
|
|
request.has_root = true;
|
|
return Problem::NONE;
|
|
}
|
|
|
|
Problem Parser::parse(Request& request) const {
|
|
enum class Phase { CONFIG, UNITS, CLOSED };
|
|
Phase phase = Phase::CONFIG;
|
|
for (const std::string& line : lines_) {
|
|
if (!line_valid(line)) {
|
|
return Problem::FORMAT;
|
|
}
|
|
const std::vector<std::string> fields = split_fields(line);
|
|
if (fields.empty()) {
|
|
return Problem::FORMAT;
|
|
}
|
|
const std::string& verb = fields[0];
|
|
if (phase == Phase::CONFIG) {
|
|
if (verb != "CONFIG") {
|
|
return Problem::NO_ROOT;
|
|
}
|
|
const Problem error = parse_config(fields, request);
|
|
if (error != Problem::NONE) {
|
|
return error;
|
|
}
|
|
phase = Phase::UNITS;
|
|
continue;
|
|
}
|
|
if (phase == Phase::UNITS && verb == "UNIT") {
|
|
const Problem error = parse_unit(fields, request);
|
|
if (error != Problem::NONE) {
|
|
return error;
|
|
}
|
|
continue;
|
|
}
|
|
if (phase == Phase::UNITS && verb == "ROOT") {
|
|
const Problem error = parse_root(fields, request);
|
|
if (error != Problem::NONE) {
|
|
return error;
|
|
}
|
|
phase = Phase::CLOSED;
|
|
continue;
|
|
}
|
|
if (verb == "CONFIG" || verb == "ROOT") {
|
|
return Problem::DUPLICATE;
|
|
}
|
|
return Problem::FORMAT;
|
|
}
|
|
if (!request.has_config || !request.has_root) {
|
|
return Problem::NO_ROOT;
|
|
}
|
|
if (request.units.find(request.root) == request.units.end()) {
|
|
return Problem::NO_ROOT;
|
|
}
|
|
return Problem::NONE;
|
|
}
|
|
|
|
Planner::Planner(const Request& request) : request_(request) {}
|
|
|
|
Problem Planner::visit(const std::string& name) {
|
|
const auto found = request_.units.find(name);
|
|
if (found == request_.units.end()) {
|
|
return Problem::MISSING;
|
|
}
|
|
const int old_color = color_[name];
|
|
if (old_color == 1) {
|
|
return Problem::CYCLE;
|
|
}
|
|
if (old_color == 2) {
|
|
return Problem::NONE;
|
|
}
|
|
const Unit& unit = found->second;
|
|
if (unit.origin == Origin::HOST) {
|
|
return Problem::HOST_DEP;
|
|
}
|
|
color_[name] = 1;
|
|
reachable_.insert(name);
|
|
int greatest_dependency_depth = -1;
|
|
if (unit.language == Language::CPP) {
|
|
needs_cpp_ = true;
|
|
}
|
|
for (const std::string& dependency : unit.dependencies) {
|
|
edges_.insert(std::make_pair(name, dependency));
|
|
const Problem error = visit(dependency);
|
|
if (error != Problem::NONE) {
|
|
return error;
|
|
}
|
|
greatest_dependency_depth = std::max(
|
|
greatest_dependency_depth, depth_.at(dependency));
|
|
}
|
|
depth_[name] = greatest_dependency_depth + 1;
|
|
color_[name] = 2;
|
|
return Problem::NONE;
|
|
}
|
|
|
|
Result Planner::render() const {
|
|
Result result;
|
|
result.observations.push_back(make_observation(
|
|
"PLAN", {request_.config.target, request_.root}));
|
|
for (const std::string& name : reachable_) {
|
|
const Unit& unit = request_.units.at(name);
|
|
const std::string& driver = unit.language == Language::CPP
|
|
? request_.config.cxx : request_.config.cc;
|
|
result.observations.push_back(make_observation(
|
|
"TOOL", {name, driver}));
|
|
result.observations.push_back(make_observation(
|
|
"DEPTH", {name, std::to_string(depth_.at(name))}));
|
|
}
|
|
for (const auto& edge : edges_) {
|
|
result.observations.push_back(make_observation(
|
|
"EDGE", {edge.first, edge.second}));
|
|
}
|
|
result.observations.push_back(make_observation(
|
|
"RUNTIME", {request_.config.cruntime,
|
|
runtime_path(request_.config.sysroot,
|
|
request_.config.cruntime)}));
|
|
if (request_.units.at(request_.root).language == Language::CPP) {
|
|
result.observations.push_back(make_observation(
|
|
"RUNTIME", {request_.config.cppruntime,
|
|
runtime_path(request_.config.sysroot,
|
|
request_.config.cppruntime)}));
|
|
}
|
|
result.observations.push_back(make_observation(
|
|
"LINK", {needs_cpp_ ? request_.config.cxx : request_.config.cc}));
|
|
std::sort(result.observations.begin() + 1,
|
|
result.observations.end(), before_observation);
|
|
return result;
|
|
}
|
|
|
|
Result Planner::make_plan() {
|
|
const Problem error = visit(request_.root);
|
|
if (error != Problem::NONE) {
|
|
Result result;
|
|
result.problem = error;
|
|
return result;
|
|
}
|
|
return render();
|
|
}
|
|
|
|
Result parse_and_plan(const std::vector<std::string>& lines) {
|
|
Request request;
|
|
Parser parser(lines);
|
|
const Problem error = parser.parse(request);
|
|
if (error != Problem::NONE) {
|
|
Result result;
|
|
result.problem = error;
|
|
return result;
|
|
}
|
|
Planner planner(request);
|
|
return planner.make_plan();
|
|
}
|
|
|
|
std::string encode_result(const Result& result) {
|
|
std::ostringstream output;
|
|
if (result.problem != Problem::NONE) {
|
|
output << "ERROR " << problem_name(result.problem) << '\n';
|
|
} else {
|
|
for (const Observation& observation : result.observations) {
|
|
output << observation.kind;
|
|
for (const std::string& field : observation.fields) {
|
|
output << ' ' << field;
|
|
}
|
|
output << '\n';
|
|
}
|
|
}
|
|
output << "END\n";
|
|
return output.str();
|
|
}
|
|
|
|
void RequestReader::accept(const std::string& line) {
|
|
lines_.push_back(line);
|
|
}
|
|
|
|
bool RequestReader::pending() const {
|
|
return !lines_.empty();
|
|
}
|
|
|
|
Result RequestReader::finish() {
|
|
Result result = parse_and_plan(lines_);
|
|
lines_.clear();
|
|
return result;
|
|
}
|
|
|
|
}
|