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: address review — stale closures, error recovery, tags threading, config merge

Adam Eivyc5d1bb535194
diff --git a/client/src/components/meatspace/post/PostDrillConfig.jsx b/client/src/components/meatspace/post/PostDrillConfig.jsx
index 74ef27b377..9dab9aeef7 100644
--- a/client/src/components/meatspace/post/PostDrillConfig.jsx
+++ b/client/src/components/meatspace/post/PostDrillConfig.jsx
@@ -74,7 +74,11 @@ export default function PostDrillConfig({ config, onSaved, onBack }) {
setSaving(true);
const updated = await updatePostConfig({
mentalMath: { drillTypes }
+ }).catch(() => {
+ setSaving(false);
+ return null;
});
+ if (!updated) return;
toast.success('POST config saved');
setSaving(false);
onSaved(updated);
diff --git a/client/src/components/meatspace/post/PostDrillRunner.jsx b/client/src/components/meatspace/post/PostDrillRunner.jsx
index 9bbf785d1c..901402d697 100644
--- a/client/src/components/meatspace/post/PostDrillRunner.jsx
+++ b/client/src/components/meatspace/post/PostDrillRunner.jsx
@@ -24,24 +24,31 @@ export default function PostDrillRunner({ session }) {
const [timeLeft, setTimeLeft] = useState(0);
const inputRef = useRef(null);
const timerRef = useRef(null);
+ const timeExpiredRef = useRef(timeExpired);
const timeLimitMs = (currentDrill?.timeLimitSec || 120) * 1000;
const totalQuestions = currentDrill?.questions?.length || 0;
+ // Keep ref current to avoid stale closure in timer
+ useEffect(() => {
+ timeExpiredRef.current = timeExpired;
+ }, [timeExpired]);
+
// Timer
useEffect(() => {
if (state !== 'drilling' || !currentDrill) return;
const startTime = Date.now();
- setTimeLeft(timeLimitMs);
+ const limit = (currentDrill.timeLimitSec || 120) * 1000;
+ setTimeLeft(limit);
timerRef.current = setInterval(() => {
const elapsed = Date.now() - startTime;
- const remaining = Math.max(0, timeLimitMs - elapsed);
+ const remaining = Math.max(0, limit - elapsed);
setTimeLeft(remaining);
if (remaining <= 0) {
clearInterval(timerRef.current);
- timeExpired();
+ timeExpiredRef.current();
}
}, 100);
diff --git a/client/src/components/meatspace/post/PostSessionLauncher.jsx b/client/src/components/meatspace/post/PostSessionLauncher.jsx
index 9e83d84df7..2d2b20330e 100644
--- a/client/src/components/meatspace/post/PostSessionLauncher.jsx
+++ b/client/src/components/meatspace/post/PostSessionLauncher.jsx
@@ -38,7 +38,12 @@ export default function PostSessionLauncher({ config, recentSessions, onStart, o
},
timeLimitSec: cfg.timeLimitSec || 120
}));
- onStart(drillConfigs);
+ // Filter out empty tag values
+ const cleanTags = {};
+ for (const [k, v] of Object.entries(tags)) {
+ if (v.trim()) cleanTags[k] = v.trim();
+ }
+ onStart(drillConfigs, cleanTags);
}
return (
diff --git a/client/src/components/meatspace/post/PostSessionResults.jsx b/client/src/components/meatspace/post/PostSessionResults.jsx
index 46a5088d89..941f52644f 100644
--- a/client/src/components/meatspace/post/PostSessionResults.jsx
+++ b/client/src/components/meatspace/post/PostSessionResults.jsx
@@ -1,4 +1,3 @@
-import { useState } from 'react';
import { CheckCircle, Save, ArrowLeft } from 'lucide-react';
const DRILL_LABELS = {
@@ -9,20 +8,14 @@ const DRILL_LABELS = {
'estimation': 'Estimation'
};
-export default function PostSessionResults({ session, onSaved, onBack }) {
+export default function PostSessionResults({ session, tags = {}, onSaved, onBack }) {
const { drillResults, sessionScore, state, saveSession } = session;
- const [tags, setTags] = useState({});
const scoreColor = sessionScore >= 80 ? 'text-port-success' :
sessionScore >= 50 ? 'text-port-warning' : 'text-port-error';
async function handleSave() {
- // Filter out empty tags
- const cleanTags = {};
- for (const [k, v] of Object.entries(tags)) {
- if (v.trim()) cleanTags[k] = v.trim();
- }
- await saveSession(cleanTags);
+ await saveSession(tags);
onSaved();
}
diff --git a/client/src/components/meatspace/tabs/PostTab.jsx b/client/src/components/meatspace/tabs/PostTab.jsx
index fd08aed0fe..6e57c86ba3 100644
--- a/client/src/components/meatspace/tabs/PostTab.jsx
+++ b/client/src/components/meatspace/tabs/PostTab.jsx
@@ -11,6 +11,7 @@ export default function PostTab() {
const [view, setView] = useState('launcher');
const [config, setConfig] = useState(null);
const [recentSessions, setRecentSessions] = useState([]);
+ const [sessionTags, setSessionTags] = useState({});
const session = usePostSession();
useEffect(() => {
@@ -26,7 +27,8 @@ export default function PostTab() {
setRecentSessions(sessions);
}
- function handleStart(drillConfigs) {
+ function handleStart(drillConfigs, tags) {
+ setSessionTags(tags || {});
session.startSession(drillConfigs);
setView('running');
}
@@ -85,6 +87,7 @@ export default function PostTab() {
return (
<PostSessionResults
session={session}
+ tags={sessionTags}
onSaved={handleSaved}
onBack={handleBack}
/>
diff --git a/client/src/hooks/usePostSession.js b/client/src/hooks/usePostSession.js
index f118cc64bb..645f132d71 100644
--- a/client/src/hooks/usePostSession.js
+++ b/client/src/hooks/usePostSession.js
@@ -34,9 +34,13 @@ export function usePostSession() {
setDrillResults([]);
setSavedSession(null);
- // Generate first drill
const first = drillConfigs[0];
- const drill = await generatePostDrill(first.type, first.config);
+ const drill = await generatePostDrill(first.type, first.config).catch(err => {
+ toast.error(`Failed to generate drill: ${err.message}`);
+ setState(STATES.IDLE);
+ return null;
+ });
+ if (!drill) return;
setCurrentDrill({ ...drill, timeLimitSec: first.timeLimitSec });
setCurrentQuestionIndex(0);
setAnswers([]);
@@ -79,7 +83,7 @@ export function usePostSession() {
setCurrentQuestionIndex(currentQuestionIndex + 1);
questionStartRef.current = Date.now();
}
- }, [state, currentDrill, currentQuestionIndex, answers]);
+ }, [state, currentDrill, currentQuestionIndex, answers, finishDrill]);
const skipQuestion = useCallback(() => {
submitAnswer(null);
@@ -128,7 +132,12 @@ export function usePostSession() {
setState(STATES.LOADING);
const next = drills[nextIndex];
- const drill = await generatePostDrill(next.type, next.config);
+ const drill = await generatePostDrill(next.type, next.config).catch(err => {
+ toast.error(`Failed to generate drill: ${err.message}`);
+ setState(STATES.IDLE);
+ return null;
+ });
+ if (!drill) return;
setCurrentDrill({ ...drill, timeLimitSec: next.timeLimitSec });
setCurrentQuestionIndex(0);
setAnswers([]);
@@ -161,7 +170,12 @@ export function usePostSession() {
modules: ['mental-math'],
tasks: drillResults,
tags
+ }).catch(err => {
+ toast.error(`Failed to save session: ${err.message}`);
+ setState(STATES.COMPLETE);
+ return null;
});
+ if (!session) return null;
setSavedSession(session);
toast.success(`POST complete — score: ${session.score}`);
setState(STATES.SAVED);
diff --git a/server/lib/postValidation.js b/server/lib/postValidation.js
index ccc320fe90..ff08ecf591 100644
--- a/server/lib/postValidation.js
+++ b/server/lib/postValidation.js
@@ -4,8 +4,6 @@ import { z } from 'zod';
// POST (Power On Self Test) VALIDATION SCHEMAS
// =============================================================================
-const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
-
// Tags for session conditions (sleep, caffeine, stress, etc.)
export const postTagsSchema = z.record(z.string().max(200));
diff --git a/server/services/meatspacePost.js b/server/services/meatspacePost.js
index 986176e3dc..6cb9a97d03 100644
--- a/server/services/meatspacePost.js
+++ b/server/services/meatspacePost.js
@@ -39,7 +39,7 @@ async function ensureMeatspaceDir() {
export async function getPostConfig() {
const config = await readJSONFile(CONFIG_FILE, DEFAULT_CONFIG);
- return { ...DEFAULT_CONFIG, ...config };
+ return deepMerge(DEFAULT_CONFIG, config);
}
export async function updatePostConfig(updates) {