Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.1

0.11.1 / src / compute_request.luc

## Worker request and report framing over the shared graph schema. A request
## carries every node's header and stamp, but a body only when the worker's
## copy is stale, and each Edit step's group text once. Both sides keep exactly the
## nodes (and so the lists) of the last request, so they agree on what's held.
from luce_prism.prism import Document, Value, Encoding
from network import Network
import project_graph as schema
import project_values as values
from luce_ui import ui

## UI side: what the worker holds after the last request it was sent.
pub class RequestWriter:
    let sent: map[int, int]
    let lists: set[int]

    pub func init(self):
        self.sent = {}
        self.lists = {}

    ## After a worker failure its mirror may be stale: send everything next.
    pub func reset(self):
        self.sent.clear()
        self.lists.clear()

    pub func encode(self, graph: Network, mode: int, normals: bool = false) -> str!:
        let doc = Document()
        schema.write_scene(doc, graph)
        doc.set("/scene", "mode", Value.integer(mode))
        doc.set("/scene", "normals", Value.boolean(normals))
        doc.set("/scene", "level", Value.integer(graph.level))
        doc.set("/scene", "gesture", Value.boolean(graph.gesture))
        let carried: map[int, str] = {}
        let sent: map[int, int] = {}
        let lists: set[int] = {}
        for node in graph.nodes:
            let path = f"/scene/nodes/n{node.id}"
            schema.write_header(doc, path, node)
            doc.set(path, "stamp", Value.integer(node.stamp))
            if self.sent[node.id] != node.stamp:
                schema.write_body(doc, path, node, carried)
                doc.set(path, "source", Value.integer(node.source_revision))
            sent[node.id] = node.stamp
            for edit in node.edits:
                lists.insert(edit.group_key)
        doc.add("/scene/lists", "lists")
        for key in carried.keys():
            if key in self.lists:
                continue
            let path = f"/scene/lists/l{doc.child_count("/scene/lists")}"
            doc.add(path, "group")
            doc.set(path, "key", Value.integer(key))
            doc.set(path, "text", Value.text(carried[key] else ""))
        self.sent.clear()
        for id in sent.keys():
            self.sent[id] = sent[id] else 0
        self.lists.clear()
        for key in lists:
            self.lists.insert(key)
        return (doc.encode(Encoding.text).bytes()).text()

## Worker side: rebuild the mirror, reusing held bodies (and their recipes).
pub func read_request(message: str, previous: Network) -> Network!:
    let doc = Document.parse(message)
    let lists: map[int, str] = {}
    for old in previous.nodes:
        for edit in old.edits:
            lists[edit.group_key] = edit.group
    for index in 0..<doc.child_count("/scene/lists"):
        let path = doc.child_at("/scene/lists", index) else error(ui.invalid, "Missing Edit group")
        lists[values.stamp(doc, path, "key")] = values.text(doc, path, "text")
    let graph = Network(false)
    schema.read_scene(doc, graph)
    for index in 0..<doc.child_count("/scene/nodes"):
        let path = doc.child_at("/scene/nodes", index) else error(ui.invalid, "Missing request node")
        let node = schema.read_header(doc, path, true)
        node.stamp = values.stamp(doc, path, "stamp")
        if schema.has_body(doc, path):
            schema.read_body(doc, path, node, lists)
            node.source_revision = values.integer(doc, path, "source", 0, 2147483647)
        else:
            let old = previous.find(node.id) else error(ui.invalid, "Compute request lost a node body")
            if old.kind != node.kind or old.stamp != node.stamp:
                error(ui.invalid, "Compute request lost a node body")
            node.enabled = old.enabled
            node.values = old.values
            node.texts = old.texts
            node.edits = old.edits
            node.guard = old.guard
            node.source_revision = old.source_revision
        graph.nodes.append(node)
    graph.compute_mode = values.integer(doc, "/scene", "mode", 0, 4)
    graph.show_normals = values.flag(doc, "/scene", "normals")
    graph.level = values.integer(doc, "/scene", "level", 0, 16)
    graph.gesture = values.flag(doc, "/scene", "gesture")
    return graph

## Per-node outcome of a cook. `cooked` is the stamp the result describes (0:
## not computed); failures carry the node's current name.
pub class Outcome:
    pub let id: int
    pub let cooked: int
    pub let failure: str
    pub let summary: str
    pub let evaluations: int
    pub let paths: list[str]
    pub var warning: str = ""
    pub func init(self, id: int, cooked: int, failure: str, summary: str, evaluations: int, paths: list[str]):
        self.id = id
        self.cooked = cooked
        self.failure = failure
        self.summary = summary
        self.evaluations = evaluations
        self.paths = paths

## `recipes` counts Edit recipes applied by this cook (not replayed from
## stored prefixes); tests and benchmarks read it.
pub func write_report(notice: str, resync: bool, outcomes: list[Outcome], recipes: int = 0) -> str!:
    let doc = Document()
    doc.add("/report", "report")
    doc.set("/report", "notice", Value.text(notice))
    doc.set("/report", "resync", Value.boolean(resync))
    doc.set("/report", "recipes", Value.integer(recipes))
    for outcome in outcomes:
        let path = f"/report/n{outcome.id}"
        doc.add(path, "node")
        doc.set(path, "id", Value.integer(outcome.id))
        doc.set(path, "cooked", Value.integer(outcome.cooked))
        doc.set(path, "failure", Value.text(outcome.failure))
        doc.set(path, "summary", Value.text(outcome.summary))
        doc.set(path, "evaluations", Value.integer(outcome.evaluations))
        doc.set(path, "paths", Value.strings([outcome.paths.length], outcome.paths))
        doc.set(path, "warning", Value.text(outcome.warning))
    return (doc.encode(Encoding.text).bytes()).text()

pub class Report:
    pub let notice: str
    pub let resync: bool
    pub let outcomes: list[Outcome]
    pub let recipes: int
    pub func init(self, notice: str, resync: bool, outcomes: list[Outcome], recipes: int):
        self.notice = notice
        self.resync = resync
        self.outcomes = outcomes
        self.recipes = recipes

pub func read_report(text: str) -> Report!:
    let doc = Document.parse(text)
    let outcomes: list[Outcome] = []
    for index in 0..<doc.child_count("/report"):
        let path = doc.child_at("/report", index) else error(ui.invalid, "Missing report node")
        let names = doc.get(path, "paths")
        let paths: list[str] = []
        for at in 0..<names.count():
            paths.append(names.text_at(at))
        let outcome = Outcome(values.integer(doc, path, "id", 1, 2147483646), values.stamp(doc, path, "cooked"),
            values.text(doc, path, "failure"), values.text(doc, path, "summary"), values.integer(doc, path, "evaluations", 0, 2147483647), paths)
        outcome.warning = values.text(doc, path, "warning")
        outcomes.append(outcome)
    return Report(values.text(doc, "/report", "notice"), values.flag(doc, "/report", "resync"), outcomes, values.integer(doc, "/report", "recipes", 0, 2147483647))