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: exclude skipped runs from the archive-scan duration rebuild too (#4107)

[._.]/ Adam Eivy2be21d27718d
diff --git a/server/services/taskLearning/metrics.js b/server/services/taskLearning/metrics.js
index c15af3c0ec..69325e9b53 100644
--- a/server/services/taskLearning/metrics.js
+++ b/server/services/taskLearning/metrics.js
@@ -1046,6 +1046,13 @@ export async function recalculateDurationStats() {
// purged counts. The residual archives-vs-counts skew of this manual rebuild predates
// #2696 and is ETA-cosmetic.
const vp = meta.result?.validationPassed;
+ // The skip sentinel is checked BEFORE the exit-code fallback (#4107). This
+ // rebuild re-derives durations straight off the archive rather than through
+ // recordTaskCompletion, so without this an archived run the live path had
+ // already declined to record — the hook aborted before evaluating it — would
+ // come back in through the same exit-code fallback the sentinel exists to
+ // bypass, banking its duration into the success-only ETAs.
+ if (isSkipLearningVerdict(vp)) continue;
const outcomeSuccess = typeof vp === 'boolean' ? vp : !!meta.result?.success;
if (!outcomeSuccess || duration <= 0) continue;
diff --git a/server/services/taskLearning/metricsDurationStats.test.js b/server/services/taskLearning/metricsDurationStats.test.js
new file mode 100644
index 0000000000..08ab7daf1b
--- /dev/null
+++ b/server/services/taskLearning/metricsDurationStats.test.js
@@ -0,0 +1,100 @@
+/**
+ * `recalculateDurationStats` re-derives the success-only duration ETAs by
+ * scanning the agent ARCHIVE directly, bypassing `recordTaskCompletion`. That
+ * makes it the second reader of `result.validationPassed`, and it applies the
+ * same exit-code fallback the #4107 skip sentinel exists to bypass — so a run
+ * the live path declined to record could otherwise come back in here and bank
+ * its duration anyway. These tests pin that it doesn't.
+ */
+
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+// The archive layout the scan walks: AGENTS_DIR/<YYYY-MM-DD>/<agentId>/metadata.json.
+// The root is inlined in both factories below — `vi.mock` is hoisted above every
+// top-level binding, so a shared const would be in its TDZ when they run.
+vi.mock('fs', () => ({
+ existsSync: vi.fn(() => true),
+ readdirSync: vi.fn((path) => (
+ path === '/tmp/portos-test-agents'
+ ? [{ name: '2026-08-14', isDirectory: () => true }]
+ : [{ name: 'agent-a', isDirectory: () => true }, { name: 'agent-b', isDirectory: () => true }]
+ ))
+}));
+
+vi.mock('./store.js', async (importActual) => {
+ const actual = await importActual();
+ return {
+ ...actual,
+ AGENTS_DIR: '/tmp/portos-test-agents',
+ tryReadFile: vi.fn(),
+ loadLearningData: vi.fn(),
+ saveLearningData: vi.fn(async () => {}),
+ emitLog: vi.fn()
+ };
+});
+
+import { recalculateDurationStats } from './metrics.js';
+import { tryReadFile, loadLearningData, saveLearningData } from './store.js';
+import { SKIP_LEARNING_VERDICT } from '../../lib/learningVerdict.js';
+
+const makeData = () => ({
+ version: 1,
+ byTaskType: {
+ 'internal-task': {
+ completed: 2, succeeded: 2, failed: 0,
+ totalDurationMs: 0, successDurationMs: 999, successMaxDurationMs: 999,
+ avgDurationMs: 0, maxDurationMs: 0, p80DurationMs: 0
+ }
+ },
+ byModelTier: {},
+ errorPatterns: {},
+ totals: { completed: 2, succeeded: 2, failed: 0, totalDurationMs: 0, successDurationMs: 999, successMaxDurationMs: 999 }
+});
+
+// Two archived runs, both exit-0, both 60s — they differ ONLY in the verdict.
+const archived = (validationPassed) => JSON.stringify({
+ metadata: { taskType: 'internal', taskDescription: 'Study the app' },
+ result: { success: true, duration: 60000, validationPassed }
+});
+
+describe('recalculateDurationStats — skip verdict (#4107)', () => {
+ let saved;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ saved = null;
+ loadLearningData.mockImplementation(async () => makeData());
+ saveLearningData.mockImplementation(async (data) => { saved = data; });
+ });
+
+ it('excludes an archived run whose hook aborted before evaluating it', async () => {
+ tryReadFile.mockImplementation(async () => archived(SKIP_LEARNING_VERDICT));
+
+ const out = await recalculateDurationStats();
+
+ // Both archived runs are skipped, so the reset-to-zero success totals stay zero
+ // rather than picking the exit-code fallback back up.
+ expect(out.successfulAgents ?? out.successCount ?? 0).toBe(0);
+ expect(saved.totals.successDurationMs).toBe(0);
+ expect(saved.byTaskType['internal-task'].successDurationMs).toBe(0);
+ });
+
+ it('still counts an archived run with NO verdict via the exit-code fallback', async () => {
+ // The regression guard: the skip must not have swallowed the ordinary
+ // predates-validationPassed path this rebuild depends on.
+ tryReadFile.mockImplementation(async () => archived(undefined));
+
+ await recalculateDurationStats();
+
+ expect(saved.totals.successDurationMs).toBe(120000); // 2 runs × 60s
+ expect(saved.byTaskType['internal-task'].successDurationMs).toBe(120000);
+ });
+
+ it('still excludes an archived run that MISSED its declared criterion', async () => {
+ tryReadFile.mockImplementation(async () => archived(false));
+
+ await recalculateDurationStats();
+
+ expect(saved.totals.successDurationMs).toBe(0);
+ });
+});