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: guard Daily Log draft/append against a switched-away day (#8422)
[._.]/ Adam Eivy3e01d9ae7fc4
diff --git a/client/src/components/brain/tabs/DailyLogTab.jsx b/client/src/components/brain/tabs/DailyLogTab.jsxindex 273a103362..a407d562b6 100644--- a/client/src/components/brain/tabs/DailyLogTab.jsx+++ b/client/src/components/brain/tabs/DailyLogTab.jsx@@ -328,11 +328,25 @@ export default function DailyLogTab() {return () => offs.forEach((off) => off());}, [date]);- // Adopt the server's entry wholesale, textarea included.- const applyEntry = (next) => {+ // Adopt the server's entry wholesale, textarea included — but only when the+ // caller is still on the day this response belongs to. `handleDraft` and+ // `handleAppend` await a slow request and the user can switch days before it+ // resolves; applying a stale response would silently overwrite the newly+ // loaded day's text with the old day's. `expectedDate` is mandatory (not+ // defaulted) so a future async caller can't bypass the check by omitting it.+ // On a mismatch the history summary still adopts the server write — the+ // original day's entry did change — but `entry`/`content` are left alone,+ // and the caller is told via the return value so it can toast where the+ // write actually landed instead of losing it silently.+ const applyEntry = (next, expectedDate) => {+ if (loadedDateRef.current !== expectedDate || next.date !== expectedDate) {+ setHistory((prev) => upsertHistory(prev, next));+ return false;+ }setEntry(next);setContent(next.content || '');setHistory((prev) => upsertHistory(prev, next));+ return true;};// One PUT attempt. Returns `{ entry }`, `{ stale, entry }` on concurrency@@ -457,15 +471,22 @@ export default function DailyLogTab() {const handleAppend = async () => {const text = quickAppend.trim();if (!text) return;+ // Capture before the await: `date` can flip to a different day while this+ // request is in flight (see applyEntry's comment above).+ const requestDate = date;setAppending(true);const res = await api.appendDailyLog(date, text, 'text', { silent: true }).catch(() => null);+ if (!mountedRef.current) return;setAppending(false);if (!res?.entry) {toast.error('Append failed');return;}- applyEntry(res.entry);- setQuickAppend('');+ if (applyEntry(res.entry, requestDate)) {+ setQuickAppend('');+ } else {+ toast(`Appended to ${formatDateFull(requestDate) || requestDate}`, { icon: '📝' });+ }};const toggleDictation = () => {@@ -548,16 +569,24 @@ export default function DailyLogTab() {toast('Save or discard your edits before drafting.', { icon: '📝' });return;}+ // Capture before the await: `date` can flip to a different day while this+ // request is in flight (see applyEntry's comment above).+ const requestDate = date;setDrafting(true);const res = await api.draftActivityDigest(date, { silent: true }).catch(() => null);+ if (!mountedRef.current) return;setDrafting(false);if (!res) {toast.error('Draft failed');return;}- if (res.entry) applyEntry(res.entry);+ const applied = res.entry ? applyEntry(res.entry, requestDate) : true;if (res.drafted) {- toast.success(res.usedLlm ? 'Drafted with AI narrative' : 'Drafted from your timeline');+ if (applied) {+ toast.success(res.usedLlm ? 'Drafted with AI narrative' : 'Drafted from your timeline');+ } else {+ toast.success(`Draft saved to ${formatDateFull(requestDate) || requestDate}`);+ }} else {toast('No tracked activity for this day yet.', { icon: '🗓️' });}diff --git a/client/src/components/brain/tabs/DailyLogTab.test.jsx b/client/src/components/brain/tabs/DailyLogTab.test.jsxindex 77a26dc5a1..ea768f05b0 100644--- a/client/src/components/brain/tabs/DailyLogTab.test.jsx+++ b/client/src/components/brain/tabs/DailyLogTab.test.jsx@@ -551,6 +551,70 @@ describe('DailyLogTab autosave', () => {});});+// A slow Draft or Quick Append response must not land on whatever day the+// user has switched to by the time it resolves (#8422).+describe('DailyLogTab draft/append date guard', () => {+ it('does not apply a Draft response to a day the user switched away from', async () => {+ let releaseDraft;+ api.draftActivityDigest.mockImplementation(() => new Promise((resolve) => {+ releaseDraft = () => resolve({+ entry: entryFor(TODAY, 'drafted content'),+ drafted: true,+ usedLlm: false,+ });+ }));+ await renderTab();++ fireEvent.click(screen.getByLabelText('Draft activity digest'));+ // Switch to yesterday while the draft is still in flight.+ fireEvent.click(screen.getByTitle('Previous day'));+ await act(async () => { await vi.advanceTimersByTimeAsync(0); });+ expect(editor().value).toBe('old day');++ // The draft resolves after the switch — it must not overwrite yesterday's+ // textarea/entry, only reflect the write to today in the history summary+ // and tell the user where the result actually landed.+ await act(async () => { releaseDraft(); await vi.advanceTimersByTimeAsync(0); });+ expect(editor().value).toBe('old day');+ expect(mockToast.success).toHaveBeenCalledWith(expect.stringMatching(/^Draft saved to /));+ });++ it('applies a Draft response normally when the user stays on the same day', async () => {+ api.draftActivityDigest.mockResolvedValue({+ entry: entryFor(TODAY, 'drafted content'),+ drafted: true,+ usedLlm: true,+ });+ await renderTab();++ fireEvent.click(screen.getByLabelText('Draft activity digest'));+ await act(async () => { await vi.advanceTimersByTimeAsync(0); });++ expect(editor().value).toBe('drafted content');+ expect(mockToast.success).toHaveBeenCalledWith('Drafted with AI narrative');+ });++ it('does not apply a Quick Append response to a day the user switched away from', async () => {+ let releaseAppend;+ api.appendDailyLog.mockImplementation(() => new Promise((resolve) => {+ releaseAppend = () => resolve({ entry: entryFor(TODAY, 'existing\n\nappended text') });+ }));+ await renderTab();++ fireEvent.change(screen.getByLabelText('Quick append a paragraph to the daily log'), {+ target: { value: 'appended text' },+ });+ fireEvent.click(screen.getByRole('button', { name: 'Append' }));+ fireEvent.click(screen.getByTitle('Previous day'));+ await act(async () => { await vi.advanceTimersByTimeAsync(0); });+ expect(editor().value).toBe('old day');++ await act(async () => { releaseAppend(); await vi.advanceTimersByTimeAsync(0); });+ expect(editor().value).toBe('old day');+ expect(mockToast).toHaveBeenCalledWith(expect.stringMatching(/^Appended to /), expect.anything());+ });+});+// The toolbar used to be one flat `flex-wrap` row of 11 controls, which spilled// into 4+ stacked rows on a 375px viewport and pushed the textarea below the// fold (#3526). jsdom has no layout engine, so these assert the structure that