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: make brain retry classification async with proper status and logging

Adam Eivy849da004936a
diff --git a/.changelog/v1.28.x.md b/.changelog/v1.28.x.md
index 848bf44976..26a79ad028 100644
--- a/.changelog/v1.28.x.md
+++ b/.changelog/v1.28.x.md
@@ -4,12 +4,13 @@ Released: YYYY-MM-DD
## Overview
-Fix brain inbox classification timeouts caused by model flag not being passed to CLI providers.
+Fix brain inbox classification — model flag, async retry, and observability improvements.
## 🐛 Fixes
- **Brain classifier model selection**: CLI providers (claude, codex, gemini) now receive `--model` flag so the configured model is actually used — previously the model parameter was ignored, defaulting to the CLI's own default (often a slower model), causing classification to time out after 5 minutes
+- **Retry classification runs async**: "Retry AI" now works like initial capture — sets status to `classifying`, returns immediately, runs AI in background, and emits socket event when done. Previously it blocked the HTTP response waiting for the AI to finish, with no visible progress in the UI
## 🔧 Improvements
-- **Classification timing logs**: Brain classifier now logs how long AI calls take (e.g., `🧠 AI responded in 3.2s`) for easier performance debugging
+- **AI call logging**: Brain service logs when AI calls start (`🧠 Calling AI: provider / model / stage`) and how long they take (`🧠 AI responded in 3.2s`) for easier debugging
diff --git a/package-lock.json b/package-lock.json
index 348b1dc2db..e321482467 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "portos",
- "version": "1.28.1",
+ "version": "1.28.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "portos",
- "version": "1.28.1",
+ "version": "1.28.2",
"license": "MIT",
"dependencies": {
"express": "^5.2.1",
diff --git a/package.json b/package.json
index f16ba65d08..43b0bb7f1e 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "portos",
- "version": "1.28.1",
+ "version": "1.28.2",
"private": true,
"description": "Local dev machine App OS portal",
"author": "Adam Eivy (@antic|@atomantic)",
diff --git a/server/services/brain.js b/server/services/brain.js
index a6bbd27e12..8d8c7ad7d6 100644
--- a/server/services/brain.js
+++ b/server/services/brain.js
@@ -48,6 +48,8 @@ async function callAI(promptStageName, variables, providerOverride, modelOverrid
const prompt = await buildPrompt(promptStageName, variables);
const model = modelOverride || provider.defaultModel;
+ console.log(`🧠 Calling AI: ${provider.id} / ${model} / ${promptStageName}`);
+
if (provider.type === 'cli') {
return new Promise((resolve, reject) => {
@@ -570,7 +572,9 @@ export async function runWeeklyReview(providerOverride, modelOverride) {
}
/**
- * Retry classification for a needs_review item
+ * Retry classification for a needs_review item.
+ * Returns immediately after setting status to 'classifying'.
+ * AI classification runs in the background and emits a socket event on completion.
*/
export async function retryClassification(inboxLogId, providerOverride, modelOverride) {
const inboxLog = await storage.getInboxLogById(inboxLogId);
@@ -582,96 +586,26 @@ export async function retryClassification(inboxLogId, providerOverride, modelOve
const provider = providerOverride || meta.defaultProvider;
const model = modelOverride || meta.defaultModel;
- // Update AI config for this retry
+ // Set status to classifying so UI shows spinner
await storage.updateInboxLog(inboxLogId, {
ai: {
providerId: provider,
modelId: model,
promptTemplateId: 'brain-classifier'
},
- status: 'needs_review',
+ status: 'classifying',
error: null
});
- // Attempt AI classification
- let classification = null;
- let aiError = null;
-
- const aiResponse = await callAI(
- 'brain-classifier',
- { capturedText: inboxLog.capturedText, now: new Date().toISOString() },
- provider,
- model
- ).catch(err => {
- aiError = err;
- return null;
- });
-
- if (aiResponse) {
- const parsed = parseJsonResponse(aiResponse);
- const validationResult = classifierOutputSchema.safeParse(parsed);
-
- if (validationResult.success) {
- classification = validationResult.data;
- } else {
- console.error(`🧠 Classification validation failed: ${JSON.stringify(validationResult.error.errors)}`);
- aiError = new Error('Invalid classification output from AI');
- }
- }
-
- // If AI failed, update entry with error
- if (!classification) {
- const errorMessage = aiError?.message || 'AI classification failed';
- await storage.updateInboxLog(inboxLogId, {
- classification: {
- destination: 'unknown',
- confidence: 0,
- title: 'Classification failed',
- extracted: {},
- reasons: [errorMessage]
- },
- status: 'needs_review',
- error: { message: errorMessage }
- });
-
- console.log(`🧠 Retry failed for ${inboxLogId}: ${errorMessage}`);
- return {
- inboxLog: await storage.getInboxLogById(inboxLogId),
- message: 'Classification failed, needs manual review'
- };
- }
-
- // If confidence too low, update but keep as needs_review
- if (classification.confidence < meta.confidenceThreshold) {
- await storage.updateInboxLog(inboxLogId, {
- classification,
- status: 'needs_review'
- });
-
- console.log(`🧠 Retry low confidence for ${inboxLogId}: ${classification.confidence}`);
- return {
- inboxLog: await storage.getInboxLogById(inboxLogId),
- message: `Low confidence (${(classification.confidence * 100).toFixed(0)}%), needs manual review`
- };
- }
-
- // Classification succeeded with high confidence - auto-file
- const filedRecord = await fileToDestination(classification.destination, classification.extracted, classification.title);
+ console.log(`🧠 Retrying classification in background: ${inboxLogId}`);
- await storage.updateInboxLog(inboxLogId, {
- classification,
- status: 'filed',
- filed: {
- destination: classification.destination,
- recordId: filedRecord.id,
- filedAt: new Date().toISOString()
- }
- });
+ // Run AI classification in background (don't await)
+ classifyInBackground(inboxLogId, inboxLog.capturedText, meta, provider, model)
+ .catch(err => console.error(`❌ Background retry failed for ${inboxLogId}: ${err.message}`));
- console.log(`🧠 Retry successful for ${inboxLogId} -> ${classification.destination}`);
return {
inboxLog: await storage.getInboxLogById(inboxLogId),
- message: `Successfully classified as ${classification.destination}`
+ message: 'Retrying classification...'
};
}