Skip to content

Graph: treat updater returning the same node instance as no edit - #6095

Closed
William Wong (compulim) with Copilot wants to merge 1 commit into
mainfrom
copilot/update-graph-upsert-logic
Closed

William Wong (compulim) with Copilot wants to merge 1 commit into
mainfrom
copilot/update-graph-upsert-logic

Conversation

Copilot AI commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

In packages/store-graph, an upsert() whose updater returns the node it received should leave that node unchanged. On insert, the updater receives undefined, so returning it must still throw.

controller.edit(transaction => {
  // No-op if `_:m-1` exists and hasn't been changed earlier in this transaction.
  // Throws if `_:m-1` doesn't exist, because the updater returns `undefined`.
  transaction.upsert('_:m-1', node => node!);
});

Behavior

  • Clean read (node not yet changed in this transaction): the updater receives the frozen node from the graph. Returning that same instance makes no edit:
    • The snapshot stays the same and listeners are not notified.
    • The node is not marked as upserted. If its parent drops it later in the same transaction, its isPartOf is still auto-fixed instead of throwing.
  • Dirty read (node already changed in this transaction): unchanged behavior. The updater receives a writable copy that it may modify in place and return. An existing test covers this, so returning the same instance here still counts as an edit.
  • Insert: unchanged. Returning undefined still fails validation in createDraftNode(). This PR adds a test for it.

A clean read never has pending hasPart/isPartOf links to confirm. Any link added by the other side during the transaction turns the node into a dirty read. So returning early on a clean read skips no conflict checks.

Changes

  • Graph.ts: the updater call moves into a runUpdater() closure, which returns undefined on a clean-read no-op. This also keeps upsert under ESLint's complexity limit of 20. The upsert() JSDoc describes the new behavior.
  • Graph.test.ts: tests for the no-op case (same snapshot, no notification), auto-fix after a no-op, and insert returning undefined.
  • OPINIONS.md: documents the rule and the dirty-read exception.

Open question

Should returning the same instance on a dirty read also be a no-op? That would mean freezing dirty reads, which breaks the current in-place-edit contract.

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
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.

2 participants