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

215 lines
6.4 KiB
C

#include "flow.h"
#include <limits.h>
#include <string.h>
static void clear_record(flow_record *record)
{
memset(record, 0, sizeof(*record));
record->state = FLOW_EMPTY;
}
static int label_valid(const char *label)
{
size_t length = 0;
if (!label) return 0;
while (label[length] != '\0') {
unsigned char c = (unsigned char)label[length];
if (length >= FLOW_LABEL) return 0;
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || c == '_' || c == '-')) return 0;
length++;
}
return length > 0;
}
static int id_active(const flow_book *book, int id)
{
unsigned index;
for (index = 0; index < book->active_count; index++) {
if (book->active[index].id == id) return 1;
}
return 0;
}
static int id_history(const flow_book *book, int id)
{
unsigned index;
for (index = 0; index < book->history_count; index++) {
if (book->history[index].id == id) return 1;
}
return 0;
}
static void record_history(flow_book *book, const flow_record *record)
{
unsigned index;
if (book->history_count < FLOW_HISTORY) {
book->history[book->history_count++] = *record;
return;
}
for (index = 1; index < FLOW_HISTORY; index++) {
book->history[index - 1] = book->history[index];
}
book->history[FLOW_HISTORY - 1] = *record;
}
void flow_init(flow_book *book, flow_port port)
{
memset(book, 0, sizeof(*book));
book->port = port;
book->policy = FLOW_JOIN;
book->next_serial = 1;
}
void flow_reset(flow_book *book)
{
flow_port port = book->port;
unsigned index;
for (index = 0; index < FLOW_CAPACITY; index++) clear_record(&book->active[index]);
for (index = 0; index < FLOW_HISTORY; index++) clear_record(&book->history[index]);
book->active_count = 0;
book->history_count = 0;
book->next_serial = 1;
book->submitted = 0;
book->created = 0;
book->create_failed = 0;
book->joined = 0;
book->detached = 0;
book->joined_total = 0;
book->policy = FLOW_JOIN;
book->port = port;
}
int flow_set_policy(flow_book *book, flow_policy policy)
{
if (!book || (policy != FLOW_JOIN && policy != FLOW_DETACH)) return -1;
book->policy = policy;
return 0;
}
int flow_submit(flow_book *book, int id, int payload, const char *label)
{
flow_record *record;
if (!book || id < 1 || id > 9999 || payload < -10000 || payload > 10000) return -1;
if (!label_valid(label)) return -1;
if (book->active_count == FLOW_CAPACITY) return -2;
if (id_active(book, id) || id_history(book, id)) return -3;
if (book->next_serial == UINT_MAX || book->submitted == UINT_MAX) return -4;
record = &book->active[book->active_count];
clear_record(record);
record->id = id;
record->payload = payload;
strcpy(record->label, label);
record->policy = book->policy;
record->state = FLOW_QUEUED;
record->serial = book->next_serial++;
book->active_count++;
book->submitted++;
return 0;
}
static int dispatch_one(flow_book *book, flow_record *record)
{
uintptr_t token = 0;
int result = 0;
int rc;
if (!book->port.create || !book->port.join || !book->port.detach) return -5;
rc = book->port.create(book->port.context, record->id, record->payload, &token);
if (rc != 0) {
book->create_failed++;
record->state = FLOW_CREATE_FAILED;
record->result = rc;
return 0;
}
book->created++;
record->token = token;
if (record->policy == FLOW_JOIN) {
rc = book->port.join(book->port.context, token, &result);
if (rc != 0) return -6;
record->result = result;
record->state = FLOW_JOINED;
book->joined++;
book->joined_total += 7;
return 0;
}
if (record->policy == FLOW_DETACH) {
rc = book->port.detach(book->port.context, token);
if (rc != 0) return -6;
record->state = FLOW_DETACHED;
book->detached++;
return 0;
}
return -7;
}
int flow_drain(flow_book *book)
{
unsigned index;
if (!book) return -1;
for (index = 0; index < book->active_count; index++) {
int rc = dispatch_one(book, &book->active[index]);
if (rc != 0) return rc;
record_history(book, &book->active[index]);
clear_record(&book->active[index]);
}
book->active_count = 0;
return 0;
}
int flow_find(const flow_book *book, int id, flow_record *record)
{
unsigned index;
if (!book || !record) return -1;
for (index = 0; index < book->active_count; index++) {
if (book->active[index].id == id) {
*record = book->active[index];
return 0;
}
}
for (index = book->history_count; index > 0; index--) {
if (book->history[index - 1].id == id) {
*record = book->history[index - 1];
return 0;
}
}
return -2;
}
int flow_history(const flow_book *book, unsigned index, flow_record *record)
{
if (!book || !record || index >= book->history_count) return -1;
*record = book->history[index];
return 0;
}
int flow_audit(const flow_book *book)
{
unsigned index;
unsigned observed_created = 0;
unsigned observed_failed = 0;
unsigned observed_joined = 0;
unsigned observed_detached = 0;
int64_t observed_total = 0;
if (!book || book->active_count > FLOW_CAPACITY || book->history_count > FLOW_HISTORY) return -1;
if (book->created + book->create_failed > book->submitted) return -2;
if (book->joined + book->detached > book->created) return -3;
for (index = 0; index < book->history_count; index++) {
const flow_record *record = &book->history[index];
if (record->state == FLOW_CREATE_FAILED) observed_failed++;
else if (record->state == FLOW_JOINED) {
observed_created++;
observed_joined++;
observed_total += record->result;
} else if (record->state == FLOW_DETACHED) {
observed_created++;
observed_detached++;
} else return -4;
}
if (book->submitted < book->active_count + book->history_count) return -5;
if (book->submitted == book->active_count + book->history_count &&
(observed_created != book->created || observed_failed != book->create_failed ||
observed_joined != book->joined || observed_detached != book->detached ||
observed_total != book->joined_total)) return -6;
return 0;
}