Home /Modules/observer/observer-demo.sleela

observer-demo.sleela

lib/observer/observer-demo.sleela · 371 line(s) · sleela

<< lib/observer/observer-demo.sleela
   SLeeLa Standard Library Definition.
   Definition: Runnable, self-checking demonstrator for the dynamic document
     observer (lib/observer). It starts a standing document, then drops
     modules in (as nested classes) while the program is running, and verifies
     the safety envelope: proposals not mutations, optimistic concurrency, the
     proposal depth cap, hop-limited conversations, fault quarantine, sender
     stamping, admission refusals, and the cycle cap. Prints PASS/FAIL per
     check and a final tally. >>
#sleela 2.5
material;
< Family: observer. Run: sleela run lib/observer/observer-demo.sleela >
class ObserverDemo {
  < ---- 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 = 39209;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-11-11";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-11-11";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 8;
  < 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; }

  class Stubborn {
    SLObserverPort port;
    int tried;
    void attach(String portName) {
      port = new SLObserverPort();
      port.configure(portName);
      tried = 0;
    }
    SLObserverPort portOf() { return port; }
    int pump() {
      int n = 0;
      while (port.hasMail()) {
        SLObserverMessage m = port.next();
        n = n + 1;
        if (m.getKind() == 5) {
          tried = tried + 1;
          port.propose(0, 0, "echo " + tried, m.getRevision());
        }
      }
      return n;
    }
  }
  class Faulty {
    SLObserverPort port;
    void attach(String portName) {
      port = new SLObserverPort();
      port.configure(portName);
    }
    SLObserverPort portOf() { return port; }
    int pump() {
      int n = 0;
      while (port.hasMail()) {
        SLObserverMessage m = port.next();
        n = n + 1;
        if (m.getKind() == 5) { port.reportFault("cannot cope with revision " + m.getRevision()); }
      }
      return n;
    }
  }

  SLObserverDocument doc;
  SLDocumentObserver obs;
  SLDocumentObserver.Auditor auditor;
  SLObserverSummaryModule.Responder summary;
  SLObserverGuardModule.Responder guard;
  SLObserverReviewModule.Responder review;
  ObserverDemo.Stubborn stubborn;
  ObserverDemo.Faulty faulty;
  int passed;
  int failed;

  void check(String label, boolean ok) {
    if (ok) { passed = passed + 1; print("  PASS  " + label); }
    else { failed = failed + 1; print("  FAIL  " + label); }
  }
  void banner(String t) { print(""); print("== " + t); }

  < One full conversation with the observer: deliver, pump every present >
  < module, route; repeat until quiet or the observer caps the cycles. >
  int settle() {
    int rounds = 0;
    while (obs.beginCycle()) {
      auditor.pump();
      if (summary != null) { summary.pump(); }
      if (guard != null) { guard.pump(); }
      if (review != null) { review.pump(); }
      if (stubborn != null) { stubborn.pump(); }
      if (faulty != null) { faulty.pump(); }
      obs.endCycle();
      rounds = rounds + 1;
    }
    return rounds;
  }

  void main() {
    passed = 0; failed = 0;
    doc = new SLObserverDocument();
    doc.configure("standing-document.txt");
    obs = new SLDocumentObserver();
    obs.configure(doc);

    banner("1. built-in nested Auditor watches the standing document");
    auditor = new SLDocumentObserver.Auditor();
    auditor.attach("auditor");
    check("auditor admitted", obs.admit(auditor.portOf()));
    int r1 = obs.edit(0, 0, "hello world", "host");
    settle();
    check("append -> revision 1", r1 == 1);
    check("auditor saw 1 change", auditor.changeCount() == 1);

    banner("2. drop in a nested module while running");
    summary = new SLObserverSummaryModule.Responder();
    summary.attach("summary");
    check("summary admitted mid-run", obs.admit(summary.portOf()));
    settle();
    obs.edit(0, 1, "second line", "host");
    settle();
    check("summary tracked revision 2", summary.latestRevision() == 2);
    check("summary answers its own summary text", summary.summary() == "rev 2, +1 ~0 -0");

    banner("3. guard responds safely: it proposes, the observer decides");
    guard = new SLObserverGuardModule.Responder();
    guard.attach("guard", "rm -rf /");
    check("guard admitted", obs.admit(guard.portOf()));
    settle();
    obs.edit(0, 2, "rm -rf /", "host");
    settle();
    check("forbidden line was redacted", doc.lineAt(2) == "[redacted]");
    check("document is at revision 4", doc.revisionNow() == 4);
    check("guard saw its proposal accepted", guard.redactionCount() == 1);
    check("auditor counted the proposal change too", auditor.changeCount() == 4);

    banner("4. module-to-module conversation, bounded by the hop limit");
    int droppedBefore = obs.droppedCount();
    review = new SLObserverReviewModule.Responder();
    review.attach("review", "auditor", 4);
    check("review admitted", obs.admit(review.portOf()));
    settle();
    check("review got two answers", review.answeredCount() == 2);
    check("review heard the auditor", review.latestAnswer() == "auditor has seen 4 change(s)");
    check("third question died at the hop limit", obs.droppedCount() > droppedBefore);

    banner("5. a stubborn module cannot loop the document forever");
    stubborn = new ObserverDemo.Stubborn();
    stubborn.attach("stubborn");
    obs.admit(stubborn.portOf());
    settle();
    int linesBefore = doc.lineCount();
    int declinedBefore = obs.declinedCount();
    obs.edit(0, 0, "trigger", "host");
    settle();
    check("exactly 3 proposals applied (depth cap)", obs.proposalDepth() == 3);
    check("the 4th proposal was declined", obs.declinedCount() == declinedBefore + 1);
    check("document grew by 1 host + 3 proposed lines", doc.lineCount() == linesBefore + 4);
    check("newest stubborn proposal sits on top", doc.lineAt(0) == "echo 3");
    check("retire stubborn", obs.retire("stubborn"));
    stubborn = null;

    banner("6. a faulty module is quarantined; the others carry on");
    faulty = new ObserverDemo.Faulty();
    faulty.attach("faulty");
    obs.admit(faulty.portOf());
    settle();
    obs.edit(1, 0, "edit A", "host"); settle();
    obs.edit(1, 0, "edit B", "host"); settle();
    check("still active after 2 faults", faulty.portOf().isActive());
    obs.edit(1, 0, "edit C", "host"); settle();
    check("quarantined at the 3rd fault", !faulty.portOf().isActive());
    check("quarantine counted", obs.quarantinedCount() == 1);
    int seen = faulty.portOf().receivedCount();
    obs.edit(1, 0, "edit D", "host"); settle();
    check("quarantined module receives nothing more", faulty.portOf().receivedCount() == seen);
    check("other modules kept observing", summary.latestRevision() == doc.revisionNow());

    banner("7. the document and the observer fail closed");
    int rev = doc.revisionNow();
    check("out-of-range replace refused", obs.edit(1, 999, "x", "host") == -1);
    check("out-of-range delete refused", obs.edit(2, -1, "", "host") == -1);
    check("unknown op refused", obs.edit(7, 0, "x", "host") == -1);
    check("null text refused", obs.edit(1, 0, null, "host") == -1);
    check("refused edits changed nothing", doc.revisionNow() == rev);

    banner("8. admission refusals and sender stamping");
    SLObserverPort dup = new SLObserverPort();
    dup.configure("summary");
    check("duplicate name refused", !obs.admit(dup));
    SLObserverPort bad = new SLObserverPort();
    bad.configure("observer");
    check("reserved name refused", !obs.admit(bad));
    SLObserverPort spoofer = new SLObserverPort();
    spoofer.configure("spoofer");
    SLObserverPort probe = new SLObserverPort();
    probe.configure("probe");
    obs.admit(spoofer);
    obs.admit(probe);
    obs.edit(0, 0, "poke", "host");
    SLObserverMessage lie = new SLObserverMessage();
    lie.configure(0, "auditor", "probe", "I am the auditor");
    spoofer.say(lie);
    SLObserverMessage drain = null;
    boolean began = obs.beginCycle();
    check("cycle began", began);
    check("admit mid-cycle refused", !obs.admit(new SLObserverPort()));
    auditor.pump(); summary.pump(); guard.pump(); review.pump();
    obs.endCycle();
    SLObserverMessage got = null;
    while (probe.hasMail()) {
      SLObserverMessage mm = probe.next();
      if (mm.getKind() == 0 && mm.getText() == "I am the auditor") { got = mm; }
    }
    check("probe received the spoofed note", got != null);
    check("observer stamped the real sender", got != null && got.getFrom() == "spoofer");
    settle();

    banner("9. listener lifecycle: alter, pause, resume, replace, reinstate");
    int seenBefore = summary.latestRevision();
    int filteredBefore = summary.portOf().filteredCount();
    check("alter to inserts only", obs.alter("summary", 1, 0, 0, -1, -1));
    obs.edit(1, 0, "replaced text", "host"); settle();
    check("a replace is filtered out for the altered listener", summary.latestRevision() == seenBefore);
    check("the filtered change was counted", summary.portOf().filteredCount() == filteredBefore + 1);
    obs.edit(0, 0, "inserted text", "host"); settle();
    check("an insert still reaches it", summary.latestRevision() == doc.revisionNow());
    check("alter to line 0 only", obs.alter("summary", 1, 1, 1, 0, 0));
    int atLine0 = summary.latestRevision();
    obs.edit(0, 3, "deep insert", "host"); settle();
    check("a change outside the line range is filtered", summary.latestRevision() == atLine0);
    obs.edit(0, 0, "top insert", "host"); settle();
    check("a change inside the range arrives", summary.latestRevision() == doc.revisionNow());
    check("a deaf filter is refused", !obs.alter("summary", 0, 0, 0, -1, -1));
    check("a bad flag is refused", !obs.alter("summary", 2, 1, 1, -1, -1));
    check("a reversed range is refused", !obs.alter("summary", 1, 1, 1, 5, 2));
    check("an unknown listener is refused", !obs.alter("nobody", 1, 1, 1, -1, -1));
    check("restore the full filter", obs.alter("summary", 1, 1, 1, -1, -1));

    int auditorBefore = auditor.changeCount();
    int pausedAt = summary.latestRevision();
    check("pause a listener", obs.pause("summary"));
    check("the roster shows it paused", obs.portStateAt(obs.indexOfPort("summary")) == 3);
    check("pausing twice is refused", !obs.pause("summary"));
    obs.edit(0, 0, "while paused 1", "host"); settle();
    obs.edit(0, 0, "while paused 2", "host"); settle();
    obs.edit(1, 0, "while paused 3", "host"); settle();
    check("a paused listener sees nothing", summary.latestRevision() == pausedAt);
    check("its misses are counted", summary.portOf().missedCount() == 3);
    check("others kept observing during the pause", auditor.changeCount() == auditorBefore + 3);
    check("a paused listener can still be altered", obs.alter("summary", 1, 1, 0, -1, -1));
    check("restore it again", obs.alter("summary", 1, 1, 1, -1, -1));
    obs.edit(0, 0, "pending", "host");
    check("a cycle begins", obs.beginCycle());
    check("resume is refused mid-cycle", !obs.resume("summary"));
    check("pause is refused mid-cycle", !obs.pause("auditor"));
    check("alter is refused mid-cycle", !obs.alter("summary", 1, 1, 1, -1, -1));
    auditor.pump(); summary.pump(); guard.pump(); review.pump();
    obs.endCycle();
    settle();
    int receivedBefore = summary.portOf().receivedCount();
    check("resume a paused listener", obs.resume("summary"));
    check("resuming a running listener is refused", !obs.resume("summary"));
    check("it was told it resumed", summary.portOf().receivedCount() == receivedBefore + 1);
    check("its missed count was reset", summary.portOf().missedCount() == 0);
    settle();
    obs.edit(0, 0, "after resume", "host"); settle();
    check("it observes again", summary.latestRevision() == doc.revisionNow());

    SLObserverSummaryModule.Responder swapped = new SLObserverSummaryModule.Responder();
    swapped.attach("summary");
    SLObserverPort oldPort = summary.portOf();
    SLObserverSummaryModule.Responder wrong = new SLObserverSummaryModule.Responder();
    wrong.attach("different");
    check("replace with a differently named port is refused", !obs.replace("summary", wrong.portOf()));
    check("replace with an unknown name is refused", !obs.replace("nobody", swapped.portOf()));
    check("hot-swap a listener", obs.replace("summary", swapped.portOf()));
    check("the old port is retired", oldPort.getState() == 2);
    check("the same slot now holds the new port", obs.portStateAt(obs.indexOfPort("summary")) == 0);
    check("the roster did not grow", obs.portTotal() == 8);
    summary = swapped;
    settle();
    obs.edit(0, 0, "to the new listener", "host"); settle();
    check("the new listener observes", summary.latestRevision() == doc.revisionNow());
    check("a used port cannot be swapped in", !obs.replace("summary", oldPort));

    int faultsSeen = faulty.portOf().faultCount();
    check("a quarantined listener is reinstated by the host", obs.reinstate("faulty"));
    check("its faults were cleared", faulty.portOf().faultCount() == 0 && faultsSeen == 3);
    int faultyReceived = faulty.portOf().receivedCount();
    obs.edit(1, 0, "edit E", "host"); settle();
    check("it receives mail again", faulty.portOf().receivedCount() > faultyReceived);
    check("reinstating an active listener is refused", !obs.reinstate("faulty"));
    check("a retired listener is final", obs.retire("probe") && !obs.reinstate("probe") && !obs.resume("probe") && !obs.pause("probe") && !obs.alter("probe", 1, 1, 1, -1, -1));

    banner("10. freeze: the lock at the observer");
    int revF = doc.revisionNow();
    int declinedF = obs.declinedCount();
    obs.freeze();
    check("a frozen observer reports it", obs.isFrozen());
    check("edits are refused while frozen", obs.edit(0, 0, "frozen insert", "host") == -1 && doc.revisionNow() == revF);
    check("the refusal is counted", obs.declinedCount() == declinedF + 1);
    check("the admin override still works", obs.editAdmin(0, 0, "override", "admin") == revF + 1 && doc.lineAt(0) == "override");
    check("and leaves the freeze in place", obs.isFrozen() && obs.edit(0, 0, "again", "host") == -1);
    int declinedP = obs.declinedCount();
    spoofer.propose(0, 0, "proposed", doc.revisionNow());
    settle();
    check("a module proposal is declined while frozen", doc.lineAt(0) == "override" && obs.declinedCount() > declinedP);
    obs.thaw();
    check("thaw restores ordinary edits", !obs.isFrozen() && obs.edit(0, 0, "thawed", "host") >= 0 && doc.lineAt(0) == "thawed");
    settle();

    banner("11. capacity and the cycle cap");
    int f = 0;
    int admitted = 0;
    while (f < 8) {
      SLObserverPort fp = new SLObserverPort();
      fp.configure("filler" + f);
      if (obs.admit(fp)) { admitted = admitted + 1; }
      f = f + 1;
    }
    check("roster is capped at 14 ports", obs.portTotal() == 14);
    check("admission beyond the cap was refused", admitted < 8);
    obs.edit(0, 0, "flood", "host");
    int rounds = settle();
    check("unpumped ports cannot spin the observer forever", rounds <= 16);

    banner("audit trail (tail)");
    int total = obs.trailSize();
    int from = (total > 10) ? (total - 10) : 0;
    int t = from;
    while (t < total) { print("  " + obs.trailAt(t)); t = t + 1; }

    banner("result");
    print("  passed " + passed + ", failed " + failed);
    print("  document: " + doc.getTitle() + " at revision " + doc.revisionNow()
        + " with " + doc.lineCount() + " line(s)");
  }
}