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]): address claude review — stale wordplay drill, mid-save training, loading snapshot

Adam Eivy3f427804fe3e
diff --git a/client/src/components/meatspace/post/WordplayTrainer.jsx b/client/src/components/meatspace/post/WordplayTrainer.jsx
index 4e154fdd1f..d939ce3318 100644
--- a/client/src/components/meatspace/post/WordplayTrainer.jsx
+++ b/client/src/components/meatspace/post/WordplayTrainer.jsx
@@ -125,6 +125,17 @@ export default function WordplayTrainer({ onBack, config, onConfigUpdate, mode =
// unrelated re-renders and after a consent-driven start.
useEffect(() => {
if (!selectedMode) return;
+ // 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.
+ if (drill && drill.type !== selectedMode) {
+ 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; }
// Wait for the cache-status fetch before deciding cold vs warm — null means
@@ -167,6 +178,9 @@ 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;
setLoading(false);
if (generated) {
setDrill(generated);
diff --git a/client/src/hooks/usePostSession.js b/client/src/hooks/usePostSession.js
index 480738b4bf..4f2dd3e80a 100644
--- a/client/src/hooks/usePostSession.js
+++ b/client/src/hooks/usePostSession.js
@@ -38,9 +38,15 @@ function loadRunSnapshot() {
let snap = null;
try { snap = JSON.parse(raw); } catch { return null; } // corrupt storage → start fresh
if (!snap || typeof snap !== 'object') return null;
- // A run persisted mid-save reads back as still-completed; the idempotent
- // submit (client-supplied id) makes a re-save safe.
- if (snap.state === 'saving') snap.state = 'complete';
+ if (snap.state === 'saving') {
+ // A scored session re-saves idempotently (client-supplied id → upsert), so
+ // restoring a mid-save run to `complete` is safe. A TRAINING save instead
+ // loops non-idempotent training-log writes; restoring it would let a re-save
+ // double-log every drill — and the entries were most likely already posted —
+ // so drop it rather than resume.
+ if (snap.isTraining) return null;
+ snap.state = 'complete';
+ }
if (!RESTORABLE_STATES.has(snap.state)) return null;
return snap;
}
@@ -131,6 +137,13 @@ export function usePostSession() {
clearRunSnapshot();
return;
}
+ // While a drill is generating (initial start, next drill, or LLM scoring),
+ // do NOT overwrite the last stable snapshot: `loading` isn't restorable, and
+ // the in-flight request can't be resumed — but the COMPLETED results already
+ // captured in the prior drilling/between-drills snapshot must survive a
+ // refresh during generation. Keeping the last good snapshot lets a refresh
+ // resume at the between-drills screen instead of dropping the whole run.
+ if (state === STATES.LOADING) return;
if (typeof sessionStorage === 'undefined') return;
sessionStorage.setItem(RUN_STORAGE_KEY, JSON.stringify({
runId, state, drills, currentDrillIndex, currentDrill, currentQuestionIndex,
diff --git a/client/src/hooks/usePostSession.test.js b/client/src/hooks/usePostSession.test.js
index fc2559cdd2..c7bed76f86 100644
--- a/client/src/hooks/usePostSession.test.js
+++ b/client/src/hooks/usePostSession.test.js
@@ -309,6 +309,43 @@ describe('usePostSession — refresh-safe run + idempotent submit (issue #2098)'
expect(second.result.current.answers).toHaveLength(1); // the mid-drill answer survived
});
+ it('keeps completed results in the snapshot during next-drill generation (does not drop the run on refresh)', async () => {
+ generatePostDrill
+ .mockResolvedValueOnce({ type: 'doubling-chain', config: {}, questions: [{ prompt: '2 x 2', expected: 4 }] })
+ .mockImplementationOnce(() => new Promise(() => {})); // drill 2 generation hangs (mid-refresh)
+
+ const { result } = renderHook(() => usePostSession());
+ await act(async () => {
+ await result.current.startSession([
+ { type: 'doubling-chain', config: {}, timeLimitSec: 60 },
+ { type: 'doubling-chain', config: {}, timeLimitSec: 60 },
+ ]);
+ });
+ act(() => { result.current.submitAnswer('4'); }); // finish drill 1 → between-drills
+ expect(result.current.state).toBe('between-drills');
+
+ act(() => { result.current.nextDrill(); }); // drill 2 generation starts and hangs → loading
+ expect(result.current.state).toBe('loading');
+
+ // The 'loading' state must NOT have overwritten the stable between-drills
+ // snapshot — drill 1's completed result must survive a refresh here.
+ const snap = JSON.parse(sessionStorage.getItem('post.activeRun'));
+ expect(snap.state).toBe('between-drills');
+ expect(snap.drillResults).toHaveLength(1);
+ });
+
+ it('does not restore a training run persisted mid-save (avoids double training-log)', () => {
+ sessionStorage.setItem('post.activeRun', JSON.stringify({
+ runId: 'r1', state: 'saving', isTraining: true,
+ drills: [{ type: 'compound-chain' }], currentDrillIndex: 0, currentDrill: null,
+ currentQuestionIndex: 0, answers: [],
+ drillResults: [{ module: 'llm-drills', type: 'compound-chain', score: 90 }],
+ sessionScore: 90, tags: {},
+ }));
+ const { result } = renderHook(() => usePostSession());
+ expect(result.current.state).toBe('idle'); // training run is dropped, not resumed
+ });
+
it('clears the persisted run on reset (no stale run resumes next mount)', async () => {
generatePostDrill.mockResolvedValue({
type: 'doubling-chain', config: { startValue: 2, steps: 1 }, questions: [{ prompt: '2 x 2', expected: 4 }],