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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: compute Timeline day totals and histogram from the full day (#11151)
[._.]/ Adam Eivy4bd79cc77cfe
diff --git a/client/src/pages/Timeline.jsx b/client/src/pages/Timeline.jsxindex cb57f09d55..856e212041 100644--- a/client/src/pages/Timeline.jsx+++ b/client/src/pages/Timeline.jsx@@ -162,6 +162,8 @@ export default function Timeline() {const events = day?.events || [];const counts = day?.counts || { total: 0, bySource: {}, byKind: {} };+ // `counts` / `histogram` cover the whole day; `events` is a bounded page of it.+ const hasMore = Boolean(day?.eventPage?.hasMore);const histogram = day?.histogram || Array.from({ length: 24 }, (_, hour) => ({ hour, count: 0 }));const dayLabel = useMemo(() => {@@ -270,6 +272,12 @@ export default function Timeline() {</div>) : (<div className="flex flex-col gap-2 pb-6">+ {hasMore && (+ <div role="status" className="rounded border border-port-border bg-port-card px-3 py-2 text-xs text-gray-400">+ Showing the latest {formatCount(day.eventPage.returned)} of {formatCount(day.eventPage.total)} events+ — earlier events are included in the totals and histogram above but not listed.+ </div>+ )}{events.map((event) => <EventRow key={event.id} event={event} timezone={day?.timezone} />)}</div>)}diff --git a/client/src/pages/Timeline.test.jsx b/client/src/pages/Timeline.test.jsxindex 715e54ef09..87119f09a8 100644--- a/client/src/pages/Timeline.test.jsx+++ b/client/src/pages/Timeline.test.jsx@@ -19,6 +19,7 @@ vi.mock('../services/api', () => ({importGmailMbox: vi.fn(),}));+import * as api from '../services/api';import Timeline from './Timeline';import { IMPORT_SOURCE_COUNT } from '../components/timeline/TimelineImportPanels';@@ -67,3 +68,25 @@ describe('Timeline import-history disclosure (#3789)', () => {expect(scrollIntoView).toHaveBeenCalledTimes(2);});});++describe('Timeline bounded event page (#11151)', () => {+ it('labels the listed rows as a page while totals stay full-day', async () => {+ api.getTimelineDay.mockResolvedValueOnce({+ date: '2026-01-02',+ today: '2026-01-02',+ timezone: 'UTC',+ events: [{ id: 'e1', source: 's', kind: 'message.received', happenedAt: '2026-01-02T12:00:00Z', title: 'Hi' }],+ eventPage: { total: 2001, returned: 1, limit: 2000, hasMore: true },+ counts: { total: 2001, bySource: { s: 2001 }, byKind: { 'message.received': 2001 } },+ histogram: Array.from({ length: 24 }, (_, hour) => ({ hour, count: hour === 8 ? 1 : 0 })),+ });+ await renderPage();+ expect(await screen.findByText('2,001 events')).toBeTruthy();+ expect(screen.getByRole('status').textContent).toMatch(/Showing the latest 1 of 2,001 events/);+ });++ it('shows no page notice when every event is listed', async () => {+ await renderPage();+ expect(screen.queryByRole('status')).toBeNull();+ });+});diff --git a/server/services/humanActivity.db.test.js b/server/services/humanActivity.db.test.jsindex f6ca6de7f1..66eb3cdb6e 100644--- a/server/services/humanActivity.db.test.js+++ b/server/services/humanActivity.db.test.js@@ -11,10 +11,14 @@* per-run nonce and is torn down in afterAll; assertions are relative to the* rows this suite inserts.*/-import { describe, it, expect, afterAll } from 'vitest';+import { describe, it, expect, afterAll, vi } from 'vitest';import { checkHealth, ensureSchema, close, query, withTransaction } from '../lib/db.js';import { requireDbOrSkip } from '../lib/dbTestGate.js';-import { recordEvents, listEvents, getDaySummary, stripParticipantsForAccount, countEventsByHandle } from './humanActivity.js';+// Pin the user timezone per test so day bounds are deterministic.+const tzState = vi.hoisted(() => ({ tz: 'UTC' }));+vi.mock('./userTimezone.js', () => ({ getUserTimezone: async () => tzState.tz }));++import { recordEvents, listEvents, getDaySummary, DAY_EVENT_PAGE_SIZE, stripParticipantsForAccount, countEventsByHandle } from './humanActivity.js';let dbReady = false;let skipReason = '';@@ -136,6 +140,76 @@ describe.skipIf(!runDb)('humanActivity store (#2150)', () => {});});+describe.skipIf(!runDb)('getDaySummary full-day aggregates (#11151)', () => {+ const DAY_SOURCE = `test-${nonce}-day`;+ const dayEvents = (n, iso, prefix, over = {}) => Array.from({ length: n }, (_, i) => ({+ source: DAY_SOURCE, kind: 'message.received', happenedAt: iso, dedupeKey: `${prefix}-${i}`, title: 'e', ...over,+ }));++ afterAll(async () => {+ tzState.tz = 'UTC';+ if (dbReady) await query('DELETE FROM human_activity_events WHERE source = $1', [DAY_SOURCE]).catch(() => {});+ });++ it('counts and buckets the whole day even when it exceeds the event page', async () => {+ tzState.tz = 'UTC';+ // Far-past day: no other suite or live data lands here.+ const date = '2001-03-04';+ const before = await getDaySummary({ date });+ await recordEvents([+ ...dayEvents(DAY_EVENT_PAGE_SIZE, '2001-03-04T12:00:00Z', 'noon'),+ ...dayEvents(1, '2001-03-04T08:00:00Z', 'early', { kind: 'calendar.event' }),+ ]);+ const s = await getDaySummary({ date });+ expect(s.counts.total - before.counts.total).toBe(DAY_EVENT_PAGE_SIZE + 1);+ expect(s.counts.bySource[DAY_SOURCE]).toBe(DAY_EVENT_PAGE_SIZE + 1);+ expect(s.counts.byKind['calendar.event'] - (before.counts.byKind['calendar.event'] || 0)).toBe(1);+ expect(s.histogram[8].count - before.histogram[8].count).toBe(1);+ expect(s.histogram[12].count - before.histogram[12].count).toBe(DAY_EVENT_PAGE_SIZE);+ // The listed page stays bounded and is honest about it.+ expect(s.events.length).toBeLessThanOrEqual(DAY_EVENT_PAGE_SIZE);+ expect(s.eventPage).toEqual({ total: s.counts.total, returned: s.events.length, limit: DAY_EVENT_PAGE_SIZE, hasMore: true });+ // Oldest-first display order.+ const times = s.events.map((e) => new Date(e.happenedAt).getTime());+ expect(times).toEqual([...times].sort((a, b) => a - b));+ });++ it('uses the same exclusive local-day bounds for aggregates and rows across a DST day', async () => {+ tzState.tz = 'America/Los_Angeles';+ // 2001-04-01 was a 23-hour day in Los Angeles: 08:00Z .. 07:00Z next day.+ const date = '2001-04-01';+ await recordEvents([+ { source: DAY_SOURCE, kind: 'message.sent', happenedAt: '2001-04-01T07:59:59Z', dedupeKey: 'dst-prev' },+ { source: DAY_SOURCE, kind: 'message.sent', happenedAt: '2001-04-01T08:00:00Z', dedupeKey: 'dst-start' },+ { source: DAY_SOURCE, kind: 'message.sent', happenedAt: '2001-04-02T06:59:59Z', dedupeKey: 'dst-last' },+ { source: DAY_SOURCE, kind: 'message.sent', happenedAt: '2001-04-02T07:00:00Z', dedupeKey: 'dst-next' },+ ]);+ const s = await getDaySummary({ date });+ const mine = s.events.filter((e) => e.source === DAY_SOURCE).map((e) => e.dedupeKey).sort();+ expect(mine).toEqual(['dst-last', 'dst-start']);+ expect(s.counts.bySource[DAY_SOURCE]).toBe(2);+ expect(s.histogram[0].count).toBeGreaterThanOrEqual(1); // 08:00Z = local 00:00+ expect(s.histogram[23].count).toBeGreaterThanOrEqual(1); // 06:59:59Z next day = local 23:59+ expect(s.eventPage.total).toBe(s.counts.total);+ });++ it('keeps aggregates and the page consistent when rows are inserted mid-read', async () => {+ tzState.tz = 'UTC';+ const date = '2001-05-06';+ await recordEvents(dayEvents(3, '2001-05-06T10:00:00Z', 'base'));+ // Insert concurrently with the read; whichever side of the snapshot the+ // insert lands on, total and page must describe the same row set.+ const [s] = await Promise.all([+ getDaySummary({ date }),+ recordEvents(dayEvents(5, '2001-05-06T11:00:00Z', 'racer')),+ ]);+ const listed = s.events.filter((e) => e.source === DAY_SOURCE).length;+ expect(s.eventPage.returned).toBe(s.events.length);+ expect(s.counts.bySource[DAY_SOURCE]).toBe(listed);+ expect(s.counts.total).toBe(s.eventPage.total);+ });+});+describe.skipIf(!runDb)('stripParticipantsForAccount — send-as alias backfill (#2855)', () => {const ACCT = `acct-${nonce}`;const OTHER_ACCT = `other-${nonce}`;diff --git a/server/services/humanActivity.js b/server/services/humanActivity.jsindex b1ece68a6f..0e579dab84 100644--- a/server/services/humanActivity.js+++ b/server/services/humanActivity.js@@ -21,7 +21,7 @@* email/handle index. No AI-provider calls happen here.*/import { v4 as uuidv4 } from '../lib/uuid.js';-import { ensureSchema, query } from '../lib/db.js';+import { ensureSchema, query, withTransaction } from '../lib/db.js';import { normalizeIdentifier } from '../lib/tribeMatch.js';import { getLocalParts, localDayRangeUtc, todayInTimezone, anchorLocalMidnightUtc } from '../lib/timezone.js';import { getUserTimezone } from './userTimezone.js';@@ -631,8 +631,20 @@ export async function sourceStats(source) {};}-// Day view: all events on a local calendar day plus an hourly histogram and-// source/kind tallies. `date` defaults to today in the user's timezone.+// Max event rows hydrated for the day view. The day's counts and histogram are+// aggregated in SQL over the WHOLE day and are independent of this page size.+export const DAY_EVENT_PAGE_SIZE = 2000;++// Day view: a bounded page of events on a local calendar day plus full-day+// aggregates (hourly histogram, source/kind tallies). `date` defaults to today+// in the user's timezone.+//+// `counts` / `histogram` cover every event in the day regardless of how many+// are hydrated into `events`. `events` holds the NEWEST `DAY_EVENT_PAGE_SIZE`+// rows, returned oldest-first; `eventPage` says how complete that page is+// ({ total, returned, limit, hasMore }). The aggregates and the page are read+// in one REPEATABLE READ snapshot so concurrent ingestion cannot make them+// disagree, and share the same exclusive [start, end) UTC bounds.export async function getDaySummary({ date } = {}) {const timezone = await getUserTimezone();// `today` is computed in the USER's configured timezone (not the browser's) and@@ -641,16 +653,46 @@ export async function getDaySummary({ date } = {}) {const today = todayInTimezone(timezone);const day = /^\d{4}-\d{2}-\d{2}$/.test(String(date || '')) ? date : today;const range = localDayRangeUtc(day, timezone);- if (!range) return { date: day, today, timezone, events: [], histogram: hourlyHistogram([], timezone), counts: summarizeCounts([]) };- const events = await listEvents({ from: range.start.toISOString(), to: range.end.toISOString(), limit: 2000 });- // listEvents returns newest-first; a day view reads better oldest-first.- const ordered = [...events].reverse();- return {- date: day,- today,- timezone,- events: ordered,- histogram: hourlyHistogram(ordered, timezone),- counts: summarizeCounts(ordered),- };+ const eventPage = (returned, total) => ({ total, returned, limit: DAY_EVENT_PAGE_SIZE, hasMore: returned < total });+ if (!range) {+ return { date: day, today, timezone, events: [], eventPage: eventPage(0, 0), histogram: hourlyHistogram([], timezone), counts: summarizeCounts([]) };+ }+ await ensureReady();+ const bounds = [range.start.toISOString(), range.end.toISOString()];+ const { aggregates, rows } = await withTransaction(async (client) => {+ // Must be the transaction's first statement: every query below then sees+ // one snapshot.+ await client.query('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');+ const agg = await client.query(+ `SELECT 'source' AS dim, source AS key, COUNT(*)::int AS n+ FROM human_activity_events WHERE happened_at >= $1 AND happened_at < $2 GROUP BY source+ UNION ALL+ SELECT 'kind', kind, COUNT(*)::int+ FROM human_activity_events WHERE happened_at >= $1 AND happened_at < $2 GROUP BY kind+ UNION ALL+ SELECT 'hour', EXTRACT(HOUR FROM happened_at AT TIME ZONE $3)::int::text, COUNT(*)::int+ FROM human_activity_events WHERE happened_at >= $1 AND happened_at < $2 GROUP BY 2`,+ [...bounds, timezone],+ );+ const page = await client.query(+ `SELECT * FROM human_activity_events+ WHERE happened_at >= $1 AND happened_at < $2+ ORDER BY happened_at DESC LIMIT $3`,+ [...bounds, DAY_EVENT_PAGE_SIZE],+ );+ return { aggregates: agg.rows, rows: page.rows };+ });++ const bySource = {};+ const byKind = {};+ const histogram = Array.from({ length: 24 }, (_, hour) => ({ hour, count: 0 }));+ for (const { dim, key, n } of aggregates) {+ if (dim === 'source') bySource[key || 'unknown'] = (bySource[key || 'unknown'] || 0) + n;+ else if (dim === 'kind') byKind[key || 'unknown'] = (byKind[key || 'unknown'] || 0) + n;+ else if (histogram[Number(key)]) histogram[Number(key)].count += n;+ }+ const total = Object.values(bySource).reduce((sum, n) => sum + n, 0);+ // Newest-first from SQL; a day view reads better oldest-first.+ const events = rows.map(rowToEvent).reverse();+ return { date: day, today, timezone, events, eventPage: eventPage(events.length, total), histogram, counts: { bySource, byKind, total } };}