Skip to content

Optimistic updateNode merge doesn't flatten properties.* into top-level fields, causing ai-chat UI to lag behind RPC round-trip #1695

Closed

Description

@malibio

Overview

In an ai-chat node, two visible symptoms share one root cause: the optimistic (pre-round-trip) merge in SharedNodeStore.updateNode() never flattens namespaced properties.* writes into the top-level fields components actually read. The UI only becomes correct once the backend RPC round-trip completes and the response is spread onto local state — so anything gated on those top-level fields appears to "hang" for the duration of one network/IPC round trip.

Problem Statement

Symptom 1 — model-selection textarea delay:
After starting a new ai-chat node and selecting a model, the textarea for typing a message takes a long time to appear.

Symptom 2 — sent message appears late:
After sending a message, it doesn't show up in the conversation until after the AI agent's response comes back — both appear to land together.

Mechanism (both symptoms):

SharedNodeStore.updateNode() (packages/desktop-app/src/lib/services/shared-node-store.svelte.ts, optimistic merge ~lines 990-996) applies the synchronous "optimistic" local update as a shallow { ...existingNode, ...changes }. When changes is a nested/namespaced shape like { properties: { model: ... } } or { properties: { messages: [...] } }, the shallow merge only updates node.properties.* — it does not flatten those values into the top-level fields (node.model, node.provider, node.messages) that AiChatNode (packages/desktop-app/src/lib/types/ai-chat-node.ts) and the viewer read.

The top-level fields are only corrected later, once backendAdapter.updateNode()'s RPC resolves and the backend's already-flattened response (produced server-side via node_to_typed_value/flatten_properties_for_api) is spread onto the local node (~lines 1192-1211 and ~1601-1640 of the same file).

Concretely:

  • handleModelSelect (ai-chat-node-viewer.svelte:144-200) writes properties.provider/properties.model. The textarea's visibility gate (ai-chat-node-viewer.svelte:432, isMessageProvider && !model) reads the top-level node.model, which stays stale until the RPC resolves.
  • handleSend (ai-chat-node-viewer.svelte:259-268) writes properties.messages. The rendered message list (persistedMessages, ai-chat-node-viewer.svelte:103-116) reads the top-level node.messages, which also stays stale until the same round trip completes — so the user's own message and the agent's reply both become visible in the same post-round-trip window, making it look like the message "waited" for the response.

Relation to Prior Work

This is directly adjacent to (but not fixed by) PR #1694 / issue #1690, "Content-only edits forced immediate persistence via redundant nodeType, and property-only updates never synced typed fields back from the backend." That PR fixed this exact class of bug — properties.* writes not syncing to top-level typed fields — in the post-response reconciliation path (the Object.assign applied after the backend RPC returns). It did not touch the earlier, purely local optimistic merge step, which has the identical shallow-merge gap one layer earlier. #1694 turned "field never becomes correct" into "field becomes correct only after a round trip" — this issue is about closing that remaining round-trip-gated gap.

ADR-049 (decisions/049-canonical-reactivity-store-pattern.md) independently documents that SharedNodeStore currently "fakes reactivity" and explicitly defers "separating the three hand-synced concerns (data, version/OCC, persistence coordination)" as future work this ADR makes possible but does not itself perform. This issue is a concrete instance of that deferred separation, not a reversal of ADR-049 — no companion nodespace-docs change is required, since ADR-049 already anticipates this class of follow-up work.

Proposed Solution

Make the optimistic merge in SharedNodeStore.updateNode() flatten known typed properties into their top-level fields synchronously, at write time, instead of waiting for the backend's flattened response. This likely means factoring out (or reusing, if one already exists client-side) a small flattening step equivalent to what node_to_typed_value/flatten_properties_for_api do server-side, and applying it to changes.properties during the optimistic merge before it's assigned into the local node.

Acceptance Criteria

Technical Specifications

Reference Files

  • shared-node-store.svelte.ts: packages/desktop-app/src/lib/services/shared-node-store.svelte.ts — optimistic merge (~lines 990-996), post-response reconciliation (~lines 1192-1211, ~1601-1640)
  • ai-chat-node.ts: packages/desktop-app/src/lib/types/ai-chat-node.ts — top-level typed fields (model, provider, messages) read by the viewer
  • ai-chat-node-viewer.svelte: packages/desktop-app/src/lib/components/viewers/ai-chat-node-viewer.svelte — handleModelSelect (144-200), textarea visibility gate (432), handleSend (259-268), persistedMessages (103-116)

Implementation Details

Investigate whether the server-side flattening logic (node_to_typed_value/flatten_properties_for_api) has a client-side equivalent already, or whether one needs to be introduced. Whatever flattening the optimistic path applies must stay consistent with what the backend produces, so the post-response reconciliation doesn't fight the optimistic value (avoid introducing a second, diverging flattening implementation).

Testing Requirements

Add coverage for SharedNodeStore.updateNode() asserting that a properties.*-shaped change is reflected in the corresponding top-level field synchronously, before any backend response is awaited (i.e., testable without mocking network timing).

Related Issues

Activity

  1. added
    bug
    on Jul 19, 2026
  2. mstomar125 commented on Jul 24, 2026

    @mstomar125
    Contributor

    ✅ Completed — merged in PR #1732

    The optimistic (pre-round-trip) updateNode merge now flattens typed properties.* into the top-level fields viewers read, so ai-chat model selection and sent messages appear immediately instead of lagging a full RPC round trip.

    Fix:

    • node-normalize.ts — OPTIMISTIC_TYPED_FIELDS (a minimal mirror of the backend node_to_typed_value promotion set, colocated + documented against convert.rs), deepMergeProperties (one-level + type-namespace deep merge), promoteTypedFields (promotes a field only if present in the write → no undefined-clobber).
    • shared-node-store.svelte.ts — the optimistic merge deep-merges properties and promotes typed fields to the top level.
    • update-protocol.ts — UpdateOptions.replaceProperties for callers that compute their own full bag.
    • reactive-node-service.svelte.ts (updateNodeProperties) + the two schema old-format migration paths (schema-field-update.ts, schema-property-form.svelte) pass replaceProperties: true.

    Why it's safe: the deep-merge affects only the local optimistic node — the persisted wire payload is still built from the raw changes.properties, so the backend stays the single flattening authority and its response reconciles the local state. Any drift between the map and convert.rs degrades optimistic latency only, never correctness.

    Verification:

    • New tests: node-normalize.test.ts (+8, helper unit tests incl. undefined-clobber + null-present + nested/flat) and shared-node-store.test.ts (+5, integration: flat ai-chat promotion, message-send preserves provider/model, deep-merge keeps siblings, nested task promotion, non-typed node untouched).
    • Caught + fixed a regression during implementation: updateNodeProperties(merge=false) expects wholesale replace — now flagged via replaceProperties.
    • Full frontend suite: 166 files, 4091 tests passing; svelte-check: 0 errors / 1768 files.
    • Independent adversarial review: CONFIRMED-GOOD (all updateNode({ properties }) callers audited; backend-echo convergence, hot-path short-circuit, and rollback all verified).
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

    bugfoundation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions