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

refactor: scope manual-run unpark to the run's own record; drop unused getTaskTypeFailureInfo (#2616)

Adam Eivya9380fbb86a3
diff --git a/server/services/taskSchedule.js b/server/services/taskSchedule.js
index c19e6d2bc9..657043dc0a 100644
--- a/server/services/taskSchedule.js
+++ b/server/services/taskSchedule.js
@@ -1081,55 +1081,27 @@ export async function recordTaskTypeSuccess(taskType, appId = null) {
}
/**
- * Read the failure-park state for a task type (or null when not parked). Mirrors
- * getPerpetualParkInfo for the failure ledger.
- */
-export async function getTaskTypeFailureInfo(taskType, appId = null) {
- const schedule = await loadSchedule();
- const record = resolveExecutionRecord(schedule, taskType, appId);
- if (!record) return null;
- return {
- consecutiveFailures: Number(record.consecutiveFailures) || 0,
- lastFailureAt: record.lastFailureAt ?? null,
- lastErrorCategory: record.lastErrorCategory ?? null,
- failureParkedAt: record.failureParkedAt ?? null,
- failureParkReason: record.failureParkReason ?? null
- };
-}
-
-/**
- * Clear a task type's failure ledger + auto-park for a manual retry or config
- * change (unpark). Clears the global record AND every per-app record for the
- * type when `appId` is null, so a config change resets the whole type; targets a
- * single app when `appId` is set (a manual per-app "Run now"). Returns true if
- * anything was cleared.
+ * Clear a task type's failure ledger + auto-park for a manual retry (unpark),
+ * scoped to exactly the record being re-run: the per-app record when `appId` is
+ * set (a per-app "Run now"), or the global (appId-less) record when null (a
+ * global self-improvement "Run now"). Does NOT touch other apps' independent
+ * ledgers — a global retrigger must not silently unpark an app whose failure
+ * cause was never addressed. (The config-change unpark in updateTaskInterval,
+ * which DOES reset every scope of the type, has its own inline clear.) Returns
+ * true if anything was cleared.
*/
export async function clearTaskTypeFailurePark(taskType, appId = null) {
const schedule = await loadSchedule();
const top = schedule.executions[executionKey(taskType)];
if (!top) return false;
- // Collect the type+app scopes whose park we clear so their stale
- // notifications can be pruned after the write (so a re-park re-notifies).
- const unparkedScopes = [];
- let changed = false;
- if (appId) {
- const wasParked = !!top.perApp?.[appId]?.failureParkedAt;
- changed = clearFailureLedgerFields(top.perApp?.[appId]);
- if (wasParked) unparkedScopes.push(appId);
- } else {
- if (top.failureParkedAt) unparkedScopes.push(null);
- changed = clearFailureLedgerFields(top);
- for (const [id, rec] of Object.entries(top.perApp || {})) {
- const wasParked = !!rec.failureParkedAt;
- if (clearFailureLedgerFields(rec)) changed = true;
- if (wasParked) unparkedScopes.push(id);
- }
- }
- if (changed) await saveSchedule(schedule);
- for (const scope of unparkedScopes) {
- await clearTaskTypeFailureParkNotification(taskType, scope);
- }
- return changed;
+ const record = appId ? top.perApp?.[appId] : top;
+ const wasParked = !!record?.failureParkedAt;
+ if (!clearFailureLedgerFields(record)) return false;
+ await saveSchedule(schedule);
+ // Prune the stale park notification (if any) so the bell clears and a later
+ // re-park can re-notify.
+ if (wasParked) await clearTaskTypeFailureParkNotification(taskType, appId);
+ return true;
}
/**
diff --git a/server/services/taskSchedule.test.js b/server/services/taskSchedule.test.js
index 8ddf7642f3..169db44be8 100644
--- a/server/services/taskSchedule.test.js
+++ b/server/services/taskSchedule.test.js
@@ -120,7 +120,6 @@ import {
setPerpetualSignature,
recordTaskTypeFailure,
recordTaskTypeSuccess,
- getTaskTypeFailureInfo,
clearTaskTypeFailurePark,
computeFailureBackoffMs,
FAILURE_BACKOFF_BASE_MS,
@@ -1551,36 +1550,41 @@ describe('taskSchedule', () => {
expect(await recordTaskTypeSuccess('security', 'app-1')).toBe(false)
})
- it('getTaskTypeFailureInfo reads back the ledger (0 defaults when absent)', async () => {
- mockSchedule({
- tasks: { security: { type: 'rotation', enabled: true } },
- executions: { 'task:security': { lastRun: null, count: 0, perApp: {
- 'app-1': { lastRun: null, count: 0, consecutiveFailures: 2, lastErrorCategory: 'timeout' }
- } } }
- })
- expect(await getTaskTypeFailureInfo('security', 'app-1')).toMatchObject({ consecutiveFailures: 2, lastErrorCategory: 'timeout' })
- expect(await getTaskTypeFailureInfo('security', 'app-2')).toBeNull()
- })
-
- it('clearTaskTypeFailurePark(appId=null) clears global + every per-app record', async () => {
+ it('clearTaskTypeFailurePark(appId=null) clears ONLY the global record, not other apps', async () => {
mockSchedule({
tasks: { security: { type: 'rotation', enabled: true } },
executions: { 'task:security': {
lastRun: null, count: 0, consecutiveFailures: 4, failureParkedAt: new Date().toISOString(),
perApp: {
- 'app-1': { lastRun: null, count: 0, consecutiveFailures: 5, failureParkedAt: new Date().toISOString() },
- 'app-2': { lastRun: null, count: 0, consecutiveFailures: 1 }
+ 'app-1': { lastRun: null, count: 0, consecutiveFailures: 5, failureParkedAt: new Date().toISOString() }
}
} }
})
expect(await clearTaskTypeFailurePark('security')).toBe(true)
const saved = JSON.parse(writeFile.mock.calls.at(-1)[1])
const top = saved.executions['task:security']
+ // Global record cleared...
expect(top.consecutiveFailures).toBeUndefined()
expect(top.failureParkedAt).toBeUndefined()
- expect(top.perApp['app-1'].consecutiveFailures).toBeUndefined()
+ // ...but app-1's independent ledger is untouched (its cause was never addressed).
+ expect(top.perApp['app-1'].consecutiveFailures).toBe(5)
+ expect(top.perApp['app-1'].failureParkedAt).toBeTruthy()
+ })
+
+ it('clearTaskTypeFailurePark(appId) clears ONLY that app, not the global record', async () => {
+ mockSchedule({
+ tasks: { security: { type: 'rotation', enabled: true } },
+ executions: { 'task:security': {
+ lastRun: null, count: 0, consecutiveFailures: 4, failureParkedAt: new Date().toISOString(),
+ perApp: { 'app-1': { lastRun: null, count: 0, consecutiveFailures: 5, failureParkedAt: new Date().toISOString() } }
+ } }
+ })
+ expect(await clearTaskTypeFailurePark('security', 'app-1')).toBe(true)
+ const saved = JSON.parse(writeFile.mock.calls.at(-1)[1])
+ const top = saved.executions['task:security']
expect(top.perApp['app-1'].failureParkedAt).toBeUndefined()
- expect(top.perApp['app-2'].consecutiveFailures).toBeUndefined()
+ expect(top.consecutiveFailures).toBe(4)
+ expect(removeByMetadata).toHaveBeenCalledWith('failureParkKey', 'security:app-1')
})
it('shouldRunTask returns failure-parked for a parked ROTATION type', async () => {