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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
feat(issue-2760): steer LI proposals away from its chronically-failing scopes
[._.]/ Adam Eivy81677edf1bda
diff --git a/server/services/layeredIntelligence.js b/server/services/layeredIntelligence.jsindex ebffd08bb0..ca8580562f 100644--- a/server/services/layeredIntelligence.js+++ b/server/services/layeredIntelligence.js@@ -166,6 +166,32 @@ export const LI_DEGRADED_MIN_SAMPLE = 4;// to merged for trackers that report no stateReason.export const PROPOSAL_OUTCOMES = ['merged', 'rejected', 'abandoned'];+// Scope-awareness thresholds (#2760). LI's own execution data shows several CoS+// task-type scopes it consistently fails at (e.g. self-improve:layered-intelligence,+// branch-reconcile, accessibility all sit at 0%) while others succeed reliably+// (plan-task, test-coverage, performance at ~100%). Since an LI proposal is later+// EXECUTED as a CoS task, proposing work that maps to a chronically-failing scope is+// systematic waste. These bound a deterministic, self-clearing classifier+// (computeScopeAwareness) that surfaces the avoid/prefer split to the reasoner so it+// can steer proposals toward scopes that actually execute.+//+// A scope is "avoid" when its lifetime success rate is below AVOID and it has enough+// completed runs to be evidence; "prefer" when at/above PREFER with the same sample+// floor. Reusing the degraded-execution boundary (50%) for AVOID keeps LI's two+// success signals — its own loop health and per-scope executability — on the same+// coin-flip line. Because the classification is recomputed from fresh learning.json+// every run, an "avoid" scope self-clears the moment its rate recovers past AVOID —+// no persisted avoid-list to go stale (the issue's "dynamic adjustment" requirement).+export const SCOPE_AVOID_SUCCESS_THRESHOLD = LI_DEGRADED_SUCCESS_THRESHOLD; // < 50% → avoid+export const SCOPE_PREFER_SUCCESS_THRESHOLD = 75; // >= 75% → prefer++// Minimum completed runs before a scope's rate is trusted for avoid/prefer, mirroring+// LI_DEGRADED_MIN_SAMPLE's rationale (0-of-1 and 0-of-50 are both "0%", only the+// second is evidence). Set to 3 per #2760 — one below the degraded floor because the+// prompt guidance is advisory (it steers the reasoner, it does not hard-suppress a+// proposal), so a slightly lower bar to surface the signal is acceptable.+export const SCOPE_AWARENESS_MIN_SAMPLE = 3;+/*** The default per-app config. PortOS (isPortos) additionally gets the meta/self* scopes so the loop can extend itself; every other app is capped at its own@@ -896,6 +922,51 @@ export function computeSelfEvalSummary({* block with calibration guidance when non-empty (#2428). `selfEvalReport` (from* computeSelfEvalSummary) is injected as a `liSelfEval` block the same way (#2700).*/+/**+ * Scope-awareness report (#2760). Classifies each CoS task-type scope by its lifetime+ * execution success rate into "avoid" (chronically failing) and "prefer" (reliably+ * succeeding), then renders guidance the reasoner can use to steer a proposal toward+ * work that will actually execute. Pure — takes the already-parsed per-type metrics+ * map (the `summary` gatherSources builds from learning.byTaskType) and returns a+ * report string, or '' when no scope has enough runs to qualify either way.+ *+ * `metricsByType[type] = { lifetimeSuccessRate: number|null, lifetimeCompleted: number, ... }`.+ * The steering is advisory, not a hard gate: LI still MAY propose in an avoid scope+ * when it is genuinely the highest-value work, but it must justify doing so. Because+ * the input is re-read every run, the split is dynamic and self-clearing — a scope+ * leaves "avoid" automatically once its rate climbs back past the threshold.+ */+export function computeScopeAwareness({ metricsByType = {} } = {}) {+ const avoid = [];+ const prefer = [];+ for (const [type, m] of Object.entries(metricsByType || {})) {+ const rate = m?.lifetimeSuccessRate;+ const n = m?.lifetimeCompleted || 0;+ // Only a scope with enough evidence is classified; a NUMBER rate is required so a+ // never-run scope (null) neither avoids nor prefers — it defaults to neutral.+ if (typeof rate !== 'number' || n < SCOPE_AWARENESS_MIN_SAMPLE) continue;+ if (rate < SCOPE_AVOID_SUCCESS_THRESHOLD) avoid.push({ type, rate, n });+ else if (rate >= SCOPE_PREFER_SUCCESS_THRESHOLD) prefer.push({ type, rate, n });+ }+ if (!avoid.length && !prefer.length) return '';+ // Worst-first for avoid, best-first for prefer — the reasoner reads the sharpest+ // signal at the top of each list.+ avoid.sort((a, b) => a.rate - b.rate);+ prefer.sort((a, b) => b.rate - a.rate);+ const fmt = ({ type, rate, n }) => `- ${type}: ${Math.round(rate)}% success over ${n} runs`;+ const lines = [];+ if (avoid.length) {+ lines.push(`AVOID — these scopes' work has historically FAILED to execute (below ${SCOPE_AVOID_SUCCESS_THRESHOLD}% success):`);+ lines.push(avoid.map(fmt).join('\n'));+ }+ if (prefer.length) {+ if (lines.length) lines.push('');+ lines.push(`PREFER — these scopes execute reliably (at or above ${SCOPE_PREFER_SUCCESS_THRESHOLD}% success):`);+ lines.push(prefer.map(fmt).join('\n'));+ }+ return lines.join('\n');+}+export function buildPrompt({ app, config, sources = {}, openIssues = [], isPortos = false, outcomesReport = '', selfEvalReport = '' }) {const allowed = (config.allowedScopes || []).filter(s =>isScopeAllowed({ scope: s, allowedScopes: config.allowedScopes, isPortos })@@ -906,7 +977,7 @@ export function buildPrompt({ app, config, sources = {}, openIssues = [], isPort// under the committed backlog it refers to instead of drifting to wherever// object key order happens to put it.const sourceBlocks = Object.entries(sources)- .filter(([k]) => k !== 'plannedWork')+ .filter(([k]) => k !== 'plannedWork' && k !== 'scopeGuidance').filter(([, v]) => typeof v === 'string' && v.trim()).map(([k, v]) => `### ${k}\n${v.trim()}`).join('\n\n');@@ -953,6 +1024,15 @@ export function buildPrompt({ app, config, sources = {}, openIssues = [], isPort? `\n### liSelfEval\n${selfEvalReport.trim()}\n\nWeigh this against the sources above before you commit to a proposal. Filing nothing (proposal: null) is a legitimate, and sometimes the correct, outcome — a marginal issue costs the user triage time and lowers your merge rate further.\n`: '';+ // Scope-awareness (#2760): your proposal, once filed, is EXECUTED as a CoS task, so+ // the historical per-scope execution-success rates below predict whether the work+ // you propose will actually land. Steer toward PREFER scopes and away from AVOID+ // ones; if the highest-value work genuinely falls in an AVOID scope, you may still+ // propose it, but scope it narrowly and justify why it will execute this time.+ const scopeGuidanceBlock = (typeof sources.scopeGuidance === 'string' && sources.scopeGuidance.trim())+ ? `\n### liScopeAwareness\n${sources.scopeGuidance.trim()}\n\nYour proposals are executed as CoS tasks in these scopes. Favor work that maps to a PREFER scope; treat AVOID scopes as needing a narrow scope and an explicit justification (or a null proposal) rather than a default.\n`+ : '';+return `You are the Layered Intelligence reasoner for the app "${app.name}". Your job is to evaluate how THIS app is performing against its OWN goals and purpose${isPortos ? '' : ', not how well PortOS\'s tooling manages it'}. Decide the SINGLE highest-value improvement to propose this run (signal, not noise), grounded in the app's own goals and its own performance metrics (user success, KPIs, production telemetry). You never write code; you return structured JSON that a deterministic system files as ONE tracker issue.${handoffNote}${metricsGuidance}Rules & guidance from the operator:@@ -966,7 +1046,7 @@ ${openList}Gathered sources:${sourceBlocks || (plannedWorkBlock ? '(no other sources available — you may propose an app-data-gap to add telemetry)' : '(no sources available — you may propose an app-data-gap to add telemetry)')}-${plannedWorkBlock}${outcomesBlock}${selfEvalBlock}+${plannedWorkBlock}${outcomesBlock}${selfEvalBlock}${scopeGuidanceBlock}Respond with JSON only (no markdown fences):{"analysis": "brief reasoning summary",@@ -1222,6 +1302,14 @@ export async function gatherSources(app, config, { cosPath = PATHS.cos, trustShe};}out.cosMetrics = JSON.stringify(summary).slice(0, 4000);+ // Scope-awareness guidance (#2760): a deterministic avoid/prefer split derived+ // from the SAME per-type rates above, so the reasoner gets an interpreted signal+ // alongside the raw JSON instead of being asked to spot the pattern itself.+ // Rendered as its own prompt block (see buildPrompt), not a generic source dump.+ // Gated implicitly on the cosMetrics source being enabled — which is the+ // PortOS-vs-managed boundary these self-execution rates belong to.+ const scopeGuidance = computeScopeAwareness({ metricsByType: summary });+ if (scopeGuidance) out.scopeGuidance = scopeGuidance;}}for (const custom of src.custom || []) {diff --git a/server/services/layeredIntelligence.test.js b/server/services/layeredIntelligence.test.jsindex ed34b11337..cc0dc6f7d8 100644--- a/server/services/layeredIntelligence.test.js+++ b/server/services/layeredIntelligence.test.js@@ -42,6 +42,10 @@ import {rejectionReasonBySlug,LI_DEGRADED_SUCCESS_THRESHOLD,LI_DEGRADED_MIN_SAMPLE,+ computeScopeAwareness,+ SCOPE_AVOID_SUCCESS_THRESHOLD,+ SCOPE_PREFER_SUCCESS_THRESHOLD,+ SCOPE_AWARENESS_MIN_SAMPLE,PROPOSAL_OUTCOMES,extractPlanSlugs,appendProposalToPlan,@@ -455,9 +459,107 @@ describe('filerForTracker / trackerSupportsPause (dispatch)', () => {});});+describe('computeScopeAwareness (#2760)', () => {+ it('returns empty string when no scope clears the sample floor', () => {+ // Both under SCOPE_AWARENESS_MIN_SAMPLE (3) → neither classified.+ expect(computeScopeAwareness({ metricsByType: {+ 'plan-task': { lifetimeSuccessRate: 100, lifetimeCompleted: 2 },+ 'accessibility': { lifetimeSuccessRate: 0, lifetimeCompleted: 1 }+ } })).toBe('');+ });++ it('returns empty string for an empty / missing map', () => {+ expect(computeScopeAwareness({})).toBe('');+ expect(computeScopeAwareness()).toBe('');+ expect(computeScopeAwareness({ metricsByType: {} })).toBe('');+ });++ it('classifies a below-threshold scope with enough runs as AVOID', () => {+ const out = computeScopeAwareness({ metricsByType: {+ 'self-improve:layered-intelligence': { lifetimeSuccessRate: 0, lifetimeCompleted: 9 }+ } });+ expect(out).toContain('AVOID');+ expect(out).toContain('self-improve:layered-intelligence: 0% success over 9 runs');+ expect(out).not.toContain('PREFER');+ });++ it('classifies an at/above-threshold scope with enough runs as PREFER', () => {+ const out = computeScopeAwareness({ metricsByType: {+ 'plan-task': { lifetimeSuccessRate: 100, lifetimeCompleted: 10 }+ } });+ expect(out).toContain('PREFER');+ expect(out).toContain('plan-task: 100% success over 10 runs');+ expect(out).not.toContain('AVOID');+ });++ it('leaves a mid-band scope (between avoid and prefer) unclassified', () => {+ // 60% is >= AVOID (50) and < PREFER (75) → neither list.+ expect(computeScopeAwareness({ metricsByType: {+ 'feature-ideas': { lifetimeSuccessRate: 60, lifetimeCompleted: 8 }+ } })).toBe('');+ });++ it('ignores a never-run scope (null rate) even with a high completed count artifact', () => {+ expect(computeScopeAwareness({ metricsByType: {+ 'idle-review': { lifetimeSuccessRate: null, lifetimeCompleted: 12 }+ } })).toBe('');+ });++ it('sorts AVOID worst-first and PREFER best-first', () => {+ const out = computeScopeAwareness({ metricsByType: {+ 'branch-reconcile': { lifetimeSuccessRate: 20, lifetimeCompleted: 5 },+ 'accessibility': { lifetimeSuccessRate: 0, lifetimeCompleted: 4 },+ 'test-coverage': { lifetimeSuccessRate: 80, lifetimeCompleted: 5 },+ 'performance': { lifetimeSuccessRate: 100, lifetimeCompleted: 3 }+ } });+ // worst avoid (accessibility 0%) precedes the milder one (branch-reconcile 20%)+ expect(out.indexOf('accessibility')).toBeLessThan(out.indexOf('branch-reconcile'));+ // best prefer (performance 100%) precedes the milder one (test-coverage 80%)+ expect(out.indexOf('performance')).toBeLessThan(out.indexOf('test-coverage'));+ // AVOID section precedes PREFER section+ expect(out.indexOf('AVOID')).toBeLessThan(out.indexOf('PREFER'));+ });++ it('honors the exported thresholds at their exact boundaries', () => {+ // exactly PREFER threshold → prefer; exactly AVOID threshold → neither (< is strict)+ const atPrefer = computeScopeAwareness({ metricsByType: {+ x: { lifetimeSuccessRate: SCOPE_PREFER_SUCCESS_THRESHOLD, lifetimeCompleted: SCOPE_AWARENESS_MIN_SAMPLE }+ } });+ expect(atPrefer).toContain('PREFER');+ const atAvoidBoundary = computeScopeAwareness({ metricsByType: {+ x: { lifetimeSuccessRate: SCOPE_AVOID_SUCCESS_THRESHOLD, lifetimeCompleted: SCOPE_AWARENESS_MIN_SAMPLE }+ } });+ expect(atAvoidBoundary).toBe(''); // 50% is not < 50 and not >= 75+ });+});+describe('buildPrompt', () => {const app = { name: 'TestApp' };+ it('injects the scope-awareness block + steering guidance only when present (#2760)', () => {+ const base = { app, isPortos: true, config: { allowedScopes: ['loop-meta'], rules: '' } };+ const without = buildPrompt(base);+ expect(without).not.toContain('liScopeAwareness');+ const withGuidance = buildPrompt({+ ...base,+ sources: { scopeGuidance: 'AVOID — these scopes have historically FAILED:\n- branch-reconcile: 0% success over 4 runs' }+ });+ expect(withGuidance).toContain('### liScopeAwareness');+ expect(withGuidance).toContain('branch-reconcile: 0% success over 4 runs');+ expect(withGuidance).toContain('executed as CoS tasks');+ });++ it('does not render scopeGuidance as a generic source block', () => {+ const out = buildPrompt({+ app, isPortos: true,+ config: { allowedScopes: ['loop-meta'], rules: '' },+ sources: { scopeGuidance: 'PREFER — plan-task: 100% success over 10 runs' }+ });+ // rendered under its dedicated heading, never as "### scopeGuidance"+ expect(out).not.toContain('### scopeGuidance');+ expect(out).toContain('### liScopeAwareness');+ });+it('only offers meta/self scopes on PortOS', () => {const nonPortos = buildPrompt({app, isPortos: false,@@ -1835,6 +1937,39 @@ describe('gatherSources cosMetrics windowed rate (issue #2460)', () => {const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: false } }, { cosPath: dir });expect(out.cosMetrics).toBeUndefined();});++ it('emits scopeGuidance derived from the same per-type rates (#2760)', async () => {+ await writeLearning({+ byTaskType: {+ 'self-improve:layered-intelligence': { completed: 9, succeeded: 0, failed: 9, successRate: 0, recentOutcomes: [] },+ 'plan-task': { completed: 10, succeeded: 10, failed: 0, successRate: 100, recentOutcomes: [] },+ 'feature-ideas': { completed: 8, succeeded: 5, failed: 3, successRate: 63, recentOutcomes: [] } // mid-band → neither+ }+ });+ const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: true } }, { cosPath: dir });+ expect(out.scopeGuidance).toContain('AVOID');+ expect(out.scopeGuidance).toContain('self-improve:layered-intelligence: 0% success over 9 runs');+ expect(out.scopeGuidance).toContain('PREFER');+ expect(out.scopeGuidance).toContain('plan-task: 100% success over 10 runs');+ expect(out.scopeGuidance).not.toContain('feature-ideas');+ });++ it('omits scopeGuidance when no scope clears the sample floor (#2760)', async () => {+ await writeLearning({+ byTaskType: { 'plan-task': { completed: 2, succeeded: 2, failed: 0, successRate: 100, recentOutcomes: [] } }+ });+ const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: true } }, { cosPath: dir });+ expect(out.cosMetrics).toBeDefined(); // raw metrics still surfaced+ expect(out.scopeGuidance).toBeUndefined();+ });++ it('does not emit scopeGuidance when cosMetrics is off (#2760)', async () => {+ await writeLearning({+ byTaskType: { 'accessibility': { completed: 4, succeeded: 0, failed: 4, successRate: 0, recentOutcomes: [] } }+ });+ const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: false } }, { cosPath: dir });+ expect(out.scopeGuidance).toBeUndefined();+ });});// ---------------------------------------------------------------------------