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.

Presence tells Harness OS and its Orange Pi apart from the TUI

bubu-dudu0aab1bb24a27
diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma
index 69df56af3..c0930f127 100644
--- a/backend/prisma/schema.prisma
+++ b/backend/prisma/schema.prisma
@@ -574,7 +574,8 @@ model UserDailyPresence {
/// Per-day record of a user on a surface other than the desktop app — `UserDailyPresence` stays the
/// desktop app's alone. One row per (user, surface, UTC day); DAU is a distinct `userId`.
/// `surface`: 'tui' (`harness tui`, reported by the local daemon like a desktop window: adapter-ws
-/// `app_presence { surface: 'tui' }`, src/lib/adapterWs.ts), 'mobile' / 'web' (the app's and the
+/// `app_presence { surface: 'tui' }`, src/lib/adapterWs.ts) — 'harness-os' / 'harness-os-pi' when that
+/// TUI is Harness OS on a PC / on an Orange Pi (cli lib/windowSurfaces.ts) —, 'mobile' / 'web' (the app's and the
/// browser's own web-ws sockets, declared with `?client=` — src/lib/webWs.ts; the daemon's relay
/// socket declares nothing and is never counted). `connections` = sessions opened that day (for
/// mobile/web one per machine socket); a session spanning midnight gives the next day `0`.
diff --git a/backend/src/lib/adapterWs.ts b/backend/src/lib/adapterWs.ts
index a4294f15d..66a18721c 100644
--- a/backend/src/lib/adapterWs.ts
+++ b/backend/src/lib/adapterWs.ts
@@ -25,7 +25,7 @@ import {
import { trackSocketLiveness } from './hub.js'
import { guardedSend, guardedSendJson } from './wsSend.js'
import { attachNodeRole, PRESENCE_TTL_SEC } from './nodeRole.js'
-import { presenceTracker, presenceWriteDue, recordTurnStarted, touchClientOnlineDay, touchMachineOnlineDay, touchUserOnlineDay, type PresenceWriteState } from './dailyTracking.js'
+import { presenceTracker, presenceWriteDue, recordTurnStarted, touchClientOnlineDay, touchMachineOnlineDay, touchUserOnlineDay, TUI_SURFACES, type ClientSurface, type PresenceWriteState } from './dailyTracking.js'
import { countryCodeFromHeaders } from './clientGeo.js'
import { recordCreatedAgent, recordDeletedAgent } from './agentTracker.js'
import type { Frame } from './tunnel.js'
@@ -308,11 +308,20 @@ async function attachAdapter(ws: WebSocket, machineId: string, userId: string, c
)
// `harness tui` on this computer: the daemon reports it like a window but names the surface, and it
// is kept out of the desktop app's table (`user_daily_client_presence`, schema.prisma).
- const touchTuiPresence = presenceTracker(
- (now, isNewConnection) => touchClientOnlineDay(userId, 'tui', now, { isNewConnection }),
- (kind, err) => logger.warn('tui presence tracking failed', { machineId, userId, kind, error: String(err) }),
- userPresenceFloors,
- )
+ // One tracker per surface it reports (TUI_SURFACES): each is its own row and its own write floor.
+ const tuiTrackers = new Map<ClientSurface, ReturnType<typeof presenceTracker>>()
+ const touchTuiPresence = (surface: ClientSurface, kind: 'open' | 'ping') => {
+ let track = tuiTrackers.get(surface)
+ if (!track) {
+ track = presenceTracker(
+ (now, isNewConnection) => touchClientOnlineDay(userId, surface, now, { isNewConnection }),
+ (k, err) => logger.warn('tui presence tracking failed', { machineId, userId, surface, kind: k, error: String(err) }),
+ userPresenceFloors,
+ )
+ tuiTrackers.set(surface, track)
+ }
+ track(kind)
+ }
// 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
@@ -436,9 +445,11 @@ async function attachAdapter(ws: WebSocket, machineId: string, userId: string, c
if (app.type === 'app_presence') {
const { kind, surface } = (app.payload ?? {}) as { kind?: unknown; surface?: unknown }
if (typeof kind !== 'string' || !APP_PRESENCE_KINDS.has(kind)) return
- // No surface = the desktop app (every daemon before this field, and the desktop window now).
- if (surface === 'tui') touchTuiPresence(kind as 'open' | 'ping')
- else touchUserPresence(kind as 'open' | 'ping')
+ // No surface = the desktop app (every daemon before this field, and the desktop window now). A
+ // TUI says where it runs (TUI_SURFACES); a name this backend does not know counts as nothing,
+ // never as the desktop app.
+ if (surface === undefined) touchUserPresence(kind as 'open' | 'ping')
+ else if (typeof surface === 'string' && TUI_SURFACES.has(surface)) touchTuiPresence(surface as ClientSurface, kind as 'open' | 'ping')
return
}
// The machine appending to its account's device key log (lib/deviceKeyLog.ts): its own key, or a
diff --git a/backend/src/lib/dailyTracking.test.ts b/backend/src/lib/dailyTracking.test.ts
index ae59b7263..9413fddd1 100644
--- a/backend/src/lib/dailyTracking.test.ts
+++ b/backend/src/lib/dailyTracking.test.ts
@@ -18,7 +18,7 @@ vi.mock('./prisma.js', () => ({
},
}))
-import { presenceTracker, presenceWriteDue, recordTurnStarted, touchClientOnlineDay, touchDeviceOnlineDay, touchMachineOnlineDay, touchUserOnlineDay } from './dailyTracking.js'
+import { CLIENT_SURFACES, presenceTracker, presenceWriteDue, recordTurnStarted, touchClientOnlineDay, touchDeviceOnlineDay, touchMachineOnlineDay, touchUserOnlineDay, TUI_SURFACES } from './dailyTracking.js'
import { utcDayStart } from '../types/analytics.js'
describe('touchUserOnlineDay', () => {
@@ -71,6 +71,15 @@ describe('touchClientOnlineDay', () => {
expect(ping.update).toEqual({ lastSeenAt: now })
expect(ping.create.connections).toBe(0)
})
+
+ it('tells a TUI on Harness OS, on a PC or an Orange Pi, from the TUI anywhere else', async () => {
+ expect([...TUI_SURFACES]).toEqual(['tui', 'harness-os', 'harness-os-pi'])
+ for (const surface of TUI_SURFACES) expect(CLIENT_SURFACES.has(surface)).toBe(true)
+ for (const other of ['desktop', 'mobile', 'web', 'orangepi', '']) expect(TUI_SURFACES.has(other)).toBe(false)
+ const now = new Date('2026-10-13T08:00:00.000Z')
+ await touchClientOnlineDay('user-1', 'harness-os-pi', now, { isNewConnection: true })
+ expect(db.clientPresenceUpsert.mock.calls[0][0].create).toMatchObject({ userId: 'user-1', surface: 'harness-os-pi' })
+ })
})
describe('presenceTracker', () => {
diff --git a/backend/src/lib/dailyTracking.ts b/backend/src/lib/dailyTracking.ts
index ec5f7a4a4..789dbfc0b 100644
--- a/backend/src/lib/dailyTracking.ts
+++ b/backend/src/lib/dailyTracking.ts
@@ -164,8 +164,11 @@ export function presenceWriteDue(last: PresenceWriteState, now: Date, minInterva
}
/** Surfaces other than the desktop app whose person is counted (`user_daily_client_presence`). */
-export type ClientSurface = 'tui' | 'mobile' | 'web'
-export const CLIENT_SURFACES: ReadonlySet<string> = new Set<ClientSurface>(['tui', 'mobile', 'web'])
+export type ClientSurface = 'tui' | 'harness-os' | 'harness-os-pi' | 'mobile' | 'web'
+export const CLIENT_SURFACES: ReadonlySet<string> = new Set<ClientSurface>(['tui', 'harness-os', 'harness-os-pi', 'mobile', 'web'])
+/** What a daemon reports a `harness tui` window as: the TUI, or Harness OS on a PC or an Orange Pi
+ * (cli lib/windowSurfaces.ts tuiPresence), so a person's source is told apart. */
+export const TUI_SURFACES: ReadonlySet<string> = new Set<ClientSurface>(['tui', 'harness-os', 'harness-os-pi'])
/**
* `touchUserOnlineDay` for the other surfaces, one row per (user, surface, day): the TUI reported by
diff --git a/cli/src/gateway/upstream.ts b/cli/src/gateway/upstream.ts
index 50f5d1348..0a6b4daf6 100644
--- a/cli/src/gateway/upstream.ts
+++ b/cli/src/gateway/upstream.ts
@@ -18,7 +18,7 @@
import { WebSocket } from 'ws'
import { hostname } from 'os'
import { env } from '../config/env.js'
-import { WINDOW_SURFACES, type LocalWindows, type WindowSurface } from '../lib/windowSurfaces.js'
+import { tuiPresence, WINDOW_SURFACES, type LocalWindows, type WindowSurface } from '../lib/windowSurfaces.js'
import { AuthSessionError, AuthSessionManager } from '../lib/authSession.js'
import { BACKEND_IDLE_DEADLINE_MS, watchSocketLiveness, type LivenessWatch } from '../lib/wsLiveness.js'
import { decodeTerminalHop, TerminalHopDirection } from '../lib/terminalBinary.js'
@@ -104,6 +104,8 @@ export class UpstreamLink {
private heartbeat: LivenessWatch | null = null
private appPing: NodeJS.Timeout | null = null
private lastAppPresenceUpAt: Record<WindowSurface, number> = { desktop: 0, tui: 0 }
+ /** What a TUI window is counted as here: read once, it does not change while the daemon runs. */
+ private readonly tuiPresence = tuiPresence()
// A window attached while there was no link to tell (cold start: the app dials this daemon before
// the daemon has dialed the backend; or a daemon restart under an open window). The session is
// real and must be counted once, so it is owed to the next link — not turned into a `ping`.
@@ -302,7 +304,9 @@ export class UpstreamLink {
t: 'up',
webEligible: false,
commanderEligible: false,
- frame: { type: 'app_presence', payload: surface === 'desktop' ? { kind } : { kind, surface } },
+ // A TUI on Harness OS says so, and on which board. A backend from before these names counted any
+ // name but `tui` as the desktop app: it is deployed first (adapterWs.ts TUI_SURFACES).
+ frame: { type: 'app_presence', payload: surface === 'desktop' ? { kind } : { kind, surface: this.tuiPresence } },
})
if (sent) this.lastAppPresenceUpAt[surface] = now
if (kind === 'open') this.appOpenOwed[surface] = !sent
diff --git a/cli/src/lib/windowSurfaces.spec.ts b/cli/src/lib/windowSurfaces.spec.ts
index cf53958a4..b3e0e8ac0 100644
--- a/cli/src/lib/windowSurfaces.spec.ts
+++ b/cli/src/lib/windowSurfaces.spec.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
-import { countWindows, localWindowsOf, WINDOW_SURFACES, windowSurfaceOf } from './windowSurfaces.js'
+import { countWindows, localWindowsOf, tuiPresence, WINDOW_SURFACES, windowSurfaceOf } from './windowSurfaces.js'
describe('the windows a person has open, per surface', () => {
it('reads a surface off the pipe: anything but tui is the desktop app', () => {
@@ -8,6 +8,20 @@ describe('the windows a person has open, per surface', () => {
for (const value of ['desktop', 'web', undefined, null, 3, {}]) expect(windowSurfaceOf(value)).toBe('desktop')
})
+ it('counts a TUI as the TUI, or as Harness OS on a PC or an Orange Pi', () => {
+ const files = (entries: Record<string, string>) => (path: string) => entries[path] ?? ''
+ const unit = '/usr/lib/systemd/user/harness-daemon.service'
+ const model = '/proc/device-tree/model'
+ expect(tuiPresence({}, files({}))).toBe('tui')
+ expect(tuiPresence({}, files({ [model]: 'Orange Pi 4 Pro\0' })), 'a board alone is not Harness OS').toBe('tui')
+ expect(tuiPresence({ HARNESS_OS: '1' }, files({}))).toBe('harness-os')
+ expect(tuiPresence({}, files({ [unit]: '[Unit]' }))).toBe('harness-os')
+ expect(tuiPresence({ HARNESS_OS: '1' }, files({ [model]: 'Orange Pi 4 Pro\0' }))).toBe('harness-os-pi')
+ expect(tuiPresence({}, files({ [unit]: '[Unit]', [model]: 'OrangePi 5\0' }))).toBe('harness-os-pi')
+ expect(tuiPresence({ HARNESS_OS: '1' }, files({ [model]: 'Raspberry Pi 5\0' }))).toBe('harness-os')
+ expect(['tui', 'harness-os']).toContain(tuiPresence())
+ })
+
it('reads window counts off the pipe, and anything unreadable as none', () => {
expect(localWindowsOf({ desktop: 2, tui: 1 })).toEqual({ desktop: 2, tui: 1 })
expect(localWindowsOf({ desktop: 2.7, tui: -1 })).toEqual({ desktop: 2, tui: 0 })
diff --git a/cli/src/lib/windowSurfaces.ts b/cli/src/lib/windowSurfaces.ts
index 2d1619881..dcab8872f 100644
--- a/cli/src/lib/windowSurfaces.ts
+++ b/cli/src/lib/windowSurfaces.ts
@@ -1,3 +1,5 @@
+import { readFileSync } from 'node:fs'
+
/**
* What a person has open on this computer: the desktop app, or `harness tui`. Each is its own presence.
* Here, not in core/api.ts: the gateway reads these off its pipe to the core, and it may take only types
@@ -7,6 +9,19 @@ export type WindowSurface = 'desktop' | 'tui'
export type LocalWindows = Record<WindowSurface, number>
export const WINDOW_SURFACES: readonly WindowSurface[] = ['desktop', 'tui']
+/**
+ * What the backend counts a `harness tui` window as (`user_daily_client_presence.surface`): the TUI, or
+ * Harness OS on a PC or an Orange Pi, so where a person comes from is told apart. Harness OS is its
+ * daemon unit (which sets HARNESS_OS=1, and whose file the OS package ships); the board is the device
+ * tree's model.
+ */
+export type TuiPresence = 'tui' | 'harness-os' | 'harness-os-pi'
+export function tuiPresence(env: NodeJS.ProcessEnv = process.env, read: (path: string) => string = readText): TuiPresence {
+ if (env.HARNESS_OS !== '1' && !read('/usr/lib/systemd/user/harness-daemon.service')) return 'tui'
+ return /orange\s*pi/i.test(read('/proc/device-tree/model')) ? 'harness-os-pi' : 'harness-os'
+}
+function readText(path: string): string { try { return readFileSync(path, 'utf8') } catch { return '' } }
+
/** A surface off the core↔gateway pipe: anything but `tui` is the desktop app. */
export const windowSurfaceOf = (value: unknown): WindowSurface => (value === 'tui' ? 'tui' : 'desktop')