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.

feat: add daily machine presence tracking for adapter websocket connections and update daily tracking module

0xken1556a9633a596b
diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma
index 2c3a023b0..a4be87f6e 100644
--- a/backend/prisma/schema.prisma
+++ b/backend/prisma/schema.prisma
@@ -421,7 +421,8 @@ model AnalyticsMachineState {
}
/// Presence/remote-usage tracking — separate from the Analytics family above (not opt-in, not
-/// collector-reported; derived directly from the web-ws relay in src/lib/webWs.ts).
+/// collector-reported; derived directly from the relays: web-ws in src/lib/webWs.ts, device-ws in
+/// src/lib/deviceWs.ts, adapter-ws in src/lib/adapterWs.ts).
/// Per-day record of whether a user's desktop app was connected to the web-ws relay. One row per
/// (user, UTC day). A connection spanning midnight produces a row for each day it touches.
@@ -475,3 +476,24 @@ model UserDailyDevicePresence {
@@index([userId, dayUtc])
@@map("user_daily_device_presence")
}
+
+/// Per-day record of whether a MACHINE's `harness` daemon was connected to the adapter-ws relay
+/// (src/lib/adapterWs.ts). One row per (machine, UTC day); a connection spanning midnight produces a
+/// row for each day it touches. `lastSeenAt` is refreshed on connect, on close, and at most every
+/// MACHINE_PRESENCE_WRITE_MS while the socket is open, so it lags real liveness by ≤ that interval.
+/// Redis `machine:{id}:mgr` remains the "online right now" answer; this is the history.
+model MachineDailyPresence {
+ id String @id @default(auto()) @map("_id") @db.ObjectId
+ machineId String // Machine.machineId
+ userId String // owner at connect time (Machine.userId), for per-user rollups
+ dayUtc DateTime
+ connections Int @default(0)
+ firstSeenAt DateTime
+ lastSeenAt DateTime
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+
+ @@unique([machineId, dayUtc])
+ @@index([userId, dayUtc])
+ @@map("machine_daily_presence")
+}
diff --git a/backend/src/lib/adapterWs.ts b/backend/src/lib/adapterWs.ts
index 44d23eab2..69192b01f 100644
--- a/backend/src/lib/adapterWs.ts
+++ b/backend/src/lib/adapterWs.ts
@@ -25,9 +25,11 @@ import {
import { trackSocketLiveness } from './hub.js'
import { guardedSend, guardedSendJson } from './wsSend.js'
import { attachNodeRole, PRESENCE_TTL_SEC } from './nodeRole.js'
+import { presenceWriteDue, touchMachineOnlineDay, type PresenceWriteState } from './dailyTracking.js'
import { recordCreatedAgent, recordDeletedAgent } from './agentTracker.js'
import type { Frame } from './tunnel.js'
import { logger } from '../utils/logger.js'
+import { utcDayKey } from '../types/analytics.js'
import { machineBillingAllowsDataPlane } from './billingState.js'
import { normalizeComputerId } from './deviceAuth.js'
import { authenticateAccessToken, SsoAuthError } from './ssoAuth.js'
@@ -74,6 +76,12 @@ const APP_STATES = new Set(['open', 'closed', 'missing'])
// semver-ish token (or longer than 64 chars) is dropped rather than stored.
const CLIENT_VERSION_RE = /^[A-Za-z0-9._+-]{1,64}$/
+// Daily presence (`machine_daily_presence`): how often an OPEN adapter socket refreshes its row's
+// `lastSeenAt`. Rides the node role's existing 15s heartbeat (plus every app ping) rather than a
+// timer of its own — this is just the floor between two Mongo writes, so a machine that is up all
+// day costs ~300 upserts, not ~6000. Connect and close always write regardless.
+const MACHINE_PRESENCE_WRITE_MS = 5 * 60_000
+
function sanitizeClientVersion(raw: string | null): string | undefined {
const v = raw?.trim()
return v && CLIENT_VERSION_RE.test(v) ? v : undefined
@@ -216,6 +224,24 @@ async function attachAdapter(ws: WebSocket, machineId: string, userId: string, c
// unnamed so a stale mirror clears. Renames while connected arrive the same way (machine_meta).
send({ t: 'down', connId: '', frame: { type: 'machine_meta', payload: { name: currentName?.trim() || seededName } } })
+ // Daily presence row for the machine, mirroring the user/device variants in webWs/deviceWs. The
+ // guard only advances on a SUCCESSFUL write, so a transient DB failure is retried on the next
+ // heartbeat instead of being silently skipped until the interval elapses again. Heartbeats also
+ // skip while a write is still in flight: `onHeartbeat` fires from two 15s sources (node role
+ // interval + app ping), and before the connect write lands `presenceWriteDue` would say "due" —
+ // without this a slow Mongo gets a second concurrent upsert precisely when it is already slow.
+ const lastMachinePresence: PresenceWriteState = { dayKey: null, wroteAt: 0 }
+ let machinePresenceInFlight = false
+ const touchMachinePresence = (kind: 'connect' | 'heartbeat' | 'close'): void => {
+ const now = new Date()
+ if (kind === 'heartbeat' && (machinePresenceInFlight || !presenceWriteDue(lastMachinePresence, now, MACHINE_PRESENCE_WRITE_MS))) return
+ machinePresenceInFlight = true
+ touchMachineOnlineDay(userId, machineId, now, { isNewConnection: kind === 'connect' })
+ .then(() => { lastMachinePresence.dayKey = utcDayKey(now); lastMachinePresence.wroteAt = now.getTime() })
+ .catch((err) => logger.warn('machine presence tracking failed', { machineId, kind, error: String(err) }))
+ .finally(() => { machinePresenceInFlight = false })
+ }
+
// The node role — down subscription, presence, `__clients` resync, node_status — is shared with
// every other backer of a machine and lives in nodeRole.ts. Only the delivery and teardown are
// adapter-specific, so only those are passed in.
@@ -244,8 +270,10 @@ async function attachAdapter(ws: WebSocket, machineId: string, userId: string, c
if (computerId) void claimMachineOwner(machineId, computerId, PRESENCE_TTL_SEC) // renew the one-computer claim
// Same tick renews the app-state key, so it shares presence's TTL and can never outlive it.
if (appState) void setMachineAppState(machineId, appState.engine, appState.state, PRESENCE_TTL_SEC)
+ touchMachinePresence('heartbeat') // throttled inside — see MACHINE_PRESENCE_WRITE_MS
},
})
+ touchMachinePresence('connect')
const terminalDownUnsub = await subscribeTerminalDown(machineId, (packet) => {
// Terminal input toward the adapter is 'must': dropping keystrokes is not recoverable client-side.
guardedSend(ws, packet, 'must', { machineId, kind: 'adapter' })
@@ -375,6 +403,7 @@ async function attachAdapter(ws: WebSocket, machineId: string, userId: string, c
// and every row builder omits the app keys when offline, so the device converges on that frame;
// this delete is what stops a reconnect from briefly re-reading the dead value.
void clearMachineAppState(machineId)
+ touchMachinePresence('close') // exact lastSeenAt at disconnect; a superseded socket must not stamp it
logger.info('adapter disconnected', { machineId })
}
}
diff --git a/backend/src/lib/dailyTracking.test.ts b/backend/src/lib/dailyTracking.test.ts
new file mode 100644
index 000000000..62b0e3649
--- /dev/null
+++ b/backend/src/lib/dailyTracking.test.ts
@@ -0,0 +1,79 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+const db = vi.hoisted(() => ({
+ machinePresenceUpsert: vi.fn(),
+}))
+
+vi.mock('./prisma.js', () => ({
+ prisma: {
+ machineDailyPresence: { upsert: db.machinePresenceUpsert },
+ },
+}))
+
+import { presenceWriteDue, touchMachineOnlineDay } from './dailyTracking.js'
+import { utcDayStart } from '../types/analytics.js'
+
+describe('touchMachineOnlineDay', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ db.machinePresenceUpsert.mockResolvedValue({})
+ })
+
+ it('upserts the (machine, UTC day) row and bumps connections on a new connection', async () => {
+ const now = new Date('2026-09-16T10:15:30.000Z')
+ await touchMachineOnlineDay('user-1', 'machine-a', now, { isNewConnection: true })
+
+ expect(db.machinePresenceUpsert).toHaveBeenCalledTimes(1)
+ const call = db.machinePresenceUpsert.mock.calls[0][0]
+ expect(call.where).toEqual({ machineId_dayUtc: { machineId: 'machine-a', dayUtc: utcDayStart(now) } })
+ expect(call.create).toEqual({
+ machineId: 'machine-a', userId: 'user-1', dayUtc: utcDayStart(now),
+ connections: 1, firstSeenAt: now, lastSeenAt: now,
+ })
+ expect(call.update).toEqual({ lastSeenAt: now, connections: { increment: 1 } })
+ })
+
+ it('only touches lastSeenAt on a heartbeat/close (no connections increment)', async () => {
+ const now = new Date('2026-09-16T23:59:59.000Z')
+ await touchMachineOnlineDay('user-1', 'machine-a', now, { isNewConnection: false })
+
+ const call = db.machinePresenceUpsert.mock.calls[0][0]
+ expect(call.update).toEqual({ lastSeenAt: now })
+ // First write of a new UTC day from a session that spans midnight: the row exists, but no
+ // connection was opened on that day.
+ expect(call.create.connections).toBe(0)
+ })
+
+ it('propagates a DB failure so the caller can log it and keep its guard unchanged', async () => {
+ db.machinePresenceUpsert.mockRejectedValueOnce(new Error('mongo down'))
+ await expect(
+ touchMachineOnlineDay('user-1', 'machine-a', new Date(), { isNewConnection: true }),
+ ).rejects.toThrow('mongo down')
+ })
+})
+
+describe('presenceWriteDue', () => {
+ const FIVE_MIN = 5 * 60_000
+ const t0 = new Date('2026-09-16T10:00:00.000Z')
+
+ it('is due before anything has been written', () => {
+ expect(presenceWriteDue({ dayKey: null, wroteAt: 0 }, t0, FIVE_MIN)).toBe(true)
+ })
+
+ it('is not due within the interval on the same UTC day', () => {
+ const last = { dayKey: '2026-09-16', wroteAt: t0.getTime() }
+ expect(presenceWriteDue(last, new Date(t0.getTime() + 15_000), FIVE_MIN)).toBe(false)
+ expect(presenceWriteDue(last, new Date(t0.getTime() + FIVE_MIN - 1), FIVE_MIN)).toBe(false)
+ })
+
+ it('is due once the interval has elapsed', () => {
+ const last = { dayKey: '2026-09-16', wroteAt: t0.getTime() }
+ expect(presenceWriteDue(last, new Date(t0.getTime() + FIVE_MIN), FIVE_MIN)).toBe(true)
+ })
+
+ it('is due when the UTC day rolled over, even inside the interval', () => {
+ const lateNight = new Date('2026-09-16T23:59:50.000Z')
+ const last = { dayKey: '2026-09-16', wroteAt: lateNight.getTime() }
+ expect(presenceWriteDue(last, new Date('2026-09-17T00:00:05.000Z'), FIVE_MIN)).toBe(true)
+ })
+})
diff --git a/backend/src/lib/dailyTracking.ts b/backend/src/lib/dailyTracking.ts
index fbb893e7a..375d26869 100644
--- a/backend/src/lib/dailyTracking.ts
+++ b/backend/src/lib/dailyTracking.ts
@@ -1,9 +1,10 @@
/**
* Presence / remote-usage daily tracking — write path for `UserDailyPresence`,
- * `UserDailyRemoteUsage` and `UserDailyDevicePresence` (prisma/schema.prisma). Separate model
- * family and separate module from the opt-in `Analytics*` telemetry in analyticsIngest.ts: these
- * signals are derived directly from the web-ws/device-ws relays (src/lib/webWs.ts,
- * src/lib/deviceWs.ts), not from a collector upload.
+ * `UserDailyRemoteUsage`, `UserDailyDevicePresence` and `MachineDailyPresence`
+ * (prisma/schema.prisma). Separate model family and separate module from the opt-in `Analytics*`
+ * telemetry in analyticsIngest.ts: these signals are derived directly from the web-ws/device-ws/
+ * adapter-ws relays (src/lib/webWs.ts, src/lib/deviceWs.ts, src/lib/adapterWs.ts), not from a
+ * collector upload.
*
* Unlike analyticsIngest.ts, none of these rows need last-write-wins ordering (there's no
* client-supplied revision to defend against), so each touch is a plain atomic `upsert` on the
@@ -14,12 +15,17 @@
* periodic re-seed, p2p_offer) — callers must not await them inline.
*/
import { prisma } from './prisma.js'
-import { utcDayStart } from '../types/analytics.js'
+import { utcDayKey, utcDayStart } from '../types/analytics.js'
/**
* Mark a user online for the UTC day containing `now`. Call once on connect
* (`isNewConnection: true`, bumps `connections`) and again whenever a day-boundary check on an
* open connection finds the day has changed (`isNewConnection: false`, just touches `lastSeenAt`).
+ *
+ * `connections` counts connections OPENED on that day: a touch that is the first write of a new
+ * UTC day but is not a connect (a session spanning midnight) creates the row with `connections: 0`,
+ * so summing the column across days never double-counts one long session. Same rule for every
+ * `touch*OnlineDay` below.
*/
export async function touchUserOnlineDay(
userId: string,
@@ -29,7 +35,7 @@ export async function touchUserOnlineDay(
const dayUtc = utcDayStart(now)
await prisma.userDailyPresence.upsert({
where: { userId_dayUtc: { userId, dayUtc } },
- create: { userId, dayUtc, connections: 1, firstSeenAt: now, lastSeenAt: now },
+ create: { userId, dayUtc, connections: opts.isNewConnection ? 1 : 0, firstSeenAt: now, lastSeenAt: now },
update: {
lastSeenAt: now,
...(opts.isNewConnection ? { connections: { increment: 1 } } : {}),
@@ -63,10 +69,47 @@ export async function touchDeviceOnlineDay(
const dayUtc = utcDayStart(now)
await prisma.userDailyDevicePresence.upsert({
where: { userId_deviceId_dayUtc: { userId, deviceId, dayUtc } },
- create: { userId, deviceId, dayUtc, connections: 1, firstSeenAt: now, lastSeenAt: now },
+ create: { userId, deviceId, dayUtc, connections: opts.isNewConnection ? 1 : 0, firstSeenAt: now, lastSeenAt: now },
+ update: {
+ lastSeenAt: now,
+ ...(opts.isNewConnection ? { connections: { increment: 1 } } : {}),
+ },
+ })
+}
+
+/**
+ * Mark a machine's `harness` daemon online for the UTC day containing `now`, mirroring
+ * `touchUserOnlineDay` for the adapter-ws relay (src/lib/adapterWs.ts). `userId` is the owner at
+ * connect time, denormalized so per-user rollups need no join through `machines`.
+ */
+export async function touchMachineOnlineDay(
+ userId: string,
+ machineId: string,
+ now: Date,
+ opts: { isNewConnection: boolean },
+): Promise<void> {
+ const dayUtc = utcDayStart(now)
+ await prisma.machineDailyPresence.upsert({
+ where: { machineId_dayUtc: { machineId, dayUtc } },
+ create: { machineId, userId, dayUtc, connections: opts.isNewConnection ? 1 : 0, firstSeenAt: now, lastSeenAt: now },
update: {
lastSeenAt: now,
...(opts.isNewConnection ? { connections: { increment: 1 } } : {}),
},
})
}
+
+/** Last SUCCESSFUL presence write for one open socket — `dayKey` null until the first one lands. */
+export interface PresenceWriteState { dayKey: string | null; wroteAt: number }
+
+/**
+ * Whether a heartbeat on an open socket should write presence now: yes when nothing has been
+ * written yet, when the UTC day rolled over since the last write (so the new day gets its row),
+ * or when the last write is at least `minIntervalMs` old. Lets a caller ride an existing
+ * high-frequency tick (15s node heartbeat) without writing Mongo at that rate.
+ */
+export function presenceWriteDue(last: PresenceWriteState, now: Date, minIntervalMs: number): boolean {
+ if (last.dayKey === null) return true
+ if (utcDayKey(now) !== last.dayKey) return true
+ return now.getTime() - last.wroteAt >= minIntervalMs
+}