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([issue-2618]): purge environmental buckets on task-type reset, incl. outage-only types (codex round 3)

[._.]/ Adam Eivy120de59b73b7
diff --git a/server/services/taskLearning/index.js b/server/services/taskLearning/index.js
index 5a269d7fb2..8e6fca6e8c 100644
--- a/server/services/taskLearning/index.js
+++ b/server/services/taskLearning/index.js
@@ -48,6 +48,7 @@ export {
computeLatencySplit,
recordFailureSignature,
recordEnvironmentalFailure,
+ purgeEnvironmentalFailuresForType,
ENVIRONMENTAL_ERROR_CATEGORIES,
resetTaskTypeLearning,
recalculateModelTierMetrics,
diff --git a/server/services/taskLearning/metrics.js b/server/services/taskLearning/metrics.js
index 817c04c7e5..478d1629de 100644
--- a/server/services/taskLearning/metrics.js
+++ b/server/services/taskLearning/metrics.js
@@ -61,6 +61,28 @@ export function recordEnvironmentalFailure(data, { category, taskType } = {}) {
return data;
}
+/**
+ * Remove a task type's contribution from every environmental bucket (issue
+ * #2618 reset parity): decrement each bucket's count by the type's share,
+ * delete the per-type entry, and drop a bucket left with nothing — so a reset
+ * type's old outages stop appearing in insights and error-share denominators.
+ * Pure — mutates `data`. Returns the number of events removed.
+ */
+export function purgeEnvironmentalFailuresForType(data, taskType) {
+ let removed = 0;
+ for (const [category, bucket] of Object.entries(data.environmentalFailures || {})) {
+ const typeCount = bucket.taskTypes?.[taskType] || 0;
+ if (typeCount === 0) continue;
+ removed += typeCount;
+ bucket.count = Math.max(0, (Number(bucket.count) || 0) - typeCount);
+ delete bucket.taskTypes[taskType];
+ if (bucket.count <= 0 && Object.keys(bucket.taskTypes || {}).length === 0) {
+ delete data.environmentalFailures[category];
+ }
+ }
+ return removed;
+}
+
/**
* Milliseconds between two ISO timestamps. Pure. Returns null (not 0) when
* either bound is missing/unparseable so an absent timestamp never masquerades
@@ -526,8 +548,18 @@ export async function resetTaskTypeLearning(taskType) {
return withLock(async () => {
const data = await loadLearningData();
+ // Purge this type from the environmental buckets FIRST (#2618): an
+ // outage-only type has no byTaskType bucket, so the purge must not sit
+ // behind the task-type-not-found early return below.
+ const environmentalRemoved = purgeEnvironmentalFailuresForType(data, taskType);
+
const metrics = data.byTaskType[taskType];
if (!metrics) {
+ if (environmentalRemoved > 0) {
+ await saveLearningData(data);
+ emitLog('info', `Reset environmental-only learning data for ${taskType} (${environmentalRemoved} outage events purged)`, { taskType, environmentalRemoved }, '📚 TaskLearning');
+ return { reset: true, reason: 'environmental-only', taskType, environmentalRemoved };
+ }
return { reset: false, reason: 'task-type-not-found', taskType };
}
diff --git a/server/services/taskLearning/metricsReset.test.js b/server/services/taskLearning/metricsReset.test.js
index e4d88e016f..e3ab647df9 100644
--- a/server/services/taskLearning/metricsReset.test.js
+++ b/server/services/taskLearning/metricsReset.test.js
@@ -110,4 +110,29 @@ describe('resetTaskTypeLearning — failure-signature + correlation purge (#2619
expect(result.reset).toBe(true);
expect(store.saved.byTaskType['self-improve:x']).toBeUndefined();
});
+
+ it('purges the type from environmental buckets, dropping emptied ones (#2618)', async () => {
+ const data = fixture();
+ data.environmentalFailures = {
+ 'rate-limit': { count: 3, lastOccurred: '2026-07-01T00:00:00Z', taskTypes: { 'self-improve:x': 2, 'self-improve:y': 1 } },
+ 'auth-error': { count: 1, lastOccurred: '2026-07-01T00:00:00Z', taskTypes: { 'self-improve:x': 1 } }
+ };
+ vi.mocked(loadLearningData).mockResolvedValue(data);
+ const result = await resetTaskTypeLearning('self-improve:x');
+ expect(result.reset).toBe(true);
+ // rate-limit keeps the other type's share; auth-error emptied → dropped.
+ expect(store.saved.environmentalFailures['rate-limit']).toEqual({ count: 1, lastOccurred: '2026-07-01T00:00:00Z', taskTypes: { 'self-improve:y': 1 } });
+ expect(store.saved.environmentalFailures['auth-error']).toBeUndefined();
+ });
+
+ it('resets an environmental-only type (no byTaskType bucket) instead of returning not-found (#2618)', async () => {
+ const data = fixture();
+ data.environmentalFailures = {
+ 'usage-limit': { count: 2, lastOccurred: '2026-07-01T00:00:00Z', taskTypes: { 'outage-only-type': 2 } }
+ };
+ vi.mocked(loadLearningData).mockResolvedValue(data);
+ const result = await resetTaskTypeLearning('outage-only-type');
+ expect(result).toMatchObject({ reset: true, reason: 'environmental-only', environmentalRemoved: 2 });
+ expect(store.saved.environmentalFailures['usage-limit']).toBeUndefined();
+ });
});