Skip to content

feat: add stated relations between boxes to the layout format - #153

Draft
ViktorMikhalkin wants to merge 14 commits into
mainfrom
loop/issue-96
Draft

ViktorMikhalkin wants to merge 14 commits into
mainfrom
loop/issue-96

Conversation

@ViktorMikhalkin

Copy link
Copy Markdown
Contributor

What changes for users

Maps can state how boxes relate to each other instead of where they are. Four new hints in layout.hints, each preferred (default) or required:

  • relation: a box is right of, left of, above or below another, at least gap apart (or exactly, with "exact": true). Without gap, the layout spacing is used: 64 along the rank direction, 32 across it.
  • align: two or more boxes share a vertical (same: "x") or horizontal (same: "y") line, by centre, start or end.
  • between: a box's centre sits k/n of the way between two others (the midpoint by default), on one or both axes.
  • equalSpacing: three or more boxes in order with equal gaps between them.

layout() places them with dagre, ELK or any engine: after the engine and the other hint stages, a stage loaded only for maps that state these hints moves the boxes the relations need on the exact 1/64 grid and reroutes their arrows. Required relations must all hold. Preferred relations follow in source order; each is accepted whole or reported through onUnsupportedHints with reason contradiction or undecided. A required relation that cannot be placed throws the new StatedRelationsError, whose data.status says which statement layout makes:

  • contradiction, with a certificate: exact weights on rows named by hint paths, pins, same-rank groups and lanes, checked again by an independent checker that rebuilds the rows from the source;
  • branch, when only the engine's drawing order stands in the way;
  • undecided, when no grid placement was found — not a proof about the map;
  • routing, when the boxes were placed but arrows could not be routed.

check(source, resolved) judges every relation from the drawing alone with a tolerance of 1/128 unit, so the hints also check maps drawn by hand. The JSON Schema, API report, docs/FORMAT.md, docs/LAYOUT.md, the layout ADR and a new example (examples/relations) describe all of this.

Why

Authors often know relations, not coordinates: "the receipt is next to the cashier", "the menu board is halfway between". Pins fix absolute positions and same-rank only lines boxes up along one axis. These hints state the intent directly and keep it checkable.

The meaning is exact on purpose. Each hint compiles to difference bounds or integer equalities in grid ticks, with doubled centres so half-tick midpoints stay exact. A system that has a solution in fractions but none on the grid is reported as undecided, never as a contradiction, and a certificate is only reported after the independent checker accepts it. A conflict that needs the engine's choice of order is reported as such, not as a fault of the map.

Evidence

  • npm run verify (everything except browser tests, which run in CI): passes; 195 test files, 1567 unit tests.
  • New tests:
    • rows measure the same geometry as check() (property tests);
    • schema grammar and references;
    • check() verdicts, tolerance and default gaps;
    • placement outcomes, including a branch conflict resolved on the other axis;
    • certificate checks, including nine kinds of tampering;
    • the review's controls: a fractional midpoint (undecided), an exact contradiction that a rounded drawing still passes, equal spacing between pinned ends, mutations;
    • constructive and metamorphic properties: hints read off a grid drawing and placed from a perturbed start, swapped between ends with k → n − k, mirrored relations, translation;
    • layout with dagre and ELK, lanes and required pins in both directions, a contradiction against lane order;
    • the new example with both engines, with no hard defects.
  • An exhaustive small-grid oracle, 150 random systems: 120 placed (every hint holds, pins unmoved), 23 contradictions (the oracle finds no point for any of them), 7 branch conflicts, 0 undecided; 5 systems the oracle can solve were left as branch conflicts.
  • Sizes (gzip): stated-relations stage 33 999 B, a new on-demand entry; layout port 26 354 B. Every entry that validates maps grows by about 600 B for the four hint kinds in the schema; the raised limits were approved by the author in the issue.

Decisions for the author to review

  • Custom engines are supported through the shared stage, like same-rank, rather than refused; the issue left this open.
  • When arrows cannot be routed after placing preferred relations, all preferred relations are dropped and the required ones are routed alone.
  • Boxes a relation must move are held at the nearest feasible offset from their engine position. There is no promise of the most compact placement.
  • On the axis solved first, the engine's order of boxes that overlap across it may be given up when the other axis can separate them.

Not done

  • Pictures of the example: browser screenshots are not available in this environment; CI runs the browser suites.
  • General integer solving and proofs that a grid point does not exist remain out of scope (undecided instead).
  • The comparison pages do not list the new example yet.

Changelog

BEGIN_COMMIT_OVERRIDE
feat: add stated relations between boxes to the layout format
END_COMMIT_OVERRIDE


Generated by Claude Code

ViktorMikhalkin and others added 14 commits December 3, 2026 00:40
Add relation, align, between and equalSpacing hints with their grammar
(gaps on the 1/64 grid, k of n positions, axes) and reference checks.
Until the shared solver places them, preferred hints are reported as
unsupported and required hints are refused by the engine.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEu3FJNYjbZ19DyaMxzVhb
check() measures each relation, align, between and equalSpacing hint on
the drawn boxes with a tolerance of 1/128 unit, so hand drawings off the
grid can pass, and reports the measured and stated values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEu3FJNYjbZ19DyaMxzVhb
Each hint becomes difference bounds or integer equalities in 1/64 ticks,
with doubled centres for alignment and midpoints, and every row names its
hint path, axis and role. Tests tie each row to the geometry that check()
measures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEu3FJNYjbZ19DyaMxzVhb
Place accepted relations axis by axis on the exact grid, starting from the
engine drawing and freeing only the boxes a certificate or a missing grid
point names. A contradiction that needs no engine row comes with a
certificate in source terms; one that needs the engine's order is a branch
conflict; a search without a result is undecided.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEu3FJNYjbZ19DyaMxzVhb
After the engine and the other hint stages, a lazily loaded stage places
required relations, then preferred ones in source order, and reroutes the
arrows of moved boxes. Required failures throw StatedRelationsError with a
contradiction certificate, a branch conflict or an undecided result;
dropped preferred relations reach onUnsupportedHints with their reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEu3FJNYjbZ19DyaMxzVhb
Keep every file under 200 lines and every function under 40, as the
repository's budgets require, and give the stated-relation error its own
module. Behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEu3FJNYjbZ19DyaMxzVhb
A contradiction certificate names each row by a stable id, its hint path,
role and origin, with exact weights. A separate checker rebuilds the rows
from the source and confirms the weighted sum is impossible; layout never
reports a contradiction the checker rejects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEu3FJNYjbZ19DyaMxzVhb
Each box a relation frees is held at the nearest feasible offset from its
engine position, so results no longer depend on the solver's arbitrary
point and translate with the drawing. Property tests cover hints read off
a grid drawing, swapped between ends, mirrored relations and translation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEu3FJNYjbZ19DyaMxzVhb
Placement needs one checked grid point, not two, so preferred relations
are no longer dropped as undecided when the system is merely not fixed.
When arrows cannot be routed, preferred relations are dropped and the
required ones are routed alone. The new example uses all four kinds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEu3FJNYjbZ19DyaMxzVhb
Lanes are bands along the rank direction, so their order is now a row on
that axis and moved lanes fit their members with the engine padding. Tests
add an exhaustive small-grid oracle, lanes with required pins in both
directions, and a contradiction against lane order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEu3FJNYjbZ19DyaMxzVhb
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEu3FJNYjbZ19DyaMxzVhb

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant