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: handle task-learning churn import failures (#8299)

[._.]/ Adam Eivy2f12001f2216
diff --git a/server/services/taskLearning/lifecycle.importFailure.test.js b/server/services/taskLearning/lifecycle.importFailure.test.js
new file mode 100644
index 0000000000..d8e12fc454
--- /dev/null
+++ b/server/services/taskLearning/lifecycle.importFailure.test.js
@@ -0,0 +1,53 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const metricMocks = vi.hoisted(() => ({
+ recordTaskCompletion: vi.fn(async () => {}),
+ recalculateModelTierMetrics: vi.fn(async () => {}),
+}));
+
+vi.mock('./store.js', async () => {
+ const { EventEmitter } = await import('node:events');
+ return { cosEvents: new EventEmitter(), emitLog: vi.fn() };
+});
+vi.mock('./metrics.js', () => metricMocks);
+vi.mock('../taskTypeHooks.js', () => ({ declaresNoCommitCriterion: vi.fn(() => false) }));
+vi.mock('../../lib/agentOutcome.js', () => ({ isAgentHandoff: vi.fn(() => false) }));
+vi.mock('../agentChurn.js', async () => {
+ throw new Error('simulated observer module evaluation failure');
+});
+
+import { cosEvents } from './store.js';
+import { recordTaskCompletion } from './metrics.js';
+import { initTaskLearning } from './lifecycle.js';
+
+describe('agent:completed listener — churn observer import failure', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ cosEvents.removeAllListeners();
+ });
+
+ it('owns a rejected lazy import when EventEmitter ignores the listener promise', async () => {
+ const unhandledRejections = [];
+ const onUnhandledRejection = (reason) => unhandledRejections.push(reason);
+ const errorLog = vi.spyOn(console, 'error').mockImplementation(() => {});
+ process.on('unhandledRejection', onUnhandledRejection);
+
+ try {
+ initTaskLearning();
+ cosEvents.emit('agent:completed', {
+ status: 'completed',
+ taskId: 'task-1',
+ result: { success: true },
+ metadata: { taskType: 'user', taskDescription: 'Example completion' },
+ });
+ await new Promise((resolve) => setImmediate(resolve));
+
+ expect(recordTaskCompletion).toHaveBeenCalledTimes(1);
+ expect(errorLog).toHaveBeenCalledWith(expect.stringMatching(/TaskLearning\/CoS churn: Failed to load churn observer/));
+ expect(unhandledRejections).toEqual([]);
+ } finally {
+ process.off('unhandledRejection', onUnhandledRejection);
+ errorLog.mockRestore();
+ }
+ });
+});
diff --git a/server/services/taskLearning/lifecycle.js b/server/services/taskLearning/lifecycle.js
index 62d4e28d63..97c853a56b 100644
--- a/server/services/taskLearning/lifecycle.js
+++ b/server/services/taskLearning/lifecycle.js
@@ -57,8 +57,12 @@ export function initTaskLearning() {
// the SAME task type is a local diagnostic signal. A coordinator is parked
// immediately; Layered Intelligence decides later whether it supports a
// concrete planned fix.
- const { observeAgentChurn } = await import('../agentChurn.js');
- await observeAgentChurn(agent, task).catch(err => {
+ const churnModule = await import('../agentChurn.js').catch(err => {
+ console.error(`❌ 🔁 TaskLearning/CoS churn: Failed to load churn observer: ${err.message}`);
+ return null;
+ });
+ if (!churnModule) return;
+ await churnModule.observeAgentChurn(agent, task).catch(err => {
console.error(`❌ 🔁 CoS churn: Failed to observe completion: ${err.message}`);
});
});