Rename planner and document rewrite: rewrite_documents moves qualified names of the renamed flow in every flow (ports, inputs, outputs) and dashboard binding (one key table, WIDGET_BINDINGS, shared with the WidgetDef accessors, legacy pages included), published and draft, as plain JSON with both variants above either version; plan_rename reports documents, counts, fingerprint, warnings and refusals; superuser plan routes for flows and dashboards; key residue classifies state as the mapper does; duplicate retargets inputs and outputs too; queue reports its Redis version and other consumers

This commit is contained in:
2026-11-02 21:06:39 +01:00
parent 81bd7c2724
commit fa2a213631
14 changed files with 1922 additions and 41 deletions
+67 -1
View File
@@ -9,12 +9,17 @@ the new name, so every family is registered here, beside the builders that
take their prefix from it, and a key of a family that is not is refused rather
than left behind.
Documents name messages too: a flow its ports, inputs and outputs, a dashboard
its bindings. Where they do is registered beside the paths into them
(`FLOW_FIELDS`, `dashboards.DASHBOARD_BINDINGS`), and `rebind` rewrites them.
Nothing here does I/O. It maps names; moving the keys and items is the state
and queue backends' work.
and queue backends' work, and writing documents the rename's.
"""
from __future__ import annotations
from collections.abc import Callable, Iterable
from dataclasses import dataclass, replace
from typing import TYPE_CHECKING, Any
@@ -87,6 +92,62 @@ ITEM_FIELDS: dict[str, str | None] = {
"parent": None,
}
#: Where a flow document names messages, as paths into its JSON (`rebind`).
#: A bare name is the flow's own and follows its key by itself, so a rename
#: moves only the qualified ones: those the flow names in another's
#: namespace, or explicitly in its own.
FLOW_FIELDS: tuple[tuple[str, ...], ...] = (
("nodes", "*", "requires", "*", "name"),
("nodes", "*", "provides", "*", "name"),
("inputs", "*", "spec", "name"),
("outputs", "*"),
)
def rebind(
document: Any, paths: Iterable[tuple[str, ...]], rename: Callable[[str], str]
) -> int:
"""Pass every string *paths* lead to in *document* through *rename*, in
place, and count those that changed.
A path is a key per level, or ``*`` for every item of a list or value of
an object. One that leads nowhere, or to anything but a string, is passed
over, so a document missing a field or holding an older shape of it is
read as far as it goes.
"""
return sum(_rebind(document, path, rename) for path in paths)
def names(document: Any, paths: Iterable[tuple[str, ...]]) -> list[str]:
"""Every string *paths* lead to in *document* (`rebind`)."""
found: list[str] = []
def keep(name: str) -> str:
found.append(name)
return name
rebind(document, paths, keep)
return found
def _rebind(node: Any, path: tuple[str, ...], rename: Callable[[str], str]) -> int:
head, rest = path[0], path[1:]
keys: Iterable[Any] = ()
if head == "*" and isinstance(node, list):
keys = range(len(node))
elif head == "*" and isinstance(node, dict):
keys = list(node)
elif isinstance(node, dict) and head in node:
keys = (head,)
changed = 0
for key in keys:
if rest:
changed += _rebind(node[key], rest, rename)
elif isinstance(node[key], str) and (moved := rename(node[key])) != node[key]:
node[key] = moved
changed += 1
return changed
def _parts(key: str) -> tuple[str, list[tuple[str, str]]]:
"""A state key's family prefix and its parts, each with what it holds.
@@ -129,6 +190,11 @@ class Rename:
def flow(self, key: str) -> str:
return self.new if key == self.old else key
def binding(self, name: str) -> str:
"""A message name as a document holds it. A bare one is local to the
flow the document is, so only a qualified one moves."""
return self.name(name) if "." in name else name
def state_key(self, key: str) -> str | None:
"""Where the state key *key* moves to, or None if it stays.