Skip to content

Connecting a node onto a filled single-valued pin deletes the node that was there, and undo does not bring it back #414

Open

Description

@matt-edmondson

What's wrong

#91 fixed the sequence-pin (Cardinality: Many) case of "connecting onto a filled pin silently drops a node". The single-valued case is still broken.

  • Coder.Graph/AstGraph.cs:343-354: OccupantAt returns null for any slot that is not Cardinality.Many.
  • AstGraph.cs:393 (Place): displaced = OccupantAt(location) is therefore always null for a One slot, so the branch at lines 417-420 that keeps a swapped-out node loose (detached.Add(displaced)) never runs.
  • AstSchema.TryAttachAt (Coder.Graph/AstSchema.cs:192-212): sends a One slot to TryAttachOperand, which simply overwrites the property (e.g. case (BinaryExpression binary, "Left")).
  • ValidateConnection (AstGraph.cs:661-694): does not refuse a filled pin.
  • AstGraphEditor.RecordMove (Coder.Graph/AstGraphEditor.cs:~1621-1645): restores only a displaced node that OccupantAt reported, so undo restores nothing here either.

The doc comment on Place promises that "dropping a node onto a filled pin swaps it in, and the user can see what came out and connect it somewhere else". For single-valued pins that promise is not kept.

Reproduction

The review reproduced this with a scratch test against AstGraph:

  1. Start from a function whose body is return a + b, and add a loose VariableReference("c").
  2. Connect c's output pin to the BinaryExpression's Left pin. Connect succeeds and binary.Left is c.
  3. a is no longer in graph.Nodes, and graph.Detached.Count == 0. The subtree is gone.
  4. Undo, which is graph.PutBack(c, AstLocation.Detached). binary.Left becomes the empty placeholder literal instead of a, and a is still not in the graph.

The same happens for any filled single-valued pin: binary/unary operands, a return value, a variable's initial value, an assignment's value, a call target, and so on. One drag destroys user work in a way that undo cannot recover.

Suggested fix

  • Make OccupantAt report the current occupant of a One slot too. That is AstSchema.ChildrenOf(parent, slot)[0] when the slot holds a node that is not IsUnfilled. Place then moves it to detached through the existing branch.
  • Check that RecordMove restores the displaced node on undo in the right order for One slots: the displaced node is put back only after the moved node has left the slot, otherwise the moved node overwrites it again.

Acceptance criteria

  • Connecting onto a filled single-valued pin leaves the previous occupant in graph.Detached, with its subtree intact.
  • Undoing that connection restores the original occupant to the slot and returns the moved node to where it came from.
  • Tests next to AstGraphSequenceUndoTests cover at least a binary operand and a return value, for both connect and undo.

Activity

  1. added
    bug
    on Oct 31, 2026
  2. matt-edmondson commented on Oct 31, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions