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: compare activity day files by local date string when expiring them (#5714)

[._.]/ Adam Eivy0877fa6afac7
diff --git a/server/routes/agentActivity.js b/server/routes/agentActivity.js
index 787ee1a35f..38837f5feb 100644
--- a/server/routes/agentActivity.js
+++ b/server/routes/agentActivity.js
@@ -26,22 +26,14 @@ const router = Router();
router.get('/', asyncHandler(async (req, res) => {
const { limit, agentIds, action } = validateRequest(agentActivityQuerySchema, req.query);
- res.json(await agentActivity.getRecentActivities({
- limit,
- agentIds: agentIds ?? null,
- action: action ?? null
- }));
+ res.json(await agentActivity.getRecentActivities({ limit, agentIds, action }));
}));
// GET /timeline - Get activity timeline (for infinite scroll)
router.get('/timeline', asyncHandler(async (req, res) => {
const { limit, agentIds, before } = validateRequest(agentActivityTimelineQuerySchema, req.query);
- res.json(await agentActivity.getActivityTimeline({
- limit,
- agentIds: agentIds ?? null,
- beforeTimestamp: before ?? null
- }));
+ res.json(await agentActivity.getActivityTimeline({ limit, agentIds, beforeTimestamp: before }));
}));
// GET /agent/:agentId - Get activities for specific agent
@@ -49,13 +41,9 @@ router.get('/agent/:agentId', asyncHandler(async (req, res) => {
const { agentId } = validateRequest(agentActivityAgentParamsSchema, req.params);
const { date, limit, offset, action } = validateRequest(agentActivityAgentQuerySchema, req.query);
- res.json(await agentActivity.getActivities(agentId, {
- // A `YYYY-MM-DD` string, which getActivityFilePath consumes as-is.
- date: date ?? null,
- limit,
- offset,
- action: action ?? null
- }));
+ // `date` stays a `YYYY-MM-DD` string, which getActivityFilePath consumes
+ // as-is; absent fields fall through to the service's own defaults.
+ res.json(await agentActivity.getActivities(agentId, { date, limit, offset, action }));
}));
// GET /agent/:agentId/stats - Get activity stats for agent
diff --git a/server/routes/agentActivity.test.js b/server/routes/agentActivity.test.js
index b9dfca5ff4..e6fdcfa765 100644
--- a/server/routes/agentActivity.test.js
+++ b/server/routes/agentActivity.test.js
@@ -231,7 +231,7 @@ describe('activity route validation', () => {
expect(activityMocks.getRecentActivities).toHaveBeenCalledWith({
limit: 10,
agentIds: ['a-1', 'a-2'],
- action: null,
+ action: undefined,
});
});
@@ -240,8 +240,8 @@ describe('activity route validation', () => {
expect(res.status).toBe(200);
expect(activityMocks.getRecentActivities).toHaveBeenCalledWith({
limit: 50,
- agentIds: null,
- action: null,
+ agentIds: undefined,
+ action: undefined,
});
});
@@ -254,8 +254,8 @@ describe('activity route validation', () => {
await get('/');
expect(activityMocks.getRecentActivities).toHaveBeenCalledWith({
limit: 50,
- agentIds: null,
- action: null,
+ agentIds: undefined,
+ action: undefined,
});
});
});
@@ -271,7 +271,7 @@ describe('activity route validation', () => {
expect(res.status).toBe(200);
expect(activityMocks.getActivityTimeline).toHaveBeenCalledWith({
limit: 25,
- agentIds: null,
+ agentIds: undefined,
beforeTimestamp: '2026-08-23T00:00:05.000Z',
});
});
@@ -295,7 +295,7 @@ describe('activity route validation', () => {
date: '2026-08-22',
limit: 10,
offset: 5,
- action: null,
+ action: undefined,
});
});
diff --git a/server/services/agentActivity.js b/server/services/agentActivity.js
index e9d074f6c7..a54d168c20 100644
--- a/server/services/agentActivity.js
+++ b/server/services/agentActivity.js
@@ -5,7 +5,7 @@
* and rate limit enforcement. Activity is stored per-agent per-day.
*/
-import { readFile, readdir } from 'fs/promises';
+import { readFile, readdir, unlink } from 'fs/promises';
import { existsSync } from 'fs';
import { join } from 'path';
import EventEmitter from 'events';
@@ -14,6 +14,10 @@ import { ensureDir, getDateString, atomicWrite, PATHS } from '../lib/fileUtils.j
const AGENTS_DIR = PATHS.agentPersonalities;
const ACTIVITY_DIR = join(AGENTS_DIR, 'activity');
+// A day file is `<YYYY-MM-DD>.json`. Anything else in an agent directory is not
+// activity and is never read as a day nor unlinked as an expired one.
+const DAY_FILE_RE = /^\d{4}-\d{2}-\d{2}\.json$/;
+
// Event emitter for activity events
export const activityEvents = new EventEmitter();
@@ -277,7 +281,7 @@ export async function getActivityTimeline(options = {}) {
for (const agentId of agentDirs) {
const files = await readdir(join(ACTIVITY_DIR, agentId));
for (const file of files) {
- if (/^\d{4}-\d{2}-\d{2}\.json$/.test(file)) dates.add(file.slice(0, -5));
+ if (DAY_FILE_RE.test(file)) dates.add(file.slice(0, -5));
}
}
@@ -331,8 +335,13 @@ export async function cleanupOldActivity(daysToKeep = DEFAULT_DAYS_TO_KEEP) {
console.warn(`⚠️ Ignoring unusable activity retention window ${daysToKeep}; keeping ${days} days`);
}
- const cutoffDate = new Date();
- cutoffDate.setDate(cutoffDate.getDate() - days);
+ // Day files are named with the LOCAL date (getDateString), so the cutoff is
+ // compared as a `YYYY-MM-DD` string too. Parsing the name with `new Date()`
+ // would yield UTC midnight and compare it against a local now-with-time —
+ // deleting the boundary day early, and shifting a whole day west of UTC.
+ const cutoff = new Date();
+ cutoff.setDate(cutoff.getDate() - days);
+ const cutoffDateStr = getDateString(cutoff);
let deletedCount = 0;
@@ -348,13 +357,9 @@ export async function cleanupOldActivity(daysToKeep = DEFAULT_DAYS_TO_KEEP) {
const files = await readdir(agentDir);
for (const file of files) {
- if (!file.endsWith('.json')) continue;
-
- const dateStr = file.replace('.json', '');
- const fileDate = new Date(dateStr);
+ if (!DAY_FILE_RE.test(file)) continue;
- if (fileDate < cutoffDate) {
- const { unlink } = await import('fs/promises');
+ if (file.slice(0, -5) < cutoffDateStr) {
await unlink(join(agentDir, file));
deletedCount++;
}
diff --git a/server/services/agentActivity.test.js b/server/services/agentActivity.test.js
index 7308dc1c45..8ef59ef48d 100644
--- a/server/services/agentActivity.test.js
+++ b/server/services/agentActivity.test.js
@@ -83,14 +83,19 @@ describe('cleanupOldActivity', () => {
vi.restoreAllMocks();
});
+ // Day files are named with the LOCAL date, so the fixtures are keyed the
+ // same way — spelled out here rather than imported, so the test does not
+ // re-implement the helper it is checking.
+ const dayKey = (daysAgo) => {
+ const d = new Date();
+ d.setDate(d.getDate() - daysAgo);
+ return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
+ };
+
const seedTodayAndOld = async () => {
- const today = new Date();
- const iso = (d) => d.toISOString().slice(0, 10);
- const old = new Date(today);
- old.setDate(old.getDate() - 400);
- await writeDay('agent-a', iso(today), [entry('today', today.toISOString())]);
- await writeDay('agent-a', iso(old), [entry('ancient', old.toISOString())]);
- return { today: iso(today), old: iso(old) };
+ await writeDay('agent-a', dayKey(0), [entry('today', '2026-01-01T00:00:00.000Z')]);
+ await writeDay('agent-a', dayKey(400), [entry('ancient', '2025-01-01T00:00:00.000Z')]);
+ return { today: dayKey(0), old: dayKey(400) };
};
const dayFiles = async () => (await readdir(join(activityRoot, 'agent-a'))).sort();
@@ -104,6 +109,25 @@ describe('cleanupOldActivity', () => {
await expect(dayFiles()).resolves.toEqual([`${today}.json`]);
});
+ it('compares the file name against the local cutoff date, not a parsed UTC instant', async () => {
+ await writeDay('agent-a', dayKey(7), [entry('boundary', '2026-01-01T00:00:00.000Z')]);
+ await writeDay('agent-a', dayKey(8), [entry('past-boundary', '2026-01-01T00:00:00.000Z')]);
+
+ // The day exactly `daysToKeep` back is still inside the window; only the
+ // day before it expires. Parsing the name as a Date made the boundary day
+ // lose to the current time-of-day and get deleted a day early.
+ await expect(cleanupOldActivity(7)).resolves.toBe(1);
+ await expect(dayFiles()).resolves.toEqual([`${dayKey(7)}.json`]);
+ });
+
+ it('never unlinks a non-day file', async () => {
+ await mkdir(join(activityRoot, 'agent-a'), { recursive: true });
+ await writeFile(join(activityRoot, 'agent-a', 'notes.json'), '{}');
+
+ await expect(cleanupOldActivity(1)).resolves.toBe(0);
+ await expect(dayFiles()).resolves.toEqual(['notes.json']);
+ });
+
it('honours a real retention window', async () => {
const { today, old } = await seedTodayAndOld();