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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (simplify): drop dead shouldCreateInvestigation, test the real gate (#1846)
[._.]/ Adam Eivyba4d32168cbf
diff --git a/server/services/agentErrorAnalysis.js b/server/services/agentErrorAnalysis.jsindex 9f52543efe..bc0e7f1b85 100644--- a/server/services/agentErrorAnalysis.js+++ b/server/services/agentErrorAnalysis.js@@ -519,28 +519,25 @@ export async function maybeCreateInvestigationTask(agentId, task, analysis) {** Decides whether a failed task should be blocked or retried, the metadata* fields to merge over `task.metadata` (timestamps excluded — they belong to- * the async wrapper), the analysis to hand to an investigation task, and- * whether such a task will actually be created. Extracted so the branching can- * be unit-tested directly (see `agentErrorAnalysis.test.js`) instead of through- * a drift-prone inline copy.+ * the async wrapper), and the analysis to hand to an investigation task.+ * Extracted so the branching can be unit-tested directly (see+ * `agentErrorAnalysis.test.js`) instead of through a drift-prone inline copy.+ * Whether an investigation task is actually created stays the sole concern of+ * {@link maybeCreateInvestigationTask}.** @returns {{* status: 'blocked'|'pending',- * shouldCreateInvestigation: boolean,* investigationAnalysis: object|null,- * metadataUpdates: object,- * failureCount?: number,- * lastErrorCategory?: string+ * metadataUpdates: { failureCount?: number, lastErrorCategory?: string, [k: string]: unknown }* }}*/export function resolveFailedTaskDecision(task, errorAnalysis) {- // Actionable errors get blocked immediately. An investigation task follows- // unless the failure is an API-access error (auth/forbidden/usage-limit),- // where a fresh agent would hit the same wall.+ // Actionable errors get blocked immediately. The investigation task (created+ // by the wrapper unless the failure is an API-access error) gets the original+ // analysis verbatim.if (errorAnalysis?.actionable) {return {status: 'blocked',- shouldCreateInvestigation: !API_ACCESS_ERROR_CATEGORIES.has(errorAnalysis.category),investigationAnalysis: errorAnalysis,metadataUpdates: {blockedReason: errorAnalysis.message,@@ -564,10 +561,7 @@ export function resolveFailedTaskDecision(task, errorAnalysis) {};return {status: 'blocked',- shouldCreateInvestigation: !API_ACCESS_ERROR_CATEGORIES.has(lastErrorCategory),investigationAnalysis: blockedAnalysis,- failureCount,- lastErrorCategory,metadataUpdates: {failureCount,lastErrorCategory,@@ -581,10 +575,7 @@ export function resolveFailedTaskDecision(task, errorAnalysis) {const compaction = errorAnalysis?.compaction || null;return {status: 'pending',- shouldCreateInvestigation: false,investigationAnalysis: null,- failureCount,- lastErrorCategory,metadataUpdates: {failureCount,lastErrorCategory,@@ -602,7 +593,7 @@ export function resolveFailedTaskDecision(task, errorAnalysis) {*/export async function resolveFailedTaskUpdate(task, errorAnalysis, agentId) {const decision = resolveFailedTaskDecision(task, errorAnalysis);- const now = new Date().toISOString();+ const { failureCount, lastErrorCategory } = decision.metadataUpdates;// Actionable errors get blocked immediately (investigation task created unless API access is denied)if (errorAnalysis?.actionable) {@@ -612,26 +603,27 @@ export async function resolveFailedTaskUpdate(task, errorAnalysis, agentId) {await maybeCreateInvestigationTask(agentId, task, decision.investigationAnalysis);return {status: decision.status,- metadata: { ...task.metadata, ...decision.metadataUpdates, blockedAt: now }+ metadata: { ...task.metadata, ...decision.metadataUpdates, blockedAt: new Date().toISOString() }};}if (decision.status === 'blocked') {- emitLog('warn', `🚫 Task ${task.id} blocked after ${decision.failureCount} failures (${decision.lastErrorCategory})`, {- taskId: task.id, failureCount: decision.failureCount, category: decision.lastErrorCategory+ emitLog('warn', `🚫 Task ${task.id} blocked after ${failureCount} failures (${lastErrorCategory})`, {+ taskId: task.id, failureCount, category: lastErrorCategory});await maybeCreateInvestigationTask(agentId, task, decision.investigationAnalysis);+ const now = new Date().toISOString();return {status: 'blocked',metadata: { ...task.metadata, ...decision.metadataUpdates, lastFailureAt: now, blockedAt: now }};}- emitLog('info', `🔄 Task ${task.id} retry ${decision.failureCount}/${MAX_TASK_RETRIES} (${decision.lastErrorCategory})`, {- taskId: task.id, failureCount: decision.failureCount, maxRetries: MAX_TASK_RETRIES, category: decision.lastErrorCategory+ emitLog('info', `🔄 Task ${task.id} retry ${failureCount}/${MAX_TASK_RETRIES} (${lastErrorCategory})`, {+ taskId: task.id, failureCount, maxRetries: MAX_TASK_RETRIES, category: lastErrorCategory});return {status: 'pending',- metadata: { ...task.metadata, ...decision.metadataUpdates, lastFailureAt: now }+ metadata: { ...task.metadata, ...decision.metadataUpdates, lastFailureAt: new Date().toISOString() }};}diff --git a/server/services/agentErrorAnalysis.test.js b/server/services/agentErrorAnalysis.test.jsindex 3a3ef6989e..8ed56c1f40 100644--- a/server/services/agentErrorAnalysis.test.js+++ b/server/services/agentErrorAnalysis.test.js@@ -1,9 +1,16 @@-import { describe, expect, it } from 'vitest';+import { describe, expect, it, vi, beforeEach } from 'vitest';++// maybeCreateInvestigationTask → createInvestigationTask → addTask does real+// store I/O; stub it so the skip-vs-create gate can be asserted in isolation.+vi.mock('./cos.js', () => ({ addTask: vi.fn(), updateTask: vi.fn() }));+import {analyzeAgentFailure,resolveFailedTaskDecision,+ maybeCreateInvestigationTask,MAX_TASK_RETRIES} from './agentErrorAnalysis.js';+import { addTask } from './cos.js';import { MAX_TOTAL_SPAWNS } from '../lib/validation.js';// analyzeAgentFailure ignores output shorter than 50 chars (treats it as a@@ -134,36 +141,28 @@ describe('resolveFailedTaskDecision', () => {const task = (metadata = {}) => ({ id: 'task-1', description: 'test', metadata });describe('actionable errors', () => {- it('blocks immediately and requests an investigation for a non-API-access category', () => {- const decision = resolveFailedTaskDecision(task(), { actionable: true, category: 'model-not-found', message: 'Model not found' });+ it('blocks immediately and hands the original analysis to the investigation', () => {+ const errorAnalysis = { actionable: true, category: 'model-not-found', message: 'Model not found' };+ const decision = resolveFailedTaskDecision(task(), errorAnalysis);expect(decision.status).toBe('blocked');- expect(decision.shouldCreateInvestigation).toBe(true);+ expect(decision.investigationAnalysis).toBe(errorAnalysis);expect(decision.metadataUpdates.blockedCategory).toBe('model-not-found');expect(decision.metadataUpdates.blockedReason).toBe('Model not found');});- it('blocks but skips investigation for API-access categories', () => {- for (const category of ['auth-error', 'forbidden', 'usage-limit']) {- const decision = resolveFailedTaskDecision(task(), { actionable: true, category, message: `${category} error` });- expect(decision.status).toBe('blocked');- expect(decision.shouldCreateInvestigation).toBe(false);- }- });-it('blocks regardless of prior failure count', () => {const decision = resolveFailedTaskDecision(task({ failureCount: 0 }), { actionable: true, category: 'bad-request', message: 'bad' });expect(decision.status).toBe('blocked');- expect(decision.shouldCreateInvestigation).toBe(true);});});describe('non-actionable errors with retry tracking', () => {- it('retries on the first failure (failureCount → 1)', () => {+ it('retries on the first failure (failureCount → 1) with no investigation', () => {const decision = resolveFailedTaskDecision(task(), { actionable: false, category: 'rate-limit', message: 'Rate limited' });expect(decision.status).toBe('pending');expect(decision.metadataUpdates.failureCount).toBe(1);expect(decision.metadataUpdates.lastErrorCategory).toBe('rate-limit');- expect(decision.shouldCreateInvestigation).toBe(false);+ expect(decision.investigationAnalysis).toBeNull();});it('blocks once failureCount reaches MAX_TASK_RETRIES', () => {@@ -172,7 +171,7 @@ describe('resolveFailedTaskDecision', () => {expect(decision.metadataUpdates.failureCount).toBe(MAX_TASK_RETRIES);expect(decision.metadataUpdates.blockedReason).toContain('Max retries exceeded');expect(decision.metadataUpdates.blockedCategory).toBe('network-error');- expect(decision.shouldCreateInvestigation).toBe(true);+ expect(decision.investigationAnalysis.message).toContain(`failed ${MAX_TASK_RETRIES} times`);});it('blocks on the total-spawn ceiling even when the retry count is low (the gap the inline copy missed)', () => {@@ -182,7 +181,7 @@ describe('resolveFailedTaskDecision', () => {);expect(decision.status).toBe('blocked');expect(decision.metadataUpdates.failureCount).toBe(1);- expect(decision.shouldCreateInvestigation).toBe(true);+ expect(decision.investigationAnalysis).not.toBeNull();});it('propagates compaction hints into the retry metadata (also missed by the inline copy)', () => {@@ -205,7 +204,31 @@ describe('resolveFailedTaskDecision', () => {const decision = resolveFailedTaskDecision(task({ failureCount: MAX_TASK_RETRIES - 1 }), null);expect(decision.status).toBe('blocked');expect(decision.metadataUpdates.failureCount).toBe(MAX_TASK_RETRIES);- expect(decision.shouldCreateInvestigation).toBe(true);});});});++// The actual "create an investigation task or skip it" decision lives in+// maybeCreateInvestigationTask (the sole owner of the API_ACCESS gate). Assert+// the real branch by spying on the store write it ultimately performs.+describe('maybeCreateInvestigationTask', () => {+ beforeEach(() => {+ addTask.mockReset();+ addTask.mockResolvedValue({ id: 'investigation-1' });+ });++ const task = { id: 'task-1', description: 'do the thing', metadata: {} };++ it('skips investigation for API-access categories (auth/forbidden/usage-limit)', async () => {+ for (const category of ['auth-error', 'forbidden', 'usage-limit']) {+ addTask.mockClear();+ await maybeCreateInvestigationTask('agent-1', task, { category, message: `${category} error` });+ expect(addTask).not.toHaveBeenCalled();+ }+ });++ it('creates an investigation task for a non-API-access category', async () => {+ await maybeCreateInvestigationTask('agent-1', task, { category: 'model-not-found', message: 'Model not found' });+ expect(addTask).toHaveBeenCalledTimes(1);+ });+});