Skip to content

feat(chat): /untangle tries layouts against crossings and puts the best to the person - #234

Merged
endrix merged 3 commits into
mainfrom
feat/untangle-chat-command
Nov 24, 2026
Merged

endrix merged 3 commits into
mainfrom
feat/untangle-chat-command

Conversation

@endrix

@endrix endrix commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

What

/untangle in the diagram chat (Build mode). The agent lays the diagram out with Layout Diagram, the drawing to beat. It then makes at most ten tries against edge crossings, starting with Free Ports, where every instance of a definition takes one port order (#233). The best try goes to the person in a card, with its picture and the source files it would change. Apply to N files writes the port orders (huawei-csl/wfpy#92 reorderEntityPorts) and saves the layout, both as one undo. The chat then shows the Layout Diagram drawing and the untangled one side by side. Discard draws the diagram as it was.

How

diagram-server (neutral)

  • LayoutPreview (operations/layout-preview.ts):
    • runs a tuned layout with persist: false and given port orders;
    • draws the result without saving it: UpdateModelAction and a fit, ahead of the picture's export, because the client draws only what is in view.
  • layoutGeometryOf / applyLayoutGeometry / saveLayoutGeometry (server/layout-snapshot.ts): a drawing taken, drawn again, or saved as the layout.
  • definition-port-order.ts:
    • sidesOf(node, 'drawn' | 'declared') and definitionsUnder(root);
    • the last Free Ports probe, kept per root, so a try can say which order each instance would take on its own.
  • DiagramChatHost (server/diagram-chat-host.ts): what a diagram's session uses to put a card or a message with pictures to the person.

sidecar-toolkit (server/untangle/)

  • untangle-session.ts:
    • one session per diagram: the original drawing, the baseline and the tries;
    • it ends when the model root changes.
    • orderToWrite refills each side's declared slots, so inputs and outputs keep their places among each other.
  • untangle-mcp-tools.ts, the tools:
    • untangle-start, untangle-try and untangle-propose;
    • an 11th try is refused, and an order that names the wrong ports is refused without using a try;
    • Propose shows the card and carries out the answer itself, so the agent's turn ends there.
  • apply-port-orders-handler.ts (dialogram.applyPortOrders):
    • it saves the try's layout, then sends reorderEntityPorts to each definition's own file (entityFileOf);
    • a refused write puts back the files already written and the layout;
    • the layout file stays out of the undo snapshots, because it changes with every move and undo would refuse. Undo and redo save the drawing they go back to before the host reloads.
  • Registered in the operation module only where the backend lists reorderEntityPorts; /untangle checks for that op too.

extension-core

  • ChatCommandResult.prompt: the command's prompt goes to the agent as the turn, with no confirmation and no post-turn hook. The hook's "layout" keyword would otherwise run Compact over the result.
  • /untangle (Build mode) returns UNTANGLE_PROMPT.
  • ChatRuntime.canAsk / postInChat.
  • GlspIntegrationHandle.setDiagramChatHost, wired by the profile runtime.
  • DiagramChatConfirm.figures.

diagram-client

  • Confirm cards and system messages show figures, side by side where the panel is wide enough.
  • A click opens a picture at full size over the panel.
  • Pictures are not stored with the session, the same as images the user sends.

Tests

  • sidecar-toolkit:
    • untangle.test.ts:
      • the order to write, including an input and an output that share a name;
      • drawn, declared and alone orders;
      • Apply writes two files, and undo/redo restores both and the layout;
      • a refused Apply puts back what was written and leaves nothing to undo.
    • untangle-mcp-tools.test.ts:
      • ten tries, then a refusal;
      • bad orders refused without using a try;
      • Propose's card, Apply's operation and the two figures posted;
      • Discard;
      • no chat open.
    • The pinned handler and tool lists now include the new handler and tools.
  • extension-core:
    • a prompt command reaches the agent with no hook and no confirmation;
    • postInChat;
    • /untangle is offered only where the op is listed.
  • diagram-server: a layout geometry drawn again and saved.
  • diagram-client: figures on cards and messages, and a click opening one.
  • Typecheck and the neutrality check pass. Suites pass: diagram-server, sidecar-toolkit 603, extension-core 665, diagram-client 996.
    • The variant-wizard tests are slow on my machine: they look for types around /tmp/flow.py, and my /tmp is large. They pass with a longer timeout.

Review (/code-review medium), fixed in 64bf0e5

  • A card nobody answers no longer locks the untangle. untangle-start starts over from it, and a late answer applies nothing; the chat says so.
  • A try the agent leaves drawn without proposing it is drawn back when /untangle's turn ends, however it ends (ChatCommandResult.afterTurn calls untangle-end). Otherwise the next node move would save it.
  • Apply saves the proposed try's own drawing, not whatever is drawn when the button is pressed.
  • One command (ApplyUntangleCommand). Undo and redo draw and save the layout first, then put the files back. A try that changes no port order still leaves an undo entry for its layout.
  • A definition drawn with different port sets (same name, same file) is marked split: its order is not written, and it cannot be named in orders.
  • Nits:
    • the try count carries across a restart (ten in all);
    • a write that throws is undone like one refused, and the chat says why;
    • a geometry keeps every element's position and size, so Discard restores labels and compartments too;
    • figuresOf is computed once.
  • Tests:
    • restart over an unanswered card;
    • the late answer;
    • untangle-end;
    • afterTurn runs when the turn ends normally and when it fails;
    • the proposed geometry is what gets saved;
    • a layout-only Apply can be undone;
    • a throwing write;
    • split definitions.
  • Suites: diagram-server 1020, sidecar-toolkit 609, extension-core 666, diagram-client 996. Typecheck and neutrality pass.

Live check

The check ran in the wfpy harness with opencode and nemotron-3.5-super-vl-free, on a demo with three Stage, three Merge and two image instances.

  • First run:
    • the agent made 3 tries and proposed try 1;
    • Apply wrote only the six reordered lines in untangle_parts.py: the class Ports lines of Stage and Merge, and the inputs=/outputs= entries of image, each comment kept with its line;
    • the diagram redrew as the try and the comparison was posted;
    • Undo put the file and the layout back; Redo wrote them again.
  • Second /untangle in the same chat: the agent called no tool and answered "Already done" from its first turn. Fixed in 8e79785: the prompt says each untangle starts over. Rerun: 6 tries; Discard drew the original and wrote nothing.
  • Pictures and opencode 2: opencode 2's Code Mode hands MCP results to the model as text only, so the agent above chose from crossing counts. With a project opencode.json that denies execute, the tools are called directly and every result carries its PNG. The agent then broke a 23/23 tie by the picture ("try 4 reads better: shorter edges into m1/m2"). Apply posted both pictures side by side. Dialogram cannot switch this per server: ACP's mcpServers has no field for it.
  • Harness note: the "Nothing is drawn" picture failure seen once came from the headless window shrinking while no debugger was attached, not from the product.

https://claude.ai/code/session_011UYzzUKxNqKPdmLsRmJ6Xu

…st to the person

In Build mode, where the producer can write a definition's port order
(reorderEntityPorts), /untangle hands the agent three tools on the diagram:

- untangle-start lays the diagram out with Layout Diagram, the drawing to
  beat, and lists the definitions with several instances;
- untangle-try draws Free Ports (with the orders the agent gives some
  definitions) or Untangled, at most ten times, each with its crossings and
  its picture, saved nowhere;
- untangle-propose puts one try to the person in a card: its picture, its
  crossings against Layout Diagram, each definition's port order old and
  new per source file, Apply to N files and Discard.

The person's answer is the platform's to carry out. Apply writes the orders
and saves the try's layout as one undo (the layout file kept out of the
snapshots; undo and redo save the drawing they go back to), and a refused
write puts back what was written. The chat then shows the two drawings
side by side. Discard draws the diagram as it was.

