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: epigenetic intervention logs are filed under the user's local day (#12972) (#13029)

[._.]/ef0bbd1f0963
diff --git a/server/services/epigenetic.js b/server/services/epigenetic.js
index 7a1f5343aa..0e808d8adc 100644
--- a/server/services/epigenetic.js
+++ b/server/services/epigenetic.js
@@ -4,6 +4,8 @@ import { atomicWrite, PATHS, ensureDir, readJSONFile, unreadableStoreError } fro
import { isPlainObject } from '../lib/objects.js';
import { createFileWriteQueue } from '../lib/fileWriteQueue.js';
+import { todayInTimezone } from '../lib/timezone.js';
+import { getUserTimezone, userLocalToday } from './userTimezone.js';
const DATA_DIR = PATHS.meatspace;
const EPIGENETIC_FILE = join(DATA_DIR, 'epigenetic.json');
@@ -266,6 +268,10 @@ export async function addIntervention(intervention) {
* Log a daily entry for a tracked intervention.
*/
export async function logEntry(interventionId, entry) {
+ // One instant for the day key and loggedAt. The server runs TZ=UTC, so the
+ // default day must come from the user's timezone (#12972).
+ const now = new Date();
+ const date = entry.date || await userLocalToday(now);
return queueWrite(async () => {
const data = await loadData();
const intervention = data.interventions[interventionId];
@@ -273,11 +279,11 @@ export async function logEntry(interventionId, entry) {
const log = {
id: randomUUID(),
- date: entry.date || new Date().toISOString().split('T')[0],
+ date,
amount: entry.amount,
unit: intervention.trackingUnit,
notes: entry.notes || '',
- loggedAt: new Date().toISOString()
+ loggedAt: now.toISOString()
};
// Avoid duplicate date entries — update existing
@@ -339,10 +345,8 @@ export async function deleteIntervention(interventionId) {
*/
export async function getComplianceSummary(days = 30) {
const data = await loadData();
- const today = new Date();
- const startDate = new Date(today);
- startDate.setDate(startDate.getDate() - days);
- const startStr = startDate.toISOString().split('T')[0];
+ const today = todayInTimezone(await getUserTimezone());
+ const startStr = shiftDay(today, -days);
const summary = {};
for (const [id, intervention] of Object.entries(data.interventions)) {
@@ -362,7 +366,7 @@ export async function getComplianceSummary(days = 30) {
expectedDays,
recentLogCount: recentLogs.length,
lastLogged: recentLogs.length > 0 ? recentLogs[recentLogs.length - 1].date : null,
- streak: calculateStreak(intervention.logs)
+ streak: calculateStreak(intervention.logs, today)
};
}
@@ -372,30 +376,24 @@ export async function getComplianceSummary(days = 30) {
/**
* Calculate the current consecutive-day streak for an intervention.
*/
-function calculateStreak(logs) {
+function calculateStreak(logs, today) {
if (logs.length === 0) return 0;
- const today = new Date().toISOString().split('T')[0];
const dates = new Set(logs.map(l => l.date));
// Check if today or yesterday has an entry (grace period)
- const yesterday = new Date();
- yesterday.setDate(yesterday.getDate() - 1);
- const yesterdayStr = yesterday.toISOString().split('T')[0];
-
- if (!dates.has(today) && !dates.has(yesterdayStr)) return 0;
+ const yesterday = shiftDay(today, -1);
+ if (!dates.has(today) && !dates.has(yesterday)) return 0;
let streak = 0;
- const startDate = dates.has(today) ? new Date() : yesterday;
-
- for (let d = new Date(startDate); ; d.setDate(d.getDate() - 1)) {
- const dateStr = d.toISOString().split('T')[0];
- if (dates.has(dateStr)) {
- streak++;
- } else {
- break;
- }
+ for (let day = dates.has(today) ? today : yesterday; dates.has(day); day = shiftDay(day, -1)) {
+ streak++;
}
-
return streak;
}
+
+// Calendar arithmetic on a YYYY-MM-DD key; UTC math keeps DST out of it.
+function shiftDay(dayStr, delta) {
+ const [y, m, d] = dayStr.split('-').map(Number);
+ return new Date(Date.UTC(y, m - 1, d + delta)).toISOString().split('T')[0];
+}
diff --git a/server/services/epigenetic.test.js b/server/services/epigenetic.test.js
index 8d9a6f68b4..bf7d7c5654 100644
--- a/server/services/epigenetic.test.js
+++ b/server/services/epigenetic.test.js
@@ -21,6 +21,15 @@ vi.mock('fs/promises', async () => {
return { ...actual, readFile: vi.fn(actual.readFile) };
});
+let mockTimezone = 'UTC';
+vi.mock('./userTimezone.js', async () => {
+ const { todayInTimezone } = await import('../lib/timezone.js');
+ return {
+ getUserTimezone: async () => mockTimezone,
+ userLocalToday: async (at = new Date()) => todayInTimezone(mockTimezone, at),
+ };
+});
+
const { atomicWrite } = await import('../lib/fileUtils.js');
const fileUtils = await vi.importActual('../lib/fileUtils.js');
const { readFile: realReadFile } = await vi.importActual('fs/promises');
@@ -94,12 +103,64 @@ describe('epigenetic intervention persistence', () => {
});
afterEach(() => {
+ mockTimezone = 'UTC';
vi.useRealTimers();
vi.restoreAllMocks();
});
afterAll(cleanup);
+ describe('user-local day keys (#12972)', () => {
+ it('files a dateless log under the user local day, not the UTC day', async () => {
+ mockTimezone = 'America/Phoenix'; // UTC-7, no DST
+ await addTracked('tz-habit');
+ vi.useFakeTimers({ toFake: ['Date'] });
+ vi.setSystemTime(new Date('2026-10-22T02:00:00Z')); // 19:00 Oct 21 local
+ const evening = await logEntry('tz-habit', { amount: 1 });
+ expect(evening.date).toBe('2026-10-21');
+
+ vi.setSystemTime(new Date('2026-10-22T15:00:00Z')); // 08:00 Oct 22 local
+ const morning = await logEntry('tz-habit', { amount: 2 });
+ expect(morning.date).toBe('2026-10-22');
+
+ const { interventions } = await getInterventions();
+ expect(interventions['tz-habit'].logs.map(l => [l.date, l.amount])).toEqual([
+ ['2026-10-21', 1],
+ ['2026-10-22', 2],
+ ]);
+ });
+
+ it('merges logs on one local day that straddle UTC midnight and honors an explicit date', async () => {
+ mockTimezone = 'America/Phoenix';
+ await addTracked('tz-merge');
+ vi.useFakeTimers({ toFake: ['Date'] });
+ vi.setSystemTime(new Date('2026-10-21T15:00:00Z'));
+ await logEntry('tz-merge', { amount: 1 });
+ vi.setSystemTime(new Date('2026-10-22T02:00:00Z'));
+ await logEntry('tz-merge', { amount: 2 });
+ await logEntry('tz-merge', { amount: 3, date: '2026-01-05' });
+
+ const { interventions } = await getInterventions();
+ expect(interventions['tz-merge'].logs.map(l => [l.date, l.amount])).toEqual([
+ ['2026-01-05', 3],
+ ['2026-10-21', 2],
+ ]);
+ });
+
+ it('counts streak and compliance by local calendar across a DST change', async () => {
+ mockTimezone = 'America/Los_Angeles'; // DST ends 2026-11-01
+ await addTracked('tz-streak');
+ for (const date of ['2026-10-31', '2026-11-01', '2026-11-02']) {
+ await logEntry('tz-streak', { amount: 1, date });
+ }
+ vi.useFakeTimers({ toFake: ['Date'] });
+ vi.setSystemTime(new Date('2026-11-03T05:00:00Z')); // 21:00 Nov 2 local, Nov 3 UTC
+ const { summary, startDate } = await getComplianceSummary(7);
+ expect(summary['tz-streak']).toMatchObject({ streak: 3, lastLogged: '2026-11-02' });
+ expect(startDate).toBe('2026-10-26');
+ });
+ });
+
const mutations = [
() => addTracked('example-new'),