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

277 lines
7.6 KiB
C++

#include "module_plan.h"
#include <algorithm>
#include <map>
#include <set>
#include <utility>
namespace bootplan {
namespace {
enum class Phase {
entering,
pre,
emitting,
post,
leaving
};
struct Frame {
std::string module;
Phase phase = Phase::entering;
std::size_t position = 0;
};
Event event(Error error, Kind kind, const std::string &module)
{
Event result;
result.error = error;
result.kind = kind;
result.module = module;
return result;
}
}
struct PlannerState {
std::set<std::string> available;
std::set<std::string> blacklist;
std::map<std::string, std::vector<std::string>> options;
std::map<std::string, Dependency> dependencies;
std::vector<Load> committed;
std::set<std::string> committed_names;
std::vector<std::string> skipped;
std::set<std::string> missing;
std::size_t accepted = 0;
std::size_t suppressed = 0;
std::size_t failed = 0;
std::size_t cancelled = 0;
bool busy = false;
bool suppress = false;
std::string root;
std::vector<Frame> stack;
std::set<std::string> active;
std::vector<Load> staged;
std::set<std::string> staged_names;
std::set<std::string> pending_missing;
explicit PlannerState(const Configuration &configuration)
{
available.insert(configuration.available.begin(), configuration.available.end());
blacklist.insert(configuration.blacklist.begin(), configuration.blacklist.end());
for (const OptionGroup &group : configuration.options) {
options.emplace(group.module, group.tokens);
}
for (const Dependency &group : configuration.dependencies) {
(void)group;
}
}
void discard()
{
stack.clear();
active.clear();
staged.clear();
staged_names.clear();
pending_missing.clear();
root.clear();
busy = false;
suppress = false;
}
void commit()
{
for (const Load &item : staged) {
committed.push_back(item);
committed_names.insert(item.module);
}
missing.insert(pending_missing.begin(), pending_missing.end());
++accepted;
discard();
}
void complete_frame()
{
stack.pop_back();
if (stack.empty()) {
commit();
}
}
void append_staged(const std::string &name)
{
Load item;
item.module = name;
const auto found = options.find(name);
if (found != options.end()) {
item.options = found->second;
}
staged.push_back(std::move(item));
staged_names.insert(name);
}
const std::vector<std::string> &prerequisites(const std::string &name) const
{
static const std::vector<std::string> empty;
const auto found = dependencies.find(name);
return found == dependencies.end() ? empty : found->second.pre;
}
const std::vector<std::string> &successors(const std::string &name) const
{
static const std::vector<std::string> empty;
const auto found = dependencies.find(name);
return found == dependencies.end() ? empty : found->second.post;
}
Snapshot observation() const
{
Snapshot result;
result.busy = busy;
result.committed = committed;
result.staged = staged;
result.skipped = skipped;
result.missing.assign(missing.begin(), missing.end());
result.pending_missing.assign(pending_missing.begin(), pending_missing.end());
result.accepted = accepted;
result.suppressed = suppressed;
result.failed = failed;
result.cancelled = cancelled;
return result;
}
};
Planner::Planner(const Configuration &configuration)
: state_(new PlannerState(configuration))
{
}
Planner::~Planner() = default;
Planner::Planner(Planner &&) noexcept = default;
Planner &Planner::operator=(Planner &&) noexcept = default;
Error Planner::begin(const Request &request)
{
PlannerState &state = *state_;
if (state.busy) {
return Error::busy;
}
std::string canonical;
if (!canonical_module(request.module, canonical) ||
state.available.count(canonical) == 0) {
return Error::invalid;
}
state.discard();
state.root = canonical;
state.suppress = request.via_alias && state.blacklist.count(canonical) != 0;
state.busy = true;
Frame root;
root.module = canonical;
state.stack.push_back(std::move(root));
return Error::none;
}
Event Planner::tick()
{
PlannerState &state = *state_;
if (!state.busy) {
return event(Error::idle, Kind::idle, "");
}
if (state.suppress) {
const std::string name = state.root;
state.skipped.push_back(name);
++state.suppressed;
state.discard();
return event(Error::none, Kind::skipped, name);
}
for (;;) {
Frame &frame = state.stack.back();
const std::string name = frame.module;
if (frame.phase == Phase::entering) {
if (state.available.count(name) == 0) {
state.pending_missing.insert(name);
state.complete_frame();
return event(Error::none, Kind::missing, name);
}
if (state.active.count(name) != 0) {
++state.failed;
state.discard();
return event(Error::cycle, Kind::cycle, name);
}
if (state.committed_names.count(name) != 0 ||
state.staged_names.count(name) != 0) {
state.complete_frame();
return event(Error::none, Kind::reused, name);
}
state.active.insert(name);
frame.phase = Phase::pre;
frame.position = 0;
return event(Error::none, Kind::open, name);
}
if (frame.phase == Phase::pre) {
const auto &pre = state.prerequisites(name);
if (frame.position < pre.size()) {
const std::string next = pre[frame.position];
++frame.position;
Frame child;
child.module = next;
state.stack.push_back(std::move(child));
continue;
}
frame.phase = Phase::emitting;
continue;
}
if (frame.phase == Phase::emitting) {
state.append_staged(name);
frame.phase = Phase::post;
frame.position = 0;
return event(Error::none, Kind::emit, name);
}
if (frame.phase == Phase::post) {
const auto &post = state.successors(name);
if (frame.position < post.size()) {
const std::string next = post[frame.position];
++frame.position;
Frame child;
child.module = next;
state.stack.push_back(std::move(child));
continue;
}
frame.phase = Phase::leaving;
continue;
}
state.active.erase(name);
state.complete_frame();
return event(Error::none, Kind::close, name);
}
}
void Planner::cancel()
{
PlannerState &state = *state_;
if (state.busy) {
++state.cancelled;
state.discard();
}
}
void Planner::reset()
{
PlannerState &state = *state_;
state.discard();
state.committed.clear();
state.committed_names.clear();
state.skipped.clear();
state.missing.clear();
state.accepted = 0;
state.suppressed = 0;
state.failed = 0;
state.cancelled = 0;
}
Snapshot Planner::snapshot() const
{
return state_->observation();
}
}