Home /Modules/moral-capacitor/SLMoralCodeGuard.sleela

SLMoralCodeGuard.sleela

lib/moral-capacitor/SLMoralCodeGuard.sleela · 144 line(s) · sleela

<< lib/moral-capacitor/SLMoralCodeGuard.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLMoralCodeGuard — the Guard of the Moral Code: the
     enforcement that a SLeeLa software product actually INCLUDES the moral code
     it claims. Before a product is permitted to run or ship, the guard checks
     that it (1) embeds a live Moral Capacitor(TM), (2) presents a Certificate of
     Morals(TM) that is well-formed, (3) was sealed by a trusted authority and
     verifies against it, (4) is within its validity span, and (5) attests a
     posture consistent with the embedded capacitor (same policy threshold, red
     lines armed). A product that fails any check is refused — the moral code is
     mandatory, not optional.
   
   This is how the project strives to include Morals and Certificates in all its
   SLeeLa software: the guard is the gate at the product boundary, the
   counterpart to SLMoralCapacitor at the operation boundary.
   
   Family: moral-capacitor. Verifies SLMoralCertificate via
     SLMoralCertificateAuthority; inspects SLMoralCapacitor. >>
#sleela 2.5
material;
class SLMoralCodeGuard {
  < ---- 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 = 10913;
  String metaAuthor = "Kiro Agent";
  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 = "2026-10-10";
  < 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; }
  < Guard outcome ordinals. >
  < the product includes the moral code: admit >
  static final int ADMIT   = 1;
  < missing/invalid moral code: refuse >
  static final int REFUSE  = 2;

  SLMoralCertificateAuthority trustedAuthority;
  String lastReason;
  int outcome;
  int configured;

  void configure(SLMoralCertificateAuthority authority) {
    trustedAuthority = authority; lastReason = ""; outcome = REFUSE; configured = 1;
  }

  < Guard a product: it must embed a live capacitor and present a valid >
  < Certificate of Morals that verifies against the trusted authority, is >
  < unexpired at `nowStamp`, and attests a posture matching the capacitor. >
  < Returns ADMIT or REFUSE; lastReason explains a refusal. >
  int guard(String product, SLMoralCapacitor cap, SLMoralCertificate cert, String nowStamp) {
    < 1. The moral code must be present at the operation boundary. >
    if (cap == null) { return refuse("no Moral Capacitor embedded in " + product); }

    < 2. A certificate must be presented and well-formed. >
    if (cert == null) { return refuse("no Certificate of Morals presented for " + product); }
    if (!cert.isWellFormed()) { return refuse("Certificate of Morals is not well-formed"); }

    < 3. The certificate must cover THIS product. >
    if (cert.subjectProduct() != product) {
      return refuse("certificate subject does not match product " + product);
    }

    < 4. The certificate must verify against the trusted authority. >
    if (!trustedAuthority.verify(cert)) {
      return refuse("certificate seal does not verify against trusted authority "
                    + trustedAuthority.authorityName());
    }

    < 5. The certificate must be within its validity span. >
    if (!withinValidity(cert, nowStamp)) {
      return refuse("certificate expired (expires " + cert.expires() + ", now " + nowStamp + ")");
    }

    < 6. The attested posture must match the embedded capacitor. >
    if (cert.dischargeThreshold() != cap.policy().threshold()) {
      return refuse("certificate threshold does not match the embedded capacitor policy");
    }
    if (cert.redlinesArmed() < 1) {
      return refuse("certificate attests no armed red lines");
    }

    return admit();
  }

  < Validity check: a certificate with an empty expiry is non-expiring. A >
  < non-empty expiry must be lexicographically >= now for ISO-8601 stamps. >
  boolean withinValidity(SLMoralCertificate cert, String nowStamp) {
    String exp = cert.expires();
    if (exp == "") { return true; }
    < ISO-8601 timestamps compare correctly as strings. >
    return exp >= nowStamp;
  }

  int refuse(String why) { outcome = REFUSE; lastReason = why; return REFUSE; }
  int admit()            { outcome = ADMIT; lastReason = "moral code present and certified"; return ADMIT; }

  boolean admitted() { return outcome == ADMIT; }
  boolean refused()  { return outcome == REFUSE; }
  String reason()    { return lastReason; }
  int lastOutcome()  { return outcome; }

  < A boolean form for a call site that just needs permit/deny. >
  boolean permits(String product, SLMoralCapacitor cap, SLMoralCertificate cert, String nowStamp) {
    return guard(product, cap, cert, nowStamp) == ADMIT;
  }

  String outcomeName() {
    if (outcome == ADMIT) { return "ADMIT"; }
    return "REFUSE";
  }
}