Skip to content

Building in the app: drop layers on a composition, edit hand-written scripts (new lines, or a faithful rewrite), move around the canvas - #187

Draft
gidili wants to merge 11 commits into
stroop-model-testingfrom
composition-drop
Draft

gidili wants to merge 11 commits into
stroop-model-testingfrom
composition-drop

Conversation

@gidili

@gidili gidili commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Stacked on #186 (it reuses that PR's build-a-model test). Its base should change once #186 merges.

Four changes, all about building on a model in the app and keeping its script right.

1. A layer dropped on a composition's box joins that composition

The problem

Dropping a layer from the sidebar onto a composition's box in the main canvas makes it look like it's inside the composition, but it isn't:

  • the saved script has no add_node for it;
  • the composition stays empty;
  • Run fails with "Inputs to Composition1 must be specified … for each of its 0 INPUT nodes".

The only way to add layers today is to open the composition first (the icon on its name), and nothing in the app says so.

The change

A dropped layer now joins the deepest composition under the pointer. It uses the same code (MetaGraph.updateGraph) that re-parents a node when it's moved. A layer dropped outside every composition stays a separate node.

This follows issue #36 (closed), which describes compositions as boxes that contain their nodes:

the composition will be visualised as a square that contains all the nodes inside … if a node gets dropped outside the composition then it's not part of that anymore

Question for the team: is #36 still the intended behaviour? Since then, moving nodes on the canvas has been turned off (disableMoveNodes: true), so I may be missing a later design decision. If "open the composition first" is the intended flow, a hint on the box would be the alternative.

2. Added layers and links are written into a hand-written script as new lines

The problem

