Skip to content
pi-c Documentation
Login

Documentation

/* S0 test-only qualification fixture: admission/commit/recovery model.
 *
 * This is deliberately tiny and NOT production recovery coverage. The fixture
 * (fixture.c) is the simulated system under test; the oracle (oracle.c) is a
 * separately written declarative invariant checker over the observable trace.
 * They share only this header: no predicate or recovery function is common.
 * No src/ runtime ABI is defined here.
 */
#ifndef PC_QUAL_H
#define PC_QUAL_H

#ifdef NDEBUG
#error "S0 qualification requires assertions; remove -DNDEBUG"
#endif

#include <stddef.h>
#include <stdint.h>

#define Q_MAX_OPS 12
#define Q_MAX_RECORDS 16
#define Q_MAX_EFFECTS 8
#define Q_MAX_TRACE 64
#define Q_MAX_CHUNKS 8
#define Q_OP_BYTES 5

typedef enum {
    Q_OP_SUBMIT = 0,
    Q_OP_COMMIT = 1,
    Q_OP_CRASH_BEFORE_COMMIT = 2,
    Q_OP_CRASH_AFTER_COMMIT = 3,
    Q_OP_EXEC_EFFECT = 4,
    Q_OP_EFFECT_DONE = 5,
    Q_OP_RESOLVE = 6,
    Q_OP_REOPEN = 7,
    Q_OP_COUNT
} q_opcode;

typedef struct {
    uint8_t opcode;
    uint16_t request_id;
    uint8_t kind;  /* submission type, used for dedup type-conflict */
    uint8_t arg;   /* safety flag / payload selector */
} q_op;

typedef enum {
    Q_STORED = 0,
    Q_REFUSED = 1,
    Q_UNKNOWN = 2
} q_outcome;

typedef struct {
    uint16_t request_id;
    uint8_t kind;
    uint8_t outcome;
    uint32_t seq;
    uint32_t attempts;
} q_record;

typedef struct {
    uint16_t request_id;
    uint8_t status;    /* 0 absent, 1 started, 2 unknown, 3 done */
    uint8_t safe;      /* replay-safe flag captured at intent time */
    uint32_t attempts;
    uint32_t payload;  /* hash of consumed provider chunks */
} q_effect;

typedef enum {
    Q_EV_NONE = 0,
    Q_EV_SUBMIT,
    Q_EV_COMMIT,
    Q_EV_REFUSED,
    Q_EV_DEDUP,
    Q_EV_CRASH,
    Q_EV_REOPEN,
    Q_EV_EFFECT_START,
    Q_EV_EFFECT_DONE,
    Q_EV_RESOLVE
} q_evkind;

typedef struct {
    uint8_t kind;
    uint8_t flags;      /* effect safety bit, crash pre/post bit */
    uint16_t request_id;
    uint32_t aux;       /* seq or chunk hash */
} q_trace_event;

typedef struct {
    q_trace_event ev[Q_MAX_TRACE];
    size_t count;
} q_trace;

typedef struct { uint8_t bytes[8]; uint8_t len; uint8_t pad[7]; } q_chunk;

typedef struct q_ctx q_ctx;

q_ctx *q_new(uint64_t seed);
void q_free(q_ctx *ctx);
void q_reset(q_ctx *ctx, uint64_t seed);

/* Observable trace + final views: the only interface the oracle may read. */
void q_run_ops(q_ctx *ctx, const q_op *ops, size_t n, q_trace *out);
const q_record *q_records(const q_ctx *ctx, size_t *n);
const q_effect *q_effects(const q_ctx *ctx, size_t *n);
uint64_t q_clock_ns(const q_ctx *ctx);

/* Deterministic inputs: seeded random stream, controlled clock, provider chunks. */
uint32_t q_prng_next(q_ctx *ctx);
void q_seeded_ops(uint64_t seed, q_op *ops, size_t cap, size_t *n);
void q_seeded_chunks(uint64_t seed, q_chunk *chunks, size_t cap, size_t *n);

/* Binary operation-sequence boundary. Returns count, or SIZE_MAX if invalid. */
size_t q_parse_ops(const uint8_t *buf, size_t len, q_op *ops, size_t cap);

/* Test-only mutant selector; 0 is correct behavior. */
void q_set_mutant(q_ctx *ctx, unsigned mutant);

typedef enum {
    Q_OK = 0,
    Q_FAIL_EFFECT_BEFORE_RECEIPT = 1,
    Q_FAIL_DUPLICATE_ADMISSION = 2,
    Q_FAIL_REPEATED_UNSAFE_EFFECT = 3,
    Q_FAIL_UNKNOWN_AS_DONE = 4,
    Q_FAIL_STATE_MISMATCH = 5
} q_verdict;

/* Declarative oracle: reads only ops, trace, and final views. */
q_verdict q_oracle_check(const q_op *ops, size_t n, const q_trace *trace,
                         const q_record *recs, size_t nrecs,
                         const q_effect *effs, size_t neffs,
                         char *detail, size_t detail_cap);

const char *q_verdict_name(q_verdict v);

#endif