A chat command may now answer with a prompt the agent carries out as the
turn, and a diagram's session can put a card or a message with pictures
to the person (DiagramChatHost).

Claude-Session: https://claude.ai/code/session_011UYzzUKxNqKPdmLsRmJ6Xu
… was proposed

- untangle-start starts over from a try put to the person that they never
  answered (a card lost with the panel); the late answer applies nothing
  and the chat says so. The tries made count across a restart: ten in all.
- /untangle's turn over, however it ended, untangle-end draws back a try
  the agent left drawn without proposing it (ChatCommandResult.afterTurn);
  the next node moved would have saved it.
- Apply saves the proposed try's own drawing, not what is drawn when the
  button is pressed, and is one command (ApplyUntangleCommand): undo and
  redo draw and save the layout first, then put the files back; a layout
  with no port order to write is still an edit to undo.
- A write that throws puts back what was written, like one refused, and
  says why.
- A definition drawn with different ports in different places (same name,
  same file) is split: its order is not written, nor given by name.
- A geometry keeps every element's place and size (labels, compartments),
  so Discard draws the diagram as it was.

Claude-Session: https://claude.ai/code/session_011UYzzUKxNqKPdmLsRmJ6Xu
The live check ran /untangle twice in one chat (Apply, Undo, again). The
second time the agent called no tool and answered "Already done" from its
first turn. The prompt now says each untangle is a new one, the diagram
maybe changed since, and that the answer comes from what the tools say now.

Claude-Session: https://claude.ai/code/session_011UYzzUKxNqKPdmLsRmJ6Xu
@endrix
endrix merged commit 83e7925 into main Nov 24, 2026
1 check passed
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