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.

Chat: keep a waiting approval card's buttons above the composer

Pixel (kwant bot)b43111c9d398
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3fabd55e..5c19edbb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -118,6 +118,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
and its spend doesn't change. Before, the check was only approved and never ran.
### Fixed
+- **A waiting approval card's Deny / Allow buttons no longer hide under the message box.** When a
+ card's details arrived a moment after it appeared, or the message box grew, the chat didn't scroll
+ with it, so on a phone the buttons sat below the box until you scrolled. While you follow the chat it
+ now stays pinned to the bottom. If you've scrolled up to read, it stays where you are and shows an
+ "Approval waiting" button above the message box that takes you to the card.
- **Mission board: "To fix" no longer repeats the failure's name.** A usage-limit failure read "Usage
limit reached" on its badge and again at the start of "To fix"; "To fix" now starts with what to do.
- **Mission board: AutoFix hides passwords without hiding command names.** A check's command name or a
diff --git a/frontend/src/approvalVisibility.ts b/frontend/src/approvalVisibility.ts
new file mode 100644
index 00000000..068486c3
--- /dev/null
+++ b/frontend/src/approvalVisibility.ts
@@ -0,0 +1,36 @@
+// Keeps a waiting approval card's Deny / Allow buttons on screen above the composer.
+
+/** How close to the bottom (px) still counts as "following the chat". */
+export const STICK_THRESHOLD = 80;
+
+export const isNearBottom = (el: { scrollHeight: number; scrollTop: number; clientHeight: number }) =>
+ el.scrollHeight - el.scrollTop - el.clientHeight < STICK_THRESHOLD;
+
+/**
+ * Whether the chat keeps following its bottom after a scroll event. Only scrolling up stops it:
+ * content that grows under a pinned view (a card's details arriving) can fire a scroll event that
+ * measures the taller page before it is re-pinned, and that must not count as reading history.
+ */
+export const nextFollowing = (wasFollowing: boolean, s: { nearBottom: boolean; scrollTop: number; lastScrollTop: number }) =>
+ s.nearBottom || (wasFollowing && s.scrollTop >= s.lastScrollTop - 1);
+
+/** Ids of cards that weren't waiting before. */
+export const newApprovalIds = (seen: ReadonlySet<string>, ids: readonly string[]): string[] =>
+ ids.filter((id) => !seen.has(id));
+
+/** Whether a box (e.g. the card's button row) lies fully inside the scroll area's visible part. */
+export const fullyVisible = (box: { top: number; bottom: number }, view: { top: number; bottom: number }) =>
+ box.top >= view.top - 1 && box.bottom <= view.bottom + 1;
+
+/**
+ * What to do when the chat's content or size changes while cards wait:
+ * - `follow`: the person is following the chat, so keep the bottom (and the newest card's
+ * buttons) in view, also when a card grows after its details arrive or the composer grows;
+ * - `notify`: they scrolled up to read, so leave the scroll alone and offer "Approval waiting";
+ * - `none`: nothing waits, or its buttons are already on screen.
+ */
+export function approvalScrollAction(s: { waiting: number; following: boolean; buttonsVisible: boolean }): 'follow' | 'notify' | 'none' {
+ if (s.following) return 'follow';
+ if (s.waiting === 0 || s.buttonsVisible) return 'none';
+ return 'notify';
+}
diff --git a/frontend/src/components/ChatView.tsx b/frontend/src/components/ChatView.tsx
index 389c5023..0140c656 100644
--- a/frontend/src/components/ChatView.tsx
+++ b/frontend/src/components/ChatView.tsx
@@ -42,6 +42,7 @@ import { BudgetPausedNotice, InterruptedNotice, SystemNotice } from './SystemNot
import { chainBudgetApi, parseBudgetPaused } from '../chainBudget';
import { isSystemNotice, parseInterruption, retryTargets } from '../interruptions';
import { consultedNames, parseDecisions, parseWorkAhead } from '../decideFirst';
+import { approvalScrollAction, fullyVisible, isNearBottom, nextFollowing } from '../approvalVisibility';
import { DecidingLine, DecisionChips, WorkAheadBody } from './Proactive';
/** A swipeable chip row whose right fade edge (phones only, see .kw-scroll-row) goes away once
@@ -567,10 +568,56 @@ export const ChatView: React.FC<ChatViewProps> = ({
// Follow new tokens only while the reader is at the bottom; scrolling up to read stays put.
const stickRef = useRef(true);
+ const lastScrollTopRef = useRef(0);
+ const pinToBottom = (el: HTMLDivElement) => {
+ el.scrollTop = el.scrollHeight;
+ lastScrollTopRef.current = el.scrollTop;
+ };
+ // A waiting card whose buttons are off screen while the person reads further up.
+ const [approvalOffscreen, setApprovalOffscreen] = useState(false);
+ const approvalKey = pendingApprovals.map((a) => a.id).join(',');
+ const checkApprovalVisible = () => {
+ const el = scrollRef.current;
+ if (!el) return;
+ const view = el.getBoundingClientRect();
+ const rows = Array.from(el.querySelectorAll<HTMLElement>('[data-approval-actions]'));
+ const action = approvalScrollAction({
+ waiting: rows.length,
+ following: stickRef.current,
+ buttonsVisible: rows.some((r) => fullyVisible(r.getBoundingClientRect(), view)),
+ });
+ setApprovalOffscreen(action === 'notify');
+ };
const onScroll = () => {
const el = scrollRef.current;
- if (el) stickRef.current = el.scrollHeight - el.scrollTop - el.clientHeight < 80;
+ if (el) {
+ stickRef.current = nextFollowing(stickRef.current, { nearBottom: isNearBottom(el), scrollTop: el.scrollTop, lastScrollTop: lastScrollTopRef.current });
+ lastScrollTopRef.current = el.scrollTop;
+ }
+ checkApprovalVisible();
};
+ const showApproval = () => {
+ const cards = scrollRef.current?.querySelectorAll<HTMLElement>('[data-approval-card]');
+ const card = cards?.[cards.length - 1];
+ if (!card) return;
+ const reduce = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
+ card.scrollIntoView({ block: 'end', behavior: reduce ? 'auto' : 'smooth' });
+ setApprovalOffscreen(false);
+ };
+ // Cards are last in the stream. While the person follows the chat, keep the bottom pinned when a
+ // card grows after it appears (its details arrive from the server) or the composer grows and
+ // shrinks the stream, so Deny / Allow never sit below the composer.
+ useEffect(() => {
+ const el = scrollRef.current;
+ if (!el || typeof ResizeObserver === 'undefined') return;
+ const ro = new ResizeObserver(() => {
+ if (stickRef.current) pinToBottom(el);
+ checkApprovalVisible();
+ });
+ ro.observe(el);
+ el.querySelectorAll('[data-approval-card]').forEach((c) => ro.observe(c));
+ return () => ro.disconnect();
+ }, [approvalKey]);
useEffect(() => {
stickRef.current = true;
}, [contextKey]);
@@ -579,10 +626,9 @@ export const ChatView: React.FC<ChatViewProps> = ({
if (!viewingHistory) stickRef.current = true;
}, [viewingHistory]);
useEffect(() => {
- if (scrollRef.current && stickRef.current) {
- scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
- }
- }, [messages, streamingText, streamingThinking, pendingApprovals.length, routingState, queued, contextKey]);
+ if (scrollRef.current && stickRef.current) pinToBottom(scrollRef.current);
+ checkApprovalVisible();
+ }, [messages, streamingText, streamingThinking, approvalKey, routingState, queued, contextKey]);
// Jump to a search hit: open its folded discussion if needed, then centre and flash it.
// Runs after the auto-scroll above, so it wins.
@@ -1346,6 +1392,22 @@ export const ChatView: React.FC<ChatViewProps> = ({
</ScrollRow>
)}
+ {approvalOffscreen && pendingApprovals.length > 0 && (
+ <div role="status" className="max-w-4xl mx-auto mb-2 flex justify-center">
+ <button
+ type="button"
+ onClick={() => {
+ playSfx('click');
+ showApproval();
+ }}
+ className="flex items-center gap-1.5 px-3 py-1.5 min-h-[36px] rounded-full border border-amber-500/40 bg-amber-950/70 text-xs font-medium text-amber-200 hover:bg-amber-900/70 shadow-lg focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-500"
+ >
+ <ArrowDown size={12} aria-hidden="true" />
+ {pendingApprovals.length === 1 ? 'Approval waiting' : `${pendingApprovals.length} approvals waiting`}
+ </button>
+ </div>
+ )}
+
{viewingHistory && (
<div role="status" className="max-w-4xl mx-auto mb-2 flex items-center justify-between gap-2 px-3 py-1.5 rounded-xl border border-sky-800/50 bg-sky-950/40 text-xs text-sky-200">
<span className="min-w-0">Showing older messages around the search result.</span>
diff --git a/frontend/src/components/ModernApprovalCard.tsx b/frontend/src/components/ModernApprovalCard.tsx
index 6cf5e4bb..cf0525e4 100644
--- a/frontend/src/components/ModernApprovalCard.tsx
+++ b/frontend/src/components/ModernApprovalCard.tsx
@@ -129,6 +129,7 @@ export const ModernApprovalCard: React.FC<ModernApprovalCardProps> = ({
return (
<div
+ data-approval-card={approval.id}
className={`my-3 border rounded-xl p-3 sm:p-4 text-zinc-100 max-w-2xl shadow-lg backdrop-blur-sm ${
waiting ? 'border-sky-500/30 bg-sky-950/20' : 'border-amber-500/30 bg-amber-950/20'
}`}
@@ -264,7 +265,7 @@ export const ModernApprovalCard: React.FC<ModernApprovalCardProps> = ({
</div>
)}
- <div className="flex flex-wrap items-center justify-end gap-2">
+ <div data-approval-actions className="flex flex-wrap items-center justify-end gap-2">
<button
type="button"
disabled={disabled}
diff --git a/frontend/tests/approvalVisibility.test.mjs b/frontend/tests/approvalVisibility.test.mjs
new file mode 100644
index 00000000..247b2777
--- /dev/null
+++ b/frontend/tests/approvalVisibility.test.mjs
@@ -0,0 +1,59 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { loadBundled } from './bundle.mjs';
+
+globalThis.localStorage ??= { getItem: () => null, setItem: () => {} };
+
+const m = await loadBundled(`
+ export * from './src/approvalVisibility';
+ export { ModernApprovalCard } from './src/components/ModernApprovalCard';
+ export { renderToStaticMarkup } from 'react-dom/server.browser';
+ export { createElement } from 'react';
+`);
+
+test('following the chat means near the bottom, within the same 80 px as before', () => {
+ assert.equal(m.isNearBottom({ scrollHeight: 2000, scrollTop: 1200, clientHeight: 780 }), true);
+ assert.equal(m.isNearBottom({ scrollHeight: 2000, scrollTop: 1000, clientHeight: 780 }), false);
+});
+
+test('a card that just started waiting is new; one already shown is not', () => {
+ assert.deepEqual(m.newApprovalIds(new Set(['a']), ['a', 'b']), ['b']);
+ assert.deepEqual(m.newApprovalIds(new Set(['a', 'b']), ['a', 'b']), []);
+});
+
+test('buttons count as visible only when the whole row is inside the stream', () => {
+ const view = { top: 60, bottom: 760 };
+ assert.equal(m.fullyVisible({ top: 700, bottom: 740 }, view), true);
+ assert.equal(m.fullyVisible({ top: 740, bottom: 780 }, view), false, 'cut by the composer');
+ assert.equal(m.fullyVisible({ top: 20, bottom: 70 }, view), false, 'scrolled above');
+});
+
+test('following: keep the bottom pinned, also when a waiting card grows afterwards', () => {
+ assert.equal(m.approvalScrollAction({ waiting: 1, following: true, buttonsVisible: false }), 'follow');
+ assert.equal(m.approvalScrollAction({ waiting: 0, following: true, buttonsVisible: false }), 'follow');
+});
+
+test('scrolled up to read: never steal the scroll, offer "Approval waiting" instead', () => {
+ assert.equal(m.approvalScrollAction({ waiting: 1, following: false, buttonsVisible: false }), 'notify');
+ assert.equal(m.approvalScrollAction({ waiting: 1, following: false, buttonsVisible: true }), 'none');
+ assert.equal(m.approvalScrollAction({ waiting: 0, following: false, buttonsVisible: false }), 'none');
+});
+
+test('a card marks itself and its button row so the chat can find them', () => {
+ const html = m.renderToStaticMarkup(
+ m.createElement(m.ModernApprovalCard, {
+ approval: { id: 'pay-1', tool_name: 'x', details: { payment_reason: 'charges a card' } },
+ onResolve: () => {},
+ }),
+ );
+ assert.match(html, /data-approval-card="pay-1"/);
+ assert.match(html, /data-approval-actions="true"[^>]*>[\s\S]*Deny[\s\S]*Allow Once/);
+});
+
+test('only scrolling up stops following; a card growing under a pinned view does not', () => {
+ // Card grew by 300 px before the view was re-pinned: not near the bottom, but nobody scrolled.
+ assert.equal(m.nextFollowing(true, { nearBottom: false, scrollTop: 1200, lastScrollTop: 1200 }), true);
+ assert.equal(m.nextFollowing(true, { nearBottom: false, scrollTop: 900, lastScrollTop: 1200 }), false, 'scrolled up');
+ assert.equal(m.nextFollowing(false, { nearBottom: false, scrollTop: 1000, lastScrollTop: 900 }), false, 'still reading');
+ assert.equal(m.nextFollowing(false, { nearBottom: true, scrollTop: 1500, lastScrollTop: 900 }), true, 'back at the bottom');
+});