#!/usr/bin/env python3 """The Oracle7 kernel, v6: META SWARM round 1 (the census: 84.5% of 5,518 holes were goals the kernel could not carry, because it only carried QUANTITIES). The head's own front door becomes a part of the kernel: - a question whose goal is a quantity: the kernel's declared and induced readings, as v5. If the head also answers it with a number, the two must agree (else the kernel declines: "kernel and head disagree"); - any other question (who, which, where, yes/no, define, compare...): the head's whole `ask` path - the 9.0 reader, the specialists, the relation store, the ego layer - under the head's own law, and its answer joins the kernel's medium as a TERM with the head's receipt (status, path, provenance); - a question the kernel can't answer as a quantity but the head can: the head's answer. Nothing about the head changes; the kernel reads it the way it reads any part. usage: module (Kernel6(head_chat=callable)); the gate: kernel6_gate.py""" from __future__ import annotations import re from kernel_v5 import Kernel5, reader NUM = re.compile(r"-?\d[\d,]*(?:\.\d+)?(?:e[+-]?\d+)?") class Kernel6(Kernel5): def __init__(self, head_chat, *a, **k): super().__init__(*a, **k) self.head_chat = head_chat # the head's front door: text, session -> reply dict def solve(self, text, session="kernel"): quantity_goal = self.goal_of(text) is not None r = super().solve(text) if quantity_goal else {"status": "declined", "reason": "not a quantity goal"} h = self.head_chat(text, session) h_ok = h.get("status") == "answered" if r.get("status") == "answered": if h_ok and quantity_goal: hv = _first_number(h.get("response")) kv = _first_number(r.get("answer")) if hv is not None and kv is not None and abs(hv - kv) > 0.02 * max(abs(hv), abs(kv), 1e-12) and \ not _same_up_to_unit(hv, kv): return {"status": "declined", "reason": "kernel and head disagree: kernel %s, head %s" % (r["answer"], h.get("response", "")[:60]), "check": "readings agree", "head": h} return dict(r, head_status=h.get("status")) if h_ok: return {"status": "answered", "answer": h.get("response"), "response": h.get("response"), "reading": "head", "path": [str(h.get("path"))], "head": h} return dict(r, head=h, hole={"question": text, "kernel": r.get("reason"), "head": h.get("status")}) def _first_number(s): m = NUM.search(str(s or "").replace("$", "")) return float(m.group(0).replace(",", "")) if m else None def _same_up_to_unit(a, b): """1000x / 1e6x apart: the same quantity in a prefixed unit (kW vs W), not a disagreement""" for f in (1e3, 1e6, 1e-3, 1e-6, 3600, 1 / 3600, 3.6e6, 1 / 3.6e6): if abs(a * f - b) <= 0.02 * max(abs(b), 1e-12): return True return False