Skip to content

ui: let a host refuse a diagram fence by its source, before any engine runs - #4146

Merged
backnotprop merged 3 commits into
mainfrom
feat/ui-diagram-source-accept
Nov 27, 2026
Merged

backnotprop merged 3 commits into
mainfrom
feat/ui-diagram-source-accept

Conversation

@backnotprop

Copy link
Copy Markdown
Owner

Lets a host that consumes @plannotator/ui refuse a diagram fence by its source, before either engine runs. Until now the only host hook, accept (DiagramSvgAcceptContext), was handed the finished svg, after the engine had already run.

What it is

  • DiagramSourceAcceptContext (components/diagramSourceAccept, type DiagramSourceAccept, also exported from the components/diagram barrel): an optional React context holding (kind, source) => boolean. It follows the same convention as DiagramSvgAcceptContext: its own small module, so a host can provide it per subtree without importing the diagram engine.
  • useDiagramRender asks it before renderDiagram, synchronously, once per (check, kind, source). A theme change or a Retry does not ask again. kind is 'mermaid' or 'graphviz'. source is the fence's text as written, before any rewriting (the sankey header normalisation runs later, inside the Pele path).
  • Only true accepts. A check that throws refuses that fence (with a console.warn), and the rest of the page renders. Unlike accept under Pele, a throw never sends the fence on to Mermaid.
  • A refused fence loads no engine (Pele, Mermaid or Graphviz) and is not parsed or drawn. It resolves to the same refused result an accept refusal gives (DIAGRAM_REFUSED_RESULT, now exported from utils/diagram-render), so the block shows its source as code, with no Expand. There is no new UI and there are no new words.
  • accept still runs after it, as before.
  • Code review: ReviewDiagramFence asks the same check and does not import its diagram engine for a refused fence. The engine shares the context module with the page (DIAGRAM_ENGINE_SHARED_MODULES), and the engine build passes.
  • With no check, every render is exactly as before. Plannotator's own apps pass none.

It stops the engines from running for a refused fence. It does not replace diagramRemoteLoads: false or an enforced page CSP, which still decide what an accepted fence loads.

Tests

  • packages/ui/components/diagram/diagramSourceAccept.test.tsx (DOM, registered in tests/dom-test-list.txt). The engines are counted through their loader seams. A refused fence gives refused, with zero Pele, Mermaid and Graphviz loads, no parse and nothing added outside the block. It looks the same as an accept refusal. A theme change does not ask again. Accepted renders the same as no check. A check that throws refuses. Only true accepts. With one fence accepted and one refused, the engines load only for the accepted one. The sankey header row reaches the check as written.
  • MarkdownFilePreview.test.tsx: a refused ReviewDiagramFence never imports the engine, and an accepted one beside it does.
  • tests/e2e/diagram-remote-loads.spec.ts (Chromium and WebKit, through useDiagramRender in a React mount): with a refusing check and no CSP, every source in the file makes zero requests to another host and loads no engine. This holds when Mermaid draws every fence and with the switch as shipped. With an accepting check, the sources behave as with no check.

Release

No version bump. The new module is covered by the existing ./components/* export wildcard. The docs say it ships in the first ui after 0.66.0.

…e runs

- New opt-in DiagramSourceAcceptContext (components/diagramSourceAccept,
  type DiagramSourceAccept, also in the components/diagram barrel):
  (kind, source) => boolean, asked by useDiagramRender before renderDiagram.
  kind is 'mermaid' or 'graphviz'; source is the fence's text as written.
  Asked synchronously, once per (check, kind, source). Only true accepts;
  a check that throws refuses that fence and never falls back to an engine.
- A refused fence loads no engine (Pele, Mermaid, Graphviz), is not parsed or
  drawn, and resolves to the same refused result an accept refusal gives, so
  the block shows its source as code. accept still runs after it.
- Code review: ReviewDiagramFence asks the same check and does not import its
  diagram engine for a refused fence; the engine shares the context module
  with the page (DIAGRAM_ENGINE_SHARED_MODULES).
- Absent: every render is exactly as before. Plannotator's apps pass none.
- Tests: components/diagram/diagramSourceAccept.test.tsx (DOM), a
  ReviewDiagramFence case in MarkdownFilePreview.test.tsx, and browser tests
  in diagram-remote-loads.spec.ts: refused, with no CSP, no request to
  another host and no engine loaded; accepted, as with no check.
- Docs: README and HANDOFF (layers, what a host must do itself, what changed
  for hosts), code-review guide.
@backnotprop backnotprop added the ci:e2e Run the full browser suite (Inbox window e2e) on this PR label Nov 27, 2026
… result

A fence that was drawn and is then refused by the host's source check
(its source changed in place, or a new check function) now shows exactly
what a fresh refusal shows: no drawing, no controls, and an open pop-out
closes. A parse error still keeps the last good render. The check's answer
is remembered per function, kind and source so the block and its viewers
share one call. DIAGRAM_REFUSED_RESULT is frozen. A structural test keeps
renderDiagram and the engine loaders to their allowed callers inside
packages/ui. The docs say which paths the check covers and how to supply
a check whose policy loads later or changes.
Remove the page-wide remembered answers: an answer now lives in the
mounted component that asked, and a remount asks again. A diagram block
hands its answer to its inline view and pop-out through a context scoped
to the block, so the host is still asked once per mounted block. The
structural test also covers the loaded-engine getters and the engine
packages' dynamic imports, and scans whole files.
@backnotprop
backnotprop marked this pull request as ready for review November 27, 2026 18:33
@backnotprop
backnotprop merged commit 21ec6b7 into main Nov 27, 2026
73 of 75 checks passed
yermakoffivan pushed a commit to yermakoffivan/plannotator that referenced this pull request Nov 27, 2026
…d Pele 1.13

core 0.25.35 carries what merged after 0.25.34 went to npm: exact-phrase
search in inbox-search (words in double quotes, backnotprop#4117, backnotprop#4124) and the
agent-gone event in external-annotation's ExternalAnnotationEvent (backnotprop#3698).
No exports subpath changes.

ui handles that agent-gone event, so its pin follows and core goes first.
ui 0.67.0 is a minor: DiagramSourceAcceptContext lets a host refuse a fence
by its source before any engine runs (backnotprop#4146), DIAGRAM_REFUSED_RESULT is
exported, Pele 1.13.0 with the inline fit changes (backnotprop#4113), the agent-gone
send failures (backnotprop#3698) and the plan header seams (backnotprop#3905). README and HANDOFF
name the new pair and the order (core first), and the hook and Pele notes
now name ui 0.67.0 instead of "the first ui after 0.66.0".

bun.lock is bun install's own output: the two package version lines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:e2e Run the full browser suite (Inbox window e2e) on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant