Skip to content

refactor: the conversation runs in a controller outside React, split by concern; useConversation renders its store - #145

Merged
bencode merged 2 commits into
feat/conversation-compactionfrom
refactor/use-conversation-folder
Oct 13, 2026
Merged

bencode merged 2 commits into
feat/conversation-compactionfrom
refactor/use-conversation-folder

Conversation

@bencode

@bencode bencode commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #144. Merge that first; this PR then retargets to main.

What

features/conversation/use-conversation.ts (945 lines) was one hook. It held all of these, with state shared through 17 refs:

  • the draft and the queue
  • the agent's lifecycle
  • switching conversations
  • runs and compaction
  • model settings
  • titles
  • the history list

That orchestration now runs in a controller outside React, split by concern. useConversation creates the controller, renders its store and feeds it what lives in React (the draft attachments and the memory switch). Its return shape is unchanged, so the panel, composer and context are untouched.

File (features/conversation/use-conversation/) Holds Lines
index.ts the hook 96
controller.ts createConversationController: builds the store, runtime and draft queue; the public API 54
state.ts ConversationState (rendered, a zustand vanilla store, as in core/agent/background.ts), Runtime (the former refs), Ctx 94
drafts.ts the draft, the queue, and draft saving 78
agent-session.ts refresh, persist, attach and dispose the agent 157
navigation.ts the initial load, switch, new and delete 178
run.ts send, run, stop, compact 134
settings.ts model, web search, providers, memory 59
titles.ts renaming, and naming from the first reply 49
history.ts the paged list, and which conversations are stored 78
messages.ts, session.ts, draft-queue.ts pure helpers 191

Each module exports plain (ctx, …) functions, and the imports run one way: history ← drafts ← titles / agent-session ← navigation, run, settings ← controller ← hook. Function bodies, messages and ordering are carried over. Reading activeRef.current became store.getState().active, which is just as synchronous.

Three things differ, none of them visibly:

  • The model config is in the store and reactive. It was a ref that rendered only as a side effect of other updates.
  • configureModel reads the current phase rather than the one closed over at render.
  • tools is taken once, when the controller is created. useWorkspaceTools memoizes it on stable inputs only, so it never changed in practice.

Tests

Rendering from a zustand store (useSyncExternalStore) happens sooner than batched useState updates did. Two integration tests waited for one signal and then asserted a second, asynchronous one synchronously:

  • the history entry after an IndexedDB write
  • the tab after the agent moved a file

Those follow-up assertions now wait (findByRole / waitFor). Each test passes 10 of 10 in isolation, and pnpm check passes twice in a row.

End to end (local server, agent-browser)

  • Sending a message, and queuing a second one while a reply runs
  • Stopping a reply
  • A draft, and a chat attachment, surviving a reload
  • Sending with an attachment: the answer quotes the file
  • Starting a new conversation, renaming an inactive one, switching, deleting the active one
  • Turning memory on: the open conversation is reattached with the memory tools
  • Building a long conversation, then /compact (40.5K to 21.1K): the summary and usage survive a reload

Turning memory off is not distinguished by this check: the model repeated its earlier answer. That path is the same memoryChanged code as turning it on.

…ssion helpers as pure modules, the history list and the draft save queue on their own
…by concern into drafts, agent session, navigation, run, settings, titles and history; useConversation only renders its store
@bencode bencode changed the title refactor: use-conversation becomes a folder of pure modules, the history list and the draft queue refactor: the conversation runs in a controller outside React, split by concern; useConversation renders its store Oct 13, 2026
@bencode
bencode merged commit 9d5c73f into feat/conversation-compaction Oct 13, 2026
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.

1 participant