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): derive completedToday from session startedAt, not the UTC-dated bucket; swallow reschedule failure

Adam Eivy631046c9faa8
diff --git a/server/routes/meatspacePostRoutes.js b/server/routes/meatspacePostRoutes.js
index e480841d59..8daa91bcac 100644
--- a/server/routes/meatspacePostRoutes.js
+++ b/server/routes/meatspacePostRoutes.js
@@ -52,9 +52,12 @@ router.put('/post/config', asyncHandler(async (req, res) => {
const data = validateRequest(postConfigUpdateSchema, req.body);
const config = await postService.updatePostConfig(data);
// Reschedule the daily reminder immediately (no restart needed) whenever
- // its enabled/time fields change — see meatspacePostReminder.js.
+ // its enabled/time fields change — see meatspacePostReminder.js. The config
+ // write above already succeeded, so a rescheduling failure (e.g. timezone
+ // settings unreadable) must not surface as a save error to the client —
+ // log and swallow it, mirroring the boot-time registration's own handling.
if (data.reminder) {
- await registerPostReminderSchedule();
+ await registerPostReminderSchedule().catch(err => console.error(`❌ POST reminder reschedule failed: ${err.message}`));
}
res.json(config);
}));
diff --git a/server/routes/meatspacePostRoutes.reminder.test.js b/server/routes/meatspacePostRoutes.reminder.test.js
index 37e8f0d0f5..7c81ebd5ba 100644
--- a/server/routes/meatspacePostRoutes.reminder.test.js
+++ b/server/routes/meatspacePostRoutes.reminder.test.js
@@ -12,7 +12,11 @@ vi.mock('../services/meatspacePost.js', () => ({
}));
vi.mock('../services/meatspacePostReminder.js', () => ({
- registerPostReminderSchedule: vi.fn(),
+ // Real implementation is async (always returns a Promise) — the route
+ // chains `.catch()` onto the call, so the mock must resolve rather than
+ // return `undefined` synchronously (clearAllMocks in beforeEach clears call
+ // history, not the configured resolution, so this default is safe to set once).
+ registerPostReminderSchedule: vi.fn().mockResolvedValue(undefined),
}));
import * as postService from '../services/meatspacePost.js';
@@ -51,4 +55,16 @@ describe('PUT /api/meatspace/post/config — reminder rescheduling', () => {
expect(r.status).toBe(200);
expect(registerPostReminderSchedule).not.toHaveBeenCalled();
});
+
+ // Regression: a rescheduling failure must not mask a config write that
+ // already succeeded — the client shouldn't be told "save failed" when the
+ // new value was in fact persisted.
+ it('still returns the saved config with a 200 when rescheduling itself throws', async () => {
+ registerPostReminderSchedule.mockRejectedValueOnce(new Error('timezone settings unreadable'));
+ const r = await request(app)
+ .put('/api/meatspace/post/config')
+ .send({ reminder: { enabled: true, time: '09:00' } });
+ expect(r.status).toBe(200);
+ expect(r.body).toEqual({ reminder: { enabled: true, time: '09:00' } });
+ });
});
diff --git a/server/services/meatspacePostReminder.js b/server/services/meatspacePostReminder.js
index 126fb921f2..985a5d22b1 100644
--- a/server/services/meatspacePostReminder.js
+++ b/server/services/meatspacePostReminder.js
@@ -19,8 +19,8 @@
*/
import { schedule, cancel } from './eventScheduler.js';
-import { getUserTimezone, getLocalParts } from '../lib/timezone.js';
-import { getPostConfig, getPostSessions, computePostStreaks } from './meatspacePost.js';
+import { getUserTimezone, getLocalParts, todayInTimezone } from '../lib/timezone.js';
+import { getPostConfig, getPostSessions } from './meatspacePost.js';
import { addNotification, NOTIFICATION_TYPES, PRIORITY_LEVELS } from './notifications.js';
export const POST_REMINDER_EVENT_ID = 'post-daily-reminder';
@@ -51,18 +51,27 @@ export async function firePostReminderIfIncomplete() {
return;
}
- const sessions = await getPostSessions();
// The cron itself fires on the user's LOCAL wall-clock time (registerPostReminderSchedule
// schedules it with `timezone` from getUserTimezone()), so "today" for the completeness
- // check must use that same local calendar day — not the raw UTC date. For any negative-UTC-
- // offset timezone (the Americas), the UTC calendar date rolls over mid-local-evening, so a
- // bare `new Date().toISOString()` here would compare against a UTC "tomorrow" that doesn't
- // match the local day the reminder time represents, producing a false nag hours after a
- // session the user already completed earlier that same local day.
+ // check must be the user's local calendar day. Sessions are day-bucketed server-side via
+ // `session.date`, stamped from the raw UTC date (see submitPostSession in meatspacePost.js)
+ // — that bucket does NOT line up with local calendar days for any non-UTC timezone, and for
+ // negative-UTC-offset zones (the Americas) the UTC date can roll over several hours before
+ // local midnight. Comparing against that bucket (even after converting only the "now" side
+ // to local time) would still mismatch for a session completed in the gap between the UTC
+ // rollover and local midnight. So this derives each session's local day directly from its
+ // precise `startedAt` timestamp instead of trusting the coarse UTC-dated bucket at all —
+ // correct regardless of what time of day, relative to either boundary, a session actually
+ // completed.
const timezone = await getUserTimezone();
- const { year, month, day } = getLocalParts(new Date(), timezone);
- const todayStr = `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
- const { completedToday } = computePostStreaks(sessions, todayStr);
+ const todayStr = todayInTimezone(timezone);
+ const sessions = await getPostSessions();
+ const completedToday = sessions.some(s => {
+ if (!s?.startedAt) return false;
+ const parts = getLocalParts(new Date(s.startedAt), timezone);
+ const localDate = `${parts.year}-${String(parts.month).padStart(2, '0')}-${String(parts.day).padStart(2, '0')}`;
+ return localDate === todayStr;
+ });
if (completedToday) {
console.log(`🔔 POST reminder: today's session already complete — no nudge`);
return;
diff --git a/server/services/meatspacePostReminder.test.js b/server/services/meatspacePostReminder.test.js
index 8364a34339..c79c102551 100644
--- a/server/services/meatspacePostReminder.test.js
+++ b/server/services/meatspacePostReminder.test.js
@@ -15,13 +15,13 @@ vi.mock('./eventScheduler.js', () => ({
vi.mock('../lib/timezone.js', () => ({
getUserTimezone: vi.fn().mockResolvedValue('UTC'),
- getLocalParts: vi.fn()
+ getLocalParts: vi.fn(),
+ todayInTimezone: vi.fn()
}));
vi.mock('./meatspacePost.js', () => ({
getPostConfig: vi.fn(),
- getPostSessions: vi.fn(),
- computePostStreaks: vi.fn()
+ getPostSessions: vi.fn()
}));
vi.mock('./notifications.js', () => ({
@@ -31,8 +31,8 @@ vi.mock('./notifications.js', () => ({
}));
import { schedule, cancel } from './eventScheduler.js';
-import { getUserTimezone, getLocalParts } from '../lib/timezone.js';
-import { getPostConfig, getPostSessions, computePostStreaks } from './meatspacePost.js';
+import { getUserTimezone, getLocalParts, todayInTimezone } from '../lib/timezone.js';
+import { getPostConfig, getPostSessions } from './meatspacePost.js';
import { addNotification } from './notifications.js';
import {
reminderTimeToCron,
@@ -119,12 +119,15 @@ describe('firePostReminderIfIncomplete', () => {
vi.clearAllMocks();
getPostSessions.mockResolvedValue([]);
getUserTimezone.mockResolvedValue('UTC');
+ todayInTimezone.mockReturnValue('2026-07-01');
+ // Default: getLocalParts on any session's startedAt resolves to "today" —
+ // individual tests override to simulate a specific local day per session.
getLocalParts.mockReturnValue({ year: 2026, month: 7, day: 1 });
});
- it('sends a notification when the reminder is enabled and today is incomplete', async () => {
+ it('sends a notification when the reminder is enabled and there are no sessions at all', async () => {
getPostConfig.mockResolvedValue({ reminder: { enabled: true, time: '09:00' } });
- computePostStreaks.mockReturnValue({ completedToday: false });
+ getPostSessions.mockResolvedValue([]);
await firePostReminderIfIncomplete();
@@ -135,53 +138,78 @@ describe('firePostReminderIfIncomplete', () => {
});
});
- it('does not nag once the session is already done today', async () => {
+ it('does not nag once a session has been completed today (by local calendar day)', async () => {
getPostConfig.mockResolvedValue({ reminder: { enabled: true, time: '09:00' } });
- computePostStreaks.mockReturnValue({ completedToday: true });
+ getPostSessions.mockResolvedValue([{ startedAt: '2026-07-01T15:00:00.000Z' }]);
+ getLocalParts.mockReturnValue({ year: 2026, month: 7, day: 1 });
await firePostReminderIfIncomplete();
expect(addNotification).not.toHaveBeenCalled();
});
+ it('nags when every session on record is from a different local day', async () => {
+ getPostConfig.mockResolvedValue({ reminder: { enabled: true, time: '09:00' } });
+ getPostSessions.mockResolvedValue([{ startedAt: '2026-06-30T15:00:00.000Z' }]);
+ getLocalParts.mockReturnValue({ year: 2026, month: 6, day: 30 }); // yesterday, not today
+
+ await firePostReminderIfIncomplete();
+
+ expect(addNotification).toHaveBeenCalledTimes(1);
+ });
+
+ it('ignores a session missing startedAt rather than crashing', async () => {
+ getPostConfig.mockResolvedValue({ reminder: { enabled: true, time: '09:00' } });
+ getPostSessions.mockResolvedValue([{ date: '2026-07-01' }]); // no startedAt
+
+ await firePostReminderIfIncomplete();
+
+ expect(addNotification).toHaveBeenCalledTimes(1);
+ });
+
it('skips entirely if disabled since registration', async () => {
getPostConfig.mockResolvedValue({ reminder: { enabled: false, time: '09:00' } });
- computePostStreaks.mockReturnValue({ completedToday: false });
await firePostReminderIfIncomplete();
expect(addNotification).not.toHaveBeenCalled();
+ expect(getPostSessions).not.toHaveBeenCalled();
});
- // Regression: the reminder cron fires on the user's LOCAL wall-clock time
- // (registerPostReminderSchedule schedules it with the user's timezone), so
- // the "is today done" check must use that same local calendar day — not the
- // raw UTC date. For a negative-UTC-offset timezone (e.g. America/Los_Angeles,
- // UTC-8), the UTC calendar date rolls over mid-local-evening: a user who
- // completed their session that morning could otherwise get a false nag once
- // the UTC date ticks over to "tomorrow" while it's still "today" for them.
- it("computes 'today' from the reminder's own local timezone, not the raw UTC date", async () => {
+ // Regression (finding 1 + its follow-up): the reminder cron fires on the
+ // user's LOCAL wall-clock time, so "is today done" must be evaluated in
+ // that same local calendar day. Sessions are day-bucketed server-side via
+ // a raw-UTC `session.date` string that does not line up with local
+ // calendar days for non-UTC timezones — so this must NOT compare against
+ // that bucket at all. Instead it derives each session's local day directly
+ // from its precise `startedAt` timestamp, which is correct regardless of
+ // what time of day (relative to either the UTC or local day boundary) the
+ // session actually completed.
+ it("derives 'today' and each session's day from the local timezone, not the raw UTC date/bucket", async () => {
getPostConfig.mockResolvedValue({ reminder: { enabled: true, time: '20:00' } });
getUserTimezone.mockResolvedValue('America/Los_Angeles');
- // Local calendar day is still "2026-07-01" even though the underlying UTC
- // clock may already read "2026-07-02" at this fire time (8pm PDT = 3am UTC).
+ todayInTimezone.mockReturnValue('2026-07-01');
+ // A session with a raw UTC session.date of "2026-07-02" (recorded late in the
+ // local evening, after the UTC date had already rolled over) still resolves to
+ // local day "2026-07-01" via its precise startedAt timestamp + getLocalParts —
+ // proving the check no longer trusts the UTC-dated bucket.
+ getPostSessions.mockResolvedValue([{ date: '2026-07-02', startedAt: '2026-07-02T03:30:00.000Z' }]);
getLocalParts.mockReturnValue({ year: 2026, month: 7, day: 1 });
- computePostStreaks.mockReturnValue({ completedToday: true });
await firePostReminderIfIncomplete();
- expect(computePostStreaks).toHaveBeenCalledWith(expect.any(Array), '2026-07-01');
- // Local day already has a completed session — must not nag.
+ expect(getLocalParts).toHaveBeenCalledWith(new Date('2026-07-02T03:30:00.000Z'), 'America/Los_Angeles');
expect(addNotification).not.toHaveBeenCalled();
});
- it('zero-pads single-digit month/day into the YYYY-MM-DD string', async () => {
+ it("zero-pads single-digit month/day when comparing a session's local day", async () => {
getPostConfig.mockResolvedValue({ reminder: { enabled: true, time: '09:00' } });
+ todayInTimezone.mockReturnValue('2026-03-05');
+ getPostSessions.mockResolvedValue([{ startedAt: '2026-03-05T12:00:00.000Z' }]);
getLocalParts.mockReturnValue({ year: 2026, month: 3, day: 5 });
- computePostStreaks.mockReturnValue({ completedToday: false });
await firePostReminderIfIncomplete();
- expect(computePostStreaks).toHaveBeenCalledWith(expect.any(Array), '2026-03-05');
+ expect(addNotification).not.toHaveBeenCalled();
});
});