Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Thread header says "Working" until the reply lands (25a) (#752)

Oleksandr Ivanchenko0a8012ae66e3
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 6afb6326..6a450dbc 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -124,6 +124,7 @@ import {
streamTarget,
waitsForOtherBot,
threadBadge,
+ writingBadge,
threadsApi,
turnsIn,
upsertThread,
@@ -4046,7 +4047,9 @@ export const SpaceApp: React.FC<SpaceAppProps> = ({
openThreadId,
turns,
awaiting: awaitingTurns,
- directBadge: threadBadge({ id: currentKwant.id, delegation: null }, turns, awaitingTurns),
+ // 25a: the open chat's badge stays "Working" while its live card is up, so it goes idle with the body
+ directBadge: writingBadge(threadBadge({ id: currentKwant.id, delegation: null }, turns, awaitingTurns), !openThreadId && currentKwant.id in live),
+ openWriting: isCurrentContextStreaming,
showArchived,
onToggleArchived: () => setShowArchived((v) => !v),
onOpenDirect: openDirectChat,
@@ -4062,10 +4065,15 @@ export const SpaceApp: React.FC<SpaceAppProps> = ({
{shownThread && (
<ThreadHeader
thread={shownThread}
- badge={threadBadge(
- { id: shownThread.id, delegation: shownThread.delegation ? delegations[shownThread.delegation.id] ?? shownThread.delegation : null },
- turns,
- awaitingTurns,
+ // 25a: "Working" and its Stop stay up while the body still shows the live card; header, body and
+ // composer go idle in the one commit that drops it (clearLive), with the saved reply
+ badge={writingBadge(
+ threadBadge(
+ { id: shownThread.id, delegation: shownThread.delegation ? delegations[shownThread.delegation.id] ?? shownThread.delegation : null },
+ turns,
+ awaitingTurns,
+ ),
+ shownThread.id in live,
)}
delegatedBy={kwantName(shownThread.delegation?.lead_kwant_id ?? shownThread.created_by)}
onBack={openDirectChat}
diff --git a/frontend/src/components/ThreadBar.tsx b/frontend/src/components/ThreadBar.tsx
index 1179a598..18acf25e 100644
--- a/frontend/src/components/ThreadBar.tsx
+++ b/frontend/src/components/ThreadBar.tsx
@@ -1,7 +1,7 @@
import React, { useEffect, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import { Archive, ArchiveRestore, ArrowLeft, ArrowRightLeft, Check, ChevronDown, HelpCircle, Loader2, MessageSquare, Plus, RotateCw, Square } from 'lucide-react';
-import { Badge, BadgeKind, Thread, TurnMap, sortThreads, threadBadge } from '../threads';
+import { Badge, BadgeKind, Thread, TurnMap, sortThreads, threadBadge, writingBadge } from '../threads';
const BADGE_STYLE: Record<BadgeKind, string> = {
working: 'text-sky-300 bg-sky-500/10 border-sky-500/30',
@@ -35,6 +35,8 @@ export interface ThreadBarProps {
awaiting?: ReadonlySet<string>;
/** Badge for the direct chat itself (working / queued). */
directBadge: Badge | null;
+ /** 25a: the open conversation's reply is still being written (its live card is up), so it reads "Working". */
+ openWriting?: boolean;
showArchived: boolean;
onToggleArchived: () => void;
onOpenDirect: () => void;
@@ -60,6 +62,7 @@ export const ThreadBar: React.FC<ThreadBarProps> = ({
turns,
awaiting,
directBadge,
+ openWriting,
showArchived,
onToggleArchived,
onOpenDirect,
@@ -117,8 +120,8 @@ export const ThreadBar: React.FC<ThreadBarProps> = ({
)}
{visible.map((t) => {
- const badge = threadBadge(t, turns, awaiting);
const isOpen = openThreadId === t.id;
+ const badge = isOpen ? writingBadge(threadBadge(t, turns, awaiting), !!openWriting) : threadBadge(t, turns, awaiting);
const delegated = t.kind === 'delegation';
return (
<li key={t.id}>
@@ -210,6 +213,7 @@ export const ThreadPicker: React.FC<ThreadBarProps> = ({
turns,
awaiting,
directBadge,
+ openWriting,
showArchived,
onToggleArchived,
onOpenDirect,
@@ -226,7 +230,7 @@ export const ThreadPicker: React.FC<ThreadBarProps> = ({
const archivedCount = threads?.filter((t) => t.archived).length ?? 0;
const current = openThreadId ? threads?.find((t) => t.id === openThreadId) : undefined;
const label = openThreadId ? current?.title ?? 'Conversation' : 'Direct chat';
- const badge = current ? threadBadge(current, turns, awaiting) : openThreadId ? null : directBadge;
+ const badge = writingBadge(current ? threadBadge(current, turns, awaiting) : openThreadId ? null : directBadge, !!openWriting);
const dot = statusDot(badge);
const close = () => {
@@ -335,8 +339,8 @@ export const ThreadPicker: React.FC<ThreadBarProps> = ({
</li>
)}
{visible.map((t) => {
- const b = threadBadge(t, turns, awaiting);
const isOpen = openThreadId === t.id;
+ const b = isOpen ? writingBadge(threadBadge(t, turns, awaiting), !!openWriting) : threadBadge(t, turns, awaiting);
const delegated = t.kind === 'delegation';
return (
<li key={t.id}>
diff --git a/frontend/src/threads.ts b/frontend/src/threads.ts
index dd496014..73425440 100644
--- a/frontend/src/threads.ts
+++ b/frontend/src/threads.ts
@@ -333,6 +333,18 @@ export function threadBadge(thread: Pick<Thread, 'id' | 'delegation'>, turns: Tu
return FINAL_LABEL[st] ?? null;
}
+/**
+ * 25a: the open chat's badge while its reply is still being written (its id is in App's `live`). The turn's
+ * `turn_ended` can land a fetch before the saved reply, and the header must not say "done" while the body
+ * still says "writing", so a missing or final badge reads "Working" until the live card goes. "Waiting for
+ * you" and "Queued" stay as they are. Only for the conversation on screen: other rows keep `threadBadge`.
+ */
+export function writingBadge(badge: Badge | null, writing: boolean): Badge | null {
+ if (!writing) return badge;
+ if (badge && (badge.kind === 'working' || badge.kind === 'waiting' || badge.kind === 'queued')) return badge;
+ return { kind: 'working', label: 'Working' };
+}
+
export const isOpenDelegation = (d: Pick<Delegation, 'status'> | null | undefined): boolean =>
!!d && (d.status === 'queued' || d.status === 'running');
diff --git a/frontend/tests/headerIdleWithReply.test.mjs b/frontend/tests/headerIdleWithReply.test.mjs
new file mode 100644
index 00000000..c528a177
--- /dev/null
+++ b/frontend/tests/headerIdleWithReply.test.mjs
@@ -0,0 +1,92 @@
+// 25a: the thread header says "Working" (with its Stop) until the reply lands. Mira's design check of #737 saw the
+// header drop "Working" and Stop ~350 ms before the body's live card and the composer Stop went (turn_ended removes the
+// turn a message fetch before clearLive). The open chat's badge now follows `live` too, so all three go idle together.
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { readFileSync } from 'node:fs';
+import { loadBundled } from './bundle.mjs';
+
+const m = await loadBundled(`
+ import React from 'react';
+ import { renderToStaticMarkup } from 'react-dom/server.browser';
+ import { ThreadHeader, ThreadBar, ThreadPicker } from './src/components/ThreadBar';
+ export * from './src/threads';
+ const noop = async () => {};
+ export const header = (thread, badge) =>
+ renderToStaticMarkup(React.createElement(ThreadHeader, { thread, badge, onBack: noop, onStop: noop, onArchive: noop }));
+ const props = (extra) => ({ kwantName: 'TechLead', error: null, showArchived: false, onToggleArchived: noop, onOpenDirect: noop,
+ onOpenThread: noop, onNewThread: noop, onRetry: noop, ...extra });
+ export const bar = (extra) => renderToStaticMarkup(React.createElement(ThreadBar, props(extra)));
+ export const picker = (extra) => renderToStaticMarkup(React.createElement(ThreadPicker, props(extra)));
+`);
+
+const WORKING = { kind: 'working', label: 'Working' };
+const thread = (id, extra = {}) => ({ id, kwant_id: 'k', kind: 'user', title: `Thread ${id}`, archived: false, delegation: null, created_at: '2026-11-16T12:00:00Z', updated_at: '2026-11-16T12:00:00Z', ...extra });
+const delegation = (status) => ({ id: 'd1', status, lead_kwant_id: 'lead', specialist_kwant_id: 'k', title: 'Task' });
+const buttons = (html) => [...html.matchAll(/<button[^>]*>(.*?)<\/button>/g)].map((b) => b[1].replace(/<[^>]+>/g, '').trim());
+
+test('writingBadge: writing with no badge, or a final one, reads Working', () => {
+ assert.deepEqual(m.writingBadge(null, true), WORKING);
+ for (const kind of ['done', 'failed', 'cancelled', 'interrupted']) {
+ assert.deepEqual(m.writingBadge({ kind, label: kind }, true), WORKING, kind);
+ }
+});
+
+test('writingBadge: Waiting for you, Queued and Working stay as they are', () => {
+ const waiting = { kind: 'waiting', label: 'Waiting for you' };
+ const queued = { kind: 'queued', label: 'Queued · #2' };
+ assert.equal(m.writingBadge(waiting, true), waiting);
+ assert.equal(m.writingBadge(queued, true), queued);
+ assert.equal(m.writingBadge(WORKING, true), WORKING);
+});
+
+test('writingBadge: not writing leaves every badge unchanged', () => {
+ assert.equal(m.writingBadge(null, false), null);
+ for (const b of [WORKING, { kind: 'done', label: 'Done' }, { kind: 'waiting', label: 'Waiting for you' }, { kind: 'queued', label: 'Queued' }]) {
+ assert.equal(m.writingBadge(b, false), b);
+ }
+});
+
+test('the header keeps Working and Stop while the open thread is live and its turn is already gone', () => {
+ const th = thread('th');
+ const noTurns = {}; // turn_ended removed the turn; the saved reply isn't on screen yet
+ const plain = m.threadBadge(th, noTurns);
+ assert.equal(plain, null, 'threadBadge alone: idle (the old header)');
+ assert.ok(!buttons(m.header(th, plain)).includes('Stop'), 'before: no Stop in the window');
+ const html = m.header(th, m.writingBadge(plain, true));
+ assert.match(html, />Working</);
+ assert.ok(buttons(html).includes('Stop'), 'after: Stop stays with the live card');
+ // a delegation already marked Done in the same window still reads Working until the body goes idle
+ const done = thread('th', { kind: 'delegation', delegation: delegation('completed') });
+ assert.ok(buttons(m.header(done, m.writingBadge(m.threadBadge(done, noTurns), true))).includes('Stop'));
+ // once clearLive drops the live card the header goes idle with it
+ assert.ok(!buttons(m.header(th, m.writingBadge(plain, false))).includes('Stop'));
+ assert.match(m.header(done, m.writingBadge(m.threadBadge(done, noTurns), false)), />Done</);
+});
+
+test('ThreadBar: only the open conversation follows openWriting; other rows keep threadBadge', () => {
+ const threads = [thread('a'), thread('b')];
+ const html = m.bar({ threads, openThreadId: 'a', turns: {}, directBadge: null, openWriting: true });
+ assert.equal((html.match(/>Working</g) ?? []).length, 1, 'just the open row');
+ assert.ok(html.indexOf('Working') > html.indexOf('Thread a') && html.indexOf('Working') < html.indexOf('Thread b'));
+ assert.doesNotMatch(m.bar({ threads, openThreadId: 'a', turns: {}, directBadge: null, openWriting: false }), />Working</);
+});
+
+test('ThreadPicker (phone): the open conversation\'s dot reads Working while its reply is written', () => {
+ const threads = [thread('a')];
+ const idle = m.picker({ threads, openThreadId: 'a', turns: {}, directBadge: null, openWriting: false });
+ const writing = m.picker({ threads, openThreadId: 'a', turns: {}, directBadge: null, openWriting: true });
+ assert.notEqual(idle, writing);
+ assert.match(writing, /Working/);
+ assert.doesNotMatch(idle, /Working/);
+});
+
+test('App feeds the open chat\'s live state to the header, the direct chat badge and the bar', () => {
+ const src = readFileSync(new URL('../src/App.tsx', import.meta.url), 'utf8');
+ const head = src.slice(src.indexOf('<ThreadHeader'), src.indexOf('delegatedBy=', src.indexOf('<ThreadHeader')));
+ assert.match(head, /badge=\{writingBadge\(/);
+ assert.match(head, /shownThread\.id in live,?\s*\)\}/);
+ assert.match(src, /directBadge: writingBadge\(threadBadge\(\{ id: currentKwant\.id, delegation: null \}, turns, awaitingTurns\), !openThreadId && currentKwant\.id in live\)/);
+ assert.match(src, /openWriting: isCurrentContextStreaming,/);
+ assert.match(src, /const isCurrentContextStreaming = openCtx in live;/);
+});
diff --git a/frontend/tests/turnEndNoStaleFrame.test.mjs b/frontend/tests/turnEndNoStaleFrame.test.mjs
index e3a88934..1961003a 100644
--- a/frontend/tests/turnEndNoStaleFrame.test.mjs
+++ b/frontend/tests/turnEndNoStaleFrame.test.mjs
@@ -33,7 +33,7 @@ function scene({ answered = new Set(['ask']) } = {}) {
let screen = [{ ...USER }, { ...ASK }];
let live = true; // the 👍 started the turn
let stream = REPLY.text; // the working line has streamed the reply
- let turns = { 'turn-1': { turn_id: 'turn-1', context_id: 'c' } };
+ let turns = { 'turn-1': { turn_id: 'turn-1', context_id: 'c', state: 'running', seq: 1 } };
const server = [{ ...USER }, { ...ASK }];
const fetches = [];
const frames = [];
@@ -44,7 +44,10 @@ function scene({ answered = new Set(['ask']) } = {}) {
const saved = screen.some((m) => m.id === 'pub');
const streamed = live && stream === REPLY.text;
const replyShown = saved || streamed;
- frames.push({ why, ids: screen.map((m) => m.id), live, stream, replyShown, twice: saved && streamed, approve: a.approveRowId(last, null, { busy: live, answered }) });
+ // 25a: the header's badge before (turns only) and now (the open chat's live card too)
+ const oldHeader = t.threadBadge({ id: 'c', delegation: null }, turns)?.kind ?? null;
+ const header = t.writingBadge(t.threadBadge({ id: 'c', delegation: null }, turns), live)?.kind ?? null;
+ frames.push({ why, ids: screen.map((m) => m.id), live, stream, replyShown, twice: saved && streamed, approve: a.approveRowId(last, null, { busy: live, answered }), oldHeader, header });
};
const refreshContext = () =>
loads.run('c', async (isLatest) => {
@@ -205,3 +208,27 @@ test('App loads the chat through ChatLoads.run, so every settle after refreshCon
const tc = src.slice(src.indexOf("case 'TurnComplete': {"), src.indexOf("case 'browser_navigated':"));
assert.match(tc, /settlesOnTurnComplete\(turnsRef\.current, event\.turn_id\) \|\| landedNewReply\(shown, /);
});
+
+test('25a: through the turn end the header says Working exactly while the body still shows the live card', async () => {
+ const s = scene();
+ const done1 = s.turnComplete();
+ s.server.push({ ...REPLY });
+ const done2 = s.turnEnded(); // the turn is gone from `turns` now; the newest load is still out
+ await s.respond(0);
+ await s.respond(1);
+ await Promise.all([done1, done2]);
+ // the old header went idle a fetch before the body (Mira's header-before-body-390.png)
+ assert.ok(s.frames.some((f) => f.live && f.oldHeader === null), 'the scene reproduces the gap');
+ for (const f of s.frames) assert.equal(f.header === 'working', f.live, `header vs body at "${f.why}" (live ${f.live}, header ${f.header})`);
+ assert.equal(s.frames.at(-1).header, null, 'and idle together once the reply is in');
+});
+
+test('22r N1 (Rigel): clearLive commits the live map with flushSync before it clears the streamed words', async () => {
+ const src = await readFile(new URL('../src/App.tsx', import.meta.url), 'utf8');
+ const body = src.slice(src.indexOf('const clearLive = '), src.indexOf('const kwantName = ', src.indexOf('const clearLive = ')));
+ const flush = body.indexOf('flushSync(() => setLive(next))');
+ const show = body.indexOf('streamStore.show(');
+ assert.ok(flush >= 0, 'setLive(next) goes through flushSync');
+ assert.ok(show > flush, 'flushSync(() => setLive(next)) comes before streamStore.show');
+ assert.doesNotMatch(body.replace('flushSync(() => setLive(next))', ''), /setLive\(/, 'no plain setLive besides it');
+});