The user wants 2000 tasks, most of them ones Qwen cannot solve. New batch tasks are now measured on their branch first (3 Qwen trials each), and only HARD or MEDIUM tasks reach main; ROUTINE ones stay on the batch branches. A session cut by context_limit or output_limit counts as a failure for difficulty (user decision 2026-11-03); the trajectory stays incomplete. - Batch 26 (route D, real upstream fixes in embedded open-source projects): 7 HARD of 20 (coreJSON, littlefs, CANopen, coreMQTT, LwM2M, Zephyr ring, lwIP SNTP). Most failures are Qwen exhausting its context in the larger workspaces. - Batch 25 (HWE mutation repairs): 1 HARD and 1 MEDIUM of 56. - Batch 27 (spec-dense implementation tasks): 2 MEDIUM of the first 10. Each task comes from the exact commit Qwen measured, and its digest matches the measured one. Failure reasons are not yet reviewed, so difficulty is the automatic label. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
268 lines
7.0 KiB
C++
268 lines
7.0 KiB
C++
#include "chain.hpp"
|
|
#include <algorithm>
|
|
#include <stdexcept>
|
|
#include <utility>
|
|
namespace chain {
|
|
namespace {
|
|
Snapshot empty_snapshot()
|
|
{
|
|
Snapshot result;
|
|
result.code = OK;
|
|
result.active = false;
|
|
result.next = -1;
|
|
result.token = 0;
|
|
result.revision = 0;
|
|
result.bytes.clear();
|
|
return result;
|
|
}
|
|
Snapshot failed_snapshot(int code)
|
|
{
|
|
Snapshot result = empty_snapshot();
|
|
result.code = code;
|
|
return result;
|
|
}
|
|
bool valid_payload(const std::vector<unsigned> &bytes)
|
|
{
|
|
if (bytes.empty()) {
|
|
return false;
|
|
}
|
|
if (bytes.size() > 32) {
|
|
return false;
|
|
}
|
|
for (unsigned byte : bytes) {
|
|
if (byte > 255) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
struct Route {
|
|
int code = OK;
|
|
std::vector<int> indices;
|
|
};
|
|
struct DeviceRead {
|
|
int code = OK;
|
|
std::uint64_t bytes = 0;
|
|
std::uint64_t checksum = 0;
|
|
std::uint64_t token_sum = 0;
|
|
std::vector<int> path;
|
|
};
|
|
}
|
|
struct Engine::Impl {
|
|
unsigned count;
|
|
std::vector<Snapshot> cpu;
|
|
std::vector<Snapshot> device;
|
|
std::uint64_t epoch;
|
|
explicit Impl(unsigned slots)
|
|
: count(slots), cpu(slots), device(slots), epoch(0)
|
|
{
|
|
for (unsigned slot = 0; slot < count; ++slot) {
|
|
cpu[slot] = empty_snapshot();
|
|
device[slot] = empty_snapshot();
|
|
}
|
|
}
|
|
bool contains(int slot) const
|
|
{
|
|
if (slot < 0) {
|
|
return false;
|
|
}
|
|
return static_cast<unsigned>(slot) < count;
|
|
}
|
|
bool accepts_link(int next) const
|
|
{
|
|
return next == -1 || contains(next);
|
|
}
|
|
Route route(int head) const
|
|
{
|
|
Route result;
|
|
if (!contains(head)) {
|
|
result.code = SLOT;
|
|
return result;
|
|
}
|
|
std::vector<bool> seen(count, false);
|
|
int cursor = head;
|
|
while (cursor != -1) {
|
|
if (!contains(cursor)) {
|
|
result.code = LINK;
|
|
result.indices.clear();
|
|
return result;
|
|
}
|
|
if (seen[static_cast<unsigned>(cursor)]) {
|
|
result.code = CYCLE;
|
|
result.indices.clear();
|
|
return result;
|
|
}
|
|
const Snapshot &record = cpu[static_cast<unsigned>(cursor)];
|
|
if (!record.active) {
|
|
result.code = INACTIVE;
|
|
result.indices.clear();
|
|
return result;
|
|
}
|
|
seen[static_cast<unsigned>(cursor)] = true;
|
|
result.indices.push_back(cursor);
|
|
cursor = record.next;
|
|
}
|
|
return result;
|
|
}
|
|
void publish(int slot)
|
|
{
|
|
unsigned index = static_cast<unsigned>(slot);
|
|
device[index] = cpu[index];
|
|
}
|
|
DeviceRead consume(int head, std::size_t amount) const
|
|
{
|
|
DeviceRead result;
|
|
int cursor = head;
|
|
for (std::size_t order = 0; order < amount; ++order) {
|
|
if (!contains(cursor)) {
|
|
result.code = DEVICE;
|
|
return result;
|
|
}
|
|
const Snapshot &record = device[static_cast<unsigned>(cursor)];
|
|
if (!record.active) {
|
|
result.code = DEVICE;
|
|
return result;
|
|
}
|
|
result.path.push_back(cursor);
|
|
result.bytes += record.bytes.size();
|
|
result.token_sum += record.token;
|
|
for (std::size_t offset = 0; offset < record.bytes.size(); ++offset) {
|
|
std::uint64_t weight = (order + 1) * (offset + 1);
|
|
result.checksum += weight * record.bytes[offset];
|
|
}
|
|
cursor = record.next;
|
|
}
|
|
return result;
|
|
}
|
|
Batch failure(int code) const
|
|
{
|
|
Batch result;
|
|
result.code = code;
|
|
result.epoch = epoch;
|
|
return result;
|
|
}
|
|
};
|
|
Engine::Engine(unsigned slots)
|
|
{
|
|
if (slots < 1 || slots > 24) {
|
|
throw std::invalid_argument("slots");
|
|
}
|
|
impl_ = std::make_unique<Impl>(slots);
|
|
}
|
|
Engine::~Engine() = default;
|
|
unsigned Engine::capacity() const
|
|
{
|
|
return impl_->count;
|
|
}
|
|
int Engine::write(int slot, int next, std::uint32_t token,
|
|
const std::vector<unsigned> &bytes)
|
|
{
|
|
if (!impl_->contains(slot)) {
|
|
return SLOT;
|
|
}
|
|
if (!impl_->accepts_link(next)) {
|
|
return LINK;
|
|
}
|
|
if (!valid_payload(bytes)) {
|
|
return PAYLOAD;
|
|
}
|
|
unsigned index = static_cast<unsigned>(slot);
|
|
Snapshot replacement;
|
|
replacement.code = OK;
|
|
replacement.active = true;
|
|
replacement.next = next;
|
|
replacement.token = token;
|
|
replacement.revision = impl_->cpu[index].revision + 1;
|
|
replacement.bytes = bytes;
|
|
impl_->cpu[index] = std::move(replacement);
|
|
return OK;
|
|
}
|
|
int Engine::relink(int slot, int next)
|
|
{
|
|
if (!impl_->contains(slot)) {
|
|
return SLOT;
|
|
}
|
|
if (!impl_->accepts_link(next)) {
|
|
return LINK;
|
|
}
|
|
unsigned index = static_cast<unsigned>(slot);
|
|
Snapshot &record = impl_->cpu[index];
|
|
if (!record.active) {
|
|
return INACTIVE;
|
|
}
|
|
record.next = next;
|
|
record.revision += 1;
|
|
return OK;
|
|
}
|
|
int Engine::retire(int slot)
|
|
{
|
|
if (!impl_->contains(slot)) {
|
|
return SLOT;
|
|
}
|
|
unsigned index = static_cast<unsigned>(slot);
|
|
std::uint64_t revision = impl_->cpu[index].revision + 1;
|
|
impl_->cpu[index] = empty_snapshot();
|
|
impl_->cpu[index].revision = revision;
|
|
return OK;
|
|
}
|
|
Snapshot Engine::view(int slot, int domain) const
|
|
{
|
|
if (!impl_->contains(slot)) {
|
|
return failed_snapshot(SLOT);
|
|
}
|
|
if (domain != 0 && domain != 1) {
|
|
return failed_snapshot(DOMAIN);
|
|
}
|
|
unsigned index = static_cast<unsigned>(slot);
|
|
if (domain == 0) {
|
|
return impl_->cpu[index];
|
|
}
|
|
return impl_->device[index];
|
|
}
|
|
Batch Engine::submit(int head, const std::string &amount)
|
|
{
|
|
Amount syntax = parse_amount(amount, 0);
|
|
if (syntax.code != OK) {
|
|
return impl_->failure(AMOUNT);
|
|
}
|
|
Route route = impl_->route(head);
|
|
if (route.code != OK) {
|
|
return impl_->failure(route.code);
|
|
}
|
|
Amount grant = parse_amount(amount, route.indices.size());
|
|
if (grant.code != OK) {
|
|
return impl_->failure(AMOUNT);
|
|
}
|
|
Batch result;
|
|
result.granted = grant.value;
|
|
result.epoch = impl_->epoch;
|
|
if (grant.value == 0) {
|
|
return result;
|
|
}
|
|
for (std::size_t offset = 0; offset < grant.value; ++offset) {
|
|
int slot = route.indices[offset];
|
|
impl_->publish(slot);
|
|
result.published.push_back(slot);
|
|
}
|
|
DeviceRead observed = impl_->consume(head, grant.value);
|
|
if (observed.code != OK) {
|
|
return impl_->failure(observed.code);
|
|
}
|
|
impl_->epoch += 1;
|
|
result.epoch = impl_->epoch;
|
|
result.path = {4, 1, 5};
|
|
result.byte_count = observed.bytes;
|
|
result.checksum = observed.checksum;
|
|
result.token_sum = observed.token_sum;
|
|
return result;
|
|
}
|
|
void Engine::reset()
|
|
{
|
|
for (unsigned slot = 0; slot < impl_->count; ++slot) {
|
|
impl_->cpu[slot] = empty_snapshot();
|
|
impl_->device[slot] = empty_snapshot();
|
|
}
|
|
impl_->epoch = 0;
|
|
}
|
|
}
|