Skip to content

fix(chat): show a new conversation's first answer, and save answers when the client leaves - #582

Merged
adeelahmad merged 3 commits into
mainfrom
claude/project-thread-jbfduq
Oct 18, 2026
Merged

adeelahmad merged 3 commits into
mainfrom
claude/project-thread-jbfduq

Conversation

@adeelahmad

Copy link
Copy Markdown
Owner

Requested by Adeel · project thread

Description

Before: asking the first question of a new conversation sometimes showed the question with "No answer was saved for this question. Ask it again to get one." even though the answer had been saved, until something made the page read the conversation again. Separately, closing the tab or losing the connection mid-answer dropped the answer for good.

After: the answer shows as soon as it is saved, and an answer is finished and saved even when the browser leaves before it ends; only Stop ends one early.

Cause (from the Clicks failures on #535, runs 39108195666 and 39108209701, reproduced locally in 1 of 7 runs of ci/clicks/run.sh --only …,first-question,chat-ask): a new conversation's page starts its first read of GET /chats/{id} while the answer is still streaming. When the stream ends, invalidateQueries doesn't read again: TanStack Query attaches to a first read still in flight (no data yet) instead of cancelling it (query.js: it only cancels when data !== undefined). That read was answered before the save, so the page shows the bare question and, with a 10 s stale time, nothing reads it again. The backend log shows exactly one GET /chats/{id} after the POST …/messages in each failure; a diagnostic on the backend generator confirmed it was never closed early, so the server did save the answer.

How:

  • refetchFresh(qc, key) (components/chat/data.ts) cancels a read under way, then invalidates, so the conversation is read after the save. Chat, the page assistant and a recording's Chat tab use it after an answer. docs/frontend.md notes the rule.
  • The answer stream (POST /chats/{cid}/messages) now runs on a thread of its own (_detached, the same pattern as /v1/chat/completions); the response only hands events over. A client that disconnects (Starlette cancels the response under uvicorn's ASGI 2.3) no longer abandons the generator before save(), and its cleanup (Answering.end, compaction, the title) runs on that thread rather than wherever the garbage collector closes it (the hazard behind fix(settings): test the LLM address and model on screen, not only the saved ones #539/feat(automations): look back and compare in running automations #542).

Motivation and Context

Flaky Clicks journeys (first-question, chat-ask) on several PRs; a real bug people hit with quick answers.

Steps to reproduce (if appropriate):

Loop ci/clicks/run.sh --only install,first-admin,setup-wizard,first-question,import-paste,chat-ask; before this change run 7 failed with the screenshot above. Tests reproduce both halves deterministically: chat-fresh-answer.test.tsx fails with invalidate-only, and test_an_answer_is_saved_when_the_client_leaves fails on main (the conversation keeps only the question).

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change; ! in the title)

Checklist:

  • The title is a Conventional Commit.
  • My code follows the code style of this project (ruff, mypy, eslint, prettier, tsc clean locally).
  • I added or updated tests.
  • My change requires documentation updates, and I have updated docs/ accordingly (assistant.md Chat, frontend.md Data, requirements.md).
  • Users need more than the title to understand it, so I wrote it under ## Unreleased in CHANGELOG.md.

Done questions (AGENTS.md)

  • Goal: delivers what the user wanted, merged on green CI (merging on green).
  • Agents: API and MCP clients get the same guarantee: an answer is saved even if they disconnect.
  • Zero setup: no new prerequisite.
  • Fewest clicks: removes the "Ask it again" step.
  • Configurable: n/a, no new setting.
  • Light: one short-lived thread per answer, as before (it ran on a threadpool thread).
  • Private and secure: n/a, no new surface.
  • Cost: an answer whose client left now finishes its model call instead of being thrown away; still in the ledger and budgets.
  • User decides: Stop still ends an answer early; defaults unchanged.
  • Upgrades: n/a, no stored-data change.
  • Honest: changelog says what changed.
  • Tests, docs and generated files updated (no API shape change, so no make openapi).
  • Open standards: n/a; no new dependencies.
  • Result reported with evidence; refine later: none.
  • Reuse: the thread-and-queue pattern of routes/completions.py; TanStack's own cancelQueries.

Generated by Claude Code

…hen the client leaves

The web app re-read a new conversation after its answer was saved with
invalidateQueries, which waits on a first read still under way (asked
before the save) instead of reading again, so the question showed with
"No answer was saved". refetchFresh drops that read and reads afresh;
Chat, the page assistant and a recording's Chat tab use it.

The answer stream now runs on a thread of its own, so a client that
leaves mid-answer no longer abandons it unsaved, and its cleanup never
runs on whichever thread collects the generator.

Also lists answering questions about Lens itself as built (as #578),
which the requirements check needs on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VGkdmhXwoXaBtU2n9nbdnt
@adeelahmad adeelahmad self-assigned this Oct 18, 2026
adeelahmad pushed a commit that referenced this pull request Oct 18, 2026
…rst load raced the stream"

This reverts commit 7f260ec: #582 carries
the same fix for main (refetchFresh in every chat panel, and the answer
written on a thread of its own), so this PR stays one concern.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ln25ykE6uBDqx21WBU94SX
@adeelahmad
adeelahmad merged commit a1f1931 into main Oct 18, 2026
11 checks passed
@adeelahmad
adeelahmad deleted the claude/project-thread-jbfduq branch October 18, 2026 11:03
adeelahmad added a commit that referenced this pull request Oct 18, 2026
* feat(timeline): emoji reactions and comments on activities

The timeline becomes a feed everyone in a namespace talks on. An opened item has a React button with a palette and shows everyone's reactions as chips; its comments open as threads with Reply, where Enter sends. A closed line sums its items' reactions and says how many comments it has. A comment on an asset's arrival is a comment on the asset, so the same thread shows on its page.

Each reaction is a reaction.added activity (noisy, by the person, with the activity as its object) and each comment a comment.added with the activity as its target, following the activity's audience, so automations and agents can act on them. Agents react and comment over MCP (react_to_activity, activity_comments, comment_on_activity) and through POST /activities/{id}/react and /activities/{id}/comments.

Reactions are one event_reaction row per person and activity, as marks are; comments reuse the comment table with an activity field. No model call is involved.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ln25ykE6uBDqx21WBU94SX

* docs(readme): list the assistant's answers about Lens itself as built

Release 0.15.0 carries #400, so the requirements check failed on main
with it still under In progress; it moves to the built list with its
checks.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ln25ykE6uBDqx21WBU94SX

* fix(chat): show a new conversation's first answer when its first load raced the stream

The page moves to /chat/{id} as a new conversation's first answer
streams. On a slow device that first load of the conversation is still
in flight when the answer ends, and the query cache folds the refetch
asked for then into the load, which predates the answer: the page showed
"No answer was saved for this question" until reloaded (the phone
clicks survey's chat-ask journey in CI). After an answer, when the
conversation read back lacks it, it is read once more.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ln25ykE6uBDqx21WBU94SX

* Revert "fix(chat): show a new conversation's first answer when its first load raced the stream"

This reverts commit 7f260ec: #582 carries
the same fix for main (refetchFresh in every chat panel, and the answer
written on a thread of its own), so this PR stays one concern.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ln25ykE6uBDqx21WBU94SX

* docs(changelog): move the reactions and comments entry to a changelog.d fragment, and name them in the README

The release commit folds Unreleased into the version section and #581 moved new entries to changelog.d/, so the
entry becomes changelog.d/timeline-social.md. The README's built line lost its reactions and comments clause in an
earlier merge; it is back.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ln25ykE6uBDqx21WBU94SX

---------

Co-authored-by: Adeel Ahmad <noreply@anthropic.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