Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix([issue-957]): share live text-suggest budget across Writers Room panels
Adam Eivyb8a46f1e1e24
diff --git a/client/src/components/writers-room/CdBridgePanel.jsx b/client/src/components/writers-room/CdBridgePanel.jsxindex 238e0fbc11..b8fea24d4c 100644--- a/client/src/components/writers-room/CdBridgePanel.jsx+++ b/client/src/components/writers-room/CdBridgePanel.jsx@@ -16,16 +16,15 @@ import useMounted from '../../hooks/useMounted';// timer: proposing a treatment is a deliberate, heavier action behind an// explicit button. It draws on the SAME daily call budget as the continuation// panel (both are text LLM calls), so the readout reflects the shared counter.-export default function CdBridgePanel({ workId, liveMode, getCursorContext, onLinked }) {+export default function CdBridgePanel({ workId, liveMode, usage, onUsageChange, getCursorContext, onLinked }) {const navigate = useNavigate();const [proposal, setProposal] = useState(null);const [loading, setLoading] = useState(false);const [sending, setSending] = useState(false);const [notice, setNotice] = useState(null);- // Local usage mirror so a fresh proposal updates the "N left today" readout- // immediately; re-synced from the prop on a parent-driven liveMode change.- const [usage, setUsage] = useState(liveMode?.usage || null);- useEffect(() => { setUsage(liveMode?.usage || null); }, [liveMode?.usage]);+ // `usage` is the SHARED live text-suggest counter, owned by WorkEditor and+ // passed in so this readout reflects budget the continuation panel spent too.+ // A fresh proposal response carries the new count, pushed up via onUsageChange.const mountedRef = useMounted();// Staleness guard for overlapping proposal requests — a second click while@@ -35,8 +34,10 @@ export default function CdBridgePanel({ workId, liveMode, getCursorContext, onLiconst genRef = useRef(0);const ctxGetterRef = useRef(getCursorContext);const workIdRef = useRef(workId);+ const onUsageChangeRef = useRef(onUsageChange);useEffect(() => { ctxGetterRef.current = getCursorContext; }, [getCursorContext]);useEffect(() => { workIdRef.current = workId; }, [workId]);+ useEffect(() => { onUsageChangeRef.current = onUsageChange; }, [onUsageChange]);const requestProposal = useCallback(async () => {const ctx = ctxGetterRef.current?.();@@ -59,7 +60,7 @@ export default function CdBridgePanel({ workId, liveMode, getCursorContext, onLiif (!mountedRef.current || gen !== genRef.current) return;setLoading(false);if (!res) return;- if (res.usage) setUsage(res.usage);+ if (res.usage) onUsageChangeRef.current?.(res.usage);if (!res.proposal) {setNotice('No treatment this time — keep writing and try again.');return;diff --git a/client/src/components/writers-room/LiveContinuationPanel.jsx b/client/src/components/writers-room/LiveContinuationPanel.jsxindex 6b5546197e..331a9dc8f3 100644--- a/client/src/components/writers-room/LiveContinuationPanel.jsx+++ b/client/src/components/writers-room/LiveContinuationPanel.jsx@@ -20,18 +20,18 @@ const KIND_TONE = {export default function LiveContinuationPanel({workId,liveMode,+ usage,+ onUsageChange,getCursorContext,onInsert,registerTrigger,}) {const [options, setOptions] = useState([]);const [loading, setLoading] = useState(false);- // Local usage so a fresh suggest updates the "N left today" readout- // immediately (the response carries the new count, which we don't push up to- // the parent's work manifest). Re-synced from the prop below so a- // parent-driven liveMode change (budget edit, work swap) isn't shadowed.- const [usage, setUsage] = useState(liveMode?.usage || null);- useEffect(() => { setUsage(liveMode?.usage || null); }, [liveMode?.usage]);+ // `usage` is the SHARED live text-suggest counter, owned by WorkEditor and+ // passed in so the CD bridge panel's readout stays in sync after this panel+ // spends budget. A fresh suggest response carries the new count, which we+ // push up via onUsageChange rather than mirroring locally.const [notice, setNotice] = useState(null);const mountedRef = useMounted();// Overlapping-call control. A fast typist can re-trigger the debounce before@@ -51,8 +51,10 @@ export default function LiveContinuationPanel({// to. Refs keep `requestSuggest` identity-stable AND current.const ctxGetterRef = useRef(getCursorContext);const workIdRef = useRef(workId);+ const onUsageChangeRef = useRef(onUsageChange);useEffect(() => { ctxGetterRef.current = getCursorContext; }, [getCursorContext]);useEffect(() => { workIdRef.current = workId; }, [workId]);+ useEffect(() => { onUsageChangeRef.current = onUsageChange; }, [onUsageChange]);const requestSuggest = useCallback(async () => {if (inFlightRef.current) { rerunPendingRef.current = true; return; }@@ -93,7 +95,7 @@ export default function LiveContinuationPanel({setLoading(false);if (!res) return;setOptions(res.options || []);- if (res.usage) setUsage(res.usage);+ if (res.usage) onUsageChangeRef.current?.(res.usage);if ((res.options || []).length === 0) setNotice('No suggestions this time — keep writing and pause again.');}, [mountedRef]);diff --git a/client/src/components/writers-room/WorkEditor.jsx b/client/src/components/writers-room/WorkEditor.jsxindex 6ee1cbd563..0ed5a5c6bb 100644--- a/client/src/components/writers-room/WorkEditor.jsx+++ b/client/src/components/writers-room/WorkEditor.jsx@@ -151,6 +151,15 @@ export default function WorkEditor({ work, onChange, onToggleExercise, exerciseO// imperative suggest fn; `liveTimerRef` is the post-typing debounce.const [liveMode, setLiveMode] = useState(work.liveMode || null);useEffect(() => { setLiveMode(work.liveMode || null); }, [work.liveMode]);+ // Shared live text-suggest usage counter. The continuation panel and the CD+ // bridge BOTH draw on the same server-side daily budget, so a single mirror+ // lives here (not one per panel) — otherwise the panel that didn't make the+ // most recent call shows a stale "N left today" readout until its own next+ // call. Seeded from / re-synced to liveMode.usage (parent-driven changes:+ // work swap, budget edit, toggle); updated by whichever panel suggests. The+ // render-preview budget is a distinct counter owned by LiveRenderPanel.+ const [liveUsage, setLiveUsage] = useState(liveMode?.usage || null);+ useEffect(() => { setLiveUsage(liveMode?.usage || null); }, [liveMode?.usage]);const liveTriggerRef = useRef(null);const liveTimerRef = useRef(null);const registerLiveTrigger = useCallback((fn) => { liveTriggerRef.current = fn; }, []);@@ -1004,6 +1013,8 @@ export default function WorkEditor({ work, onChange, onToggleExercise, exerciseO<LiveContinuationPanelworkId={work.id}liveMode={liveMode}+ usage={liveUsage}+ onUsageChange={setLiveUsage}getCursorContext={getCursorContext}onInsert={insertAtCursor}registerTrigger={registerLiveTrigger}@@ -1012,6 +1023,8 @@ export default function WorkEditor({ work, onChange, onToggleExercise, exerciseO<CdBridgePanelworkId={work.id}liveMode={liveMode}+ usage={liveUsage}+ onUsageChange={setLiveUsage}getCursorContext={getCursorContext}onLinked={(cdProjectId) => onChange?.({ ...work, cdProjectId })}/>diff --git a/client/src/components/writers-room/liveUsageSync.test.jsx b/client/src/components/writers-room/liveUsageSync.test.jsxnew file mode 100644index 0000000000..aaef2f9985--- /dev/null+++ b/client/src/components/writers-room/liveUsageSync.test.jsx@@ -0,0 +1,83 @@+import { describe, it, expect, vi, beforeEach } from 'vitest';+import { useState } from 'react';+import { render, screen, fireEvent, waitFor } from '@testing-library/react';+import { MemoryRouter } from 'react-router-dom';++// Mock the API so a suggest/proposal call returns a deterministic usage count.+const suggestWritersRoomContinuation = vi.fn();+const suggestWritersRoomCdBridge = vi.fn();+const sendWritersRoomCdBridge = vi.fn();+vi.mock('../../services/apiWritersRoom', () => ({+ suggestWritersRoomContinuation: (...args) => suggestWritersRoomContinuation(...args),+ suggestWritersRoomCdBridge: (...args) => suggestWritersRoomCdBridge(...args),+ sendWritersRoomCdBridge: (...args) => sendWritersRoomCdBridge(...args),+}));++import LiveContinuationPanel from './LiveContinuationPanel';+import CdBridgePanel from './CdBridgePanel';++beforeEach(() => {+ suggestWritersRoomContinuation.mockReset();+ suggestWritersRoomCdBridge.mockReset();+ sendWritersRoomCdBridge.mockReset();+});++const liveMode = { enabled: true, dailyCallBudget: 100, usage: { count: 0 } };+const getCursorContext = () => ({ before: 'Some prose before. ', after: '', selection: '' });++// Harness mirrors WorkEditor's lifted shared-usage state: a single `liveUsage`+// owned by the parent, passed to both panels with one setter.+function Harness() {+ const [liveUsage, setLiveUsage] = useState(liveMode.usage);+ return (+ <MemoryRouter>+ <LiveContinuationPanel+ workId="wr-work-1"+ liveMode={liveMode}+ usage={liveUsage}+ onUsageChange={setLiveUsage}+ getCursorContext={getCursorContext}+ onInsert={() => {}}+ registerTrigger={() => {}}+ />+ <CdBridgePanel+ workId="wr-work-1"+ liveMode={liveMode}+ usage={liveUsage}+ onUsageChange={setLiveUsage}+ getCursorContext={getCursorContext}+ onLinked={() => {}}+ />+ </MemoryRouter>+ );+}++describe('live text-suggest budget shares across panels', () => {+ it('a continuation suggest updates the CD bridge readout too', async () => {+ suggestWritersRoomContinuation.mockResolvedValue({ options: [], usage: { count: 7 } });+ render(<Harness />);++ // Both panels start at the full budget.+ expect(screen.getAllByText('100 / 100 left today')).toHaveLength(2);++ // Spend budget from the continuation panel.+ fireEvent.click(screen.getByTitle('Suggest a continuation from the cursor'));++ // BOTH readouts reflect the new count — the CD bridge panel did NOT make+ // the call but still updates because the counter is lifted into the parent.+ await waitFor(() => {+ expect(screen.getAllByText('93 / 100 left today')).toHaveLength(2);+ });+ });++ it('a CD bridge proposal updates the continuation readout too', async () => {+ suggestWritersRoomCdBridge.mockResolvedValue({ proposal: null, usage: { count: 12 } });+ render(<Harness />);++ fireEvent.click(screen.getByTitle('Propose a Creative Director treatment from the cursor'));++ await waitFor(() => {+ expect(screen.getAllByText('88 / 100 left today')).toHaveLength(2);+ });+ });+});