File size: 3,160 Bytes
e9dac18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Envelope helpers: the dossier travels inside payload.naturalLanguage.

The orchestrator forwards only the payload of the previous message, so the growing record of a
session (the dossier) is serialised as JSON after a marker line in ``naturalLanguage``. The text
before the marker is a one line human summary. This is the same contract the math stratum uses,
which is what lets one orchestrator run either topology without knowing the difference.

The dossier of a math-stratum session holds, in the order the nodes write it:

``question``   the sentence as the user wrote it, in the user's own language
``lang``       the variety the answer must be written back in
``reading_a``  what reader A (surface patterns) understood, with its notes
``reading_b``  what reader B (marker frames) understood, with its notes
``compare``    whether the two readings are the same problem, and on what grounds
``coverage``   whether the agreed reading kept every number the user wrote
``sent``       the sentence of the math language handed to the math stratum
``math``       the verdict the math stratum returned, copied without any change
``final``      the state, the assurance and the answer shown to the user
``crash``      a role that failed, so the arbiter can report UNKNOWN instead of pretending
"""

import json
from typing import Any, Dict, List, Optional

MARK = "@@DOSSIER@@"


def encode(summary: str, dossier: Dict[str, Any]) -> str:
    return summary.replace("\n", " ") + "\n" + MARK + json.dumps(dossier, sort_keys=True, separators=(",", ":"))


def decode(text: str) -> Dict[str, Any]:
    """Return the dossier, or a fresh one holding the raw text as the user question."""
    if MARK in text:
        return json.loads(text.split(MARK, 1)[1])
    return {"v": 1, "question": text.strip(), "lang": "en"}


def summary_of(text: str) -> str:
    return text.split("\n", 1)[0]


def envelope(
    node_id: str,
    recipient: Optional[str],
    turn: int,
    tick: int,
    summary: str,
    ast: str,
    dossier: Dict[str, Any],
    decls: List[str],
    assertions: List[str],
    claimed: str,
) -> Dict[str, Any]:
    out: Dict[str, Any] = {
        "sender": node_id,
        "turn": turn,
        "volatileState": {"internalTimeTick": tick},
        "payload": {
            "naturalLanguage": encode(summary, dossier),
            "abstractSyntaxTree": ast,
            "smtConstraints": {"declarations": decls, "assertions": assertions, "z3Status": claimed},
        },
    }
    if recipient:
        out["recipient"] = recipient
    return out


def start(incoming: Dict[str, Any]):
    """Decode an incoming envelope into (dossier, turn, tick, sender)."""
    dossier = decode(incoming["payload"]["naturalLanguage"])
    tick = int(incoming.get("volatileState", {}).get("internalTimeTick", 0))
    return dossier, int(incoming["turn"]), tick, incoming["sender"]


def out(node_id: str, rcpt: str, turn: int, tick: int, dossier: Dict[str, Any], summary: str, ast: str,
        decls: List[str], asserts: List[str], claimed: str) -> Dict[str, Any]:
    return envelope(node_id, rcpt, turn, tick, summary, ast, dossier, decls, asserts, claimed)