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: stop never-run cron tasks from firing an immediate catch-up for a pre-creation slot
[._.]/ Adam Eivy279ab25c9b02
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex c52edf9a56..a54cc33c9a 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -1,5 +1,7 @@# Unreleased+- **A never-run cron task no longer fires an immediate "catch-up" for a scheduled slot that predates when the task was set up.** `shouldRunTask`'s cron catch-up bounded a never-run task only by "the occurrence before the most recent slot" — which is always earlier than that slot, so the guard was vacuous and *every* never-run cron task reported `cron-catch-up` for its most recent past slot. For long periods this was jarring: a weekly `branch-cleanup` scheduled `0 9 * * 0` (Sunday 09:00) that was enabled mid-week immediately showed as **due now (catch-up)** on the Schedule Timeline for *last* Sunday's slot — a slot that elapsed before the task existed and was never actually missed. Tasks now carry a `createdAt` timestamp (stamped when first configured; `loadSchedule` backfills existing installs to "now", which only suppresses catch-up for already-past slots and never affects future cadence), and never-run catch-up only fires for a slot that elapsed *after* `createdAt`. So a weekly task enabled on a Friday now waits for the coming Sunday instead of running immediately, while a task that was genuinely configured before a missed slot (daemon down / check window missed) still catches up as before.+## Security- **[issue-2371] The autonomous Autofixer now repairs crashed apps in an isolated sandbox behind an explicit promotion gate — untrusted crash logs can no longer drive a credentialed agent against your live checkout.** The autofixer built a file-editing CLI prompt straight from raw PM2 stderr/stdout and ran it every 15 minutes in the managed repo with the full host environment inherited, so a payload reflected into a crashing process's log could become instructions to an agent with shell + file access and every ambient credential. It now: cuts a disposable, detached git worktree at HEAD and runs the agent there (a hard rollback boundary — no git checkout means no autonomous repair at all); hands the agent an allowlisted environment stripped of unrelated secrets (DB passwords, cloud keys, GitHub tokens, filesystem-backed service-account creds — only the direct AI-provider API-key vars the CLI reads survive); fences the logs as clearly-labelled UNTRUSTED evidence with a per-session, forge-resistant delimiter and "data, not instructions" framing; for the default Claude provider denies the shell/network toolset so the agent can only read/edit files; validates the resulting diff (size cap, rejects absolute/traversal paths and edits to `.env`/`.git`/`.github`/secrets, and blocks any diff that writes a credential — matched by pattern or by a live env secret value — into the repo, closing the read-then-promote exfil path); optionally runs a user-configured `verifyCommand` in the worktree; and only promotes the change to the live checkout + restarts the process when the user has explicitly enabled `settings.autofixer.autoPromote` (default **off** — otherwise the validated patch is staged beside the session for manual review), rolling the patch back if the post-apply restart fails. Adversarial tests cover fenced prompt-injection payloads and credential-access attempts. (Non-Claude CLI providers retain their own provider key in-env with network access, since PortOS can't portably constrain their toolset — the worktree + promotion gate + diff secret-scan still contain file changes; hardening that residual — e.g. hard-gating to constrainable providers or a network namespace — is a documented known limitation.)diff --git a/server/services/taskSchedule.js b/server/services/taskSchedule.jsindex 1489b1ae13..c4283902ce 100644--- a/server/services/taskSchedule.js+++ b/server/services/taskSchedule.js@@ -550,6 +550,14 @@ export async function loadSchedule() {let needsSave = false;for (const [taskType, config] of Object.entries(schedule.tasks)) {if (enforceManagedAgentOptions(taskType, config)) needsSave = true;+ // Stamp a creation timestamp the first time we see a task so the cron+ // catch-up bound (shouldRunTask) never replays a slot that predates the+ // task. Backfilling to "now" is conservative: it only suppresses catch-up+ // for slots already in the past — future slots fire on their real cadence.+ if (!config.createdAt) {+ config.createdAt = new Date().toISOString();+ needsSave = true;+ }if (!config.prompt && DEFAULT_TASK_PROMPTS[taskType]) {// No prompt set — initialize with current default and versionconfig.prompt = DEFAULT_TASK_PROMPTS[taskType];@@ -628,7 +636,7 @@ export async function updateTaskInterval(taskType, settings) {const schedule = await loadSchedule();if (!schedule.tasks[taskType]) {- schedule.tasks[taskType] = { type: INTERVAL_TYPES.ROTATION, enabled: false, providerId: null, model: null };+ schedule.tasks[taskType] = { type: INTERVAL_TYPES.ROTATION, enabled: false, providerId: null, model: null, createdAt: new Date().toISOString() };}// Normalize empty/whitespace prompts to null (treated as "use default")@@ -1094,12 +1102,16 @@ export async function shouldRunTask(taskType, appId = null) {if (lastRun) {lookbackBound = lastRun;} else {- // Never-run: only catch up if the most-recent occurrence is within ONE cron- // period of now (e.g. daily cron catches up to ~24h, hourly to ~1h). The bound- // is "the occurrence before prevRun" — anything older has already been missed- // by more than one period and shouldn't be replayed.- const beforePrev = parseCronToPrevRun(cronExpr, new Date(prevRunMs - 60_000), timezone);- lookbackBound = beforePrev ? beforePrev.getTime() : 0;+ // Never-run: only catch up to a slot that elapsed AFTER the task was+ // configured. Without this bound a never-run task always fires its most+ // recent past slot, so a weekly "Sunday 09:00" task enabled on a Friday+ // immediately reads as "due now (catch-up)" for last Sunday — a slot that+ // predates the task and was never actually missed. `createdAt` is stamped+ // when the task is first seen (loadSchedule backfills it for existing+ // installs), so catch-up only recovers slots the task was around for. An+ // un-backfilled task (createdAt absent) yields bound 0 → the legacy+ // always-catch-up behavior.+ lookbackBound = safeDate(interval.createdAt);}if (prevRunMs > lookbackBound && prevRunMs <= now) {// Compute nextRun for telemetry/reportingdiff --git a/server/services/taskSchedule.test.js b/server/services/taskSchedule.test.jsindex 7ebd534b5f..81acf751f6 100644--- a/server/services/taskSchedule.test.js+++ b/server/services/taskSchedule.test.js@@ -762,21 +762,19 @@ describe('taskSchedule', () => {})describe('cron catch-up', () => {- it('catches up a never-run cron when the most-recent slot is within one period', async () => {- // Cron: 0 9 * * * (daily 9 AM). The most recent past 9 AM already elapsed.- // First call (from=now): most-recent past 9 AM.- // Second call (from=prev-60s): one occurrence earlier (the lookback bound).+ it('catches up a never-run cron when the missed slot elapsed after the task was created', async () => {+ // Cron: 0 9 * * * (daily 9 AM). The task was configured yesterday and the+ // daemon missed today's 9 AM slot — so the elapsed slot is genuinely missed+ // and should fire now. The catch-up bound is the task's createdAt.const todayNineAm = recentNineAm()- const yesterdayNineAm = new Date(todayNineAm.getTime() - 24 * 60 * 60 * 1000)+ const twoDaysAgo = new Date(todayNineAm.getTime() - 2 * 24 * 60 * 60 * 1000)- parseCronToPrevRun- .mockReturnValueOnce(todayNineAm) // most-recent past occurrence- .mockReturnValueOnce(yesterdayNineAm) // one period earlier (the bound)+ parseCronToPrevRun.mockReturnValueOnce(todayNineAm) // most-recent past occurrenceparseCronToNextRun.mockReturnValueOnce(new Date(todayNineAm.getTime() + 24 * 60 * 60 * 1000))mockSchedule({tasks: {- 'plan-task': { type: 'cron', enabled: true, cronExpression: '0 9 * * *', providerId: null, model: null, prompt: null }+ 'plan-task': { type: 'cron', enabled: true, cronExpression: '0 9 * * *', providerId: null, model: null, prompt: null, createdAt: twoDaysAgo.toISOString() }}})@@ -786,6 +784,29 @@ describe('taskSchedule', () => {expect(result.missedSlot).toBe(todayNineAm.toISOString())})+ it('does NOT catch up a never-run cron whose most-recent slot predates the task', async () => {+ // The reported bug: a weekly "Sunday 09:00" task enabled mid-week must NOT+ // immediately fire for last Sunday's slot — that slot elapsed before the+ // task existed, so there was nothing to miss. It waits for the next Sunday.+ const now = Date.now()+ const lastSunday = new Date(now - 3 * 24 * 60 * 60 * 1000) // slot before creation+ const nextSunday = new Date(now + 4 * 24 * 60 * 60 * 1000)+ const createdYesterday = new Date(now - 1 * 24 * 60 * 60 * 1000) // task created after last Sunday++ parseCronToPrevRun.mockReturnValueOnce(lastSunday)+ parseCronToNextRun.mockReturnValue(nextSunday)++ mockSchedule({+ tasks: {+ 'branch-cleanup': { type: 'cron', enabled: true, cronExpression: '0 9 * * 0', providerId: null, model: null, prompt: null, createdAt: createdYesterday.toISOString() }+ }+ })++ const result = await shouldRunTask('branch-cleanup')+ expect(result.shouldRun).toBe(false)+ expect(result.reason).toBe('cron-cooldown')+ })+it('catches up after the recorded lastRun even if the daemon missed the slot', async () => {// Cron fired yesterday, then daemon was down across today's 9 AM.// Catch-up bound is the recorded lastRun (yesterday), so today's 9 AM counts as missed.@@ -899,17 +920,15 @@ describe('taskSchedule', () => {const tomorrowNineAm = new Date(todayNineAm.getTime() + 24 * 60 * 60 * 1000)const yesterdayNineAm = new Date(todayNineAm.getTime() - 24 * 60 * 60 * 1000)- // shouldRunTask iterates both tasks. For plan-task it calls prev twice (catch-up- // path); for code-quality it doesn't call cron helpers at all.- parseCronToPrevRun- .mockReturnValueOnce(todayNineAm) // plan-task prevRun- .mockReturnValueOnce(yesterdayNineAm) // plan-task beforePrev (bound)+ // shouldRunTask iterates both tasks. plan-task was created before its missed+ // slot (createdAt bound), so its elapsed 9 AM counts as a genuine catch-up.+ parseCronToPrevRun.mockReturnValueOnce(todayNineAm) // plan-task prevRunparseCronToNextRun.mockReturnValue(tomorrowNineAm)mockSchedule({tasks: {'code-quality': { type: 'weekly', enabled: true, providerId: null, model: null, prompt: null, runAfter: [] },- 'plan-task': { type: 'cron', enabled: true, cronExpression: '0 9 * * *', providerId: null, model: null, prompt: null }+ 'plan-task': { type: 'cron', enabled: true, cronExpression: '0 9 * * *', providerId: null, model: null, prompt: null, createdAt: yesterdayNineAm.toISOString() }}})@@ -927,14 +946,12 @@ describe('taskSchedule', () => {const todayNineAm = recentNineAm()const tomorrowNineAm = new Date(todayNineAm.getTime() + 24 * 60 * 60 * 1000)const yesterdayNineAm = new Date(todayNineAm.getTime() - 24 * 60 * 60 * 1000)- parseCronToPrevRun- .mockReturnValueOnce(todayNineAm)- .mockReturnValueOnce(yesterdayNineAm)+ parseCronToPrevRun.mockReturnValueOnce(todayNineAm)parseCronToNextRun.mockReturnValue(tomorrowNineAm)mockSchedule({tasks: {- 'pr-watcher': { type: 'cron', enabled: true, cronExpression: '0 9 * * *', providerId: null, model: null, prompt: null },+ 'pr-watcher': { type: 'cron', enabled: true, cronExpression: '0 9 * * *', providerId: null, model: null, prompt: null, createdAt: yesterdayNineAm.toISOString() },'claim-issue': { type: 'perpetual', enabled: true, providerId: null, model: null, prompt: null }}})