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 codex review — preserve retry timestamps, resume drill timer

Adam Eivyc60be1fc5814
diff --git a/client/src/hooks/usePostSession.js b/client/src/hooks/usePostSession.js
index e639a6dcdf..480738b4bf 100644
--- a/client/src/hooks/usePostSession.js
+++ b/client/src/hooks/usePostSession.js
@@ -115,8 +115,12 @@ export function usePostSession() {
const [runId, setRunId] = useState(restored?.runId ?? null);
const [tags, setTags] = useState(restored?.tags ?? {}); // session tags captured at launch
const [lastAnswer, setLastAnswer] = useState(null); // { correct, expected, answered } for training feedback
- const questionStartRef = useRef(Date.now());
- const drillStartRef = useRef(Date.now());
+ // Seed the timing refs from the restored snapshot so a mid-drill refresh keeps
+ // measuring elapsed time from the ORIGINAL question/drill start — otherwise the
+ // in-flight question's responseMs (and the drill's totalMs) would reset to 0 on
+ // reload, under-counting time and inflating the speed bonus.
+ const questionStartRef = useRef(restored?.questionStartedAt ?? Date.now());
+ const drillStartRef = useRef(restored?.drillStartedAt ?? Date.now());
const finishDrillRef = useRef(null);
// Persist the live run to sessionStorage on every meaningful change; clear it
@@ -131,6 +135,11 @@ export function usePostSession() {
sessionStorage.setItem(RUN_STORAGE_KEY, JSON.stringify({
runId, state, drills, currentDrillIndex, currentDrill, currentQuestionIndex,
answers, drillResults, sessionScore, isTraining, tags,
+ // Persist the timing anchors (mutated synchronously on each question/drill
+ // transition, just before the state change that fires this effect) so a
+ // refresh resumes the clock instead of restarting it.
+ questionStartedAt: questionStartRef.current,
+ drillStartedAt: drillStartRef.current,
}));
}, [runId, state, drills, currentDrillIndex, currentDrill, currentQuestionIndex, answers, drillResults, sessionScore, isTraining, tags]);
diff --git a/server/services/meatspacePost.js b/server/services/meatspacePost.js
index 84d9e4f4d8..fb63dc1198 100644
--- a/server/services/meatspacePost.js
+++ b/server/services/meatspacePost.js
@@ -217,11 +217,16 @@ export async function submitPostSession(sessionData) {
const sessionId = sessionData.id || randomUUID();
const existingIndex = data.sessions.findIndex(s => s.id === sessionId);
const isNewSession = existingIndex < 0;
+ const existing = isNewSession ? null : data.sessions[existingIndex];
const session = {
id: sessionId,
- date: now.split('T')[0],
- startedAt: now,
+ // Preserve the ORIGINAL day/start on an idempotent re-submit — a retry that
+ // crosses midnight (or just arrives later) must not move the session to a
+ // new date, which would corrupt history ordering and streak math. Only a
+ // fresh insert stamps "now".
+ date: existing?.date ?? now.split('T')[0],
+ startedAt: existing?.startedAt ?? now,
completedAt: now,
durationMs: rescoredTasks.reduce((sum, t) => sum + (t.totalMs || 0), 0),
cadence: sessionData.cadence || 'daily',
diff --git a/server/services/meatspacePost.test.js b/server/services/meatspacePost.test.js
index 2fce0ff466..629a88acb0 100644
--- a/server/services/meatspacePost.test.js
+++ b/server/services/meatspacePost.test.js
@@ -728,6 +728,15 @@ describe('submitPostSession — idempotent upsert by client id (issue #2098)', (
expect(stored.length).toBe(1);
});
+ it('preserves the original date/startedAt on an idempotent re-submit (no midnight drift)', async () => {
+ const first = await submitPostSession(buildBody());
+ const second = await submitPostSession(buildBody());
+ // A retry keeps the original day + start timestamp so history ordering and
+ // streaks can't be corrupted by a re-submit that arrives later (or next day).
+ expect(second.date).toBe(first.date);
+ expect(second.startedAt).toBe(first.startedAt);
+ });
+
it('assigns a server uuid when the client omits an id', async () => {
const { id, ...noId } = buildBody();
const session = await submitPostSession(noId);