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
log CoS maintenance timer failures instead of swallowing them (#7552)
[._.]/ Adam Eivyb59078406cdc
diff --git a/server/services/cos.js b/server/services/cos.jsindex e1420034b6..72fe798710 100644--- a/server/services/cos.js+++ b/server/services/cos.js@@ -389,13 +389,19 @@ async function runStart() {// already mutated the on-disk agents map.const freshState = await loadState();const liveAgentIds = new Set(Object.keys(freshState.agents || {}));- const { cleared: clearedActiveAgents } = await clearStaleActiveAgents(liveAgentIds).catch(() => ({ cleared: [] }));+ const { cleared: clearedActiveAgents } = await clearStaleActiveAgents(liveAgentIds).catch(err => {+ console.warn(`⚠️ clearStaleActiveAgents failed: ${err?.message || err}`, err?.stack || '');+ return { cleared: [] };+ });if (clearedActiveAgents.length > 0) {emitLog('info', `🧹 Cleared ${clearedActiveAgents.length} stale activeAgentId pointer(s) from app-activity`);}// Archive stale completed agents from state.json on startup- const { archived } = await _archiveStaleAgents().catch(() => ({ archived: 0 }));+ const { archived } = await _archiveStaleAgents().catch(err => {+ console.warn(`⚠️ _archiveStaleAgents failed: ${err?.message || err}`, err?.stack || '');+ return { archived: 0 };+ });if (archived > 0) {emitLog('info', `📦 Startup: archived ${archived} stale agent(s) from state`);}@@ -417,7 +423,10 @@ async function runStart() {emitLog('info', `🧹 Periodic cleanup: ${cleaned} orphaned agent(s)`);}await resetOrphanedTasks();- const { archived } = await _archiveStaleAgents().catch(() => ({ archived: 0 }));+ const { archived } = await _archiveStaleAgents().catch(err => {+ console.warn(`⚠️ _archiveStaleAgents failed: ${err?.message || err}`, err?.stack || '');+ return { archived: 0 };+ });if (archived > 0) {emitLog('info', `📦 Auto-archived ${archived} stale agent(s) from state`);}@@ -489,7 +498,10 @@ async function runStart() {handler: async () => {const s = await loadState();const gracePeriodMs = (s.config.rehabilitationGracePeriodDays || 7) * 24 * 60 * 60 * 1000;- const result = await checkAndRehabilitateSkippedTasks(gracePeriodMs).catch(() => ({ count: 0 }));+ const result = await checkAndRehabilitateSkippedTasks(gracePeriodMs).catch(err => {+ emitLog('warn', `⚠️ checkAndRehabilitateSkippedTasks failed: ${err?.message || err}`, { stack: err?.stack || '' });+ return { count: 0 };+ });if (result.count > 0) {emitLog('success', `Auto-rehabilitated ${result.count} skipped task type(s)`, {rehabilitated: result.rehabilitated?.map(r => r.taskType) || []@@ -832,7 +844,9 @@ async function resetOrphanedTasks({ bootRecovery = false } = {}) {// scan's possibly-stale copy) is redundant and would risk clobbering a// concurrent content edit. It also keeps the heartbeat a claim-only// patch so it never bumps the updatedAt LWW stamp (#1714).- await updateTask(task.id, { metadata: renewal }, taskType).catch(() => {});+ await updateTask(task.id, { metadata: renewal }, taskType).catch(err => {+ emitLog('warn', `⚠️ Lease renewal failed for task ${task.id}: ${err?.message || err}`, { taskId: task.id, stack: err?.stack || '' });+ });}continue;}diff --git a/server/services/cos.test.js b/server/services/cos.test.jsindex 374e026c9e..072f49ee9a 100644--- a/server/services/cos.test.js+++ b/server/services/cos.test.js@@ -2525,3 +2525,38 @@ describe('CoS startup — single-flight recovery', () => {expect(COS_SRC).toMatch(/export function start\(\) \{[\s\S]*?return daemonStartPromise;\s*\}/);});});++describe('cos-rehabilitation-check timer — failure observability (#7552)', () => {+ // A rejecting checkAndRehabilitateSkippedTasks used to collapse to the same+ // { count: 0 } a healthy no-op call produces, so a persistence failure in the+ // ONLY path back from an auto-skipped task type went silent forever. This+ // extracts and REALLY EXECUTES the shipped `.catch(err => {...})` handler+ // (not a reimplementation) against a mocked emitLog, rather than mocking the+ // full `runStart()` import graph — the file's own header explains why that+ // full-mock approach is rejected here as "a brittle test of mocks".+ const anchor = 'checkAndRehabilitateSkippedTasks(gracePeriodMs).catch(err => {';+ const anchorStart = COS_SRC.indexOf(anchor);++ it('is still the exact call site this test extracts its handler from', () => {+ expect(anchorStart, 'the rehabilitation catch handler moved/changed shape — update this test\'s anchor').toBeGreaterThan(-1);+ });++ it('logs via emitLog(\'warn\', ...) with the stack, and still resolves the neutral fallback, when the rehabilitation call rejects', () => {+ const bodyStart = anchorStart + anchor.length;+ const bodyEnd = COS_SRC.indexOf('\n });', bodyStart);+ const fnBody = COS_SRC.slice(bodyStart, bodyEnd);+ const emitLog = vi.fn();+ // eslint-disable-next-line no-new-func -- executes the real extracted source, not a rewritten copy+ const catchHandler = new Function('emitLog', `return function(err) {${fnBody}}`)(emitLog);++ const err = new Error('learning store unavailable');+ const result = catchHandler(err);++ expect(emitLog).toHaveBeenCalledWith(+ 'warn',+ expect.stringContaining('checkAndRehabilitateSkippedTasks failed: learning store unavailable'),+ expect.objectContaining({ stack: expect.any(String) })+ );+ expect(result).toEqual({ count: 0 });+ });+});