From 00b030cafe4f361593bfe7a2a837d3c76d448155 Mon Sep 17 00:00:00 2001 From: Mihaela Stoica Date: Mon, 23 Nov 2026 11:25:38 +0000 Subject: [PATCH 1/3] fix(chat): lock the composer input while the empty placeholder thread is shown Until an opened chat's thread runtime attaches, assistant-ui shows its empty placeholder thread, whose composer throws on every edit. Typing then sent each keystroke into it from SyncPlugin's Lexical listener, so the empty-thread error reached the renderer unhandled and the text was lost. The window lasts seconds when the Kilo server is still starting. The Lexical editor is now read-only while the placeholder is shown. Only the placeholder has no extras: every real thread, a draft included, carries its message queue controls. --- plugins/chat/src/test/fake-kilo-server.ts | 4 +++ .../web/composer/composer.browser.test.tsx | 24 +++++++++++++ .../composer/kilo-lexical-composer-input.tsx | 34 ++++++++++++++++++- .../chat/src/web/runtime/use-chat-runtime.ts | 2 ++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/plugins/chat/src/test/fake-kilo-server.ts b/plugins/chat/src/test/fake-kilo-server.ts index 2ac7b4d1..2054652c 100644 --- a/plugins/chat/src/test/fake-kilo-server.ts +++ b/plugins/chat/src/test/fake-kilo-server.ts @@ -331,6 +331,7 @@ export class FakeKiloServer { const chat = this.chats.get(decodeURIComponent(id)); if (!chat) return json({ name: "NotFoundError", data: { message: "No such chat" } }, 404); if (!action && request.method === "GET") { + await this.chatReadGate; if (this.chatReadDelayMs) await new Promise((resolve) => setTimeout(resolve, this.chatReadDelayMs)); return json(this.#info(chat)); @@ -412,6 +413,9 @@ export class FakeKiloServer { /** Milliseconds a chat's details take to arrive, as from a busy server. */ chatReadDelayMs = 0; + /** Holds answering `/session/:id` (a chat's details) until the test releases it. */ + chatReadGate?: Promise; + /** Milliseconds a chat's messages take to arrive, as a long history does. */ historyReadDelayMs = 0; diff --git a/plugins/chat/src/web/composer/composer.browser.test.tsx b/plugins/chat/src/web/composer/composer.browser.test.tsx index 1ff841f1..3679bd20 100644 --- a/plugins/chat/src/web/composer/composer.browser.test.tsx +++ b/plugins/chat/src/web/composer/composer.browser.test.tsx @@ -751,3 +751,27 @@ describe("a kept work session's composer", () => { await expect.element(shownInput()).toHaveTextContent("After"); }); }); + +describe("the composer of a chat still opening", () => { + it("takes no typing until the chat's thread is ready, then takes it", async () => { + const server = new FakeKiloServer(); + server.addChat({ id: "ses_a", directory: "/projects/app", title: "Chat" }); + let release!: () => void; + server.chatReadGate = new Promise((resolve) => { + release = resolve; + }); + await renderChat({ + server, + pane: agentPane({ object: chatPaneObject("ses_a") }), + chats: [chatSummary("ses_a")], + }); + await expect.poll(() => server.requestsTo("GET", "/session/ses_a").length).toBeGreaterThan(0); + + await expect.element(input()).toHaveAttribute("contenteditable", "false"); + + release(); + await expect.element(input()).toHaveAttribute("contenteditable", "true"); + await userEvent.type(input(), "Hello"); + await expect.element(input()).toHaveTextContent("Hello"); + }); +}); diff --git a/plugins/chat/src/web/composer/kilo-lexical-composer-input.tsx b/plugins/chat/src/web/composer/kilo-lexical-composer-input.tsx index c4537562..d30113b2 100644 --- a/plugins/chat/src/web/composer/kilo-lexical-composer-input.tsx +++ b/plugins/chat/src/web/composer/kilo-lexical-composer-input.tsx @@ -10,7 +10,7 @@ import { KEY_ENTER_COMMAND, PASTE_COMMAND, } from "lexical"; -import type { Unstable_DirectiveFormatter } from "@assistant-ui/react"; +import type { AssistantState, Unstable_DirectiveFormatter } from "@assistant-ui/react"; import { type CSSProperties, type FC, @@ -80,6 +80,7 @@ export const KiloLexicalComposerInput: FC = ({ style={style} > + @@ -131,6 +132,37 @@ function LexicalInputAria({ label }: { label: string }) { return null; } +/** + * Whether the runtime shows assistant-ui's empty placeholder thread instead of + * a real one. Only the placeholder has no extras: every real thread, a draft + * included, carries its message queue controls, given by the `messageQueue` + * with `userControls` that use-chat-runtime passes react-opencode. + */ +const showsPlaceholderThread = (s: AssistantState) => s.thread.extras === undefined; + +/** + * Keeps the editor read-only while the runtime shows assistant-ui's empty + * placeholder thread, as it does until an opened chat's thread runtime is + * attached (seconds on a server still starting). The placeholder's composer + * throws on every edit, and SyncPlugin pushes each keystroke into it from a + * Lexical listener, where nothing catches the error. + * + * LexicalComposerInput sets the editor's editable state from its own rule + * (thread disabled, dictation blocking input) first, so this repeats that rule + * and has the last word; recheck it when upgrading @assistant-ui/react-lexical. + */ +function PlaceholderThreadLockPlugin() { + const [editor] = useLexicalComposerContext(); + const editable = useAuiState( + (s) => + !showsPlaceholderThread(s) && !s.thread.isDisabled && !s.composer.dictation?.inputDisabled, + ); + useEffect(() => { + editor.setEditable(editable); + }, [editor, editable]); + return null; +} + // LexicalComposerInput has no equivalent of ComposerPrimitive.Input's // addAttachmentOnPaste, so pasted files must be intercepted here. Runs at HIGH // priority, ahead of PlainTextPlugin's EDITOR-priority text insertion; returning diff --git a/plugins/chat/src/web/runtime/use-chat-runtime.ts b/plugins/chat/src/web/runtime/use-chat-runtime.ts index 84096cd0..d7a68b8f 100644 --- a/plugins/chat/src/web/runtime/use-chat-runtime.ts +++ b/plugins/chat/src/web/runtime/use-chat-runtime.ts @@ -158,6 +158,8 @@ export function useChatRuntime(options: ChatRuntimeOptions): AssistantRuntime { }, messageQueue: { cancelBehavior: "pause", + // Also gives every real thread extras, which the composer input tells + // assistant-ui's empty placeholder thread apart by. userControls: true, create: createQueue, }, From f7789ce1cd7f9082bc200947f2e16a87890366d4 Mon Sep 17 00:00:00 2001 From: Mihaela Stoica Date: Mon, 23 Nov 2026 11:28:13 +0000 Subject: [PATCH 2/3] =?UTF-8?q?feat(chat):=20say=20"Loading=20chat?= =?UTF-8?q?=E2=80=A6"=20in=20the=20composer=20when=20a=20chat=20is=20slow?= =?UTF-8?q?=20to=20open?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While the empty placeholder thread is shown the composer input is locked, which on a slow open left it ignoring clicks with no explanation. Its placeholder now reads "Loading chat…" (and the input is aria-busy) once the placeholder thread has been shown for 400 ms. Most chats replace it within a frame, where an immediate swap would only flash. --- .../web/composer/composer.browser.test.tsx | 36 ++++++++++++++++++- .../composer/kilo-lexical-composer-input.tsx | 22 +++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/plugins/chat/src/web/composer/composer.browser.test.tsx b/plugins/chat/src/web/composer/composer.browser.test.tsx index 3679bd20..95a4dfb4 100644 --- a/plugins/chat/src/web/composer/composer.browser.test.tsx +++ b/plugins/chat/src/web/composer/composer.browser.test.tsx @@ -753,7 +753,8 @@ describe("a kept work session's composer", () => { }); describe("the composer of a chat still opening", () => { - it("takes no typing until the chat's thread is ready, then takes it", async () => { + /** Opens chat "ses_a" in the pane, its details held until `release` is called. */ + async function openHeldChat() { const server = new FakeKiloServer(); server.addChat({ id: "ses_a", directory: "/projects/app", title: "Chat" }); let release!: () => void; @@ -766,6 +767,11 @@ describe("the composer of a chat still opening", () => { chats: [chatSummary("ses_a")], }); await expect.poll(() => server.requestsTo("GET", "/session/ses_a").length).toBeGreaterThan(0); + return release; + } + + it("takes no typing until the chat's thread is ready, then takes it", async () => { + const release = await openHeldChat(); await expect.element(input()).toHaveAttribute("contenteditable", "false"); @@ -774,4 +780,32 @@ describe("the composer of a chat still opening", () => { await userEvent.type(input(), "Hello"); await expect.element(input()).toHaveTextContent("Hello"); }); + + it("says the chat is loading while it's slow to open", async () => { + const release = await openHeldChat(); + + await expect.element(page.getByText("Loading chat…")).toBeVisible(); + + release(); + await expect.element(page.getByText("Loading chat…")).not.toBeInTheDocument(); + }); + + it("doesn't flash the loading text for a chat that opens at once", async () => { + let flashed = false; + const watcher = new MutationObserver(() => { + if (document.body.textContent?.includes("Loading chat…")) flashed = true; + }); + watcher.observe(document.body, { childList: true, subtree: true, characterData: true }); + const server = new FakeKiloServer(); + server.addChat({ id: "ses_a", directory: "/projects/app", title: "Chat" }); + await renderChat({ + server, + pane: agentPane({ object: chatPaneObject("ses_a") }), + chats: [chatSummary("ses_a")], + }); + + await expect.element(input()).toHaveAttribute("contenteditable", "true"); + watcher.disconnect(); + expect(flashed).toBe(false); + }); }); diff --git a/plugins/chat/src/web/composer/kilo-lexical-composer-input.tsx b/plugins/chat/src/web/composer/kilo-lexical-composer-input.tsx index d30113b2..876352e3 100644 --- a/plugins/chat/src/web/composer/kilo-lexical-composer-input.tsx +++ b/plugins/chat/src/web/composer/kilo-lexical-composer-input.tsx @@ -56,6 +56,7 @@ export const KiloLexicalComposerInput: FC = ({ style, }) => { const editorGeneration = useEditorGeneration(); + const slowToOpen = useSlowToOpen(); // Cached on `ref`, so a re-render doesn't detach and re-attach it (and set the label again). const attachInput = (node: HTMLDivElement | null) => { if (typeof ref === "function") ref(node); @@ -68,10 +69,11 @@ export const KiloLexicalComposerInput: FC = ({ ref={attachInput} formatter={formatter} directiveChip={KiloDirectiveChip} - placeholder={placeholder} + placeholder={slowToOpen ? "Loading chat…" : placeholder} cancelOnEscape={cancelOnEscape} submitMode={submitMode} aria-label="Message input" + aria-busy={slowToOpen || undefined} onKeyDown={onKeyDown} className={cn( "aui-composer-input caret-foreground max-h-32 w-full bg-transparent px-1 pt-0 pb-1 text-sm", @@ -140,6 +142,24 @@ function LexicalInputAria({ label }: { label: string }) { */ const showsPlaceholderThread = (s: AssistantState) => s.thread.extras === undefined; +const SLOW_TO_OPEN_DELAY_MS = 400; + +/** + * Whether the placeholder thread has been shown long enough to say so. Most + * chats replace it within a frame, where a "Loading chat…" would only flash. + */ +function useSlowToOpen(): boolean { + const opening = useAuiState(showsPlaceholderThread); + const [slow, setSlow] = useState(false); + if (!opening && slow) setSlow(false); + useEffect(() => { + if (!opening || slow) return; + const timeoutId = window.setTimeout(() => setSlow(true), SLOW_TO_OPEN_DELAY_MS); + return () => window.clearTimeout(timeoutId); + }, [opening, slow]); + return opening && slow; +} + /** * Keeps the editor read-only while the runtime shows assistant-ui's empty * placeholder thread, as it does until an opened chat's thread runtime is From ca6163cdf889a240eb1dbbb506fe316de511fb23 Mon Sep 17 00:00:00 2001 From: Mihaela Stoica Date: Mon, 23 Nov 2026 13:06:31 +0000 Subject: [PATCH 3/3] test(chat): disconnect the loading-flash observer even when the test fails --- plugins/chat/src/web/composer/composer.browser.test.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/chat/src/web/composer/composer.browser.test.tsx b/plugins/chat/src/web/composer/composer.browser.test.tsx index 95a4dfb4..0ca1ee59 100644 --- a/plugins/chat/src/web/composer/composer.browser.test.tsx +++ b/plugins/chat/src/web/composer/composer.browser.test.tsx @@ -1,6 +1,6 @@ import { FileTextIcon, GitBranchIcon, SparklesIcon } from "lucide-react"; import { useState } from "react"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, onTestFinished, vi } from "vitest"; import { page, userEvent } from "vitest/browser"; import { KernelCommand, type CommandHandler, type PluginEntry } from "@kilo-desktop/kernel"; @@ -796,6 +796,7 @@ describe("the composer of a chat still opening", () => { if (document.body.textContent?.includes("Loading chat…")) flashed = true; }); watcher.observe(document.body, { childList: true, subtree: true, characterData: true }); + onTestFinished(() => watcher.disconnect()); const server = new FakeKiloServer(); server.addChat({ id: "ses_a", directory: "/projects/app", title: "Chat" }); await renderChat({