In a script loaded from a file, changed values are already written into the file one argument at a time (#186). Adding a layer or a link was different: the whole file was regenerated from the diagram. That loses what the diagram doesn't show (weights, comments, the script's own variables and code), so #186 asks "Rewrite the script?" first. For the Cohen & Huston Stroop model, the regenerated script doesn't even build.

The change

Run, Update Model and Save As now add a few lines to the script, using its own variables. Adding a layer to the Stroop model and linking it both ways with the TASK layer gives:

# Added in PsyNeuLink Viewer
TransferMechanism1 = pnl.TransferMechanism(integrator_mode = False, termination_threshold = None, name = 'TransferMechanism1', output_ports = ['RESULT'], input_ports = ['InputPort_0'])
TransferMechanism1_to_TASK = pnl.MappingProjection(sender = TransferMechanism1, receiver = task_layer, name = 'TransferMechanism1 to TASK')
TASK_to_TransferMechanism1 = pnl.MappingProjection(sender = task_layer, receiver = TransferMechanism1, name = 'TASK to TransferMechanism1')
Bidirectional_Stroop.add_node(TransferMechanism1)
Bidirectional_Stroop.add_projection(TransferMechanism1_to_TASK)
Bidirectional_Stroop.add_projection(TASK_to_TransferMechanism1)
  • Where the lines go: right after the last top-level line that builds what they refer to, so before any code that runs or plots the model. The rest of the file stays byte for byte.
  • How they're generated: by the same code that writes a new script (CodeGenerator, which can now write only some components). New variables keep clear of every name the script uses.
  • Links are named after their layers ("TASK to TransferMechanism1"), not the app's internal ids, so the script and PsyNeuLink's messages are readable.
  • Links connect layers, not ports: the app names a loaded layer's ports after its links (for example TASK_input_port_TASK_recurrent_projection), not after PsyNeuLink's ports.
  • Nothing half-written: the new script is built before the file is written. If it doesn't build, the file and the model stay as they were. Writing the same change again (for example after a run that failed) adds nothing.
  • When the lines can't be placed (for example, a layer is set inside an if for an option): the app asks "Rewrite the script?", says why, and Cancel leaves the file untouched.
  • Unchanged cases: scripts the app generated itself are regenerated as before. Anything removed or moved (e.g. a layer dragged out of its composition with the select tool) goes through the rewrite question (see 3).

Fixed along the way:

  • New layers get a name no component has yet. The count restarted in each session, so a reopened file could get a second TransferMechanism1.
  • PsyNeuLink names in values are written as pnl.X: DDM's input_format = SCALAR broke scripts that don't do from psyneulink import *.

Found, not fixed here

  • The viewer only shows components the script assigns to a top-level variable. Layers built inline (pathways=[pnl.TransferMechanism(...)]) don't appear.

3. Rewriting a loaded script keeps the model

The problem

Some changes can't be written into a hand-written script line by line: a layer dragged out of its composition (select tool), layers or links removed, or added lines with no safe place to go. Those used to regenerate the whole script from the app's copy of the model. For a loaded model like the Stroop one, that copy has no weights or layer sizes and has made-up port names, so the regenerated script was a different model. For Stroop it failed outright ("already has an identical Projection") on the recurrent layers' self-links.

The change

The change is made to the live PsyNeuLink model, and PsyNeuLink writes the script for it (get_mdf_serialized + generate_script_from_mdf, in model/scriptRewrite.py):

  • Building the model: it's built afresh from the script with the changed values written in, and the additions are run in it. Then layers are moved or removed (Composition.remove_node / add_node) and links removed (remove_projection). A layer moved out of a composition loses its links there; that's what remove_node does.
  • What's kept: every layer, link, weight and setting. Names are put back ("COLORS HIDDEN", not the exporter's "COLORS_HIDDEN"), and layers outside any composition are written too.
  • Checked before writing: the new script is built and compared with the live model: layers (type and size), links and weights, and a 3-trial run with every input at 1. If anything differs, nothing is written and the app says why. That's the case for models with learning, which the exporter leaves out.
  • Asked first: the "Rewrite the script?" dialog says what's kept and what's lost (comments, the script's variable names, helper code, and code that runs or plots the model). The app then opens the file again, so the diagram shows what's in it.
  • Generated scripts unchanged: scripts the app generated itself are still regenerated from the app's model.

Found along the way: PsyNeuLink 0.21.1 writes a broken script when a model is exported a second time (the app reads json_summary on load, which counts): it connects projections to input ports the new script doesn't have. 0.15.3 is fine. The rewrite builds the model afresh and exports it once. We'll report this upstream separately.

4. Moving around the canvas

Panning and zooming were never turned off: meta-diagram 0.3.2 ignores the disableZoom and disableMoveCanvas settings. But nothing showed it. The Stroop model opened at 100% with most of it below the window, and scrolling zoomed, which on a trackpad is where people expect to pan.

  • Opening a model zooms out to fit it between the tool bar and the Model Hierarchy panel, no further than 50%, so names stay readable. A bigger model shows its top part. It never zooms in.
  • Zoom controls at the bottom left: zoom out, the zoom level (click it to go back to 100%), zoom in, and fit the whole model.
  • Scrolling (mouse wheel, two fingers on a trackpad) pans. Pinching, or Ctrl/⌘ + scroll, zooms around the pointer, as in Figma or Miro. Something inside the canvas that scrolls by itself, such as a long properties panel, still scrolls.
  • The view of a model is kept when the canvas is drawn again, for example after closing a composition.
  • An opened composition is fitted in its window the same way, and the zoom controls work on it while it's open.
  • Found, not fixed: panning or zooming over an open composition also resizes its window (handlePostUpdates, width × zoom). It looks wrong but does no harm in the tests; it needs its own look.

Question for the team: what were disableZoom, disableMoveCanvas, disableMoveNodes and disableDeleteDefaultKey (psyneu-65) meant for? They have no effect today: the canvas pans, zooms and moves nodes (Delete does nothing, for another reason I haven't looked into).

Tests

  • e2e/build-a-model.spec.ts drops one layer on a composition's box and one outside it, saves, and checks that the saved script's composition contains only the first.
  • e2e/published-models.spec.ts drops a layer on the Stroop composition and links it both ways with the recurrent TASK layer. It then checks that the file gained only the new lines, and that running in the app matches plain PsyNeuLink running the file.
  • e2e/hand-written-scripts.spec.ts covers the "Rewrite?" question (Cancel, and agreeing from Run). It also checks that a new layer doesn't reuse a name the script already has.
  • src/server/tests/test_script_additions.py (18 tests) adds to Stroop and to other scripts. It uses every layer type the sidebar offers, with the values captured from the app, and writes each one twice. It also covers failures, reopening the file, and Save As. The tests pass on PsyNeuLink 0.15.3 and 0.21.1.
  • src/client/tests/scriptEdits.test.js checks which changes can be written into a script.
  • src/server/tests/test_script_rewrite.py: Stroop with TASK_INPUT moved out (same weights and results as plain PsyNeuLink taking it out), a second rewrite of the rewritten script, a value change with a move, learning refused, and additions rewritten. The tests pass on PsyNeuLink 0.15.3 and 0.21.1.
  • e2e/published-models.spec.ts also drags TASK_INPUT out of the Stroop composition, agrees to the rewrite, and compares the new file with plain PsyNeuLink.
  • e2e/canvas-navigation.spec.ts checks fitting on opening, scrolling and Ctrl + scroll, the zoom buttons, that the view is kept after opening a composition, and that an opened composition is fitted and can be zoomed.
  • The e2e tests also pass with the app window shrunk to 1024×700, closer to CI's screens.

🤖 Generated with Claude Code

gidili and others added 6 commits December 5, 2026 17:32
Dropped from the sidebar onto a composition's box in the main canvas, a layer looked like it was
inside but stayed a separate, loose node: the saved script had no add_node for it, the
composition stayed empty, and Run failed with "Inputs to Composition1 must be specified ... for
each of its 0 INPUT nodes". Only dropping into the composition's own canvas (opened with the
icon on its name) added layers to it, and nothing said so.

A dropped layer now joins the deepest composition under the pointer, with the same code that
re-parents a moved node. This follows issue #36 ("the composition will be visualised as a square
that contains all the nodes inside ... if a node gets dropped outside the composition then it's
not part of that anymore"). A layer dropped outside every composition stays a loose node.

Test: build-a-model.spec.ts drops one layer on the box and one outside, saves, and checks the
saved script's composition contains only the first.

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

On CI's smaller screen the fixed spot fell inside the composition's box, so the layer meant to
stay outside joined it. The spot is now chosen beside or below the box, wherever the canvas has
room. Passes with the default window and with the window shrunk to 1024x700.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M7yBhRTy54udAfmgtfbNjQ
Brings in #186's latest changes, including the appdmg retry for CI's Intel runners.

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

Adding a layer or a link to a loaded script used to mean regenerating the whole file from the
diagram (losing comments, weights and the script's own code), behind a "Rewrite the script?"
question; for the Stroop model the regenerated script didn't even build. Now Run, Update Model
and Save As write additions as a few new lines, using the script's own variables:

    # Added in PsyNeuLink Viewer
    TransferMechanism1 = pnl.TransferMechanism(...)
    projection_... = pnl.MappingProjection(sender = TransferMechanism1, receiver = task_layer, ...)
    Bidirectional_Stroop.add_node(TransferMechanism1)
    Bidirectional_Stroop.add_projection(projection_...)

- Placed after the last top-level line that builds what they refer to, so before any code that
  runs or plots the model; the rest of the file stays byte for byte. If a variable they need is
  made inside a loop, function or if-block, nothing is written and the error is shown.
- Generated with the same code as a new script (CodeGenerator, now able to write only some
  components); new variables keep clear of every name the script uses.
- Links connect layers, not the ports the app shows: for a loaded layer those are named after
  its links (e.g. TASK_input_port_TASK_recurrent_projection), not PsyNeuLink's ports.
- The new script is built before the file is written; if it doesn't build, the file and the
  model are left as they were. Repeating a change (e.g. after a failed run) adds nothing again.
- Components are looked up in the script's variables, which also works with PsyNeuLink 0.21.1
  (where reading a projection's loggable items fails and cut the component list short).
- The app (scriptEdits.ts) still sends removed, renamed or moved items to the regenerate path,
  with its question; the app currently has no way to remove, rename or move items.

Tests: Python unit tests for adding to Stroop and other scripts (pass on PsyNeuLink 0.15.3 and
0.21.1), Jest tests for which changes can be written in, and an e2e test that drops a layer on
the Stroop composition, links it both ways with a recurrent layer, runs it, and compares with
plain PsyNeuLink running the file. It replaces the e2e test of the "Rewrite?" question, which
adding a layer no longer asks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M7yBhRTy54udAfmgtfbNjQ
…ix repeats and names

Follow-up to writing added layers and links into the script as new lines:

- When the new lines can't be placed (e.g. a layer they link to is set inside an if-block), the
  backend's error starts with CANT_ADD_LINES and the app asks "Rewrite the script?", saying why,
  then sends the same Run / Update Model / Save As again without the edits. Cancel leaves the
  file untouched. Before, this was only an error with no way forward.
- New layers get a name no component in the model has. The count restarts each session, so a
  reopened file could get a second "TransferMechanism1" (a bug before this change too).
- Psyneulink names in values are written as pnl.X (DDM's input_format = pnl.SCALAR): a
  hand-written script may not import psyneulink's names, and DDM broke it.
- Writing an addition again (e.g. after a failed run) uses the generator's rendering of values
  and leaves empty ones alone, so 'HollowMatrix' stays a string and required defaults
  (ContrastiveHebbianMechanism's sizes) stay.
- A link counts as written if the projection named after it exists, not if its layers are
  already linked: a second link between linked layers isn't skipped silently, PsyNeuLink's own
  error ("already has an identical Projection") is shown.
- Scripts the app generated itself are regenerated with additions, as before; only hand-written
  ones get new lines.

Tests: every layer type the sidebar offers, with the values captured from the app
(tests/fixtures/app_new_layers.json), added to Stroop twice; reopening the file and adding
again; a layer set inside an if-block. e2e: hand-written-scripts.spec.ts checks the question,
Cancel, and the regenerated script. Python tests pass on PsyNeuLink 0.15.3 and 0.21.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M7yBhRTy54udAfmgtfbNjQ
- hand-written-scripts.spec.ts: the script already has a TransferMechanism1, and the layer
  dropped in the app gets another name; agreeing to regenerate now goes through Run (the
  request users send most), which then runs the regenerated script.
- A value the panel leaves as null again removes the argument, as before: writing an addition
  again no longer needs it written as None (the generator's rendering already gives 'None').

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M7yBhRTy54udAfmgtfbNjQ
@gidili gidili changed the title A layer dropped on a composition's box joins that composition Adding layers in the app: drop them on a composition's box, and write them into hand-written scripts as new lines Dec 6, 2026
gidili and others added 2 commits December 5, 2026 23:39
The every-layer-type test rebuilt the Stroop model twice per type (24 rebuilds), and the step
hit its 10-minute limit on CI's Intel runner. It now adds all types in one change and repeats it
once (same checks, 2 rebuilds); the step's limit is 20 minutes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M7yBhRTy54udAfmgtfbNjQ
Panning and zooming were never turned off (meta-diagram 0.3.2 ignores the disableZoom and
disableMoveCanvas settings), but nothing showed it: the Stroop model opened at 100% with most
of it below the window, and scrolling zoomed, which on a trackpad is where people expect to pan.

- Opening a model zooms out to fit it in the part of the canvas between the tool bar and the
  Model Hierarchy panel, no further than 50% (smaller, names can't be read): a bigger model
  shows its top part. It never zooms in.
- Zoom controls at the bottom left: zoom out, the zoom level (click: back to 100%), zoom in, and
  fit the whole model.
- Scrolling (mouse wheel, two fingers on a trackpad) pans; pinching, or Ctrl/⌘ + scroll, zooms
  around the pointer, as in Figma or Miro. Something inside the canvas that scrolls by itself
  (a long properties panel) still scrolls.
- The view of a model is kept when the canvas is drawn again, e.g. after closing a composition.
- Over an open composition there are no zoom controls and no fitting: its dialog covers the
  controls, and panning or zooming there also resizes the composition (handlePostUpdates).

Tests: e2e/canvas-navigation.spec.ts (also passes with the window at 1024×700).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M7yBhRTy54udAfmgtfbNjQ
@gidili gidili changed the title Adding layers in the app: drop them on a composition's box, and write them into hand-written scripts as new lines Building in the app: drop layers on a composition, write additions into hand-written scripts, move around the canvas Dec 6, 2026
Changes that can't be written into a hand-written script line by line (a layer dragged out of
its composition, layers or links removed, additions with no place for a line) used to
regenerate the script from the app's copy of the model. For a loaded model like the Stroop one
that lost the weights and layer sizes, turned the app's port names into real input ports, and
failed outright on recurrent layers' self-links.

Now the change is made to the live model and PsyNeuLink writes the script for it
(get_mdf_serialized + generate_script_from_mdf, model/scriptRewrite.py):

- Built afresh from the script with the values written in, the additions run in it, then layers
  moved or removed (Composition.remove_node / add_node, which takes a moved layer's links in the
  old composition with it) and links removed (remove_projection).
- The export keeps every layer, link, weight and setting; names are put back ("COLORS HIDDEN",
  not the exporter's "COLORS_HIDDEN"), and layers outside any composition are written too.
- Before anything is written, the new script is built and compared with the live model: layers
  (type, size), links and weights, and three trials run with every input at 1. If they differ,
  nothing is written and the reason is shown, e.g. for learning, which the exporter leaves out.
- Built once and exported once: PsyNeuLink 0.21.1 writes a broken script for a model it has
  serialized before (json_summary does), referring to input ports that don't exist.

The app (scriptEdits.ts) now sends moves and removals instead of giving up, asks before a
rewrite (the dialog says what's kept and what's lost: comments, variable names, other code), and
opens the file again afterwards so the diagram is the new file's. When added lines can't be
placed, agreeing sends the same changes again asking for a rewrite. Scripts the app generated
itself are still regenerated from the app's model.

Tests: test_script_rewrite.py (Stroop with a layer moved out: same weights and results as plain
PsyNeuLink; a second rewrite; a value with a move; learning refused; additions rewritten), on
PsyNeuLink 0.15.3 and 0.21.1; Jest for the reported changes; an e2e test dragging TASK_INPUT out
of the Stroop composition and comparing with plain PsyNeuLink.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M7yBhRTy54udAfmgtfbNjQ
@gidili gidili changed the title Building in the app: drop layers on a composition, write additions into hand-written scripts, move around the canvas Building in the app: drop layers on a composition, edit hand-written scripts (new lines, or a faithful rewrite), move around the canvas Dec 6, 2026
gidili and others added 2 commits December 6, 2026 01:13
The box's resize handles are drawn after its name chip, and the top one is a strip along the
box's top edge. Zoomed far out (CI's smaller screen, after "Fit to window"), it caught the click
meant for the chip's icon, so the composition couldn't be opened.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M7yBhRTy54udAfmgtfbNjQ
…er their layers

- Opening a composition (the icon on its name) showed its layers where they are in the whole
  model, at 100%: for the Stroop model, mostly off to the right and below the dialog. It's now
  fitted like a model being opened (down to 50%, top first), with the zoom controls inside the
  dialog. Coming back keeps the main view as it was.
- Links added in the app were written as e.g.
  "projection_6167e875cedf = pnl.MappingProjection(..., name = 'projection_6167e875cedf')",
  after the app's id for the link. They're now named after the layers they connect:
  "TASK_INPUT_to_Extra = pnl.MappingProjection(..., name = 'TASK_INPUT to Extra')".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M7yBhRTy54udAfmgtfbNjQ
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