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: derive LI's learning-store key so selfEval's execution-health signal actually fires (#2700)

[._.]/ Adam Eivy218e68807c0b
diff --git a/server/services/layeredIntelligence.js b/server/services/layeredIntelligence.js
index f1b9b84ca7..b0372a4e36 100644
--- a/server/services/layeredIntelligence.js
+++ b/server/services/layeredIntelligence.js
@@ -29,7 +29,7 @@ import { fetchPublicText } from '../lib/safeUrlFetch.js';
import { validateCommand } from '../lib/commandSecurity.js';
import { getSettings } from './settings.js';
import { createTicket, searchIssues, addLabels, escapeJql } from './jira.js';
-import { computeWindowedStats, computeEffectiveSuccessRate } from './taskLearning/store.js';
+import { computeWindowedStats, computeEffectiveSuccessRate, extractTaskType } from './taskLearning/store.js';
// Tracker labels + slug marker. The slug is the stable dedup key the reasoner
// chooses; it is embedded in each filed issue body so a later run (or the
@@ -124,11 +124,25 @@ export const LOW_MERGE_RATE_THRESHOLD = 20;
// it stops proposing. A rate needs a sample before it means anything.
export const LOW_MERGE_RATE_MIN_SAMPLE = 4;
-// The `learning.json` byTaskType key the LI loop's own agent runs land under —
-// LI's scheduled task type (see taskSchedule.js SELF_IMPROVEMENT_TASK_TYPES).
-// This is the bucket computeSelfEvalSummary reads to judge whether the LI
-// machinery ITSELF is healthy, as opposed to how its proposals fare downstream.
-export const LI_TASK_TYPE = 'layered-intelligence';
+// LI's SCHEDULE name (taskSchedule.js SELF_IMPROVEMENT_TASK_TYPES) — also the key
+// the type-failure ledger uses. NOT the key its runs are recorded under.
+export const LI_SCHEDULED_TASK_TYPE = 'layered-intelligence';
+
+// The `learning.json` byTaskType key LI's own agent runs actually land under — the
+// bucket computeSelfEvalSummary reads to judge whether the LI machinery ITSELF is
+// healthy (as opposed to how its proposals fare downstream once filed).
+//
+// DERIVED, never restated: a scheduled LI task is generated with
+// `metadata.analysisType = 'layered-intelligence'` (cosTaskGenerator's
+// generateSelfImprovementTaskForType), and extractTaskType's FIRST branch turns any
+// task carrying an analysisType into `self-improve:<type>` — so these runs are
+// recorded under `self-improve:layered-intelligence`, not the bare schedule name.
+// The bare name IS correct in two OTHER stores (the schedule map and the
+// type-failure ledger), which makes this an easy and silent thing to get wrong:
+// guessing it would leave the execution-health signal permanently reading "no LI
+// runs recorded yet". Building the key with the same function the WRITER uses means
+// it cannot drift out of sync with however task types are keyed later.
+export const LI_TASK_TYPE = extractTaskType({ metadata: { analysisType: LI_SCHEDULED_TASK_TYPE } });
// LI-task success rate (%) below which selfEval reports the loop's own execution
// as DEGRADED (#2700) — a separate failure mode from a low merge rate: the merge
@@ -724,7 +738,11 @@ export function computeSelfEvalSummary({
} else if (!liTaskStats.metrics) {
lines.push('- LI execution health: no LI runs recorded yet — this loop has no execution history.');
} else {
- const { successRate, source, windowedCompleted } = computeEffectiveSuccessRate(liTaskStats.metrics);
+ // Forward the clock seam: computeEffectiveSuccessRate age-filters the recent
+ // ring, so without `now` this branch would read the real wall clock while the
+ // suppression-window branch above uses the injected one — the same summary
+ // reasoning against two different "nows", and a non-pure "pure" function.
+ const { successRate, source, windowedCompleted } = computeEffectiveSuccessRate(liTaskStats.metrics, { now });
const sample = source === 'windowed' ? windowedCompleted : (liTaskStats.metrics.completed || 0);
if (successRate === null) {
lines.push('- LI execution health: no completed LI runs recorded yet — success rate unknown.');
diff --git a/server/services/layeredIntelligence.test.js b/server/services/layeredIntelligence.test.js
index 5d4e590159..ea61f24277 100644
--- a/server/services/layeredIntelligence.test.js
+++ b/server/services/layeredIntelligence.test.js
@@ -2,6 +2,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { mkdtemp, readFile, writeFile, rm } from 'fs/promises';
import { tmpdir } from 'os';
import { join } from 'path';
+import { extractTaskType } from './taskLearning/store.js';
import {
defaultLayeredIntelligenceConfig,
getEffectiveConfig,
@@ -34,6 +35,7 @@ import {
summarizeOutcomeStats,
readLiTaskMetrics,
LI_TASK_TYPE,
+ LI_SCHEDULED_TASK_TYPE,
LI_DEGRADED_SUCCESS_THRESHOLD,
LI_DEGRADED_MIN_SAMPLE,
PROPOSAL_OUTCOMES,
@@ -794,11 +796,46 @@ describe('computeSelfEvalSummary (#2700)', () => {
expect(report).not.toContain('GUIDANCE — low self-confidence');
});
- it('exposes the LI task-type key and degraded thresholds', () => {
- expect(LI_TASK_TYPE).toBe('layered-intelligence');
+ it('exposes the degraded thresholds', () => {
expect(LI_DEGRADED_SUCCESS_THRESHOLD).toBe(50);
expect(LI_DEGRADED_MIN_SAMPLE).toBe(4);
});
+
+ it('honors the injected clock when ageing the LI run window', () => {
+ const now = Date.now();
+ const DAY_MS = 24 * 60 * 60 * 1000;
+ // 6 in-window failures (enough for the 'windowed' branch, which needs >= 5)
+ // alongside a lifetime rate that says the loop is healthy: the windowed read
+ // must win, which it can only do if `now` reaches computeWindowedStats.
+ const recentOutcomes = Array.from({ length: 6 }, (_, i) => ({
+ t: new Date(now - (i + 1) * DAY_MS).toISOString(), s: false
+ }));
+ const liTaskStats = { read: true, metrics: { completed: 50, succeeded: 50, failed: 0, successRate: 100, recentOutcomes } };
+ expect(computeSelfEvalSummary({ liTaskStats, now })).toContain('0% of 6 windowed LI runs succeeded — DEGRADED');
+ // Wind the clock past the window: the same ring ages out and the lifetime rate
+ // takes over. A hard-wired Date.now() would report DEGRADED here too.
+ const later = now + 365 * DAY_MS;
+ expect(computeSelfEvalSummary({ liTaskStats, now: later })).toContain('100% of 50 lifetime LI runs succeeded');
+ });
+});
+
+describe('LI_TASK_TYPE (#2700)', () => {
+ // The learning store keys LI's runs by extractTaskType, whose FIRST branch
+ // prefixes any task carrying an analysisType. Asserting against the real
+ // function (not a restated literal) is the point: a hand-written
+ // 'layered-intelligence' silently matches no bucket, leaving execution health
+ // permanently reading "no LI runs recorded yet" with every test still green.
+ it('matches the key extractTaskType records a real scheduled LI task under', () => {
+ // The task shape cosTaskGenerator.generateSelfImprovementTaskForType builds.
+ const liTask = { metadata: { analysisType: LI_SCHEDULED_TASK_TYPE, autoGenerated: true, selfImprovement: true } };
+ expect(LI_TASK_TYPE).toBe(extractTaskType(liTask));
+ });
+
+ it('is the self-improve-prefixed key, NOT the bare schedule name', () => {
+ expect(LI_SCHEDULED_TASK_TYPE).toBe('layered-intelligence');
+ expect(LI_TASK_TYPE).toBe('self-improve:layered-intelligence');
+ expect(LI_TASK_TYPE).not.toBe(LI_SCHEDULED_TASK_TYPE);
+ });
});
describe('readLiTaskMetrics (#2700)', () => {