#include "sentinel.h" #include #include #include #include #ifdef _WIN32 #include #else #include #include #include #include #endif #include "gguf.h" #define DEAD_RATIO 0.15f // strato "spento": attività sotto il 15% del normale #define HOT_RATIO 5.0f // strato "esploso": attività oltre 5 volte il normale #define STREAK_ALARM 6 // token consecutivi prima di dare l'allarme static const char *ROLE_NAME[] = {"analisi", "controllo", "sicurezza", "riparazione", "coordinamento"}; void sent_init(Sentinels *s) { memset(s, 0, sizeof *s); pthread_mutex_init(&s->mu, NULL); } void sent_setup(Sentinels *s, const Atlas *a, const char *const *region_names) { pthread_mutex_lock(&s->mu); bool off[SENT_MAX_GROUPS + 1] = {0}; // i presidi spenti dall'utente restano spenti for (int i = 0; i < s->n_groups; i++) if (!s->g[i].enabled && s->g[i].region <= ATLAS_MAX_REGIONS) off[s->g[i].region] = true; const int L = a->n_layer; s->n_layer = L; free(s->mu_act); free(s->sd_act); s->mu_act = calloc((size_t)L, sizeof(float)); s->sd_act = calloc((size_t)L, sizeof(float)); for (int l = 0; l < L; l++) { // attività normale: media e dispersione sugli stimoli double m = 0, m2 = 0; for (int p = 0; p < a->n_act; p++) { double v = a->layer_act[(size_t)p * L + l]; m += v; m2 += v * v; } m /= a->n_act > 0 ? a->n_act : 1; double var = m2 / (a->n_act > 0 ? a->n_act : 1) - m * m; s->mu_act[l] = (float)m; s->sd_act[l] = (float)fmax(sqrt(var > 0 ? var : 0), 0.05 * m + 1e-6); } if (!s->hash_ref) s->hash_ref = calloc((size_t)L + 1, sizeof(uint64_t)); s->n_groups = 0; SentGroup *n = &s->g[s->n_groups++]; // nucleo di coordinamento memset(n, 0, sizeof *n); snprintf(n->name, sizeof n->name, "Nucleo di coordinamento"); n->region = 0; n->first = 0; n->last = L - 1; n->enabled = !off[0]; n->ratio = -1; n->n = SENT_NUCLEUS; for (int k = 0; k < n->n; k++) n->s[k] = (Sentinel){SR_COORDINAMENTO, 0, L - 1, 0.3f, false}; snprintf(n->report, sizeof n->report, "In ascolto dei presidi."); for (int r = 1; r <= a->n_region && s->n_groups < SENT_MAX_GROUPS; r++) { const AtlasRegion *R = &a->region[r - 1]; SentGroup *g = &s->g[s->n_groups++]; memset(g, 0, sizeof *g); g->region = r; snprintf(g->name, sizeof g->name, "Presidio di %s", region_names && region_names[r] ? region_names[r] : R->name); g->first = R->first; g->last = R->last; g->enabled = !off[r]; g->ratio = -1; g->n = SENT_PER_GROUP; int span = R->last - R->first + 1; for (int k = 0; k < SENT_PER_GROUP; k++) { SentRole role = (SentRole)(k / 4); int q = k % 4; // analisi e sicurezza si dividono gli strati in quarti int f = R->first + q * span / 4, t = R->first + (q + 1) * span / 4 - 1; if (t < f) t = f; if (role == SR_CONTROLLO || role == SR_RIPARAZIONE) { f = R->first; t = R->last; } g->s[k] = (Sentinel){role, f, t, 0.1f, false}; } snprintf(g->report, sizeof g->report, "Sorveglio gli strati %d-%d.", R->first, R->last); } pthread_mutex_unlock(&s->mu); } int sent_group_of_layer(Sentinels *s, int layer) { for (int i = 1; i < s->n_groups; i++) if (layer >= s->g[i].first && layer <= s->g[i].last) return i; return layer >= s->n_layer ? 0 : -1; } void sent_set_repairing(Sentinels *s, int group, bool on) { if (group < 0 || group >= s->n_groups) return; pthread_mutex_lock(&s->mu); s->g[group].repairing = on; pthread_mutex_unlock(&s->mu); } void sent_report(Sentinels *s, int group, bool alarm, const char *text) { if (group < 0 || group >= s->n_groups) return; pthread_mutex_lock(&s->mu); SentGroup *g = &s->g[group]; g->alarm = alarm; if (!alarm) g->dead_streak = g->hot_streak = 0; if (text) snprintf(g->report, sizeof g->report, "%s", text); pthread_mutex_unlock(&s->mu); } // ---- analisi -------------------------------------------------------------------------- int sent_observe(Sentinels *s, const float *layer_mean, const bool *expected_dead, char *msg, size_t cap) { int fired = -1; pthread_mutex_lock(&s->mu); int alarms = 0; for (int i = 1; i < s->n_groups; i++) { SentGroup *g = &s->g[i]; if (!g->enabled) { for (int k = 0; k < g->n; k++) g->s[k].act = 0.02f; continue; } bool dead = false, hot = false, broken = false; int where = -1; for (int k = 0; k < g->n; k++) { Sentinel *t = &g->s[k]; if (t->role == SR_ANALISI) { float dev = 0; bool bad = false; for (int l = t->first; l <= t->last; l++) { if (expected_dead && expected_dead[l]) continue; float m = layer_mean[l], mu = s->mu_act[l]; if (!isfinite(m)) { broken = bad = true; where = l; continue; } if (mu > 0 && m < DEAD_RATIO * mu) { dead = bad = true; where = l; } if (mu > 0 && m > HOT_RATIO * mu) { hot = bad = true; where = l; } float d = fabsf(m - mu) / s->sd_act[l]; if (d > dev) dev = d; } t->alarm = bad; t->act = bad ? 1.0f : 0.25f + 0.75f * fminf(1.0f, dev / 4.0f); } else if (t->role == SR_CONTROLLO) { t->act = g->ratio < 0 ? 0.3f : fminf(1.0f, fmaxf(0.1f, g->ratio)); t->alarm = g->ratio >= 0 && g->alarm && g->ratio < 0.5f; } else if (t->role == SR_SICUREZZA) { t->act = g->weights_bad ? 1.0f : 0.15f; t->alarm = g->weights_bad; } else { t->act = g->repairing ? 1.0f : 0.08f; t->alarm = false; } } g->dead_streak = dead ? g->dead_streak + 1 : 0; g->hot_streak = hot ? g->hot_streak + 1 : 0; if (!g->alarm && (broken || g->dead_streak >= STREAK_ALARM || g->hot_streak >= STREAK_ALARM)) { g->alarm = true; snprintf(g->report, sizeof g->report, "%s allo strato %d: %s.", broken ? "Valori corrotti" : dead ? "Attività spenta" : "Attività esplosa", where, broken ? "numeri non validi nei neuroni" : dead ? "sotto il 15% del normale" : "oltre 5 volte il normale"); if (fired < 0) { fired = i; snprintf(msg, cap, "%s: %s", g->name, g->report); } } alarms += g->alarm; } SentGroup *n = &s->g[0]; for (int k = 0; k < n->n; k++) n->s[k].act = n->enabled ? (alarms ? 1.0f : 0.35f) : 0.02f; pthread_mutex_unlock(&s->mu); return fired; } // ---- controllo funzionale ---------------------------------------------------------------- int sent_control(Sentinels *s, Brain *b, const Atlas *a, const float *expected) { BrainInfo info = brain_info(b); int low = 0; for (int r = 1; r <= a->n_region; r++) { int gi = -1; pthread_mutex_lock(&s->mu); for (int i = 1; i < s->n_groups; i++) if (s->g[i].region == r && s->g[i].enabled) gi = i; pthread_mutex_unlock(&s->mu); const AtlasRegion *R = &a->region[r - 1]; if (gi < 0 || R->resp_base <= 0) continue; const AtlasCategory *c = &a->cat[R->resp_cat]; double sum = 0; int prompts = 0; for (int p = 0; p < c->n_prompts; p++) { // stessi testi della misura di riferimento brain_accum_begin(b); brain_feed(b, c->prompts[p]); long nt; const double *acc = brain_accum(b, &nt); brain_accum_end(b); if (nt <= 0) continue; double ps = 0; long pn = 0; for (long k = 0; k < a->n_sel; k++) { if (a->sel_cat[k] != R->resp_cat || a->sel_layer[k] < R->first || a->sel_layer[k] > R->last) continue; ps += acc[(size_t)a->sel_layer[k] * info.n_ff + a->sel_idx[k]] / nt; pn++; } if (pn) { sum += ps / pn; prompts++; } } if (!prompts) continue; float ratio = (float)(sum / prompts / R->resp_base); float e = expected ? expected[r] : 1.0f; bool bad = e > 0 && ratio < 0.5f * e; low += bad; pthread_mutex_lock(&s->mu); SentGroup *g = &s->g[gi]; g->ratio = ratio; if (bad) g->alarm = true; if (e <= 0) snprintf(g->report, sizeof g->report, "L'area è stata eliminata dall'utente: la sua specialità (%s) risponde al %.0f%%, come previsto.", c->name, ratio * 100); else snprintf(g->report, sizeof g->report, "La specialità dell'area (%s) risponde al %.0f%% del normale%s.", c->name, ratio * 100, bad ? ": troppo poco" : e < 1 ? " (attenuata per scelta dell'utente)" : ""); pthread_mutex_unlock(&s->mu); } return low; } // ---- sicurezza: impronta digitale dei pesi --------------------------------------------------- static uint64_t mix(uint64_t h) { h ^= h >> 33; h *= 0xff51afd7ed558ccdULL; h ^= h >> 33; h *= 0xc4ceb9fe1a85ec53ULL; h ^= h >> 33; return h; } static uint64_t hash_bytes(const uint8_t *p, size_t n, uint64_t seed) { const uint64_t K = 0x9E3779B97F4A7C15ULL; uint64_t a = seed, b = seed ^ K, c = seed + K, d = ~seed; size_t i = 0; for (; i + 32 <= n; i += 32) { // quattro corsie indipendenti: veloce uint64_t w[4]; memcpy(w, p + i, 32); a = (a ^ w[0]) * K; b = (b ^ w[1]) * K; c = (c ^ w[2]) * K; d = (d ^ w[3]) * K; a ^= a >> 29; b ^= b >> 29; c ^= c >> 29; d ^= d >> 29; } uint64_t t = 0; for (; i < n; i++) t = (t << 8) | p[i]; return mix(a ^ mix(b) ^ mix(c ^ t) ^ mix(d) ^ n); } int sent_model_layers(const char *model_path) { struct gguf_init_params gp = {.no_alloc = true, .ctx = NULL}; struct gguf_context *g = gguf_init_from_file(model_path, gp); if (!g) return -1; int max = -1; for (int64_t t = 0; t < gguf_get_n_tensors(g); t++) { const char *name = gguf_get_tensor_name(g, t); if (!strncmp(name, "blk.", 4)) { int l = atoi(name + 4); if (l > max) max = l; } } gguf_free(g); return max + 1; } bool sent_hash_model(const char *model_path, int n_layer, uint64_t *out) { struct gguf_init_params gp = {.no_alloc = true, .ctx = NULL}; struct gguf_context *g = gguf_init_from_file(model_path, gp); if (!g) return false; const uint8_t *map = NULL; size_t size = 0; #ifdef _WIN32 HANDLE f = CreateFileA(model_path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); HANDLE m = NULL; if (f != INVALID_HANDLE_VALUE) { LARGE_INTEGER sz; GetFileSizeEx(f, &sz); size = (size_t)sz.QuadPart; m = CreateFileMappingA(f, NULL, PAGE_READONLY, 0, 0, NULL); if (m) map = MapViewOfFile(m, FILE_MAP_READ, 0, 0, 0); } #else int fd = open(model_path, O_RDONLY); if (fd >= 0) { struct stat st; fstat(fd, &st); size = (size_t)st.st_size; void *p = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); map = p == MAP_FAILED ? NULL : p; close(fd); } #endif bool ok = map != NULL; for (int l = 0; l <= n_layer; l++) out[l] = 0x243F6A8885A308D3ULL; size_t base = gguf_get_data_offset(g); for (int64_t t = 0; ok && t < gguf_get_n_tensors(g); t++) { const char *name = gguf_get_tensor_name(g, t); size_t off = base + gguf_get_tensor_offset(g, t), n = gguf_get_tensor_size(g, t); if (off + n > size) { ok = false; break; } int l = n_layer; if (!strncmp(name, "blk.", 4)) { l = atoi(name + 4); if (l < 0 || l >= n_layer) l = n_layer; } uint64_t h = hash_bytes((const uint8_t *)name, strlen(name), 1); out[l] = mix(out[l] ^ hash_bytes(map + off, n, h)); } #ifdef _WIN32 if (map) UnmapViewOfFile(map); if (m) CloseHandle(m); if (f != INVALID_HANDLE_VALUE) CloseHandle(f); #else if (map) munmap((void *)map, size); #endif gguf_free(g); return ok; } int sent_integrity(Sentinels *s, const uint64_t *now, const char *ref_path, int *bad, int bad_cap) { const int L = s->n_layer; uint64_t *ref = calloc((size_t)L + 1, sizeof(uint64_t)); FILE *f = fopen(ref_path, "rb"); int got = 0; if (f) { unsigned long long v; int l; while (fscanf(f, "%d %llx", &l, &v) == 2) if (l >= 0 && l <= L) { ref[l] = v; got++; } fclose(f); } int n_bad = 0; if (got < L + 1) { // primo avvio: questo è il riferimento f = fopen(ref_path, "wb"); if (!f) { free(ref); return -1; } for (int l = 0; l <= L; l++) fprintf(f, "%d %016llx\n", l, (unsigned long long)now[l]); fclose(f); memcpy(ref, now, sizeof(uint64_t) * ((size_t)L + 1)); } else { for (int l = 0; l <= L; l++) if (ref[l] != now[l] && n_bad < bad_cap) bad[n_bad++] = l; } pthread_mutex_lock(&s->mu); memcpy(s->hash_ref, ref, sizeof(uint64_t) * ((size_t)L + 1)); s->hash_known = true; for (int i = 0; i < s->n_groups; i++) s->g[i].weights_bad = false; for (int k = 0; k < n_bad; k++) { int gi = bad[k] >= L ? 0 : -1; for (int i = 1; i < s->n_groups && gi < 0; i++) if (bad[k] >= s->g[i].first && bad[k] <= s->g[i].last) gi = i; if (gi >= 0) { s->g[gi].weights_bad = true; s->g[gi].alarm = true; } } pthread_mutex_unlock(&s->mu); free(ref); return n_bad; } // ---- uscita ------------------------------------------------------------------------------ void sent_json(Sentinels *s, SBuf *o) { pthread_mutex_lock(&s->mu); sb_puts(o, "["); for (int i = 0; i < s->n_groups; i++) { const SentGroup *g = &s->g[i]; sb_printf(o, "%s{\"index\":%d,\"region\":%d,\"name\":", i ? "," : "", i, g->region); sb_json_str(o, g->name); sb_printf(o, ",\"first\":%d,\"last\":%d,\"enabled\":%s,\"alarm\":%s,\"repairing\":%s,\"ratio\":%.2f,\"weights_bad\":%s,\"report\":", g->first, g->last, g->enabled ? "true" : "false", g->alarm ? "true" : "false", g->repairing ? "true" : "false", g->ratio, g->weights_bad ? "true" : "false"); sb_json_str(o, g->report); sb_puts(o, ",\"sentinels\":["); for (int k = 0; k < g->n; k++) sb_printf(o, "%s[\"%s\",%d,%d]", k ? "," : "", ROLE_NAME[g->s[k].role], g->s[k].first, g->s[k].last); sb_puts(o, "]}"); } sb_puts(o, "]"); pthread_mutex_unlock(&s->mu); } void sent_frame(Sentinels *s, SBuf *o, uint32_t seq) { pthread_mutex_lock(&s->mu); uint16_t ng = (uint16_t)s->n_groups; sb_putn(o, "D\x01", 2); sb_putn(o, (const char *)&ng, 2); sb_putn(o, (const char *)&seq, 4); for (int i = 0; i < s->n_groups; i++) { const SentGroup *g = &s->g[i]; uint8_t h[2] = {(uint8_t)g->region, (uint8_t)(!g->enabled ? 3 : g->repairing ? 2 : g->alarm ? 1 : 0)}; uint16_t n = (uint16_t)g->n; sb_putn(o, (const char *)h, 2); sb_putn(o, (const char *)&n, 2); for (int k = 0; k < g->n; k++) { uint8_t rb[2] = {(uint8_t)g->s[k].role, (uint8_t)g->s[k].alarm}; sb_putn(o, (const char *)rb, 2); sb_putn(o, (const char *)&g->s[k].act, 4); } } pthread_mutex_unlock(&s->mu); }