Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix duplicate scheduled improvement runs: dedup raw tasks by metadata.app

[._.]/ Adam Eivy0f3aee7890d5
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index ffea82f80f..faa647bce2 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -69,6 +69,7 @@
## Fixed
+- **Scheduled improvement tasks no longer run twice.** When the periodic evaluation loop and the improvement-check timer fired close together, each took its own snapshot of the pending-task list, both found no matching task, and both queued an identical managed-app improvement (e.g. two overlapping `[Improvement: PortOS] Performance Analysis` runs). The `addTask` duplicate backstop that should have caught the second was silently disabled for these tasks: it read the candidate app only from top-level `taskData.app`, but queue-path improvement tasks arrive pre-built (`raw: true`) with the app in `metadata.app` — so the app-scoped dedup compared `null` against the existing task's `portos` and never matched. `addTask` now reads the candidate app from `taskData.metadata?.app` too, so the second concurrent queue is correctly rejected. (`server/services/cosTaskStore.js`)
- **PM2 standardization no longer overwrites a PM2 config PortOS didn't generate.** Standardizing an app used to always regenerate `ecosystem.config.cjs`, silently replacing a hand-written one — losing its custom ports and settings. A config PortOS didn't generate is now preserved by default (and its ports are left untouched in `.env`/Vite too); PortOS only regenerates its own previously-generated config. To deliberately replace a custom config, opt in with the overwrite flag when standardizing.
- **CoS TUI agents running `/do:release`/`/do:pr`/`/do:rpr` no longer get prematurely reaped as "done" while a reviewer is still working** — the idle-timeout reaper only special-cased the `/do:next --swarm` merge-queue wait (#2074); a slow multi-reviewer pass (e.g. codex reading a large diff) could go silent past the default 3-minute idle window and get finalized as a false `idle-complete` success before the release ever reached the merge gate, leaving the PR open with the task dashboard showing "completed" (observed on agent-61508f36, PR #2084, which sat open+unmerged for hours). Added a `createReviewLoopTracker` (`server/lib/tuiHandshake.js`) that latches on multi-reviewer-loop chrome — the review-plan status line (agent list in brackets), a review-pass counter (N of M), or the aggregate report heading — and extends the idle reaper's grace to 15 minutes while active — mirroring the merge-queue fix exactly, including surfacing a timeout as a needs-manual-finish failure (`reason: 'review-loop-idle-timeout'`) instead of a silent success. (Deliberately worded here without reproducing the literal trigger strings — this changelog entry itself would otherwise false-latch the tracker once a future release run reads it.)
- **[issue-2107] A song scored in a minor key now shows the correct key signature.** Typing `key: D minor` (or `Em`, `F# minor`, `Bbm`, …) into a song's score header used to render the signature for the *parallel major* instead — e.g. "D minor" showed D major's 2 sharps on a staff full of natural notes. Minor keys now resolve to their relative major's signature (D minor → F, 1 flat), and the reference-audio-to-score feature spells accidentals on the correct side for minor keys too. Plain major keys ("D", "D major", "Dmaj") are unaffected.
diff --git a/server/services/cosTaskStore.js b/server/services/cosTaskStore.js
index 68f8e099d9..4ac0fa8bd6 100644
--- a/server/services/cosTaskStore.js
+++ b/server/services/cosTaskStore.js
@@ -199,7 +199,16 @@ export async function addTask(taskData, taskType = 'user', { raw = false, ignore
// tick. The completing task is about to become `completed`, so ignoring it is
// correct, not a dedup hole.
const normalizedDesc = firstLine(taskData.description).toLowerCase();
- const targetApp = taskData.app || null;
+ // The candidate's app can arrive two ways: non-raw tasks pass it top-level as
+ // `taskData.app` (used below to build `metadata.app`); raw tasks — the queue-path
+ // improvement tasks and on-demand generated tasks — arrive pre-built with the app
+ // already in `metadata.app` and NO top-level `app`. Read both, or the app-scoped
+ // dedup silently no-ops for raw managed-app tasks: `targetApp` would be `null` and
+ // never equal the existing task's `metadata.app`, so two concurrent
+ // `queueEligibleImprovementTasks` snapshots (the periodic evaluation + the
+ // improvement-check timer firing close together) each add an identical
+ // `[Improvement: PortOS] …` task, producing the overlapping duplicate runs.
+ const targetApp = taskData.app ?? taskData.metadata?.app ?? null;
const duplicate = tasks.find(t =>
t.id !== ignoreTaskId &&
(t.status === 'pending' || t.status === 'in_progress') &&
diff --git a/server/services/cosTaskStore.test.js b/server/services/cosTaskStore.test.js
index 2cbac698bf..a1620c8872 100644
--- a/server/services/cosTaskStore.test.js
+++ b/server/services/cosTaskStore.test.js
@@ -177,6 +177,38 @@ describe('cosTaskStore.addTask', () => {
expect(tasks).toHaveLength(2);
});
+ it('rejects a raw duplicate whose app lives in metadata.app (queue-path improvement tasks)', async () => {
+ // Queue-path improvement tasks (generateManagedAppImprovementTaskForType) arrive
+ // pre-built with `raw: true` and carry the app in `metadata.app`, NOT top-level
+ // `taskData.app`. Two concurrent queueEligibleImprovementTasks snapshots each add
+ // an identical `[Improvement: PortOS] …` task; the second must be rejected as a
+ // duplicate (regression for the overlapping-duplicate-runs bug).
+ const rawTask = (id) => ({
+ id, description: '[Improvement: PortOS] Performance Analysis', status: 'pending',
+ priority: 'LOW', priorityValue: PRIORITY_VALUES.LOW, taskType: 'internal', autoApproved: true,
+ metadata: { app: 'portos', analysisType: 'performance' }
+ });
+ const first = await addTask(rawTask('sys-perf-1'), 'internal', { raw: true });
+ expect(first.duplicate).toBeUndefined();
+ const second = await addTask(rawTask('sys-perf-2'), 'internal', { raw: true });
+ expect(second.duplicate).toBe(true);
+ const { tasks } = await getCosTasks();
+ expect(tasks.filter(t => firstLine(t.description) === '[Improvement: PortOS] Performance Analysis')).toHaveLength(1);
+ });
+
+ it('does NOT treat raw tasks with the same description against different metadata.app as duplicates', async () => {
+ const rawTask = (id, app) => ({
+ id, description: 'shared raw', status: 'pending',
+ priority: 'LOW', priorityValue: PRIORITY_VALUES.LOW, taskType: 'internal', autoApproved: true,
+ metadata: { app }
+ });
+ await addTask(rawTask('sys-a', 'portos'), 'internal', { raw: true });
+ const second = await addTask(rawTask('sys-b', 'bookloom'), 'internal', { raw: true });
+ expect(second.duplicate).toBeUndefined();
+ const { tasks } = await getCosTasks();
+ expect(tasks.filter(t => firstLine(t.description) === 'shared raw')).toHaveLength(2);
+ });
+
it('ignoreTaskId excludes one in-flight task from the dedup scan (perpetual drain-on-completion)', async () => {
// The just-completed perpetual task is still in_progress on disk when the
// refill re-queues an identical first-line for the same app. Passing its id
@@ -438,13 +470,14 @@ describe('addTask — first-line dedup (source guards)', () => {
it('addTask scopes dedup by metadata.app', () => {
// Same description against two different apps must NOT trip the duplicate
- // check — the dedup predicate compares the candidate's `metadata?.app` (or
- // null) against the new task's `taskData.app`.
+ // check — the dedup predicate compares the existing task's `metadata?.app`
+ // (or null) against the candidate's app, which for raw tasks lives in
+ // `taskData.metadata?.app` (top-level `taskData.app` is non-raw only).
const start = STORE_SRC.indexOf('export async function addTask');
const end = STORE_SRC.indexOf('export async function', start + 1);
const fnBody = STORE_SRC.slice(start, end === -1 ? undefined : end);
expect(fnBody).toMatch(/t\.metadata\?\.app\s*\|\|\s*null/);
- expect(fnBody).toMatch(/taskData\.app\s*\|\|\s*null/);
+ expect(fnBody).toMatch(/taskData\.metadata\?\.app/);
});
});