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.

fix(project-limit): push time-driven changes; claim nothing while off

review9ab69685ee70
diff --git a/src/core/project-limit.test.ts b/src/core/project-limit.test.ts
index 4b93731a1..8d4894ce4 100644
--- a/src/core/project-limit.test.ts
+++ b/src/core/project-limit.test.ts
@@ -441,7 +441,9 @@ describe('project limit: claim', () => {
reply(404, {}),
reply(409, { error: 'something_new' }),
notJson(200),
- reply(200, {})
+ reply(200, {}),
+ reply(200, []), // JSON, but not an object
+ reply(200, 'ok')
]) {
net.claim = r
expect(await handle.claim()).toEqual({ ok: false, error: 'server', state: handle.state() })
@@ -533,6 +535,34 @@ describe('project limit: claim', () => {
expect(settled).toEqual({ ok: false, error: 'offline', state: handle.state() })
})
+ it('a body cut off mid-read is offline, not a server error', async () => {
+ const { handle } = await boot({ file: { latched: true, epoch: 1 }, openProjects: () => undefined })
+ // undici rejects the body read with `TypeError: terminated` when the connection drops mid-body.
+ for (const status of [200, 409, 500]) {
+ net.claim = () =>
+ Promise.resolve({
+ ok: status === 200,
+ status,
+ json: () => Promise.reject(new TypeError('terminated'))
+ })
+ expect(await handle.claim()).toEqual({ ok: false, error: 'offline', state: handle.state() })
+ }
+ })
+
+ it('an inactive install claims nothing: no request, no machine read', async () => {
+ const { handle } = await boot()
+ fetchMock.mockClear()
+ const r = await handle.claim()
+ expect(r).toEqual({ ok: true, state: handle.state() })
+ expect(r.state.active).toBe(false)
+ expect(fetchMock).not.toHaveBeenCalled()
+ expect(machineSpy).not.toHaveBeenCalled()
+ // The network gate still answers first: DO_NOT_TRACK without consent is consent_required.
+ process.env.DO_NOT_TRACK = '1'
+ expect(await handle.claim()).toEqual({ ok: false, error: 'consent_required', state: handle.state() })
+ expect(fetchMock).not.toHaveBeenCalled()
+ })
+
it('never rejects, even when reading the machine fails', async () => {
const { handle } = await boot({
file: { latched: true },
@@ -692,6 +722,40 @@ describe('project limit: wiring', () => {
expect(notified.at(-1)).toEqual(handle.state())
})
+ // Nothing but time changes these; the 6 h tick must push them (an app open for days, or one
+ // that never hears from api.nodeterm.dev, would otherwise stay dormant until a restart).
+ it('pushes the deadline crossing on the 6 h tick', async () => {
+ vi.setSystemTime(DEADLINE - HOUR)
+ const { handle } = await boot()
+ expect(handle.state().active).toBe(false)
+ await vi.advanceTimersByTimeAsync(6 * HOUR)
+ expect(notified.at(-1)?.active).toBe(true)
+ })
+
+ it('pushes an off answer going stale on the 6 h tick', async () => {
+ vi.setSystemTime(AFTER)
+ const { handle } = await boot({ file: { latched: false, offAt: AFTER - 20 * HOUR } })
+ expect(handle.state().active).toBe(false)
+ await vi.advanceTimersByTimeAsync(6 * HOUR)
+ expect(notified.at(-1)?.active).toBe(true)
+ })
+
+ it('pushes the Pro grace lapsing on the 6 h tick', async () => {
+ // Expired 30 days minus one hour ago: still Pro for the limit at boot, not six hours later.
+ writeLicense({ token: entitlementToken(-(30 * 24 * 3600 - 3600)) })
+ const { handle } = await boot()
+ expect(handle.state().pro).toBe(true)
+ await vi.advanceTimersByTimeAsync(6 * HOUR)
+ expect(notified.at(-1)?.pro).toBe(false)
+ })
+
+ it('a tick that changes nothing pushes nothing', async () => {
+ await boot()
+ const n = notified.length
+ await vi.advanceTimersByTimeAsync(6 * HOUR)
+ expect(notified.length).toBe(n)
+ })
+
it('reads the check feed at init and every 6 hours', async () => {
await boot()
expect(checkCalls()).toBe(1)
diff --git a/src/core/project-limit.ts b/src/core/project-limit.ts
index eb08d6c04..111b16ccb 100644
--- a/src/core/project-limit.ts
+++ b/src/core/project-limit.ts
@@ -26,11 +26,15 @@
* claim also caches the hash it sent, if that one came from the OS.
*
* CLAIMS are lazy and never reject: one request shared by concurrent callers, an 8 s timeout over
- * the whole request including the body, and every failure folded into a `ClaimError`.
+ * the whole request including the body, and every failure folded into a `ClaimError`. While the
+ * limit is off a claim asks nobody (no request, no machine read), whoever calls it.
*
* `projectLimit:changed` goes to THIS machine's windows only, through the shell's `notify`. Never
* `platform().broadcast`: on the desktop that fans out to every relay peer, and this state carries
* the host's Pro and DO_NOT_TRACK facts (see HOST_ONLY_CHANNEL_PREFIXES for the inbound half).
+ * It is pushed whenever the state differs from the last one sent: after a server word, and on the
+ * 6 h tick for what time alone changes (the deadline passing, an "off" going stale, Pro's grace
+ * lapsing) — otherwise a long-running app would stay dormant past the deadline until a restart.
*
* PERSISTENCE: `<userData>/project-limit.json`, written through `writeFileAtomic`, only when
* something changed. A missing, unparsable or hand-edited file never throws: each wrong-typed
@@ -111,6 +115,8 @@ let stored: Stored = {}
/** The machine hash from a successful OS read; null = none yet (provisional trust, see header). */
let osMachine: string | null = null
let claimInFlight: Promise<ClaimResult> | null = null
+/** The JSON of the last state sent to the windows (seeded at init: the renderer reads it then). */
+let lastSent: string | null = null
let writes: Promise<void> = Promise.resolve()
/** Background reads and writes, so tests can wait for them (`__projectLimitIdleForTests`). */
const pending = new Set<Promise<unknown>>()
@@ -203,16 +209,29 @@ function state(): ProjectLimitState {
return compute()
}
+/** Send the state to this machine's windows, unconditionally (`refresh()`). */
function notify(): void {
- deps.notify(state())
+ const s = state()
+ lastSent = JSON.stringify(s)
+ deps.notify(s)
+}
+
+/** Send the state only if it differs from the last one sent. Compared against what was SENT, not
+ * against the state a moment ago, so a change that came with time alone — the anchor crossing the
+ * deadline, an `offAt` going stale, Pro's grace lapsing — is pushed by the next call too. */
+function pushIfChanged(): void {
+ const s = state()
+ const json = JSON.stringify(s)
+ if (json === lastSent) return
+ lastSent = json
+ deps.notify(s)
}
/** Install `next`, save it when `save`, and tell this machine's windows if what they see changed. */
function commit(next: Stored, save: boolean): Promise<void> {
- const before = JSON.stringify(compute())
stored = next
const saved = save ? persist() : Promise.resolve()
- if (JSON.stringify(compute()) !== before) notify()
+ pushIfChanged()
return saved
}
@@ -257,7 +276,10 @@ function fail(error: ClaimError, retryAfter?: number): ClaimResult {
async function runClaim(): Promise<ClaimResult> {
const gate = licenseNetworkGate()
if (gate !== 'ok') return fail(gate)
- recordFirstLaunchOpenCount()
+ // Dormant: there is no limit to claim an allowance against, so nothing is asked — no request and
+ // no machine read, whoever calls. After the gate, so `consent_required` / `disabled` still surface.
+ const now = state()
+ if (!now.active) return { ok: true, state: now }
const deviceId = getDeviceId()
const machine = await deps.machineHash()
const count = stored.firstLaunchOpenCount
@@ -279,9 +301,10 @@ async function runClaim(): Promise<ClaimResult> {
status = res.status
try {
body = await res.json()
- } catch {
- // A body cut off by the timeout is the network failing; one that is not JSON is the server's.
- if (ctrl.signal.aborted) return fail('offline')
+ } catch (err) {
+ // Only a body that arrived and is not JSON is the server's. A body cut off by the timeout, or
+ // by the connection dropping mid-read (undici: `TypeError: terminated`), is the network failing.
+ if (ctrl.signal.aborted || !(err instanceof SyntaxError)) return fail('offline')
body = undefined
}
} catch {
@@ -334,6 +357,7 @@ export function initProjectLimit(d: ProjectLimitDeps): ProjectLimitHandle {
stored = load()
osMachine = null
claimInFlight = null
+ lastSent = JSON.stringify(compute())
const handle: ProjectLimitHandle = { state, claim, refresh: notify }
platform().handle(IPC.projectLimitState, () => handle.state())
platform().handle(IPC.projectLimitClaim, () => handle.claim())
@@ -357,7 +381,11 @@ export function initProjectLimit(d: ProjectLimitDeps): ProjectLimitHandle {
track(fetchCheck().catch(() => {}))
}
readCheck()
- setInterval(readCheck, CHECK_INTERVAL_MS).unref()
+ setInterval(() => {
+ // Time alone can change the state (see pushIfChanged); the check below covers the server's word.
+ pushIfChanged()
+ readCheck()
+ }, CHECK_INTERVAL_MS).unref()
return handle
}