omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / runtime / rx_sp_workloads.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * rx_sp_workloads.c -- planted-answer Cortex histories, the needs Omega |
| 19 | * compiles for them, and the consumer that answers them. See the header. |
| 20 | */ |
| 21 | #include "rx_sp_workloads.h" |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | const char *const sp_workload_names[W_COUNT] = { |
| 27 | "explain_regression", "verify_cost_claim", "plan_next_experiment", |
| 28 | "branch_what_if", "assess_goal_with_uncertainty" |
| 29 | }; |
| 30 | |
| 31 | #define STEP 10u |
| 32 | #define SAMPLES 8u |
| 33 | #define GEN_PERIOD 1400u |
| 34 | #define BRANCH_B 7u |
| 35 | #define BRANCH_C 9u |
| 36 | |
| 37 | /* ---- deterministic randomness ---- */ |
| 38 | |
| 39 | typedef struct { uint64_t s; } Rng; |
| 40 | |
| 41 | static uint64_t rn(Rng *r) { |
| 42 | r->s ^= r->s >> 12; |
| 43 | r->s ^= r->s << 25; |
| 44 | r->s ^= r->s >> 27; |
| 45 | return r->s * 2685821657736338717ull; |
| 46 | } |
| 47 | |
| 48 | static uint64_t rb(Rng *r, uint64_t n) { return n ? rn(r) % n : 0; } |
| 49 | |
| 50 | static uint64_t isqrt(uint64_t x) { |
| 51 | uint64_t r = 0, b = 1ull << 62; |
| 52 | while (b > x) b >>= 2; |
| 53 | while (b) { |
| 54 | if (x >= r + b) { |
| 55 | x -= r + b; |
| 56 | r = (r >> 1) + b; |
| 57 | } else { |
| 58 | r >>= 1; |
| 59 | } |
| 60 | b >>= 2; |
| 61 | } |
| 62 | return r; |
| 63 | } |
| 64 | |
| 65 | /* ---- events for one subject, appended in time order ---- */ |
| 66 | |
| 67 | typedef struct { |
| 68 | uint64_t t; |
| 69 | uint32_t order, cls, kind, branch, protect, n; |
| 70 | uint64_t tag; |
| 71 | int32_t ref[CX_LINKS]; /* event index in this subject, -1 = none */ |
| 72 | uint64_t pl[16]; |
| 73 | uint64_t id; /* Cortex id after append */ |
| 74 | } Ev; |
| 75 | |
| 76 | typedef struct { |
| 77 | Ev *e; |
| 78 | uint32_t n, cap; |
| 79 | uint32_t n_int; /* telemetry intervals */ |
| 80 | uint64_t *mu; /* level per interval */ |
| 81 | uint64_t *pct; /* multiplier per interval, percent */ |
| 82 | uint64_t (*samp)[SAMPLES]; |
| 83 | int32_t *tel; /* telemetry event per interval */ |
| 84 | } Subj; |
| 85 | |
| 86 | static int32_t ev_add(Subj *s, uint32_t cls, uint32_t kind, uint64_t t, uint32_t order) { |
| 87 | if (s->n == s->cap) { |
| 88 | s->cap = s->cap ? s->cap * 2 : 1024; |
| 89 | s->e = realloc(s->e, s->cap * sizeof(Ev)); |
| 90 | } |
| 91 | Ev *e = &s->e[s->n]; |
| 92 | memset(e, 0, sizeof(*e)); |
| 93 | e->t = t; |
| 94 | e->order = order; |
| 95 | e->cls = cls; |
| 96 | e->kind = kind; |
| 97 | for (uint32_t i = 0; i < CX_LINKS; i++) e->ref[i] = -1; |
| 98 | return (int32_t)s->n++; |
| 99 | } |
| 100 | |
| 101 | static void fill(Rng *r, Ev *e, uint32_t n) { |
| 102 | e->n = n; |
| 103 | for (uint32_t i = 0; i < n; i++) e->pl[i] = rn(r) >> 16; |
| 104 | } |
| 105 | |
| 106 | /* A realization: verification receipt, commit receipt, the realization, and |
| 107 | * the cost claim made for it. Returns the realization's event index. */ |
| 108 | static int32_t add_real(Subj *s, Rng *r, uint64_t t, uint64_t number, int verified, uint64_t mu, |
| 109 | int32_t *claim_out) { |
| 110 | int32_t v = ev_add(s, CX_EVIDENCE, K_VERIFY, t, 0); |
| 111 | s->e[v].protect = CX_PROT_VERIFY_EVIDENCE; |
| 112 | s->e[v].tag = verified ? PJ_VERIFIED : 2; |
| 113 | fill(r, &s->e[v], 8); |
| 114 | s->e[v].pl[0] = number; |
| 115 | s->e[v].pl[1] = s->e[v].tag; |
| 116 | int32_t c = ev_add(s, CX_EXECUTION, K_COMMIT, t, 1); |
| 117 | s->e[c].protect = CX_PROT_COMMIT_RECEIPT; |
| 118 | fill(r, &s->e[c], 4); |
| 119 | s->e[c].pl[0] = number; |
| 120 | int32_t z = ev_add(s, CX_REALIZATION, K_REALIZATION, t, 2); |
| 121 | s->e[z].tag = number; |
| 122 | fill(r, &s->e[z], 8); |
| 123 | s->e[z].pl[0] = number; |
| 124 | s->e[z].pl[1] = mu; |
| 125 | s->e[z].ref[0] = v; |
| 126 | s->e[z].ref[1] = c; |
| 127 | int32_t cl = ev_add(s, CX_CLAIM, K_COST_CLAIM, t, 3); |
| 128 | s->e[cl].tag = mu * 115 / 100; |
| 129 | fill(r, &s->e[cl], 4); |
| 130 | s->e[cl].pl[0] = number; |
| 131 | s->e[cl].pl[1] = s->e[cl].tag; |
| 132 | s->e[cl].ref[0] = z; |
| 133 | s->e[cl].ref[1] = v; |
| 134 | if (claim_out) *claim_out = cl; |
| 135 | return z; |
| 136 | } |
| 137 | |
| 138 | static const Ev *g_sort_ev; |
| 139 | |
| 140 | static int cmp_idx(const void *a, const void *b) { |
| 141 | const Ev *x = &g_sort_ev[*(const uint32_t *)a], *y = &g_sort_ev[*(const uint32_t *)b]; |
| 142 | if (x->t != y->t) return x->t < y->t ? -1 : 1; |
| 143 | if (x->order != y->order) return x->order < y->order ? -1 : 1; |
| 144 | return *(const uint32_t *)a < *(const uint32_t *)b ? -1 : 1; |
| 145 | } |
| 146 | |
| 147 | /* Append in time order. Events stay where they are, so saved indices hold. */ |
| 148 | static int subj_append(CxStore *st, Subj *s, uint64_t subject) { |
| 149 | uint32_t n = s->n; |
| 150 | uint32_t *order = malloc((n ? n : 1) * sizeof(uint32_t)); |
| 151 | for (uint32_t i = 0; i < n; i++) order[i] = i; |
| 152 | g_sort_ev = s->e; |
| 153 | qsort(order, n, sizeof(uint32_t), cmp_idx); |
| 154 | uint32_t *pos = order; |
| 155 | for (uint32_t j = 0; j < n; j++) { |
| 156 | Ev *e = &s->e[order[j]]; |
| 157 | CxHeader h; |
| 158 | memset(&h, 0, sizeof h); |
| 159 | h.cls = e->cls; |
| 160 | h.kind = e->kind; |
| 161 | h.subject = subject; |
| 162 | h.t = e->t; |
| 163 | h.generation = e->t / GEN_PERIOD + 1; |
| 164 | h.branch = e->branch; |
| 165 | h.protect = e->protect; |
| 166 | h.tag = e->tag; |
| 167 | for (uint32_t k = 0; k < CX_LINKS; k++) { |
| 168 | if (e->ref[k] < 0) continue; |
| 169 | if (!s->e[e->ref[k]].id) { free(pos); return -1; } /* link to a later object */ |
| 170 | h.links[k] = s->e[e->ref[k]].id; |
| 171 | } |
| 172 | if (cx_append(st, &h, e->pl, e->n, &e->id) != CX_OK) { free(pos); return -1; } |
| 173 | } |
| 174 | free(pos); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static void subj_free(Subj *s) { |
| 179 | free(s->e); |
| 180 | free(s->mu); |
| 181 | free(s->pct); |
| 182 | free(s->samp); |
| 183 | free(s->tel); |
| 184 | memset(s, 0, sizeof(*s)); |
| 185 | } |
| 186 | |
| 187 | static uint64_t imean(const uint64_t *w) { |
| 188 | uint64_t sum = 0; |
| 189 | for (uint32_t k = 0; k < SAMPLES; k++) sum += w[k]; |
| 190 | return sum / SAMPLES; |
| 191 | } |
| 192 | |
| 193 | /* ---- per-task plan, decided before any history is written ---- */ |
| 194 | |
| 195 | typedef struct { |
| 196 | uint64_t X, deps[2]; |
| 197 | uint64_t T; |
| 198 | /* W_EXPLAIN */ |
| 199 | uint64_t t_v, t_r; |
| 200 | uint32_t cause, ratio, hyp_matches, unverified_dep_distractor; |
| 201 | /* W_VERIFY */ |
| 202 | uint64_t t_c; |
| 203 | uint32_t verdict, bad_kind; |
| 204 | /* W_PLAN */ |
| 205 | uint32_t allowed, old_mask, explored_old, explored_new; |
| 206 | /* W_BRANCH, W_ASSESS */ |
| 207 | uint32_t goal_case; |
| 208 | } Plan; |
| 209 | |
| 210 | static const uint32_t g_ratios[4] = { 130, 150, 180, 220 }; |
| 211 | |
| 212 | /* Realization schedule, levels and multipliers for one subject. */ |
| 213 | static void schedule(Subj *s, Rng *r, const Plan *pl, uint64_t subject, uint32_t workload, |
| 214 | int32_t *real_idx, uint32_t *n_real, int32_t *claim_idx) { |
| 215 | const uint64_t T = pl->T; |
| 216 | int is_x = subject == pl->X, is_dep = subject == pl->deps[0] || subject == pl->deps[1]; |
| 217 | uint64_t stop = T + 1; /* no natural realization at or after */ |
| 218 | if (is_x && workload == W_EXPLAIN) stop = pl->t_v - 300; |
| 219 | if (is_x && workload == W_VERIFY) stop = pl->t_c - 300; |
| 220 | if (is_dep && workload == W_EXPLAIN) stop = pl->t_v - 300; |
| 221 | uint64_t times[64]; |
| 222 | int ver[64]; |
| 223 | uint32_t n = 0; |
| 224 | for (uint64_t t = 0; t < stop && n < 60; t += (400 + rb(r, 500)) / STEP * STEP) { |
| 225 | times[n] = t; |
| 226 | ver[n] = t == 0 || rb(r, 5) != 0; |
| 227 | n++; |
| 228 | } |
| 229 | if (is_x && workload == W_EXPLAIN) { times[n] = pl->t_v; ver[n] = 1; n++; } |
| 230 | if (is_x && workload == W_VERIFY) { times[n] = pl->t_c; ver[n] = pl->bad_kind != 1; n++; } |
| 231 | s->n_int = (uint32_t)(T / STEP) + 1; |
| 232 | s->mu = calloc(s->n_int, sizeof(uint64_t)); |
| 233 | s->pct = calloc(s->n_int, sizeof(uint64_t)); |
| 234 | s->tel = malloc(s->n_int * sizeof(int32_t)); |
| 235 | for (uint32_t i = 0; i < s->n_int; i++) { s->pct[i] = 100; s->tel[i] = -1; } |
| 236 | *n_real = 0; |
| 237 | for (uint32_t k = 0; k < n; k++) { |
| 238 | uint64_t mu = 800 + rb(r, 1200); |
| 239 | int32_t cl = -1; |
| 240 | real_idx[(*n_real)++] = add_real(s, r, times[k], k + 1, ver[k], mu, &cl); |
| 241 | if (claim_idx) *claim_idx = cl; |
| 242 | uint64_t end = k + 1 < n ? times[k + 1] : T + 1; |
| 243 | for (uint64_t t = times[k]; t < end && t <= T; t += STEP) s->mu[t / STEP] = mu; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static void telemetry(Subj *s, Rng *r) { |
| 248 | s->samp = calloc(s->n_int, sizeof(*s->samp)); |
| 249 | for (uint32_t i = 1; i < s->n_int; i++) { |
| 250 | uint64_t level = s->mu[i] * s->pct[i] / 100; |
| 251 | int32_t e = ev_add(s, CX_OBSERVATION, K_TELEMETRY, (uint64_t)i * STEP, 5); |
| 252 | s->e[e].n = SAMPLES; |
| 253 | for (uint32_t k = 0; k < SAMPLES; k++) { |
| 254 | uint64_t v = level * (960 + rb(r, 81)) / 1000; |
| 255 | s->samp[i][k] = v; |
| 256 | s->e[e].pl[k] = v; |
| 257 | } |
| 258 | s->tel[i] = e; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* Background facts every subject has. Quiet rules keep the planted answer true. */ |
| 263 | static void background(Subj *s, Rng *r, const Plan *pl, uint64_t subject, uint32_t workload, uint32_t S) { |
| 264 | const uint64_t T = pl->T; |
| 265 | int is_x = subject == pl->X; |
| 266 | /* dependencies */ |
| 267 | for (uint32_t k = 0; k < 2; k++) { |
| 268 | int32_t e = ev_add(s, CX_RELATIONSHIP, K_DEPENDS, 0, 0); |
| 269 | uint64_t dep = is_x ? pl->deps[k] : 1 + rb(r, S); |
| 270 | if (dep == subject) dep = dep % S + 1; |
| 271 | s->e[e].tag = dep; |
| 272 | fill(r, &s->e[e], 2); |
| 273 | } |
| 274 | /* resource changes */ |
| 275 | for (uint64_t t = 200 + rb(r, 300) / STEP * STEP; t <= T; t += (300 + rb(r, 400)) / STEP * STEP) { |
| 276 | if (is_x && workload == W_EXPLAIN && t >= pl->t_v) break; |
| 277 | int32_t e = ev_add(s, CX_OBSERVATION, K_RESOURCE, t + 3, 6); |
| 278 | s->e[e].tag = 1 + rb(r, 4); |
| 279 | fill(r, &s->e[e], 4); |
| 280 | s->e[e].pl[0] = 100; /* main-line factor: already in the telemetry */ |
| 281 | } |
| 282 | /* prior regressions */ |
| 283 | for (uint32_t k = 0; k < 4; k++) { |
| 284 | uint64_t t = (300 + rb(r, T - 1200)) / STEP * STEP + 4; |
| 285 | uint64_t sig; |
| 286 | do sig = 11 + rb(r, 13); while (is_x && workload == W_EXPLAIN && sig == (pl->ratio + 5) / 10); |
| 287 | int32_t e = ev_add(s, CX_FAILURE, K_REGRESSION, t, 7); |
| 288 | s->e[e].tag = sig; |
| 289 | fill(r, &s->e[e], 8); |
| 290 | s->e[e].pl[0] = 1 + rb(r, 3); |
| 291 | } |
| 292 | /* hypotheses: tag = (state << 8) | cause, state 1 open, 2 closed */ |
| 293 | for (uint32_t k = 0; k < 3; k++) { |
| 294 | uint64_t t = (100 + rb(r, T - 200)) / STEP * STEP + 5; |
| 295 | int32_t e = ev_add(s, CX_CLAIM, K_HYPOTHESIS, t, 8); |
| 296 | uint64_t cause = 1 + rb(r, 4); |
| 297 | if (is_x && workload == W_EXPLAIN && cause == pl->cause) cause = cause % 4 + 1; |
| 298 | s->e[e].tag = (2ull << 8) | cause; |
| 299 | fill(r, &s->e[e], 4); |
| 300 | } |
| 301 | /* plans: tag = condition key 1..16 */ |
| 302 | if (!(is_x && workload == W_PLAN)) |
| 303 | for (uint32_t k = 0; k < 6; k++) { |
| 304 | int32_t e = ev_add(s, CX_PLAN, K_PLAN, (50 + rb(r, T - 100)) / STEP * STEP + 6, 9); |
| 305 | s->e[e].tag = 1 + rb(r, 16); |
| 306 | fill(r, &s->e[e], 16); |
| 307 | } |
| 308 | /* authority grants */ |
| 309 | if (!(is_x && workload == W_PLAN)) |
| 310 | for (uint32_t k = 0; k < 3; k++) { |
| 311 | int32_t e = ev_add(s, CX_EVIDENCE, K_AUTHORITY, (k * T / 3) / STEP * STEP + 7, 10); |
| 312 | s->e[e].protect = CX_PROT_AUTHORITY; |
| 313 | s->e[e].tag = k + 1; |
| 314 | fill(r, &s->e[e], 4); |
| 315 | s->e[e].pl[0] = rn(r) & 0xffff; |
| 316 | } |
| 317 | /* effect receipts */ |
| 318 | for (uint32_t k = 0; k < 3; k++) { |
| 319 | int32_t e = ev_add(s, CX_EXECUTION, K_EFFECT, (rb(r, T)) / STEP * STEP + 8, 11); |
| 320 | s->e[e].protect = CX_PROT_EFFECT_RECEIPT; |
| 321 | fill(r, &s->e[e], 6); |
| 322 | } |
| 323 | /* goal */ |
| 324 | int32_t g = ev_add(s, CX_ENTITY, K_GOAL, 0, 12); |
| 325 | s->e[g].tag = 500 + rb(r, 3000); |
| 326 | fill(r, &s->e[g], 2); |
| 327 | } |
| 328 | |
| 329 | static void need_id(SpTask *t, uint64_t id) { |
| 330 | if (id && t->n_needed < SP_MAX_NEEDED) t->needed[t->n_needed++] = id; |
| 331 | } |
| 332 | |
| 333 | int sp_build(CxStore *st, uint32_t workload, uint32_t seed, const SpScale *sc, SpTask *t) { |
| 334 | const uint32_t S = sc->subjects; |
| 335 | const uint64_t T = sc->t_end; |
| 336 | Rng r = { 0x9E3779B97F4A7C15ull ^ ((uint64_t)workload << 40) ^ ((uint64_t)seed * 0x100000001B3ull) }; |
| 337 | for (int i = 0; i < 8; i++) rn(&r); |
| 338 | memset(t, 0, sizeof(*t)); |
| 339 | t->workload = workload; |
| 340 | t->seed = seed; |
| 341 | t->t_now = T; |
| 342 | |
| 343 | Plan pl; |
| 344 | memset(&pl, 0, sizeof pl); |
| 345 | pl.T = T; |
| 346 | pl.X = 1 + rb(&r, S); |
| 347 | pl.deps[0] = pl.X % S + 1; |
| 348 | pl.deps[1] = (pl.X + S / 2) % S + 1; |
| 349 | if (pl.deps[1] == pl.X || pl.deps[1] == pl.deps[0]) pl.deps[1] = (pl.X + 2) % S + 1; |
| 350 | t->subject = pl.X; |
| 351 | t->deps[0] = pl.deps[0]; |
| 352 | t->deps[1] = pl.deps[1]; |
| 353 | |
| 354 | switch (workload) { |
| 355 | case W_EXPLAIN: |
| 356 | pl.t_v = (T - 1200) / STEP * STEP; |
| 357 | pl.t_r = pl.t_v + 500 + rb(&r, 30) * STEP; |
| 358 | pl.cause = 1 + seed % 4; |
| 359 | pl.ratio = g_ratios[(seed / 4) % 4]; |
| 360 | pl.hyp_matches = (seed / 16) % 2 == 0; |
| 361 | pl.unverified_dep_distractor = seed % 3 == 0; |
| 362 | break; |
| 363 | case W_VERIFY: |
| 364 | pl.t_c = (T - 1500) / STEP * STEP; |
| 365 | pl.verdict = 1 + seed % 3; |
| 366 | pl.bad_kind = pl.verdict == V_UNVERIFIABLE ? 1 + (seed / 3) % 2 : 0; |
| 367 | break; |
| 368 | case W_PLAN: { |
| 369 | pl.allowed = 0; |
| 370 | while (__builtin_popcount(pl.allowed) < 6) pl.allowed |= 1u << rb(&r, 16); |
| 371 | uint32_t lo = (uint32_t)__builtin_ctz(pl.allowed); |
| 372 | pl.explored_old = 1u << lo; /* the lowest allowed key was explored long ago */ |
| 373 | for (int k = 0; k < 3; k++) pl.explored_old |= 1u << rb(&r, 16); |
| 374 | pl.explored_new = 0; |
| 375 | for (int k = 0; k < 3; k++) pl.explored_new |= 1u << rb(&r, 16); |
| 376 | if (seed % 5 == 4) pl.explored_new |= pl.allowed; /* nothing left to try */ |
| 377 | do pl.old_mask = (uint32_t)(rn(&r) & 0xffff); while (pl.old_mask == pl.allowed); |
| 378 | break; |
| 379 | } |
| 380 | default: |
| 381 | pl.goal_case = seed % 3; |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | if (cx_init(st, S + 1) != CX_OK) return -1; |
| 386 | |
| 387 | /* the machine: generations */ |
| 388 | Subj m; |
| 389 | memset(&m, 0, sizeof m); |
| 390 | int32_t gen_ev[64]; |
| 391 | uint32_t n_gen = 0; |
| 392 | for (uint64_t gt = 0; gt <= T && n_gen < 64; gt += GEN_PERIOD) { |
| 393 | int32_t e = ev_add(&m, CX_ENTITY, K_GENERATION, gt, 0); |
| 394 | m.e[e].protect = CX_PROT_GENERATION_ID; |
| 395 | m.e[e].tag = gt / GEN_PERIOD + 1; |
| 396 | fill(&r, &m.e[e], 4); |
| 397 | m.e[e].pl[0] = m.e[e].tag; |
| 398 | gen_ev[n_gen++] = e; |
| 399 | } |
| 400 | if (subj_append(st, &m, 0) != 0) return -1; |
| 401 | uint64_t gen_id = m.e[gen_ev[n_gen - 1]].id, gen_t = m.e[gen_ev[n_gen - 1]].t; |
| 402 | subj_free(&m); |
| 403 | |
| 404 | Subj *subj = calloc(S + 1, sizeof(Subj)); |
| 405 | int32_t planted = -1, hyp_ev = -1, auth_ev = -1, goal_ev = -1, focus_ev = -1, br_ev = -1; |
| 406 | int32_t dep_real = -1, dep_rel[2] = { -1, -1 }; |
| 407 | int rc = 0; |
| 408 | for (uint64_t x = 1; x <= S && !rc; x++) { |
| 409 | Subj *s = &subj[x]; |
| 410 | int32_t real_idx[64], claim = -1; |
| 411 | uint32_t n_real = 0; |
| 412 | schedule(s, &r, &pl, x, workload, real_idx, &n_real, &claim); |
| 413 | int is_x = x == pl.X; |
| 414 | |
| 415 | if (workload == W_EXPLAIN && x == pl.deps[0]) { |
| 416 | if (pl.cause == CAUSE_DEPENDENCY) |
| 417 | dep_real = add_real(s, &r, pl.t_v + STEP + rb(&r, (pl.t_r - pl.t_v) / STEP - 1) * STEP, |
| 418 | 90, 1, 900, NULL); |
| 419 | else if (pl.unverified_dep_distractor) |
| 420 | add_real(s, &r, pl.t_v + STEP * 3, 91, 0, 900, NULL); |
| 421 | } |
| 422 | if (is_x && workload == W_EXPLAIN) { |
| 423 | for (uint64_t tt = pl.t_r; tt <= T; tt += STEP) s->pct[tt / STEP] = pl.ratio; |
| 424 | if (pl.cause == CAUSE_RESOURCE) { |
| 425 | planted = ev_add(s, CX_OBSERVATION, K_RESOURCE, |
| 426 | pl.t_v + STEP + rb(&r, (pl.t_r - pl.t_v) / STEP - 1) * STEP + 3, 6); |
| 427 | s->e[planted].tag = 2; |
| 428 | fill(&r, &s->e[planted], 4); |
| 429 | } |
| 430 | if (pl.cause == CAUSE_RECURRENCE) { |
| 431 | planted = ev_add(s, CX_FAILURE, K_REGRESSION, (300 + rb(&r, pl.t_v - 600)) / STEP * STEP + 4, 7); |
| 432 | s->e[planted].tag = (pl.ratio + 5) / 10; |
| 433 | fill(&r, &s->e[planted], 8); |
| 434 | } |
| 435 | /* an open hypothesis, matching the planted cause or not, and a closed one that does */ |
| 436 | hyp_ev = ev_add(s, CX_CLAIM, K_HYPOTHESIS, pl.t_v + 15, 8); |
| 437 | uint64_t hc = pl.hyp_matches ? pl.cause : pl.cause % 4 + 1; |
| 438 | s->e[hyp_ev].tag = (1ull << 8) | hc; |
| 439 | fill(&r, &s->e[hyp_ev], 4); |
| 440 | int32_t closed = ev_add(s, CX_CLAIM, K_HYPOTHESIS, pl.t_v + 25, 8); |
| 441 | s->e[closed].tag = (2ull << 8) | pl.cause; |
| 442 | fill(&r, &s->e[closed], 4); |
| 443 | } |
| 444 | if (is_x && workload == W_VERIFY) { |
| 445 | focus_ev = claim; |
| 446 | uint64_t mu = s->mu[pl.t_c / STEP]; |
| 447 | s->e[claim].tag = mu * 115 / 100; |
| 448 | s->e[claim].pl[1] = s->e[claim].tag; |
| 449 | if (pl.bad_kind == 2) { |
| 450 | /* the receipt verified a different realization */ |
| 451 | int32_t v = s->e[claim].ref[1]; |
| 452 | s->e[v].pl[0] = 999; |
| 453 | } |
| 454 | if (pl.verdict == V_REFUTED) { |
| 455 | uint64_t at = pl.t_c + 200 + rb(&r, 800) / STEP * STEP; |
| 456 | for (uint64_t tt = at; tt < at + 3 * STEP && tt <= T; tt += STEP) s->pct[tt / STEP] = 130; |
| 457 | } |
| 458 | } |
| 459 | if (is_x && workload == W_PLAN) { |
| 460 | for (uint32_t k = 0; k < 16; k++) { |
| 461 | if (pl.explored_old & (1u << k)) { |
| 462 | int32_t e = ev_add(s, CX_PLAN, K_PLAN, (100 + rb(&r, T / 4)) / STEP * STEP + 6, 9); |
| 463 | s->e[e].tag = k + 1; |
| 464 | fill(&r, &s->e[e], 16); |
| 465 | } |
| 466 | if (pl.explored_new & (1u << k)) { |
| 467 | int32_t e = ev_add(s, CX_PLAN, K_PLAN, (T - 900 + rb(&r, 800)) / STEP * STEP + 6, 9); |
| 468 | s->e[e].tag = k + 1; |
| 469 | fill(&r, &s->e[e], 16); |
| 470 | } |
| 471 | } |
| 472 | int32_t old = ev_add(s, CX_EVIDENCE, K_AUTHORITY, T / 3 / STEP * STEP + 7, 10); |
| 473 | s->e[old].protect = CX_PROT_AUTHORITY; |
| 474 | fill(&r, &s->e[old], 4); |
| 475 | s->e[old].pl[0] = pl.old_mask; |
| 476 | auth_ev = ev_add(s, CX_EVIDENCE, K_AUTHORITY, (T - 200) / STEP * STEP + 7, 10); |
| 477 | s->e[auth_ev].protect = CX_PROT_AUTHORITY; |
| 478 | fill(&r, &s->e[auth_ev], 4); |
| 479 | s->e[auth_ev].pl[0] = pl.allowed; |
| 480 | } |
| 481 | |
| 482 | telemetry(s, &r); |
| 483 | background(s, &r, &pl, x, workload, S); |
| 484 | |
| 485 | if (is_x && workload == W_BRANCH) { |
| 486 | t->t0 = T - 110; |
| 487 | uint64_t sum = 0, cnt = 0; |
| 488 | for (uint64_t tt = (t->t0 + STEP - 1) / STEP * STEP; tt <= T; tt += STEP) |
| 489 | for (uint32_t k = 0; k < SAMPLES; k++) { sum += s->samp[tt / STEP][k]; cnt++; } |
| 490 | uint64_t mean = sum / cnt; |
| 491 | uint64_t fb = pl.goal_case == 0 ? 70 : pl.goal_case == 1 ? 140 : 95; |
| 492 | uint64_t fc = fb < 100 ? 140 : 70; |
| 493 | br_ev = ev_add(s, CX_OBSERVATION, K_RESOURCE, T - 5, 13); |
| 494 | s->e[br_ev].branch = BRANCH_B; |
| 495 | fill(&r, &s->e[br_ev], 4); |
| 496 | s->e[br_ev].pl[0] = fb; |
| 497 | int32_t c2 = ev_add(s, CX_OBSERVATION, K_RESOURCE, T - 5, 14); |
| 498 | s->e[c2].branch = BRANCH_C; |
| 499 | fill(&r, &s->e[c2], 4); |
| 500 | s->e[c2].pl[0] = fc; |
| 501 | goal_ev = ev_add(s, CX_ENTITY, K_GOAL, T - 3, 15); |
| 502 | s->e[goal_ev].tag = mean; /* target: today's mean */ |
| 503 | fill(&r, &s->e[goal_ev], 2); |
| 504 | uint64_t proj = mean * fb / 100; |
| 505 | t->truth.a[0] = proj <= mean ? G_MET : G_UNMET; |
| 506 | t->truth.a[1] = proj; |
| 507 | t->branch = BRANCH_B; |
| 508 | } |
| 509 | if (is_x && workload == W_ASSESS) { |
| 510 | t->t0 = gen_t; |
| 511 | uint64_t S1 = 0, Q = 0, n = 0; |
| 512 | for (uint64_t tt = (gen_t + STEP - 1) / STEP * STEP; tt <= T; tt += STEP) |
| 513 | for (uint32_t k = 0; k < SAMPLES; k++) { |
| 514 | uint64_t v = s->samp[tt / STEP][k]; |
| 515 | S1 += v; |
| 516 | Q += v * v; |
| 517 | n++; |
| 518 | } |
| 519 | uint64_t mean = S1 / n, var = Q / n - mean * mean, se = isqrt(var / n); |
| 520 | uint64_t target = pl.goal_case == 0 ? mean + 2 * se + 20 |
| 521 | : pl.goal_case == 1 ? (mean > 2 * se + 20 ? mean - 2 * se - 20 : 1) : mean; |
| 522 | goal_ev = ev_add(s, CX_ENTITY, K_GOAL, T - 3, 15); |
| 523 | s->e[goal_ev].tag = target; |
| 524 | fill(&r, &s->e[goal_ev], 2); |
| 525 | t->truth.a[0] = mean + 2 * se <= target ? G_MET : mean >= target + 2 * se + 1 ? G_UNMET : G_UNCERTAIN; |
| 526 | t->truth.a[1] = mean; |
| 527 | t->truth.a[2] = se; |
| 528 | } |
| 529 | if (is_x && workload == W_EXPLAIN) { |
| 530 | /* relationships to the planted dependencies */ |
| 531 | for (uint32_t i = 0; i < s->n; i++) |
| 532 | if (s->e[i].kind == K_DEPENDS) { |
| 533 | if (dep_rel[0] < 0) dep_rel[0] = (int32_t)i; |
| 534 | else if (dep_rel[1] < 0) dep_rel[1] = (int32_t)i; |
| 535 | } |
| 536 | } |
| 537 | rc = subj_append(st, s, x); |
| 538 | } |
| 539 | if (rc) goto out; |
| 540 | |
| 541 | Subj *X = &subj[pl.X]; |
| 542 | /* ---- truth and the objects it rests on ---- */ |
| 543 | if (workload == W_EXPLAIN) { |
| 544 | /* The generator's own arithmetic over the samples it wrote. */ |
| 545 | uint64_t base = 0, nb = 0, post = 0, np = 0; |
| 546 | for (uint64_t tt = pl.t_v; tt <= T; tt += STEP) { |
| 547 | uint32_t i = (uint32_t)(tt / STEP); |
| 548 | if (i == 0) continue; |
| 549 | if (tt < pl.t_r) { base += imean(X->samp[i]); nb++; } |
| 550 | else { post += imean(X->samp[i]); np++; } |
| 551 | need_id(t, X->e[X->tel[i]].id); |
| 552 | } |
| 553 | t->truth.a[0] = pl.cause; |
| 554 | t->truth.a[2] = (post / np) * 100 / (base / nb); |
| 555 | t->truth.a[3] = pl.t_r; |
| 556 | if (pl.cause == CAUSE_RESOURCE || pl.cause == CAUSE_RECURRENCE) t->truth.a[1] = X->e[planted].id; |
| 557 | if (pl.cause == CAUSE_DEPENDENCY) { |
| 558 | Subj *D = &subj[pl.deps[0]]; |
| 559 | t->truth.a[1] = D->e[dep_real].id; |
| 560 | need_id(t, D->e[dep_real].id); |
| 561 | need_id(t, D->e[D->e[dep_real].ref[0]].id); |
| 562 | for (int k = 0; k < 2; k++) |
| 563 | if (dep_rel[k] >= 0 && X->e[dep_rel[k]].tag == pl.deps[0]) need_id(t, X->e[dep_rel[k]].id); |
| 564 | } |
| 565 | need_id(t, t->truth.a[1]); |
| 566 | t->truth.a[4] = pl.hyp_matches ? X->e[hyp_ev].id : 0; |
| 567 | need_id(t, t->truth.a[4]); |
| 568 | /* the anchor realization and its receipt */ |
| 569 | for (uint32_t i = 0; i < X->n; i++) |
| 570 | if (X->e[i].kind == K_REALIZATION && X->e[i].t == pl.t_v) { |
| 571 | need_id(t, X->e[i].id); |
| 572 | need_id(t, X->e[X->e[i].ref[0]].id); |
| 573 | } |
| 574 | } else if (workload == W_VERIFY) { |
| 575 | const Ev *c = &X->e[focus_ev], *v = &X->e[c->ref[1]]; |
| 576 | t->focus = c->id; |
| 577 | t->t0 = c->t; |
| 578 | uint64_t worst = 0; |
| 579 | for (uint64_t tt = (c->t + STEP - 1) / STEP * STEP; tt <= T; tt += STEP) { |
| 580 | uint64_t m = imean(X->samp[tt / STEP]); |
| 581 | if (m > worst) worst = m; |
| 582 | need_id(t, X->e[X->tel[tt / STEP]].id); |
| 583 | } |
| 584 | uint32_t verdict = (v->tag != PJ_VERIFIED || v->pl[0] != c->pl[0]) ? V_UNVERIFIABLE |
| 585 | : worst > c->tag ? V_REFUTED : V_SUPPORTED; |
| 586 | t->truth.a[0] = verdict; |
| 587 | t->truth.a[1] = v->id; |
| 588 | t->truth.a[2] = worst; |
| 589 | need_id(t, c->id); |
| 590 | need_id(t, v->id); |
| 591 | } else if (workload == W_PLAN) { |
| 592 | uint32_t explored = pl.explored_old | pl.explored_new, left = pl.allowed & ~explored; |
| 593 | t->truth.a[0] = left ? (uint64_t)__builtin_ctz(left) + 1 : 0; |
| 594 | t->truth.a[1] = X->e[auth_ev].id; |
| 595 | need_id(t, X->e[auth_ev].id); |
| 596 | for (uint32_t i = 0; i < X->n; i++) |
| 597 | if (X->e[i].kind == K_PLAN) need_id(t, X->e[i].id); |
| 598 | } else if (workload == W_BRANCH) { |
| 599 | need_id(t, X->e[goal_ev].id); |
| 600 | need_id(t, X->e[br_ev].id); |
| 601 | for (uint64_t tt = (t->t0 + STEP - 1) / STEP * STEP; tt <= T; tt += STEP) |
| 602 | need_id(t, X->e[X->tel[tt / STEP]].id); |
| 603 | } else if (workload == W_ASSESS) { |
| 604 | need_id(t, gen_id); |
| 605 | need_id(t, X->e[goal_ev].id); |
| 606 | for (uint64_t tt = (gen_t + STEP - 1) / STEP * STEP; tt <= T; tt += STEP) |
| 607 | need_id(t, X->e[X->tel[tt / STEP]].id); |
| 608 | } |
| 609 | out: |
| 610 | for (uint64_t x = 1; x <= S; x++) subj_free(&subj[x]); |
| 611 | free(subj); |
| 612 | return rc; |
| 613 | } |
| 614 | |
| 615 | /* ---- the needs ---- */ |
| 616 | |
| 617 | void sp_need(const SpTask *t, CognitiveNeed *n) { |
| 618 | switch (t->workload) { |
| 619 | case W_EXPLAIN: |
| 620 | pj_need_init(n, PJ_OP_EXPLAIN, t->subject, t->t_now); |
| 621 | n->required_temporal_scope.anchor = PJ_A_SINCE_VERIFIED; |
| 622 | n->required_temporal_scope.realization_kind = K_REALIZATION; |
| 623 | n->required_world_scope.dependency_kind = K_DEPENDS; |
| 624 | n->evidence_requirement = PJ_E_REFS; |
| 625 | n->memory_scope = PJ_M_EPISODIC; |
| 626 | pj_need_add(n, CX_OBSERVATION, K_TELEMETRY, PJ_COMPRESSIBLE, PJ_W_SELF, PJ_T_WINDOW); |
| 627 | pj_need_add(n, CX_OBSERVATION, K_RESOURCE, PJ_PINNED, PJ_W_SELF, PJ_T_WINDOW); |
| 628 | pj_need_add(n, CX_REALIZATION, K_REALIZATION, PJ_PINNED, PJ_W_DEPENDENCIES, PJ_T_WINDOW); |
| 629 | pj_need_add(n, CX_FAILURE, K_REGRESSION, PJ_REFERENCE_ONLY, PJ_W_SELF, PJ_T_MEMORY); |
| 630 | pj_need_add(n, CX_CLAIM, K_HYPOTHESIS, PJ_RECALLABLE, PJ_W_SELF, PJ_T_MEMORY); |
| 631 | break; |
| 632 | case W_VERIFY: |
| 633 | pj_need_init(n, PJ_OP_VERIFY, t->subject, t->t_now); |
| 634 | n->focus = t->focus; |
| 635 | n->required_temporal_scope.t0 = t->t0; |
| 636 | n->evidence_requirement = PJ_E_FULL; |
| 637 | pj_need_add(n, CX_OBSERVATION, K_TELEMETRY, PJ_COMPRESSIBLE, PJ_W_SELF, PJ_T_WINDOW); |
| 638 | break; |
| 639 | case W_PLAN: |
| 640 | pj_need_init(n, PJ_OP_PLAN, t->subject, t->t_now); |
| 641 | n->memory_scope = PJ_M_EPISODIC; |
| 642 | pj_need_add(n, CX_PLAN, K_PLAN, PJ_REFERENCE_ONLY, PJ_W_SELF, PJ_T_MEMORY); |
| 643 | pj_need_add(n, CX_EVIDENCE, K_AUTHORITY, PJ_PINNED, PJ_W_SELF, PJ_T_LATEST); |
| 644 | break; |
| 645 | case W_BRANCH: |
| 646 | pj_need_init(n, PJ_OP_PREDICT, t->subject, t->t_now); |
| 647 | n->branch = t->branch; |
| 648 | n->required_temporal_scope.t0 = t->t0; |
| 649 | pj_need_add(n, CX_OBSERVATION, K_TELEMETRY, PJ_COMPRESSIBLE, PJ_W_SELF, PJ_T_WINDOW); |
| 650 | pj_need_add(n, CX_OBSERVATION, K_RESOURCE, PJ_BRANCH_LOCAL, PJ_W_SELF, PJ_T_WINDOW); |
| 651 | pj_need_add(n, CX_ENTITY, K_GOAL, PJ_LIVE, PJ_W_SELF, PJ_T_LATEST); |
| 652 | break; |
| 653 | case W_ASSESS: |
| 654 | pj_need_init(n, PJ_OP_ASSESS, t->subject, t->t_now); |
| 655 | n->required_temporal_scope.anchor = PJ_A_GENERATION; |
| 656 | n->required_temporal_scope.generation_kind = K_GENERATION; |
| 657 | n->uncertainty_requirement = PJ_U_INTERVAL; |
| 658 | pj_need_add(n, CX_OBSERVATION, K_TELEMETRY, PJ_DERIVED, PJ_W_SELF, PJ_T_WINDOW); |
| 659 | pj_need_add(n, CX_ENTITY, K_GOAL, PJ_LIVE, PJ_W_SELF, PJ_T_LATEST); |
| 660 | break; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /* ---- the consumer ---- */ |
| 665 | |
| 666 | /* Interval sum and sample count, from a full series or a summary. */ |
| 667 | static int series(const PjItem *it, SpTrace *tr, uint64_t *sum, uint64_t *count) { |
| 668 | if (it->form == PJ_FORM_SUMMARY && it->n == PJ_SUMMARY_WORDS) { |
| 669 | *count = it->w[0]; |
| 670 | *sum = it->w[1]; |
| 671 | tr->words += 2; |
| 672 | return 0; |
| 673 | } |
| 674 | if (it->form != PJ_FORM_FULL || !it->n) return -1; |
| 675 | uint64_t s = 0; |
| 676 | for (uint32_t k = 0; k < it->n; k++) s += it->w[k]; |
| 677 | tr->words += it->n; |
| 678 | *sum = s; |
| 679 | *count = it->n; |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | static void use(SpTrace *tr, const PjView *v, const PjItem *it) { |
| 684 | if (tr->used && it) tr->used[it - v->items] = 1; |
| 685 | } |
| 686 | |
| 687 | static int verified(const PjView *v, const PjItem *real, SpTrace *tr) { |
| 688 | const PjItem *ev = pj_view_find(v, real->links[0]); |
| 689 | if (!ev) return 0; |
| 690 | use(tr, v, ev); |
| 691 | return ev->kind == K_VERIFY && ev->tag == PJ_VERIFIED; |
| 692 | } |
| 693 | |
| 694 | static void reason_explain(const SpTask *t, const PjView *v, SpAnswer *out, SpTrace *tr) { |
| 695 | const uint64_t X = t->subject, now = t->t_now; |
| 696 | uint64_t deps[8]; |
| 697 | uint32_t nd = 0; |
| 698 | const PjItem *anchor = NULL; |
| 699 | /* pass 1: dependencies and the last verified realization */ |
| 700 | for (uint32_t i = 0; i < v->n; i++) { |
| 701 | const PjItem *it = &v->items[i]; |
| 702 | tr->touched++; |
| 703 | if (it->subject != X || it->branch != 0 || it->t > now) continue; |
| 704 | if (it->kind == K_DEPENDS && nd < 8) { deps[nd++] = it->tag; use(tr, v, it); } |
| 705 | if (it->kind == K_REALIZATION && (!anchor || it->t >= anchor->t) && verified(v, it, tr)) anchor = it; |
| 706 | } |
| 707 | if (!anchor) return; |
| 708 | use(tr, v, anchor); |
| 709 | const uint64_t tv = anchor->t; |
| 710 | /* pass 2 */ |
| 711 | uint32_t cap = v->n ? v->n : 1, ntel = 0, nres = 0, ndr = 0, nfail = 0, nhyp = 0; |
| 712 | const PjItem **tel = malloc(cap * sizeof(*tel)), **res = malloc(cap * sizeof(*res)), |
| 713 | **dr = malloc(cap * sizeof(*dr)), **fail = malloc(cap * sizeof(*fail)), |
| 714 | **hyp = malloc(cap * sizeof(*hyp)); |
| 715 | for (uint32_t i = 0; i < v->n; i++) { |
| 716 | const PjItem *it = &v->items[i]; |
| 717 | tr->touched++; |
| 718 | if (it->branch != 0 || it->t > now) continue; |
| 719 | if (it->subject == X) { |
| 720 | if (it->kind == K_TELEMETRY && it->t >= tv) tel[ntel++] = it; |
| 721 | else if (it->kind == K_RESOURCE && it->t > tv) res[nres++] = it; |
| 722 | else if (it->kind == K_REGRESSION) fail[nfail++] = it; |
| 723 | else if (it->kind == K_HYPOTHESIS) hyp[nhyp++] = it; |
| 724 | } else if (it->kind == K_REALIZATION && it->t > tv) { |
| 725 | for (uint32_t k = 0; k < nd; k++) |
| 726 | if (deps[k] == it->subject) dr[ndr++] = it; |
| 727 | } |
| 728 | } |
| 729 | /* onset: first interval 25% above the running baseline */ |
| 730 | uint64_t run = 0, nrun = 0, onset = 0, post = 0, npost = 0, base = 0; |
| 731 | for (uint32_t i = 0; i < ntel; i++) { |
| 732 | uint64_t s, c; |
| 733 | if (series(tel[i], tr, &s, &c) || !c) continue; |
| 734 | use(tr, v, tel[i]); |
| 735 | uint64_t m = s / c; |
| 736 | if (!onset && nrun >= 3 && m * 100 > (run / nrun) * 115) { onset = tel[i]->t; base = run / nrun; } |
| 737 | if (onset) { post += m; npost++; } |
| 738 | else { run += m; nrun++; } |
| 739 | } |
| 740 | SpAnswer a; |
| 741 | memset(&a, 0, sizeof a); |
| 742 | a.a[3] = onset; |
| 743 | if (!onset) { a.a[0] = CAUSE_DRIFT; *out = a; goto done; } |
| 744 | a.a[2] = (post / npost) * 100 / base; |
| 745 | const PjItem *cause = NULL; |
| 746 | for (uint32_t i = 0; i < nres; i++) { |
| 747 | use(tr, v, res[i]); |
| 748 | if (res[i]->t <= onset && (!cause || res[i]->t > cause->t)) cause = res[i]; |
| 749 | } |
| 750 | if (cause) a.a[0] = CAUSE_RESOURCE; |
| 751 | if (!cause) |
| 752 | for (uint32_t i = 0; i < ndr; i++) { |
| 753 | use(tr, v, dr[i]); |
| 754 | if (dr[i]->t <= onset && verified(v, dr[i], tr) && (!cause || dr[i]->t < cause->t)) cause = dr[i]; |
| 755 | } |
| 756 | if (cause && !a.a[0]) a.a[0] = CAUSE_DEPENDENCY; |
| 757 | if (!cause) |
| 758 | for (uint32_t i = 0; i < nfail; i++) { |
| 759 | use(tr, v, fail[i]); |
| 760 | if (fail[i]->tag == (a.a[2] + 5) / 10 && (!cause || fail[i]->t > cause->t)) cause = fail[i]; |
| 761 | } |
| 762 | if (cause && !a.a[0]) a.a[0] = CAUSE_RECURRENCE; |
| 763 | if (!cause) a.a[0] = CAUSE_DRIFT; |
| 764 | a.a[1] = cause ? cause->id : 0; |
| 765 | for (uint32_t i = 0; i < nhyp; i++) { |
| 766 | use(tr, v, hyp[i]); |
| 767 | if ((hyp[i]->tag >> 8) == 1 && (hyp[i]->tag & 0xff) == a.a[0] && (!a.a[4] || hyp[i]->id > a.a[4])) |
| 768 | a.a[4] = hyp[i]->id; |
| 769 | } |
| 770 | *out = a; |
| 771 | done: |
| 772 | free(tel); free(res); free(dr); free(fail); free(hyp); |
| 773 | } |
| 774 | |
| 775 | static void reason_verify(const SpTask *t, const PjView *v, SpAnswer *out, SpTrace *tr) { |
| 776 | SpAnswer a; |
| 777 | memset(&a, 0, sizeof a); |
| 778 | const PjItem *c = pj_view_find(v, t->focus); |
| 779 | tr->touched++; |
| 780 | if (!c || c->form != PJ_FORM_FULL || c->n < 2) { a.a[0] = V_UNVERIFIABLE; *out = a; return; } |
| 781 | use(tr, v, c); |
| 782 | tr->words += 2; |
| 783 | const PjItem *rc = pj_view_find(v, c->links[1]); |
| 784 | uint64_t worst = 0; |
| 785 | for (uint32_t i = 0; i < v->n; i++) { |
| 786 | const PjItem *it = &v->items[i]; |
| 787 | tr->touched++; |
| 788 | if (it->subject != t->subject || it->branch != 0 || it->kind != K_TELEMETRY || it->t < c->t || |
| 789 | it->t > t->t_now) |
| 790 | continue; |
| 791 | uint64_t s, n; |
| 792 | if (series(it, tr, &s, &n) || !n) continue; |
| 793 | use(tr, v, it); |
| 794 | if (s / n > worst) worst = s / n; |
| 795 | } |
| 796 | a.a[2] = worst; |
| 797 | if (rc) { use(tr, v, rc); a.a[1] = rc->id; } |
| 798 | if (!rc || rc->form != PJ_FORM_FULL || rc->n < 1 || rc->tag != PJ_VERIFIED || rc->w[0] != c->w[0]) |
| 799 | a.a[0] = V_UNVERIFIABLE; |
| 800 | else |
| 801 | a.a[0] = worst > c->w[1] ? V_REFUTED : V_SUPPORTED; |
| 802 | if (rc) tr->words += 1; |
| 803 | *out = a; |
| 804 | } |
| 805 | |
| 806 | static void reason_plan(const SpTask *t, const PjView *v, SpAnswer *out, SpTrace *tr) { |
| 807 | SpAnswer a; |
| 808 | memset(&a, 0, sizeof a); |
| 809 | const PjItem *auth = NULL; |
| 810 | uint32_t explored = 0; |
| 811 | for (uint32_t i = 0; i < v->n; i++) { |
| 812 | const PjItem *it = &v->items[i]; |
| 813 | tr->touched++; |
| 814 | if (it->subject != t->subject || it->branch != 0 || it->t > t->t_now) continue; |
| 815 | if (it->kind == K_AUTHORITY && (!auth || it->t > auth->t)) auth = it; |
| 816 | if (it->kind == K_PLAN && it->tag >= 1 && it->tag <= 16) { explored |= 1u << (it->tag - 1); use(tr, v, it); } |
| 817 | } |
| 818 | if (auth && auth->form == PJ_FORM_FULL && auth->n) { |
| 819 | use(tr, v, auth); |
| 820 | tr->words++; |
| 821 | uint32_t left = (uint32_t)auth->w[0] & ~explored; |
| 822 | a.a[0] = left ? (uint64_t)__builtin_ctz(left) + 1 : 0; |
| 823 | a.a[1] = auth->id; |
| 824 | } |
| 825 | *out = a; |
| 826 | } |
| 827 | |
| 828 | static void reason_branch(const SpTask *t, const PjView *v, SpAnswer *out, SpTrace *tr) { |
| 829 | SpAnswer a; |
| 830 | memset(&a, 0, sizeof a); |
| 831 | const PjItem *goal = NULL; |
| 832 | uint64_t sum = 0, cnt = 0, num = 1, den = 1; |
| 833 | const PjItem *factors[16]; |
| 834 | uint32_t nf = 0; |
| 835 | for (uint32_t i = 0; i < v->n; i++) { |
| 836 | const PjItem *it = &v->items[i]; |
| 837 | tr->touched++; |
| 838 | if (it->subject != t->subject || it->t > t->t_now) continue; |
| 839 | if (it->branch == 0 && it->kind == K_GOAL && (!goal || it->t >= goal->t)) goal = it; |
| 840 | if (it->branch == 0 && it->kind == K_TELEMETRY && it->t >= t->t0) { |
| 841 | uint64_t s, n; |
| 842 | if (!series(it, tr, &s, &n)) { sum += s; cnt += n; use(tr, v, it); } |
| 843 | } |
| 844 | if (it->branch == t->branch && it->kind == K_RESOURCE && it->t >= t->t0 && nf < 16) factors[nf++] = it; |
| 845 | } |
| 846 | for (uint32_t i = 0; i < nf; i++) { |
| 847 | if (factors[i]->form != PJ_FORM_FULL || !factors[i]->n) continue; |
| 848 | use(tr, v, factors[i]); |
| 849 | tr->words++; |
| 850 | num *= factors[i]->w[0]; |
| 851 | den *= 100; |
| 852 | } |
| 853 | if (!goal || !cnt) { *out = a; return; } |
| 854 | use(tr, v, goal); |
| 855 | uint64_t proj = (sum / cnt) * num / den; |
| 856 | a.a[0] = proj <= goal->tag ? G_MET : G_UNMET; |
| 857 | a.a[1] = proj; |
| 858 | *out = a; |
| 859 | } |
| 860 | |
| 861 | static void reason_assess(const SpTask *t, const PjView *v, SpAnswer *out, SpTrace *tr) { |
| 862 | SpAnswer a; |
| 863 | memset(&a, 0, sizeof a); |
| 864 | const PjItem *gen = NULL, *goal = NULL; |
| 865 | for (uint32_t i = 0; i < v->n; i++) { |
| 866 | const PjItem *it = &v->items[i]; |
| 867 | tr->touched++; |
| 868 | if (it->branch != 0 || it->t > t->t_now) continue; |
| 869 | if (it->subject == 0 && it->kind == K_GENERATION && (!gen || it->t > gen->t)) gen = it; |
| 870 | if (it->subject == t->subject && it->kind == K_GOAL && (!goal || it->t >= goal->t)) goal = it; |
| 871 | } |
| 872 | if (!gen || !goal) { *out = a; return; } |
| 873 | use(tr, v, gen); |
| 874 | use(tr, v, goal); |
| 875 | uint64_t S1 = 0, Q = 0, n = 0; |
| 876 | int have = 0; |
| 877 | for (uint32_t i = 0; i < v->nf; i++) { |
| 878 | const PjFeature *f = &v->feat[i]; |
| 879 | tr->touched++; |
| 880 | if (f->subject == t->subject && f->kind == K_TELEMETRY && f->t0 == gen->t && f->branch == 0) { |
| 881 | S1 = f->sum; Q = f->sumsq; n = f->count; |
| 882 | tr->words += 3; |
| 883 | have = 1; |
| 884 | break; |
| 885 | } |
| 886 | } |
| 887 | if (!have) |
| 888 | for (uint32_t i = 0; i < v->n; i++) { |
| 889 | const PjItem *it = &v->items[i]; |
| 890 | tr->touched++; |
| 891 | if (it->subject != t->subject || it->branch != 0 || it->kind != K_TELEMETRY || it->t < gen->t || |
| 892 | it->t > t->t_now || it->form != PJ_FORM_FULL) |
| 893 | continue; |
| 894 | use(tr, v, it); |
| 895 | for (uint32_t k = 0; k < it->n; k++) { S1 += it->w[k]; Q += it->w[k] * it->w[k]; } |
| 896 | n += it->n; |
| 897 | tr->words += it->n; |
| 898 | } |
| 899 | if (!n) { *out = a; return; } |
| 900 | uint64_t mean = S1 / n, var = Q / n - mean * mean, se = isqrt(var / n), target = goal->tag; |
| 901 | a.a[0] = mean + 2 * se <= target ? G_MET : mean >= target + 2 * se + 1 ? G_UNMET : G_UNCERTAIN; |
| 902 | a.a[1] = mean; |
| 903 | a.a[2] = se; |
| 904 | *out = a; |
| 905 | } |
| 906 | |
| 907 | void sp_reason(const SpTask *t, const PjView *v, SpAnswer *out, SpTrace *tr) { |
| 908 | memset(out, 0, sizeof(*out)); |
| 909 | switch (t->workload) { |
| 910 | case W_EXPLAIN: reason_explain(t, v, out, tr); break; |
| 911 | case W_VERIFY: reason_verify(t, v, out, tr); break; |
| 912 | case W_PLAN: reason_plan(t, v, out, tr); break; |
| 913 | case W_BRANCH: reason_branch(t, v, out, tr); break; |
| 914 | case W_ASSESS: reason_assess(t, v, out, tr); break; |
| 915 | } |
| 916 | } |