Skip to content

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

address review (codex): drop stale generate response when author switches mid-POST

[._.]/ Adam Eivy7f12b4a8d19e
diff --git a/client/src/pages/Authors.jsx b/client/src/pages/Authors.jsx
index 978fb5185d..e6e372b897 100644
--- a/client/src/pages/Authors.jsx
+++ b/client/src/pages/Authors.jsx
@@ -76,13 +76,18 @@ export default function Authors() {
const [startingGen, setStartingGen] = useState(false);
const [genJobId, setGenJobId] = useState(null);
const fileInputRef = useRef(null);
+ // Bumped on every author switch / new-author. A generate request captures
+ // this before its POST and bails if it changed by the time the POST resolves
+ // — closes the pre-jobId round-trip window where `clearGeneration` alone
+ // can't stop a stale response from writing the wrong persona's headshot.
+ const genRequestRef = useRef(0);
const gen = useMediaJobProgress(genJobId);
const isGenerating = startingGen || !!genJobId;
const setHeadshot = (url) => setForm((f) => ({ ...f, headshotImageUrl: url }));
// Drop any in-flight render so its completion can't write the wrong author.
- const clearGeneration = () => { setGenJobId(null); setStartingGen(false); };
+ const clearGeneration = () => { genRequestRef.current += 1; setGenJobId(null); setStartingGen(false); };
// Land the finished async render into the form, or surface a failure. Cleared
// genJobId on author switch (see selectAuthor/startCreate) prevents a stale
@@ -105,6 +110,7 @@ export default function Authors() {
toast.error('Add a physical description or headshot style to generate from');
return;
}
+ const requestId = genRequestRef.current;
setStartingGen(true);
// `silent: true` — this catch owns the error toast, so suppress the
// apiCore `request()` helper's default toast to avoid firing two.
@@ -114,12 +120,15 @@ export default function Authors() {
negativePrompt: `${DEFAULT_NEGATIVE_PROMPT}, extra limbs, nsfw, nude`,
width: 768,
height: 1024,
- }, { silent: true }).catch((err) => {
- toast.error(err.message || 'Headshot generation failed');
- return null;
- });
+ }, { silent: true }).catch((err) => ({ error: err }));
+ // Superseded by an author switch during the POST round-trip — clearGeneration
+ // already reset state; drop this response so it can't land on the new author.
+ if (genRequestRef.current !== requestId) return;
setStartingGen(false);
- if (!queued) return;
+ if (queued?.error) {
+ toast.error(queued.error.message || 'Headshot generation failed');
+ return;
+ }
if (queued.jobId) {
// Async backend — track progress until the job completes.
setGenJobId(queued.jobId);
diff --git a/client/src/pages/Authors.test.jsx b/client/src/pages/Authors.test.jsx
index 6bde0b95f3..a56c27b4f6 100644
--- a/client/src/pages/Authors.test.jsx
+++ b/client/src/pages/Authors.test.jsx
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
-import { render, screen, fireEvent, waitFor } from '@testing-library/react';
+import { render, screen, fireEvent, waitFor, act } from '@testing-library/react';
import Authors from './Authors';
const listAuthors = vi.fn();
@@ -139,6 +139,32 @@ describe('Authors headshot generation', () => {
expect(screen.queryByAltText('Author headshot')).toBeNull();
});
+ it('drops a generate response that resolves after switching authors', async () => {
+ // The POST is still in flight when the user switches authors — the stale
+ // continuation must not write into the newly selected persona.
+ let resolveGen;
+ generateImage.mockReturnValue(new Promise((r) => { resolveGen = r; }));
+ listAuthors.mockResolvedValue([
+ { id: 'a1', name: 'Alice', headshotImageUrl: '' },
+ { id: 'a2', name: 'Bob', headshotImageUrl: '' },
+ ]);
+ render(<Authors />);
+
+ fireEvent.click(await screen.findByRole('button', { name: 'Alice' }));
+ fireEvent.change(screen.getByPlaceholderText(/silver-streaked dark hair/i), {
+ target: { value: 'Alice description' },
+ });
+ fireEvent.click(screen.getByRole('button', { name: /Generate/i }));
+ await waitFor(() => expect(generateImage).toHaveBeenCalledTimes(1));
+
+ // Switch to Bob BEFORE the POST resolves, then let it resolve.
+ fireEvent.click(screen.getByRole('button', { name: 'Bob' }));
+ await act(async () => { resolveGen({ path: '/data/images/alice.png' }); });
+
+ expect(screen.queryByAltText('Author headshot')).toBeNull();
+ expect(toastSuccess).not.toHaveBeenCalledWith('Headshot generated');
+ });
+
it('toasts exactly once when generation fails', async () => {
generateImage.mockRejectedValue(new Error('backend down'));
await openCreateForm();