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>
228 lines
5.7 KiB
C++
228 lines
5.7 KiB
C++
#include "ledger.hpp"
|
|
#include <algorithm>
|
|
|
|
namespace scoped {
|
|
namespace {
|
|
Result invalid() {
|
|
Result result;
|
|
result.code = -1;
|
|
return result;
|
|
}
|
|
|
|
void accumulate(Result& target, const Result& source) {
|
|
target.total += source.total;
|
|
target.visited += source.visited;
|
|
target.probes += source.probes;
|
|
target.stolen += source.stolen;
|
|
if (source.code != 0) {
|
|
target.code = source.code;
|
|
}
|
|
}
|
|
|
|
bool owner_ok(int owner) {
|
|
return owner >= 1 && owner <= owner_limit;
|
|
}
|
|
|
|
bool weight_ok(int weight) {
|
|
return weight >= -weight_limit && weight <= weight_limit;
|
|
}
|
|
}
|
|
|
|
Ledger::Ledger() {
|
|
reset();
|
|
}
|
|
|
|
void Ledger::reset() {
|
|
groups_.fill(Slot{});
|
|
gate_.reset();
|
|
}
|
|
|
|
bool Ledger::live(int id) const {
|
|
return id >= 0 && id < group_limit && groups_[id].live;
|
|
}
|
|
|
|
bool Ledger::parent_ok(int parent) const {
|
|
return parent == -1 || live(parent);
|
|
}
|
|
|
|
bool Ledger::descendant(int id, int ancestor) const {
|
|
int cursor = id;
|
|
while (cursor != -1) {
|
|
if (cursor == ancestor) {
|
|
return true;
|
|
}
|
|
cursor = groups_[cursor].parent;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::vector<int> Ledger::children(int id) const {
|
|
std::vector<int> result;
|
|
for (int child = 0; child < group_limit; ++child) {
|
|
if (groups_[child].live && groups_[child].parent == id) {
|
|
result.push_back(child);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::vector<int> Ledger::subtree(int id) const {
|
|
std::vector<int> result;
|
|
std::vector<int> pending{id};
|
|
while (!pending.empty()) {
|
|
const int current = pending.back();
|
|
pending.pop_back();
|
|
result.push_back(current);
|
|
const auto next = children(current);
|
|
pending.insert(pending.end(), next.rbegin(), next.rend());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Result Ledger::add(int id, int parent, int weight) {
|
|
if (id < 0 || id >= group_limit || live(id) || !parent_ok(parent) || !weight_ok(weight)) {
|
|
return invalid();
|
|
}
|
|
groups_[id] = Slot{true, parent, weight};
|
|
return Result{};
|
|
}
|
|
|
|
Result Ledger::move(int id, int parent) {
|
|
if (!live(id) || !parent_ok(parent)) {
|
|
return invalid();
|
|
}
|
|
if (parent != -1 && descendant(parent, id)) {
|
|
return invalid();
|
|
}
|
|
groups_[id].parent = parent;
|
|
return Result{};
|
|
}
|
|
|
|
Result Ledger::set_weight(int id, int weight) {
|
|
if (!live(id) || !weight_ok(weight)) {
|
|
return invalid();
|
|
}
|
|
groups_[id].weight = weight;
|
|
return Result{};
|
|
}
|
|
|
|
Result Ledger::erase(int id) {
|
|
if (!live(id)) {
|
|
return invalid();
|
|
}
|
|
for (int current : subtree(id)) {
|
|
groups_[current] = Slot{};
|
|
}
|
|
return Result{};
|
|
}
|
|
|
|
Result Ledger::report(int owner, int id) {
|
|
if (!owner_ok(owner) || !live(id)) {
|
|
return invalid();
|
|
}
|
|
return report_node(owner, id, 0);
|
|
}
|
|
|
|
Result Ledger::report_node(int owner, int id, int level) {
|
|
struct Frame { int id; int level; bool exit; };
|
|
std::vector<Frame> work{{id, level, false}};
|
|
Result result;
|
|
int held = 0;
|
|
while (!work.empty()) {
|
|
const auto frame = work.back();
|
|
work.pop_back();
|
|
if (frame.exit) {
|
|
gate_.release(owner);
|
|
--held;
|
|
continue;
|
|
}
|
|
if (!gate_.acquire(owner)) {
|
|
result.code = -2;
|
|
break;
|
|
}
|
|
++held;
|
|
++result.visited;
|
|
result.total += static_cast<std::int64_t>(groups_[frame.id].weight) * (frame.level + 1);
|
|
work.push_back(Frame{frame.id, frame.level, true});
|
|
const auto next = children(frame.id);
|
|
for (auto child = next.rbegin(); child != next.rend(); ++child) {
|
|
work.push_back(Frame{*child, frame.level + 1, false});
|
|
}
|
|
}
|
|
while (held > 0) {
|
|
gate_.release(owner);
|
|
--held;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Result Ledger::adjust(int owner, int id, int delta, int other) {
|
|
if (!owner_ok(owner) || !owner_ok(other) || owner == other || !live(id) || !weight_ok(delta)) {
|
|
return invalid();
|
|
}
|
|
for (int current : subtree(id)) {
|
|
if (!weight_ok(groups_[current].weight + delta)) {
|
|
return invalid();
|
|
}
|
|
}
|
|
return adjust_node(owner, id, 0, delta, other);
|
|
}
|
|
|
|
Result Ledger::adjust_node(int owner, int id, int level, int delta, int other) {
|
|
struct Frame { int id; int level; bool exit; };
|
|
std::vector<Frame> pending{{id, level, false}};
|
|
Result result;
|
|
int active = 0;
|
|
while (!pending.empty()) {
|
|
const Frame frame = pending.back();
|
|
pending.pop_back();
|
|
if (frame.exit) {
|
|
++result.probes;
|
|
if (gate_.acquire(other)) {
|
|
++result.stolen;
|
|
gate_.release(other);
|
|
}
|
|
gate_.release(owner);
|
|
--active;
|
|
} else {
|
|
if (!gate_.acquire(owner)) {
|
|
result.code = -2;
|
|
break;
|
|
}
|
|
++active;
|
|
groups_[frame.id].weight += delta;
|
|
++result.visited;
|
|
result.total += static_cast<std::int64_t>(groups_[frame.id].weight) * (frame.level + 1);
|
|
pending.push_back(Frame{frame.id, frame.level, true});
|
|
const auto next = children(frame.id);
|
|
for (auto child = next.rbegin(); child != next.rend(); ++child) {
|
|
pending.push_back(Frame{*child, frame.level + 1, false});
|
|
}
|
|
}
|
|
}
|
|
while (active > 0) {
|
|
gate_.release(owner);
|
|
--active;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Gate& Ledger::gate() {
|
|
return gate_;
|
|
}
|
|
|
|
const Gate& Ledger::gate() const {
|
|
return gate_;
|
|
}
|
|
|
|
std::vector<Group> Ledger::snapshot() const {
|
|
std::vector<Group> result;
|
|
for (int id = 0; id < group_limit; ++id) {
|
|
if (groups_[id].live) {
|
|
result.push_back(Group{id, groups_[id].parent, groups_[id].weight});
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|