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: evaluate every app's cron readiness against one tick clock before preparing any (#11755)
[._.]/ Adam Eivy37e23930cfc4
diff --git a/server/services/cosTaskGenerator.cronQueue.test.js b/server/services/cosTaskGenerator.cronQueue.test.jsindex ebf77001e1..29cc7dfa2a 100644--- a/server/services/cosTaskGenerator.cronQueue.test.js+++ b/server/services/cosTaskGenerator.cronQueue.test.js@@ -1,4 +1,4 @@-import { beforeEach, describe, expect, it, vi } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';vi.mock('./cosEvents.js', () => ({ cosEvents: { emit: vi.fn(), on: vi.fn() }, emitLog: vi.fn() }));vi.mock('./apps.js', async (original) => ({@@ -14,6 +14,7 @@ vi.mock('./taskSchedule.js', async (original) => ({import { queueEligibleImprovementTasks } from './cosTaskGenerator.js';import { getDueTasks, recordExecution } from './taskSchedule.js';import { loadAppActivity } from './appActivity.js';+import { getActiveApps } from './apps.js';import { cosEvents } from './cosEvents.js';const state = { config: { improvementEnabled: true, appReviewCooldownMs: 60_000 } };@@ -77,6 +78,40 @@ describe('scheduled managed-app queue admission', () => {expect(cosEvents.emit).not.toHaveBeenCalled();});+ describe('multi-app tick', () => {+ afterEach(() => {+ vi.useRealTimers();+ getActiveApps.mockResolvedValue([{ id: 'example-app', name: 'Example App' }]);+ });++ it('queues every app on a shared cron slot even when earlier preparation outlasts the minute, once per minute', async () => {+ const slotStart = new Date('2026-09-09T11:30:05Z').getTime();+ const inSlot = now => Math.floor(now / 60_000) === Math.floor(slotStart / 60_000);+ vi.useFakeTimers();+ vi.setSystemTime(slotStart);+ const apps = [{ id: 'app-1', name: 'App One' }, { id: 'app-2', name: 'App Two' }];+ getActiveApps.mockResolvedValue(apps);+ // Readiness is judged against the clock the caller supplies, as the real gate does.+ const lastRun = {};+ getDueTasks.mockImplementation(async (appId, { now = Date.now() } = {}) =>+ appId && inSlot(now) && !lastRun[appId] ? [dueCron('do-release')] : []);+ recordExecution.mockImplementation(async (_key, appId) => { lastRun[appId] = Date.now(); });+ prepareTask.mockImplementation(async (taskType, app) => {+ vi.advanceTimersByTime(90_000);+ return { task: { description: taskType, metadata: { app: app.id, analysisType: taskType } }, pendingPerpetualDispatch: null };+ });++ await run();+ expect(persistTask.mock.calls.map(([task]) => task.metadata.app)).toEqual(['app-1', 'app-2']);+ expect(getDueTasks.mock.calls.filter(([id]) => id).every(([, opts]) => opts.now === slotStart)).toBe(true);++ // A later tick must not re-queue an occurrence that was already recorded.+ persistTask.mockClear();+ await run();+ expect(persistTask).not.toHaveBeenCalled();+ });+ });+it('retains one background drain per app and holds it behind unrelated work', async () => {due = [drain, { ...drain, taskType: 'observability' }];await run([queuedTask('existing-work')]);diff --git a/server/services/cosTaskGenerator.js b/server/services/cosTaskGenerator.jsindex c579f667b3..87f204f0be 100644--- a/server/services/cosTaskGenerator.js+++ b/server/services/cosTaskGenerator.js@@ -1249,6 +1249,9 @@ export async function queueEligibleImprovementTasks(state, cosTaskData, {const { existingTaskTypes, appsWithPendingImprovement, blockedTaskTypes, appsWithBlockedImprovement } =buildImprovementDedupSets(existingTasks, { ignoreTaskId });+ // One clock for the whole tick: cron eligibility lasts a single minute, and+ // preparing earlier apps can take longer than that (#11755).+ const tickNow = Date.now();let queued = 0;// Install-wide task types have no managed-app target: their cadence is@@ -1262,7 +1265,7 @@ export async function queueEligibleImprovementTasks(state, cosTaskData, {// analysis type, matching buildImprovementDedupSets for globally-scoped tasks// and ensuring one pending/blocked task suppresses only its own duplicate.queued += await queueDueInstallWideImprovementTasks({- dueTasks: await getDueTasks(), state, taskSchedule, existingTaskTypes,+ dueTasks: await getDueTasks(null, { now: tickNow }), state, taskSchedule, existingTaskTypes,blockedTaskTypes, ignoreTaskId, wakeAfterRecord});@@ -1277,15 +1280,25 @@ export async function queueEligibleImprovementTasks(state, cosTaskData, {// Queue eligible improvement tasks for all managed apps (including PortOS)const apps = await getActiveApps().catch(() => []);+ // Snapshot every app's due set against `tickNow` BEFORE any prepareTask runs:+ // prepare does network and git work, so evaluating lazily would judge later+ // apps against a clock that already left the cron minute.+ const appDueSnapshots = [];for (const app of apps) {+ const continuingTaskType = perpetualContinuation?.appId === app.id ? perpetualContinuation.taskType : null;+ appDueSnapshots.push({+ app, continuingTaskType,+ dueTasks: await getDueTasks(app.id, { continuingTaskType, now: tickNow })+ });+ }++ for (const { app, continuingTaskType, dueTasks } of appDueSnapshots) {// Capture every due clock slot before applying the background-drain cap.// Cron eligibility lasts one minute: a busy app or a sibling cron must not// discard that occurrence. Persist it now; spawn admission still enforces// concurrency, budgets and cooldowns when the task actually executes.// Use the readiness reason rather than the global interval: an app may// supply its own cron while the global task remains on-demand.- const continuingTaskType = perpetualContinuation?.appId === app.id ? perpetualContinuation.taskType : null;- const dueTasks = await getDueTasks(app.id, { continuingTaskType });const cronTasks = dueTasks.filter(task => task.reason === TASK_READINESS_REASON.CRON_DUE);const appBusy = appsWithPendingImprovement.has(app.id);if (appBusy && cronTasks.length === 0) {diff --git a/server/services/taskSchedule.js b/server/services/taskSchedule.jsindex 24affae41c..96a0e152e9 100644--- a/server/services/taskSchedule.js+++ b/server/services/taskSchedule.js@@ -868,7 +868,9 @@ async function checkRunAfterDeps(schedule, taskType, appId = null, featureEnablereturn { satisfied: pending.length === 0, pending };}-async function evaluateTaskReadiness(taskType, appId, { featureEnabled, continuingPerpetualDrain, schedule: injected = null }) {+// `now` pins the clock for one scheduler tick so every app is judged against the+// same instant, however long earlier apps took to prepare (#11755).+async function evaluateTaskReadiness(taskType, appId, { featureEnabled, continuingPerpetualDrain, schedule: injected = null, now = Date.now() }) {if (appId && requiresInstallWideTarget(taskType)) {return { shouldRun: false, reason: TASK_READINESS_REASON.REQUIRES_INSTALL_WIDE_TARGET };}@@ -895,7 +897,7 @@ async function evaluateTaskReadiness(taskType, appId, { featureEnabled, continui// Weekday-only tasks skip weekends (timezone-aware)if (interval.weekdaysOnly) {- const { dayOfWeek } = getLocalParts(new Date(), timezone);+ const { dayOfWeek } = getLocalParts(new Date(now), timezone);if (dayOfWeek === 0 || dayOfWeek === 6) {return { shouldRun: false, reason: TASK_READINESS_REASON.WEEKDAY_ONLY };}@@ -949,7 +951,6 @@ async function evaluateTaskReadiness(taskType, appId, { featureEnabled, continui};}- const now = Date.now();const lastRun = appExecution.lastRun ? new Date(appExecution.lastRun).getTime() : 0;let result;@@ -1071,22 +1072,22 @@ async function evaluateTaskReadiness(taskType, appId, { featureEnabled, continui/*** Check whether a task should initiate a scheduled run for an app or globally.*/-export async function shouldRunTask(taskType, appId = null, { featureEnabled = createFeatureGate(), schedule = null } = {}) {- return evaluateTaskReadiness(taskType, appId, { featureEnabled, continuingPerpetualDrain: false, schedule });+export async function shouldRunTask(taskType, appId = null, { featureEnabled = createFeatureGate(), schedule = null, now = Date.now() } = {}) {+ return evaluateTaskReadiness(taskType, appId, { featureEnabled, continuingPerpetualDrain: false, schedule, now });}/*** Check whether an already-started perpetual drain should take another hop.* If asked to continue a non-perpetual type, its normal cadence remains in force.*/-export async function shouldContinuePerpetualDrain(taskType, appId = null, { featureEnabled = createFeatureGate() } = {}) {- return evaluateTaskReadiness(taskType, appId, { featureEnabled, continuingPerpetualDrain: true });+export async function shouldContinuePerpetualDrain(taskType, appId = null, { featureEnabled = createFeatureGate(), now = Date.now() } = {}) {+ return evaluateTaskReadiness(taskType, appId, { featureEnabled, continuingPerpetualDrain: true, now });}/*** Get all enabled task types that are due to run (optionally for a specific app)*/-export async function getDueTasks(appId = null, { continuingTaskType = null } = {}) {+export async function getDueTasks(appId = null, { continuingTaskType = null, now = Date.now() } = {}) {const schedule = await loadSchedule();const due = [];const featureEnabled = createFeatureGate();@@ -1097,7 +1098,7 @@ export async function getDueTasks(appId = null, { continuingTaskType = null } =const checkTask = taskType === continuingTaskType? shouldContinuePerpetualDrain: shouldRunTask;- const check = await checkTask(taskType, appId, { featureEnabled });+ const check = await checkTask(taskType, appId, { featureEnabled, now });if (check.shouldRun) {due.push({ taskType, reason: check.reason, interval });}diff --git a/server/services/taskSchedule.test.js b/server/services/taskSchedule.test.jsindex 933e439d52..d6cb293a0e 100644--- a/server/services/taskSchedule.test.js+++ b/server/services/taskSchedule.test.js@@ -1392,6 +1392,23 @@ describe('taskSchedule', () => {})})+ it('judges readiness against a pinned `now` instead of the wall clock', async () => {+ vi.useFakeTimers()+ vi.setSystemTime(new Date('2026-09-09T11:31:30Z'))+ const actual = await vi.importActual('./eventScheduler.js')+ parseCronToNextRun.mockImplementation(actual.parseCronToNextRun)+ getAppTaskTypeInterval.mockResolvedValue(null)+ mockSchedule({+ tasks: { 'do-release': { type: 'cron', enabled: true, cronExpression: '30 4 * * *', runAfter: [] } },+ executions: { 'task:do-release': { perApp: { 'app-1': { lastRun: '2026-09-08T05:01:00Z', count: 1 } } } }+ })+ const pinned = new Date('2026-09-09T11:30:20Z').getTime()+ expect(await shouldRunTask('do-release', 'app-1')).toMatchObject({ shouldRun: false, reason: 'cron-cooldown' })+ expect(await shouldRunTask('do-release', 'app-1', { now: pinned })).toMatchObject({ shouldRun: true, reason: 'cron-due' })+ expect((await getDueTasks('app-1', { now: pinned })).map(t => t.taskType)).toContain('do-release')+ expect((await getDueTasks('app-1')).map(t => t.taskType)).not.toContain('do-release')+ })+it('waits for the next per-app slot after downtime, including never-run tasks', async () => {vi.useFakeTimers()vi.setSystemTime(new Date('2026-09-09T12:00:00Z'))