Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

refactor: throttle device daily presence updates and refresh lastSeenAt on close

0xken15db8153e95860
diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma
index 6f1bf3d77..3d278c2ba 100644
--- a/backend/prisma/schema.prisma
+++ b/backend/prisma/schema.prisma
@@ -494,8 +494,13 @@ model UserDailyRemoteUsage {
}
/// Per-day record of whether a user's device was connected to the device-ws relay
-/// (src/lib/deviceWs.ts). One row per (user, device, UTC day); a connection spanning
-/// midnight produces a row for each day it touches.
+/// (src/lib/deviceWs.ts). One row per (user, device, UTC day); a connection spanning midnight
+/// produces a row for each day it touches, the later ones with `connections: 0`.
+/// `lastSeenAt` is refreshed on connect, on close, and at most every DEVICE_PRESENCE_WRITE_MS while
+/// the socket is open, so it lags real liveness by <= that interval — the same rule as
+/// MachineDailyPresence. (Before 2026-09-23 it only moved on a RECONNECT, so a device that stayed
+/// connected read as last-seen-at-midnight all day; rows older than that cannot be used to measure
+/// how long a device was up.) Redis `device:{id}` remains the "online right now" answer.
model UserDailyDevicePresence {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String
diff --git a/backend/src/lib/dailyTracking.test.ts b/backend/src/lib/dailyTracking.test.ts
index 26199a723..b55815b9c 100644
--- a/backend/src/lib/dailyTracking.test.ts
+++ b/backend/src/lib/dailyTracking.test.ts
@@ -4,6 +4,7 @@ const db = vi.hoisted(() => ({
userPresenceUpsert: vi.fn(),
machinePresenceUpsert: vi.fn(),
agentPresenceUpsert: vi.fn(),
+ devicePresenceUpsert: vi.fn(),
}))
vi.mock('./prisma.js', () => ({
@@ -11,10 +12,11 @@ vi.mock('./prisma.js', () => ({
userDailyPresence: { upsert: db.userPresenceUpsert },
machineDailyPresence: { upsert: db.machinePresenceUpsert },
agentDailyPresence: { upsert: db.agentPresenceUpsert },
+ userDailyDevicePresence: { upsert: db.devicePresenceUpsert },
},
}))
-import { presenceWriteDue, recordTurnStarted, touchMachineOnlineDay, touchUserOnlineDay } from './dailyTracking.js'
+import { presenceWriteDue, recordTurnStarted, touchDeviceOnlineDay, touchMachineOnlineDay, touchUserOnlineDay } from './dailyTracking.js'
import { utcDayStart } from '../types/analytics.js'
describe('touchUserOnlineDay', () => {
@@ -49,6 +51,38 @@ describe('touchUserOnlineDay', () => {
})
})
+describe('touchDeviceOnlineDay', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ db.devicePresenceUpsert.mockResolvedValue({})
+ })
+
+ it('a connect upserts the (user, device, UTC day) row, bumps connections and stamps the country', async () => {
+ const now = new Date('2026-09-23T10:15:30.000Z')
+ await touchDeviceOnlineDay('user-1', 'device-a', now, { isNewConnection: true, countryCode: 'VN' })
+
+ const call = db.devicePresenceUpsert.mock.calls[0][0]
+ expect(call.where).toEqual({
+ userId_deviceId_dayUtc: { userId: 'user-1', deviceId: 'device-a', dayUtc: utcDayStart(now) },
+ })
+ expect(call.create).toEqual({
+ userId: 'user-1', deviceId: 'device-a', dayUtc: utcDayStart(now),
+ connections: 1, firstSeenAt: now, lastSeenAt: now, countryCode: 'VN',
+ })
+ expect(call.update).toEqual({ lastSeenAt: now, connections: { increment: 1 }, countryCode: 'VN' })
+ })
+
+ it('a refresh on an open socket only moves lastSeenAt — and a row it creates after midnight counts no connection', async () => {
+ const now = new Date('2026-09-23T00:05:00.000Z')
+ await touchDeviceOnlineDay('user-1', 'device-a', now, { isNewConnection: false })
+
+ const call = db.devicePresenceUpsert.mock.calls[0][0]
+ expect(call.update).toEqual({ lastSeenAt: now })
+ expect(call.create.connections).toBe(0)
+ expect(call.create.countryCode).toBeUndefined()
+ })
+})
+
describe('touchMachineOnlineDay', () => {
beforeEach(() => {
vi.clearAllMocks()
diff --git a/backend/src/lib/deviceWs.ts b/backend/src/lib/deviceWs.ts
index f7dadf544..4eba13278 100644
--- a/backend/src/lib/deviceWs.ts
+++ b/backend/src/lib/deviceWs.ts
@@ -28,7 +28,7 @@ import { transcribe, MAX_PCM, normalizeLang } from './stt.js'
import { reserveVoice, releaseVoice } from './voiceBudget.js'
import { prisma } from './prisma.js'
import { deviceService } from '../services/index.js'
-import { touchDeviceOnlineDay } from './dailyTracking.js'
+import { presenceWriteDue, touchDeviceOnlineDay, type PresenceWriteState } from './dailyTracking.js'
import { countryCodeFromHeaders } from './clientGeo.js'
import { utcDayKey } from '../types/analytics.js'
import { agentLimit, recordCreatedAgent } from './agentTracker.js'
@@ -76,6 +76,11 @@ const wss = createWss(WS_LIMITS.device, { echoFirstProtocol: true })
// Presence refresh must beat the key TTL (45s in bus.setDevicePresence) with margin.
const DEVICE_PRESENCE_REFRESH_MS = 15_000
+// Daily device presence (`user_daily_device_presence`): how often an OPEN device socket refreshes its
+// row's `lastSeenAt`. Same value and same reasoning as MACHINE_PRESENCE_WRITE_MS in adapterWs.ts —
+// this rides the 15s presence tick above, so the constant is only the floor between two Mongo writes
+// and a device that is up all day costs ~300 upserts, not ~6000. Connect and close always write.
+const DEVICE_PRESENCE_WRITE_MS = 5 * 60_000
// How long a graceful close gets to complete before the socket is forced shut.
const CLOSE_GRACE_MS = 2_000
// Device chunks normally arrive every ~20ms. A 10s idle window tolerates transient network stalls
@@ -460,30 +465,36 @@ function relay(device: WebSocket, opts: RelayOpts): void {
let presenceDeviceId: string | null = null
let controlUnsub: (() => void) | null = null
let machineListUnsub: (() => void) | null = null
- // Daily device presence: mark today online on first presence start for this connection, and
- // re-check on the existing 15s refresh tick / on close so a connection spanning UTC midnight
- // gets counted for the new day too (mirrors webWs.ts's touchUserOnlineDay/touchPresence). The
- // guard only advances on a SUCCESSFUL write, so a transient DB failure gets retried on the next
- // tick instead of being silently skipped for the rest of the day.
- let lastDevicePresenceDayKey: string | null = null
- const touchDevicePresence = (id: string, isNewConnection: boolean): void => {
- const now = new Date()
- const dayKey = utcDayKey(now)
- if (!isNewConnection && dayKey === lastDevicePresenceDayKey) return
- touchDeviceOnlineDay(userId, id, now, { isNewConnection, countryCode: opts.countryCode })
- .then(() => { lastDevicePresenceDayKey = dayKey })
- .catch((err) => logger.warn('device presence tracking failed', { userId, deviceId: id, error: String(err) }))
+ // Daily device presence: mark today online when this connection starts, refresh `lastSeenAt` on the
+ // existing 15s tick (floored to DEVICE_PRESENCE_WRITE_MS), and write once more on close. Same shape
+ // as adapterWs.ts's machine presence, and for the same reason: until this rode the floor instead of
+ // a day-key guard, `lastSeenAt` only ever moved on a RECONNECT, so a device that stayed connected
+ // read as last-seen-at-midnight all day and no session length could be derived from the row at all.
+ // `presenceWriteDue` also writes whenever the UTC day rolled over since the last write, which is
+ // what gives a connection spanning midnight its row on the new day (with `connections: 0`).
+ // The state only advances on a SUCCESSFUL write, so a transient DB failure is retried on the next
+ // tick rather than silently skipped for the rest of the interval.
+ const lastDevicePresence: PresenceWriteState = { dayKey: null, wroteAt: 0 }
+ let devicePresenceInFlight = false
+ const touchDevicePresence = (id: string, kind: 'connect' | 'tick' | 'close', at?: Date): void => {
+ const now = at ?? new Date()
+ if (kind === 'tick' && (devicePresenceInFlight || !presenceWriteDue(lastDevicePresence, now, DEVICE_PRESENCE_WRITE_MS))) return
+ devicePresenceInFlight = true
+ touchDeviceOnlineDay(userId, id, now, { isNewConnection: kind === 'connect', countryCode: opts.countryCode })
+ .then(() => { lastDevicePresence.dayKey = utcDayKey(now); lastDevicePresence.wroteAt = now.getTime() })
+ .catch((err) => logger.warn('device presence tracking failed', { userId, deviceId: id, kind, error: String(err) }))
+ .finally(() => { devicePresenceInFlight = false })
}
const startPresence = (id: string): void => {
if (closed || presenceDeviceId === id) return
presenceDeviceId = id
void setDevicePresence(id, connToken)
void publishDeviceStatus(userId, { deviceId: id, online: true })
- touchDevicePresence(id, true)
+ touchDevicePresence(id, 'connect')
if (presenceTimer) clearInterval(presenceTimer)
presenceTimer = setInterval(() => {
void setDevicePresence(id, connToken)
- touchDevicePresence(id, false)
+ touchDevicePresence(id, 'tick')
}, DEVICE_PRESENCE_REFRESH_MS)
// Control channel: a revoke must reach the device even when it's parked on the machine PICKER
// (no hub attach → the machine-targeted pushDeviceRevoked can't reach it). Deliver the frame the
@@ -873,10 +884,17 @@ function relay(device: WebSocket, opts: RelayOpts): void {
// keeps its own key → we stay silent). lastSeenAt = the moment the device actually went away.
const seenAt = new Date()
fireAndForget(clearDevicePresence(id, connToken).then((gone) => {
- if (gone) return publishDeviceStatus(userId, { deviceId: id, online: false, lastSeenAt: seenAt.toISOString() })
+ if (!gone) return
+ // Only a socket that REALLY owned this device writes the goodbye. `gone` is false when a
+ // superseding reconnect already holds the presence key: the device is online on the newer
+ // socket, and letting this dying one stamp `lastSeenAt` would date the row to the moment the
+ // OLD connection was reaped (up to ~100s after the handover) — the same reason the offline
+ // publish above is conditional, and the same rule adapterWs.ts applies to a superseded
+ // adapter. `seenAt` rather than "now": the moment of the close, not of the Redis round trip.
+ touchDevicePresence(id, 'close', seenAt)
+ return publishDeviceStatus(userId, { deviceId: id, online: false, lastSeenAt: seenAt.toISOString() })
}), 'device offline publish', { userId, deviceId: id })
void prisma.deviceBinding.update({ where: { deviceId: id }, data: { lastSeenAt: seenAt } }).catch(() => { /* best effort */ })
- touchDevicePresence(id, false)
}
for (const [, unsub] of statusSubs) unsub()
statusSubs.clear()