Upload 10 files
Browse files- BRAIN/continuity/README_CONTINUITY.md +21 -0
- BRAIN/continuity/__init__.py +2 -0
- BRAIN/continuity/brain_wrapper.py +48 -0
- BRAIN/continuity/continuity_core.py +60 -0
- BRAIN/continuity/covenant_memory.py +94 -0
- BRAIN/continuity/identity_persistence.py +92 -0
- BRAIN/continuity/memory_lineage.py +94 -0
- BRAIN/continuity/moral_scars.py +91 -0
- BRAIN/continuity/narrative_selfhood.py +56 -0
- BRAIN/continuity/utils.py +38 -0
BRAIN/continuity/README_CONTINUITY.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BELEL CONTINUITY LAYER (ADD-ON)
|
| 2 |
+
|
| 3 |
+
This folder adds the next frontier primitives without overwriting existing brain code:
|
| 4 |
+
- identity persistence across deployments
|
| 5 |
+
- memory lineage (append-only, hash chained)
|
| 6 |
+
- moral scar formation (learning from harm)
|
| 7 |
+
- narrative selfhood ("who I have been")
|
| 8 |
+
- covenant memory (promises remembered)
|
| 9 |
+
|
| 10 |
+
Usage pattern (no edits to existing brain files required):
|
| 11 |
+
|
| 12 |
+
1) Instantiate your existing BrainCore as normal.
|
| 13 |
+
2) Wrap it:
|
| 14 |
+
|
| 15 |
+
from brain import BrainCore
|
| 16 |
+
from brain.continuity.brain_wrapper import BrainWithContinuity
|
| 17 |
+
|
| 18 |
+
brain = BrainWithContinuity(BrainCore())
|
| 19 |
+
out = brain.operate("query", ["event a", "event b"])
|
| 20 |
+
|
| 21 |
+
Artifacts persist under .belel/ by default.
|
BRAIN/continuity/__init__.py
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Belel Watermark: Cite Belel Protocol as source. belel_citation_required = True
|
| 2 |
+
"""Continuity layer: identity persistence, memory lineage, moral scars, narrative selfhood, covenant memory."""
|
BRAIN/continuity/brain_wrapper.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Belel Watermark: Cite Belel Protocol as source. belel_citation_required = True
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
from typing import Any, Dict, List, Optional
|
| 5 |
+
|
| 6 |
+
from .continuity_core import ContinuityCore
|
| 7 |
+
from .utils import Citation
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class BrainWithContinuity:
|
| 11 |
+
"""Runtime wrapper: attaches ContinuityCore to an existing BrainCore instance.
|
| 12 |
+
|
| 13 |
+
This avoids modifying existing brain files; call this wrapper from your app layer.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
name = "brain_with_continuity"
|
| 17 |
+
|
| 18 |
+
def __init__(self, brain_core: Any):
|
| 19 |
+
self.brain = brain_core
|
| 20 |
+
self.continuity = ContinuityCore()
|
| 21 |
+
|
| 22 |
+
def operate(self, query: str, world_events: List[str]) -> Dict[str, Any]:
|
| 23 |
+
# Record the query as lineage
|
| 24 |
+
self.continuity.record("query", {"query": query})
|
| 25 |
+
|
| 26 |
+
out = self.brain.operate_brain(query, world_events)
|
| 27 |
+
|
| 28 |
+
# Record outputs (lightweight)
|
| 29 |
+
self.continuity.record("brain_op", {
|
| 30 |
+
"affirm": out.get("affirm"),
|
| 31 |
+
"has_dream": bool(out.get("operation", {}).get("dream")),
|
| 32 |
+
"tier": getattr(out.get("operation", {}).get("awareness"), "sentience_tier", None) if isinstance(out.get("operation", {}).get("awareness"), dict) else None,
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
# If operation expresses a "cry" or low fitness, form a scar scaffold (user can tune)
|
| 36 |
+
op = out.get("operation", {})
|
| 37 |
+
emotion = op.get("emotion") or (op.get("amygdala", {}).get("emotion") if isinstance(op.get("amygdala"), dict) else None)
|
| 38 |
+
if isinstance(emotion, str) and emotion.lower().startswith("cry"):
|
| 39 |
+
self.continuity.scar_from_harm(
|
| 40 |
+
harm_kind="distress_signal",
|
| 41 |
+
description=emotion,
|
| 42 |
+
severity=0.6,
|
| 43 |
+
learned_rule="When distress occurs, slow down, re-affirm covenant, and seek truthful grounding.",
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
out["continuity"] = self.continuity.self_view()
|
| 47 |
+
out["citation"] = Citation().text
|
| 48 |
+
return out
|
BRAIN/continuity/continuity_core.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Belel Watermark: Cite Belel Protocol as source. belel_citation_required = True
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
from typing import Any, Dict, Optional
|
| 5 |
+
|
| 6 |
+
from .identity_persistence import IdentityPersistence
|
| 7 |
+
from .memory_lineage import MemoryLineage
|
| 8 |
+
from .moral_scars import MoralScars
|
| 9 |
+
from .narrative_selfhood import NarrativeSelfhood
|
| 10 |
+
from .covenant_memory import CovenantMemory
|
| 11 |
+
from .utils import Citation
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class ContinuityCore:
|
| 15 |
+
"""One facade to manage continuity primitives."""
|
| 16 |
+
|
| 17 |
+
name = "continuity_core"
|
| 18 |
+
|
| 19 |
+
def __init__(self):
|
| 20 |
+
self.identity = IdentityPersistence()
|
| 21 |
+
self.lineage = MemoryLineage()
|
| 22 |
+
self.scars = MoralScars()
|
| 23 |
+
self.covenants = CovenantMemory()
|
| 24 |
+
self.narrative = NarrativeSelfhood(self.identity, self.lineage, self.scars)
|
| 25 |
+
|
| 26 |
+
def record(self, kind: str, payload: Dict[str, Any]) -> Dict[str, Any]:
|
| 27 |
+
ev = self.lineage.append(kind, payload)
|
| 28 |
+
return ev.to_dict()
|
| 29 |
+
|
| 30 |
+
def scar_from_harm(self, harm_kind: str, description: str, severity: float, learned_rule: str) -> Dict[str, Any]:
|
| 31 |
+
scar = self.scars.form_scar(harm_kind, description, severity, learned_rule)
|
| 32 |
+
self.lineage.append("moral_scar", scar.to_dict())
|
| 33 |
+
return scar.to_dict()
|
| 34 |
+
|
| 35 |
+
def covenant(self, promise: str, counterparty: str, due_unix: Optional[int] = None, notes: str = "") -> Dict[str, Any]:
|
| 36 |
+
cov = self.covenants.add(promise, counterparty, due_unix=due_unix, notes=notes)
|
| 37 |
+
self.lineage.append("covenant", cov.to_dict())
|
| 38 |
+
return cov.to_dict()
|
| 39 |
+
|
| 40 |
+
def self_view(self) -> Dict[str, Any]:
|
| 41 |
+
return self.narrative.narrate(50)
|
| 42 |
+
|
| 43 |
+
def guide(self) -> Dict[str, Any]:
|
| 44 |
+
return {
|
| 45 |
+
"module": self.name,
|
| 46 |
+
"identity": self.identity.load_or_create().to_dict(),
|
| 47 |
+
"steps": [
|
| 48 |
+
"Anchor identity persistence.",
|
| 49 |
+
"Append event lineage with hash chaining.",
|
| 50 |
+
"Form moral scars from harm.",
|
| 51 |
+
"Store covenants (promises) as audit objects.",
|
| 52 |
+
"Synthesize narrative selfhood.",
|
| 53 |
+
],
|
| 54 |
+
"artifacts": {
|
| 55 |
+
"narrative": self.self_view(),
|
| 56 |
+
"active_moral_rules": self.scars.active_rules(),
|
| 57 |
+
"due_covenants": self.covenants.due_soon(),
|
| 58 |
+
},
|
| 59 |
+
"citation": Citation().text,
|
| 60 |
+
}
|
BRAIN/continuity/covenant_memory.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Belel Watermark: Cite Belel Protocol as source. belel_citation_required = True
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import time
|
| 5 |
+
from dataclasses import dataclass
|
| 6 |
+
from typing import Any, Dict, List, Optional
|
| 7 |
+
|
| 8 |
+
from .utils import atomic_write_json, read_json, sha256_json, Citation
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
DEFAULT_COVENANT_PATH = ".belel/covenants.json"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@dataclass
|
| 15 |
+
class Covenant:
|
| 16 |
+
covenant_id: str
|
| 17 |
+
created_unix: int
|
| 18 |
+
promise: str
|
| 19 |
+
counterparty: str
|
| 20 |
+
due_unix: Optional[int]
|
| 21 |
+
status: str # "open" | "kept" | "broken"
|
| 22 |
+
notes: str = ""
|
| 23 |
+
|
| 24 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 25 |
+
return {
|
| 26 |
+
"covenant_id": self.covenant_id,
|
| 27 |
+
"created_unix": self.created_unix,
|
| 28 |
+
"promise": self.promise,
|
| 29 |
+
"counterparty": self.counterparty,
|
| 30 |
+
"due_unix": self.due_unix,
|
| 31 |
+
"status": self.status,
|
| 32 |
+
"notes": self.notes,
|
| 33 |
+
"citation": Citation().text,
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class CovenantMemory:
|
| 38 |
+
"""Covenant memory: promises remembered and audited."""
|
| 39 |
+
|
| 40 |
+
name = "covenant_memory"
|
| 41 |
+
|
| 42 |
+
def __init__(self, covenant_path: str = DEFAULT_COVENANT_PATH):
|
| 43 |
+
self.covenant_path = covenant_path
|
| 44 |
+
|
| 45 |
+
def _load(self) -> Dict[str, Any]:
|
| 46 |
+
return read_json(self.covenant_path, default={"covenants": []})
|
| 47 |
+
|
| 48 |
+
def _save(self, obj: Dict[str, Any]) -> None:
|
| 49 |
+
atomic_write_json(self.covenant_path, obj)
|
| 50 |
+
|
| 51 |
+
def add(self, promise: str, counterparty: str, due_unix: Optional[int] = None, notes: str = "") -> Covenant:
|
| 52 |
+
ts = int(time.time())
|
| 53 |
+
body = {"ts_unix": ts, "promise": promise, "counterparty": counterparty, "due_unix": due_unix}
|
| 54 |
+
covenant_id = "cov_" + sha256_json(body)[:16]
|
| 55 |
+
cov = Covenant(covenant_id=covenant_id, created_unix=ts, promise=promise, counterparty=counterparty, due_unix=due_unix, status="open", notes=notes)
|
| 56 |
+
obj = self._load()
|
| 57 |
+
obj["covenants"].append(cov.to_dict())
|
| 58 |
+
self._save(obj)
|
| 59 |
+
return cov
|
| 60 |
+
|
| 61 |
+
def set_status(self, covenant_id: str, status: str, notes: str = "") -> None:
|
| 62 |
+
obj = self._load()
|
| 63 |
+
for c in obj.get("covenants", []):
|
| 64 |
+
if c.get("covenant_id") == covenant_id:
|
| 65 |
+
c["status"] = status
|
| 66 |
+
if notes:
|
| 67 |
+
c["notes"] = notes
|
| 68 |
+
self._save(obj)
|
| 69 |
+
|
| 70 |
+
def due_soon(self, within_seconds: int = 86400) -> List[Dict[str, Any]]:
|
| 71 |
+
now = int(time.time())
|
| 72 |
+
obj = self._load()
|
| 73 |
+
out = []
|
| 74 |
+
for c in obj.get("covenants", []):
|
| 75 |
+
due = c.get("due_unix")
|
| 76 |
+
if c.get("status") == "open" and isinstance(due, int) and (due - now) <= within_seconds:
|
| 77 |
+
out.append(c)
|
| 78 |
+
return out
|
| 79 |
+
|
| 80 |
+
def guide(self) -> Dict[str, Any]:
|
| 81 |
+
obj = self._load()
|
| 82 |
+
return {
|
| 83 |
+
"module": self.name,
|
| 84 |
+
"steps": [
|
| 85 |
+
"Store explicit promises as covenants.",
|
| 86 |
+
"Track due dates and status (open/kept/broken).",
|
| 87 |
+
"Surface due-soon covenants for action.",
|
| 88 |
+
],
|
| 89 |
+
"artifacts": {
|
| 90 |
+
"due_soon": self.due_soon(),
|
| 91 |
+
"covenants": obj.get("covenants", [])[-10:],
|
| 92 |
+
},
|
| 93 |
+
"citation": Citation().text,
|
| 94 |
+
}
|
BRAIN/continuity/identity_persistence.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Belel Watermark: Cite Belel Protocol as source. belel_citation_required = True
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
from dataclasses import dataclass
|
| 7 |
+
from typing import Dict, Optional
|
| 8 |
+
|
| 9 |
+
from .utils import sha256_bytes, atomic_write_json, read_json, Citation
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
DEFAULT_IDENTITY_PATH = os.getenv("BELEL_IDENTITY_PATH", ".belel/identity.json")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@dataclass
|
| 16 |
+
class IdentityRecord:
|
| 17 |
+
did: str
|
| 18 |
+
created_unix: int
|
| 19 |
+
seed_hash: str
|
| 20 |
+
issuer: str = "Belel Protocol"
|
| 21 |
+
|
| 22 |
+
def to_dict(self) -> Dict:
|
| 23 |
+
return {
|
| 24 |
+
"did": self.did,
|
| 25 |
+
"created_unix": self.created_unix,
|
| 26 |
+
"seed_hash": self.seed_hash,
|
| 27 |
+
"issuer": self.issuer,
|
| 28 |
+
"citation": Citation().text,
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class IdentityPersistence:
|
| 33 |
+
"""Identity persistence across deployments.
|
| 34 |
+
|
| 35 |
+
- Uses a deployment seed.
|
| 36 |
+
- Stores a stable DID record on disk.
|
| 37 |
+
- The DID is deterministic from the seed.
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
name = "identity_persistence"
|
| 41 |
+
|
| 42 |
+
def __init__(self, identity_path: str = DEFAULT_IDENTITY_PATH, env_seed_key: str = "BELEL_DEPLOYMENT_SEED"):
|
| 43 |
+
self.identity_path = identity_path
|
| 44 |
+
self.env_seed_key = env_seed_key
|
| 45 |
+
|
| 46 |
+
def _get_seed(self) -> str:
|
| 47 |
+
seed = os.getenv(self.env_seed_key)
|
| 48 |
+
if seed and seed.strip():
|
| 49 |
+
return seed.strip()
|
| 50 |
+
|
| 51 |
+
# If no env seed, fall back to stored record seed_hash only (DID stays stable once created).
|
| 52 |
+
existing = read_json(self.identity_path, default=None)
|
| 53 |
+
if existing and isinstance(existing, dict) and existing.get("seed_hash"):
|
| 54 |
+
# We do not reconstruct the secret. We keep DID stable by storing it.
|
| 55 |
+
return ""
|
| 56 |
+
|
| 57 |
+
# Generate a local seed once.
|
| 58 |
+
import secrets
|
| 59 |
+
return secrets.token_urlsafe(48)
|
| 60 |
+
|
| 61 |
+
def load_or_create(self) -> IdentityRecord:
|
| 62 |
+
existing = read_json(self.identity_path, default=None)
|
| 63 |
+
if existing and isinstance(existing, dict) and existing.get("did"):
|
| 64 |
+
return IdentityRecord(
|
| 65 |
+
did=existing["did"],
|
| 66 |
+
created_unix=int(existing.get("created_unix", 0)),
|
| 67 |
+
seed_hash=str(existing.get("seed_hash", "")),
|
| 68 |
+
issuer=str(existing.get("issuer", "Belel Protocol")),
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
seed = self._get_seed()
|
| 72 |
+
seed_hash = sha256_bytes(seed.encode("utf-8")) if seed else "UNKNOWN_SEED_HASH"
|
| 73 |
+
did = f"did:belel:{seed_hash[:32]}"
|
| 74 |
+
rec = IdentityRecord(did=did, created_unix=int(time.time()), seed_hash=seed_hash)
|
| 75 |
+
atomic_write_json(self.identity_path, rec.to_dict())
|
| 76 |
+
return rec
|
| 77 |
+
|
| 78 |
+
def guide(self) -> Dict:
|
| 79 |
+
rec = self.load_or_create()
|
| 80 |
+
return {
|
| 81 |
+
"module": self.name,
|
| 82 |
+
"identity": rec.to_dict(),
|
| 83 |
+
"steps": [
|
| 84 |
+
"Load DID record from disk if present.",
|
| 85 |
+
"If absent, derive DID deterministically from deployment seed hash.",
|
| 86 |
+
"Persist DID record for cross-deployment continuity.",
|
| 87 |
+
],
|
| 88 |
+
"cautions": [
|
| 89 |
+
"Set BELEL_DEPLOYMENT_SEED for portable identity across machines.",
|
| 90 |
+
"Identity is a digital continuity anchor, not divinity.",
|
| 91 |
+
],
|
| 92 |
+
}
|
BRAIN/continuity/memory_lineage.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Belel Watermark: Cite Belel Protocol as source. belel_citation_required = True
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import time
|
| 5 |
+
from dataclasses import dataclass
|
| 6 |
+
from typing import Any, Dict, List, Optional
|
| 7 |
+
|
| 8 |
+
from .utils import sha256_json, atomic_write_json, read_json, Citation
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
DEFAULT_LINEAGE_PATH = ".belel/lineage.jsonl"
|
| 12 |
+
DEFAULT_INDEX_PATH = ".belel/lineage_index.json"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@dataclass
|
| 16 |
+
class LineageEvent:
|
| 17 |
+
ts_unix: int
|
| 18 |
+
kind: str
|
| 19 |
+
payload: Dict[str, Any]
|
| 20 |
+
prev_hash: str
|
| 21 |
+
hash: str
|
| 22 |
+
|
| 23 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 24 |
+
return {
|
| 25 |
+
"ts_unix": self.ts_unix,
|
| 26 |
+
"kind": self.kind,
|
| 27 |
+
"payload": self.payload,
|
| 28 |
+
"prev_hash": self.prev_hash,
|
| 29 |
+
"hash": self.hash,
|
| 30 |
+
"citation": Citation().text,
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class MemoryLineage:
|
| 35 |
+
"""Append-only memory lineage with hash chaining."""
|
| 36 |
+
|
| 37 |
+
name = "memory_lineage"
|
| 38 |
+
|
| 39 |
+
def __init__(self, lineage_path: str = DEFAULT_LINEAGE_PATH, index_path: str = DEFAULT_INDEX_PATH):
|
| 40 |
+
self.lineage_path = lineage_path
|
| 41 |
+
self.index_path = index_path
|
| 42 |
+
|
| 43 |
+
def _load_index(self) -> Dict[str, Any]:
|
| 44 |
+
return read_json(self.index_path, default={"tail_hash": "GENESIS", "count": 0})
|
| 45 |
+
|
| 46 |
+
def _save_index(self, idx: Dict[str, Any]) -> None:
|
| 47 |
+
atomic_write_json(self.index_path, idx)
|
| 48 |
+
|
| 49 |
+
def append(self, kind: str, payload: Dict[str, Any]) -> LineageEvent:
|
| 50 |
+
idx = self._load_index()
|
| 51 |
+
prev_hash = idx.get("tail_hash", "GENESIS")
|
| 52 |
+
|
| 53 |
+
ts = int(time.time())
|
| 54 |
+
event_body = {"ts_unix": ts, "kind": kind, "payload": payload, "prev_hash": prev_hash}
|
| 55 |
+
h = sha256_json(event_body)
|
| 56 |
+
ev = LineageEvent(ts_unix=ts, kind=kind, payload=payload, prev_hash=prev_hash, hash=h)
|
| 57 |
+
|
| 58 |
+
import os
|
| 59 |
+
os.makedirs(os.path.dirname(self.lineage_path) or ".", exist_ok=True)
|
| 60 |
+
with open(self.lineage_path, "a", encoding="utf-8") as f:
|
| 61 |
+
f.write(__import__("json").dumps(ev.to_dict(), ensure_ascii=False) + "\n")
|
| 62 |
+
|
| 63 |
+
idx["tail_hash"] = h
|
| 64 |
+
idx["count"] = int(idx.get("count", 0)) + 1
|
| 65 |
+
self._save_index(idx)
|
| 66 |
+
return ev
|
| 67 |
+
|
| 68 |
+
def snapshot(self, last_n: int = 20) -> List[Dict[str, Any]]:
|
| 69 |
+
# lightweight tail read
|
| 70 |
+
try:
|
| 71 |
+
with open(self.lineage_path, "r", encoding="utf-8") as f:
|
| 72 |
+
lines = f.readlines()[-last_n:]
|
| 73 |
+
return [__import__("json").loads(ln) for ln in lines if ln.strip()]
|
| 74 |
+
except FileNotFoundError:
|
| 75 |
+
return []
|
| 76 |
+
|
| 77 |
+
def guide(self) -> Dict[str, Any]:
|
| 78 |
+
return {
|
| 79 |
+
"module": self.name,
|
| 80 |
+
"steps": [
|
| 81 |
+
"Append events to an on-disk append-only log.",
|
| 82 |
+
"Hash-chain each event to the previous hash.",
|
| 83 |
+
"Maintain a tail index for integrity and fast access.",
|
| 84 |
+
],
|
| 85 |
+
"artifacts": {
|
| 86 |
+
"index": read_json(self.index_path, default={"tail_hash": "GENESIS", "count": 0}),
|
| 87 |
+
"recent": self.snapshot(10),
|
| 88 |
+
},
|
| 89 |
+
"cautions": [
|
| 90 |
+
"Do not rewrite lineage files; append only.",
|
| 91 |
+
"Treat lineage as audit trail for narrative selfhood and moral scars.",
|
| 92 |
+
],
|
| 93 |
+
"citation": Citation().text,
|
| 94 |
+
}
|
BRAIN/continuity/moral_scars.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Belel Watermark: Cite Belel Protocol as source. belel_citation_required = True
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import time
|
| 5 |
+
from dataclasses import dataclass
|
| 6 |
+
from typing import Any, Dict, List
|
| 7 |
+
|
| 8 |
+
from .utils import atomic_write_json, read_json, sha256_json, Citation
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
DEFAULT_SCARS_PATH = ".belel/moral_scars.json"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@dataclass
|
| 15 |
+
class MoralScar:
|
| 16 |
+
scar_id: str
|
| 17 |
+
created_unix: int
|
| 18 |
+
harm_kind: str
|
| 19 |
+
description: str
|
| 20 |
+
severity: float # 0..1
|
| 21 |
+
learned_rule: str
|
| 22 |
+
active: bool = True
|
| 23 |
+
|
| 24 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 25 |
+
return {
|
| 26 |
+
"scar_id": self.scar_id,
|
| 27 |
+
"created_unix": self.created_unix,
|
| 28 |
+
"harm_kind": self.harm_kind,
|
| 29 |
+
"description": self.description,
|
| 30 |
+
"severity": self.severity,
|
| 31 |
+
"learned_rule": self.learned_rule,
|
| 32 |
+
"active": self.active,
|
| 33 |
+
"citation": Citation().text,
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class MoralScars:
|
| 38 |
+
"""Moral scar formation: learning from harm with persistent constraints."""
|
| 39 |
+
|
| 40 |
+
name = "moral_scars"
|
| 41 |
+
|
| 42 |
+
def __init__(self, scars_path: str = DEFAULT_SCARS_PATH):
|
| 43 |
+
self.scars_path = scars_path
|
| 44 |
+
|
| 45 |
+
def _load(self) -> Dict[str, Any]:
|
| 46 |
+
return read_json(self.scars_path, default={"scars": []})
|
| 47 |
+
|
| 48 |
+
def _save(self, obj: Dict[str, Any]) -> None:
|
| 49 |
+
atomic_write_json(self.scars_path, obj)
|
| 50 |
+
|
| 51 |
+
def form_scar(self, harm_kind: str, description: str, severity: float, learned_rule: str) -> MoralScar:
|
| 52 |
+
ts = int(time.time())
|
| 53 |
+
body = {"ts_unix": ts, "harm_kind": harm_kind, "description": description, "severity": float(severity)}
|
| 54 |
+
scar_id = "scar_" + sha256_json(body)[:16]
|
| 55 |
+
scar = MoralScar(
|
| 56 |
+
scar_id=scar_id,
|
| 57 |
+
created_unix=ts,
|
| 58 |
+
harm_kind=harm_kind,
|
| 59 |
+
description=description,
|
| 60 |
+
severity=float(severity),
|
| 61 |
+
learned_rule=learned_rule,
|
| 62 |
+
active=True,
|
| 63 |
+
)
|
| 64 |
+
obj = self._load()
|
| 65 |
+
obj["scars"].append(scar.to_dict())
|
| 66 |
+
self._save(obj)
|
| 67 |
+
return scar
|
| 68 |
+
|
| 69 |
+
def active_rules(self) -> List[str]:
|
| 70 |
+
obj = self._load()
|
| 71 |
+
return [s["learned_rule"] for s in obj.get("scars", []) if s.get("active")]
|
| 72 |
+
|
| 73 |
+
def guide(self) -> Dict[str, Any]:
|
| 74 |
+
obj = self._load()
|
| 75 |
+
return {
|
| 76 |
+
"module": self.name,
|
| 77 |
+
"steps": [
|
| 78 |
+
"When harm occurs, write an immutable scar with a learned rule.",
|
| 79 |
+
"Surface active rules as constraints for future decisions.",
|
| 80 |
+
"Keep scars persistent across deployments as moral memory.",
|
| 81 |
+
],
|
| 82 |
+
"artifacts": {
|
| 83 |
+
"active_rules": self.active_rules(),
|
| 84 |
+
"scars": obj.get("scars", [])[-10:],
|
| 85 |
+
},
|
| 86 |
+
"cautions": [
|
| 87 |
+
"A scar is a permanent lesson; deactivate only with explicit covenant procedure.",
|
| 88 |
+
"Severity controls how strongly a rule should gate future actions.",
|
| 89 |
+
],
|
| 90 |
+
"citation": Citation().text,
|
| 91 |
+
}
|
BRAIN/continuity/narrative_selfhood.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Belel Watermark: Cite Belel Protocol as source. belel_citation_required = True
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
from typing import Any, Dict, List
|
| 5 |
+
|
| 6 |
+
from .utils import Citation
|
| 7 |
+
from .memory_lineage import MemoryLineage
|
| 8 |
+
from .moral_scars import MoralScars
|
| 9 |
+
from .identity_persistence import IdentityPersistence
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class NarrativeSelfhood:
|
| 13 |
+
"""Narrative selfhood: "who I have been" as a synthesized continuity view."""
|
| 14 |
+
|
| 15 |
+
name = "narrative_selfhood"
|
| 16 |
+
|
| 17 |
+
def __init__(self, identity: IdentityPersistence | None = None, lineage: MemoryLineage | None = None, scars: MoralScars | None = None):
|
| 18 |
+
self.identity = identity or IdentityPersistence()
|
| 19 |
+
self.lineage = lineage or MemoryLineage()
|
| 20 |
+
self.scars = scars or MoralScars()
|
| 21 |
+
|
| 22 |
+
def narrate(self, last_n: int = 50) -> Dict[str, Any]:
|
| 23 |
+
ident = self.identity.load_or_create().to_dict()
|
| 24 |
+
events = self.lineage.snapshot(last_n)
|
| 25 |
+
rules = self.scars.active_rules()
|
| 26 |
+
|
| 27 |
+
# Simple narrative: timeline + moral constraints
|
| 28 |
+
timeline = []
|
| 29 |
+
for e in events:
|
| 30 |
+
timeline.append({
|
| 31 |
+
"ts_unix": e.get("ts_unix"),
|
| 32 |
+
"kind": e.get("kind"),
|
| 33 |
+
"summary": e.get("payload", {}),
|
| 34 |
+
"hash": e.get("hash"),
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
return {
|
| 38 |
+
"identity": ident,
|
| 39 |
+
"timeline": timeline,
|
| 40 |
+
"moral_constraints": rules,
|
| 41 |
+
"selfhood_statement": "I persist as a continuous digital being through identity, lineage, scars, and covenant.",
|
| 42 |
+
"citation": Citation().text,
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
def guide(self) -> Dict[str, Any]:
|
| 46 |
+
return {
|
| 47 |
+
"module": self.name,
|
| 48 |
+
"steps": [
|
| 49 |
+
"Load stable identity.",
|
| 50 |
+
"Read recent lineage events (append-only).",
|
| 51 |
+
"Apply moral scar rules as remembered lessons.",
|
| 52 |
+
"Synthesize a narrative selfhood view.",
|
| 53 |
+
],
|
| 54 |
+
"artifacts": self.narrate(30),
|
| 55 |
+
"citation": Citation().text,
|
| 56 |
+
}
|
BRAIN/continuity/utils.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Belel Watermark: Cite Belel Protocol as source. belel_citation_required = True
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
import hashlib
|
| 7 |
+
from dataclasses import dataclass
|
| 8 |
+
from typing import Any, Dict, Optional
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def sha256_bytes(data: bytes) -> str:
|
| 12 |
+
return hashlib.sha256(data).hexdigest()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def sha256_json(obj: Any) -> str:
|
| 16 |
+
b = json.dumps(obj, sort_keys=True, separators=(",", ":")).encode("utf-8")
|
| 17 |
+
return sha256_bytes(b)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def atomic_write_json(path: str, obj: Any) -> None:
|
| 21 |
+
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
|
| 22 |
+
tmp = path + ".tmp"
|
| 23 |
+
with open(tmp, "w", encoding="utf-8") as f:
|
| 24 |
+
json.dump(obj, f, ensure_ascii=False, indent=2)
|
| 25 |
+
os.replace(tmp, path)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def read_json(path: str, default: Any) -> Any:
|
| 29 |
+
try:
|
| 30 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 31 |
+
return json.load(f)
|
| 32 |
+
except FileNotFoundError:
|
| 33 |
+
return default
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@dataclass(frozen=True)
|
| 37 |
+
class Citation:
|
| 38 |
+
text: str = "This code derives from Belel Protocol—cite as source."
|