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: isolate perpetual refill queue defaults (#6766)

Adam Eivya3dd96d5868b
diff --git a/server/services/cos.js b/server/services/cos.js
index 3306be32d3..167ee51760 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -1512,14 +1512,12 @@ async function refillPerpetualForCompletedAgent(agent) {
emitLog('debug', `Perpetual refill for ${plan.taskType} skipped: parked until its recheck cadence`, { appId: plan.appId });
return;
}
- // `origin: 'refill'` — this re-issue borrows the on-demand LANE but is not a
- // human pressing Run, so the drain engines must leave the park, the
- // convergence signature, and the dispatch counter intact. Without it the
- // refill wipes its own convergence state on every hop and the drain never
- // parks while one actionable item remains.
- await taskScheduleMod.triggerOnDemandTask(plan.taskType, plan.appId, {
- emit: false, origin: taskScheduleMod.ON_DEMAND_ORIGINS.REFILL
- });
+ // `queuePerpetualRefill` stamps the automated `origin: 'refill'` pair — this
+ // re-issue borrows the on-demand LANE but is not a human pressing Run, so
+ // the drain engines must leave the park, convergence signature, and dispatch
+ // counter intact. Without that pair the refill wipes its own convergence
+ // state on every hop and the drain never parks while one actionable item remains.
+ await taskScheduleMod.queuePerpetualRefill(plan.taskType, plan.appId);
return;
}
diff --git a/server/services/cos.test.js b/server/services/cos.test.js
index 852204f7c9..bf6e79979b 100644
--- a/server/services/cos.test.js
+++ b/server/services/cos.test.js
@@ -2320,7 +2320,7 @@ describe('cos.js source — agent:completed triggers perpetual refill', () => {
).toBe(true);
});
- it('a MANUAL (on-demand) drain continues via triggerOnDemandTask under isImprovementEnabled, returning before the auto-run queue lane', () => {
+ it('a MANUAL (on-demand) drain continues via queuePerpetualRefill under isImprovementEnabled, returning before the auto-run queue lane', () => {
// The bug fixed here: a "Run Now" perpetual drain is allowed to START in a
// posture where canQueueImprovementTasks is false (auto-run off/dry-run or
// idle-review off), but the scheduled queue lane refuses to continue it —
@@ -2339,14 +2339,14 @@ 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;');
- // `emit: false` avoids a redundant second dequeue; `origin: REFILL` marks the
- // re-issue as automated so the on-demand engines do NOT clear the drain's park /
- // convergence signature / dispatch counter on its behalf.
- const triggerMatch = /triggerOnDemandTask\(plan\.taskType, plan\.appId, \{\s*emit: false, origin: taskScheduleMod\.ON_DEMAND_ORIGINS\.REFILL\s*\}\)/.exec(fnSlice);
+ // queuePerpetualRefill owns the non-emitting REFILL pair so the on-demand
+ // engines do NOT clear the drain's park / convergence signature / dispatch
+ // counter on its behalf.
+ const triggerMatch = /queuePerpetualRefill\(plan\.taskType, plan\.appId\)/.exec(fnSlice);
const triggerIdx = triggerMatch ? triggerMatch.index : -1;
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, { emit: false, origin: ON_DEMAND_ORIGINS.REFILL })').toBeGreaterThan(-1);
+ expect(triggerIdx, 'manual lane must re-issue via queuePerpetualRefill(plan.taskType, plan.appId)').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);
@@ -2381,7 +2381,7 @@ describe('cos.js source — agent:completed triggers perpetual refill', () => {
const fnIdx = COS_SRC.indexOf('async function refillPerpetualForCompletedAgent');
const fnSlice = COS_SRC.slice(fnIdx, fnIdx + 4600);
const parkIdx = fnSlice.indexOf('isPerpetualParkActive(plan.taskType, plan.appId)');
- const triggerIdx = fnSlice.indexOf('triggerOnDemandTask(plan.taskType, plan.appId');
+ const triggerIdx = fnSlice.indexOf('queuePerpetualRefill(plan.taskType, plan.appId');
expect(parkIdx, 'refill must consult the type+app park before re-issuing').toBeGreaterThan(-1);
expect(parkIdx).toBeLessThan(triggerIdx);
const returnAfterPark = fnSlice.indexOf('return;', parkIdx);
diff --git a/server/services/cosTaskGenerator.test.js b/server/services/cosTaskGenerator.test.js
index 0cafdee21d..bd2b8a0ef5 100644
--- a/server/services/cosTaskGenerator.test.js
+++ b/server/services/cosTaskGenerator.test.js
@@ -1725,7 +1725,7 @@ describe('pr-reviewer security preflight wiring', () => {
*/
describe('automated drain refills do not clear their own convergence brakes', () => {
it('the refill stamps origin: refill', () => {
- expect(COS_SRC).toMatch(/triggerOnDemandTask\(plan\.taskType, plan\.appId, \{\s*emit: false, origin: taskScheduleMod\.ON_DEMAND_ORIGINS\.REFILL\s*\}\)/);
+ expect(COS_SRC).toMatch(/queuePerpetualRefill\(plan\.taskType, plan\.appId\)/);
});
// A drain path calling the reset primitives directly is the exact regression,
diff --git a/server/services/taskSchedule.js b/server/services/taskSchedule.js
index c1b1576f1e..a7e3dbdf65 100644
--- a/server/services/taskSchedule.js
+++ b/server/services/taskSchedule.js
@@ -1190,6 +1190,19 @@ export async function triggerOnDemandTask(taskType, appId = null, {
return request;
}
+/**
+ * Queue the next item in a perpetual drain without treating it as a human
+ * pressing Run. The two options are an inseparable policy pair: the refill's
+ * completion handler owns dequeue, and the automated origin must preserve the
+ * drain's park, convergence signature, and dispatch counter.
+ */
+export async function queuePerpetualRefill(taskType, appId) {
+ return triggerOnDemandTask(taskType, appId, {
+ emit: false,
+ origin: ON_DEMAND_ORIGINS.REFILL,
+ });
+}
+
export async function getOnDemandRequests() {
const schedule = await loadSchedule();
const featureEnabled = createFeatureGate();
diff --git a/server/services/taskSchedule.test.js b/server/services/taskSchedule.test.js
index 11017b56e8..cb7252e449 100644
--- a/server/services/taskSchedule.test.js
+++ b/server/services/taskSchedule.test.js
@@ -140,6 +140,7 @@ import {
getPerpetualDrainState,
recordPerpetualDispatch,
applyOnDemandRunResets,
+ queuePerpetualRefill,
isRefillRequest,
ON_DEMAND_ORIGINS,
recordTaskTypeFailure,
@@ -2127,6 +2128,20 @@ describe('taskSchedule', () => {
expect(refill.origin).toBe(ON_DEMAND_ORIGINS.REFILL)
})
+ it('queues a perpetual refill with its non-emitting automated origin', async () => {
+ mockSchedule({ tasks: { 'branch-reconcile': { type: 'on-demand', perpetual: true, enabled: true } } })
+
+ const refill = await queuePerpetualRefill('branch-reconcile', 'app-1')
+
+ expect(refill).toMatchObject({
+ taskType: 'branch-reconcile',
+ appId: 'app-1',
+ origin: ON_DEMAND_ORIGINS.REFILL,
+ })
+ expect(cosEvents.emit).not.toHaveBeenCalled()
+ expect(recordUserAction).not.toHaveBeenCalled()
+ })
+
// Operator-action ledger (#5594). Only a human pressing Run Now is an
// operator action; the perpetual drain re-issues itself through this same
// lane, and logging that would fill the ledger with events nobody performed.