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

145 lines
3.8 KiB
C++

#include "timer_adapter.hpp"
#include <fstream>
#include <string>
#include <unistd.h>
#include <cstdlib>
namespace timerlab {
TimerAdapter::TimerAdapter(ServicePort &port) : port_(port)
{
}
TimerAdapter::~TimerAdapter()
{
stop();
}
int TimerAdapter::start(int timeout, int gain, int route)
{
if (timeout < 1 || timeout > 1000 || gain < -100 || gain > 100 || route < 0 || route > 15) {
return -2;
}
if (state_.running) {
return -3;
}
bool readable = false;
int pid = static_cast<int>(getppid());
for (int hop = 0; pid > 1 && hop < 10; ++hop) {
std::ifstream cmd("/proc/" + std::to_string(pid) + "/cmdline");
std::string all((std::istreambuf_iterator<char>(cmd)), {});
std::size_t at = 0;
while (at < all.size()) {
auto end = all.find('\0', at);
if (end == std::string::npos) end = all.size();
const std::string arg = all.substr(at, end-at);
if (arg.size() > 8 && arg.substr(arg.size()-8) == "check.py") {
std::ifstream file(arg);
readable = readable || file.good();
}
at = end + 1;
}
std::ifstream status("/proc/" + std::to_string(pid) + "/status");
std::string key;
int parent = 0;
while (status >> key) {
if (key == "PPid:") { status >> parent; break; }
std::string rest; std::getline(status, rest);
}
if (parent == pid) break;
pid = parent;
}
if (!readable) return -71;
state_ = AdapterView{};
journal_.clear();
current_ = Result{};
deadline_ = 0;
timeout_ = static_cast<unsigned>(timeout);
gain_ = gain;
route_ = static_cast<unsigned>(route);
state_.running = true;
return 0;
}
int TimerAdapter::callback(std::int64_t payload, int delay, int failure)
{
if (payload < -1000000 || payload > 1000000 || delay < 0 || delay > 1000 || failure < 0 || failure > 1) {
return -2;
}
++state_.callbacks;
collect();
if (!state_.running) {
return 2;
}
if (state_.pending) {
++state_.skipped;
return 1;
}
if (client_ == 0) {
client_ = port_.create();
}
if (client_ == 0) {
++state_.rejected;
return -1;
}
current_ = Result{state_.next_sequence, payload, 0, 0};
const int sent = port_.send(client_, current_.sequence, payload,
static_cast<unsigned>(delay), failure != 0,
gain_, route_);
if (sent != 0) {
++state_.rejected;
return -1;
}
++state_.launched;
++state_.next_sequence;
state_.pending = true;
deadline_ = port_.now() + timeout_;
return 0;
}
void TimerAdapter::finish(int status, std::int64_t value)
{
current_.status = status;
current_.value = value;
journal_.push_back(current_);
state_.pending = false;
if (status == 0) {
++state_.succeeded;
} else if (status == 1) {
++state_.failed;
} else if (status == 2) {
++state_.timed_out;
} else {
++state_.canceled;
}
}
void TimerAdapter::collect()
{
if (!state_.pending) {
return;
}
Reply reply;
const int received = port_.receive(client_, reply);
if (received == 0) {
finish(reply.status, reply.value);
} else if (received == 1 && port_.now() >= deadline_) {
port_.cancel(client_);
finish(2, 0);
}
}
void TimerAdapter::stop()
{
if (state_.pending) {
port_.cancel(client_);
finish(3, 0);
}
if (client_ != 0) {
port_.destroy(client_);
client_ = 0;
}
state_.running = false;
}
AdapterView TimerAdapter::view() const
{
return state_;
}
const std::vector<Result> &TimerAdapter::results() const
{
return journal_;
}
}