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
Merge batch 11-05 i1725: #3157 #3150 #3130 #3146 (#3164)
0xken1505305d755342
diff --git a/backend/integration/superTabComputerMoves.test.ts b/backend/integration/superTabComputerMoves.test.tsindex 6985aadef..9f8c74cba 100644--- a/backend/integration/superTabComputerMoves.test.ts+++ b/backend/integration/superTabComputerMoves.test.ts@@ -1,4 +1,6 @@+import { execFile } from 'node:child_process'import { randomUUID } from 'node:crypto'+import { join } from 'node:path'import { afterAll, beforeAll, describe, expect, it } from 'vitest'import type { MongoMemoryReplSet } from 'mongodb-memory-server-core'import type { PrismaClient } from '@prisma/client'@@ -17,6 +19,7 @@ const user = (sub: string, email: string): AuthUser => ({ sub, email, autonomousconst owner = user('500000000000000000000009', '[email protected]'), kelvinUser = user(kelvin, '[email protected]'), ttUser = user(tt, '[email protected]')let replica: MongoMemoryReplSetlet db: PrismaClient+let uri: stringlet store: SuperTabStore/** Each machine's key in its account's device key log (lib/deviceKeyLog.ts): one key per computer, whatever account it signs* in to, so a computer's machines of two accounts hold the same one (e2ee/store.ts identity.json). */@@ -26,7 +29,7 @@ const keys: Array<{ userId: string; machineId: string; pub: string }> = []beforeAll(async () => {- ({ replica, db } = await planDatabase('computer_moves'))+ ({ replica, db, uri } = await planDatabase('computer_moves'))const machine = (machineId: string, userId: string, computerId: string, extra: Record<string, unknown> = {}) => db.machine.create({ data: {machineId, userId, computerId, sessionKind: 'computer', apiKey: `fixture-${machineId}`, managerId: '', autonomousEnv: 'prod', authMode: 'remote', ...extra } })await machine('kelvin-mac', kelvin, COMPUTER)@@ -172,6 +175,91 @@ describe('the one-time cleanup of computers listed under two accounts', () => {})})+// Task #3227 (Ken, 11-05): on prod the write moved nothing where the dry run, run just after, listed a move. The dry run read+// the entry alone; the write runs machine.moved as its owner, through every check a command meets (the Swarm loaded for them,+// their access to it, the move read from both account rows). One computer, one Swarm, the same data through both.+describe('the cleanup\'s dry run and its write', () => {+ const run = (tab: string, command: Command, as: AuthUser = owner) => store.execute(as, tab, { operationId: randomUUID(), command })+ const machinesIn = async (tab: string) => (await store.read(owner, tab)).state.machines!.map(m => `${m.machineId}:${m.contributorId}`).sort()+ let computer = 0+ /** A computer listed in one new Swarm under kelvin (seen first) and ttkhoahcmus (seen last), its key in both logs. */+ async function listedTwice() {+ const id = (0xf0 + computer++).toString(16).repeat(16), from = `kelvin-${id.slice(0, 4)}`, to = `tt-${id.slice(0, 4)}`+ const at = (minutes: number) => new Date(Date.now() - minutes * 60_000)+ for (const [machineId, userId, seen] of [[from, kelvin, at(50)], [to, tt, at(1)]] as const) {+ await db.machine.create({ data: { machineId, userId, computerId: id, sessionKind: 'computer', apiKey: `fixture-${machineId}`, managerId: '',+ autonomousEnv: 'prod', authMode: 'remote', lastSeenAt: seen } })+ }+ keys.push({ userId: kelvin, machineId: from, pub: `pub-${id}` }, { userId: tt, machineId: to, pub: `pub-${id}` })+ const tab = (await store.create(owner, randomUUID(), 'Twice')).id+ for (const person of [kelvinUser, ttUser]) {+ const invitation = await run(tab, { type: 'invite.create', email: person.email, role: 'collaborator', conversationId: 'lead' })+ await run(tab, { type: 'invite.accept', inviteId: invitation.result.inviteId as string }, person)+ }+ for (const [machineId, as] of [[from, kelvinUser], [to, ttUser]] as const) {+ await run(tab, { type: 'machine.contribute', machineId, name: 'MacBooks-MacBook-Pro-7', cwd: '/work', scope: 'swarm', engines: ['claude'], maxAgents: 2,+ teamAccountOnly: true }, as)+ }+ return { tab, from, to }+ }+ const mine = (moves: Array<{ tabId: string }>, tab: string) => moves.filter(m => m.tabId === tab)++ it('decide the same moves on the same data, and say why one is left', async () => {+ const { tab, from, to } = await listedTwice()+ // The entry's owner was taken out of the Swarm's Workspace and is still listed in the Swarm (a Workspace removal whose leave+ // did not reach this Swarm): no command of theirs reaches it, machine.moved included.+ const { workspaceId } = (await store.read(owner, tab)).state+ const membership = { workspaceId: workspaceId!, userId: kelvin }+ await db.swarmWorkspaceMember.updateMany({ where: membership, data: { removedAt: new Date() } })+ try {+ const dry = await cleanUpComputers(db, store, 'prod', { write: false })+ const written = await cleanUpComputers(db, store, 'prod', { write: true })+ expect(mine(written.moves, tab)).toEqual([])+ expect(mine(dry.moves, tab)).toEqual(mine(written.moves, tab))+ // Both name the entry left and the Swarm's own refusal, so a dry run says what the write will not do.+ const left = { tabId: tab, from, fromOwner: kelvin, to, toOwner: tt, code: 'WORKSPACE_ACCESS_REVOKED' }+ expect(mine(dry.left, tab)).toEqual([left])+ expect(mine(written.left, tab)).toEqual([left])+ } finally { await db.swarmWorkspaceMember.updateMany({ where: membership, data: { removedAt: null } }) }+ expect(await machinesIn(tab)).toEqual([`${from}:${kelvin}`, `${to}:${tt}`])++ // The Swarm reachable again: both decide the move, and the write makes it.+ const dry = await cleanUpComputers(db, store, 'prod', { write: false })+ const moved = { tabId: tab, from, fromOwner: kelvin, to, toOwner: tt }+ expect(mine(dry.moves, tab)).toEqual([moved])+ expect(mine((await cleanUpComputers(db, store, 'prod', { write: true })).moves, tab)).toEqual([moved])+ expect(await machinesIn(tab)).toEqual([`${to}:${tt}`])+ })+})++// Task #3227: on prod the script printed its JSON and never exited, and its env check refused to start without billing's+// credential key. Run as Ken runs it, with only the database's URL, and Redis nowhere to be reached.+describe('the cleanup script', () => {+ const script = (env: NodeJS.ProcessEnv) => new Promise<{ code: number; stdout: string; stderr: string }>(resolve => {+ execFile(join(process.cwd(), 'node_modules/.bin/tsx'), ['scripts/swarm-computer-moves/run.ts'],+ { env: { PATH: process.env.PATH, HOME: process.env.HOME, DATABASE_URL: uri, REDIS_URL: 'redis://127.0.0.1:1', ...env }, timeout: 60_000 },+ (error, stdout, stderr) => resolve({ code: error ? (typeof error.code === 'number' ? error.code : -1) : 0, stdout, stderr }))+ })++ it('runs without billing\'s environment and exits once it has printed', async () => {+ const started = Date.now()+ const { code, stdout } = await script({ SWARM_WORKSPACES_REQUIRED: '0' })+ expect(code).toBe(0)+ const printed = JSON.parse(stdout.slice(stdout.indexOf('{'))) as { write: boolean; env: string; moves: unknown[]; left: unknown[] }+ expect(printed).toMatchObject({ write: false, env: 'prod', moves: expect.any(Array), left: expect.any(Array) })+ expect(Date.now() - started).toBeLessThan(60_000)+ }, 90_000)++ // Security review 1 of #3146: the Workspace check reads anything but '1' as not required, so a run that forgot the server's+ // setting would pass a Swarm the server refuses, in its dry run and its write alike. It must be said, either way.+ it('will not start unless told whether this server requires Workspaces', async () => {+ const { code, stdout, stderr } = await script({})+ expect(code).toBe(1)+ expect(stdout).toBe('')+ expect(stderr).toContain('SWARM_WORKSPACES_REQUIRED')+ }, 90_000)+})+describe("a Mac whose owner's Team account is about to join", () => {const run = (tab: string, command: Command, as: AuthUser = owner) => store.execute(as, tab, { operationId: randomUUID(), command })it('reads joining, not "not set up", until its Team account is listed, from an indexed read of the owners\' Team machines', async () => {diff --git a/backend/integration/superTabThumbnails.test.ts b/backend/integration/superTabThumbnails.test.tsnew file mode 100644index 000000000..ff7aa036d--- /dev/null+++ b/backend/integration/superTabThumbnails.test.ts@@ -0,0 +1,266 @@+import { randomBytes, randomUUID } from 'node:crypto'+import { execFile } from 'node:child_process'+import { promisify } from 'node:util'+import { join } from 'node:path'+import { tmpdir } from 'node:os'+import { Readable } from 'node:stream'+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'+import { MongoMemoryReplSet } from 'mongodb-memory-server-core'+import { PrismaClient } from '@prisma/client'+import sharp from 'sharp'+import type { AuthUser } from '../src/lib/ssoAuth.js'+import { SuperTabStore } from '../src/superTabs/store.js'+import { MemoryAttachmentStore, MemoryAvatarStore, type Attachment, type Attachments } from '../src/superTabs/attachments.js'+import { thumbKeyOf, thumbnailsReserve as realReserve } from '../src/superTabs/thumbnails.js'++// The thumbnailer's queue is full while this says so (thumbnails.ts thumbnailsBusy).+const queue = vi.hoisted(() => ({ busy: false }))+vi.mock('../src/superTabs/thumbnails.js', async importOriginal => {+ const original = await importOriginal<typeof import('../src/superTabs/thumbnails.js')>()+ return { ...original, thumbnailsReserve: () => queue.busy ? null : original.thumbnailsReserve() }+})+import { sweepUnsentAttachments } from '../src/lib/attachmentSweep.js'+import { PURGE_MARGIN_MS, purgeDeletedSwarms, purgeFiles } from '../src/superTabs/purge.js'+import { SWARM_DELETE_GRACE_MS } from '../src/superTabs/deletion.js'++// Task #3177: the server makes a thumbnail when a picture is uploaded; a chat draws it, the viewer opens the original, and+// whoever may read the original may read the thumbnail and nobody else.+let replica: MongoMemoryReplSet+let db: PrismaClient+const owner: AuthUser = { sub: '240000000000000000000001', email: '[email protected]', autonomousEnv: 'prod', role: 'user' }+const peer: AuthUser = { sub: '240000000000000000000002', email: '[email protected]', autonomousEnv: 'prod', role: 'user' }+const config = { engine: 'codex', model: 'fixture-model', effort: 'high', contextWindow: 200000, engineVersion: 'fixture' }++beforeAll(async () => {+ replica = await MongoMemoryReplSet.create({ binary: { version: '7.0.24', downloadDir: join(tmpdir(), 'harness-super-tab-mongodb-binaries') },+ replSet: { count: 1, ip: '127.0.0.1', storageEngine: 'wiredTiger' } })+ const uri = replica.getUri(`swarm_thumbnails_test_${randomUUID().replaceAll('-', '_')}`)+ await promisify(execFile)(join(process.cwd(), 'node_modules/.bin/prisma'), ['db', 'push', '--skip-generate'], {+ env: { ...process.env, DATABASE_URL: uri }, timeout: 60000,+ })+ db = new PrismaClient({ datasources: { db: { url: uri } } })+ for (const user of [owner, peer]) {+ await db.user.create({ data: { id: user.sub, externalId: `fixture-${user.sub}`, email: user.email, autonomousEnv: user.autonomousEnv } })+ }+ await db.machine.create({ data: { machineId: 'mac', userId: owner.sub, apiKey: 'fixture-mac', managerId: 'fixture', autonomousEnv: 'prod', authMode: 'remote' } })+}, 120_000)+afterAll(async () => {+ await db?.$disconnect()+ await replica?.stop()+})++/** Noise, so a picture is large and its thumbnail clearly smaller. */+async function jpeg(width: number, height: number) {+ return sharp(randomBytes(width * height * 3), { raw: { width, height, channels: 3 } }).jpeg({ quality: 90 }).toBuffer()+}+const gif = () => sharp(randomBytes(900 * 900 * 3), { raw: { width: 900, height: 900, channels: 3 } }).gif().toBuffer()+const BASE = 'https://cdn.example.test/'+const keyOf = (url: string) => url.slice(BASE.length)++async function swarm(now: () => number = Date.now) {+ const files = new MemoryAttachmentStore()+ const attachments: Attachments = { store: files, prefix: 'super-tabs', publicBase: 'https://cdn.example.test', avatars: new MemoryAvatarStore('https://avatars.example.test'), avatarPrefix: 'super-tabs' }+ const store = new SuperTabStore(db, now, attachments)+ const tab = (await store.create(owner, randomUUID(), 'Pictures', 'public')).id+ const execute = (command: Parameters<SuperTabStore['execute']>[2]['command'], user = owner) => store.execute(user, tab, { operationId: randomUUID(), command })+ await execute({ type: 'machine.contribute', machineId: 'mac', name: 'Mac', cwd: '/repo', engines: ['codex'], maxAgents: 8 })+ await execute({ type: 'agent.add', conversationId: 'lead', machineId: 'mac', harnessId: randomUUID(), name: 'Lead', configuration: config, lead: true })+ const invitation = await execute({ type: 'invite.create', email: peer.email, role: 'collaborator', conversationId: 'lead' })+ await execute({ type: 'invite.accept', inviteId: invitation.result.inviteId as string }, peer)+ const upload = async (bytes: Buffer, name = 'photo.jpg', mime = 'image/jpeg') =>+ (await store.upload(owner, tab, 'lead', { name, mime, body: Readable.from([bytes]) })).attachment+ const send = async (attachment: Attachment) =>+ String((await execute({ type: 'message.send', conversationId: 'lead', text: '', replyTo: null, recipientIds: [], attachmentIds: [attachment.id] })).result.messageId)+ const read = async (messageId: string, user = owner) => (await store.messages(user, tab, 'lead', { messageId })).messages[0]!.attachments as unknown as Attachment[]+ return { files, attachments, store, tab, execute, upload, send, read }+}++describe('a picture uploaded through the form', () => {+ it('gets a thumbnail beside it, with both sizes in the answer', async () => {+ const f = await swarm()+ const original = await jpeg(2000, 1500)+ const file = await f.upload(original)+ expect(file).toMatchObject({ mime: 'image/jpeg', width: 2000, height: 1500, thumb: { width: 720, height: 540 } })+ expect(file.thumb!.size).toBeGreaterThan(0)+ expect(file.thumb!.size).toBeLessThan(original.byteLength * 0.8)+ // Beside the original: same folder and random segment.+ expect(keyOf(file.thumb!.url)).toBe(thumbKeyOf(keyOf(file.url)))+ expect(f.files.objects.get(keyOf(file.thumb!.url))).toMatchObject({ contentType: 'image/webp' })+ expect((await sharp(f.files.objects.get(keyOf(file.thumb!.url))!.bytes).metadata())).toMatchObject({ format: 'webp', width: 720, height: 540 })+ expect(f.files.objects.get(keyOf(file.url))!.bytes.equals(original)).toBe(true)+ // Each is linked, so a chat can draw either.+ expect(f.files.open(file.link!)).not.toBeNull()+ expect(f.files.open(file.thumb!.link!)!.byteLength).toBe(file.thumb!.size)+ expect(file.thumb!.expiresAt).toBe(file.expiresAt)+ // The row keeps what is needed to send it.+ expect((await db.superTabAttachment.findFirstOrThrow({ where: { id: file.id } })).image).toMatchObject({ width: 2000, height: 1500, thumb: { width: 720, height: 540 } })+ })++ it('travels with the message, which keeps no link of either, and is read with both links', async () => {+ const f = await swarm()+ const file = await f.upload(await jpeg(1800, 1200))+ const messageId = await f.send(file)+ const [read] = await f.read(messageId)+ expect(read).toMatchObject({ id: file.id, width: 1800, height: 1200, thumb: { url: file.thumb!.url, width: 720, height: 480, size: file.thumb!.size } })+ expect(f.files.open(read!.thumb!.link!)).not.toBeNull()+ const stored = (await db.superTabMessage.findFirstOrThrow({ where: { id: messageId } })).attachments as unknown as Array<Record<string, unknown>>+ expect(Object.keys(stored[0]!).sort()).toEqual(['height', 'id', 'mime', 'name', 'size', 'thumb', 'url', 'width'])+ expect(Object.keys(stored[0]!.thumb as object).sort()).toEqual(['height', 'size', 'url', 'width'])+ })++ it('gives a small picture its size and no thumbnail, and anything else neither', async () => {+ const f = await swarm()+ const small = await sharp(randomBytes(400 * 300 * 3), { raw: { width: 400, height: 300, channels: 3 } }).webp({ quality: 80 }).toBuffer()+ const smallFile = await f.upload(small, 'small.webp', 'image/webp')+ expect(smallFile).toMatchObject({ width: 400, height: 300 })+ expect(smallFile.thumb).toBeUndefined()+ const animated = await f.upload(await gif(), 'move.gif', 'image/gif')+ const text = await f.upload(Buffer.from('hello'), 'notes.txt', 'text/plain')+ for (const file of [animated, text]) {+ expect(file.thumb).toBeUndefined()+ expect(file.width).toBeUndefined()+ expect(file.height).toBeUndefined()+ }+ expect([...f.files.objects.keys()].filter(k => k.endsWith('.thumb.webp'))).toEqual([])+ })++ it('still uploads a picture that will not decode', async () => {+ const f = await swarm()+ const broken = (await jpeg(800, 600)).subarray(0, 400)+ const file = await f.upload(broken)+ expect(file).toMatchObject({ name: 'photo.jpg', size: 400 })+ expect(file.thumb).toBeUndefined()+ expect(f.files.objects.get(keyOf(file.url))!.bytes.equals(broken)).toBe(true)+ })++ it('keeps a file sent before thumbnails working: no facts, its original is linked as before', async () => {+ const f = await swarm()+ const file = await f.upload(Buffer.from('old'), 'old.txt', 'text/plain')+ const [read] = await f.read(await f.send(file))+ expect(read).toMatchObject({ id: file.id, name: 'old.txt' })+ expect(read!.link).toBeTruthy()+ expect(read!.thumb).toBeUndefined()+ })+})++describe('when thumbnails are backed up', () => {+ it('answers a form upload at once with the file and no thumbnail', async () => {+ const f = await swarm()+ queue.busy = true+ try {+ const original = await jpeg(2000, 1500)+ const file = await f.upload(original)+ expect(file).toMatchObject({ mime: 'image/jpeg', size: original.byteLength })+ expect(file.thumb).toBeUndefined()+ expect(f.files.objects.get(keyOf(file.url))!.bytes.equals(original)).toBe(true)+ expect([...f.files.objects.keys()].filter(k => k.endsWith('.thumb.webp'))).toEqual([])+ } finally { queue.busy = false }+ })++ it('completes a direct upload at once with the file and no thumbnail', async () => {+ const f = await swarm()+ const original = await jpeg(2400, 1600)+ const { upload } = await f.store.uploadLink(owner, f.tab, 'lead', { name: 'big.jpg', mime: 'image/jpeg', size: original.byteLength })+ expect(await f.files.receive(upload.url, upload.headers, original)).toBe(200)+ queue.busy = true+ try {+ const { attachment } = await f.store.completeUpload(owner, f.tab, upload.id)+ expect(attachment).toMatchObject({ name: 'big.jpg', size: original.byteLength })+ expect(attachment.thumb).toBeUndefined()+ } finally { queue.busy = false }+ })+})++describe('the places a picture holds while it waits for its thumbnail', () => {+ it('are all given back: many uploads in a row, finished or failed, are all thumbnailed', async () => {+ const f = await swarm()+ const picture = await jpeg(1200, 900)+ // A client that drops the connection after the picture's first bytes.+ const dropped = () => f.store.upload(owner, f.tab, 'lead', { name: 'cut.jpg', mime: 'image/jpeg',+ body: Readable.from((async function* () { yield picture.subarray(0, 2000); throw new Error('client dropped') })()) }).catch(() => null)+ for (let i = 0; i < 9; i++) await dropped()+ for (let i = 0; i < 9; i++) expect((await f.upload(picture)).thumb, `upload ${i}`).toBeDefined()+ })++ it('are limited: with every place taken a picture upload is not held and gets no thumbnail', async () => {+ const f = await swarm()+ const taken = Array.from({ length: 16 }, () => realReserve())+ try {+ const file = await f.upload(await jpeg(1200, 900))+ expect(file.thumb).toBeUndefined()+ } finally { taken.forEach(release => release?.()) }+ expect((await f.upload(await jpeg(1200, 900))).thumb).toBeDefined()+ })+})++describe('a picture uploaded straight to the bucket', () => {+ it('gets its thumbnail when the upload is completed', async () => {+ const f = await swarm()+ const original = await jpeg(2400, 1600)+ const { upload } = await f.store.uploadLink(owner, f.tab, 'lead', { name: 'big.jpg', mime: 'image/jpeg', size: original.byteLength })+ expect(await f.files.receive(upload.url, upload.headers, original)).toBe(200)+ const { attachment } = await f.store.completeUpload(owner, f.tab, upload.id)+ expect(attachment).toMatchObject({ width: 2400, height: 1600, thumb: { width: 720, height: 480 } })+ expect(f.files.objects.has(keyOf(attachment.thumb!.url))).toBe(true)+ // Answered again after a lost answer: the same facts.+ expect((await f.store.completeUpload(owner, f.tab, upload.id)).attachment).toMatchObject({ width: 2400, thumb: { width: 720 } })+ const [read] = await f.read(await f.send(attachment))+ expect(read).toMatchObject({ width: 2400, height: 1600, thumb: { width: 720, height: 480 } })+ })+})++describe('who can open a thumbnail', () => {+ it('is whoever can open the original: a member gets both links, a removed member neither', async () => {+ const f = await swarm()+ const file = await f.upload(await jpeg(1600, 1200))+ const messageId = await f.send(file)+ const [asPeer] = await f.read(messageId, peer)+ expect(f.files.open(asPeer!.link!)).not.toBeNull()+ expect(f.files.open(asPeer!.thumb!.link!)).not.toBeNull()+ const fresh = await f.store.fileLink(peer, f.tab, 'lead', messageId, file.id)+ expect(f.files.open(fresh.attachment.thumb!.link!)).not.toBeNull()++ await f.execute({ type: 'member.remove', userId: peer.sub })+ await expect(f.store.messages(peer, f.tab, 'lead', { messageId })).rejects.toBeTruthy()+ const refused = await f.store.fileLink(peer, f.tab, 'lead', messageId, file.id).catch(error => error as { code?: string })+ expect(refused).toMatchObject({ code: expect.any(String) })+ expect(JSON.stringify(refused)).not.toContain('thumb')+ // The links the member kept stop opening within their lifetime, the thumbnail's with the original's.+ expect(f.files.open(asPeer!.thumb!.link!, asPeer!.thumb!.expiresAt!)).toBeNull()+ })++ it('is not made for anyone once the Swarm is deleted', async () => {+ const f = await swarm()+ const file = await f.upload(await jpeg(1600, 1200))+ const messageId = await f.send(file)+ await f.execute({ type: 'swarm.delete', name: 'Pictures' })+ await expect(f.store.fileLink(owner, f.tab, 'lead', messageId, file.id)).rejects.toBeTruthy()+ })+})++describe('removing a stored picture removes its thumbnail', () => {+ it('goes with the original in the unsent-file sweep', async () => {+ const f = await swarm()+ const kept = await f.upload(await jpeg(1600, 1200))+ const dropped = await f.upload(await jpeg(1600, 1200))+ await f.send(kept)+ for (const file of [kept, dropped]) await db.superTabAttachment.update({ where: { id: file.id }, data: { createdAt: new Date(Date.now() - 25 * 3_600_000) } })+ await sweepUnsentAttachments(db, f.attachments, new Date(), { chunk: 10, chunks: 2, pauseMs: 0 })+ expect(f.files.objects.has(keyOf(dropped.url))).toBe(false)+ expect(f.files.objects.has(keyOf(dropped.thumb!.url))).toBe(false)+ expect(f.files.objects.has(keyOf(kept.url))).toBe(true)+ expect(f.files.objects.has(keyOf(kept.thumb!.url))).toBe(true)+ })++ it('goes with the original when a deleted Swarm is purged', async () => {+ let now = 1_800_000_000_000+ const f = await swarm(() => now)+ const file = await f.upload(await jpeg(1600, 1200))+ await f.send(file)+ await f.execute({ type: 'swarm.delete', name: 'Pictures' })+ expect(f.files.objects.has(keyOf(file.thumb!.url))).toBe(true)+ await purgeDeletedSwarms(db, purgeFiles(f.attachments), now + SWARM_DELETE_GRACE_MS + PURGE_MARGIN_MS)+ expect(f.files.objects.has(keyOf(file.url))).toBe(false)+ expect(f.files.objects.has(keyOf(file.thumb!.url))).toBe(false)+ })+})diff --git a/backend/package-lock.json b/backend/package-lock.jsonindex 417ee75c5..6cc681834 100644--- a/backend/package-lock.json+++ b/backend/package-lock.json@@ -21,6 +21,7 @@"http-proxy": "^1.18.1","ioredis": "^5.11.1","prom-client": "^15.1.3",+ "sharp": "^0.35.5","ws": "^8.21.0","zod": "^4.0.0"},@@ -98,6 +99,16 @@"node": ">=18"}},+ "node_modules/@emnapi/runtime": {+ "version": "1.11.3",+ "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.11.3.tgz",+ "integrity": "sha512-Xz4Tpyki7XyrpbUK1jR1AhdAdaXyhhY4lZ3neLodmhpuWfy2PAQN5B46sAiU4liOXGLkHypn/qU+jvfWSCYYLA==",+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "tslib": "^2.4.0"+ }+ },"node_modules/@esbuild/aix-ppc64": {"version": "0.27.7","resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.27.7.tgz",@@ -786,6 +797,506 @@"url": "https://github.com/sponsors/sindresorhus"}},+ "node_modules/@img/colour": {+ "version": "1.1.0",+ "resolved": "https://registry.npmjs.org/@img/colour/-/colour-1.1.0.tgz",+ "integrity": "sha512-Td76q7j57o/tLVdgS746cYARfSyxk8iEfRxewL9h4OMzYhbW4TAcppl0mT4eyqXddh6L/jwoM75mo7ixa/pCeQ==",+ "license": "MIT",+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@img/sharp-darwin-arm64": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-darwin-arm64/-/sharp-darwin-arm64-0.35.5.tgz",+ "integrity": "sha512-QRUlFQ0WxvdWyqqG/WtI3iupfD5rBzmCHXSdPsY91sAtVtTo7Q4cb6zOccZ3gqEqkr0f1As1ehLqmEpDsRf+lg==",+ "cpu": [+ "arm64"+ ],+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ },+ "optionalDependencies": {+ "@img/sharp-libvips-darwin-arm64": "1.3.4"+ }+ },+ "node_modules/@img/sharp-darwin-x64": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-darwin-x64/-/sharp-darwin-x64-0.35.5.tgz",+ "integrity": "sha512-+BR255RhDlpygUpOc/Jdt1nT6DQ3XG/ERo5wbcdOf5Q320dKtPCKPLR1LJs9VGXRaMa8l1uUa0tkCNOXiAxZUw==",+ "cpu": [+ "x64"+ ],+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ },+ "optionalDependencies": {+ "@img/sharp-libvips-darwin-x64": "1.3.4"+ }+ },+ "node_modules/@img/sharp-freebsd-wasm32": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-freebsd-wasm32/-/sharp-freebsd-wasm32-0.35.5.tgz",+ "integrity": "sha512-Y/z91nEZ4uIBX5X3nfTovjU9lHNKFYbL2lpHCLVNmXQK03VIZvXBBt0KxbPGp2SdGSF+2mQU4e+hQaWOt86iAw==",+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "freebsd"+ ],+ "dependencies": {+ "@img/sharp-wasm32": "0.35.5"+ },+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-libvips-darwin-arm64": {+ "version": "1.3.4",+ "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-arm64/-/sharp-libvips-darwin-arm64-1.3.4.tgz",+ "integrity": "sha512-5R89nBYiRdUlSWJxPhO+GVtaXzXSxKnRu/xqMn3KTA3L9EB9Oy/P+Nn2f2vlhPuUdy/Zusb2DarbyTpGCfEDuw==",+ "cpu": [+ "arm64"+ ],+ "license": "LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "darwin"+ ],+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-libvips-darwin-x64": {+ "version": "1.3.4",+ "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-x64/-/sharp-libvips-darwin-x64-1.3.4.tgz",+ "integrity": "sha512-iR2OKH80yi0U+dUplyh3/xdpFvps6YkCwsXenIJxqxR1v9o+xtKTGbS9H7cps+2Vxjc8B1j96p75NmTGjIhtpQ==",+ "cpu": [+ "x64"+ ],+ "license": "LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "darwin"+ ],+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-libvips-linux-arm": {+ "version": "1.3.4",+ "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm/-/sharp-libvips-linux-arm-1.3.4.tgz",+ "integrity": "sha512-LmRtTsOHuvM2+wlO2Db37dx5MiZhB0FvSunciw48YjdOkZz9KAiRbm8ujeMOA1INqmei5NapFxYEK1D1ZSidmw==",+ "cpu": [+ "arm"+ ],+ "license": "LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "linux"+ ],+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-libvips-linux-arm64": {+ "version": "1.3.4",+ "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm64/-/sharp-libvips-linux-arm64-1.3.4.tgz",+ "integrity": "sha512-Y3dgX/6lE2QhQb+Gxy0WZxfg9MEm/JBjamZpS2IklP7xIQoKN4hzAm7KcMVGtaVDt3neE9OKBC7vAfonA/Lr1A==",+ "cpu": [+ "arm64"+ ],+ "license": "LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "linux"+ ],+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-libvips-linux-ppc64": {+ "version": "1.3.4",+ "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-ppc64/-/sharp-libvips-linux-ppc64-1.3.4.tgz",+ "integrity": "sha512-Le6boB8Tai0Nis+gIxIpKx68UDVVIqdR8Tin5Yf1z2LJJQLDJvCDRqRu+jC2qCoD+eIomonmOwB4smBRxfVpYQ==",+ "cpu": [+ "ppc64"+ ],+ "license": "LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "linux"+ ],+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-libvips-linux-riscv64": {+ "version": "1.3.4",+ "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-riscv64/-/sharp-libvips-linux-riscv64-1.3.4.tgz",+ "integrity": "sha512-aHkkIEHPRdQEegJN20MLmGtxYD9R2wQr3Cwpddnu5+YKMt6Uzax7S9h5gpZTo8wyrGuZSlfQ63OevL5mTyOC7Q==",+ "cpu": [+ "riscv64"+ ],+ "license": "LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "linux"+ ],+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-libvips-linux-s390x": {+ "version": "1.3.4",+ "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-s390x/-/sharp-libvips-linux-s390x-1.3.4.tgz",+ "integrity": "sha512-ra/mB6MikESDUO7Yg+Mi95bFBb9GsObURuhnOv3OqknjGe9sZrG8tCe9q0xSIGrtLgvgw0gKnFWcK4blSgQOuQ==",+ "cpu": [+ "s390x"+ ],+ "license": "LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "linux"+ ],+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-libvips-linux-x64": {+ "version": "1.3.4",+ "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-x64/-/sharp-libvips-linux-x64-1.3.4.tgz",+ "integrity": "sha512-GJ//SSXbnwSDes02umB3nDJLFcQzw8a18V8fyhqr6tV515tOEMdImjjxj1AoafMRz56F3PHgftnj1QEKSU1zkw==",+ "cpu": [+ "x64"+ ],+ "license": "LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "linux"+ ],+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-libvips-linuxmusl-arm64": {+ "version": "1.3.4",+ "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-arm64/-/sharp-libvips-linuxmusl-arm64-1.3.4.tgz",+ "integrity": "sha512-hvulFwtjUcagsis6BBxHwGFwWoNZjgYmULGVrZcyfNbjA8hKILbRxGg15/7w5HDyXHXUos/j6baAWqnCyQ2DWA==",+ "cpu": [+ "arm64"+ ],+ "license": "LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "linux"+ ],+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-libvips-linuxmusl-x64": {+ "version": "1.3.4",+ "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-x64/-/sharp-libvips-linuxmusl-x64-1.3.4.tgz",+ "integrity": "sha512-6zXKeE/p39I1AmA3cJG35eyBGNqNddLnUXjhwBnsGjFPWqf5VKkDBEqaEkPDoTEtkxwi2vv8Tcr2mDyP4So7Fg==",+ "cpu": [+ "x64"+ ],+ "license": "LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "linux"+ ],+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-linux-arm": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm/-/sharp-linux-arm-0.35.5.tgz",+ "integrity": "sha512-LEaXK2WdXVK5ykcw0buWyPMsmLLL2vpHLD6yrNSW+JGEL3BZPA4tpKN6iaMc4AxTTAoaX/sU1rOL51lcIz48ZQ==",+ "cpu": [+ "arm"+ ],+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ },+ "optionalDependencies": {+ "@img/sharp-libvips-linux-arm": "1.3.4"+ }+ },+ "node_modules/@img/sharp-linux-arm64": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm64/-/sharp-linux-arm64-0.35.5.tgz",+ "integrity": "sha512-LYVx5JTsOM2CBzmxreh+nl64/3H6Xb09iSLknqH47z2T2DFFxDeFLP5y4dJwe6H7uGQlHPyEEtIqyo3DYsRwdQ==",+ "cpu": [+ "arm64"+ ],+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ },+ "optionalDependencies": {+ "@img/sharp-libvips-linux-arm64": "1.3.4"+ }+ },+ "node_modules/@img/sharp-linux-ppc64": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-linux-ppc64/-/sharp-linux-ppc64-0.35.5.tgz",+ "integrity": "sha512-QVxAAq8evVRI9ia2vqgwrmWucn5Dfv+JdWzj75pD8omHLPSP7f8p20O8jxzjCcuCEQEOtYOZUmX1hkiZ0kdevA==",+ "cpu": [+ "ppc64"+ ],+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ },+ "optionalDependencies": {+ "@img/sharp-libvips-linux-ppc64": "1.3.4"+ }+ },+ "node_modules/@img/sharp-linux-riscv64": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-linux-riscv64/-/sharp-linux-riscv64-0.35.5.tgz",+ "integrity": "sha512-LtdreXguaavKODPIfzJ4kffx7UNt1omwtK0rch4EBbbSTXPnxWmYSayXdLJw0fJzQ97kHt1gL/yh4tvU+nCyRQ==",+ "cpu": [+ "riscv64"+ ],+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ },+ "optionalDependencies": {+ "@img/sharp-libvips-linux-riscv64": "1.3.4"+ }+ },+ "node_modules/@img/sharp-linux-s390x": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-linux-s390x/-/sharp-linux-s390x-0.35.5.tgz",+ "integrity": "sha512-UZasTOFiYzotTsGOCu42BfUzP6Tu6Do/947iRm1RsLKvlllxwGcn4RN27LibGWceix4Y+Pmw3jsnTcCQIgWjqA==",+ "cpu": [+ "s390x"+ ],+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ },+ "optionalDependencies": {+ "@img/sharp-libvips-linux-s390x": "1.3.4"+ }+ },+ "node_modules/@img/sharp-linux-x64": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-linux-x64/-/sharp-linux-x64-0.35.5.tgz",+ "integrity": "sha512-SxFtLTeJInhAA9Q836kux2vZNeOBQEx658qvbboZScr0wIARym3IcGmW7KpVD5sbVg0Ojy+udFQdayYIZyoNog==",+ "cpu": [+ "x64"+ ],+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ },+ "optionalDependencies": {+ "@img/sharp-libvips-linux-x64": "1.3.4"+ }+ },+ "node_modules/@img/sharp-linuxmusl-arm64": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-arm64/-/sharp-linuxmusl-arm64-0.35.5.tgz",+ "integrity": "sha512-9HbMclmI1zlNkFRs3z9/eBtDjfD0sGlrX1z6b1qwmiFY5ElDLh4BC0LPBdVp7z1DXFiKlIcznf+ZlsuZzLxQqg==",+ "cpu": [+ "arm64"+ ],+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ },+ "optionalDependencies": {+ "@img/sharp-libvips-linuxmusl-arm64": "1.3.4"+ }+ },+ "node_modules/@img/sharp-linuxmusl-x64": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-x64/-/sharp-linuxmusl-x64-0.35.5.tgz",+ "integrity": "sha512-4KOphqB035HrVdqLZfCgMzzERrQkkzOwRhl4OAkRO1YCldbaFjySXMaK534Mo0V+LndnlJk+sbUyLeU0ULyD1A==",+ "cpu": [+ "x64"+ ],+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ },+ "optionalDependencies": {+ "@img/sharp-libvips-linuxmusl-x64": "1.3.4"+ }+ },+ "node_modules/@img/sharp-wasm32": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-wasm32/-/sharp-wasm32-0.35.5.tgz",+ "integrity": "sha512-Ptsga1su4tQx+LLF1ECS9U6nz5kmrXKo6XVbtR48Ke3ZRxxgaWBu7IDtEe1quo8hiupwm6WFqxVlXaSf7IINGQ==",+ "license": "Apache-2.0 AND LGPL-3.0-or-later AND MIT",+ "optional": true,+ "dependencies": {+ "@emnapi/runtime": "^1.11.3"+ },+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-webcontainers-wasm32": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-webcontainers-wasm32/-/sharp-webcontainers-wasm32-0.35.5.tgz",+ "integrity": "sha512-hfhF/FmoQyTUkA0bIKFOtw536BQSeBMe6BF6QyWlrPxT754+TFLaZ7sKKTfvvM0yJgKgaYTwnFCIZ/GuDw5SUA==",+ "cpu": [+ "wasm32"+ ],+ "license": "Apache-2.0",+ "optional": true,+ "dependencies": {+ "@img/sharp-wasm32": "0.35.5"+ },+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-win32-arm64": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-win32-arm64/-/sharp-win32-arm64-0.35.5.tgz",+ "integrity": "sha512-X4t7g+7ZA5DKblCBEXGjUqqemj4vczING/5viFwAL8h4N3qYeyjwdCvRLHi4EdOUI+2Z7UFlp1VM+p/AuEtm6Q==",+ "cpu": [+ "arm64"+ ],+ "license": "Apache-2.0 AND LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-win32-ia32": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-win32-ia32/-/sharp-win32-ia32-0.35.5.tgz",+ "integrity": "sha512-5Zm82LoBc43nhwNybZlG7Y1KO//Zhsn306fQl29ZOuStHLGTo3BWL83q3cznX0poxSAMuYL1On/BHBxkBeKr6A==",+ "cpu": [+ "ia32"+ ],+ "license": "Apache-2.0 AND LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": "^20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },+ "node_modules/@img/sharp-win32-x64": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/@img/sharp-win32-x64/-/sharp-win32-x64-0.35.5.tgz",+ "integrity": "sha512-x76eH0vEiHlcMQu8Y8IenntaACtddpT6W0wmXtWrnKcnKI7ME5DdgqhAD6SEWOEl1v2zDvkZDhFA9KnURwpfqg==",+ "cpu": [+ "x64"+ ],+ "license": "Apache-2.0 AND LGPL-3.0-or-later",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ }+ },"node_modules/@ioredis/commands": {"version": "1.10.0","resolved": "https://registry.npmjs.org/@ioredis/commands/-/commands-1.10.0.tgz",@@ -1977,7 +2488,6 @@"version": "2.1.2","resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz","integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==",- "dev": true,"license": "Apache-2.0","engines": {"node": ">=8"@@ -3953,6 +4463,55 @@"integrity": "sha512-oeM1lpU/UvhTxw+g3cIfxXHyJRc/uidd3yK1P242gzHds0udQBYzs3y8j4gCCW+ZJ7ad0yctld8RYO+bdurlvw==","license": "MIT"},+ "node_modules/sharp": {+ "version": "0.35.5",+ "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.35.5.tgz",+ "integrity": "sha512-Ywn4OnzGukp7CDMrp08RQ50YKmuwG47brZgIVPTvBaaAfQlRlygrRqSrxdCiL9M+LlzLBiJ68IR1QqvzHyjC7g==",+ "license": "Apache-2.0",+ "dependencies": {+ "@img/colour": "^1.1.0",+ "detect-libc": "^2.1.2",+ "semver": "^7.8.5"+ },+ "engines": {+ "node": ">=20.9.0"+ },+ "funding": {+ "url": "https://opencollective.com/libvips"+ },+ "optionalDependencies": {+ "@img/sharp-darwin-arm64": "0.35.5",+ "@img/sharp-darwin-x64": "0.35.5",+ "@img/sharp-freebsd-wasm32": "0.35.5",+ "@img/sharp-libvips-darwin-arm64": "1.3.4",+ "@img/sharp-libvips-darwin-x64": "1.3.4",+ "@img/sharp-libvips-linux-arm": "1.3.4",+ "@img/sharp-libvips-linux-arm64": "1.3.4",+ "@img/sharp-libvips-linux-ppc64": "1.3.4",+ "@img/sharp-libvips-linux-riscv64": "1.3.4",+ "@img/sharp-libvips-linux-s390x": "1.3.4",+ "@img/sharp-libvips-linux-x64": "1.3.4",+ "@img/sharp-libvips-linuxmusl-arm64": "1.3.4",+ "@img/sharp-libvips-linuxmusl-x64": "1.3.4",+ "@img/sharp-linux-arm": "0.35.5",+ "@img/sharp-linux-arm64": "0.35.5",+ "@img/sharp-linux-ppc64": "0.35.5",+ "@img/sharp-linux-riscv64": "0.35.5",+ "@img/sharp-linux-s390x": "0.35.5",+ "@img/sharp-linux-x64": "0.35.5",+ "@img/sharp-linuxmusl-arm64": "0.35.5",+ "@img/sharp-linuxmusl-x64": "0.35.5",+ "@img/sharp-webcontainers-wasm32": "0.35.5",+ "@img/sharp-win32-arm64": "0.35.5",+ "@img/sharp-win32-ia32": "0.35.5",+ "@img/sharp-win32-x64": "0.35.5"+ },+ "peerDependenciesMeta": {+ "@types/node": {+ "optional": true+ }+ }+ },"node_modules/shell-quote": {"version": "1.9.0","resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.9.0.tgz",@@ -4305,7 +4864,7 @@"version": "2.8.1","resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz","integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",- "dev": true,+ "devOptional": true,"license": "0BSD"},"node_modules/tsx": {diff --git a/backend/package.json b/backend/package.jsonindex e842a8ec3..72938f6dd 100644--- a/backend/package.json+++ b/backend/package.json@@ -41,6 +41,7 @@"http-proxy": "^1.18.1","ioredis": "^5.11.1","prom-client": "^15.1.3",+ "sharp": "^0.35.5","ws": "^8.21.0","zod": "^4.0.0"},diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prismaindex 9a24bace8..0b638c90a 100644--- a/backend/prisma/schema.prisma+++ b/backend/prisma/schema.prisma@@ -1409,6 +1409,11 @@ model SuperTabAttachment {/// A voice message's facts (superTabs/voiceMessages.ts StoredVoice): its length, waveform and words, read once before it is/// sent (store.ts readVoice). Absent on any other file. Removed with its row: the unsent-file sweep's, or its message's.voice Json?+ /// A picture's size and its thumbnail (superTabs/thumbnails.ts StoredImage): `{ width, height, thumb?: { url, width, height, size } }`,+ /// made when the picture was uploaded. The thumbnail object sits beside the original (`<objectKey>.thumb.webp`) and goes with+ /// it: the unsent-file sweep (lib/attachmentSweep.ts) and the Swarm purge (superTabs/purge.ts) remove both. Absent on a file+ /// that is not a PNG, JPEG or WebP, on one stored before thumbnails, and on one that would not decode. About 150 bytes.+ image Json?/// When a readVoice call claimed this file to transcribe it; unset once it stored `voice`. A claim older than two minutes/// is taken over.voiceSince DateTime?diff --git a/backend/scripts/swarm-computer-moves/run.ts b/backend/scripts/swarm-computer-moves/run.tsindex d42f61ba8..7f87985f2 100644--- a/backend/scripts/swarm-computer-moves/run.ts+++ b/backend/scripts/swarm-computer-moves/run.ts@@ -1,28 +1,48 @@// One row per computer (#3122): computers listed in Swarms under two accounts before a sign-in moved them. From this release a// sign-in moves the computer's entries by itself (superTabs/computerMoves.ts); this moves the ones listed twice already, once.-// Dry run by default: it writes nothing and prints each computer shared by two accounts, and every move the write would make-// (Swarm, old entry and owner, new entry and owner), decided exactly as the write decides. `--write` prints the moves it made:-// what Ken's go covered, and what each old owner can add back with Add machine.+// Dry run by default: it writes nothing and prints each computer shared by two accounts, every move the write would make+// (Swarm, old entry and owner, new entry and owner), and every move the Swarm refuses (`left`, with its refusal's code). Both+// decide through the same checks, the dry run without a transaction, so what it prints is what the write does (task #3227:+// it once listed a move the write then refused). `--write` prints the moves it made and the ones it left: what Ken's go+// covered, and what each old owner can add back with Add machine.// It reads every machine row that has a computer id, once, on the (computerId, userId) index: check that read's plan and the// collection's size on the read-only login (dry run) before the go.// Rows count as one computer by their computer id alone, which is self-declared: a pinned ADAPTER_COMPUTER_ID shared by// containers under different accounts reads as one computer here. The write moves an entry only where both accounts' logs// hold the same machine key for it (computerMoves.ts sameComputerKey), so those containers are listed and left alone.-// cd backend && DATABASE_URL="$(read-only URL from its environment file)" npx tsx scripts/swarm-computer-moves/run.ts+// It needs DATABASE_URL and SWARM_WORKSPACES_REQUIRED, set to '1' or '0' as the server has it: it decides which Swarms a+// person may reach, and read unset it would pass a Swarm the server refuses (security review 1 of #3146), so it must be said.+// Billing is off here (it moves no plan or payment), so billing's credential key is not needed. It exits once it has printed.+// cd backend && DATABASE_URL="$(read-only URL from its environment file)" SWARM_WORKSPACES_REQUIRED=<as the server> npx tsx scripts/swarm-computer-moves/run.ts// `--write` is a production write: run it only in the backend release Ken says go for, with the release's own login.// cd backend && npx tsx scripts/swarm-computer-moves/run.ts --write-// Safe to run twice: an entry moved is gone. It prints JSON only, machine and account ids, never an email or a name. What it-// moved can be added back by its owner (Add machine).-import { PrismaClient } from '@prisma/client'-import { SuperTabStore } from '../../src/superTabs/store.js'-import { cleanUpComputers } from '../../src/superTabs/computerMoves.js'+// Safe to run twice: an entry moved is gone. It prints JSON only, machine and account ids and refusal codes, never an email or+// a name. What it moved can be added back by its owner (Add machine).+/** Written in full before the exit below: a pipe's writes are asynchronous, and an exit drops what is still queued. */+const print = (stream: NodeJS.WriteStream, text: string) => new Promise<void>((resolve, reject) => stream.write(`${text}\n`, e => e ? reject(e) : resolve()))+if (process.env.SWARM_WORKSPACES_REQUIRED !== '0' && process.env.SWARM_WORKSPACES_REQUIRED !== '1') {+ await print(process.stderr, 'Set SWARM_WORKSPACES_REQUIRED to 1 or 0, as the server has it: it decides which Swarms a person may reach.')+ process.exit(1)+}+process.env.HARNESS_BILLING_ENABLED ??= 'false'+const [{ PrismaClient }, { SuperTabStore }, { cleanUpComputers }] = await Promise.all([+ import('@prisma/client'), import('../../src/superTabs/store.js'), import('../../src/superTabs/computerMoves.js'),+])const write = process.argv.includes('--write')const env = process.argv.find(arg => arg.startsWith('--env='))?.slice('--env='.length) ?? 'prod'const db = new PrismaClient()try {const result = await cleanUpComputers(db, new SuperTabStore(db), env, { write })- console.log(JSON.stringify({ write, env, ...result }, null, 2))+ await print(process.stdout, JSON.stringify({ write, env, ...result }, null, 2))+} catch (error) {+ // Its code and first line only: a database error carries its query, and this prints ids and codes alone.+ const code = typeof (error as { code?: unknown }).code === 'string' ? `${(error as { code: string }).code}: ` : ''+ await print(process.stderr, `The cleanup failed: ${code}${(error instanceof Error ? error.message : String(error)).split('\n')[0]}`).catch(() => {})+ process.exitCode = 1} finally {- await db.$disconnect()+ await db.$disconnect().catch(() => {})+ // The store's modules open the bus's Redis clients on import (lib/bus.ts), which reconnect for ever and whose quit waits for+ // a connection that may never come: the script ends here, once it has printed, instead of on them.+ process.exit()}diff --git a/backend/src/lib/attachmentSweep.ts b/backend/src/lib/attachmentSweep.tsindex 1d8ef02fc..b3b259ff9 100644--- a/backend/src/lib/attachmentSweep.ts+++ b/backend/src/lib/attachmentSweep.ts@@ -2,6 +2,7 @@ import { setTimeout as delay } from 'node:timers/promises'import type { PrismaClient } from '@prisma/client'import { AVATAR_PURPOSE, EXPIRED_UPLOAD, PENDING_UPLOAD_PURPOSE, REMOVING_UPLOAD_PURPOSE, UNSENT_LIFE_MS, type AttachmentStore, type Attachments } from '../superTabs/attachments.js'import { logger } from '../utils/logger.js'+import { imageFacts, thumbKeyOf } from '../superTabs/thumbnails.js'/*** Removes Swarm uploads nobody used within a day (Diego, 10-30): chat files never sent and pictures never set, row and stored@@ -66,7 +67,7 @@ export function failureCode(error: unknown): string {/** Removes up to [chunk] taken uploads, skipping those whose file failed earlier in this run. */async function remove(db: PrismaClient, attachments: Attachments, chunk: number, failedIds: Set<string>, log: Log): Promise<AttachmentSweepPass & { found: number }> {const found = await db.$runCommandRaw({ find: COLLECTION, filter: { usedBy: EXPIRED_UPLOAD, _id: { $nin: [...failedIds] } },- projection: { _id: 1, objectKey: 1, purpose: 1 }, limit: chunk, singleBatch: true } as never) as Found<{ _id: string; objectKey: string; purpose?: string | null }>+ projection: { _id: 1, objectKey: 1, purpose: 1, image: 1 }, limit: chunk, singleBatch: true } as never) as Found<{ _id: string; objectKey: string; purpose?: string | null; image?: unknown }>const rows = found.cursor.firstBatchconst gone: string[] = []for (const row of rows) {@@ -74,6 +75,8 @@ async function remove(db: PrismaClient, attachments: Attachments, chunk: number,try {if (!store) throw new Error('No avatar store is configured')await store.delete(row.objectKey)+ // A picture's thumbnail sits beside it and goes with it (superTabs/thumbnails.ts); one failing keeps the row for the next run.+ if (imageFacts(row.image)?.thumb) await store.delete(thumbKeyOf(row.objectKey))gone.push(row._id)} catch (error) {// The key and the error's text hold the file's name and link key: only the row id and the error's code are logged.diff --git a/backend/src/superTabs/attachments.ts b/backend/src/superTabs/attachments.tsindex 69861e02b..dc5eb4c76 100644--- a/backend/src/superTabs/attachments.ts+++ b/backend/src/superTabs/attachments.ts@@ -12,7 +12,12 @@ import type { VoiceFacts } from './voiceMessages.js'* open the public address; only a file with neither comes from a server from before links. None of the three is stored. */export interface Attachment { id: string; name: string; mime: string; size: number; url: string; link?: string; expiresAt?: number; unlinked?: true/** A voice message's length, waveform and whether it has words (voiceMessages.ts); its words are the message's text. */- voice?: VoiceFacts }+ voice?: VoiceFacts+ /** A picture's size as it is shown, and the small WebP the server made of it at upload (thumbnails.ts): a chat draws that and+ * the viewer opens the original. `thumb.url` is its public address beside the original's; a read adds `link` and+ * `expiresAt` (or `unlinked`) exactly as for the file, from the same read (fileLinks.ts). Absent on a file without them. */+ width?: number; height?: number+ thumb?: { url: string; width: number; height: number; size: number; link?: string; expiresAt?: number; unlinked?: true } }/** One chat file, whichever way it comes. Every client checks the same number before it sends (attachments.limits.test.ts). */export const MAX_ATTACHMENT_BYTES = 256 * 1024 * 1024@@ -69,6 +74,8 @@ export interface DirectUploads {stored(objectKey: string): Promise<{ size: number } | null>/** Its first `bytes` bytes. */head(objectKey: string, bytes: number): Promise<Buffer>+ /** The whole object, for a picture to be thumbnailed once its upload is complete; `size` is what `stored` answered. */+ read(objectKey: string): Promise<Buffer>/** Removes it; an object already gone counts as removed. */delete(objectKey: string): Promise<void>}@@ -263,6 +270,10 @@ export class GcsAttachmentStore implements AttachmentStore, DirectUploads {const [content] = await (await this.files()).file(objectKey).download({ start: 0, end: bytes - 1 })return content}+ async read(objectKey: string) {+ const [content] = await (await this.files()).file(objectKey).download()+ return content+ }async delete(objectKey: string) {await (await this.files()).file(objectKey).delete({ ignoreNotFound: true })}@@ -343,6 +354,7 @@ export class MemoryAttachmentStore implements AttachmentStore, DirectUploads {}async stored(objectKey: string) { const object = this.objects.get(objectKey); return object ? { size: object.bytes.byteLength } : null }async head(objectKey: string, bytes: number) { return (this.objects.get(objectKey)?.bytes ?? Buffer.alloc(0)).subarray(0, bytes) }+ async read(objectKey: string) { return this.objects.get(objectKey)?.bytes ?? Buffer.alloc(0) }async delete(objectKey: string) { this.objects.delete(objectKey) }}diff --git a/backend/src/superTabs/computerMoves.ts b/backend/src/superTabs/computerMoves.tsindex d75bbe4a8..2cadba6f3 100644--- a/backend/src/superTabs/computerMoves.ts+++ b/backend/src/superTabs/computerMoves.ts@@ -27,6 +27,10 @@ export async function otherAccountsOnComputer(db: Reader, { computerId, ownerId,* [toOwner]'s [to]. Ids only, so the cleanup's output names what Ken's go covers and what each owner may add back. */export interface ComputerMove { tabId: string; from: string; fromOwner: string; to: string; toOwner: string }+/** A move the Swarm refused, dry run and write alike, with the refusal's code (WORKSPACE_ACCESS_REVOKED, NOT_FOUND, ...): what+ * the cleanup leaves, and why, so its dry run says what the write will not do (task #3227). Ids and a code only. */+export interface LeftMove extends ComputerMove { code: string }+/*** The entry of [ownerId]'s machine [machineId] that a sign-in of [to] on the same computer moves, or none: the computer's own* entry (not a Team account's), not being removed, still [ownerId]'s, and not reporting, since two live accounts on one@@ -96,7 +100,7 @@ export async function teamCleanupPending(db: Reader & Pick<Prisma.TransactionCli}/** What the one-time cleanup found: each computer with live rows of two or more accounts, the one seen last, and the others. */-export interface CleanUp { computers: Array<{ signedIn: { machineId: string; ownerId: string }; others: string[] }>; moves: ComputerMove[] }+export interface CleanUp { computers: Array<{ signedIn: { machineId: string; ownerId: string }; others: string[] }>; moves: ComputerMove[]; left: LeftMove[] }/*** The one-time cleanup of computers listed under two accounts before moves existed (Scholes, 11-05: MacBooks-MacBook-Pro-7 three@@ -105,7 +109,7 @@ export interface CleanUp { computers: Array<{ signedIn: { machineId: string; own* false it writes nothing and lists the moves the write would make, decided the same way. Safe to run again: an entry moved* is gone, and nothing else is written.*/-export async function cleanUpComputers(db: Reader, store: { computerSignedIn(signIn: ComputerSignIn, options: { write: boolean }): Promise<ComputerMove[]> },+export async function cleanUpComputers(db: Reader, store: { computerSignedIn(signIn: ComputerSignIn, options: { write: boolean; left: LeftMove[] }): Promise<ComputerMove[]> },autonomousEnv: string,{ write }: { write: boolean }): Promise<CleanUp> {const groups = await db.machine.aggregateRaw({ pipeline: [@@ -113,15 +117,17 @@ export async function cleanUpComputers(db: Reader, store: { computerSignedIn(sig{ $group: { _id: '$computerId', accounts: { $addToSet: '$userId' }, rows: { $push: { machineId: '$machineId', userId: '$userId', seen: '$lastSeenAt' } } } },{ $match: { 'accounts.1': { $exists: true } } }],options: { hint: { computerId: 1, userId: 1 } } }) as unknown as Array<{ _id: string; rows: Array<{ machineId: string; userId: string; seen?: { $date: string } | null }> }>- const result: CleanUp = { computers: [], moves: [] }+ const result: CleanUp = { computers: [], moves: [], left: [] }for (const group of groups) {const seen = (row: (typeof group.rows)[number]) => row.seen ? Date.parse(row.seen.$date) : 0const [last, next] = [...group.rows].sort((a, b) => seen(b) - seen(a))if (!last || !seen(last) || (next && seen(next) === seen(last))) continueresult.computers.push({ signedIn: { machineId: last.machineId, ownerId: last.userId },others: group.rows.filter(row => row.userId !== last.userId).map(row => row.machineId) })- // The dry run decides as the write does, so what it lists is what the write moves.- result.moves.push(...await store.computerSignedIn({ machineId: last.machineId, ownerId: last.userId, computerId: group._id, autonomousEnv }, { write }))+ // The dry run decides as the write does, through the same checks, so what it lists is what the write moves, and what it+ // leaves the write leaves for the same reason.+ result.moves.push(...await store.computerSignedIn({ machineId: last.machineId, ownerId: last.userId, computerId: group._id, autonomousEnv },+ { write, left: result.left }))}return result}diff --git a/backend/src/superTabs/fileLinks.test.ts b/backend/src/superTabs/fileLinks.test.tsindex ccb1ff03c..1e96b4bef 100644--- a/backend/src/superTabs/fileLinks.test.ts+++ b/backend/src/superTabs/fileLinks.test.ts@@ -98,6 +98,71 @@ describe('chat file links', () => {expect(count).toBe(MAX_CACHED_LINKS + 1)})+ describe('a picture\'s thumbnail', () => {+ const picture = (key: string, thumbKey: string | null, id = 'pic'): Attachment => ({ ...file(key, id), mime: 'image/jpeg', width: 2000, height: 1500,+ ...thumbKey ? { thumb: { url: `${base}/${thumbKey}`, width: 720, height: 540, size: 40_000 } } : {} })++ it('is linked in the same read as its original, with the same expiry', async () => {+ const bucket = new MemoryAttachmentStore(() => now)+ const [read] = await new FileLinks(bucket, base, 'super-tabs').forRows('tab1', [row(picture('super-tabs/tab1/abc/p.jpg', 'super-tabs/tab1/abc/p.jpg.thumb.webp'))], now)+ const [linked] = read!.attachments+ expect(linked).toMatchObject({ width: 2000, height: 1500, link: expect.any(String), expiresAt: fileLinkExpiry(now),+ thumb: { url: `${base}/super-tabs/tab1/abc/p.jpg.thumb.webp`, width: 720, height: 540, size: 40_000, link: expect.stringContaining('p.jpg.thumb.webp'), expiresAt: fileLinkExpiry(now) } })+ expect(Object.keys(linked!.thumb!).sort()).toEqual(['expiresAt', 'height', 'link', 'size', 'url', 'width'])+ })++ it('is never linked for a file that is not linked itself', async () => {+ const [read] = await new FileLinks(new MemoryAttachmentStore(() => now), base, 'super-tabs').forRows('tab1', [row(+ picture('super-tabs/tab2/abc/other.jpg', 'super-tabs/tab1/abc/other.jpg.thumb.webp', 'other'))], now)+ expect(read!.attachments[0]).toMatchObject({ unlinked: true })+ expect(read!.attachments[0]!.link).toBeUndefined()+ expect(read!.attachments[0]!.thumb?.link).toBeUndefined()+ })++ it('is dropped, never linked, when its address is outside the Swarm\'s own folder', async () => {+ const keys = ['super-tabs/tab2/abc/p.jpg.thumb.webp', 'avatars/tab1/abc/avatar', 'super-tabs/tab1x/abc/p.webp', 'super-tabs/tab1/../tab2/abc/p.webp', 'super-tabs/tab1/abc/p.webp?x=1']+ const [read] = await new FileLinks(new MemoryAttachmentStore(() => now), base, 'super-tabs').forRows('tab1', [row(+ ...keys.map((key, i) => picture(`super-tabs/tab1/abc/p${i}.jpg`, key, `p${i}`)),+ { ...picture('super-tabs/tab1/abc/q.jpg', null, 'q'), thumb: { url: 'https://evil.example.test/super-tabs/tab1/abc/q.webp', width: 1, height: 1, size: 1 } })], now)+ for (const linked of read!.attachments) {+ expect(linked.link).toBeTruthy()+ expect(linked.thumb).toBeUndefined()+ }+ })++ it('is linked only when it is this very file\'s own thumbnail, never another file of the same Swarm', async () => {+ const bucket = new MemoryAttachmentStore(() => now)+ const other = 'super-tabs/tab1/zzz/private-chat-secret.jpg'+ bucket.objects.set(other, { bytes: Buffer.from('secret'), contentType: 'image/jpeg', contentDisposition: 'inline' })+ const own = 'super-tabs/tab1/abc/p.jpg'+ const [read] = await new FileLinks(bucket, base, 'super-tabs').forRows('tab1', [row(+ picture(own, other, 'elsewhere'),+ picture(own, 'super-tabs/tab1/zzz/p.jpg.thumb.webp', 'other-folder'),+ picture(own, 'super-tabs/tab1/abc/q.jpg.thumb.webp', 'other-name'),+ picture(own, own, 'itself'),+ picture(own, 'super-tabs/tab1/abc/p.jpg.thumb.webp', 'own'))], now)+ const byId = Object.fromEntries(read!.attachments.map(a => [a.id, a]))+ for (const id of ['elsewhere', 'other-folder', 'other-name', 'itself']) {+ expect(byId[id]!.thumb, id).toBeUndefined()+ expect(JSON.stringify(byId[id])).not.toContain('private-chat-secret')+ }+ expect(byId.own!.thumb?.link).toContain('p.jpg.thumb.webp')+ })++ it('is dropped when what is stored is not a thumbnail', async () => {+ const [read] = await new FileLinks(new MemoryAttachmentStore(() => now), base, 'super-tabs').forRows('tab1', [row(+ { ...picture('super-tabs/tab1/abc/a.jpg', null, 'a'), thumb: { url: 5, width: 'x' } as never },+ { ...picture('super-tabs/tab1/abc/b.jpg', null, 'b'), width: 0 })], now)+ expect(read!.attachments[0]).not.toHaveProperty('thumb')+ expect(read!.attachments[1]).not.toHaveProperty('width')+ })++ it('leaves a file without one as before', async () => {+ const [read] = await new FileLinks(new MemoryAttachmentStore(() => now), base, 'super-tabs').forRows('tab1', [row(file('super-tabs/tab1/abc/old.png'))], now)+ expect(Object.keys(read!.attachments[0]!).sort()).toEqual(['expiresAt', 'id', 'link', 'mime', 'name', 'size', 'url'])+ })+ })+it('leaves rows without files as they are', async () => {const rows = [{ id: 'a' }, { id: 'b', attachments: null }, { id: 'c', attachments: [] }]expect(await new FileLinks({ signedUrl: async () => { throw new Error('never') } }, base, 'super-tabs').forRows('tab1', rows, now)).toBe(rows)diff --git a/backend/src/superTabs/fileLinks.ts b/backend/src/superTabs/fileLinks.tsindex e8db43585..556bda983 100644--- a/backend/src/superTabs/fileLinks.ts+++ b/backend/src/superTabs/fileLinks.ts@@ -1,5 +1,6 @@import { logger } from '../utils/logger.js'import { errorCode, trimPrefix, type Attachment, type AttachmentStore } from './attachments.js'+import { imageFacts, thumbKeyOf } from './thumbnails.js'import { voiceFacts } from './voiceMessages.js'/**@@ -50,13 +51,33 @@ export class FileLinks {const expiresAt = fileLinkExpiry(now)if (expiresAt !== this.window || this.signed.size >= MAX_CACHED_LINKS) { this.signed.clear(); this.window = expiresAt }const keys = new Set<string>()- for (const row of rows) for (const file of files(row)) { const key = this.keyOf(tabId, file.url); if (key) keys.add(key) }+ // A thumbnail is linked in the very read that links its original, for the same reader, and only inside the Swarm's folder.+ // ...and only this file's own: every conversation of a Swarm shares one folder, so a thumbnail address naming any other file in+ // it would hand a reader of this message a link to a file of a chat they may not read. The rule is enforced here, not left to+ // what is stored.+ const thumbKey = (file: Attachment, facts: ReturnType<typeof imageFacts>) => {+ const key = this.keyOf(tabId, file.url), thumb = facts?.thumb ? this.keyOf(tabId, facts.thumb.url) : null+ return key && thumb && thumb === thumbKeyOf(key) ? thumb : null+ }+ const picture = (file: Attachment) => {+ const facts = this.keyOf(tabId, file.url) ? imageFacts(file) : null+ return facts && facts.thumb && !thumbKey(file, facts) ? { width: facts.width, height: facts.height } : facts+ }+ for (const row of rows) for (const file of files(row)) {+ const key = this.keyOf(tabId, file.url)+ if (key) keys.add(key)+ const thumb = thumbKey(file, picture(file))+ if (thumb) keys.add(thumb)+ }const links = await this.signAll([...keys], expiresAt)return rows.map(row => !Array.isArray(row.attachments) ? row : { ...row, attachments: files(row).map(file => {const key = this.keyOf(tabId, file.url), url = key ? links.get(key) : undefinedconst voice = voiceFacts(file.voice)+ const facts = picture(file), own = facts ? thumbKey(file, facts) : null, thumbUrl = own ? links.get(own) : undefined+ const image = !facts ? {} : { width: facts.width, height: facts.height,+ ...facts.thumb && own ? { thumb: { ...facts.thumb, ...thumbUrl ? { link: thumbUrl, expiresAt } : { unlinked: true as const } } } : {} }return { id: file.id, name: file.name, mime: file.mime, size: file.size, url: typeof file.url === 'string' ? file.url : '',- ...url ? { link: url, expiresAt } : { unlinked: true as const }, ...voice ? { voice } : {} } satisfies Attachment+ ...url ? { link: url, expiresAt } : { unlinked: true as const }, ...voice ? { voice } : {}, ...image } satisfies Attachment}) })}diff --git a/backend/src/superTabs/purge.ts b/backend/src/superTabs/purge.tsindex 1b5395a8d..450a2d8f5 100644--- a/backend/src/superTabs/purge.ts+++ b/backend/src/superTabs/purge.ts@@ -2,6 +2,7 @@ import type { PrismaClient } from '@prisma/client'import { logger } from '../utils/logger.js'import { AVATAR_PURPOSE, attachmentsFromEnv, SharedAvatarStore, errorCode, trimPrefix, type Attachments, type AttachmentStore } from './attachments.js'import { readBy } from './parts/indexed.js'+import { imageFacts, thumbKeyOf } from './thumbnails.js'import { ms } from './parts/light.js'// The purge of deleted Swarms whose grace is over (deletion.ts). Each goes in three steps, so a pass that stops anywhere is@@ -45,14 +46,18 @@ const folder = (prefix: string, tabId: string) => [prefix, tabId].filter(Boolean* the next one carries on, its record kept so it is found again. */async function purgeAttachments(db: PrismaClient, tabId: string, files: PurgeFiles | null, page: number): Promise<boolean> {for (;;) {- const rows = await db.superTabAttachment.findMany({ where: { tabId }, select: { id: true, objectKey: true, purpose: true }, take: page })+ const rows = await db.superTabAttachment.findMany({ where: { tabId }, select: { id: true, objectKey: true, purpose: true, image: true }, take: page })for (const row of rows) {const avatar = row.purpose === AVATAR_PURPOSEconst remover = files ? avatar ? files.avatars : files.chat : nullconst own = files ? row.objectKey.startsWith(folder(avatar ? files.avatarPrefix : files.prefix, tabId)) : false// A key outside this Swarm's folder is never another's to remove from here: its record goes, the object stays.if (remover && own) {- try { await remover.delete(row.objectKey) } catch (error) {+ try {+ await remover.delete(row.objectKey)+ // A picture's thumbnail sits beside it (thumbnails.ts) and goes with it.+ if (imageFacts(row.image)?.thumb) await remover.delete(thumbKeyOf(row.objectKey))+ } catch (error) {logger.warn('[swarm-purge] a stored file could not be removed yet', { tabId, id: row.id, code: errorCode(error) })return false}diff --git a/backend/src/superTabs/routeRecord.test.ts b/backend/src/superTabs/routeRecord.test.tsindex 06e23dfb0..cfdd00eb6 100644--- a/backend/src/superTabs/routeRecord.test.ts+++ b/backend/src/superTabs/routeRecord.test.ts@@ -126,9 +126,10 @@ describe('a route decision of every engine on every machine, as stored (task #27}// Measured 11-04: 14,568 bytes, of which the 42 slim fits are 5,376 (128 bytes a candidate, its 64-character account most of it);- // 15,282 bytes since each keeps its fit status too (task #3163), from a CLI that sends no % left or reset.- it('stores a record that names no pick, 42 candidates each with its account, spend and status, in under 16 KB (measured 15,282 bytes)', () => {- expect(size(storedRecord(pickless()))).toBeLessThan(16_000)+ // 15,282 bytes since each keeps its fit status too (task #3163), from a CLI that sends no % left or reset; 16,038 with its+ // coverage (task #3255, 18 bytes a candidate).+ it('stores a record that names no pick, 42 candidates each with its account, spend, status and coverage, in under 16.5 KB (measured 16,038 bytes)', () => {+ expect(size(storedRecord(pickless()))).toBeLessThan(16_500)})it('keeps of every candidate of a record that names no pick who and where it is, its title and its account, spend and status', () => {@@ -136,7 +137,7 @@ describe('a route decision of every engine on every machine, as stored (task #27for (const [i, c] of r.candidates.entries()) {const kept = back.candidates[i]!expect(kept).toEqual({ id: c.id, kind: c.kind, ...c.agentId ? { agentId: c.agentId } : {}, machineId: c.machineId, engine: c.engine, model: c.model,- effort: c.effort, title: c.title, fit: { account: c.fit!.account, spend: c.fit!.spend, status: c.fit!.status } })+ effort: c.effort, title: c.title, fit: { account: c.fit!.account, spend: c.fit!.spend, status: c.fit!.status, coverage: c.fit!.coverage } })}})@@ -158,12 +159,30 @@ describe('a route decision of every engine on every machine, as stored (task #27it('keeps of every candidate of a record that names no pick its fit status, weekly % left and reset beside its account and spend', () => {const r = quoted(), back = readRecord(storedRecord(r))for (const [i, c] of r.candidates.entries()) {- expect(back.candidates[i]!.fit).toEqual({ account: c.fit!.account, spend: c.fit!.spend, status: c.fit!.status, left: c.fit!.left, resetsAt: c.fit!.resetsAt })+ expect(back.candidates[i]!.fit).toEqual({ account: c.fit!.account, spend: c.fit!.spend, status: c.fit!.status, coverage: c.fit!.coverage,+ left: c.fit!.left, resetsAt: c.fit!.resetsAt })}})- // Measured 11-05: 16,742 bytes, the 42 candidates' status, % left and reset 2,174 of it (about 52 bytes a candidate).- it('stores a record that names no pick, 42 candidates each with its quota, in under 17 KB (measured 16,742 bytes)', () => {- expect(size(storedRecord(quoted()))).toBeLessThan(17_000)+ // Task #3255 (Ken, 2026-11-05): quota at route time is measured on every candidate, a Codex as much as a Claude, and how far+ // its quota covers the task (coverage) was still dropped. A candidate whose quota was not read keeps its null coverage.+ it('keeps of a recorded Codex and Claude candidate the whole quota fit: status, coverage, weekly % left and reset', () => {+ const r = quoted()+ r.candidates[2]!.fit = { ...r.candidates[2]!.fit!, status: 'unknown', coverage: null, left: null, resetsAt: null }+ const back = readRecord(storedRecord(r)).candidates+ for (const engine of ['claude', 'codex'] as const) {+ const i = r.candidates.findIndex(c => c.engine === engine && c.kind === 'new')+ const { status, coverage, left, resetsAt } = r.candidates[i]!.fit!+ expect(back[i]!.fit, engine).toMatchObject({ status, coverage, left, resetsAt })+ expect(status, engine).toEqual(expect.any(String))+ }+ expect(back[2]!.engine).toBe('claude')+ expect(back[2]!.fit).toMatchObject({ status: 'unknown', coverage: null, left: null, resetsAt: null })+ for (const c of back) expect(c.fit).toHaveProperty('coverage')+ })+ // Measured 11-05: 16,742 bytes, the 42 candidates' status, % left and reset 2,174 of it (about 52 bytes a candidate); 17,228+ // with their coverage (task #3255).+ it('stores a record that names no pick, 42 candidates each with its quota, in under 17.5 KB (measured 17,228 bytes)', () => {+ expect(size(storedRecord(quoted()))).toBeLessThan(17_500)})it('keeps no fit of a candidate that had none, and no account of one that had none', () => {@@ -172,7 +191,7 @@ describe('a route decision of every engine on every machine, as stored (task #27delete r.candidates[1]!.fit!.accountconst [first, second] = storedRecord(r).candidatesexpect(first).not.toHaveProperty('fit')- expect(second!.fit).toEqual({ spend: { '5-hour': 2.5, weekly: 1 }, status: r.candidates[1]!.fit!.status })+ expect(second!.fit).toEqual({ spend: { '5-hour': 2.5, weekly: 1 }, status: r.candidates[1]!.fit!.status, coverage: r.candidates[1]!.fit!.coverage })})it('keeps a grid model on a candidate that runs one', () => {diff --git a/backend/src/superTabs/routeRecord.ts b/backend/src/superTabs/routeRecord.tsindex 7e3c1de25..172d64bd2 100644--- a/backend/src/superTabs/routeRecord.ts+++ b/backend/src/superTabs/routeRecord.ts@@ -81,9 +81,9 @@ export const routeRecordSchema = z.object({.refine(d => new Set(d.candidates.map(c => c.id)).size === d.candidates.length, 'Candidate ids must be unique.')/** The part of a quota fit every candidate of a record without a pick keeps: what the route history reads of the one that ran,- * and its quota when the route ran (task #3163: with account and spend alone, quota at route time could not be measured).- * Rows written before keep no status, % left or reset. */-export type KeptFit = Pick<z.infer<typeof fit>, 'account' | 'spend'> & Partial<Pick<z.infer<typeof fit>, 'status' | 'left' | 'resetsAt'>>+ * and its quota when the route ran (task #3163: with account and spend alone, quota at route time could not be measured), and+ * how far that quota covered the task (task #3255). Rows written before keep no status, coverage, % left or reset. */+export type KeptFit = Pick<z.infer<typeof fit>, 'account' | 'spend'> & Partial<Pick<z.infer<typeof fit>, 'status' | 'coverage' | 'left' | 'resetsAt'>>/** A candidate as a decision keeps it: the pick whole; any other only who and where it is and its title, and with no pick its* account and spend too (storedRecord). */export type KeptCandidate = Omit<RouteCandidate, 'quality' | 'cost' | 'label' | 'fit'> & Partial<Pick<RouteCandidate, 'quality' | 'cost' | 'label'>>@@ -104,10 +104,11 @@ export interface StoredRouteRecord extends Omit<ReadRouteRecord, 'excluded'> {* and where it is (a start or assignment matching it, startedAs; a person choosing it, route.override; its Team-work check) and* its title (the chat lines, Desktop's Details). Its label, strength, cost and quota fit are read of the pick only: the route* showed the others when it ran, and the route history takes the account and spend of the pick (cli service.ts routeHistory).- * A record with no pick (task #2901) keeps every candidate's account, spend and quota instead, as any of them may be the one that- * runs and nobody knows which until the start. Each exclusion reason is kept once, with its titles in order. */-function keptFit({ account, spend, status, left, resetsAt }: z.infer<typeof fit>): KeptFit {- return { ...account ? { account } : {}, spend, status, ...left !== undefined ? { left } : {}, ...resetsAt !== undefined ? { resetsAt } : {} }+ * A record with no pick (task #2901) keeps every candidate's account, spend and quota (status, coverage, % left and reset) instead,+ * as any of them may be the one that runs and nobody knows which until the start. Each exclusion reason is kept once, with its+ * titles in order. */+function keptFit({ account, spend, status, coverage, left, resetsAt }: z.infer<typeof fit>): KeptFit {+ return { ...account ? { account } : {}, spend, status, coverage, ...left !== undefined ? { left } : {}, ...resetsAt !== undefined ? { resetsAt } : {} }}export function storedRecord(record: RouteRecord): StoredRouteRecord {diff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.tsindex 1614278dc..cc5db4690 100644--- a/backend/src/superTabs/store.ts+++ b/backend/src/superTabs/store.ts@@ -32,9 +32,10 @@ import { threadSummaries } from './threads.js'import { closeProposal, databaseProposalReads, openProposalRows, openProposals, proposalChat, type OpenProposal } from './proposals.js'import { attachmentMime, attachmentName, AttachmentTooLarge, AVATAR_PURPOSE, AVATAR_TYPES, checkedType, directUploads, errorCode, EXPIRED_UPLOAD, HOURLY_UPLOAD_BYTES, HOURLY_UPLOADS, MAX_ATTACHMENT_BYTES, MAX_AVATAR_BYTES, MAX_FORM_ATTACHMENT_BYTES, objectKeyFor, peekType, PENDING_UPLOAD_PURPOSE, publicUrl, TOO_LARGE_MESSAGE, trimPrefix, typeFromHead, UPLOAD_LINK_MS,type Attachment, type Attachments, type AttachmentStore, type AvatarStore } from './attachments.js'+import { makeThumbnail, MAX_READ_BACK_BYTES, pictureFields, THUMBNAILED_TYPES, thumbKeyOf, thumbnailsReserve, type StoredImage } from './thumbnails.js'import { FileLinks } from './fileLinks.js'import { randomBytes, randomUUID } from 'node:crypto'-import type { Readable } from 'node:stream'+import { Readable } from 'node:stream'import { logger } from '../utils/logger.js'import { noteClaim, recordDeliveryCall } from '../lib/deliveryCalls.js'import { retryTransient } from './retry.js'@@ -65,7 +66,7 @@ import { clock, insertParts, loadActive, loadGroups, loadLightGroups, loadMachinimport { groupsBy, listingDrops, quietSince, readBy, withoutHints } from './parts/indexed.js'import { autoJoinSwarms, machineSwarms, memberSwarms } from './swarmRows.js'import type { MachineRename } from './machineRenames.js'-import { movableEntry, otherAccountsOnComputer, sameComputerKey, type ComputerMove, type ComputerSignIn, type MachineKeys } from './computerMoves.js'+import { otherAccountsOnComputer, sameComputerKey, type ComputerMove, type ComputerSignIn, type LeftMove, type MachineKeys } from './computerMoves.js'import { activeMachineKeysOf } from '../lib/deviceKeyLog.js'import { commitParts, indexesOf, machineContentChanged, numberMessages, type Touch, type Written } from './parts/commit.js'import { loadDeliveryParts, ms } from './parts/light.js'@@ -2194,8 +2195,9 @@ export class SuperTabStore {if (!name.success || !mime.success) fail('INVALID_ATTACHMENT', 'Attach a file with a printable name and a content type.', 400)const key = await this.uploader(tabId, tx => this.poster(tx, user, tabId, conversationId, asAgentId).then(p => p.actor))const row = await this.keep(attachments.store, attachments.prefix, tabId, key, { name: name.data, conversationId },- async () => checkedType(mime.data, file.body), file.body, MAX_FORM_ATTACHMENT_BYTES)- return { attachment: await this.linkedFile(tabId, { id: row.id, name: row.name, mime: row.mime, size: row.size, url: publicUrl(attachments.publicBase, row.objectKey) }) }+ async () => checkedType(mime.data, file.body), file.body, MAX_FORM_ATTACHMENT_BYTES, (objectKey, bytes) => this.pictureOf(attachments, objectKey, bytes))+ return { attachment: await this.linkedFile(tabId, { id: row.id, name: row.name, mime: row.mime, size: row.size, url: publicUrl(attachments.publicBase, row.objectKey),+ ...pictureFields(row.image) }) }}/** A link to put one file of `size` bytes straight into the bucket, for a file too big for this server's form route. The@@ -2266,9 +2268,9 @@ export class SuperTabStore {await this.poster(tx, user, tabId, found!.conversationId, asAgentId)return found!})- const sendable = async (mime: string) => ({ attachment: await this.linkedFile(tabId, { id: row.id, name: row.name, mime, size: row.size,- url: publicUrl(attachments.publicBase, row.objectKey) }) })- if (!row.purpose) return sendable(row.mime)+ const sendable = async (mime: string, image: unknown) => ({ attachment: await this.linkedFile(tabId, { id: row.id, name: row.name, mime, size: row.size,+ url: publicUrl(attachments.publicBase, row.objectKey), ...pictureFields(image) }) })+ if (!row.purpose) return sendable(row.mime, row.image)let stored: { size: number } | null, head: Buffertry {stored = await direct.stored(row.objectKey)@@ -2284,11 +2286,23 @@ export class SuperTabStore {unavailable()}const mime = typeFromHead(row.mime, head!)- const finished = await this.db.superTabAttachment.updateMany({ where: { id: row.id, tabId, purpose: PENDING_UPLOAD_PURPOSE }, data: { purpose: { unset: true }, mime } })- if (finished.count === 1) return sendable(mime)+ // A picture sent straight to the bucket is read back once for its thumbnail, up to a size; any failure only means none.+ let image: StoredImage | null = null+ const place = THUMBNAILED_TYPES.has(mime) && stored!.size <= MAX_READ_BACK_BYTES ? thumbnailsReserve() : null+ if (place) {+ try { image = await this.pictureOf(attachments, row.objectKey, await direct.read(row.objectKey)) } catch (error) {+ logger.warn('super-tab upload thumbnail failed', { tabId, code: errorCode(error) })+ } finally { place() }+ }+ const finished = await this.db.superTabAttachment.updateMany({ where: { id: row.id, tabId, purpose: PENDING_UPLOAD_PURPOSE },+ data: { purpose: { unset: true }, mime, ...image ? { image: image as unknown as Prisma.InputJsonValue } : {} } })+ if (finished.count === 1) return sendable(mime, image)// Another finish of the same upload won: the same answer. The sweep claimed it first: its file is going, so it is refused.const now = await this.db.superTabAttachment.findFirst({ where: { id: row.id, tabId } })- return now && !now.purpose && !now.usedBy ? sendable(now.mime) : unavailable()+ if (now && !now.purpose && !now.usedBy) return sendable(now.mime, now.image)+ // The row will not carry the thumbnail just stored, so nothing would ever remove it: it goes now.+ if (image?.thumb) await attachments.store.delete(thumbKeyOf(row.objectKey)).catch(() => {})+ return unavailable()}/**@@ -2424,14 +2438,26 @@ export class SuperTabStore {/** Streams one file into the bucket under a random key, as the type `typed` decides from its first bytes, and records it. */private async keep(bucket: AttachmentStore, prefix: string, tabId: string, actorKey: string, file: { name: string; conversationId: string; purpose?: string },- typed: () => Promise<{ mime: string; body: Readable }>, original: Readable, cap: number) {+ typed: () => Promise<{ mime: string; body: Readable }>, original: Readable, cap: number,+ /** Given the stored picture's bytes, its facts and stored thumbnail (pictureOf); a picture is held in memory for it, up to `cap`. */+ thumbnail?: (objectKey: string, bytes: Buffer) => Promise<StoredImage | null>) {const objectKey = objectKeyFor(prefix, tabId, file.name)let size: number, mime: string+ const held: Buffer[] = []+ // A place to hold the picture whole for its thumbnail, given back when its thumbnail is made or the upload fails.+ let place: (() => void) | null = nulltry {const checked = await typed()mime = checked.mime- size = await bucket.put(objectKey, { name: file.name, mime }, checked.body, cap)+ if (thumbnail && THUMBNAILED_TYPES.has(mime)) place = thumbnailsReserve()+ const tee = place !== null+ const body = !tee ? checked.body : Readable.from((async function* () {+ for await (const chunk of checked.body) { held.push(Buffer.from(chunk)); yield chunk }+ })())+ size = await bucket.put(objectKey, { name: file.name, mime }, body, cap)} catch (error) {+ place?.()+ held.length = 0if (error instanceof SuperTabError) throw errorif (error instanceof AttachmentTooLarge) {if (file.purpose === AVATAR_PURPOSE) fail('AVATAR_TOO_LARGE', 'An avatar can be at most 1 MB.', 413)@@ -2442,8 +2468,39 @@ export class SuperTabStore {logger.error('super-tab attachment storage failed', { tabId, error: error instanceof Error ? error.message : String(error) })fail('ATTACHMENTS_UNAVAILABLE', 'Could not store the file just now. Try again.', 503)}- return this.db.superTabAttachment.create({ data: { id: randomBytes(12).toString('hex'), tabId, conversationId: file.conversationId,- actorKey, name: file.name, mime, size, objectKey, ...(file.purpose ? { purpose: file.purpose } : {}) } })+ // After the original is stored, so a picture that will not decode is just a file. Never fails the upload.+ // The chunks go as soon as they are one buffer, so a picture waiting for its thumbnail is held once.+ const whole = thumbnail && held.length ? Buffer.concat(held) : null+ held.length = 0+ let image: StoredImage | null = null+ try { image = whole ? await thumbnail!(objectKey, whole).catch(() => null) : null } finally { place?.() }+ try {+ return await this.db.superTabAttachment.create({ data: { id: randomBytes(12).toString('hex'), tabId, conversationId: file.conversationId,+ actorKey, name: file.name, mime, size, objectKey, ...(file.purpose ? { purpose: file.purpose } : {}),+ ...image ? { image: image as unknown as Prisma.InputJsonValue } : {} } })+ } catch (error) {+ // No row will name the thumbnail, so nothing would remove it.+ if (image?.thumb) await bucket.delete(thumbKeyOf(objectKey)).catch(() => {})+ throw error+ }+ }++ /** A picture's size and its thumbnail, made and stored beside the original; null when it is not a picture this reads. A thumbnail+ * that cannot be stored leaves the size and no thumbnail. Never throws. */+ private async pictureOf(attachments: Attachments, objectKey: string, bytes: Buffer): Promise<StoredImage | null> {+ const made = await makeThumbnail(bytes)+ if (!made) return null+ const image: StoredImage = { width: made.width, height: made.height }+ if (made.thumb) {+ const key = thumbKeyOf(objectKey)+ try {+ await attachments.store.put(key, { name: 'thumbnail.webp', mime: 'image/webp' }, Readable.from([made.thumb.bytes]), made.thumb.bytes.byteLength)+ image.thumb = { url: publicUrl(attachments.publicBase, key), width: made.thumb.width, height: made.thumb.height, size: made.thumb.bytes.byteLength }+ } catch (error) {+ logger.warn('super-tab thumbnail could not be stored', { code: errorCode(error) })+ }+ }+ return image}/** The actor, once it may post in this conversation. */@@ -2471,7 +2528,8 @@ export class SuperTabStore {const voice = voiceFacts(row.voice)if (!voice && row.voiceSince) fail('VOICE_TRANSCRIBING', 'This voice message is still being transcribed. Try again in a moment.', 409)if (voice) voiceWords[row.id] = voiceTranscript(row.voice)- return { id: row.id, name: row.name, mime: row.mime, size: row.size, url: publicUrl(attachments.publicBase, row.objectKey), ...voice ? { voice } : {} }+ return { id: row.id, name: row.name, mime: row.mime, size: row.size, url: publicUrl(attachments.publicBase, row.objectKey), ...voice ? { voice } : {},+ ...pictureFields(row.image) }}) }}@@ -2505,7 +2563,8 @@ export class SuperTabStore {const heard = new Set((await messagesById(tx, state.id, command.conversationId, handed.map(r => r.usedBy!))).filter(m => !m.buddySession).map(m => m.id))handed = handed.filter(r => heard.has(r.usedBy!))}- return { attachments: [...own, ...handed].map(row => ({ id: row.id, name: row.name, mime: row.mime, size: row.size, url: publicUrl(attachments.publicBase, row.objectKey) })),+ return { attachments: [...own, ...handed].map(row => ({ id: row.id, name: row.name, mime: row.mime, size: row.size, url: publicUrl(attachments.publicBase, row.objectKey),+ ...pictureFields(row.image) })),claimed: own.map(r => r.id), proposed: handed.map(r => r.id) }}@@ -4357,9 +4416,10 @@ export class SuperTabStore {* Another account's sign-in on a computer (computerMoves.ts, #3122): each Swarm with an entry of the computer's previous* accounts moves it to [signIn]'s machine, by the store-only machine.moved run as that entry's owner. One Swarm at a time;* one that fails is logged and left to the next connect. With [write] false (the cleanup's dry run) nothing is written, and- * each entry is decided as machine.moved decides (computerMoves.ts movableEntry). The moves made, or that would be.+ * each entry is decided as the write decides it (wouldMove). The moves made, or that would be; [left] gets each entry the+ * Swarm refused to move, with its refusal's code, in both.*/- async computerSignedIn(signIn: ComputerSignIn, { write = true }: { write?: boolean } = {}): Promise<ComputerMove[]> {+ async computerSignedIn(signIn: ComputerSignIn, { write = true, left }: { write?: boolean; left?: LeftMove[] } = {}): Promise<ComputerMove[]> {const others = await otherAccountsOnComputer(this.db, signIn)if (!others.length) return []const to = await this.db.machine.findFirst({ where: { machineId: signIn.machineId, userId: signIn.ownerId, autonomousEnv: signIn.autonomousEnv, ...machineAlive },@@ -4378,25 +4438,42 @@ export class SuperTabStore {const user: AuthUser = { sub: from.userId, email: person.email, autonomousEnv: signIn.autonomousEnv as AuthUser['autonomousEnv'], role: 'user' }const move = { from: from.machineId, fromOwner: from.userId, to: signIn.machineId, toOwner: signIn.ownerId }for (const tabId of await machineSwarms(this.db, signIn.autonomousEnv, from.machineId, MACHINE_RENAME_SWARMS)) {+ const movedTo = { machineId: signIn.machineId, ownerId: signIn.ownerId }try {- if (!write) {- const swarm = await this.db.superTab.findUnique({ where: { id: tabId }, select: SWARM_ROW })- if (!swarm || swarm.deletedAt) continue- const { state } = await loadRow(this.db as unknown as Transaction, requireParts(swarm))- if (movableEntry(state, from.machineId, from.userId, { machineId: signIn.machineId, ownerId: signIn.ownerId }, this.now())) moves.push({ tabId, ...move })- continue- }- const done = await this.executeOnce(user, tabId, { operationId: `computer-move-${randomUUID()}`, command: { type: 'machine.moved', machineId: from.machineId } },- undefined, undefined, false, { movedTo: { machineId: signIn.machineId, ownerId: signIn.ownerId } })- if (done.result.moved) moves.push({ tabId, ...move })+ const moved = write+ ? (await this.executeOnce(user, tabId, { operationId: `computer-move-${randomUUID()}`, command: { type: 'machine.moved', machineId: from.machineId } },+ undefined, undefined, false, { movedTo })).result.moved === true+ : await this.wouldMove(user, tabId, from.machineId, movedTo)+ if (moved) moves.push({ tabId, ...move })} catch (error) {- logger.warn('[swarm] a computer move did not reach a Swarm', { tabId, machineId: from.machineId, code: error instanceof SuperTabError ? error.code : 'unknown' })+ const code = error instanceof SuperTabError ? error.code : 'unknown'+ left?.push({ tabId, ...move, code })+ logger.warn('[swarm] a computer move did not reach a Swarm', { tabId, machineId: from.machineId, code })}}}return moves}+ /**+ * Whether machine.moved, run by the store as [user] for [movedTo], moves [user]'s entry [machineId] in Swarm [tabId]: the+ * checks executeOnce makes for it, in its order (the Swarm loaded for [user], their access to it and their membership, the+ * move read from both account rows), then the command itself on a copy. Nothing is written and no transaction is opened, so+ * the read-only login runs it. It throws the refusal the write meets. The cleanup's dry run read the entry alone, and on prod+ * listed a move the write then refused (task #3227).+ */+ private async wouldMove(user: AuthUser, tabId: string, machineId: string, movedTo: { machineId: string; ownerId: string }): Promise<boolean> {+ const reader = this.db as unknown as Transaction+ const command: Command = { type: 'machine.moved', machineId }+ const first = await this.load(reader, user, tabId, namedFor(command))+ const { state: original } = await this.ensure(reader, first, await this.moreNamed(reader, tabId, first.state, command, user.sub))+ const actor = await this.actor(reader, user, original)+ author(original, actor)+ const context: CommandContext = { operationId: `computer-move-${randomUUID()}`, now: this.now(),+ computerMove: actor.kind === 'person' ? await this.computerMove(reader, original, actor.userId, machineId, movedTo) : undefined }+ return applyCommand(cloneData(original), actor, command, context).result.moved === true+ }+/** The move [ownerId]'s entry [machineId] makes to [to], read from both account rows in this transaction: the same computer,* two accounts, neither a Team sign-in. None otherwise, which machine.moved refuses. */private async computerMove(tx: Transaction, state: SuperTabState, ownerId: string, machineId: string, to: { machineId: string; ownerId: string }):diff --git a/backend/src/superTabs/thumbnails.test.ts b/backend/src/superTabs/thumbnails.test.tsnew file mode 100644index 000000000..d18bf57b9--- /dev/null+++ b/backend/src/superTabs/thumbnails.test.ts@@ -0,0 +1,239 @@+import { crc32 } from 'node:zlib'+import { randomBytes } from 'node:crypto'+import { describe, expect, it } from 'vitest'+import sharp from 'sharp'+import { createThumbnailer, imageFacts, makeThumbnail, MAX_THUMB_PIXELS, THUMB_LONG_SIDE, thumbKeyOf, type ImageFacts } from './thumbnails.js'++// Task #3177: the server makes a small copy of a picture when it is uploaded, so a chat draws that and not the whole file.++/** Noise, so the original is large and a thumbnail is clearly smaller. */+const noise = (width: number, height: number) => ({ raw: { width, height, channels: 3 as const }, data: randomBytes(width * height * 3) })+async function jpeg(width: number, height: number, orientation?: number) {+ const { raw, data } = noise(width, height)+ return sharp(data, { raw }).jpeg({ quality: 95 }).withMetadata(orientation ? { orientation } : {}).toBuffer()+}+/** Stripes of eight colours: a real many-colour picture whose palette a GIF encoder finds at once. GIF-encoding noise of+ * the same size takes about 3 s on a fast Mac, which put this file's 5 s test timeout at the mercy of CI load (batch #3152). */+function stripes(width: number, height: number) {+ const data = Buffer.alloc(width * height * 3)+ for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {+ const colour = ((x >> 5) + (y >> 5)) % 8, at = (y * width + x) * 3+ data[at] = (colour & 1) * 255; data[at + 1] = ((colour >> 1) & 1) * 255; data[at + 2] = ((colour >> 2) & 1) * 255+ }+ return { raw: { width, height, channels: 3 as const }, data }+}+async function png(width: number, height: number) {+ const { raw, data } = noise(width, height)+ return sharp(data, { raw }).png().toBuffer()+}++/** A PNG whose header claims [width] x [height] and holds no pixels: enough to read its size, never to decode it. */+function pngClaiming(width: number, height: number): Buffer {+ const chunk = (type: string, body: Buffer) => {+ const head = Buffer.alloc(8); head.writeUInt32BE(body.length, 0); head.write(type, 4, 'ascii')+ const crc = Buffer.alloc(4); crc.writeUInt32BE(crc32(Buffer.concat([head.subarray(4), body])), 0)+ return Buffer.concat([head, body, crc])+ }+ const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(width, 0); ihdr.writeUInt32BE(height, 4); ihdr[8] = 8; ihdr[9] = 2+ return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', ihdr), chunk('IEND', Buffer.alloc(0))])+}++describe('makeThumbnail', () => {+ it('makes a WebP of 720 px on the long side, with its size, for a large JPEG', async () => {+ const original = await jpeg(2000, 1500)+ const facts = await makeThumbnail(original)+ expect(facts).toMatchObject({ width: 2000, height: 1500, thumb: { width: THUMB_LONG_SIDE, height: 540 } })+ const meta = await sharp(facts!.thumb!.bytes).metadata()+ expect({ format: meta.format, width: meta.width, height: meta.height }).toEqual({ format: 'webp', width: 720, height: 540 })+ expect(facts!.thumb!.bytes.byteLength).toBeLessThan(original.byteLength * 0.8)+ })++ it('also thumbnails a large PNG, and a tall picture on its height', async () => {+ const wide = await makeThumbnail(await png(1600, 900))+ expect(wide?.thumb).toMatchObject({ width: 720, height: 405 })+ const tall = await makeThumbnail(await jpeg(900, 1800))+ expect(tall?.thumb).toMatchObject({ width: 360, height: 720 })+ })++ it('shows a picture as it is held: the facts and the thumbnail follow its EXIF rotation, and carry none', async () => {+ const facts = await makeThumbnail(await jpeg(1600, 800, 6))+ expect(facts).toMatchObject({ width: 800, height: 1600, thumb: { width: 360, height: 720 } })+ const meta = await sharp(facts!.thumb!.bytes).metadata()+ expect(meta.exif).toBeUndefined()+ expect(meta.orientation ?? 1).toBe(1)+ })++ it('keeps width and height but no thumbnail when a smaller copy is not worth it', async () => {+ const small = await sharp(noise(400, 300).data, { raw: noise(400, 300).raw }).webp({ quality: 80 }).toBuffer()+ const facts = await makeThumbnail(small)+ expect(facts).toEqual({ width: 400, height: 300 })+ })++ it('never enlarges', async () => {+ const facts = await makeThumbnail(await png(300, 200))+ expect(facts?.thumb ? facts.thumb.width <= 300 && facts.thumb.height <= 200 : true).toBe(true)+ })++ it('leaves an animated WebP alone, as a GIF, so it keeps moving in the chat', async () => {+ const frames = 3, width = 300, height = 200+ const animated = await sharp(randomBytes(width * height * 3 * frames), { raw: { width, height: height * frames, channels: 3, pageHeight: height } })+ .webp({ loop: 0, delay: [100, 100, 100] }).toBuffer()+ expect((await sharp(animated).metadata()).pages).toBe(3)+ expect(await makeThumbnail(animated)).toBeNull()+ })++ it('answers nothing for what is not a picture it handles', async () => {+ // A real GIF, past the thumbnail's long side, that a GIF encoder writes in milliseconds.+ const pattern = stripes(900, 900)+ const gif = await sharp(pattern.data, { raw: pattern.raw }).gif().toBuffer()+ expect(await sharp(gif).metadata()).toMatchObject({ format: 'gif', width: 900, height: 900 })+ expect(await makeThumbnail(gif)).toBeNull()+ expect(await makeThumbnail(Buffer.from('not a picture at all'))).toBeNull()+ expect(await makeThumbnail(Buffer.alloc(0))).toBeNull()+ expect(await makeThumbnail((await jpeg(800, 600)).subarray(0, 300))).toBeNull()+ expect(await makeThumbnail(Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" width="900" height="900"></svg>'))).toBeNull()+ })++ it('gives up on a picture past 64 megapixels without decoding it', async () => {+ expect(await makeThumbnail(pngClaiming(20_000, 20_000))).toBeNull()+ })++ it('never throws', async () => {+ await expect(makeThumbnail(randomBytes(5000))).resolves.toBeNull()+ await expect(makeThumbnail(pngClaiming(100, 100))).resolves.toBeNull()+ })+})++describe('what a picture may cost', () => {+ it('stops at 40 megapixels, which covers screenshots and cameras', async () => {+ expect(MAX_THUMB_PIXELS).toBe(40_000_000)+ expect(await makeThumbnail(pngClaiming(6_400, 6_400))).toBeNull()+ })++ it('answers nothing for a PNG with a good header and bad pixels, and for noise inside a JPEG', async () => {+ const png = pngClaiming(200, 200)+ const idat = Buffer.alloc(12 + 40); idat.writeUInt32BE(40, 0); idat.write('IDAT', 4, 'ascii'); randomBytes(40).copy(idat, 8)+ const corrupt = Buffer.concat([png.subarray(0, png.length - 12), idat, png.subarray(png.length - 12)])+ expect(await makeThumbnail(corrupt)).toBeNull()+ const jpg = await jpeg(800, 600)+ randomBytes(2000).copy(jpg, 1000)+ await expect(makeThumbnail(jpg)).resolves.toSatisfy(facts => facts === null || typeof facts.width === 'number')+ })+})++describe('the thumbnailer\'s limits', () => {+ const facts: ImageFacts = { width: 10, height: 10 }+ /** A decode the test finishes by hand, and how many started. */+ function slowDecode() {+ const pending: Array<() => void> = []+ const state = { started: 0, release: () => { pending.splice(0).forEach(done => done()) } }+ const decode = () => { state.started++; return new Promise<ImageFacts | null>(resolve => pending.push(() => resolve(facts))) }+ return { state, decode }+ }+ const bytes = Buffer.from('x')+ const later = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))++ it('keeps a slot until its decode ends, even when the caller gave up waiting', async () => {+ const { state, decode } = slowDecode()+ const thumbnailer = createThumbnailer({ atOnce: 1, giveUpMs: 30, maxWaiting: 10, decode })+ expect(await thumbnailer.make(bytes)).toBeNull()+ expect(state.started).toBe(1)+ // The first decode still runs: nothing else starts beside it, and a caller that gave up before its turn is never decoded.+ expect(await thumbnailer.make(bytes)).toBeNull()+ expect(state.started).toBe(1)+ state.release()+ await later(10)+ expect(state.started).toBe(1)+ const third = thumbnailer.make(bytes)+ await later(5)+ expect(state.started).toBe(2)+ state.release()+ expect(await third).toEqual(facts)+ })++ it('does at most atOnce decodes together', async () => {+ const { state, decode } = slowDecode()+ const thumbnailer = createThumbnailer({ atOnce: 2, giveUpMs: 1_000, maxWaiting: 10, decode })+ const all = [1, 2, 3, 4].map(() => thumbnailer.make(bytes))+ await later(10)+ expect(state.started).toBe(2)+ state.release()+ await later(10)+ expect(state.started).toBe(4)+ state.release()+ expect(await Promise.all(all)).toEqual([facts, facts, facts, facts])+ })++ it('answers at once with nothing when its queue is full, and says it is busy', async () => {+ const { state, decode } = slowDecode()+ const thumbnailer = createThumbnailer({ atOnce: 1, giveUpMs: 1_000, maxWaiting: 1, decode })+ const running = thumbnailer.make(bytes)+ const waiting = thumbnailer.make(bytes)+ await later(5)+ expect(thumbnailer.busy()).toBe(true)+ const started = Date.now()+ expect(await thumbnailer.make(bytes)).toBeNull()+ expect(Date.now() - started).toBeLessThan(200)+ expect(state.started).toBe(1)+ state.release(); await later(5); state.release()+ expect(await Promise.all([running, waiting])).toEqual([facts, facts])+ expect(thumbnailer.busy()).toBe(false)+ })++ it('lets only as many pictures be held as it could work on, and takes a place back when it is given up', async () => {+ const thumbnailer = createThumbnailer({ atOnce: 1, maxWaiting: 1, giveUpMs: 1_000, decode: async () => facts })+ const first = thumbnailer.reserve(), second = thumbnailer.reserve()+ expect(first).toBeTypeOf('function')+ expect(second).toBeTypeOf('function')+ expect(thumbnailer.busy()).toBe(true)+ expect(thumbnailer.reserve()).toBeNull()+ first!()+ first!()+ expect(thumbnailer.busy()).toBe(false)+ const third = thumbnailer.reserve()+ expect(third).toBeTypeOf('function')+ expect(thumbnailer.reserve()).toBeNull()+ second!(); third!()+ expect(thumbnailer.reserve()).toBeTypeOf('function')+ })++ it('still makes the thumbnail of a picture that holds its place', async () => {+ const thumbnailer = createThumbnailer({ atOnce: 1, maxWaiting: 1, giveUpMs: 1_000, decode: async () => facts })+ const held = [thumbnailer.reserve(), thumbnailer.reserve()]+ expect(thumbnailer.busy()).toBe(true)+ expect(await thumbnailer.make(bytes)).toEqual(facts)+ held.forEach(release => release?.())+ })++ it('a decode that throws is nothing, and frees its place', async () => {+ let calls = 0+ const thumbnailer = createThumbnailer({ atOnce: 1, giveUpMs: 1_000, maxWaiting: 1, decode: async () => { if (calls++ === 0) throw new Error('boom'); return facts } })+ expect(await thumbnailer.make(bytes)).toBeNull()+ expect(await thumbnailer.make(bytes)).toEqual(facts)+ })+})++describe('thumbKeyOf', () => {+ it('sits beside the original, in its folder', () => {+ expect(thumbKeyOf('super-tabs/tab1/abcd/shot.png')).toBe('super-tabs/tab1/abcd/shot.png.thumb.webp')+ })+})++describe('imageFacts', () => {+ const good = { width: 2000, height: 1500, thumb: { url: 'https://cdn.example.test/super-tabs/t/k/a.png.thumb.webp', width: 720, height: 540, size: 41_000 } }++ it('keeps what a stored row holds', () => {+ expect(imageFacts(good)).toEqual(good)+ expect(imageFacts({ width: 400, height: 300 })).toEqual({ width: 400, height: 300 })+ })++ it('drops anything that is not a size, a thumbnail address or a count', () => {+ expect(imageFacts(null)).toBeNull()+ expect(imageFacts('x')).toBeNull()+ expect(imageFacts({ width: 0, height: 10 })).toBeNull()+ expect(imageFacts({ width: 1.5, height: 10 })).toBeNull()+ expect(imageFacts({ width: 10, height: 10, thumb: { url: 5, width: 1, height: 1, size: 1 } })).toEqual({ width: 10, height: 10 })+ expect(imageFacts({ width: 10, height: 10, thumb: { ...good.thumb, size: -1 } })).toEqual({ width: 10, height: 10 })+ expect(imageFacts({ ...good, extra: 'x', thumb: { ...good.thumb, extra: 'y' } })).toEqual(good)+ })+})diff --git a/backend/src/superTabs/thumbnails.ts b/backend/src/superTabs/thumbnails.tsnew file mode 100644index 000000000..63dd9dc6f--- /dev/null+++ b/backend/src/superTabs/thumbnails.ts@@ -0,0 +1,139 @@+import sharp from 'sharp'+import { logger } from '../utils/logger.js'++/**+ * Thumbnails of chat pictures (task #3177): when a PNG, JPEG or WebP is uploaded the server keeps a small WebP copy beside it,+ * so a chat draws that and only the full-screen viewer reads the original. A GIF is left alone (a copy would stop it moving).+ * The copy's address and size travel with the file (`Attachment.thumb`); a file without one is drawn from the original, as+ * before. A failure here never fails an upload: it only means there is no thumbnail.+ */+export const THUMB_LONG_SIDE = 720+const THUMB_QUALITY = 80+/** A thumbnail is kept only when it is clearly smaller than the picture it stands for. */+const WORTH_KEEPING = 0.8+/** Past this many pixels a picture is not decoded (a small file can claim a huge canvas). Rotating a picture holds all of it in+ * memory: at 40 MP that is up to about 320 MB in the worst case (16-bit with an alpha channel), per decode at once. */+export const MAX_THUMB_PIXELS = 40_000_000+/** A caller waits no longer than this for a thumbnail; its decode may run on (libvips cannot be cancelled) and keeps its place. */+const THUMB_GIVES_UP_MS = 10_000+/** Thumbnails made at once in one process: each decodes a picture in memory. */+const AT_ONCE = 2+/** Pictures that may wait for a place; past this the next gets no thumbnail at once, so a burst of uploads piles up neither+ * memory (each waiting picture is held whole) nor time in the upload's answer. */+const MAX_WAITING = 4+/** Pictures this server thumbnails, whatever type they were declared as. */+const FORMATS: ReadonlySet<string> = new Set(['png', 'jpeg', 'webp'])++sharp.cache(false)+sharp.concurrency(1)++export interface Thumbnail { bytes: Buffer; width: number; height: number }+/** A picture's size as it is shown (EXIF rotation applied), and its thumbnail when one is worth keeping. */+export interface ImageFacts { width: number; height: number; thumb?: Thumbnail }++/** What a message keeps of a picture: its size, and where its thumbnail is (an address in the same Swarm folder). */+export interface StoredImage { width: number; height: number; thumb?: { url: string; width: number; height: number; size: number } }++/** The thumbnail of the object at [objectKey] is stored beside it: same folder, same random segment. */+export const thumbKeyOf = (objectKey: string) => `${objectKey}.thumb.webp`++const positive = (value: unknown): value is number => typeof value === 'number' && Number.isSafeInteger(value) && value > 0++export interface ThumbnailerOptions { atOnce?: number; maxWaiting?: number; giveUpMs?: number; decode?: (bytes: Buffer) => Promise<ImageFacts | null> }++/**+ * Makes thumbnails, a few at a time. A decode keeps its place until it ends, however long it takes: a caller gives up after+ * `giveUpMs` and gets nothing, but a place is freed only by its decode ending, so `atOnce` is a real limit on memory and CPU. At+ * most `maxWaiting` wait for a place; a picture arriving past that gets nothing at once (`busy` says so beforehand, so a caller+ * need not even hold the picture). A caller that gave up before its turn is never decoded.+ */+export function createThumbnailer(options: ThumbnailerOptions = {}) {+ const atOnce = options.atOnce ?? AT_ONCE, maxWaiting = options.maxWaiting ?? MAX_WAITING, giveUpMs = options.giveUpMs ?? THUMB_GIVES_UP_MS+ const decode = options.decode ?? thumbnailOf+ let running = 0, reserved = 0+ const waiting: Array<() => void> = []+ const queueFull = () => running >= atOnce && waiting.length >= maxWaiting+ /** Pictures that may be held whole for a thumbnail at once: as many as it could be working on or have waiting. */+ const capacity = atOnce + maxWaiting+ const busy = () => queueFull() || reserved >= capacity++ /** A place to hold one picture in memory until its thumbnail is made or given up, or null when every place is taken: the+ * caller then does not hold it. Given back by calling the function it returns (once; more calls do nothing). */+ function reserve(): (() => void) | null {+ if (reserved >= capacity) return null+ reserved+++ let given = false+ return () => { if (!given) { given = true; reserved-- } }+ }++ async function make(bytes: Buffer): Promise<ImageFacts | null> {+ if (!bytes.byteLength || queueFull()) return null+ let abandoned = false+ const work = (async () => {+ // A place passed on by a decode that ended is ours without `running` changing.+ if (running >= atOnce) await new Promise<void>(resolve => waiting.push(resolve)); else running+++ try { return abandoned ? null : await decode(bytes) } finally {+ const next = waiting.shift()+ if (next) next(); else running--+ }+ })().catch(error => {+ // A decoder's message can quote what it read: only that it failed is logged.+ logger.warn('[thumbnails] could not read a picture', { error: error instanceof Error ? error.name : 'unknown' })+ return null+ })+ let timer: NodeJS.Timeout | undefined+ const gaveUp = new Promise<null>(resolve => { timer = setTimeout(() => { abandoned = true; resolve(null) }, giveUpMs); timer.unref() })+ try { return await Promise.race([work, gaveUp]) } finally { clearTimeout(timer) }+ }+ return { make, busy, reserve }+}++const shared = createThumbnailer()+/** The picture's size and thumbnail, or null when [bytes] are not a PNG, JPEG or WebP this can read within its limits, or when+ * thumbnails are backed up. Never throws. */+export const makeThumbnail = (bytes: Buffer): Promise<ImageFacts | null> => shared.make(bytes)+/** A place to hold one picture for its thumbnail, or null when none is free (the picture is then not held, and gets no thumbnail).+ * The caller gives it back, whatever happens. Without this, every upload under way would hold its whole body. */+export const thumbnailsReserve = (): (() => void) | null => shared.reserve()++async function thumbnailOf(bytes: Buffer): Promise<ImageFacts | null> {+ const input = () => sharp(bytes, { limitInputPixels: MAX_THUMB_PIXELS, failOn: 'error' })+ const meta = await input().metadata()+ if (!meta.format || !FORMATS.has(meta.format) || !positive(meta.width) || !positive(meta.height)) return null+ if (meta.width * meta.height > MAX_THUMB_PIXELS) return null+ // An animated picture is left alone, as a GIF is: a thumbnail would be its first frame and stop it moving.+ if ((meta.pages ?? 1) > 1) return null+ // EXIF orientation 5 to 8 turns the picture a quarter: what it shows is the other way round.+ const turned = (meta.orientation ?? 1) >= 5+ const width = turned ? meta.height : meta.width, height = turned ? meta.width : meta.height+ const made = await input().rotate().resize({ width: THUMB_LONG_SIDE, height: THUMB_LONG_SIDE, fit: 'inside', withoutEnlargement: true })+ .webp({ quality: THUMB_QUALITY }).toBuffer({ resolveWithObject: true })+ if (made.data.byteLength >= bytes.byteLength * WORTH_KEEPING) return { width, height }+ return { width, height, thumb: { bytes: made.data, width: made.info.width, height: made.info.height } }+}++/** A stored row's or message file's picture facts, read defensively: only a size, and a thumbnail with an address and counts. */+export function imageFacts(stored: unknown): StoredImage | null {+ if (!stored || typeof stored !== 'object') return null+ const { width, height, thumb } = stored as { width?: unknown; height?: unknown; thumb?: unknown }+ if (!positive(width) || !positive(height)) return null+ const facts: StoredImage = { width, height }+ if (thumb && typeof thumb === 'object') {+ const t = thumb as { url?: unknown; width?: unknown; height?: unknown; size?: unknown }+ if (typeof t.url === 'string' && t.url && positive(t.width) && positive(t.height) && positive(t.size)) {+ facts.thumb = { url: t.url, width: t.width, height: t.height, size: t.size }+ }+ }+ return facts+}++/** The picture facts a file carries when a stored row's `image` is read for the upload's answer or a message. */+export function pictureFields(stored: unknown): { width?: number; height?: number; thumb?: { url: string; width: number; height: number; size: number } } {+ const facts = imageFacts(stored)+ return facts ? { width: facts.width, height: facts.height, ...facts.thumb ? { thumb: facts.thumb } : {} } : {}+}++/** Pictures the server reads back from the bucket for a thumbnail when they were sent straight to it. A bigger one gets none. */+export const MAX_READ_BACK_BYTES = 32 * 1024 * 1024+/** The types thumbnailed, as an upload's checked type names them. */+export const THUMBNAILED_TYPES: ReadonlySet<string> = new Set(['image/png', 'image/jpeg', 'image/webp'])diff --git a/backend/vitest.super-tabs.config.ts b/backend/vitest.super-tabs.config.tsindex 253ad6ed2..433383d52 100644--- a/backend/vitest.super-tabs.config.ts+++ b/backend/vitest.super-tabs.config.ts@@ -8,5 +8,5 @@ export default defineConfig({ test: { include: ['integration/superTabs.test.ts','integration/superTabBuddyProbes.test.ts', 'integration/superTabBuddySessions.test.ts', 'integration/superTabSwarmBuddy.test.ts', 'integration/superTabPersonalLead.test.ts', 'integration/superTabCosts.test.ts', 'integration/harnessSharesList.test.ts', 'integration/harnessSharesIndexPlan.test.ts', 'integration/atlasSnapshot.test.ts', 'integration/queryStats.test.ts','integration/superTabEvidence.test.ts', 'integration/superTabAttemptModels.test.ts', 'integration/superTabNamedReads.test.ts', 'integration/swarmSweepPlans.test.ts', 'integration/overdueDecisions.test.ts', 'integration/workerIndexPlans.test.ts', 'integration/superTabReflection.test.ts','integration/superTabMessagePages.test.ts', 'integration/superTabDeliveryCompaction.test.ts', 'integration/superTabNoticeDigest.test.ts', 'integration/superTabPmSchedule.test.ts', 'integration/superTabDigestHour.test.ts', 'integration/superTabCommandLoads.test.ts', 'integration/superTabThreads.test.ts', 'integration/superTabMachineRemovals.test.ts',- 'integration/superTabInstructions.test.ts', 'integration/superTabWorkflow.test.ts', 'integration/superTabTaskLines.test.ts', 'integration/superTabDeletion.test.ts', 'integration/communityReports.test.ts', 'integration/superTabMachineNames.test.ts', 'integration/superTabComputerMoves.test.ts', 'integration/superTabTagsClear.test.ts', 'integration/swarmAgentTombstones.test.ts', 'integration/superTabPullRequestEvents.test.ts', 'integration/superTabProposals.test.ts', 'integration/superTabRequestReads.test.ts', 'integration/superTabVoiceMessages.test.ts', 'integration/superTabProjectChatWorkers.test.ts', 'integration/prismaQueryTiming.test.ts', 'integration/secondaryReads.test.ts', 'integration/pushDevices.test.ts'], hookTimeout: 120000, testTimeout: 30000, setupFiles: ['integration/noDefaultDigest.test-support.ts', 'integration/cacheOn.test-support.ts', 'src/lib/noRedis.test-support.ts'],+ 'integration/superTabInstructions.test.ts', 'integration/superTabWorkflow.test.ts', 'integration/superTabTaskLines.test.ts', 'integration/superTabDeletion.test.ts', 'integration/communityReports.test.ts', 'integration/superTabMachineNames.test.ts', 'integration/superTabComputerMoves.test.ts', 'integration/superTabTagsClear.test.ts', 'integration/swarmAgentTombstones.test.ts', 'integration/superTabPullRequestEvents.test.ts', 'integration/superTabProposals.test.ts', 'integration/superTabRequestReads.test.ts', 'integration/superTabVoiceMessages.test.ts', 'integration/superTabThumbnails.test.ts', 'integration/superTabProjectChatWorkers.test.ts', 'integration/prismaQueryTiming.test.ts', 'integration/secondaryReads.test.ts', 'integration/pushDevices.test.ts'], hookTimeout: 120000, testTimeout: 30000, setupFiles: ['integration/noDefaultDigest.test-support.ts', 'integration/cacheOn.test-support.ts', 'src/lib/noRedis.test-support.ts'],fileParallelism: false, env: { NODE_ENV: 'test', HARNESS_CREDENTIAL_ENCRYPTION_KEY: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=' } } })diff --git a/desktop/lib/super_tabs/attachment_image.dart b/desktop/lib/super_tabs/attachment_image.dartindex 9ac36f260..b47632cfa 100644--- a/desktop/lib/super_tabs/attachment_image.dart+++ b/desktop/lib/super_tabs/attachment_image.dart@@ -36,6 +36,14 @@ class _SuperTabInlineImageState extends State<SuperTabInlineImage> {Uri? _url;bool _asking = false, _retried = false, _failed = false;+ /// [_url] is the thumbnail the server made, not the original: the chat+ /// draws that, and the original when it is missing or will not load.+ bool _thumb = false;++ /// The thumbnail that failed, whose later errors (a picture reports one+ /// more than once) say nothing about the original drawn in its place.+ Uri? _thumbFailed;+/// How long after each failed ask for a link the next one is made.static const _askAgainAfter = [Duration(seconds: 2),@@ -62,13 +70,16 @@ class _SuperTabInlineImageState extends State<SuperTabInlineImage> {}_ref = widget.file;_retried = _failed = false;+ _thumbFailed = null;_askAgain?.cancel();_waited = 0;_start();}void _start() {- _url = _ref.current;+ final thumb = _ref.currentThumb;+ _thumb = thumb != null;+ _url = thumb ?? _ref.current;// Asked after this frame: the view's fresh-link source is read then.if (_url == null) {WidgetsBinding.instance.addPostFrameCallback((_) => _ask(stale: false));@@ -103,9 +114,12 @@ class _SuperTabInlineImageState extends State<SuperTabInlineImage> {_askAgain = Timer(wait, () => _ask(stale: stale));return;}+ // A fresh answer brings the thumbnail's new link too.+ final thumb = url == null ? null : _ref.currentThumb;setState(() {_asking = false;- _url = url;+ _thumb = thumb != null;+ _url = thumb ?? url;_failed = url == null;});}@@ -118,7 +132,19 @@ class _SuperTabInlineImageState extends State<SuperTabInlineImage> {/// The picture did not load. A short-lived link may have ended under it:/// asked for once more. An older server's permanent link is not.- void _broke() {+ void _broke(Uri failed) {+ if (failed == _thumbFailed) return;+ if (_thumb) {+ // The thumbnail is missing or will not load: the original is drawn.+ _thumb = false;+ _thumbFailed = failed;+ WidgetsBinding.instance.addPostFrameCallback((_) {+ if (!mounted) return;+ setState(() => _url = _ref.current);+ if (_url == null) _ask(stale: false);+ });+ return;+ }final signed = _url?.host == kSuperTabFilesHost;if (_retried || !signed || _failed) {if (!_failed) {@@ -166,13 +192,16 @@ class _SuperTabInlineImageState extends State<SuperTabInlineImage> {borderRadius: radius,child: ExcludeSemantics(child: Image(+ // One state per address: a failed thumbnail's error must not+ // greet the original that takes its place.+ key: ValueKey(url.toString()),image: attachmentPicture(url),fit: BoxFit.contain,frameBuilder: (_, child, frame, synchronous) =>frame == null && !synchronous ? _loading() : child,errorBuilder: (_, _, _) {if (_failed) return _broken(file);- _broke();+ _broke(url);return _loading();},),diff --git a/desktop/lib/super_tabs/file_links.dart b/desktop/lib/super_tabs/file_links.dartindex 8912922fd..de7e790cf 100644--- a/desktop/lib/super_tabs/file_links.dart+++ b/desktop/lib/super_tabs/file_links.dart@@ -90,6 +90,19 @@ class SuperTabFileRef {return attachmentLink(_latest.url);}+ /// The picture's thumbnail link while it opens for more than+ /// [kSuperTabLinkMargin], else null: the chat then draws the original. Only+ /// ever a signed link, and never for a file the server could not link.+ Uri? get currentThumb {+ final thumb = _latest.thumb;+ if (thumb == null || thumb.unlinked || _latest.unlinked) return null;+ final signed = thumb.link, ends = thumb.expiresAt;+ final link = signed == null ? null : attachmentSignedLink(signed);+ if (link == null || ends == null) return null;+ final left = ends - superTabFileClock().millisecondsSinceEpoch;+ return left > kSuperTabLinkMargin.inMilliseconds ? link : null;+ }+/// Whether the message names the file only by addresses this app refuses/// to open: a signed link off [kSuperTabFilesHost] or not https, or, from/// an older server, a stored address off the attachments host. A file notdiff --git a/desktop/lib/super_tabs/model.dart b/desktop/lib/super_tabs/model.dartindex 7a034bf8d..8ffc95b9d 100644--- a/desktop/lib/super_tabs/model.dart+++ b/desktop/lib/super_tabs/model.dart@@ -1205,7 +1205,10 @@ class SuperTabAttachment {expiresAt = row['expiresAt'] is num? (row['expiresAt'] as num).toInt(): null,- voice = SuperTabVoice.read(row['voice']);+ voice = SuperTabVoice.read(row['voice']),+ width = _positive(row['width']),+ height = _positive(row['height']),+ thumb = SuperTabThumb.read(row['thumb']);/// [url] is the address the file was stored with, on the attachments CDN:/// what an older server hands out, and what stops opening once the bucket@@ -1230,6 +1233,13 @@ class SuperTabAttachment {/// server.final SuperTabVoice? voice;+ /// A picture's size as it is shown, and the small copy the server made of+ /// it at upload (backend superTabs/thumbnails.ts): a chat draws [thumb] and+ /// the full-screen viewer opens the original. Null for any other file, a+ /// picture sent before thumbnails, and from an older server.+ final int? width, height;+ final SuperTabThumb? thumb;+/// The kinds the backend serves to be seen in place; every other kind is/// served as a download.bool get isImage => const {@@ -1240,6 +1250,59 @@ class SuperTabAttachment {}.contains(mime);}+int? _positive(Object? value) =>+ value is num && value == value.toInt() && value > 0 ? value.toInt() : null;++/// The thumbnail of a picture: linked in the same read as the original, so+/// whoever may open the original may open it and nobody else.+class SuperTabThumb {+ const SuperTabThumb({+ required this.url,+ required this.width,+ required this.height,+ required this.size,+ this.link,+ this.expiresAt,+ this.unlinked = false,+ });++ /// [row] as a thumbnail, or null when any part of it does not read: the+ /// picture is then drawn from the original.+ static SuperTabThumb? read(Object? row) {+ if (row is! Map) return null;+ final width = _positive(row['width']),+ height = _positive(row['height']),+ size = _positive(row['size']);+ if (row['url'] is! String ||+ width == null ||+ height == null ||+ size == null) {+ return null;+ }+ return SuperTabThumb(+ url: row['url'] as String,+ width: width,+ height: height,+ size: size,+ link: row['link'] is String ? row['link'] as String : null,+ expiresAt: row['expiresAt'] is num+ ? (row['expiresAt'] as num).toInt()+ : null,+ unlinked: row['unlinked'] == true,+ );+ }++ final String url;+ final int width, height, size;++ /// Its short-lived signed link, as [SuperTabAttachment.link] is the original's.+ final String? link;+ final int? expiresAt;++ /// The server could not sign it this time: the original is drawn.+ final bool unlinked;+}+/// A voice message's clip, as the server read it once before it was sent:/// its words are the message's text.class SuperTabVoice {diff --git a/desktop/test/super_tab_thumbnails_test.dart b/desktop/test/super_tab_thumbnails_test.dartnew file mode 100644index 000000000..663cf1992--- /dev/null+++ b/desktop/test/super_tab_thumbnails_test.dart@@ -0,0 +1,241 @@+// Task #3177: a chat draws a picture from the small thumbnail the server made+// at upload (backend thumbnails.ts) and the full-screen viewer opens the+// original; a file without a thumbnail, or whose thumbnail cannot be had, is+// drawn from the original as before.+import 'dart:async';+import 'dart:ui' as ui;++import 'package:flutter/material.dart';+import 'package:flutter/services.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness/super_tabs/attachment_viewer.dart';+import 'package:harness/super_tabs/controller.dart';+import 'package:harness/super_tabs/file_links.dart';+import 'package:harness/super_tabs/model.dart';+import 'package:harness/super_tabs/view.dart';++import 'support/super_tab_fixture.dart';++final _now = DateTime.utc(2026, 11, 5, 9);+final _ms = _now.millisecondsSinceEpoch;++String _signed(String key, {String sig = '1'}) =>+ 'https://storage.googleapis.com/bucket/super-tabs/tab/$key?X-Goog-Signature=$sig';+const _original = 'abc/photo.jpg';+const _thumbKey = 'abc/photo.jpg.thumb.webp';+String _cdn(String key) =>+ 'https://cdn.harness.autonomous.ai/super-tabs/tab/$key';++/// A picture as a read hands it. [thumb] adds the thumbnail; [thumbLink] and+/// [thumbExpiresAt] are its signed link and end (none when [thumbUnlinked]).+Map<String, dynamic> _picture({+ bool thumb = true,+ bool thumbUnlinked = false,+ int? thumbExpiresAt,+}) => {+ 'id': 'p1',+ 'name': 'photo.jpg',+ 'mime': 'image/jpeg',+ 'size': 4000000,+ 'url': _cdn(_original),+ 'link': _signed(_original),+ 'expiresAt': _ms + 600000,+ if (thumb) ...{+ 'width': 4000,+ 'height': 3000,+ 'thumb': {+ 'url': _cdn(_thumbKey),+ 'width': 720,+ 'height': 540,+ 'size': 40000,+ if (thumbUnlinked)+ 'unlinked': true+ else ...{+ 'link': _signed(_thumbKey),+ 'expiresAt': thumbExpiresAt ?? _ms + 600000,+ },+ },+ },+};++Future<Uint8List> _png(WidgetTester tester) async =>+ (await tester.runAsync(() async {+ final recorder = ui.PictureRecorder();+ Canvas(recorder).drawRect(+ const Rect.fromLTWH(0, 0, 40, 30),+ Paint()..color = const Color(0xFF3060A0),+ );+ final image = await recorder.endRecording().toImage(40, 30);+ final data = await image.toByteData(format: ui.ImageByteFormat.png);+ return data!.buffer.asUint8List();+ }))!;++void main() {+ late List<Uri> drawn;+ late Map<String, Uint8List> pictures;+ late ImageProvider Function(Uri) image;++ setUp(() {+ drawn = [];+ pictures = {};+ image = attachmentImage;+ superTabFileClock = () => _now;+ attachmentImage = (url) {+ drawn.add(url);+ return MemoryImage(pictures[url.toString()] ?? Uint8List(3));+ };+ });+ tearDown(() {+ attachmentImage = image;+ superTabFileClock = DateTime.now;+ });++ Future<(ConversationServer, SuperTabController)> show(+ WidgetTester tester,+ Map<String, dynamic> file,+ ) async {+ final server = ConversationServer()..append('Here', attachments: [file]);+ final controller = server.controller();+ await controller.refresh();+ await tester.binding.setSurfaceSize(const Size(1100, 800));+ addTearDown(() => tester.binding.setSurfaceSize(null));+ await tester.pumpWidget(+ MaterialApp(+ home: Scaffold(+ body: SuperTabView(+ controller: controller,+ onInvite: () {},+ onAddAgent: (_) {},+ onOpenTerminal: (_) {},+ machineName: (_) => 'Test Mac',+ ),+ ),+ ),+ );+ await tester.pumpAndSettle();+ await tester.runAsync(() => Future<void>.delayed(Durations.short2));+ await tester.pumpAndSettle();+ return (server, controller);+ }++ Future<void> close(WidgetTester tester, SuperTabController c) async {+ expect(tester.takeException(), isNull);+ await tester.pumpWidget(const SizedBox());+ await c.flush();+ c.dispose();+ }++ // A widget asks for its picture on each build: what matters is which, in order.+ List<Uri> urls() => drawn.toSet().toList();++ final inline = find.byKey(const ValueKey('super-attachment-inline:p1'));++ group('the facts a message carries', () {+ test('a thumbnail and the picture\'s size are read', () {+ final file = SuperTabAttachment.fromJson(_picture());+ expect(file.width, 4000);+ expect(file.height, 3000);+ expect(file.thumb, isNotNull);+ expect(file.thumb!.link, _signed(_thumbKey));+ expect(file.thumb!.width, 720);+ expect(file.thumb!.expiresAt, _ms + 600000);+ expect(file.thumb!.unlinked, isFalse);+ });++ test('a file from an older server has none', () {+ final file = SuperTabAttachment.fromJson(_picture(thumb: false));+ expect(file.thumb, isNull);+ expect(file.width, isNull);+ });++ test('a field of the wrong type reads as absent, never a failed row', () {+ final row = _picture()+ ..['thumb'] = {'url': 5, 'width': 'wide'}+ ..['width'] = 'x';+ final file = SuperTabAttachment.fromJson(row);+ expect(file.thumb, isNull);+ expect(file.width, isNull);+ expect(file.id, 'p1');+ });+ });++ group('the chat', () {+ testWidgets('draws the thumbnail and does not fetch the original', (+ tester,+ ) async {+ pictures[_signed(_thumbKey)] = await _png(tester);+ pictures[_signed(_original)] = await _png(tester);+ final (_, c) = await show(tester, _picture());++ expect(inline, findsOneWidget);+ expect(urls(), [Uri.parse(_signed(_thumbKey))]);+ await close(tester, c);+ });++ testWidgets('opens the original full screen', (tester) async {+ pictures[_signed(_thumbKey)] = await _png(tester);+ pictures[_signed(_original)] = await _png(tester);+ final (_, c) = await show(tester, _picture());++ await tester.tap(inline);+ await tester.pumpAndSettle();+ expect(urls(), contains(Uri.parse(_signed(_original))));+ await tester.tap(find.byKey(const ValueKey('super-image-close')));+ await tester.pumpAndSettle();+ await close(tester, c);+ });++ testWidgets('draws the original when the file has no thumbnail', (+ tester,+ ) async {+ pictures[_signed(_original)] = await _png(tester);+ final (_, c) = await show(tester, _picture(thumb: false));++ expect(urls(), [Uri.parse(_signed(_original))]);+ await close(tester, c);+ });++ testWidgets('draws the original when the thumbnail could not be signed', (+ tester,+ ) async {+ pictures[_signed(_original)] = await _png(tester);+ final (_, c) = await show(tester, _picture(thumbUnlinked: true));++ expect(urls(), [Uri.parse(_signed(_original))]);+ await close(tester, c);+ });++ testWidgets(+ 'draws the original when the thumbnail\'s link is about to end',+ (tester) async {+ pictures[_signed(_original)] = await _png(tester);+ final (_, c) = await show(+ tester,+ _picture(thumbExpiresAt: _ms + 10000),+ );++ expect(urls(), [Uri.parse(_signed(_original))]);+ await close(tester, c);+ },+ );++ testWidgets('falls back to the original when the thumbnail will not load', (+ tester,+ ) async {+ // No bytes for the thumbnail: it fails to decode.+ pictures[_signed(_original)] = await _png(tester);+ final (_, c) = await show(tester, _picture());+ // The original starts loading once the thumbnail has failed: give its+ // decode the time it has in the app.+ await tester.runAsync(() => Future<void>.delayed(Durations.short2));+ await tester.pumpAndSettle();++ expect(urls(), [+ Uri.parse(_signed(_thumbKey)),+ Uri.parse(_signed(_original)),+ ]);+ expect(find.text('photo.jpg can’t be shown here.'), findsNothing);+ await close(tester, c);+ });+ });+}diff --git a/docs/plans/2026-11-05-server-image-thumbnails-plan.md b/docs/plans/2026-11-05-server-image-thumbnails-plan.mdnew file mode 100644index 000000000..b87749d8b--- /dev/null+++ b/docs/plans/2026-11-05-server-image-thumbnails-plan.md@@ -0,0 +1,117 @@+# The server makes image thumbnails at upload: plan++> Written by the Engineer per the project instructions (writing-plans, then test-driven-development task by task). No+> Execution Handoff: the Engineer implements it.++**Goal:** A chat shows a picture from a small thumbnail (about 720 px on its long side) that the server made when the+picture was uploaded, not from the full file; the full-screen viewer still opens the original. Pictures sent before keep+working. Task #3177 (Huy's speed plan, part B2), requested by Huy.++**Architecture:** When an upload is a PNG, JPEG or WebP picture, the backend reads its size and, when a smaller copy is+worth it, writes a WebP thumbnail next to the original in the bucket (same Swarm folder, same random key, `.thumb.webp`+suffix: `thumbKeyOf(objectKey)`, so nothing needs the key stored). Its facts go on the upload's row+(`SuperTabAttachment.image`: width, height, and the thumbnail's public address, width, height, size). `message.send` copies them into the message's file (`Attachment.width`, `height`, `thumb`), as it copies a+voice clip's facts today. Every read signs the thumbnail's link in the same step that signs the original's+(`fileLinks.ts forRows`), so whoever gets one gets the other, and nobody else gets either. Desktop (and the web, which is the+Desktop build) and the phone draw `thumb` in the chat and the original in the viewer; a file without `thumb`, or whose+thumbnail cannot be fetched, is drawn from the original, as now.++**Tech Stack:** TypeScript backend (vitest), `sharp` (libvips, prebuilt for the backend's node:22-bookworm image and macOS),+Dart/Flutter (Desktop and phone widget tests with the in-memory server).++**Spec:** the task's brief (#3177).++## Design++- Which files: `image/png`, `image/jpeg`, `image/webp`. GIF is left alone (a thumbnail would stop its animation).+ Whatever the declared type, only bytes that decode as a picture get facts; anything else is stored as before.+- Size: long side 720 px (`fit: inside`, never enlarged), WebP quality 80, EXIF orientation applied and metadata dropped+ (no location in a thumbnail). A thumbnail is kept only when it is under 80% of the original's bytes; otherwise the row+ keeps width and height and the chat draws the original.+- Form uploads (up to 10 MB): the bytes stream to the bucket as now and are also kept in memory for the thumbnail (they+ are capped at 10 MB already). Direct uploads (over 10 MB, straight to the bucket): on completion, a picture up to 32 MB+ is read back from the bucket for its thumbnail; a bigger one gets none.+- growth: one extra object per picture, about 50 to 150 KB; one small embedded object per row and per message file.+- concurrency: at most 2 thumbnails at once per server process, libvips on one thread each; a decode past 64 megapixels or+ 10 seconds is given up, and the upload succeeds without a thumbnail. A failure to make or store one never fails the+ upload.+- read cost: no new query or index. A read signs one more link per picture with a thumbnail, through the same per-window+ link cache (`FILE_LINK_WINDOW_MS`).+- scale: at 10x uploads, thumbnail CPU grows with picture uploads (about 50 to 300 ms each), off the event loop.+- compatibility: `width`, `height`, `thumb` are new optional fields. Older apps ignore them; newer apps read a file without+ them as before. Pictures sent before this have none and are drawn from the original.+- migration + rollback: none. Rolling back stops new thumbnails; files already sent keep theirs, which older code ignores.+- audience: a thumbnail's link is only ever made inside the read that links its original, for the same reader, and only for+ a key inside that Swarm's own folder (`FileLinks.keyOf`). There is no separate thumbnail route.+- identity: N/A, because nothing new is asked of who the reader is; the existing read decides it.+- failure: no thumbnail when the picture does not decode, is too big, or storing it fails; the chat draws the original.+- removal: every path that removes a stored file removes its thumbnail too: the unsent-file sweep, Delete Swarm's purge, and+ an upload whose row could not be written (the thumbnail goes at once). A row says it has a thumbnail (`image.thumb`), so+ a file without one costs no extra delete.+- platforms: backend (Linux container, macOS for development); Desktop on macOS, Windows, Linux and the web; phone on iOS+ and Android.++## Global Constraints++- Access to a thumbnail follows its original's exactly.+- No CDN work (a separate question for Dee and Ken).+- Swarm code stays out of `cli/src/core`. Desktop tests use a memory store and a fake sign-in.+- Phone: a picture over 10 MB stays a chip, thumbnail or not (a bigger fetch on opening is a separate decision).+- Task #3178 (B1, Tamarack) changes how Desktop caches and decodes inline pictures in the same files: this task only picks+ which link the inline picture draws (the thumbnail when there is one), keyed as its own picture (`<file id>:thumb`).++## Review Focus++- That a thumbnail's link is made only with its original's, for the same reader, inside the Swarm's folder (`fileLinks.ts`).+- Decoding untrusted pictures: the pixel and time limits, and that a failure never fails the upload.+- That every removal of a stored file removes its thumbnail.++## Tasks++### Task 1: Backend: make the thumbnail at upload++**Files:** `backend/src/superTabs/thumbnails.ts` (new), `thumbnails.test.ts` (new), `attachments.ts` (Attachment fields,+`DirectUploads.read`), `store.ts` (`keep`, `completeUpload`, `sendable`, `proposable`), `backend/prisma/schema.prisma`+(`SuperTabAttachment.image Json?`), `backend/package.json` (`sharp`).+**Interfaces:** `makeThumbnail(bytes): Promise<ImageFacts | null>`; `ImageFacts { width; height; thumb?: { bytes; width; height } }`;+`Attachment.width?`, `height?`, `thumb?: { url; width; height; size; link?; expiresAt?; unlinked? }`.++- [x] Failing tests: a 2000x1500 JPEG upload gets a thumbnail of 720x540 stored next to it, with sizes in the upload's answer+ and in the sent message; a small PNG gets width and height and no thumbnail; a GIF and a file that is not a picture+ get neither; a picture that fails to decode still uploads; a direct upload's picture gets one at completion.+- [x] Implement; run the new tests and `attachments.test.ts`.++### Task 2: Backend: the thumbnail's link follows the original's++**Files:** `backend/src/superTabs/fileLinks.ts`, its tests, the store's file-link tests.++- [x] Failing tests: a reader of the message gets both links; a person who cannot read the message gets neither (the page+ read and the fresh-link route); a `thumb` whose address is outside the Swarm's folder is dropped, never linked.+- [x] Implement.++### Task 3: Backend: removing a file removes its thumbnail++**Files:** `backend/src/lib/attachmentSweep.ts`, `backend/src/superTabs/purge.ts`, their tests.++- [x] Failing tests: the unsent-file sweep and Delete Swarm's purge remove the thumbnail object with the original.+- [x] Implement.++### Task 4: Desktop and web: the chat draws the thumbnail, the viewer the original++**Files:** `desktop/lib/super_tabs/model.dart`, `attachment_image.dart`, a test.++- [ ] Failing widget test: a file with a thumbnail is drawn in the chat from the thumbnail's link and opened in the viewer+ from the original's; a file without one is drawn from the original.+- [x] Implement.++### Task 5: Phone: the same++**Files:** `mobile/lib/swarm/model.dart`, `swarm_attachment.dart`, a test.++- [ ] Failing widget test, as Task 4.+- [x] Implement.++### Task 6: Before and after bytes++- [x] Measure original and thumbnail bytes over real pictures (the repo's screenshots and covers: 120 files, 47.8 MB to 3.0 MB, in the+ pull request), and give Huy the task #3176 steps to take the same numbers on his Mac after the release.diff --git a/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.kt b/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.ktindex c5c59282b..19f89a554 100644--- a/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.kt+++ b/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.kt@@ -156,7 +156,7 @@ class MainActivity : FlutterActivity() {}receiveLink(intent)- // `harness/push` — this phone's push address, and the pushes tapped (Dart:+ // `harness/push` — this phone's push address, the pushes tapped, and those still shown (Dart:// `lib/notify/push_channel.dart`; drawn by Android itself). `address` turns pushes on and answers// the FCM token; `forget` turns them off and deletes the token, so a phone signed out is reached// by nothing. A tapped push's data waits here until `take`, as a link does; `arrived` says more@@ -170,6 +170,13 @@ class MainActivity : FlutterActivity() {result.success(ArrayList(waitingPushes))waitingPushes.clear()}+ // The keys of our pushes still shown: the app does not post them again. The server tags each+ // push with the key the app gives its own notice for the same reply or tag.+ "shown" -> result.success(+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) emptyList()+ else getSystemService(android.app.NotificationManager::class.java).activeNotifications+ .mapNotNull { shown -> shown.tag?.takeIf { it.startsWith("reply:") || it.startsWith("mention:") } }+ )else -> result.notImplemented()}}diff --git a/mobile/ios/Runner/AppDelegate.swift b/mobile/ios/Runner/AppDelegate.swiftindex 52f307a3f..c019ecee1 100644--- a/mobile/ios/Runner/AppDelegate.swift+++ b/mobile/ios/Runner/AppDelegate.swift@@ -85,8 +85,8 @@ import UserNotifications}}-/// `harness/push` — this phone's push address, and the pushes the person tapped (Dart:-/// `lib/notify/push_channel.dart`).+/// `harness/push` — this phone's push address, the pushes the person tapped, and the pushes still shown+/// (Dart: `lib/notify/push_channel.dart`).////// `address` asks iOS for the APNs device token, which comes back to the app delegate; every call/// waiting then hears it. `forget` stops iOS delivering to it: the phone signed out. `changed` tells@@ -122,6 +122,15 @@ final class PushChannel {case "take":result(self.waiting)self.waiting.removeAll()+ case "shown":+ // The keys of our pushes still in the notification centre: the app does not post them again+ // (Dart: `PushChannel.shown`). The server keys a push as the app keys its own notice.+ UNUserNotificationCenter.current().getDeliveredNotifications { delivered in+ let keys = delivered+ .filter { $0.request.trigger is UNPushNotificationTrigger }+ .compactMap { $0.request.content.userInfo["key"] as? String }+ DispatchQueue.main.async { result(keys) }+ }default:result(FlutterMethodNotImplemented)}diff --git a/mobile/lib/notify/push_channel.dart b/mobile/lib/notify/push_channel.dartindex adba2fb9e..3d670ec8f 100644--- a/mobile/lib/notify/push_channel.dart+++ b/mobile/lib/notify/push_channel.dart@@ -55,6 +55,28 @@ class PushChannel {}}+ /// The pushes still shown is asked as the app comes back: an answer later than this is not waited for.+ static const shownTimeout = Duration(milliseconds: 500);++ /// The keys of Harness's pushes still in the notification centre. The server keys a push as the+ /// app keys its own notice for the same reply or tag (`backend/src/push/notices.ts`), so the app+ /// does not post it a second time ([SwarmTold.addAll]). Empty where it cannot tell.+ Future<Set<String>> shown() async {+ try {+ final keys = await _channel+ .invokeListMethod<Object?>('shown')+ .timeout(shownTimeout);+ return {...?keys?.whereType<String>()};+ } on MissingPluginException {+ return const {};+ } on PlatformException catch (error) {+ appLog.warn('push', 'pushes shown not read: ${error.code}');+ return const {};+ } on TimeoutException {+ return const {};+ }+ }+/// Hears the pushes tapped ([onOpened], each with its data) and new addresses ([onChanged]) until/// [close]. Takes what already waits, such as the push that launched the app.void listen({diff --git a/mobile/lib/phone/phone_shell.dart b/mobile/lib/phone/phone_shell.dartindex 999910611..a49e6ade3 100644--- a/mobile/lib/phone/phone_shell.dart+++ b/mobile/lib/phone/phone_shell.dart@@ -127,7 +127,7 @@ class _PhoneShellState extends State<PhoneShell> with WidgetsBindingObserver {voice: VoiceInputScope.maybeOf(context) == null? MicVoiceRecorder.new: null,- )..foreground = state == null || _inFront(state);+ )..foreground = _swarmFront = state == null || _inFront(state);_swarmFiles = SwarmAttachmentKeeper(_swarm);_swarmNotices.openedSwarm.addListener(_openNoticedSwarm);// A message link (Desktop's Copy link to message): one that launched the@@ -429,6 +429,18 @@ class _PhoneShellState extends State<PhoneShell> with WidgetsBindingObserver {}/// In front of the person: resumed, or only covered for a moment (the app switcher, a call).+ /// Whether Swarm was last told the app is in front ([didChangeAppLifecycleState]).+ bool _swarmFront = true;++ /// Back in front: what pushes showed while the app was away is told already+ /// (task #2905), so the reads that start now post none of it again.+ Future<void> _swarmToFront() async {+ final shown = await widget.notifier.pushesShown();+ if (!mounted || !_swarmFront) return;+ _swarm.toldElsewhere(shown);+ _swarm.foreground = true;+ }+static bool _inFront(AppLifecycleState state) =>state == AppLifecycleState.resumed || state == AppLifecycleState.inactive;@@ -459,7 +471,13 @@ class _PhoneShellState extends State<PhoneShell> with WidgetsBindingObserver {@overridevoid didChangeAppLifecycleState(AppLifecycleState state) {// Swarm is read only in front of the person.- _swarm.foreground = _inFront(state);+ if (!_inFront(state)) {+ _swarmFront = false;+ _swarm.foreground = false;+ } else if (!_swarmFront) {+ _swarmFront = true;+ unawaited(_swarmToFront());+ }if (state == AppLifecycleState.inactive) {// The app switcher coming up is `inactive`, and a swipe there can end the app without it// ever reaching `paused` — so the machine cache is written here as well. Seediff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dartindex b0c8c82a3..914e1e08c 100644--- a/mobile/lib/state/app_state.dart+++ b/mobile/lib/state/app_state.dart@@ -3640,6 +3640,11 @@ class AppNotifier extends ChangeNotifier {_pushLifecycle = AppLifecycleListener(onResume: _syncPush);}+ /// The keys of this app's pushes still in the notification centre (notify/push_channel.dart):+ /// none where there is no push.+ Future<Set<String>> pushesShown() async =>+ await _push?.channel.shown() ?? const {};+/// Tells the backend where to push to this phone while signed in: once the account is known, when/// notices are allowed, when the OS hands over a new address, and back in the app.void _syncPush() {diff --git a/mobile/lib/swarm/model.dart b/mobile/lib/swarm/model.dartindex 4e1cbb22f..5f04955a8 100644--- a/mobile/lib/swarm/model.dart+++ b/mobile/lib/swarm/model.dart@@ -1171,7 +1171,10 @@ class SuperTabAttachment {expiresAt = row['expiresAt'] is num? (row['expiresAt'] as num).toInt(): null,- voice = SuperTabVoice.read(row['voice']);+ voice = SuperTabVoice.read(row['voice']),+ width = _positive(row['width']),+ height = _positive(row['height']),+ thumb = SuperTabThumb.read(row['thumb']);/// [url] is the address the file was stored with, on the attachments CDN:/// what an older server hands out, and what stops opening once the bucket@@ -1195,6 +1198,13 @@ class SuperTabAttachment {/// (swarm_voice_bubble.dart); null for any other file.final SuperTabVoice? voice;+ /// A picture's size as it is shown, and the small copy the backend made of+ /// it at upload (superTabs/thumbnails.ts): the chat draws [thumb] and the+ /// full-screen viewer opens the original. Null for any other file, a picture+ /// sent before thumbnails, and from an older backend.+ final int? width, height;+ final SuperTabThumb? thumb;+/// The kinds the backend serves to be seen in place; every other kind is/// served as a download.bool get isImage => const {@@ -1205,6 +1215,59 @@ class SuperTabAttachment {}.contains(mime);}+int? _positive(Object? value) =>+ value is num && value == value.toInt() && value > 0 ? value.toInt() : null;++/// The thumbnail of a picture: linked in the same read as the original, so+/// whoever may open the original may open it and nobody else.+class SuperTabThumb {+ const SuperTabThumb({+ required this.url,+ required this.width,+ required this.height,+ required this.size,+ this.link,+ this.expiresAt,+ this.unlinked = false,+ });++ /// [row] as a thumbnail, or null when any part of it does not read: the+ /// picture is then drawn from the original.+ static SuperTabThumb? read(Object? row) {+ if (row is! Map) return null;+ final width = _positive(row['width']),+ height = _positive(row['height']),+ size = _positive(row['size']);+ if (row['url'] is! String ||+ width == null ||+ height == null ||+ size == null) {+ return null;+ }+ return SuperTabThumb(+ url: row['url'] as String,+ width: width,+ height: height,+ size: size,+ link: row['link'] is String ? row['link'] as String : null,+ expiresAt: row['expiresAt'] is num+ ? (row['expiresAt'] as num).toInt()+ : null,+ unlinked: row['unlinked'] == true,+ );+ }++ final String url;+ final int width, height, size;++ /// Its short-lived signed link, as [SuperTabAttachment.link] is the original's.+ final String? link;+ final int? expiresAt;++ /// The backend could not sign it this time: the original is drawn.+ final bool unlinked;+}+/// A voice message's clip, as the backend read it before it was sent/// (backend superTabs/voiceMessages.ts): its words are the message's text.class SuperTabVoice {diff --git a/mobile/lib/swarm/swarm_attachment.dart b/mobile/lib/swarm/swarm_attachment.dartindex 5b5c0ef1d..ed44d8ed4 100644--- a/mobile/lib/swarm/swarm_attachment.dart+++ b/mobile/lib/swarm/swarm_attachment.dart@@ -28,12 +28,18 @@ class SwarmAttachment extends StatelessWidget {required this.file,required this.link,required this.chip,+ this.thumb,this.store,this.fileRef,});final SuperTabAttachment file;+ /// [file]'s thumbnail link, when it has one that opens now+ /// ([SwarmFileRef.currentThumb]): the chat draws that, and the original+ /// only when the picture is opened full screen.+ final Uri? thumb;+/// [file]'s link, when it is on Harness's file storage — the only place a/// picture is fetched from, as the chip only opens a link from there.final Uri? link;@@ -58,6 +64,7 @@ class SwarmAttachment extends StatelessWidget {return _Preview(file: file,url: link,+ thumb: thumb,fileRef: ref,chip: chip,store: store ?? swarmAttachments,@@ -100,6 +107,7 @@ class _Preview extends StatefulWidget {const _Preview({required this.file,required this.url,+ required this.thumb,required this.fileRef,required this.chip,required this.store,@@ -109,6 +117,9 @@ class _Preview extends StatefulWidget {/// The link at hand; null when a fresh one is asked of [fileRef] first.final Uri? url;++ /// The thumbnail's link at hand, drawn in the chat in place of [url].+ final Uri? thumb;final SwarmFileRef? fileRef;final Widget chip;final SwarmAttachmentStore store;@@ -128,6 +139,10 @@ class _PreviewState extends State<_Preview> {/// The picture could not be fetched or decoded: the chip, which still opens it.bool _failed = false;+ /// What [_local] holds is the thumbnail, not the original: the viewer then+ /// fetches the original when the picture is opened.+ bool _thumbShown = false;+@overridevoid initState() {super.initState();@@ -139,6 +154,7 @@ class _PreviewState extends State<_Preview> {void didUpdateWidget(_Preview old) {super.didUpdateWidget(old);if (old.url == widget.url &&+ old.thumb == widget.thumb &&identical(old.store, widget.store) &&identical(old.fileRef?.reader, widget.fileRef?.reader)) {return;@@ -156,15 +172,35 @@ class _PreviewState extends State<_Preview> {void _ask() {_turn = widget.store.turns.value;- _local = swarmKeptFile(- widget.store,- widget.file,- at: widget.url,- ref: widget.fileRef,- );+ _thumbShown = false;+ final thumb = widget.thumb;+ _local = thumb == null ? _original() : _thumbnailOrOriginal(thumb);_failed = false;}+ Future<File> _original() => swarmKeptFile(+ widget.store,+ widget.file,+ at: widget.url,+ ref: widget.fileRef,+ );++ /// The thumbnail, kept apart from the original (their addresses and so their+ /// links differ); the original when the thumbnail cannot be had.+ Future<File> _thumbnailOrOriginal(Uri thumb) async {+ try {+ final file = await widget.store.file(+ thumb,+ key: 'swarm-file:${widget.file.id}:thumb',+ );+ _thumbShown = true;+ return file;+ } catch (_) {+ _thumbShown = false;+ return _original();+ }+ }+/// The files kept changed hands: the picture is asked for again, for/// whoever they are kept for now.void _turned() {@@ -260,6 +296,22 @@ class _PreviewState extends State<_Preview> {void _open(BuildContext context, File file) {final turn = _turn;if (turn != widget.store.turns.value) return;+ if (!_thumbShown) return _push(context, file, turn);+ unawaited(_openOriginal(context, file, turn));+ }++ /// The viewer shows the original: fetched now, as the thumbnail in the chat is+ /// not what a pinch should zoom into. If it cannot be had, the thumbnail opens.+ Future<void> _openOriginal(BuildContext context, File thumb, int turn) async {+ File shown = thumb;+ try {+ shown = await _original();+ } catch (_) {}+ if (!context.mounted || turn != widget.store.turns.value) return;+ _push(context, shown, turn);+ }++ void _push(BuildContext context, File file, int turn) {final name = widget.file.name;final store = widget.store;unawaited(diff --git a/mobile/lib/swarm/swarm_file_links.dart b/mobile/lib/swarm/swarm_file_links.dartindex 55049e7ac..30828209d 100644--- a/mobile/lib/swarm/swarm_file_links.dart+++ b/mobile/lib/swarm/swarm_file_links.dart@@ -121,6 +121,19 @@ class SwarmFileRef {return swarmAttachmentLink(_latest.url);}+ /// The picture's thumbnail link while it opens for more than+ /// [swarmLinkMargin], else null: the chat then draws the original. Only ever+ /// a signed link, and never for a file the backend could not link.+ Uri? get currentThumb {+ final thumb = _latest.thumb;+ if (thumb == null || thumb.unlinked || _latest.unlinked) return null;+ final signed = thumb.link, ends = thumb.expiresAt;+ final link = signed == null ? null : swarmSignedLink(signed);+ if (link == null || ends == null) return null;+ final left = ends - swarmFileClock().millisecondsSinceEpoch;+ return left > swarmLinkMargin.inMilliseconds ? link : null;+ }+/// Whether the message names the file only by addresses this app refuses.bool get refused => file.link != null? swarmSignedLink(file.link) == nulldiff --git a/mobile/lib/swarm/swarm_message_row.dart b/mobile/lib/swarm/swarm_message_row.dartindex 1edd45f43..0bc10c01b 100644--- a/mobile/lib/swarm/swarm_message_row.dart+++ b/mobile/lib/swarm/swarm_message_row.dart@@ -401,6 +401,7 @@ class SwarmMessageRow extends StatelessWidget {return SwarmAttachment(file: file,link: link,+ thumb: ref.currentThumb,fileRef: ref,chip: SwarmFileChip(file: file, link: link, fileRef: ref),);diff --git a/mobile/lib/swarm/swarm_notices.dart b/mobile/lib/swarm/swarm_notices.dartindex 6ba320374..479daa526 100644--- a/mobile/lib/swarm/swarm_notices.dart+++ b/mobile/lib/swarm/swarm_notices.dart@@ -303,6 +303,10 @@ class SwarmTold {return true;}+ /// Records [keys] as told somewhere else: pushes already in the+ /// notification centre (task #2905).+ void addAll(Iterable<String> keys) => keys.forEach(add);+/// Whose notices these are: another account, or none (Swarm turned off),/// forgets every key.set account(String? id) {diff --git a/mobile/lib/swarm/swarm_session.dart b/mobile/lib/swarm/swarm_session.dartindex c6278cf3c..93bbf7b35 100644--- a/mobile/lib/swarm/swarm_session.dart+++ b/mobile/lib/swarm/swarm_session.dart@@ -446,6 +446,10 @@ class SwarmHost extends ChangeNotifier {session?.foreground = value;}+ /// Notices the person was already shown by a push while the app was away:+ /// never posted again ([SwarmTold.addAll]).+ void toldElsewhere(Iterable<String> keys) => _told.addAll(keys);+/// A computer may have come up: a Swarm waiting for one reads at once.void _computersChanged() {session?.inbox.computerUp();diff --git a/mobile/test/notify/push_channel_test.dart b/mobile/test/notify/push_channel_test.dartindex 0aea64a3f..46c87adbc 100644--- a/mobile/test/notify/push_channel_test.dart+++ b/mobile/test/notify/push_channel_test.dart@@ -151,4 +151,20 @@ void main() {expect(waiting, isNotEmpty);});});++ group('the pushes still in the notification centre (task #2905)', () {+ test('are their keys, so the app does not post them again', () async {+ answer = (call) => call.method == 'shown'+ ? ['reply:tab:row1', 'mention:t1', 7, null]+ : null;+ expect(await PushChannel().shown(), {'reply:tab:row1', 'mention:t1'});+ });++ test('are none where the native side is missing or fails', () async {+ answer = (call) => throw PlatformException(code: 'shown');+ expect(await PushChannel().shown(), isEmpty);+ messenger.setMockMethodCallHandler(method, null);+ expect(await PushChannel().shown(), isEmpty);+ });+ });}diff --git a/mobile/test/swarm/swarm_attachment_fakes.dart b/mobile/test/swarm/swarm_attachment_fakes.dartindex be6d49904..cb838a5a4 100644--- a/mobile/test/swarm/swarm_attachment_fakes.dart+++ b/mobile/test/swarm/swarm_attachment_fakes.dart@@ -29,6 +29,9 @@ class FakeFileStorage implements FileService {/// The status every answer carries.int status = 200;+ /// A status for one address, over [status].+ final statusFor = <String, int>{};+/// What every answer carries.List<int> bytes = pixelPng;@@ -39,7 +42,7 @@ class FakeFileStorage implements FileService {}) async {asked.add((url: url, headers: headers));await hold?.future;- return _Answer(status, bytes);+ return _Answer(statusFor[url] ?? status, bytes);}}diff --git a/mobile/test/swarm/swarm_notices_test.dart b/mobile/test/swarm/swarm_notices_test.dartindex 126a42b2b..258aea459 100644--- a/mobile/test/swarm/swarm_notices_test.dart+++ b/mobile/test/swarm/swarm_notices_test.dart@@ -285,6 +285,34 @@ void main() {expect(notices.shown[0].key, isNot(notices.shown[1].key));});+ test('is not posted again when its push is already in the notification '+ 'centre (task #2905)', () {+ final notices = _Recording();+ final told = SwarmTold();+ // The server keys a push as the phone keys its notice: the reply's+ // Activity row, the tag's id.+ told.addAll(['reply:tab-b:a1', 'mention:m1']);+ final announcer = SwarmAnnouncer(+ notices,+ quiet: (_) => false,+ told: told,+ );+ announcer.announceReply(+ swarmId: 'tab-b',+ entry: reply('a1'),+ project: 'M',+ );+ announcer.announce(mention());+ expect(notices.shown, isEmpty);++ announcer.announceReply(+ swarmId: 'tab-b',+ entry: reply('a2'),+ project: 'M',+ );+ expect(notices.shown.single.key, 'reply:tab-b:a2');+ });+test('is not posted while the person looks at the Swarm', () {final notices = _Recording();final asked = <SwarmNeed?>[];diff --git a/mobile/test/swarm/swarm_thumbnails_test.dart b/mobile/test/swarm/swarm_thumbnails_test.dartnew file mode 100644index 000000000..2d8535ee1--- /dev/null+++ b/mobile/test/swarm/swarm_thumbnails_test.dart@@ -0,0 +1,270 @@+import 'dart:io';+import 'dart:ui' as ui;++import 'package:flutter/material.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/swarm/model.dart';+import 'package:harness_mobile/swarm/swarm_attachment.dart';+import 'package:harness_mobile/swarm/swarm_attachment_store.dart';+import 'package:harness_mobile/swarm/swarm_file_links.dart';++import '../temp_folder.dart';+import 'picture_loads.dart';+import 'swarm_attachment_fakes.dart';++// Task #3177: a chat draws a picture from the small thumbnail the server made at upload (backend thumbnails.ts) and the full+// screen viewer opens the original; a picture without a thumbnail, or whose thumbnail cannot be fetched, is drawn from the+// original as before.+void main() {+ late Directory root;+ late FakeFileStorage storage;+ late PictureReads reads;+ late SwarmAttachmentStore store;++ final original = Uri.parse('https://cdn.example/swarm/photo.jpg');+ final thumb = Uri.parse('https://cdn.example/swarm/photo.jpg.thumb.webp');++ Map<String, dynamic> row({bool withThumb = true}) => {+ 'id': 'file-1',+ 'name': 'photo.jpg',+ 'mime': 'image/jpeg',+ 'size': 4000000,+ 'url': original.toString(),+ if (withThumb) ...{+ 'width': 4000,+ 'height': 3000,+ 'thumb': {+ 'url': thumb.toString(),+ 'width': 720,+ 'height': 540,+ 'size': 40000,+ 'link': thumb.toString(),+ 'expiresAt': 1,+ },+ },+ };++ setUp(() {+ root = Directory.systemTemp.createTempSync('swarm-thumbnails-');+ storage = FakeFileStorage();+ reads = PictureReads();+ store = storeIn(root, storage, reads: reads);+ });+ tearDown(() => deleteTempFolder(root));++ final preview = find.byKey(const ValueKey('swarm-attachment-preview-file-1'));+ final viewer = find.byKey(const ValueKey('swarm-attachment-viewer'));++ Future<void> pump(+ WidgetTester tester, {+ bool withThumb = true,+ bool useThumb = true,+ }) async {+ tester.view.devicePixelRatio = 2;+ addTearDown(tester.view.reset);+ await tester.runAsync(() => store.keepOnly('person-me'));+ await tester.pumpWidget(+ MaterialApp(+ home: Scaffold(+ body: SizedBox(+ width: 320,+ child: SwarmAttachment(+ file: SuperTabAttachment.fromJson(row(withThumb: withThumb)),+ link: original,+ thumb: withThumb && useThumb ? thumb : null,+ chip: const Text('chip', key: ValueKey('chip')),+ store: store,+ ),+ ),+ ),+ ),+ );+ }++ Future<void> until(WidgetTester tester, bool Function() done) async {+ for (var i = 0; i < 100 && !done(); i++) {+ await tester.runAsync(+ () => Future<void>.delayed(const Duration(milliseconds: 10)),+ );+ await tester.pump();+ }+ expect(done(), isTrue);+ }++ bool drawn(WidgetTester tester) => find+ .descendant(of: preview, matching: find.byType(RawImage))+ .evaluate()+ .any((e) => (e.widget as RawImage).image != null);++ Future<List<int>> png(WidgetTester tester) async =>+ (await tester.runAsync(() async {+ final recorder = ui.PictureRecorder();+ Canvas(recorder).drawRect(+ const Rect.fromLTWH(0, 0, 40, 30),+ Paint()..color = const Color(0xFF3060A0),+ );+ final image = await recorder.endRecording().toImage(40, 30);+ return (await image.toByteData(format: ui.ImageByteFormat.png))!.buffer+ .asUint8List();+ }))!;++ Future<void> closed(WidgetTester tester) async {+ await picturesLoaded(tester);+ await tester.pumpWidget(const SizedBox());+ await tester.pump(const Duration(minutes: 1));+ }++ List<String> asked() => [for (final a in storage.asked) a.url];++ group('the facts a message carries', () {+ test('a thumbnail and the picture\'s size are read', () {+ final file = SuperTabAttachment.fromJson(row());+ expect((file.width, file.height), (4000, 3000));+ expect(file.thumb?.url, thumb.toString());+ expect(+ (file.thumb?.width, file.thumb?.height, file.thumb?.size),+ (720, 540, 40000),+ );+ expect(file.thumb?.expiresAt, 1);+ });++ test('a file from an older server has none', () {+ final file = SuperTabAttachment.fromJson(row(withThumb: false));+ expect(file.thumb, isNull);+ expect(file.width, isNull);+ });++ test('a field of the wrong type reads as absent, never a failed row', () {+ final file = SuperTabAttachment.fromJson({+ ...row(),+ 'thumb': {'url': 5, 'width': 'wide'},+ 'width': 'x',+ });+ expect(file.thumb, isNull);+ expect(file.width, isNull);+ expect(file.id, 'file-1');+ });+ });++ group('the file ref', () {+ final t = DateTime.utc(2026, 11, 5, 9);+ setUp(() => swarmFileClock = () => t);+ tearDown(() => swarmFileClock = DateTime.now);++ SwarmFileRef ref(Map<String, dynamic> thumbRow, {bool unlinked = false}) =>+ SwarmFileRef(+ SuperTabAttachment.fromJson({+ ...row(),+ 'link': 'https://storage.googleapis.com/b/p.jpg?s=1',+ 'expiresAt': t.millisecondsSinceEpoch + 600000,+ if (unlinked) 'unlinked': true,+ 'thumb': {+ 'url': thumb.toString(),+ 'width': 720,+ 'height': 540,+ 'size': 4,+ ...thumbRow,+ },+ }),+ );++ test('hands the thumbnail\'s signed link while it opens for more than a minute', () {+ final link = ref({+ 'link': 'https://storage.googleapis.com/b/p.jpg.thumb.webp?s=1',+ 'expiresAt': t.millisecondsSinceEpoch + 600000,+ }).currentThumb;+ expect(link?.path, '/b/p.jpg.thumb.webp');+ });++ test('hands none for one about to end, unsigned, unlinked or off the files host', () {+ expect(+ ref({+ 'link': 'https://storage.googleapis.com/b/t.webp?s=1',+ 'expiresAt': t.millisecondsSinceEpoch + 10000,+ }).currentThumb,+ isNull,+ );+ expect(ref({}).currentThumb, isNull);+ expect(ref({'unlinked': true}).currentThumb, isNull);+ expect(+ ref({+ 'link': 'https://evil.example/t.webp',+ 'expiresAt': t.millisecondsSinceEpoch + 600000,+ }).currentThumb,+ isNull,+ );+ expect(+ ref({+ 'link': 'https://storage.googleapis.com/b/t.webp?s=1',+ 'expiresAt': t.millisecondsSinceEpoch + 600000,+ }, unlinked: true).currentThumb,+ isNull,+ );+ });+ });++ group('the chat', () {+ testWidgets('draws the thumbnail and fetches only that', (tester) async {+ storage.bytes = await png(tester);+ await pump(tester);+ await until(tester, () => drawn(tester));++ expect(asked(), [thumb.toString()]);+ await closed(tester);+ });++ testWidgets(+ 'fetches the original when the picture is opened, and shows that',+ (tester) async {+ storage.bytes = await png(tester);+ await pump(tester);+ await until(tester, () => drawn(tester));++ await tester.tap(preview);+ await until(tester, () => viewer.evaluate().isNotEmpty);++ // Kept apart: the thumbnail's cache entry is not the original's.+ expect(asked(), [thumb.toString(), original.toString()]);+ await picturesLoaded(tester);+ await tester.pumpWidget(const SizedBox());+ await tester.pump(const Duration(minutes: 1));+ },+ );++ testWidgets('draws the original when the file has no thumbnail', (+ tester,+ ) async {+ storage.bytes = await png(tester);+ await pump(tester, withThumb: false);+ await until(tester, () => drawn(tester));++ expect(asked(), [original.toString()]);+ await closed(tester);+ });++ testWidgets('draws the original when no thumbnail link is at hand', (+ tester,+ ) async {+ storage.bytes = await png(tester);+ await pump(tester, useThumb: false);+ await until(tester, () => drawn(tester));++ expect(asked(), [original.toString()]);+ await closed(tester);+ });++ testWidgets(+ 'falls back to the original when the thumbnail cannot be fetched',+ (tester) async {+ storage.bytes = await png(tester);+ storage.statusFor[thumb.toString()] = 404;+ await pump(tester);+ await until(tester, () => drawn(tester));++ expect(asked(), [thumb.toString(), original.toString()]);+ expect(find.byKey(const ValueKey('chip')), findsNothing);+ await closed(tester);+ },+ );+ });+}