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(license): seats in the entitlement (premium→N, absent→1, free→0) (team-access Task 1)

enesbe4612065f58
diff --git a/src/core/license.test.ts b/src/core/license.test.ts
index d009a3e96..c655e41bc 100644
--- a/src/core/license.test.ts
+++ b/src/core/license.test.ts
@@ -21,15 +21,17 @@ vi.mock('./entitlement-key', () => ({
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, deviceId = 'test-device'): string {
+/** Mint a token the same way the server does: base64url(payload).base64url(sig).
+ * `seats` is included only when provided, so an old token (no seats field) can be simulated. */
+function mint(ttlSeconds: number, deviceId = 'test-device', seats?: number): string {
const payload = Buffer.from(
JSON.stringify({
deviceId,
tier: 'pro',
licenseId: 'lic_test',
iat: Math.floor(Date.now() / 1000),
- exp: Math.floor(Date.now() / 1000) + ttlSeconds
+ exp: Math.floor(Date.now() / 1000) + ttlSeconds,
+ ...(seats !== undefined ? { seats } : {})
})
).toString('base64url')
const sig = crypto.sign(null, Buffer.from(payload), privateKey).toString('base64url')
@@ -168,3 +170,81 @@ describe('license entitlement refresh', () => {
expect(status.active).toBe(false)
})
})
+
+describe('license seats entitlement', () => {
+ let fetchMock: ReturnType<typeof vi.fn>
+ let fake: import('./platform-fake').FakePlatform
+
+ beforeEach(async () => {
+ vi.useFakeTimers({ toFake: ['setInterval', 'Date'] })
+ h.userData = mkdtempSync(path.join(tmpdir(), 'nt-license-seats-'))
+ writeFileSync(path.join(h.userData, 'device-id'), 'test-device')
+ delete process.env.DO_NOT_TRACK
+ delete process.env.NODETERM_TELEMETRY_DISABLED
+ process.env.NODETERM_API_BASE = 'http://127.0.0.1:1'
+ // Reject any refresh call → offline grace keeps the stored token intact for the assertions.
+ fetchMock = vi.fn().mockRejectedValue(new Error('offline'))
+ vi.stubGlobal('fetch', fetchMock)
+ vi.resetModules()
+ const { initPlatform } = await import('./platform')
+ const { fakePlatform } = await import('./platform-fake')
+ fake = fakePlatform({ userDataDir: h.userData, isPackaged: false })
+ initPlatform(fake)
+ })
+
+ afterEach(async () => {
+ vi.unstubAllGlobals()
+ vi.useRealTimers()
+ const { resetPlatformForTests } = await import('./platform')
+ resetPlatformForTests()
+ rmSync(h.userData, { recursive: true, force: true })
+ })
+
+ function storeToken(token?: string): void {
+ writeFileSync(path.join(h.userData, 'license.json'), JSON.stringify({ token }))
+ }
+
+ it('a premium token carrying seats:5 surfaces seats 5', async () => {
+ storeToken(mint(7 * 24 * 60 * 60, 'test-device', 5))
+ const { initLicense, licensedSeats } = await import('./license')
+ initLicense()
+ await flush()
+ const status = (await fake.handlers[IPC.licenseStatus]()) as LicenseStatus
+ expect(status.active).toBe(true)
+ expect(status.seats).toBe(5)
+ expect(licensedSeats()).toBe(5)
+ })
+
+ it('a premium token with no seats field defaults to 1 seat (backward compat)', async () => {
+ storeToken(mint(7 * 24 * 60 * 60))
+ const { initLicense, licensedSeats } = await import('./license')
+ initLicense()
+ await flush()
+ const status = (await fake.handlers[IPC.licenseStatus]()) as LicenseStatus
+ expect(status.active).toBe(true)
+ expect(status.seats).toBe(1)
+ expect(licensedSeats()).toBe(1)
+ })
+
+ it('an absent / non-premium token has 0 seats', async () => {
+ storeToken(undefined)
+ const { initLicense, licensedSeats } = await import('./license')
+ initLicense()
+ await flush()
+ const status = (await fake.handlers[IPC.licenseStatus]()) as LicenseStatus
+ expect(status.active).toBe(false)
+ expect(status.seats).toBe(0)
+ expect(licensedSeats()).toBe(0)
+ })
+
+ it('an expired token is not premium and has 0 seats', async () => {
+ storeToken(mint(-60, 'test-device', 5))
+ const { initLicense, licensedSeats } = await import('./license')
+ initLicense()
+ await flush()
+ const status = (await fake.handlers[IPC.licenseStatus]()) as LicenseStatus
+ expect(status.active).toBe(false)
+ expect(status.seats).toBe(0)
+ expect(licensedSeats()).toBe(0)
+ })
+})
diff --git a/src/core/license.ts b/src/core/license.ts
index 079236aad..9c28b82eb 100644
--- a/src/core/license.ts
+++ b/src/core/license.ts
@@ -58,6 +58,9 @@ interface Payload {
tier: string
licenseId: string
exp: number
+ /** Seat cap for the relay host (Team Access). Rides the same signed token; an old token
+ * lacks it, in which case it resolves to 1 seat. See `seatsFrom`. */
+ seats?: number
}
// Offline verification of our compact Ed25519 token: base64url(payload).base64url(sig).
@@ -84,11 +87,27 @@ function verify(token: string | undefined): Payload | null {
}
}
+// Resolve the seat cap from a (possibly null) verified payload — the single source of truth so
+// statusFrom() and licensedSeats() can't drift: premium token → its seats ?? 1 (a premium token
+// with no seats field = today's single-peer behavior), inactive/free/invalid → 0.
+function seatsFrom(p: Payload | null): number {
+ return p ? p.seats ?? 1 : 0
+}
+
function statusFrom(token: string | undefined, error: string | null = null): LicenseStatus {
const p = verify(token)
return p
- ? { tier: p.tier, active: true, expiresAt: p.exp, error: null }
- : { tier: null, active: false, expiresAt: null, error }
+ ? { tier: p.tier, active: true, expiresAt: p.exp, seats: seatsFrom(p), error: null }
+ : { tier: null, active: false, expiresAt: null, seats: 0, error }
+}
+
+/**
+ * The seat cap the current stored entitlement grants: premium → its `seats` (absent → 1), else 0.
+ * The relay host reads this to enforce how many devices may connect at once. Kept consistent with
+ * `statusFrom` by sharing `seatsFrom`/`verify` — do not reimplement the resolution here.
+ */
+export function licensedSeats(): number {
+ return seatsFrom(verify(load().token))
}
async function call(path: string, body: unknown): Promise<{ token?: string; error?: string }> {
diff --git a/src/renderer/state/entitlement.ts b/src/renderer/state/entitlement.ts
index dd79f530a..6352d848f 100644
--- a/src/renderer/state/entitlement.ts
+++ b/src/renderer/state/entitlement.ts
@@ -12,7 +12,7 @@ interface EntitlementState {
deactivate(): Promise<void>
}
-const EMPTY: LicenseStatus = { tier: null, active: false, expiresAt: null, error: null }
+const EMPTY: LicenseStatus = { tier: null, active: false, expiresAt: null, seats: 0, error: null }
export const useEntitlement = create<EntitlementState>((set) => {
const apply = (status: LicenseStatus) => set({ status, isPremium: status.active })
diff --git a/src/shared/types.ts b/src/shared/types.ts
index d60550d1d..92e700f6a 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -1083,6 +1083,8 @@ export interface LicenseStatus {
active: boolean
/** Unix seconds when the entitlement expires, or null. */
expiresAt: number | null
+ /** Seat cap for the relay host (Team Access): premium → the token's seats (absent → 1), free/inactive → 0. */
+ seats: number
/** Last activation/refresh error reason code, or null. */
error: string | null
}