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

fix([issue-2098]): per-run generation token for wordplay to prevent stale-drill swap and spinner wedge

Adam Eivyf3b065244502
diff --git a/client/src/components/meatspace/post/WordplayTrainer.jsx b/client/src/components/meatspace/post/WordplayTrainer.jsx
index 74a9b9d4f7..db8ad108ad 100644
--- a/client/src/components/meatspace/post/WordplayTrainer.jsx
+++ b/client/src/components/meatspace/post/WordplayTrainer.jsx
@@ -68,6 +68,12 @@ export default function WordplayTrainer({ onBack, config, onConfigUpdate, mode =
// Which mode we've already kicked off generation for, so the URL-driven
// effect below doesn't regenerate on every render.
const initiatedRef = useRef(null);
+ // Per-run generation token (CLAUDE.md's {target, generation} pattern). Every
+ // generation start bumps it; a completing runMode aborts unless its captured
+ // token is still current — so a superseded run (re-enter the same mode, or a
+ // direct mode→mode URL change mid-generation) can't land a stale drill or
+ // strand `loading`. Bumped on every clear too, to invalidate an in-flight run.
+ const genRef = useRef(0);
const [drill, setDrill] = useState(null);
const [loading, setLoading] = useState(false);
const [questionIndex, setQuestionIndex] = useState(0);
@@ -132,6 +138,7 @@ export default function WordplayTrainer({ onBack, config, onConfigUpdate, mode =
// generating" (the `drill || loading` guard below), wedging it on a
// permanent spinner. Guard against a re-run loop — only clear when dirty.
if (drill || loading || feedback || results.length || initiatedRef.current) {
+ genRef.current += 1; // invalidate any in-flight generation for the mode we left
setDrill(null); setLoading(false); setQuestionIndex(0);
setInputValue(''); setItems([]); setFeedback(null); setResults([]);
initiatedRef.current = null;
@@ -140,17 +147,22 @@ export default function WordplayTrainer({ onBack, config, onConfigUpdate, mode =
}
// A drill left over from a PREVIOUS mode (e.g. browser-back to the grid, then
// pick a different mode — the URL changed without going through
- // handleBackToModes) must be cleared first. Otherwise the `drill || loading`
- // guard below would keep the stale drill and skip generating this mode's, so
- // this mode's UI would render against the wrong drill's data.
+ // handleBackToModes) must be cleared first, and any in-flight generation for
+ // it invalidated, so this mode's UI never renders against the wrong drill.
if (drill && drill.type !== selectedMode) {
+ genRef.current += 1;
setDrill(null); setQuestionIndex(0); setInputValue(''); setItems([]);
setFeedback(null); setResults([]);
initiatedRef.current = null;
return; // re-runs with drill cleared, then generates below
}
if (initiatedRef.current === selectedMode) return;
- if (drill || loading) { initiatedRef.current = selectedMode; return; }
+ // Short-circuit ONLY on an existing drill (which, past the clear above, must
+ // match this mode). Do NOT short-circuit on `loading`: if a run is loading it
+ // belongs to a DIFFERENT mode (initiatedRef !== selectedMode here), so fall
+ // through to generate this mode — the genRef token neutralizes the orphaned
+ // run's result instead of letting its stale `loading` wedge this one.
+ if (drill) { initiatedRef.current = selectedMode; return; }
// Wait for the cache-status fetch before deciding cold vs warm — null means
// "not loaded yet", which must NOT be treated as cold (would bounce a warm
// mode back to the grid on every fresh load).
@@ -177,8 +189,9 @@ export default function WordplayTrainer({ onBack, config, onConfigUpdate, mode =
const currentPrompt = prompts[questionIndex];
async function runMode(modeId, useProviderId, useModel) {
- // Mark this mode as initiated synchronously (before any await) so the
- // URL-driven effect never fires a second generation for the same mode.
+ // Capture a fresh generation token synchronously (before any await), and mark
+ // this mode initiated so the URL effect doesn't fire a second generation.
+ const gen = ++genRef.current;
initiatedRef.current = modeId;
setLoading(true);
setDrill(null);
@@ -191,9 +204,10 @@ export default function WordplayTrainer({ onBack, config, onConfigUpdate, mode =
setActiveModel(useModel);
const generated = await generatePostDrill(modeId, { count: 5 }, useProviderId, useModel).catch(() => null);
- // Abort if a newer mode was selected mid-generation (browser-back then a
- // different mode) — this stale drill must not land under the new mode's URL.
- if (initiatedRef.current !== modeId) return;
+ // Abort if a newer generation superseded this one (re-enter same mode, a
+ // different mode, or a clear) — this stale drill must not land, and this run
+ // must not touch `loading` (which now belongs to the newer run).
+ if (gen !== genRef.current) return;
setLoading(false);
if (generated) {
setDrill(generated);
diff --git a/client/src/components/meatspace/post/WordplayTrainer.test.jsx b/client/src/components/meatspace/post/WordplayTrainer.test.jsx
index 933597bea9..b684c12207 100644
--- a/client/src/components/meatspace/post/WordplayTrainer.test.jsx
+++ b/client/src/components/meatspace/post/WordplayTrainer.test.jsx
@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { useState } from 'react';
-import { render, screen, fireEvent, waitFor } from '@testing-library/react';
+import { render, screen, fireEvent, waitFor, act } from '@testing-library/react';
// Stub every API call WordplayTrainer (and the shared WordplayDrillUI scoring
// core it now delegates to) can reach — mirrors the mocking convention used
@@ -110,6 +110,33 @@ describe('WordplayTrainer — training-log persistence (issue #2097)', () => {
await waitFor(() => expect(screen.getByText('news___')).toBeInTheDocument()); // B generated, not wedged
});
+ it('ignores a superseded generation when the same mode is re-entered mid-generation (issue #2098)', async () => {
+ let resolveFirst;
+ generatePostDrill
+ .mockImplementationOnce(() => new Promise(r => { resolveFirst = r; })) // run #1 (deferred)
+ .mockResolvedValueOnce({
+ type: 'compound-chain',
+ challenges: [{ rootWord: 'water', position: 'prefix', minExpected: 1 }],
+ }); // run #2
+
+ render(<TrainerHarness onBack={() => {}} config={{}} onConfigUpdate={() => {}} />);
+
+ fireEvent.click(await screen.findByText('Compound Chain')); // enter A → run #1 (deferred)
+ await waitFor(() => expect(screen.getByText(/Generating/i)).toBeInTheDocument());
+ fireEvent.click(screen.getAllByRole('button')[0]); // back to grid (invalidates run #1)
+ await screen.findByText('Compound Chain');
+ fireEvent.click(screen.getByText('Compound Chain')); // re-enter A → run #2 resolves
+ await waitFor(() => expect(screen.getByText('water')).toBeInTheDocument());
+
+ // The stale run #1 finally resolves — its token is superseded, so it must NOT
+ // swap the drill out from under the live run #2.
+ await act(async () => {
+ resolveFirst({ type: 'compound-chain', challenges: [{ rootWord: 'fire', position: 'prefix', minExpected: 1 }] });
+ });
+ expect(screen.getByText('water')).toBeInTheDocument();
+ expect(screen.queryByText('fire')).toBeNull();
+ });
+
it('does not submit a training entry before the round completes', async () => {
generatePostDrill.mockResolvedValue({
type: 'bridge-word',