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(memory): add safe JSON parsing guards to prevent crashes

Adam Eivy9b4dff003c43
diff --git a/.changelog/v0.10.x.md b/.changelog/v0.10.x.md
index b3474019ad..1fdcf2cfbe 100644
--- a/.changelog/v0.10.x.md
+++ b/.changelog/v0.10.x.md
@@ -286,6 +286,14 @@ This release introduces a structured changelog documentation system for creating
### Brain Memory Delete Handler
- **Fixed Delete Success Detection**: The MemoryTab delete handler was checking `result !== null` to determine success, but successful API responses might not return a value. Changed to track failures explicitly with a boolean flag, fixing false-positive "Deleted" toasts when delete operations failed
+### Memory Approval Mobile Responsiveness
+- **Touch-Friendly Action Buttons**: Approve, reject, and edit buttons in the pending approval section now have 44px minimum touch targets for accessibility on mobile
+- **Responsive Card Layout**: Memory cards switch from horizontal to stacked layout on small screens, preventing cramped text and buttons
+- **Mobile-Optimized Modal**: Memory edit modal includes proper mobile padding, responsive form fields, and stacked layouts for category/app dropdowns
+- **View Toggle Buttons**: List/timeline/graph view buttons now have 40px minimum height for easier mobile tapping
+- **Search Controls**: Search input and clear button sized appropriately for touch interaction
+- **Type Filter Pills**: Memory type filter buttons increased to 36px height for better mobile accessibility
+
### Task Enhancement Empty Response Handling
- **Fixed "Description is required" Error**: When task enhancement returned an empty response from the AI provider, the client would proceed to submit the task with an empty description, triggering a validation error. Now both client and server validate that the enhanced description is non-empty before proceeding
- **Graceful Fallback on Enhancement Failure**: Task enhancement failures (empty result or AI error) now fall back to using the original task description instead of aborting - shows a warning toast and continues adding the task
@@ -318,6 +326,7 @@ This release introduces a structured changelog documentation system for creating
- `weeklyDigest.js`, `taskLearning.js`, `appActivity.js`, `taskSchedule.js`
- `brainStorage.js`, `subAgentSpawner.js`, `digital-twin.js`, `streamingDetect.js`
- `routes/scaffold.js`, `routes/detect.js`
+- **Fixed BM25 Index & Memory Classifier**: Added pre-validation guards to `memoryBM25.js` and `memoryClassifier.js` that were using raw `JSON.parse()` without empty/malformed content checks - now gracefully handle empty files with sensible defaults
- **Root Cause**: Empty or corrupted JSON files would cause `JSON.parse()` to throw "Unexpected end of JSON input", crashing services. Now all JSON file reads use safe parsing with sensible defaults
### PM2 jlist ANSI Code Parsing
diff --git a/server/services/memoryBM25.js b/server/services/memoryBM25.js
index b6041e93fe..4ad83db410 100644
--- a/server/services/memoryBM25.js
+++ b/server/services/memoryBM25.js
@@ -49,6 +49,19 @@ async function loadIndex() {
}
const data = await fs.readFile(INDEX_FILE, 'utf-8')
+ // Handle empty or malformed index file
+ if (!data || !data.trim()) {
+ console.log('⚠️ BM25 index file empty, creating fresh index')
+ indexCache = createEmptyIndex()
+ return indexCache
+ }
+ // Validate JSON structure before parsing
+ const trimmed = data.trim()
+ if (!trimmed.startsWith('{') || !trimmed.endsWith('}')) {
+ console.log('⚠️ BM25 index file malformed, creating fresh index')
+ indexCache = createEmptyIndex()
+ return indexCache
+ }
indexCache = deserializeIndex(JSON.parse(data))
return indexCache
}
diff --git a/server/services/memoryClassifier.js b/server/services/memoryClassifier.js
index 7efa202534..d7f174cf9b 100644
--- a/server/services/memoryClassifier.js
+++ b/server/services/memoryClassifier.js
@@ -38,7 +38,13 @@ async function loadConfig() {
if (existsSync(MEMORY_CONFIG_FILE)) {
const content = await readFile(MEMORY_CONFIG_FILE, 'utf-8');
- configCache = { ...DEFAULT_CONFIG, ...JSON.parse(content) };
+ // Handle empty or malformed config file
+ if (content && content.trim() && content.trim().startsWith('{') && content.trim().endsWith('}')) {
+ configCache = { ...DEFAULT_CONFIG, ...JSON.parse(content) };
+ } else {
+ console.log('⚠️ Memory classifier config file empty/malformed, using defaults');
+ configCache = DEFAULT_CONFIG;
+ }
} else {
configCache = DEFAULT_CONFIG;
}
@@ -180,7 +186,14 @@ function parseLLMResponse(response) {
return { memories: [], rejected: [], parseError: true };
}
- const parsed = JSON.parse(jsonMatch[1]);
+ let parsed;
+ const jsonStr = jsonMatch[1].trim();
+ // Validate JSON structure before parsing
+ if (!jsonStr || !(jsonStr.startsWith('{') && jsonStr.endsWith('}'))) {
+ console.log('⚠️ Extracted JSON appears malformed');
+ return { memories: [], rejected: [], parseError: true };
+ }
+ parsed = JSON.parse(jsonStr);
// Validate structure
if (!Array.isArray(parsed.memories)) {