Repository navigation
Description
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) writesproperties.provider/properties.model. The textarea's visibility gate (ai-chat-node-viewer.svelte:432,isMessageProvider && !model) reads the top-levelnode.model, which stays stale until the RPC resolves.handleSend(ai-chat-node-viewer.svelte:259-268) writesproperties.messages. The rendered message list (persistedMessages,ai-chat-node-viewer.svelte:103-116) reads the top-levelnode.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
- Selecting a model on an ai-chat node makes the message textarea appear immediately (no visible delay pending an RPC round trip)
- Sending a message makes it appear immediately in the conversation view, before the agent's response arrives
-
SharedNodeStore.updateNode()'s optimistic merge flattensproperties.*changes into corresponding top-level fields for all typed node kinds that rely on top-level reads (not just ai-chat) - No regression in the post-response reconciliation path fixed by Content-only edits forced immediate (non-debounced) persistence via redundant nodeType, and property-only updates never synced typed fields back from the backend #1694/Content-only edits forced immediate (non-debounced) persistence via redundant nodeType, and property-only updates never synced typed fields back from the backend #1690 (backend-authoritative values still win once the RPC resolves)
- Tests pass (
bun run test:all) - Code passes
bun run quality:fix
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
- Content-only edits forced immediate (non-debounced) persistence via redundant nodeType, and property-only updates never synced typed fields back from the backend #1690 (prior fix to the post-response reconciliation path for this same class of bug)
Activity
mstomar125 commentedon Jul 24, 2026
✅ Completed — merged in PR #1732
The optimistic (pre-round-trip)
updateNodemerge now flattens typedproperties.*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 backendnode_to_typed_valuepromotion set, colocated + documented againstconvert.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-mergespropertiesand promotes typed fields to the top level.update-protocol.ts—UpdateOptions.replacePropertiesfor 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) passreplaceProperties: 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 andconvert.rsdegrades optimistic latency only, never correctness.Verification:
node-normalize.test.ts(+8, helper unit tests incl. undefined-clobber + null-present + nested/flat) andshared-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).updateNodeProperties(merge=false)expects wholesale replace — now flagged viareplaceProperties.svelte-check: 0 errors / 1768 files.updateNode({ properties })callers audited; backend-echo convergence, hot-path short-circuit, and rollback all verified).