Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.8

0.11.8 / 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
import node_paths

## 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))
        doc.set("/scene", "cv_nets", Value.integer(graph.cv_nets))
        doc.set("/scene", "cad_quality", Value.integer(graph.cad_quality))
        doc.set("/scene", "picking_cad", Value.boolean(graph.picking_cad))
        doc.set("/scene", "projecting", Value.integer(graph.projecting))
        doc.set("/scene", "slicing", Value.integer(graph.slicing))
        doc.set("/scene", "sketch_object", Value.text(graph.sketch_object))
        doc.set("/scene", "folder", Value.text(graph.folder))
        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))
            # Node paths are names, not in a stamp (a rename that rewrites
            # them recooks nothing), so a body holding them always goes.
            # A holding Edit node's steps stay behind (its result goes by key).
            var holds = false
            if let kind = graph.kinds.of(node.kind):
                holds = kind.holds
            # An action the worker runs (held_base: edit_held.luc) goes with it.
            let steps = not holds or node.held_base != 0
            if self.sent[node.id] != node.stamp or node_paths.holds_paths(node.kind):
                schema.write_body(doc, path, node, carried, steps)
                doc.set(path, "source", Value.integer(node.source_revision))
            sent[node.id] = node.stamp
            if steps:
                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. A node sent without a body is the one
## the worker holds, kept as it is (only its header read again): a drag's
## request makes only the nodes it changed.
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)
    graph.kinds = previous.kinds
    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 stamp = values.stamp(doc, path, "stamp")
        if not schema.has_body(doc, path):
            # Its body is the one the worker holds: the same node, taking the
            # header (a rename, a move in the network, its wires).
            let old = previous.find(values.integer(doc, path, "id", 1, 2147483646)) else error(ui.invalid, "Compute request lost a node body")
            if old.kind != doc.type_at(path) or old.stamp != stamp:
                error(ui.invalid, "Compute request lost a node body")
            schema.apply_header(doc, path, old, true)
            graph.nodes.append(old)
            continue
        let node = schema.read_header(doc, path, true)
        node.stamp = stamp
        # A feature's memo outlives its stamp (a Fillet's radius dragged).
        if let old = previous.find(node.id):
            if old.kind == node.kind:
                node.memo = old.memo
        schema.read_body(doc, path, node, lists, graph.kinds)
        node.source_revision = values.integer(doc, path, "source", 0, 2147483647)
        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")
    graph.cv_nets = values.integer(doc, "/scene", "cv_nets", -1, 2147483646)
    graph.cad_quality = values.integer(doc, "/scene", "cad_quality", 1, 64)
    graph.picking_cad = values.flag(doc, "/scene", "picking_cad")
    graph.projecting = values.integer(doc, "/scene", "projecting", -1, 2147483646)
    graph.slicing = values.integer(doc, "/scene", "slicing", -1, 2147483646)
    graph.sketch_object = values.text(doc, "/scene", "sketch_object")
    graph.folder = values.text(doc, "/scene", "folder")
    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 var console: str = ""
    pub var sketch_state: str = ""
    pub var automatic: str = ""
    pub var handles: str = ""
    ## Which paths name CAD curves.
    pub var curves: list[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. `shadows`: the input
## body's edges cast for the Sketch the Project tool works in, as of that
## node's stamp `shadows_stamp` (sketch_node.shadows_text). `section`: the
## cap of the sliced Sketch's body, as of its stamp `section_stamp`
## (slice_view.luc). `results` and `result_bytes`: what the worker's result
## store holds after the cook (tests read them).
pub func write_report(notice: str, resync: bool, outcomes: list[Outcome], recipes: int = 0, shadows: str = "", shadows_stamp: int = 0, section: str = "", section_stamp: int = 0, results: int = 0, result_bytes: int = 0) -> str!:
    let doc = Document()
    doc.add("/report", "report")
    doc.set("/report", "shadows", Value.text(shadows))
    doc.set("/report", "shadows_stamp", Value.integer(shadows_stamp))
    doc.set("/report", "section", Value.text(section))
    doc.set("/report", "section_stamp", Value.integer(section_stamp))
    doc.set("/report", "notice", Value.text(notice))
    doc.set("/report", "resync", Value.boolean(resync))
    doc.set("/report", "recipes", Value.integer(recipes))
    doc.set("/report", "results", Value.integer(results))
    doc.set("/report", "result_bytes", Value.integer(result_bytes))
    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))
        doc.set(path, "console", Value.text(outcome.console))
        doc.set(path, "sketch_state", Value.text(outcome.sketch_state))
        doc.set(path, "automatic", Value.text(outcome.automatic))
        doc.set(path, "handles", Value.text(outcome.handles))
        doc.set(path, "curves", Value.strings([outcome.curves.length], outcome.curves))
    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 var results: int = 0
    pub var result_bytes: int = 0
    pub var shadows: str = ""
    pub var shadows_stamp: int = 0
    pub var section: str = ""
    pub var section_stamp: int = 0
    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")
        outcome.console = values.text(doc, path, "console")
        outcome.sketch_state = values.text(doc, path, "sketch_state")
        outcome.automatic = values.text(doc, path, "automatic")
        outcome.handles = values.text(doc, path, "handles")
        let curve_names = doc.get(path, "curves")
        for at in 0..<curve_names.count():
            outcome.curves.append(curve_names.text_at(at))
        outcomes.append(outcome)
    let report = Report(values.text(doc, "/report", "notice"), values.flag(doc, "/report", "resync"), outcomes, values.integer(doc, "/report", "recipes", 0, 2147483647))
    report.results = values.integer(doc, "/report", "results", 0, 2147483647)
    report.result_bytes = values.stamp(doc, "/report", "result_bytes")
    report.shadows = values.text(doc, "/report", "shadows")
    report.shadows_stamp = values.stamp(doc, "/report", "shadows_stamp")
    report.section = values.text(doc, "/report", "section")
    report.section_stamp = values.stamp(doc, "/report", "section_stamp")
    return report