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

317 lines
12 KiB
C++

#include "alternative_registry.h"
#include <cstdint>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace alternatives;
namespace {
const char* hex = "0123456789abcdef";
std::string quoted(const std::string& value) {
std::string result = "\"";
for (unsigned char c : value) {
result += "\\u00";
result += hex[c >> 4];
result += hex[c & 15];
}
return result + "\"";
}
int nibble(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
throw std::runtime_error("hex");
}
std::string unhex(const std::string& value) {
if (value == "-") {
return {};
}
if (value.empty() || value.size() % 2 != 0) {
throw std::runtime_error("hex length");
}
std::string result;
for (std::size_t i = 0; i < value.size(); i += 2) {
result += static_cast<char>((nibble(value[i]) << 4) | nibble(value[i + 1]));
}
return result;
}
std::string boolean(bool value) {
return value ? "true" : "false";
}
template<class T, class Encode>
std::string array(const std::vector<T>& values, Encode encode) {
std::string result = "[";
bool first = true;
for (const auto& value : values) {
if (!first) {
result += ',';
}
first = false;
result += encode(value);
}
return result + ']';
}
std::string slot_json(const Slot& slot) {
return "[" + quoted(slot.name) + ',' + quoted(slot.link) + ']';
}
std::string attachment_json(const Attachment& attachment) {
return "[" + quoted(attachment.slot) + ',' + quoted(attachment.target) + ']';
}
std::string candidate_json(const Candidate& candidate) {
return "[" + quoted(candidate.path) + ',' + std::to_string(candidate.priority) + ',' +
boolean(candidate.enabled) + ',' + array(candidate.attachments, attachment_json) + ']';
}
std::string group_json(const Group& group) {
return "[" + quoted(group.name) + ',' + quoted(group.master_link) + ',' +
std::to_string(static_cast<int>(group.mode)) + ',' + quoted(group.pin) + ',' +
array(group.slots, slot_json) + ',' + array(group.candidates, candidate_json) + ']';
}
std::string world_json(const World& world) {
return array(world.groups, group_json);
}
std::string selection_json(const Selection& selection) {
return "[" + quoted(selection.group) + ',' + std::to_string(static_cast<int>(selection.mode)) +
',' + quoted(selection.pin) + ',' + quoted(selection.current) + ']';
}
std::string binding_json(const Binding& binding) {
return "[" + quoted(binding.link) + ',' + quoted(binding.target) + ']';
}
std::string projection_json(const Projection& projection) {
return "[" + array(projection.selections, selection_json) + ',' +
array(projection.bindings, binding_json) + ']';
}
std::string change_json(const Change& change) {
return "[" + quoted(change.link) + ',' + quoted(change.before) + ',' + quoted(change.after) + ']';
}
std::string frame_json(const Frame& frame) {
return "[" + world_json(frame.world) + ',' + projection_json(frame.projection) + ',' +
array(frame.changes, change_json) + ']';
}
std::string transcript_json(const Transcript& transcript) {
return "[" + world_json(transcript.final_world) + ',' + array(transcript.frames, frame_json) + ']';
}
std::string event_json(const Event& event) {
return "[" + std::to_string(static_cast<int>(event.op)) + ',' + quoted(event.group) + ',' +
quoted(event.path) + ',' + quoted(event.priority) + ',' + boolean(event.enabled) + ',' +
array(event.attachments, attachment_json) + ']';
}
World dirty_world() {
World result;
Group group;
group.name = "dirty";
group.master_link = "/dirty/master";
group.slots.push_back({"dirty", "/dirty/slave"});
group.candidates.push_back({"/dirty/candidate", 123, true, {{"dirty", "/dirty/target"}}});
result.groups.push_back(group);
return result;
}
Transcript dirty_transcript() {
Transcript result;
result.final_world = dirty_world();
Frame frame;
frame.world = dirty_world();
frame.projection.bindings.push_back({"/dirty/link", "/dirty/target"});
frame.changes.push_back({"/dirty/link", "old", "new"});
result.frames.push_back(frame);
return result;
}
void mutate_copy(World& world) {
if (world.groups.empty()) {
world.groups.push_back({});
}
auto& group = world.groups.front();
group.name = "copy_changed";
group.pin = "/copy/pin";
if (group.slots.empty()) {
group.slots.push_back({});
}
group.slots.front().name = "copy_slot";
group.slots.front().link = "/copy/link";
if (group.candidates.empty()) {
group.candidates.push_back({});
}
auto& candidate = group.candidates.front();
candidate.path = "/copy/candidate";
candidate.priority = -123;
candidate.enabled = !candidate.enabled;
if (candidate.attachments.empty()) {
candidate.attachments.push_back({});
}
candidate.attachments.front() = {"copy_slot", "/copy/target"};
}
void mutate_copy(Transcript& transcript) {
mutate_copy(transcript.final_world);
if (transcript.frames.empty()) {
transcript.frames.push_back({});
}
auto& frame = transcript.frames.front();
mutate_copy(frame.world);
frame.projection.selections.push_back({"copy", Mode::manual, "/copy", "/copy"});
if (frame.projection.bindings.empty()) {
frame.projection.bindings.push_back({});
}
frame.projection.bindings.front() = {"/copy/link", "/copy/target"};
if (frame.changes.empty()) {
frame.changes.push_back({});
}
frame.changes.front() = {"/copy/link", "before_copy", "after_copy"};
}
void end_of_command(std::istringstream& input) {
std::string extra;
if (input >> extra) {
throw std::runtime_error("extra token");
}
}
std::string token(std::istringstream& input) {
std::string result;
if (!(input >> result)) {
throw std::runtime_error("missing token");
}
return result;
}
std::size_t index(std::istringstream& input) {
return static_cast<std::size_t>(std::stoull(token(input)));
}
int number(std::istringstream& input) {
return std::stoi(token(input));
}
} // namespace
int main() {
std::vector<Group> raw;
World world;
std::vector<Event> events;
Transcript transcript;
bool world_ok = false;
bool transcript_ok = false;
std::string line;
try {
while (std::getline(std::cin, line)) {
std::istringstream input(line);
const std::string command = token(input);
std::string result;
if (command == "R") {
raw.clear();
result = "0";
} else if (command == "G") {
Group group;
group.name = unhex(token(input));
group.master_link = unhex(token(input));
group.mode = static_cast<Mode>(number(input));
group.pin = unhex(token(input));
raw.push_back(std::move(group));
result = "0";
} else if (command == "S") {
const auto gi = index(input);
Slot slot;
slot.name = unhex(token(input));
slot.link = unhex(token(input));
raw.at(gi).slots.push_back(std::move(slot));
result = "0";
} else if (command == "C") {
const auto gi = index(input);
Candidate candidate;
candidate.path = unhex(token(input));
candidate.priority = static_cast<std::int32_t>(number(input));
candidate.enabled = number(input) != 0;
raw.at(gi).candidates.push_back(std::move(candidate));
result = "0";
} else if (command == "T") {
const auto gi = index(input);
const auto ci = index(input);
Attachment attachment;
attachment.slot = unhex(token(input));
attachment.target = unhex(token(input));
raw.at(gi).candidates.at(ci).attachments.push_back(std::move(attachment));
result = "0";
} else if (command == "W") {
const std::string before = array(raw, group_json);
world = dirty_world();
const Error error = compile_world(raw, world);
world_ok = error == Error::none;
const bool unchanged = before == array(raw, group_json);
result = "[" + std::to_string(static_cast<int>(error)) + ',' +
world_json(world) + ',' + boolean(unchanged) + ']';
} else if (command == "K") {
const std::string text = unhex(token(input));
const std::string before = text;
std::int32_t value = 987654321;
const Error error = parse_priority(text, value);
result = "[" + std::to_string(static_cast<int>(error)) + ',' +
std::to_string(value) + ',' + boolean(text == before) + ']';
} else if (command == "I") {
const std::string text = unhex(token(input));
const std::string before = text;
const bool identifier = valid_id(text);
const bool path = valid_path(text);
const bool unchanged = before == text;
result = "[" + boolean(identifier) + ',' + boolean(path) + ',' + boolean(unchanged) + ']';
} else if (command == "P") {
if (!world_ok) {
throw std::runtime_error("world unavailable");
}
const std::string before = world_json(world);
const Projection projected = project(world);
result = "[" + projection_json(projected) + ',' + boolean(before == world_json(world)) + ']';
} else if (command == "X") {
events.clear();
result = "0";
} else if (command == "E") {
Event event;
event.op = static_cast<Operation>(number(input));
event.group = unhex(token(input));
event.path = unhex(token(input));
event.priority = unhex(token(input));
event.enabled = number(input) != 0;
const auto count = index(input);
for (std::size_t i = 0; i < count; ++i) {
Attachment attachment;
attachment.slot = unhex(token(input));
attachment.target = unhex(token(input));
event.attachments.push_back(std::move(attachment));
}
events.push_back(std::move(event));
result = "0";
} else if (command == "F") {
if (!world_ok) {
throw std::runtime_error("world unavailable");
}
const std::string before = world_json(world) + array(events, event_json);
transcript = dirty_transcript();
const Error error = replay(world, events, transcript);
transcript_ok = error == Error::none;
const bool unchanged = before == world_json(world) + array(events, event_json);
result = "[" + std::to_string(static_cast<int>(error)) + ',' +
transcript_json(transcript) + ',' + boolean(unchanged) + ']';
} else if (command == "U") {
if (!transcript_ok) {
throw std::runtime_error("transcript unavailable");
}
world = transcript.final_world;
world_ok = true;
result = "0";
} else if (command == "J") {
const std::string before_world = world_json(world);
const std::string before_transcript = transcript_json(transcript);
World world_copy = world;
Transcript transcript_copy = transcript;
mutate_copy(world_copy);
mutate_copy(transcript_copy);
result = "[" + boolean(before_world == world_json(world)) + ',' +
boolean(before_transcript == transcript_json(transcript)) + ']';
} else {
throw std::runtime_error("unknown command");
}
end_of_command(input);
std::cout << result << '\n';
}
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 2;
}
return 0;
}