Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(core): monthly relay remote-access quota for the free tier

enes65f75ea74927
diff --git a/src/core/relay-quota.test.ts b/src/core/relay-quota.test.ts
new file mode 100644
index 000000000..f5ef4fece
--- /dev/null
+++ b/src/core/relay-quota.test.ts
@@ -0,0 +1,105 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
+import { mkdtempSync, rmSync, readFileSync, writeFileSync } from 'fs'
+import { tmpdir } from 'os'
+import path from 'path'
+
+const h = vi.hoisted(() => ({ premium: false }))
+vi.mock('./license', () => ({ isPremium: () => h.premium }))
+
+let userData = ''
+
+async function boot() {
+ vi.resetModules()
+ const { initPlatform } = await import('./platform')
+ const { fakePlatform } = await import('./platform-fake')
+ const fake = fakePlatform({ userDataDir: userData, isPackaged: false })
+ initPlatform(fake)
+ const quota = await import('./relay-quota')
+ return { fake, quota }
+}
+
+describe('relay-quota', () => {
+ beforeEach(() => {
+ vi.useFakeTimers({ toFake: ['Date'] })
+ vi.setSystemTime(new Date('2026-07-15T12:00:00'))
+ h.premium = false
+ userData = mkdtempSync(path.join(tmpdir(), 'nt-relay-quota-'))
+ })
+ afterEach(async () => {
+ const { resetPlatformForTests } = await import('./platform')
+ resetPlatformForTests()
+ vi.useRealTimers()
+ rmSync(userData, { recursive: true, force: true })
+ })
+
+ it('counts a fresh (peer, day) and stays silent-compatible on the first use', async () => {
+ const { quota } = await boot()
+ expect(quota.consumeRelayUse('phoneA')).toEqual({ kind: 'ok', used: 1, limit: 5 })
+ })
+
+ it('same peer same local day never double-counts', async () => {
+ const { quota } = await boot()
+ quota.consumeRelayUse('phoneA')
+ expect(quota.consumeRelayUse('phoneA')).toEqual({ kind: 'already', used: 1, limit: 5 })
+ })
+
+ it('same peer on a NEW day consumes a new slot', async () => {
+ const { quota } = await boot()
+ quota.consumeRelayUse('phoneA')
+ vi.setSystemTime(new Date('2026-07-16T12:00:00'))
+ expect(quota.consumeRelayUse('phoneA')).toEqual({ kind: 'ok', used: 2, limit: 5 })
+ })
+
+ it('exhausts at the limit and refuses a 6th distinct pair', async () => {
+ const { quota } = await boot()
+ for (const p of ['a', 'b', 'c', 'd', 'e']) quota.consumeRelayUse(p)
+ expect(quota.consumeRelayUse('f')).toEqual({ kind: 'exhausted', used: 5, limit: 5 })
+ // …but a peer already counted today still gets in:
+ expect(quota.consumeRelayUse('a')).toEqual({ kind: 'already', used: 5, limit: 5 })
+ })
+
+ it('month rollover resets the ledger', async () => {
+ const { quota } = await boot()
+ for (const p of ['a', 'b', 'c', 'd', 'e']) quota.consumeRelayUse(p)
+ vi.setSystemTime(new Date('2026-08-01T09:00:00'))
+ expect(quota.consumeRelayUse('f')).toEqual({ kind: 'ok', used: 1, limit: 5 })
+ })
+
+ it('clock rollback cannot mint a fresh month (lastSeen anchor)', async () => {
+ const { quota } = await boot()
+ for (const p of ['a', 'b', 'c', 'd', 'e']) quota.consumeRelayUse(p)
+ vi.setSystemTime(new Date('2026-06-01T09:00:00')) // roll BACK before the ledger month
+ expect(quota.consumeRelayUse('f').kind).toBe('exhausted')
+ })
+
+ it('premium bypasses without writing the ledger', async () => {
+ h.premium = true
+ const { quota } = await boot()
+ expect(quota.consumeRelayUse('phoneA')).toEqual({ kind: 'unlimited' })
+ expect(quota.relayQuotaStatus().unlimited).toBe(true)
+ })
+
+ it('relayQuotaAvailable: true under limit, true when only a today-peer could enter, false when cold-exhausted', async () => {
+ const { quota } = await boot()
+ expect(quota.relayQuotaAvailable()).toBe(true)
+ for (const p of ['a', 'b', 'c', 'd', 'e']) quota.consumeRelayUse(p)
+ expect(quota.relayQuotaAvailable()).toBe(true) // entries today → an already-counted peer may return
+ vi.setSystemTime(new Date('2026-07-20T12:00:00')) // same month, later day: no entry is "today"
+ expect(quota.relayQuotaAvailable()).toBe(false)
+ })
+
+ it('status handler + change broadcast go over CorePlatform', async () => {
+ const { fake, quota } = await boot()
+ quota.initRelayQuota()
+ quota.consumeRelayUse('phoneA')
+ await vi.waitFor(() => {
+ expect(fake.sent.some((s) => s.channel === 'relay-quota:changed')).toBe(true)
+ })
+ })
+
+ it('a corrupt store file degrades to an empty ledger', async () => {
+ const { quota } = await boot()
+ writeFileSync(path.join(userData, 'relay-quota.json'), '{not json')
+ expect(quota.consumeRelayUse('a')).toEqual({ kind: 'ok', used: 1, limit: 5 })
+ })
+})
diff --git a/src/core/relay-quota.ts b/src/core/relay-quota.ts
new file mode 100644
index 000000000..82e2976c7
--- /dev/null
+++ b/src/core/relay-quota.ts
@@ -0,0 +1,115 @@
+// Monthly free-tier quota for relay remote access (spec docs/superpowers/specs/
+// 2026-07-15-nodeterm-pro-mobile-paywall-design.md §4). Pro (license.ts isPremium)
+// bypasses. Unit: (peerKey, local day) — the same phone re-bridging on the same local
+// day never consumes a second use. The month derives from max(now, lastSeen), so a
+// rolled-back clock can neither mint a fresh month nor revive a day.
+import { readFileSync, writeFileSync } from 'fs'
+import path from 'path'
+import { platform } from './platform'
+import { IPC } from '../shared/ipc'
+import { isPremium } from './license'
+import type { RelayQuotaStatus } from '../shared/types'
+
+export const RELAY_QUOTA_LIMIT = 5
+
+interface Stored {
+ month?: string // '2026-07'
+ entries?: { peer: string; day: string }[] // day: '2026-07-15' (local)
+ /** Largest unix-seconds timestamp this install has observed — the clock-rollback anchor. */
+ lastSeen?: number
+}
+
+function file(): string {
+ return path.join(platform().userDataDir, 'relay-quota.json')
+}
+function load(): Stored {
+ try {
+ return JSON.parse(readFileSync(file(), 'utf-8')) as Stored
+ } catch {
+ return {}
+ }
+}
+// Synchronous so consecutive consumeRelayUse() calls (a sync API) observe prior
+// entries — a fresh readFileSync load in each call must see the previous write.
+function save(s: Stored): void {
+ try {
+ writeFileSync(file(), JSON.stringify(s), 'utf-8')
+ } catch {
+ // best-effort persistence; a failed write must never crash a bridge admission
+ }
+}
+
+function effectiveNow(s: Stored): Date {
+ return new Date(Math.max(Date.now(), (s.lastSeen ?? 0) * 1000))
+}
+function dayOf(d: Date): string {
+ const m = String(d.getMonth() + 1).padStart(2, '0')
+ const day = String(d.getDate()).padStart(2, '0')
+ return `${d.getFullYear()}-${m}-${day}`
+}
+
+/** Load, roll into the current (anchored) month, and advance the anchor. */
+function normalized(): Required<Stored> {
+ const s = load()
+ const day = dayOf(effectiveNow(s))
+ const month = day.slice(0, 7)
+ const lastSeen = Math.max(s.lastSeen ?? 0, Math.floor(Date.now() / 1000))
+ if (s.month !== month) return { month, entries: [], lastSeen }
+ return { month, entries: s.entries ?? [], lastSeen }
+}
+
+export type RelayConsume =
+ | { kind: 'unlimited' }
+ | { kind: 'ok'; used: number; limit: number }
+ | { kind: 'already'; used: number; limit: number }
+ | { kind: 'exhausted'; used: number; limit: number }
+
+export function relayQuotaStatus(): RelayQuotaStatus {
+ const s = normalized()
+ return {
+ month: s.month,
+ used: s.entries.length,
+ limit: RELAY_QUOTA_LIMIT,
+ unlimited: isPremium()
+ }
+}
+
+/**
+ * True when a relay bridge could be admitted right now: Pro, a free slot left, or at
+ * least one peer already counted TODAY (that peer may reconnect for free). Used by the
+ * standing host's reconcile to decide whether hosting is worth running at all.
+ */
+export function relayQuotaAvailable(): boolean {
+ if (isPremium()) return true
+ const s = normalized()
+ if (s.entries.length < RELAY_QUOTA_LIMIT) return true
+ const today = dayOf(effectiveNow(s))
+ return s.entries.some((e) => e.day === today)
+}
+
+export function broadcastRelayQuota(): void {
+ platform().broadcast(IPC.relayQuotaChanged, relayQuotaStatus())
+}
+
+/** Admit-or-refuse a relay bridge from `peerKey`, consuming a (peer, day) slot if fresh. */
+export function consumeRelayUse(peerKey: string): RelayConsume {
+ if (isPremium()) return { kind: 'unlimited' }
+ const s = normalized()
+ const day = dayOf(effectiveNow(s))
+ if (s.entries.some((e) => e.peer === peerKey && e.day === day)) {
+ save(s) // persist the advanced anchor / month rollover
+ return { kind: 'already', used: s.entries.length, limit: RELAY_QUOTA_LIMIT }
+ }
+ if (s.entries.length >= RELAY_QUOTA_LIMIT) {
+ save(s)
+ return { kind: 'exhausted', used: s.entries.length, limit: RELAY_QUOTA_LIMIT }
+ }
+ s.entries.push({ peer: peerKey, day })
+ save(s)
+ broadcastRelayQuota()
+ return { kind: 'ok', used: s.entries.length, limit: RELAY_QUOTA_LIMIT }
+}
+
+export function initRelayQuota(): void {
+ platform().handle(IPC.relayQuotaStatus, () => relayQuotaStatus())
+}
diff --git a/src/shared/ipc.ts b/src/shared/ipc.ts
index 6e11eb08b..12b8e44ca 100644
--- a/src/shared/ipc.ts
+++ b/src/shared/ipc.ts
@@ -66,6 +66,8 @@ export const IPC = {
licenseStatus: 'license:status',
licenseChanged: 'license:changed',
licenseUpgrade: 'license:upgrade',
+ relayQuotaStatus: 'relay-quota:status',
+ relayQuotaChanged: 'relay-quota:changed',
appRestartToUpdate: 'app:restart-to-update',
announcementsFetch: 'announcements:fetch',
usageFetch: 'usage:fetch',
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 727ba58d6..fe3b5115d 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -1103,6 +1103,19 @@ export interface LicenseApi {
onChange(listener: (s: LicenseStatus) => void): () => void
}
+/** Free-tier relay remote-access quota (spec 2026-07-15). Pro reports `unlimited: true`. */
+export interface RelayQuotaStatus {
+ month: string // '2026-07'
+ used: number // distinct (peer, local day) pairs consumed this month
+ limit: number // RELAY_QUOTA_LIMIT
+ unlimited: boolean // true when Pro
+}
+
+export interface RelayQuotaApi {
+ getStatus(): Promise<RelayQuotaStatus>
+ onChange(listener: (s: RelayQuotaStatus) => void): () => void
+}
+
export interface RemoteHostApi {
/**
* Enter host mode: mint a pairing token, connect to the relay as the host, and return the
@@ -1299,6 +1312,7 @@ export interface NodeTerminalApi {
updates: UpdateApi
announcements: AnnouncementsApi
license: LicenseApi
+ relayQuota: RelayQuotaApi
contextLink: ContextLinkApi
usage: UsageApi
context: ContextApi