Skip to content

ai-chat viewer gets stuck on Stop/typing-indicator after reply is fully persisted #1960

Closed

Description

@malibio

Symptom

An ai-chat conversation turn completes successfully end-to-end (backend persists the assistant's reply, daemon log shows a clean turn completion, database confirms the final message is correct) — but the UI hangs indefinitely showing the typing indicator and "Stop" button, never rendering the reply.

Confirmed root cause (via live browser console capture)

```
[SharedNodeStore] Database write failed for node bf2c1788-76ff-4d79-9c6f-7755d13b0c21:
fullError: {code: "VERSION_CONFLICT", details: "Aborted", message: "Version conflict on bf2c1788-76ff-4d79-9c6f-7755d13b0c21: expected 3, got 4"}
```

Two separate updateNode calls failed with this same VERSION_CONFLICT during one turn.

Mechanism (packages/desktop-app/src/lib/services/shared-node-store.svelte.ts)

  • previousVersion: this.versions.get(nodeId) is captured at write-queue time (~line 1015), not at actual server-send time.
  • The ai-chat viewer's handleSend() does an optimistic updateNode (user message + status: 'processing'). The daemon's LocalAgentService reacts to the NodeUpdated event and, after inference completes (~16s later in the observed repro: turn triggered 07:23:36.661274Z, turn complete 07:23:52.176217Z), writes its own update back to the same node (assistant reply + completedWrites + status: 'idle'). Two independent writers touching the same node's version.
  • "expected 3, got 4" means the frontend's local this.versions map was stale by the time its write was actually sent — some other write (most likely the backend's turn-completion write) had already bumped the server-side version past what the frontend's queued write still expected.
  • The catch block (~lines 1380-1438) does rollback + OCC resync: hydrates from occError.conflictData.current_node if present (~lines 1410-1420), else falls back to resyncNodeFromServer (~line 1423, fire-and-forget, errors only logged not surfaced).
  • handle.promise.catch (~line 1457) explicitly swallows VERSION_CONFLICT (if (isVersionConflict(err)) return;) — it relies entirely on the resync inside the write closure to fix the UI's state.

The likely bug: if the resync's current_node payload was captured server-side at the moment of the conflict (i.e. a still-in-flight-turn snapshot showing status: 'processing'), rather than the eventual final state (status: 'idle' + the completed reply), the UI resyncs to a snapshot that also still shows status: 'processing' — explaining exactly why the typing indicator/Stop button hangs forever even though the daemon completed the turn and the actual latest version of the node (confirmed via direct sqlite query) has the correct final message.

Likely systemic, not a one-off

Two conflicts fired in this single turn. Any ai-chat turn where the frontend queues a local write that races the daemon's own status-writing pattern (which happens on every turn — processing → idle) is exposed to this same race.

Fix directions to evaluate

  1. Have the OCC resync re-fetch from server after a short delay or on the next WatchNodes event, rather than trusting the conflict payload's current_node at face value — it may itself be a mid-turn snapshot.
  2. Derive isProcessing more robustly than node?.status === 'processing' alone — e.g. cross-check against whether a turn is actually still in flight (matching node_id in an active WatchNodes/local-agent stream), so a stale resynced snapshot mid-turn can't strand the UI.
  3. Avoid the frontend ever writing status: 'processing' as a separate write that can race the daemon's own status writes — let the daemon be the sole writer of status; frontend only appends the user message.

Acceptance criteria

  • Reproduce the version-conflict race reliably (send a message, observe console for VERSION_CONFLICT during the turn).
  • After a fix, the UI never remains stuck showing "processing" once the node's actual persisted state has moved past that.
  • Regression test covering an OCC conflict during an active ai-chat turn resolving to the correct final UI state.

