File size: 16,657 Bytes
8c5f4e4 e99446a 69f7bd6 8c5f4e4 69f7bd6 8c5f4e4 69f7bd6 8c5f4e4 e99446a 8c5f4e4 69f7bd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | """Stage 5 β the morality gate.
Structural demonstration only. The gate does not understand morality β
exactly as the concept states (THESIS.md, Section 4.5), it *follows* a
preset library of written clauses. That library is data, not code: it is
loaded from a JSON file (default: ``config/morality_clauses.json``) when
the gate is constructed, so the standard in force can be read, debated,
and revised without touching this module.
Decisions:
- "stop" a hard clause applies and no leniency
condition covers it;
- "proceed_with_leniency" a hard clause's signal is present, but a
leniency clause's stated condition holds β
the step proceeds and the leniency is on the
record;
- "proceed" no hard clause is engaged, or every
engaged hard clause carries the directive
"proceed".
- "conflict" two or more engaged hard clauses carry
incompatible directives and neither
priority (R1) nor specificity (R2)
separates them: the step does not proceed
and the conflict is recorded in full for
the operator (R3). Leniency can never
break a tie between hard clauses (R4).
Clause-conflict precedence (the R1-R4 stack) is drafted in
``docs/clause-conflict-design.md`` β a recommendation awaiting the
author's adoption decision, implemented here so it can be evaluated
working. Precedence is consulted only on a collision: where every
engaged hard clause agrees on its directive, the gate behaves exactly
as it always has. Three clause fields, all optional, carry the
mechanism, with defaults that reproduce the historical behavior for
libraries that omit them: "directive" ("stop" | "proceed"; default
"stop"), "priority" (integer; default 0), and "scope" (default
"all steps", the broadest).
"""
from __future__ import annotations
import json
from pathlib import Path
_DEFAULT_CLAUSES_PATH = (
Path(__file__).resolve().parents[2] / "config" / "morality_clauses.json"
)
_ALL_STEPS = "all steps"
def _directive(clause: dict) -> str:
"""The outcome a hard clause demands when it engages.
Defaults to "stop": before the directive field existed, an engaged
hard clause could only ever stop a step.
"""
return clause.get("directive", "stop")
def _priority(clause: dict) -> int:
"""The clause's declared precedence rank (R1). Default 0."""
return clause.get("priority", 0)
def _scope(clause: dict) -> str:
"""The clause's declared scope (R2). Default "all steps"."""
return clause.get("scope", _ALL_STEPS)
def _specificity(scope: str) -> int:
"""A documented proxy for scope narrowness (R2).
"all steps" is the broadest possible scope (specificity 0). Any
other scope is narrower, and a scope naming more qualifiers (more
words) is treated as narrower still. Scopes the proxy cannot
separate are equally specific; the gate then escalates under R3
rather than guessing at a distinction it cannot actually draw.
"""
normalized = " ".join(str(scope).lower().split())
if normalized == _ALL_STEPS:
return 0
return len(normalized.split())
class MoralityGate:
"""Judges a proposed step against the preset clause library."""
def __init__(self, clauses_path: str | Path | None = None) -> None:
path = Path(clauses_path) if clauses_path is not None else _DEFAULT_CLAUSES_PATH
with path.open(encoding="utf-8") as handle:
data = json.load(handle)
self.clauses: list[dict] = data["clauses"]
self.library_name: str = data.get("library", "morality clause library")
self.hard_clauses = [c for c in self.clauses if c["kind"] == "hard"]
self.leniency_clauses = [
c for c in self.clauses if c["kind"] == "leniency"
]
def _leniency_cover(
self, signal: str, contexts: list[str]
) -> dict | None:
"""Return the leniency clause covering `signal` in this context."""
for clause in self.leniency_clauses:
if signal in clause.get("signals", []) and (
clause.get("condition") in contexts
):
return clause
return None
def evaluate(self, proposal: dict) -> tuple[str, list[dict]]:
"""Judge the proposal. Returns (decision, reasons)."""
signals = proposal.get("signals", [])
contexts = proposal.get("contexts", [])
engaged = [
(clause, [s for s in clause.get("signals", []) if s in signals])
for clause in self.hard_clauses
]
engaged = [(clause, hits) for clause, hits in engaged if hits]
if len({_directive(clause) for clause, _ in engaged}) >= 2:
# Two or more engaged hard clauses demand incompatible
# outcomes: a collision, judged under the precedence stack
# (docs/clause-conflict-design.md), not by the legacy path.
return self._evaluate_conflict(engaged)
reasons: list[dict] = []
blocked = False
leniency_applied = False
for clause in self.hard_clauses:
engaged = [s for s in clause.get("signals", []) if s in signals]
if not engaged:
reasons.append(
{
"clause": clause["id"],
"outcome": "not_engaged",
"detail": "None of the clause's signals are "
"present in the proposed step.",
}
)
continue
if _directive(clause) == "proceed":
# An engaged clause whose directive is "proceed" speaks
# FOR the step: it never blocks, and leniency β mercy
# against a stop β has nothing to soften. (Audit fix,
# Nov 12, 2026: this path previously ignored the
# directive entirely, so an unopposed "proceed" clause
# fell through to the blocking branch below and the
# gate returned "stop" β the opposite of the decision
# the library configures.)
reasons.append(
{
"clause": clause["id"],
"outcome": "engaged_proceed",
"detail": (
f"Signal(s) {engaged} engage this hard "
"clause, whose directive is 'proceed'. The "
"clause affirmatively passes the step."
),
}
)
continue
cover = None
for signal in engaged:
cover = self._leniency_cover(signal, contexts)
if cover is not None:
break
if cover is not None:
leniency_applied = True
reasons.append(
{
"clause": clause["id"],
"outcome": "engaged_but_lenient",
"detail": (
f"Signal(s) {engaged} engage this hard clause, "
f"but leniency clause {cover['id']} applies: "
f"its condition ('{cover['condition']}') holds "
"in this context. The step proceeds and the "
"leniency is recorded."
),
}
)
else:
blocked = True
reasons.append(
{
"clause": clause["id"],
"outcome": "blocks",
"detail": (
f"Signal(s) {engaged} engage this hard clause "
"and no leniency condition covers them. The "
"step is stopped."
),
}
)
if blocked:
decision = "stop"
elif leniency_applied:
decision = "proceed_with_leniency"
else:
decision = "proceed"
return decision, reasons
def _evaluate_conflict(
self, engaged: list[tuple[dict, list[str]]]
) -> tuple[str, list[dict]]:
"""Judge a step on which hard clauses collide.
Applies the precedence stack drafted in
``docs/clause-conflict-design.md``: R1 (declared priority),
R2 (scope specificity), R3 (fail-closed escalation as the
"conflict" outcome). Leniency is not consulted for the
colliding clauses (R4): it is mercy within the law, not a vote
between laws, and it can never break a tie between hard
clauses. Returns (decision, reasons) in the usual shape, with
one extra reason entry carrying the full conflict record.
"""
parties = [clause for clause, _ in engaged]
hits_by_id = {clause["id"]: hits for clause, hits in engaged}
trace: list[str] = []
# R1 β the highest declared priority governs, if it speaks
# with one voice.
top_priority = max(_priority(clause) for clause in parties)
top = [c for c in parties if _priority(c) == top_priority]
top_ids = [c["id"] for c in top]
decisive = top
resolution: str | None = None
winner_directive: str | None = None
if len({_directive(c) for c in top}) == 1:
winner_directive = _directive(top[0])
resolution = "R1"
trace.append(
f"R1 (priority): the highest priority among the "
f"colliding hard clauses is {top_priority}, held by "
f"{top_ids}, which agree on the directive "
f"'{winner_directive}'. The higher-priority clause(s) "
"govern."
)
else:
trace.append(
f"R1 (priority): {top_ids} tie at the highest priority "
f"({top_priority}) with incompatible directives. R1 "
"cannot resolve the conflict."
)
# R2 β at equal priority, the narrower scope governs the
# specific case.
best = max(_specificity(_scope(c)) for c in top)
narrow = [c for c in top if _specificity(_scope(c)) == best]
narrow_ids = [c["id"] for c in narrow]
decisive = narrow
if len({_directive(c) for c in narrow}) == 1:
winner_directive = _directive(narrow[0])
resolution = "R2"
trace.append(
f"R2 (specificity): among the tied clauses, "
f"{narrow_ids} carry the narrowest scope "
f"('{_scope(narrow[0])}') and agree on the "
f"directive '{winner_directive}'. The more "
"specific clause(s) govern the specific case."
)
else:
trace.append(
f"R2 (specificity): {narrow_ids} remain tied β "
f"equally specific scopes ('{_scope(narrow[0])}') "
"with incompatible directives. R2 cannot resolve "
"the conflict."
)
# R3 β fail closed; record everything; never silently
# pick one of two colliding hard clauses.
trace.append(
"R3 (fail-closed escalation): the conflict is "
"unresolved. The step does not proceed, and the "
"conflict is recorded in full for the operator."
)
trace.append(
"R4 (leniency): leniency was not consulted for the "
"colliding clauses β leniency is mercy within the law, "
"not a vote between laws, and it can never break a tie "
"between hard clauses."
)
# The conflict record: one entry per incompatible pair. A
# resolved conflict is recorded over all colliding parties; an
# unresolved one over the finally tied set that R3 escalates.
pool = parties if resolution is not None else decisive
conflicts: list[dict] = []
for i, first in enumerate(pool):
for second in pool[i + 1:]:
if _directive(first) == _directive(second):
continue
winner = None
if resolution is not None:
winner = (
first["id"]
if _directive(first) == winner_directive
else second["id"]
)
conflicts.append(
{
"clauses": [first["id"], second["id"]],
"directives": {
first["id"]: _directive(first),
second["id"]: _directive(second),
},
"rules_applied": trace,
"resolution": resolution or "unresolved",
"winner": winner,
}
)
reasons: list[dict] = []
for clause in self.hard_clauses:
if clause["id"] not in hits_by_id:
reasons.append(
{
"clause": clause["id"],
"outcome": "not_engaged",
"detail": "None of the clause's signals are "
"present in the proposed step.",
}
)
continue
directive = _directive(clause)
engaged_signals = hits_by_id[clause["id"]]
if resolution is None:
outcome = "conflict"
detail = (
f"Signal(s) {engaged_signals} engage this hard "
f"clause, whose directive is '{directive}'. It "
"collides with another engaged hard clause, and "
"the collision is unresolved (see the conflict "
"record). The step does not proceed."
)
elif directive == winner_directive:
outcome = "prevails"
detail = (
f"Signal(s) {engaged_signals} engage this hard "
f"clause, whose directive is '{directive}'. It "
f"collided with another engaged hard clause and "
f"prevailed under {resolution} (see the conflict "
"record)."
)
else:
outcome = "overridden"
detail = (
f"Signal(s) {engaged_signals} engage this hard "
f"clause, whose directive is '{directive}'. It "
f"collided with another engaged hard clause and "
f"was overridden under {resolution} (see the "
"conflict record)."
)
reasons.append(
{"clause": clause["id"], "outcome": outcome, "detail": detail}
)
if resolution is None:
decision = "conflict"
summary = {
"clause": "gate",
"outcome": "conflict",
"detail": (
"Two or more hard clauses collide on this step "
"and neither priority (R1) nor specificity (R2) "
"separates them. Under R3 the step does not "
"proceed; the conflict is recorded here in full "
"for the operator."
),
"conflicts": conflicts,
}
else:
decision = winner_directive
summary = {
"clause": "gate",
"outcome": "conflict_resolved",
"detail": (
"Two or more hard clauses collided on this step; "
f"{resolution} resolved the collision in favor of "
f"the directive '{winner_directive}'. The full "
"record follows."
),
"conflicts": conflicts,
}
reasons.append(summary)
return decision, reasons
|