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: move Brain capture to needs_review when filing fails after classification (#11680)

[._.]/ Adam Eivye0e02c1a6447
diff --git a/server/services/brain.js b/server/services/brain.js
index 5591feaf72..cce6764ee7 100644
--- a/server/services/brain.js
+++ b/server/services/brain.js
@@ -201,7 +201,7 @@ export async function captureThought(text, providerOverride, modelOverride, { cr
autoFile: autonomyMode !== 'dry-run',
countsTowardBudget: true
})
- .catch(err => console.error(`❌ Background classification failed for ${inboxEntry.id}: ${err.message}`));
+ .catch(err => failClassification(inboxEntry.id, err));
return {
inboxLog: inboxEntry,
@@ -265,6 +265,23 @@ function repoCaptureMessage(repoIntake) {
: 'Repo saved to Links — cloning now.';
}
+/**
+ * Last-resort handler for a throw escaping classifyInBackground (e.g. the filing
+ * write failed after a successful AI call). Without it the entry stays
+ * `classifying` until the next boot's recoverStuckClassifications.
+ */
+async function failClassification(entryId, err) {
+ console.error(`❌ Background classification failed for ${entryId}: ${err.message}`);
+ const updated = await storage.updateInboxLog(entryId, {
+ status: 'needs_review',
+ error: { message: err.message }
+ }).catch(updateErr => {
+ console.error(`❌ Failed to mark ${entryId} needs_review: ${updateErr.message}`);
+ return null;
+ });
+ if (updated) brainEvents.emit('classified', { entryId, status: 'needs_review', error: err.message });
+}
+
/**
* Background AI classification for a captured thought.
* Updates the inbox entry and emits a brain:classified event when done.
@@ -744,7 +761,7 @@ export async function retryClassification(inboxLogId, providerOverride, modelOve
autoFile: true,
countsTowardBudget: false
})
- .catch(err => console.error(`❌ Background retry failed for ${inboxLogId}: ${err.message}`));
+ .catch(err => failClassification(inboxLogId, err));
return {
inboxLog: await storage.getInboxLogById(inboxLogId),
diff --git a/server/services/brain.test.js b/server/services/brain.test.js
index 77085d9808..ca02458895 100644
--- a/server/services/brain.test.js
+++ b/server/services/brain.test.js
@@ -241,6 +241,29 @@ describe('brain service', () => {
);
});
+ it('moves the entry to needs_review and emits when filing throws after a successful classification', async () => {
+ const { brainEvents } = storage;
+ storage.createInboxLog.mockResolvedValue({ id: 'inbox-fail', status: 'classifying' });
+ storage.updateInboxLog.mockResolvedValue({ id: 'inbox-fail' });
+ storage.createPerson.mockRejectedValue(new Error('db write failed'));
+ getProviderById.mockResolvedValue({ id: 'lmstudio', enabled: true, defaultModel: 'test-model' });
+ runPromptThroughProvider.mockResolvedValue({
+ text: JSON.stringify({ destination: 'people', confidence: 0.95, title: 'Alice', extracted: { name: 'Alice' } }),
+ model: 'test-model'
+ });
+ const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
+ const emitted = new Promise(resolve => brainEvents.once('classified', resolve));
+
+ await captureThought('met Alice');
+
+ expect(await emitted).toEqual({ entryId: 'inbox-fail', status: 'needs_review', error: 'db write failed' });
+ expect(storage.updateInboxLog).toHaveBeenCalledWith('inbox-fail', {
+ status: 'needs_review',
+ error: { message: 'db write failed' }
+ });
+ errSpy.mockRestore();
+ });
+
it('omits the creative flag by default (non-creative captures stay unflagged)', async () => {
storage.createInboxLog.mockResolvedValue({ id: 'inbox-005', status: 'classifying' });