Batch 21 (20261027-A-bos-redo-21) rebuilt 40 of the 53 rejected Bos-qiao environments: hidden assertions that ran inside the candidate's process now run in a trusted parent that reads observations from a low-UID worker; gold bytes are unchanged. 9 were excluded for scope (ROS2, oversize, security core), 3 are blocked because the source requires an exec bit the platform does not freeze, 1 lacks requests/bs4 in trace6. Batch 18 (20261026-C-hwe-codegen-18) adds its last 34 of 134 tasks; the first 100 landed in 2f32624d. Claude reviewed 74/74 USABLE against current digests, canonical families, no ID or source overlap with main, plain public docs. Only the new task directories are taken; the stale pre-batch-20 copies of concur-release-capsule and resource-temp-link on the batch 21 branch and both branches' progress notes stay as main has them. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
200 lines
5.9 KiB
C++
200 lines
5.9 KiB
C++
#include "runtime.hpp"
|
|
#include <algorithm>
|
|
#include <numeric>
|
|
namespace cancelrt {
|
|
bool Runtime::valid_id(std::int64_t id) {
|
|
return bounded(id, 1, 31);
|
|
}
|
|
bool Runtime::bounded(std::int64_t value, std::int64_t lo, std::int64_t hi) {
|
|
return value >= lo && value <= hi;
|
|
}
|
|
Reply Runtime::fail(Code code) {
|
|
Reply reply;
|
|
reply.code = code;
|
|
return reply;
|
|
}
|
|
Reply Runtime::observe(const Job& job) {
|
|
Reply reply;
|
|
reply.total = static_cast<int>(job.input.size());
|
|
reply.done = static_cast<int>(job.output.size());
|
|
reply.factor = job.factor;
|
|
reply.bias = job.bias;
|
|
reply.attempts = job.attempts;
|
|
reply.releases = job.releases;
|
|
reply.active = job.active ? 1 : 0;
|
|
reply.sum = std::accumulate(job.output.begin(), job.output.end(), std::int64_t{0});
|
|
return reply;
|
|
}
|
|
Job Runtime::replay(const Journal& journal) {
|
|
Job job;
|
|
for (const auto& event : journal) {
|
|
switch (event.kind) {
|
|
case Kind::CREATE:
|
|
for (int index = 0; index < event.a; ++index) {
|
|
job.input.push_back(event.b + index);
|
|
}
|
|
break;
|
|
case Kind::CONFIGURE:
|
|
job.factor = static_cast<int>(event.a);
|
|
job.bias = static_cast<int>(event.b);
|
|
break;
|
|
case Kind::WRITE:
|
|
for (int index = 0; index < event.b; ++index) {
|
|
job.input[static_cast<std::size_t>(event.a + index)] = event.c;
|
|
}
|
|
break;
|
|
case Kind::COMMIT:
|
|
job.output.push_back(event.a);
|
|
break;
|
|
case Kind::ACQUIRE:
|
|
job.active = true;
|
|
++job.attempts;
|
|
break;
|
|
case Kind::RELEASE:
|
|
job.active = false;
|
|
++job.releases;
|
|
break;
|
|
case Kind::REWIND:
|
|
job.output.clear();
|
|
job.attempts = 0;
|
|
job.releases = 0;
|
|
job.active = false;
|
|
break;
|
|
}
|
|
}
|
|
return job;
|
|
}
|
|
Reply Runtime::create(std::int64_t id, std::int64_t total, std::int64_t seed) {
|
|
if (!valid_id(id)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (!bounded(total, 0, 32)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (!bounded(seed, -1000, 1000)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (jobs_.find(static_cast<int>(id)) != jobs_.end()) {
|
|
return fail(Code::EXISTS);
|
|
}
|
|
Journal journal;
|
|
journal.push_back({Kind::CREATE, total, seed});
|
|
auto inserted = jobs_.emplace(static_cast<int>(id), std::move(journal));
|
|
return observe(replay(inserted.first->second));
|
|
}
|
|
Reply Runtime::configure(std::int64_t id, std::int64_t factor, std::int64_t bias) {
|
|
if (!valid_id(id)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (!bounded(factor, -8, 8)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (!bounded(bias, -1000, 1000)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
auto found = jobs_.find(static_cast<int>(id));
|
|
if (found == jobs_.end()) {
|
|
return fail(Code::MISSING);
|
|
}
|
|
Job job = replay(found->second);
|
|
if (!job.output.empty()) {
|
|
return fail(Code::BUSY);
|
|
}
|
|
found->second.push_back({Kind::CONFIGURE, factor, bias});
|
|
return observe(replay(found->second));
|
|
}
|
|
Reply Runtime::write(std::int64_t id, std::int64_t offset, std::int64_t count, std::int64_t value) {
|
|
if (!valid_id(id)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (!bounded(offset, 0, 32)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (!bounded(count, 0, 32)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (!bounded(value, -1000, 1000)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
auto found = jobs_.find(static_cast<int>(id));
|
|
if (found == jobs_.end()) {
|
|
return fail(Code::MISSING);
|
|
}
|
|
Job job = replay(found->second);
|
|
if (offset + count > static_cast<std::int64_t>(job.input.size())) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (!job.output.empty()) {
|
|
return fail(Code::BUSY);
|
|
}
|
|
found->second.push_back({Kind::WRITE, offset, count, value});
|
|
return observe(replay(found->second));
|
|
}
|
|
Reply Runtime::read(std::int64_t id, std::int64_t offset, std::int64_t count, std::int64_t view) const {
|
|
if (!valid_id(id)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (!bounded(offset, 0, 32)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (!bounded(count, 0, 32)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
if (!bounded(view, 0, 1)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
auto found = jobs_.find(static_cast<int>(id));
|
|
if (found == jobs_.end()) {
|
|
return fail(Code::MISSING);
|
|
}
|
|
const Job job = replay(found->second);
|
|
const auto& data = view == 0 ? job.input : job.output;
|
|
if (offset + count > static_cast<std::int64_t>(data.size())) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
Reply reply = observe(job);
|
|
auto first = data.begin() + static_cast<std::ptrdiff_t>(offset);
|
|
reply.values.assign(first, first + static_cast<std::ptrdiff_t>(count));
|
|
return reply;
|
|
}
|
|
Reply Runtime::inspect(std::int64_t id) const {
|
|
if (!valid_id(id)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
auto found = jobs_.find(static_cast<int>(id));
|
|
if (found == jobs_.end()) {
|
|
return fail(Code::MISSING);
|
|
}
|
|
return observe(replay(found->second));
|
|
}
|
|
Reply Runtime::rewind(std::int64_t id) {
|
|
if (!valid_id(id)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
auto found = jobs_.find(static_cast<int>(id));
|
|
if (found == jobs_.end()) {
|
|
return fail(Code::MISSING);
|
|
}
|
|
found->second.push_back({Kind::REWIND});
|
|
return observe(replay(found->second));
|
|
}
|
|
Reply Runtime::erase(std::int64_t id) {
|
|
if (!valid_id(id)) {
|
|
return fail(Code::RANGE);
|
|
}
|
|
auto found = jobs_.find(static_cast<int>(id));
|
|
if (found == jobs_.end()) {
|
|
return fail(Code::MISSING);
|
|
}
|
|
jobs_.erase(found);
|
|
return Reply{};
|
|
}
|
|
Reply Runtime::list() const {
|
|
Reply reply;
|
|
for (const auto& entry : jobs_) {
|
|
reply.ids.push_back(entry.first);
|
|
}
|
|
return reply;
|
|
}
|
|
}
|