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 (agy): validate real calendar dates, allow UTC offsets, and wire the timeline appId filter

[._.]/ Adam Eivy49bc740eb214
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 4f2b5a641f..ff580e60c9 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -38,4 +38,4 @@
- The remaining trigger-anchored dropdowns — "Run on App" on a scheduled task, and the Share and Sync buttons on series and universe cards — now use the same shared popover-positioning hook as the notification panel. "Run on App" opened a panel pinned to its button's left edge that merely got narrower on a phone rather than moving, so it ran off the right edge; the Share and Sync menus always opened downward even with no room below. All three now clamp into the viewport, flip when there is no room, and stay attached to their button as the page scrolls.
- The theme switcher, the media collection picker, and the Shell photo composer now dismiss through the shared click-outside hook instead of three private copies of the same two-ref check, so the rule that a portaled panel counts as "inside" its own trigger lives in one place. Escape still closes each of them.
- Collapsible section headers in the canon cards, the character detail editor, and the digital-twin interview analysis card now announce their open/closed state to a screen reader. They were five separate hand-rolled copies of the same header, none of which reported that state, and all five now share one primitive so a layout fix lands in one place instead of three.
-- The memory list and timeline endpoints now validate their query strings against the Zod schemas that were written for them. Both handlers had been hand-parsing `req.query`, so the declared bounds went unenforced: an unknown memory type, a garbled `limit`, or a nonsense date range was quietly accepted and passed to the database. Both schemas now also accept the comma-separated, string-valued form a query string actually arrives in, and the list `limit` ceiling is 500 — the bound the route has always enforced in practice — rather than the 100 its schema claimed.
+- The memory list and timeline endpoints now validate their query strings against the Zod schemas that were written for them. Both handlers had been hand-parsing `req.query`, so the declared bounds went unenforced: an unknown memory type, a garbled `limit`, or a nonsense date range was quietly accepted and passed to the database. Both schemas now also accept the comma-separated, string-valued form a query string actually arrives in, and the list `limit` ceiling is 500 — the bound the route has always enforced in practice — rather than the 100 its schema claimed. The timeline endpoint also finally honours an `appId` filter, which both of its backends have always supported but the hand-written parsing never passed along.
diff --git a/server/lib/memoryValidation.js b/server/lib/memoryValidation.js
index 7a62175927..d3412bd401 100644
--- a/server/lib/memoryValidation.js
+++ b/server/lib/memoryValidation.js
@@ -104,11 +104,27 @@ const emptyToUndefined = (inner) => z.preprocess(
inner
);
+// A `YYYY-MM-DD` string can match the shape and still name a day that doesn't
+// exist (`2026-02-30`). `Date.parse` rolls those over instead of failing, so
+// round-trip the parts — otherwise Postgres is the one that rejects the date,
+// as a 500 rather than a 400.
+const isRealCalendarDate = (v) => {
+ const [year, month, day] = v.split('-').map(Number);
+ const parsed = new Date(Date.UTC(year, month - 1, day));
+ return parsed.getUTCFullYear() === year
+ && parsed.getUTCMonth() === month - 1
+ && parsed.getUTCDate() === day;
+};
+
// `startDate`/`endDate` reach Postgres as `created_at >= $n` bind params, so a
-// bare calendar date is as usable as a full timestamp — accept either.
+// bare calendar date is as usable as a full timestamp — accept either. The
+// datetime branch allows a `+HH:MM` offset, not just a `Z` suffix, since either
+// is a legitimate serialization of the same instant.
const dateBoundary = z.union([
- z.string().datetime(),
- z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'must be an ISO datetime or YYYY-MM-DD date')
+ z.string().datetime({ offset: true }),
+ z.string()
+ .regex(/^\d{4}-\d{2}-\d{2}$/, 'must be an ISO datetime or YYYY-MM-DD date')
+ .refine(isRealCalendarDate, 'must be a real calendar date')
]);
// List/filter query schema (GET /api/memory — validated against req.query).
@@ -116,8 +132,8 @@ const dateBoundary = z.union([
// has always enforced via parsePagination's maxLimit.
export const memoryListSchema = z.object({
types: csvList(z.array(memoryTypeEnum).optional()),
- categories: csvList(z.array(z.string()).optional()),
- tags: csvList(z.array(z.string()).optional()),
+ categories: csvList(z.array(z.string().max(100)).optional()),
+ tags: csvList(z.array(z.string().max(50)).optional()),
status: emptyToUndefined(memoryStatusEnum.optional().default('active')),
appId: emptyToUndefined(z.string().max(100).optional()),
limit: numeric(z.number().int().min(1).max(500).optional().default(50)),
@@ -131,6 +147,10 @@ export const memoryTimelineSchema = z.object({
startDate: emptyToUndefined(dateBoundary.optional()),
endDate: emptyToUndefined(dateBoundary.optional()),
types: csvList(z.array(memoryTypeEnum).optional()),
+ // Both timeline backends filter on appId (including the `__not_brain`
+ // sentinel the CoS Memory tab uses); it was unreachable while the handler
+ // hand-built its options object.
+ appId: emptyToUndefined(z.string().max(100).optional()),
limit: numeric(z.number().int().min(1).max(500).optional().default(100))
});
diff --git a/server/lib/memoryValidation.test.js b/server/lib/memoryValidation.test.js
index 8aa4cd2e47..d253274715 100644
--- a/server/lib/memoryValidation.test.js
+++ b/server/lib/memoryValidation.test.js
@@ -391,6 +391,22 @@ describe('memoryValidation.js', () => {
it('should reject a malformed date boundary', () => {
expect(memoryTimelineSchema.safeParse({ startDate: 'last tuesday' }).success).toBe(false);
});
+
+ it('should reject a date that matches the shape but is not a real day', () => {
+ expect(memoryTimelineSchema.safeParse({ startDate: '2026-02-30' }).success).toBe(false);
+ expect(memoryTimelineSchema.safeParse({ startDate: '2026-13-01' }).success).toBe(false);
+ expect(memoryTimelineSchema.safeParse({ startDate: '2024-02-29' }).success).toBe(true);
+ });
+
+ it('should accept a datetime with a UTC offset', () => {
+ expect(memoryTimelineSchema.safeParse({ startDate: '2026-01-01T00:00:00+02:00' }).success).toBe(true);
+ });
+
+ it('should accept an appId filter', () => {
+ const result = memoryTimelineSchema.safeParse({ appId: '__not_brain' });
+ expect(result.success).toBe(true);
+ expect(result.data.appId).toBe('__not_brain');
+ });
});
describe('memoryExtractSchema', () => {
diff --git a/server/routes/memory.test.js b/server/routes/memory.test.js
index 7d7a307f82..0bdae7adfd 100644
--- a/server/routes/memory.test.js
+++ b/server/routes/memory.test.js
@@ -267,6 +267,34 @@ describe('Memory Routes', () => {
expect(response.status).toBe(400);
expect(getMemories).not.toHaveBeenCalled();
});
+
+ it('should reject an unknown sortBy', async () => {
+ const response = await request(app).get('/api/memory?sortBy=unknown');
+
+ expect(response.status).toBe(400);
+ expect(getMemories).not.toHaveBeenCalled();
+ });
+
+ it('should reject an unknown sortOrder', async () => {
+ const response = await request(app).get('/api/memory?sortOrder=up');
+
+ expect(response.status).toBe(400);
+ expect(getMemories).not.toHaveBeenCalled();
+ });
+
+ it('should reject a negative offset', async () => {
+ const response = await request(app).get('/api/memory?offset=-1');
+
+ expect(response.status).toBe(400);
+ expect(getMemories).not.toHaveBeenCalled();
+ });
+
+ it('should reject an unknown status', async () => {
+ const response = await request(app).get('/api/memory?status=deleted');
+
+ expect(response.status).toBe(400);
+ expect(getMemories).not.toHaveBeenCalled();
+ });
});
// ===========================================================================
@@ -312,5 +340,21 @@ describe('Memory Routes', () => {
expect(response.status).toBe(400);
expect(getTimeline).not.toHaveBeenCalled();
});
+
+ it('should forward the appId filter to the backend', async () => {
+ getTimeline.mockResolvedValue({});
+
+ const response = await request(app).get('/api/memory/timeline?appId=__not_brain');
+
+ expect(response.status).toBe(200);
+ expect(getTimeline).toHaveBeenCalledWith({ appId: '__not_brain', limit: 100 });
+ });
+
+ it('should reject a date that matches the shape but is not a real day', async () => {
+ const response = await request(app).get('/api/memory/timeline?startDate=2026-02-30');
+
+ expect(response.status).toBe(400);
+ expect(getTimeline).not.toHaveBeenCalled();
+ });
});
});