Skip to content

API Reference parrot.outputs.a2ui.graph.layout

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.outputs.a2ui.graph.layout

View source

Deterministic layered layout for the Graph vocabulary (FEAT-529 Module 4).

Layout is SERVER-SIDE PREPARATION, not styling (spec §2 Overview rule 3): positions computed here travel on the wire (GraphLayout.positions) so every renderer — the bundled UI, ECharts, and the static SVG lanes — draws the same picture from the same numbers, with layout: "none" where the renderer supports native layout. Positions are emitted ALREADY oriented for the requested direction — renderers read direction only for arrow/label placement hints, never to re-transform a position (spec §7 "Positions and direction").

Algorithm (a hand-written, dependency-light layered/Sugiyama-style layout, not a full crossing-minimization implementation):

  1. Cycle breaking — a stable DFS (3-colour, input node order) finds every back edge; each is logically reversed for ranking purposes only and recorded in :attr:LayoutResult.reversed_edges (the ORIGINAL (from, to) pair — renderers draw the arrowhead on the original direction and flip it, per spec §7). Cycles are legal for every kind except "dag" (rejected earlier, at the model level).
  2. Rank assignment — longest-path layering over the now-acyclic edge set, via :func:networkx.topological_sort (the one place networkx, already a core hard dependency, is used — spec §3 Module 4 permits it for "topological generations / cycle detection only").
  3. Crossing reduction — four barycentre sweeps (down/up/down/up), stable-sorting each rank's nodes by the mean cross-axis position of their neighbours in the adjacent rank, falling back to the current position when a node has none.
  4. Coordinate assignment — integer rank/slot indices scaled by rank_sep/node_sep (abstract units) only at the very end, so the algorithm itself is exact-integer and platform-independent; axes are swapped/placed per direction (TB/BT: rank axis is Y; LR/RL: rank axis is X; BT/RL mirror the rank axis).
  5. Group bounding boxes — the padded min/max of each group's member positions.

Pure and synchronous: no I/O beyond logging.getLogger(__name__) at debug (per spec §7 "Patterns to Follow").

Class GraphTooLargeError

Raised by :func:compute_positions above :data:MAX_STATIC_NODES.

Subclasses :class:~parrot.outputs.a2ui.catalog.base.CatalogValidationError (same reasoning as MermaidCodecError) so the LLM producer's validate-retry-degrade loop needs no new plumbing; static renderers catch it explicitly to degrade instead (Module 6).

Parameter Type Default Description
node_count int

Attributes:

Attribute Type Description
node_count The offending node count.

Class LayoutResult

The output of :func:compute_positions.

Fields:

Field Type Default Description
positions dict[str, Position] Final, direction-oriented positions, keyed by node id. Abstract units, origin top-left.
width float Overall layout width (abstract units), including padding.
height float Overall layout height (abstract units), including padding.
group_boxes dict[str, tuple[float, float, float, float]] Per-group (x, y, w, h) bounding box, padded around its members' positions.
reversed_edges list[tuple[str, str]] The ORIGINAL (from, to) pairs that were logically reversed to break a cycle for ranking purposes — renderers draw the arrowhead on the original direction and flip it (spec §7).

Attribute MAX_STATIC_NODES

Function compute_positions

Compute a deterministic layered layout for spec.

compute_positions(spec: GraphSpec, *, rank_sep: float = 80.0, node_sep: float = 40.0) -> LayoutResult

Parameter Type Default Description
spec GraphSpec The graph to lay out. Its own layout.rank_sep/ layout.node_sep are NOT read here — callers (the builder) pass them explicitly if they want to override the defaults.
rank_sep float 80.0 Abstract-unit spacing between consecutive ranks.
node_sep float 40.0 Abstract-unit spacing between consecutive nodes within the same rank.

Returns: LayoutResult — class:LayoutResult with positions for every node, overall dimensions, per-group bounding boxes, and any reversed edges.

Raises:

  • GraphTooLargeError — If len(spec.nodes) > MAX_STATIC_NODES.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally