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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(remote): charge relay quota on approval, not bridge; hourly reconcile for month rollover
enes5f51369097c3
diff --git a/src/core/relay-quota.test.ts b/src/core/relay-quota.test.tsindex f5ef4fece..eb1015610 100644--- a/src/core/relay-quota.test.ts+++ b/src/core/relay-quota.test.ts@@ -88,6 +88,33 @@ describe('relay-quota', () => {expect(quota.relayQuotaAvailable()).toBe(false)})+ it('peek returns ok with the projected used but persists nothing', async () => {+ const { quota } = await boot()+ expect(quota.peekRelayUse('phoneA')).toEqual({ kind: 'ok', used: 1, limit: 5 })+ // Nothing was charged: a real consume still counts and returns the same projected used.+ expect(quota.consumeRelayUse('phoneA')).toEqual({ kind: 'ok', used: 1, limit: 5 })+ })++ it("peek reports 'already' for a peer counted today", async () => {+ const { quota } = await boot()+ quota.consumeRelayUse('phoneA')+ expect(quota.peekRelayUse('phoneA')).toEqual({ kind: 'already', used: 1, limit: 5 })+ })++ it("peek reports 'exhausted' at the cap without charging", async () => {+ const { quota } = await boot()+ for (const p of ['a', 'b', 'c', 'd', 'e']) quota.consumeRelayUse(p)+ expect(quota.peekRelayUse('f')).toEqual({ kind: 'exhausted', used: 5, limit: 5 })+ // Still exhausted (peek didn't add an entry), and a fresh consume also refuses.+ expect(quota.peekRelayUse('f')).toEqual({ kind: 'exhausted', used: 5, limit: 5 })+ })++ it("peek reports 'unlimited' when premium", async () => {+ h.premium = true+ const { quota } = await boot()+ expect(quota.peekRelayUse('phoneA')).toEqual({ kind: 'unlimited' })+ })+it('status handler + change broadcast go over CorePlatform', async () => {const { fake, quota } = await boot()quota.initRelayQuota()diff --git a/src/core/relay-quota.ts b/src/core/relay-quota.tsindex 82e2976c7..4aaabde7c 100644--- a/src/core/relay-quota.ts+++ b/src/core/relay-quota.ts@@ -110,6 +110,20 @@ export function consumeRelayUse(peerKey: string): RelayConsume {return { kind: 'ok', used: s.entries.length, limit: RELAY_QUOTA_LIMIT }}+/** Non-mutating admission check: what consumeRelayUse WOULD return, persisting nothing. */+export function peekRelayUse(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)) {+ return { kind: 'already', used: s.entries.length, limit: RELAY_QUOTA_LIMIT }+ }+ if (s.entries.length >= RELAY_QUOTA_LIMIT) {+ return { kind: 'exhausted', used: s.entries.length, limit: RELAY_QUOTA_LIMIT }+ }+ return { kind: 'ok', used: s.entries.length + 1, limit: RELAY_QUOTA_LIMIT }+}+export function initRelayQuota(): void {platform().handle(IPC.relayQuotaStatus, () => relayQuotaStatus())}diff --git a/src/main/remote/standing-host.ts b/src/main/remote/standing-host.tsindex 1770ceb51..a762bc0c8 100644--- a/src/main/remote/standing-host.ts+++ b/src/main/remote/standing-host.ts@@ -24,7 +24,7 @@ import { IPC } from '../../shared/ipc'import type { CanvasMutation, Settings } from '../../shared/types'import { PtyManager } from '../../core/pty-manager'import { getStoredEntitlement, isPremium } from '../../core/license'-import { consumeRelayUse, relayQuotaAvailable } from '../../core/relay-quota'+import { consumeRelayUse, peekRelayUse, relayQuotaAvailable } from '../../core/relay-quota'import { getDeviceId } from '../../core/device-id'import { createPhonePresence, type PhonePresence } from './phone-presence'import { publicKeyToB64, type KeyPair } from './e2ee'@@ -215,6 +215,27 @@ export function initStandingHost(if (running && pendingCount() < TARGET_PENDING) void connectOne()}+ // Charge a (peer, day) slot at APPROVAL time (auto-approve for a pinned device, or the human's+ // OK for an unknown one) — never at bridge time, so a declined/timed-out prompt costs nothing.+ // Returns whether the peer is admitted. On an exhausted race (another peer took the last slot+ // between the earlier peek and now) it drops the session and refuses; 2nd+ use of the month+ // notifies; 1st use / already-counted / Pro (unlimited) is silent.+ function chargeAndAdmit(pooled: Pooled, pub: string | null): boolean {+ const q = consumeRelayUse(pub ?? 'unknown-peer')+ if (q.kind === 'exhausted') {+ notifyQuota(+ `Remote access limit reached (${q.limit}/${q.limit} this month). Upgrade to Pro for unlimited access.`+ )+ removeFromPool(pooled)+ ensurePool()+ return false+ }+ if (q.kind === 'ok' && q.used >= 2) {+ notifyQuota(`Remote access ${q.used}/${q.limit} this month`)+ }+ return true+ }+function scheduleReconnect(): void {if (!running || reconnectTimer) returnconst delay = RECONNECT_DELAYS_MS[Math.min(reconnectAttempt, RECONNECT_DELAYS_MS.length - 1)]@@ -258,10 +279,11 @@ export function initStandingHost(}const s = pooled.sessionconst pub = s.peerPublicKeyB64()- // Free-tier metering: every bridged phone consumes a (peer, local-day) slot — pinned- // or not. 1st use of the month is silent, 2nd+ notifies, over-limit is refused- // BEFORE any approval so an exhausted free host never serves a new pair.- const q = consumeRelayUse(pub ?? 'unknown-peer')+ // Free-tier admission GATE (non-mutating): refuse an over-limit free host a NEW pair before any+ // approval, but DON'T charge yet — the slot is spent only when the pair is actually approved+ // (pinned auto-approve, or the human's OK), never merely by bridging. A peer already counted+ // today ('already') or Pro ('unlimited') falls straight through to the approval flow.+ const q = peekRelayUse(pub ?? 'unknown-peer')if (q.kind === 'exhausted') {notifyQuota(`Remote access limit reached (${q.limit}/${q.limit} this month). Upgrade to Pro for unlimited access.`@@ -270,9 +292,6 @@ export function initStandingHost(ensurePool()return}- if (q.kind === 'ok' && q.used >= 2) {- notifyQuota(`Remote access ${q.used}/${q.limit} this month`)- }let storetry {store = await loadApprovedDevices()@@ -281,6 +300,9 @@ export function initStandingHost(}if (!pool.has(pooled)) return // torn down while the disk read was in flightif (pub && isPinned(store, pub)) {+ // Auto-approve → this is the charge point for a pinned device. An exhausted race here drops+ // the session inside the helper; only a genuine admission approves.+ if (!chargeAndAdmit(pooled, pub)) returns.approve()return}@@ -403,6 +425,9 @@ export function initStandingHost(// browse socket may have already closed. Prefer the live peer's key, else the remembered one.const pub = p.session.peerPublicKeyB64() ?? p.approvalPubclearApproval(p)+ // Charge the slot at the human's OK (a reject/timeout never reaches here → costs nothing). An+ // exhausted race drops the session inside the helper; don't pin or approve a refused peer.+ if (!chargeAndAdmit(p, pub)) returnif (pub) {void loadApprovedDevices().then((store) => saveApprovedDevices(pinDevice(store, pub)))@@ -419,6 +444,14 @@ export function initStandingHost(ensurePool()})+ // Month rollover / quota changes can re-admit a stopped free-tier host (or retire an exhausted+ // one). reconcile() is idempotent, so a slow hourly tick is safe and cheap.+ // NOT cleared in stop(): stop() also fires transiently (exhaustion, lapsed Pro), and this tick is+ // exactly what must revive hosting later. The interval is unref'd and dies with the process, and+ // initStandingHost is constructed once, so there is nothing to clean up.+ const reconcileTimer = setInterval(() => reconcile(), 60 * 60 * 1000)+ reconcileTimer.unref?.()+return {setEnabled(next) {enabled = next