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: address claude review — forward-only migration bump + getWindowedStats test (#2460)

Adam Eivyc6e3bccb9f40
diff --git a/scripts/migrations/188-cos-learning-recent-outcomes-ring.js b/scripts/migrations/188-cos-learning-recent-outcomes-ring.js
index 7dd741ce05..4db89edec6 100644
--- a/scripts/migrations/188-cos-learning-recent-outcomes-ring.js
+++ b/scripts/migrations/188-cos-learning-recent-outcomes-ring.js
@@ -56,7 +56,9 @@ export async function up({ rootDir }) {
}
}
- const bumpedVersion = data.version !== 2;
+ // Forward-only: only bump an absent/older version up to 2. Never downgrade a
+ // store that is already at 2+ (a future v3, or a re-run against edited data).
+ const bumpedVersion = !(typeof data.version === 'number' && data.version >= 2);
if (bumpedVersion) data.version = 2;
if (initialized > 0 || bumpedVersion) {
diff --git a/server/services/layeredIntelligence.js b/server/services/layeredIntelligence.js
index 8974496715..7ec61d752b 100644
--- a/server/services/layeredIntelligence.js
+++ b/server/services/layeredIntelligence.js
@@ -618,6 +618,9 @@ export async function gatherSources(app, config, { cosPath = PATHS.cos } = {}) {
// since-resolved failure burst age out of the "is work needed" signal instead
// of permanently depressing it; `recentSuccessRate` is null when there are no
// in-window runs, in which case the reasoner leans on the lifetime rate.
+ // Note the intentional rename: computeWindowedStats' internal `windowed*`
+ // fields are surfaced to the reasoner as `recent*` (reads more naturally in
+ // the prompt context) — same concept, deliberately different label here.
const summary = {};
for (const [type, m] of Object.entries(learning.byTaskType)) {
const windowed = computeWindowedStats(m?.recentOutcomes);
diff --git a/server/services/taskLearning/metrics.test.js b/server/services/taskLearning/metrics.test.js
index 7862e5b752..c76587216a 100644
--- a/server/services/taskLearning/metrics.test.js
+++ b/server/services/taskLearning/metrics.test.js
@@ -1,5 +1,17 @@
-import { describe, it, expect } from 'vitest';
-import { buildTaskTelemetryContext, computeLatencySplit, recordFailureSignature } from './metrics.js';
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+// Override ONLY loadLearningData so the getWindowedStats wrapper can be exercised
+// without touching the real learning.json (repo rule: DB/file-backed tests must
+// never mutate real data). Everything else in store.js stays the real
+// implementation via importActual, so the pure telemetry tests below are
+// unaffected.
+vi.mock('./store.js', async (importActual) => {
+ const actual = await importActual();
+ return { ...actual, loadLearningData: vi.fn() };
+});
+
+import { buildTaskTelemetryContext, computeLatencySplit, recordFailureSignature, getWindowedStats } from './metrics.js';
+import { loadLearningData } from './store.js';
// buildTaskTelemetryContext + recordFailureSignature are the pure telemetry
// enrichment core added for issue #2329. They take no I/O, so every branch is
@@ -347,3 +359,42 @@ describe('computeLatencySplit (#2329)', () => {
expect(out.source.queue).toBe('measured');
});
});
+
+describe('getWindowedStats (issue #2460)', () => {
+ beforeEach(() => { loadLearningData.mockReset(); });
+
+ it('windows the task type ring and returns computeWindowedStats output', async () => {
+ loadLearningData.mockResolvedValue({
+ byTaskType: {
+ 'self-improve:ui': {
+ recentOutcomes: [
+ { t: '2026-07-10T00:00:00.000Z', s: false },
+ { t: '2026-07-10T00:01:00.000Z', s: true },
+ { t: '2026-07-10T00:02:00.000Z', s: true }
+ ]
+ }
+ }
+ });
+ const stats = await getWindowedStats('self-improve:ui', { maxAgeMs: Infinity });
+ expect(stats.windowedCompleted).toBe(3);
+ expect(stats.windowedSucceeded).toBe(2);
+ expect(stats.windowedSuccessRate).toBe(67);
+ });
+
+ it('returns the null-successRate sentinel for an absent task type', async () => {
+ loadLearningData.mockResolvedValue({ byTaskType: {} });
+ const stats = await getWindowedStats('does-not-exist');
+ expect(stats.windowedCompleted).toBe(0);
+ expect(stats.windowedSuccessRate).toBeNull();
+ });
+
+ it('honors an explicit maxCount window', async () => {
+ const recentOutcomes = [];
+ for (let i = 0; i < 10; i++) recentOutcomes.push({ t: `old-${i}`, s: false });
+ for (let i = 0; i < 3; i++) recentOutcomes.push({ t: `new-${i}`, s: true });
+ loadLearningData.mockResolvedValue({ byTaskType: { 'auto-fix': { recentOutcomes } } });
+ const stats = await getWindowedStats('auto-fix', { maxCount: 3, maxAgeMs: Infinity });
+ expect(stats.windowedCompleted).toBe(3);
+ expect(stats.windowedSuccessRate).toBe(100);
+ });
+});
diff --git a/server/services/taskLearning/store.js b/server/services/taskLearning/store.js
index 03179831df..54664449a9 100644
--- a/server/services/taskLearning/store.js
+++ b/server/services/taskLearning/store.js
@@ -66,7 +66,7 @@ export const DEFAULT_WINDOW_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000; // 30 days
/**
* Append a recent outcome to a task-type metrics bucket's bounded ring (issue
- * #2460). Pure — mutates and returns `metrics`. Stores the compact `{ t, s }`
+ * #2460). Mutates `metrics` in place and returns it for chaining. Stores the compact `{ t, s }`
* shape (ISO timestamp + success bool). Tolerates a bucket that predates the ring
* (older learning.json before migration 188) by initializing it. Enforces
* `RECENT_OUTCOMES_CAP`, dropping the oldest sample first.