The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(devlog): the device key log stays fast and correct as it grows, with old clients unchanged (#986)
0xken154271b0e2f2c4
diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prismaindex cd11b8ca5..a0797a4fd 100644--- a/backend/prisma/schema.prisma+++ b/backend/prisma/schema.prisma@@ -864,6 +864,26 @@ model DeviceKeyLogEntry {@@map("device_key_log")}+/// The account's device key log as of entry `seq` (lib/deviceKeyLog.ts), so an append does not verify+/// the whole log again. Only a cache of `device_key_log`: it is checked against the log's real head on+/// every read, a log that moved on is replayed from here, and one that does not fit is replayed from+/// the start. Devices never see it.+model DeviceKeyLogState {+ id String @id @default(auto()) @map("_id") @db.ObjectId+ userId String @unique+ seq Int+ hash String+ /// Active keys by pub, as `DevLogState.active`.+ active Json+ /// Pubs a `remove` took out, as `DevLogState.removed`.+ removed Json+ /// pub → the Harness session that added that key (`DeviceKeyLogEntry.sessionId`), for active keys.+ sessions Json+ updatedAt DateTime @updatedAt++ @@map("device_key_log_state")+}+/// Public/private link metadata only. The owner daemon keeps the final policy and encrypted discussion.model HarnessLink {id String @id @map("_id")diff --git a/backend/src/lib/deviceKeyLog.test.ts b/backend/src/lib/deviceKeyLog.test.tsindex 336572331..c6170184c 100644--- a/backend/src/lib/deviceKeyLog.test.ts+++ b/backend/src/lib/deviceKeyLog.test.ts@@ -7,6 +7,8 @@ type Row = { userId: string; seq: number; hash: string; entry: string; sessionIdconst m = vi.hoisted(() => ({rows: [] as Array<{ userId: string; seq: number; hash: string; entry: string; sessionId: string | null }>,failNextCreate: null as unknown,+ snaps: new Map<string, Record<string, unknown> & { userId: string; seq: number; hash: string }>(),+ failSnapshot: null as unknown,publishChanged: vi.fn(), publishDown: vi.fn(), revoke: vi.fn(),seen: {} as Record<string, Record<string, string>>,hsetCalls: 0,@@ -23,6 +25,22 @@ vi.mock('./prisma.js', () => ({m.rows.push(data); return data}),},+ deviceKeyLogState: {+ findUnique: vi.fn(async ({ where }: { where: { userId: string } }) => structuredClone(m.snaps.get(where.userId) ?? null)),+ updateMany: vi.fn(async ({ where, data }: { where: { userId: string; seq: number | { lt: number }; hash?: string }; data: Record<string, unknown> }) => {+ if (m.failSnapshot) throw m.failSnapshot+ const cur = m.snaps.get(where.userId)+ const fits = cur && (typeof where.seq === 'number' ? cur.seq === where.seq && cur.hash === where.hash : cur.seq < where.seq.lt)+ if (!fits) return { count: 0 }+ m.snaps.set(where.userId, { ...cur, ...structuredClone(data) } as typeof cur)+ return { count: 1 }+ }),+ create: vi.fn(async ({ data }: { data: { userId: string; seq: number; hash: string } }) => {+ if (m.failSnapshot) throw m.failSnapshot+ if (m.snaps.has(data.userId)) throw Object.assign(new Error('Unique constraint failed'), { code: 'P2002' })+ m.snaps.set(data.userId, structuredClone(data)); return data+ }),+ },harnessSession: { updateMany: m.revoke },},}))@@ -43,7 +61,8 @@ vi.mock('./bus.js', () => ({},}))import { appendDeviceKey, deviceKeysSeen, readDeviceKeyLog, touchDeviceKey } from './deviceKeyLog.js'-import { emptyDevLogState, nextDevLogEntry, signDevLogEntry, applyDevLogEntries, type DevLogState } from './deviceLogCore.js'+import { emptyDevLogState, nextDevLogEntry, signDevLogEntry, applyDevLogEntries, DEVLOG_MAX_ACTIVE, type DevLogState } from './deviceLogCore.js'+import { prisma } from './prisma.js'const b64 = (u: Uint8Array): string => Buffer.from(u).toString('base64')const key = (n: number) => { const priv = new Uint8Array(32).fill(n); return { priv, pub: b64(ed25519.getPublicKey(priv)) } }@@ -68,6 +87,8 @@ describe('appendDeviceKey', () => {beforeEach(() => {m.rows.length = 0m.failNextCreate = null+ m.snaps.clear()+ m.failSnapshot = nullm.seen = {}m.hsetCalls = 0vi.clearAllMocks()@@ -150,6 +171,106 @@ describe('appendDeviceKey', () => {expect(m.publishDown).not.toHaveBeenCalled()})+ const findMany = vi.mocked(prisma.deviceKeyLogEntry.findMany)+ const findFirst = vi.mocked(prisma.deviceKeyLogEntry.findFirst)+ const box1Joins = async () => appendDeviceKey(USER, await addEntry(box1, 'machine', MID1, 'box1'), { kind: 'machine', machineId: MID1 })+ const phoneJoins = async () => appendDeviceKey(USER, await addEntry(phone, 'viewer', '', 'phone'), { kind: 'viewer', harnessSessionId: 'sess-1' })+ /** Appends box2, counting only the reads the append itself makes. */+ async function box2Joins() {+ const e = await addEntry(box2, 'machine', MID2, 'box2')+ findMany.mockClear(); findFirst.mockClear()+ return appendDeviceKey(USER, e, { kind: 'machine', machineId: MID2 })+ }++ it('appends on top of a snapshot that matches the head, without reading the log', async () => {+ await box1Joins(); await phoneJoins()+ expect(m.snaps.get(USER)).toMatchObject({ seq: 2, sessions: { [phone.pub]: 'sess-1' } })+ expect(await box2Joins()).toMatchObject({ ok: true, head: { seq: 3 } })+ expect(findMany).not.toHaveBeenCalled()+ expect(m.snaps.get(USER)).toMatchObject({ seq: 3, hash: m.rows[2].hash })+ expect(Object.keys(m.snaps.get(USER)!.active as object)).toHaveLength(3)+ })++ it('verifies only what came after a stale snapshot', async () => {+ await box1Joins()+ m.failSnapshot = new Error('mongo down')+ await phoneJoins()+ m.failSnapshot = null+ expect(m.snaps.get(USER)).toMatchObject({ seq: 1 })+ expect(await box2Joins()).toMatchObject({ ok: true, head: { seq: 3 } })+ expect(findMany).toHaveBeenCalledTimes(1)+ expect(findMany.mock.calls[0][0]).toMatchObject({ where: { userId: USER, seq: { gt: 1 } } })+ expect(m.snaps.get(USER)).toMatchObject({ seq: 3, sessions: { [phone.pub]: 'sess-1' } })+ })++ it('replays the whole log once when there is no snapshot, and keeps one', async () => {+ await box1Joins(); await phoneJoins()+ m.snaps.clear()+ expect(await box2Joins()).toMatchObject({ ok: true, head: { seq: 3 } })+ expect(findMany.mock.calls.map(c => c[0]!.where)).toEqual([{ userId: USER }])+ expect(m.snaps.get(USER)).toMatchObject({ seq: 3, hash: m.rows[2].hash, sessions: { [phone.pub]: 'sess-1' } })+ })++ it.each([+ ['an older snapshot off the chain', { seq: 1, hash: Buffer.alloc(32, 9).toString('base64') }],+ ['a snapshot past the head', { seq: 9 }],+ ['a snapshot with another hash at the head', { hash: Buffer.alloc(32, 9).toString('base64') }],+ ])('replays the whole log over %s', async (_, bad) => {+ await box1Joins(); await phoneJoins()+ m.snaps.set(USER, { ...m.snaps.get(USER)!, ...bad })+ expect(await box2Joins()).toMatchObject({ ok: true, head: { seq: 3 } })+ expect(findMany.mock.calls.some(c => c[0]!.where?.seq === undefined)).toBe(true)+ expect(m.snaps.get(USER)).toMatchObject({ seq: 3, hash: m.rows[2].hash })+ })++ it('still appends when the snapshot cannot be saved', async () => {+ m.failSnapshot = new Error('mongo down')+ expect(await box1Joins()).toMatchObject({ ok: true, head: { seq: 1 } })+ expect(m.rows).toHaveLength(1)+ expect(m.snaps.size).toBe(0)+ expect(m.publishChanged).toHaveBeenCalled()+ })++ it('answers a lost race with the head alone, not the whole log again', async () => {+ await box1Joins()+ const e = await addEntry(phone, 'viewer', '', 'phone')+ findMany.mockClear(); findFirst.mockClear()+ m.failNextCreate = Object.assign(new Error('Unique constraint failed'), { code: 'P2002' })+ expect(await appendDeviceKey(USER, e, { kind: 'viewer' })).toMatchObject({ ok: false, status: 409, code: 'STALE_HEAD', head: { seq: 1, hash: m.rows[0].hash } })+ expect(findMany).not.toHaveBeenCalled()+ expect(findFirst).toHaveBeenCalledTimes(2)+ })++ it('signs out the session the snapshot remembers for a removed phone', async () => {+ await phoneJoins(); await box1Joins()+ const e = await removeEntry(phone.pub, box1)+ findMany.mockClear()+ await appendDeviceKey(USER, e, { kind: 'machine', machineId: MID1 })+ expect(findMany).not.toHaveBeenCalled()+ expect(m.revoke).toHaveBeenCalledWith(expect.objectContaining({ where: expect.objectContaining({ id: 'sess-1' }) }))+ expect(m.snaps.get(USER)).toMatchObject({ seq: 3, sessions: {}, removed: [phone.pub] })+ })++ it('refuses a key past the most an account may have active', async () => {+ let s = emptyDevLogState(USER)+ const viewer = (i: number) => { const priv = new Uint8Array(32).fill(0x5a); priv[0] = i & 0xff; priv[1] = i >> 8; return { priv, pub: b64(ed25519.getPublicKey(priv)) } }+ for (let i = 0; i < DEVLOG_MAX_ACTIVE; i++) {+ const k = viewer(i)+ const e = signDevLogEntry(nextDevLogEntry(s, { op: 'add', pub: k.pub, kind: 'viewer', machineId: '', label: `v${i}`, signer: k.pub }, 1_000), k.priv)+ s = applyDevLogEntries(s, [e]).state+ m.rows.push({ userId: USER, seq: e.seq, hash: s.head.hash, entry: JSON.stringify(e), sessionId: null })+ }+ const k = viewer(DEVLOG_MAX_ACTIVE)+ const e = signDevLogEntry(nextDevLogEntry(s, { op: 'add', pub: k.pub, kind: 'viewer', machineId: '', label: 'one too many', signer: k.pub }, 1_000), k.priv)+ expect(await appendDeviceKey(USER, e, { kind: 'viewer' })).toMatchObject({ ok: false, status: 400, code: 'TOO_MANY' })+ expect(m.rows).toHaveLength(DEVLOG_MAX_ACTIVE)+ // The replay that found the log full kept a snapshot, so the next try does not verify it all again.+ expect(m.snaps.get(USER)).toMatchObject({ seq: DEVLOG_MAX_ACTIVE })+ findMany.mockClear()+ expect(await appendDeviceKey(USER, e, { kind: 'viewer' })).toMatchObject({ ok: false, status: 400, code: 'TOO_MANY' })+ expect(findMany).not.toHaveBeenCalled()+ })+it('reads from a head, with the head itself', async () => {await appendDeviceKey(USER, await addEntry(box1, 'machine', MID1, 'box1'), { kind: 'machine', machineId: MID1 })await appendDeviceKey(USER, await addEntry(phone, 'viewer', '', 'phone'), { kind: 'viewer' })diff --git a/backend/src/lib/deviceKeyLog.ts b/backend/src/lib/deviceKeyLog.tsindex 94c2a8918..295780996 100644--- a/backend/src/lib/deviceKeyLog.ts+++ b/backend/src/lib/deviceKeyLog.ts@@ -1,3 +1,4 @@+import type { Prisma } from '@prisma/client'import { prisma } from './prisma.js'import { pub, publishDeviceKeysChanged, publishDown } from './bus.js'import { logger } from '../utils/logger.js'@@ -35,26 +36,93 @@ export type DeviceKeyChannel =| { kind: 'viewer'; harnessSessionId?: string }export async function readDeviceKeyLog(userId: string, since: number): Promise<{ acct: string; head: DevLogHead; entries: unknown[] }> {- const [rows, last] = await Promise.all([+ const [rows, head] = await Promise.all([prisma.deviceKeyLogEntry.findMany({ where: { userId, seq: { gt: since } }, orderBy: { seq: 'asc' }, take: PAGE }),- prisma.deviceKeyLogEntry.findFirst({ where: { userId }, orderBy: { seq: 'desc' }, select: { seq: true, hash: true } }),+ logHead(userId),])- const head = last ?? emptyDevLogState(userId).head- return { acct: userId, head: { seq: head.seq, hash: head.hash }, entries: rows.map(r => JSON.parse(r.entry) as unknown) }+ return { acct: userId, head, entries: rows.map(r => JSON.parse(r.entry) as unknown) }}-/** The whole log, verified. It is small and read only when something is appended. */-async function currentState(userId: string): Promise<{ state: DevLogState; sessions: Map<string, string> }> {- const rows = await prisma.deviceKeyLogEntry.findMany({ where: { userId }, orderBy: { seq: 'asc' } })- const { state } = applyDevLogEntries(emptyDevLogState(userId), rows.map(r => JSON.parse(r.entry) as unknown))- const sessions = new Map<string, string>()- for (const r of rows) {- const e = JSON.parse(r.entry) as DevLogEntry- if (r.sessionId && e.op === 'add') sessions.set(e.pub, r.sessionId)+async function logHead(userId: string): Promise<DevLogHead> {+ const last = await prisma.deviceKeyLogEntry.findFirst({ where: { userId }, orderBy: { seq: 'desc' }, select: { seq: true, hash: true } })+ return last ? { seq: last.seq, hash: last.hash } : emptyDevLogState(userId).head+}++/** pub → the Harness session that added that key, for the active keys that carried one. */+type Sessions = Map<string, string>++/** Keep [sessions] in step with one more entry, and the session its row recorded. */+function trackSession(sessions: Sessions, entry: DevLogEntry, sessionId: string | null | undefined): void {+ if (entry.op === 'remove') sessions.delete(entry.pub)+ else if (sessionId) sessions.set(entry.pub, sessionId)+}++/** [rows] verified on top of [base], every rule checked again; [sessions] follows them. */+function replay(base: DevLogState, sessions: Sessions, rows: Array<{ entry: string; sessionId: string | null }>): DevLogState {+ const entries = rows.map(r => JSON.parse(r.entry) as DevLogEntry)+ const { state } = applyDevLogEntries(base, entries)+ entries.forEach((e, i) => trackSession(sessions, e, rows[i].sessionId))+ return state+}++/**+ * The log as of its head, verified. Verifying it whole costs about a millisecond an entry, too much on+ * every append once a log grows, so it starts from the snapshot (DeviceKeyLogState) and verifies only+ * what came after it. The snapshot is checked against the real head each time, never trusted to be+ * current — any pod may have appended since. One that does not fit the log is replayed over from the+ * start. `hashes` holds only what was replayed: nothing here looks an older entry up.+ */+async function currentState(userId: string): Promise<{ state: DevLogState; sessions: Sessions }> {+ const [snap, head] = await Promise.all([+ prisma.deviceKeyLogState.findUnique({ where: { userId } }),+ logHead(userId),+ ])+ if (snap) {+ try {+ const state: DevLogState = {+ acct: userId, head: { seq: snap.seq, hash: snap.hash }, hashes: [],+ active: snap.active as unknown as DevLogState['active'], removed: snap.removed as string[],+ }+ const sessions: Sessions = new Map(Object.entries(snap.sessions as Record<string, string>))+ if (snap.seq === head.seq && snap.hash === head.hash) return { state, sessions }+ if (snap.seq >= head.seq) throw new Error('snapshot is not on the way to the head')+ const rows = await prisma.deviceKeyLogEntry.findMany({ where: { userId, seq: { gt: snap.seq } }, orderBy: { seq: 'asc' } })+ const next = replay(state, sessions, rows)+ await saveSnapshot(userId, next, sessions)+ return { state: next, sessions }+ } catch (err) {+ logger.warn('device key log snapshot does not fit the log; replaying the whole log', { userId, seq: snap.seq, error: String(err) })+ }}+ const rows = await prisma.deviceKeyLogEntry.findMany({ where: { userId }, orderBy: { seq: 'asc' } })+ const sessions: Sessions = new Map()+ const state = replay(emptyDevLogState(userId), sessions, rows)+ if (rows.length || snap) await saveSnapshot(userId, state, sessions, snap ?? undefined)return { state, sessions }}+/**+ * Keep the snapshot at [state]. It only moves forward, so a slower append cannot put an older one back+ * — except over [replacing], a snapshot found not to fit the log, which is overwritten only if it is+ * still the one that was read. Best effort: without it the next append replays from where it stands.+ */+async function saveSnapshot(userId: string, state: DevLogState, sessions: Sessions, replacing?: DevLogHead): Promise<void> {+ const data = {+ seq: state.head.seq, hash: state.head.hash, active: state.active as unknown as Prisma.InputJsonObject,+ removed: state.removed, sessions: Object.fromEntries(sessions),+ }+ try {+ const { count } = await prisma.deviceKeyLogState.updateMany({+ where: replacing ? { userId, seq: replacing.seq, hash: replacing.hash } : { userId, seq: { lt: data.seq } },+ data,+ })+ if (count === 0 && !replacing) await prisma.deviceKeyLogState.create({ data: { userId, ...data } })+ } catch (err) {+ // A unique violation is a snapshot already there, at this head or past it: nothing to save.+ if (!isUniqueViolation(err)) logger.warn('device key log snapshot not saved', { userId, seq: data.seq, error: String(err) })+ }+}+function channelAllows(entry: DevLogEntry, state: DevLogState, channel: DeviceKeyChannel): boolean {const signer = state.active[entry.signer]if (channel.kind === 'machine') {@@ -87,23 +155,19 @@ export async function appendDeviceKey(userId: string, raw: unknown, channel: Devthrow err}const head = applied.state.head+ // The Harness session that added this key (a phone signed in by QR, or a computer a phone signed+ // in), so removing the key signs that session out too.+ const sessionId = entry.op === 'add' ? channel.harnessSessionId ?? null : nulltry {- await prisma.deviceKeyLogEntry.create({- data: {- userId, seq: entry.seq, hash: head.hash, entry: JSON.stringify(entry),- // The Harness session that added this key (a phone signed in by QR, or a computer a phone- // signed in), so removing the key signs that session out too.- sessionId: entry.op === 'add' ? channel.harnessSessionId ?? null : null,- },- })+ await prisma.deviceKeyLogEntry.create({ data: { userId, seq: entry.seq, hash: head.hash, entry: JSON.stringify(entry), sessionId } })} catch (err) {// Another device appended at this head first.- if (isUniqueViolation(err)) {- const { state: now } = await currentState(userId)- return { ok: false, status: 409, code: 'STALE_HEAD', head: now.head }- }+ if (isUniqueViolation(err)) return { ok: false, status: 409, code: 'STALE_HEAD', head: await logHead(userId) }throw err}+ const nextSessions = new Map(sessions)+ trackSession(nextSessions, entry, sessionId)+ await saveSnapshot(userId, applied.state, nextSessions)await publishDeviceKeysChanged(userId, head).catch(() => { /* best effort: devices also re-read on their own */ })touchDeviceKey(userId, entry.signer)for (const gone of applied.removed) await signOut(userId, gone.kind, gone.machineId, gone.pub, sessions.get(gone.pub))diff --git a/backend/src/routes/deviceKeys.test.ts b/backend/src/routes/deviceKeys.test.tsindex 131dc6fd1..541eb3b4d 100644--- a/backend/src/routes/deviceKeys.test.ts+++ b/backend/src/routes/deviceKeys.test.ts@@ -1,7 +1,7 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import Fastify, { type FastifyInstance } from 'fastify'-const m = vi.hoisted(() => ({ auth: vi.fn(), read: vi.fn(), append: vi.fn(), seen: vi.fn() }))-vi.mock('../lib/deviceKeyLog.js', () => ({ readDeviceKeyLog: m.read, appendDeviceKey: m.append, deviceKeysSeen: m.seen }))+const m = vi.hoisted(() => ({ auth: vi.fn(), read: vi.fn(), append: vi.fn(), seen: vi.fn(), touch: vi.fn() }))+vi.mock('../lib/deviceKeyLog.js', () => ({ readDeviceKeyLog: m.read, appendDeviceKey: m.append, deviceKeysSeen: m.seen, touchDeviceKey: m.touch }))vi.mock('../lib/ssoAuth.js', async original => ({ ...await original<typeof import('../lib/ssoAuth.js')>(), authenticateAccessToken: m.auth }))import { deviceKeyRoutes } from './deviceKeys.js'import { registerAuthMiddleware } from '../middlewares/authMiddleware.js'@@ -32,6 +32,26 @@ describe('device key routes', () => {expect(r.headers['cache-control']).toBe('no-store')expect(r.json().data).toEqual({ acct: 'user-1', head, entries: [] })expect(m.read).toHaveBeenCalledWith('user-1', 2)+ expect(m.touch).not.toHaveBeenCalled()+ })++ it('counts a read as the reading key being in use, and reads the same either way', async () => {+ m.read.mockResolvedValue({ acct: 'user-1', head, entries: [] })+ const self = Buffer.alloc(32, 4).toString('base64')+ const r = await app.inject({ method: 'GET', url: `/api/device-keys?since=2&self=${encodeURIComponent(self)}`, headers: auth })+ expect(r.statusCode).toBe(200)+ expect(r.json().data).toEqual({ acct: 'user-1', head, entries: [] })+ expect(m.touch).toHaveBeenCalledWith('user-1', self)+ expect(m.read).toHaveBeenCalledWith('user-1', 2)+ })++ it('reads the log even when self is not a key', async () => {+ m.read.mockResolvedValue({ acct: 'user-1', head, entries: [] })+ for (const q of ['self=nope', 'self=a&self=b']) {+ const r = await app.inject({ method: 'GET', url: `/api/device-keys?since=0&${q}`, headers: auth })+ expect(r.statusCode).toBe(200)+ expect(r.json().data).toEqual({ acct: 'user-1', head, entries: [] })+ }})it('appends over the viewer channel, carrying a Harness session', async () => {diff --git a/backend/src/routes/deviceKeys.ts b/backend/src/routes/deviceKeys.tsindex b83728745..2c7c08d2f 100644--- a/backend/src/routes/deviceKeys.ts+++ b/backend/src/routes/deviceKeys.ts@@ -1,10 +1,16 @@import type { FastifyInstance } from 'fastify'import { z } from 'zod'-import { appendDeviceKey, deviceKeysSeen, readDeviceKeyLog } from '../lib/deviceKeyLog.js'+import { appendDeviceKey, deviceKeysSeen, readDeviceKeyLog, touchDeviceKey } from '../lib/deviceKeyLog.js'import { validateBody, validateQuery } from '../middlewares/validation.js'import { sendError, sendSuccess } from '../utils/response.js'-const query = z.object({ since: z.coerce.number().int().min(0).default(0) })+const query = z.object({+ since: z.coerce.number().int().min(0).default(0),+ // The reading device's own key: reading the log counts as the key being in use, so an app that is+ // opened but never opens a session is not offered for removal. Anything else is ignored, not+ // refused — the read must work the same with or without it.+ self: z.string().optional().catch(undefined),+})/** Appends per account per minute, per process — a viewer appends its key once per sign-in and a* removal when someone asks. Generous for that; a loop cannot rebuild the log on every request. */@@ -31,6 +37,7 @@ const body = z.object({ entry: z.record(z.string(), z.unknown()) }).strict()export async function deviceKeyRoutes(app: FastifyInstance): Promise<void> {app.get<{ Querystring: z.infer<typeof query> }>('/api/device-keys', { preHandler: validateQuery(query) }, async (req, reply) => {reply.header('Cache-Control', 'no-store')+ if (req.query.self !== undefined) touchDeviceKey(req.user!.sub, req.query.self)sendSuccess(reply, await readDeviceKeyLog(req.user!.sub, req.query.since))})diff --git a/cli/src/gateway/start.ts b/cli/src/gateway/start.tsindex ec48a255d..abcc5a07d 100644--- a/cli/src/gateway/start.ts+++ b/cli/src/gateway/start.ts@@ -206,6 +206,13 @@ export function startGateway(host: GatewayHost): StartedGateway {syncer.start()}if (host.signedIn) { groupStarted = true; syncer.start() }+ // When each key last opened a session or read the log, from the backend: the Devices list's "last+ // active", and what `sweepStale` judges an unused app key by. null when it could not be asked.+ const deviceKeysSeen = async (): Promise<Record<string, number> | null> => {+ const r = await host.backend('GET', '/api/device-keys/seen').catch(() => null)+ const seen = r?.status === 200 ? (r.body.data as { seen?: unknown } | undefined)?.seen : undefined+ return seen && typeof seen === 'object' ? seen as Record<string, number> : null+ }const devlog = new DeviceLogSyncer({store: new DeviceLogStore(),identity: () => { const id = relayIdentityStore.getIdentity(); return { pub: b64e(id.pub), priv: id.priv } },@@ -225,7 +232,9 @@ export function startGateway(host: GatewayHost): StartedGateway {syncer.reset()},fetch: async (since) => {- const r = await host.backend('GET', `/api/device-keys?since=${since}`)+ // `self`: this read counts as this machine's key being used (a backend from before it ignores it).+ const self = encodeURIComponent(b64e(relayIdentityStore.getIdentity().pub))+ const r = await host.backend('GET', `/api/device-keys?since=${since}&self=${self}`)const data = r.status === 200 ? r.body.data as Partial<DeviceLogFetched> | undefined : undefinedconst head = data?.head as { seq?: unknown; hash?: unknown } | undefinedif (!data || typeof data.acct !== 'string' || !Array.isArray(data.entries) || typeof head?.seq !== 'number'@@ -274,9 +283,13 @@ export function startGateway(host: GatewayHost): StartedGateway {spendIdentity()host.events.revoked()},+ seen: deviceKeysSeen,changed: () => host.events.toWindows({ type: 'device_keys_changed', payload: {} }),log: (line) => console.log(line),})+ // Each register is also the offer of a sweep of unused app keys (at most one per 6 hours, on one+ // machine of the account: deviceLogSyncer `sweepStale`).+ const registerAndSweep = (): void => { void devlog.register().then(() => devlog.sweepStale()) }devLogSyncer = devlogsyncer.devlog = devlog// Removed while online: the backend's `machine_revoked` arrives before this machine reads the log, and@@ -301,9 +314,9 @@ export function startGateway(host: GatewayHost): StartedGateway {// never registered after `harness login` signed it in (register does nothing while signed out).gateway.onLinkUp = () => {startGroup()- void devlog.register()+ registerAndSweep()}- const devlogTimer = setInterval(() => { void devlog.register() }, 10 * 60_000)+ const devlogTimer = setInterval(registerAndSweep, 10 * 60_000)devlogTimer.unref()// Signed out there is no account, and no device log of one, to find a key on.const readForHello = unknownHelloReader(devlog, (line) => console.log(line))@@ -384,9 +397,7 @@ export function startGateway(host: GatewayHost): StartedGateway {devicesList: async () => {// When each key last opened a session, from the backend — a hint for removing apps not used in a// long while. Without it the list is still the list.- const seen = await host.backend('GET', '/api/device-keys/seen').catch(() => null)- const lastSeen = seen?.status === 200 ? (seen.body.data as { seen?: unknown } | undefined)?.seen : undefined- return { status: 200, body: { ...devlog.list(), lastSeen: lastSeen && typeof lastSeen === 'object' ? lastSeen : {} } }+ return { status: 200, body: { ...devlog.list(), lastSeen: await deviceKeysSeen() ?? {} } }},devicesRemove: async (pub) => {syncer.remove(pub)@@ -432,7 +443,7 @@ export function startGateway(host: GatewayHost): StartedGateway {account: (next) => {account = nextstartGroup()- void devlog.register()+ registerAndSweep()},reachable: (machineIds) => { reachable = machineIds ? new Set(machineIds) : null },lane,diff --git a/cli/src/lib/e2ee/deviceDisplay.spec.ts b/cli/src/lib/e2ee/deviceDisplay.spec.tsindex 55abfc86c..0b893a32f 100644--- a/cli/src/lib/e2ee/deviceDisplay.spec.ts+++ b/cli/src/lib/e2ee/deviceDisplay.spec.ts@@ -145,6 +145,13 @@ describe('formatDeviceList', () => {expect(lines).toContain(` This machine: mbp ${FP_SELF} (not registered yet)`)expect(lines.join('\n')).not.toContain('(this machine)')})+ it('says why the account refused this machine\'s key', () => {+ const l = { ...listing(), registerError: 'TOO_MANY' }+ l.members = l.members.filter((m) => !m.self)+ const lines = formatDeviceList(l, NOW, { label: 'mbp', fp: FP_SELF })+ expect(lines).toContain(' ⚠ This account has too many devices — remove unused ones: harness devices remove <fingerprint>')+ expect(formatDeviceList({ ...l, registerError: 'X' }, NOW, { label: 'mbp', fp: FP_SELF })).toContain(" ⚠ The account refused this computer's key (X) — see the daemon's log")+ })it('prints no header at all without a self row or fallback', () => {const l = listing()l.members = l.members.filter((m) => !m.self)diff --git a/cli/src/lib/e2ee/deviceDisplay.ts b/cli/src/lib/e2ee/deviceDisplay.tsindex f83527308..f02af80d9 100644--- a/cli/src/lib/e2ee/deviceDisplay.ts+++ b/cli/src/lib/e2ee/deviceDisplay.ts@@ -149,6 +149,7 @@ export function formatDeviceList(members: DeviceRow[]; lastSeen?: Record<string, number>; pending?: string[]; suspended?: string[]conflict?: Pick<DevLogConflict, 'label' | 'fingerprint' | 'addedAt' | 'afterJoin'> | nulldeparted?: Array<Pick<DevLogDeparted, 'label' | 'fingerprint' | 'removedBy' | 'removedByLabel' | 'selfRemoved'>>+ registerError?: string},now: number,selfFallback?: { label: string; fp: string },@@ -158,7 +159,17 @@ export function formatDeviceList(const lines: string[] = ['']const self = rows.find((r) => r.self)if (self) lines.push(` This machine: ${nameOf(self)} ${self.fingerprint}`, '')- else if (selfFallback) lines.push(` This machine: ${selfFallback.label} ${selfFallback.fp} (not registered yet)`, '')+ else if (selfFallback) {+ lines.push(` This machine: ${selfFallback.label} ${selfFallback.fp} (not registered yet)`)+ // The backend refused this machine's key: say why, or the person waits for a join that never comes.+ const why = listing.registerError+ if (why) {+ lines.push(why === 'TOO_MANY'+ ? ' ⚠ This account has too many devices — remove unused ones: harness devices remove <fingerprint>'+ : ` ⚠ The account refused this computer's key (${why}) — see the daemon's log`)+ }+ lines.push('')+ }const c = listing.conflictif (c) {// No "added <when>": the holder's `addedAt` is whatever its signer wrote.diff --git a/cli/src/lib/e2ee/deviceLogSyncer.spec.ts b/cli/src/lib/e2ee/deviceLogSyncer.spec.tsindex edde7c41f..7b1be564a 100644--- a/cli/src/lib/e2ee/deviceLogSyncer.spec.ts+++ b/cli/src/lib/e2ee/deviceLogSyncer.spec.ts@@ -10,6 +10,7 @@ import {import { DeviceLogStore } from './deviceLogStore.js'import { DeviceLogSyncer, devLogDivergence, type DeviceLogAppendAnswer, type DeviceLogFetched, type DeviceLogRebaseline } from './deviceLogSyncer.js'import { ADOPTED_SIGN_IN } from '../authSession.js'+import { b64d, fingerprint } from './core.js'const b64 = (u: Uint8Array): string => Buffer.from(u).toString('base64')const key = (n: number) => { const priv = new Uint8Array(32).fill(n); return { priv, pub: b64(ed25519.getPublicKey(priv)) } }@@ -72,6 +73,7 @@ class FakeBackend {function setup(opts: {known?: string[]; tombstoned?: string[]; blocked?: string[]; store?: DeviceLogStore; backend?: FakeBackend; signIn?: () => string | null; signInAt?: number | nullisTrusted?: (pub: string) => boolean; signInAcct?: () => string | null; switchAccount?: (from: string, to: string) => void+ now?: () => number; seen?: () => Promise<Record<string, number> | null>; log?: (line: string) => void} = {}) {const backend = opts.backend ?? new FakeBackend()const store = opts.store ?? new DeviceLogStore(join(mkdtempSync(join(tmpdir(), 'devlog-')), 'devlog.json'))@@ -107,7 +109,9 @@ function setup(opts: {resume: calls.resume,signedOut: calls.signedOut,changed: calls.changed,- now: () => 5_000,+ ...(opts.seen ? { seen: opts.seen } : {}),+ ...(opts.log ? { log: opts.log } : {}),+ now: opts.now ?? (() => 5_000),sleep: async () => {},})return { backend, store, syncer, calls, trusted }@@ -2031,3 +2035,187 @@ describe('DeviceLogSyncer — a review (rebaseline --yes)', () => {})})})++/** Serve the log one entry per page, as a backend may. */+function onePerPage(backend: FakeBackend): void {+ const real = backend.fetch.getMockImplementation()!+ backend.fetch.mockImplementation(async (since) => { const g = await real(since); return g && { ...g, entries: g.entries.slice(0, 1) } })+}++describe('DeviceLogSyncer — a log longer than a usual read', () => {+ const many = (n: number) => Array.from({ length: n }, (_, i) => key(10 + i))++ it('a first read goes past the usual page cap, so nothing on the account is announced', async () => {+ const t = setup()+ for (const k of many(25)) t.backend.add(k, 'viewer', '', 'app')+ onePerPage(t.backend)+ await t.syncer.refresh()+ expect(t.store.read()).toMatchObject({ joinedSeq: 25, pending: [] })+ expect(t.store.read().joining).toBeUndefined()+ expect(t.syncer.list().baseline).toHaveLength(25)+ expect(t.calls.announce).not.toHaveBeenCalled()+ // Once joined, a read is back to the usual cap.+ for (const k of many(50).slice(25)) t.backend.add(k, 'viewer', '', 'app')+ t.backend.fetch.mockClear()+ await t.syncer.refresh()+ expect(t.backend.fetch).toHaveBeenCalledTimes(20)+ expect(t.store.read().state?.head.seq).toBe(45)+ })++ it('a review reads a list longer than the usual page cap', async () => {+ const t = setup()+ t.backend.add(box2, 'machine', MID2, 'box2')+ await t.syncer.register()+ t.backend.lie = t.backend.entries.slice(0, 1)+ await t.syncer.refresh()+ expect(t.store.read().frozen).not.toBeNull()+ t.backend.lie = null+ for (const k of many(25)) t.backend.add(k, 'viewer', '', 'app')+ onePerPage(t.backend)+ const done = await t.syncer.rebaseline(true) as DeviceLogRebaseline+ expect(done.head).toEqual(t.backend.state.head)+ expect(t.store.read().frozen).toBeNull()+ expect(t.store.read().state?.head.seq).toBe(27)+ })+})++describe('DeviceLogSyncer.remove asked again from its own drop', () => {+ it('appends one removal and answers ok to both asks', async () => {+ // The trust group hands a dropped key straight back (`onDropped` → remove) before the first ask's+ // first await: that second ask must join the first, not append a removal of its own.+ const t = setup()+ t.backend.add(phone, 'viewer', '', 'phone')+ await t.syncer.register()+ let again: Promise<unknown> | null = null+ t.calls.drop.mockImplementation((pub: string) => { again ??= t.syncer.remove(pub) })+ const first = await t.syncer.remove(phone.pub)+ expect(first).toEqual({ ok: true })+ expect(await again).toEqual({ ok: true })+ expect(t.backend.entries.filter((e) => e.op === 'remove' && e.pub === phone.pub)).toHaveLength(1)+ })+})++describe('DeviceLogSyncer — register refusals', () => {+ it('keeps why the backend refused this machine\'s key, until a register gets through', async () => {+ const t = setup()+ t.backend.append.mockImplementationOnce(async () => ({ error: 'TOO_MANY' }))+ await t.syncer.register()+ expect(t.syncer.list().registerError).toBe('TOO_MANY')+ await t.syncer.register()+ expect(t.backend.state.active[me.pub]).toBeDefined()+ expect(t.syncer.list().registerError).toBeUndefined()+ })++ it('a race (STALE_HEAD) is not a refusal', async () => {+ const t = setup()+ t.backend.append.mockImplementation(async () => ({ error: 'STALE_HEAD' }))+ await t.syncer.register()+ expect(t.syncer.list().registerError).toBeUndefined()+ })+})++describe('DeviceLogSyncer.sweepStale', () => {+ const DAY = 24 * 60 * 60_000+ const NOW = 400 * DAY+ /** Machine keys whose public key sorts before / after this machine's. */+ const pool = Array.from({ length: 40 }, (_, i) => key(100 + i))+ const smaller = pool.find((k) => k.pub < me.pub)!+ const larger = pool.find((k) => k.pub > me.pub)!+ let clock: number+ let seen: Record<string, number> | null+ let lines: string[]+ let t: ReturnType<typeof setup>+ beforeEach(() => {+ clock = NOW+ seen = {}+ lines = []+ t = setup({ now: () => clock, seen: async () => seen, log: (l) => lines.push(l) })+ })+ const active = (pub: string): boolean => !!t.backend.state.active[pub]++ it('removes only app keys added and last used over 180 days ago', async () => {+ const stale = key(50), usedLately = key(51), young = key(52), suspended = key(53)+ t.backend.add(larger, 'machine', MID2, 'old-box', 1_000)+ t.backend.add(stale, 'viewer', '', 'old-browser', 1_000)+ t.backend.add(usedLately, 'viewer', '', 'phone', 1_000)+ t.backend.add(young, 'viewer', '', 'new-browser', NOW - 10 * DAY)+ t.backend.add(suspended, 'viewer', '', 'forked', 1_000)+ seen = { [usedLately.pub]: NOW - DAY, [larger.pub]: 1_000 }+ await t.syncer.register()+ t.store.write({ ...t.store.read(), suspended: [suspended.pub] })+ await t.syncer.sweepStale()+ expect(active(stale.pub)).toBe(false)+ expect([usedLately, young, suspended, larger, me].every((k) => active(k.pub))).toBe(true)+ expect(t.backend.entries.at(-1)).toMatchObject({ op: 'remove', pub: stale.pub, signer: me.pub })+ expect(lines).toContain(`[devlog] removed unused device old-browser (${fingerprint(b64d(stale.pub))}), last used 1970-01-01`)+ })++ it('a key the backend has no last use for counts as used when it was added', async () => {+ const recent = key(50)+ t.backend.add(recent, 'viewer', '', 'app', NOW - 200 * DAY)+ seen = { [recent.pub]: NOW - 100 * DAY }+ await t.syncer.register()+ await t.syncer.sweepStale()+ expect(active(recent.pub)).toBe(true)+ seen = {}+ clock += 7 * 60 * 60_000+ await t.syncer.sweepStale()+ expect(active(recent.pub)).toBe(false)+ })++ it('the machine with the smallest key goes first; the next takes over a day later', async () => {+ // A machine before this one in key order (its turn first) that never sweeps — a build from before the+ // sweep, seen on a rollout — must not keep stale keys on the account for good.+ const stale = key(50)+ t.backend.add(smaller, 'machine', MID2, 'other-box', 1_000)+ t.backend.add(stale, 'viewer', '', 'old-browser', NOW - 180 * DAY - DAY / 2)+ await t.syncer.register()+ await t.syncer.sweepStale()+ expect(active(stale.pub)).toBe(true) // stale for the first machine, not yet for the second+ clock += DAY+ await t.syncer.sweepStale()+ expect(active(stale.pub)).toBe(false)+ })++ it('nothing while the log is frozen, or when last use cannot be asked', async () => {+ const stale = key(50)+ t.backend.add(stale, 'viewer', '', 'old-browser', 1_000)+ await t.syncer.register()+ seen = null+ await t.syncer.sweepStale()+ expect(active(stale.pub)).toBe(true)+ t.store.write({ ...t.store.read(), frozen: { reason: 'fork', at: NOW, lastGoodHead: t.store.read().state!.head } })+ seen = {}+ await t.syncer.sweepStale()+ expect(active(stale.pub)).toBe(true)+ // Neither counted as a sweep: the next offer runs.+ t.store.write({ ...t.store.read(), frozen: null })+ await t.syncer.sweepStale()+ expect(active(stale.pub)).toBe(false)+ })++ it('at most 20 per sweep, and one sweep per 6 hours', async () => {+ const apps = pool.slice(0, 25)+ for (const k of apps) t.backend.add(k, 'viewer', '', 'app', 1_000)+ await t.syncer.register()+ await t.syncer.sweepStale()+ expect(apps.filter((k) => active(k.pub))).toHaveLength(5)+ clock += 5 * 60 * 60_000+ await t.syncer.sweepStale()+ expect(apps.filter((k) => active(k.pub))).toHaveLength(5)+ clock += 60 * 60_000+ await t.syncer.sweepStale()+ expect(apps.filter((k) => active(k.pub))).toHaveLength(0)+ })++ it('stops at the first refusal', async () => {+ const a = key(50), b = key(51)+ t.backend.add(a, 'viewer', '', 'a', 1_000)+ t.backend.add(b, 'viewer', '', 'b', 1_000)+ await t.syncer.register()+ t.backend.append.mockImplementationOnce(async () => ({ error: 'RATE_LIMITED' }))+ await t.syncer.sweepStale()+ expect(active(a.pub) && active(b.pub)).toBe(true)+ expect(t.backend.append).toHaveBeenCalledTimes(2) // register + the refused removal+ })+})diff --git a/cli/src/lib/e2ee/deviceLogSyncer.ts b/cli/src/lib/e2ee/deviceLogSyncer.tsindex 694c40abb..7a3756fc0 100644--- a/cli/src/lib/e2ee/deviceLogSyncer.ts+++ b/cli/src/lib/e2ee/deviceLogSyncer.ts@@ -96,6 +96,9 @@ export interface DeviceLogSyncerDeps {resume?: () => void/** This machine's own key was removed from the log: it is signed out. */signedOut: () => void+ /** When each key last opened a session or read the log (ms), from the backend; null when it could not+ * be asked. Only `sweepStale` reads it. */+ seen?: () => Promise<Record<string, number> | null>/** Something a window shows changed (the list, the frozen line). */changed?: () => voidlog?: (line: string) => void@@ -125,6 +128,9 @@ export interface DeviceLogListing {conflict: DevLogConflict | null/** New keys removed before anyone marked them as seen: flagged until dismissed (oldest first). */departed: DevLogDeparted[]+ /** Why the backend last refused this machine's own key (`TOO_MANY`, …), until a register gets through.+ * Without it a refusal only reached the daemon's log, and the machine just stayed "not registered". */+ registerError?: string}/** A device taken out of the account's log after this machine joined it. */@@ -168,6 +174,21 @@ export interface DeviceLogRebaseline {const APPEND_ATTEMPTS = 5const PAGES = 20+/** The page cap of a first read of a log (and of a review): 100,000 entries. At PAGES a log past 10,000+ * entries ended its join part-way, and every key after the cut was announced as a new device. */+const JOIN_PAGES = 200+/** `sweepStale`: an app key unused this long, and added at least this long ago, is taken out of the log —+ * a browser whose storage was cleared, an app reinstalled, never comes back for its key, and at 256+ * active keys (DEVLOG_MAX_ACTIVE) no new device can join the account. */+export const STALE_AFTER_MS = 180 * 24 * 60 * 60_000+/** At most one sweep per this long on a machine; the timer that offers it runs every 10 minutes. */+const SWEEP_EVERY_MS = 6 * 60 * 60_000+/** At most this many removals per sweep (the next one goes on), each SWEEP_GAP_MS apart: the backend+ * takes 10 appends a minute from a machine, and a person's own `devices remove` must still get through. */+const SWEEP_MAX = 20+const SWEEP_GAP_MS = 10_000+/** How much later each next machine (by key order) takes over a sweep the ones before it have not done. */+const SWEEP_TURN_MS = 24 * 60 * 60_000/** The newest hashes a `group_sync` carries, so a fork can be located. */const GOSSIP_HASHES = 64@@ -219,6 +240,11 @@ export class DeviceLogSyncer {private historying: Promise<DeviceLogHistory> | null = null/** Every entry fetched for the history so far (seq 1..n), this run only: never written to disk. */private histCache: { acct: string; entries: DevLogEntry[] } | null = null+ /** Why the backend last refused this machine's own key; this run only (the file format stays as older+ * versions read it). */+ private registerError: string | null = null+ /** When `sweepStale` last ran here (ms); this run only. */+ private lastSweep: number | null = nullconstructor(private readonly deps: DeviceLogSyncerDeps) {this.now = deps.now ?? (() => Date.now())@@ -239,6 +265,7 @@ export class DeviceLogSyncer {if (state.removed.includes(pub)) { this.deps.signedOut(); return }const mine = state.active[pub]if (mine) {+ this.registerError = nullthis.clearConflict()if (mine.label === self.label || mine.machineId !== self.machineId) return} else {@@ -250,8 +277,13 @@ export class DeviceLogSyncer {}, this.now()), priv)const answer = await this.deps.append(entry)if (!answer) return- if ('head' in answer && !('error' in answer)) { await this.refresh(); return }- if (answer.error !== 'STALE_HEAD') { this.deps.log?.(`[devlog] register refused: ${answer.error}`); return }+ if ('head' in answer && !('error' in answer)) { this.registerError = null; await this.refresh(); return }+ if (answer.error !== 'STALE_HEAD') {+ this.registerError = answer.error+ this.deps.log?.(`[devlog] register refused: ${answer.error}`)+ this.deps.changed?.()+ return+ }await this.refresh()await this.sleep(200 + Math.floor(Math.random() * 800) * (attempt + 1))}@@ -474,7 +506,9 @@ export class DeviceLogSyncer {// The first read of a log (a new install, or an existing sign-in from before the log existed) only// learns what is there: every key it adopts, and none of it is news (it is before `joinedSeq`).let bootstrap = !this.deps.store.read().state?.head.seq- for (let page = 0; page < PAGES; page++) {+ // The cap is read each page: a first read only learns what is there, so it may go far; once it is+ // over (`endJoin`, unconditional below) a stalled or endless backend gets the usual PAGES.+ for (let page = 0; page < (this.deps.store.read().joining ? JOIN_PAGES : PAGES); page++) {const file = this.deps.store.read()const since = file.state?.head.seq ?? 0const got = await this.deps.fetch(since)@@ -734,7 +768,11 @@ export class DeviceLogSyncer {const running = this.inFlightRemovals.get(pub)if (running) return runningthis.removing.add(pub)- const task = this.removeOnce(pub).finally(() => { this.removing.delete(pub); this.inFlightRemovals.delete(pub) })+ // Started a tick later, once it is in the map: `removeOnce` drops the key before its first await, and+ // that drop's `onDropped` asks again at once — begun right away, the second ask found no entry and+ // appended its own removal, and this one came back NOT_IN_LOG for a key it had just taken out.+ const task = Promise.resolve().then(() => this.removeOnce(pub))+ .finally(() => { this.removing.delete(pub); this.inFlightRemovals.delete(pub) })this.inFlightRemovals.set(pub, task)return task}@@ -763,6 +801,56 @@ export class DeviceLogSyncer {return { ok: false, error: 'UNAVAILABLE' }}+ /**+ * Take out of the log the app keys nobody has used in STALE_AFTER_MS (and added that long ago too), at+ * most SWEEP_MAX at a time and one sweep per SWEEP_EVERY_MS. The machine whose key is the smallest of+ * the active machine keys goes first; each next one in that order waits a further SWEEP_TURN_MS before+ * it takes a key, so one machine does it with no coordination — and when that one is a build from before+ * the sweep (seen on a rollout: it never would) or is off, the next takes over a day later. Two that+ * both do get NOT_ACTIVE on the second removal, harmless. Never a machine key: when a machine was last used+ * is not something the backend can tell. Only on this sign-in's log, and never while it is frozen.+ * Never throws.+ *+ * ponytail: last-seen comes from the backend unverified, so a backend that lies about it can have a+ * machine remove an app key older than STALE_AFTER_MS (the removal is in every device's history, signed+ * by this machine). Upgrade path: a last-used time the app signs itself.+ */+ async sweepStale(): Promise<void> {+ try {+ const seen = this.deps.seen+ const now = this.now()+ if (!seen || (this.lastSweep !== null && now - this.lastSweep < SWEEP_EVERY_MS) || !this.current()) return+ const file = this.deps.store.read()+ const state = file.state+ if (!state || file.frozen) return+ const selfPub = this.deps.identity().pub+ const machines = Object.values(state.active).filter((m) => m.kind === 'machine').map((m) => m.pub).sort()+ const turn = machines.indexOf(selfPub)+ if (turn < 0) return+ this.lastSweep = now+ const lastUsed = await seen()+ // Not asked: try again on the next offer rather than read every key as never used.+ if (!lastUsed) { this.lastSweep = null; return }+ const cutoff = now - STALE_AFTER_MS - turn * SWEEP_TURN_MS+ const suspended = this.suspendedKeys()+ const usedAt = (m: DevLogMember): number => { const t = lastUsed[m.pub]; return typeof t === 'number' && Number.isFinite(t) ? t : m.addedAt }+ const stale = Object.values(state.active)+ .filter((m) => m.kind === 'viewer' && m.pub !== selfPub && !suspended.includes(m.pub) && m.addedAt < cutoff && usedAt(m) < cutoff)+ .sort((a, b) => a.seq - b.seq)+ .slice(0, SWEEP_MAX)+ for (const [i, m] of stale.entries()) {+ if (i) await this.sleep(SWEEP_GAP_MS)+ const r = await this.remove(m.pub)+ // Already out of the log (another machine's sweep, a person's own removal): nothing to do.+ if (!r.ok && r.error === 'NOT_IN_LOG') continue+ if (!r.ok) { this.deps.log?.(`[devlog] could not remove unused device ${m.label || '(no name)'} (${fp(m.pub)}): ${r.detail ?? r.error}`); return }+ this.deps.log?.(`[devlog] removed unused device ${m.label || '(no name)'} (${fp(m.pub)}), last used ${new Date(usedAt(m)).toISOString().slice(0, 10)}`)+ }+ } catch (err) {+ this.deps.log?.(`[devlog] sweep failed: ${err instanceof Error ? err.message : String(err)}`)+ }+ }+/*** What trusting the backend's log again would change; with `confirm`, do it. `expected` is the head* the person was shown in the preview: if the backend's log is not that one any more, nothing is@@ -772,7 +860,8 @@ export class DeviceLogSyncer {const entries: unknown[] = []let acct = ''let head: DevLogHead = emptyDevLogState('').head- for (let page = 0; page < PAGES; page++) {+ // A review reads the whole list, so a log past PAGES' worth can still be reviewed.+ for (let page = 0; page < JOIN_PAGES; page++) {const got = await this.deps.fetch(entries.length)if (!got) return nullif (!Number.isSafeInteger(got.head.seq) || got.head.seq < 0) return null@@ -1061,6 +1150,7 @@ export class DeviceLogSyncer {return {head: file.state?.head ?? null, frozen: file.frozen, self: selfPub, members: withSeen, frozenPeers,pending, suspended, joinedSeq, baseline, baselineSeen: file.baselineSeen ?? true, conflict, departed: file.departed ?? [],+ ...(this.registerError ? { registerError: this.registerError } : {}),}}}diff --git a/desktop/lib/api/api_client.dart b/desktop/lib/api/api_client.dartindex bf69e89e9..3b510bde6 100644--- a/desktop/lib/api/api_client.dart+++ b/desktop/lib/api/api_client.dart@@ -293,9 +293,11 @@ class ApiClient {/// `GET /api/device-keys?since=` — the log from [since]; null when there is none to read (an older/// backend, signed out, or unreachable). See `viewer/device_log_sync.dart`.- Future<DeviceLogFetched?> deviceKeys(int since) async {+ ///+ /// [self]: this app's own key, which the backend counts as used just now (an older one ignores it).+ Future<DeviceLogFetched?> deviceKeys(int since, {String? self}) async {try {- final res = await _dio.get('/api/device-keys', queryParameters: {'since': since});+ final res = await _dio.get('/api/device-keys', queryParameters: {'since': since, 'self': ?self});if (res.statusCode != 200) return null;final data = unwrapApiResponse(res);if (data is! Map) return null;diff --git a/desktop/lib/settings/sections/account_devices_section.dart b/desktop/lib/settings/sections/account_devices_section.dartindex 68a0dc942..fd03742ae 100644--- a/desktop/lib/settings/sections/account_devices_section.dart+++ b/desktop/lib/settings/sections/account_devices_section.dart@@ -236,6 +236,7 @@ class _AccountDevicesSectionState extends State<AccountDevicesSection> {crossAxisAlignment: CrossAxisAlignment.stretch,children: [if (devices != null && devices.frozen) _FrozenLine(devices: devices, onTrustAgain: _trustAgain),+ if (devices?.registerError case final code?) _RefusedLine(code: code),if (devices != null && !devices.baselineSeen && !_baselineDone && !_app.baselineSeenLocally && devices.baseline.isNotEmpty)_BaselinePanel(devices: devices.baseline, onGotIt: () => unawaited(_gotIt())),if (_error case final error?)@@ -395,6 +396,33 @@ class _BaselinePanel extends StatelessWidget {);}+/// The backend refused this device a place on the account ([AccountDevices.registerError]).+class _RefusedLine extends StatelessWidget {+ const _RefusedLine({required this.code});++ final String code;++ @override+ Widget build(BuildContext context) => Padding(+ padding: const EdgeInsets.only(bottom: 12),+ child: DecoratedBox(+ decoration: BoxDecoration(+ color: grid.AppPalette.warn.withValues(alpha: 0.10),+ borderRadius: BorderRadius.circular(8),+ border: Border.all(color: grid.AppPalette.warn.withValues(alpha: 0.28)),+ ),+ child: Padding(+ padding: const EdgeInsets.all(12),+ child: Text(+ registerRefusalSentence(code),+ key: const Key('account-devices-refused'),+ style: grid.AppType.body(color: grid.AppPalette.textPrimary),+ ),+ ),+ ),+ );+}+class _FrozenLine extends StatelessWidget {const _FrozenLine({required this.devices, required this.onTrustAgain});diff --git a/desktop/lib/state/account_devices.dart b/desktop/lib/state/account_devices.dartindex 794e9b806..9245a46e6 100644--- a/desktop/lib/state/account_devices.dart+++ b/desktop/lib/state/account_devices.dart@@ -108,6 +108,11 @@ class DeviceConflict {}}+/// What the Devices list says when the backend refused this device a place on the account ([code]).+String registerRefusalSentence(String code) => code == 'TOO_MANY'+ ? 'This device couldn’t join: your account has too many devices. Remove ones you no longer use.'+ : 'This device couldn’t join your account’s device list ($code).';+/// The account's devices and whether the list can be trusted as it stands.class AccountDevices {const AccountDevices({@@ -121,6 +126,7 @@ class AccountDevices {this.conflict,this.historyAvailable = true,this.departed = const [],+ this.registerError,});final List<AccountDevice> devices;@@ -152,6 +158,10 @@ class AccountDevices {/// they are cleared (their own "Got it" is), so opening it must not mark them seen.final List<String> departed;+ /// Why the backend last refused this end a place in the log (`TOO_MANY`, …); null when it did not,+ /// or the daemon predates the field.+ final String? registerError;+/// The keys the log's own listing named as the baseline panel's (`baseline`), when it sent them; null/// from a daemon that predates it, which falls back to the `seq <= joinedSeq` rule.final List<String>? baselineKeys;@@ -233,6 +243,7 @@ class AccountDevices {conflict: conflict,historyAvailable: historyAvailable,departed: departed,+ registerError: registerError,);static AccountDevices? fromDaemon(Map<String, dynamic>? raw) {@@ -253,6 +264,7 @@ class AccountDevices {conflict: DeviceConflict.fromJson(raw['conflict']),historyAvailable: pending is List,departed: [for (final d in DeviceLogDeparted.listFromJson(raw['departed'])) d.pub],+ registerError: raw['registerError'] is String ? raw['registerError'] as String : null,).withLastSeen(parseLastSeen(raw['lastSeen']));}@@ -265,6 +277,7 @@ class AccountDevices {baselineSeen: listing.baselineSeen,baselineKeys: listing.baseline,departed: [for (final d in listing.departed) d.pub],+ registerError: listing.registerError,);}diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dartindex 3f82d4d36..ae3cd36cf 100644--- a/desktop/lib/state/app_state.dart+++ b/desktop/lib/state/app_state.dart@@ -116,6 +116,7 @@ import '../notify/system_notifications.dart' as notify_system;import 'account_devices.dart';import '../viewer/device_log.dart' show DevLogHead;import '../viewer/device_log_sync.dart';+import '../e2ee/bytes.dart' show b64e;import '../viewer/device_history.dart' show DeviceLogHistory;enum AppStatus {@@ -6865,6 +6866,17 @@ class AppNotifier extends ChangeNotifier {notifyListeners();}+ /// The backend refused this device a place on the account: it has too many devices (`TOO_MANY`).+ /// A machine asking for its password says so, beside [deviceListNeedsReview].+ bool get deviceListTooMany => _deviceListTooMany;+ bool _deviceListTooMany = false;++ @visibleForTesting+ set deviceListTooManyForTest(bool tooMany) {+ _deviceListTooMany = tooMany;+ notifyListeners();+ }+/// Everything this window said about the account's devices belongs to the account that is leaving/// (logout and a runtime sign-out both): the banners, the unsaved dismissals, and any read still in/// flight, which must not write the old account's listing under the next one.@@ -6878,6 +6890,7 @@ class AppNotifier extends ChangeNotifier {_dismissedDevices.clear();_baselineSeenLocally = false;_deviceListFrozen = false;+ _deviceListTooMany = false;_pendingBootReadDone = false;_pendingSyncGeneration++;}@@ -6900,7 +6913,11 @@ class AppNotifier extends ChangeNotifier {if (services == null) return;final log = _deviceLog = ViewerDeviceLog(keys: services.keys,- fetch: (since) => api.deviceKeys(since),+ // This app's own key rides each read, so the backend counts it as used (not as abandoned).+ fetch: (since) async => api.deviceKeys(+ since,+ self: await services.keys.identity().then<String?>((i) => b64e(i.pub), onError: (Object _) => null),+ ),append: (entry) => api.appendDeviceKey(entry),label: _deviceLabel,onAnnounce: (m) => _whenSignedIn(() {@@ -7017,9 +7034,11 @@ class AppNotifier extends ChangeNotifier {var daemon = false;var legacyDaemon = false;var frozen = false;+ String? registerError;if (_deviceLog case final log?) {final listing = await log.list();frozen = listing.frozen != null;+ registerError = listing.registerError;pending = listing.pending;departed = listing.departed;known = [for (final r in listing.members) NewDeviceNotice.fromMember(r.member, suspended: r.suspended)];@@ -7035,6 +7054,7 @@ class AppNotifier extends ChangeNotifier {pending = devices?.pending ?? const [];departed = DeviceLogDeparted.listFromJson(raw['departed']);conflict = devices?.conflict;+ registerError = devices?.registerError;known = [for (final d in devices?.devices ?? const <AccountDevice>[])NewDeviceNotice(@@ -7053,8 +7073,10 @@ class AppNotifier extends ChangeNotifier {if (_disposed || generation != _pendingSyncGeneration || status == AppStatus.unauthenticated) return;_pendingBootReadDone = true;if (legacyDaemon) return;- var changed = frozen != _deviceListFrozen;+ final tooMany = registerError == 'TOO_MANY';+ var changed = frozen != _deviceListFrozen || tooMany != _deviceListTooMany;_deviceListFrozen = frozen;+ _deviceListTooMany = tooMany;// Oldest first: the band names the first, and "(+n more)" the rest.final order = {for (var i = 0; i < pending.length; i++) pending[i]: i};final next = [diff --git a/desktop/lib/viewer/device_log_sync.dart b/desktop/lib/viewer/device_log_sync.dartindex f36eadd9f..c9452dcde 100644--- a/desktop/lib/viewer/device_log_sync.dart+++ b/desktop/lib/viewer/device_log_sync.dart@@ -167,6 +167,10 @@ const _resetWindowMs = 10 * 60 * 1000;const _appendAttempts = 5;const _pages = 20;+/// The page cap while a log is first read ([_File.joining]) or reviewed ([ViewerDeviceLog.rebaseline]):+/// 100,000 entries. A first read cut short at [_pages] would take every key past it for news.+const _joinPages = 200;+/// How many other accounts' logs are kept ([ViewerKeyStore.deviceLogArchive]); the oldest goes first.const _archivedKept = 4;@@ -206,6 +210,7 @@ class DeviceLogListing {this.baseline = const [],this.baselineSeen = true,this.departed = const [],+ this.registerError,});static const empty = DeviceLogListing(members: [], frozen: null, frozenPeers: []);@@ -234,6 +239,10 @@ class DeviceLogListing {/// New keys removed before anyone marked them as seen (oldest first): flagged until [ViewerDeviceLog.dismiss]/// names them (`pub`, or `pubs`) or clears every one.final List<DeviceLogDeparted> departed;++ /// Why the backend last refused to put this app's key into the log (`TOO_MANY`, …), until a+ /// register lands; null when it never did (a lost race or no answer is not a refusal).+ final String? registerError;}/// The newest hashes a `group_sync` carries, so a fork can be located.@@ -366,6 +375,9 @@ class ViewerDeviceLog {/// Set while [register] runs.bool _registering = false;+ /// [DeviceLogListing.registerError]: in memory only, never in the file.+ String? _registerError;+/// The [register] under way, which [ensureRegistered] lets finish before it judges.Future<void>? _registerRun;Future<DeviceLogRegistration>? _ensuring;@@ -447,6 +459,7 @@ class ViewerDeviceLog {continue;}final mine = state.active[pub];+ if (mine != null) _registerError = null;if (mine != null && (mine.label == name || mine.kind != 'viewer')) return;final entry = await signDevLogEntry(nextDevLogEntry(state, op: 'add', pub: pub, kind: 'viewer', machineId: '', label: name, signer: pub, at: _now()),@@ -455,10 +468,14 @@ class ViewerDeviceLog {final answer = await append(entry);if (answer == null) return;if (answer.error == null) {+ _registerError = null;await refresh();return;}- if (answer.error != 'STALE_HEAD') return;+ if (answer.error != 'STALE_HEAD') {+ _registerError = answer.error;+ return;+ }await refresh();await _sleep(Duration(milliseconds: 200 + _random.nextInt(800) * (attempt + 1)));}@@ -852,7 +869,8 @@ class ViewerDeviceLog {Future<void> _readPages() async {await _marks();var bootstrap = ((await _read()).state?.head.seq ?? 0) == 0;- for (var page = 0; page < _pages; page++) {+ // The file's own `joining` decides, each page: the first page of a fresh log is what sets it.+ for (var page = 0; page < ((await _read()).joining ? _joinPages : _pages); page++) {final seen = (await _read()).state;final since = seen?.head.seq ?? 0;final got = await fetch(since);@@ -1257,7 +1275,7 @@ class ViewerDeviceLog {final entries = <Object?>[];var acct = '';DevLogHead? head;- for (var page = 0; page < _pages; page++) {+ for (var page = 0; page < _joinPages; page++) {final got = await fetch(entries.length);if (got == null || !_isPosition(got.head.seq)) return null;acct = got.acct;@@ -1619,6 +1637,7 @@ class ViewerDeviceLog {],baselineSeen: file.baselineSeen,departed: file.departed,+ registerError: _registerError,);}}diff --git a/desktop/lib/widgets/machine_picker_form.dart b/desktop/lib/widgets/machine_picker_form.dartindex 1a6e7e438..36f589e7b 100644--- a/desktop/lib/widgets/machine_picker_form.dart+++ b/desktop/lib/widgets/machine_picker_form.dart@@ -20,6 +20,7 @@ import 'link_another_machine_dialog.dart'kLinkServerInstallCommand,kLinkServerLoginCommand,kLinkServerStartCommand;+import 'new_device_notice.dart' show DeviceListReviewLine;import 'terminal_text_action.dart';enum MachinePickerFormKind { connect, rename, password, delete, app, cli }@@ -769,6 +770,12 @@ class MachinePickerFormState extends State<MachinePickerForm> {'remote-password-confirm-field',),],+ // Asked for a password because this app's device list is frozen, or the account had no room+ // for it: the devices list is the other way in, here as under every other password prompt.+ if (widget.kind != MachinePickerFormKind.rename && !_password) ...[+ gap,+ DeviceListReviewLine(notifier: app),+ ],],gap,if (_busy || _message != null) ...[diff --git a/desktop/lib/widgets/new_device_notice.dart b/desktop/lib/widgets/new_device_notice.dartindex 78f8d99f7..832389457 100644--- a/desktop/lib/widgets/new_device_notice.dart+++ b/desktop/lib/widgets/new_device_notice.dart@@ -8,7 +8,7 @@ import '../settings/sections/account_device_history.dart';import '../settings/settings_screen.dart';import '../settings/settings_section.dart';import '../shared/theme/app_theme.dart' as grid;-import '../state/account_devices.dart' show DeviceConflict;+import '../state/account_devices.dart' show DeviceConflict, registerRefusalSentence;import '../state/app_state.dart';import '../viewer/device_log_sync.dart' show DeviceDepartedCopy, DeviceRemovalCopy, DeviceRemovalNotice;import 'window_chrome.dart';@@ -287,28 +287,38 @@ void showDeviceListReview(BuildContext context, AppNotifier notifier) => unawait),);-/// The line under a machine's password prompt while this app's device list is frozen: the password is-/// one way in, the review the other. Nothing while the list needs no review.+/// The line under a machine's password prompt while this app's device list is frozen — or the backend+/// refused this app a place on the account for having too many devices: the password is one way in,+/// the devices list the other. Nothing while neither holds.class DeviceListReviewLine extends StatelessWidget {const DeviceListReviewLine({super.key, required this.notifier, this.center = false});final AppNotifier notifier;final bool center;+ // Listens itself: it sits under prompts that do not rebuild when the device list changes.@override- Widget build(BuildContext context) {+ Widget build(BuildContext context) =>+ ListenableBuilder(listenable: notifier, builder: _build);++ Widget _build(BuildContext context, Widget? _) {grid.AppTheme.watch(context);- if (!notifier.deviceListNeedsReview) return const SizedBox.shrink();+ final review = notifier.deviceListNeedsReview, tooMany = notifier.deviceListTooMany;+ if (!review && !tooMany) return const SizedBox.shrink();return Wrap(alignment: center ? WrapAlignment.center : WrapAlignment.start,crossAxisAlignment: WrapCrossAlignment.center,spacing: 4,children: [- Text(- 'Your device list needs a review.',- textAlign: center ? TextAlign.center : TextAlign.start,- style: grid.AppType.body(color: grid.AppPalette.textSecondary),- ),+ for (final line in [+ if (review) 'Your device list needs a review.',+ if (tooMany) registerRefusalSentence('TOO_MANY'),+ ])+ Text(+ line,+ textAlign: center ? TextAlign.center : TextAlign.start,+ style: grid.AppType.body(color: grid.AppPalette.textSecondary),+ ),TextButton(key: const Key('device-list-review'),onPressed: () => showDeviceListReview(context, notifier),diff --git a/desktop/lib/widgets/pane_grid.dart b/desktop/lib/widgets/pane_grid.dartindex a42c1d869..bb554e605 100644--- a/desktop/lib/widgets/pane_grid.dart+++ b/desktop/lib/widgets/pane_grid.dart@@ -19,6 +19,7 @@ import '../shared/theme/appearance_prefs_store.dart';// `hide TerminalKey`: this file's own shortcut-label class, unused here, collides with xterm's// `TerminalKey` (needed for the local image-drop Ctrl+V nudge — see `_dropImage`).import '../shortcuts/app_shortcuts.dart' hide TerminalKey;+import '../state/account_devices.dart' show registerRefusalSentence;import '../state/app_state.dart';import '../state/pane_preset.dart';import '../state/pane_arrangement.dart';@@ -1579,14 +1580,21 @@ class _PaneContent extends StatelessWidget {: 'Waiting for this machine. Retained output is read only.',);} else if (needsLink) {- // A frozen device list may be why it asks: the band says so, beside- // the password.- final review = notifier.deviceListNeedsReview;+ // A frozen device list — or an account with no room for this device —+ // may be why it asks: the band says so, beside the password.+ final review =+ notifier.deviceListNeedsReview || notifier.deviceListTooMany;notice = terminalNotice(label: 'Link required',icon: AppIcons.unlink,detail: review- ? '${machine.machine.displayName} needs linking. Your device list needs a review.'+ ? [+ '${machine.machine.displayName} needs linking.',+ if (notifier.deviceListNeedsReview)+ 'Your device list needs a review.',+ if (notifier.deviceListTooMany)+ registerRefusalSentence('TOO_MANY'),+ ].join(' '): '${machine.machine.displayName} needs linking. Retained output is read only.',// A tile still showing its last screen gets the same way out as an// empty one — the band's button asks for the remote password.diff --git a/desktop/test/account_devices_section_test.dart b/desktop/test/account_devices_section_test.dartindex a46f79c95..5f64683e6 100644--- a/desktop/test/account_devices_section_test.dart+++ b/desktop/test/account_devices_section_test.dart@@ -192,6 +192,27 @@ void main() {await tester.pump();}+ testWidgets('a refusal to register is said: too many devices plainly, any other with its code', (tester) async {+ await open(tester, AccountDevices.fromDaemon({+ 'members': <Object>[],+ 'pending': <Object>[],+ 'registerError': 'TOO_MANY',+ })!);+ expect(+ find.text('This device couldn’t join: your account has too many devices. Remove ones you no longer use.'),+ findsOneWidget,+ );+ await tester.pumpWidget(const SizedBox());+ app.dispose();+ await open(tester, AccountDevices(devices: [fakeDevice('me', self: true)], registerError: 'FORBIDDEN'));+ expect(find.text('This device couldn’t join your account’s device list (FORBIDDEN).'), findsOneWidget);+ await tester.pumpWidget(const SizedBox());+ app.dispose();+ // A daemon that predates the field: nothing.+ await open(tester, AccountDevices.fromDaemon({'members': <Object>[], 'pending': <Object>[]})!);+ expect(find.byKey(const Key('account-devices-refused')), findsNothing);+ });+testWidgets('a device the daemon kept as pending is badged New though no banner is up', (tester) async {await open(tester, AccountDevices(devices: [fakeDevice('me', self: true), fakeDevice('old'), fakeDevice('late', label: 'Late iPad')],diff --git a/desktop/test/desktop_resource_forms_edge_test.dart b/desktop/test/desktop_resource_forms_edge_test.dartindex e99ddaa0b..261fde5f5 100644--- a/desktop/test/desktop_resource_forms_edge_test.dart+++ b/desktop/test/desktop_resource_forms_edge_test.dart@@ -354,4 +354,31 @@ void main() {await tester.pumpAndSettle();expect(tester.takeException(), isNull);});++ testWidgets(+ 'a machine asking for its password says why when the account has no room for this app',+ (tester) async {+ // The password is one way in; the devices list is the other — under the search panel's prompt+ // too, which is where a browser that could not join the device list is sent first.+ final app = _App();+ addTearDown(app.dispose);+ await _mount(+ tester,+ MachinePickerForm(+ app: app,+ kind: MachinePickerFormKind.connect,+ machineId: 'm',+ onClose: (_) {},+ onFocusChanged: (_) {},+ ),+ );+ final line = find.textContaining('your account has too many devices');+ expect(line, findsNothing);+ app.deviceListTooManyForTest = true;+ await tester.pump();+ expect(line, findsOneWidget);+ expect(find.byKey(const Key('device-list-review')), findsOneWidget);+ expect(tester.takeException(), isNull);+ },+ );}diff --git a/desktop/test/device_viewer_notices_test.dart b/desktop/test/device_viewer_notices_test.dartindex 48fef5346..c4078c5c1 100644--- a/desktop/test/device_viewer_notices_test.dart+++ b/desktop/test/device_viewer_notices_test.dart@@ -119,7 +119,11 @@ class _Backend {],);+ /// Every append is refused with this code while set.+ String? refuse;+Future<DeviceLogAppendAnswer> append(DevLogEntry entry) async {+ if (refuse case final code?) return (head: null, error: code);if (entry.seq != state.head.seq + 1 || entry.prev != state.head.hash) {return (head: state.head, error: 'STALE_HEAD');}@@ -139,9 +143,14 @@ class _Api extends ApiClient {/// When set, the last-seen request waits on it (the backend can delay it as long as it likes).Completer<void>? seenGate;+ /// The `self` every read of the log named.+ final selves = <String?>[];+@override- Future<DeviceLogFetched?> deviceKeys(int since) async =>- offline ? null : backend.fetch(since);+ Future<DeviceLogFetched?> deviceKeys(int since, {String? self}) async {+ selves.add(self);+ return offline ? null : backend.fetch(since);+ }@overrideFuture<DeviceLogAppendAnswer?> appendDeviceKey(DevLogEntry entry) =>@@ -298,6 +307,24 @@ void main() {return app;}+ test('every read of the log names this app\'s key; too many devices is said until a register lands', () async {+ await backend.add(box2, 'machine', 'b' * 32, 'box2');+ backend.refuse = 'TOO_MANY';+ final app = await openAs(backend, 'user-a');+ final me = b64e((await keys.identity()).pub);+ final api = app.api as _Api;+ expect(api.selves, isNotEmpty);+ expect(api.selves.toSet(), {me});+ expect(app.deviceListTooMany, isTrue);+ expect((await logOf(app).list()).registerError, 'TOO_MANY');++ backend.refuse = null;+ await logOf(app).register();+ await settle();+ expect(backend.state.active[me], isNotNull);+ expect(app.deviceListTooMany, isFalse);+ });+test('with no machine to hear pushes from, the machine list is read again on its own and on coming back to the tab', () async {final app = AppNotifier(config: AppConfig.dev,diff --git a/desktop/test/machines_panel_test.dart b/desktop/test/machines_panel_test.dartindex 36e8f85c3..3c3d33de3 100644--- a/desktop/test/machines_panel_test.dart+++ b/desktop/test/machines_panel_test.dart@@ -611,6 +611,26 @@ void main() {await tester.pumpWidget(const SizedBox());});+ testWidgets(+ 'too many devices on the account is said beside a machine\'s password',+ (tester) async {+ app+ ..add('remote', 'Mac mini')+ ..deviceListTooManyForTest = true;+ await mount(tester);+ const line =+ 'This device couldn’t join: your account has too many devices. Remove ones you no longer use.';+ expect(find.text(line), findsOneWidget);+ expect(find.text('Your device list needs a review.'), findsNothing);+ expect(find.byKey(const Key('device-list-review')), findsOneWidget);++ app.deviceListTooManyForTest = false;+ await tester.pumpAndSettle();+ expect(find.text(line), findsNothing);+ await tester.pumpWidget(const SizedBox());+ },+ );+testWidgets('connection continues across closing and reopening', (tester,) async {diff --git a/desktop/test/pane_needs_link_test.dart b/desktop/test/pane_needs_link_test.dartindex d845d52af..4d0607d98 100644--- a/desktop/test/pane_needs_link_test.dart+++ b/desktop/test/pane_needs_link_test.dart@@ -235,6 +235,24 @@ void main() {await finish(tester);});+ testWidgets('too many devices on the account is said on the tile too', (+ tester,+ ) async {+ app = appWith()..deviceListTooManyForTest = true;+ final session = terminal('a0', <TerminalBinaryFrame>[]);+ app.adoptSessionForTest(session);+ app.panes.single.machineId = 'remote-1';+ await pump(tester);++ expect(+ find.textContaining('your account has too many devices'),+ findsOneWidget,+ );+ expect(find.textContaining('needs a review'), findsNothing);+ expect(find.text('Your devices'), findsOneWidget);+ await finish(tester);+ });+testWidgets('a list that needs no review says nothing about it', (tester,) async {diff --git a/desktop/test/viewer/device_log_sync_test.dart b/desktop/test/viewer/device_log_sync_test.dartindex b23646cce..da511547f 100644--- a/desktop/test/viewer/device_log_sync_test.dart+++ b/desktop/test/viewer/device_log_sync_test.dart@@ -179,7 +179,11 @@ class _Backend {return (acct: acct, head: head, entries: [for (final e in log) if (e.seq > since) e.toJson()]);}+ /// Every append is refused with this code while set.+ String? refuse;+Future<DeviceLogAppendAnswer?> append(DevLogEntry entry) async {+ if (refuse case final code?) return (head: null, error: code);if (entry.seq != state.head.seq + 1 || entry.prev != state.head.hash) {return (head: state.head, error: 'STALE_HEAD');}@@ -222,6 +226,31 @@ void main() {box3 = await E2eeIdentity.fromSeed(List.filled(32, 3));});+ test('a refusal to register is kept for the listing until a register lands; no answer is no refusal', () async {+ var reachable = false;+ final log = ViewerDeviceLog(+ keys: keys,+ fetch: backend.fetch,+ append: (e) async => reachable ? backend.append(e) : null,+ label: () => 'my-phone',+ now: () => 5000,+ sleep: (_) async {},+ );+ await log.register();+ expect((await log.list()).registerError, isNull);+ reachable = true;+ backend.refuse = 'TOO_MANY';+ await log.register();+ expect((await log.list()).registerError, 'TOO_MANY');+ // Nothing of it is written down.+ expect((await keys.deviceLog())! as Map, isNot(contains('registerError')));+ backend.refuse = null;+ await log.register();+ final me = b64e((await keys.identity()).pub);+ expect(backend.state.active[me], isNotNull);+ expect((await log.list()).registerError, isNull);+ });+test('registers this app and pins every machine already in the log, announcing none', () async {await backend.add(box2, 'machine', _mid2, 'box2');final log = makeLog();@@ -846,6 +875,39 @@ void main() {expect((await log.list()).pending, [b64e(evil.pub)]);});+ /// The backend's answers one entry at a time: a log of n entries is n pages.+ Future<DeviceLogFetched?> onePerPage(int since) async {+ final got = await backend.fetch(since);+ return got == null ? null : (acct: got.acct, head: got.head, entries: got.entries.take(1).toList());+ }++ Future<void> addViewers(int n) async {+ for (var i = 0; i < n; i++) {+ await backend.add(await E2eeIdentity.fromSeed(List.filled(32, 100 + i)), 'viewer', '', 'app $i');+ }+ }++ test('a first read longer than the usual page cap is all joined: none of it is news', () async {+ await addViewers(25);+ final log = logWith(onePerPage);+ await log.refresh();+ await log.refresh();+ expect(announced, isEmpty);+ expect((await log.list()).joinedSeq, 25);+ // Past the join, the usual cap (and news) again.+ await backend.add(evil, 'viewer', '', 'evil');+ await log.refresh();+ expect(announced.map((m) => m.label), ['evil']);+ });++ test('a review reads a list longer than the usual page cap', () async {+ await addViewers(25);+ final preview = await logWith(onePerPage).rebaseline(confirm: false);+ expect(preview, isNotNull);+ expect(preview!.head, backend.state.head);+ expect(preview.added, hasLength(25));+ });+test('a head that is not a position is not read at all', () async {await backend.add(box2, 'machine', _mid2, 'box2');final log = logWith((since) async {diff --git a/mobile/lib/api/api_client.dart b/mobile/lib/api/api_client.dartindex ed0c7b669..219208b7b 100644--- a/mobile/lib/api/api_client.dart+++ b/mobile/lib/api/api_client.dart@@ -95,9 +95,11 @@ class ApiClient {/// `GET /api/device-keys?since=` — the log from [since]; null when there is none to read (an older/// backend, signed out, or unreachable).- Future<DeviceLogFetched?> deviceKeys(int since) async {+ ///+ /// [self]: this app's own key, which the backend counts as used just now (an older one ignores it).+ Future<DeviceLogFetched?> deviceKeys(int since, {String? self}) async {try {- final res = await _dio.get('/api/device-keys', queryParameters: {'since': since});+ final res = await _dio.get('/api/device-keys', queryParameters: {'since': since, 'self': ?self});if (res.statusCode != 200) return null;final data = unwrapApiResponse(res);if (data is! Map) return null;diff --git a/mobile/lib/phone/devices_page.dart b/mobile/lib/phone/devices_page.dartindex d90152750..22ee78b47 100644--- a/mobile/lib/phone/devices_page.dart+++ b/mobile/lib/phone/devices_page.dart@@ -355,6 +355,22 @@ class _DevicesPageState extends State<DevicesPage> {],),],+ if (listing?.registerError case final code?) ...[+ const SizedBox(height: 12),+ SettingsGroup(+ children: [+ SettingsRow(+ key: const ValueKey('devices-refused'),+ title: 'This device couldn’t join',+ detail: code == 'TOO_MANY'+ ? 'Your account has too many devices. Remove ones you no longer use.'+ : 'Harness refused it a place on your account’s device list ($code).',+ destructive: true,+ detailLines: null,+ ),+ ],+ ),+ ],if (listing != null &&!listing.baselineSeen &&!_baselineHidden) ...[diff --git a/mobile/lib/phone/welcome/unlock_computer.dart b/mobile/lib/phone/welcome/unlock_computer.dartindex 6e8b8fcb1..6e094feb0 100644--- a/mobile/lib/phone/welcome/unlock_computer.dart+++ b/mobile/lib/phone/welcome/unlock_computer.dart@@ -176,11 +176,14 @@ class _UnlockComputerState extends State<UnlockComputer> {'On $name, open Harness ▸ Add Phone…, then scan the QR code.',style: tty.style(size: TtySize.row, color: tty.faint),),- // This phone's copy of the device list is frozen, so it pins no computer from it: a review is- // the other way in, beside the code and the password.+ // This phone's copy of the device list is frozen, so it pins no computer from it — or the+ // account has too many devices to let this phone in: the list is the other way in, beside the+ // code and the password.ListenableBuilder(listenable: widget.notifier,- builder: (context, _) => !widget.notifier.deviceListNeedsReview+ builder: (context, _) =>+ !widget.notifier.deviceListNeedsReview &&+ !widget.notifier.deviceListTooMany? const SizedBox.shrink(): Padding(padding: const EdgeInsets.only(top: 12),@@ -188,7 +191,13 @@ class _UnlockComputerState extends State<UnlockComputer> {children: [Expanded(child: Text(- 'Your device list needs a review.',+ [+ if (widget.notifier.deviceListNeedsReview)+ 'Your device list needs a review.',+ if (widget.notifier.deviceListTooMany)+ 'This device couldn’t join: your account has too many devices. '+ 'Remove ones you no longer use.',+ ].join('\n'),style: tty.style(size: TtySize.meta,color: tty.yellow,diff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dartindex d3b19426f..4d3179c57 100644--- a/mobile/lib/state/app_state.dart+++ b/mobile/lib/state/app_state.dart@@ -11,6 +11,7 @@ import '../api/api_client.dart';import '../viewer/direct_auth_api.dart';import '../viewer/direct_link.dart';import '../viewer/group_sync.dart';+import '../e2ee/bytes.dart' show b64e;import '../viewer/viewer_services.dart';import '../auth/auth_session.dart';import '../auth/peer_link_client.dart';@@ -1565,6 +1566,7 @@ class AppNotifier extends ChangeNotifier {deviceRemovals.clear();_failedDismissals.clear();_deviceListFrozen = false;+ _deviceListTooMany = false;}/// What an account leaving takes with it: the refusals being settled, the machine list re-read@@ -2294,6 +2296,17 @@ class AppNotifier extends ChangeNotifier {notifyListeners();}+ /// The backend refused this phone a place on the account: it has too many devices (`TOO_MANY`).+ /// A machine asking for its password says so, beside [deviceListNeedsReview].+ bool get deviceListTooMany => _deviceListTooMany;+ bool _deviceListTooMany = false;++ @visibleForTesting+ set deviceListTooManyForTest(bool tooMany) {+ _deviceListTooMany = tooMany;+ notifyListeners();+ }+/// Devices this phone dismissed whose write to the log failed: the banner must not bring them back/// on the next read of `pending`. Gone with the sign-in, or when the key is announced afresh.final Set<String> _failedDismissals = {};@@ -2312,7 +2325,11 @@ class AppNotifier extends ChangeNotifier {void _initDeviceLog() {final log = _deviceLog = ViewerDeviceLog(keys: viewer.keys,- fetch: (since) => api.deviceKeys(since),+ // This phone's own key rides each read, so the backend counts it as used (not as abandoned).+ fetch: (since) async => api.deviceKeys(+ since,+ self: await viewer.keys.identity().then<String?>((i) => b64e(i.pub), onError: (Object _) => null),+ ),append: (entry) => api.appendDeviceKey(entry),label: () => phoneClientDescriptor().name,// The log saves "announced" before it calls back, so a call that found the app still starting@@ -2389,8 +2406,10 @@ class AppNotifier extends ChangeNotifier {}if (_disposed || gen != _pendingSyncGen || status != AppStatus.authenticated) return;final frozen = listing.frozen != null;- final frozenChanged = frozen != _deviceListFrozen;+ final tooMany = listing.registerError == 'TOO_MANY';+ final frozenChanged = frozen != _deviceListFrozen || tooMany != _deviceListTooMany;_deviceListFrozen = frozen;+ _deviceListTooMany = tooMany;final pending = listing.pending.toSet();_suspendedNew..clear()diff --git a/mobile/lib/viewer/device_log_sync.dart b/mobile/lib/viewer/device_log_sync.dartindex f36eadd9f..c9452dcde 100644--- a/mobile/lib/viewer/device_log_sync.dart+++ b/mobile/lib/viewer/device_log_sync.dart@@ -167,6 +167,10 @@ const _resetWindowMs = 10 * 60 * 1000;const _appendAttempts = 5;const _pages = 20;+/// The page cap while a log is first read ([_File.joining]) or reviewed ([ViewerDeviceLog.rebaseline]):+/// 100,000 entries. A first read cut short at [_pages] would take every key past it for news.+const _joinPages = 200;+/// How many other accounts' logs are kept ([ViewerKeyStore.deviceLogArchive]); the oldest goes first.const _archivedKept = 4;@@ -206,6 +210,7 @@ class DeviceLogListing {this.baseline = const [],this.baselineSeen = true,this.departed = const [],+ this.registerError,});static const empty = DeviceLogListing(members: [], frozen: null, frozenPeers: []);@@ -234,6 +239,10 @@ class DeviceLogListing {/// New keys removed before anyone marked them as seen (oldest first): flagged until [ViewerDeviceLog.dismiss]/// names them (`pub`, or `pubs`) or clears every one.final List<DeviceLogDeparted> departed;++ /// Why the backend last refused to put this app's key into the log (`TOO_MANY`, …), until a+ /// register lands; null when it never did (a lost race or no answer is not a refusal).+ final String? registerError;}/// The newest hashes a `group_sync` carries, so a fork can be located.@@ -366,6 +375,9 @@ class ViewerDeviceLog {/// Set while [register] runs.bool _registering = false;+ /// [DeviceLogListing.registerError]: in memory only, never in the file.+ String? _registerError;+/// The [register] under way, which [ensureRegistered] lets finish before it judges.Future<void>? _registerRun;Future<DeviceLogRegistration>? _ensuring;@@ -447,6 +459,7 @@ class ViewerDeviceLog {continue;}final mine = state.active[pub];+ if (mine != null) _registerError = null;if (mine != null && (mine.label == name || mine.kind != 'viewer')) return;final entry = await signDevLogEntry(nextDevLogEntry(state, op: 'add', pub: pub, kind: 'viewer', machineId: '', label: name, signer: pub, at: _now()),@@ -455,10 +468,14 @@ class ViewerDeviceLog {final answer = await append(entry);if (answer == null) return;if (answer.error == null) {+ _registerError = null;await refresh();return;}- if (answer.error != 'STALE_HEAD') return;+ if (answer.error != 'STALE_HEAD') {+ _registerError = answer.error;+ return;+ }await refresh();await _sleep(Duration(milliseconds: 200 + _random.nextInt(800) * (attempt + 1)));}@@ -852,7 +869,8 @@ class ViewerDeviceLog {Future<void> _readPages() async {await _marks();var bootstrap = ((await _read()).state?.head.seq ?? 0) == 0;- for (var page = 0; page < _pages; page++) {+ // The file's own `joining` decides, each page: the first page of a fresh log is what sets it.+ for (var page = 0; page < ((await _read()).joining ? _joinPages : _pages); page++) {final seen = (await _read()).state;final since = seen?.head.seq ?? 0;final got = await fetch(since);@@ -1257,7 +1275,7 @@ class ViewerDeviceLog {final entries = <Object?>[];var acct = '';DevLogHead? head;- for (var page = 0; page < _pages; page++) {+ for (var page = 0; page < _joinPages; page++) {final got = await fetch(entries.length);if (got == null || !_isPosition(got.head.seq)) return null;acct = got.acct;@@ -1619,6 +1637,7 @@ class ViewerDeviceLog {],baselineSeen: file.baselineSeen,departed: file.departed,+ registerError: _registerError,);}}diff --git a/mobile/test/phone/devices_fixture.dart b/mobile/test/phone/devices_fixture.dartindex e13ac1c52..8eeb6d0d1 100644--- a/mobile/test/phone/devices_fixture.dart+++ b/mobile/test/phone/devices_fixture.dart@@ -145,8 +145,12 @@ class DevicesApp extends AppNotifier {baseline: baseline,baselineSeen: baselineSeen,departed: departed,+ registerError: registerError,);+ /// Why the backend last refused this phone a place in the log.+ String? registerError;+/// When set, [deviceHistory] waits for it before answering (a read still in flight).Completer<void>? historyGate;diff --git a/mobile/test/phone/devices_page_test.dart b/mobile/test/phone/devices_page_test.dartindex 8a44e758d..a52cc4ac6 100644--- a/mobile/test/phone/devices_page_test.dart+++ b/mobile/test/phone/devices_page_test.dart@@ -301,6 +301,28 @@ void main() {expect(panel, findsNothing);});+ testWidgets(+ 'a refusal to register is a row: too many devices plainly, any other with its code',+ (tester) async {+ await pump(tester);+ expect(find.byKey(const ValueKey('devices-refused')), findsNothing);+ app.registerError = 'TOO_MANY';+ await tester.pumpWidget(const SizedBox());+ await pump(tester);+ expect(find.text('This device couldn’t join'), findsOneWidget);+ expect(+ find.text(+ 'Your account has too many devices. Remove ones you no longer use.',+ ),+ findsOneWidget,+ );+ app.registerError = 'FORBIDDEN';+ await tester.pumpWidget(const SizedBox());+ await pump(tester);+ expect(find.textContaining('(FORBIDDEN)'), findsOneWidget);+ },+ );+testWidgets('no baseline panel once it was seen', (tester) async {await pump(tester);expect(find.byKey(const Key('account-devices-baseline')), findsNothing);diff --git a/mobile/test/state/device_pending_sync_test.dart b/mobile/test/state/device_pending_sync_test.dartindex 2c45d9462..816f2553c 100644--- a/mobile/test/state/device_pending_sync_test.dart+++ b/mobile/test/state/device_pending_sync_test.dart@@ -104,9 +104,10 @@ class _Backend {/// Every append is refused while set: this phone never gets into the log.bool refuseAppends = false;+ String refuseWith = 'FORBIDDEN';Future<DeviceLogAppendAnswer?> append(DevLogEntry entry) async {- if (refuseAppends) return (head: null, error: 'FORBIDDEN');+ if (refuseAppends) return (head: null, error: refuseWith);if (entry.seq != state.head.seq + 1 || entry.prev != state.head.hash) {return (head: state.head, error: 'STALE_HEAD');}@@ -120,8 +121,14 @@ class _Api extends FakeApi {final _Backend backend;+ /// The `self` every read of the log named.+ final selves = <String?>[];+@override- Future<DeviceLogFetched?> deviceKeys(int since) => backend.fetch(since);+ Future<DeviceLogFetched?> deviceKeys(int since, {String? self}) {+ selves.add(self);+ return backend.fetch(since);+ }@overrideFuture<DeviceLogAppendAnswer?> appendDeviceKey(DevLogEntry entry) =>@@ -307,6 +314,25 @@ void main() {expect(secondNotices.keys, isEmpty);});+ test('every read of the log names this phone\'s key; too many devices is said until a register lands', () async {+ await backend.add(box2, 'machine', _mid2, 'box2');+ backend+ ..refuseAppends = true+ ..refuseWith = 'TOO_MANY';+ final (app, _) = await launch();+ final me = b64e((await app.viewer.keys.identity()).pub);+ final api = app.api as _Api;+ expect(api.selves, isNotEmpty);+ expect(api.selves.toSet(), {me});+ expect(app.deviceListTooMany, isTrue);++ backend.refuseAppends = false;+ await app.deviceLog!.register();+ await settle();+ expect(backend.state.active[me], isNotNull);+ expect(app.deviceListTooMany, isFalse);+ });+test('"It’s mine" on one device holds across a restart', () async {final (first, _) = await launch();await backend.add(box2, 'machine', _mid2, 'box2');diff --git a/mobile/test/unlock_by_scan_test.dart b/mobile/test/unlock_by_scan_test.dartindex 4e9abfee1..29ec07ab5 100644--- a/mobile/test/unlock_by_scan_test.dart+++ b/mobile/test/unlock_by_scan_test.dart@@ -171,4 +171,23 @@ void main() {);},);++ testWidgets(+ 'too many devices on the account says so beside the password, with the way to the list',+ (tester) async {+ final (app, _) = await pump(tester, _Links(const CliLinkConnectResult()));+ expect(find.textContaining('too many devices'), findsNothing);++ app.deviceListTooManyForTest = true;+ await tester.pump();+ expect(+ find.text(+ 'This device couldn’t join: your account has too many devices. Remove ones you no longer use.',+ ),+ findsOneWidget,+ );+ expect(find.textContaining('needs a review'), findsNothing);+ expect(find.byKey(const ValueKey('device-list-review')), findsOneWidget);+ },+ );}diff --git a/mobile/test/viewer/device_log_sync_test.dart b/mobile/test/viewer/device_log_sync_test.dartindex dbfaa7f29..bcb6062fd 100644--- a/mobile/test/viewer/device_log_sync_test.dart+++ b/mobile/test/viewer/device_log_sync_test.dart@@ -162,7 +162,11 @@ class _Backend {return (acct: acct, head: head, entries: [for (final e in log) if (e.seq > since) e.toJson()]);}+ /// Every append is refused with this code while set.+ String? refuse;+Future<DeviceLogAppendAnswer?> append(DevLogEntry entry) async {+ if (refuse case final code?) return (head: null, error: code);if (entry.seq != state.head.seq + 1 || entry.prev != state.head.hash) {return (head: state.head, error: 'STALE_HEAD');}@@ -205,6 +209,31 @@ void main() {box3 = await E2eeIdentity.fromSeed(List.filled(32, 3));});+ test('a refusal to register is kept for the listing until a register lands; no answer is no refusal', () async {+ var reachable = false;+ final log = ViewerDeviceLog(+ keys: keys,+ fetch: backend.fetch,+ append: (e) async => reachable ? backend.append(e) : null,+ label: () => 'my-phone',+ now: () => 5000,+ sleep: (_) async {},+ );+ await log.register();+ expect((await log.list()).registerError, isNull);+ reachable = true;+ backend.refuse = 'TOO_MANY';+ await log.register();+ expect((await log.list()).registerError, 'TOO_MANY');+ // Nothing of it is written down.+ expect((await keys.deviceLog())! as Map, isNot(contains('registerError')));+ backend.refuse = null;+ await log.register();+ final me = b64e((await keys.identity()).pub);+ expect(backend.state.active[me], isNotNull);+ expect((await log.list()).registerError, isNull);+ });+test('registers this app and pins every machine already in the log, announcing none', () async {await backend.add(box2, 'machine', _mid2, 'box2');final log = makeLog();@@ -840,6 +869,39 @@ void main() {expect((await log.list()).pending, [b64e(evil.pub)]);});+ /// The backend's answers one entry at a time: a log of n entries is n pages.+ Future<DeviceLogFetched?> onePerPage(int since) async {+ final got = await backend.fetch(since);+ return got == null ? null : (acct: got.acct, head: got.head, entries: got.entries.take(1).toList());+ }++ Future<void> addViewers(int n) async {+ for (var i = 0; i < n; i++) {+ await backend.add(await E2eeIdentity.fromSeed(List.filled(32, 100 + i)), 'viewer', '', 'app $i');+ }+ }++ test('a first read longer than the usual page cap is all joined: none of it is news', () async {+ await addViewers(25);+ final log = logWith(onePerPage);+ await log.refresh();+ await log.refresh();+ expect(announced, isEmpty);+ expect((await log.list()).joinedSeq, 25);+ // Past the join, the usual cap (and news) again.+ await backend.add(evil, 'viewer', '', 'evil');+ await log.refresh();+ expect(announced.map((m) => m.label), ['evil']);+ });++ test('a review reads a list longer than the usual page cap', () async {+ await addViewers(25);+ final preview = await logWith(onePerPage).rebaseline(confirm: false);+ expect(preview, isNotNull);+ expect(preview!.head, backend.state.head);+ expect(preview.added, hasLength(25));+ });+test('a head that is not a position is not read at all', () async {await backend.add(box2, 'machine', _mid2, 'box2');final log = logWith((since) async {