Skip to content

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.

fix(devlog): a device key log page never claims a head past the entries it carries (#1789)

Bubu-Dudu-52hz7c0f68b45015
diff --git a/backend/src/lib/deviceKeyLog.test.ts b/backend/src/lib/deviceKeyLog.test.ts
index 83cc95783..77a15c00a 100644
--- a/backend/src/lib/deviceKeyLog.test.ts
+++ b/backend/src/lib/deviceKeyLog.test.ts
@@ -18,8 +18,9 @@ const m = vi.hoisted(() => ({
vi.mock('./prisma.js', () => ({
prisma: {
deviceKeyLogEntry: {
- findMany: vi.fn(async ({ where }: { where: { userId: string; seq?: { gt: number } } }) =>
- m.rows.filter(r => r.userId === where.userId && r.seq > (where.seq?.gt ?? 0)).sort((a, b) => a.seq - b.seq)),
+ findMany: vi.fn(async ({ where }: { where: { userId: string; seq?: { gt: number; lte?: number } } }) =>
+ m.rows.filter(r => r.userId === where.userId && r.seq > (where.seq?.gt ?? 0) && r.seq <= (where.seq?.lte ?? Infinity))
+ .sort((a, b) => a.seq - b.seq)),
findFirst: vi.fn(async ({ where }: { where: { userId: string } }) =>
m.rows.filter(r => r.userId === where.userId).sort((a, b) => b.seq - a.seq)[0] ?? null),
create: vi.fn(async ({ data }: { data: Row }) => {
@@ -314,6 +315,22 @@ describe('appendDeviceKey', () => {
expect(page.entries).toHaveLength(1)
expect((await readDeviceKeyLog('nobody', 0)).head.seq).toBe(0)
})
+
+ it('never answers a head past the entries it carries, when an append lands between its reads', async () => {
+ await appendDeviceKey(USER, await addEntry(box1, 'machine', MID1, 'box1'), { kind: 'machine', machineId: MID1 })
+ const late = await addEntry(phone, 'viewer', '', 'phone')
+ const row = { userId: USER, seq: 2, hash: applyDevLogEntries(await state(), [late]).state.head.hash, entry: JSON.stringify(late), sessionId: null }
+ // Whichever of the two reads comes first, the append lands right after it.
+ let landed = false
+ const land = () => { if (!landed) { landed = true; m.rows.push(row) } }
+ const findMany = vi.mocked(prisma.deviceKeyLogEntry.findMany), findFirst = vi.mocked(prisma.deviceKeyLogEntry.findFirst)
+ const many = findMany.getMockImplementation()!, first = findFirst.getMockImplementation()!
+ findMany.mockImplementationOnce((args) => { const out = many(args); land(); return out })
+ findFirst.mockImplementationOnce((args) => { const out = first(args); land(); return out })
+ const page = await readDeviceKeyLog(USER, 0)
+ expect(landed).toBe(true)
+ expect(page.entries).toHaveLength(page.head.seq)
+ })
})
describe('when a key was last seen', () => {
diff --git a/backend/src/lib/deviceKeyLog.ts b/backend/src/lib/deviceKeyLog.ts
index c41b27750..7d067d626 100644
--- a/backend/src/lib/deviceKeyLog.ts
+++ b/backend/src/lib/deviceKeyLog.ts
@@ -37,11 +37,17 @@ export type DeviceKeyChannel =
| { kind: 'machine'; machineId: string; harnessSessionId?: string; team?: boolean }
| { kind: 'viewer'; harnessSessionId?: string }
+/**
+ * The entries after `since`, up to the head they are read with. The head comes first and the entries stop
+ * at it: read side by side, an append landing between the two reads gave a head past the entries the page
+ * carried, and a device reads that as a list held back and freezes it (a Harness OS box registering its
+ * key as it signed in from a phone, 2026-10-22).
+ */
export async function readDeviceKeyLog(userId: string, since: number): Promise<{ acct: string; head: DevLogHead; entries: unknown[] }> {
- const [rows, head] = await Promise.all([
- prisma.deviceKeyLogEntry.findMany({ where: { userId, seq: { gt: since } }, orderBy: { seq: 'asc' }, take: PAGE }),
- logHead(userId),
- ])
+ const head = await logHead(userId)
+ const rows = head.seq > since
+ ? await prisma.deviceKeyLogEntry.findMany({ where: { userId, seq: { gt: since, lte: head.seq } }, orderBy: { seq: 'asc' }, take: PAGE })
+ : []
return { acct: userId, head, entries: rows.map(r => JSON.parse(r.entry) as unknown) }
}