Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix([issue-2620]): stop LI re-reasoning completed PLAN items and mis-counting closes as merged
[._.]/ Adam Eivy96fd160baf9c
diff --git a/server/services/autonomousJobs/layeredIntelligenceHooks.js b/server/services/autonomousJobs/layeredIntelligenceHooks.jsindex e3daa9a533..02ae0b534c 100644--- a/server/services/autonomousJobs/layeredIntelligenceHooks.js+++ b/server/services/autonomousJobs/layeredIntelligenceHooks.js@@ -214,7 +214,8 @@ async function readIssues({ filer, forgeCli, cwd, jira, config }) {const planContent = await tryReadFile(join(cwd, 'PLAN.md'))// extractPlanSlugs preserves each tag's checkbox state ({ slug, state }): a// `- [x]` item reads 'closed' (with no closedAt) so the outcome loop can- // reconcile it and it falls out of the dedup window, while `- [ ]` stays open.+ // reconcile it, and it stays PERMANENTLY within the dedup window — a completed+ // plan item never needs re-proposal (#2620) — while `- [ ]` stays open.existingIssues = extractPlanSlugs(planContent || '')}return { openIssues, existingIssues, trackerReadFailed }diff --git a/server/services/autonomousJobs/layeredIntelligenceHooks.test.js b/server/services/autonomousJobs/layeredIntelligenceHooks.test.jsindex fbc6dbdd85..ff54ceee6d 100644--- a/server/services/autonomousJobs/layeredIntelligenceHooks.test.js+++ b/server/services/autonomousJobs/layeredIntelligenceHooks.test.js@@ -318,15 +318,17 @@ describe('processTaskOutput', () => {});it('reports a re-proposed already-tracked PLAN slug as duplicate without resetting its outcome (#2435)', async () => {- // A checked `- [x]` item passes the dedup guard (re-proposable), but- // appendProposalToPlan writes nothing (tag already present) and returns- // duplicate — the hook must NOT report `filed` or record a fresh outcome.+ // Since #2620 a checked `- [x]` item stays within the dedup window, so this+ // path normally never reaches the append. Belt-and-suspenders: should the+ // dedup guard ever miss, appendProposalToPlan writes nothing (tag already+ // present) and returns duplicate — the hook must NOT report `filed` or+ // record a fresh outcome.resolveAppWorkTracker.mockResolvedValue({ resolved: 'plan', forge: null });li.validateReasonerResponse.mockReturnValue({proposal: { scope: 'app-improvement', slug: 'add-metrics', title: 'Add metrics', body: 'do it' },pause: null});- li.isProposalDuplicate.mockReturnValue(false); // out of dedup window (checked, no closedAt)+ li.isProposalDuplicate.mockReturnValue(false); // simulate a dedup-guard missli.appendProposalToPlan.mockResolvedValue({ success: true, duplicate: true });li.getEffectiveConfig.mockReturnValue({ allowedScopes: ['app-improvement'], sources: { outcomes: true } });const out = await processTaskOutput({ appId: 'app-1', success: true, payload: { proposal: {} } });diff --git a/server/services/layeredIntelligence.js b/server/services/layeredIntelligence.jsindex 33d00af3d5..930fb47a9c 100644--- a/server/services/layeredIntelligence.js+++ b/server/services/layeredIntelligence.js@@ -207,15 +207,20 @@ export function normalizeSlug(slug) {/*** Whether an existing tracker issue is still within the dedup suppression window:- * OPEN, or CLOSED within CLOSED_SUPPRESSION_MS. A closed-long-ago (or unknown- * close time, treated as long ago) issue falls out of the window so its work can- * be re-proposed. Shared by both the slug dedup and the semantic dedup so the two+ * OPEN, or CLOSED within CLOSED_SUPPRESSION_MS. Only a closed-long-ago issue+ * falls out of the window so its work can be re-proposed. A closed issue with a+ * missing/unparseable `closedAt` is PERMANENTLY in-window (suppressed): the only+ * producer of that shape is a checked `- [x]` PLAN.md item (checkboxes carry no+ * timestamp), and a completed plan item never needs re-proposal — treating it as+ * "closed long ago" made the reasoner re-propose every done item on every run+ * (#2620). Shared by both the slug dedup and the semantic dedup so the two* guards agree on which issues still count.*/export function isIssueWithinDedupWindow(issue, now = Date.now()) {if ((issue?.state || '').toLowerCase() === 'open') return true;const closedAt = issue?.closedAt ? Date.parse(issue.closedAt) : NaN;- return Number.isFinite(closedAt) && now - closedAt <= CLOSED_SUPPRESSION_MS;+ if (!Number.isFinite(closedAt)) return true;+ return now - closedAt <= CLOSED_SUPPRESSION_MS;}/**@@ -425,17 +430,24 @@ export function trackerSupportsPause(resolved) {* Derive a resolved outcome for a filed proposal from its live tracker issue.* Pure — the reconciler feeds it a `{ state, stateReason, closedAt }` issue:* - still open (or unknown state) → null (unresolved)+ * - closed as "completed" → 'merged'* - closed as "not planned" → 'rejected'- * - closed for any other reason → 'merged' (the common auto-close-on-merge- * path; glab/jira report no stateReason)- * GitHub reports `stateReason` ('completed' | 'not_planned' | 'reopened'); other- * forges omit it, so a bare closed issue reads as merged.+ * - closed with any OTHER reason → 'abandoned' (duplicate/stale/etc. —+ * counting these as merged inflated the+ * merge-rate calibration signal, #2620)+ * - closed with NO reason → 'merged' (graceful fallback: glab/jira+ * and the plan filer report no stateReason,+ * and their common close path IS a merge —+ * absent ≠ other, per the sentinel rule)+ * GitHub reports `stateReason` ('completed' | 'not_planned' | 'reopened' | …);+ * other trackers omit it, so a bare closed issue reads as merged.*/export function deriveOutcome(issue) {if ((issue?.state || '').toLowerCase() !== 'closed') return null;const reason = (issue?.stateReason || '').toLowerCase().replace(/[\s-]+/g, '_');if (reason === 'not_planned') return 'rejected';- return 'merged';+ if (reason === '' || reason === 'completed') return 'merged';+ return 'abandoned';}/**@@ -1113,8 +1125,8 @@ export async function appendProposalToPlan({ repoPath, appName, slug, title, bod* checkbox stays `open` (still tracked/suppressed) rather than collapsing to* `closed` — a missing checkbox must not silently make an item re-proposable.* A `closed` item carries no `closedAt` (PLAN.md checkboxes have no timestamp),- * which `isIssueWithinDedupWindow` already treats as out-of-window → the item- * becomes re-proposable, exactly the desired behavior for a completed proposal.+ * which `isIssueWithinDedupWindow` treats as permanently in-window → a completed+ * proposal stays suppressed forever instead of being re-reasoned every run (#2620).*/export function extractPlanSlugs(planContent) {if (typeof planContent !== 'string') return [];diff --git a/server/services/layeredIntelligence.test.js b/server/services/layeredIntelligence.test.jsindex 2601695132..26fdecab38 100644--- a/server/services/layeredIntelligence.test.js+++ b/server/services/layeredIntelligence.test.js@@ -214,13 +214,13 @@ describe('isProposalDuplicate (dedup)', () => {expect(isProposalDuplicate({ slug: 'add-metrics', existingIssues: existing, now })).toBe(false);});- it('ALLOWS re-file for a checked PLAN item (closed with no closedAt — #2435)', () => {- // A `- [x]` PLAN item reads as closed with no timestamp; it must fall OUT of- // the dedup window so a completed proposal can be re-proposed, while an- // unchecked `- [ ]` item (state: 'open') stays suppressed.+ 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(false);+ expect(isProposalDuplicate({ slug: 'add-metrics', existingIssues: closed, now })).toBe(true);expect(isProposalDuplicate({ slug: 'add-metrics', existingIssues: open, now })).toBe(true);});@@ -517,9 +517,19 @@ describe('deriveOutcome', () => {expect(deriveOutcome({ state: 'closed', stateReason: 'not planned' })).toBe('rejected');});- it('maps any other closed reason (incl. glab/jira with no reason) to merged', () => {+ 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');});});@@ -546,6 +556,20 @@ describe('computeOutcomesReport', () => {expect(report).toContain('Common rejection reasons: too complex');});+ 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', outcomeReason: 'dup' }+ ];+ 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']);});@@ -576,7 +600,7 @@ describe('extractPlanSlugs', () => {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 make it re-proposable) — it stays open/suppressed.+ // (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' }]);@@ -1073,8 +1097,11 @@ describe('semantic dedup — pure helpers', () => {expect(isIssueWithinDedupWindow({ state: 'closed', closedAt: recent }, NOW)).toBe(true);expect(isIssueWithinDedupWindow({ state: 'closed', closedAt: old }, NOW)).toBe(false);});- it('closed with unknown close time falls out of window', () => {- expect(isIssueWithinDedupWindow({ state: 'closed' }, NOW)).toBe(false);+ it('closed with missing/unparseable close time stays permanently in-window (#2620)', () => {+ // The only producer of closed-without-closedAt is a checked `- [x]` PLAN+ // item — a completed proposal 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();diff --git a/server/services/layeredIntelligenceOutcomes.test.js b/server/services/layeredIntelligenceOutcomes.test.jsindex f9eee898ad..fcaa52a988 100644--- a/server/services/layeredIntelligenceOutcomes.test.js+++ b/server/services/layeredIntelligenceOutcomes.test.js@@ -109,20 +109,26 @@ describe('reconcileOutcomes', () => {it('resolves unresolved records from the fresh tracker state', async () => {await recordFiledProposal({ appId: 'app-1', slug: 'merged-one', scope: 'app-data-gap' }, store);await recordFiledProposal({ appId: 'app-1', slug: 'rejected-one', scope: 'app-improvement' }, store);+ await recordFiledProposal({ appId: 'app-1', slug: 'abandoned-one', scope: 'app-improvement' }, store);await recordFiledProposal({ appId: 'app-1', slug: 'still-open', scope: 'app-improvement' }, store);const existingIssues = [{ slug: 'merged-one', state: 'closed', stateReason: 'completed', closedAt: '2026-07-01T00:00:00Z' },{ slug: 'rejected-one', state: 'closed', stateReason: 'not_planned' },+ { slug: 'abandoned-one', state: 'closed', stateReason: 'duplicate', closedAt: '2026-07-02T00:00:00Z' },{ slug: 'still-open', state: 'open' }];const updated = await reconcileOutcomes({ appId: 'app-1', existingIssues }, store);- expect(updated).toBe(2);+ expect(updated).toBe(3);const byslug = Object.fromEntries((await listOutcomes({ appId: 'app-1' }, store)).map(r => [r.slug, r]));expect(byslug['merged-one'].outcome).toBe('merged');expect(byslug['merged-one'].outcomeAt).toBe('2026-07-01T00:00:00Z');expect(byslug['rejected-one'].outcome).toBe('rejected');+ // A close with an unrecognized present reason persists as abandoned (#2620) —+ // it must not inflate the merged count the reasoner calibrates against.+ expect(byslug['abandoned-one'].outcome).toBe('abandoned');+ expect(byslug['abandoned-one'].outcomeReason).toBe('duplicate');expect(byslug['still-open'].outcome).toBeNull();});