File size: 2,169 Bytes
c0aca40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f74e990
 
 
 
 
 
 
 
 
 
 
 
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
"""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.
"""

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 problem statement."""
    if MARK in text:
        return json.loads(text.split(MARK, 1)[1])
    return {"v": 1, "problem": text.strip()}


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)