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

address review (claude): drain manual re-issue via a single dequeue

Adam Eivy2058127864c3
diff --git a/server/services/cos.js b/server/services/cos.js
index 5e5cb75ef1..052c89affb 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -1253,10 +1253,12 @@ async function refillPerpetualForCompletedAgent(agent) {
if (plan.lane === 'onDemand') {
// Manual drain: gate on the master Improve flag (what the manual trigger
// itself uses), NOT canQueueImprovementTasks, and re-issue an on-demand
- // request. The completion handler's subsequent dequeueNextTask({ ignoreTaskId })
- // processes it dedup-safely against the still-`in_progress` completing task.
+ // request. `emit: false` — the completion handler's subsequent
+ // dequeueNextTask({ ignoreTaskId }) drains the re-issue dedup-safely against
+ // the still-`in_progress` completing task; letting triggerOnDemandTask emit
+ // its event would fire a redundant SECOND dequeue of the same request.
if (!isImprovementEnabled(state)) return;
- await taskScheduleMod.triggerOnDemandTask(plan.taskType, plan.appId);
+ await taskScheduleMod.triggerOnDemandTask(plan.taskType, plan.appId, { emit: false });
return;
}
diff --git a/server/services/cos.test.js b/server/services/cos.test.js
index 51543b046e..15d90a41f1 100644
--- a/server/services/cos.test.js
+++ b/server/services/cos.test.js
@@ -1369,10 +1369,10 @@ describe('cos.js source — agent:completed triggers perpetual refill', () => {
expect(/plan\.lane === 'onDemand'/.test(fnSlice), 'refill must branch on the on-demand lane').toBe(true);
const improveGateIdx = fnSlice.indexOf('if (!isImprovementEnabled(state)) return;');
- const triggerIdx = fnSlice.indexOf('triggerOnDemandTask(plan.taskType, plan.appId)');
+ const triggerIdx = fnSlice.indexOf('triggerOnDemandTask(plan.taskType, plan.appId, { emit: false })');
const queueIdx = fnSlice.indexOf('queueEligibleImprovementTasks(state, cosTaskData');
expect(improveGateIdx, 'manual lane must gate on isImprovementEnabled').toBeGreaterThan(-1);
- expect(triggerIdx, 'manual lane must re-issue via triggerOnDemandTask(plan.taskType, plan.appId)').toBeGreaterThan(-1);
+ expect(triggerIdx, 'manual lane must re-issue via triggerOnDemandTask(plan.taskType, plan.appId, { emit: false }) — emit:false avoids a redundant second dequeue').toBeGreaterThan(-1);
expect(queueIdx, 'scheduled queue lane must still exist').toBeGreaterThan(-1);
// Improve gate precedes the re-issue; the manual lane returns before the queue lane.
expect(improveGateIdx).toBeLessThan(triggerIdx);
diff --git a/server/services/taskSchedule.js b/server/services/taskSchedule.js
index 46d7786a51..c087782d3d 100644
--- a/server/services/taskSchedule.js
+++ b/server/services/taskSchedule.js
@@ -1582,7 +1582,7 @@ export async function deleteTemplateTask(templateId) {
// On-Demand Requests
// ============================================================
-export async function triggerOnDemandTask(taskType, appId = null) {
+export async function triggerOnDemandTask(taskType, appId = null, { emit = true } = {}) {
const schedule = await loadSchedule();
// Cheap per-task-type check first; the master-flag check pays a state.json read.
@@ -1615,7 +1615,14 @@ export async function triggerOnDemandTask(taskType, appId = null) {
await saveSchedule(schedule);
emitLog('info', `On-demand task requested: ${taskType}`, { appId }, '📅 TaskSchedule');
- cosEvents.emit('task:on-demand-requested', request);
+ // The event's only consumer is a `dequeueNextTask()` trigger (cos.js). Callers
+ // that already own their own dequeue (the perpetual drain-on-completion refill,
+ // which enqueues a re-issue and then drains it via its OWN
+ // dequeueNextTask({ ignoreTaskId })) pass `emit: false` to avoid a redundant
+ // SECOND dequeue of the same request — which would re-run recordExecution + the
+ // work-detector probe. The manual "Run Now" route keeps the default so its
+ // request is picked up promptly.
+ if (emit) cosEvents.emit('task:on-demand-requested', request);
return request;
}