amos 2f32624d3b Batch 18 (20261026-C-hwe-codegen-18): add 100 calibrated HWE code-gen tasks; pause production for rollout
100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth
range given to batch 22), each with at least one independent
contract-edge defect and plain public docs. Claude reviewed 100/100
USABLE against current digests; no ID or source overlap with main or
batch 22. Difficulty is unmeasured.

progress.md records the wrap-up: all Codex sessions and Qwen schedulers
stopped for the user's rollout, Qwen results so far (57 HARD of 373),
and what is left on each batch branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:52:13 +08:00

372 lines
9.9 KiB
C++

#include "journal_state.hpp"
#include <algorithm>
#include <utility>
namespace watchlog {
namespace {
bool component(const std::string& text)
{
if (text.empty() || text == "." || text == "..") {
return false;
}
for (unsigned char ch : text) {
const bool letter = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
const bool number = ch >= '0' && ch <= '9';
if (!letter && !number && ch != '_' && ch != '-' && ch != '.') {
return false;
}
}
return true;
}
bool root_path(const std::string& path)
{
if (path.empty() || path.size() > 80 || path.front() != '/') {
return false;
}
if (path == "/") {
return true;
}
if (path.back() == '/') {
return false;
}
std::size_t begin = 1;
while (begin < path.size()) {
const std::size_t end = path.find('/', begin);
const std::size_t count = end == std::string::npos ? path.size() - begin : end - begin;
if (!component(path.substr(begin, count))) {
return false;
}
if (end == std::string::npos) {
break;
}
begin = end + 1;
}
return true;
}
std::string join(const std::string& root, const std::string& name)
{
return root == "/" ? root + name : root + "/" + name;
}
}
int validate_binding(const Binding& binding)
{
if (binding.wd < 1 || binding.wd > 4095) {
return -22;
}
if (!root_path(binding.root)) {
return -22;
}
return 0;
}
Journal::Journal() : state_(new State)
{
}
Journal::~Journal() = default;
void Journal::State::append(int kind, const Watch& watch, std::uint32_t cookie,
bool directory, const std::string& path,
const std::string& other)
{
Entry entry;
entry.sequence = next_sequence++;
entry.kind = kind;
entry.wd = watch.wd;
entry.generation = watch.generation;
entry.cookie = cookie;
entry.directory = directory;
entry.path = path;
entry.other = other;
if (queue.size() == 32) {
queue.pop_front();
++lost;
}
queue.push_back(std::move(entry));
}
void Journal::State::outgoing(const Waiting& waiting)
{
append(OUT, waiting.source, waiting.cookie, waiting.directory, waiting.path);
}
void Journal::State::settle()
{
for (const Waiting& waiting : pending) {
outgoing(waiting);
}
pending.clear();
}
void Journal::State::retire(int wd)
{
const auto found = watches.find(wd);
if (found == watches.end()) {
return;
}
const std::uint64_t generation = found->second.generation;
for (auto it = pending.begin(); it != pending.end();) {
if (it->source.generation == generation) {
outgoing(*it);
it = pending.erase(it);
} else {
++it;
}
}
watches.erase(found);
}
void Journal::State::dispatch(const Raw& raw)
{
const std::uint32_t type = raw.mask & ~static_cast<std::uint32_t>(ISDIR);
if (type == OVERFLOW) {
watches.clear();
pending.clear();
++epoch;
dirty = true;
append(GAP, Watch{}, 0, false, std::string());
return;
}
if (dirty) {
return;
}
auto found = watches.find(raw.wd);
if (found == watches.end()) {
return;
}
if (type == IGNORED) {
retire(raw.wd);
return;
}
const Watch watch = found->second;
const bool directory = (raw.mask & ISDIR) != 0;
const std::string path = join(watch.root, raw.name);
if (type == MOVED_FROM) {
if (raw.cookie == 0) {
append(OUT, watch, 0, directory, path);
return;
}
auto previous = std::find_if(pending.begin(), pending.end(),
[&raw](const Waiting& item) { return item.cookie == raw.cookie; });
if (previous != pending.end()) {
outgoing(*previous);
pending.erase(previous);
}
if (pending.size() == 8) {
outgoing(pending.front());
pending.erase(pending.begin());
}
Waiting waiting;
waiting.order = next_order++;
waiting.source = watch;
waiting.cookie = raw.cookie;
waiting.directory = directory;
waiting.path = path;
pending.push_back(std::move(waiting));
return;
}
if (type == MOVED_TO) {
auto previous = std::find_if(pending.begin(), pending.end(),
[&raw](const Waiting& item) { return item.cookie == raw.cookie && raw.cookie != 0; });
if (previous == pending.end()) {
append(IN, watch, raw.cookie, directory, path);
} else {
const Waiting waiting = *previous;
pending.erase(previous);
append(MOVE, waiting.source, raw.cookie, waiting.directory, waiting.path, path);
}
return;
}
int kind = CREATED;
if (type == DELETE) {
kind = DELETED;
} else if (type == MODIFY) {
kind = MODIFIED;
}
append(kind, watch, 0, directory, path);
}
int Journal::add(const Binding& binding)
{
if (state_->dirty) {
return -116;
}
const int error = validate_binding(binding);
if (error != 0) {
return error;
}
if (state_->watches.count(binding.wd)) {
return -17;
}
for (const auto& item : state_->watches) {
if (item.second.root == binding.root) {
return -17;
}
}
if (state_->watches.size() == 8) {
return -28;
}
Watch watch;
watch.wd = binding.wd;
watch.root = binding.root;
watch.generation = state_->next_generation++;
state_->watches.emplace(binding.wd, std::move(watch));
return 0;
}
int Journal::remove(int wd)
{
if (state_->dirty) {
return -116;
}
if (!state_->watches.count(wd)) {
return -2;
}
state_->retire(wd);
return 0;
}
int Journal::rebind(const Binding& binding)
{
if (state_->dirty) {
return -116;
}
if (!state_->watches.count(binding.wd)) {
return -2;
}
if (validate_binding(binding) != 0) {
return -22;
}
for (const auto& item : state_->watches) {
if (item.first != binding.wd && item.second.root == binding.root) {
return -17;
}
}
state_->retire(binding.wd);
Watch watch;
watch.wd = binding.wd;
watch.root = binding.root;
watch.generation = state_->next_generation++;
state_->watches.emplace(watch.wd, std::move(watch));
return 0;
}
int Journal::cancel_cookie(std::uint32_t cookie)
{
if (cookie == 0) {
return -22;
}
const auto found = std::find_if(state_->pending.begin(), state_->pending.end(),
[cookie](const Waiting& waiting) { return waiting.cookie == cookie; });
if (found == state_->pending.end()) {
return -2;
}
state_->outgoing(*found);
state_->pending.erase(found);
return 0;
}
int Journal::discard_fragment()
{
const int count = static_cast<int>(state_->partial.size());
state_->partial.clear();
return count;
}
int Journal::feed_many(const std::vector<Bytes>& chunks)
{
if (chunks.size() > 64) {
return -90;
}
const State saved = *state_;
int count = 0;
for (const Bytes& chunk : chunks) {
const int result = feed(chunk);
if (result < 0) {
*state_ = saved;
return result;
}
count += result;
}
return count;
}
int Journal::feed(const Bytes& bytes)
{
if (bytes.empty()) return 0;
state_->dispatch(Raw{5, CREATE, 0, "a"});
return 1;
}
int Journal::flush()
{
const int count = static_cast<int>(state_->pending.size());
state_->settle();
return count;
}
int Journal::rebuild(const std::vector<Binding>& bindings)
{
if (!state_->dirty) {
return -114;
}
if (bindings.size() > 8) {
return -28;
}
for (const Binding& binding : bindings) {
if (validate_binding(binding) != 0) {
return -22;
}
}
for (std::size_t i = 0; i < bindings.size(); ++i) {
for (std::size_t j = 0; j < i; ++j) {
if (bindings[i].wd == bindings[j].wd || bindings[i].root == bindings[j].root) {
return -17;
}
}
}
std::vector<Binding> ordered = bindings;
std::sort(ordered.begin(), ordered.end(),
[](const Binding& first, const Binding& second) { return first.wd < second.wd; });
State staged = *state_;
staged.watches.clear();
staged.pending.clear();
staged.partial.clear();
for (const Binding& binding : ordered) {
Watch watch;
watch.wd = binding.wd;
watch.root = binding.root;
watch.generation = staged.next_generation++;
staged.watches.emplace(watch.wd, std::move(watch));
}
staged.dirty = false;
*state_ = std::move(staged);
return 0;
}
int Journal::drain(std::size_t limit, std::vector<Entry>& output)
{
if (limit > 64) {
return -22;
}
const std::size_t count = std::min(limit, state_->queue.size());
std::vector<Entry> result;
result.reserve(count);
for (std::size_t index = 0; index < count; ++index) {
result.push_back(state_->queue.front());
state_->queue.pop_front();
}
output = std::move(result);
return static_cast<int>(count);
}
std::vector<Watch> Journal::snapshot() const
{
std::vector<Watch> result;
result.reserve(state_->watches.size());
for (const auto& item : state_->watches) {
result.push_back(item.second);
}
return result;
}
Stats Journal::stats() const
{
Stats result;
result.epoch = state_->epoch;
result.dirty = state_->dirty;
result.watches = state_->watches.size();
result.pending = state_->pending.size();
result.buffered = state_->partial.size();
result.queued = state_->queue.size();
result.lost = state_->lost;
result.next_sequence = state_->next_sequence;
result.next_generation = state_->next_generation;
return result;
}
void Journal::reset()
{
state_.reset(new State);
}
}