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(license): don't lose Pro to the launch broadcast race or mid-session token expiry
enes63a863705c72
diff --git a/src/main/license.test.ts b/src/main/license.test.tsnew file mode 100644index 000000000..4632f6768--- /dev/null+++ b/src/main/license.test.ts@@ -0,0 +1,139 @@+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'+import crypto from 'node:crypto'+import { mkdtempSync, rmSync } from 'fs'+import { tmpdir } from 'os'+import path from 'path'+import { IPC } from '../shared/ipc'+import type { LicenseStatus } from '../shared/types'++// One temp userData dir per run; hoisted so the electron mock factory can see it.+const h = vi.hoisted(() => ({ userData: '', publicKeyPem: '' }))++vi.mock('electron', () => ({+ app: { getPath: () => h.userData, isPackaged: false },+ ipcMain: { handle: vi.fn() },+ shell: { openExternal: vi.fn() }+}))++vi.mock('./entitlement-key', () => ({+ get ENTITLEMENT_PUBLIC_KEY() {+ return h.publicKeyPem+ }+}))++const { publicKey, privateKey } = crypto.generateKeyPairSync('ed25519')+h.publicKeyPem = publicKey.export({ type: 'spki', format: 'pem' }).toString()++/** Mint a token the same way the server does: base64url(payload).base64url(sig). */+function mint(ttlSeconds: number): string {+ const payload = Buffer.from(+ JSON.stringify({+ deviceId: 'test-device',+ tier: 'pro',+ licenseId: 'lic_test',+ iat: Math.floor(Date.now() / 1000),+ exp: Math.floor(Date.now() / 1000) + ttlSeconds+ })+ ).toString('base64url')+ const sig = crypto.sign(null, Buffer.from(payload), privateKey).toString('base64url')+ return `${payload}.${sig}`+}++function jsonResponse(body: unknown): { ok: boolean; status: number; json: () => Promise<unknown> } {+ return { ok: true, status: 200, json: async () => body }+}++function fakeWindow(): { win: never; sent: LicenseStatus[] } {+ const sent: LicenseStatus[] = []+ const win = {+ isDestroyed: () => false,+ webContents: {+ send: (channel: string, status: LicenseStatus) => {+ if (channel === IPC.licenseChanged) sent.push(status)+ }+ }+ }+ return { win: win as never, sent }+}++const HOUR = 60 * 60 * 1000++/** Let real I/O (fs writes in save()) and microtasks settle — setTimeout stays unfaked. */+async function flush(): Promise<void> {+ for (let i = 0; i < 3; i++) await new Promise((r) => setTimeout(r, 25))+}++describe('license entitlement refresh', () => {+ let fetchMock: ReturnType<typeof vi.fn>++ beforeEach(() => {+ // Only the refresh interval and the clock are faked: fetch/fs promises and the+ // 8s abort timers stay on the real event loop so awaits actually settle.+ vi.useFakeTimers({ toFake: ['setInterval', 'Date'] })+ h.userData = mkdtempSync(path.join(tmpdir(), 'nt-license-test-'))+ delete process.env.DO_NOT_TRACK+ delete process.env.NODETERM_TELEMETRY_DISABLED+ process.env.NODETERM_API_BASE = 'http://127.0.0.1:1'+ fetchMock = vi.fn()+ vi.stubGlobal('fetch', fetchMock)+ vi.resetModules()+ })++ afterEach(() => {+ vi.unstubAllGlobals()+ vi.useRealTimers()+ rmSync(h.userData, { recursive: true, force: true })+ })++ it('launch refresh (device-bound) stores the minted token and broadcasts Pro', async () => {+ const token = mint(7 * 24 * 60 * 60)+ fetchMock.mockResolvedValue(jsonResponse({ active: true, token }))+ const { initLicense } = await import('./license')+ const { win, sent } = fakeWindow()+ initLicense(win)+ await flush()++ expect(fetchMock).toHaveBeenCalledTimes(1)+ expect(sent.length).toBe(1)+ expect(sent[0].active).toBe(true)+ expect(sent[0].tier).toBe('pro')+ })++ it('keeps refreshing periodically so a mid-session token expiry re-mints instead of dropping Pro', async () => {+ // Server hands out short-lived tokens (7d in prod); simulate one that expires+ // within the session, then a re-mint on the next poll.+ fetchMock.mockImplementation(async () =>+ jsonResponse({ active: true, token: mint(7 * 24 * 60 * 60) })+ )+ const { initLicense } = await import('./license')+ const { win, sent } = fakeWindow()+ initLicense(win)+ await flush()+ expect(fetchMock).toHaveBeenCalledTimes(1)++ // A day passes in-session: the app must have polled again on its own —+ // launch-only refresh means the token silently expires after 7 days.+ await vi.advanceTimersByTimeAsync(24 * HOUR)+ await flush()+ expect(fetchMock.mock.calls.length).toBeGreaterThanOrEqual(2)+ const last = sent[sent.length - 1]+ expect(last.active).toBe(true)+ })++ it('a periodic refresh that finds the device revoked drops Pro mid-session', async () => {+ fetchMock.mockResolvedValueOnce(+ jsonResponse({ active: true, token: mint(7 * 24 * 60 * 60) })+ )+ const { initLicense } = await import('./license')+ const { win, sent } = fakeWindow()+ initLicense(win)+ await flush()+ expect(sent[sent.length - 1].active).toBe(true)++ // From now on the server says: not entitled (canceled subscription).+ fetchMock.mockResolvedValue(jsonResponse({ active: false }))+ await vi.advanceTimersByTimeAsync(24 * HOUR)+ await flush()+ expect(sent[sent.length - 1].active).toBe(false)+ })+})diff --git a/src/main/license.ts b/src/main/license.tsindex d53abd729..18b9d9ccb 100644--- a/src/main/license.ts+++ b/src/main/license.ts@@ -18,6 +18,11 @@ const API_BASE = process.env.NODETERM_API_BASE || 'https://api.nodeterm.dev'// NODETERM_CHECKOUT_URL overrides it for testing (e.g. the test-mode link).const CHECKOUT_URL = process.env.NODETERM_CHECKOUT_URL || 'https://buy.stripe.com/9B65kFeraflH9ora4A7EQ00'+// Tokens are short-lived (the server mints 7-day entitlements) and the app is designed to+// stay open for days, so a launch-only refresh would let the token expire mid-session and+// silently drop Pro. Re-refresh on the same 6h cadence as the check/update polls.+const REFRESH_INTERVAL_MS = 6 * 60 * 60 * 1000+// Same gate as telemetry/check: never hit the prod API from a dev/unsigned build unless a// local server is targeted explicitly, and honor DO_NOT_TRACK / the kill switch.function allowed(): boolean {@@ -179,8 +184,8 @@ export function initLicense(win: BrowserWindow): void {return status})- // On launch: re-establish entitlement, keeping the last valid token on failure (offline grace).- void (async () => {+ // Re-establish entitlement, keeping the last valid token on failure (offline grace).+ const refresh = async (): Promise<void> => {const stored = load()if (stored.key) {// Key-paste flow: refresh against the stored key.@@ -208,5 +213,9 @@ export function initLicense(win: BrowserWindow): void {broadcast(statusFrom(undefined))}}- })()+ }++ // On launch + every 6h while the app stays open (see REFRESH_INTERVAL_MS).+ void refresh()+ setInterval(() => void refresh(), REFRESH_INTERVAL_MS).unref()}diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsxindex aa3e5447f..8950c573e 100644--- a/src/renderer/canvas/Canvas.tsx+++ b/src/renderer/canvas/Canvas.tsx@@ -104,6 +104,7 @@ import { useSessionNaming } from '../state/sessionNaming'import { useSshServers } from '../state/sshServers'import { useSshConn } from '../state/sshConn'import { requireProOr } from '../state/upgradeGate'+import { useEntitlement } from '../state/entitlement'import type { SshServer } from '@shared/ssh'import type { SshProjectStatus, TranscriptHit } from '@shared/types'import {@@ -562,6 +563,10 @@ export function Canvas() {// 1) Load the whole workspace once and hydrate the projects store.useEffect(() => {let cancelled = false+ // Pull the current license status: the main process broadcasts it on launch, but that+ // broadcast races renderer load and is dropped if it fires first — without this pull a+ // Pro user can start (and stay) gated as free until the next restart.+ void useEntitlement.getState().hydrate()useSettings.getState().hydrate()