Home /Modules/vm/SLVmAudit.sleela

SLVmAudit.sleela

lib/vm/src/sleela/SLVmAudit.sleela · 129 line(s) · sleela

<< lib/vm/src/sleela/SLVmAudit.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLVmAudit — the OOD equivalent of the native audit trail
     (virtual-machine/vm-version/009/vm_audit.{h,c}, v009). It records
     structured events (CONFIG/DENIAL/PRESSURE/SATURATE/PLATFORM/ERROR/INFO)
     with a monotonic sequence number and a running FNV-1a chain hash so the
     trail is ordered and tamper-evident, mirroring each event to SLVmReport.
     The native side is a 256-entry ring; the Sleela surface keeps the counters,
     the running chain hash, and the last event (the ring body is native).
   
   Mirrors: enum VmAuditKind, vm_audit_record/result/total/count/chain/report.
   
   Family: vm. Composes SLVmReport, SLVmResult. >>
#sleela 2.5
material;
class SLVmAudit {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 11282;
  String metaAuthor = "unknown";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "unknown";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  static final int INFO     = 0;
  static final int CONFIG   = 1;
  static final int DENIAL   = 2;
  static final int PRESSURE = 3;
  static final int SATURATE = 4;
  static final int PLATFORM = 5;
  static final int ERROR    = 6;

  < ever recorded >
  int    total;
  < low 31 bits of the running chain hash (surface int) >
  int    chainLow;
  int    lastSeq;
  int    lastKind;
  String lastSubsystem;
  String lastMessage;

  void init() { total = 0; chainLow = 0; lastSeq = 0; lastKind = INFO; lastSubsystem = ""; lastMessage = ""; }

  int severityFor(int kind) {
    if (kind == DENIAL || kind == ERROR)   { return 3; }
    if (kind == PRESSURE || kind == SATURATE || kind == PLATFORM) { return 2; }
    if (kind == CONFIG)  { return 1; }
    return 0;
  }

  < simple positive-int FNV-1a fold step (surface has no unsigned/xor; use >
  < multiply+add+mod to keep a stable, order-sensitive running value). >
  int fold(int h, int v) {
    int x = (h * 131 + v) % 2147483647;
    if (x < 0) { x = x + 2147483647; }
    return x;
  }

  < record an event (mirror vm_audit_record): seq++, fold the chain, mirror to report. >
  int log(int kind, String subsystem, String message, SLVmReport rep) {
    total = total + 1;
    lastSeq = total; lastKind = kind;
    lastSubsystem = (subsystem == null) ? "vm" : subsystem;
    lastMessage = (message == null) ? "" : message;
    chainLow = fold(fold(chainLow, total), kind);
    if (rep != null) { rep.emit(severityFor(kind), lastSubsystem, kindName(kind), lastMessage); }
    return lastSeq;
  }

  < fold a typed result into the trail (mirror vm_audit_result). >
  int logResult(SLVmResult r, SLVmReport rep) {
    if (r == null) { return total; }
    int k;
    if (r.code == 7)         { k = DENIAL; }
    else if (r.code == 6) { k = SATURATE; }
    else if (r.code == 2)     { k = PRESSURE; }
    else if (r.code == 4) { k = PLATFORM; }
    else if (r.code == 0)        { k = INFO; }
    else { k = ERROR; }
    return log(k, r.subsystem, r.message, rep);
  }

  int totalEvents() { return total; }
  int chain() { return chainLow; }

  String kindName(int k) {
    if (k == CONFIG)   { return "CONFIG"; }
    if (k == DENIAL)   { return "DENIAL"; }
    if (k == PRESSURE) { return "PRESSURE"; }
    if (k == SATURATE) { return "SATURATE"; }
    if (k == PLATFORM) { return "PLATFORM"; }
    if (k == ERROR)    { return "ERROR"; }
    return "INFO";
  }
}