Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(assistant): references tool uses a valid date window for preset ranges

Carl-Gerhard Lindesvärde824c13bc202
diff --git a/packages/core/src/modules/assistant/src/tools/references.test.ts b/packages/core/src/modules/assistant/src/tools/references.test.ts
new file mode 100644
index 000000000..2263563db
--- /dev/null
+++ b/packages/core/src/modules/assistant/src/tools/references.test.ts
@@ -0,0 +1,47 @@
+import { expect, test } from 'bun:test';
+import { getChartStartEndDate } from '../../../report/src/chart-dates';
+import { chatRunContext } from '../run-context';
+import { getReferenceDateWindow } from './references';
+
+const RUN_CONTEXT = {
+ userId: 'user_1',
+ projectId: 'proj_1',
+ organizationId: 'org_1',
+ timezone: 'Europe/Stockholm',
+};
+
+test('a preset range yields valid bounds matching the chart window', () => {
+ const window = chatRunContext.run(RUN_CONTEXT, () =>
+ getReferenceDateWindow({ range: '7d' })
+ );
+ const chartWindow = getChartStartEndDate(
+ { range: '7d', startDate: null, endDate: null },
+ RUN_CONTEXT.timezone
+ );
+
+ expect(Number.isNaN(window.gte.getTime())).toBe(false);
+ expect(Number.isNaN(window.lte.getTime())).toBe(false);
+ expect(window.gte).toEqual(new Date(chartWindow.startDate));
+ expect(window.lte).toEqual(new Date(chartWindow.endDate));
+});
+
+test('explicit dates cover the whole of both days like the chart window', () => {
+ const window = getReferenceDateWindow({
+ startDate: '2026-09-01',
+ endDate: '2026-09-10',
+ });
+ const chartWindow = getChartStartEndDate(
+ { range: '30d', startDate: '2026-09-01', endDate: '2026-09-10' },
+ 'UTC'
+ );
+
+ expect(window.gte).toEqual(new Date(chartWindow.startDate));
+ expect(window.lte).toEqual(new Date(chartWindow.endDate));
+});
+
+test('a start date alone runs to today', () => {
+ const window = getReferenceDateWindow({ startDate: '2026-09-01' });
+
+ expect(window.gte).toEqual(new Date('2026-09-01 00:00:00'));
+ expect(window.lte.getTime()).toBeGreaterThan(Date.now() - 86_400_000);
+});
diff --git a/packages/core/src/modules/assistant/src/tools/references.ts b/packages/core/src/modules/assistant/src/tools/references.ts
index 925087f60..6e8a04d04 100644
--- a/packages/core/src/modules/assistant/src/tools/references.ts
+++ b/packages/core/src/modules/assistant/src/tools/references.ts
@@ -1,5 +1,8 @@
import { z } from 'zod';
import type { ServiceDeps } from '../../../../services';
+import { getChartStartEndDate } from '../../../report/src/chart-dates';
+import type { PageContext } from '../context';
+import { chatRunContext } from '../run-context';
import { chatTool, resolveDateRange, truncateRows } from './helpers';
/**
@@ -8,6 +11,23 @@ import { chatTool, resolveDateRange, truncateRows } from './helpers';
* off-platform events.
*/
+// Never read: `resolveDateRange` always returns both bounds, which is the case
+// where `getChartStartEndDate` only normalizes them.
+const UNUSED_FALLBACK_RANGE = '30d';
+
+/** The same window the chart's reference annotations use for these filters. */
+export function getReferenceDateWindow(filters: PageContext['filters']): {
+ gte: Date;
+ lte: Date;
+} {
+ const timezone = chatRunContext.getStore()?.timezone ?? 'UTC';
+ const { startDate, endDate } = getChartStartEndDate(
+ { ...resolveDateRange(filters), range: UNUSED_FALLBACK_RANGE },
+ timezone
+ );
+ return { gte: new Date(startDate), lte: new Date(endDate) };
+}
+
export const listReferences = (deps: ServiceDeps) =>
chatTool(
{
@@ -31,33 +51,16 @@ export const listReferences = (deps: ServiceDeps) =>
}),
},
async ({ startDate, endDate, search, limit }, context) => {
- const range = resolveDateRange({
+ const dateWindow = getReferenceDateWindow({
...context.pageContext?.filters,
startDate: startDate ?? context.pageContext?.filters?.startDate,
endDate: endDate ?? context.pageContext?.filters?.endDate,
});
-
- // KNOWN DEFECT, deliberately left: these two bounds do not agree, and
- // neither is safe for every shape `resolveDateRange` returns. It yields
- // `YYYY-MM-DD` from explicit dates and from its 30-day default, but
- // `YYYY-MM-DD HH:mm:ss` in the PROJECT's timezone when the page carries a
- // range preset (`chart-dates.ts` `getDatesFromRange`). On that second
- // shape the `lte` concatenation builds "2026-09-10
- // 23:59:59T23:59:59.999Z", which is an Invalid Date, and the `gte` is
- // parsed as LOCAL time rather than UTC. Do NOT "align" the two by
- // appending `T00:00:00.000Z` to the `gte` — that reads as equivalent only
- // if `range.startDate` were always date-only, and it is not. Repairing it
- // changes which rows the preset path returns, and picking the right
- // window means settling which clock `Reference.date` is stored in —
- // `modules/reference`'s `getChartReferences` owns that answer.
const db = deps.db;
const rows = await db.reference.findMany({
where: {
projectId: context.projectId,
- date: {
- gte: new Date(range.startDate),
- lte: new Date(`${range.endDate}T23:59:59.999Z`),
- },
+ date: dateWindow,
...(search
? {
OR: [