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

perf([cos-on-demand-mark-app-review-started-dedupe]): mark each app review-started once per CoS evaluation cycle

[._.]/ Adam Eivyc45799aeb8f0
diff --git a/server/services/cos.js b/server/services/cos.js
index 7d4b6a1535..748faef33c 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -796,6 +796,9 @@ export async function evaluateTasks() {
const liveSchedule = await taskSchedule.loadSchedule();
const onDemandRequests = Array.isArray(liveSchedule?.onDemandRequests) ? liveSchedule.onDemandRequests : [];
+ // Track apps already marked review-started this cycle so multiple on-demand
+ // requests for the same app don't each rewrite its activity record.
+ const reviewStartedApps = new Set();
if (onDemandRequests.length > 0 && tasksToSpawn.length < availableSlots) {
for (const request of onDemandRequests) {
if (tasksToSpawn.length >= availableSlots) break;
@@ -831,7 +834,10 @@ export async function evaluateTasks() {
if (targetApp) {
emitLog('info', `Processing on-demand improvement: ${request.taskType} for ${targetApp.name}`, { requestId: request.id, appId: targetApp.id });
- await markAppReviewStarted(targetApp.id, `on-demand-${Date.now()}`);
+ if (!reviewStartedApps.has(targetApp.id)) {
+ await markAppReviewStarted(targetApp.id, `on-demand-${Date.now()}`);
+ reviewStartedApps.add(targetApp.id);
+ }
await taskSchedule.recordExecution(`task:${request.taskType}`, targetApp.id);
task = await generateManagedAppImprovementTaskForType(request.taskType, targetApp, state, { skipPreconditions: true });
} else {
diff --git a/server/services/cos.test.js b/server/services/cos.test.js
index 8a1e0a0161..6fa86c5814 100644
--- a/server/services/cos.test.js
+++ b/server/services/cos.test.js
@@ -649,6 +649,24 @@ describe('cos.js source — priority + capacity invariants', () => {
expect(evalFn).toMatch(/tasksToSpawn\.length\s*===\s*0\s*&&\s*state\.config\.idleReviewEnabled/);
});
+ it('on-demand loop dedupes markAppReviewStarted per app via reviewStartedApps set', () => {
+ // Multiple on-demand requests targeting the same app should mark its
+ // activity record review-started only once per evaluation cycle — without
+ // the guard, each request rewrites the same record. Pin (a) the set is
+ // declared and (b) markAppReviewStarted is gated on it.
+ const fnStart = COS_SRC.indexOf('export async function evaluateTasks');
+ const fnBody = extractFnBody(COS_SRC, fnStart);
+
+ expect(
+ fnBody,
+ 'evaluateTasks must declare a reviewStartedApps set to dedupe per-app marks'
+ ).toMatch(/const\s+reviewStartedApps\s*=\s*new\s+Set\(/);
+ expect(
+ fnBody,
+ 'markAppReviewStarted must be gated on !reviewStartedApps.has(targetApp.id)'
+ ).toMatch(/if\s*\(\s*!\s*reviewStartedApps\.has\(\s*targetApp\.id\s*\)\s*\)/);
+ });
+
it('queueEligibleImprovementTasks routes through generateManagedAppImprovementTaskForType', () => {
// Regression guard: a 2026-05-21 incident saw two `plan-task` agents both
// open PRs for the same PLAN.md slug because the queue path was writing