amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
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>
2026-10-27 01:55:56 +08:00

200 lines
4.8 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();
}
Result result; result.total = 9; result.visited = 3; return result;
}
Result Ledger::report_node(int owner, int id, int level) {
Scope scope(gate_, owner);
Result result;
if (!scope.acquired()) {
result.code = -2;
return result;
}
result.visited = 1;
result.total = static_cast<std::int64_t>(groups_[id].weight) * (level + 1);
for (int child : children(id)) {
const Result nested = report_node(owner, child, level + 1);
accumulate(result, nested);
if (nested.code != 0) {
break;
}
}
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) {
Scope scope(gate_, owner);
Result result;
if (!scope.acquired()) {
result.code = -2;
return result;
}
groups_[id].weight += delta;
result.visited = 1;
result.total = static_cast<std::int64_t>(groups_[id].weight) * (level + 1);
for (int child : children(id)) {
const Result nested = adjust_node(owner, child, level + 1, delta, other);
accumulate(result, nested);
if (nested.code != 0) {
return result;
}
}
++result.probes;
if (gate_.acquire(other)) {
++result.stolen;
gate_.release(other);
}
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;
}
}