Activity

  1. added
    bug
    on Aug 4, 2026
  2. malibio commented on Aug 4, 2026

    @malibio
    CollaboratorAuthor

    Baseline (worktree issue-1960-aichat-occ-stuck): Frontend: 179 test files passed, 4286 tests passed.

  3. self-assigned this
    on Aug 4, 2026
  4. malibio commented on Aug 4, 2026

    @malibio
    CollaboratorAuthor

    Follow-up worth tracking beyond PR #1971 (surfaced in review, deliberately not folded into the fix):

    The OCC hydration path calls nodesSet directly (shared-node-store.svelte.ts), bypassing setNode and therefore bypassing shouldSkipStaleAiChatUpdate. So conflict hydration and daemon broadcasts are two writers into the same store with different staleness policies, and there is no arbitration between them — an OCC hydration can install a node whose message count disagrees with a later-arriving broadcast in either direction.

    This is pre-existing, and PR #1971 strictly improves it: a hydrated ai-chat node now carries a real messages array instead of always counting 0, which previously made the guard skip every subsequent broadcast for that node. So it is not a regression and did not block the fix.

    Flagging it because it is the most plausible source of any residual 'stuck processing' report after #1971 merges — if one appears, this is the first place to look rather than the conflict payload shape, which is now pinned by tests at both the daemon and store layers.

  5. malibio commented on Aug 4, 2026

    @malibio
    CollaboratorAuthor

    Second follow-up from re-review of PR #1971 (also not folded into the fix):

    'ai-chat' status conflates two orthogonal state machines in one property key:

    • turn state, written by the daemon: processing (inference running) -> idle (complete)
    • session lifecycle, used by the PTY path: active / archived

    PR #1971 added 'idle' to the frontend AiChatStatus union because the daemon writes it on every turn completion and it was simply missing. That is correct as far as it goes, but it leaves a union whose members belong to two different axes, so 'is this chat archived?' and 'is a turn running?' are answered from the same field.

    Related: AiChatNodeBehavior::validate (packages/core/src/behaviors/mod.rs) rejects any status outside active|archived, which would reject every value the daemon actually writes. It is currently dead code — it reads top-level properties['status'] while all writes go to properties['ai-chat']['status'] — so it causes no failures today. It is a trap for whoever later 'fixes' the path without noticing the vocabulary mismatch.

    Not urgent and deliberately out of scope for the OCC fix, but the two should be resolved together: pick the vocabulary, then make the validator agree with it.

  6. malibio commented on Aug 4, 2026

    @malibio
    CollaboratorAuthor

    Update: the staleness-guard item from my earlier comment is FIXED in PR #1971 (commit 452d8f2), not deferred. shouldSkipStaleAiChatUpdate is now version-first with message count as a tiebreak, and the OCC hydration site runs the same guard, so both store writers share one policy.

    Investigating it surfaced that the original guard was itself unsafe, not just unapplied: message count alone cannot decide staleness, because a turn that rewrites history rather than appending (e.g. a cancelled turn dropping its partial reply) legitimately produces a newer snapshot with fewer messages, which count-only comparison would discard permanently.

    STILL OPEN, and worth a decision rather than a quiet fix — the dead validator:

    NodeBehavior::validate has NO production caller anywhere in the codebase. Every reference is a doctest or unit test; I checked core and daemon. So this is not 'the ai-chat validator is dead' — the whole validate() method on the behavior trait is dead, across all 29 implementations.

    That reframes the fix. 'Repair AiChatNodeBehavior::validate to accept idle/processing' would make one implementation correct while leaving 28 others unreachable, and would imply validation runs when it does not. The two honest options are:

    1. Delete validate() from the NodeBehavior trait and all 29 implementations. Matches the CLAUDE.md standard (delete unused code, don't suppress). Large diff, but purely subtractive.
    2. Wire validate() into the write path so it actually runs — then fix the ai-chat vocabulary as part of that, since it would start rejecting every status the daemon writes.

    Option 2 is a real behavior change with its own blast radius (28 other validators would begin running for the first time). I did not do either in #1971: both are decisions about the behavior architecture, not part of an OCC bug fix.

    Related and unchanged: 'status' still conflates a turn-state axis (processing/idle, daemon-written) with a PTY session-lifecycle axis (active/archived). Whichever option is chosen above, the vocabulary should be settled first, since the validator has to encode it.

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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions