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: stop CoS learning backfill and live completions from double-counting an agent (#13095)

Adam Eivy004b4c163d07
diff --git a/server/services/taskLearning/executionDurations.test.js b/server/services/taskLearning/executionDurations.test.js
index 537f7af6c6..9c1a5ca196 100644
--- a/server/services/taskLearning/executionDurations.test.js
+++ b/server/services/taskLearning/executionDurations.test.js
@@ -41,8 +41,10 @@ const emptyData = () => ({
const TASK = { id: 't1', description: '[self-improvement] release-check - verify the release', taskType: 'scheduled' };
+// Each fixture is a distinct run: the recorder dedupes on agent id (#13095).
+let agentSeq = 0;
const agentOn = ({ providerId = 'ollama', model = 'local-coder', effort = 'low', duration, errorCategory = null } = {}) => ({
- id: 'agent-x',
+ id: `agent-x-${++agentSeq}`,
startedAt: '2026-09-01T10:00:00.000Z',
completedAt: '2026-09-01T10:10:00.000Z',
metadata: {
diff --git a/server/services/taskLearning/metrics.countedAgents.test.js b/server/services/taskLearning/metrics.countedAgents.test.js
new file mode 100644
index 0000000000..dd87e08775
--- /dev/null
+++ b/server/services/taskLearning/metrics.countedAgents.test.js
@@ -0,0 +1,75 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+// In-memory stand-in for learning.json so the real recordTaskCompletion /
+// backfillFromHistory run end to end without touching the install's data.
+const mem = vi.hoisted(() => ({ data: null }));
+
+vi.mock('./store.js', async (importActual) => {
+ const actual = await importActual();
+ return {
+ ...actual,
+ loadLearningData: vi.fn(async () => structuredClone(mem.data)),
+ saveLearningData: vi.fn(async (d) => { mem.data = structuredClone(d); }),
+ };
+});
+vi.mock('../cosAgentLifecycle.js', () => ({ getAgents: vi.fn(async () => mem.agents) }));
+vi.mock('../layeredIntelligenceOutcomes.js', () => ({ recordProposalExecution: vi.fn(async () => {}) }));
+
+import { recordTaskCompletion } from './metrics.js';
+import { backfillFromHistory } from './lifecycle.js';
+import { COUNTED_AGENT_IDS_CAP } from './store.js';
+
+const agent = (id, success = true) => ({
+ id,
+ status: 'completed',
+ taskId: `task-${id}`,
+ metadata: { taskType: 'internal', modelTier: 'medium', taskDescription: `task ${id}` },
+ result: { success, duration: 1000 },
+});
+const taskOf = (a) => ({ id: a.taskId, description: a.metadata.taskDescription, taskType: a.metadata.taskType, metadata: a.metadata });
+const freshData = () => ({
+ version: 2, byTaskType: {}, byTaskTypeExecution: {}, byModelTier: {}, errorPatterns: {},
+ failureSignatures: {}, routingAccuracy: {}, environmentalFailures: {}, correlationWindow: [],
+ totals: { completed: 0, succeeded: 0, failed: 0, totalDurationMs: 0, avgDurationMs: 0 },
+});
+
+describe('completion ledger (#13095)', () => {
+ beforeEach(() => {
+ mem.data = freshData();
+ mem.agents = [];
+ });
+
+ it('running backfill twice leaves the metrics identical to running it once', async () => {
+ mem.agents = [agent('a1'), agent('a2', false), agent('a3')];
+ expect(await backfillFromHistory()).toBe(3);
+ const once = structuredClone(mem.data);
+ expect(once.totals.completed).toBe(3);
+
+ expect(await backfillFromHistory()).toBe(0);
+ expect(mem.data).toEqual(once);
+ });
+
+ it('a live completion does not double-count an agent a backfill already counted, and vice versa', async () => {
+ const a = agent('a1');
+ mem.agents = [a];
+ await backfillFromHistory();
+ await recordTaskCompletion(a, taskOf(a));
+ expect(mem.data.totals.completed).toBe(1);
+
+ const b = agent('b1');
+ await recordTaskCompletion(b, taskOf(b));
+ mem.agents = [b];
+ expect(await backfillFromHistory()).toBe(0);
+ expect(mem.data.totals.completed).toBe(2);
+ expect(Object.values(mem.data.byTaskType)[0].completed).toBe(2);
+ });
+
+ it('bounds the ledger to the newest ids', async () => {
+ mem.data.countedAgentIds = Array.from({ length: COUNTED_AGENT_IDS_CAP }, (_, i) => `old-${i}`);
+ const a = agent('fresh');
+ await recordTaskCompletion(a, taskOf(a));
+ expect(mem.data.countedAgentIds).toHaveLength(COUNTED_AGENT_IDS_CAP);
+ expect(mem.data.countedAgentIds.at(-1)).toBe('fresh');
+ expect(mem.data.countedAgentIds[0]).toBe('old-1');
+ });
+});
diff --git a/server/services/taskLearning/metrics.js b/server/services/taskLearning/metrics.js
index 3f22a2e33d..29ec5c72a9 100644
--- a/server/services/taskLearning/metrics.js
+++ b/server/services/taskLearning/metrics.js
@@ -23,6 +23,8 @@ import {
buildRecurrenceInsight,
recurrenceMilestoneReached,
appendRecentOutcome,
+ hasCountedAgent,
+ markAgentCounted,
computeWindowedStats,
executionDurationKey,
DEFAULT_WINDOW_MAX_COUNT,
@@ -440,7 +442,8 @@ export function buildLiExecutionVerdict({ liProposal, success, validationPassed,
/**
* Record a completed task for learning.
*
- * Returns the persisted learning data, or `null` when the run was SKIPPED —
+ * Returns the persisted learning data, or `null` when the run was SKIPPED or its
+ * agent id was already counted (#13095) —
* `result.validationPassed` carrying `SKIP_LEARNING_VERDICT` means nothing ever
* evaluated it, so there is no honest outcome to bank (#4107). Callers ignore
* the return today; the null is the machine-readable form of "no write happened".
@@ -477,6 +480,12 @@ export async function recordTaskCompletion(agent, task) {
const result = await withLock(async () => {
const data = await loadLearningData();
+ // Idempotence (#13095): an agent already folded into the aggregates (by the live
+ // listener or an earlier backfill) is not counted again. Checked under the lock
+ // before any derived payload is captured, so a repeat writes nothing at all.
+ const countedAgentId = agent.agentId || agent.id;
+ if (hasCountedAgent(data, countedAgentId)) return null;
+
const taskType = telemetry.taskType;
const modelTier = agent.metadata?.modelTier || 'unknown';
const success = telemetry.success;
@@ -713,6 +722,7 @@ export async function recordTaskCompletion(agent, task) {
Object.assign(data.totals, calculateDurationETA(data.totals));
}
+ markAgentCounted(data, countedAgentId);
await saveLearningData(data);
const { provider, model, component, routingReason } = telemetry.executionContext;
diff --git a/server/services/taskLearning/store.js b/server/services/taskLearning/store.js
index 44d724c7b3..6c6d7ae488 100644
--- a/server/services/taskLearning/store.js
+++ b/server/services/taskLearning/store.js
@@ -366,6 +366,26 @@ export async function loadLearningData() {
return data;
}
+// Ledger of agent ids already folded into the aggregates (#13095). Both the live
+// `agent:completed` listener and `backfillFromHistory` feed `recordTaskCompletion`,
+// so the ledger lives beside the metrics it protects and is checked under the same
+// lock. Bounded FIFO: ids are short, and the newest runs are the ones a repeated
+// backfill or a replayed completion can plausibly re-present.
+export const COUNTED_AGENT_IDS_CAP = 10000;
+
+export function hasCountedAgent(data, agentId) {
+ return !!agentId && Array.isArray(data.countedAgentIds) && data.countedAgentIds.includes(agentId);
+}
+
+export function markAgentCounted(data, agentId) {
+ if (!agentId) return;
+ if (!Array.isArray(data.countedAgentIds)) data.countedAgentIds = [];
+ data.countedAgentIds.push(agentId);
+ if (data.countedAgentIds.length > COUNTED_AGENT_IDS_CAP) {
+ data.countedAgentIds = data.countedAgentIds.slice(-COUNTED_AGENT_IDS_CAP);
+ }
+}
+
/**
* Save learning data to file
*/