amos f611da1e57 Batch 24: add 23 calibrated tasks written from Bos seed directions
Batch 24 (20261102-C-bos-seed-24) screened all 3078 Bos seed queries.
Only 23 themes were in scope (pure user-space reliability, at most one
task per theme); the rest were single operations or explanations,
installs and networking, system administration, ROS2, board-only or GUI
requests. Two finished drafts were withdrawn as too close to existing
tasks. Seeds supply direction only: all sources, contracts and tests are
new, and no task shares more than 14 characters with its seed text.

Each task passed the local gate and the trace6 Docker gate (5 runs per
sample class). Claude rebuilt the catalog from the calibration receipts:
23/23 USABLE. 16 of 23 are C/C++.

Only the task directories are taken; the batch worktree's lowered disk
floor in rollout/core.py stays out of main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 17:21:50 +08:00

135 lines
4.2 KiB
C++

#include "clock.hpp"
#include <algorithm>
#include <limits>
namespace shared_clock {
namespace {
using Image = std::array<std::uint8_t, 64>;
void store(Image& image, std::size_t offset, std::uint64_t value, unsigned width) {
for (unsigned index = 0; index < width; ++index) {
image[offset + index] = static_cast<std::uint8_t>(value & 255);
value >>= 8;
}
}
std::uint64_t fetch(const Image& image, std::size_t offset, unsigned width) {
std::uint64_t value = 0;
for (unsigned index = 0; index < width; ++index) {
value |= static_cast<std::uint64_t>(image[offset + index]) << (8 * index);
}
return value;
}
std::uint64_t checksum(const Image& image) {
std::uint64_t value = 14695981039346656037ULL;
for (std::size_t index = 0; index < 56; ++index) {
value ^= image[index];
value *= 1099511628211ULL;
}
return value;
}
std::int64_t signed_word(std::uint64_t value) {
if (value <= static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max())) {
return static_cast<std::int64_t>(value);
}
return -1 - static_cast<std::int64_t>(~value);
}
void header(Image& image, const Snapshot& snapshot) {
image[0] = 'C';
image[1] = 'K';
image[2] = 'S';
image[3] = '1';
store(image, 4, 1, 2);
store(image, 6, 64, 2);
store(image, 8, snapshot.sequence, 8);
}
void payload(Image& image, const Snapshot& snapshot) {
store(image, 16, snapshot.local, 8);
store(image, 24, snapshot.remote, 8);
store(image, 32, static_cast<std::uint64_t>(snapshot.offset), 8);
store(image, 40, snapshot.delay, 8);
store(image, 48, snapshot.id, 4);
store(image, 52, 0, 4);
}
}
Page::Page() {
reset();
}
void Page::reset() {
image_.fill(0);
}
Status Page::load(const std::vector<std::uint8_t>& data) {
if (data.size() != image_.size()) {
return Status::Invalid;
}
std::copy(data.begin(), data.end(), image_.begin());
return Status::Ok;
}
std::vector<std::uint8_t> Page::bytes() const {
return {image_.begin(), image_.end()};
}
Status Page::publish(const Snapshot& snapshot, unsigned failpoint) {
if (failpoint > 3) {
return Status::Invalid;
}
if (validate_snapshot(snapshot) != Status::Ok) {
return Status::Range;
}
const Image saved = image_;
header(image_, snapshot);
if (failpoint != 1) {
payload(image_, snapshot);
if (failpoint != 2) {
store(image_, 56, checksum(image_), 8);
}
}
if (failpoint != 0) {
image_ = saved;
return Status::PublishFailed;
}
return Status::Ok;
}
Status Page::read(Snapshot& out) const {
if (std::all_of(image_.begin(), image_.end(), [](std::uint8_t byte) { return byte == 0; })) {
return Status::Empty;
}
if (image_[0] != 'C' || image_[1] != 'K' || image_[2] != 'S' || image_[3] != '1' ||
fetch(image_, 4, 2) != 1 || fetch(image_, 6, 2) != 64 ||
fetch(image_, 52, 4) != 0 || fetch(image_, 56, 8) != checksum(image_)) {
return Status::Corrupt;
}
Snapshot decoded;
decoded.sequence = fetch(image_, 8, 8);
decoded.local = fetch(image_, 16, 8);
decoded.remote = fetch(image_, 24, 8);
decoded.offset = signed_word(fetch(image_, 32, 8));
decoded.delay = fetch(image_, 40, 8);
decoded.id = static_cast<std::uint32_t>(fetch(image_, 48, 4));
if (validate_snapshot(decoded) != Status::Ok) {
return Status::Corrupt;
}
out = decoded;
return Status::Ok;
}
Status project(const Page& page, Tick now, Tick max_age, Projection& out) {
Snapshot snapshot;
const Status status = page.read(snapshot);
if (status != Status::Ok) {
return status;
}
if (now > LIMIT || max_age > LIMIT) {
return Status::Range;
}
if (now < snapshot.local || now - snapshot.local > max_age) {
return Status::Stale;
}
const Tick elapsed = now - snapshot.local;
if (elapsed > LIMIT - snapshot.remote) {
return Status::Range;
}
Projection result;
result.sequence = snapshot.sequence;
result.remote_ticks = snapshot.remote + elapsed;
ticks_to_microseconds(result.remote_ticks, result.microseconds);
out = result;
return Status::Ok;
}
}