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: catch up a missed weekly privacy recheck after downtime (#12625)

[._.]/ Adam Eivy7696e70c28a2
diff --git a/server/lib/privacyValidation.js b/server/lib/privacyValidation.js
index 163bff40fc..e95f0536e0 100644
--- a/server/lib/privacyValidation.js
+++ b/server/lib/privacyValidation.js
@@ -420,6 +420,10 @@ export const privacyRecheckConfigSchema = z.object({
cronExpression: cronExpressionSchema.optional(),
autoApproveOptOutEmails: z.boolean().optional(),
autoSubmitWebForms: z.boolean().optional(),
+ // Server-stamped: the last completed recheck, and when the schedule was last
+ // switched on (the floor for missed-slot catch-up when no run has happened yet).
+ lastRunAt: z.string().datetime().optional(),
+ enabledAt: z.string().datetime().optional(),
}).strict();
// The `privacy` settings slice (only `recheck` for now — room to grow).
diff --git a/server/routes/privacy.js b/server/routes/privacy.js
index 0fd45d165b..4eb79ebd46 100644
--- a/server/routes/privacy.js
+++ b/server/routes/privacy.js
@@ -399,14 +399,19 @@ router.get('/optout/schedule', asyncHandler(async (_req, res) => {
// scheduler so the change takes effect immediately (the cron expression is
// locked in at registration). Enabling creates the cron; disabling removes it.
router.put('/optout/schedule', asyncHandler(async (req, res) => {
- const patch = validateRequest(privacyRecheckConfigSchema, req.body ?? {});
- await updateSettingsWith((current) => ({
- ...current,
- privacy: {
- ...(current.privacy || {}),
- recheck: { ...(current.privacy?.recheck || {}), ...patch },
- },
- }));
+ // lastRunAt/enabledAt are server-stamped; a client can't set them here.
+ const { lastRunAt: _lastRunAt, enabledAt: _enabledAt, ...patch } = validateRequest(privacyRecheckConfigSchema, req.body ?? {});
+ await updateSettingsWith((current) => {
+ const existing = current.privacy?.recheck || {};
+ const turningOn = patch.enabled === true && existing.enabled !== true;
+ return {
+ ...current,
+ privacy: {
+ ...(current.privacy || {}),
+ recheck: { ...existing, ...patch, ...(turningOn && { enabledAt: new Date().toISOString() }) },
+ },
+ };
+ });
await restartPrivacyRecheckScheduler();
res.json(await getPrivacyRecheckStatus());
}));
diff --git a/server/services/bootstrap.js b/server/services/bootstrap.js
index e817d41b76..e5cf6e56ad 100644
--- a/server/services/bootstrap.js
+++ b/server/services/bootstrap.js
@@ -517,7 +517,7 @@ const startBackgroundServices = ({ spawnerReady, io, httpsEnabled, port }) => {
// Initialize Privacy Center opt-out recheck scheduler — OFF by default; only
// re-runs the broker scan + opt-out pass when the user opts in via
// Settings → Privacy (sanctioned scheduled-automation exception) (#2145).
- startPrivacyRecheckScheduler().catch(err => logBootstrapFailure('❌ Privacy recheck scheduler init failed', err));
+ startPrivacyRecheckScheduler({ catchUpMissedSlot: true }).catch(err => logBootstrapFailure('❌ Privacy recheck scheduler init failed', err));
// Quota-burn loop — ONE install-level loop that spends subscription quota that
// would otherwise expire. OFF by default: the tick returns before touching a
// provider unless the user enabled it on the Quota Burn page, where the
diff --git a/server/services/privacyRecheckScheduler.js b/server/services/privacyRecheckScheduler.js
index 1230ccbfeb..b5084ae5ca 100644
--- a/server/services/privacyRecheckScheduler.js
+++ b/server/services/privacyRecheckScheduler.js
@@ -13,8 +13,8 @@
* without a restart.
*/
-import { schedule, cancel, parseCronToNextRun } from './eventScheduler.js';
-import { getSettings } from './settings.js';
+import { schedule, cancel, parseCronToNextRun, parseCronToPrevRun } from './eventScheduler.js';
+import { getSettings, updateSettingsWith } from './settings.js';
import { getUserTimezone } from './userTimezone.js';
import { runScanPass } from './privacyScan.js';
import { runOptOutPass } from './privacyOptOut.js';
@@ -23,7 +23,61 @@ import { listSubjects } from './privacySubjects.js';
const EVENT_ID = 'privacy-recheck';
const DEFAULT_CRON = '0 4 * * 0'; // weekly, Sun 4am
-export async function startPrivacyRecheckScheduler() {
+// Boot delay before a missed-slot catch-up runs, mirroring backupScheduler.js
+// (#8456): stays clear of boot-time migrations and store warm-up.
+const CATCHUP_BOOT_DELAY_MS = 5 * 60_000;
+
+// At most one catch-up evaluation per process boot, and its pending timer so
+// stopPrivacyRecheckScheduler() can cancel it.
+let catchUpEvaluated = false;
+let catchUpTimer = null;
+
+const toMs = (iso) => {
+ const ms = iso ? new Date(iso).getTime() : NaN;
+ return Number.isFinite(ms) ? ms : NaN;
+};
+
+/** Settings-gated run shared by the cron handler and the missed-slot catch-up. */
+async function runIfEnabled(logReason) {
+ const current = await getSettings();
+ if (current.privacy?.recheck?.enabled !== true) {
+ console.log(`🛡️ Privacy recheck: disabled since registration — skipping ${logReason} run`);
+ return;
+ }
+ await runScheduledRecheck();
+}
+
+/**
+ * If the latest cron slot elapsed without a recheck (server down across it),
+ * schedule one catch-up after a boot delay — however many slots were missed.
+ * With no `lastRunAt` yet, only a slot after `enabledAt` counts as missed, so
+ * enabling the feature never replays a slot that predates it.
+ */
+function maybeCatchUpMissedSlot(recheck, cronExpression, timezone) {
+ if (catchUpEvaluated) return;
+ catchUpEvaluated = true;
+ const prevRun = parseCronToPrevRun(cronExpression, new Date(), timezone);
+ if (!prevRun) return;
+ const prevRunMs = prevRun.getTime();
+ const lastRunMs = toMs(recheck.lastRunAt);
+ const enabledAtMs = toMs(recheck.enabledAt);
+ if (Number.isFinite(lastRunMs) ? lastRunMs >= prevRunMs : !(enabledAtMs < prevRunMs)) return;
+
+ console.log(`🛡️ Privacy recheck scheduler: missed slot ${prevRun.toISOString()} — catching up in ${Math.round(CATCHUP_BOOT_DELAY_MS / 60_000)}m`);
+ catchUpTimer = setTimeout(() => {
+ catchUpTimer = null;
+ runIfEnabled('missed-slot catch-up').catch((err) =>
+ console.error(`❌ Privacy recheck catch-up failed: ${err.message}`));
+ }, CATCHUP_BOOT_DELAY_MS);
+ catchUpTimer.unref?.();
+}
+
+/**
+ * @param {object} [options]
+ * @param {boolean} [options.catchUpMissedSlot] - boot only; a settings-save
+ * restart must not replay a slot the user did not miss.
+ */
+export async function startPrivacyRecheckScheduler({ catchUpMissedSlot = false } = {}) {
const settings = await getSettings();
const recheck = settings.privacy?.recheck || {};
@@ -40,18 +94,12 @@ export async function startPrivacyRecheckScheduler() {
type: 'cron',
cron: cronExpression,
timezone,
- handler: async () => {
- const current = await getSettings();
- if (current.privacy?.recheck?.enabled !== true) {
- console.log('🛡️ Privacy recheck: disabled since registration — skipping run');
- return;
- }
- await runScheduledRecheck();
- },
+ handler: () => runIfEnabled('scheduled'),
metadata: { source: 'privacyRecheckScheduler' },
});
console.log(`🛡️ Privacy recheck scheduler: registered at cron "${cronExpression}"`);
+ if (catchUpMissedSlot) maybeCatchUpMissedSlot(recheck, cronExpression, timezone);
}
// Each scheduled purpose, in run order: scan first (re-checks due cases + finds
@@ -86,10 +134,22 @@ async function runScheduledRecheck() {
}
}
}
+ // Stamped on completion, partial failures included (they are isolated above).
+ await updateSettingsWith((current) => ({
+ ...current,
+ privacy: {
+ ...(current.privacy || {}),
+ recheck: { ...(current.privacy?.recheck || {}), lastRunAt: new Date().toISOString() },
+ },
+ }));
}
export function stopPrivacyRecheckScheduler() {
cancel(EVENT_ID);
+ if (catchUpTimer) {
+ clearTimeout(catchUpTimer);
+ catchUpTimer = null;
+ }
console.log('🛡️ Privacy recheck scheduler: stopped');
}
@@ -126,5 +186,11 @@ export async function getPrivacyRecheckStatus() {
autoApproveOptOutEmails: recheck.autoApproveOptOutEmails === true,
autoSubmitWebForms: recheck.autoSubmitWebForms === true,
nextRun,
+ lastRunAt: recheck.lastRunAt ?? null,
};
}
+
+/** Test seam: allow another boot-time catch-up evaluation in the same process. */
+export function __resetCatchUpForTests() {
+ catchUpEvaluated = false;
+}
diff --git a/server/services/privacyRecheckScheduler.test.js b/server/services/privacyRecheckScheduler.test.js
index d52c9e890d..b28c49b17f 100644
--- a/server/services/privacyRecheckScheduler.test.js
+++ b/server/services/privacyRecheckScheduler.test.js
@@ -8,10 +8,13 @@
* grant never triggers the submission pass.
*/
-import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
-const { listSubjectsMock, runScanPassMock, runOptOutPassMock, scheduleMock } = vi.hoisted(() => ({
+const { listSubjectsMock, runScanPassMock, runOptOutPassMock, scheduleMock, getSettingsMock, updateSettingsWithMock, prevRunMock } = vi.hoisted(() => ({
scheduleMock: vi.fn(),
+ getSettingsMock: vi.fn(),
+ updateSettingsWithMock: vi.fn(async () => ({})),
+ prevRunMock: vi.fn(),
listSubjectsMock: vi.fn(),
runScanPassMock: vi.fn(async () => ({})),
runOptOutPassMock: vi.fn(async () => ({})),
@@ -20,11 +23,11 @@ const { listSubjectsMock, runScanPassMock, runOptOutPassMock, scheduleMock } = v
vi.mock('./privacySubjects.js', () => ({ listSubjects: listSubjectsMock }));
vi.mock('./privacyScan.js', () => ({ runScanPass: runScanPassMock }));
vi.mock('./privacyOptOut.js', () => ({ runOptOutPass: runOptOutPassMock }));
-vi.mock('./eventScheduler.js', () => ({ schedule: scheduleMock, cancel: vi.fn(), parseCronToNextRun: vi.fn() }));
-vi.mock('./settings.js', () => ({ getSettings: vi.fn(async () => ({ privacy: { recheck: { enabled: true } } })) }));
+vi.mock('./eventScheduler.js', () => ({ schedule: scheduleMock, cancel: vi.fn(), parseCronToNextRun: vi.fn(), parseCronToPrevRun: prevRunMock }));
+vi.mock('./settings.js', () => ({ getSettings: getSettingsMock, updateSettingsWith: updateSettingsWithMock }));
vi.mock('./userTimezone.js', () => ({ getUserTimezone: vi.fn(async () => 'UTC') }));
-const { startPrivacyRecheckScheduler } = await import('./privacyRecheckScheduler.js');
+const { startPrivacyRecheckScheduler, getPrivacyRecheckStatus, __resetCatchUpForTests } = await import('./privacyRecheckScheduler.js');
// Register the cron, then fire its handler the way the event scheduler would.
const runScheduledRecheck = async () => {
@@ -35,8 +38,14 @@ const runScheduledRecheck = async () => {
const ranFor = (mock) => mock.mock.calls.map(([arg]) => arg.subjectId);
+const SLOT = new Date('2026-10-18T04:00:00.000Z');
+const setRecheck = (recheck) => getSettingsMock.mockResolvedValue({ privacy: { recheck } });
+
beforeEach(() => {
vi.clearAllMocks();
+ __resetCatchUpForTests();
+ setRecheck({ enabled: true });
+ prevRunMock.mockReturnValue(SLOT);
listSubjectsMock.mockResolvedValue([
{ id: 'vault-only', activeScopes: ['pii_vault'] },
{ id: 'scan-only', activeScopes: ['broker_scan', 'pii_vault'] },
@@ -60,3 +69,70 @@ describe('runScheduledRecheck', () => {
expect(ranFor(runOptOutPassMock)).toEqual(['both']);
});
});
+
+describe('lastRunAt persistence', () => {
+ it('stamps lastRunAt after a run, even when a subject failed, and status returns it', async () => {
+ runScanPassMock.mockRejectedValueOnce(new Error('broker down'));
+ await runScheduledRecheck();
+ const mutate = updateSettingsWithMock.mock.calls.at(-1)[0];
+ const next = mutate({ privacy: { recheck: { enabled: true, cronExpression: '0 4 * * 0' } } });
+ expect(next.privacy.recheck.enabled).toBe(true);
+ expect(Date.parse(next.privacy.recheck.lastRunAt)).not.toBeNaN();
+
+ setRecheck({ enabled: true, lastRunAt: '2026-10-18T04:01:00.000Z' });
+ expect((await getPrivacyRecheckStatus()).lastRunAt).toBe('2026-10-18T04:01:00.000Z');
+ });
+});
+
+describe('missed-slot catch-up', () => {
+ beforeEach(() => vi.useFakeTimers());
+ afterEach(() => vi.useRealTimers());
+
+ const bootAndAdvance = async () => {
+ await startPrivacyRecheckScheduler({ catchUpMissedSlot: true });
+ await vi.advanceTimersByTimeAsync(5 * 60_000 + 1);
+ };
+
+ it('runs exactly one catch-up when the last run predates the latest slot', async () => {
+ setRecheck({ enabled: true, lastRunAt: '2026-10-11T04:01:00.000Z' });
+ await bootAndAdvance();
+ expect(runScanPassMock).toHaveBeenCalledTimes(2);
+ // A second boot evaluation in the same process never re-arms.
+ await bootAndAdvance();
+ expect(runScanPassMock).toHaveBeenCalledTimes(2);
+ });
+
+ it('schedules none once a run completed after the latest slot', async () => {
+ setRecheck({ enabled: true, lastRunAt: '2026-10-18T04:02:00.000Z' });
+ await bootAndAdvance();
+ expect(runScanPassMock).not.toHaveBeenCalled();
+ });
+
+ it('with no lastRunAt, catches up only if enabled before the missed slot', async () => {
+ setRecheck({ enabled: true, enabledAt: '2026-10-19T00:00:00.000Z' });
+ await bootAndAdvance();
+ expect(runScanPassMock).not.toHaveBeenCalled();
+
+ __resetCatchUpForTests();
+ setRecheck({ enabled: true, enabledAt: '2026-10-01T00:00:00.000Z' });
+ await bootAndAdvance();
+ expect(runScanPassMock).toHaveBeenCalledTimes(2);
+ });
+
+ it('schedules no catch-up when disabled, or when not a boot-time start', async () => {
+ setRecheck({ enabled: false, lastRunAt: '2026-10-01T00:00:00.000Z' });
+ await bootAndAdvance();
+ setRecheck({ enabled: true, lastRunAt: '2026-10-01T00:00:00.000Z' });
+ await startPrivacyRecheckScheduler();
+ await vi.advanceTimersByTimeAsync(10 * 60_000);
+ expect(runScanPassMock).not.toHaveBeenCalled();
+ });
+
+ it('skips the catch-up run if disabled before the boot delay elapses', async () => {
+ setRecheck({ enabled: true, lastRunAt: '2026-10-01T00:00:00.000Z' });
+ await startPrivacyRecheckScheduler({ catchUpMissedSlot: true });
+ setRecheck({ enabled: false });
+ await vi.advanceTimersByTimeAsync(5 * 60_000 + 1);
+ expect(runScanPassMock).not.toHaveBeenCalled();
+ });
+});