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-04 b1346: #2887 #2919 #2856 #2920 (#2942)
0xken153c08e553f171
diff --git a/backend/integration/superTabNotAvailable.test.ts b/backend/integration/superTabNotAvailable.test.tsindex aaee418c2..7251352d2 100644--- a/backend/integration/superTabNotAvailable.test.ts+++ b/backend/integration/superTabNotAvailable.test.ts@@ -123,6 +123,7 @@ function calls(tab: string, chat: string, project: string) {json('GET', 'GET /api/super-tabs/:tabId/projects/:projectId/instructions/revisions/:revision', `${t}/projects/${project}/instructions/revisions/1`),json('POST', 'POST /api/super-tabs/:tabId/attachments/uploads', `${t}/attachments/uploads`, { conversationId: chat, name: 'notes.txt', mime: 'text/plain', size: 12_000_000 }),json('POST', 'POST /api/super-tabs/:tabId/attachments/uploads/:uploadId/complete', `${t}/attachments/uploads/${MISSING}/complete`, {}),+ json('POST', 'POST /api/super-tabs/:tabId/attachments/:attachmentId/voice', `${t}/attachments/${MISSING}/voice?lang=en`, {}),json('GET', 'GET /api/super-tabs/:tabId/projects/:projectId/github', `${t}/projects/${project}/github`),json('POST', 'POST /api/super-tabs/:tabId/projects/:projectId/github/approve', `${t}/projects/${project}/github/approve`, { scope: 'project' }),json('GET', 'GET /api/super-tabs/:tabId/projects/:projectId/roles/:roleId', `${t}/projects/${project}/roles/${MISSING}`),diff --git a/backend/integration/superTabVoiceMessages.test.ts b/backend/integration/superTabVoiceMessages.test.tsnew file mode 100644index 000000000..b84d6b336--- /dev/null+++ b/backend/integration/superTabVoiceMessages.test.ts@@ -0,0 +1,232 @@+import { 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 type { AuthUser } from '../src/lib/ssoAuth.js'+import { SuperTabStore, type VoiceTranscription } from '../src/superTabs/store.js'+import { MemoryAttachmentStore } from '../src/superTabs/attachments.js'+import { logger } from '../src/utils/logger.js'+import { FIELDS, preview, row } from '../src/superTabs/read/inbox.js'+import { readBy } from '../src/superTabs/parts/indexed.js'++// Chat voice messages (task #2584): the phone uploads a WAV, the server transcribes it once before it is sent, and the words+// become the message's text, so agents read them in the delivery written with the message.+let replica: MongoMemoryReplSet+let db: PrismaClient+const owner: AuthUser = { sub: '230000000000000000000001', email: '[email protected]', autonomousEnv: 'prod', role: 'user' }+const peer: AuthUser = { sub: '230000000000000000000002', 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_voice_messages_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()+})++/** 16 kHz mono 16-bit PCM WAV, [seconds] long, loud. */+function wav(seconds: number): Buffer {+ const samples = Math.round(seconds * 16_000)+ const out = Buffer.alloc(44 + samples * 2)+ out.write('RIFF', 0, 'ascii')+ out.writeUInt32LE(36 + samples * 2, 4)+ out.write('WAVE', 8, 'ascii')+ out.write('fmt ', 12, 'ascii')+ out.writeUInt32LE(16, 16)+ out.writeUInt16LE(1, 20)+ out.writeUInt16LE(1, 22)+ out.writeUInt32LE(16_000, 24)+ out.writeUInt32LE(32_000, 28)+ out.writeUInt16LE(2, 32)+ out.writeUInt16LE(16, 34)+ out.write('data', 36, 'ascii')+ out.writeUInt32LE(samples * 2, 40)+ for (let i = 0; i < samples; i++) out.writeInt16LE(i % 2 ? 9_000 : -9_000, 44 + i * 2)+ return out+}++/** The transcription service and the daily voice quota, faked: what each was asked, and what each answers. */+function fakeVoice(words = 'Ship the parser today.') {+ const calls = { transcribe: 0, reserved: [] as string[], committed: [] as string[], released: [] as string[] }+ const voice: VoiceTranscription & { calls: typeof calls; answer: () => Promise<string>; quota: 'ok' | 'limit' } = {+ calls, quota: 'ok', answer: async () => words,+ async reserve(_user, id) { calls.reserved.push(id); return voice.quota },+ async transcribe() { calls.transcribe++; return voice.answer() },+ async commit(id) { calls.committed.push(id) },+ async release(id) { calls.released.push(id) },+ }+ return voice+}++async function swarm(voice = fakeVoice(), now: () => number = Date.now) {+ const files = new MemoryAttachmentStore()+ const store = new SuperTabStore(db, now, { store: files, prefix: 'super-tabs', publicBase: 'https://cdn.example.test' }, { voice })+ const tab = (await store.create(owner, randomUUID(), 'Voice', 'public')).id+ const execute = (command: Parameters<SuperTabStore['execute']>[2]['command'], user = owner, asAgentId?: string) =>+ store.execute(user, tab, { operationId: randomUUID(), command }, asAgentId)+ await execute({ type: 'machine.contribute', machineId: 'mac', name: 'Mac', cwd: '/repo', engines: ['codex'], maxAgents: 8 })+ const lead = String((await execute({ type: 'agent.add', conversationId: 'lead', machineId: 'mac', harnessId: randomUUID(), name: 'Lead', configuration: config, lead: true })).result.agentId)+ const upload = async (bytes: Buffer, name = 'Voice message.wav', mime = 'audio/wav', user = owner) =>+ (await store.upload(user, tab, 'lead', { name, mime, body: Readable.from([bytes]) })).attachment+ return { store, tab, execute, lead, upload, voice, files }+}++describe('reading a voice file', () => {+ it('answers its length, waveform and that it was transcribed, and transcribes it once', async () => {+ const f = await swarm()+ const file = await f.upload(wav(81))+ const first = await f.store.readVoice(owner, f.tab, file.id, 'en')+ expect(first.voice).toMatchObject({ durationMs: 81_000, transcribed: true })+ expect(first.voice.waveform).toHaveLength(48)+ expect(first.voice).not.toHaveProperty('transcript')+ expect(await f.store.readVoice(owner, f.tab, file.id, 'en')).toEqual(first)+ expect(f.voice.calls).toMatchObject({ transcribe: 1, reserved: [`voice:${file.id}`], committed: [`voice:${file.id}`] })+ })++ it('lets one of two calls at once transcribe, and the other wait', async () => {+ const f = await swarm()+ let finish!: (words: string) => void+ f.voice.answer = () => new Promise(resolve => { finish = resolve })+ const file = await f.upload(wav(2))+ const first = f.store.readVoice(owner, f.tab, file.id, 'en')+ await vi.waitFor(() => expect(f.voice.calls.transcribe).toBe(1))+ await expect(f.store.readVoice(owner, f.tab, file.id, 'en')).rejects.toMatchObject({ code: 'VOICE_TRANSCRIBING', status: 409 })+ finish('Hello.')+ expect((await first).voice.transcribed).toBe(true)+ expect(f.voice.calls.transcribe).toBe(1)+ })++ it('takes over a claim left for over two minutes', async () => {+ let at = Date.now()+ const f = await swarm(fakeVoice(), () => at)+ const file = await f.upload(wav(2))+ await db.superTabAttachment.update({ where: { id: file.id }, data: { voiceSince: new Date(at - 60_000) } })+ await expect(f.store.readVoice(owner, f.tab, file.id, 'en')).rejects.toMatchObject({ code: 'VOICE_TRANSCRIBING' })+ at += 61_000 + 60_000+ expect((await f.store.readVoice(owner, f.tab, file.id, 'en')).voice.transcribed).toBe(true)+ })++ it('refuses another actor\'s file, a sent one, an agent caller, a file that is not WAV and a clip over 10:05', async () => {+ const f = await swarm()+ const file = await f.upload(wav(2))+ await expect(f.store.readVoice(peer, f.tab, file.id, 'en')).rejects.toMatchObject({ status: 403 })+ const agents = (await f.store.upload(owner, f.tab, 'lead', { name: 'Voice message.wav', mime: 'audio/wav', body: Readable.from([wav(2)]) }, f.lead)).attachment+ await expect(f.store.readVoice(owner, f.tab, agents.id, 'en')).rejects.toMatchObject({ code: 'ATTACHMENT_UNAVAILABLE' })+ await expect(f.store.readVoice(owner, f.tab, file.id, 'en', f.lead)).rejects.toMatchObject({ code: 'VOICE_PEOPLE_ONLY' })+ const text = await f.upload(Buffer.from('hello, this is not audio at all, only some text that is long enough'), 'notes.wav')+ await expect(f.store.readVoice(owner, f.tab, text.id, 'en')).rejects.toMatchObject({ code: 'UNSUPPORTED_AUDIO', status: 415 })+ const png = await f.upload(wav(2), 'shot.png', 'image/png')+ await expect(f.store.readVoice(owner, f.tab, png.id, 'en')).rejects.toMatchObject({ code: 'UNSUPPORTED_AUDIO' })+ await f.execute({ type: 'message.send', conversationId: 'lead', text: '', replyTo: null, recipientIds: [], attachmentIds: [file.id] })+ await expect(f.store.readVoice(owner, f.tab, file.id, 'en')).rejects.toMatchObject({ code: 'ATTACHMENT_UNAVAILABLE' })+ // Over the form's 10 MB: a long clip is stored straight in the bucket in production; here, as if it were.+ const long = await f.upload(wav(1))+ const row = await db.superTabAttachment.findUniqueOrThrow({ where: { id: long.id } })+ const big = wav(606)+ f.files.objects.set(row.objectKey, { ...f.files.objects.get(row.objectKey)!, bytes: big })+ await db.superTabAttachment.update({ where: { id: long.id }, data: { size: big.length } })+ await expect(f.store.readVoice(owner, f.tab, long.id, 'en')).rejects.toMatchObject({ code: 'VOICE_TOO_LONG', status: 413 })+ expect(f.voice.calls.transcribe).toBe(0)+ })++ it('sends without words past the daily voice limit or when transcription fails, once, and logs no words', async () => {+ const voice = fakeVoice('my secret plan')+ const f = await swarm(voice)+ const logged: string[] = []+ const spies = (['info', 'warn', 'error'] as const).map(level => vi.spyOn(logger, level).mockImplementation((...args: unknown[]) => { logged.push(JSON.stringify(args)); return logger }))+ voice.quota = 'limit'+ const limited = await f.upload(wav(3))+ expect((await f.store.readVoice(owner, f.tab, limited.id, 'en')).voice).toMatchObject({ durationMs: 3000, transcribed: false, failure: 'limit' })+ expect(voice.calls.transcribe).toBe(0)+ voice.quota = 'ok'+ voice.answer = async () => { throw new Error('Deepgram HTTP 503') }+ const failed = await f.upload(wav(3))+ expect((await f.store.readVoice(owner, f.tab, failed.id, 'en')).voice).toMatchObject({ transcribed: false, failure: 'failed' })+ expect(voice.calls.released).toEqual([`voice:${failed.id}`])+ voice.answer = async () => 'my secret plan'+ expect((await f.store.readVoice(owner, f.tab, failed.id, 'en')).voice).toMatchObject({ failure: 'failed' })+ const spoken = await f.upload(wav(3))+ await f.store.readVoice(owner, f.tab, spoken.id, 'en')+ spies.forEach(spy => spy.mockRestore())+ expect(logged.some(line => line.includes('super-tab voice'))).toBe(true)+ expect(logged.join('\n')).not.toContain('my secret plan')+ })+})++describe('sending a voice message', () => {+ const send = (f: Awaited<ReturnType<typeof swarm>>, id: string) =>+ f.execute({ type: 'message.send', conversationId: 'lead', text: '', replyTo: null, recipientIds: [], attachmentIds: [id] })++ it('stores its words as the message\'s text and its facts on the file, never its words twice', async () => {+ const f = await swarm()+ const file = await f.upload(wav(81))+ await f.store.readVoice(owner, f.tab, file.id, 'en')+ await send(f, file.id)+ const message = (await db.superTabMessage.findMany({ where: { tabId: f.tab, conversationId: 'lead' } }))+ .find(m => JSON.stringify(m.attachments ?? null).includes(file.id))+ expect(message!.text).toBe('Ship the parser today.')+ expect(message!.attachments).toEqual([expect.objectContaining({ id: file.id, mime: 'audio/wav', voice: expect.objectContaining({ durationMs: 81_000, transcribed: true }) })])+ expect(JSON.stringify(message!.attachments)).not.toContain('Ship the parser')+ const [delivery] = await db.superTabDelivery.findMany({ where: { tabId: f.tab, messageId: message!.id } })+ expect(delivery!.agentId).toBe(f.lead)+ })++ it('waits for a file still being transcribed', async () => {+ const f = await swarm()+ const file = await f.upload(wav(2))+ await db.superTabAttachment.update({ where: { id: file.id }, data: { voiceSince: new Date() } })+ await expect(send(f, file.id)).rejects.toMatchObject({ code: 'VOICE_TRANSCRIBING', status: 409 })+ })+})++describe('what agents and previews read of a voice message', () => {+ async function sent(words: string, quota: 'ok' | 'limit' = 'ok') {+ const voice = fakeVoice(words)+ voice.quota = quota+ const f = await swarm(voice)+ const file = await f.upload(wav(81))+ await f.store.readVoice(owner, f.tab, file.id, 'en')+ const messageId = String((await f.execute({ type: 'message.send', conversationId: 'lead', text: '', replyTo: null, recipientIds: [], attachmentIds: [file.id] })).result.messageId)+ return { f, messageId }+ }+ const claim = async (f: Awaited<ReturnType<typeof swarm>>, messageId: string) => {+ const delivery = await db.superTabDelivery.findFirstOrThrow({ where: { tabId: f.tab, messageId } })+ return (await f.store.delivery(owner, f.tab, f.lead, delivery.id, { type: 'claim', claimId: randomUUID() })).message+ }++ it('hands an agent the words, marked as a voice message with its length', async () => {+ const { f, messageId } = await sent('hello team')+ expect((await claim(f, messageId))!.text).toBe('Voice message (1:21): hello team')+ // What is stored stays the words alone: people read them under the bubble.+ expect((await db.superTabMessage.findUniqueOrThrow({ where: { id: messageId } })).text).toBe('hello team')+ })++ it('tells an agent when a voice message has no words', async () => {+ const { f, messageId } = await sent('unheard', 'limit')+ expect((await claim(f, messageId))!.text).toBe('Voice message (1:21), not transcribed.')+ })++ it('previews it as "Voice message (1:21)", and a plain message as its text', async () => {+ const { f, messageId } = await sent('hello team')+ const plain = String((await f.execute({ type: 'message.send', conversationId: 'lead', text: 'Typed words', replyTo: null, recipientIds: [] })).result.messageId)+ const rows = (await readBy(db, 'SuperTabMessage', 'id', [{ $match: { _id: { $in: [messageId, plain] }, tabId: f.tab } },+ { $project: { ...FIELDS, conversationId: 1 } }])).map(row)+ expect(Object.fromEntries(rows.map(r => [r.id, preview(r).text]))).toEqual({ [messageId]: 'Voice message (1:21)', [plain]: 'Typed words' })+ })+})diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prismaindex b97cd0b3a..0c575fd2e 100644--- a/backend/prisma/schema.prisma+++ b/backend/prisma/schema.prisma@@ -1371,6 +1371,12 @@ model SuperTabAttachment {proposedBy String?/// `avatar` for a profile picture (superTabs/profile.ts), which no message sends; absent on a chat file.purpose String?+ /// 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?+ /// 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?createdAt DateTime @default(now())@@index([tabId, actorKey, createdAt])diff --git a/backend/src/lib/wav.ts b/backend/src/lib/wav.tsnew file mode 100644index 000000000..1f5e9f67e--- /dev/null+++ b/backend/src/lib/wav.ts@@ -0,0 +1,26 @@+// What a PCM WAV file says about itself: the voice route and the chat's voice messages charge and draw a clip by it.++const WAV_HEADER_BYTES = 44+const WAVE_FORMAT_PCM = 1++/**+ * The clip's length in milliseconds, as Deepgram will hear it, or null when it cannot be told from the bytes.+ *+ * Only uncompressed PCM WAV is accepted, which is all the phone (mobile/lib/phone/voice_wav.dart) and the dial+ * (cli/src/cable/cableHost.ts wav()) ever send: its length is the bytes after the header at the rate the header+ * states. Compressed audio (MP3, Opus, or a compressed codec inside a WAV) is many times longer than its bytes+ * suggest, so charging it by size would let one account transcribe hours a day on a 10-minute plan. The header+ * is the caller's to write, so a rate, channel count or sample size outside what a microphone records (which+ * would make a long clip read as no time at all) is refused too.+ */+export function wavDurationMs(audio: Buffer): number | null {+ if (audio.length < WAV_HEADER_BYTES) return null+ if (audio.toString('ascii', 0, 4) !== 'RIFF' || audio.toString('ascii', 8, 12) !== 'WAVE' || audio.toString('ascii', 12, 16) !== 'fmt ') return null+ if (audio.readUInt16LE(20) !== WAVE_FORMAT_PCM) return null+ const channels = audio.readUInt16LE(22)+ const sampleRate = audio.readUInt32LE(24)+ const bits = audio.readUInt16LE(34)+ if (channels < 1 || channels > 2 || sampleRate < 8_000 || sampleRate > 48_000 || ![8, 16, 24, 32].includes(bits)) return null+ const bytesPerSecond = sampleRate * channels * (bits / 8)+ return Math.ceil(((audio.length - WAV_HEADER_BYTES) / bytesPerSecond) * 1000)+}diff --git a/backend/src/routes/superTabs.ts b/backend/src/routes/superTabs.tsindex 2d24154f4..5d78e3ae8 100644--- a/backend/src/routes/superTabs.ts+++ b/backend/src/routes/superTabs.ts@@ -59,6 +59,8 @@ const id = z.string().min(1).max(160).regex(/^[A-Za-z0-9_-]+$/)const tabParams = z.object({ tabId: id })const agentQuery = z.object({ asAgentId: id.optional() }).strict()const uploadParams = z.object({ tabId: id, uploadId: id })+const voiceParams = z.object({ tabId: id, attachmentId: id })+const voiceQuery = agentQuery.extend({ lang: z.string().max(16).optional() })// The store checks the name, type and size; this only bounds what reaches it.const uploadLinkBody = z.object({ conversationId: id, name: z.string().max(1000), mime: z.string().max(1000), size: z.number().int() }).strict()const emptyBody = z.object({}).strict().optional()@@ -237,9 +239,10 @@ const updateRequired = (_req: unknown, reply: FastifyReply) => sendError(reply,/** The store this server runs on. Loaded only here: importing the bus opens its Redis connections. */async function productionStore(github: GitHubTokens | null): Promise<SuperTabStore> {- const [bus, { announceSwarmDeliveries, announceSwarmTerminal }, { announceSwarmHostChanges }, { announceSwarmChanges }, { tellSessionViewers }] = await Promise.all([+ const [bus, { announceSwarmDeliveries, announceSwarmTerminal }, { announceSwarmHostChanges }, { announceSwarmChanges }, { tellSessionViewers },+ { VoiceQuotaExceededError, voiceQuotaService }, { pickWavLang, transcribeWavWithDeepgram }] = await Promise.all([import('../lib/bus.js'), import('../lib/swarmDeliveryPushes.js'), import('../lib/swarmHostPushes.js'), import('../lib/swarmClientPushes.js'),- import('./harnessLinks.js')])+ import('./harnessLinks.js'), import('../lib/voiceQuota.js'), import('../lib/deepgramWav.js')])return new SuperTabStore(prisma, Date.now, attachmentsFromEnv(env),// A task that ends revokes the token it was given; a revoke that fails is logged inside and never blocks the write.{ workEnded: (tabId, ended) => { void github?.release(tabId, ended).catch(error => logger.warn('[github] release failed', { tabId, error: String(error) })) },@@ -264,7 +267,16 @@ async function productionStore(github: GitHubTokens | null): Promise<SuperTabStowatchTerminal: (requestId, onChange) => bus.subscribeTerminalAnswered(requestId, onChange),// Its observer sockets may be on any instance.accessChanged: tabId => { void bus.publishSwarmAccessChanged(tabId) },- sessionsConfidential: sessions => { void tellSessionViewers(sessions) } })+ sessionsConfidential: sessions => { void tellSessionViewers(sessions) },+ // Chat voice messages are transcribed as the dial's and the phone's voice input are (routes/voice.ts), on the same daily+ // quota of the person's account.+ voice: {+ reserve: (user, id, durationMs) => voiceQuotaService.reserveForUser(user.sub, user.autonomousEnv ?? 'prod', id, durationMs)+ .then(() => 'ok' as const, error => { if (error instanceof VoiceQuotaExceededError) return 'limit' as const; throw error }),+ transcribe: (wav, lang) => transcribeWavWithDeepgram(wav, 'audio/wav', pickWavLang(lang)),+ commit: id => voiceQuotaService.commit(id),+ release: id => voiceQuotaService.release(id).then(() => undefined),+ } })}export async function superTabRoutes(app: FastifyInstance, options: { store?: SuperTabStore; github?: GitHubTokens | null; reads?: SwarmReads; hosts?: HostReads;@@ -620,6 +632,11 @@ export async function superTabRoutes(app: FastifyInstance, options: { store?: Suapp.post<{ Params: z.infer<typeof uploadParams>; Querystring: z.infer<typeof agentQuery>; Body: z.infer<typeof emptyBody> }>('/api/super-tabs/:tabId/attachments/uploads/:uploadId/complete',{ bodyLimit: 1024, preHandler: [validateParams(uploadParams), validateQuery(agentQuery), validateBody(emptyBody)] },(req, reply) => respond(reply, () => store.completeUpload(req.user!, req.params.tabId, req.params.uploadId, req.query.asAgentId)))+ // A voice message's file, uploaded either way above, is read once before it is sent: its length, waveform and words (store.ts+ // readVoice). Its answer carries no words; message.send makes them the message's text.+ app.post<{ Params: z.infer<typeof voiceParams>; Querystring: z.infer<typeof voiceQuery>; Body: z.infer<typeof emptyBody> }>('/api/super-tabs/:tabId/attachments/:attachmentId/voice',+ { bodyLimit: 1024, preHandler: [validateParams(voiceParams), validateQuery(voiceQuery), validateBody(emptyBody)] },+ (req, reply) => respond(reply, () => store.readVoice(req.user!, req.params.tabId, req.params.attachmentId, req.query.lang ?? 'en', req.query.asAgentId)))// An avatar picture (superTabs/profile.ts) comes the same way, alone in its form; the command that sets it names whose it is.await app.register(async scope => {await scope.register(multipart, { limits: { fileSize: MAX_AVATAR_BYTES + 1, files: 1, fields: 0, parts: 1 } })diff --git a/backend/src/routes/voice.ts b/backend/src/routes/voice.tsindex 037b92c23..286d5b2e0 100644--- a/backend/src/routes/voice.ts+++ b/backend/src/routes/voice.ts@@ -30,6 +30,7 @@ import multipart from '@fastify/multipart'import { VOICE_WAV_LANGS, transcribeWavWithDeepgram } from '../lib/deepgramWav.js'import { createRateLimiter } from '../lib/rateLimit.js'+import { wavDurationMs } from '../lib/wav.js'import { releaseVoice, reserveVoice } from '../lib/voiceBudget.js'import { VoiceQuotaExceededError, voiceQuotaService } from '../lib/voiceQuota.js'import { sendError, sendSuccess } from '../utils/response.js'@@ -44,31 +45,6 @@ const MAX_AUDIO_BYTES = 25 * 1024 * 1024const PER_MINUTE = 10const MINUTE_MS = 60_000-const WAV_HEADER_BYTES = 44-const WAVE_FORMAT_PCM = 1--/**- * The clip's length in milliseconds, as Deepgram will hear it, or null when it cannot be told from the bytes.- *- * Only uncompressed PCM WAV is accepted, which is all the phone (mobile/lib/phone/voice_wav.dart) and the dial- * (cli/src/cable/cableHost.ts wav()) ever send: its length is the bytes after the header at the rate the header- * states. Compressed audio (MP3, Opus, or a compressed codec inside a WAV) is many times longer than its bytes- * suggest, so charging it by size would let one account transcribe hours a day on a 10-minute plan. The header- * is the caller's to write, so a rate, channel count or sample size outside what a microphone records (which- * would make a long clip read as no time at all) is refused too.- */-export function wavDurationMs(audio: Buffer): number | null {- if (audio.length < WAV_HEADER_BYTES) return null- if (audio.toString('ascii', 0, 4) !== 'RIFF' || audio.toString('ascii', 8, 12) !== 'WAVE' || audio.toString('ascii', 12, 16) !== 'fmt ') return null- if (audio.readUInt16LE(20) !== WAVE_FORMAT_PCM) return null- const channels = audio.readUInt16LE(22)- const sampleRate = audio.readUInt32LE(24)- const bits = audio.readUInt16LE(34)- if (channels < 1 || channels > 2 || sampleRate < 8_000 || sampleRate > 48_000 || ![8, 16, 24, 32].includes(bits)) return null- const bytesPerSecond = sampleRate * channels * (bits / 8)- return Math.ceil(((audio.length - WAV_HEADER_BYTES) / bytesPerSecond) * 1000)-}-function dailyLimitMessage(err: VoiceQuotaExceededError): string {const minutes = Math.round(err.snapshot.limitSeconds / 60)return `You've used today's voice limit (${minutes} minutes a day). It resets at midnight UTC.`diff --git a/backend/src/superTabs/attachments.ts b/backend/src/superTabs/attachments.tsindex a41ecf0e7..9de4bd9c0 100644--- a/backend/src/superTabs/attachments.ts+++ b/backend/src/superTabs/attachments.ts@@ -2,13 +2,16 @@ import { createHmac, randomBytes } from 'node:crypto'import { Readable, Transform, Writable } from 'node:stream'import { pipeline } from 'node:stream/promises'import { z } from 'zod'+import type { VoiceFacts } from './voiceMessages.js'/** Metadata as a message stores it. The bytes stay in the bucket. `url` is the file's public address on the CDN: it names the* object, and apps from before signed links open it, until the bucket is no longer public (docs/swarms.md, "Chat file* links"). Each read adds `link`, a short-lived signed link of that read, and `expiresAt` (epoch ms), when it stops opening* (fileLinks.ts). A read that could not sign one says so with `unlinked: true` instead, so an app asks again rather than* 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 }+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 }/** 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 * 1024diff --git a/backend/src/superTabs/commands.test.ts b/backend/src/superTabs/commands.test.tsindex 022520936..ee171c202 100644--- a/backend/src/superTabs/commands.test.ts+++ b/backend/src/superTabs/commands.test.ts@@ -1578,6 +1578,34 @@ describe('message attachments', () => {})})+describe('voice messages', () => {+ const voice = { durationMs: 81_000, waveform: new Array(48).fill(7), transcribed: true }+ const clip = { id: 'voice-1', name: 'Voice message.wav', mime: 'audio/wav', size: 2_592_044, url: 'https://cdn.example.test/super-tabs/tab/abc/Voice_message.wav', voice }+ const file = { id: 'file-1', name: 'screen.png', mime: 'image/png', size: 12, url: 'https://cdn.example.test/super-tabs/tab/abc/screen.png' }+ const send = (attachmentIds: string[], text = ''): Command => ({ type: 'message.send', conversationId: 'lead', text, replyTo: null, recipientIds: [], attachmentIds })+ it('makes the clip\'s words the message\'s text, and keeps its facts on the file', () => {+ const f = fixture(), lead = f.add('lead', 'owner', 'lead', true)+ const sent = f.run(send(['voice-1']), owner, { attachments: [clip], voiceWords: { 'voice-1': 'Ship the parser today.' } })+ expect(sent.messages).toEqual([expect.objectContaining({ text: 'Ship the parser today.', attachments: [clip], recipients: [lead] })])+ })+ it('sends a clip with no words with an empty text', () => {+ const f = fixture()+ const quiet = { ...clip, voice: { ...voice, transcribed: false, failure: 'limit' as const } }+ expect(f.run(send(['voice-1']), owner, { attachments: [quiet] }).messages[0]).toMatchObject({ text: '', attachments: [quiet] })+ })+ it('sends a voice message alone: no typed text, tags or other files with it', () => {+ const f = fixture()+ expect(() => f.run(send(['voice-1'], 'and also this'), owner, { attachments: [clip] })).toThrow(expect.objectContaining({ code: 'VOICE_ALONE', status: 400 }))+ expect(() => f.run(send(['voice-1', 'file-1']), owner, { attachments: [clip, file] })).toThrow(expect.objectContaining({ code: 'VOICE_ALONE' }))+ })+ it('removes a credential spoken in it, as from typed text', () => {+ const f = fixture()+ const token = ['ghp', 'Fixture0Token1Not2Real3Value4abcdefgh'].join('_')+ const sent = f.run(send(['voice-1']), owner, { attachments: [clip], voiceWords: { 'voice-1': `the token is ${token}` } })+ expect(sent.messages[0]!.text).toBe(`the token is ${CREDENTIAL_MARKER}`)+ })+})+describe('finished worker decisions', () => {function finished() {const f = fixture(), lead = f.add('lead', 'owner', 'lead', true), group = f.group()diff --git a/backend/src/superTabs/commands.ts b/backend/src/superTabs/commands.tsindex 8b6ea5f6d..564adae5b 100644--- a/backend/src/superTabs/commands.ts+++ b/backend/src/superTabs/commands.ts@@ -485,6 +485,8 @@ export interface CommandContext {/** The files `message.send` or `buddy.propose` names, each checked by the store: uploaded by this actor to this conversation and* unused, or for a proposal, sent there by its Buddy's owner (store.ts proposable). */attachments?: Attachment[]+ /** The words of each voice message's file among [attachments], by its id, read by the store (store.ts readVoice). */+ voiceWords?: Record<string, string>/** The avatar upload `member.avatar` or `agent.avatar` names, checked by the store the same way (profile.ts). */avatarUpload?: AvatarUpload/** The message `message.react` names, read by the store (reactions.ts). */@@ -3055,6 +3057,11 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,const ids = [...new Set(command.attachmentIds ?? [])]const attachments = ids.map(id => context.attachments?.find(a => a.id === id) ?? fail('ATTACHMENT_UNAVAILABLE', 'Upload this file again before sending it.', 400))if (!command.text && !attachments.length) fail('TEXT_REQUIRED', 'Write a message or attach a file.', 400)+ // A voice message is its clip alone, and its words are its text: what agents, search and older apps read (voiceMessages.ts).+ const voice = attachments.find(a => a.voice)+ if (voice && (attachments.length > 1 || command.text || command.mentions?.length)) {+ fail('VOICE_ALONE', 'Send a voice message on its own, without text, tags or other files.', 400)+ }// The text is where tags live, as people tag in chat: each name it says tags that person, if they may read here. A tag// picked in an older client (or the CLI's --mention) that the text does not say is written into it, so every client// shows the same words. Only from the command, so a retry writes the same text.@@ -3063,7 +3070,7 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,const names = picked.length || command.text.includes('@') ? swarmUsernames(state) : new Map<string, string>()const named = namedIn(state, command.text, names)const unsaid = picked.filter(id => !named.includes(id) && !saysName(command.text, names.get(id)!))- const text = withTags(unsaid.map(id => `@${names.get(id)}`), command.text)+ const text = voice ? context.voiceWords?.[voice.id] ?? '' : withTags(unsaid.map(id => `@${names.get(id)}`), command.text)// A tag would tell a person about an agent-only notice they cannot see.const mentions = [...new Set([...picked, ...command.agentOnly ? [] : textTags(state, actor, group, named)])].slice(0, MAX_MENTIONS)if (command.mentionsIfNoWorker && !mentions.length) fail('INVALID_MENTION', 'Hold a tag only for a message that tags someone.', 400)diff --git a/backend/src/superTabs/fileLinks.test.ts b/backend/src/superTabs/fileLinks.test.tsindex aad21e4fd..ccb1ff03c 100644--- a/backend/src/superTabs/fileLinks.test.ts+++ b/backend/src/superTabs/fileLinks.test.ts@@ -25,6 +25,16 @@ describe('chat file links', () => {expect(Object.keys(linked!).sort()).toEqual(['expiresAt', 'id', 'link', 'mime', 'name', 'size', 'url'])})+ it('keeps a voice message\'s facts on its file, and drops a malformed one', async () => {+ const voice = { durationMs: 81_000, waveform: [1, 2, 3], transcribed: false, failure: 'limit' as const }+ const [read] = await new FileLinks(new MemoryAttachmentStore(() => now), base, 'super-tabs').forRows('tab1', [row(+ { ...file('super-tabs/tab1/abc/Voice_message.wav', 'clip'), voice },+ { ...file('super-tabs/tab1/abc/odd.wav', 'odd'), voice: { durationMs: 'long' } as never },+ )], now)+ expect(read!.attachments[0]).toMatchObject({ id: 'clip', voice })+ expect(read!.attachments[1]).not.toHaveProperty('voice')+ })+it('links only files in the reading Swarm\'s own folder', async () => {const links = new FileLinks(new MemoryAttachmentStore(() => now), base, 'super-tabs')const [read] = await links.forRows('tab1', [row(diff --git a/backend/src/superTabs/fileLinks.ts b/backend/src/superTabs/fileLinks.tsindex 7ce9c4546..e8db43585 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 { voiceFacts } from './voiceMessages.js'/*** Chat files are opened only through short-lived signed links, made on each read for whoever can read the message now@@ -53,8 +54,9 @@ export class FileLinks {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) : undefined+ const voice = voiceFacts(file.voice)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 } } satisfies Attachment+ ...url ? { link: url, expiresAt } : { unlinked: true as const }, ...voice ? { voice } : {} } satisfies Attachment}) })}diff --git a/backend/src/superTabs/read/inbox.ts b/backend/src/superTabs/read/inbox.tsindex 832438680..1af436ce4 100644--- a/backend/src/superTabs/read/inbox.ts+++ b/backend/src/superTabs/read/inbox.ts@@ -28,6 +28,7 @@ import { deliveredCopies, type ThreadReply } from '../pageReads.js'import { threadSummaries } from '../threads.js'import { sessionReadsRow } from '../buddySessions.js'import { readMarks, readThreadMarkRows, type ThreadMarkRow } from '../readMarks.js'+import { voiceLabel } from '../voiceMessages.js'import { readBy } from '../parts/indexed.js'import { logger } from '../../utils/logger.js'import { ms } from '../parts/light.js'@@ -74,11 +75,13 @@ export interface Row { id: string; conversationId: string; sequence: number; aut/** A preview's fields, its text cut by the database so a long message never leaves it whole. */export const FIELDS = { sequence: 1, author: 1, text: { $substrCP: [{ $ifNull: ['$text', ''] }, 0, PREVIEW_CHARS] }, createdAt: 1, attachments: { $size: { $ifNull: ['$attachments', []] } },+ // A voice message's length: its preview names it rather than quoting its words (voiceMessages.ts voiceLabel).+ voiceMs: { $arrayElemAt: [{ $ifNull: ['$attachments.voice.durationMs', []] }, 0] },thread: 1, alsoInChat: 1, mentions: 1, source: 1, recipientIds: 1, buddyProject: 1 }/** A scan for unread replies: what a reply is counted, named and placed by, never its text. */export const SCAN_FIELDS = { sequence: 1, author: 1, createdAt: 1, thread: 1, mentions: 1, source: 1, recipientIds: 1, buddyProject: 1 }export const row = (raw: Record<string, unknown>): Row => ({ id: raw._id as string, conversationId: raw.conversationId as string, sequence: raw.sequence as number, author: raw.author as Author,- text: (raw.text ?? '') as string, createdAt: new Date(ms(raw.createdAt)), attachments: typeof raw.attachments === 'number' ? raw.attachments : 0,+ text: typeof raw.voiceMs === 'number' ? voiceLabel(raw.voiceMs) : (raw.text ?? '') as string, createdAt: new Date(ms(raw.createdAt)), attachments: typeof raw.attachments === 'number' ? raw.attachments : 0,thread: (raw.thread ?? null) as string | null, alsoInChat: raw.alsoInChat === true, mentions: (raw.mentions ?? []) as string[], source: raw.source ?? null,recipientIds: (raw.recipientIds ?? []) as string[], buddyProject: typeof raw.buddyProject === 'string' ? raw.buddyProject : null })export const preview = (m: Row): InboxMessage => ({ id: m.id, sequence: m.sequence, author: m.author, text: m.text,diff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.tsindex da10c1c91..3eafceacf 100644--- a/backend/src/superTabs/store.ts+++ b/backend/src/superTabs/store.ts@@ -7,6 +7,9 @@ import { createHash } from 'node:crypto'import { isDeepStrictEqual } from 'node:util'import type { Machine, Prisma, PrismaClient, SuperTabDelivery, SuperTabMessage } from '@prisma/client'import type { AuthUser } from '../lib/ssoAuth.js'+import { releaseVoice, reserveVoice } from '../lib/voiceBudget.js'+import { wavDurationMs } from '../lib/wav.js'+import { agentText, MAX_VOICE_BYTES, MAX_VOICE_MS, VOICE_CLAIM_MS, VOICE_TYPES, voiceFacts, voiceTranscript, voiceWaveform, type StoredVoice, type VoiceFacts } from './voiceMessages.js'import { machineAlive } from '../lib/prisma.js'import { pullRequestsIn, type PushOrigin } from '../github/pushOrigin.js'import { githubRepository } from '../github/repository.js'@@ -357,6 +360,19 @@ export interface SuperTabStoreHooks {/** A commit changed a Swarm these people are in, or were in before it: each one's apps read it again* (lib/swarmClientPushes.ts). Called after the write commits; it must not throw. */swarmChanged?: (change: SwarmChanged) => void+ /** Transcribes voice messages (readVoice), on the transcription service and the daily voice quota the voice route uses. Left+ * out, voice messages are refused. */+ voice?: VoiceTranscription+}++/** What readVoice needs of the voice service: the daily quota of the person who sends, keyed by an id that makes a retry no new+ * charge, and the words of a WAV. */+export interface VoiceTranscription {+ /** Charges [durationMs] to [user]'s day; 'limit' when that would pass their plan's daily voice seconds. */+ reserve(user: AuthUser, id: string, durationMs: number): Promise<'ok' | 'limit'>+ transcribe(wav: Buffer, lang: string): Promise<string>+ commit(id: string): Promise<void>+ release(id: string): Promise<void>}/** A report call's host checks that need no Swarm (SuperTabStore.machineHost): the caller's account row for the machine. */@@ -1620,7 +1636,7 @@ export class SuperTabStore {// stays with that message, and a proposal may carry one its Buddy's owner sent in their chat.let claimed: string[] = [], proposed: string[] = []if (command.type === 'message.send' && command.attachmentIds?.length) {- context.attachments = await this.sendable(tx, tabId, actor, command)+ ({ attachments: context.attachments, voiceWords: context.voiceWords } = await this.sendable(tx, tabId, actor, command))claimed = context.attachments.map(a => a.id)}if (command.type === 'buddy.propose' && command.attachmentIds?.length) ({ attachments: context.attachments, claimed, proposed } = await this.proposable(tx, original, actor, command))@@ -2129,6 +2145,107 @@ export class SuperTabStore {return now && !now.purpose && !now.usedBy ? sendable(now.mime) : unavailable()}+ /**+ * A voice message's file, read once before it is sent (voiceMessages.ts): its length and waveform from its bytes, and its words+ * from the transcription service, charged to the sender's daily voice quota. The words stay on the row for message.send, which+ * makes them the message's text; they are never answered here or logged. Only the person who uploaded it, while they may post in+ * its conversation, and only before it is sent. A second call answers what the first stored; one made while the first+ * transcribes is told to wait, and a claim left for VOICE_CLAIM_MS is taken over. No words is final for the file: the message+ * still sends, and says so.+ */+ async readVoice(user: AuthUser, tabId: string, attachmentId: string, lang: string, asAgentId?: string): Promise<{ voice: VoiceFacts }> {+ const unavailable = () => fail('ATTACHMENT_UNAVAILABLE', 'Attach this file again before sending it.', 400)+ // Who may reach the Swarm first, as on every person route: one they cannot reach is answered as one that does not exist.+ const row = await transaction(this.db, async tx => {+ const { state } = await this.load(tx, user, tabId, {})+ const actor = await this.actor(tx, user, state, asAgentId)+ author(state, actor)+ if (actor.kind === 'agent') fail('VOICE_PEOPLE_ONLY', 'Only people send voice messages.', 403)+ const found = await tx.superTabAttachment.findFirst({ where: { tabId, id: attachmentId } })+ if (!found || found.actorKey !== actorKey(actor) || found.usedBy || found.purpose) unavailable()+ await this.poster(tx, user, tabId, found!.conversationId)+ return found!+ })+ const attachments = this.attachments ?? fail('ATTACHMENTS_UNAVAILABLE', 'File attachments are not available on this server.', 503)+ const direct = directUploads(attachments.store) ?? fail('ATTACHMENTS_UNAVAILABLE', 'Voice messages cannot be sent to this server.', 503)+ const voice = this.hooks.voice ?? fail('VOICE_UNAVAILABLE', 'Voice messages are not available on this server.', 503)+ const stored = voiceFacts(row.voice)+ if (stored) return { voice: stored }+ if (!VOICE_TYPES.has(row.mime)) fail('UNSUPPORTED_AUDIO', 'A voice message is a WAV recording.', 415)+ if (row.size > MAX_VOICE_BYTES) fail('VOICE_TOO_LONG', 'A voice message can be up to 10 minutes long.', 413)+ const since = new Date(this.now())+ // Read and claimed in one transaction: a call that stores its words writes the same row, so one of the two is retried after+ // the other and sees what it did.+ const claimed = await transaction(this.db, async tx => {+ const now = await tx.superTabAttachment.findFirst({ where: { id: row.id, tabId }, select: { voice: true, voiceSince: true, usedBy: true } })+ if (!now || now.usedBy) unavailable()+ const done = voiceFacts(now!.voice)+ if (done) return done+ if (now!.voiceSince && now!.voiceSince.getTime() > since.getTime() - VOICE_CLAIM_MS) return null+ await tx.superTabAttachment.update({ where: { id: row.id }, data: { voiceSince: since } })+ return 'claimed' as const+ })+ if (claimed !== 'claimed') return claimed ? { voice: claimed } : fail('VOICE_TRANSCRIBING', 'This voice message is still being transcribed. Try again in a moment.', 409)+ const unclaim = () => this.db.superTabAttachment.updateMany({ where: { id: row.id, tabId, voiceSince: since }, data: { voiceSince: { unset: true } } })+ if (!reserveVoice(row.size)) {+ await unclaim()+ fail('VOICE_BUSY', 'Voice is busy right now. Try again in a moment.', 503)+ }+ const started = this.now()+ let facts: StoredVoice+ try {+ let wav: Buffer+ try { wav = await direct.head(row.objectKey, row.size) } catch (error) {+ logger.error('super-tab voice read failed', { tabId, code: errorCode(error) })+ await unclaim()+ fail('ATTACHMENTS_UNAVAILABLE', 'Could not read the voice message just now. Try again.', 503)+ }+ const durationMs = wavDurationMs(wav!)+ if (durationMs === null || durationMs > MAX_VOICE_MS) {+ await unclaim()+ if (durationMs === null) fail('UNSUPPORTED_AUDIO', 'A voice message is a WAV recording.', 415)+ fail('VOICE_TOO_LONG', 'A voice message can be up to 10 minutes long.', 413)+ }+ facts = { durationMs: durationMs!, waveform: voiceWaveform(wav!), ...await this.transcribed(voice, user, `voice:${row.id}`, wav!, durationMs!, lang) }+ } finally { releaseVoice(row.size) }+ const kept = await this.db.superTabAttachment.updateMany({ where: { id: row.id, tabId, voiceSince: since },+ data: { voice: facts as unknown as Prisma.InputJsonValue, voiceSince: { unset: true } } })+ // The words are never logged: whether they came, how long the clip was and how long it took tell all that is needed.+ logger.info('super-tab voice', { tabId, outcome: facts.failure ?? 'transcribed', durationMs: facts.durationMs, bytes: row.size,+ ms: this.now() - started, chars: facts.transcript?.length ?? 0 })+ if (kept.count !== 1) return this.voiceOf(tabId, row.id)+ return { voice: voiceFacts(facts)! }+ }++ /** The words of [wav], charged to [user]'s day under [id]: none, and why, past their limit or when the service fails. */+ private async transcribed(voice: VoiceTranscription, user: AuthUser, id: string, wav: Buffer, durationMs: number, lang: string):+ Promise<Pick<StoredVoice, 'transcribed' | 'failure' | 'transcript'>> {+ const none = (failure: 'limit' | 'failed') => ({ transcribed: false, failure, transcript: null })+ let quota: 'ok' | 'limit'+ try { quota = await voice.reserve(user, id, durationMs) } catch (error) {+ logger.error('super-tab voice quota unavailable', { error: error instanceof Error ? error.name : 'unknown' })+ return none('failed')+ }+ if (quota === 'limit') return none('limit')+ try {+ const transcript = await voice.transcribe(wav, lang)+ // Left pending, the cleanup worker hands these seconds back: logged, never silent.+ await voice.commit(id).catch(() => logger.error('super-tab voice quota commit failed', { id }))+ return { transcribed: true, transcript }+ } catch (error) {+ await voice.release(id).catch(() => { /* the cleanup worker releases a stale pending one */ })+ logger.error('super-tab voice transcription failed', { error: error instanceof Error ? error.message.slice(0, 120) : 'unknown' })+ return none('failed')+ }+ }++ /** What another call stored for this file, or that it is still transcribing it. */+ private async voiceOf(tabId: string, id: string): Promise<{ voice: VoiceFacts }> {+ const now = await this.db.superTabAttachment.findFirst({ where: { id, tabId }, select: { voice: true } })+ const facts = voiceFacts(now?.voice)+ return facts ? { voice: facts } : fail('VOICE_TRANSCRIBING', 'This voice message is still being transcribed. Try again in a moment.', 409)+ }+/** A picture for a person's or an agent's avatar (profile.ts): png, jpg or webp by its real bytes, at most 1 MB. Any* member who may write uploads one; the command that sets it decides whose it becomes. */async uploadAvatar(user: AuthUser, tabId: string, file: { body: Readable }, asAgentId?: string) {@@ -2193,19 +2310,23 @@ export class SuperTabStore {return { state, actor }}- private async sendable(tx: Transaction, tabId: string, actor: Actor, command: SendCommand): Promise<Attachment[]> {+ private async sendable(tx: Transaction, tabId: string, actor: Actor, command: SendCommand): Promise<{ attachments: Attachment[]; voiceWords: Record<string, string> }> {const attachments = this.attachments ?? fail('ATTACHMENTS_UNAVAILABLE', 'File attachments are not available on this server.', 503)const ids = [...new Set(command.attachmentIds)]const rows = await tx.superTabAttachment.findMany({ where: { tabId, id: { in: ids } } })- return ids.map(id => {+ const voiceWords: Record<string, string> = {}+ return { voiceWords, attachments: ids.map(id => {const row = rows.find(r => r.id === id)if (!row || row.actorKey !== actorKey(actor) || row.conversationId !== command.conversationId || row.purpose) {fail('ATTACHMENT_UNAVAILABLE', 'Attach this file again before sending it.', 400)}if (row.usedBy === EXPIRED_UPLOAD) fail('ATTACHMENT_EXPIRED', 'This file was not sent within a day and was removed. Attach it again.', 410)if (row.usedBy) fail('ATTACHMENT_USED', 'This file was already sent. Attach it again to send it in another message.')- return { id: row.id, name: row.name, mime: row.mime, size: row.size, url: publicUrl(attachments.publicBase, row.objectKey) }- })+ 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 } : {} }+ }) }}/**@@ -3752,7 +3873,9 @@ export class SuperTabStore {// A Buddy reads no machine names, in what it is handed too (security review 2 of #2178). News from outside its chat// (buddyNews.ts) is marked, so its host words it as news to note, never a message to answer there.const target = agent(state, agentId), group = conversation(state, delivery.conversationId)- const message = row && isBuddy(target) ? rowForBuddy(row) : row+ const shown = row && isBuddy(target) ? rowForBuddy(row) : row+ // A voice message reaches an agent as its words, marked as one (voiceMessages.ts); people hear the clip.+ const message = shown && { ...shown, text: agentText(shown.text, shown.attachments) }// Only a claim's answer is typed: an acknowledgement reads nothing for it.const other = row && action.type === 'claim' ? await this.otherAccounts(tx, state, target, row) : { message: false, quoted: [] }return { delivery: updated, message, ...isBuddy(target) && !group.agentIds.includes(target.id) ? { buddyNews: true as const } : {},diff --git a/backend/src/superTabs/voiceMessages.test.ts b/backend/src/superTabs/voiceMessages.test.tsnew file mode 100644index 000000000..90988a0d2--- /dev/null+++ b/backend/src/superTabs/voiceMessages.test.ts@@ -0,0 +1,63 @@+import { describe, expect, it } from 'vitest'++import { wavDurationMs } from '../lib/wav.js'+import { voiceLabel, voiceLength, voiceWaveform } from './voiceMessages.js'++/** 16 kHz mono 16-bit PCM WAV of `samples`, each one a sample value. */+function wav(samples: number[], sampleRate = 16_000): Buffer {+ const out = Buffer.alloc(44 + samples.length * 2)+ out.write('RIFF', 0, 'ascii')+ out.writeUInt32LE(36 + samples.length * 2, 4)+ out.write('WAVE', 8, 'ascii')+ out.write('fmt ', 12, 'ascii')+ out.writeUInt32LE(16, 16)+ out.writeUInt16LE(1, 20)+ out.writeUInt16LE(1, 22)+ out.writeUInt32LE(sampleRate, 24)+ out.writeUInt32LE(sampleRate * 2, 28)+ out.writeUInt16LE(2, 32)+ out.writeUInt16LE(16, 34)+ out.write('data', 36, 'ascii')+ out.writeUInt32LE(samples.length * 2, 40)+ samples.forEach((sample, i) => out.writeInt16LE(sample, 44 + i * 2))+ return out+}++describe('voice message length', () => {+ it('is written M:SS, rounded down to the second', () => {+ expect(voiceLength(81_000)).toBe('1:21')+ expect(voiceLength(81_999)).toBe('1:21')+ expect(voiceLength(600_000)).toBe('10:00')+ expect(voiceLength(999)).toBe('0:00')+ expect(voiceLength(9_000)).toBe('0:09')+ })++ it('labels a voice message with its length', () => {+ expect(voiceLabel(81_000)).toBe('Voice message (1:21)')+ })++ it('is told from a WAV file the way the voice route tells it', () => {+ expect(wavDurationMs(wav(new Array(16_000).fill(0)))).toBe(1000)+ expect(wavDurationMs(Buffer.from('not a wav at all, just some bytes here, long enough to have a header'))).toBeNull()+ })+})++describe('voice message waveform', () => {+ it('is 48 bars of silence for a silent clip', () => {+ expect(voiceWaveform(wav(new Array(16_000).fill(0)))).toEqual(new Array(48).fill(0))+ })++ it('rises where the clip gets loud', () => {+ const quiet = new Array(8_000).fill(0)+ const loud = Array.from({ length: 8_000 }, (_, i) => (i % 2 ? 12_000 : -12_000))+ const bars = voiceWaveform(wav([...quiet, ...loud]))+ expect(bars).toHaveLength(48)+ expect(Math.max(...bars.slice(0, 24))).toBe(0)+ expect(Math.min(...bars.slice(24))).toBeGreaterThan(20)+ expect(Math.max(...bars)).toBeLessThanOrEqual(31)+ })++ it('still has 48 bars for a clip shorter than 48 samples', () => {+ expect(voiceWaveform(wav([5_000, -5_000, 5_000]))).toHaveLength(48)+ })+})diff --git a/backend/src/superTabs/voiceMessages.ts b/backend/src/superTabs/voiceMessages.tsnew file mode 100644index 000000000..9d1c5e4c5--- /dev/null+++ b/backend/src/superTabs/voiceMessages.ts@@ -0,0 +1,83 @@+// A chat voice message: a WAV the phone recorded, sent as a file, whose words the server transcribes once before it is sent+// (store.ts readVoice). People hear it; agents and previews read what this file says about it.++/** The longest clip read as a voice message. The phone stops a take at 10:00; the margin covers the recorder's tail. */+export const MAX_VOICE_MS = 605_000++/** Bars in a voice message's waveform, as both apps draw it. */+export const VOICE_BARS = 48+/** A bar's height runs 0..VOICE_BAR_MAX. */+const VOICE_BAR_MAX = 31+const WAV_HEADER_BYTES = 44++/** `M:SS`, rounded down to the second: what a voice bubble shows and its label carries. */+export function voiceLength(durationMs: number): string {+ const seconds = Math.max(0, Math.floor(durationMs / 1000))+ return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`+}++/** What previews, activity and tags say for a voice message. */+export const voiceLabel = (durationMs: number) => `Voice message (${voiceLength(durationMs)})`++/**+ * How loud each of [bars] equal slices of a 16-bit PCM WAV is, 0..31: its RMS on the decibel scale the phone's live wave+ * uses (mobile/lib/phone/voice_wav.dart pcm16Level), so a sent message looks like the take did while it was recorded.+ * Worked out here from the bytes rather than taken from the sender, so every viewer draws the same file the same way.+ */+export function voiceWaveform(wav: Buffer, bars = VOICE_BARS): number[] {+ const samples = Math.max(0, Math.floor((wav.length - WAV_HEADER_BYTES) / 2))+ return Array.from({ length: bars }, (_, bar) => {+ const from = Math.floor((bar * samples) / bars), to = Math.max(from + 1, Math.floor(((bar + 1) * samples) / bars))+ let sum = 0, count = 0+ for (let i = from; i < Math.min(to, samples); i++, count++) sum += wav.readInt16LE(WAV_HEADER_BYTES + i * 2) ** 2+ const rms = count ? Math.sqrt(sum / count) : 0+ if (rms < 1) return 0+ const level = (20 * Math.log10(rms / 32768) - QUIET_DB) / (LOUD_DB - QUIET_DB)+ return Math.round(Math.min(1, Math.max(0, level)) * VOICE_BAR_MAX)+ })+}++const QUIET_DB = -55+const LOUD_DB = -10++/** The types a voice message's file may be uploaded as: the phone's recorder makes PCM WAV (mobile/lib/phone/voice_wav.dart). */+export const VOICE_TYPES: ReadonlySet<string> = new Set(['audio/wav', 'audio/x-wav', 'audio/wave'])+/** A bound on the bytes read for one clip, as the voice route's (routes/voice.ts MAX_AUDIO_BYTES): 10:05 of 16 kHz mono is 19.4 MB. */+export const MAX_VOICE_BYTES = 25 * 1024 * 1024+/** How long a call that claimed a file to transcribe it holds it before another may take over. */+export const VOICE_CLAIM_MS = 2 * 60_000++/** What a client and a message are told about a voice message's file. Why it has no words, when it has none: the sender's daily+ * voice limit was used, or the transcription service failed. Either is final for that file. */+export interface VoiceFacts { durationMs: number; waveform: number[]; transcribed: boolean; failure?: 'limit' | 'failed' }+/** As the file's row keeps it: with its words, which only message.send reads, to make them the message's text. */+export interface StoredVoice extends VoiceFacts { transcript: string | null }++/** The facts a row keeps, without its words; null for a row that is not a voice message's. */+export function voiceFacts(stored: unknown): VoiceFacts | null {+ if (!stored || typeof stored !== 'object') return null+ const { durationMs, waveform, transcribed, failure } = stored as Partial<StoredVoice>+ if (typeof durationMs !== 'number' || !Array.isArray(waveform) || typeof transcribed !== 'boolean') return null+ return { durationMs, waveform, transcribed, ...failure ? { failure } : {} }+}++/** The words a row keeps, or '' when it has none. */+export const voiceTranscript = (stored: unknown): string =>+ stored && typeof stored === 'object' && typeof (stored as StoredVoice).transcript === 'string' ? (stored as StoredVoice).transcript! : ''++/** The length of the voice message among [attachments] (a message's stored files), or null when it holds none. */+export function voiceMsOf(attachments: unknown): number | null {+ if (!Array.isArray(attachments)) return null+ for (const file of attachments) {+ const facts = file && typeof file === 'object' ? voiceFacts((file as { voice?: unknown }).voice) : null+ if (facts) return facts.durationMs+ }+ return null+}++/** What an agent is handed for a message: a voice message's words, marked as one with its length; any other message's text. */+export function agentText(text: string, attachments: unknown): string {+ const ms = voiceMsOf(attachments)+ if (ms === null) return text+ return text ? `${voiceLabel(ms)}: ${text}` : `${voiceLabel(ms)}, not transcribed.`+}diff --git a/backend/vitest.super-tabs.config.ts b/backend/vitest.super-tabs.config.tsindex 6020f4dee..3192a315f 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/workerIndexPlans.test.ts', 'integration/superTabReflection.test.ts','integration/superTabMessagePages.test.ts', 'integration/superTabDeliveryCompaction.test.ts', 'integration/superTabNoticeDigest.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/superTabTagsClear.test.ts', 'integration/swarmAgentTombstones.test.ts', 'integration/superTabPullRequestEvents.test.ts', 'integration/superTabProposals.test.ts', 'integration/superTabRequestReads.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/superTabTagsClear.test.ts', 'integration/swarmAgentTombstones.test.ts', 'integration/superTabPullRequestEvents.test.ts', 'integration/superTabProposals.test.ts', 'integration/superTabRequestReads.test.ts', 'integration/superTabVoiceMessages.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_chips.dart b/desktop/lib/super_tabs/attachment_chips.dartindex 568011de7..00be1d167 100644--- a/desktop/lib/super_tabs/attachment_chips.dart+++ b/desktop/lib/super_tabs/attachment_chips.dart@@ -16,6 +16,7 @@ import 'attachment_text_viewer.dart'import 'attachment_viewer.dart';import 'file_links.dart';import 'model.dart';+import 'voice_bubble.dart';/// The files a message carries, as Slack shows them: each picture on/// Harness's file storage in place (`attachment_image.dart`, on a computer),@@ -23,17 +24,20 @@ import 'model.dart';/// — a text file in a viewer that fetches it then, anything else in the/// browser. Links last minutes (backend fileLinks.ts): one about to end is/// asked for again, of message [messageId] in [conversationId], before it is-/// opened.+/// opened. A voice message's file is a bubble that plays it+/// (`voice_bubble.dart`), with the message's [words] behind its →A.class SuperTabAttachments extends StatelessWidget {const SuperTabAttachments(this.attachments, {super.key,this.conversationId,this.messageId,+ this.words = '',});final List<SuperTabAttachment> attachments;final String? conversationId, messageId;+ final String words;@overrideWidget build(BuildContext context) {@@ -44,15 +48,26 @@ class SuperTabAttachments extends StatelessWidget {messageId: messageId,fresh: fresh,);+ final voices = <SuperTabAttachment>[];final pictures = <SuperTabAttachment>[];final chips = <SuperTabAttachment>[];for (final file in attachments) {+ if (file.voice != null) {+ voices.add(file);+ continue;+ }(superTabShowsInPlace(ref(file)) ? pictures : chips).add(file);}return Column(crossAxisAlignment: CrossAxisAlignment.start,spacing: grid.AppDesktop.controlGap,children: [+ for (final file in voices)+ SuperTabVoiceBubble(+ key: ValueKey('super-attachment-voice:${file.id}'),+ file: ref(file),+ words: words,+ ),for (final file in pictures)SuperTabInlineImage(key: ValueKey('super-attachment-picture:${file.id}'),diff --git a/desktop/lib/super_tabs/message_link_preview.dart b/desktop/lib/super_tabs/message_link_preview.dartindex 1bbfd9650..b139244df 100644--- a/desktop/lib/super_tabs/message_link_preview.dart+++ b/desktop/lib/super_tabs/message_link_preview.dart@@ -4,6 +4,7 @@ import '../shared/theme/app_theme.dart' as grid;import '../widgets/desktop_chrome.dart';import 'message_link.dart';import 'model.dart';+import 'voice_length.dart';/// What a message link points at, for a reader. Anything the reader may not/// read, or that is gone, is the same [unavailable]: it gives no title, author@@ -110,9 +111,10 @@ class _Preview extends StatelessWidget {),const SizedBox(height: 2),Text(- message.text.trim().isEmpty- ? 'Attachment'- : message.text.replaceAll(RegExp(r'\s+'), ' '),+ switch (superTabQuoteText(message)) {+ final said when said.trim().isEmpty => 'Attachment',+ final said => said.replaceAll(RegExp(r'\s+'), ' '),+ },maxLines: 2,overflow: TextOverflow.ellipsis,style: DesktopChrome.metadata(),diff --git a/desktop/lib/super_tabs/message_row.dart b/desktop/lib/super_tabs/message_row.dartindex 31e6a4466..012b6e777 100644--- a/desktop/lib/super_tabs/message_row.dart+++ b/desktop/lib/super_tabs/message_row.dart@@ -22,6 +22,7 @@ import 'pull_requests.dart';import 'reactions.dart';import 'reminders.dart';import 'swarm_type.dart';+import 'voice_length.dart';/// The face [message]'s row shows for its sender, shown as [author]: the/// [agent] or [person] who wrote it, while they are in the Swarm.@@ -435,6 +436,7 @@ class _SuperTabMessageRowState extends State<SuperTabMessageRow> {Widget build(BuildContext context) {grid.AppTheme.watch(context);final message = widget.message;+ final voiceMessage = superTabVoiceOf(message) != null;final active = _hovered || _focused;// A message that tags the reader stands out, as Slack marks one that// names you: an accent bar on its edge over a wash of the accent.@@ -617,11 +619,12 @@ class _SuperTabMessageRowState extends State<SuperTabMessageRow> {widget.replyAuthor ??widget.reply?.authorName ??'Earlier message',- text:- widget.reply?.text ??- (widget.replyGone- ? 'The original message is no longer in this conversation.'- : 'The original message isn’t loaded.'),+ text: switch (widget.reply) {+ final reply? => superTabQuoteText(reply),+ null when widget.replyGone => 'The original message is no longer in this conversation.',+ null =>+ 'The original message isn’t loaded.',+ },onTap: widget.onOpenReply == null? null: () => widget.onOpenReply!(context),@@ -635,7 +638,9 @@ class _SuperTabMessageRowState extends State<SuperTabMessageRow> {style: DesktopChrome.metadata(),),),- if (message.text.isNotEmpty)+ // A voice message's text is its words, shown+ // under its bubble from →A.+ if (message.text.isNotEmpty && !voiceMessage)Opacity(opacity: widget.dimmed ? 0.6 : 1,child: ConstrainedBox(@@ -658,12 +663,15 @@ class _SuperTabMessageRowState extends State<SuperTabMessageRow> {opacity: widget.dimmed ? 0.6 : 1,child: Padding(padding: EdgeInsets.only(- top: message.text.isEmpty ? 0 : 8,+ top: message.text.isEmpty || voiceMessage+ ? 0+ : 8,),child: SuperTabAttachments(message.attachments,conversationId: message.conversationId,messageId: message.id,+ words: message.text,),),),diff --git a/desktop/lib/super_tabs/model.dart b/desktop/lib/super_tabs/model.dartindex 4b6fefb9b..1c47a0498 100644--- a/desktop/lib/super_tabs/model.dart+++ b/desktop/lib/super_tabs/model.dart@@ -1198,7 +1198,8 @@ class SuperTabAttachment {unlinked = row['unlinked'] == true,expiresAt = row['expiresAt'] is num? (row['expiresAt'] as num).toInt()- : null;+ : null,+ voice = SuperTabVoice.read(row['voice']);/// [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@@ -1218,6 +1219,11 @@ class SuperTabAttachment {/// When [link] stops opening, in epoch milliseconds.final int? expiresAt;+ /// The clip's facts when this file is a voice message (backend+ /// superTabs/voiceMessages.ts); null for any other file, and from an older+ /// server.+ final SuperTabVoice? voice;+/// The kinds the backend serves to be seen in place; every other kind is/// served as a download.bool get isImage => const {@@ -1228,6 +1234,57 @@ class SuperTabAttachment {}.contains(mime);}+/// A voice message's clip, as the server read it once before it was sent:+/// its words are the message's text.+class SuperTabVoice {+ const SuperTabVoice({+ required this.durationMs,+ required this.waveform,+ required this.transcribed,+ this.failure,+ });++ /// [row] as a clip, or null when any part of it does not read: the file is+ /// still shown, as an ordinary file.+ static SuperTabVoice? read(Object? row) {+ if (row is! Map) return null;+ final duration = row['durationMs'], bars = row['waveform'];+ final transcribed = row['transcribed'], failure = row['failure'];+ if (duration is! num ||+ duration < 0 ||+ bars is! List ||+ !bars.every((bar) => bar is num) ||+ transcribed is! bool) {+ return null;+ }+ return SuperTabVoice(+ durationMs: duration.toInt(),+ waveform: List.unmodifiable([+ for (final bar in bars.cast<num>()) bar.toInt().clamp(0, barMax),+ ]),+ transcribed: transcribed,+ failure: failure is String ? failure : null,+ );+ }++ /// The tallest bar of [waveform].+ static const barMax = 31;++ final int durationMs;++ /// The clip's loudness in bars, each 0 to [barMax].+ final List<int> waveform;++ /// Whether the message's text is the clip's words.+ final bool transcribed;++ /// Why it has no words: `limit` (the sender's daily voice quota was used)+ /// or `failed`.+ final String? failure;++ Duration get length => Duration(milliseconds: durationMs);+}+/// A message's files. A row that does not read is left out, so one bad row/// cannot hide the page of messages it came in.List<SuperTabAttachment> _attachments(Object? rows) => List.unmodifiable([diff --git a/desktop/lib/super_tabs/view.dart b/desktop/lib/super_tabs/view.dartindex 5d35fbd32..bf918496f 100644--- a/desktop/lib/super_tabs/view.dart+++ b/desktop/lib/super_tabs/view.dart@@ -1,12 +1,8 @@import 'dart:async';--import 'package:clock/clock.dart';-import 'dart:math' as math;import 'package:clock/clock.dart';import 'package:flutter/foundation.dart' show kIsWeb;-import 'package:clock/clock.dart';import 'package:flutter/material.dart';import 'package:flutter/rendering.dart'show RenderSliverMultiBoxAdaptor, SliverMultiBoxAdaptorParentData;@@ -91,6 +87,7 @@ import 'visible_tick.dart';import 'buddy_rows.dart';import 'task_groups.dart';import 'tasks_view.dart';+import 'voice_length.dart';import '../widgets/dial_list_scroll.dart';import '../widgets/laid_out_list.dart';@@ -4466,7 +4463,7 @@ class _SuperTabViewState extends State<SuperTabView>Expanded(child: SuperTabReplyPreview(author: 'Replying to ${_authorName(reply)}',- text: reply.text,+ text: superTabQuoteText(reply),),),fingerTarget(diff --git a/desktop/lib/super_tabs/voice_bubble.dart b/desktop/lib/super_tabs/voice_bubble.dartnew file mode 100644index 000000000..fc69525e3--- /dev/null+++ b/desktop/lib/super_tabs/voice_bubble.dart@@ -0,0 +1,356 @@+import 'dart:async';+import 'dart:io';++import 'package:flutter/foundation.dart';+import 'package:flutter/material.dart';+import 'package:path/path.dart' as p;+import 'package:video_player/video_player.dart';++import '../core/open_in_browser.dart';+import '../shared/theme/app_icons.dart';+import '../shared/theme/app_theme.dart' as grid;+import '../widgets/desktop_chrome.dart';+import '../widgets/finger_target.dart';+import 'attachment_viewer.dart';+import 'file_links.dart';+import 'model.dart';+import 'voice_length.dart';++/// The most a voice file is read into its local copy: the server takes a+/// clip of at most 10:05 of 16 kHz mono WAV, 19.4 MB. A larger one opens as+/// a file chip's does.+const kSuperTabVoiceMaxBytes = 20 * 1024 * 1024;++/// Makes the player for a voice message, from a local copy of its file (a+/// `file:` [source]), or in a browser, which has no files, from its link.+@visibleForTesting+VideoPlayerController Function(Uri source) superTabVoicePlayer = _player;++VideoPlayerController _player(Uri source) => source.isScheme('file')+ ? VideoPlayerController.file(File.fromUri(source))+ : VideoPlayerController.networkUrl(source);++/// video_player has no Linux or Windows implementation; there Play opens the+/// file in the system's player, as its chip would.+bool get _playsHere =>+ kIsWeb ||+ !const {+ TargetPlatform.linux,+ TargetPlatform.windows,+ }.contains(defaultTargetPlatform);++/// A voice message as Telegram draws one: `( ▶ ) ıllıllııılll 1:21 →A`. Play+/// plays a copy of the file read once to this computer, never the signed+/// link, which stops opening within minutes; →A shows the [words] under it.+class SuperTabVoiceBubble extends StatefulWidget {+ const SuperTabVoiceBubble({+ super.key,+ required this.file,+ required this.words,+ });++ /// A file whose [SuperTabAttachment.voice] is set.+ final SuperTabFileRef file;++ /// The message's text: the clip's words, when it was transcribed.+ final String words;++ @override+ State<SuperTabVoiceBubble> createState() => _SuperTabVoiceBubbleState();+}++class _SuperTabVoiceBubbleState extends State<SuperTabVoiceBubble> {+ VideoPlayerController? _playing;++ /// Where the local copy is; removed with the bubble.+ Directory? _copy;++ bool _loading = false, _showWords = false;++ /// The last read failed: the next one asks for a fresh link first.+ bool _stale = false;++ SuperTabVoice get _voice => widget.file.file.voice!;++ void _say(String text) =>+ ScaffoldMessenger.maybeOf(context)+ ?.showSnackBar(SnackBar(content: Text(text)));++ void _changed() {+ final player = _playing;+ if (player == null || !mounted) return;+ // A browser plays from a link, which may have stopped opening by the next+ // play: a finished one is made again then, from a link that opens now.+ if (kIsWeb && player.value.isCompleted) {+ _playing = null;+ player.removeListener(_changed);+ unawaited(player.dispose());+ }+ setState(() {});+ }++ Future<void> _toggle() async {+ if (_playing case final player?) {+ // A finished clip plays again from its start.+ return player.value.isPlaying ? player.pause() : player.play();+ }+ if (_loading) return;+ final ref = widget.file;+ if (ref.refused) {+ _say('This file isn’t on Harness’s file storage, so it wasn’t opened.');+ return;+ }+ setState(() => _loading = true);+ try {+ final link = await superTabOpenableLink(+ context,+ ref,+ stale: _stale,+ onProblem: _say,+ );+ if (link == null || !mounted) return;+ if (!_playsHere) {+ await openInBrowser(link);+ return;+ }+ await _start(kIsWeb ? link : await _copyOf(link));+ } on AttachmentTooLarge {+ if (!mounted) return;+ if (await superTabOpenableLink(context, ref) case final link?) {+ await openInBrowser(link);+ }+ } catch (_) {+ _stale = true;+ if (mounted) _say('Couldn’t play the voice message. Try again.');+ } finally {+ if (mounted) setState(() => _loading = false);+ }+ }++ /// The file at [link] read to this computer, once for this bubble.+ Future<Uri> _copyOf(Uri link) async {+ final bytes = await readAttachment(link, maxBytes: kSuperTabVoiceMaxBytes);+ final dir = _copy ??= await Directory.systemTemp.createTemp(+ 'harness-voice-',+ );+ final name = p.extension(widget.file.file.name);+ final copy = File(p.join(dir.path, 'voice${name.isEmpty ? '.wav' : name}'));+ await copy.writeAsBytes(bytes, flush: true);+ if (!mounted) _forget();+ return copy.uri;+ }++ Future<void> _start(Uri source) async {+ if (!mounted) return;+ final player = superTabVoicePlayer(source);+ try {+ await player.initialize();+ } catch (_) {+ unawaited(player.dispose());+ rethrow;+ }+ if (!mounted) {+ unawaited(player.dispose());+ return;+ }+ _playing = player..addListener(_changed);+ await player.play();+ }++ void _forget() {+ final dir = _copy;+ _copy = null;+ if (dir != null) {+ unawaited(dir.delete(recursive: true).then((_) {}, onError: (_) {}));+ }+ }++ @override+ void dispose() {+ final player = _playing;+ _playing = null;+ if (player != null) {+ player.removeListener(_changed);+ unawaited(player.dispose());+ }+ _forget();+ super.dispose();+ }++ @override+ Widget build(BuildContext context) {+ grid.AppTheme.watch(context);+ final id = widget.file.file.id, voice = _voice;+ final value = _playing?.value;+ final playing = value?.isPlaying ?? false;+ final length = value != null && value.duration > Duration.zero+ ? value.duration+ : voice.length;+ // At rest the bubble says the clip's length; once begun, how far in.+ final position = value == null || value.isCompleted+ ? Duration.zero+ : value.position;+ final played = length > Duration.zero+ ? (position.inMilliseconds / length.inMilliseconds).clamp(0.0, 1.0)+ : 0.0;+ final finger = workspaceIsCompact(context);+ final bubble = Material(+ // The file chip's fill: the bubble is the chip of a voice file.+ color: DesktopChrome.foreground.withValues(alpha: .055),+ shape: const StadiumBorder(),+ child: Padding(+ padding: const EdgeInsets.symmetric(+ horizontal: grid.AppDesktop.menuInset,+ vertical: grid.AppDesktop.menuInset,+ ),+ child: Row(+ mainAxisSize: MainAxisSize.min,+ children: [+ fingerTarget(+ enabled: finger,+ _PlayButton(+ key: ValueKey('super-voice-play:$id'),+ playing: playing,+ onPressed: _loading ? null : () => unawaited(_toggle()),+ ),+ ),+ const SizedBox(width: grid.AppDesktop.controlGap),+ _Waveform(id: id, bars: voice.waveform, played: played),+ const SizedBox(width: grid.AppDesktop.controlGap),+ Text(+ superTabVoiceLength(position > Duration.zero ? position : length),+ style: DesktopChrome.metadata().copyWith(+ fontFeatures: const [FontFeature.tabularFigures()],+ ),+ ),+ const SizedBox(width: grid.AppDesktop.groupGap),+ fingerTarget(+ enabled: finger,+ Tooltip(+ message: _showWords ? 'Hide words' : 'Show words',+ child: TextButton(+ key: ValueKey('super-voice-words-toggle:$id'),+ onPressed: () => setState(() => _showWords = !_showWords),+ style: TextButton.styleFrom(+ minimumSize: const Size.square(+ DesktopChrome.compactControlHeight,+ ),+ padding: const EdgeInsets.symmetric(+ horizontal: grid.AppDesktop.menuInset,+ ),+ tapTargetSize: MaterialTapTargetSize.shrinkWrap,+ visualDensity: VisualDensity.compact,+ ),+ child: Text(+ '→A',+ style: DesktopChrome.control(+ color: _showWords+ ? DesktopChrome.accent+ : DesktopChrome.muted,+ medium: true,+ ),+ ),+ ),+ ),+ ),+ ],+ ),+ ),+ );+ if (!_showWords) return bubble;+ return Column(+ crossAxisAlignment: CrossAxisAlignment.start,+ spacing: grid.AppDesktop.controlGap,+ children: [+ bubble,+ ConstrainedBox(+ // The width a message's text keeps (message_row.dart).+ constraints: const BoxConstraints(maxWidth: 960),+ // The chat's selection area makes the words selectable.+ child: voice.transcribed+ ? Text(+ widget.words,+ key: ValueKey('super-voice-words:$id'),+ style: DesktopChrome.text(),+ )+ : Text(+ voice.failure == 'limit'+ ? 'Not transcribed: today’s voice limit was used'+ : 'Not transcribed',+ key: ValueKey('super-voice-words:$id'),+ style: DesktopChrome.metadata(),+ ),+ ),+ ],+ );+ }+}++class _PlayButton extends StatelessWidget {+ const _PlayButton({super.key, required this.playing, this.onPressed});+ final bool playing;+ final VoidCallback? onPressed;++ @override+ Widget build(BuildContext context) => Tooltip(+ message: playing ? 'Pause' : 'Play',+ child: Material(+ color: DesktopChrome.accent,+ shape: const CircleBorder(),+ child: InkWell(+ customBorder: const CircleBorder(),+ onTap: onPressed,+ child: SizedBox.square(+ dimension: DesktopChrome.compactControlHeight,+ child: Icon(+ playing ? AppIcons.pause : AppIcons.play,+ size: AppIcons.metadataSize,+ color: DesktopChrome.onSelection,+ ),+ ),+ ),+ ),+ );+}++/// The clip's loudness, one bar per slice: the [played] fraction in the+/// accent, the rest muted.+class _Waveform extends StatelessWidget {+ const _Waveform({required this.id, required this.bars, required this.played});+ final String id;+ final List<int> bars;+ final double played;++ /// A bar, and the gap after it, are each a list row's gap wide; the+ /// tallest is an icon's height, the quietest a dot.+ static const _bar = grid.AppDesktop.listRowGap;+ static const _height = AppIcons.controlSize;++ @override+ Widget build(BuildContext context) {+ final filled = (played * bars.length).floor();+ return SizedBox(+ height: _height,+ child: Row(+ mainAxisSize: MainAxisSize.min,+ spacing: _bar,+ children: [+ for (final (i, bar) in bars.indexed)+ SizedBox(+ width: _bar,+ height: _bar + (_height - _bar) * bar / SuperTabVoice.barMax,+ child: DecoratedBox(+ key: ValueKey('super-voice-bar:$id:$i'),+ decoration: BoxDecoration(+ color: i < filled+ ? DesktopChrome.accent+ : DesktopChrome.muted,+ borderRadius: BorderRadius.circular(_bar / 2),+ ),+ ),+ ),+ ],+ ),+ );+ }+}diff --git a/desktop/lib/super_tabs/voice_length.dart b/desktop/lib/super_tabs/voice_length.dartnew file mode 100644index 000000000..19a7041bb--- /dev/null+++ b/desktop/lib/super_tabs/voice_length.dart@@ -0,0 +1,28 @@+import 'model.dart';++/// [length] as `M:SS`, rounded down to the second, as the server writes it in+/// agents' text and previews (backend superTabs/voiceMessages.ts).+String superTabVoiceLength(Duration length) {+ final seconds = length.inSeconds;+ return '${seconds ~/ 60}:${(seconds % 60).toString().padLeft(2, '0')}';+}++/// What a quote of a voice message says instead of its words.+String superTabVoiceLabel(Duration length) =>+ 'Voice message (${superTabVoiceLength(length)})';++/// [message]'s voice file, when it is a voice message.+SuperTabAttachment? superTabVoiceOf(SuperTabMessage message) {+ for (final file in message.attachments) {+ if (file.voice != null) return file;+ }+ return null;+}++/// [message] as a quote shows it: a voice message by its length, any other by+/// its text.+String superTabQuoteText(SuperTabMessage message) =>+ switch (superTabVoiceOf(message)?.voice) {+ final voice? => superTabVoiceLabel(voice.length),+ null => message.text,+ };diff --git a/desktop/macos/Flutter/GeneratedPluginRegistrant.swift b/desktop/macos/Flutter/GeneratedPluginRegistrant.swiftindex d3e11baa8..21d01c51a 100644--- a/desktop/macos/Flutter/GeneratedPluginRegistrant.swift+++ b/desktop/macos/Flutter/GeneratedPluginRegistrant.swift@@ -10,6 +10,7 @@ import file_selector_macosimport package_info_plusimport screen_retriever_macosimport url_launcher_macos+import video_player_avfoundationimport webview_flutter_wkwebviewimport window_manager@@ -19,6 +20,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))ScreenRetrieverMacosPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverMacosPlugin"))UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))+ VideoPlayerPlugin.register(with: registry.registrar(forPlugin: "VideoPlayerPlugin"))WebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "WebViewFlutterPlugin"))WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))}diff --git a/desktop/pubspec.lock b/desktop/pubspec.lockindex b6fbfd0c0..836ff91b2 100644--- a/desktop/pubspec.lock+++ b/desktop/pubspec.lock@@ -89,6 +89,14 @@ packages:url: "https://pub.dev"source: hostedversion: "2.9.0"+ csslib:+ dependency: transitive+ description:+ name: csslib+ sha256: "09bad715f418841f976c77db72d5398dc1253c21fb9c0c7f0b0b985860b2d58e"+ url: "https://pub.dev"+ source: hosted+ version: "1.0.2"cupertino_icons:dependency: "direct main"description:@@ -298,6 +306,14 @@ packages:url: "https://pub.dev"source: hostedversion: "17.5.0"+ html:+ dependency: transitive+ description:+ name: html+ sha256: "43b67b8f43321ab066817dfac5619596c98bb1b61624e77203bb4351785f9699"+ url: "https://pub.dev"+ source: hosted+ version: "0.15.7"http:dependency: transitivedescription:@@ -788,6 +804,46 @@ packages:url: "https://pub.dev"source: hostedversion: "2.4.2"+ video_player:+ dependency: "direct main"+ description:+ name: video_player+ sha256: "07ae1e03f2d4c8e2e9a75ee678f90acbc4506d4458a9c628512d119a1eee5754"+ url: "https://pub.dev"+ source: hosted+ version: "2.14.1"+ video_player_android:+ dependency: transitive+ description:+ name: video_player_android+ sha256: d27054dea34d748a44f06d433a57005d2aac69944485b0abbaed3303dbcfa3f1+ url: "https://pub.dev"+ source: hosted+ version: "2.12.2"+ video_player_avfoundation:+ dependency: transitive+ description:+ name: video_player_avfoundation+ sha256: "7d620d8c69d00c158a5c57bf3d2a051833b547631203ff724664fbf301024bc1"+ url: "https://pub.dev"+ source: hosted+ version: "2.12.3"+ video_player_platform_interface:+ dependency: transitive+ description:+ name: video_player_platform_interface+ sha256: "92c0fbabe20c788e71fd10d26cea998d0d253282e65d145aed0818731cf593ce"+ url: "https://pub.dev"+ source: hosted+ version: "6.9.0"+ video_player_web:+ dependency: transitive+ description:+ name: video_player_web+ sha256: "9f3c00be2ef9b76a95d94ac5119fb843dca6f2c69e6c9968f6f2b6c9e7afbdeb"+ url: "https://pub.dev"+ source: hosted+ version: "2.4.0"vm_service:dependency: transitivedescription:@@ -909,4 +965,4 @@ packages:version: "0.0.6"sdks:dart: ">=3.13.0 <4.0.0"- flutter: ">=3.44.0"+ flutter: ">=3.47.0"diff --git a/desktop/pubspec.yaml b/desktop/pubspec.yamlindex bab709740..269cdf114 100644--- a/desktop/pubspec.yaml+++ b/desktop/pubspec.yaml@@ -108,6 +108,13 @@ dependencies:# Swarm chat (super_tabs/zone_time.dart), daylight saving included. Pure# Dart; its 10-year data is a deferred import, out of the web's first load.timezone: ^0.11.2+ # Plays a chat's voice messages (`super_tabs/voice_bubble.dart`), from a+ # local copy of the file. A native plugin, first-party Flutter: AVPlayer on+ # macOS, through its Swift package, so it lands in the macOS SPM package list+ # and GeneratedPluginRegistrant; the browser's own media element on the web.+ # It has no Linux or Windows implementation, so there the bubble's play+ # opens the file as a file chip does. See CLAUDE.md on what a plugin costs.+ video_player: ^2.14.0dev_dependencies:flutter_test:diff --git a/desktop/test/screenshots/voice-messages/voice-chat-dark.png b/desktop/test/screenshots/voice-messages/voice-chat-dark.pngnew file mode 100644index 000000000..13137195dBinary files /dev/null and b/desktop/test/screenshots/voice-messages/voice-chat-dark.png differdiff --git a/desktop/test/screenshots/voice-messages/voice-chat-light.png b/desktop/test/screenshots/voice-messages/voice-chat-light.pngnew file mode 100644index 000000000..3ac92456dBinary files /dev/null and b/desktop/test/screenshots/voice-messages/voice-chat-light.png differdiff --git a/desktop/test/screenshots/voice-messages/voice-playing-dark.png b/desktop/test/screenshots/voice-messages/voice-playing-dark.pngnew file mode 100644index 000000000..ce6d0c8caBinary files /dev/null and b/desktop/test/screenshots/voice-messages/voice-playing-dark.png differdiff --git a/desktop/test/screenshots/voice-messages/voice-playing-light.png b/desktop/test/screenshots/voice-messages/voice-playing-light.pngnew file mode 100644index 000000000..ecc75825eBinary files /dev/null and b/desktop/test/screenshots/voice-messages/voice-playing-light.png differdiff --git a/desktop/test/screenshots/voice-messages/voice-words-dark.png b/desktop/test/screenshots/voice-messages/voice-words-dark.pngnew file mode 100644index 000000000..0bb22ee6fBinary files /dev/null and b/desktop/test/screenshots/voice-messages/voice-words-dark.png differdiff --git a/desktop/test/screenshots/voice-messages/voice-words-light.png b/desktop/test/screenshots/voice-messages/voice-words-light.pngnew file mode 100644index 000000000..48b971177Binary files /dev/null and b/desktop/test/screenshots/voice-messages/voice-words-light.png differdiff --git a/desktop/test/super_tab_voice_message_shots_test.dart b/desktop/test/super_tab_voice_message_shots_test.dartnew file mode 100644index 000000000..c2cc3b9e2--- /dev/null+++ b/desktop/test/super_tab_voice_message_shots_test.dart@@ -0,0 +1,176 @@+import 'dart:io';+import 'dart:typed_data';++import 'package:flutter/material.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness/shared/theme/app_theme.dart' as grid;+import 'package:harness/super_tabs/attachment_viewer.dart';+import 'package:harness/super_tabs/view.dart';+import 'package:harness/super_tabs/voice_bubble.dart';+import 'package:video_player/video_player.dart';++import 'support/capture.dart';+import 'support/super_tab_fixture.dart';++/// Set to a folder to write the voice message PR's pictures there.+final shots = Platform.environment['SUPER_TAB_VOICE_SHOTS'];++/// A chat with a voice message, as the PR shows it (#2584): at rest, with its+/// words open under →A, and playing at 40%, in both appearances.+void main() {+ setUpAll(() => loadCaptureFonts(shots));++ for (final brightness in Brightness.values) {+ testWidgets(+ 'voice messages in ${brightness.name}',+ (tester) async {+ final previous = grid.AppTheme.brightness.value;+ grid.AppTheme.brightness.value = brightness;+ addTearDown(() => grid.AppTheme.brightness.value = previous);+ tester.view.devicePixelRatio = 2;+ tester.view.physicalSize = const Size(2400, 1400);+ addTearDown(tester.view.reset);++ final fetcher = attachmentFetcher, makePlayer = superTabVoicePlayer;+ _Player? player;+ attachmentFetcher = (url, {maxBytes}) async => Uint8List(64);+ superTabVoicePlayer = (_) => player = _Player();+ addTearDown(() {+ attachmentFetcher = fetcher;+ superTabVoicePlayer = makePlayer;+ });++ final server = ConversationServer();+ (server.state['people'] as List).add({+ 'id': 'dee',+ 'email': '[email protected]',+ 'role': 'collaborator',+ });+ final now = DateTime.now().toUtc();+ void say(+ String text,+ Map<String, Object> author, {+ List<Map<String, Object>> files = const [],+ String? replyTo,+ int secondsAgo = 0,+ }) {+ server.append(text, attachments: files);+ server.messages.last+ ..['author'] = author+ ..['createdAt'] = now+ .subtract(Duration(seconds: secondsAgo))+ .toIso8601String();+ if (replyTo != null) server.messages.last['replyTo'] = replyTo;+ }++ const dee = {'kind': 'person', 'id': 'dee', 'name': '[email protected]'};+ const pm = {+ 'kind': 'agent',+ 'id': 'pm',+ 'name': 'Lead',+ 'displayName': 'River',+ };+ // A spoken message's loudness: quiet at the edges, louder in phrases.+ final waveform = [+ for (var i = 0; i < 48; i++)+ (4 + 24 * ((i * 37) % 11) / 10 * (1 - (i - 24).abs() / 30)).round(),+ ];+ say('The sidebar is fixed on my phone now.', dee, secondsAgo: 400);+ say(+ 'The login screen still hangs after I pick Google. Can someone '+ 'look at it today, and then at the sidebar on the phone? It '+ 'scrolls under the keyboard.',+ dee,+ secondsAgo: 300,+ files: [+ {+ 'id': 'v1',+ 'name': 'Voice message.wav',+ 'mime': 'audio/wav',+ 'size': 2592044,+ 'url': 'https://cdn.harness.autonomous.ai/v1/Voice%20message.wav',+ 'voice': {+ 'durationMs': 81000,+ 'waveform': waveform,+ 'transcribed': true,+ },+ },+ ],+ );+ say(+ 'On it: the login hang first, then the sidebar.',+ pm,+ replyTo: 'message-2',+ secondsAgo: 240,+ );+ ((server.state['conversations'] as List)[0] as Map)['leadId'] = 'pm';+ final c = server.controller(account: 'dee');+ await c.refresh();+ await tester.pumpWidget(+ MaterialApp(+ debugShowCheckedModeBanner: false,+ theme: grid.buildAppTheme(brightness: brightness),+ home: Scaffold(+ body: SuperTabView(+ controller: c,+ onInvite: () {},+ onAddAgent: (_) {},+ onOpenTerminal: (_) {},+ machineName: (_) => 'Mac Studio',+ ),+ ),+ ),+ );+ await tester.pumpAndSettle();+ await captureWindow(tester, shots, 'voice-chat-${brightness.name}');++ final words = find.byKey(const ValueKey('super-voice-words-toggle:v1'));+ await tester.tap(words);+ await tester.pumpAndSettle();+ await captureWindow(tester, shots, 'voice-words-${brightness.name}');+ await tester.tap(words);+ await tester.pumpAndSettle();++ await tester.tap(find.byTooltip('Play'));+ for (var i = 0; i < 20 && player == null; i++) {+ await tester.runAsync(+ () => Future<void>.delayed(const Duration(milliseconds: 10)),+ );+ await tester.pump();+ }+ player!.value = player!.value.copyWith(+ position: const Duration(milliseconds: 32400),+ );+ await tester.pumpAndSettle();+ expect(find.byTooltip('Pause'), findsOneWidget);+ await captureWindow(tester, shots, 'voice-playing-${brightness.name}');++ await tester.pumpWidget(const SizedBox());+ await c.flush();+ c.dispose();+ await tester.runAsync(+ () => Future<void>.delayed(const Duration(milliseconds: 50)),+ );+ },+ skip: shots == null,+ variant: TargetPlatformVariant.only(TargetPlatform.macOS),+ );+ }+}++/// A player that plays nothing and stays where it is put.+class _Player extends VideoPlayerController {+ _Player() : super.file(File('/voice/clip.wav'));++ @override+ Future<void> initialize() async => value = value.copyWith(+ isInitialized: true,+ duration: const Duration(seconds: 81),+ );++ @override+ Future<void> play() async => value = value.copyWith(isPlaying: true);++ @override+ Future<void> pause() async => value = value.copyWith(isPlaying: false);+}diff --git a/desktop/test/super_tab_voice_message_test.dart b/desktop/test/super_tab_voice_message_test.dartnew file mode 100644index 000000000..4bf72c776--- /dev/null+++ b/desktop/test/super_tab_voice_message_test.dart@@ -0,0 +1,513 @@+// Voice messages in a Swarm conversation (#2584): a message whose one file+// carries `voice`, its words the message's text. The model reads the clip's+// facts, every quote of one says its length rather than its words, and the+// chat draws it as a bubble that plays a local copy of the file.+import 'dart:io';++import 'package:flutter/foundation.dart';+import 'package:flutter/material.dart';+import 'package:flutter/services.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness/core/open_in_browser.dart';+import 'package:harness/super_tabs/attachment_viewer.dart';+import 'package:harness/super_tabs/controller.dart';+import 'package:harness/super_tabs/message_link.dart';+import 'package:harness/super_tabs/message_row.dart';+import 'package:harness/super_tabs/model.dart';+import 'package:harness/super_tabs/message_text.dart';+import 'package:harness/super_tabs/view.dart';+import 'package:harness/super_tabs/voice_bubble.dart';+import 'package:harness/super_tabs/voice_length.dart';+import 'package:harness/widgets/desktop_chrome.dart';+import 'package:video_player/video_player.dart';++import 'support/super_tab_fixture.dart';++const _words = 'Ship the login fix today, then look at the sidebar.';++/// A voice file as the backend's message reads give it (backend messages.ts).+Map<String, dynamic> voiceRow({+ String id = 'v1',+ int durationMs = 81000,+ bool transcribed = true,+ String? failure,+ Object? waveform,+}) => {+ 'id': id,+ 'name': 'Voice message.wav',+ 'mime': 'audio/wav',+ 'size': 2592044,+ 'url': 'https://cdn.harness.autonomous.ai/$id/Voice%20message.wav',+ 'voice': {+ 'durationMs': durationMs,+ 'waveform': waveform ?? [for (var i = 0; i < 48; i++) (i * 7) % 32],+ 'transcribed': transcribed,+ 'failure': ?failure,+ },+};++SuperTabAttachment attachment(Map<String, dynamic> row) =>+ SuperTabAttachment.fromJson(row);++void main() {+ late SuperTabController c;++ Future<void> show(WidgetTester tester, ConversationServer server) async {+ await tester.binding.setSurfaceSize(const Size(1200, 900));+ addTearDown(() => tester.binding.setSurfaceSize(null));+ c = server.controller();+ await c.refresh();+ await tester.pumpWidget(+ MaterialApp(+ home: Scaffold(+ body: SuperTabView(+ controller: c,+ onInvite: () {},+ onAddAgent: (_) {},+ onOpenTerminal: (_) {},+ machineName: (_) => 'Team Mac',+ ),+ ),+ ),+ );+ await tester.pumpAndSettle();+ }++ Future<void> close(WidgetTester tester) async {+ await tester.pumpWidget(const SizedBox());+ await c.flush();+ c.dispose();+ }++ ConversationServer withVoice() =>+ ConversationServer()..append(_words, attachments: [voiceRow()]);++ group('the model', () {+ test('reads a voice file’s length, waveform and whether it has words', () {+ final voice = attachment(voiceRow()).voice!;+ expect(voice.durationMs, 81000);+ expect(voice.waveform, hasLength(48));+ expect(voice.waveform.take(3), [0, 7, 14]);+ expect(voice.transcribed, isTrue);+ expect(voice.failure, isNull);+ });++ test('a file with no words says why', () {+ final voice = attachment(voiceRow(transcribed: false, failure: 'limit'))+ .voice!;+ expect(voice.transcribed, isFalse);+ expect(voice.failure, 'limit');+ });++ test('a waveform value out of range is held to 0..31', () {+ final voice = attachment(voiceRow(waveform: [-4, 0, 31, 40, 12.6]))+ .voice!;+ expect(voice.waveform, [0, 0, 31, 31, 12]);+ });++ test('an old file has no voice', () {+ final row = voiceRow()..remove('voice');+ expect(attachment(row).voice, isNull);+ });++ test('a malformed voice reads as none and keeps the file', () {+ for (final bad in <Object?>[+ 'voice',+ 42,+ <String, Object?>{'durationMs': 'long', 'waveform': <int>[]},+ <String, Object?>{'durationMs': 81000, 'waveform': 'bars'},+ <String, Object?>{'durationMs': 81000, 'transcribed': true},+ <String, Object?>{+ 'durationMs': -1,+ 'waveform': <int>[],+ 'transcribed': true,+ },+ <String, Object?>{+ 'durationMs': 81000,+ 'waveform': ['a'],+ 'transcribed': 'yes',+ },+ ]) {+ final file = attachment(voiceRow()..['voice'] = bad);+ expect(file.voice, isNull, reason: '$bad');+ expect(file.id, 'v1', reason: '$bad');+ }+ final message = SuperTabMessage.fromJson({+ 'id': 'm1',+ 'conversationId': 'lead',+ 'sequence': 1,+ 'text': '',+ 'author': {'kind': 'person', 'id': 'owner', 'name': 'Owner'},+ 'attachments': [voiceRow()..['voice'] = 'broken'],+ });+ expect(message.attachments.single.voice, isNull);+ });++ test('a message’s voice file is the one that carries voice', () {+ SuperTabMessage message(List<Map<String, dynamic>> files) =>+ SuperTabMessage.fromJson({+ 'id': 'm1',+ 'conversationId': 'lead',+ 'sequence': 1,+ 'text': _words,+ 'author': {'kind': 'person', 'id': 'owner', 'name': 'Owner'},+ 'attachments': files,+ });+ expect(superTabVoiceOf(message([voiceRow()]))?.id, 'v1');+ expect(superTabVoiceOf(message([voiceRow()..remove('voice')])), isNull);+ expect(superTabVoiceOf(message([])), isNull);+ });+ });++ group('the length', () {+ test('is M:SS, rounded down to the second', () {+ expect(superTabVoiceLength(const Duration(seconds: 81)), '1:21');+ expect(superTabVoiceLength(const Duration(seconds: 600)), '10:00');+ expect(superTabVoiceLength(const Duration(milliseconds: 999)), '0:00');+ expect(superTabVoiceLength(const Duration(milliseconds: 81999)), '1:21');+ expect(superTabVoiceLength(const Duration(seconds: 9)), '0:09');+ expect(superTabVoiceLength(Duration.zero), '0:00');+ });++ test('labels a voice message', () {+ expect(+ superTabVoiceLabel(const Duration(seconds: 81)),+ 'Voice message (1:21)',+ );+ });+ });++ group('quotes of a voice message', () {+ testWidgets('a reply quotes its length, not its words', (tester) async {+ final server = withVoice()..append('On it.');+ server.messages.last['replyTo'] = 'message-1';+ await show(tester, server);+ final quote = find.descendant(+ of: find.byKey(const ValueKey('super-message-message-2')),+ matching: find.byType(SuperTabReplyPreview),+ );+ expect(+ find.descendant(of: quote, matching: find.text('Voice message (1:21)')),+ findsOneWidget,+ );+ expect(+ find.descendant(of: quote, matching: find.textContaining('Ship')),+ findsNothing,+ );+ await close(tester);+ });++ testWidgets('the composer’s Replying to quotes its length', (tester) async {+ await show(tester, withVoice());+ final row = find.byKey(const ValueKey('super-message-message-1'));+ tester+ .widget<Focus>(+ find.descendant(of: row, matching: find.byType(Focus)).first,+ )+ .focusNode!+ .requestFocus();+ await tester.pump();+ await tester.sendKeyEvent(LogicalKeyboardKey.keyR);+ await tester.pump();+ final quote = find.ancestor(+ of: find.textContaining('Replying to'),+ matching: find.byType(SuperTabReplyPreview),+ );+ expect(quote, findsOneWidget);+ expect(+ find.descendant(of: quote, matching: find.text('Voice message (1:21)')),+ findsOneWidget,+ );+ await close(tester);+ });++ testWidgets('a link to it previews its length', (tester) async {+ const link = SwarmMessageLink(+ tabId: 'tab',+ conversationId: 'lead',+ messageId: 'message-1',+ );+ await show(tester, withVoice()..append('See ${link.url}'));+ final preview = find.byKey(+ const ValueKey('super-message-link-preview-message-1'),+ );+ expect(preview, findsOneWidget);+ expect(+ find.descendant(+ of: preview,+ matching: find.text('Voice message (1:21)'),+ ),+ findsOneWidget,+ );+ await close(tester);+ });+ });++ group('the bubble', () {+ final url = Uri.parse(+ 'https://cdn.harness.autonomous.ai/v1/Voice%20message.wav',+ );+ final clip = Uint8List.fromList([for (var i = 0; i < 64; i++) i]);+ final fetched = <Uri>[], opened = <Uri>[], sources = <Uri>[];+ late _Player player;++ setUp(() {+ fetched.clear();+ opened.clear();+ sources.clear();+ final fetcher = attachmentFetcher,+ browser = browserOpener,+ makePlayer = superTabVoicePlayer;+ attachmentFetcher = (url, {maxBytes}) async {+ fetched.add(url);+ return clip;+ };+ browserOpener = (url) async {+ opened.add(url);+ return true;+ };+ superTabVoicePlayer = (source) {+ sources.add(source);+ return player = _Player();+ };+ addTearDown(() {+ attachmentFetcher = fetcher;+ browserOpener = browser;+ superTabVoicePlayer = makePlayer;+ });+ });++ final bubble = find.byKey(const ValueKey('super-attachment-voice:v1'));+ final toggle = find.byKey(const ValueKey('super-voice-words-toggle:v1'));+ final bars = find.byWidgetPredicate(+ (widget) => switch (widget.key) {+ ValueKey<String>(:final value) => value.startsWith(+ 'super-voice-bar:v1:',+ ),+ _ => false,+ },+ );+ Color? colorOf(Element bar) =>+ ((bar.widget as DecoratedBox).decoration as BoxDecoration).color;++ /// Lets the copy be written to disk, which the test's clock does not run.+ Future<void> settleFiles(WidgetTester tester) async {+ for (var i = 0; i < 20 && sources.isEmpty; i++) {+ await tester.runAsync(+ () => Future<void>.delayed(const Duration(milliseconds: 10)),+ );+ await tester.pump();+ }+ await tester.pump();+ }++ testWidgets('shows play, the waveform, its length and →A, and no text', (+ tester,+ ) async {+ await show(tester, withVoice());+ final row = find.byKey(const ValueKey('super-message-message-1'));+ expect(find.descendant(of: row, matching: bubble), findsOneWidget);+ expect(+ find.descendant(of: bubble, matching: find.byTooltip('Play')),+ findsOneWidget,+ );+ expect(bars, findsNWidgets(48));+ expect(+ find.descendant(of: bubble, matching: find.text('1:21')),+ findsOneWidget,+ );+ expect(+ find.descendant(of: bubble, matching: find.text('→A')),+ findsOneWidget,+ );+ expect(find.text(_words), findsNothing);+ expect(+ find.descendant(of: row, matching: find.byType(SuperTabMessageText)),+ findsNothing,+ );+ // Not a file chip as well.+ expect(find.text('Voice message.wav'), findsNothing);+ expect(fetched, isEmpty);+ await close(tester);+ });++ testWidgets('→A shows the words under the bubble, and again hides them', (+ tester,+ ) async {+ await show(tester, withVoice());+ expect(find.byTooltip('Show words'), findsOneWidget);+ await tester.tap(toggle);+ await tester.pumpAndSettle();+ expect(find.text(_words), findsOneWidget);+ expect(find.byTooltip('Hide words'), findsOneWidget);+ expect(+ tester.getTopLeft(find.text(_words)).dy,+ greaterThan(tester.getBottomLeft(toggle).dy),+ );+ await tester.tap(toggle);+ await tester.pumpAndSettle();+ expect(find.text(_words), findsNothing);+ await close(tester);+ });++ testWidgets('a clip with no words says so, and why', (tester) async {+ await show(+ tester,+ ConversationServer()+ ..append('', attachments: [voiceRow(transcribed: false)])+ ..append(+ '',+ attachments: [+ voiceRow(id: 'v2', transcribed: false, failure: 'limit'),+ ],+ ),+ );+ await tester.tap(toggle);+ await tester.tap(+ find.byKey(const ValueKey('super-voice-words-toggle:v2')),+ );+ await tester.pumpAndSettle();+ expect(find.text('Not transcribed'), findsOneWidget);+ expect(+ find.text('Not transcribed: today’s voice limit was used'),+ findsOneWidget,+ );+ await close(tester);+ });++ testWidgets('Play reads the file into a local copy and plays that', (+ tester,+ ) async {+ await show(tester, withVoice());+ await tester.tap(find.byTooltip('Play'));+ await settleFiles(tester);++ expect(fetched, [url]);+ final source = sources.single;+ expect(source.isScheme('file'), isTrue, reason: '$source');+ final copy = File.fromUri(source);+ expect(copy.readAsBytesSync(), clip);+ expect(player.calls, ['initialize', 'play']);+ expect(+ find.descendant(of: bubble, matching: find.byTooltip('Pause')),+ findsOneWidget,+ );++ await tester.tap(find.byTooltip('Pause'));+ await tester.pump();+ expect(player.calls.last, 'pause');+ expect(find.byTooltip('Play'), findsOneWidget);+ // Playing again plays the same copy, not another read.+ await tester.tap(find.byTooltip('Play'));+ await tester.pump();+ expect(player.calls.last, 'play');+ expect(fetched, hasLength(1));++ // The copy goes with the bubble.+ await close(tester);+ await tester.runAsync(+ () => Future<void>.delayed(const Duration(milliseconds: 50)),+ );+ expect(player.disposed, isTrue);+ expect(copy.existsSync(), isFalse);+ });++ testWidgets('the played part of the waveform fills in the accent', (+ tester,+ ) async {+ await show(tester, withVoice());+ final at = find.descendant(of: bubble, matching: bars);+ expect(at.evaluate().map(colorOf).toSet(), {+ DesktopChrome.muted,+ }, reason: 'nothing played yet');+ await tester.tap(find.byTooltip('Play'));+ await settleFiles(tester);+ player.value = player.value.copyWith(+ position: const Duration(milliseconds: 32400),+ );+ await tester.pump();++ final colors = at.evaluate().map(colorOf).toList();+ expect(colors.take(19).toSet(), {DesktopChrome.accent});+ expect(colors.skip(19).toSet(), {DesktopChrome.muted});+ // Playing, it counts up.+ expect(+ find.descendant(of: bubble, matching: find.text('0:32')),+ findsOneWidget,+ );++ player.finish();+ await tester.pump();+ expect(+ find.descendant(of: bubble, matching: find.text('1:21')),+ findsOneWidget,+ );+ expect(find.byTooltip('Play'), findsOneWidget);+ await close(tester);+ });++ testWidgets(+ 'on Linux and Windows Play opens the file as a chip does',+ (tester) async {+ await show(tester, withVoice());+ await tester.tap(find.byTooltip('Play'));+ await tester.pumpAndSettle();+ expect(opened, [url]);+ expect(fetched, isEmpty);+ expect(sources, isEmpty);+ await close(tester);+ },+ variant: TargetPlatformVariant({+ TargetPlatform.linux,+ TargetPlatform.windows,+ }),+ );+ });+}++/// A player that plays nothing, and says what it was asked.+class _Player extends VideoPlayerController {+ _Player() : super.file(File('/voice/clip.wav'));++ final List<String> calls = [];+ bool disposed = false;++ @override+ Future<void> initialize() async {+ calls.add('initialize');+ value = value.copyWith(+ isInitialized: true,+ duration: const Duration(seconds: 81),+ );+ }++ @override+ Future<void> play() async {+ calls.add('play');+ value = value.copyWith(isPlaying: true, isCompleted: false);+ }++ @override+ Future<void> pause() async {+ calls.add('pause');+ value = value.copyWith(isPlaying: false);+ }++ @override+ Future<void> seekTo(Duration position) async {+ calls.add('seekTo');+ value = value.copyWith(position: position);+ }++ @override+ Future<void> dispose() async {+ disposed = true;+ await super.dispose();+ }++ void finish() => value = value.copyWith(+ isPlaying: false,+ isCompleted: true,+ position: const Duration(seconds: 81),+ );+}diff --git a/docs/plans/2026-11-04-chat-voice-messages-plan.md b/docs/plans/2026-11-04-chat-voice-messages-plan.mdnew file mode 100644index 000000000..1b98ddb7c--- /dev/null+++ b/docs/plans/2026-11-04-chat-voice-messages-plan.md@@ -0,0 +1,239 @@+# Chat voice messages Implementation Plan++> **For agentic workers:** Follow the project Engineer workflow: plan first in a draft PR; implement task by task with a committed failing test before its passing code; one independent review step on the pull request.++**Goal:** The phone's mic records a voice message that is sent as audio, as Telegram does. People hear it on phone and Desktop, agents read its words. Strengthens "Tell the PM what you want, and your crew ships it": a person on the go tells the crew by speaking, and the crew gets the words.++**Architecture:** The phone records 16 kHz mono PCM WAV with the recorder it already has (`phone/voice_recorder.dart`) and uploads it as an ordinary chat file. Before sending, it asks the server to read the file as a voice message. The server transcribes the file once, with the existing Deepgram WAV path (`lib/deepgramWav.ts`) under the existing daily voice quota, and works out the clip's length and waveform from the bytes. It stores all three on the attachment row. `message.send` then carries them: the words become the message's text and the attachment gains `voice: { durationMs, waveform, transcribed }`. Deliveries are written in the same transaction as the message, so agents get the words at once. At claim time the delivered text is marked `Voice message (1:21): …`. Server previews say `Voice message (1:21)`. Both apps draw a voice bubble for an attachment that has `voice`, and play it with `video_player`.++**Tech Stack:** TypeScript, Fastify, Prisma on MongoDB, Vitest (backend); Flutter/Dart with `record` and `video_player` (phone); Flutter/Dart with `video_player` (Desktop).++**Spec:** Task #2584 in Super Tab conversation e18fc8a3f98dafce9bd0a53c (Dee, 2026-11-04; brief from River the same day).++## Why transcribe before send, not after++Chat messages are never changed after they are written. The message and its agent deliveries are written in one transaction (`store.ts writeMessages`), and a host types a message's text as it claims it. If the words came after the send, agents would get an empty message, unless the server added a message-edit path and a delivery hold for this one feature. Transcribing when the file arrives, before it can be sent, keeps message rows write-once. Deliveries stay as they are, and every reader, including old Desktop and CLI releases, sees the words as the message's text.++## Why WAV++The recorder already records PCM WAV, and Deepgram's WAV path is measured per language (`deepgramWav.ts`). The server can only charge a clip against the daily voice quota once it knows the clip's length, and for WAV the length follows from the header and the size (`wavDurationMs`). A compressed format would need a decoder or a client-stated length, and the caller would control what it is charged. The cost is 1.9 MB a minute, 19 MB at the 10:00 cap. Over 10 MB the upload goes up through the existing upload link.++## Global Constraints++- Phone recording row: `( X ) ● 0:14 ıllıllıııllıı.ılı ( ↑ )`. Tony's composer card stays. X discards the take, the red dot shows the running time, the live waveform moves, and Send posts the take. Nothing goes into the text box.+- Voice bubble, phone and Desktop: `( ▶ ) ıllıllııılllıı.ılıı.ıll 1:21 →A`. It has play/pause, a waveform with the played part filled, the length, and →A. →A shows the words under the bubble, and a second tap hides them.+- A take stops at exactly 10:00 with Send ready. Locking the phone or leaving the chat stops the take and keeps it, with Send and X still there.+- The old path that dictated words into the box is removed: `swarm_dictation.dart`, its iOS channel and the recognizer branch of `swarm_voice.dart`. The keyboard's own dictation stays.+- Desktop plays voice messages and does not record them.+- The server transcribes each voice file once and stores the words with the message. The audio stays attached like any file.+- Agents receive `Voice message (M:SS): <words>` as the message's text. A file that could not be transcribed reaches them as `Voice message (M:SS), not transcribed.`+- Activity, tags, inbox and thread previews, and the phone's and Desktop's own reply quotes, say `Voice message (M:SS)`.+- Lengths are written `M:SS`, rounded down to the second: 81 s is `1:21`, 600 s is `10:00`.+- Transcripts never reach a log line, an error or telemetry. Audio is stored like any other attachment.+- Harness Design Language tokens on both apps; no hard-coded colours or sizes.+- Run `dart format` only on the files changed. Swarm code stays out of cli/src/core.+- Never run a test that signs the Mac out of the real CLI; Desktop tests use a memory store and a fake sign-in.++## Design++- **Growth:** the voice fields live on the `SuperTabAttachment` row and on the message's `attachments` JSON. They are deleted with their row: an unsent file's row by the unsent-file sweep after 24 h, a sent message's copy with its message. One clip is at most 10:05 (19.3 MB) and its waveform at most 48 numbers.+- **Concurrency:** one attachment row. The first call claims it with a conditional write on a `voiceSince` date (`voice` not set, and `voiceSince` not set or stale). A second call made while the first runs gets `VOICE_TRANSCRIBING`, 409, and the phone retries it after a second, at most 30 times. A claim older than 2 minutes counts as lost, and the next call takes it over. The quota reservation is keyed by `voice:<attachmentId>`, so a retake is never charged twice.+- **Read cost:** the transcription request reads the file's own bytes once, at most 19.3 MB, held within the existing process-wide voice memory budget (`reserveVoice`). `message.send` reads the `voice` field alongside the attachment fields it already loads. Message pages get one more small field per voice attachment.+- **Scale:** at 10× (hundreds of voice messages a day), Deepgram calls and bucket reads grow linearly. The voice memory budget refuses work beyond what the process can hold, with `VOICE_BUSY`. The first warning is the `super-tab voice` log line's `ms` and `outcome` counts.+- **Compatibility:** old Desktop and CLI releases ignore `voice`. They show the words as the message text plus a `Voice message.wav` file chip, and agents of an old CLI get the words with the file. New clients against an old server (the backend ships first, so only briefly): the voice route answers 404, and the phone keeps the take, sends nothing and says "Voice messages need a newer server". Messages without `voice` render as today.+- **Migration + rollback:** two new optional fields on `SuperTabAttachment`: `voice` (Json) and `voiceSince` (DateTime). MongoDB needs no migration. After a rollback the old server keeps the field and ignores it, and sent messages keep their text and file.+- **Audience:** the voice route allows only the actor that uploaded the file, while that actor may still post in its conversation, and only for an unused file with no `purpose`. A sent message's words are read through the same message reads as its text, under the existing guards. Buddies get the row through `rowForBuddy` as today.+- **Identity:** transcription is charged to the uploading person's own daily voice quota (`voiceQuotaService.reserveForUser`). Only people send voice messages: an agent caller (`asAgentId`) is refused.+- **Failure:** every failure is named, and the message still sends. Over the daily voice limit: `transcribed: false` with `failure: 'limit'`, the phone says "Today's voice limit is used; sent without words", and agents read "not transcribed". Deepgram down: `failure: 'failed'`, final like the limit (once per file), and the message sends without words. Not WAV, or over 10:05: refused with 415 or 413. A log line carries the outcome, the size and the length, never the words.+- **Platforms:** recording on iOS and Android. Playback on iOS, Android, macOS and web with `video_player`. On Linux and Windows, `video_player` has no implementation, so the bubble's play button opens the file in the system's player, as a file chip does today.++## Review Focus++1. **A take that hits 10:00 while the phone is locked** stays held, with Send ready, when the app comes back: no auto-send, nothing lost. Test in Task 6.+2. **Sending the same take twice** (a retry after a lost answer) makes one message and one charge. Test in Task 2 (once per file) and Task 3 (the second `message.send` of a used file is refused, as today).+3. **A voice message replied to or quoted** shows `Voice message (1:21)`, not an empty quote. Test in Tasks 8 and 10.+4. **A transcript with a credential in it** is redacted like typed text before it becomes the message's text. Test in Task 3.+5. **A link that expires during a long playback.** The player plays from a local copy of the file, never from the signed link. Tests in Tasks 8 and 10 check that it plays a file path.++---++### Task 1: Voice clip facts (backend)++**Files:**+- Create: `backend/src/lib/wav.ts` (`wavDurationMs` moves here from `routes/voice.ts`; the route imports it)+- Create: `backend/src/superTabs/voiceMessages.ts`+- Test: `backend/src/superTabs/voiceMessages.test.ts`++**Interfaces:**+- Produces: `wavDurationMs(audio: Buffer): number | null` (unchanged behaviour); `voiceWaveform(wav: Buffer, bars = 48): number[]` (each 0–31, the RMS of its slice on the dB scale of the phone's `pcm16Level`); `voiceLength(durationMs: number): string` (`M:SS`); `voiceLabel(durationMs: number): string` (`Voice message (1:21)`); `MAX_VOICE_MS = 605_000`.++- [ ] Step 1: write failing tests: `voiceLength(81_000) === '1:21'`, `voiceLength(600_000) === '10:00'`, `voiceLength(999) === '0:00'`; `voiceLabel(81_000) === 'Voice message (1:21)'`; a WAV of silence gives 48 zeros, a WAV whose second half is a loud tone gives low first bars and high last bars, and a WAV shorter than 48 samples still gives 48 bars.+- [ ] Step 2: run `npx vitest run src/superTabs/voiceMessages.test.ts` and expect a FAIL on a missing module.+- [ ] Step 3: implement. `routes/voice.test.ts` still passes with the moved function.+- [ ] Step 4: run both tests and expect a PASS. Commit.++### Task 2: Transcribe a voice file once (backend)++**Files:**+- Modify: `backend/prisma/schema.prisma` (`SuperTabAttachment.voice Json?` and `voiceSince DateTime?`, documented)+- Modify: `backend/src/superTabs/store.ts` (`readVoice`, beside `completeUpload`)+- Modify: `backend/src/routes/superTabs.ts` (`POST /api/super-tabs/:tabId/attachments/:attachmentId/voice`, empty body)+- Test: `backend/src/superTabs/voiceMessages.store.test.ts` (memory store, mocked `transcribeWavWithDeepgram` and `voiceQuotaService`)++**Interfaces:**+- Produces: `store.readVoice(user, tabId, attachmentId, env): Promise<{ attachment: Attachment & { voice: VoiceFacts } }>` where `VoiceFacts = { durationMs: number; waveform: number[]; transcribed: boolean; failure?: 'limit' | 'failed' }`. The stored row adds `transcript: string | null`, which is never returned by this route.++- [ ] Step 1: write failing tests:+ - A WAV upload answers `durationMs`, 48 `waveform` values and `transcribed: true`, and calls Deepgram once.+ - A second call answers the same and calls Deepgram no more.+ - Two calls at once: one transcribes and the other gets `VOICE_TRANSCRIBING`.+ - A claim older than 2 minutes is taken over.+ - Another actor's file, a used file and a pending upload each give `ATTACHMENT_UNAVAILABLE`.+ - A non-WAV file gives `UNSUPPORTED_AUDIO`, 415.+ - A clip over 10:05 gives `VOICE_TOO_LONG`, 413.+ - Quota exceeded gives `transcribed: false, failure: 'limit'` and no Deepgram call.+ - A Deepgram error gives `failure: 'failed'`, with the quota released, and a later call answers the same.+ - `asAgentId` is refused.+ - No log line contains the transcript (spy on the logger).+- [ ] Step 2: run them and expect FAIL.+- [ ] Step 3: implement. Read the bytes with `direct.head(objectKey, size)` under `reserveVoice`. Reserve the quota under the id `voice:<id>` and call `transcribeWavWithDeepgram(bytes, 'audio/wav', lang)`, both through an injected `SuperTabStoreHooks.voice`, so tests fake them; the production store wires the real ones in `routes/superTabs.ts`. Then store `{ durationMs, waveform, transcribed, failure?, transcript }`. Language: the phone passes its picked voice language as `?lang=` (`pickWavLang`), and it is `en` when none is given.+- [ ] Step 4: run them and expect PASS. Commit.++### Task 3: A voice file sends as a voice message (backend)++**Files:**+- Modify: `backend/src/superTabs/attachments.ts` (`Attachment.voice?`)+- Modify: `backend/src/superTabs/store.ts` (the attachments loaded for `message.send` carry `voice` and `transcript`)+- Modify: `backend/src/superTabs/commands.ts` (in `message.send`)+- Test: `backend/src/superTabs/commands.test.ts` (`describe('voice messages')`)++**Interfaces:**+- Produces: a message whose one attachment has `voice: { durationMs, waveform, transcribed }`, and whose `text` is the stored transcript, or `''` when there is none.++- [ ] Step 1: write failing tests:+ - Sending a transcribed voice file stores its words as the text, and the attachment carries `voice` without `transcript`.+ - A voice file with other files or with typed text gives `VOICE_ALONE`, 400.+ - A file still transcribing gives `VOICE_TRANSCRIBING`, 409.+ - An untranscribed file sends with an empty text.+ - A credential in the words is redacted.+- [ ] Step 2: run them and expect FAIL.+- [ ] Step 3: implement.+- [ ] Step 4: run them and expect PASS. Commit.++### Task 4: Agents read the words; previews say "Voice message" (backend)++**Files:**+- Modify: `backend/src/superTabs/store.ts` (the claim's row mapping, beside `rowForBuddy`)+- Modify: `backend/src/superTabs/read/inbox.ts` (its `FIELDS` projection and `row`, which activity, the inbox and followed threads all read through, name a voice message `Voice message (M:SS)`). `mentions.ts` and `catchUp.ts` stay as they are: a voice message tags no one, and catch-up lists only status rows and tags. `noticeDigest.ts` stays too: a digest line shows a notice's words, and an agent reads a voice message's words there without the mark (review of #2887, Minor 3).+- Modify: `backend/src/superTabs/voiceMessages.ts` (`agentText(text, attachments)`, `voiceMsOf(attachments)`)+- Test: `backend/src/superTabs/voiceMessages.test.ts` and each reader's existing test file++- [ ] Step 1: write failing tests:+ - A claimed delivery of a voice message has `Voice message (1:21): hello team` as its text.+ - An untranscribed one has `Voice message (1:21), not transcribed.`+ - A Buddy's claim is marked the same way.+ - Activity, inbox, followed-thread, tag and catch-up previews of a voice message say `Voice message (1:21)`.+ - A plain message's preview is unchanged.+- [ ] Step 2: run them and expect FAIL.+- [ ] Step 3: implement.+- [ ] Step 4: run them and the tests of what imports the changed code, and expect PASS. Commit.++### Task 5: The model and the API (phone)++**Files:**+- Modify: `mobile/lib/swarm/model.dart` (`SuperTabVoice { int durationMs; List<int> waveform; bool transcribed }`, `SuperTabAttachment.voice`, parsed defensively, with a bad value read as no voice)+- Modify: `mobile/lib/swarm/swarm_outgoing_files.dart` (`wav` → `audio/wav`)+- Modify: `mobile/lib/swarm/swarm_request.dart`, `mobile/lib/state/app_state.dart`, `mobile/lib/services/api_client.dart` (`readSwarmVoice(tabId, attachmentId, lang)`)+- Create: `mobile/lib/swarm/voice_length.dart` (`swarmVoiceLength(Duration) → 'M:SS'`, `swarmVoiceLabel(Duration) → 'Voice message (M:SS)'`)+- Test: `mobile/test/swarm/swarm_voice_model_test.dart`++- [ ] Steps: failing tests for parsing, a bad `voice` value, the length and the label (the same cases as Task 1); then the implementation. Commit both.++### Task 6: Recording a voice message (phone)++**Files:**+- Rewrite: `mobile/lib/swarm/swarm_voice.dart` (`SwarmVoice` records takes; `SwarmVoiceRow` draws X, the dot with the time, the wave and Send)+- Delete: `mobile/lib/swarm/swarm_dictation.dart`, the dictation channel in `mobile/ios/Runner/AppDelegate.swift`, its `Info.plist` speech-recognition entry, and `test/swarm/swarm_dictation_test.dart`, `swarm_chat_dictation_test.dart`+- Modify: `mobile/lib/swarm/swarm_session.dart`, `mobile/lib/phone/phone_shell.dart` (`SwarmHost.voice` makes a `VoiceRecorder`; the `dictation` lookup goes)+- Test: rewrite `mobile/test/swarm/swarm_chat_voice_test.dart`++**Interfaces:**+- Produces: `SwarmVoice({required VoiceRecorder Function() recorder, required Future<void> Function(VoiceTake take) send})` with `tap()`, `discard()`, `send()`, `stop()`, `hide()` (stops and keeps the take), `state` (`off | recording | held | sending`), `elapsed`, `level`. `SwarmVoice.maxTake = Duration(minutes: 10)`.++- [ ] Step 1: write failing tests:+ - A tap records; Send calls `send` with the take and leaves the text box as it was.+ - X discards the take and sends nothing.+ - At 10:00 (fake async) the take stops by itself, Send stays, and nothing is sent.+ - `hide()` and the app being paused stop the take and keep it.+ - A refused microphone shows the existing notice.+ - A second tap while recording stops the take and holds it.+- [ ] Step 2: run them and expect FAIL.+- [ ] Step 3: implement. Keep `SwarmVoiceWave` and the composer card; the row is the mockup's.+- [ ] Step 4: run them and expect PASS. Commit.++### Task 7: Sending a take (phone)++**Files:**+- Modify: `mobile/lib/swarm/swarm_conversation.dart`, `swarm_thread.dart` (`sendVoice(VoiceTake take)`: upload `Voice message.wav`, `readSwarmVoice`, then `message.send` with that one file)+- Modify: `mobile/lib/swarm/swarm_conversation_page.dart`, `swarm_thread_page.dart`, `swarm_new_message_sheet.dart` (wire `send`)+- Test: `mobile/test/swarm/swarm_chat_voice_test.dart`, `swarm_thread_voice_test.dart`++- [ ] Step 1: write failing tests:+ - A sent take posts `message.send` with the uploaded file id and empty text, after one voice read.+ - `VOICE_TRANSCRIBING` is retried.+ - A 404 from an old server keeps the take, sends nothing and says "Voice messages need a newer server".+ - `failure: 'limit'` shows "Today's voice limit is used; sent without words".+ - The draft in the box stays.+- [ ] Step 2: run them and expect FAIL.+- [ ] Step 3: implement.+- [ ] Step 4: run them and expect PASS. Commit.++### Task 8: The voice bubble (phone)++**Files:**+- Create: `mobile/lib/swarm/swarm_voice_bubble.dart` (`SwarmVoiceBubble({required SuperTabAttachment file, required String words, SwarmFileRef fileRef})`)+- Modify: `mobile/lib/swarm/swarm_message_row.dart` (an attachment with `voice` draws the bubble; the message's text is not drawn as text; reply quotes use `swarmVoiceLabel`), `swarm_conversation_page.dart` `_Pending` (a pending take draws the bubble from its local take)+- Test: `mobile/test/swarm/swarm_voice_bubble_test.dart`; screenshots in `mobile/test/render/swarm_voice_render_test.dart`++- [ ] Step 1: write failing tests:+ - A voice message shows play, the waveform, `1:21` and →A, and no text.+ - →A shows the words and a second tap hides them.+ - Play loads the cached local file into the player (a fake player factory) and shows pause.+ - The played fraction fills the bars.+ - A reply to a voice message quotes `Voice message (1:21)`.+ - An untranscribed message's →A says "No words: not transcribed".+- [ ] Step 2: run them and expect FAIL.+- [ ] Step 3: implement with `VideoPlayerController.file` behind a factory the tests replace.+- [ ] Step 4: run them and expect PASS. Light and dark screenshots of the row and the bubble. Commit.++### Task 9: The model and previews (Desktop)++**Files:**+- Modify: `desktop/lib/super_tabs/model.dart` (`SuperTabVoice`, `SuperTabAttachment.voice`)+- Create: `desktop/lib/super_tabs/voice_length.dart`+- Modify: `desktop/lib/super_tabs/message_link_preview.dart`, `message_row.dart` (reply quote), `view.dart` (composer "Replying to")+- Test: `desktop/test/super_tab_voice_message_test.dart`++- [ ] Steps: failing tests for parsing, the label, and each of the three quotes saying `Voice message (1:21)`; then the implementation. Commit both.++### Task 10: The voice bubble and playback (Desktop)++**Files:**+- Modify: `desktop/pubspec.yaml` (`video_player`, with the reason in its comment: AVPlayer on macOS, `<audio>`/`<video>` on the web, first-party, with a Swift package)+- Create: `desktop/lib/super_tabs/voice_bubble.dart`+- Modify: `desktop/lib/super_tabs/attachment_chips.dart` (a third bucket for an attachment with `voice`), `message_row.dart` (no text block for a voice message)+- Test: `desktop/test/super_tab_voice_message_test.dart`; screenshots in `desktop/test/super_tab_voice_message_shots_test.dart` (`captureWindow`, light and dark)++- [ ] Step 1: write failing tests:+ - The bubble shows play, the waveform, `1:21` and →A, with no text block.+ - →A toggles the words.+ - Play reads the file through `readAttachment` to a temporary file and plays it (a fake player factory).+ - On Linux and Windows, play opens the file the way a chip does.+- [ ] Step 2: run them and expect FAIL.+- [ ] Step 3: implement.+- [ ] Step 4: run them and expect PASS. Light and dark screenshots. Commit.++### Task 11: Evidence++- [ ] Locally: the changed tests, `npx tsc --noEmit`, and `flutter analyze` on the touched files of both apps. No whole suite runs locally (project instructions, revision 56): the PR's CI runs each touched component's suite once it is marked ready, and its green run is the suite evidence.+- [ ] A PR comment with red/green per task, and the screenshots in the PR body.diff --git a/docs/plans/assets/2026-11-04-chat-voice-messages/phone-message-dark.png b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-message-dark.pngnew file mode 100644index 000000000..10717c3a3Binary files /dev/null and b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-message-dark.png differdiff --git a/docs/plans/assets/2026-11-04-chat-voice-messages/phone-message-light.png b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-message-light.pngnew file mode 100644index 000000000..a486936ebBinary files /dev/null and b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-message-light.png differdiff --git a/docs/plans/assets/2026-11-04-chat-voice-messages/phone-recording-dark.png b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-recording-dark.pngnew file mode 100644index 000000000..db7cf116cBinary files /dev/null and b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-recording-dark.png differdiff --git a/docs/plans/assets/2026-11-04-chat-voice-messages/phone-recording-light.png b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-recording-light.pngnew file mode 100644index 000000000..5df2f5394Binary files /dev/null and b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-recording-light.png differdiff --git a/docs/plans/assets/2026-11-04-chat-voice-messages/phone-words-dark.png b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-words-dark.pngnew file mode 100644index 000000000..d724651f6Binary files /dev/null and b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-words-dark.png differdiff --git a/docs/plans/assets/2026-11-04-chat-voice-messages/phone-words-light.png b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-words-light.pngnew file mode 100644index 000000000..edd93dab5Binary files /dev/null and b/docs/plans/assets/2026-11-04-chat-voice-messages/phone-words-light.png differdiff --git a/mobile/ios/Runner/AppDelegate.swift b/mobile/ios/Runner/AppDelegate.swiftindex a6fa75788..698f39d23 100644--- a/mobile/ios/Runner/AppDelegate.swift+++ b/mobile/ios/Runner/AppDelegate.swift@@ -4,7 +4,6 @@ import AuthenticationServicesimport Flutterimport Networkimport NetworkExtension-import Speechimport UIKitimport UserNotifications@@ -29,7 +28,6 @@ import UserNotificationsHandoffChannel.register(with: engineBridge.pluginRegistry)ChimeChannel.register(with: engineBridge.pluginRegistry)AppBadgeChannel.register(with: engineBridge.pluginRegistry)- DictationChannel.register(with: engineBridge.pluginRegistry)LinkChannel.register(with: engineBridge.pluginRegistry)SetupWifiChannel.register(with: engineBridge.pluginRegistry)}@@ -222,300 +220,6 @@ final class LinkChannel: NSObject, FlutterSceneLifeCycleDelegate {}}-/// `harness/dictation` — the phone's own speech recognizer, the one the keyboard's dictation uses-/// (Dart: `SwarmChannelDictation` in `lib/swarm/swarm_dictation.dart`): a Swarm chat's message box-/// fills with the words as they are said.-///-/// `access` asks for speech recognition, then the microphone, the first time, and answers `allowed`,-/// `denied` (speech recognition refused or restricted: Dart records and has the backend transcribe,-/// as before) or `noMic`. `start` takes Dart's `take` and a `locale`, and answers false when the-/// phone has no recognizer for it now. Then, each with the take: `words` (the recognizer's latest-/// best guess, `text`; `final` once it says that guess is finished; `from` and `to`, where its words-/// begin and end in the session's audio, 0 when it gives no times — Dart works out what is done from-/// these), `level` (the microphone's RMS, ten a second), `paused` whenever the recognizer ends a-/// session by itself, and once `end` — `done`, `noSpeech` or `failed`. `stop` ends listening, and the-/// last words and the end follow; `cancel` ends it with nothing more said.-///-/// ⚠️ **A take listens until it is stopped** (Tony, 10-29: "until I tap"). iOS ends a recognizer's-/// session by itself — after a pause, a long silence, its own minute — and the microphone is kept-/// open through that: `paused` says so, and Dart's `resume` starts the next session at once, false-/// when it cannot. Only a call or another app taking the audio (an interruption) ends the take as-/// `failed`. The screen does not dim or lock meanwhile: talking is not a touch.-///-/// ⚠️ **Set as the keyboard's dictation is** (Tony, 10-29: "exactly like the keyboard"): the-/// dictation hint, punctuation, live guesses, no contextual words — and on the phone or on Apple's-/// servers as the recognizer chooses, not forced on the phone, where Vietnamese can be heard worse.-/// Nothing heard is logged or kept here.-final class DictationChannel {- private static var shared: DictationChannel?-- private let channel: FlutterMethodChannel-- /// The take listening now.- private var current: Take?-- static func register(with registry: FlutterPluginRegistry) {- guard let messenger = registry.registrar(forPlugin: "HarnessDictation")?.messenger() else { return }- shared = DictationChannel(messenger: messenger)- }-- private init(messenger: FlutterBinaryMessenger) {- channel = FlutterMethodChannel(name: "harness/dictation", binaryMessenger: messenger)- channel.setMethodCallHandler { [weak self] call, result in- self?.handle(call, result: result)- }- }-- private func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {- let arguments = call.arguments as? [String: Any]- let take = arguments?["take"] as? Int- switch call.method {- case "access":- Self.access { result($0) }- case "start":- guard let take, let locale = arguments?["locale"] as? String else {- result(FlutterError(code: "bad-args", message: "A take and a locale", details: nil))- return- }- result(start(take, locale: locale))- case "resume":- guard let current, current.id == take else {- result(false)- return- }- result(resume(current))- case "stop":- if let current, current.id == take { stop(current) }- result(nil)- case "cancel":- if current?.id == take { finish(nil) }- result(nil)- default:- result(FlutterMethodNotImplemented)- }- }-- /// Speech recognition, then the microphone: asked the first time, and answered at once after.- private static func access(_ answer: @escaping (String) -> Void) {- SFSpeechRecognizer.requestAuthorization { status in- guard status == .authorized else {- DispatchQueue.main.async { answer("denied") }- return- }- let microphone: (Bool) -> Void = { granted in- DispatchQueue.main.async { answer(granted ? "allowed" : "noMic") }- }- if #available(iOS 17.0, *) {- AVAudioApplication.requestRecordPermission(completionHandler: microphone)- } else {- AVAudioSession.sharedInstance().requestRecordPermission(microphone)- }- }- }-- /// Listens for [take] in [locale]: the microphone opens, and the recognizer's first session- /// starts. False, with nothing open, when it cannot.- private func start(_ take: Int, locale: String) -> Bool {- finish(nil)- guard let recognizer = SFSpeechRecognizer(locale: Locale(identifier: locale)),- recognizer.isAvailable- else { return false }-- let session = AVAudioSession.sharedInstance()- let engine = AVAudioEngine()- let feed = Feed()- var measured = 0.0- let heard: AVAudioNodeTapBlock = { [weak self] buffer, _ in- feed.append(buffer)- let now = CACurrentMediaTime()- guard now - measured >= 0.1, let rms = Self.rms(buffer) else { return }- measured = now- DispatchQueue.main.async {- guard let self, self.current?.id == take else { return }- self.channel.invokeMethod("level", arguments: ["take": take, "rms": rms])- }- }- let input = engine.inputNode- do {- try session.setCategory(.record, mode: .measurement, options: .duckOthers)- try session.setActive(true, options: .notifyOthersOnDeactivation)- let format = input.outputFormat(forBus: 0)- guard format.sampleRate > 0, format.channelCount > 0 else {- try? session.setActive(false, options: .notifyOthersOnDeactivation)- return false- }- input.installTap(onBus: 0, bufferSize: 1024, format: format, block: heard)- engine.prepare()- try engine.start()- } catch {- input.removeTap(onBus: 0)- engine.stop()- try? session.setActive(false, options: .notifyOthersOnDeactivation)- return false- }-- let listening = Take(- id: take, recognizer: recognizer, engine: engine, feed: feed,- idleWas: UIApplication.shared.isIdleTimerDisabled)- current = listening- // A call, a timer's alarm, another app's audio: the microphone is gone, and the take with it.- listening.interruption = NotificationCenter.default.addObserver(- forName: AVAudioSession.interruptionNotification, object: session, queue: .main- ) { [weak self] note in- let type = (note.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt)- .flatMap(AVAudioSession.InterruptionType.init(rawValue:))- guard let self, self.current === listening, type == .began else { return }- self.finish("failed")- }- UIApplication.shared.isIdleTimerDisabled = true- listen(listening)- return true- }-- /// A new session of the recognizer's in [take], fed by the microphone from now on.- private func listen(_ take: Take) {- let request = SFSpeechAudioBufferRecognitionRequest()- request.shouldReportPartialResults = true- request.taskHint = .dictation- request.contextualStrings = []- if #available(iOS 16.0, *) { request.addsPunctuation = true }- take.session += 1- let session = take.session- take.feed.request = request- take.task = take.recognizer.recognitionTask(with: request) { [weak self] result, error in- let text = result?.bestTranscription.formattedString- let segments = result?.bestTranscription.segments ?? []- let from = segments.first?.timestamp ?? 0- let to = segments.last.map { $0.timestamp + $0.duration } ?? 0- // Finished: the session's last guess, or one the recognizer marks as the end of what was said- // before a pause, after which it may start its words over.- let finished = result.map { $0.isFinal || $0.speechRecognitionMetadata != nil } ?? false- let final = result?.isFinal ?? false- // "No speech detected": ended, or given up, before a word was heard.- let silent = (error as NSError?).map { $0.domain == "kAFAssistantErrorDomain" && $0.code == 1110 }- DispatchQueue.main.async {- guard let self, self.current === take, take.session == session, take.task != nil else { return }- if let text {- self.channel.invokeMethod(- "words",- arguments: ["take": take.id, "text": text, "final": finished, "from": from, "to": to])- }- guard final || silent != nil else { return }- take.task = nil- take.feed.request = nil- let end = final ? "done" : silent == true ? "noSpeech" : "failed"- if take.stopping {- self.finish(end)- } else {- self.channel.invokeMethod("paused", arguments: ["take": take.id, "end": end])- }- }- }- }-- /// The next session, after the recognizer ended one by itself. False when the take cannot go on:- /// it is stopping, its microphone stopped, or the recognizer cannot be had now.- private func resume(_ take: Take) -> Bool {- if take.task != nil { return true }- guard !take.stopping, take.engine.isRunning, take.recognizer.isAvailable else { return false }- listen(take)- return true- }-- /// No more audio for [take]: the session's last words, then the end — at once when no session is- /// listening, between one the recognizer ended and the next.- private func stop(_ take: Take) {- take.stopping = true- take.engine.stop()- take.engine.inputNode.removeTap(onBus: 0)- guard let request = take.feed.request else {- finish("done")- return- }- take.feed.request = nil- request.endAudio()- }-- /// Closes the take listening now, and says how it ended — or nothing, for a cancel.- private func finish(_ end: String?) {- guard let take = current else { return }- current = nil- take.task?.cancel()- take.task = nil- take.engine.stop()- take.engine.inputNode.removeTap(onBus: 0)- take.feed.request?.endAudio()- take.feed.request = nil- if let interruption = take.interruption { NotificationCenter.default.removeObserver(interruption) }- try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)- UIApplication.shared.isIdleTimerDisabled = take.idleWas- if let end { channel.invokeMethod("end", arguments: ["take": take.id, "end": end]) }- }-- /// How loud [buffer] is, 0…1 of full scale.- private static func rms(_ buffer: AVAudioPCMBuffer) -> Double? {- guard let samples = buffer.floatChannelData?[0], buffer.frameLength > 0 else { return nil }- let count = Int(buffer.frameLength)- var sum: Float = 0- for index in 0..<count { sum += samples[index] * samples[index] }- return Double((sum / Float(count)).squareRoot())- }-}--/// One take, from `start` to its end: the microphone open throughout, its tap on the engine's-/// input, while the recognizer's sessions come and go in it ([DictationChannel]).-private final class Take {- let id: Int- let recognizer: SFSpeechRecognizer- let engine: AVAudioEngine- let feed: Feed-- /// Whether the screen was kept awake before the take, as it is again after.- let idleWas: Bool-- /// The recognizer's session now, and its number: what an earlier one still says lands nowhere.- var task: SFSpeechRecognitionTask?- var session = 0-- /// `stop` was asked: the session ending now is the take's last.- var stopping = false- var interruption: NSObjectProtocol?-- init(id: Int, recognizer: SFSpeechRecognizer, engine: AVAudioEngine, feed: Feed, idleWas: Bool) {- self.id = id- self.recognizer = recognizer- self.engine = engine- self.feed = feed- self.idleWas = idleWas- }-}--/// Where the microphone's audio goes: the request of the recognizer's session now. Changed on the-/// main thread while the tap appends to it on the audio thread, so behind a lock.-private final class Feed {- private let lock = NSLock()- private var current: SFSpeechAudioBufferRecognitionRequest?-- var request: SFSpeechAudioBufferRecognitionRequest? {- get {- lock.lock()- defer { lock.unlock() }- return current- }- set {- lock.lock()- defer { lock.unlock() }- current = newValue- }- }-- func append(_ buffer: AVAudioPCMBuffer) {- lock.lock()- defer { lock.unlock() }- current?.append(buffer)- }-}-/// `harness/app_badge` — the app icon's count (Dart: `setSwarmBadge` in `lib/notify/system_notices.dart`):/// what the Swarms ask of the person, the drawer's waiting count. `set` takes the count; zero takes it/// off. Drawn only where the person allowed badges with the notification prompt.diff --git a/mobile/ios/Runner/Info.plist b/mobile/ios/Runner/Info.plistindex 8ec34cf40..b345a0125 100644--- a/mobile/ios/Runner/Info.plist+++ b/mobile/ios/Runner/Info.plist@@ -74,18 +74,11 @@which only adds its own when the app has none. --><key>NSLocalNetworkUsageDescription</key><string>Connect to the computers running Harness on your local network.</string>- <!-- Voice input, what tapping a terminal's mic opens. Asked for on the- first tap, not at launch. A terminal's recording is transcribed by the- Harness backend (`/api/voice/stt`, the dial's own path), not by the OS. -->+ <!-- Voice input, what tapping a terminal's mic opens, and a chat's voice+ messages. Asked for on the first tap, not at launch. Both are+ transcribed by the Harness backend, not by the OS. --><key>NSMicrophoneUsageDescription</key>- <string>Talk instead of typing, in a terminal or a chat.</string>- <!-- A Swarm chat's mic: the phone's own recognizer, the keyboard's- dictation, writes the words into the message box as they are said- (`DictationChannel` in AppDelegate.swift). Asked for on its first tap,- before the microphone. Refused, the chat records and the backend- transcribes, as a terminal does. -->- <key>NSSpeechRecognitionUsageDescription</key>- <string>Show what you say in a chat's message box as you say it.</string>+ <string>Talk to a terminal instead of typing, and send voice messages in a chat.</string><!-- Save Image and Save Video in the share sheet of a terminal's mediapreview (`phone/media_preview_page.dart`). Adding to the library is allit does, and iOS ends the app at that button when this string isdiff --git a/mobile/lib/app_shell.dart b/mobile/lib/app_shell.dartindex 19b9f370c..a0936253e 100644--- a/mobile/lib/app_shell.dart+++ b/mobile/lib/app_shell.dart@@ -11,7 +11,10 @@ import 'state/app_state.dart';import 'viewer/viewer_services.dart';import 'ws/terminal_transport_plugin.dart';import 'shared/theme/app_theme.dart' as grid;+import 'shared/theme/appearance_builder.dart';import 'shared/theme/appearance_prefs_store.dart';+import 'shared/theme/color_palette.dart';+import 'shared/theme/system_bars.dart';import 'core/machine_cache.dart';import 'core/startup.dart';import 'logging/app_log.dart';@@ -19,6 +22,8 @@ import 'logging/install.dart';import 'logging/launch_frames.dart';import 'logging/startup_trace.dart';+export 'shared/theme/system_bars.dart' show systemBarsFor;+/// A screen the app puts up for one of its states — signed in, signed out, starting.typedef AuthenticatedScreenBuilder = Widget Function(AppNotifier app);@@ -113,14 +118,15 @@ class HarnessApp extends StatelessWidget {// `buildAppTheme` bakes `AppControl.*Scaled` into plain numbers at the// moment it runs, so a UI size that changed without rebuilding this would// repaint nothing at all.- return ValueListenableBuilder<AppearancePrefs>(- valueListenable: appearancePrefsStore,- builder: (context, prefs, _) => _app(prefs),+ //+ // The palette is on `AppTheme` by the time `_app` runs: the one Theme+ // resolves to for this phone's light or dark, and a terminal's beside it.+ return AppearanceBuilder(+ builder: (context, prefs, palette) => _app(prefs, palette),);}- Widget _app(AppearancePrefs prefs) {- grid.AppTheme.palette.value = prefs.palette;+ Widget _app(AppearancePrefs prefs, HarnessPalette palette) {// ⚠️ ORDER MATTERS, and it is why this is a statement rather than something// tucked into the tree below: `buildAppTheme` reads `AppFont.sans` and// `AppControl.*Scaled`, so the settings have to be on `AppFont` BEFORE the@@ -153,9 +159,10 @@ class HarnessApp extends StatelessWidget {),// The design system's own `buildAppTheme` — see the note where a second,// hand-written `ThemeData` used to shadow it, in `lib/theme/app_theme.dart`.- // One theme, no `darkTheme`/`themeMode` to resolve between: the chosen- // palette says whether it is light or dark.- theme: grid.buildAppTheme(brightness: prefs.palette.brightness),+ // One theme, no `darkTheme`/`themeMode` to resolve between: the palette+ // says whether it is light or dark, and Settings' Theme (System, Dark or+ // Light) has already chosen which palette — see [AppearanceBuilder].+ theme: grid.buildAppTheme(brightness: palette.brightness),// The UI size reaches every `Text` as a text SCALE rather than as hundreds// of edited call sites. `withClampedTextScaling` with both bounds equal IS// the way to force a factor — MediaQuery has no "set the scale"@@ -221,23 +228,6 @@ class _GridTokenScope extends StatelessWidget {}}-/// The system bars' style for an app of [brightness]: icons that contrast with it, over the-/// transparent bars `main.dart` draws edge to edge under. The colours are restated because an-/// [AnnotatedRegion]'s style replaces the one set at launch.-SystemUiOverlayStyle systemBarsFor(Brightness brightness) {- // The icons are the opposite of the app; iOS's `statusBarBrightness` names the GROUND instead.- final icons = brightness == Brightness.dark- ? Brightness.light- : Brightness.dark;- return SystemUiOverlayStyle(- statusBarColor: const Color(0x00000000),- systemNavigationBarColor: const Color(0x00000000),- statusBarIconBrightness: icons,- systemNavigationBarIconBrightness: icons,- statusBarBrightness: brightness,- );-}-/// Which screen the app's state calls for.////// ⚠️ **What a desktop window also had here, and why it is gone.** The macOS application menudiff --git a/mobile/lib/demo/main_sample.dart b/mobile/lib/demo/main_sample.dartindex 77af0d52a..2fe947bc4 100644--- a/mobile/lib/demo/main_sample.dart+++ b/mobile/lib/demo/main_sample.dart@@ -4,7 +4,7 @@ import 'package:flutter/services.dart';import 'package:harness_mobile/app_shell.dart' show systemBarsFor;import 'package:harness_mobile/core/startup.dart';import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;-import 'package:harness_mobile/shared/theme/appearance_prefs_store.dart';+import 'package:harness_mobile/shared/theme/appearance_builder.dart';import 'sample_mode.dart';@@ -25,18 +25,17 @@ class SampleApp extends StatelessWidget {const SampleApp({super.key});@override- Widget build(BuildContext context) => ValueListenableBuilder<AppearancePrefs>(- valueListenable: appearancePrefsStore,- builder: (context, prefs, _) {- // What `HarnessApp` does before it builds its theme: the palette and the type settings go- // on first, because the theme bakes them in.- grid.AppTheme.palette.value = prefs.palette;+ Widget build(BuildContext context) => AppearanceBuilder(+ builder: (context, prefs, palette) {+ // What `HarnessApp` does before it builds its theme: the palette (on `AppTheme` already,+ // from [AppearanceBuilder]) and the type settings go on first, because the theme bakes them+ // in.grid.AppTheme.fonts.apply(uiFamily: prefs.uiFamily,uiScale: prefs.uiSize / grid.AppFont.uiSizeDefault,codeSize: grid.AppFont.codeSize,);- final brightness = prefs.palette.brightness;+ final brightness = palette.brightness;grid.AppTheme.brightness.value = brightness;return MaterialApp(title: 'Harness sample',diff --git a/mobile/lib/measurement/task_feedback.dart b/mobile/lib/measurement/task_feedback.dartindex 92dceedf4..ed825269f 100644--- a/mobile/lib/measurement/task_feedback.dart+++ b/mobile/lib/measurement/task_feedback.dart@@ -1,6 +1,7 @@import 'package:flutter/material.dart';import '../shared/widgets/product_feedback.dart';+import '../terminal/terminal_look.dart';Future<void> showTaskFeedback(BuildContext context, {@@ -9,7 +10,8 @@ Future<void> showTaskFeedback(}) async {final permitted = choice();await showDialog<void>(- context: context,+ // The app's dialog even from a terminal's menu — see [TerminalLook.outside].+ context: TerminalLook.outside(context),builder: (context) => AlertDialog(title: const Text('Rate task outcome'),content: SingleChildScrollView(diff --git a/mobile/lib/phone/agent_model_sheet.dart b/mobile/lib/phone/agent_model_sheet.dartindex 005f77a2e..db4c99f85 100644--- a/mobile/lib/phone/agent_model_sheet.dart+++ b/mobile/lib/phone/agent_model_sheet.dart@@ -27,6 +27,7 @@ import 'package:flutter/material.dart';import 'package:harness_mobile/core/models.dart';import 'package:harness_mobile/shared/theme/app_theme.dart';import 'package:harness_mobile/state/app_state.dart';+import 'package:harness_mobile/terminal/terminal_look.dart';import 'package:harness_mobile/widgets/engine_identity.dart';import 'agent_model_controller.dart';@@ -45,7 +46,8 @@ Future<void> showAgentModelSheet(// and the sheet's own context is gone by then.final messenger = ScaffoldMessenger.maybeOf(context);return showModalBottomSheet<void>(- context: context,+ // The app's sheet even over a terminal's page — see [TerminalLook.outside].+ context: TerminalLook.outside(context),useRootNavigator: true,showDragHandle: true,backgroundColor: AppPalette.panelBg,diff --git a/mobile/lib/phone/agent_swipe.dart b/mobile/lib/phone/agent_swipe.dartindex 26bf7108f..21b4529e7 100644--- a/mobile/lib/phone/agent_swipe.dart+++ b/mobile/lib/phone/agent_swipe.dart@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';import 'package:flutter/rendering.dart' show ScrollCacheExtent;import 'package:harness_mobile/state/app_state.dart';+import 'package:harness_mobile/terminal/terminal_look.dart';import 'package:harness_mobile/terminal/terminal_session.dart'show TerminalSessionStatus;@@ -360,8 +361,13 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {/// Every pager currently mounted — what [_detachAll] asks before closing a pane it attached.static final Set<_AgentSwipeHostState> _livePagers = {};+ /// The terminal's page keeps the terminal's palette whatever the app wears — dark, while+ /// Settings' Theme is chosen — around every page of the pager, which is every way a terminal is+ /// shown ([TerminalLook]).@override- Widget build(BuildContext context) {+ Widget build(BuildContext context) => TerminalLook(child: _pages(context));++ Widget _pages(BuildContext context) {final neighbours = _neighbours;final controller = _controller;if (neighbours == null || controller == null || neighbours.isEmpty) {diff --git a/mobile/lib/phone/daemon_sheet.dart b/mobile/lib/phone/daemon_sheet.dartindex acb1a8cf2..e621dbe0a 100644--- a/mobile/lib/phone/daemon_sheet.dart+++ b/mobile/lib/phone/daemon_sheet.dart@@ -14,6 +14,7 @@ import 'package:harness_mobile/daemons/roster.dart';import 'package:harness_mobile/daemons/zoo.dart';import 'package:harness_mobile/daemons/zoo_client.dart';import 'package:harness_mobile/shared/theme/app_theme.dart' show AppFont;+import 'package:harness_mobile/terminal/terminal_look.dart';import 'daemon_consent.dart';import 'daemon_hatch.dart';@@ -29,7 +30,8 @@ Future<void> showDaemonSheet(BuildContext context, DaemonHostState host) {unawaited(host.zoo.refresh());final navigator = Navigator.of(context, rootNavigator: true);return showModalBottomSheet<void>(- context: context,+ // The app's sheet even from a terminal's header — see [TerminalLook.outside].+ context: TerminalLook.outside(context),useRootNavigator: true,isScrollControlled: true,backgroundColor: DaemonInk.ground,diff --git a/mobile/lib/phone/new_agent_chooser.dart b/mobile/lib/phone/new_agent_chooser.dartindex 41de839e6..8cef73768 100644--- a/mobile/lib/phone/new_agent_chooser.dart+++ b/mobile/lib/phone/new_agent_chooser.dart@@ -1,5 +1,7 @@import 'package:flutter/material.dart';+import 'package:harness_mobile/terminal/terminal_look.dart';+import 'find_row.dart';import 'tty.dart';import 'tty_controls.dart';@@ -59,12 +61,13 @@ Future<T?> showNewAgentChooser<T extends Object>(String? title,bool autofocusSearch = false,}) => showModalBottomSheet<T>(- context: context,+ // The app's sheet even from a terminal's `+` — see [TerminalLook.outside].+ context: TerminalLook.outside(context),useRootNavigator: true,useSafeArea: true,isScrollControlled: true,showDragHandle: true,- backgroundColor: Tty.of(context).ground,+ backgroundColor: Tty.of(TerminalLook.outside(context)).ground,constraints: BoxConstraints(maxHeight: MediaQuery.sizeOf(context).height * 0.85,),diff --git a/mobile/lib/phone/phone_sheet.dart b/mobile/lib/phone/phone_sheet.dartindex b8def755b..69712771b 100644--- a/mobile/lib/phone/phone_sheet.dart+++ b/mobile/lib/phone/phone_sheet.dart@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';import 'package:lucide_icons_flutter/lucide_icons.dart';import 'package:harness_mobile/shared/theme/app_theme.dart';+import 'package:harness_mobile/terminal/terminal_look.dart';import 'package:harness_mobile/shared/widgets/app_dialog.dart'showappDialogButtonStyle,@@ -160,9 +161,12 @@ Future<void> showPhoneSheet(/// a menu of actions. Push it on the root navigator; held, it can be taken away again/// ([NavigatorState.removeRoute]) when the page that opened it goes.ModalBottomSheetRoute<T> phoneSheetRoute<T>(- BuildContext context, {+ BuildContext opener, {required WidgetBuilder builder,}) {+ // The app's sheet even over a terminal's page, which keeps a look of its own — see+ // [TerminalLook.outside].+ final context = TerminalLook.outside(opener);assert(debugCheckHasMediaQuery(context));assert(debugCheckHasMaterialLocalizations(context));// What `showModalBottomSheet` does, with [_PhoneSheetRoute] pushed in place of its route —diff --git a/mobile/lib/phone/phone_shell.dart b/mobile/lib/phone/phone_shell.dartindex d0f928c6f..f31f1e5bd 100644--- a/mobile/lib/phone/phone_shell.dart+++ b/mobile/lib/phone/phone_shell.dart@@ -1,5 +1,4 @@import 'dart:async';-import 'dart:io' show Platform;import 'package:flutter/cupertino.dart' show CupertinoPageRoute;import 'package:flutter/material.dart';@@ -11,7 +10,6 @@ import '../p2p/phone_terminal_p2p.dart';import '../swarm/swarm_activity_page.dart' show swarmActivityRoute;import '../swarm/swarm_attachment_store.dart';import '../swarm/swarm_computers.dart';-import '../swarm/swarm_dictation.dart';import '../swarm/swarm_home_root.dart';import '../swarm/swarm_inbox.dart' show SwarmDecisionNeed;import '../swarm/swarm_message_link.dart';@@ -28,8 +26,9 @@ import 'new_device_banner.dart';import 'phone_navigation.dart';import 'phone_shell_scope.dart';import 'phone_status.dart';+import 'resume_agent.dart';import 'settings_page.dart' show openPhoneSettings;-import 'voice_input_controller.dart';+import 'voice_recorder.dart';import 'voice_input_scope.dart';import 'welcome/connect_code.dart';@@ -121,18 +120,10 @@ class _PhoneShellState extends State<PhoneShell> with WidgetsBindingObserver {uploadAvatar: swarmAvatarUploadVia((tabId, png) => widget.notifier.api.uploadSwarmAvatar(tabId, png),),- // A chat's mic hears as a terminal's does: through the backend's- // speech-to-text, or sample mode's, which records nothing.- voice: () =>- VoiceInputScope.maybeOf(context)?.create() ??- VoiceInputController(- transcriber: (wav, lang) =>- widget.notifier.api.transcribeVoice(wav, lang: lang),- ),- // And first through the iPhone's own recognizer, the words in the box- // as they are said. Not in sample mode, which opens no microphone.- dictation: Platform.isIOS && VoiceInputScope.maybeOf(context) == null- ? SwarmChannelDictation()+ // A chat's mic records voice messages with the terminal's recorder. Not+ // in sample mode, which has no account to send them with.+ voice: VoiceInputScope.maybeOf(context) == null+ ? MicVoiceRecorder.new: null,)..foreground = state == null || _inFront(state);_swarmFiles = SwarmAttachmentKeeper(_swarm);@@ -603,6 +594,10 @@ class _PhoneShellState extends State<PhoneShell> with WidgetsBindingObserver {onShowSwarm: _showSwarm,canOpenMachine: (machineId) =>phoneMachineOpensTerminals(widget.notifier.machineStates[machineId]),+ agentStopped: (machineId, agentId) =>+ phoneAgentStopped(widget.notifier, machineId, agentId),+ onResumeAgent: (machineId, agentId) =>+ phoneResumeAgent(widget.notifier, machineId, agentId),// Rebuilt when the root switches, so back from Harness can reach Swarm Home.child: ListenableBuilder(listenable: _home,diff --git a/mobile/lib/phone/phone_shell_scope.dart b/mobile/lib/phone/phone_shell_scope.dartindex 515d5f6e5..26960b657 100644--- a/mobile/lib/phone/phone_shell_scope.dart+++ b/mobile/lib/phone/phone_shell_scope.dart@@ -12,6 +12,8 @@ class PhoneShellScope extends InheritedWidget {required this.onOpenAgent,this.onShowSwarm,this.canOpenMachine,+ this.agentStopped,+ this.onResumeAgent,required super.child,});@@ -27,6 +29,18 @@ class PhoneShellScope extends InheritedWidget {/// ([onOpenAgent]). Null where nothing can be opened.final bool Function(String machineId)? canOpenMachine;+ /// Whether [agentId] on [machineId] is stopped there, as this phone's list+ /// of the account's own machines says: null while it runs or is not listed;+ /// true for stopped work [onResumeAgent] can bring back, false for stopped+ /// work it cannot. Null where nothing can be resumed.+ final bool? Function(String machineId, String agentId)? agentStopped;++ /// Brings [agentId]'s stopped work on [machineId] back and waits for its+ /// terminal: null once it can be opened ([onOpenAgent]), or what to say.+ /// Null where nothing can be resumed.+ final Future<String?> Function(String machineId, String agentId)?+ onResumeAgent;+/// Swarm Home at the root, every page over it closed: Swarm asked for from/// the terminal (its mark, Find). Null where the shell has no Swarm Home to/// go back to, and a Swarm page is pushed instead (`openSwarmHome`).diff --git a/mobile/lib/phone/resume_agent.dart b/mobile/lib/phone/resume_agent.dartindex 696080a68..34d30a9fe 100644--- a/mobile/lib/phone/resume_agent.dart+++ b/mobile/lib/phone/resume_agent.dart@@ -87,3 +87,54 @@ bool _resumedTerminal(AppNotifier notifier, AgentEntry entry, Agent stopped) {current.launchState != 'failed' &&stopped.resumedAsPromised(current);}++/// Whether [agentId]'s session on [machineId] is stopped there, as this phone's+/// list of that machine's agents says — what the Swarm's Open terminal reads+/// (`PhoneShellScope.agentStopped`). Null while it runs, or when the phone does+/// not list it, or lists it on a machine another account shares (the+/// desktop's Resume is for the person's own machines alone,+/// `belongsToOtherAccount`); true for stopped work [resumeAgentForOpen] can+/// bring back ([Agent.canPauseAndResume]), false for stopped work it cannot.+bool? phoneAgentStopped(+ AppNotifier notifier,+ String machineId,+ String agentId,+) {+ final machine = notifier.stateOf(machineId);+ final agent = machine?.agents.where((a) => a.id == agentId).firstOrNull;+ if (machine == null ||+ machine.machine.shared ||+ agent == null ||+ !agent.isStopped) {+ return null;+ }+ return agent.canPauseAndResume;+}++/// Brings [agentId]'s stopped work on [machineId] back for the Swarm's Open+/// terminal (`PhoneShellScope.onResumeAgent`), the desktop's+/// `resumeStoppedSession`: [resumeAgentForOpen], read afresh at the press.+/// Null once there is a terminal to open — at once for work that is no longer+/// stopped — or what to say.+Future<String?> phoneResumeAgent(+ AppNotifier notifier,+ String machineId,+ String agentId,+) async {+ final machine = notifier.stateOf(machineId);+ // Never offered there ([phoneAgentStopped]): refused should it be asked.+ if (machine != null && machine.machine.shared) {+ return 'Only the person whose machine runs this agent can resume it.';+ }+ final agent = machine?.agents.where((a) => a.id == agentId).firstOrNull;+ if (machine == null || agent == null) {+ // The desktop pane's words for a harness its machine no longer lists.+ return 'This harness is no longer on '+ '${machine?.machine.displayName ?? 'this machine'}.';+ }+ if (!agent.isStopped) return null;+ return resumeAgentForOpen(+ notifier,+ AgentEntry(machine: machine, agent: agent),+ );+}diff --git a/mobile/lib/phone/settings_page.dart b/mobile/lib/phone/settings_page.dartindex 2b7399bb6..82d7da6dd 100644--- a/mobile/lib/phone/settings_page.dart+++ b/mobile/lib/phone/settings_page.dart@@ -21,7 +21,13 @@ import 'package:harness_mobile/shared/theme/color_palette.dart';import 'package:harness_mobile/state/app_state.dart';import 'package:harness_mobile/swarm/swarm_start.dart' show SwarmIntroPage;import 'package:harness_mobile/swarm/swarm_style.dart'- show SwarmAvatar, SwarmBackButton, SwarmCaption, SwarmRowGlyph, SwarmTopBar;+ show+ SwarmAvatar,+ SwarmBackButton,+ SwarmCaption,+ SwarmRowGlyph,+ SwarmSegments,+ SwarmTopBar;import 'package:harness_mobile/terminal/terminal_font_store.dart';import 'package:harness_mobile/terminal/terminal_theme_store.dart';@@ -89,6 +95,7 @@ class SettingsPage extends StatelessWidget {required this.notifier,this.large = true,this.experiments,+ this.appearance,this.swarm = false,});@@ -103,6 +110,10 @@ class SettingsPage extends StatelessWidget {/// tap there never writes to the developer's own state file.final ExperimentalFeaturesStore? experiments;+ /// What Appearance changes: [appearancePrefsStore] unless a test hands in its own, for the same+ /// reason as [experiments].+ final AppearancePrefsStore? appearance;+/// The tab's big title. Off when the page is pushed from a terminal's menu, where it needs the/// back chevron a large header does not draw.final bool large;@@ -118,6 +129,7 @@ class SettingsPage extends StatelessWidget {final body = _Body(notifier: notifier,experiments: experiments ?? experimentalFeaturesStore,+ appearance: appearance ?? appearancePrefsStore,);// In the app's face, as Swarm's pages are; only the bar's title is the terminal's.if (swarm) {@@ -196,10 +208,15 @@ class SettingsPage extends StatelessWidget {}class _Body extends StatelessWidget {- const _Body({required this.notifier, required this.experiments});+ const _Body({+ required this.notifier,+ required this.experiments,+ required this.appearance,+ });final AppNotifier notifier;final ExperimentalFeaturesStore experiments;+ final AppearancePrefsStore appearance;@overrideWidget build(BuildContext context) => ListView(@@ -266,7 +283,17 @@ class _Body extends StatelessWidget {SettingsGroup(children: [_VoiceLanguageRow()]),],const SettingsCaption('Appearance'),- SettingsGroup(children: [_PaletteRow(), _TextSizeRow()]),+ SettingsGroup(+ children: [+ // Swarm's Settings asks light or dark — System, Dark, Light — where the terminal's offers+ // the eight palettes. The terminal's own page stays dark under either answer.+ if (SettingsSwarmLook.of(context))+ _ThemeRow(store: appearance)+ else+ _PaletteRow(store: appearance),+ _TextSizeRow(),+ ],+ ),// The desktop's Labs ▸ Experimental, and in the same place: after how the app looks, before// Help. Features still being built, each off until turned on here.const SettingsCaption('Experimental'),@@ -1102,10 +1129,11 @@ class _TerminalThemeRow extends StatelessWidget {valueListenable: terminalThemeStore,builder: (context, choice, _) {AppTheme.watch(context);- // "Same as the app" names a rule; the palette it resolves to names a colour.+ // "Same as the app" names a rule; the palette it resolves to names a colour — the+ // terminal's own, which stays dark while Swarm's Theme is chosen, whatever the app wears.String label(TerminalThemeChoice option) =>option == TerminalThemeChoice.matchApp- ? appearancePrefsStore.value.palette.label+ ? AppTheme.terminalPalette.label: option.label;return SettingsRow(title: 'Colors',@@ -1138,28 +1166,36 @@ class _TerminalThemeRow extends StatelessWidget {/// repaints on the tap anyway — [AppearancePrefsStore.setPalette] moves the notifier before it/// writes — so the preview IS the app behind the sheet, at full size, which no swatch can beat.///-/// Under Dark and Light captions, because a light palette is the whole of light mode: there is no-/// other switch, and a person looking for one scans for the word.+/// Under Dark and Light captions, because a light palette is the whole of light mode in the+/// terminal's Settings: Swarm's Settings asks light or dark instead ([_ThemeRow]).+///+/// It names the palette on screen — which Swarm's Theme may have chosen (Paper, under Light) — and+/// a pick here hands light and dark back to the palette ([AppearancePrefsStore.setPalette]).class _PaletteRow extends StatelessWidget {+ const _PaletteRow({required this.store});++ final AppearancePrefsStore store;+@overrideWidget build(BuildContext context) => ValueListenableBuilder<AppearancePrefs>(- valueListenable: appearancePrefsStore,+ valueListenable: store,builder: (context, prefs, _) {AppTheme.watch(context);+ final shown = AppTheme.palette.value;PhoneSheetAction choice(HarnessPalette palette) => PhoneSheetAction(- icon: palette == prefs.palette+ icon: palette == shown? LucideIcons.check300: LucideIcons.swatchBook300,label: palette.label,value: palette.description,- onTap: () => unawaited(appearancePrefsStore.setPalette(palette)),+ onTap: () => unawaited(store.setPalette(palette)),);return SettingsRow(// "App colors", beside the terminal's own "Colors": both read Graphite by default, and// two rows with one value looked like the same setting twice.title: 'App colors',glyph: LucideIcons.contrast300,- value: prefs.palette.label,+ value: shown.label,onTap: () => showPhoneSheet(context,title: 'App colors',@@ -1182,6 +1218,48 @@ class _PaletteRow extends StatelessWidget {);}+/// Light or dark, in Settings opened from Swarm: System | Dark | Light, in App colors' place.+///+/// One tap, and every screen turns at once — the page behind is the preview — except the+/// terminal's page, which keeps the dark side ([AppearancePrefs.terminalPalette]). Dark and Light+/// keep the palette App colors chose while it is on their side, else Graphite and Paper. System+/// follows the phone as it turns. Until it is touched, the control shows the side the palette is+/// on, which is what that answer would draw.+class _ThemeRow extends StatelessWidget {+ const _ThemeRow({required this.store});++ final AppearancePrefsStore store;++ @override+ Widget build(BuildContext context) => ValueListenableBuilder<AppearancePrefs>(+ valueListenable: store,+ builder: (context, prefs, _) {+ AppTheme.watch(context);+ return SettingsRow(+ key: const ValueKey('settings-theme'),+ title: 'Theme',+ glyph: LucideIcons.sunMoon300,+ // A little over half the row, so the title keeps its word at the largest text size; past+ // that the control shrinks rather than pushing the row over its edge.+ trailing: ConstrainedBox(+ constraints: BoxConstraints(+ maxWidth: MediaQuery.sizeOf(context).width * .55,+ ),+ child: FittedBox(+ fit: BoxFit.scaleDown,+ child: SwarmSegments<AppThemeMode>(+ values: AppThemeMode.values,+ selected: prefs.shownThemeMode,+ label: (mode) => mode.label,+ onSelected: (mode) => unawaited(store.setThemeMode(mode)),+ ),+ ),+ ),+ );+ },+ );+}+/// The app's own text size — everything that is not the terminal.class _TextSizeRow extends StatelessWidget {@overridediff --git a/mobile/lib/phone/terminal_key_bar.dart b/mobile/lib/phone/terminal_key_bar.dartindex d2e3d6504..029c6df66 100644--- a/mobile/lib/phone/terminal_key_bar.dart+++ b/mobile/lib/phone/terminal_key_bar.dart@@ -10,6 +10,7 @@ import 'package:harness_mobile/clipboard/native_clipboard.dart';import 'package:harness_mobile/shared/theme/app_theme.dart';import 'package:harness_mobile/terminal/key_hints.dart';import 'package:harness_mobile/terminal/terminal_font_store.dart';+import 'package:harness_mobile/terminal/terminal_look.dart';import 'phone_sheet.dart';import 'tty.dart';@@ -542,11 +543,15 @@ class _TerminalKeyBarState extends State<TerminalKeyBar>List<Widget> _rule() => _canSendImage? [const SizedBox(width: 6),- Container(width: 1, height: 22, color: AppGlass.hair),+ Container(width: 1, height: 22, color: _hair),const SizedBox(width: 6),]: [const SizedBox(width: _gap)];+ /// The rules' hairline, on the terminal's palette ([TerminalLook]): this bar sits on a+ /// terminal's page, which stays dark in a light app.+ Color get _hair => TerminalLook.read(context, () => AppGlass.hair);+/// How wide [_rule] is — `6 + 1 + 6`, or the single gap in its place.double get _ruleWidth => _canSendImage ? 13 : _gap;@@ -619,7 +624,7 @@ class _TerminalKeyBarState extends State<TerminalKeyBar>/// the way [_rule] sets the phone's own two apart.List<Widget> _hintRule() => [const SizedBox(width: 6),- Container(width: 1, height: 22, color: AppGlass.hair),+ Container(width: 1, height: 22, color: _hair),const SizedBox(width: 6),];@@ -940,9 +945,13 @@ class _KeyCapState extends State<_KeyCap> {style: style(lit? foreground- : widget.live- ? AppPalette.textSecondary- : AppPalette.textFaint,+ // On the terminal's palette, as the bar is ([TerminalLook]).+ : TerminalLook.read(+ context,+ () => widget.live+ ? AppPalette.textSecondary+ : AppPalette.textFaint,+ ),FontWeight.w400,),),diff --git a/mobile/lib/phone/terminal_page.dart b/mobile/lib/phone/terminal_page.dartindex 88d79d4af..ccd4c41c0 100644--- a/mobile/lib/phone/terminal_page.dart+++ b/mobile/lib/phone/terminal_page.dart@@ -29,6 +29,7 @@ import 'package:harness_mobile/shared/widgets/skeleton.dart';import 'package:harness_mobile/state/app_state.dart';import 'package:harness_mobile/terminal/image_transcode.dart';import 'package:harness_mobile/terminal/terminal_font_store.dart';+import 'package:harness_mobile/terminal/terminal_look.dart';import 'package:harness_mobile/terminal/terminal_theme.dart';import 'package:harness_mobile/terminal/terminal_theme_store.dart';import 'package:harness_mobile/terminal/elicitation_press.dart';@@ -2522,7 +2523,8 @@ class _TerminalPageState extends State<TerminalPage>// already took. See [_navigationBar].final navigationBar = _navigationBar;return Scaffold(- backgroundColor: AppPalette.windowBg,+ // On the terminal's palette, which stays dark in a light app — see [TerminalLook].+ backgroundColor: TerminalLook.read(context, () => AppPalette.windowBg),// ⚠️ No fab. New agent is the `+` in the header — see the note there.// A Scaffold fab floats over the body, and the body here is the// terminal: it covered the newest line of output, which on a page@@ -3828,8 +3830,11 @@ class _AgentGone extends StatelessWidget {final VoidCallback onPickAnother;+ // Its tokens read on the terminal's palette, which stays dark in a light app ([TerminalLook]).@override- Widget build(BuildContext context) => Center(+ Widget build(BuildContext context) => TerminalLook.read(context, _body);++ Widget _body() => Center(child: Padding(padding: const EdgeInsets.symmetric(horizontal: 32),child: Column(@@ -4012,7 +4017,11 @@ class _ReconnectingState extends State<_Reconnecting> {// Still with animations off: three quarters of a ring, so it reads as waiting.child: CircularProgressIndicator(strokeWidth: 2,- color: AppPalette.accent,+ // The terminal's accent, which stays dark in a light app ([TerminalLook]).+ color: TerminalLook.read(+ context,+ () => AppPalette.accent,+ ),value: still ? 0.75 : null,),),@@ -4198,9 +4207,10 @@ class _AttachingState extends State<_Attaching> with TickerProviderStateMixin {// same band. `height` is xterm's line-height multiplier.final lineHeight = fontSize * style.height;// Resolved exactly as [TerminalPanel] resolves it for the view underneath,- // so the skeleton's ground and the terminal's are the same colour.+ // so the skeleton's ground and the terminal's are the same colour — on the+ // terminal's palette, as the panel's is ([TerminalLook]).final ground = terminalScreenThemeFor(- AppTheme.palette.value,+ TerminalLook.paletteOf(context),terminalThemeStore.value,).background;// ⚠️ **Mixed against the terminal's OWN ground, not taken fromdiff --git a/mobile/lib/phone/terminal_title.dart b/mobile/lib/phone/terminal_title.dartindex aa957063f..b7a5e672f 100644--- a/mobile/lib/phone/terminal_title.dart+++ b/mobile/lib/phone/terminal_title.dart@@ -3,6 +3,7 @@ import 'package:flutter/services.dart';import 'package:lucide_icons_flutter/lucide_icons.dart';import 'package:harness_mobile/shared/theme/app_theme.dart';+import 'package:harness_mobile/terminal/terminal_look.dart';import 'phone_status.dart' show PhoneMachineStatus, phoneMachineWord;import 'tty.dart';@@ -103,7 +104,7 @@ class TerminalTitle extends StatelessWidget {////// With [asking] the whole stack is its button — a tap opens Find, which is also the way off an/// agent whose machine is away. Without it the words are the title's, like the rest of it.- Widget? _notes(Tty tty, double height) {+ Widget? _notes(BuildContext context, Tty tty, double height) {final asking = this.asking;if (asking == null && !away) return null;Text note(String text, Color color, {Key? key}) => Text(@@ -133,10 +134,11 @@ class TerminalTitle extends StatelessWidget {if (asking != null) note(asking, tty.yellow),// The app's warning amber, not the terminal's red: a machine offline is something// to know, not an error — and the 16 ANSI colours have no orange.+ // On the terminal's palette, as the title is ([TerminalLook]).if (away)note(_awayWords,- AppPalette.warn,+ TerminalLook.read(context, () => AppPalette.warn),key: const ValueKey('terminal-title-away'),),],@@ -250,7 +252,7 @@ class TerminalTitle extends StatelessWidget {size: TtySize.meta,),),- ?_notes(tty, height),+ ?_notes(context, tty, height),?daemon,],),diff --git a/mobile/lib/phone/tty.dart b/mobile/lib/phone/tty.dartindex 46eda4f89..05a3ca82f 100644--- a/mobile/lib/phone/tty.dart+++ b/mobile/lib/phone/tty.dart@@ -3,6 +3,7 @@ import 'package:xterm/xterm.dart' show TerminalTheme;import 'package:harness_mobile/shared/theme/app_theme.dart';import 'package:harness_mobile/terminal/terminal_font_store.dart';+import 'package:harness_mobile/terminal/terminal_look.dart';import 'package:harness_mobile/terminal/terminal_theme.dart';import 'package:harness_mobile/terminal/terminal_theme_store.dart';@@ -14,7 +15,8 @@ import 'package:harness_mobile/terminal/terminal_theme_store.dart';/// agent's terminal is the product; around it, the phone draws what tmux and fzf would.////// Colours come from the terminal's resolved theme ([terminalThemeFor]), so the chrome changes with-/// the terminal when its colours do.+/// the terminal when its colours do. On a terminal's page ([TerminalLook]) that theme is resolved on+/// the terminal's palette, which stays dark in a light app; everywhere else on the app's.class Tty {Tty._(this.theme, this.fontFamily, this.fontFallback, this.fontSize);@@ -23,7 +25,10 @@ class Tty {AppTheme.watch(context);final font = terminalFontStore.value;return Tty._(- terminalThemeFor(AppTheme.palette.value, terminalThemeStore.value),+ terminalThemeFor(+ TerminalLook.paletteOf(context),+ terminalThemeStore.value,+ ),font.fontFamily,font.fontFamilyFallback,// The terminal's own size, exactly: chrome and output share one cell, so a status line or andiff --git a/mobile/lib/phone/voice_mic_face.dart b/mobile/lib/phone/voice_mic_face.dartindex 1f9bf286e..30813ac01 100644--- a/mobile/lib/phone/voice_mic_face.dart+++ b/mobile/lib/phone/voice_mic_face.dart@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';import 'package:lucide_icons_flutter/lucide_icons.dart';import 'package:harness_mobile/shared/theme/app_theme.dart';+import 'package:harness_mobile/terminal/terminal_look.dart';import 'floating_glass.dart';import 'tty.dart';@@ -625,7 +626,8 @@ class _HeldBadge extends StatelessWidget {height: size,decoration: BoxDecoration(shape: BoxShape.circle,- color: AppPalette.warn,+ // On the terminal's palette where the mic sits on a terminal's page ([TerminalLook]).+ color: TerminalLook.read(context, () => AppPalette.warn),// A ring the terminal's own near-black, so the dot reads as sitting// ON the rim rather than as a stain on the fill — its ground, on a// light one.diff --git a/mobile/lib/shared/theme/app_theme.dart b/mobile/lib/shared/theme/app_theme.dartindex ed4dcc7b0..1fc2f39bb 100644--- a/mobile/lib/shared/theme/app_theme.dart+++ b/mobile/lib/shared/theme/app_theme.dart@@ -23,7 +23,22 @@ abstract final class AppTheme {/// through the same [watch], since a widget that reads one token generally/// reads both.static final FontNotifier fonts = FontNotifier();- static final palette = ValueNotifier(HarnessPalette.graphite);+ static final palette = PaletteNotifier(HarnessPalette.graphite);++ /// The palette a terminal's page wears when it is not [palette], or null for "the same".+ ///+ /// Set from Settings ▸ Appearance ▸ Theme (`AppearancePrefs.terminalPalette`): with the theme+ /// chosen there the terminal keeps the dark side whatever the app wears, so Light — or System on+ /// a light phone — leaves the agent's page dark. App colors leaves it null: a palette picked+ /// there dresses the terminal too, as it always has.+ ///+ /// Read through [terminalPalette], and only under a `TerminalLook`+ /// (`terminal/terminal_look.dart`), which is what marks a terminal's page.+ static final terminalPaletteOverride = ValueNotifier<HarnessPalette?>(null);++ /// The palette a terminal's page wears — see [terminalPaletteOverride].+ static HarnessPalette get terminalPalette =>+ terminalPaletteOverride.value ?? palette.value;static bool get isDark => brightness.value == Brightness.dark;@@ -58,6 +73,25 @@ abstract final class AppTheme {});}+ /// Read tokens as they would resolve on [other] — its colours AND its+ /// brightness — the way [as] reads another brightness, and on the same terms:+ /// swapped silently, read synchronously, put back in a `finally`.+ ///+ /// For a page that wears a palette the app does not: a terminal's, under a+ /// `TerminalLook`, which stays dark in a light app.+ static T asPalette<T>(HarnessPalette other, T Function() read) {+ final previous = palette.value;+ if (previous == other) return as(other.brightness, read);+ return palette.muted(() {+ try {+ palette.value = other;+ return as(other.brightness, read);+ } finally {+ palette.value = previous;+ }+ });+ }+/// Registers the calling widget to rebuild whenever [brightness] flips, and/// returns the current value.///@@ -86,6 +120,9 @@ abstract final class AppTheme {context.dependOnInheritedWidgetOfExactType<_BrightnessScope>();context.dependOnInheritedWidgetOfExactType<_FontScope>();context.dependOnInheritedWidgetOfExactType<_PaletteScope>();+ // The terminal's palette can move while the app's stays put — App colors picking the very+ // palette Theme had resolved to, for one — and a terminal's page has to hear that too.+ context.dependOnInheritedWidgetOfExactType<_TerminalPaletteScope>();return brightness.value;}}@@ -122,14 +159,26 @@ class FontNotifier extends ChangeNotifier {/// spurious rebuilds to draw three previews, all to end up back where we/// started. [muted] suppresses the broadcast for a change that is, from the/// outside, not a change at all.-class BrightnessNotifier extends ValueNotifier<Brightness> {+class BrightnessNotifier extends QuietNotifier<Brightness> {BrightnessNotifier(super.value);+}++/// The palette global, silent in the same way as [BrightnessNotifier] — for+/// [AppTheme.asPalette], which swaps both.+class PaletteNotifier extends QuietNotifier<HarnessPalette> {+ PaletteNotifier(super.value);+}++/// A [ValueNotifier] that can be moved and moved back without a broadcast —+/// see [BrightnessNotifier].+class QuietNotifier<T> extends ValueNotifier<T> {+ QuietNotifier(super.value);bool _muted = false;/// Run [body] with notifications suppressed. Only for a swap that restores the/// original value before anything can observe it — see [AppTheme.as].- T muted<T>(T Function() body) {+ R muted<R>(R Function() body) {final previous = _muted;_muted = true;try {@@ -165,7 +214,13 @@ class BrightnessScope extends StatelessWidget {notifier: AppTheme.brightness,child: _FontScope(notifier: AppTheme.fonts,- child: _PaletteScope(notifier: AppTheme.palette, child: child),+ child: _PaletteScope(+ notifier: AppTheme.palette,+ child: _TerminalPaletteScope(+ notifier: AppTheme.terminalPaletteOverride,+ child: child,+ ),+ ),),);}@@ -183,6 +238,11 @@ class _PaletteScope extends InheritedNotifier<ValueNotifier<HarnessPalette>> {const _PaletteScope({required super.notifier, required super.child});}+class _TerminalPaletteScope+ extends InheritedNotifier<ValueNotifier<HarnessPalette?>> {+ const _TerminalPaletteScope({required super.notifier, required super.child});+}+/// The app's palette — a warm paper white with near-black ink in light, a deep/// charcoal with off-white ink in dark (the design-system "Codex" direction)./// Centralized so every pane reads the same surfaces/accents instead of re-typingdiff --git a/mobile/lib/shared/theme/appearance_builder.dart b/mobile/lib/shared/theme/appearance_builder.dartnew file mode 100644index 000000000..72472f31e--- /dev/null+++ b/mobile/lib/shared/theme/appearance_builder.dart@@ -0,0 +1,40 @@+import 'package:flutter/widgets.dart';++import 'appearance_prefs_store.dart';+import 'color_palette.dart';++/// Builds the app on its appearance: [store]'s choices, put on `AppTheme` by [applyAppearance]+/// for this phone's own light or dark before [builder] runs — the order the theme needs, since+/// `buildAppTheme` bakes the palette in.+///+/// It hears both: a choice in Settings, and the phone itself turning light or dark, which is what+/// Theme's System follows. The phone's side is read from the [MediaQuery] the root `View` puts+/// above the app, so it is there before `MaterialApp` is.+class AppearanceBuilder extends StatelessWidget {+ const AppearanceBuilder({super.key, this.store, required this.builder});++ /// [appearancePrefsStore] unless a test hands in its own.+ final AppearancePrefsStore? store;++ /// The app, with the choices and the palette it wears for them.+ final Widget Function(+ BuildContext context,+ AppearancePrefs prefs,+ HarnessPalette palette,+ )+ builder;++ @override+ Widget build(BuildContext context) => ValueListenableBuilder<AppearancePrefs>(+ valueListenable: store ?? appearancePrefsStore,+ builder: (context, prefs, _) => builder(+ context,+ prefs,+ applyAppearance(+ prefs,+ platform:+ MediaQuery.maybePlatformBrightnessOf(context) ?? Brightness.dark,+ ),+ ),+ );+}diff --git a/mobile/lib/shared/theme/appearance_prefs_store.dart b/mobile/lib/shared/theme/appearance_prefs_store.dartindex b09ce454e..c5ce7e715 100644--- a/mobile/lib/shared/theme/appearance_prefs_store.dart+++ b/mobile/lib/shared/theme/appearance_prefs_store.dart@@ -5,6 +5,28 @@ import '../../core/local_key_value_store.dart';import 'app_theme.dart';import 'color_palette.dart';+/// Light or dark, as Settings opened from Swarm offers it: System, Dark or Light.+///+/// Beside [HarnessPalette], not instead of it. App colors (the terminal's Settings) still picks an+/// exact palette, and a palette carries its own brightness; Theme only says which side of it to+/// wear — see [AppearancePrefs.paletteFor].+enum AppThemeMode {+ /// This phone's own light or dark, followed as it changes.+ system('System'),+ dark('Dark'),+ light('Light');++ const AppThemeMode(this.label);++ /// What the control says.+ final String label;++ /// The saved name read back; anything else — a build that had a mode this one does not — is+ /// null, which is "the palette decides", as before Theme existed.+ static AppThemeMode? fromId(String? id) =>+ values.where((mode) => mode.name == id).firstOrNull;+}+/// The app's coordinated palette and UI typography on this computer.////// This does not change the terminal's type. The terminal renders a grid a remote@@ -18,10 +40,49 @@ class AppearancePrefs {this.uiFamily,this.uiSize = uiSizeDefault,this.palette = HarnessPalette.graphite,+ this.themeMode,});+ /// The palette chosen in App colors — or, before any was, the shipped one.final HarnessPalette palette;+ /// Settings ▸ Appearance ▸ Theme, as Swarm's Settings offers it. Null — the default, and what+ /// App colors sets — means [palette] decides on its own, light or dark, as it always has. So+ /// nobody's app changes until they choose here.+ final AppThemeMode? themeMode;++ /// The palette each side falls back to when [palette] is on the other: the phone's two themes,+ /// as Swarm's pages are drawn and reviewed in.+ static const darkPalette = HarnessPalette.graphite;+ static const lightPalette = HarnessPalette.paper;++ /// The palette the app wears on a phone whose own appearance is [platform].+ ///+ /// [palette] itself while it is on the side [themeMode] asks for — so a Midnight chosen in App+ /// colors stays Midnight under Dark, and comes back after a trip to Light — and the shipped+ /// palette of that side when it is not.+ HarnessPalette paletteFor(Brightness platform) => _side(switch (themeMode) {+ null => palette.brightness,+ AppThemeMode.system => platform,+ AppThemeMode.dark => Brightness.dark,+ AppThemeMode.light => Brightness.light,+ });++ /// The palette a terminal's page wears: the dark side whenever Theme is chosen, whatever the app+ /// wears around it — a terminal is read as a terminal. Left to App colors ([themeMode] null),+ /// the terminal wears [palette], light or dark, as before.+ HarnessPalette get terminalPalette =>+ themeMode == null ? palette : _side(Brightness.dark);++ /// What Theme's control shows chosen: [themeMode], or the side [palette] is on while App colors+ /// decides — which is exactly what Dark or Light would draw.+ AppThemeMode get shownThemeMode =>+ themeMode ?? (palette.isDark ? AppThemeMode.dark : AppThemeMode.light);++ HarnessPalette _side(Brightness side) => palette.brightness == side+ ? palette+ : (side == Brightness.dark ? darkPalette : lightPalette);+/// The face the app's chrome is set in. `null` means the system font, which is/// what [AppFont.sans] falls back to.///@@ -47,15 +108,20 @@ class AppearancePrefs {/// [uiFamily] is nullable and `copyWith` cannot express "unset" with/// `?? this.x`, so going back to the system font needs its own flag. Without/// one, choosing System silently does nothing.+ ///+ /// [themeMode] is nullable for the same reason, and [clearThemeMode] is its flag.AppearancePrefs copyWith({String? uiFamily,double? uiSize,HarnessPalette? palette,+ AppThemeMode? themeMode,bool clearUiFamily = false,+ bool clearThemeMode = false,}) => AppearancePrefs(uiFamily: clearUiFamily ? null : (uiFamily ?? this.uiFamily),uiSize: uiSize ?? this.uiSize,palette: palette ?? this.palette,+ themeMode: clearThemeMode ? null : (themeMode ?? this.themeMode),);@override@@ -63,10 +129,11 @@ class AppearancePrefs {other is AppearancePrefs &&other.uiFamily == uiFamily &&other.uiSize == uiSize &&- other.palette == palette;+ other.palette == palette &&+ other.themeMode == themeMode;@override- int get hashCode => Object.hash(uiFamily, uiSize, palette);+ int get hashCode => Object.hash(uiFamily, uiSize, palette, themeMode);}/// The user's appearance choices, remembered across launches.@@ -84,6 +151,10 @@ class AppearancePrefsStore extends ValueNotifier<AppearancePrefs> {static const _familyKey = 'app_ui_font_family';static const _sizeKey = 'app_ui_font_size';static const _paletteKey = 'app_color_palette';+ static const _themeModeKey = 'app_theme_mode';++ /// The palette and the theme mode on their way to disk — one write loop for the two, because+ /// App colors moves both at once (see [setPalette]).Future<void>? _paletteSave;final LocalKeyValueStore _storage;@@ -99,11 +170,13 @@ class AppearancePrefsStore extends ValueNotifier<AppearancePrefs> {_familyKey,_sizeKey,_paletteKey,+ _themeModeKey,]);value = AppearancePrefs(uiFamily: _familyFrom(saved[_familyKey]),uiSize: _sizeFrom(saved[_sizeKey]),palette: HarnessPalette.fromId(saved[_paletteKey]),+ themeMode: AppThemeMode.fromId(saved[_themeModeKey]),);} catch (_) {value = const AppearancePrefs();@@ -112,9 +185,22 @@ class AppearancePrefsStore extends ValueNotifier<AppearancePrefs> {/// Preview now, persist in order. Rapid choices coalesce while storage is/// busy so an older write cannot replace the user's final selection.+ ///+ /// App colors names an exact palette, so it hands light and dark back to the palette too: a+ /// Theme left on System would otherwise answer a tap on Midnight, on a light phone, with Paper.Future<void> setPalette(HarnessPalette palette) {- if (value.palette == palette) return _paletteSave ?? Future.value();- value = value.copyWith(palette: palette);+ if (value.palette == palette && value.themeMode == null) {+ return _paletteSave ?? Future.value();+ }+ value = value.copyWith(palette: palette, clearThemeMode: true);+ return _paletteSave ??= _savePalette();+ }++ /// Theme's System, Dark or Light. The palette stays as App colors left it — see+ /// [AppearancePrefs.paletteFor] — and persists in the same ordered loop as [setPalette].+ Future<void> setThemeMode(AppThemeMode mode) {+ if (value.themeMode == mode) return _paletteSave ?? Future.value();+ value = value.copyWith(themeMode: mode);return _paletteSave ??= _savePalette();}@@ -122,8 +208,15 @@ class AppearancePrefsStore extends ValueNotifier<AppearancePrefs> {try {while (true) {final id = value.palette.name;+ final mode = value.themeMode?.name;await _storage.write(_paletteKey, id);- if (value.palette.name == id) break;+ if (mode == null) {+ // Deleted, not written as '': absent IS "the palette decides".+ await _storage.delete(_themeModeKey);+ } else {+ await _storage.write(_themeModeKey, mode);+ }+ if (value.palette.name == id && value.themeMode?.name == mode) break;}} catch (_) {// The chosen palette remains usable for this run if storage fails.@@ -172,6 +265,7 @@ class AppearancePrefsStore extends ValueNotifier<AppearancePrefs> {await _storage.delete(_familyKey);await _storage.delete(_sizeKey);await _storage.delete(_paletteKey);+ await _storage.delete(_themeModeKey);} catch (_) {// See above.}@@ -199,6 +293,21 @@ class AppearancePrefsStore extends ValueNotifier<AppearancePrefs> {: size.clamp(AppearancePrefs.uiSizeMin, AppearancePrefs.uiSizeMax);}+/// Puts [prefs]' palettes on [AppTheme] for a phone whose own appearance is [platform], and returns+/// the app's: [AppearancePrefs.paletteFor] as the app's, and [AppearancePrefs.terminalPalette] as a+/// terminal page's when it differs. What the app does before it builds its theme — see+/// `AppearanceBuilder`.+HarnessPalette applyAppearance(+ AppearancePrefs prefs, {+ required Brightness platform,+}) {+ final app = prefs.paletteFor(platform);+ final terminal = prefs.terminalPalette;+ AppTheme.palette.value = app;+ AppTheme.terminalPaletteOverride.value = terminal == app ? null : terminal;+ return app;+}+/// The one instance the app reads.////// Here rather than beside `main()` for the same reason `terminalFontStore` is:diff --git a/mobile/lib/shared/theme/system_bars.dart b/mobile/lib/shared/theme/system_bars.dartnew file mode 100644index 000000000..9e659527d--- /dev/null+++ b/mobile/lib/shared/theme/system_bars.dart@@ -0,0 +1,18 @@+import 'package:flutter/services.dart';++/// The system bars' style for an app of [brightness]: icons that contrast with it, over the+/// transparent bars `main.dart` draws edge to edge under. The colours are restated because an+/// [AnnotatedRegion]'s style replaces the one set at launch.+SystemUiOverlayStyle systemBarsFor(Brightness brightness) {+ // The icons are the opposite of the app; iOS's `statusBarBrightness` names the GROUND instead.+ final icons = brightness == Brightness.dark+ ? Brightness.light+ : Brightness.dark;+ return SystemUiOverlayStyle(+ statusBarColor: const Color(0x00000000),+ systemNavigationBarColor: const Color(0x00000000),+ statusBarIconBrightness: icons,+ systemNavigationBarIconBrightness: icons,+ statusBarBrightness: brightness,+ );+}diff --git a/mobile/lib/swarm/model.dart b/mobile/lib/swarm/model.dartindex 01615dedd..bf2b6da55 100644--- a/mobile/lib/swarm/model.dart+++ b/mobile/lib/swarm/model.dart@@ -1163,7 +1163,8 @@ class SuperTabAttachment {unlinked = row['unlinked'] == true,expiresAt = row['expiresAt'] is num? (row['expiresAt'] as num).toInt()- : null;+ : null,+ voice = SuperTabVoice.read(row['voice']);/// [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@@ -1183,6 +1184,10 @@ class SuperTabAttachment {/// When [link] stops opening, in epoch milliseconds.final int? expiresAt;+ /// What makes this file a voice message, drawn as one+ /// (swarm_voice_bubble.dart); null for any other file.+ final SuperTabVoice? voice;+/// The kinds the backend serves to be seen in place; every other kind is/// served as a download.bool get isImage => const {@@ -1193,6 +1198,48 @@ class SuperTabAttachment {}.contains(mime);}+/// 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 {+ const SuperTabVoice({+ required this.length,+ required this.waveform,+ required this.transcribed,+ this.overLimit = false,+ });++ /// The tallest bar of [waveform].+ static const barMax = 31;++ /// A voice that does not read as one is none: the file still shows, as a+ /// file.+ static SuperTabVoice? read(Object? row) {+ if (row is! Map) return null;+ final ms = row['durationMs'], bars = row['waveform'];+ final transcribed = row['transcribed'];+ if (ms is! num || bars is! List || transcribed is! bool) return null;+ return SuperTabVoice(+ length: Duration(milliseconds: ms.toInt()),+ waveform: List.unmodifiable([+ for (final bar in bars) bar is num ? bar.toInt().clamp(0, barMax) : 0,+ ]),+ transcribed: transcribed,+ overLimit: row['failure'] == 'limit',+ );+ }++ final Duration length;++ /// How loud each slice of the clip is, 0 to [barMax].+ final List<int> waveform;++ /// Whether its words were heard; without them the message's text is empty.+ final bool transcribed;++ /// It has no words because its sender's daily voice limit was used.+ final bool overLimit;+}+/// A message's files. A row that does not read is left out, so one bad row/// cannot hide the page of messages it came in.List<SuperTabAttachment> _attachments(Object? rows) => List.unmodifiable([diff --git a/mobile/lib/swarm/swarm_buddy_chat.dart b/mobile/lib/swarm/swarm_buddy_chat.dartindex 58c38dfa5..918be5932 100644--- a/mobile/lib/swarm/swarm_buddy_chat.dart+++ b/mobile/lib/swarm/swarm_buddy_chat.dart@@ -587,6 +587,7 @@ class SwarmBuddyMenuButton extends StatelessWidget {actions: [if (wrote) ...[swarmTerminalAction(+ context: context,agent: () => swarmBuddy(reader, actions.projectId),allowed: () =>!reader.accessRevoked &&@@ -594,6 +595,7 @@ class SwarmBuddyMenuButton extends StatelessWidget {swarmCanWrite(reader),shell: shell,computers: computers,+ until: reader,),PhoneSheetAction(icon: LucideIcons.pencil,diff --git a/mobile/lib/swarm/swarm_conversation.dart b/mobile/lib/swarm/swarm_conversation.dartindex 9660080dd..71f3017a2 100644--- a/mobile/lib/swarm/swarm_conversation.dart+++ b/mobile/lib/swarm/swarm_conversation.dart@@ -3,6 +3,8 @@ import 'dart:async';import 'package:clock/clock.dart';import 'package:flutter/foundation.dart';+import 'package:harness_mobile/phone/voice_recorder.dart' show VoiceTake;+import 'model.dart';import 'prompt_alerts.dart';import 'swarm_chat_cache.dart';@@ -13,6 +15,7 @@ import 'swarm_outgoing_files.dart';import 'swarm_reader.dart';import 'swarm_request.dart';import 'swarm_uploads.dart';+import 'swarm_voice_send.dart';/// One conversation on the phone: its messages and what the person posts to/// it — the desktop's `SuperTabController` messages and composer, for one@@ -944,6 +947,59 @@ class SwarmConversation extends ChangeNotifierString? _startedRoot;+ /// Sends [take] as a voice message, alone, as a reply to [replyingTo] when+ /// there is one: its clip goes up, the backend reads it once in [lang], and+ /// the post names that file. What is typed stays in the box. Not sent, the+ /// take is the voice row's to keep; a post whose answer was lost is the+ /// outbox's, sent again from where it shows like any other.+ Future<SwarmVoicePosted> sendVoice(VoiceTake take, String lang) async {+ final group = conversation;+ if (uploading || _disposed || !canPost || group == null) {+ return (sent: false, notice: null);+ }+ final clip = SwarmOutgoingFile(swarmVoiceFileName, take.wav);+ // A failure says itself in the chat, as a file's does.+ final ids = await _uploadAll([clip]);+ if (ids == null || _disposed) return (sent: false, notice: null);+ final read = await swarmReadVoice(+ reader.request,+ tabId: reader.tabId,+ attachmentId: ids.single,+ lang: lang,+ );+ final voice = read.voice;+ if (_disposed || voice == null) return (sent: false, notice: read.refused);+ final operationId = superTabOperationId();+ final post = SwarmPost(+ body: {+ 'operationId': operationId,+ 'command': {+ 'type': 'message.send',+ 'conversationId': id,+ 'text': '',+ 'replyTo': _replyingTo?.id,+ 'recipientIds': const <String>[],+ 'attachmentIds': ids,+ if (reader.snapshot?.swarmBuddy == true) 'oneBuddy': true,+ },+ },+ messageId: superTabMessageId(+ tabId: reader.tabId,+ accountId: reader.accountId,+ operationId: operationId,+ ),+ files: [clip],+ voice: voice,+ );+ _replyingTo = null;+ error = null;+ final outcome = await _outbox.add(post);+ _answered(post, outcome);+ final sent =+ outcome == SwarmPostOutcome.taken || outcome == SwarmPostOutcome.lost;+ return (sent: sent, notice: sent ? swarmVoiceSentNote(voice) : null);+ }+/// The thread whose session the last post started, once the backend took/// it; read once.String? takeStartedSession() {diff --git a/mobile/lib/swarm/swarm_conversation_page.dart b/mobile/lib/swarm/swarm_conversation_page.dartindex ecc5cd71d..1207b9a8a 100644--- a/mobile/lib/swarm/swarm_conversation_page.dart+++ b/mobile/lib/swarm/swarm_conversation_page.dart@@ -9,6 +9,8 @@ import 'package:harness_mobile/phone/composing_keyboard.dart';import 'package:harness_mobile/phone/phone_navigation.dart';import 'package:harness_mobile/phone/tty.dart';import 'package:harness_mobile/phone/tty_controls.dart';+import 'package:harness_mobile/phone/voice_language_store.dart';+import 'package:harness_mobile/phone/voice_recorder.dart' show VoiceTake;import 'package:harness_mobile/shared/theme/app_ink.dart';import 'package:harness_mobile/shared/theme/app_space.dart';import 'package:harness_mobile/shared/theme/app_theme.dart';@@ -58,6 +60,9 @@ import 'swarm_thread_line.dart';import 'swarm_thread_page.dart';import 'swarm_unread_line.dart';import 'swarm_voice.dart';+import 'swarm_voice_bubble.dart';+import 'swarm_voice_send.dart' show SwarmVoicePosted;+import 'voice_length.dart';import 'team_glance.dart';final _conversationReferencePattern = RegExp(r'(?<![\w/])#([1-9][0-9]*)(?!\w)');@@ -338,13 +343,7 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>}if (_voice == null) {if (SwarmScope.voiceOf(context) case final create?) {- _voice = SwarmVoice(- create: create,- dictation: SwarmScope.dictationOf(context),- draft: () => _conversation?.draft ?? '',- write: (text) => _conversation?.draft = text,- post: _voiceSend,- );+ _voice = SwarmVoice(recorder: create, post: _voiceSend);}}// A covered chat hears nothing: its take goes with it.@@ -837,11 +836,13 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>});}- /// The voice row's Send, once its take has ended into the draft: the- /// composer's own Send, in the chat shown now.- void _voiceSend() {+ /// The voice row's Send: the take as a voice message, in the chat shown+ /// now, its words heard in the mic's language.+ Future<SwarmVoicePosted> _voiceSend(VoiceTake take) async {final c = _conversation;- if (mounted && c != null && c.canSend) _send(c);+ if (!mounted || c == null) return (sent: false, notice: null);+ _target = null;+ return c.sendVoice(take, voiceLanguageStore.value);}/// Sends the draft, back at the foot of the chat where the post shows.@@ -1030,6 +1031,7 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>(picture: null, status: null),text: post.text,files: post.files,+ voice: post.voice,mentions: _view.value?.mentions ?? SwarmMentions(const []),state: post.state,quote: null,@@ -2093,7 +2095,7 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>: SwarmQuote(key: ValueKey('swarm-quote-${m.id}'),author: quoted,- text: reply.text,+ text: swarmQuoteText(reply),onTap: () => unawaited(_jumpTo(c, m.replyTo!)),),actions: acting,@@ -2226,13 +2228,17 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>(picture: null, status: null),text: post.text,files: post.files,+ voice: post.voice,mentions: view.mentions,state: post.state,quote: post.replyTo == null? null: reply == null? _readQuote(session, c, c.id, post.replyTo!)- : SwarmQuote(author: _quoted(c, reply), text: reply.text),+ : SwarmQuote(+ author: _quoted(c, reply),+ text: swarmQuoteText(reply),+ ),onSendAgain: () => unawaited(_retry(c, post)),onDelete: () => c.deletePost(post),);@@ -2333,7 +2339,7 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>final query = c.mentionQuery;final replying = c.replyingTo;final voice = _voice;- final talking = voice?.face != null;+ final talking = voice?.active ?? false;// A post on its way never holds the next back; one not confirmed is sent// again from where it shows. Files alone are a post too.final canSend = c.canSend;@@ -2392,7 +2398,7 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>),TextSpan(text:- ': ${replying.text.replaceAll(RegExp(r'\s+'), ' ')}',+ ': ${swarmQuoteText(replying).replaceAll(RegExp(r'\s+'), ' ')}',),],),@@ -2450,21 +2456,6 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>style: AppType.caption(color: tty.yellow),),),- // Quiet: nothing went wrong, the words stay in the box.- if (_voice?.stopped case final stopped?)- Padding(- padding: const EdgeInsets.fromLTRB(- AppSpace.xs,- 0,- AppSpace.xs,- AppSpace.s,- ),- child: Text(- stopped,- key: const ValueKey('swarm-voice-stopped'),- style: AppType.caption(color: AppPalette.textSecondary),- ),- ),ValueListenableBuilder(valueListenable: _sessionNote,builder: (context, note, _) => note == null || talking@@ -3253,6 +3244,7 @@ class _Pending extends StatelessWidget {required this.name,required this.text,required this.files,+ this.voice,required this.mentions,required this.state,required this.quote,@@ -3267,6 +3259,10 @@ class _Pending extends StatelessWidget {/// nothing until the post is read back with their links.final List<SwarmOutgoingFile> files;+ /// What makes it a voice message, its one file the clip: drawn as its+ /// bubble, which plays once the post is read back.+ final SuperTabVoice? voice;+/// The person's own face.final SwarmLook look;final SwarmMentions mentions;@@ -3311,17 +3307,30 @@ class _Pending extends StatelessWidget {),),),- for (final file in files)- SwarmFileChip(+ if (voice case final voice? when files.length == 1)+ SwarmVoiceBubble(file: SuperTabAttachment.fromJson({'id': '',- 'name': file.name,- 'mime': swarmMimeForName(file.name),- 'size': file.size,+ 'name': files.single.name,+ 'mime': swarmMimeForName(files.single.name),+ 'size': files.single.size,'url': '',}),- link: null,- ),+ voice: voice,+ words: '',+ )+ else+ for (final file in files)+ SwarmFileChip(+ file: SuperTabAttachment.fromJson({+ 'id': '',+ 'name': file.name,+ 'mime': swarmMimeForName(file.name),+ 'size': file.size,+ 'url': '',+ }),+ link: null,+ ),if (state != SwarmPostState.confirmed)Padding(padding: const EdgeInsets.only(top: AppSpace.xs),diff --git a/mobile/lib/swarm/swarm_dictation.dart b/mobile/lib/swarm/swarm_dictation.dartdeleted file mode 100644index 78a1aabc2..000000000--- a/mobile/lib/swarm/swarm_dictation.dart+++ /dev/null@@ -1,329 +0,0 @@-import 'dart:async';-import 'dart:math' as math;--import 'package:flutter/services.dart';--/// What the person said about the phone's speech recognizer and its-/// microphone, asked the first time a chat's mic is tapped.-enum SwarmDictationAccess {- /// Both allowed: words show as they are said.- allowed,-- /// Speech recognition refused or restricted: the chat records and has the- /// backend transcribe instead, as before.- denied,-- /// The microphone refused: nothing can be heard either way.- noMic,-}--/// How a take with the phone's own recognizer ended ([SwarmDictation.start]'s-/// `onEnd`).-enum SwarmDictationEnd {- /// Stopped, with its last words delivered.- done,-- /// Stopped, and nothing was said in its last session.- noSpeech,-- /// The recognizer failed, or cannot go on: what it delivered so far stays,- /// the rest is lost.- failed,-}--/// The phone's own speech recognizer, the one the keyboard's dictation uses-/// (Tony, 10-28): its words come back while the person is still talking.-/// iOS only ([SwarmChannelDictation]); elsewhere a chat records and the-/// backend transcribes.-///-/// One take at a time: [start] ends any earlier one without a word.-abstract interface class SwarmDictation {- /// Asks for speech recognition and the microphone the first time; after- /// that, what the person answered.- Future<SwarmDictationAccess> access();-- /// Listens in [locale] (`vi-VN`) until [stop] or [cancel] (Tony, 10-29:- /// "until I tap"). False when the phone has no recognizer for it now.- /// [onWords] hears every word so far each time — revised as the recognizer- /// changes its mind — [onLevel] how loud the voice is over the room (0…1),- /// and [onEnd] once: after [stop], or when listening cannot go on.- Future<bool> start(- String locale, {- required void Function(String words) onWords,- required void Function(double level) onLevel,- required void Function(SwarmDictationEnd end) onEnd,- });-- /// Stops listening: the last words, then [start]'s `onEnd`.- Future<void> stop();-- /// Stops listening and drops anything still to come: no more words, and no- /// `onEnd`.- Future<void> cancel();-}--/// The recognizer's locale for a code from `voiceLanguages`: Vietnamese for-/// one it does not know.-String swarmDictationLocale(String language) => switch (language) {- 'en' => 'en-US',- 'es' => 'es-ES',- 'fr' => 'fr-FR',- 'ja' => 'ja-JP',- 'it' => 'it-IT',- _ => 'vi-VN',-};--/// [SwarmDictation] over `harness/dictation` (iOS: `DictationChannel` in-/// `AppDelegate.swift`, `SFSpeechRecognizer`).-///-/// Each take has a number of its own, sent with every call and said back with-/// every event, so words from a take already ended land nowhere.-///-/// ⚠️ **Its guesses replace each other ([_stitched]), never add up.** The-/// recognizer says its latest best guess at the words not yet finished, and-/// rewrites it as it goes ("1", "Một 2", "123"); only a guess it says is-/// finished, or one its next words begin after in the take's audio — on the-/// phone it can start over after a pause, without a word — is kept, once, in-/// front of what it says next. Never a guess by how the words look: Tony's-/// 'một hai ba bốn năm' came out as 'Một 2 12345' that way (10-29).-///-/// ⚠️ **A take outlasts the recognizer's sessions ([_resume]).** iOS ends a-/// session by itself — after a pause, a long silence, its own minute — and-/// the native side says so (`paused`) with the microphone still on: what it-/// said is kept, and a new session starts at once.-class SwarmChannelDictation implements SwarmDictation {- SwarmChannelDictation({MethodChannel? channel, DateTime Function()? clock})- : _channel = channel ?? const MethodChannel('harness/dictation'),- _clock = clock ?? DateTime.now {- _channel.setMethodCallHandler(_heard);- }-- final MethodChannel _channel;- final DateTime Function() _clock;- int _take = 0;- ({- void Function(String) words,- void Function(double) level,- void Function(SwarmDictationEnd) end,- })?- _to;-- /// What the recognizer has finished saying in this take, and its guess at- /// what comes after: where that ends in the take's audio, and whether it- /// said that guess was finished.- String _done = '', _guess = '';- double _guessTo = 0;- bool _guessFinished = false;-- /// How far a guess may begin before the last one ended and still be new- /// words: the recognizer's own times overlap a little at a word's edge.- static const _overlap = 0.05;-- /// [stop] was asked: a session ending now is the take's last.- bool _stopping = false;-- /// When sessions ended since the recognizer last said a word. [_quick] of- /// them within [_quickWithin] is a recognizer that cannot hear now —- /// offline, or broken — rather than one waiting through a silence.- final List<DateTime> _pauses = [];- static const _quick = 3;- static const _quickWithin = Duration(seconds: 2);-- /// The microphone's last [_roomWindow] levels, in dB, a tenth of a second- /// each: the quietest of them is the room, and the wave shows how far the- /// voice is above it ([_level]) — whatever the microphone's own gain, which- /// the recognizer's audio session leaves low.- final List<double> _recent = [];- static const _roomWindow = 30;-- /// The quietest a room is taken to be: below it, near-digital silence.- static const _silence = -75.0;-- /// A voice [_overRoom] dB over the room starts the wave, and [_overRoom] +- /// [_range] fills it.- static const _overRoom = 3.0, _range = 25.0;-- @override- Future<SwarmDictationAccess> access() async {- try {- return switch (await _channel.invokeMethod<String>('access')) {- 'allowed' => SwarmDictationAccess.allowed,- 'noMic' => SwarmDictationAccess.noMic,- _ => SwarmDictationAccess.denied,- };- } on PlatformException {- return SwarmDictationAccess.denied;- } on MissingPluginException {- return SwarmDictationAccess.denied;- }- }-- @override- Future<bool> start(- String locale, {- required void Function(String words) onWords,- required void Function(double level) onLevel,- required void Function(SwarmDictationEnd end) onEnd,- }) async {- final take = ++_take;- _to = (words: onWords, level: onLevel, end: onEnd);- _done = _guess = '';- _guessTo = 0;- _guessFinished = false;- _stopping = false;- _pauses.clear();- _recent.clear();- try {- final started = await _channel.invokeMethod<bool>('start', {- 'take': take,- 'locale': locale,- });- if (started != true && take == _take) _to = null;- return started == true;- } on PlatformException {- if (take == _take) _to = null;- return false;- }- }-- @override- Future<void> stop() async {- _stopping = true;- try {- await _channel.invokeMethod<void>('stop', {'take': _take});- } on PlatformException {- _ended(_take, SwarmDictationEnd.failed);- }- }-- @override- Future<void> cancel() async {- final take = _take++;- _to = null;- try {- await _channel.invokeMethod<void>('cancel', {'take': take});- } on PlatformException {- // Nothing more is wanted from it either way.- }- }-- Future<void> _heard(MethodCall call) async {- final arguments = call.arguments;- if (arguments is! Map || arguments['take'] != _take) return;- final to = _to;- if (to == null) return;- switch (call.method) {- case 'words':- if (arguments['text'] case final String text) {- _pauses.clear();- to.words(- _stitched(- text,- finished: arguments['final'] == true,- from: _seconds(arguments['from']),- to: _seconds(arguments['to']),- ),- );- }- case 'level':- if (arguments['rms'] case final num rms) {- to.level(_level(rms.toDouble()));- }- case 'paused':- await _resume(_take);- case 'end':- _ended(_take, switch (arguments['end']) {- 'done' => SwarmDictationEnd.done,- 'noSpeech' => SwarmDictationEnd.noSpeech,- _ => SwarmDictationEnd.failed,- });- }- }-- /// The recognizer ended its session by itself: its words are kept, and the- /// take listens on in a new one — unless it is stopping, or sessions keep- /// ending at once with nothing heard, or the phone will not start another.- Future<void> _resume(int take) async {- if (_stopping) return;- _done = _joined(_done, _guess);- _guess = '';- _guessTo = 0;- _guessFinished = false;- final now = _clock();- _pauses- ..removeWhere((at) => now.difference(at) > _quickWithin)- ..add(now);- if (_pauses.length >= _quick) return _giveUp(take);- var resumed = false;- try {- resumed =- await _channel.invokeMethod<bool>('resume', {'take': take}) == true;- } on PlatformException {- resumed = false;- }- if (!resumed && take == _take && !_stopping) _giveUp(take);- }-- /// Listening cannot go on: the take ends as failed, its words kept.- void _giveUp(int take) {- _ended(take, SwarmDictationEnd.failed);- unawaited(cancel());- }-- /// How far [rms] (0…1 of full scale) is above the room, for the wave.- double _level(double rms) {- if (rms <= 0) return 0;- final db = 20 * math.log(rms) / math.ln10;- _recent.add(db);- if (_recent.length > _roomWindow) _recent.removeAt(0);- final room = math.max(_recent.reduce(math.min), _silence);- return ((db - room - _overRoom) / _range).clamp(0.0, 1.0);- }-- /// Every word of the take so far: what is [_done], then [guess], the- /// recognizer's latest — which replaces the last, unless that one is done.- ///- /// The last guess is done when this one begins after it ended in the take's- /// audio ([from], [to]: seconds, 0 where the recognizer gives none). Without- /// both times, when the recognizer said it was [finished] and this one is not the- /// same words again with more after them, as a recognizer that keeps the- /// whole take in each guess says it.- String _stitched(- String guess, {- required bool finished,- required double from,- required double to,- }) {- final timed = from > 0 && _guessTo > 0;- final after = timed && from >= _guessTo - _overlap;- final again = _plain(guess).join(' ').startsWith(_plain(_guess).join(' '));- if (_guess.isNotEmpty && (after || (!timed && _guessFinished && !again))) {- _done = _joined(_done, _guess);- }- _guess = guess;- _guessTo = to;- _guessFinished = finished;- return _joined(_done, guess);- }-- static double _seconds(Object? value) =>- value is num && value.isFinite ? value.toDouble() : 0;-- /// [text]'s words without case or punctuation, to be compared.- static List<String> _plain(String text) => text- .toLowerCase()- .replaceAll(RegExp(r'[^\p{L}\p{N}\s]', unicode: true), '')- .split(RegExp(r'\s+'))- .where((word) => word.isNotEmpty)- .toList();-- static String _joined(String kept, String text) =>- kept.isEmpty || text.isEmpty ? '$kept$text' : '$kept $text';-- void _ended(int take, SwarmDictationEnd end) {- if (take != _take) return;- final to = _to;- _to = null;- to?.end(end);- }-}diff --git a/mobile/lib/swarm/swarm_link_previews.dart b/mobile/lib/swarm/swarm_link_previews.dartindex ba546a2e1..760e55325 100644--- a/mobile/lib/swarm/swarm_link_previews.dart+++ b/mobile/lib/swarm/swarm_link_previews.dart@@ -6,6 +6,7 @@ import 'swarm_message_link.dart';import 'swarm_message_reads.dart';import 'swarm_message_row.dart';import 'swarm_reader.dart';+import 'voice_length.dart';/// [message]'s author as a link's preview names them: "You" for the reader's/// own, else as its Swarm's roster names them, or the name the message was@@ -89,15 +90,16 @@ class SwarmLinkPreviews extends StatelessWidget {);}final here = link.tabId == tabId && link.conversationId == chatId;+ final words = swarmQuoteText(message).trim();return SwarmQuote(key: key,author: swarmLinkAuthor(reader, message),chat: here? null: reader.snapshot?.conversation(link.conversationId)?.label,- text: message.text.trim().isEmpty+ text: words.isEmpty? 'Attachment'- : message.text.trim().replaceAll(RegExp(r'\s+'), ' '),+ : words.replaceAll(RegExp(r'\s+'), ' '),label: 'Go to message',onTap: () => onOpen(link),);@@ -138,11 +140,11 @@ class SwarmReadQuote extends StatelessWidget {final original = read.message;return SwarmQuote(author: original == null ? null : author(original),- text:- original?.text ??- (read.state == SwarmReadState.gone- ? 'The original message is no longer in this chat.'- : null),+ text: original != null+ ? swarmQuoteText(original)+ : read.state == SwarmReadState.gone+ ? 'The original message is no longer in this chat.'+ : null,onTap: onTap,);},diff --git a/mobile/lib/swarm/swarm_message_row.dart b/mobile/lib/swarm/swarm_message_row.dartindex 0bd43f56e..655e4b21e 100644--- a/mobile/lib/swarm/swarm_message_row.dart+++ b/mobile/lib/swarm/swarm_message_row.dart@@ -24,6 +24,7 @@ import 'swarm_file_kind.dart';import 'swarm_file_links.dart';import 'swarm_file_viewer.dart';import 'swarm_message_text.dart';+import 'swarm_voice_bubble.dart';import 'swarm_message_style.dart';import 'swarm_face.dart';import 'swarm_ship_row.dart';@@ -373,6 +374,15 @@ class SwarmMessageRow extends StatelessWidget {messageId: message.id,reader: SwarmFiles.of(context),);+ // A voice message's clip is its bubble, its words behind →A.+ if (file.voice case final voice?) {+ return SwarmVoiceBubble(+ file: file,+ voice: voice,+ words: message.text,+ fileRef: ref,+ );+ }final link = ref.current;return SwarmAttachment(file: file,@@ -441,23 +451,25 @@ class SwarmMessageRow extends StatelessWidget {if (lit) SizedBox(key: ValueKey('swarm-lit-${message.id}')),?quote,?swarmTerminalNote(message),- for (final part in swarmTextParts(message.text))- if (part.code)- SwarmCodeBlock(code: part.text, name: part.name, onTap: onTap)- else- Padding(- padding: const EdgeInsets.only(top: SwarmMessageFrame.bodyGap),- child: SwarmMessageText(- text: part.text,- mentionNames: mentions.names,- references: references,- tagged: tagged,- addresses: taggedAddresses,- labels: taggedLabels,- onTap: onTap,- onOpenConversation: onOpenConversation,+ // A voice message's text is its words: under its bubble, on →A.+ if (!message.attachments.any((file) => file.voice != null))+ for (final part in swarmTextParts(message.text))+ if (part.code)+ SwarmCodeBlock(code: part.text, name: part.name, onTap: onTap)+ else+ Padding(+ padding: const EdgeInsets.only(top: SwarmMessageFrame.bodyGap),+ child: SwarmMessageText(+ text: part.text,+ mentionNames: mentions.names,+ references: references,+ tagged: tagged,+ addresses: taggedAddresses,+ labels: taggedLabels,+ onTap: onTap,+ onOpenConversation: onOpenConversation,+ ),),- ),?swarmCutShort(message),for (final file in message.attachments) _attachment(context, file),?card,diff --git a/mobile/lib/swarm/swarm_new_message_sheet.dart b/mobile/lib/swarm/swarm_new_message_sheet.dartindex fbf257ada..f13af56f3 100644--- a/mobile/lib/swarm/swarm_new_message_sheet.dart+++ b/mobile/lib/swarm/swarm_new_message_sheet.dart@@ -9,6 +9,8 @@ import 'package:unorm_dart/unorm_dart.dart' as unorm;import 'package:harness_mobile/core/project_folder.dart' show foldDiacritics;import 'package:harness_mobile/phone/tty.dart';import 'package:harness_mobile/phone/tty_controls.dart';+import 'package:harness_mobile/phone/voice_language_store.dart';+import 'package:harness_mobile/phone/voice_recorder.dart' show VoiceTake;import 'package:harness_mobile/shared/theme/app_icon_size.dart';import 'package:harness_mobile/shared/theme/app_ink.dart';import 'package:harness_mobile/shared/theme/app_space.dart';@@ -37,6 +39,7 @@ import 'swarm_row.dart';import 'swarm_session.dart';import 'swarm_style.dart';import 'swarm_voice.dart';+import 'swarm_voice_send.dart' show SwarmVoicePosted;/// A chat New message can post to: chat [chatId] of Swarm [swarmId].typedef SwarmChatPick = ({String swarmId, String chatId});@@ -287,21 +290,8 @@ class _SwarmNewMessageSheetState extends State<SwarmNewMessageSheet>_visible = TickerMode.valuesOf(context).enabled;if (_voice == null) {if (SwarmScope.voiceOf(context) case final create?) {- _voice = SwarmVoice(- create: create,- dictation: SwarmScope.dictationOf(context),- draft: () => _text.text,- write: (text) {- if (_locked || _closing) return;- _text.value = TextEditingValue(- text: text,- selection: TextSelection.collapsed(offset: text.length),- );- },- post: () {- if (mounted && _canSend) unawaited(_send());- },- )..addListener(_redraw);+ _voice = SwarmVoice(recorder: create, post: _sendVoice)+ ..addListener(_redraw);}}// A covered sheet hears nothing: its take goes with it.@@ -503,6 +493,37 @@ class _SwarmNewMessageSheetState extends State<SwarmNewMessageSheet>setState(() {});}+ /// Posts [take] as a voice message to the chat picked, through that chat's+ /// own send, its words heard in the mic's language; what is typed stays.+ /// Taken, the sheet closes with it, as a typed post's does.+ Future<SwarmVoicePosted> _sendVoice(VoiceTake take) async {+ final pick = _picked;+ if (pick == null || _find(pick) == null || _closing || _locked) {+ return (sent: false, notice: null);+ }+ final reader = _inbox.reader(pick.swarmId);+ if (reader == null || reader.accessRevoked) {+ return (sent: false, notice: null);+ }+ if (_chat case final held?+ when held.reader != reader || held.id != pick.chatId) {+ _forgetChat();+ }+ final chat = _chat ??= SwarmConversation(reader, pick.chatId, shown: false)+ ..composer.addListener(_redraw);+ setState(() => _error = _note = null);+ final posted = await chat.sendVoice(take, voiceLanguageStore.value);+ if (!mounted || _closing || _chat != chat) return posted;+ if (!posted.sent) {+ setState(() => _error = chat.error);+ return posted;+ }+ chat.shown = true;+ _close(pick);+ setState(() {});+ return posted;+ }+@overrideWidget build(BuildContext context) {AppTheme.watch(context);@@ -713,7 +734,7 @@ class _SwarmNewMessageSheetState extends State<SwarmNewMessageSheet>final canSend = _canSend;final locked = _locked;final voice = _voice;- final talking = voice?.face != null;+ final talking = voice?.active ?? false;Widget note(String text, String key, Color color) => Padding(padding: const EdgeInsets.fromLTRB(AppSpace.xs,@@ -739,12 +760,6 @@ class _SwarmNewMessageSheetState extends State<SwarmNewMessageSheet>note(text, 'swarm-new-message-attach-note', tty.yellow),if (_voice?.notice case final notice?)note(notice, 'swarm-new-message-voice-note', tty.yellow),- if (_voice?.stopped case final stopped?)- note(- stopped,- 'swarm-new-message-voice-stopped',- AppPalette.textSecondary,- ),Flexible(child: SwarmComposerCard(focus: _focus,diff --git a/mobile/lib/swarm/swarm_outbox.dart b/mobile/lib/swarm/swarm_outbox.dartindex c831483c0..335a932d4 100644--- a/mobile/lib/swarm/swarm_outbox.dart+++ b/mobile/lib/swarm/swarm_outbox.dart@@ -44,6 +44,7 @@ class SwarmPost {required this.body,required this.messageId,this.files = const [],+ this.voice,this.held,});@@ -57,6 +58,10 @@ class SwarmPost {/// The files that went with it, already uploaded.final List<SwarmOutgoingFile> files;+ /// A voice message's facts, as the backend read its one file: how it shows+ /// while on its way. Not kept across a restart, when it shows as its file.+ final SuperTabVoice? voice;+/// What the composer held when it left (draft, mentions, reply): a refusal/// puts it back.final Object? held;diff --git a/mobile/lib/swarm/swarm_outgoing_files.dart b/mobile/lib/swarm/swarm_outgoing_files.dartindex 9ebba8221..391d0654d 100644--- a/mobile/lib/swarm/swarm_outgoing_files.dart+++ b/mobile/lib/swarm/swarm_outgoing_files.dart@@ -248,4 +248,6 @@ const _mimes = {'log': 'text/plain','md': 'text/markdown','csv': 'text/csv',+ // A voice message's clip (swarm_voice.dart), which the backend reads as one.+ 'wav': 'audio/wav',};diff --git a/mobile/lib/swarm/swarm_project_page.dart b/mobile/lib/swarm/swarm_project_page.dartindex de135143f..25b15fd55 100644--- a/mobile/lib/swarm/swarm_project_page.dart+++ b/mobile/lib/swarm/swarm_project_page.dart@@ -527,12 +527,16 @@ class _SwarmProjectPageState extends State<SwarmProjectPage> {builder: (_) => SwarmProjectDetailsSheet(reader: reader,projectId: project.id,+ // The page's context, not the sheet's: a resume's question is asked+ // once the sheet has gone.terminal: (shell, computers) => allowed()? swarmTerminalAction(+ context: context,agent: () => swarmProjectPm(reader.snapshot, project.id),allowed: allowed,shell: shell,computers: computers,+ until: reader,): null,onStatus: _openStatusSheet,@@ -963,10 +967,12 @@ class _ProjectMenu extends StatelessWidget {// computers do ([SwarmScope.computersOf] depends on its host).final terminal = allowed()? swarmTerminalAction(+ context: context,agent: () => swarmProjectPm(reader.snapshot, projectId),allowed: allowed,shell: shell,computers: computers,+ until: reader,): null;return MenuTheme(diff --git a/mobile/lib/swarm/swarm_session.dart b/mobile/lib/swarm/swarm_session.dartindex 780a661e4..ca284ec6e 100644--- a/mobile/lib/swarm/swarm_session.dart+++ b/mobile/lib/swarm/swarm_session.dart@@ -5,12 +5,11 @@ import 'package:flutter/widgets.dart';import '../core/harness_file_store.dart';import '../core/local_key_value_store.dart';import '../notify/system_notices.dart';-import '../phone/voice_input_controller.dart';+import '../phone/voice_recorder.dart';import '../settings/experimental_features.dart';import 'swarm_activity.dart';import 'swarm_chat_peeks.dart';import 'swarm_choices.dart';-import 'swarm_dictation.dart';import 'swarm_face.dart';import 'swarm_home_cache.dart';import 'swarm_home_prefs.dart';@@ -351,7 +350,6 @@ class SwarmHost extends ChangeNotifier {SwarmKeptStore? kept,this.uploadAvatar,this.voice,- this.dictation,}) : features = features ?? experimentalFeaturesStore,homeCache = homeCache ?? SwarmHomeCache.device(),readMarks = readMarks ?? HarnessFileStore.shared,@@ -398,15 +396,10 @@ class SwarmHost extends ChangeNotifier {/// go, and only the built-in pictures are offered.final SwarmAvatarUpload? uploadAvatar;- /// A new voice input for a chat's mic, which owns and disposes it — the- /// terminal's speech-to-text; null where none can be heard, and the+ /// A new microphone for a chat's voice messages (swarm_voice.dart), which+ /// the chat owns and disposes; null where none can be heard, and the/// composer has no mic.- final VoiceInputController Function()? voice;-- /// The phone's own speech recognizer, which shows a chat's words as they- /// are said; null where there is none, and the mic records and has [voice]- /// transcribe the take.- final SwarmDictation? dictation;+ final VoiceRecorder Function()? voice;SwarmSession? session;bool _foreground = true;@@ -537,15 +530,8 @@ class SwarmScope extends InheritedNotifier<SwarmHost> {?.notifier?.computers;- /// How a chat's mic makes its voice input ([SwarmHost.voice]); null+ /// How a chat's mic makes its microphone ([SwarmHost.voice]); null/// outside the shell or where none can be heard.- static VoiceInputController Function()? voiceOf(BuildContext context) =>+ static VoiceRecorder Function()? voiceOf(BuildContext context) =>context.dependOnInheritedWidgetOfExactType<SwarmScope>()?.notifier?.voice;-- /// The phone's own recognizer for a chat's mic ([SwarmHost.dictation]);- /// null outside the shell or where there is none.- static SwarmDictation? dictationOf(BuildContext context) => context- .dependOnInheritedWidgetOfExactType<SwarmScope>()- ?.notifier- ?.dictation;}diff --git a/mobile/lib/swarm/swarm_style.dart b/mobile/lib/swarm/swarm_style.dartindex d5dc119d8..7994ef490 100644--- a/mobile/lib/swarm/swarm_style.dart+++ b/mobile/lib/swarm/swarm_style.dart@@ -796,6 +796,126 @@ class SwarmButton extends StatelessWidget {}}+/// A choice of a few, side by side, that takes effect on the tap — Settings'+/// Theme: System | Dark | Light.+///+/// Swarm's chips (Activity's filters) drawn as one control: a pill track in the+/// search surface, the chosen segment filled in the primary ink with the+/// page's ground for its words, the others in the secondary ink. A segment is+/// a compact control ([AppControl.heightSmall]) with a control's words+/// ([AppType.label]); it is pressed across 44 points of height though it draws+/// 28, so the track sits in a 56-point row as the − + stepper does.+class SwarmSegments<T> extends StatelessWidget {+ const SwarmSegments({+ super.key,+ required this.values,+ required this.selected,+ required this.label,+ required this.onSelected,+ });++ final List<T> values;+ final T selected;+ final String Function(T value) label;++ /// A segment tapped that is not [selected] already.+ final ValueChanged<T> onSelected;++ @override+ Widget build(BuildContext context) {+ AppTheme.watch(context);+ return DecoratedBox(+ decoration: ShapeDecoration(+ color: AppPalette.swarmSearchSurface,+ shape: const StadiumBorder(),+ ),+ child: Padding(+ padding: const EdgeInsets.all(AppSpace.xs),+ child: Row(+ mainAxisSize: MainAxisSize.min,+ children: [+ for (final value in values)+ _SwarmSegment(+ label: label(value),+ selected: value == selected,+ onTap: () => onSelected(value),+ ),+ ],+ ),+ ),+ );+ }+}++class _SwarmSegment extends StatelessWidget {+ const _SwarmSegment({+ required this.label,+ required this.selected,+ required this.onTap,+ });++ final String label;+ final bool selected;+ final VoidCallback onTap;++ @override+ Widget build(BuildContext context) {+ AppTheme.watch(context);+ // The chosen one is already chosen: no click, nothing sent.+ final VoidCallback? choose = selected+ ? null+ : () {+ HapticFeedback.selectionClick();+ onTap();+ };+ return Semantics(+ button: true,+ selected: selected,+ label: label,+ // Its children's semantics are excluded, the InkWell's tap with them: a+ // screen reader taps through this node (review of #2920).+ onTap: choose,+ excludeSemantics: true,+ child: TouchTarget(+ child: Material(+ color: selected ? AppPalette.textPrimary : Colors.transparent,+ shape: const StadiumBorder(),+ clipBehavior: Clip.antiAlias,+ child: InkWell(+ onTap: choose,+ child: ConstrainedBox(+ constraints: const BoxConstraints(+ minHeight: AppControl.heightSmall,+ minWidth: minTouchTarget,+ ),+ child: Padding(+ // Three at the label size, System the longest, fit a 375-point+ // phone's row beside its title without shrinking.+ padding: const EdgeInsets.symmetric(horizontal: AppSpace.s),+ child: Center(+ widthFactor: 1,+ heightFactor: 1,+ child: Text(+ label,+ maxLines: 1,+ style: AppType.label(+ color: selected+ ? AppPalette.windowBg+ : AppPalette.textSecondary,+ fontWeight: selected ? AppFont.semibold : AppFont.medium,+ height: 1.2,+ ),+ ),+ ),+ ),+ ),+ ),+ ),+ ),+ );+ }+}+/// A round 44-point button holding one glyph: the drawer's close, the top/// bar's menu and back ([size] 24 there).class SwarmIconButton extends StatelessWidget {diff --git a/mobile/lib/swarm/swarm_terminal_resume.dart b/mobile/lib/swarm/swarm_terminal_resume.dartnew file mode 100644index 000000000..63b975b3c--- /dev/null+++ b/mobile/lib/swarm/swarm_terminal_resume.dart@@ -0,0 +1,196 @@+import 'package:flutter/material.dart';++import 'package:harness_mobile/phone/phone_shell_scope.dart';+import 'package:harness_mobile/shared/theme/app_space.dart';+import 'package:harness_mobile/shared/theme/app_theme.dart';+import 'package:harness_mobile/shared/theme/app_type.dart';++import 'model.dart';+import 'swarm_style.dart';++/// Asks before [agent]'s stopped session is started again on its machine,+/// then resumes it ([PhoneShellScope.onResumeAgent]) and, once its terminal+/// is back, opens it ([PhoneShellScope.onOpenAgent]): the desktop's Resume+/// on a Swarm agent's stopped pane (`workspace.dart`), for the person's own+/// machines alone. The desktop resumes at once; the phone asks first, as it+/// does before archiving, saying which machine it runs on ([machineName]).+///+/// The question goes, as Cancel, once [allowed] no longer holds, checked+/// whenever [until] changes (access lost, signed out, a viewer now). A resume+/// the machine refuses says why under the question, and Resume tries again.+Future<void> confirmSwarmResume(+ BuildContext context, {+ required SuperTabAgent agent,+ required String? machineName,+ required PhoneShellScope shell,+ required bool Function() allowed,+ Listenable? until,+}) async {+ final resume = shell.onResumeAgent;+ if (resume == null || !context.mounted || !allowed()) return;+ final resumed = await showDialog<bool>(+ context: context,+ builder: (_) => SwarmResumeDialog(+ name: agent.displayName ?? agent.name,+ machineName: machineName,+ resume: () => resume(agent.machineId, agent.harnessId),+ allowed: allowed,+ until: until,+ ),+ );+ if (resumed != true || !context.mounted || !allowed()) return;+ shell.onOpenAgent(agent.machineId, agent.harnessId);+}++/// [confirmSwarmResume]'s question: "Resume River?", where it runs, then+/// Cancel and Resume. Pops true once [resume] has brought the terminal back.+class SwarmResumeDialog extends StatefulWidget {+ const SwarmResumeDialog({+ super.key,+ required this.name,+ required this.machineName,+ required this.resume,+ required this.allowed,+ this.until,+ });++ final String name;+ final String? machineName;++ /// Null once the terminal can be opened, or what to say.+ final Future<String?> Function() resume;+ final bool Function() allowed;+ final Listenable? until;++ @override+ State<SwarmResumeDialog> createState() => _SwarmResumeDialogState();+}++class _SwarmResumeDialogState extends State<SwarmResumeDialog> {+ bool _busy = false;+ bool _closing = false;+ String? _error;++ @override+ void initState() {+ super.initState();+ widget.until?.addListener(_check);+ }++ @override+ void didUpdateWidget(SwarmResumeDialog oldWidget) {+ super.didUpdateWidget(oldWidget);+ if (oldWidget.until != widget.until) {+ oldWidget.until?.removeListener(_check);+ widget.until?.addListener(_check);+ }+ }++ @override+ void dispose() {+ widget.until?.removeListener(_check);+ super.dispose();+ }++ void _check() {+ if (widget.allowed()) return;+ _close(false);+ }++ /// Pops with [result], once: not while a listener is being told, after+ /// this frame.+ void _close(bool result) {+ if (_closing) return;+ _closing = true;+ WidgetsBinding.instance.addPostFrameCallback((_) {+ if (!mounted) return;+ final route = ModalRoute.of(context);+ if (route == null || !route.isActive) return;+ if (route.isCurrent) {+ Navigator.of(context).pop(result);+ } else {+ Navigator.of(context).removeRoute(route);+ }+ });+ WidgetsBinding.instance.ensureVisualUpdate();+ }++ Future<void> _resume() async {+ if (_busy || _closing || !widget.allowed()) return;+ setState(() {+ _busy = true;+ _error = null;+ });+ String? error;+ try {+ error = await widget.resume();+ } catch (_) {+ error = 'Could not resume this agent. Try again.';+ }+ if (!mounted || _closing) return;+ if (error == null) {+ _close(true);+ return;+ }+ setState(() {+ _busy = false;+ _error = error;+ });+ }++ @override+ Widget build(BuildContext context) {+ AppTheme.watch(context);+ final machine = widget.machineName;+ final error = _error;+ // Neither the veil nor back closes it while the machine is starting it.+ return PopScope(canPop: !_busy, child: _dialog(machine, error));+ }++ Widget _dialog(String? machine, String? error) {+ return AlertDialog(+ title: Text(+ 'Resume ${widget.name}?',+ key: const ValueKey('swarm-resume-title'),+ style: AppType.heading(color: AppPalette.textPrimary),+ ),+ content: Column(+ mainAxisSize: MainAxisSize.min,+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ Text(+ machine == null || machine.isEmpty+ ? 'This agent is stopped.'+ : 'This agent is stopped on $machine.',+ style: AppType.body(color: AppPalette.textSecondary),+ ),+ if (error != null) ...[+ const SizedBox(height: AppSpace.s),+ Text(+ error,+ key: const ValueKey('swarm-resume-error'),+ style: AppType.body(color: Theme.of(context).colorScheme.error),+ ),+ ],+ ],+ ),+ actions: [+ SwarmButton(+ key: const ValueKey('swarm-resume-cancel'),+ label: 'Cancel',+ style: SwarmButtonStyle.link,+ // The machine is already starting it: nothing to cancel then.+ onPressed: _busy ? null : () => _close(false),+ ),+ SwarmButton(+ key: const ValueKey('swarm-resume-confirm'),+ label: 'Resume',+ busyLabel: 'Resuming…',+ busy: _busy,+ style: SwarmButtonStyle.fill,+ onPressed: _resume,+ ),+ ],+ );+ }+}diff --git a/mobile/lib/swarm/swarm_terminal_row.dart b/mobile/lib/swarm/swarm_terminal_row.dartindex 7e2c0b3fc..d09f91b16 100644--- a/mobile/lib/swarm/swarm_terminal_row.dart+++ b/mobile/lib/swarm/swarm_terminal_row.dart@@ -1,3 +1,6 @@+import 'dart:async';++import 'package:flutter/widgets.dart';import 'package:lucide_icons_flutter/lucide_icons.dart';import 'package:harness_mobile/phone/phone_shell_scope.dart';@@ -5,19 +8,24 @@ import 'package:harness_mobile/phone/phone_sheet.dart';import 'model.dart';import 'swarm_start.dart' show SwarmComputers, SwarmOwnComputer;+import 'swarm_terminal_resume.dart';/// Why this phone cannot open [agent]'s terminal now, in the word its Open/// terminal row says; null when it can (Tony, 11-01). Team's rule: the/// agent's machine linked to this phone and not reported offline/// ([canOpen], `PhoneShellScope.canOpenMachine`). "Stopped" with no agent-/// running or one being stopped; "Not linked" for a machine this phone does-/// not have among the person's own [own] and cannot open (not linked here,-/// or a Harness Team account's); "Offline" for one it has but that is-/// reported offline, or an agent the Swarm says is down.+/// running or one being stopped, or a session its machine lists as stopped+/// ([stopped], `PhoneShellScope.agentStopped`; the desktop pane's word,+/// whether or not a resume can bring it back, [swarmTerminalResumes]); "Not+/// linked" for a machine this phone does not have among the person's own+/// [own] and cannot open (not linked here, or a Harness Team account's);+/// "Offline" for one it has but that is reported offline, or an agent the+/// Swarm says is down.String? swarmTerminalBlock(SuperTabAgent? agent, {required bool Function(String machineId)? canOpen,required List<SwarmOwnComputer> own,+ bool? Function(String machineId, String harnessId)? stopped,}) {if (agent == null ||agent.workDecision == 'stop' ||@@ -27,42 +35,98 @@ String? swarmTerminalBlock(}final opens = canOpen?.call(agent.machineId) == true;if (!opens && !own.any((m) => m.id == agent.machineId)) return 'Not linked';- if (!opens || agent.downSince != null) return 'Offline';+ if (!opens) return 'Offline';+ // Its machine answers, and says the session is stopped: the Swarm then+ // reads it as down too, which is no machine offline.+ if (stopped?.call(agent.machineId, agent.harnessId) != null) return 'Stopped';+ if (agent.downSince != null) return 'Offline';return null;}+/// Whether [agent]'s Open terminal row resumes it first: the Swarm has not+/// stopped it, and its machine — one this phone opens ([canOpen]) — lists its+/// session as stopped work a resume can bring back ([stopped] true). The+/// desktop's Resume on a Swarm agent's stopped pane, which its own machines+/// alone offer; the caller has checked the person may write.+bool swarmTerminalResumes(+ SuperTabAgent? agent, {+ required bool Function(String machineId)? canOpen,+ required bool? Function(String machineId, String harnessId)? stopped,+}) =>+ agent != null &&+ agent.workDecision != 'stop' &&+ agent.harnessId.isNotEmpty &&+ agent.machineId.isNotEmpty &&+ canOpen?.call(agent.machineId) == true &&+ stopped?.call(agent.machineId, agent.harnessId) == true;+/// A sheet's Open terminal row for the agent [agent] reads now: it opens that/// agent's terminal on this phone ([PhoneShellScope.onOpenAgent]), or is-/// dimmed with [swarmTerminalBlock]'s word. Checked again at the tap: still-/// [allowed], still the same agent, its machine still one the phone opens.+/// dimmed with [swarmTerminalBlock]'s word. Stopped work its own machine can+/// bring back says "Stopped" and is not dimmed: the tap asks first, over+/// [context], then resumes it and opens it ([confirmSwarmResume]; the question+/// goes once [allowed] no longer holds, or the agent is replaced or stopped by+/// the Swarm, checked as [until] changes). Checked+/// again at the tap: still [allowed], still the same agent, its machine still+/// one the phone opens, and a resume asked for only if it is still stopped./// The caller rebuilds it as the Swarm and [computers] change.PhoneSheetAction swarmTerminalAction({+ required BuildContext context,required SuperTabAgent? Function() agent,required bool Function() allowed,required PhoneShellScope? shell,required SwarmComputers? computers,+ Listenable? until,}) {String? block(SuperTabAgent? a) => swarmTerminalBlock(a,canOpen: shell?.canOpenMachine,own: computers?.own ?? const [],+ stopped: shell?.agentStopped,);+ bool resumes(SuperTabAgent? a) =>+ shell?.onResumeAgent != null &&+ swarmTerminalResumes(+ a,+ canOpen: shell?.canOpenMachine,+ stopped: shell?.agentStopped,+ );final shown = agent();final why = block(shown);return PhoneSheetAction(icon: LucideIcons.squareTerminal,label: 'Open terminal',value: why,- enabled: why == null,+ enabled: why == null || resumes(shown),onTap: () {final now = agent();- if (shell == null ||- now == null ||- now.id != shown?.id ||- !allowed() ||- block(now) != null) {+ if (shell == null || now == null || now.id != shown?.id || !allowed()) {+ return;+ }+ if (resumes(now)) {+ final machine = computers?.own+ .where((m) => m.id == now.machineId)+ .firstOrNull+ ?.name;+ unawaited(+ confirmSwarmResume(+ context,+ agent: now,+ machineName: machine,+ shell: shell,+ // Still the person's to write, still the agent asked about, and+ // not stopped by the Swarm meanwhile (its Buddy removed).+ allowed: () => switch (agent()) {+ final a? =>+ allowed() && a.id == now.id && a.workDecision != 'stop',+ null => false,+ },+ until: until,+ ),+ );return;}+ if (block(now) != null) return;shell.onOpenAgent(now.machineId, now.harnessId);},);diff --git a/mobile/lib/swarm/swarm_thread.dart b/mobile/lib/swarm/swarm_thread.dartindex 1c4892f98..cf7a38f36 100644--- a/mobile/lib/swarm/swarm_thread.dart+++ b/mobile/lib/swarm/swarm_thread.dart@@ -2,6 +2,8 @@ import 'dart:async';import 'package:flutter/foundation.dart';+import 'package:harness_mobile/phone/voice_recorder.dart' show VoiceTake;+import 'model.dart';import 'swarm_buddy_sessions.dart';import 'swarm_conversation.dart';@@ -10,6 +12,7 @@ import 'swarm_outgoing_files.dart';import 'swarm_reader.dart';import 'swarm_request.dart';import 'swarm_uploads.dart';+import 'swarm_voice_send.dart';/// Where a thread opens: on one of its messages, or at its first unread/// reply — the newest when all are read. Activity, a link and a tag open a@@ -651,6 +654,64 @@ class SwarmThread extends ChangeNotifier}}+ /// Sends [take] as a voice message in the thread, alone, as the chat's+ /// [SwarmConversation.sendVoice] does: what is typed stays in the box.+ Future<SwarmVoicePosted> sendVoice(VoiceTake take, String lang) async {+ if (uploading || _disposed || !canPost || chat.conversation == null) {+ return (sent: false, notice: null);+ }+ final epoch = _epoch;+ bool current() => !_disposed && epoch == _epoch;+ final clip = SwarmOutgoingFile(swarmVoiceFileName, take.wav);+ error = null;+ final up = await _uploads.upload([clip]);+ if (!current()) return (sent: false, notice: null);+ if (up.failure case final failure?) {+ if (failure is! SuperTabFailure || !_refusedChat(failure)) {+ error = swarmUploadError(failure, 1);+ _tell();+ }+ return (sent: false, notice: null);+ }+ final ids = up.ids;+ if (ids == null) return (sent: false, notice: null);+ final read = await swarmReadVoice(+ reader.request,+ tabId: reader.tabId,+ attachmentId: ids.single,+ lang: lang,+ );+ final voice = read.voice;+ if (!current() || voice == null) return (sent: false, notice: read.refused);+ final operationId = superTabOperationId();+ final post = SwarmPost(+ body: {+ 'operationId': operationId,+ 'command': {+ 'type': 'message.send',+ 'conversationId': chat.id,+ 'text': '',+ 'replyTo': root,+ 'recipientIds': const <String>[],+ 'attachmentIds': ids,+ if (reader.snapshot?.swarmBuddy == true) 'oneBuddy': true,+ },+ },+ messageId: superTabMessageId(+ tabId: reader.tabId,+ accountId: reader.accountId,+ operationId: operationId,+ ),+ files: [clip],+ voice: voice,+ );+ final outcome = await _outbox.add(post);+ _answered(post, outcome, current);+ final sent =+ outcome == SwarmPostOutcome.taken || outcome == SwarmPostOutcome.lost;+ return (sent: sent, notice: sent ? swarmVoiceSentNote(voice) : null);+ }+/// Sends the draft to the thread's root, to the agents it names and tagging/// the people it names, also to the chat with [alsoSend]/// — or, with nothing typed while a reply is [unconfirmed], sends that onediff --git a/mobile/lib/swarm/swarm_thread_page.dart b/mobile/lib/swarm/swarm_thread_page.dartindex 65253c3bc..e7cdbe0da 100644--- a/mobile/lib/swarm/swarm_thread_page.dart+++ b/mobile/lib/swarm/swarm_thread_page.dart@@ -5,6 +5,7 @@ import 'package:lucide_icons_flutter/lucide_icons.dart';import 'package:harness_mobile/phone/tty.dart';import 'package:harness_mobile/phone/tty_controls.dart';+import 'package:harness_mobile/phone/voice_language_store.dart';import 'package:harness_mobile/shared/theme/app_ink.dart';import 'package:harness_mobile/shared/theme/app_space.dart';import 'package:harness_mobile/shared/theme/app_theme.dart';@@ -264,15 +265,10 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {if (_voice == null) {if (SwarmScope.voiceOf(context) case final create?) {_voice = SwarmVoice(- create: create,- dictation: SwarmScope.dictationOf(context),- draft: () => _thread.draft,- write: (text) {- if (_mayReply) _thread.draft = text;- },- post: () {- if (mounted && _mayReply && _thread.canSend) unawaited(_send());- },+ recorder: create,+ post: (take) async => mounted && _mayReply+ ? _thread.sendVoice(take, voiceLanguageStore.value)+ : (sent: false, notice: null),);}}@@ -706,7 +702,7 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {final canSend = t.canSend;final canAttach = !t.uploading && t.files.room > 0;final voice = _voice;- final talking = voice?.face != null;+ final talking = voice?.active ?? false;final snapshot = t.reader.snapshot;final query = t.mentionQuery;final taggable =@@ -765,21 +761,6 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {style: AppType.caption(color: tty.yellow),),),- // Quiet: nothing went wrong, the words stay in the box.- if (_voice?.stopped case final stopped?)- Padding(- padding: const EdgeInsets.fromLTRB(- AppSpace.xs,- 0,- AppSpace.xs,- AppSpace.s,- ),- child: Text(- stopped,- key: const ValueKey('swarm-thread-voice-stopped'),- style: AppType.caption(color: AppPalette.textSecondary),- ),- ),SwarmComposerCard(focus: _focus,chips: t.files.files.isEmpty ? null : SwarmOutgoingChips(box: t),diff --git a/mobile/lib/swarm/swarm_voice.dart b/mobile/lib/swarm/swarm_voice.dartindex 67a0bb8ac..f43117c82 100644--- a/mobile/lib/swarm/swarm_voice.dart+++ b/mobile/lib/swarm/swarm_voice.dart@@ -1,6 +1,7 @@import 'dart:async';import 'dart:math' as math;+import 'package:clock/clock.dart';import 'package:flutter/foundation.dart';import 'package:flutter/material.dart';import 'package:flutter/scheduler.dart';@@ -9,603 +10,278 @@ import 'package:lucide_icons_flutter/lucide_icons.dart';import 'package:harness_mobile/phone/tty.dart';import 'package:harness_mobile/phone/tty_controls.dart';-import 'package:harness_mobile/phone/voice_input_controller.dart';import 'package:harness_mobile/phone/voice_language_store.dart';-import 'package:harness_mobile/phone/voice_mic_face.dart';import 'package:harness_mobile/phone/voice_notice.dart';+import 'package:harness_mobile/phone/voice_recorder.dart';import 'package:harness_mobile/shared/theme/app_icon_size.dart';import 'package:harness_mobile/shared/theme/app_ink.dart';import 'package:harness_mobile/shared/theme/app_space.dart';import 'package:harness_mobile/shared/theme/app_theme.dart';-import 'package:harness_mobile/shared/theme/swarm_tokens_set_b.dart';+import 'package:harness_mobile/shared/theme/app_type.dart';import 'swarm_conversation_page.dart' show SwarmRoundButton;-import 'swarm_dictation.dart';--/// Talking instead of typing in a chat, into the message box (Tony, 10-28:-/// "like the Claude app"). Tap the mic to talk. While talking the composer-/// shows [SwarmVoiceRow]: [stop] ends the take with its words left in the box,-/// [cancel] throws away what the take added, and [send] ends it and posts-/// the box. Voice never sends by itself (Tony, 11-01): sending is a tap on-/// Send, the composer's or the row's. No pause ends a take (10-29:-/// "until I tap") — save [idle] without a new word, which stops listening-/// with the words left in the box. A long press on the mic picks the language-/// ([voiceLanguageStore]).-///-/// Two ways to hear, the first wherever it can:-///-/// - **The phone's own recognizer** ([dictation]), the one the keyboard's-/// dictation uses: the words show in the box as they are said. Asked for-/// once, with the microphone.-/// - **The terminal's voice input** ([VoiceInputController]): the take is-/// recorded and the backend transcribes it as it ends — at [stop], or at-/// [VoiceInputController.maxTake] — its words left in the box, unsent.-/// They reach the box only once transcribed, after the take: no live words.-/// Where there is no recognizer (Android, sample mode), speech recognition-/// was refused, or the recognizer has no such language.+import 'swarm_voice_send.dart' show SwarmVoicePosted;+import 'voice_length.dart';++/// Where a chat's voice message is.+enum SwarmVoiceState {+ /// No take: the composer shows its mic.+ off,++ /// The microphone is asked for, or opening.+ starting,++ /// Recording.+ recording,++ /// Stopped and kept, with Send and X: by a tap, at [SwarmVoice.maxTake], or+ /// as the chat went out of sight.+ held,++ /// Its audio is on its way.+ sending,+}++/// A chat's voice messages, as Telegram's (Dee, 11-04: "just send the full+/// audio"). A tap on the mic records; the composer then shows+/// [SwarmVoiceRow]: X throws the take away, Send posts it as audio+/// ([post]), and its words come back from the backend for agents to read.+/// Nothing goes into the message box: the keyboard's own dictation is there+/// for typing by voice.///-/// ⚠️ **Only while the chat is seen.** A take goes the moment the chat does —-/// covered by another screen ([hide]) or the app sent to the background — so-/// nothing is recorded, or sent to be transcribed, behind the person's back.-/// Behind a system prompt a recorded take is kept, but its time limit ends-/// it only once the app is back; a take that ended just before goes to be-/// transcribed only then, too.+/// A take is never sent by itself. It stops and is kept, Send still there,+/// at [maxTake], and when the chat goes out of sight: covered by another+/// screen ([hide]), the phone locked or the app sent away. A system prompt or+/// Control Center only covers the app, and the take goes on. A long press on+/// the mic picks the language its words are heard in ([voiceLanguageStore]).class SwarmVoice extends ChangeNotifier {- SwarmVoice({- required this.create,- this.dictation,- required this.draft,- required this.write,- required this.post,- });+ SwarmVoice({required this.recorder, required this.post});- /// How long the recognizer listens without hearing a new word before it- /// stops by itself: the microphone is released and the words stay in the- /// box, for Send or the next take (Lead, review of #2056: a microphone open- /// for nobody). A shorter pause never ends it.- static const idle = Duration(minutes: 2);-- /// What the chat says, quietly, as the recognizer stops by itself after- /// [idle], for [stoppedLinger] (Lead, 10-29: a mic that went still with no- /// word said why).- static const stoppedListening = 'Stopped listening';- static const stoppedLinger = Duration(seconds: 3);-- /// What the chat says as a [send] ends with nothing to post (Tony, 11-01).- static const nothingSent = "Couldn't hear anything, nothing sent";-- /// How long the recognizer has to say it has stopped, after [stop] asked it- /// to, before its take is given up with what the box holds left there.- static const _endWait = Duration(seconds: 2);-- /// Makes the voice input, which this owns and disposes.- final VoiceInputController Function() create;-- /// The phone's own recognizer; null where there is none.- final SwarmDictation? dictation;-- /// The draft as it is now, and the draft written anew.- final String Function() draft;- final void Function(String text) write;-- /// Posts the draft through the composer's own Send, once [send]'s take- /// has ended into it.- final VoidCallback post;-- /// The recognizer's take: asking for it, listening, or waiting for it to- /// say it has stopped — as only [stop] asks.- _Live _live = _Live.off;-- /// Bumped by everything that ends a recognizer's take, so what an earlier- /// one still says lands nowhere.- int _liveTake = 0;-- /// Speech recognition was refused: the mic records from now on.- bool _liveRefused = false;-- /// What the recognizer shows is spliced into the draft after [_liveFrom]:- /// the draft as the take began, or as the person last left it. [_liveShown]- /// is the draft as the recognizer last wrote it; a draft that is not, the- /// person changed — their change stays, with [_liveHeard] as it was then in- /// it ([_liveBase]).- String _liveFrom = '', _liveShown = '';-- /// Every word the recognizer has heard in the take, as it last said them;- /// and those of them already in [_liveFrom].- List<String> _liveHeard = const [], _liveBase = const [];- Timer? _liveEnd, _liveIdle, _liveNoticeTimer, _stoppedTimer;- final ValueNotifier<double> _liveLevel = ValueNotifier(0);- String? _liveNotice;-- /// The row's Send under way ([send]): the take ends, then the draft is- /// posted once. Replaced by everything that ends a take otherwise, so a- /// take cancelled or gone meanwhile posts nothing.- Object? _sending;-- VoiceInputController? _voice;- bool _disposed = false;-- /// Hears the app go to the background, from the first tap.- AppLifecycleListener? _lifecycle;+ /// The longest take; it stops there with Send ready.+ static const maxTake = Duration(minutes: 10);- /// The app is in front: not behind a system prompt or Control Center, nor- /// gone. Only then does a take reach its limit and go to be transcribed.- bool _inFront = true;-- /// The take reached [VoiceInputController.maxTake] while the app was not- /// in front: it ends as the app comes back.- bool _limitReached = false;-- /// A take whose microphone closed while the app was not in front, waiting- /// for it to come back ([_mayTranscribe]).- Completer<bool>? _held;-- /// The voice input, from the first tap: a recorder opens a platform- /// channel, and a chat nobody talks to should not.- VoiceInputController? get voice => _voice;-- /// What went wrong with the last take, as the terminal says it.- String? get notice => _liveNotice ?? _voice?.notice;-- /// [stoppedListening] for a moment after the recognizer stopped by itself;- /// null otherwise.- String? get stopped => _stoppedTimer == null ? null : stoppedListening;-- /// The mic's face: null at rest.- VoiceMicFace? get face => switch (_live) {- _Live.asking => VoiceMicFace.starting,- _Live.listening => VoiceMicFace.listening,- _Live.ending => VoiceMicFace.busy,- _Live.off => switch (_voice) {- null => null,- final voice when voice.isSending => VoiceMicFace.busy,- final voice => switch (voice.status) {- VoiceInputStatus.starting => VoiceMicFace.starting,- VoiceInputStatus.listening => VoiceMicFace.listening,- VoiceInputStatus.transcribing => VoiceMicFace.busy,- VoiceInputStatus.idle || VoiceInputStatus.unavailable => null,- },- },- };+ /// What a take that came back empty says.+ static const nothingRecorded = "Couldn't record anything · tap the mic again";- /// The row's Send is under way: the take is ending, then the draft posts.- /// The row takes no tap meanwhile.- bool get sending => _sending != null;-- /// The row's Send can be tapped: listening, and with the phone's- /// recognizer only once the box has words — the words a recording heard- /// reach it only as it ends.- bool get canSend {- if (_disposed || _sending != null) return false;- if (_live != _Live.off) {- return _live == _Live.listening && draft().trim().isNotEmpty;- }- final voice = _voice;- return voice != null &&- !voice.isSending &&- voice.status == VoiceInputStatus.listening;- }+ /// What a send that failed without saying why says; the take is kept.+ static const notSent = 'Not sent · tap Send to try again';- /// The microphone was refused: the mic is off.- bool get off =>- _live == _Live.off && _voice?.status == VoiceInputStatus.unavailable;+ /// Makes the microphone on the first tap, which this owns and disposes: a+ /// recorder opens a platform channel, and a chat nobody talks in should not.+ final VoiceRecorder Function() recorder;- /// How loud the microphone is, for the face's bars.- ValueListenable<double>? get level =>- _live == _Live.off ? _voice?.level : _liveLevel;+ /// Posts a take as a voice message, and answers whether it went and what+ /// to say about it. Not sent, the take is kept for another Send.+ final Future<SwarmVoicePosted> Function(VoiceTake take) post;- /// Starts a take, or calls it off while the microphone opens or its- /// permission is asked. While listening it is [stop]; while the words are- /// on their way it does nothing.- void tap() {- if (_disposed) return;- switch (_live) {- case _Live.asking:- _offLive();- return;- case _Live.listening:- _endLive();- return;- case _Live.ending:- return;- case _Live.off:- }- _setLiveNotice(null);- _stoppedTimer?.cancel();- _stoppedTimer = null;- final recorded = _voice;- final underWay =- recorded != null &&- (recorded.isSending ||- (recorded.status != VoiceInputStatus.idle &&- recorded.status != VoiceInputStatus.unavailable));- if (!underWay && dictation != null && !_liveRefused) {- unawaited(_listen(dictation!));- return;- }- _tapRecorded(recorded ?? _start());- }+ VoiceRecorder? _recorder;+ SwarmVoiceState _state = SwarmVoiceState.off;+ VoiceTake? _take;+ String? _notice;+ bool _refused = false, _disposed = false;- /// [tap] on the terminal's voice input.- void _tapRecorded(VoiceInputController voice) {- if (voice.isSending) return;- switch (voice.status) {- case VoiceInputStatus.idle || VoiceInputStatus.unavailable:- unawaited(voice.startListening());- case VoiceInputStatus.starting:- unawaited(voice.stopListening());- case VoiceInputStatus.listening:- _finish(voice);- case VoiceInputStatus.transcribing:- break;- }- }+ /// Bumped by everything that ends a take, so what an earlier one still+ /// awaits lands nowhere.+ int _turn = 0;+ Timer? _limit;+ Stopwatch? _clock;- /// Ends the take with its words left in the box. Nothing is sent.- void stop() {- if (_disposed) return;- if (_live == _Live.listening) {- _endLive();- return;- }- final voice = _voice;- if (voice != null && voice.status == VoiceInputStatus.listening) {- _finish(voice);- }- }+ /// Stops a take as the app goes out of sight, from the first tap.+ AppLifecycleListener? _lifecycle;- /// Ends the take and posts the draft with its words, once ([post]): the- /// recognizer's last words, waited for no longer than it is at [stop]; or- /// the recording, once transcribed. A take that ends with nothing to post- /// posts nothing and says [nothingSent]; what was typed stays.- void send() {- if (!canSend) return;- final sending = _sending = Object();- if (_live == _Live.listening) {- _endLive();- return;- }- notifyListeners();- unawaited(_sendRecorded(_voice!, sending));- }+ SwarmVoiceState get state => _state;- /// [send] on a recording: what was typed, then the words transcribed.- Future<void> _sendRecorded(VoiceInputController voice, Object sending) async {- var posted = false;- await voice.submit((words) async {- // Gone meanwhile: its words land nowhere.- if (_sending != sending) return true;- write(_joined(draft(), words));- posted = true;- post();- return true;- });- if (_disposed || _sending != sending) return;- _sending = null;- if (!posted) _setLiveNotice(nothingSent);- notifyListeners();- }+ /// What went wrong with the last take or send; null otherwise.+ String? get notice => _notice;++ /// The microphone was refused: the mic is off.+ bool get off => _refused;++ /// The take is under way or kept: the composer shows the voice row.+ bool get active => _state != SwarmVoiceState.off;++ /// Send can be tapped: a take is recording or kept.+ bool get canSend =>+ _state == SwarmVoiceState.recording || _state == SwarmVoiceState.held;++ /// How long the take has run, or the kept take's length.+ Duration get length => _take?.length ?? _clock?.elapsed ?? Duration.zero;- /// Throws the take away: what it added leaves the box, the person's edits- /// stay. A recorded take is dropped with its words, even while they are- /// being transcribed ([VoiceInputController.clear]).- void cancel() {+ /// How loud the microphone is, for the row's wave; silence from a recorder+ /// with no meter.+ ValueListenable<double> get level => switch (_recorder) {+ final VoiceLevelMeter meter => meter.level,+ _ => _silence,+ };+ static final ValueListenable<double> _silence = ValueNotifier(0);++ /// Starts a take; while one records, stops and keeps it; while the+ /// microphone opens, calls it off.+ void tap() {if (_disposed) return;- _sending = null;- if (_live != _Live.off) {- if (_live != _Live.asking && draft() == _liveShown) write(_liveFrom);- _offLive();- return;+ switch (_state) {+ case SwarmVoiceState.off:+ unawaited(_start());+ case SwarmVoiceState.starting:+ discard();+ case SwarmVoiceState.recording:+ unawaited(_stop());+ case SwarmVoiceState.held || SwarmVoiceState.sending:+ break;}- final voice = _voice;- if (voice != null && !voice.isIdle) voice.clear();}- /// Throws away the take under way — opening, recording or being- /// transcribed — with its words: the chat went out of sight. What it was- /// waiting on lands nowhere ([VoiceInputController.clear]), and the next- /// tap starts afresh.- ///- /// ⚠️ `clear`, never `stopListening` or a [_finish]: those transcribe the- /// take, and a take nobody can see must not leave the phone.+ /// Stops the take and keeps it, with Send and X: the chat went out of+ /// sight. A take still opening is called off.void hide() {- _sending = null;- _offLive();- _release(false);- final voice = _voice;- if (voice == null || voice.isIdle) return;- voice.clear();+ if (_state == SwarmVoiceState.starting) discard();+ if (_state == SwarmVoiceState.recording) unawaited(_stop());}- VoiceInputController _start() {- final voice = _voice = create();- voice.addListener(_changed);- voice.onTakeLimit = () async => _limit(voice);- voice.mayTranscribe = _mayTranscribe;- _watchApp();- return voice;+ /// Throws the take away, recorded or kept, and sends nothing.+ void discard() {+ if (_disposed || _state == SwarmVoiceState.sending) return;+ final recording =+ _state == SwarmVoiceState.recording ||+ _state == SwarmVoiceState.starting;+ _end();+ if (recording) unawaited(_recorder?.cancel());+ _notice = null;+ _set(SwarmVoiceState.off);}- /// Hears the app come and go, from the first tap.- void _watchApp() {- if (_lifecycle != null) return;- final state = WidgetsBinding.instance.lifecycleState;- _inFront = state == null || state == AppLifecycleState.resumed;- _lifecycle = AppLifecycleListener(onStateChange: _lifecycleChanged);+ /// Posts the take, stopping it first while it records.+ void send() {+ if (!canSend) return;+ unawaited(_send());}- /// A take with the phone's recognizer: asked for, then listening in the- /// long-press language until the next tap, for as long as the chat is in- /// front. Refused, or without the language, the tap records instead.- Future<void> _listen(SwarmDictation dictation) async {- final take = ++_liveTake;+ Future<void> _start() async {+ final turn = ++_turn;+ _notice = null;+ _set(SwarmVoiceState.starting);_watchApp();- _live = _Live.asking;- notifyListeners();- final access = await dictation.access();- if (_disposed || take != _liveTake) return;- if (access != SwarmDictationAccess.allowed) {- if (access == SwarmDictationAccess.denied) _liveRefused = true;- _live = _Live.off;- _tapRecorded(_voice ?? _start());- notifyListeners();+ final mic = _recorder ??= recorder();+ if (!await mic.allowed()) {+ if (turn != _turn) return;+ _refused = true;+ _notice = VoiceNotice.unavailable;+ _set(SwarmVoiceState.off);return;}- _liveFrom = _liveShown = draft();- _liveHeard = _liveBase = const [];- final started = await dictation.start(- swarmDictationLocale(voiceLanguageStore.value),- onWords: (words) => _liveWords(take, words),- onLevel: (level) {- if (take == _liveTake) _liveLevel.value = level;- },- onEnd: (end) => _liveEnded(take, end),- );- if (_disposed || take != _liveTake) {- if (started) unawaited(dictation.cancel());+ _refused = false;+ if (turn != _turn) return;+ try {+ await mic.start();+ } on Exception {+ if (turn != _turn) return;+ _notice = VoiceNotice.couldNotStart;+ _set(SwarmVoiceState.off);return;}- if (!started) {- _live = _Live.off;- _tapRecorded(_voice ?? _start());- notifyListeners();+ // Called off while the microphone opened.+ if (turn != _turn) {+ unawaited(mic.cancel());return;}- _live = _Live.listening;- _waitForWords(take);- notifyListeners();- }-- /// Starts [idle] over: a take that hears no new word in it is called off.- void _waitForWords(int take) {- _liveIdle?.cancel();- _liveIdle = Timer(idle, () {- if (take != _liveTake || _live != _Live.listening) return;- _stoppedTimer = Timer(stoppedLinger, () {- _stoppedTimer = null;- if (!_disposed) notifyListeners();- });- _offLive();- });- }-- /// Every word the recognizer has heard so far, into the draft after- /// [_liveFrom]: those not already in it, which — after an edit — are the- /// ones past what the recognizer said then and says still, however it has- /// revised it since. A spoken word may show twice; none is lost.- void _liveWords(int take, String text) {- if (take != _liveTake || _live == _Live.off) return;- final words = text.split(RegExp(r'\s+')).where((w) => w.isNotEmpty);- final now = draft();- if (now != _liveShown) {- _liveFrom = now;- _liveBase = _liveHeard;- }- final heard = words.toList();- final changed = heard.join(' ') != _liveHeard.join(' ');- _liveHeard = heard;- final kept = _sameStart(_liveBase, heard);- _liveShown = _joined(_liveFrom, heard.skip(kept).join(' '));- if (_liveShown != now) write(_liveShown);- if (changed && _live == _Live.listening) _waitForWords(take);- }-- /// How many words [a] and [b] begin with alike, case and punctuation aside.- static int _sameStart(List<String> a, List<String> b) {- String plain(String word) => word.toLowerCase().replaceAll(- RegExp(r'[^\p{L}\p{N}]', unicode: true),- '',- );- var same = 0;- while (same < a.length &&- same < b.length &&- plain(a[same]) == plain(b[same])) {- same++;+ _take = null;+ _clock = clock.stopwatch()..start();+ _limit = Timer(maxTake, () => unawaited(_stop()));+ _set(SwarmVoiceState.recording);+ }++ /// Ends the recording and keeps it; true once there is a take to send.+ Future<bool> _stop() async {+ if (_state != SwarmVoiceState.recording) return _take != null;+ final turn = _turn;+ _limit?.cancel();+ _clock?.stop();+ // Held while the recorder finishes: no second stop, X or Send meanwhile.+ _set(SwarmVoiceState.held);+ final take = await _recorder!.stop();+ if (turn != _turn || _disposed) return false;+ if (take == null) {+ _end();+ _notice = nothingRecorded;+ _set(SwarmVoiceState.off);+ return false;}- return same;- }-- /// [stop]: asks the recognizer to stop — its last words, then its end- /// ([_liveEnded]), with the words left in the box.- void _endLive() {- final dictation = this.dictation;- if (_live != _Live.listening || dictation == null) return;- final take = _liveTake;- _live = _Live.ending;- _stopLiveTimers();- _liveEnd = Timer(_endWait, () {- if (take != _liveTake) return;- unawaited(dictation.cancel());- _liveEnded(take, SwarmDictationEnd.failed);- });- notifyListeners();- unawaited(dictation.stop());- }-- /// The recognizer has stopped, as [stop] or [send] asked or because it- /// cannot go on: its words stay in the box, and only [send]'s are posted.- /// No notice after a stop with words in the box, however the recognizer's- /// last session ended.- void _liveEnded(int take, SwarmDictationEnd end) {- if (take != _liveTake || _live == _Live.off) return;- final stopped = _live == _Live.ending && draft().trim().isNotEmpty;- final send = _sending != null && _live == _Live.ending;- _sending = null;- _liveTake++;- _live = _Live.off;- _stopLiveTimers();- _liveLevel.value = 0;- if (send && stopped) post();- _setLiveNotice(switch (end) {- _ when send && !stopped => nothingSent,- _ when stopped => null,- SwarmDictationEnd.failed => VoiceNotice.notTranscribed,- _ when _liveHeard.isEmpty => VoiceNotice.nothingHeard,- _ => null,- });+ _take = take;notifyListeners();+ return true;}- /// Calls off the recognizer's take, whatever it is doing: the words already- /// shown stay in the draft, and nothing is sent.- void _offLive() {- if (_live == _Live.off) return;- _sending = null;- final listened = _live != _Live.asking;- _liveTake++;- _live = _Live.off;- _stopLiveTimers();- _liveLevel.value = 0;- if (listened) unawaited(dictation?.cancel());- if (!_disposed) notifyListeners();- }-- void _stopLiveTimers() {- _liveEnd?.cancel();- _liveIdle?.cancel();- _liveEnd = _liveIdle = null;- }-- /// A notice of the recognizer's, which goes by itself as the terminal's do.- void _setLiveNotice(String? notice) {- _liveNoticeTimer?.cancel();- _liveNoticeTimer = null;- _liveNotice = notice;- if (notice == null) return;- _liveNoticeTimer = Timer(VoiceInputController.noticeLinger, () {- _liveNotice = null;- if (!_disposed) notifyListeners();- });- }-- /// [words] after [draft], a space between them unless the draft ends in one.- static String _joined(String draft, String words) {- if (words.isEmpty) return draft;- final spaced = draft.isEmpty || RegExp(r'\s$').hasMatch(draft);- return '$draft${spaced ? '' : ' '}$words';- }-- void _lifecycleChanged(AppLifecycleState state) {- switch (state) {- case AppLifecycleState.resumed:- _inFront = true;- _release(true);- _back();- // ⚠️ Kept, not thrown away: the microphone permission prompt itself- // leaves the app inactive, and so do Control Center and a call's banner,- // and the take asked for must survive them. Only its ending waits.- //- // The recognizer's take is not, once it listens: it has no prompt left- // to survive, and what it hears goes to be recognized at once.- case AppLifecycleState.inactive:- _inFront = false;- if (_live != _Live.asking) _offLive();- case AppLifecycleState.hidden ||- AppLifecycleState.paused ||- AppLifecycleState.detached:- _inFront = false;- hide();- }- }-- /// The app is back in front: a take that reached its limit meanwhile ends- /// now.- void _back() {- final voice = _voice;- if (voice == null || voice.status != VoiceInputStatus.listening) return;- if (!_limitReached) return;- _limitReached = false;- _finish(voice);- }-- /// The take reached [VoiceInputController.maxTake]: it ends into the box,- /// as a tap would — once the app is in front, never behind a prompt.- void _limit(VoiceInputController voice) {- if (_inFront) {- _finish(voice);- } else {- _limitReached = true;+ Future<void> _send() async {+ if (!await _stop()) return;+ final take = _take!, turn = _turn;+ _notice = null;+ _set(SwarmVoiceState.sending);+ SwarmVoicePosted posted;+ try {+ posted = await post(take);+ } on Object {+ // Whatever failed, the take stays with Send and X: a row left on+ // sending would hold it out of reach.+ posted = (sent: false, notice: notSent);}- }-- /// Whether a take may go to be transcribed, asked once its microphone has- /// closed: at once in front; behind a system prompt, once the app is back —- /// and never if it goes away instead ([hide]).- ///- /// ⚠️ Asked again here, not only as the take ends: the microphone takes a- /// moment to close, and Control Center can come down in it.- Future<bool> _mayTranscribe() async {- while (!_inFront) {- if (!await (_held ??= Completer()).future) return false;+ if (turn != _turn || _disposed) return;+ final (:sent, :notice) = posted;+ _notice = notice;+ if (!sent) {+ _set(SwarmVoiceState.held);+ return;}- return true;+ _end();+ _set(SwarmVoiceState.off);}- /// Lets a take held by [_mayTranscribe] go to be transcribed, or drops it.- void _release(bool go) {- final held = _held;- _held = null;- held?.complete(go);+ void _end() {+ _turn++;+ _limit?.cancel();+ _limit = null;+ _clock = null;+ _take = null;}- /// Ends the take, its words into the draft. A take that fails keeps the- /// draft as it was and leaves its [notice]. Nothing is sent.- void _finish(VoiceInputController voice) {- unawaited(- voice.submit((words) async {- write(_joined(draft(), words));- return true;- }),- );- }+ void _watchApp() => _lifecycle ??= AppLifecycleListener(+ onStateChange: (state) {+ if (state == AppLifecycleState.hidden ||+ state == AppLifecycleState.paused ||+ state == AppLifecycleState.detached) {+ hide();+ }+ },+ );- void _changed() {- if (_voice?.status != VoiceInputStatus.listening) _limitReached = false;+ void _set(SwarmVoiceState state) {+ if (_disposed) return;+ _state = state;notifyListeners();}- /// Throws away a take still under way: the microphone closes with the chat.@overridevoid dispose() {_disposed = true;- _offLive();- _liveNoticeTimer?.cancel();- _stoppedTimer?.cancel();- _liveLevel.dispose();- _release(false);+ final recording =+ _state == SwarmVoiceState.recording ||+ _state == SwarmVoiceState.starting;+ _end();_lifecycle?.dispose();- if (_voice case final voice?) {- voice.removeListener(_changed);- voice.dispose();+ final mic = _recorder;+ if (mic != null) {+ unawaited(() async {+ if (recording) await mic.cancel();+ await mic.dispose();+ }());}super.dispose();}}/// The composer's mic: a 36pt circle in a 44pt target, like the card's other-/// round buttons. A tap starts a take; a long press picks the language, from-/// the terminal's list. While a take is under way the composer shows-/// [SwarmVoiceRow] in its place, so the mic draws no live face of its own.+/// round buttons. A tap starts a voice message; a long press picks the+/// language its words are heard in, from the terminal's list. While a take is+/// under way the composer shows [SwarmVoiceRow] in its place.class SwarmVoiceMic extends StatelessWidget {const SwarmVoiceMic({super.key, required this.voice});@@ -628,7 +304,7 @@ class SwarmVoiceMic extends StatelessWidget {enabled: true,onTap: talk,onLongPress: chooseLanguage,- label: off ? 'Voice input is off' : 'Talk',+ label: off ? 'Voice input is off' : 'Record',onLongPressHint: 'Choose language',excludeSemantics: true,child: GestureDetector(@@ -649,17 +325,14 @@ class SwarmVoiceMic extends StatelessWidget {}}-/// The card's toolbar while a take is under way (Tony, 11-01): cancel, the-/// wave, stop and Send — no +, Tag or mic.+/// The card's toolbar while a voice message is under way (Dee, 11-04):///-/// - Opening ([VoiceMicFace.starting]): cancel, a flat wave and Send off.-/// - Listening: cancel, the wave following [SwarmVoice.level], stop, and-/// Send ([SwarmVoice.send]) — with the phone's recognizer only once the box-/// has words; a recording's reach it only as it ends.-/// - Busy after stop (the recognizer stopping, or the recording being-/// transcribed): cancel, the terminal's busy face where stop was, Send off.-/// - Sending ([SwarmVoice.sending]): Send's spinner, and nothing else takes-/// a tap until the post has gone.+/// ( X ) ● 0:14 ıllıllıııllıı.ılı ( ↑ )+///+/// X throws the take away; the red dot and the time run while it records,+/// and the wave follows [SwarmVoice.level]. A kept take shows its length with+/// the dot and the wave at rest. Send posts it, with a spinner until it has+/// gone, and nothing else takes a tap meanwhile.////// Every label is one word: VoiceOver reads a label aloud, and a sentence/// read into an open microphone lands in the take.@@ -668,23 +341,23 @@ class SwarmVoiceRow extends StatelessWidget {final SwarmVoice voice;- /// The composer could post now, words aside: New message has a chat- /// picked and no post on its way.+ /// The composer could post now: New message has a chat picked and no post+ /// on its way.final bool canPost;@overrideWidget build(BuildContext context) {final tty = Tty.of(context);- final face = voice.face;final fill = tty.text.withValues(alpha: 0.08);- final sending = voice.sending;+ final sending = voice.state == SwarmVoiceState.sending;+ final recording = voice.state == SwarmVoiceState.recording;final canSend = canPost && voice.canSend;return Row(children: [_VoiceButton(key: const ValueKey('swarm-voice-cancel'),- label: 'Cancel',- onTap: sending ? null : voice.cancel,+ label: 'Discard',+ onTap: sending ? null : voice.discard,child: _VoiceRound(fill: fill,child: Icon(@@ -694,51 +367,27 @@ class SwarmVoiceRow extends StatelessWidget {),),),- const SizedBox(width: AppSpace.xs),+ const SizedBox(width: AppSpace.s),+ Container(+ key: const ValueKey('swarm-voice-dot'),+ width: AppSpace.s,+ height: AppSpace.s,+ decoration: BoxDecoration(+ color: recording ? tty.red : AppPalette.textFaint,+ shape: BoxShape.circle,+ ),+ ),+ const SizedBox(width: AppSpace.s),+ _TakeClock(voice: voice),+ const SizedBox(width: AppSpace.s),Expanded(child: SwarmVoiceWave(key: const ValueKey('swarm-voice-wave'),- level: face == VoiceMicFace.listening ? voice.level : null,- flat: face != VoiceMicFace.listening,+ level: recording ? voice.level : null,+ flat: !recording,),),const SizedBox(width: AppSpace.xs),- switch (face) {- _ when sending => const SizedBox(width: 44),- VoiceMicFace.listening => _VoiceButton(- key: const ValueKey('swarm-voice-stop'),- label: 'Stop',- onTap: voice.stop,- child: _VoiceRound(- fill: fill,- child: Container(- width: 14,- height: 14,- decoration: BoxDecoration(- color: AppPalette.textPrimary,- borderRadius: BorderRadius.circular(SwarmComposer.stopRadius),- ),- ),- ),- ),- VoiceMicFace.starting || null => const SizedBox(width: 44),- final face => Semantics(- key: const ValueKey('swarm-voice-busy'),- label: 'Working',- excludeSemantics: true,- child: SizedBox.square(- dimension: 44,- child: Center(- child: SizedBox.square(- dimension: 36,- child: FittedBox(- child: VoiceMicCore(face: face, dead: false),- ),- ),- ),- ),- ),- },SwarmRoundButton(key: const ValueKey('swarm-voice-send'),label: 'Send',@@ -770,6 +419,46 @@ class SwarmVoiceRow extends StatelessWidget {}}+/// The take's time beside the red dot: it runs, a second at a time, only+/// while the take records.+class _TakeClock extends StatefulWidget {+ const _TakeClock({required this.voice});++ final SwarmVoice voice;++ @override+ State<_TakeClock> createState() => _TakeClockState();+}++class _TakeClockState extends State<_TakeClock> {+ Timer? _tick;++ @override+ Widget build(BuildContext context) {+ final recording = widget.voice.state == SwarmVoiceState.recording;+ if (recording && _tick == null) {+ _tick = Timer.periodic(const Duration(seconds: 1), (_) {+ if (mounted) setState(() {});+ });+ } else if (!recording) {+ _tick?.cancel();+ _tick = null;+ }+ return Text(+ swarmVoiceLength(widget.voice.length),+ key: const ValueKey('swarm-voice-time'),+ style: AppType.caption(color: AppPalette.textPrimary)+ .copyWith(fontFeatures: const [FontFeature.tabularFigures()]),+ );+ }++ @override+ void dispose() {+ _tick?.cancel();+ super.dispose();+ }+}+/// A 44pt target with [child] on a 36pt circle of [fill].class _VoiceRound extends StatelessWidget {const _VoiceRound({required this.fill, required this.child});@@ -999,4 +688,3 @@ class _WavePainter extends CustomPainter {}/// Where [SwarmVoice]'s take with the phone's recognizer is.-enum _Live { off, asking, listening, ending }diff --git a/mobile/lib/swarm/swarm_voice_bubble.dart b/mobile/lib/swarm/swarm_voice_bubble.dartnew file mode 100644index 000000000..9853153bc--- /dev/null+++ b/mobile/lib/swarm/swarm_voice_bubble.dart@@ -0,0 +1,388 @@+import 'dart:async';+import 'dart:io';+import 'dart:math' as math;++import 'package:flutter/material.dart';+import 'package:flutter/services.dart';+import 'package:lucide_icons_flutter/lucide_icons.dart';+import 'package:video_player/video_player.dart';++import 'package:harness_mobile/phone/tty.dart';+import 'package:harness_mobile/shared/theme/app_icon_size.dart';+import 'package:harness_mobile/shared/theme/app_ink.dart';+import 'package:harness_mobile/shared/theme/app_space.dart';+import 'package:harness_mobile/shared/theme/app_theme.dart';+import 'package:harness_mobile/shared/theme/app_type.dart';++import 'model.dart';+import 'swarm_attachment_store.dart';+import 'swarm_file_links.dart';+import 'voice_length.dart';++/// What →A says for a voice message with no words.+const swarmVoiceNoWords = 'Not transcribed';+const swarmVoiceNoWordsLimit = "Not transcribed: today's voice limit was used";++/// A copy of [ref]'s file on the phone, kept in the account's file cache+/// (swarm_attachment_store.dart); null when none could be had. Replaced by+/// tests.+Future<File?> Function(BuildContext context, SwarmFileRef ref) swarmVoiceFile =+ _cachedCopy;++/// The player of a voice message's local copy. Replaced by tests.+VideoPlayerController Function(File file) swarmVoicePlayer =+ VideoPlayerController.file;++/// From the copy on the phone, never from the signed link: that link lasts+/// minutes, and a long clip played, paused and played again outlives it.+Future<File?> _cachedCopy(BuildContext context, SwarmFileRef ref) async {+ for (final stale in [false, true]) {+ if (!context.mounted) return null;+ final link = await swarmOpenableLink(context, ref, stale: stale);+ if (link == null) return null;+ try {+ return await swarmAttachments.file(link, key: 'voice:${ref.file.id}');+ } on Object {+ // The link may have run out in between: once more with a fresh one.+ }+ }+ return null;+}++/// A voice message (task #2584), on the phone as on Desktop:+///+/// ( ▶ ) ıllıllııılllıı.ılıı.ıll 1:21 →A+///+/// Play hears it, and the bars it has played fill in; the time counts what+/// was played, its length at rest. →A shows its [words] under it, the+/// message's text, and a second tap hides them. With no [fileRef] — a post+/// still on its way — nothing plays yet.+class SwarmVoiceBubble extends StatefulWidget {+ const SwarmVoiceBubble({+ super.key,+ required this.file,+ required this.voice,+ required this.words,+ this.fileRef,+ });++ final SuperTabAttachment file;+ final SuperTabVoice voice;+ final String words;+ final SwarmFileRef? fileRef;++ @override+ State<SwarmVoiceBubble> createState() => _SwarmVoiceBubbleState();+}++class _SwarmVoiceBubbleState extends State<SwarmVoiceBubble> {+ VideoPlayerController? _player;+ bool _opening = false, _words = false;+ String? _problem;++ bool get _playing => _player?.value.isPlaying ?? false;++ Future<void> _toggle() async {+ HapticFeedback.lightImpact();+ final player = _player;+ if (player != null) {+ if (player.value.isPlaying) {+ await player.pause();+ } else {+ if (player.value.isCompleted) await player.seekTo(Duration.zero);+ await player.play();+ }+ return;+ }+ final ref = widget.fileRef;+ if (ref == null || _opening) return;+ setState(() {+ _opening = true;+ _problem = null;+ });+ final copy = await swarmVoiceFile(context, ref);+ if (!mounted) return;+ if (copy == null) {+ setState(() {+ _opening = false;+ _problem = "Couldn't load it · tap to try again";+ });+ return;+ }+ final made = swarmVoicePlayer(copy);+ try {+ await made.initialize();+ if (!mounted) {+ unawaited(made.dispose());+ return;+ }+ made.addListener(_changed);+ _player = made;+ await made.play();+ } on Object {+ unawaited(made.dispose());+ if (mounted) _problem = "Couldn't play it · tap to try again";+ }+ if (mounted) setState(() => _opening = false);+ }++ void _changed() {+ if (mounted) setState(() {});+ }++ @override+ void dispose() {+ final player = _player;+ if (player != null) {+ player.removeListener(_changed);+ unawaited(player.dispose());+ }+ super.dispose();+ }++ @override+ Widget build(BuildContext context) {+ final tty = Tty.of(context);+ final value = _player?.value;+ final length = widget.voice.length;+ final position = value?.position ?? Duration.zero;+ final played = length.inMilliseconds == 0 || value == null+ ? 0.0+ : (position.inMilliseconds / length.inMilliseconds).clamp(0.0, 1.0);+ final playing = _playing;+ final shown = value != null && (playing || position > Duration.zero)+ ? position+ : length;+ final words = widget.words.trim();+ return Padding(+ key: ValueKey('swarm-voice-bubble-${widget.file.id}'),+ padding: const EdgeInsets.only(top: AppSpace.s),+ child: Column(+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ Container(+ padding: const EdgeInsets.fromLTRB(+ AppSpace.xs,+ AppSpace.xs,+ AppSpace.s,+ AppSpace.xs,+ ),+ decoration: BoxDecoration(+ color: tty.text.withValues(alpha: 0.08),+ borderRadius: BorderRadius.circular(AppSpace.xl),+ ),+ child: Row(+ mainAxisSize: MainAxisSize.min,+ children: [+ Semantics(+ button: true,+ label: playing ? 'Pause' : 'Play',+ excludeSemantics: true,+ child: GestureDetector(+ key: ValueKey(+ playing ? 'swarm-voice-pause' : 'swarm-voice-play',+ ),+ behavior: HitTestBehavior.opaque,+ onTap: () => unawaited(_toggle()),+ child: SizedBox.square(+ dimension: 44,+ child: Center(+ child: SizedBox.square(+ dimension: 36,+ child: DecoratedBox(+ decoration: BoxDecoration(+ color: widget.fileRef == null+ ? AppPalette.textFaint+ : AppPalette.accent,+ shape: BoxShape.circle,+ ),+ child: Center(+ child: _opening+ ? SizedBox.square(+ dimension: AppIconSize.besideText - 4,+ child: CircularProgressIndicator(+ strokeWidth: 2,+ color: AppInk.onFill,+ ),+ )+ : Icon(+ playing+ ? LucideIcons.pause+ : LucideIcons.play,+ size: AppIconSize.besideText,+ color: AppInk.onFill,+ ),+ ),+ ),+ ),+ ),+ ),+ ),+ ),+ const SizedBox(width: AppSpace.s),+ // As wide as the column leaves it: a phone's narrow message+ // column draws fewer bars.+ Flexible(+ child: SwarmVoiceBars(+ key: const ValueKey('swarm-voice-bars'),+ waveform: widget.voice.waveform,+ played: played,+ playedColor: AppPalette.accent,+ restColor: AppPalette.textFaint,+ ),+ ),+ const SizedBox(width: AppSpace.s),+ Text(+ swarmVoiceLength(shown),+ style: AppType.caption(color: AppPalette.textSecondary)+ .copyWith(+ fontFeatures: const [FontFeature.tabularFigures()],+ ),+ ),+ const SizedBox(width: AppSpace.s),+ Semantics(+ button: true,+ label: _words ? 'Hide words' : 'Show words',+ excludeSemantics: true,+ child: GestureDetector(+ key: const ValueKey('swarm-voice-words-toggle'),+ behavior: HitTestBehavior.opaque,+ onTap: () => setState(() => _words = !_words),+ child: SizedBox(+ height: 44,+ child: Center(+ child: Text(+ '→A',+ style: AppType.label(+ color: _words+ ? AppPalette.accent+ : AppPalette.textSecondary,+ ),+ ),+ ),+ ),+ ),+ ),+ ],+ ),+ ),+ if (_words)+ Padding(+ padding: const EdgeInsets.only(+ top: AppSpace.s,+ left: AppSpace.xs,+ ),+ child: Text(+ words.isNotEmpty+ ? words+ : widget.voice.overLimit+ ? swarmVoiceNoWordsLimit+ : swarmVoiceNoWords,+ style: words.isNotEmpty+ ? AppType.body(color: AppPalette.textPrimary)+ : AppType.caption(color: AppPalette.textSecondary),+ ),+ ),+ if (_problem case final problem?)+ Padding(+ padding: const EdgeInsets.only(+ top: AppSpace.xs,+ left: AppSpace.xs,+ ),+ child: Text(problem, style: AppType.caption(color: tty.yellow)),+ ),+ ],+ ),+ );+ }+}++/// A voice message's waveform: one bar for each of its [waveform] values,+/// or, where that is wider than the room it has, as many as fit, each the+/// loudest of the values it stands for. The [played] part is in+/// [playedColor], the rest in [restColor].+class SwarmVoiceBars extends StatelessWidget {+ const SwarmVoiceBars({+ super.key,+ required this.waveform,+ required this.played,+ required this.playedColor,+ required this.restColor,+ });++ final List<int> waveform;++ /// How much of it was played, 0 to 1.+ final double played;+ final Color playedColor, restColor;++ static const double bar = 2, gap = 2, low = 3, high = 24;++ @override+ Widget build(BuildContext context) => LayoutBuilder(+ builder: (context, room) {+ final fit = ((room.maxWidth + gap) / (bar + gap)).floor();+ final bars = _fitted(math.max(1, math.min(waveform.length, fit)));+ return SizedBox(+ width: bars.length * (bar + gap) - gap,+ height: high,+ child: CustomPaint(+ painter: _BarsPainter(bars, played, playedColor, restColor),+ ),+ );+ },+ );++ List<int> _fitted(int count) {+ if (count >= waveform.length) return waveform;+ return [+ for (var i = 0; i < count; i++)+ waveform+ .sublist(+ i * waveform.length ~/ count,+ math.max(+ i * waveform.length ~/ count + 1,+ (i + 1) * waveform.length ~/ count,+ ),+ )+ .reduce(math.max),+ ];+ }+}++class _BarsPainter extends CustomPainter {+ _BarsPainter(this.waveform, this.played, this.playedColor, this.restColor);++ final List<int> waveform;+ final double played;+ final Color playedColor, restColor;++ @override+ void paint(Canvas canvas, Size size) {+ const bar = SwarmVoiceBars.bar, step = bar + SwarmVoiceBars.gap;+ final paint = Paint()..strokeCap = StrokeCap.round;+ for (var i = 0; i < waveform.length; i++) {+ final level = waveform[i] / SuperTabVoice.barMax;+ final height =+ SwarmVoiceBars.low ++ (SwarmVoiceBars.high - SwarmVoiceBars.low) * level;+ paint.color = (i + 0.5) / waveform.length <= played+ ? playedColor+ : restColor;+ canvas.drawRRect(+ RRect.fromRectAndRadius(+ Rect.fromLTWH(i * step, (size.height - height) / 2, bar, height),+ const Radius.circular(bar / 2),+ ),+ paint,+ );+ }+ }++ @override+ bool shouldRepaint(_BarsPainter old) =>+ old.played != played ||+ old.waveform != waveform ||+ old.playedColor != playedColor ||+ old.restColor != restColor;+}diff --git a/mobile/lib/swarm/swarm_voice_send.dart b/mobile/lib/swarm/swarm_voice_send.dartnew file mode 100644index 000000000..a07022bdc--- /dev/null+++ b/mobile/lib/swarm/swarm_voice_send.dart@@ -0,0 +1,76 @@+import 'package:flutter/foundation.dart';++import 'model.dart';+import 'swarm_request.dart';++/// The name a voice message's clip is uploaded under: what an older app shows+/// for it, as a file.+const swarmVoiceFileName = 'Voice message.wav';++/// What the chat says when a voice message went without its words.+const swarmVoiceOverLimit = "Sent without words: today's voice limit is used";+const swarmVoiceNotTranscribed = "Sent without words: they couldn't be heard";++/// What the chat says when the read of a clip got no answer, from the+/// backend or the computer it goes through; the take is kept.+const swarmVoiceNoAnswer = "Not sent · couldn't reach the server";++/// What the chat says to a server from before voice messages; the take is+/// kept.+const swarmVoiceNeedsServer = 'Voice messages need a newer server';++/// How long to wait before asking again while the backend still transcribes+/// a clip another try started.+@visibleForTesting+Duration swarmVoiceRetry = const Duration(seconds: 1);++/// How many times to ask before giving up on a clip still being transcribed.+const _voiceAsks = 30;++/// Whether a take went, and what to say about it either way.+typedef SwarmVoicePosted = ({bool sent, String? notice});++/// Has the backend read an uploaded clip as a voice message, once, in the+/// language its words are heard in (backend store.ts readVoice): its length,+/// waveform and whether it has words. Its words stay on the backend, which+/// makes them the message's text. Answers the refusal to show when it could+/// not.+Future<({SuperTabVoice? voice, String? refused})> swarmReadVoice(+ SwarmRequest request, {+ required String tabId,+ required String attachmentId,+ required String lang,+}) async {+ final path =+ '/api/super-tabs/$tabId/attachments/$attachmentId/voice'+ '?lang=${Uri.encodeQueryComponent(lang)}';+ for (var ask = 1; ; ask++) {+ try {+ final answer = superTabMap(await request('POST', path, body: const {}));+ final voice = SuperTabVoice.read(answer['voice']);+ return voice == null+ ? (voice: null, refused: swarmVoiceNeedsServer)+ : (voice: voice, refused: null);+ } on SuperTabFailure catch (failure) {+ if (failure.code == 'VOICE_TRANSCRIBING' && ask < _voiceAsks) {+ await Future<void>.delayed(swarmVoiceRetry);+ continue;+ }+ if (failure.statusCode == 404) {+ return (voice: null, refused: swarmVoiceNeedsServer);+ }+ return (voice: null, refused: failure.message);+ } on Object {+ // A timeout or a lost connection, forwarding through a computer too:+ // nothing was sent, and the take waits for another Send.+ return (voice: null, refused: swarmVoiceNoAnswer);+ }+ }+}++/// What a sent voice message says, when it went without words.+String? swarmVoiceSentNote(SuperTabVoice voice) => voice.transcribed+ ? null+ : voice.overLimit+ ? swarmVoiceOverLimit+ : swarmVoiceNotTranscribed;diff --git a/mobile/lib/swarm/swarm_you_page.dart b/mobile/lib/swarm/swarm_you_page.dartindex ef8b67ae1..5e6605013 100644--- a/mobile/lib/swarm/swarm_you_page.dart+++ b/mobile/lib/swarm/swarm_you_page.dart@@ -126,8 +126,9 @@ class SwarmYouButton extends StatelessWidget {/// You, as Tony's prototype C4 draws it (2026-10-30), opened from the avatar/// on Home and Activity since it stopped being a tab (2026-11-03):/// who they are in the Swarm Home shows — their face, their name and that-/// Swarm — then their status, their name and their username, each with Change for their-/// profile, where it is set ([showSwarmProfile]); Your models, with how many of their own machines'+/// Swarm — then their name with their status under it in one row ([_Profile]),+/// and their username, each with Change for their profile, where it is set+/// ([showSwarmProfile]); Your models, with how many of their own machines'/// models the Swarms can use now, and Your devices, with how many of their/// own computers are online; and Settings. Only what is theirs counts: not a/// teammate's, not one shared with them, not a Harness Team account's (#2253's@@ -213,30 +214,16 @@ class SwarmYouPage extends StatelessWidget {me: me,swarm: me == null ? null : you!.name,),- const SwarmCaption('Status'),+ const SwarmCaption('Profile'),SwarmGroup(children: [- _Status(- key: const ValueKey('swarm-you-status'),+ _Profile(+ key: const ValueKey('swarm-you-profile'),me: me,onTap: profile,),],),- const SwarmCaption('Name'),- SwarmGroup(- children: [- SwarmRow(- key: const ValueKey('swarm-you-name'),- leading: const SwarmRowGlyph(LucideIcons.user300),- // How the Swarm shows them, the username until they set one;- // nothing of a Swarm before it is read, or once it is taken away.- title: me == null ? 'Name' : me.name ?? 'Add your name',- trailing: profile == null ? null : const _Change(),- onTap: profile,- ),- ],- ),const SwarmCaption('Username'),SwarmGroup(children: [@@ -395,10 +382,13 @@ class _Me extends StatelessWidget {}}-/// The person's status, Active while none is set, and Change for their-/// profile; "Set a status" and no way in before they are read.-class _Status extends StatelessWidget {- const _Status({super.key, required this.me, required this.onTap});+/// The person's name and status in one row, both set in their profile+/// (Tony 2026-11-04: a Status row and a Name row opened the same sheet). The+/// name the Swarm shows them by, "Add your name" until they set one; under it+/// their status after a dot, Active while none is set; Change for their+/// profile. "Name" and "Set a status", with no way in, before they are read.+class _Profile extends StatelessWidget {+ const _Profile({super.key, required this.me, required this.onTap});final SuperTabPerson? me;final VoidCallback? onTap;@@ -407,21 +397,25 @@ class _Status extends StatelessWidget {Widget build(BuildContext context) {AppTheme.watch(context);final me = this.me;- Widget row(String title, {required bool known}) => SwarmRow(- leading: SizedBox(- width: SwarmRowGlyph.width,- child: Center(- child: Container(- width: 10,- height: 10,+ Widget row(String status, {required bool known}) => SwarmRow(+ leading: const SwarmRowGlyph(LucideIcons.user300),+ // Nothing of a Swarm before it is read, or once it is taken away.+ title: me == null ? 'Name' : me.name ?? 'Add your name',+ detail: Row(+ key: const ValueKey('swarm-you-status'),+ children: [+ Container(+ width: 8,+ height: 8,decoration: BoxDecoration(shape: BoxShape.circle,color: known ? AppColors.success : AppPalette.textFaint,),),- ),+ const SizedBox(width: AppSpace.s),+ Flexible(child: SwarmRow.line(status)),+ ],),- title: title,trailing: onTap == null ? null : const _Change(),onTap: onTap,);diff --git a/mobile/lib/swarm/voice_length.dart b/mobile/lib/swarm/voice_length.dartnew file mode 100644index 000000000..e8cfd51c0--- /dev/null+++ b/mobile/lib/swarm/voice_length.dart@@ -0,0 +1,21 @@+import 'model.dart';++/// A voice message's length as its bubble and its label write it: `M:SS`,+/// rounded down to the second (the backend's `voiceLength`).+String swarmVoiceLength(Duration length) {+ final seconds = length.inSeconds < 0 ? 0 : length.inSeconds;+ return '${seconds ~/ 60}:${(seconds % 60).toString().padLeft(2, '0')}';+}++/// What a quote or a preview says for a voice message, in place of its words.+String swarmVoiceLabel(Duration length) =>+ 'Voice message (${swarmVoiceLength(length)})';++/// [message] as a quote shows it: a voice message by its length, any other+/// by its text.+String swarmQuoteText(SuperTabMessage message) {+ for (final file in message.attachments) {+ if (file.voice case final voice?) return swarmVoiceLabel(voice.length);+ }+ return message.text;+}diff --git a/mobile/lib/terminal/terminal_look.dart b/mobile/lib/terminal/terminal_look.dartnew file mode 100644index 000000000..7e2c7d892--- /dev/null+++ b/mobile/lib/terminal/terminal_look.dart@@ -0,0 +1,114 @@+import 'package:flutter/material.dart';+import 'package:flutter/services.dart';++import '../shared/theme/app_theme.dart';+import '../shared/theme/color_palette.dart';+import '../shared/theme/system_bars.dart';++/// A terminal's page. Everything under it wears [AppTheme.terminalPalette] instead of the app's+/// palette: while Settings ▸ Appearance ▸ Theme is chosen that is the dark side, so Light — or+/// System on a light phone — turns every screen light except the terminal, which stays dark.+/// Left to App colors, the two palettes are one and this changes nothing.+///+/// Three things carry the palette down, because the app's colours reach a widget three ways:+///+/// - **[Tty]** (the terminal-drawn chrome: header, key bar, input) and the terminal's own screen+/// resolve their theme from [paletteOf].+/// - **Material's theme** — a [Theme] built on the terminal's palette, so a button, a spinner or a+/// tooltip that takes its colours from `Theme.of` matches the page it sits on.+/// - **The design tokens** (`AppPalette.windowBg` and the rest) are globals, read with no context,+/// so they cannot follow a subtree on their own: each one read on a terminal's page goes through+/// [read], which resolves it on the terminal's palette.+///+/// It also sets the status bar for the page's own ground: light icons over a dark terminal in a+/// light app.+///+/// ⚠️ **The same tree whether or not the palettes differ.** A Theme switched while a terminal is+/// open (Settings pushed over it) must repaint it, never remount it — remounting a terminal drops+/// its view, its scroll position and its input connection.+class TerminalLook extends StatelessWidget {+ const TerminalLook({super.key, required this.child});++ final Widget child;++ /// Whether [context] stands on a terminal's page. Read without a dependency: whether a widget is+ /// under one does not change while it is mounted, and the palettes it reads come with+ /// [AppTheme.watch].+ static bool on(BuildContext context) =>+ context.getInheritedWidgetOfExactType<_TerminalLookScope>() != null;++ /// Where a sheet or a dialog opened from [context] takes its look from: the place just above the+ /// terminal's page when [context] is on one, else [context] itself.+ ///+ /// A sheet is the app's, not the page's, so it follows the Theme like every other screen. Opened+ /// from the page's own context it would be drawn half and half: Material's sheet routes carry+ /// the opener's [Theme] across (`InheritedTheme.capture`), and this page's is the terminal's,+ /// while what the sheet draws itself reads the app's palette.+ static BuildContext outside(BuildContext context) {+ final scope = context+ .getElementForInheritedWidgetOfExactType<_TerminalLookScope>();+ if (scope == null) return context;+ // The scope's parent is the [TerminalLook] itself: above its Theme, and not under its scope.+ BuildContext? look;+ scope.visitAncestorElements((element) {+ look = element;+ return false;+ });+ return look ?? context;+ }++ /// The palette [context] is drawn on: the terminal's on a terminal's page, else the app's.+ static HarnessPalette paletteOf(BuildContext context) =>+ on(context) ? AppTheme.terminalPalette : AppTheme.palette.value;++ /// [read] as the design tokens resolve on [context]'s palette — see [AppTheme.asPalette], and+ /// do not `await` inside it. Off a terminal's page, or while the palettes agree, it is a plain+ /// call.+ static T read<T>(BuildContext context, T Function() read) {+ if (!on(context)) return read();+ final palette = AppTheme.terminalPalette;+ if (palette == AppTheme.palette.value) return read();+ return AppTheme.asPalette(palette, read);+ }++ @override+ Widget build(BuildContext context) {+ AppTheme.watch(context);+ final palette = AppTheme.terminalPalette;+ final app = Theme.of(context);+ return _TerminalLookScope(+ child: Theme(+ data: palette == AppTheme.palette.value ? app : _themeFor(palette, app),+ child: AnnotatedRegion<SystemUiOverlayStyle>(+ value: systemBarsFor(palette.brightness),+ child: child,+ ),+ ),+ );+ }++ /// The last theme built for a terminal's palette, kept while the app's theme ([app]) is the one+ /// it was built beside: the page rebuilds far more often than either changes, and building a+ /// theme is not free.+ static (HarnessPalette, ThemeData, ThemeData)? _built;++ static ThemeData _themeFor(HarnessPalette palette, ThemeData app) {+ if (_built case (final p, final a, final theme)+ when p == palette && identical(a, app)) {+ return theme;+ }+ final theme = AppTheme.asPalette(+ palette,+ () => buildAppTheme(brightness: palette.brightness),+ );+ _built = (palette, app, theme);+ return theme;+ }+}++class _TerminalLookScope extends InheritedWidget {+ const _TerminalLookScope({required super.child});++ @override+ bool updateShouldNotify(_TerminalLookScope oldWidget) => false;+}diff --git a/mobile/lib/widgets/terminal_pane_badges.dart b/mobile/lib/widgets/terminal_pane_badges.dartindex 4383f856a..cae3e08da 100644--- a/mobile/lib/widgets/terminal_pane_badges.dart+++ b/mobile/lib/widgets/terminal_pane_badges.dart@@ -1,6 +1,7 @@import 'package:flutter/material.dart';import '../shared/theme/app_theme.dart' as grid;+import '../terminal/terminal_look.dart';import '../theme/app_theme.dart';/// Image/file transfer progress with a cancel action, kept in the pane's corner.@@ -15,9 +16,14 @@ class TransferProgressBadge extends StatelessWidget {required this.onCancel,});+ // In the pane's corner, so on the terminal's palette ([TerminalLook]).@overrideWidget build(BuildContext context) {grid.AppTheme.watch(context);+ return TerminalLook.read(context, _badge);+ }++ Widget _badge() {final percentLabel = fraction == null? '': ' · ${(fraction! * 100).round()}%';diff --git a/mobile/lib/widgets/terminal_panel.dart b/mobile/lib/widgets/terminal_panel.dartindex 6b9a93aa7..61b9c850d 100644--- a/mobile/lib/widgets/terminal_panel.dart+++ b/mobile/lib/widgets/terminal_panel.dart@@ -15,6 +15,7 @@ import '../state/app_state.dart';import '../terminal/terminal_snapshot.dart';import '../terminal/terminal_binary.dart';import '../terminal/terminal_find.dart';+import '../terminal/terminal_look.dart';import '../terminal/terminal_font_store.dart';import '../terminal/terminal_link_opener.dart';import '../terminal/remote_media_download.dart';@@ -1867,13 +1868,13 @@ class _TerminalPanelState extends State<TerminalPanel>grid.AppTheme.watch(context);final session = widget.session;_syncTerminal(session.terminal);- final colors = terminalThemeFor(- grid.AppTheme.palette.value,- terminalThemeStore.value,- );+ // On the terminal's palette, which stays dark in a light app while Settings'+ // Theme is chosen ([TerminalLook]); the app's anywhere else.+ final palette = TerminalLook.paletteOf(context);+ final colors = terminalThemeFor(palette, terminalThemeStore.value);final foreground = colors.foreground;return ColoredBox(- color: grid.AppPalette.windowBg,+ color: TerminalLook.read(context, () => grid.AppPalette.windowBg),child: Column(children: [Expanded(@@ -1921,7 +1922,7 @@ class _TerminalPanelState extends State<TerminalPanel>// shows nothing. See xterm's `TerminalView.predictsEcho`.predictsEcho: true,theme: terminalScreenThemeFor(- grid.AppTheme.palette.value,+ palette,terminalThemeStore.value,),// ⚠️ **Nothing top or bottom, and that is the wholediff --git a/mobile/test/phone/phone_shell_swarm_voice_test.dart b/mobile/test/phone/phone_shell_swarm_voice_test.dartindex d5f88e3ec..49978196b 100644--- a/mobile/test/phone/phone_shell_swarm_voice_test.dart+++ b/mobile/test/phone/phone_shell_swarm_voice_test.dart@@ -10,8 +10,8 @@ import 'package:harness_mobile/swarm/swarm_session.dart';import '../voice_fakes.dart';import 'devices_fixture.dart';-/// The shell hands its Swarm chats a mic: the terminal's speech-to-text, or-/// in sample mode the sample's own, which records nothing.+/// The shell hands its Swarm chats a mic for voice messages, the terminal's+/// recorder; sample mode, which has no account to send them with, none.void main() {late DevicesApp app;@@ -32,7 +32,9 @@ void main() {SwarmHost host(WidgetTester tester) =>tester.widget<SwarmScope>(find.byType(SwarmScope)).notifier!;- testWidgets('a Swarm chat in the shell can be talked to', (tester) async {+ testWidgets('a Swarm chat in the shell can record voice messages', (+ tester,+ ) async {await tester.pumpWidget(MaterialApp(home: PhoneShell(notifier: app)));await tester.pump();// The shell's first reads, on their timers, as a launch has them.@@ -42,9 +44,7 @@ void main() {await tester.pumpWidget(const SizedBox());});- testWidgets('in sample mode a Swarm chat hears through the sample', (- tester,- ) async {+ testWidgets('in sample mode a Swarm chat has no mic', (tester) async {final sample = VoiceInputController(transcriber: (_, _) async => '',recorder: FakeVoiceRecorder(),@@ -62,7 +62,7 @@ void main() {// The shell's first reads, on their timers, as a launch has them.await tester.pump(const Duration(seconds: 1));- expect(host(tester).voice!(), same(sample));+ expect(host(tester).voice, isNull);await tester.pumpWidget(const SizedBox());});}diff --git a/mobile/test/phone_edge/settings_swarm_look_test.dart b/mobile/test/phone_edge/settings_swarm_look_test.dartindex 110395644..d3cc29a42 100644--- a/mobile/test/phone_edge/settings_swarm_look_test.dart+++ b/mobile/test/phone_edge/settings_swarm_look_test.dart@@ -160,7 +160,7 @@ void main() {('Size', LucideIcons.aLargeSmall300),('Colors', LucideIcons.palette300),('Language', LucideIcons.mic300),- ('App colors', LucideIcons.contrast300),+ ('Theme', LucideIcons.sunMoon300),('Text size', LucideIcons.aLargeSmall300),('Swarm', LucideIcons.flaskConical300),('Help', LucideIcons.circleHelp300),@@ -253,7 +253,7 @@ void main() {await reveal(tester, find.text('Language'));expect(inCard('Font', 'Language'), findsOneWidget);expect(inCard('Font', 'Colors'), findsOneWidget);- expect(inCard('Font', 'App colors'), findsNothing);+ expect(inCard('Font', 'Theme'), findsNothing);await close(tester, app);});diff --git a/mobile/test/phone_edge/settings_theme_test.dart b/mobile/test/phone_edge/settings_theme_test.dartnew file mode 100644index 000000000..8650c6973--- /dev/null+++ b/mobile/test/phone_edge/settings_theme_test.dart@@ -0,0 +1,365 @@+import 'package:flutter/material.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/core/models.dart';+import 'package:harness_mobile/phone/settings_page.dart';+import 'package:harness_mobile/settings/experimental_features.dart';+import 'package:harness_mobile/shared/theme/app_space.dart';+import 'package:harness_mobile/shared/theme/app_theme.dart';+import 'package:harness_mobile/shared/theme/app_type.dart';+import 'package:harness_mobile/shared/theme/appearance_builder.dart';+import 'package:harness_mobile/shared/theme/appearance_prefs_store.dart';+import 'package:harness_mobile/shared/theme/color_palette.dart';+import 'package:harness_mobile/state/app_state.dart';+import 'package:harness_mobile/swarm/swarm_row.dart';+import 'package:harness_mobile/swarm/swarm_style.dart';+import 'package:lucide_icons_flutter/lucide_icons.dart';+import 'package:package_info_plus/package_info_plus.dart';++import '../voice_fakes.dart' show MemoryKeyValueStore;+import 'edge_fixture.dart';++/// Settings opened from Swarm asks light or dark — Theme: System | Dark | Light — in App colors'+/// place, and every screen turns on the tap; the terminal keeps the dark side. Settings opened from+/// the terminal keeps App colors, which names the palette on screen and, picked, hands light and+/// dark back to the palette.+void main() {+ setUpAll(+ () => PackageInfo.setMockInitialValues(+ appName: 'Harness',+ packageName: 'ai.autonomous.harness',+ version: '1.2.3',+ buildNumber: '456',+ buildSignature: '',+ ),+ );++ tearDown(() {+ AppTheme.palette.value = HarnessPalette.graphite;+ AppTheme.brightness.value = Brightness.dark;+ AppTheme.terminalPaletteOverride.value = null;+ });++ /// Settings pushed over a page, in an app built as `HarnessApp` builds it: on [store]'s+ /// appearance ([AppearanceBuilder]), its brightness set from the palette as `_GridTokenScope`+ /// sets it — so a choice here repaints the whole app, as it does on a phone.+ Future<(AppNotifier, AppearancePrefsStore, MemoryKeyValueStore)> openSettings(+ WidgetTester tester, {+ required bool swarm,+ AppearancePrefs prefs = const AppearancePrefs(),+ Phone phone = largePhone,+ double textScale = 1,+ }) async {+ setPhone(tester, phone);+ final storage = MemoryKeyValueStore();+ final store = AppearancePrefsStore(storage: storage)..value = prefs;+ final app = edgeApp()+ ..currentUser = const CurrentUserProfile(+ id: 'u',+ name: 'Ada Lovelace',+ email: '[email protected]',+ );+ final page = SettingsPage(+ notifier: app,+ large: false,+ swarm: swarm,+ experiments: ExperimentalFeaturesStore(storage: MemoryKeyValueStore()),+ appearance: store,+ );+ await tester.pumpWidget(+ AppearanceBuilder(+ store: store,+ builder: (context, prefs, palette) {+ AppTheme.brightness.value = palette.brightness;+ return phoneApp(+ Builder(+ builder: (context) => TextButton(+ onPressed: () =>+ Navigator.of(context)+ .push(MaterialPageRoute<void>(builder: (_) => page)),+ child: const Text('open'),+ ),+ ),+ brightness: palette.brightness,+ textScale: textScale,+ );+ },+ ),+ );+ await tester.tap(find.text('open'));+ await frames(tester);+ return (app, store, storage);+ }++ Future<void> close(WidgetTester tester, AppNotifier app) async {+ await tester.pumpWidget(const SizedBox());+ await tester.pump(const Duration(seconds: 3));+ app.dispose();+ await tester.pump(const Duration(seconds: 30));+ }++ Future<void> reveal(WidgetTester tester, Finder finder) async {+ await tester.scrollUntilVisible(+ finder,+ 200,+ scrollable: find.byType(Scrollable).last,+ );+ await tester.pump();+ }++ Finder rowOf(String title) =>+ find.ancestor(of: find.text(title), matching: find.byType(SwarmRow));+ Finder segment(String label) =>+ find.descendant(of: rowOf('Theme'), matching: find.text(label));++ /// The fill a Theme segment wears: the primary ink when chosen.+ Color? fillOf(WidgetTester tester, String label) => tester+ .widget<Material>(+ find+ .ancestor(of: segment(label), matching: find.byType(Material))+ .first,+ )+ .color;++ /// The ground Settings is drawn on.+ Color? ground(WidgetTester tester) => tester+ .widget<Scaffold>(+ find+ .descendant(+ of: find.byType(SettingsPage),+ matching: find.byType(Scaffold),+ )+ .first,+ )+ .backgroundColor;++ group('opened from Swarm', () {+ testWidgets("Theme stands in App colors' place: System, Dark, Light — "+ 'Dark, for the shipped Graphite', (tester) async {+ final (app, _, _) = await openSettings(tester, swarm: true);++ await reveal(tester, find.text('Theme'));+ expect(find.text('App colors'), findsNothing);+ expect(+ find.descendant(+ of: rowOf('Theme'),+ matching: find.byIcon(LucideIcons.sunMoon300),+ ),+ findsOneWidget,+ );+ expect(+ find.descendant(+ of: find.ancestor(+ of: find.text('Theme'),+ matching: find.byType(SwarmGroup),+ ),+ matching: find.text('Text size'),+ ),+ findsOneWidget,+ reason: "in Appearance's card",+ );+ for (final label in ['System', 'Dark', 'Light']) {+ expect(segment(label), findsOneWidget, reason: label);+ }+ expect(fillOf(tester, 'Dark'), AppPalette.textPrimary);+ expect(fillOf(tester, 'System'), Colors.transparent);+ expect(fillOf(tester, 'Light'), Colors.transparent);+ expect(+ tester.getSemantics(find.bySemanticsLabel('Dark')),+ isSemantics(label: 'Dark', isButton: true, isSelected: true),+ );+ expect(+ tester.getSemantics(find.bySemanticsLabel('Light')),+ isSemantics(label: 'Light', isButton: true, isSelected: false),+ );+ await close(tester, app);+ });++ testWidgets('a screen reader changes the theme: each segment not chosen '+ 'takes a tap, the chosen one none (review of #2920)', (tester) async {+ final (app, store, storage) = await openSettings(tester, swarm: true);+ await reveal(tester, find.text('Theme'));+ expect(+ tester.getSemantics(find.bySemanticsLabel('Light')),+ isSemantics(label: 'Light', isButton: true, isSelected: false, hasTapAction: true),+ );+ expect(+ tester.getSemantics(find.bySemanticsLabel('System')),+ isSemantics(label: 'System', isButton: true, isSelected: false, hasTapAction: true),+ );+ expect(+ tester.getSemantics(find.bySemanticsLabel('Dark')),+ isSemantics(label: 'Dark', isButton: true, isSelected: true, hasTapAction: false),+ );+ tester.semantics.tap(find.semantics.byLabel('Light'));+ await frames(tester);+ expect(store.value.themeMode, AppThemeMode.light);+ expect(storage.values['app_theme_mode'], 'light');+ expect(fillOf(tester, 'Light'), AppPalette.textPrimary);+ await close(tester, app);+ });++ testWidgets("Light turns the app light at once and is kept; the terminal's "+ 'colours stay Graphite', (tester) async {+ final (app, store, storage) = await openSettings(tester, swarm: true);+ expect(ground(tester), HarnessPalette.graphite.background);++ await reveal(tester, find.text('Theme'));+ await tester.tap(segment('Light'));+ await frames(tester);++ expect(store.value.themeMode, AppThemeMode.light);+ expect(storage.values['app_theme_mode'], 'light');+ expect(AppTheme.palette.value, HarnessPalette.paper);+ expect(ground(tester), HarnessPalette.paper.background, reason: 'now');+ expect(fillOf(tester, 'Light'), AppPalette.textPrimary);+ expect(fillOf(tester, 'Dark'), Colors.transparent);++ await reveal(tester, find.text('Colors'));+ expect(+ find.descendant(of: rowOf('Colors'), matching: find.text('Graphite')),+ findsOneWidget,+ reason: 'the terminal keeps the dark side',+ );+ expect(AppTheme.terminalPalette, HarnessPalette.graphite);++ await reveal(tester, find.text('Theme'));+ await tester.tap(segment('Dark'));+ await frames(tester);+ expect(AppTheme.palette.value, HarnessPalette.graphite);+ expect(ground(tester), HarnessPalette.graphite.background);+ await close(tester, app);+ });++ testWidgets('System follows the phone, and keeps following it', (+ tester,+ ) async {+ addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);+ tester.platformDispatcher.platformBrightnessTestValue = Brightness.light;+ final (app, store, _) = await openSettings(tester, swarm: true);+ expect(+ AppTheme.palette.value,+ HarnessPalette.graphite,+ reason: 'untouched, a light phone changes nothing',+ );++ await reveal(tester, find.text('Theme'));+ await tester.tap(segment('System'));+ await frames(tester);+ expect(store.value.themeMode, AppThemeMode.system);+ expect(ground(tester), HarnessPalette.paper.background);++ tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;+ await frames(tester);+ expect(ground(tester), HarnessPalette.graphite.background);+ expect(fillOf(tester, 'System'), AppPalette.textPrimary);+ await close(tester, app);+ });++ testWidgets('a palette chosen in App colors stays on its side', (+ tester,+ ) async {+ final (app, _, _) = await openSettings(+ tester,+ swarm: true,+ prefs: const AppearancePrefs(palette: HarnessPalette.midnight),+ );++ await reveal(tester, find.text('Theme'));+ await tester.tap(segment('Light'));+ await frames(tester);+ expect(AppTheme.palette.value, HarnessPalette.paper);+ expect(AppTheme.terminalPalette, HarnessPalette.midnight);++ await tester.tap(segment('Dark'));+ await frames(tester);+ expect(AppTheme.palette.value, HarnessPalette.midnight, reason: 'back');+ await close(tester, app);+ });++ testWidgets("draws a compact control with a control's words", (+ tester,+ ) async {+ final (app, _, _) = await openSettings(+ tester,+ swarm: true,+ phone: smallPhone,+ );+ await reveal(tester, find.text('Theme'));++ // The test font draws every glyph an em wide, so widths here are not a+ // phone's; the roles are.+ expect(+ tester.getSize(find.byType(SwarmSegments<AppThemeMode>)).height,+ AppControl.heightSmall + 2 * AppSpace.xs,+ );+ final words = tester.widget<Text>(segment('Light')).style!;+ expect(words.fontSize, AppType.bodySize);+ expect(words.fontWeight, AppFont.medium);+ expect(+ tester.widget<Text>(segment('Dark')).style!.fontWeight,+ AppFont.semibold,+ reason: 'the chosen one',+ );+ await close(tester, app);+ });++ testWidgets('fits the smallest phone at the largest text size', (+ tester,+ ) async {+ final errors = await collectErrors(() async {+ final (app, _, _) = await openSettings(+ tester,+ swarm: true,+ phone: smallPhone,+ textScale: AppearancePrefs.uiSizeMax / AppearancePrefs.uiSizeDefault,+ );+ await reveal(tester, find.text('Theme'));+ for (final label in ['System', 'Dark', 'Light']) {+ expect(segment(label), findsOneWidget, reason: label);+ }+ await close(tester, app);+ });+ expect(errors, isEmpty);+ });+ });++ group('opened from the terminal', () {+ testWidgets('keeps App colors and no Theme; it names the palette on '+ 'screen', (tester) async {+ final (app, _, _) = await openSettings(+ tester,+ swarm: false,+ prefs: const AppearancePrefs(themeMode: AppThemeMode.light),+ );++ await reveal(tester, find.text('App colors'));+ expect(find.text('Theme'), findsNothing);+ expect(find.byType(SwarmSegments<AppThemeMode>), findsNothing);+ expect(find.text('Paper'), findsOneWidget, reason: "Light's palette");+ await close(tester, app);+ });++ testWidgets('a palette picked there hands light and dark back to it', (+ tester,+ ) async {+ final (app, store, storage) = await openSettings(+ tester,+ swarm: false,+ prefs: const AppearancePrefs(themeMode: AppThemeMode.light),+ );++ await reveal(tester, find.text('App colors'));+ await tester.tap(find.text('App colors'));+ await frames(tester);+ await tester.tap(find.text('Midnight'));+ await frames(tester);++ expect(store.value.themeMode, isNull);+ expect(store.value.palette, HarnessPalette.midnight);+ expect(storage.values.containsKey('app_theme_mode'), isFalse);+ expect(AppTheme.palette.value, HarnessPalette.midnight);+ expect(ground(tester), HarnessPalette.midnight.background);+ await close(tester, app);+ });+ });+}diff --git a/mobile/test/phone_resume_agent_test.dart b/mobile/test/phone_resume_agent_test.dartindex 6bba5a646..b60542e59 100644--- a/mobile/test/phone_resume_agent_test.dart+++ b/mobile/test/phone_resume_agent_test.dart@@ -1,5 +1,6 @@import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/core/models.dart';+import 'package:harness_mobile/phone/resume_agent.dart';import 'package:harness_mobile/state/app_state.dart';import 'agent_pager_fixture.dart';@@ -217,4 +218,117 @@ void main() {final c = app.stateOf('m')!.agents.where((a) => a.id == 'c').single;expect(c.isStopped, isTrue);});++ // What the Swarm's Open terminal reads and asks for (`PhoneShellScope`'s+ // agentStopped and onResumeAgent), on the person's own machines alone.+ group("the Swarm's resume", () {+ test('reads stopped work, and whether a resume can bring it back', () {+ final app = stoppedApp(_ResumeConn());+ addTearDown(app.dispose);++ expect(phoneAgentStopped(app, 'm', 'b'), isTrue);+ expect(phoneAgentStopped(app, 'm', 'a'), isNull, reason: 'running');+ expect(phoneAgentStopped(app, 'm', 'gone'), isNull);+ expect(phoneAgentStopped(app, 'other', 'b'), isNull);++ final machine = app.stateOf('m')!;+ machine.agents = [+ for (final agent in machine.agents)+ agent.id == 'b'+ ? Agent.fromJson(+ {...agentJson(stopped: true)}..remove('sessionId'),+ )+ : agent,+ ];+ expect(+ phoneAgentStopped(app, 'm', 'b'),+ isFalse,+ reason: 'no saved conversation to bring back',+ );+ });++ test('never on a machine another account shares', () async {+ final conn = _ResumeConn()..answer = (_, payload) => created(payload);+ final app = stoppedApp(conn);+ addTearDown(app.dispose);+ const shared = Machine(+ machineId: 's',+ authMode: MachineAuthMode.remote,+ name: 'Ken’s Mac',+ shared: true,+ );+ app.machineStates['s'] = MachineState(shared)+ ..nodeOnline = true+ ..connectionStatus = ConnectionStatus.connected+ ..agents = [Agent.fromJson(agentJson(stopped: true))];++ expect(phoneAgentStopped(app, 's', 'b'), isNull);+ expect(+ await phoneResumeAgent(app, 's', 'b'),+ 'Only the person whose machine runs this agent can resume it.',+ );+ expect(conn.requests, isEmpty);+ });++ test(+ 'resumes stopped work, then answers once its terminal is back',+ () async {+ final conn = _ResumeConn()..answer = (_, payload) => created(payload);+ final app = stoppedApp(conn);+ addTearDown(app.dispose);++ expect(await phoneResumeAgent(app, 'm', 'b'), isNull);+ expect(conn.requests.single.$1, 'agent_resume');+ expect(+ app.stateOf('m')!.agents.firstWhere((a) => a.id == 'b').isStopped,+ isFalse,+ );+ },+ );++ test('work no longer stopped is opened as it is, asking the machine '+ 'nothing', () async {+ final conn = _ResumeConn();+ final app = stoppedApp(conn);+ addTearDown(app.dispose);++ expect(await phoneResumeAgent(app, 'm', 'a'), isNull);+ expect(conn.requests, isEmpty);+ });++ test('a harness its machine no longer lists says so', () async {+ final conn = _ResumeConn();+ final app = stoppedApp(conn);+ addTearDown(app.dispose);++ expect(+ await phoneResumeAgent(app, 'm', 'gone'),+ 'This harness is no longer on Test host.',+ );+ expect(+ await phoneResumeAgent(app, 'nowhere', 'b'),+ 'This harness is no longer on this machine.',+ );+ expect(conn.requests, isEmpty);+ });++ test(+ "a resume the machine refuses comes back in the machine's words",+ () async {+ final conn = _ResumeConn()+ ..answer = (_, payload) => {+ 'creationId': payload['creationId'],+ 'state': 'failed',+ 'failure': {+ 'code': 'ENGINE_LAUNCH_REFUSED',+ 'detail': 'Use another engine.',+ },+ };+ final app = stoppedApp(conn);+ addTearDown(app.dispose);++ expect(await phoneResumeAgent(app, 'm', 'b'), 'Use another engine.');+ },+ );+ });}diff --git a/mobile/test/render/swarm_composer_render_test.dart b/mobile/test/render/swarm_composer_render_test.dartindex bdb4e35a5..a5de5c952 100644--- a/mobile/test/render/swarm_composer_render_test.dart+++ b/mobile/test/render/swarm_composer_render_test.dart@@ -8,7 +8,6 @@ import 'package:flutter/rendering.dart';import 'package:flutter/services.dart';import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/notify/system_notices.dart';-import 'package:harness_mobile/phone/voice_input_controller.dart';import 'package:harness_mobile/phone/voice_recorder.dart';import 'package:harness_mobile/settings/experimental_features.dart';import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;@@ -20,7 +19,6 @@ import 'package:harness_mobile/swarm/swarm_session.dart';import 'package:image_picker/image_picker.dart' show XFile;import '../voice_fakes.dart';-import '../swarm/swarm_chat_dictation_test.dart' show FakeDictation;import '../swarm/swarm_fixture.dart';/// Renders a phone Swarm chat's composer in the states Tony's composer brief@@ -165,8 +163,6 @@ void main() {Future<GlobalKey> chat(WidgetTester tester, {_MeteredRecorder? recorder,- FakeTranscriber? stt,- FakeDictation? dictation,}) async {await tester.runAsync(_loadFonts);final photos = (await tester.runAsync(@@ -213,12 +209,7 @@ void main() {upload: stuck,notices: SilentSystemNotices(),features: features,- voice: () => VoiceInputController(- transcriber: (stt ?? FakeTranscriber()).call,- recorder: recorder ?? _MeteredRecorder(),- language: ValueNotifier('en'),- ),- dictation: dictation,+ voice: () => recorder ?? _MeteredRecorder(),)..foreground = false;addTearDown(host.dispose);final key = GlobalKey();@@ -309,7 +300,9 @@ void main() {await shoot(tester, key, 'composer-files-$look');});- testWidgets('listening ($look)', skip: skip, (tester) async {+ // A voice message recording (task #2584): X, the red dot and its time,+ // the wave and Send; what was typed stays in the box behind it.+ testWidgets('recording ($look)', skip: skip, (tester) async {final recorder = _MeteredRecorder();final key = await chat(tester, recorder: recorder);await type(tester, 'Check whether PR #2466');@@ -322,38 +315,15 @@ void main() {recorder.meter.value = level;await tester.pump(const Duration(milliseconds: 120));}- await shoot(tester, key, 'composer-listening-$look');+ await shoot(tester, key, 'composer-recording-$look');});- // Tony, 11-01: the voice row's Send, right of stop: off on an empty box- // with the phone's recognizer, live once it has words.- for (final words in ['', 'Check whether PR #2466 has merged']) {- final name = words.isEmpty ? 'off' : 'live';- testWidgets('voice row Send $name, dictation ($look)', skip: skip, (- tester,- ) async {- final dictation = FakeDictation();- final key = await chat(tester, dictation: dictation);- await tester.showKeyboard(find.byType(TextField));- await tester.tap(find.byKey(const ValueKey('swarm-mic')));- for (var i = 0; i < 5; i++) {- await tester.pump();- }- if (words.isNotEmpty) dictation.say(words);- for (var i = 0; i < 24; i++) {- dictation.loud(const [0.3, 0.7, 0.5, 0.9][i % 4]);- await tester.pump(const Duration(milliseconds: 120));- }- await shoot(tester, key, 'composer-voice-send-$name-$look');- });- }-testWidgets('voice row Send while sending ($look)', skip: skip, (tester,) async {final recorder = _MeteredRecorder();- final stt = FakeTranscriber()..pending = Completer<String>();- final key = await chat(tester, recorder: recorder, stt: stt);+ // Its clip's upload never answers: Send's spinner stays.+ final key = await chat(tester, recorder: recorder);await type(tester, 'Check whether PR #2466');await tester.tap(find.byKey(const ValueKey('swarm-mic')));for (var i = 0; i < 24; i++) {diff --git a/mobile/test/render/swarm_thread_render_test.dart b/mobile/test/render/swarm_thread_render_test.dartindex cb06e38d4..3e4c948b7 100644--- a/mobile/test/render/swarm_thread_render_test.dart+++ b/mobile/test/render/swarm_thread_render_test.dart@@ -7,7 +7,6 @@ import 'package:flutter/rendering.dart';import 'package:flutter/services.dart';import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/notify/system_notices.dart';-import 'package:harness_mobile/phone/voice_input_controller.dart';import 'package:harness_mobile/settings/experimental_features.dart';import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;import 'package:harness_mobile/shared/theme/color_palette.dart';@@ -311,11 +310,7 @@ void main() {notices: SilentSystemNotices(),features: features,// The app's mic, in the chat's box and the thread's.- voice: () => VoiceInputController(- transcriber: FakeTranscriber().call,- recorder: FakeVoiceRecorder(),- language: ValueNotifier('en'),- ),+ voice: () => FakeVoiceRecorder(),)..foreground = false;app = host;addTearDown(host.dispose);diff --git a/mobile/test/render/swarm_voice_render_test.dart b/mobile/test/render/swarm_voice_render_test.dartindex 6c1429b72..cf13f2661 100644--- a/mobile/test/render/swarm_voice_render_test.dart+++ b/mobile/test/render/swarm_voice_render_test.dart@@ -1,4 +1,5 @@import 'dart:io';+import 'dart:math' as math;import 'dart:ui' as ui;import 'package:flutter/foundation.dart';@@ -7,7 +8,6 @@ import 'package:flutter/rendering.dart';import 'package:flutter/services.dart';import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/notify/system_notices.dart';-import 'package:harness_mobile/phone/voice_input_controller.dart';import 'package:harness_mobile/phone/voice_recorder.dart';import 'package:harness_mobile/settings/experimental_features.dart';import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;@@ -18,9 +18,10 @@ import 'package:harness_mobile/swarm/swarm_session.dart';import '../voice_fakes.dart';import '../swarm/swarm_fixture.dart';-/// Renders a phone Swarm chat's mic — at rest, listening, with what was heard-/// in the box, and refused — in light and dark, at iPhone 14 size in the-/// system fonts, to PNGs a reviewer can look at without a phone.+/// Renders a phone Swarm chat's voice messages (task #2584) — the mic at+/// rest, a take recording, a voice message with its words hidden and shown,+/// and the mic refused — in light and dark, at iPhone 14 size in the system+/// fonts, to PNGs a reviewer can look at without a phone.////// Skipped unless PHONE_RENDER_DIR names a directory to write into:///@@ -80,8 +81,9 @@ void main() {'g1',title: 'Login crash on iPhone',agentIds: ['agent-lead', 'agent-builder'],+ leadId: 'agent-lead',status: 'working',- lastMessageSequence: 1,+ lastMessageSequence: 2,),],);@@ -104,6 +106,38 @@ void main() {'reactions': <Object>[],'createdAt': DateTime.utc(2026, 10, 28, 4, 1).toIso8601String(),},+ {+ 'id': 'm2',+ 'conversationId': 'g1',+ 'text':+ 'Yes, ship it today, and tell me when the build is up so I '+ 'can try the login on my phone.',+ 'sequence': 2,+ 'author': {'kind': 'person', 'id': me, 'name': 'dee'},+ 'attachments': [+ {+ 'id': 'clip',+ 'name': 'Voice message.wav',+ 'mime': 'audio/wav',+ 'size': 2592044,+ 'url': 'https://cdn.example.test/super-tabs/$tab/a/clip.wav',+ 'voice': {+ 'durationMs': 81000,+ 'waveform': [+ for (var i = 0; i < 48; i++)+ (6 ++ 20 *+ (0.5 + 0.5 * math.sin(i * 0.7)) *+ (i % 5 == 0 ? 0.4 : 1))+ .round(),+ ],+ 'transcribed': true,+ },+ },+ ],+ 'reactions': <Object>[],+ 'createdAt': DateTime.utc(2026, 10, 28, 4, 3).toIso8601String(),+ },],'next': null,};@@ -128,7 +162,6 @@ void main() {Future<GlobalKey> chat(WidgetTester tester,_MeteredRecorder recorder,- FakeTranscriber stt,) async {await tester.runAsync(_loadFonts);// A palette carries its brightness: a light one is how the app goes light.@@ -163,11 +196,7 @@ void main() {request: backend().request,notices: SilentSystemNotices(),features: features,- voice: () => VoiceInputController(- transcriber: stt.call,- recorder: recorder,- language: ValueNotifier('en'),- ),+ voice: () => recorder,)..foreground = false;addTearDown(host.dispose);final key = GlobalKey();@@ -199,37 +228,35 @@ void main() {}}- testWidgets('mic at rest ($look)', skip: skip, (tester) async {- final key = await chat(tester, _MeteredRecorder(), FakeTranscriber());- await shoot(tester, key, 'swarm-voice-rest-$look');+ testWidgets('mic at rest, a voice message ($look)', skip: skip, (+ tester,+ ) async {+ final key = await chat(tester, _MeteredRecorder());+ await shoot(tester, key, 'swarm-voice-message-$look');});- testWidgets('listening ($look)', skip: skip, (tester) async {- final recorder = _MeteredRecorder();- final key = await chat(tester, recorder, FakeTranscriber());- await tapMic(tester);- recorder.meter.value = 0.7;- await tester.pump(const Duration(milliseconds: 400));- await shoot(tester, key, 'swarm-voice-listening-$look');+ testWidgets('a voice message\'s words open ($look)', skip: skip, (+ tester,+ ) async {+ final key = await chat(tester, _MeteredRecorder());+ await tester.tap(find.byKey(const ValueKey('swarm-voice-words-toggle')));+ await tester.pumpAndSettle();+ await shoot(tester, key, 'swarm-voice-words-$look');});- testWidgets('heard into the box ($look)', skip: skip, (tester) async {- final stt = FakeTranscriber()- ..replies.add('Yes, ship it today and tell me when it is up.');- final key = await chat(tester, _MeteredRecorder(), stt);+ testWidgets('recording ($look)', skip: skip, (tester) async {+ final recorder = _MeteredRecorder();+ final key = await chat(tester, recorder);await tapMic(tester);- // Stop: the words land in the box, unsent.- await tester.tap(find.byKey(const ValueKey('swarm-voice-stop')));- await tester.pumpAndSettle();- await shoot(tester, key, 'swarm-voice-heard-$look');+ for (var i = 0; i < 40; i++) {+ recorder.meter.value = const [0.2, 0.6, 0.9, 0.4, 0.7, 0.3][i % 6];+ await tester.pump(const Duration(milliseconds: 350));+ }+ await shoot(tester, key, 'swarm-voice-recording-$look');});testWidgets('mic refused ($look)', skip: skip, (tester) async {- final key = await chat(- tester,- _MeteredRecorder()..permitted = false,- FakeTranscriber(),- );+ final key = await chat(tester, _MeteredRecorder()..permitted = false);await tapMic(tester);await tester.pumpAndSettle();await shoot(tester, key, 'swarm-voice-refused-$look');diff --git a/mobile/test/shared/appearance_builder_test.dart b/mobile/test/shared/appearance_builder_test.dartnew file mode 100644index 000000000..dc411fc94--- /dev/null+++ b/mobile/test/shared/appearance_builder_test.dart@@ -0,0 +1,89 @@+import 'package:flutter/widgets.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/shared/theme/app_theme.dart';+import 'package:harness_mobile/shared/theme/appearance_builder.dart';+import 'package:harness_mobile/shared/theme/appearance_prefs_store.dart';+import 'package:harness_mobile/shared/theme/color_palette.dart';++import '../voice_fakes.dart' show MemoryKeyValueStore;++/// What the app does before it builds its theme: the palette Settings' Theme resolves to, for this+/// phone's own light or dark, on `AppTheme` — and built again as either moves.+void main() {+ tearDown(() {+ AppTheme.palette.value = HarnessPalette.graphite;+ AppTheme.terminalPaletteOverride.value = null;+ });++ Future<List<HarnessPalette>> pump(+ WidgetTester tester,+ AppearancePrefsStore store,+ ) async {+ final built = <HarnessPalette>[];+ await tester.pumpWidget(+ AppearanceBuilder(+ store: store,+ builder: (context, prefs, palette) {+ expect(+ AppTheme.palette.value,+ palette,+ reason: 'on before the build',+ );+ built.add(palette);+ return const SizedBox();+ },+ ),+ );+ return built;+ }++ testWidgets('System follows the phone as it turns', (tester) async {+ addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);+ tester.platformDispatcher.platformBrightnessTestValue = Brightness.light;+ final store = AppearancePrefsStore(storage: MemoryKeyValueStore());+ await store.setThemeMode(AppThemeMode.system);++ final built = await pump(tester, store);+ expect(built.last, HarnessPalette.paper);+ expect(+ AppTheme.terminalPalette,+ HarnessPalette.graphite,+ reason: 'the terminal stays dark',+ );++ tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;+ await tester.pump();+ expect(built.last, HarnessPalette.graphite);+ expect(AppTheme.terminalPaletteOverride.value, isNull);+ });++ testWidgets('Dark and Light hold whatever the phone says', (tester) async {+ addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);+ tester.platformDispatcher.platformBrightnessTestValue = Brightness.light;+ final store = AppearancePrefsStore(storage: MemoryKeyValueStore());+ await store.setThemeMode(AppThemeMode.dark);++ final built = await pump(tester, store);+ expect(built.last, HarnessPalette.graphite);++ await store.setThemeMode(AppThemeMode.light);+ await tester.pump();+ expect(built.last, HarnessPalette.paper);++ tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;+ await tester.pump();+ expect(AppTheme.palette.value, HarnessPalette.paper);+ });++ testWidgets('left to App colors, a light phone changes nothing', (+ tester,+ ) async {+ addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);+ tester.platformDispatcher.platformBrightnessTestValue = Brightness.light;+ final store = AppearancePrefsStore(storage: MemoryKeyValueStore());++ final built = await pump(tester, store);+ expect(built.last, HarnessPalette.graphite, reason: 'the shipped dark');+ expect(AppTheme.terminalPaletteOverride.value, isNull);+ });+}diff --git a/mobile/test/shared/appearance_prefs_store_test.dart b/mobile/test/shared/appearance_prefs_store_test.dartindex 64cbd09ca..9f63e4b86 100644--- a/mobile/test/shared/appearance_prefs_store_test.dart+++ b/mobile/test/shared/appearance_prefs_store_test.dart@@ -1,7 +1,9 @@import 'dart:async';+import 'dart:ui' show Brightness;import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/core/local_key_value_store.dart';+import 'package:harness_mobile/shared/theme/app_theme.dart';import 'package:harness_mobile/shared/theme/appearance_prefs_store.dart';import 'package:harness_mobile/shared/theme/color_palette.dart';@@ -199,6 +201,204 @@ void main() {});});+ group('theme (System | Dark | Light, Swarm\'s Settings)', () {+ tearDown(() {+ AppTheme.palette.value = HarnessPalette.graphite;+ AppTheme.terminalPaletteOverride.value = null;+ });++ test('unset, the palette decides, as before Theme existed', () {+ for (final palette in HarnessPalette.values) {+ final prefs = AppearancePrefs(palette: palette);+ for (final phone in Brightness.values) {+ expect(prefs.paletteFor(phone), palette, reason: palette.name);+ }+ expect(prefs.terminalPalette, palette, reason: 'App colors dresses it');+ expect(+ prefs.shownThemeMode,+ palette.isDark ? AppThemeMode.dark : AppThemeMode.light,+ );+ }+ });++ test('Dark and Light keep a palette on their side, else the shipped one '+ 'of that side', () {+ const midnight = AppearancePrefs(+ palette: HarnessPalette.midnight,+ themeMode: AppThemeMode.dark,+ );+ expect(midnight.paletteFor(Brightness.light), HarnessPalette.midnight);+ expect(+ midnight.copyWith(themeMode: AppThemeMode.light).paletteFor(+ Brightness.dark,+ ),+ HarnessPalette.paper,+ );+ const mist = AppearancePrefs(+ palette: HarnessPalette.mist,+ themeMode: AppThemeMode.light,+ );+ expect(mist.paletteFor(Brightness.dark), HarnessPalette.mist);+ expect(+ mist.copyWith(themeMode: AppThemeMode.dark).paletteFor(Brightness.light),+ HarnessPalette.graphite,+ );+ });++ test('System follows the phone', () {+ const prefs = AppearancePrefs(themeMode: AppThemeMode.system);+ expect(prefs.paletteFor(Brightness.dark), HarnessPalette.graphite);+ expect(prefs.paletteFor(Brightness.light), HarnessPalette.paper);+ expect(prefs.shownThemeMode, AppThemeMode.system);+ });++ test("once Theme is chosen the terminal keeps the dark side, whatever "+ 'the app wears', () {+ for (final mode in AppThemeMode.values) {+ expect(+ AppearancePrefs(themeMode: mode).terminalPalette,+ HarnessPalette.graphite,+ reason: mode.name,+ );+ expect(+ AppearancePrefs(+ palette: HarnessPalette.forest,+ themeMode: mode,+ ).terminalPalette,+ HarnessPalette.forest,+ reason: 'a dark palette chosen in App colors stays: ${mode.name}',+ );+ expect(+ AppearancePrefs(+ palette: HarnessPalette.paper,+ themeMode: mode,+ ).terminalPalette,+ HarnessPalette.graphite,+ reason: 'never light: ${mode.name}',+ );+ }+ });++ test('is written beside the palette and read back', () async {+ final storage = _Memory();+ final store = AppearancePrefsStore(storage: storage);+ var repaints = 0;+ store.addListener(() => repaints++);++ await store.setThemeMode(AppThemeMode.light);+ expect(repaints, 1);+ expect(storage.values['app_theme_mode'], 'light');+ expect(store.value.palette, HarnessPalette.graphite, reason: 'kept');++ final again = AppearancePrefsStore(storage: storage);+ await again.load();+ expect(again.value.themeMode, AppThemeMode.light);+ });++ test('the mode already on is no write', () async {+ final storage = _Memory();+ final store = AppearancePrefsStore(storage: storage);+ await store.setThemeMode(AppThemeMode.dark);+ final writes = storage.writes.length;+ await store.setThemeMode(AppThemeMode.dark);+ expect(storage.writes, hasLength(writes));+ });++ test('a mode from another build is no mode: the palette decides', () async {+ final store = AppearancePrefsStore(+ storage: _Memory({'app_theme_mode': 'sepia'}),+ );+ await store.load();+ expect(store.value.themeMode, isNull);+ expect(AppThemeMode.fromId(null), isNull);+ for (final mode in AppThemeMode.values) {+ expect(AppThemeMode.fromId(mode.name), mode);+ }+ });++ test('taps faster than the disk end on the LAST one', () async {+ final storage = _Memory()..gate = Completer<void>();+ final store = AppearancePrefsStore(storage: storage);++ final first = store.setThemeMode(AppThemeMode.light);+ final second = store.setThemeMode(AppThemeMode.system);+ final third = store.setPalette(HarnessPalette.ember);+ final fourth = store.setThemeMode(AppThemeMode.dark);+ expect(+ identical(first, second) &&+ identical(second, third) &&+ identical(third, fourth),+ isTrue,+ reason: 'one save in flight, which the later taps ride on',+ );++ storage.gate!.complete();+ await fourth;+ expect(storage.values['app_theme_mode'], 'dark');+ expect(storage.values['app_color_palette'], 'ember');+ expect(store.value.themeMode, AppThemeMode.dark);+ });++ test('App colors hands light and dark back to the palette — even the '+ 'palette that was already on', () async {+ final storage = _Memory();+ final store = AppearancePrefsStore(storage: storage);+ await store.setThemeMode(AppThemeMode.light);+ expect(store.value.paletteFor(Brightness.dark), HarnessPalette.paper);++ // Graphite is the stored palette already; Light had it drawn as Paper.+ await store.setPalette(HarnessPalette.graphite);+ expect(store.value.themeMode, isNull);+ expect(store.value.paletteFor(Brightness.light), HarnessPalette.graphite);+ expect(storage.values.containsKey('app_theme_mode'), isFalse);+ });++ test('reset forgets it', () async {+ final storage = _Memory();+ final store = AppearancePrefsStore(storage: storage);+ await store.setThemeMode(AppThemeMode.system);+ await store.reset();+ expect(store.value.themeMode, isNull);+ expect(storage.values.containsKey('app_theme_mode'), isFalse);+ });++ test('a disk that fails keeps the mode for this run', () async {+ final store = AppearancePrefsStore(storage: _Memory()..failing = true);+ await store.setThemeMode(AppThemeMode.light);+ expect(store.value.themeMode, AppThemeMode.light);+ });++ test("applyAppearance puts the app's palette and the terminal's on "+ 'AppTheme', () {+ expect(+ applyAppearance(+ const AppearancePrefs(themeMode: AppThemeMode.light),+ platform: Brightness.dark,+ ),+ HarnessPalette.paper,+ );+ expect(AppTheme.palette.value, HarnessPalette.paper);+ expect(AppTheme.terminalPaletteOverride.value, HarnessPalette.graphite);+ expect(AppTheme.terminalPalette, HarnessPalette.graphite);++ // Dark: the two agree, so there is no override to carry.+ applyAppearance(+ const AppearancePrefs(themeMode: AppThemeMode.dark),+ platform: Brightness.light,+ );+ expect(AppTheme.palette.value, HarnessPalette.graphite);+ expect(AppTheme.terminalPaletteOverride.value, isNull);++ // App colors' Paper: the terminal is light with the app, as before.+ applyAppearance(+ const AppearancePrefs(palette: HarnessPalette.paper),+ platform: Brightness.dark,+ );+ expect(AppTheme.palette.value, HarnessPalette.paper);+ expect(AppTheme.terminalPalette, HarnessPalette.paper);+ });+ });+test('prefs compare by value, so an unchanged load repaints nothing', () {const a = AppearancePrefs(uiFamily: 'Inter', uiSize: 15);expect(a, const AppearancePrefs(uiFamily: 'Inter', uiSize: 15));@@ -209,5 +409,10 @@ void main() {expect(a == const AppearancePrefs(uiSize: 15), isFalse);expect(a.copyWith(clearUiFamily: true).uiFamily, isNull);expect(a.copyWith(palette: HarnessPalette.dusk).uiFamily, 'Inter');++ final light = a.copyWith(themeMode: AppThemeMode.light);+ expect(light == a, isFalse, reason: 'the mode is part of the value');+ expect(light.copyWith(uiSize: 16).themeMode, AppThemeMode.light);+ expect(light.copyWith(clearThemeMode: true), a);});}diff --git a/mobile/test/swarm/swarm_chat_dictation_test.dart b/mobile/test/swarm/swarm_chat_dictation_test.dartdeleted file mode 100644index 14e8dbe6a..000000000--- a/mobile/test/swarm/swarm_chat_dictation_test.dart+++ /dev/null@@ -1,1180 +0,0 @@-import 'dart:async';--import 'package:flutter/foundation.dart';-import 'package:flutter/material.dart';-import 'package:flutter_test/flutter_test.dart';-import 'package:harness_mobile/notify/system_notices.dart';-import 'package:harness_mobile/phone/voice_input_controller.dart';-import 'package:harness_mobile/phone/voice_language_store.dart';-import 'package:harness_mobile/phone/voice_mic_face.dart';-import 'package:harness_mobile/settings/experimental_features.dart';-import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;-import 'package:harness_mobile/swarm/swarm_conversation_page.dart';-import 'package:harness_mobile/swarm/swarm_dictation.dart';-import 'package:harness_mobile/swarm/swarm_file_picker.dart';-import 'package:harness_mobile/swarm/swarm_session.dart';-import 'package:harness_mobile/swarm/swarm_voice.dart';-import 'package:image_picker/image_picker.dart' show XFile;--import '../voice_fakes.dart';-import 'swarm_fixture.dart';--/// A Swarm chat's mic on the phone's own speech recognizer (Tony, 10-28: the-/// one the keyboard's dictation uses): the words show in the message box as-/// they are said, and it listens on — through any pause — until Stop ends it-/// with the words left in the box, or Cancel takes back what it added. Voice-/// never sends by itself (Tony, 11-01): Send does. Only two minutes without a-/// new word stop it before that, the words left in the box (Lead, review of-/// #2056). Refused, or without the language, the mic records and the backend-/// transcribes.-void main() {- const messages = '/api/super-tabs/$tab/conversations/g1/messages';- const commands = '/api/super-tabs/$tab/commands';-- late FakeSwarm fake;- late FakeDictation dictation;- late FakeVoiceRecorder recorder;- late FakeTranscriber stt;- late List<String> uploads;- late String language;-- /// Who is signed in, in the last [pump].- late ValueNotifier<String?> account;-- setUp(() {- dictation = FakeDictation();- recorder = FakeVoiceRecorder();- stt = FakeTranscriber();- uploads = [];- language = voiceLanguageStore.value;- fake = FakeSwarm();- fake.routes['/api/super-tabs/list'] = (_) => {- 'tabs': [- {'id': tab, 'name': 'Phone Swarm', 'ownerId': me},- ],- 'next': null,- };- fake.routes['/api/super-tabs/$tab/parts'] = (_) => firstLoad(- groups: [- groupData(- 'g1',- title: 'Fix login',- agentIds: ['agent-lead', 'agent-builder'],- lastMessageSequence: 1,- ),- ],- );- fake.routes['/api/super-tabs/$tab/changes'] = (_) => changes(cursor: 'c2');- fake.routes['/api/super-tabs/$tab/decisions?state=open'] = (_) => {- 'decisions': <Object>[],- };- fake.routes['/api/super-tabs/$tab/mentions?state=waiting'] = (_) => {- 'mentions': <Object>[],- };- fake.routes[messages] = (_) => {'messages': <Object>[], 'next': null};- fake.routes[commands] = (_) => {'result': <String, dynamic>{}};- });-- tearDown(() => voiceLanguageStore.value = language);-- Future<void> pump(WidgetTester tester, {bool reduceMotion = false}) async {- final features = ExperimentalFeaturesStore(storage: MemoryKeyValueStore())- ..value = {PhoneExperiment.swarm};- account = ValueNotifier<String?>(me);- final host = SwarmHost(- accountId: () => account.value,- account: account,- request: fake.request,- notices: SilentSystemNotices(),- features: features,- upload:- ({- required tabId,- required conversationId,- required name,- required mime,- required file,- required cancel,- required onProgress,- }) {- uploads.add(name);- return Completer<Map<String, dynamic>>().future;- },- voice: () => VoiceInputController(- transcriber: stt.call,- recorder: recorder,- language: ValueNotifier('vi'),- ),- dictation: dictation,- )..foreground = false;- addTearDown(host.dispose);- await tester.pumpWidget(- MaterialApp(- theme: grid.buildAppTheme(brightness: Brightness.dark),- builder: (context, child) => MediaQuery(- data: MediaQuery.of(context)- .copyWith(disableAnimations: reduceMotion),- child: SwarmScope(notifier: host, child: child!),- ),- home: const SwarmConversationPage(swarmId: tab, conversationId: 'g1'),- ),- );- await tester.pumpAndSettle();- }-- Finder key(String value) => find.byKey(ValueKey(value));-- /// A few frames, for the take's awaits to land. Never settling: the mic's- /// bars move for as long as it listens.- Future<void> frames(WidgetTester tester) async {- for (var i = 0; i < 5; i++) {- await tester.pump();- }- }-- Future<void> tapMic(WidgetTester tester) async {- await tester.tap(key('swarm-mic'));- await frames(tester);- }-- /// The voice row's Stop: the take ends, its words left in the box.- Future<void> stop(WidgetTester tester) async {- await tester.tap(key('swarm-voice-stop'));- await frames(tester);- }-- /// The voice row's Cancel: what the take added leaves the box.- Future<void> cancel(WidgetTester tester) async {- await tester.tap(key('swarm-voice-cancel'));- await frames(tester);- }-- /// Back at rest: the mic and Send in the toolbar, no voice row.- void atRest() {- expect(key('swarm-voice-wave'), findsNothing);- expect(key('swarm-mic'), findsOneWidget);- expect(key('swarm-send'), findsOneWidget);- }-- Future<void> say(WidgetTester tester, String words) async {- dictation.say(words);- await frames(tester);- }-- String box(WidgetTester tester) =>- tester.widget<TextField>(find.byType(TextField)).controller!.text;-- Future<void> lifecycle(- WidgetTester tester,- List<AppLifecycleState> states,- ) async {- for (final state in states) {- tester.binding.handleAppLifecycleStateChanged(state);- }- await frames(tester);- }-- testWidgets('words show in the box as they are said, revised as the '- 'recognizer changes its mind', (tester) async {- voiceLanguageStore.value = 'vi';- await pump(tester);-- await tapMic(tester);- expect(dictation.locales, ['vi-VN']);- expect(recorder.starts, 0, reason: 'the recognizer has the microphone');- expect(key('swarm-voice-stop'), findsOneWidget);- expect(key('swarm-mic'), findsNothing);-- await say(tester, 'hôm');- expect(box(tester), 'hôm');- await say(tester, 'hôm nay mình làm');- expect(box(tester), 'hôm nay mình làm');- await say(tester, 'Hôm nay mình làm gì?');- expect(box(tester), 'Hôm nay mình làm gì?');- expect(dictation.listening, isTrue);- expect(fake.commands, isEmpty);- });-- // Tony, 11-01: voice never sends by itself; sending is a tap on Send.- testWidgets('Stop ends listening with its last words in the box, sends '- 'nothing and says nothing; Send then posts them', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'một hai ba');-- dictation.lastWords = 'một hai ba bốn';- await stop(tester);- await tester.pumpAndSettle();-- expect(dictation.stops, 1);- expect(box(tester), 'một hai ba bốn');- expect(fake.commands, isEmpty, reason: 'Stop never sends');- expect(key('swarm-voice-note'), findsNothing);- atRest();-- await tester.tap(key('swarm-send'));- await tester.pumpAndSettle();- expect(fake.commands.single['text'], 'một hai ba bốn');- expect(box(tester), isEmpty);- });-- testWidgets('what was typed stays in front, and an edit made while talking '- 'is kept in the box after Stop', (tester) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Nova:');- await tester.pump();-- await tapMic(tester);- await say(tester, 'ship it');- expect(box(tester), 'Nova: ship it');-- await tester.enterText(find.byType(TextField), 'Nova: ship it now');- await tester.pump();- await say(tester, 'ship it today');- expect(box(tester), 'Nova: ship it now today');-- await stop(tester);- await tester.pumpAndSettle();- expect(box(tester), 'Nova: ship it now today');- expect(fake.commands, isEmpty);- });-- testWidgets('Cancel takes back what the take said and keeps what was typed '- 'before it', (tester) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Nova:');- await tester.pump();-- await tapMic(tester);- await say(tester, 'ship it');- expect(box(tester), 'Nova: ship it');-- await cancel(tester);- await tester.pumpAndSettle();- expect(box(tester), 'Nova:');- expect(dictation.cancels, 1, reason: 'the microphone released');- expect(dictation.listening, isFalse);- expect(fake.commands, isEmpty);- expect(key('swarm-voice-note'), findsNothing);- atRest();- });-- testWidgets('Cancel keeps an edit made during the take and drops only the '- 'words said after it', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'ship it');- await tester.enterText(find.byType(TextField), 'ship it now');- await tester.pump();- await say(tester, 'ship it today');- expect(box(tester), 'ship it now today');-- await cancel(tester);- await tester.pumpAndSettle();- expect(box(tester), 'ship it now');- expect(fake.commands, isEmpty);- });-- testWidgets('Cancel right after an edit, with no word since, keeps the box '- 'as the person left it', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'ship it');- await tester.enterText(find.byType(TextField), 'ship it now!');- await tester.pump();-- await cancel(tester);- await tester.pumpAndSettle();- expect(box(tester), 'ship it now!');- expect(fake.commands, isEmpty);- });-- testWidgets('Cancel while the recognizer is stopping drops what the take '- 'said', (tester) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Draft');- await tester.pump();- await tapMic(tester);- await say(tester, 'gửi đi');-- dictation.stopsSilently = true;- await stop(tester);- expect(key('swarm-voice-busy'), findsOneWidget);- await cancel(tester);- await tester.pumpAndSettle();-- expect(box(tester), 'Draft');- expect(dictation.cancels, 1);- expect(fake.commands, isEmpty);- atRest();- });-- testWidgets("only the row's Send during a take: no composer Send, +, Tag "- 'or X; they come back after Stop', (tester) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Draft');- await tester.pump();- expect(key('swarm-field-clear'), findsOneWidget);-- await tapMic(tester);- expect(key('swarm-send'), findsNothing);- expect(key('swarm-attach'), findsNothing);- expect(key('swarm-tag'), findsNothing);- expect(key('swarm-mic'), findsNothing);- expect(key('swarm-field-clear'), findsNothing);- expect(key('swarm-voice-cancel'), findsOneWidget);- expect(key('swarm-voice-wave'), findsOneWidget);- expect(key('swarm-voice-stop'), findsOneWidget);- expect(key('swarm-voice-send'), findsOneWidget);-- await say(tester, 'ship it');- await stop(tester);- await tester.pumpAndSettle();- atRest();- expect(key('swarm-voice-send'), findsNothing);- expect(key('swarm-attach'), findsOneWidget);- expect(key('swarm-tag'), findsOneWidget);- expect(key('swarm-field-clear'), findsOneWidget);- expect(- tester.widget<SwarmRoundButton>(key('swarm-send')).onTap,- isNotNull,- reason: 'Send is live again',- );- });-- // Tony, 11-01: Send right of stop ends the take and posts the box, the last- // words included. With the recognizer the words are in the box as they are- // said: Send is off while it is empty.- group("the voice row's Send", () {- Future<void> rowSend(WidgetTester tester) async {- await tester.tap(key('swarm-voice-send'));- await frames(tester);- }-- SwarmRoundButton sendButton(WidgetTester tester) =>- tester.widget<SwarmRoundButton>(key('swarm-voice-send'));-- testWidgets('off while the box is empty, on once it has words', (- tester,- ) async {- await pump(tester);- await tapMic(tester);- expect(sendButton(tester).onTap, isNull, reason: 'empty box');- await rowSend(tester);- expect(dictation.stops, 0, reason: 'an off Send ends nothing');- expect(dictation.listening, isTrue);-- await say(tester, 'ship');- expect(sendButton(tester).onTap, isNotNull);-- await tester.enterText(find.byType(TextField), ' ');- await tester.pump();- expect(sendButton(tester).onTap, isNull, reason: 'only a space');- expect(fake.commands, isEmpty);- });-- testWidgets('one tap ends the take and posts the box with its last '- 'words, once', (tester) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Nova:');- await tester.pump();- await tapMic(tester);- await say(tester, 'ship the');-- dictation.lastWords = 'ship the fix today';- await rowSend(tester);- await tester.pumpAndSettle();-- expect(dictation.stops, 1);- expect(fake.commands.single['text'], 'Nova: ship the fix today');- expect(box(tester), isEmpty);- expect(key('swarm-voice-note'), findsNothing);- atRest();- });-- testWidgets('waits for the last words no longer than about two seconds, '- 'then posts what the box holds', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'ship it');-- dictation.stopsSilently = true;- await rowSend(tester);- expect(key('swarm-voice-sending'), findsOneWidget);- await tester.pump(const Duration(milliseconds: 1900));- expect(fake.commands, isEmpty, reason: 'still waiting for its words');-- await tester.pump(const Duration(milliseconds: 200));- await tester.pumpAndSettle();- expect(fake.commands.single['text'], 'ship it');- expect(dictation.cancels, 1, reason: 'the microphone released');- atRest();- });-- testWidgets('a double tap posts once', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'just once');-- dictation.stopsSilently = true;- final at = tester.getCenter(key('swarm-voice-send'));- for (var i = 0; i < 2; i++) {- await tester.tapAt(at);- await tester.pump(const Duration(milliseconds: 40));- }- expect(dictation.stops, 1);- dictation.end(SwarmDictationEnd.done);- await tester.pump();- await tester.tapAt(at);- await tester.pumpAndSettle();-- expect(fake.commands, hasLength(1));- expect(fake.commands.single['text'], 'just once');- });-- testWidgets('while it ends the row takes no tap: cancel does not take '- 'the words back', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'keep these');-- dictation.stopsSilently = true;- await rowSend(tester);- expect(sendButton(tester).onTap, isNull);- await cancel(tester);- expect(box(tester), 'keep these');-- dictation.end(SwarmDictationEnd.done);- await tester.pumpAndSettle();- expect(fake.commands.single['text'], 'keep these');- });-- testWidgets('a box emptied before the take ended posts nothing and says '- 'one line', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'oops');-- dictation.stopsSilently = true;- await rowSend(tester);- await tester.enterText(find.byType(TextField), '');- await tester.pump();- dictation.end(SwarmDictationEnd.done);- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(find.text(SwarmVoice.nothingSent), findsOneWidget);- });-- testWidgets('the app leaving while the take ends posts nothing; the words '- 'stay in the box', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'not yet');-- dictation.stopsSilently = true;- await rowSend(tester);- await lifecycle(tester, [AppLifecycleState.inactive]);- await lifecycle(tester, [AppLifecycleState.resumed]);- dictation.end(SwarmDictationEnd.done);- await tester.pump(const Duration(seconds: 3));- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(box(tester), 'not yet');- expect(find.text(SwarmVoice.nothingSent), findsNothing);- });-- testWidgets('signed out while the take ends: nothing is posted', (- tester,- ) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'after sign out');-- dictation.stopsSilently = true;- await rowSend(tester);- account.value = null;- await frames(tester);- dictation.end(SwarmDictationEnd.done);- await tester.pump(const Duration(seconds: 3));- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- });- });-- testWidgets('the @ picker hides during a take and comes back after Cancel', (- tester,- ) async {- await pump(tester);- await tester.enterText(find.byType(TextField), '@');- await tester.pump();- expect(key('swarm-mention-picker'), findsOneWidget);-- await tapMic(tester);- expect(key('swarm-mention-picker'), findsNothing);-- await cancel(tester);- await tester.pumpAndSettle();- expect(box(tester), '@');- expect(key('swarm-mention-picker'), findsOneWidget);- });-- // Lead, review of #2056 at a43760af3: an edit, then the recognizer revising- // what it heard into fewer words.- testWidgets('an edit, then a shorter revision of what was said, drops no '- 'spoken word', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'một hai');- expect(box(tester), 'một hai');-- await tester.enterText(find.byType(TextField), 'một hai,');- await tester.pump();- await say(tester, '12');- await say(tester, '123 bốn');- expect(box(tester), 'một hai, 123 bốn');-- await stop(tester);- await tester.pumpAndSettle();- expect(box(tester), 'một hai, 123 bốn');- expect(fake.commands, isEmpty);- });-- // Tony, 10-29: "listening continues until I tap".- testWidgets('listening goes on through a silence of nearly two minutes '- 'until Stop, which leaves what was said in the box', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'xong rồi');-- for (var i = 0; i < 11; i++) {- await tester.pump(const Duration(seconds: 10));- }- await tester.pump(const Duration(seconds: 9));- expect(dictation.stops, 0);- expect(dictation.listening, isTrue);- expect(key('swarm-voice-stop'), findsOneWidget);- expect(fake.commands, isEmpty);-- await stop(tester);- await tester.pumpAndSettle();- expect(box(tester), 'xong rồi');- expect(fake.commands, isEmpty, reason: 'only Send sends');- });-- testWidgets('talking with pauses under two minutes never stops it', (- tester,- ) async {- await pump(tester);- await tapMic(tester);-- var said = 'một';- await say(tester, said);- for (var i = 0; i < 5; i++) {- await tester.pump(const Duration(seconds: 115));- said = '$said hai';- await say(tester, said);- }- expect(dictation.listening, isTrue, reason: 'ten minutes in');- expect(dictation.stops + dictation.cancels, 0);-- await stop(tester);- await tester.pumpAndSettle();- expect(box(tester), said);- expect(fake.commands, isEmpty);- });-- // Lead, review of #2056 at a43760af3 (privacy): the microphone is not held- // open for nobody.- testWidgets('two minutes without a new word stop listening and release the '- 'mic; the words stay, for Send or another take', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'gần xong');- await tester.pump(const Duration(seconds: 60));- // The same words said again by the recognizer are not new ones.- await say(tester, 'gần xong');-- await tester.pump(const Duration(seconds: 59));- expect(dictation.listening, isTrue);- await tester.pump(const Duration(seconds: 1));- await frames(tester);- expect(dictation.listening, isFalse);- expect(dictation.cancels, 1, reason: 'the microphone released at once');- atRest();- expect(box(tester), 'gần xong');- expect(fake.commands, isEmpty);-- await tester.tap(key('swarm-send'));- await tester.pumpAndSettle();- expect(fake.commands.single['text'], 'gần xong');-- await tapMic(tester);- expect(dictation.listening, isTrue, reason: 'a new take');- expect(dictation.locales, hasLength(2));- });-- testWidgets('two minutes with nothing said at all stop listening too', (- tester,- ) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Draft');- await tester.pump();- await tapMic(tester);-- await tester.pump(const Duration(minutes: 2));- await frames(tester);- expect(dictation.listening, isFalse);- expect(dictation.cancels, 1);- expect(box(tester), 'Draft');- expect(fake.commands, isEmpty);- });-- testWidgets('stopping by itself says so quietly for three seconds; the '- 'words stay in the box', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'almost done');- expect(find.text('Stopped listening'), findsNothing);-- await tester.pump(const Duration(minutes: 2));- await frames(tester);- expect(dictation.listening, isFalse);- expect(- tester.widget<Text>(key('swarm-voice-stopped')).data,- 'Stopped listening',- );- expect(box(tester), 'almost done');- expect(key('swarm-voice-note'), findsNothing, reason: 'nothing went wrong');-- await tester.pump(const Duration(milliseconds: 2900));- expect(find.text('Stopped listening'), findsOneWidget);- await tester.pump(const Duration(milliseconds: 100));- expect(find.text('Stopped listening'), findsNothing);- expect(box(tester), 'almost done');- expect(fake.commands, isEmpty);- });-- testWidgets('Stop ends no take with Stopped listening, and a new take '- 'takes it away', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'Ship it');- await stop(tester);- await tester.pumpAndSettle();- expect(find.text('Stopped listening'), findsNothing, reason: 'stopped');-- await tapMic(tester);- await tester.pump(const Duration(minutes: 2));- await frames(tester);- expect(find.text('Stopped listening'), findsOneWidget);- await tapMic(tester);- expect(find.text('Stopped listening'), findsNothing);- });-- testWidgets('Stop with nothing said sends nothing', (tester) async {- await pump(tester);- await tapMic(tester);- await stop(tester);- await tester.pumpAndSettle();-- expect(dictation.stops, 1);- expect(box(tester), isEmpty);- expect(fake.commands, isEmpty);- });-- testWidgets('Stop with nothing said leaves what was typed, unsent and with '- 'no notice', (tester) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Draft');- await tester.pump();-- await tapMic(tester);- await stop(tester);- await tester.pumpAndSettle();-- expect(box(tester), 'Draft');- expect(fake.commands, isEmpty);- expect(find.text("Didn't catch that · tap the mic again"), findsNothing);- });-- testWidgets('Stop with nothing said and an empty box says so', (- tester,- ) async {- await pump(tester);- await tapMic(tester);- await stop(tester);- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(find.text("Didn't catch that · tap the mic again"), findsOneWidget);- });-- testWidgets('a recognizer that never says it stopped is given up after two '- 'seconds, its words left in the box, unsent', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'gửi đi');-- dictation.stopsSilently = true;- await stop(tester);- await tester.pump(const Duration(milliseconds: 1900));- expect(- tester.widget<VoiceMicCore>(find.byType(VoiceMicCore)).face,- VoiceMicFace.busy,- reason: 'still waiting for its last words',- );- expect(key('swarm-voice-stop'), findsNothing);- expect(key('swarm-voice-cancel'), findsOneWidget);-- await tester.pump(const Duration(milliseconds: 200));- await frames(tester);- expect(find.byType(VoiceMicCore), findsNothing);- expect(dictation.cancels, 1);- await tester.pumpAndSettle();- expect(box(tester), 'gửi đi');- expect(fake.commands, isEmpty);- expect(key('swarm-voice-note'), findsNothing);- atRest();- });-- testWidgets('Stop after the box was emptied leaves it empty and sends '- 'nothing, though files wait to go', (tester) async {- final documents = pickSwarmFiles, copies = swarmPickerCopyFolders;- addTearDown(() {- pickSwarmFiles = documents;- swarmPickerCopyFolders = copies;- });- swarmPickerCopyFolders = () async => const [];- pickSwarmFiles = () async => [- XFile.fromData(- Uint8List(12),- path: '/picker-copies/crash.log',- length: 12,- ),- ];- await pump(tester);- await tester.tap(key('swarm-attach'));- await tester.pumpAndSettle();- await tester.tap(key('swarm-attach-files'));- await tester.pumpAndSettle();-- await tapMic(tester);- await say(tester, 'nhầm rồi');- await tester.enterText(find.byType(TextField), '');- await tester.pump();- dictation.lastWords = 'nhầm rồi';- await stop(tester);- await tester.pumpAndSettle();-- expect(box(tester), isEmpty);- expect(uploads, isEmpty);- expect(fake.commands, isEmpty);- expect(key('swarm-outgoing-crash.log'), findsOneWidget);- expect(- tester.widget<SwarmRoundButton>(key('swarm-send')).onTap,- isNotNull,- reason: 'the files can still go, by Send',- );- });-- testWidgets('listening that cannot go on stops, its words left unsent', (- tester,- ) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'một phút rồi');-- dictation.end(SwarmDictationEnd.done);- await frames(tester);-- expect(box(tester), 'một phút rồi');- expect(fake.commands, isEmpty);- expect(find.byType(VoiceMicCore), findsNothing);- });-- testWidgets('a recognizer that fails says so and keeps what it heard', (- tester,- ) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'một');-- dictation.end(SwarmDictationEnd.failed);- await frames(tester);-- expect(box(tester), 'một');- expect(- find.text("Couldn't transcribe · tap the mic again"),- findsOneWidget,- );- expect(fake.commands, isEmpty);- });-- testWidgets('Stop on a recognizer that fails as it stops keeps the box, '- 'says nothing and sends nothing', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'gửi');-- dictation.stopEnd = SwarmDictationEnd.failed;- await stop(tester);- await tester.pumpAndSettle();-- expect(box(tester), 'gửi');- expect(fake.commands, isEmpty);- expect(find.text("Couldn't transcribe · tap the mic again"), findsNothing);- });-- testWidgets('Stop keeps what was said though the last session heard '- 'nothing', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'xin chào');-- dictation.stopEnd = SwarmDictationEnd.noSpeech;- await stop(tester);- await tester.pumpAndSettle();-- expect(box(tester), 'xin chào');- expect(fake.commands, isEmpty);- expect(find.text("Didn't catch that · tap the mic again"), findsNothing);- });-- testWidgets('nothing said before the recognizer gives up is said so', (- tester,- ) async {- await pump(tester);- await tapMic(tester);-- dictation.end(SwarmDictationEnd.noSpeech);- await frames(tester);-- expect(find.text("Didn't catch that · tap the mic again"), findsOneWidget);- });-- testWidgets('the wave follows how loud the recognizer hears', (tester) async {- await pump(tester);- await tapMic(tester);-- dictation.loud(0.8);- await tester.pump();-- expect(- tester.widget<SwarmVoiceWave>(key('swarm-voice-wave')).level!.value,- 0.8,- );- });-- /// The wave's bars as it paints them now, on a 200 x 44 strip: each one's- /// height.- List<double> waveBars(WidgetTester tester) {- final painter = tester- .widget<CustomPaint>(- find.descendant(- of: key('swarm-voice-wave'),- matching: find.byType(CustomPaint),- ),- )- .painter!;- final canvas = _BarCanvas();- painter.paint(canvas, const Size(200, 44));- return canvas.heights;- }-- double total(List<double> bars) => bars.fold(0, (sum, bar) => sum + bar);-- /// Half a second of the wave's frames.- Future<void> wave(WidgetTester tester) async {- for (var i = 0; i < 10; i++) {- await tester.pump(const Duration(milliseconds: 50));- }- }-- // Lead, 10-29: "a live wave animation driven by the mic level".- testWidgets('the wave rises as the voice does', (tester) async {- await pump(tester);- await tapMic(tester);- dictation.loud(0);- await wave(tester);- final quiet = total(waveBars(tester));-- dictation.loud(0.8);- await wave(tester);- expect(total(waveBars(tester)), greaterThan(quiet + 40));- expect(waveBars(tester).every((h) => h >= 3 && h <= 24), isTrue);- });-- testWidgets('the wave is not frozen in silence: it keeps moving', (- tester,- ) async {- await pump(tester);- await tapMic(tester);- dictation.loud(0);- await wave(tester);- final before = waveBars(tester);-- await wave(tester);- final after = waveBars(tester);- expect(after, isNot(equals(before)), reason: 'the history scrolls');- expect(after.any((h) => h > 3), isTrue, reason: 'a breath, not a line');- });-- testWidgets('with Reduce Motion on, the wave holds still but follows the '- 'voice', (tester) async {- await pump(tester, reduceMotion: true);- await tapMic(tester);- dictation.loud(0);- await tester.pump();- final quiet = waveBars(tester);-- await wave(tester);- expect(waveBars(tester), equals(quiet), reason: 'nothing scrolls');-- dictation.loud(0.8);- await tester.pump();- expect(total(waveBars(tester)), greaterThan(total(quiet) + 40));- });-- testWidgets('the long-press language is the one listened in', (tester) async {- voiceLanguageStore.value = 'ja';- await pump(tester);-- await tapMic(tester);-- expect(dictation.locales, ['ja-JP']);- });-- testWidgets('speech recognition refused: the mic records, Stop transcribes '- 'into the box unsent, and it is not asked again', (tester) async {- dictation.answer = SwarmDictationAccess.denied;- await pump(tester);- stt.replies.addAll(['ghi âm', 'lần nữa']);-- await tapMic(tester);- expect(dictation.locales, isEmpty);- expect(recorder.recording, isTrue);- await stop(tester);- await tester.pumpAndSettle();- expect(box(tester), 'ghi âm');- expect(fake.commands, isEmpty);-- await tapMic(tester);- expect(dictation.asks, 1);- expect(recorder.recording, isTrue);- });-- testWidgets('a language the recognizer lacks records and transcribes '- 'instead', (tester) async {- dictation.hasLocale = false;- await pump(tester);-- await tapMic(tester);-- expect(dictation.locales, hasLength(1));- expect(recorder.recording, isTrue);- });-- testWidgets('Cancel while the permission prompt is up calls the take off', (- tester,- ) async {- dictation.pendingAccess = Completer();- await pump(tester);-- await tapMic(tester);- expect(key('swarm-voice-cancel'), findsOneWidget);- expect(key('swarm-voice-stop'), findsNothing, reason: 'still opening');- await cancel(tester);- dictation.pendingAccess!.complete(SwarmDictationAccess.allowed);- await frames(tester);-- expect(dictation.locales, isEmpty);- expect(recorder.starts, 0);- atRest();- });-- testWidgets('the permission prompt over the app keeps the take it asks for', (- tester,- ) async {- addTearDown(- () => tester.binding.handleAppLifecycleStateChanged(- AppLifecycleState.resumed,- ),- );- dictation.pendingAccess = Completer();- await pump(tester);-- await tapMic(tester);- await lifecycle(tester, [AppLifecycleState.inactive]);- dictation.pendingAccess!.complete(SwarmDictationAccess.allowed);- await lifecycle(tester, [AppLifecycleState.resumed]);-- expect(dictation.listening, isTrue);- await say(tester, 'được');- expect(box(tester), 'được');- });-- testWidgets('Control Center or the app going away stops listening; the '- 'words already shown stay', (tester) async {- addTearDown(- () => tester.binding.handleAppLifecycleStateChanged(- AppLifecycleState.resumed,- ),- );- await pump(tester);- await tapMic(tester);- await say(tester, 'trước');-- await lifecycle(tester, [AppLifecycleState.inactive]);-- expect(dictation.cancels, 1);- expect(dictation.listening, isFalse);- await lifecycle(tester, [AppLifecycleState.resumed]);- await tester.pump(const Duration(seconds: 5));- expect(box(tester), 'trước');- expect(fake.commands, isEmpty);- });-- testWidgets('what a take called off still says lands nowhere, even in the '- 'next take', (tester) async {- addTearDown(- () => tester.binding.handleAppLifecycleStateChanged(- AppLifecycleState.resumed,- ),- );- await pump(tester);- await tapMic(tester);- await say(tester, 'trước');- final first = dictation.heard.single;- await lifecycle(tester, [- AppLifecycleState.inactive,- AppLifecycleState.resumed,- ]);- await tapMic(tester);- await say(tester, 'sau');-- first('trước nữa');- await frames(tester);-- expect(box(tester), 'trước sau');- });-- testWidgets('covering the chat stops listening', (tester) async {- await pump(tester);- await tapMic(tester);- await say(tester, 'đang nói');-- final context = tester.element(find.byType(SwarmConversationPage));- unawaited(- Navigator.of(context).push(- MaterialPageRoute<void>(- builder: (_) => const Scaffold(body: Text('Other screen')),- ),- ),- );- await tester.pump();- await tester.pump(const Duration(seconds: 1));-- expect(dictation.cancels, 1);- expect(dictation.listening, isFalse);- expect(fake.commands, isEmpty);- });-- testWidgets('leaving the chat stops listening', (tester) async {- await pump(tester);- await tapMic(tester);-- await tester.pumpWidget(const SizedBox());-- expect(dictation.cancels, 1);- });-}--/// The phone's own recognizer, spoken for by the test: [say] is what it-/// heard so far, [end] how it stopped by itself; [stop] delivers [lastWords]-/// and ends with [stopEnd] — or, [stopsSilently], never says it has.-class FakeDictation implements SwarmDictation {- SwarmDictationAccess answer = SwarmDictationAccess.allowed;- Completer<SwarmDictationAccess>? pendingAccess;- bool hasLocale = true;- String? lastWords;- SwarmDictationEnd stopEnd = SwarmDictationEnd.done;- bool stopsSilently = false;-- final List<String> locales = [];-- /// Each take's `onWords`, called off or not.- final List<void Function(String)> heard = [];- int asks = 0, stops = 0, cancels = 0;-- void Function(String)? _words;- void Function(double)? _level;- void Function(SwarmDictationEnd)? _end;-- bool get listening => _end != null;-- void say(String words) => _words?.call(words);-- void loud(double level) => _level?.call(level);-- void end(SwarmDictationEnd end) {- final ended = _end;- _forget();- ended?.call(end);- }-- @override- Future<SwarmDictationAccess> access() {- asks++;- return pendingAccess?.future ?? Future.value(answer);- }-- @override- Future<bool> start(- String locale, {- required void Function(String words) onWords,- required void Function(double level) onLevel,- required void Function(SwarmDictationEnd end) onEnd,- }) async {- locales.add(locale);- if (!hasLocale) return false;- heard.add(onWords);- _words = onWords;- _level = onLevel;- _end = onEnd;- return true;- }-- @override- Future<void> stop() async {- stops++;- if (stopsSilently) return;- if (lastWords case final words?) say(words);- end(stopEnd);- }-- @override- Future<void> cancel() async {- cancels++;- _forget();- }-- void _forget() {- _words = null;- _level = null;- _end = null;- }-}--/// A canvas that keeps the height of each bar painted on it, and nothing else.-class _BarCanvas implements Canvas {- final List<double> heights = [];-- @override- void drawRRect(RRect rrect, Paint paint) => heights.add(rrect.height);-- @override- dynamic noSuchMethod(Invocation invocation) => null;-}diff --git a/mobile/test/swarm/swarm_chat_voice_test.dart b/mobile/test/swarm/swarm_chat_voice_test.dartindex 788abf5a4..133827506 100644--- a/mobile/test/swarm/swarm_chat_voice_test.dart+++ b/mobile/test/swarm/swarm_chat_voice_test.dart@@ -4,38 +4,30 @@ import 'package:flutter/foundation.dart';import 'package:flutter/material.dart';import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/notify/system_notices.dart';-import 'package:harness_mobile/phone/voice_input_controller.dart';-import 'package:harness_mobile/phone/voice_mic_face.dart';import 'package:harness_mobile/phone/voice_recorder.dart';import 'package:harness_mobile/settings/experimental_features.dart';import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;import 'package:harness_mobile/swarm/swarm_conversation_page.dart';-import 'package:harness_mobile/swarm/swarm_request.dart';import 'package:harness_mobile/swarm/swarm_session.dart';-import 'package:harness_mobile/swarm/swarm_voice.dart';import '../voice_fakes.dart';import 'swarm_fixture.dart';-/// Talking instead of typing in a Swarm chat (Tony, 10-28), where the phone-/// has no speech recognizer of its own to use: the terminal's mic in the-/// composer records, and what the backend hears lands in the message box as-/// the take ends. While a take runs the toolbar is cancel, wave, stop and-/// Send: stop leaves the words in the box and sends nothing — sending is a-/// tap on Send, the row's or the composer's (Tony, 11-01) — and nothing but-/// stop, Send, cancel or its 5-minute limit ends a take (Tony, 10-29). A long press on the mic picks the language.-/// With the phone's recognizer: `swarm_chat_dictation_test`.+/// A Swarm chat's voice messages (task #2584, Dee: "just send the full+/// audio"): the composer's mic records, and its row is X, the red dot and the+/// time, the wave and Send. Send posts the take as audio; nothing goes into+/// the message box.void main() {const messages = '/api/super-tabs/$tab/conversations/g1/messages';const commands = '/api/super-tabs/$tab/commands';late FakeSwarm fake;late _MeteredRecorder recorder;- late FakeTranscriber stt;+ late FakeUploads uploads;setUp(() {recorder = _MeteredRecorder();- stt = FakeTranscriber();+ uploads = FakeUploads();fake = FakeSwarm();fake.routes['/api/super-tabs/list'] = (_) => {'tabs': [@@ -76,13 +68,20 @@ void main() {'next': null,};fake.routes[commands] = (_) => {'result': <String, dynamic>{}};+ fake.routes['/api/super-tabs/$tab/attachments/'] = (_) => {+ 'voice': {+ 'durationMs': 2000,+ 'waveform': List.filled(48, 9),+ 'transcribed': true,+ },+ };});/// Who is signed in, in the last [pump].late ValueNotifier<String?> account;- /// Chat g1 on screen, its composer's mic heard through [recorder] and- /// [stt] — or with no voice input at all.+ /// Chat g1 on screen, its composer's mic recording into [recorder] — or+ /// with no mic at all.Future<SwarmHost> pump(WidgetTester tester, {bool voice = true}) async {final features = ExperimentalFeaturesStore(storage: MemoryKeyValueStore())..value = {PhoneExperiment.swarm};@@ -91,15 +90,10 @@ void main() {accountId: () => account.value,account: account,request: fake.request,+ upload: uploads.upload,notices: SilentSystemNotices(),features: features,- voice: voice- ? () => VoiceInputController(- transcriber: stt.call,- recorder: recorder,- language: ValueNotifier('en'),- )- : null,+ voice: voice ? () => recorder : null,)..foreground = false;addTearDown(host.dispose);await tester.pumpWidget(@@ -128,1044 +122,129 @@ void main() {await frames(tester);}- /// The voice row's stop: the take ends with its words left in the box.- Future<void> stop(WidgetTester tester) async {- await tester.tap(key('swarm-voice-stop'));- await frames(tester);- }-- /// The voice row's cancel: the take is thrown away.- Future<void> cancel(WidgetTester tester) async {- await tester.tap(key('swarm-voice-cancel'));- await frames(tester);- }+ TextField field(WidgetTester tester) =>+ tester.widget<TextField>(find.byType(TextField));- Future<void> send(WidgetTester tester) async {- await tester.tap(key('swarm-send'));- await tester.pumpAndSettle();- }-- String box(WidgetTester tester) =>- tester.widget<TextField>(find.byType(TextField)).controller!.text;-- testWidgets('stop ends the take with what it heard in the box and sends '- 'nothing; Send then posts it', (tester) async {+ testWidgets('a tap records: X, the red dot and its time, the wave and '+ 'Send, and nothing in the box', (tester) async {await pump(tester);- stt.replies.add('ship the fix today');-+ await tester.enterText(find.byType(TextField), 'typed first');await tapMic(tester);expect(recorder.recording, isTrue);- expect(key('swarm-voice-stop'), findsOneWidget);- expect(- find.byType(SwarmVoiceWave),- findsOneWidget,- reason: 'the wave, moving with the voice',- );-- await stop(tester);- await tester.pumpAndSettle();- expect(recorder.recording, isFalse);- expect(box(tester), 'ship the fix today');- expect(fake.commands, isEmpty, reason: 'voice never sends by itself');-- await send(tester);- expect(fake.commands.single['text'], 'ship the fix today');- expect(box(tester), isEmpty);- });-- testWidgets('what was typed stays in front of what is said; stop leaves '- 'both in the box, unsent', (tester) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Nova:');- await tester.pump();- stt.replies.add('ship the fix');-- await tapMic(tester);- await stop(tester);- await tester.pumpAndSettle();-- expect(box(tester), 'Nova: ship the fix');- expect(fake.commands, isEmpty);- await send(tester);- expect(fake.commands.single['text'], 'Nova: ship the fix');- });-- testWidgets('a stop with nothing heard sends nothing and says so', (- tester,- ) async {- await pump(tester);-- await tapMic(tester);- await stop(tester);- await tester.pumpAndSettle();-- expect(recorder.recording, isFalse);- expect(box(tester), isEmpty);- expect(fake.commands, isEmpty);- expect(find.text("Didn't catch that · tap the mic again"), findsOneWidget);- });-- // Tony, 10-29: "listening continues until I tap".- testWidgets('a pause after speaking does not end the take: it records until '- 'stop, which leaves the words in the box', (tester) async {- await pump(tester);- stt.replies.add('looks good');-- await tapMic(tester);- recorder.meter.value = 0.8;- await tester.pump(const Duration(milliseconds: 500));- recorder.meter.value = 0.02;- await tester.pump(const Duration(seconds: 30));- expect(recorder.recording, isTrue);- expect(fake.commands, isEmpty);-- await stop(tester);- await tester.pumpAndSettle();- expect(box(tester), 'looks good');- expect(fake.commands, isEmpty);- });-- testWidgets('the take runs on through quiet before anything is said, and '- 'through a short pause', (tester) async {- await pump(tester);-- await tapMic(tester);- recorder.meter.value = 0.02;- await tester.pump(const Duration(seconds: 10));- expect(recorder.recording, isTrue, reason: 'nothing said yet');-- recorder.meter.value = 0.8;- await tester.pump(const Duration(milliseconds: 500));- recorder.meter.value = 0.02;- await tester.pump(const Duration(seconds: 2));- recorder.meter.value = 0.8;- await tester.pump(const Duration(milliseconds: 500));- recorder.meter.value = 0.02;- await tester.pump(const Duration(seconds: 2));- expect(recorder.recording, isTrue, reason: 'talking again restarts it');- });-- testWidgets('while the microphone opens the row is cancel and a flat '- 'wave, no stop; cancel calls the take off', (tester) async {- recorder.pendingPermission = Completer();- await pump(tester);-- await tapMic(tester);- expect(key('swarm-voice-cancel'), findsOneWidget);- expect(key('swarm-voice-wave'), findsOneWidget);- expect(tester.widget<SwarmVoiceWave>(key('swarm-voice-wave')).flat, isTrue);- expect(key('swarm-voice-stop'), findsNothing);+ for (final part in [+ 'swarm-voice-cancel',+ 'swarm-voice-dot',+ 'swarm-voice-time',+ 'swarm-voice-wave',+ 'swarm-voice-send',+ ]) {+ expect(key(part), findsOneWidget, reason: part);+ }expect(key('swarm-mic'), findsNothing);-- await cancel(tester);- recorder.pendingPermission!.complete(true);- await frames(tester);-- expect(recorder.starts, 0);- expect(key('swarm-voice-cancel'), findsNothing, reason: 'back at rest');- expect(key('swarm-mic'), findsOneWidget);+ expect(find.text('0:00'), findsOneWidget);+ await tester.pump(const Duration(seconds: 3));+ expect(find.text('0:03'), findsOneWidget);+ expect(field(tester).controller!.text, 'typed first');});- testWidgets('a refused microphone says so, and records nothing', (- tester,- ) async {- recorder.permitted = false;+ testWidgets("the row's Send posts the take as a voice message and leaves "+ 'the box as it was', (tester) async {await pump(tester);-+ await tester.enterText(find.byType(TextField), 'typed first');await tapMic(tester);-- expect(recorder.starts, 0);- expect(find.text('No mic access · allow it in Settings'), findsOneWidget);- });-- testWidgets('a take that is not heard says so, and leaves the box as it '- 'was', (tester) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Draft');- await tester.pump();- stt.fails = true;-- await tapMic(tester);- await stop(tester);-- expect(box(tester), 'Draft');- expect(- find.text("Couldn't transcribe · tap the mic again"),- findsOneWidget,- );- expect(fake.commands, isEmpty);- });-- testWidgets('a long press on the mic opens the voice language list', (- tester,- ) async {- await pump(tester);-- await tester.longPress(key('swarm-mic'));- await tester.pumpAndSettle();-- expect(find.text('Voice input language'), findsOneWidget);- expect(find.text('Tiếng Việt'), findsOneWidget);- expect(recorder.starts, 0, reason: 'a long press records nothing');- // Put away without choosing: a choice writes the real preferences file.- });-- testWidgets('VoiceOver can talk, stop, cancel and choose the language, '- 'each a one-word label while the mic is open', (tester) async {- final semantics = tester.ensureSemantics();- await pump(tester);- stt.replies.add('on my way');-- expect(- tester.getSemantics(key('swarm-mic')),- matchesSemantics(- label: 'Talk',- isButton: true,- hasEnabledState: true,- isEnabled: true,- hasTapAction: true,- hasLongPressAction: true,- onLongPressHint: 'Choose language',- ),- );-- tester.semantics.tap(find.semantics.byLabel('Talk'));+ await tester.tap(key('swarm-voice-send'));await frames(tester);- expect(recorder.recording, isTrue);- for (final (control, label) in [- ('swarm-voice-cancel', 'Cancel'),- ('swarm-voice-stop', 'Stop'),- ]) {- expect(- tester.getSemantics(key(control)),- matchesSemantics(- label: label,- isButton: true,- hasEnabledState: true,- isEnabled: true,- hasTapAction: true,- ),- );- }-- tester.semantics.tap(find.semantics.byLabel('Stop'));- await tester.pumpAndSettle();- expect(box(tester), 'on my way');- expect(fake.commands, isEmpty);-- tester.semantics.tap(find.semantics.byLabel('Talk'));+ await tester.pump(const Duration(milliseconds: 100));await frames(tester);- tester.semantics.tap(find.semantics.byLabel('Cancel'));- await tester.pumpAndSettle();- expect(recorder.recording, isFalse);- expect(box(tester), 'on my way', reason: 'what was there before stays');- tester.semantics.longPress(find.semantics.byLabel('Talk'));- await tester.pumpAndSettle();- expect(find.text('Voice input language'), findsOneWidget);- semantics.dispose();+ expect(uploads.asked.single.mime, 'audio/wav');+ final sent = fake.commands.single;+ expect(sent['text'], '');+ expect(sent['attachmentIds'], ['id-Voice message.wav']);+ expect(key('swarm-mic'), findsOneWidget, reason: 'back at rest');+ expect(field(tester).controller!.text, 'typed first');});- testWidgets('leaving the chat mid-take closes the microphone', (- tester,- ) async {+ testWidgets('X throws the take away and sends nothing', (tester) async {await pump(tester);await tapMic(tester);- expect(recorder.recording, isTrue);-- await tester.pumpWidget(const SizedBox());-- expect(recorder.cancels, 1);- expect(recorder.recording, isFalse);- });-- /// Another screen over the chat, its push done.- Future<void> cover(WidgetTester tester) async {- final context = tester.element(find.byType(SwarmConversationPage));- unawaited(- Navigator.of(context).push(- MaterialPageRoute<void>(- builder: (_) => const Scaffold(body: Text('Other screen')),- ),- ),- );- await tester.pump();- await tester.pump(const Duration(seconds: 1));- }-- Future<void> uncover(WidgetTester tester) async {- final context = tester.element(find.text('Other screen'));- Navigator.of(context).pop();- await tester.pump();- await tester.pump(const Duration(seconds: 1));- }-- Future<void> lifecycle(- WidgetTester tester,- List<AppLifecycleState> states,- ) async {- for (final state in states) {- tester.binding.handleAppLifecycleStateChanged(state);- }+ await tester.tap(key('swarm-voice-cancel'));await frames(tester);- }-- // Review of #2014 at ad9dee05d: the reviewer's probe, as it was.- testWidgets('covering a chat cancels its recording', (tester) async {- await pump(tester);- await tapMic(tester);- expect(recorder.recording, isTrue);-- await cover(tester);-- expect(- recorder.recording,- isFalse,- reason: 'A covered chat must not keep recording speech',- );- });-- testWidgets('a take covered after speaking is thrown away, not '- 'transcribed', (tester) async {- await pump(tester);- stt.replies.add('said behind another screen');- await tapMic(tester);- recorder.meter.value = 0.8;- await tester.pump(const Duration(milliseconds: 500));- recorder.meter.value = 0.02;-- await cover(tester);- await tester.pump(const Duration(seconds: 5));-expect(recorder.cancels, 1);- expect(recorder.stops, 0, reason: 'a take is only stopped to be sent');- expect(stt.calls, isEmpty);- await uncover(tester);- expect(box(tester), isEmpty);- });-- testWidgets('the app going to the background mid-take throws the take '- 'away; a system prompt over it does not', (tester) async {- addTearDown(- () => tester.binding.handleAppLifecycleStateChanged(- AppLifecycleState.resumed,- ),- );- await pump(tester);- await tapMic(tester);-- // The microphone permission prompt and Control Center leave the app- // inactive, still in front.- await lifecycle(tester, [AppLifecycleState.inactive]);- expect(recorder.recording, isTrue);-- await lifecycle(tester, [- AppLifecycleState.hidden,- AppLifecycleState.paused,- ]);- expect(recorder.recording, isFalse);- expect(recorder.cancels, 1);-- await lifecycle(tester, [- AppLifecycleState.hidden,- AppLifecycleState.inactive,- AppLifecycleState.resumed,- ]);- await tester.pump(const Duration(seconds: 5));- expect(stt.calls, isEmpty);- expect(box(tester), isEmpty);- expect(find.byType(VoiceMicCore), findsNothing, reason: 'back at rest');- });-- // Review of #2014 at 42c8eac9c: the reviewer's probe, in its order.- testWidgets('quiet under a system prompt ends nothing; the take goes on '- 'once the app is back, until stop', (tester) async {- addTearDown(- () => tester.binding.handleAppLifecycleStateChanged(- AppLifecycleState.resumed,- ),- );- await pump(tester);- stt.replies.add('said before Control Center');- await tapMic(tester);- recorder.meter.value = 0.8;- await tester.pump(const Duration(milliseconds: 500));-- await lifecycle(tester, [AppLifecycleState.inactive]);- recorder.meter.value = 0.02;- await tester.pump(const Duration(seconds: 4));- expect(stt.calls, isEmpty, reason: 'Control Center over the chat');- expect(recorder.recording, isTrue, reason: 'the take is kept');-- await lifecycle(tester, [AppLifecycleState.resumed]);- await tester.pump(const Duration(seconds: 10));- expect(recorder.recording, isTrue);- await stop(tester);- await tester.pumpAndSettle();- expect(box(tester), 'said before Control Center');expect(fake.commands, isEmpty);+ expect(uploads.asked, isEmpty);+ expect(key('swarm-mic'), findsOneWidget);});- testWidgets('a take kept under a system prompt goes if the app then goes '- 'away', (tester) async {- addTearDown(- () => tester.binding.handleAppLifecycleStateChanged(- AppLifecycleState.resumed,- ),- );+ testWidgets('a take kept as the phone locks, Send ready', (tester) async {await pump(tester);await tapMic(tester);- recorder.meter.value = 0.8;- await tester.pump(const Duration(milliseconds: 500));- recorder.meter.value = 0.02;- await tester.pump(const Duration(seconds: 2));-- await lifecycle(tester, [AppLifecycleState.inactive]);- await tester.pump(const Duration(seconds: 4));- expect(stt.calls, isEmpty);- expect(recorder.recording, isTrue);-- await lifecycle(tester, [- AppLifecycleState.hidden,- AppLifecycleState.paused,- ]);- await lifecycle(tester, [- AppLifecycleState.hidden,+ // The phone locked, and back: through inactive each way, as iOS goes.+ for (final state in [AppLifecycleState.inactive,- AppLifecycleState.resumed,- ]);- await tester.pump(const Duration(seconds: 5));- expect(recorder.cancels, 1);- expect(stt.calls, isEmpty);- expect(box(tester), isEmpty);- });-- testWidgets('a take that reaches its limit puts its words in the box', (- tester,- ) async {- await pump(tester);- stt.replies.add('five minutes of it');- await tapMic(tester);-- await tester.pump(const Duration(minutes: 5));- await frames(tester);-- expect(recorder.recording, isFalse);- expect(box(tester), 'five minutes of it');- expect(fake.commands, isEmpty);- });-- testWidgets('a take that reaches its limit under a system prompt is held '- 'until the app is back', (tester) async {- addTearDown(- () => tester.binding.handleAppLifecycleStateChanged(- AppLifecycleState.resumed,- ),- );- await pump(tester);- stt.replies.add('held over the limit');- await tapMic(tester);-- await lifecycle(tester, [AppLifecycleState.inactive]);- await tester.pump(const Duration(minutes: 5, seconds: 10));- expect(stt.calls, isEmpty);- expect(recorder.recording, isTrue);-- await lifecycle(tester, [AppLifecycleState.resumed]);- await frames(tester);- expect(recorder.recording, isFalse);- expect(box(tester), 'held over the limit');- });-- // Review of #2014 at 61a2bce93: the reviewer's probe, with stop ending the- // take now (Tony, 11-01) — a system prompt comes up while the microphone is- // still closing.- testWidgets('a take whose microphone closes under a system prompt is sent '- 'to be transcribed only once the app is back, into the box', (- tester,- ) async {- addTearDown(- () => tester.binding.handleAppLifecycleStateChanged(- AppLifecycleState.resumed,- ),- );- await pump(tester);- stt.replies.add('said just before Control Center');- recorder.heldStop = Completer();- await tapMic(tester);- recorder.meter.value = 0.8;- await tester.pump(const Duration(milliseconds: 500));- await stop(tester);- expect(recorder.stops, 1);- expect(stt.calls, isEmpty);-- await lifecycle(tester, [AppLifecycleState.inactive]);- recorder.heldStop!.complete(recorder.captured);- await frames(tester);- await tester.pump(const Duration(seconds: 10));- expect(- stt.calls,- isEmpty,- reason: 'checked again once the microphone has closed',- );- expect(fake.commands, isEmpty);-- await lifecycle(tester, [AppLifecycleState.resumed]);- await tester.pumpAndSettle();- expect(stt.calls, hasLength(1));- expect(box(tester), 'said just before Control Center');- expect(fake.commands, isEmpty);- });-- testWidgets('a take that reaches its limit as a system prompt comes up '- 'waits for the app too', (tester) async {- addTearDown(- () => tester.binding.handleAppLifecycleStateChanged(- AppLifecycleState.resumed,- ),- );- await pump(tester);- stt.replies.add('five minutes, then Control Center');- recorder.heldStop = Completer();- await tapMic(tester);- await tester.pump(const Duration(minutes: 5));- await frames(tester);- expect(recorder.stops, 1);-- await lifecycle(tester, [AppLifecycleState.inactive]);- recorder.heldStop!.complete(recorder.captured);- await frames(tester);- expect(stt.calls, isEmpty);-- await lifecycle(tester, [AppLifecycleState.resumed]);- expect(box(tester), 'five minutes, then Control Center');- });-- testWidgets('a take held under a system prompt is thrown away if the app '- 'goes away', (tester) async {- addTearDown(- () => tester.binding.handleAppLifecycleStateChanged(- AppLifecycleState.resumed,- ),- );- await pump(tester);- stt.replies.add('never heard');- recorder.heldStop = Completer();- await tapMic(tester);- await stop(tester);- expect(recorder.stops, 1);-- await lifecycle(tester, [AppLifecycleState.inactive]);- recorder.heldStop!.complete(recorder.captured);- await frames(tester);- await lifecycle(tester, [- AppLifecycleState.hidden,- AppLifecycleState.paused,- ]);- await lifecycle(tester, [AppLifecycleState.hidden,AppLifecycleState.inactive,AppLifecycleState.resumed,- ]);- await tester.pump(const Duration(seconds: 5));-- expect(stt.calls, isEmpty);- expect(box(tester), isEmpty);- expect(find.byType(VoiceMicCore), findsNothing, reason: 'back at rest');- });-- testWidgets('a take whose microphone closes after the chat was covered is '- 'not transcribed', (tester) async {- await pump(tester);- stt.replies.add('said behind another screen');- recorder.heldStop = Completer();- await tapMic(tester);- await stop(tester);-- await cover(tester);- recorder.heldStop!.complete(recorder.captured);- await frames(tester);- await uncover(tester);-- expect(stt.calls, isEmpty);- expect(box(tester), isEmpty);- });-- testWidgets('back on the chat, the mic starts a fresh take', (tester) async {- await pump(tester);- stt.replies.add('second try');- await tapMic(tester);- await cover(tester);- await uncover(tester);-- await tapMic(tester);- expect(recorder.starts, 2);- expect(recorder.recording, isTrue);- await stop(tester);- await tester.pumpAndSettle();- expect(box(tester), 'second try');- expect(fake.commands, isEmpty);- });-- testWidgets('a microphone allowed after the chat was covered never opens', (- tester,- ) async {- recorder.pendingPermission = Completer();- await pump(tester);- await tapMic(tester);-- await cover(tester);- recorder.pendingPermission!.complete(true);- await frames(tester);-- expect(recorder.starts, 0);- });-- testWidgets('words transcribed after the chat was covered are dropped', (- tester,- ) async {- await pump(tester);- stt.pending = Completer();- await tapMic(tester);- await stop(tester);- expect(stt.calls, hasLength(1), reason: 'sent while the chat was seen');-- await cover(tester);- stt.pending!.complete('late words');- await frames(tester);- await uncover(tester);-- expect(box(tester), isEmpty);- expect(fake.commands, isEmpty);- });-- testWidgets('during a take the toolbar is cancel, wave, stop and the '- "row's Send: no +, Tag, mic, X or @ picker; all come back after stop", (- tester,- ) async {- await pump(tester);- await tester.enterText(find.byType(TextField), '@');- await tester.pump();- expect(key('swarm-mention-picker'), findsOneWidget);- expect(key('swarm-field-clear'), findsOneWidget);- stt.replies.add('Nova');-- await tapMic(tester);- for (final gone in [- 'swarm-send',- 'swarm-attach',- 'swarm-tag',- 'swarm-mic',- 'swarm-field-clear',- 'swarm-mention-picker',- ]) {- expect(key(gone), findsNothing, reason: '$gone during a take');- }- for (final shown in [- 'swarm-voice-cancel',- 'swarm-voice-wave',- 'swarm-voice-stop',- 'swarm-voice-send',- ]) {- expect(key(shown), findsOneWidget, reason: '$shown during a take');- }- // Right of stop, as the composer's Send is right of the mic.- expect(- tester.getCenter(key('swarm-voice-send')).dx,- greaterThan(tester.getCenter(key('swarm-voice-stop')).dx),- );-- await stop(tester);- await tester.pumpAndSettle();- expect(box(tester), '@ Nova');- expect(key('swarm-voice-send'), findsNothing);- for (final back in [- 'swarm-send',- 'swarm-attach',- 'swarm-tag',- 'swarm-mic',- 'swarm-field-clear',]) {- expect(key(back), findsOneWidget, reason: '$back after stop');+ tester.binding.handleAppLifecycleStateChanged(state);+ await frames(tester);}- expect(key('swarm-voice-stop'), findsNothing);- expect(fake.commands, isEmpty);- });-- testWidgets('cancel throws the take away and leaves what was typed before', (- tester,- ) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Draft');- await tester.pump();- stt.replies.add('never wanted');-- await tapMic(tester);- recorder.meter.value = 0.8;- await tester.pump(const Duration(milliseconds: 500));- await cancel(tester);- await tester.pumpAndSettle();-expect(recorder.recording, isFalse);- expect(recorder.cancels, 1);- expect(stt.calls, isEmpty, reason: 'a cancelled take is not transcribed');- expect(box(tester), 'Draft');+ expect(key('swarm-voice-send'), findsOneWidget);expect(fake.commands, isEmpty);- expect(key('swarm-mic'), findsOneWidget);});- testWidgets('while the words are being transcribed the row is cancel and '- 'the busy face, no stop; cancel then puts nothing in the box', (+ testWidgets('a voice message draws its bubble, its words behind →A', (tester,) async {+ fake.routes[messages] = (_) => {+ 'messages': [+ {+ 'id': 'm1',+ 'conversationId': 'g1',+ 'text': 'Ship the parser today.',+ 'sequence': 1,+ 'author': {'kind': 'person', 'id': 'dee', 'name': 'dee'},+ 'attachments': [+ {+ 'id': 'clip',+ 'name': 'Voice message.wav',+ 'mime': 'audio/wav',+ 'size': 2592044,+ 'url': 'https://cdn.example.test/super-tabs/$tab/a/clip.wav',+ 'voice': {+ 'durationMs': 81000,+ 'waveform': List.filled(48, 9),+ 'transcribed': true,+ },+ },+ ],+ 'reactions': <Object>[],+ 'createdAt': DateTime.utc(2026, 11, 4, 2).toIso8601String(),+ },+ ],+ 'next': null,+ };await pump(tester);- await tester.enterText(find.byType(TextField), 'Draft');+ expect(key('swarm-voice-bubble-clip'), findsOneWidget);+ expect(find.text('1:21'), findsOneWidget);+ expect(find.text('Ship the parser today.'), findsNothing);+ expect(find.text('Voice message.wav'), findsNothing, reason: 'no chip');+ await tester.tap(key('swarm-voice-words-toggle'));await tester.pump();- stt.pending = Completer();-- await tapMic(tester);- await stop(tester);- expect(stt.calls, hasLength(1));- expect(key('swarm-voice-busy'), findsOneWidget);- expect(key('swarm-voice-stop'), findsNothing);- expect(key('swarm-send'), findsNothing);- expect(- tester.widget<SwarmRoundButton>(key('swarm-voice-send')).onTap,- isNull,- reason: 'after stop the words go to the box, not out',- );-- await cancel(tester);- stt.pending!.complete('late words');- await frames(tester);- await tester.pumpAndSettle();-- expect(box(tester), 'Draft');- expect(fake.commands, isEmpty);- expect(key('swarm-voice-busy'), findsNothing);- expect(key('swarm-mic'), findsOneWidget);+ expect(find.text('Ship the parser today.'), findsOneWidget);});- testWidgets('with no voice input the composer has no mic', (tester) async {+ testWidgets('no mic where none can be heard', (tester) async {await pump(tester, voice: false);-expect(key('swarm-mic'), findsNothing);- expect(key('swarm-send'), findsOneWidget);- });-- // Tony, 11-01: Send right of stop ends the take and posts it, last words- // and all. A recording's words reach the box only once transcribed, so the- // row's Send is live from the first moment of the take.- group("the voice row's Send", () {- Future<void> rowSend(WidgetTester tester) async {- await tester.tap(key('swarm-voice-send'));- await frames(tester);- }-- SwarmRoundButton sendButton(WidgetTester tester) =>- tester.widget<SwarmRoundButton>(key('swarm-voice-send'));-- testWidgets('live while recording with an empty box; one tap stops, '- 'transcribes and posts the words once', (tester) async {- await pump(tester);- stt.pending = Completer<String>();-- await tapMic(tester);- expect(box(tester), isEmpty);- expect(sendButton(tester).onTap, isNotNull, reason: 'empty box, live');-- await rowSend(tester);- expect(recorder.recording, isFalse, reason: 'the take ended');- expect(stt.calls, hasLength(1));- expect(key('swarm-voice-sending'), findsOneWidget);- expect(fake.commands, isEmpty, reason: 'not before the words are back');-- stt.pending!.complete('ship the fix today');- await tester.pumpAndSettle();- expect(fake.commands.single['text'], 'ship the fix today');- expect(box(tester), isEmpty);- expect(key('swarm-voice-wave'), findsNothing);- expect(key('swarm-mic'), findsOneWidget);- expect(key('swarm-voice-note'), findsNothing);- });-- testWidgets('posts what was typed, then what was said', (tester) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Nova:');- await tester.pump();- stt.replies.add('ship the fix');-- await tapMic(tester);- await rowSend(tester);- await tester.pumpAndSettle();-- expect(fake.commands.single['text'], 'Nova: ship the fix');- expect(box(tester), isEmpty);- });-- testWidgets('while it sends the row takes no tap: a second Send, stop or '- 'cancel changes nothing, and the post goes once', (tester) async {- await pump(tester);- stt.pending = Completer<String>();-- await tapMic(tester);- await rowSend(tester);- expect(sendButton(tester).onTap, isNull);- expect(key('swarm-voice-stop'), findsNothing);- await rowSend(tester);- await cancel(tester);- expect(stt.calls, hasLength(1));- expect(- key('swarm-voice-sending'),- findsOneWidget,- reason: 'not called off',- );-- stt.pending!.complete('once');- await tester.pumpAndSettle();- expect(fake.commands, hasLength(1));- expect(fake.commands.single['text'], 'once');- });-- testWidgets('a double tap posts once', (tester) async {- await pump(tester);- stt.pending = Completer<String>();-- await tapMic(tester);- // Where Send is, whatever is there by then: the row's Send, or the- // composer's once the row has gone.- final at = tester.getCenter(key('swarm-voice-send'));- for (var i = 0; i < 2; i++) {- await tester.tapAt(at);- await tester.pump(const Duration(milliseconds: 40));- }- stt.pending!.complete('just once');- await tester.pump();- await tester.tapAt(at);- await tester.pumpAndSettle();-- expect(fake.commands, hasLength(1));- expect(fake.commands.single['text'], 'just once');- });-- testWidgets('nothing heard posts nothing, keeps what was typed, and says '- 'one line', (tester) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Draft');- await tester.pump();- stt.replies.add('');-- await tapMic(tester);- await rowSend(tester);- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(box(tester), 'Draft');- expect(key('swarm-voice-note'), findsOneWidget);- expect(find.text(SwarmVoice.nothingSent), findsOneWidget);- expect(find.text("Didn't catch that · tap the mic again"), findsNothing);- expect(key('swarm-mic'), findsOneWidget);- });-- testWidgets('a transcription that fails posts nothing and says the same '- 'line', (tester) async {- await pump(tester);- await tester.enterText(find.byType(TextField), 'Draft');- await tester.pump();- stt.fails = true;-- await tapMic(tester);- await rowSend(tester);- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(box(tester), 'Draft');- expect(find.text(SwarmVoice.nothingSent), findsOneWidget);- });-- testWidgets('a silent take posts nothing and says the same line', (- tester,- ) async {- await pump(tester);- recorder.captured = (- wav: Uint8List.fromList([0, 0]),- length: const Duration(seconds: 2),- peak: 0,- );-- await tapMic(tester);- await rowSend(tester);- await tester.pumpAndSettle();-- expect(stt.calls, isEmpty, reason: 'silence is not uploaded');- expect(fake.commands, isEmpty);- expect(find.text(SwarmVoice.nothingSent), findsOneWidget);- });-- testWidgets('off while the microphone opens', (tester) async {- await pump(tester);- recorder.pendingPermission = Completer<bool>();-- await tapMic(tester);- expect(sendButton(tester).onTap, isNull);- recorder.pendingPermission!.complete(true);- await frames(tester);- expect(sendButton(tester).onTap, isNotNull);- });-- testWidgets('a chat covered while its Send transcribes posts nothing, and '- 'says nothing', (tester) async {- await pump(tester);- stt.pending = Completer<String>();- await tapMic(tester);- await rowSend(tester);-- final navigator = tester.state<NavigatorState>(find.byType(Navigator));- unawaited(- navigator.push(- MaterialPageRoute<void>(builder: (_) => const SizedBox()),- ),- );- await tester.pumpAndSettle();- stt.pending!.complete('behind your back');- await tester.pumpAndSettle();- navigator.pop();- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(box(tester), isEmpty);- expect(find.text(SwarmVoice.nothingSent), findsNothing);- expect(key('swarm-mic'), findsOneWidget);- });-- testWidgets('signed out while its Send transcribes: nothing is posted', (- tester,- ) async {- await pump(tester);- stt.pending = Completer<String>();- await tapMic(tester);- await rowSend(tester);-- account.value = null;- await frames(tester);- stt.pending!.complete('after sign out');- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(find.text('after sign out'), findsNothing);- });- });-- // The composer goes with the right to post, and its mic with it: a take- // must not go on recording, or land its words, once the person can no- // longer post here (as a thread's reply box already guards).- group('access lost mid-take', () {- const refused = SuperTabFailure('FORBIDDEN', 'Not yours.', statusCode: 403);-- /// The Swarm read again: refused, or with the person a [role].- Future<void> reread(- WidgetTester tester,- SwarmHost host, {- bool lost = false,- String? role,- }) async {- if (lost) {- fake.routes['/api/super-tabs/$tab/changes'] = (_) => throw refused;- fake.routes['/api/super-tabs/$tab/parts'] = (_) => throw refused;- } else {- final parts = fake.routes['/api/super-tabs/$tab/parts']!;- fake.routes['/api/super-tabs/$tab/parts'] = (body) => {- ...(parts(body) as Map<String, dynamic>),- 'members': [part(me, 2, person(me, role: role!))],- };- fake.routes['/api/super-tabs/$tab/changes'] = (_) => {'reload': true};- }- await host.session!.inbox.reader(tab)!.refresh();- await frames(tester);- }-- testWidgets('signed out: the microphone closes, nothing is transcribed '- 'or sent', (tester) async {- await pump(tester);- stt.replies.add('after sign out');- await tapMic(tester);- expect(recorder.recording, isTrue);-- account.value = null;- await frames(tester);-- expect(recorder.recording, isFalse);- expect(stt.calls, isEmpty);- expect(fake.commands, isEmpty);- });-- testWidgets('made read-only: the take goes with the composer', (- tester,- ) async {- final host = await pump(tester);- await tapMic(tester);- expect(recorder.recording, isTrue);-- await reread(tester, host, role: 'viewer');-- expect(key('swarm-read-only'), findsOneWidget);- expect(key('swarm-mic'), findsNothing);- expect(recorder.recording, isFalse);- expect(stt.calls, isEmpty);- expect(fake.commands, isEmpty);- });-- testWidgets('the Swarm lost: the microphone closes', (tester) async {- final host = await pump(tester);- await tapMic(tester);- expect(recorder.recording, isTrue);-- await reread(tester, host, lost: true);-- expect(recorder.recording, isFalse);- expect(stt.calls, isEmpty);- });-- testWidgets('words transcribed after the person was made read-only land '- 'nowhere: not sent, and not kept in the draft', (tester) async {- final host = await pump(tester);- stt.pending = Completer<String>();- await tapMic(tester);- recorder.meter.value = 0.8;- await tester.pump(const Duration(milliseconds: 500));- await stop(tester);- expect(stt.calls, hasLength(1), reason: 'the take is being transcribed');-- await reread(tester, host, role: 'viewer');- stt.pending!.complete('too late');- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(find.text('too late'), findsNothing);-- // Nor kept in the draft for when the person may post again.- await reread(tester, host, role: 'owner');- await tester.pumpAndSettle();- expect(key('swarm-mic'), findsOneWidget);- expect(box(tester), isEmpty);- });});}-/// A microphone that also says how loud it is, for the mic's bars.+/// A microphone that also says how loud it is: what the wave follows.class _MeteredRecorder extends FakeVoiceRecorder implements VoiceLevelMeter {final meter = ValueNotifier<double>(0);- /// Holds [stop] open until the test completes it: a microphone that takes- /// its time to close, as a real one does.- Completer<VoiceTake?>? heldStop;-@overrideValueListenable<double> get level => meter;-- @override- Future<VoiceTake?> stop() async {- final take = await super.stop();- return heldStop?.future ?? take;- }}diff --git a/mobile/test/swarm/swarm_composer_test.dart b/mobile/test/swarm/swarm_composer_test.dartindex 0256ddbbc..6258192ed 100644--- a/mobile/test/swarm/swarm_composer_test.dart+++ b/mobile/test/swarm/swarm_composer_test.dart@@ -5,7 +5,6 @@ import 'package:flutter/rendering.dart' show RenderParagraph;import 'package:flutter/services.dart';import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/notify/system_notices.dart';-import 'package:harness_mobile/phone/voice_input_controller.dart';import 'package:harness_mobile/settings/experimental_features.dart';import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;import 'package:harness_mobile/shared/theme/app_theme.dart' show AppPalette;@@ -177,9 +176,8 @@ void main() {/// takes the keyboard's inset out for its body).late ValueNotifier<String?> account;- /// The microphone and the transcriber of the last [pump] with a voice.+ /// The microphone of the last [pump] with a voice.late FakeVoiceRecorder recorder;- late FakeTranscriber stt;Future<SwarmHost> pump(WidgetTester tester,@@ -192,7 +190,6 @@ void main() {..value = {PhoneExperiment.swarm};account = ValueNotifier<String?>(me);recorder = FakeVoiceRecorder();- stt = FakeTranscriber();final host = SwarmHost(accountId: () => account.value,account: account,@@ -201,13 +198,7 @@ void main() {notices: SilentSystemNotices(),features: features,readMarks: MemoryKeyValueStore(),- voice: voice- ? () => VoiceInputController(- transcriber: stt.call,- recorder: recorder,- language: ValueNotifier('en'),- )- : null,+ voice: voice ? () => recorder : null,)..foreground = false;addTearDown(host.dispose);const page = SwarmConversationPage(swarmId: tab, conversationId: 'g1');@@ -1087,9 +1078,9 @@ void main() {}testWidgets('signed out mid-take: the microphone closes, and nothing is '- 'transcribed or kept', (tester) async {- await pump(tester, backend([message('m1', 1)]), voice: true);- stt.replies.add('after sign out');+ 'sent', (tester) async {+ final fake = backend([message('m1', 1)]);+ await pump(tester, fake, voice: true);await tester.tap(key('swarm-mic'));await tester.pump();await tester.pump();@@ -1101,8 +1092,7 @@ void main() {}expect(recorder.recording, isFalse);- expect(stt.calls, isEmpty);- expect(find.text('after sign out'), findsNothing);+ expect(fake.commands, isEmpty);});testWidgets('made read-only mid-take: the take goes with the composer', (@@ -1120,27 +1110,24 @@ void main() {expect(key('swarm-composer-card'), findsNothing);expect(key('swarm-read-only'), findsOneWidget);expect(recorder.recording, isFalse);- expect(stt.calls, isEmpty);+ expect(fake.commands, isEmpty);});- testWidgets('words transcribed after the Swarm was lost land nowhere', (+ testWidgets('a take held when the Swarm was lost is never sent', (tester,) async {final fake = backend([message('m1', 1)]);final host = await pump(tester, fake, voice: true);- stt.pending = Completer<String>();await tester.tap(key('swarm-mic'));await tester.pump();await tester.pump();- await tester.tap(key('swarm-voice-stop'));- await tester.pump();- expect(stt.calls, hasLength(1), reason: 'the take is being transcribed');+ expect(recorder.recording, isTrue);await reread(tester, host, fake, lost: true);- stt.pending!.complete('too late');await tester.pumpAndSettle();- expect(find.text('too late'), findsNothing);+ expect(recorder.recording, isFalse);+ expect(key('swarm-voice-send'), findsNothing);expect(fake.commands, isEmpty);});diff --git a/mobile/test/swarm/swarm_dictation_test.dart b/mobile/test/swarm/swarm_dictation_test.dartdeleted file mode 100644index b89df4963..000000000--- a/mobile/test/swarm/swarm_dictation_test.dart+++ /dev/null@@ -1,537 +0,0 @@-import 'dart:math' as math;--import 'package:flutter/services.dart';-import 'package:flutter_test/flutter_test.dart';-import 'package:harness_mobile/swarm/swarm_dictation.dart';--/// The phone's own recognizer as the chat reaches it: `harness/dictation`,-/// its native side answered here, and what it says routed to the take that-/// is listening — never to one already ended.-void main() {- TestWidgetsFlutterBinding.ensureInitialized();- const channel = MethodChannel('harness/dictation');- const codec = StandardMethodCodec();- final messenger =- TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;-- /// What the native side was asked, and how it answers each method.- late List<MethodCall> calls;- late Map<String, Object? Function(MethodCall)> answers;-- setUp(() {- calls = [];- answers = {- 'start': (_) => true,- 'resume': (_) => true,- 'access': (_) => 'allowed',- };- messenger.setMockMethodCallHandler(channel, (call) async {- calls.add(call);- return answers[call.method]?.call(call);- });- });- tearDown(() => messenger.setMockMethodCallHandler(channel, null));-- /// The native side saying [method] about [take].- Future<void> native(String method, int take, [Map<String, Object?>? more]) =>- messenger.handlePlatformMessage(- channel.name,- codec.encodeMethodCall(MethodCall(method, {'take': take, ...?more})),- (_) {},- );-- /// What a take heard, in order.- late List<Object> heard;-- Future<bool> start(SwarmChannelDictation dictation, [String? locale]) =>- dictation.start(- locale ?? 'vi-VN',- onWords: (words) => heard.add(words),- onLevel: (level) => heard.add(level),- onEnd: (end) => heard.add(end),- );-- setUp(() => heard = []);-- int take(MethodCall call) => (call.arguments as Map)['take'] as int;-- group('access', () {- test('says what the person allowed', () async {- final dictation = SwarmChannelDictation();- for (final (answer, access) in [- ('allowed', SwarmDictationAccess.allowed),- ('noMic', SwarmDictationAccess.noMic),- ('denied', SwarmDictationAccess.denied),- ('restricted', SwarmDictationAccess.denied),- ]) {- answers['access'] = (_) => answer;- expect(await dictation.access(), access, reason: answer);- }- });-- test('a native side that fails, or is not there, is a refusal', () async {- final dictation = SwarmChannelDictation();- answers['access'] = (_) => throw PlatformException(code: 'boom');- expect(await dictation.access(), SwarmDictationAccess.denied);-- messenger.setMockMethodCallHandler(channel, null);- expect(await dictation.access(), SwarmDictationAccess.denied);- });- });-- group('a take', () {- test('listens in its locale, and hears its words, level and end', () async {- final dictation = SwarmChannelDictation();- expect(await start(dictation, 'ja-JP'), isTrue);- final started = calls.single;- expect(started.method, 'start');- expect((started.arguments as Map)['locale'], 'ja-JP');- final id = take(started);-- await native('words', id, {'text': 'こんにちは'});- await native('level', id, {'rms': 0.1});- await native('end', id, {'end': 'done'});- await native('words', id, {'text': 'after the end'});-- expect(heard, ['こんにちは', isA<double>(), SwarmDictationEnd.done]);- });-- test(- 'ends as the native side says, and anything unknown is a failure',- () async {- final dictation = SwarmChannelDictation();- for (final (end, expected) in [- ('noSpeech', SwarmDictationEnd.noSpeech),- ('failed', SwarmDictationEnd.failed),- ('whatever', SwarmDictationEnd.failed),- ]) {- heard.clear();- calls.clear();- await start(dictation);- await native('end', take(calls.single), {'end': end});- expect(heard, [expected], reason: end);- }- },- );-- test('stop asks for this take, whose end then comes once', () async {- final dictation = SwarmChannelDictation();- await start(dictation);- final id = take(calls.single);-- await dictation.stop();- expect(calls.last.method, 'stop');- expect(take(calls.last), id);- await native('end', id, {'end': 'done'});- await native('end', id, {'end': 'failed'});-- expect(heard, [SwarmDictationEnd.done]);- });-- test('a stop that fails ends the take as failed', () async {- final dictation = SwarmChannelDictation();- answers['stop'] = (_) => throw PlatformException(code: 'boom');- await start(dictation);-- await dictation.stop();-- expect(heard, [SwarmDictationEnd.failed]);- });-- test('cancel asks for this take, and nothing of it comes after', () async {- final dictation = SwarmChannelDictation();- await start(dictation);- final id = take(calls.single);-- await dictation.cancel();- expect(calls.last.method, 'cancel');- expect(take(calls.last), id);- await native('words', id, {'text': 'late'});- await native('end', id, {'end': 'done'});-- expect(heard, isEmpty);- });-- test('a cancel that fails is still a cancel', () async {- final dictation = SwarmChannelDictation();- answers['cancel'] = (_) => throw PlatformException(code: 'boom');- await start(dictation);- final id = take(calls.single);-- await dictation.cancel();- await native('words', id, {'text': 'late'});-- expect(heard, isEmpty);- });-- test('an earlier take\'s words land nowhere in the next one', () async {- final dictation = SwarmChannelDictation();- await start(dictation);- final first = take(calls.single);- await dictation.cancel();- await start(dictation);- final second = take(calls.last);- expect(second, isNot(first));-- await native('words', first, {'text': 'old'});- await native('words', second, {'text': 'new'});-- expect(heard, ['new']);- });-- test(- 'without the locale, or failing to start, it does not listen',- () async {- final dictation = SwarmChannelDictation();- answers['start'] = (_) => false;- expect(await start(dictation), isFalse);- await native('words', take(calls.last), {'text': 'nothing'});-- answers['start'] = (_) => throw PlatformException(code: 'boom');- expect(await start(dictation), isFalse);- await native('words', take(calls.last), {'text': 'nothing'});-- expect(heard, isEmpty);- },- );-- test('what it says without a take, or malformed, is ignored', () async {- final dictation = SwarmChannelDictation();- await start(dictation);- final id = take(calls.single);-- await messenger.handlePlatformMessage(- channel.name,- codec.encodeMethodCall(const MethodCall('words', 'text')),- (_) {},- );- await native('words', id, {'text': 42});- await native('level', id, {'rms': 'loud'});-- expect(heard, isEmpty);- });- });-- group('the words of a take', () {- /// What [start] heard last: every word so far.- String said() => heard.whereType<String>().last;-- late SwarmChannelDictation dictation;- late int id;-- setUp(() async {- dictation = SwarmChannelDictation();- await start(dictation);- id = take(calls.single);- });-- /// The recognizer's latest best guess at what is being said: [from] and- /// [to] where its words begin and end in the take's audio (0 where it- /// does not say), [finished] when it says that guess is done.- Future<void> guess(- String text, {- double from = 0,- double to = 0,- bool finished = false,- }) => native('words', id, {- 'text': text,- 'final': finished,- 'from': from,- 'to': to,- });-- // Tony, 10-29: 'một hai ba bốn năm' came out as 'Một 2 12345'.- test('each guess replaces the last, however it is rewritten', () async {- await guess('1', from: 0.4, to: 0.8);- await guess('Một 2', from: 0.4, to: 1.2);- await guess('123', from: 0.4, to: 1.6);- await guess('1234', from: 0.4, to: 2.0);- await guess('12345', from: 0.4, to: 2.4);- expect(said(), '12345');- });-- test('each guess replaces the last where it says no times', () async {- await guess('1');- await guess('Một 2');- await guess('Một hai ba');- expect(said(), 'Một hai ba');- await guess('123');- expect(said(), '123');- });-- test('a guess with times after one without replaces it, unless that one '- 'was finished', () async {- await guess('1');- await guess('Một 2', from: 0.4, to: 1.2);- expect(said(), 'Một 2');-- await guess('Một hai.', finished: true);- await guess('Ba', from: 3.0, to: 3.3);- expect(said(), 'Một hai. Ba');- });-- test('a finished sentence, then new words after a pause, keeps the '- 'sentence exactly once', () async {- await guess('Hôm nay mình làm gì', from: 0.3, to: 2.0);- await guess('Hôm nay mình làm gì.', from: 0.3, to: 2.1, finished: true);- await guess('Ngày', from: 3.6, to: 3.9);- expect(said(), 'Hôm nay mình làm gì. Ngày');- await guess('Ngày mai', from: 3.6, to: 4.3);- expect(said(), 'Hôm nay mình làm gì. Ngày mai');- });-- test(- 'a finished sentence, then new words, where it says no times',- () async {- await guess('Hôm nay mình làm gì.', finished: true);- await guess('Ngày');- await guess('Ngày mai');- expect(said(), 'Hôm nay mình làm gì. Ngày mai');- },- );-- test('words that start after the last ended are new, though nothing '- 'said the last was finished', () async {- await guess('hôm nay mình làm', from: 0.3, to: 2.0);- await guess('gì', from: 3.4, to: 3.6);- expect(said(), 'hôm nay mình làm gì');- await guess('gì nữa', from: 3.4, to: 4.0);- expect(said(), 'hôm nay mình làm gì nữa');- });-- test('a guess said to be finished and then revised is replaced, not '- 'repeated', () async {- await guess('1', from: 0.4, to: 0.8, finished: true);- await guess('Một 2', from: 0.4, to: 1.2);- expect(said(), 'Một 2');- });-- test('a recognizer that says everything again after a finished sentence '- 'is not doubled', () async {- await guess('Hôm nay', from: 0.3, to: 1.0, finished: true);- await guess('Hôm nay mình', from: 0.3, to: 1.5);- expect(said(), 'Hôm nay mình');-- await guess('Hôm nay mình.', finished: true);- await guess('Hôm nay mình làm');- expect(said(), 'Hôm nay mình làm', reason: 'where it says no times');- });-- test('the last words, as the take stops, come once', () async {- await guess('một hai', from: 0.3, to: 1.0);- await guess('Một hai.', from: 0.3, to: 1.0, finished: true);- expect(said(), 'Một hai.');- });-- test('keeps nothing from one take for the next', () async {- await guess('Một hai ba.', from: 0.3, to: 1.5, finished: true);- await guess('bốn', from: 2.5, to: 2.8);- await dictation.cancel();-- await start(dictation);- id = take(calls.last);- await guess('năm', from: 0.3, to: 0.6);-- expect(said(), 'năm');- });- });-- test('the long-press language as the recognizer\'s locale', () {- expect(swarmDictationLocale('vi'), 'vi-VN');- expect(swarmDictationLocale('en'), 'en-US');- expect(swarmDictationLocale('es'), 'es-ES');- expect(swarmDictationLocale('fr'), 'fr-FR');- expect(swarmDictationLocale('ja'), 'ja-JP');- expect(swarmDictationLocale('it'), 'it-IT');- expect(- swarmDictationLocale('xx'),- 'vi-VN',- reason: 'Vietnamese by default',- );- });-- group('a take listens until it is stopped (Tony, 10-29)', () {- /// What [start] heard last: every word so far.- String said() => heard.whereType<String>().last;-- var now = DateTime(2026, 10, 29, 9);- late SwarmChannelDictation dictation;- late int id;-- setUp(() async {- now = DateTime(2026, 10, 29, 9);- dictation = SwarmChannelDictation(clock: () => now);- await start(dictation);- id = take(calls.single);- });-- Future<void> guess(- String text, {- double from = 0,- double to = 0,- bool finished = false,- }) => native('words', id, {- 'text': text,- 'final': finished,- 'from': from,- 'to': to,- });-- /// The recognizer ending its session by itself, the microphone still on.- Future<void> paused([String end = 'done']) =>- native('paused', id, {'end': end});-- List<MethodCall> resumes() =>- calls.where((call) => call.method == 'resume').toList();-- test('a session the recognizer ends by itself is resumed at once, its '- 'words kept', () async {- await guess('Hôm nay mình làm gì.', from: 0.3, to: 2.1, finished: true);- await paused();- expect(resumes().single.arguments, {'take': id});-- // A new session's times start again from nothing.- await guess('Ngày', from: 0.2, to: 0.5);- await guess('Ngày mai', from: 0.2, to: 0.9);- expect(said(), 'Hôm nay mình làm gì. Ngày mai');- expect(heard.whereType<SwarmDictationEnd>(), isEmpty);- });-- test('a session that ends on a guess it never finished keeps it', () async {- await guess('hôm nay mình', from: 0.3, to: 1.2);- await paused('noSpeech');- await guess('làm gì', from: 0.1, to: 0.6);- expect(said(), 'hôm nay mình làm gì');- });-- test('a long silence, session after session, keeps listening', () async {- await guess('một', from: 0.3, to: 0.6);- for (var i = 0; i < 6; i++) {- now = now.add(const Duration(seconds: 20));- await paused('noSpeech');- }- expect(resumes(), hasLength(6));- expect(heard.whereType<SwarmDictationEnd>(), isEmpty);- await guess('hai', from: 0.2, to: 0.5);- expect(said(), 'một hai');- });-- test('sessions that keep ending at once, hearing nothing, are given up: '- 'the take ends as failed, its words kept', () async {- await guess('một', from: 0.3, to: 0.6);- await paused();- await paused('failed');- now = now.add(const Duration(milliseconds: 900));- await paused('failed');-- expect(heard.whereType<SwarmDictationEnd>(), [SwarmDictationEnd.failed]);- expect(resumes(), hasLength(2));- expect(calls.last.method, 'cancel');- expect(said(), 'một');- await paused();- expect(resumes(), hasLength(2), reason: 'given up for good');- });-- test('words between sessions that end quickly keep it going', () async {- for (final word in ['một', 'hai', 'ba', 'bốn']) {- await guess(word, from: 0.1, to: 0.3);- await paused();- }- expect(heard.whereType<SwarmDictationEnd>(), isEmpty);- expect(said(), 'một hai ba bốn');- });-- test('a resume the phone refuses ends the take as failed', () async {- answers['resume'] = (_) => false;- await guess('một', from: 0.3, to: 0.6);- await paused();-- expect(heard.whereType<SwarmDictationEnd>(), [SwarmDictationEnd.failed]);- expect(calls.last.method, 'cancel');- });-- test('a resume that fails ends the take as failed', () async {- answers['resume'] = (_) => throw PlatformException(code: 'boom');- await paused();-- expect(heard.whereType<SwarmDictationEnd>(), [SwarmDictationEnd.failed]);- });-- test('a session ending as the take stops is not resumed', () async {- await guess('một hai', from: 0.3, to: 0.9);- await dictation.stop();- await paused();- await native('end', id, {'end': 'done'});-- expect(resumes(), isEmpty);- expect(heard.whereType<SwarmDictationEnd>(), [SwarmDictationEnd.done]);- expect(said(), 'một hai');- });-- test('an earlier take\'s session ending is not resumed', () async {- await dictation.cancel();- await start(dictation);- await paused();-- expect(resumes(), isEmpty);- });- });-- group('the level the wave follows (Tony, 10-29: "a live wave")', () {- late SwarmChannelDictation dictation;- late int id;-- setUp(() async {- dictation = SwarmChannelDictation();- await start(dictation);- id = take(calls.single);- });-- /// The microphone at [db] dBFS, [times] buffers of a tenth of a second.- Future<void> sound(double db, [int times = 1]) async {- for (var i = 0; i < times; i++) {- await native('level', id, {'rms': math.pow(10, db / 20)});- }- }-- double last() => heard.whereType<double>().last;-- test('a voice above the room fills the wave and the room alone leaves it '- 'low, however loud the microphone takes them', () async {- for (final (room, voice) in [(-70.0, -45.0), (-40.0, -15.0)]) {- await dictation.cancel();- await start(dictation);- id = take(calls.last);- await sound(room, 30);- expect(last(), lessThan(0.05), reason: 'the room at $room dB');- await sound(voice);- expect(last(), greaterThan(0.6), reason: 'a voice at $voice dB');- await sound(room + 2);- expect(last(), lessThan(0.05), reason: 'the room again');- }- });-- test(- 'the click the microphone opens on is forgotten within seconds',- () async {- await sound(-95);- await sound(-50, 35);- expect(last(), lessThan(0.05));- },- );-- test('a room that grows louder is followed', () async {- await sound(-60, 30);- await sound(-45, 35);- expect(last(), lessThan(0.05));- });-- test('near-digital silence never moves the wave', () async {- await sound(-100, 30);- await sound(-80);- expect(last(), 0);- await native('level', id, {'rms': 0});- expect(last(), 0);- });- });-}diff --git a/mobile/test/swarm/swarm_display_name_shots_test.dart b/mobile/test/swarm/swarm_display_name_shots_test.dartindex 71b650050..28b3e768c 100644--- a/mobile/test/swarm/swarm_display_name_shots_test.dart+++ b/mobile/test/swarm/swarm_display_name_shots_test.dart@@ -19,8 +19,8 @@ import '../voice_fakes.dart';import 'swarm_fixture.dart';/// Review screenshots of the name a person sets (plan-/// 2026-11-03-person-display-name, Task 6): You with a Name row above-/// Username, and a chat whose author and tag read by that name beside one+/// 2026-11-03-person-display-name, Task 6): You with its Profile row+/// (name and status) above Username, and a chat whose author and tag read by that name beside one/// who set none. On a 390-point/// phone in both phone themes, from made-up data only, to the folder/// `SWARM_DISPLAY_NAME_SHOTS` names.diff --git a/mobile/test/swarm/swarm_display_name_test.dart b/mobile/test/swarm/swarm_display_name_test.dartindex 16d2f3e63..ee4a73c27 100644--- a/mobile/test/swarm/swarm_display_name_test.dart+++ b/mobile/test/swarm/swarm_display_name_test.dart@@ -406,7 +406,7 @@ void main() {}Future<void> openProfile(WidgetTester tester) async {- await tester.tap(key('swarm-you-name'));+ await tester.tap(key('swarm-you-profile'));await tester.pumpAndSettle();expect(key('swarm-name-field'), findsOneWidget);}@@ -422,15 +422,17 @@ void main() {await settle(tester);}- testWidgets('with a name: You heads with it, a Name row above Username '- 'shows it with Change, and Username keeps the handle', (tester) async {+ testWidgets('with a name: You heads with it, the Profile row above '+ 'Username shows it with Change, and Username keeps the handle', (+ tester,+ ) async {await pump(tester, name: 'Minh Nguyen');expect(inside('swarm-you-me', find.text('Minh Nguyen')), findsOne);- expect(inside('swarm-you-name', find.text('Minh Nguyen')), findsOne);- expect(inside('swarm-you-name', find.text('Change')), findsOne);+ expect(inside('swarm-you-profile', find.text('Minh Nguyen')), findsOne);+ expect(inside('swarm-you-profile', find.text('Change')), findsOne);expect(inside('swarm-you-username', find.text('tony')), findsOne);expect(- tester.getTopLeft(key('swarm-you-name')).dy,+ tester.getTopLeft(key('swarm-you-profile')).dy,lessThan(tester.getTopLeft(key('swarm-you-username')).dy),);});@@ -441,7 +443,7 @@ void main() {await pump(tester);expect(inside('swarm-you-me', find.text('tony')), findsOne);expect(inside('swarm-you-username', find.text('tony')), findsOne);- expect(inside('swarm-you-name', find.text('Change')), findsOne);+ expect(inside('swarm-you-profile', find.text('Change')), findsOne);});testWidgets('the Name field sits above Username, and a name saves '@@ -470,7 +472,7 @@ void main() {await tester.tap(key('swarm-profile-close'));await tester.pumpAndSettle();expect(inside('swarm-you-me', find.text('Minh Nguyen')), findsOne);- expect(inside('swarm-you-name', find.text('Minh Nguyen')), findsOne);+ expect(inside('swarm-you-profile', find.text('Minh Nguyen')), findsOne);});testWidgets('an emptied field clears it with null, and the username 'diff --git a/mobile/test/swarm/swarm_link_previews_test.dart b/mobile/test/swarm/swarm_link_previews_test.dartindex 9ff6b8889..7d6373e19 100644--- a/mobile/test/swarm/swarm_link_previews_test.dart+++ b/mobile/test/swarm/swarm_link_previews_test.dart@@ -50,6 +50,25 @@ void main() {'createdAt': DateTime.utc(2026, 10, 27, 1, minute).toIso8601String(),};+ /// [m] as a voice message: a 1:21 recording whose words are its text.+ Map<String, dynamic> voiced(Map<String, dynamic> m) => {+ ...m,+ 'attachments': [+ {+ 'id': 'clip-${m['id']}',+ 'name': 'Voice message.wav',+ 'mime': 'audio/wav',+ 'size': 2592044,+ 'url': 'https://cdn.example.test/super-tabs/$tab/a/clip.wav',+ 'voice': {+ 'durationMs': 81000,+ 'waveform': List.filled(48, 9),+ 'transcribed': true,+ },+ },+ ],+ };+/// Swarm [tab] with chat g1 reading [page] as its latest messages, and/// chat g2, "Release notes"; with [other], the account also has Swarm/// tab-2, whose chat h1 is "Billing". [members] join the reader in [tab].@@ -378,6 +397,23 @@ void main() {await leave(tester);});+ testWidgets('a link to a voice message says Voice message and its length, '+ 'not its words', (tester) async {+ final fake = backend([+ message('m1', 1, author: me, text: 'See harness://swarm/$tab/g2/v1'),+ ]);+ fake.routes[one('g2', 'v1')] = (_) => {+ 'messages': [+ voiced(message('v1', 7, conversation: 'g2', text: 'Ship it today')),+ ],+ };+ await pump(tester, fake);++ expect(inPreview('$tab/g2/v1', 'Voice message (1:21)'), findsOneWidget);+ expect(find.textContaining('Ship it today'), findsNothing);+ await leave(tester);+ });+group('a reply whose original is not loaded', () {List<Map<String, dynamic>> page() => [for (var i = 10; i < 40; i++)@@ -451,6 +487,27 @@ void main() {await leave(tester);});+ testWidgets('quotes a voice message as Voice message and its length, not '+ 'its words', (tester) async {+ final fake = backend(page(), next: older);+ fake.routes[one('g1', 'm1')] = (_) => {+ 'messages': [+ voiced(message('m1', 1, author: 'agent-lead', text: 'Ship it today')),+ ],+ };+ await pump(tester, fake);++ expect(+ find.descendant(+ of: key('swarm-quote-m40'),+ matching: find.text('Voice message (1:21)'),+ ),+ findsOneWidget,+ );+ expect(find.textContaining('Ship it today'), findsNothing);+ await leave(tester);+ });+testWidgets('says it isn\'t loaded when that read fails, and asks again ''only a minute later', (tester) async {final fake = backend(page(), next: older);diff --git a/mobile/test/swarm/swarm_models_devices_test.dart b/mobile/test/swarm/swarm_models_devices_test.dartindex 6d031abb8..2784cf31a 100644--- a/mobile/test/swarm/swarm_models_devices_test.dart+++ b/mobile/test/swarm/swarm_models_devices_test.dart@@ -140,7 +140,7 @@ void main() {findsOneWidget,);expect(- y(tester, 'swarm-you-status'),+ y(tester, 'swarm-you-profile'),lessThan(y(tester, 'swarm-you-models')),);expect(diff --git a/mobile/test/swarm/swarm_new_message_shots_test.dart b/mobile/test/swarm/swarm_new_message_shots_test.dartindex 843548838..28d12fb73 100644--- a/mobile/test/swarm/swarm_new_message_shots_test.dart+++ b/mobile/test/swarm/swarm_new_message_shots_test.dart@@ -7,7 +7,6 @@ import 'package:flutter/rendering.dart';import 'package:flutter/services.dart';import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/notify/system_notices.dart';-import 'package:harness_mobile/phone/voice_input_controller.dart';import 'package:harness_mobile/settings/experimental_features.dart';import 'package:harness_mobile/shared/theme/app_theme.dart';import 'package:harness_mobile/shared/theme/color_palette.dart';@@ -161,11 +160,7 @@ void main() {notices: SilentSystemNotices(),features: features,readMarks: MemoryKeyValueStore(),- voice: () => VoiceInputController(- transcriber: FakeTranscriber().call,- recorder: FakeVoiceRecorder(),- language: ValueNotifier('en'),- ),+ voice: () => FakeVoiceRecorder(),)..foreground = false;final boundary = GlobalKey();await tester.pumpWidget(diff --git a/mobile/test/swarm/swarm_new_message_test.dart b/mobile/test/swarm/swarm_new_message_test.dartindex 56bac6408..fd51e592e 100644--- a/mobile/test/swarm/swarm_new_message_test.dart+++ b/mobile/test/swarm/swarm_new_message_test.dart@@ -10,18 +10,16 @@ import 'package:image_picker/image_picker.dart' show XFile;import 'package:harness_mobile/notify/system_notices.dart';import 'package:harness_mobile/phone/out_of_sight.dart';-import 'package:harness_mobile/phone/voice_input_controller.dart';+import 'package:harness_mobile/phone/voice_recorder.dart';import 'package:harness_mobile/settings/experimental_features.dart';import 'package:harness_mobile/shared/theme/app_theme.dart' show AppPalette;import 'package:harness_mobile/swarm/swarm_conversation_page.dart';-import 'package:harness_mobile/swarm/swarm_dictation.dart';import 'package:harness_mobile/swarm/swarm_file_picker.dart';import 'package:harness_mobile/swarm/swarm_new_message_sheet.dart';import 'package:harness_mobile/swarm/swarm_page.dart';import 'package:harness_mobile/swarm/swarm_project_page.dart';import 'package:harness_mobile/swarm/swarm_request.dart';import 'package:harness_mobile/swarm/swarm_session.dart';-import 'package:harness_mobile/swarm/swarm_voice.dart';import '../voice_fakes.dart';import 'swarm_fixture.dart';@@ -216,8 +214,7 @@ void main() {WidgetTester tester,FakeSwarm fake, {SwarmUpload upload = refuseSwarmUpload,- VoiceInputController Function()? voice,- SwarmDictation? dictation,+ VoiceRecorder Function()? voice,ValueListenable<bool>? out,bool settle = true,Size size = const Size(390, 844),@@ -237,7 +234,6 @@ void main() {features: features,readMarks: MemoryKeyValueStore(),voice: voice,- dictation: dictation,)..foreground = false;addTearDown(host.dispose);await tester.pumpWidget(@@ -896,26 +892,26 @@ void main() {});/// The sheet open on the Lead's chat — or on none, unless [pick] — its- /// mic heard by [dictation], or recorded and transcribed by [stt].+ /// mic recording into [mic].Future<FakeSwarm> talking(- WidgetTester tester,- _Dictation? dictation, {- FakeTranscriber? stt,+ WidgetTester tester, {+ FakeVoiceRecorder? mic,bool pick = true,- void Function(SwarmHost host)? host,}) async {final fake = backend();- final pumped = await pump(+ fake.routes['/api/super-tabs/$tab/attachments/'] = (_) => {+ 'voice': {+ 'durationMs': 2000,+ 'waveform': List.filled(48, 9),+ 'transcribed': true,+ },+ };+ await pump(tester,fake,- voice: () => VoiceInputController(- transcriber: (stt ?? FakeTranscriber()).call,- recorder: FakeVoiceRecorder(),- language: ValueNotifier('vi'),- ),- dictation: dictation,+ upload: FakeUploads().upload,+ voice: () => mic ?? FakeVoiceRecorder(),);- host?.call(pumped);await open(tester);if (pick) await tester.tap(row(leadChat));await tester.pump();@@ -932,147 +928,47 @@ void main() {}}- testWidgets('the mic writes into the box; stop keeps the words and sends '- 'nothing, and Send then sends to the chat picked', (tester) async {- final dictation = _Dictation();- final fake = await talking(tester, dictation);-- await tapIn(tester, 'swarm-mic');- dictation.say('Mai họp lúc 9 giờ');- await tester.pump();- expect(field(tester), 'Mai họp lúc 9 giờ');- expect(inSheet('swarm-new-message-send'), findsNothing);- expect(inSheet('swarm-new-message-attach'), findsNothing);- expect(inSheet('swarm-new-message-clear'), findsNothing);- expect(inSheet('swarm-voice-stop'), findsOneWidget);-- await tapIn(tester, 'swarm-voice-stop');- await tester.pumpAndSettle();- expect(sends(fake), isEmpty, reason: 'voice never sends by itself');- expect(field(tester), 'Mai họp lúc 9 giờ');- expect(sheetOpen(), isTrue);- expect(inSheet('swarm-new-message-send'), findsOneWidget);- expect(inSheet('swarm-new-message-clear'), findsOneWidget);-- await tester.tap(inSheet('swarm-new-message-send'));- await tester.pumpAndSettle();- expect(sends(fake).single['command']['text'], 'Mai họp lúc 9 giờ');- expect(sends(fake).single['command']['conversationId'], leadChat);- expect(sheetOpen(), isFalse);- });-- // Tony, 11-01: Send right of stop ends the take and posts it.- testWidgets("the row's Send ends the take and posts its last words to the "- 'chat picked, once, and the sheet closes', (tester) async {- final dictation = _Dictation();- final fake = await talking(tester, dictation);-- await tapIn(tester, 'swarm-mic');- expect(- tester.widget<SwarmRoundButton>(inSheet('swarm-voice-send')).onTap,- isNull,- reason: 'an empty box',- );- dictation.say('Mai họp');- await tester.pump();- dictation.lastWords = 'Mai họp lúc 9 giờ';- final at = tester.getCenter(inSheet('swarm-voice-send'));- await tester.tapAt(at);- await tester.pump();- await tester.tapAt(at);- await tester.pumpAndSettle();-- expect(sends(fake), hasLength(1));- expect(sends(fake).single['command']['text'], 'Mai họp lúc 9 giờ');- expect(sends(fake).single['command']['conversationId'], leadChat);- expect(sheetOpen(), isFalse);- });-- testWidgets("with no chat picked the row's Send is off", (tester) async {- final dictation = _Dictation();- final fake = await talking(tester, dictation, pick: false);-- await tapIn(tester, 'swarm-mic');- dictation.say('cho ai đây');- await tester.pump();- expect(field(tester), 'cho ai đây');- expect(- tester.widget<SwarmRoundButton>(inSheet('swarm-voice-send')).onTap,- isNull,- );- await tapIn(tester, 'swarm-voice-send');- expect(sends(fake), isEmpty);- expect(inSheet('swarm-voice-stop'), findsOneWidget, reason: 'listening');- });-- testWidgets("a recording: the row's Send posts what was typed and what "- 'was said; nothing heard posts nothing and keeps the sheet', (+ testWidgets("the mic records; the row's Send posts the take to the chat "+ 'picked as a voice message, and nothing goes in the box', (tester,) async {- final stt = FakeTranscriber()..replies.addAll(['', 'lúc 9 giờ']);- final fake = await talking(tester, null, stt: stt);- await write(tester, 'Mai họp');-+ final fake = await talking(tester);+ await write(tester, 'Ghi chú:');await tapIn(tester, 'swarm-mic');- expect(- tester.widget<SwarmRoundButton>(inSheet('swarm-voice-send')).onTap,- isNotNull,- reason: 'live while recording',- );- await tapIn(tester, 'swarm-voice-send');- await tester.pumpAndSettle();- expect(sends(fake), isEmpty);- expect(field(tester), 'Mai họp');- expect(sheetOpen(), isTrue);- expect(find.text(SwarmVoice.nothingSent), findsOneWidget);+ expect(inSheet('swarm-voice-send'), findsOneWidget);+ expect(field(tester), 'Ghi chú:');- await tapIn(tester, 'swarm-mic');await tapIn(tester, 'swarm-voice-send');await tester.pumpAndSettle();- expect(sends(fake).single['command']['text'], 'Mai họp lúc 9 giờ');- expect(sheetOpen(), isFalse);+ final sent = sends(fake).single['command'] as Map<String, dynamic>;+ expect(sent['text'], '');+ expect(sent['attachmentIds'], ['id-Voice message.wav']);+ expect(find.byType(SwarmNewMessageSheet), findsNothing);});- testWidgets("the Swarm's read taken away while the row's Send transcribes "- 'closes the sheet and posts nothing', (tester) async {- final stt = FakeTranscriber()..pending = Completer<String>();- late SwarmHost host;- final fake = await talking(tester, null, stt: stt, host: (h) => host = h);+ testWidgets("with no chat picked the row's Send is off", (tester) async {+ final fake = await talking(tester, pick: false);await tapIn(tester, 'swarm-mic');await tapIn(tester, 'swarm-voice-send');- expect(inSheet('swarm-voice-sending'), findsOneWidget);-- for (final route in ['parts', 'changes']) {- fake.routes['/api/super-tabs/$tab/$route'] = (_) =>- throw SuperTabFailure('FORBIDDEN', 'No access.', statusCode: 403);- }- unawaited(host.session!.inbox.reader(tab)!.refresh());- await tester.pumpAndSettle();- expect(sheetOpen(), isFalse);- stt.pending!.complete('bí mật');- await tester.pumpAndSettle();-expect(sends(fake), isEmpty);- expect(find.text('bí mật'), findsNothing);+ expect(inSheet('swarm-voice-cancel'), findsOneWidget, reason: 'kept');});- testWidgets('cancel takes back what the take added and keeps what was '- 'typed before it', (tester) async {- final dictation = _Dictation();- final fake = await talking(tester, dictation);+ testWidgets('X throws the take away and keeps what was typed', (+ tester,+ ) async {+ final mic = FakeVoiceRecorder();+ final fake = await talking(tester, mic: mic);await write(tester, 'Ghi chú:');-await tapIn(tester, 'swarm-mic');- dictation.say('bỏ câu này');- await tester.pump();- expect(field(tester), 'Ghi chú: bỏ câu này');+ expect(mic.recording, isTrue);await tapIn(tester, 'swarm-voice-cancel');await tester.pumpAndSettle();+ expect(mic.cancels, 1);expect(field(tester), 'Ghi chú:');expect(sends(fake), isEmpty);expect(inSheet('swarm-mic'), findsOneWidget);- expect(inSheet('swarm-new-message-send'), findsOneWidget);});testWidgets('taken while Home is out of sight, it opens nothing, and the '@@ -1452,40 +1348,3 @@ void main() {expect(swarmSearchFold('Œuvre'), 'œuvre', reason: 'no accent to fold');});}--/// The phone's own recognizer, spoken for by the test.-class _Dictation implements SwarmDictation {- void Function(String)? _words;- void Function(SwarmDictationEnd)? _end;-- /// What [stop] hears last, before it ends.- String? lastWords;-- void say(String words) => _words?.call(words);-- @override- Future<SwarmDictationAccess> access() async => SwarmDictationAccess.allowed;-- @override- Future<bool> start(- String locale, {- required void Function(String words) onWords,- required void Function(double level) onLevel,- required void Function(SwarmDictationEnd end) onEnd,- }) async {- _words = onWords;- _end = onEnd;- return true;- }-- @override- Future<void> stop() async {- if (lastWords case final words?) say(words);- final end = _end;- _words = _end = null;- end?.call(SwarmDictationEnd.done);- }-- @override- Future<void> cancel() async => _words = _end = null;-}diff --git a/mobile/test/swarm/swarm_profile_ways_test.dart b/mobile/test/swarm/swarm_profile_ways_test.dartindex aaa3e971d..e69e864e9 100644--- a/mobile/test/swarm/swarm_profile_ways_test.dart+++ b/mobile/test/swarm/swarm_profile_ways_test.dart@@ -93,7 +93,7 @@ void main() {Finder inside(String value, Finder finder) =>find.descendant(of: key(value), matching: finder);- testWidgets('You starts with you, and its status opens your profile', (+ testWidgets('You starts with you, and its profile row opens your profile', (tester,) async {await pump(tester, const SwarmYouPage());@@ -103,10 +103,10 @@ void main() {findsOneWidget,);expect(- tester.getSize(key('swarm-you-status')).height,+ tester.getSize(key('swarm-you-profile')).height,greaterThanOrEqualTo(44),);- await tester.tap(key('swarm-you-status'));+ await tester.tap(key('swarm-you-profile'));await frames(tester);expect(find.text('Your profile'), findsOneWidget);});diff --git a/mobile/test/swarm/swarm_terminal_resume_test.dart b/mobile/test/swarm/swarm_terminal_resume_test.dartnew file mode 100644index 000000000..812b4178f--- /dev/null+++ b/mobile/test/swarm/swarm_terminal_resume_test.dart@@ -0,0 +1,769 @@+import 'dart:async';++import 'package:flutter/material.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/notify/system_notices.dart';+import 'package:harness_mobile/phone/phone_shell_scope.dart';+import 'package:harness_mobile/settings/experimental_features.dart';+import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;+import 'package:harness_mobile/swarm/model.dart';+import 'package:harness_mobile/swarm/swarm_conversation_page.dart';+import 'package:harness_mobile/swarm/swarm_project_details.dart';+import 'package:harness_mobile/swarm/swarm_project_page.dart';+import 'package:harness_mobile/swarm/swarm_project_tabs.dart';+import 'package:harness_mobile/swarm/swarm_request.dart';+import 'package:harness_mobile/swarm/swarm_session.dart';+import 'package:harness_mobile/swarm/swarm_terminal_row.dart';++import '../voice_fakes.dart';+import 'swarm_computers_fake.dart';+import 'swarm_fixture.dart';++/// Resume a stopped agent from Swarm (task 36): the desktop's Resume on a+/// Swarm agent's stopped pane, from the Open terminal rows of the Buddy chat's+/// ⋮, the project's ⋮ and the project's details. Stopped work the person's+/// own machine can bring back says "Stopped" and is not dimmed; the tap asks+/// first, names the machine, resumes it, then opens its terminal. Anything+/// else stopped stays dimmed, and nothing resumes on a machine this phone+/// does not open.+void main() {+ const buddyChat = 'buddy-me';+ const changesPath = '/api/super-tabs/$tab/changes';+ const wren = ('mac-1', 'harness-agent-wren');+ const river = ('mac-1', 'harness-agent-lead');++ void commonRoutes(FakeSwarm fake) {+ fake.routes['/api/super-tabs/list'] = (_) => {+ 'tabs': [+ {'id': tab, 'name': 'Phone Swarm', 'ownerId': me},+ ],+ 'next': null,+ };+ fake.routes[changesPath] = (_) => changes(cursor: 'c2');+ fake.routes['/api/super-tabs/$tab/decisions?state=open'] = (_) => {+ 'decisions': <Object>[],+ };+ fake.routes['/api/super-tabs/$tab/mentions?state=waiting'] = (_) => {+ 'mentions': <Object>[],+ };+ for (final chat in [buddyChat, leadChat]) {+ fake.routes['/api/super-tabs/$tab/conversations/$chat/messages'] = (_) =>+ {'messages': <Object>[], 'next': null};+ }+ }++ /// One Swarm where the person has their Buddy chat in Mobile, with Wren,+ /// their own Buddy on mac-1, unless [buddy] says otherwise.+ FakeSwarm buddyBackend({String role = 'owner', Map<String, dynamic>? buddy}) {+ final fake = FakeSwarm();+ commonRoutes(fake);+ final wrenData = buddy ?? buddyData('agent-wren', displayName: 'Wren');+ fake.routes['/api/super-tabs/$tab/parts'] = (_) => {+ ...firstLoad(+ groups: [+ buddyChatData(buddyChat, agentIds: [wrenData['id'] as String]),+ ],+ agents: [+ agent('agent-lead', name: 'Lead', displayName: 'River'),+ wrenData,+ ],+ ),+ 'members': [part(me, 1, person(me, role: role))],+ };+ return fake;+ }++ /// Mobile's project chat, whose PM is River on mac-1 unless [pm] says+ /// otherwise.+ FakeSwarm projectBackend({Map<String, dynamic>? pm}) {+ final fake = FakeSwarm();+ commonRoutes(fake);+ fake.routes['/api/super-tabs/$tab/parts'] = (_) => firstLoad(+ agents: [pm ?? agent('agent-lead', name: 'Lead', displayName: 'River')],+ );+ return fake;+ }++ /// The phone's machines as the shell sees them: [opens] those it can open+ /// now; [stoppedOn] each session its machines list as stopped (true: a+ /// resume can bring it back); every terminal it was asked to open lands in+ /// [opened], every resume asked for in [resumed]. A resume answers+ /// [resumeError] (null: back, and no longer stopped), or waits for [held].+ late Set<String> opens;+ late Map<(String, String), bool> stoppedOn;+ late List<(String, String)> opened;+ late List<(String, String)> resumed;+ late FakeComputers computers;+ String? resumeError;+ Completer<String?>? held;+ late bool canResume;++ setUp(() {+ opens = {'mac-1'};+ stoppedOn = {};+ opened = [];+ resumed = [];+ resumeError = null;+ held = null;+ canResume = true;+ computers = FakeComputers(const [])+ ..own = const [(id: 'mac-1', name: 'Tonys-MacBook-Pro', away: false)];+ });++ Future<String?> resume(String machineId, String agentId) async {+ resumed.add((machineId, agentId));+ final error = await (held?.future ?? Future.value(resumeError));+ if (error == null) stoppedOn.remove((machineId, agentId));+ return error;+ }++ Future<SwarmHost> pump(+ WidgetTester tester,+ FakeSwarm fake,+ Widget home, {+ ValueNotifier<String?>? account,+ }) async {+ account ??= ValueNotifier<String?>(me);+ final host = SwarmHost(+ accountId: () => account!.value,+ account: account,+ request: fake.request,+ upload: refuseSwarmUpload,+ notices: SilentSystemNotices(),+ computers: computers,+ features: ExperimentalFeaturesStore(storage: MemoryKeyValueStore())+ ..value = {PhoneExperiment.swarm},+ )..foreground = false;+ addTearDown(host.dispose);+ await tester.pumpWidget(+ MaterialApp(+ theme: grid.buildAppTheme(brightness: Brightness.dark),+ builder: (context, child) => PhoneShellScope(+ onMachineLinked: (_) {},+ onOpenAgent: (machine, agent) => opened.add((machine, agent)),+ canOpenMachine: opens.contains,+ agentStopped: (machine, agent) => stoppedOn[(machine, agent)],+ onResumeAgent: canResume ? resume : null,+ child: SwarmScope(notifier: host, child: child!),+ ),+ home: home,+ ),+ );+ await tester.pumpAndSettle();+ return host;+ }++ Future<SwarmHost> pumpBuddy(WidgetTester tester, FakeSwarm fake) => pump(+ tester,+ fake,+ const SwarmConversationPage(swarmId: tab, conversationId: buddyChat),+ );++ Future<SwarmHost> pumpProject(+ WidgetTester tester,+ FakeSwarm fake, {+ ValueNotifier<String?>? account,+ }) => pump(+ tester,+ fake,+ const SwarmProjectPage(+ swarmId: tab,+ projectId: project,+ tabs: SwarmProjectTab.homeC,+ ),+ account: account,+ );++ Finder key(String value) => find.byKey(ValueKey(value));+ final question = key('swarm-resume-title');++ /// The word at the end of the Open terminal row, or null with none.+ String? word() {+ for (final w in const ['Stopped', 'Offline', 'Not linked']) {+ if (find.text(w).evaluate().isNotEmpty) return w;+ }+ return null;+ }++ /// Opens the Buddy chat's ⋮ and taps Open terminal.+ Future<void> tapBuddyRow(WidgetTester tester) async {+ await tester.tap(key('swarm-buddy-menu'));+ await tester.pumpAndSettle();+ await tester.tap(find.text('Open terminal'));+ await tester.pumpAndSettle();+ }++ /// Opens the project's ⋮ and taps Open terminal.+ Future<void> tapProjectRow(WidgetTester tester) async {+ await tester.tap(key('swarm-project-menu'));+ await tester.pumpAndSettle();+ await tester.tap(find.text('Open terminal'), warnIfMissed: false);+ await tester.pumpAndSettle();+ }++ Future<void> confirm(WidgetTester tester) async {+ await tester.tap(key('swarm-resume-confirm'));+ await tester.pumpAndSettle();+ }++ group('Buddy chat ⋮', () {+ testWidgets('stopped work its machine can bring back: Stopped, not '+ 'dimmed; the tap asks first and names the machine', (tester) async {+ stoppedOn[wren] = true;+ await pumpBuddy(tester, buddyBackend());+ await tester.tap(key('swarm-buddy-menu'));+ await tester.pumpAndSettle();+ expect(word(), 'Stopped');++ await tester.tap(find.text('Open terminal'));+ await tester.pumpAndSettle();+ expect(find.text('Rename Buddy'), findsNothing, reason: 'sheet closed');+ expect(find.text('Resume Wren?'), findsOneWidget);+ expect(+ find.text('This agent is stopped on Tonys-MacBook-Pro.'),+ findsOneWidget,+ );+ expect(resumed, isEmpty, reason: 'nothing resumes before the answer');+ expect(opened, isEmpty);+ });++ testWidgets('Cancel resumes nothing and opens nothing', (tester) async {+ stoppedOn[wren] = true;+ await pumpBuddy(tester, buddyBackend());+ await tapBuddyRow(tester);++ await tester.tap(key('swarm-resume-cancel'));+ await tester.pumpAndSettle();+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ expect(opened, isEmpty);+ });++ testWidgets('Resume brings it back, then opens its terminal', (+ tester,+ ) async {+ stoppedOn[wren] = true;+ await pumpBuddy(tester, buddyBackend());+ await tapBuddyRow(tester);+ await confirm(tester);++ expect(resumed, [wren]);+ expect(question, findsNothing);+ expect(opened, [wren]);+ });++ testWidgets('while it resumes: Resuming…, and neither Cancel, the veil '+ 'nor back closes the question', (tester) async {+ stoppedOn[wren] = true;+ held = Completer<String?>();+ await pumpBuddy(tester, buddyBackend());+ await tapBuddyRow(tester);+ await tester.tap(key('swarm-resume-confirm'));+ await tester.pump();++ expect(find.text('Resuming…'), findsOneWidget);+ await tester.tap(key('swarm-resume-cancel'), warnIfMissed: false);+ await tester.pump();+ await tester.tapAt(const Offset(4, 4));+ await tester.pump();+ await tester.binding.handlePopRoute();+ await tester.pump();+ expect(question, findsOneWidget);+ expect(opened, isEmpty);++ held!.complete(null);+ await tester.pumpAndSettle();+ expect(question, findsNothing);+ expect(resumed, [wren], reason: 'asked once');+ expect(opened, [wren]);+ });++ testWidgets('a resume the machine refuses says why, opens nothing, and '+ 'Resume tries again', (tester) async {+ stoppedOn[wren] = true;+ resumeError =+ 'This harness has no supported saved conversation to resume.';+ await pumpBuddy(tester, buddyBackend());+ await tapBuddyRow(tester);+ await confirm(tester);++ expect(question, findsOneWidget);+ expect(+ find.text(+ 'This harness has no supported saved conversation to resume.',+ ),+ findsOneWidget,+ );+ expect(opened, isEmpty);++ resumeError = null;+ await confirm(tester);+ expect(resumed, [wren, wren]);+ expect(question, findsNothing);+ expect(opened, [wren]);+ });++ testWidgets('stopped work its machine cannot bring back: Stopped, dimmed, '+ 'and the tap asks nothing', (tester) async {+ stoppedOn[wren] = false;+ await pumpBuddy(tester, buddyBackend());+ await tapBuddyRow(tester);++ expect(word(), 'Stopped');+ expect(find.text('Rename Buddy'), findsOneWidget, reason: 'sheet stays');+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ expect(opened, isEmpty);+ });++ testWidgets('the Swarm reads a stopped session as down: still Stopped, '+ 'and resumable, not Offline', (tester) async {+ stoppedOn[wren] = true;+ await pumpBuddy(+ tester,+ buddyBackend(+ buddy: {+ ...buddyData('agent-wren', displayName: 'Wren'),+ 'downSince': '2026-11-01T07:00:00Z',+ },+ ),+ );+ await tester.tap(key('swarm-buddy-menu'));+ await tester.pumpAndSettle();+ expect(word(), 'Stopped');+ await tester.tap(find.text('Open terminal'));+ await tester.pumpAndSettle();+ await confirm(tester);+ expect(opened, [wren]);+ });++ testWidgets('no resume where the shell offers none: Stopped, dimmed', (+ tester,+ ) async {+ stoppedOn[wren] = true;+ canResume = false;+ await pumpBuddy(tester, buddyBackend());+ await tapBuddyRow(tester);+ expect(word(), 'Stopped');+ expect(question, findsNothing);+ expect(opened, isEmpty);+ });++ testWidgets('its machine offline: Offline, never a resume', (tester) async {+ stoppedOn[wren] = true;+ opens = {};+ computers.own = const [+ (id: 'mac-1', name: 'Tonys-MacBook-Pro', away: true),+ ];+ await pumpBuddy(tester, buddyBackend());+ await tapBuddyRow(tester);+ expect(word(), 'Offline');+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ });++ testWidgets('resumed elsewhere between the sheet and the tap: opens it, '+ 'and asks nothing', (tester) async {+ stoppedOn[wren] = true;+ await pumpBuddy(tester, buddyBackend());+ await tester.tap(key('swarm-buddy-menu'));+ await tester.pumpAndSettle();+ expect(word(), 'Stopped');++ stoppedOn.clear();+ await tester.tap(find.text('Open terminal'));+ await tester.pumpAndSettle();+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ expect(opened, [wren]);+ });++ testWidgets('stopped between the sheet and the tap: asks first', (+ tester,+ ) async {+ await pumpBuddy(tester, buddyBackend());+ await tester.tap(key('swarm-buddy-menu'));+ await tester.pumpAndSettle();+ expect(word(), isNull);++ stoppedOn[wren] = true;+ await tester.tap(find.text('Open terminal'));+ await tester.pumpAndSettle();+ expect(find.text('Resume Wren?'), findsOneWidget);+ expect(opened, isEmpty);+ });++ testWidgets('access lost with the question open closes it, resuming and '+ 'opening nothing', (tester) async {+ stoppedOn[wren] = true;+ final host = await pumpBuddy(tester, buddyBackend());+ await tapBuddyRow(tester);+ expect(question, findsOneWidget);++ host.session!.inbox.reader(tab)!.revoke();+ await tester.pumpAndSettle();+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ expect(opened, isEmpty);+ });++ testWidgets('access lost while it resumes: the question goes, and the '+ 'terminal is not opened', (tester) async {+ stoppedOn[wren] = true;+ held = Completer<String?>();+ final host = await pumpBuddy(tester, buddyBackend());+ await tapBuddyRow(tester);+ await tester.tap(key('swarm-resume-confirm'));+ await tester.pump();++ host.session!.inbox.reader(tab)!.revoke();+ await tester.pumpAndSettle();+ expect(question, findsNothing);++ held!.complete(null);+ await tester.pumpAndSettle();+ expect(resumed, [wren]);+ expect(opened, isEmpty);+ });++ testWidgets('the Buddy removed elsewhere with the question open closes '+ 'it, and never resumes its session', (tester) async {+ stoppedOn[wren] = true;+ final fake = buddyBackend();+ final host = await pumpBuddy(tester, fake);+ await tapBuddyRow(tester);+ expect(question, findsOneWidget);++ fake.routes[changesPath] = (_) => {+ ...changes(cursor: 'c3', lastChangedAt: 200),+ 'agents': [+ part('agent-wren', 2, {+ ...buddyData('agent-wren', displayName: 'Wren'),+ 'workDecision': 'stop',+ }),+ ],+ };+ await host.session!.inbox.reader(tab)!.refresh();+ await tester.pumpAndSettle();+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ expect(opened, isEmpty);+ });++ testWidgets('a viewer has no Open terminal, so no resume', (tester) async {+ stoppedOn[wren] = true;+ await pumpBuddy(tester, buddyBackend(role: 'viewer'));+ expect(find.text('Open terminal'), findsNothing);+ expect(resumed, isEmpty);+ });+ });++ group("project's ⋮", () {+ testWidgets("the PM's stopped work: asks, resumes it, opens its terminal", (+ tester,+ ) async {+ stoppedOn[river] = true;+ await pumpProject(tester, projectBackend());+ await tester.tap(key('swarm-project-menu'));+ await tester.pumpAndSettle();+ final row = key('swarm-project-menu-terminal');+ expect(+ find.descendant(of: row, matching: find.text('Stopped')),+ findsOneWidget,+ );+ expect(tester.widget<MenuItemButton>(row).onPressed, isNotNull);++ await tester.tap(find.text('Open terminal'));+ await tester.pumpAndSettle();+ expect(find.text('Resume River?'), findsOneWidget);+ expect(+ find.text('This agent is stopped on Tonys-MacBook-Pro.'),+ findsOneWidget,+ );+ await confirm(tester);+ expect(resumed, [river]);+ expect(opened, [river]);+ });++ testWidgets("a teammate's machine is never resumed from here: Not linked, "+ 'whatever it lists', (tester) async {+ // Ken's computer: not among the person's own, and the phone does not+ // open it.+ const ken = ('mac-ken', 'harness-agent-lead');+ stoppedOn[ken] = true;+ await pumpProject(+ tester,+ projectBackend(+ pm: {+ ...agent(+ 'agent-lead',+ name: 'Lead',+ displayName: 'River',+ machineId: 'mac-ken',+ ),+ 'machineOwnerId': 'person-ken',+ },+ ),+ );+ await tapProjectRow(tester);+ expect(word(), 'Not linked');+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ expect(opened, isEmpty);+ });++ testWidgets('a PM the Swarm stopped is not resumed: Stopped, dimmed', (+ tester,+ ) async {+ stoppedOn[river] = true;+ await pumpProject(+ tester,+ projectBackend(+ pm: {+ ...agent('agent-lead', name: 'Lead', displayName: 'River'),+ 'workDecision': 'stop',+ },+ ),+ );+ await tester.tap(key('swarm-project-menu'));+ await tester.pumpAndSettle();+ final row = key('swarm-project-menu-terminal');+ expect(tester.widget<MenuItemButton>(row).onPressed, isNull);+ expect(word(), 'Stopped');+ expect(resumed, isEmpty);+ });++ testWidgets('the PM stopped by the Swarm with the question open closes '+ 'it, and never resumes its session', (tester) async {+ stoppedOn[river] = true;+ final fake = projectBackend();+ final host = await pumpProject(tester, fake);+ await tapProjectRow(tester);+ expect(question, findsOneWidget);++ fake.routes[changesPath] = (_) => {+ ...changes(cursor: 'c3', lastChangedAt: 200),+ 'agents': [+ part('agent-lead', 2, {+ ...agent('agent-lead', name: 'Lead', displayName: 'River'),+ 'workDecision': 'stop',+ }),+ ],+ };+ await host.session!.inbox.reader(tab)!.refresh();+ await tester.pumpAndSettle();+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ expect(opened, isEmpty);+ });++ testWidgets('signing out with the question open closes it', (tester) async {+ stoppedOn[river] = true;+ final account = ValueNotifier<String?>(me);+ await pumpProject(tester, projectBackend(), account: account);+ await tapProjectRow(tester);+ expect(question, findsOneWidget);++ account.value = null;+ await tester.pumpAndSettle();+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ expect(opened, isEmpty);+ });++ testWidgets("the PM replaced while the question is open closes it, and "+ 'never resumes the one it replaced', (tester) async {+ stoppedOn[river] = true;+ final fake = projectBackend();+ final host = await pumpProject(tester, fake);+ await tapProjectRow(tester);+ expect(question, findsOneWidget);++ fake.routes[changesPath] = (_) => {+ ...changes(+ cursor: 'c3',+ lastChangedAt: 200,+ groups: [+ part(+ leadChat,+ 5,+ groupData(+ leadChat,+ title: 'Mobile',+ agentIds: ['agent-nova'],+ leadId: 'agent-nova',+ ),+ ),+ ],+ ),+ 'agents': [+ part('agent-nova', 2, agent('agent-nova', displayName: 'Nova')),+ ],+ };+ await host.session!.inbox.reader(tab)!.refresh();+ await tester.pumpAndSettle();+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ expect(opened, isEmpty);+ });+ });++ // The project's details (a tap on its #name), whose Open terminal is the+ // menu's row: the same resume, asked once the details are gone.+ group('project details', () {+ Finder terminal() => key('swarm-project-details-terminal');+ bool detailsOpen() =>+ find.byType(SwarmProjectDetailsSheet).evaluate().isNotEmpty;++ Future<void> openDetails(WidgetTester tester) async {+ await tester.tap(key('swarm-title-button'));+ await tester.pumpAndSettle();+ }++ testWidgets("the PM's stopped work: heard Stopped, not greyed; the tap "+ 'closes the details, asks, resumes it, then opens it', (tester) async {+ final semantics = tester.ensureSemantics();+ stoppedOn[river] = true;+ await pumpProject(tester, projectBackend());+ await openDetails(tester);+ expect(+ tester.getSemantics(terminal()),+ isSemantics(+ label: 'Open terminal, Stopped',+ isButton: true,+ hasEnabledState: true,+ isEnabled: true,+ ),+ );++ await tester.tap(terminal());+ await tester.pumpAndSettle();+ expect(detailsOpen(), isFalse);+ expect(find.text('Resume River?'), findsOneWidget);+ expect(+ find.text('This agent is stopped on Tonys-MacBook-Pro.'),+ findsOneWidget,+ );+ expect(resumed, isEmpty);++ await confirm(tester);+ expect(resumed, [river]);+ expect(question, findsNothing);+ expect(opened, [river]);+ semantics.dispose();+ });++ testWidgets('Cancel resumes nothing and opens nothing', (tester) async {+ stoppedOn[river] = true;+ await pumpProject(tester, projectBackend());+ await openDetails(tester);+ await tester.tap(terminal());+ await tester.pumpAndSettle();++ await tester.tap(key('swarm-resume-cancel'));+ await tester.pumpAndSettle();+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ expect(opened, isEmpty);+ });++ testWidgets('stopped work its machine cannot bring back: greyed, and the '+ 'tap asks nothing', (tester) async {+ stoppedOn[river] = false;+ await pumpProject(tester, projectBackend());+ await openDetails(tester);++ await tester.tap(terminal());+ await tester.pumpAndSettle();+ expect(detailsOpen(), isTrue);+ expect(question, findsNothing);+ expect(resumed, isEmpty);+ expect(opened, isEmpty);+ });+ });++ group('the row rule', () {+ final wrenAgent = SuperTabAgent.fromJson(buddyData('agent-wren'));+ bool? stopped(String m, String a) => stoppedOn[(m, a)];++ test('a session its machine lists as stopped reads Stopped, resumable '+ 'or not, where the machine opens', () {+ for (final value in [true, false]) {+ stoppedOn = {wren: value};+ expect(+ swarmTerminalBlock(+ wrenAgent,+ canOpen: {'mac-1'}.contains,+ own: const [],+ stopped: stopped,+ ),+ 'Stopped',+ );+ expect(+ swarmTerminalResumes(+ wrenAgent,+ canOpen: {'mac-1'}.contains,+ stopped: stopped,+ ),+ value,+ );+ }+ });++ test('a running session, or none listed, keeps the old words', () {+ stoppedOn = {};+ expect(+ swarmTerminalBlock(+ wrenAgent,+ canOpen: {'mac-1'}.contains,+ own: const [],+ stopped: stopped,+ ),+ isNull,+ );+ expect(+ swarmTerminalResumes(+ wrenAgent,+ canOpen: {'mac-1'}.contains,+ stopped: stopped,+ ),+ isFalse,+ );+ });++ test('a machine the phone does not open is never resumed', () {+ stoppedOn = {wren: true};+ expect(+ swarmTerminalBlock(+ wrenAgent,+ canOpen: (_) => false,+ own: const [],+ stopped: stopped,+ ),+ 'Not linked',+ );+ expect(+ swarmTerminalResumes(wrenAgent, canOpen: null, stopped: stopped),+ isFalse,+ );+ });++ test('an agent the Swarm stopped, or none, is never resumed', () {+ stoppedOn = {wren: true};+ final stoppedBySwarm = SuperTabAgent.fromJson({+ ...buddyData('agent-wren'),+ 'workDecision': 'stop',+ });+ for (final a in [stoppedBySwarm, null]) {+ expect(+ swarmTerminalResumes(+ a,+ canOpen: {'mac-1'}.contains,+ stopped: stopped,+ ),+ isFalse,+ );+ }+ });+ });+}diff --git a/mobile/test/swarm/swarm_thread_voice_test.dart b/mobile/test/swarm/swarm_thread_voice_test.dartindex ecead6d05..70e798275 100644--- a/mobile/test/swarm/swarm_thread_voice_test.dart+++ b/mobile/test/swarm/swarm_thread_voice_test.dart@@ -3,26 +3,19 @@ import 'dart:async';import 'package:flutter/material.dart';import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/notify/system_notices.dart';-import 'package:harness_mobile/phone/voice_input_controller.dart';import 'package:harness_mobile/settings/experimental_features.dart';import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;import 'package:harness_mobile/swarm/swarm_conversation_page.dart';import 'package:harness_mobile/swarm/swarm_session.dart';import 'package:harness_mobile/swarm/swarm_thread_page.dart';-import 'package:harness_mobile/swarm/swarm_voice.dart';import '../voice_fakes.dart';-import 'swarm_chat_dictation_test.dart' show FakeDictation;import 'swarm_fixture.dart';-/// Talking instead of typing in a thread (Dee, 11-01: "how do I voice in a-/// thread?"): the main chat's mic, in the thread's reply box beside Send. Tap-/// to talk; while talking the toolbar is cancel, the wave, stop and Send.-/// Voice never sends by itself (Tony, 11-01): stop leaves the words in the-/// box, and Send — the row's, or the box's after stop — posts them as a reply,-/// to the chat too with Also send. A take goes,-/// nothing of it written or sent, once the person can no longer reply there.-/// The mic itself: `swarm_chat_voice_test` and `swarm_chat_dictation_test`.+/// Voice messages in a thread (task #2584): the main chat's mic, in the+/// thread's reply box beside Send. Its row's Send posts the take as a reply,+/// as audio; nothing goes into the box. A take never leaves the phone once the+/// person can no longer reply there. The mic itself: `swarm_chat_voice_test`.void main() {const messages = '/api/super-tabs/$tab/conversations/g1/messages';const commands = '/api/super-tabs/$tab/commands';@@ -30,7 +23,7 @@ void main() {late FakeSwarm fake;late FakeVoiceRecorder recorder;- late FakeTranscriber stt;+ late FakeUploads uploads;late ValueNotifier<String?> account;Map<String, dynamic> message(String id, int sequence, String text) => {@@ -64,7 +57,7 @@ void main() {setUp(() {recorder = FakeVoiceRecorder();- stt = FakeTranscriber();+ uploads = FakeUploads();fake = FakeSwarm();fake.routes['/api/super-tabs/list'] = (_) => {'tabs': [@@ -105,15 +98,18 @@ void main() {'threads': [line],};fake.routes[commands] = (_) => {'result': <String, dynamic>{}};+ fake.routes['/api/super-tabs/$tab/attachments/'] = (_) => {+ 'voice': {+ 'durationMs': 2000,+ 'waveform': List.filled(48, 9),+ 'transcribed': true,+ },+ };});- /// Chat g1's thread m1 open, its box's mic heard through [recorder] and- /// [stt] — or the phone's own [dictation], or no voice input at all.- Future<SwarmHost> pump(- WidgetTester tester, {- bool voice = true,- FakeDictation? dictation,- }) async {+ /// Chat g1's thread m1 open, its box's mic recording into [recorder] — or+ /// with no mic at all.+ Future<SwarmHost> pump(WidgetTester tester, {bool voice = true}) async {final features = ExperimentalFeaturesStore(storage: MemoryKeyValueStore())..value = {PhoneExperiment.swarm};account = ValueNotifier<String?>(me);@@ -121,16 +117,10 @@ void main() {accountId: () => account.value,account: account,request: fake.request,+ upload: uploads.upload,notices: SilentSystemNotices(),features: features,- voice: voice- ? () => VoiceInputController(- transcriber: stt.call,- recorder: recorder,- language: ValueNotifier('en'),- )- : null,- dictation: dictation,+ voice: voice ? () => recorder : null,)..foreground = false;addTearDown(host.dispose);await tester.pumpWidget(@@ -165,18 +155,6 @@ void main() {await frames(tester);}- /// The voice row's stop: the take ends, its words left in the box.- Future<void> stop(WidgetTester tester) async {- await tester.tap(inBox(key('swarm-voice-stop')));- await frames(tester);- }-- /// The voice row's cancel: what the take added leaves the box.- Future<void> cancel(WidgetTester tester) async {- await tester.tap(inBox(key('swarm-voice-cancel')));- await frames(tester);- }-String box(WidgetTester tester) => tester.widget<TextField>(find.descendant(@@ -187,12 +165,6 @@ void main() {.controller!.text;- /// The thread's draft as its chat keeps it, for the next time it opens.- String kept(WidgetTester tester) => tester- .widget<SwarmThreadPage>(find.byType(SwarmThreadPage))- .chat- .threadDraft('m1');-/// The Swarm read again: [listed] or not, the person as [role].Future<void> reread(WidgetTester tester,@@ -232,516 +204,34 @@ void main() {expect(key('swarm-thread-send'), findsOneWidget);});- testWidgets('stop leaves what was said in the reply box and sends nothing; '- 'Send then posts it as a reply in the thread, not to the chat', (- tester,- ) async {- await pump(tester);- stt.replies.add('ship the fix today');-- await tapMic(tester);- expect(recorder.recording, isTrue);- expect(inBox(key('swarm-voice-stop')), findsOneWidget);-- await stop(tester);- await tester.pumpAndSettle();-- expect(recorder.recording, isFalse);- expect(fake.commands, isEmpty, reason: 'voice never sends by itself');- expect(box(tester), 'ship the fix today');- expect(inBox(key('swarm-voice-stop')), findsNothing);-- await tester.tap(key('swarm-thread-send'));- await tester.pumpAndSettle();-- expect(fake.commands.single, {- 'type': 'message.send',- 'conversationId': 'g1',- 'text': 'ship the fix today',- 'replyTo': 'm1',- 'recipientIds': <String>[],- });- expect(box(tester), isEmpty);- });-- testWidgets('what was typed goes with what is said', (tester) async {- await pump(tester);- await tester.enterText(key('swarm-thread-field'), 'Nova:');- await tester.pump();- stt.replies.add('ship the fix');-- await tapMic(tester);- await stop(tester);- await tester.pumpAndSettle();- expect(box(tester), 'Nova: ship the fix');- expect(fake.commands, isEmpty);-- await tester.tap(key('swarm-thread-send'));- await tester.pumpAndSettle();-- expect(fake.commands.single['text'], 'Nova: ship the fix');- expect(fake.commands.single['replyTo'], 'm1');- });-- testWidgets('Also send ticked, a take, stop, then Send posts to the chat '- 'too: the tick survives the take, and takes no taps during it', (- tester,- ) async {- await pump(tester);- await tester.tap(key('swarm-thread-options'));- await tester.pump();- await tester.tap(key('swarm-thread-also-send'));- await tester.pump();- stt.replies.add('also for everyone');- final box = tester.getRect(key('swarm-thread-box'));- bool? ticked() => tester- .widget<Semantics>(key('swarm-thread-also-send'))- .properties- .checked;-- await tapMic(tester);- expect(- tester.getRect(key('swarm-thread-box')),- box,- reason: 'Reply options stays as it was: the box does not jump',- );- // In sight, as it will be sent: a tap during the take changes nothing.- await tester.tap(key('swarm-thread-also-send'), warnIfMissed: false);- await tester.tap(key('swarm-thread-options'), warnIfMissed: false);- await tester.pump();- expect(ticked(), isTrue);- expect(key('swarm-thread-also-send'), findsOneWidget, reason: 'still open');- await stop(tester);- await tester.pumpAndSettle();- expect(fake.commands, isEmpty);- expect(- tester- .widget<Semantics>(key('swarm-thread-also-send'))- .properties- .checked,- isTrue,- reason: 'still ticked after the take',- );-- await tester.tap(key('swarm-thread-send'));- await tester.pumpAndSettle();-- expect(fake.commands.single['text'], 'also for everyone');- expect(fake.commands.single['replyTo'], 'm1');- expect(fake.commands.single['alsoSend'], isTrue);- });-- // Tony, 11-01: the row's Send, as in the main chat.- testWidgets("the row's Send ends the take and posts it as one reply, to the "- 'chat too with Also send ticked', (tester) async {- await pump(tester);- await tester.tap(key('swarm-thread-options'));- await tester.pump();- await tester.tap(key('swarm-thread-also-send'));- await tester.pump();- await tester.enterText(key('swarm-thread-field'), 'Nova:');- await tester.pump();- stt.pending = Completer<String>();-- await tapMic(tester);- final at = tester.getCenter(inBox(key('swarm-voice-send')));- await tester.tapAt(at);- await tester.pump(const Duration(milliseconds: 40));- await tester.tapAt(at);- await frames(tester);- expect(inBox(key('swarm-voice-sending')), findsOneWidget);- stt.pending!.complete('ship it');- await tester.pumpAndSettle();-- expect(fake.commands, hasLength(1));- expect(fake.commands.single['text'], 'Nova: ship it');- expect(fake.commands.single['replyTo'], 'm1');- expect(fake.commands.single['alsoSend'], isTrue);- expect(box(tester), isEmpty);- });-- testWidgets("the row's Send with nothing heard posts nothing and keeps "- 'what was typed', (tester) async {+ testWidgets("the row's Send posts the take as a reply, as audio, and the "+ 'box keeps what was typed', (tester) async {await pump(tester);- await tester.enterText(key('swarm-thread-field'), 'Draft');- await tester.pump();-- await tapMic(tester);- await tester.tap(inBox(key('swarm-voice-send')));- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(box(tester), 'Draft');- expect(find.text(SwarmVoice.nothingSent), findsOneWidget);- });-- testWidgets("access lost while the row's Send transcribes: nothing posted, "- 'nothing kept', (tester) async {- final host = await pump(tester);- stt.pending = Completer<String>();+ await tester.enterText(inBox(find.byType(TextField)), 'typed first');await tapMic(tester);+ expect(inBox(key('swarm-voice-send')), findsOneWidget);await tester.tap(inBox(key('swarm-voice-send')));await frames(tester);-- await reread(tester, host, listed: false);- stt.pending!.complete('too late');- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(find.text('too late'), findsNothing);- expect(kept(tester), isEmpty);- expect(find.text(SwarmVoice.nothingSent), findsNothing);- });-- // After #2471 a reply on its way no longer holds the box: a take stopped- // meanwhile leaves its words for the next reply, which Send posts after the- // first, and the first keeps its own text.- testWidgets('a take stopped while a reply is still on its way: Send posts '- 'it as the next reply, in order', (tester) async {- await pump(tester);- final first = Completer<Object?>();- fake.routes[commands] = (_) =>- first.isCompleted ? {'result': <String, dynamic>{}} : first.future;- await tester.enterText(key('swarm-thread-field'), 'typed reply');- await tester.pump();- await tester.tap(key('swarm-thread-send'));+ await tester.pump(const Duration(milliseconds: 100));await frames(tester);- expect(fake.commands.single['text'], 'typed reply');- expect(box(tester), isEmpty);- stt.replies.add('spoken reply');- await tapMic(tester);- await stop(tester);- await frames(tester);- expect(box(tester), 'spoken reply');- expect(fake.commands, hasLength(1), reason: 'stop sends nothing');- await tester.tap(key('swarm-thread-send'));- await frames(tester);- first.complete({'result': <String, dynamic>{}});- await tester.pumpAndSettle();-- expect(- [for (final c in fake.commands) (c['text'], c['replyTo'])],- [('typed reply', 'm1'), ('spoken reply', 'm1')],- );- expect(box(tester), isEmpty);+ final sent = fake.commands.single;+ expect(sent['replyTo'], 'm1');+ expect(sent['text'], '');+ expect(sent['attachmentIds'], ['id-Voice message.wav']);+ expect(box(tester), 'typed first');});- testWidgets('a take not heard says so in the thread, and sends nothing', (- tester,- ) async {- await pump(tester);-- await tapMic(tester);- await stop(tester);- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(inBox(key('swarm-thread-voice-note')), findsOneWidget);- });-- testWidgets("during a take the toolbar is cancel, the wave, stop and the "- "row's Send: no box Send, + or X, which come back after stop; Reply "- 'options stays, folded', (tester) async {- await pump(tester);- await tester.enterText(key('swarm-thread-field'), 'Nova:');- await tester.pump();- expect(inBox(key('swarm-thread-clear')), findsOneWidget);- stt.replies.add('ship it');-- await tapMic(tester);-- for (final gone in [- 'swarm-thread-send',- 'swarm-thread-attach',- 'swarm-thread-clear',- 'swarm-mic',- ]) {- expect(inBox(key(gone)), findsNothing, reason: gone);- }- for (final shown in [- 'swarm-voice-cancel',- 'swarm-voice-wave',- 'swarm-voice-stop',- 'swarm-voice-send',- 'swarm-thread-options',- ]) {- expect(inBox(key(shown)), findsOneWidget, reason: shown);- }- // Folded it stays: a tap during the take does not open it.- await tester.tap(key('swarm-thread-options'), warnIfMissed: false);- await tester.pump();- expect(inBox(key('swarm-thread-also-send')), findsNothing);-- await stop(tester);- await tester.pumpAndSettle();-- for (final back in [- 'swarm-thread-send',- 'swarm-thread-options',- 'swarm-thread-attach',- 'swarm-thread-clear',- 'swarm-mic',- ]) {- expect(inBox(key(back)), findsOneWidget, reason: back);- }- expect(inBox(key('swarm-voice-stop')), findsNothing);- expect(fake.commands, isEmpty);- });-- testWidgets('cancel throws the take away: nothing transcribed, the box as '- 'it was before, nothing sent', (tester) async {- await pump(tester);- await tester.enterText(key('swarm-thread-field'), 'Nova:');- await tester.pump();- stt.replies.add('never said');-- await tapMic(tester);- expect(recorder.recording, isTrue);- await cancel(tester);- await tester.pumpAndSettle();-- expect(recorder.recording, isFalse);- expect(recorder.cancels, 1);- expect(stt.calls, isEmpty, reason: 'nothing leaves the phone');- expect(box(tester), 'Nova:');- expect(fake.commands, isEmpty);- expect(inBox(key('swarm-mic')), findsOneWidget);- });-- testWidgets('cancel while the take is transcribed puts nothing in the box', (- tester,- ) async {- await pump(tester);- stt.pending = Completer<String>();- await tapMic(tester);- await stop(tester);- expect(stt.calls, hasLength(1), reason: 'the take is being transcribed');- expect(inBox(key('swarm-voice-stop')), findsNothing, reason: 'busy');- expect(inBox(key('swarm-voice-cancel')), findsOneWidget);-- await cancel(tester);- stt.pending!.complete('too late');- await tester.pumpAndSettle();-- expect(box(tester), isEmpty);- expect(fake.commands, isEmpty);- expect(inBox(key('swarm-mic')), findsOneWidget);- });-- testWidgets('taken out of the chat mid-take: the microphone closes, and '- 'nothing is transcribed, written or sent', (tester) async {- final host = await pump(tester);- stt.replies.add('the secret plan');- await tapMic(tester);- expect(recorder.recording, isTrue);-- await reread(tester, host, listed: false);-- expect(find.text('This chat is not open to you.'), findsOneWidget);- expect(recorder.recording, isFalse);- expect(recorder.cancels, 1);- expect(stt.calls, isEmpty, reason: 'nothing leaves the phone');- await tester.pumpAndSettle();- expect(fake.commands, isEmpty);- expect(find.text('the secret plan'), findsNothing);- });-- testWidgets('words transcribed after access was lost land nowhere', (- tester,- ) async {- final host = await pump(tester);- stt.pending = Completer<String>();- await tapMic(tester);- await stop(tester);- expect(stt.calls, hasLength(1), reason: 'the take is being transcribed');-- await reread(tester, host, listed: false);- stt.pending!.complete('too late');- await tester.pumpAndSettle();-- expect(fake.commands, isEmpty);- expect(find.text('too late'), findsNothing);- expect(kept(tester), isEmpty, reason: 'not kept for the thread either');- });-- testWidgets('made read-only mid-take: the take goes with the box, and '- 'nothing is sent', (tester) async {+ testWidgets('access lost mid-take: the microphone closes and nothing is '+ 'sent', (tester) async {final host = await pump(tester);- stt.replies.add('one more thing');await tapMic(tester);expect(recorder.recording, isTrue);await reread(tester, host, role: 'viewer');- expect(key('swarm-thread-box'), findsNothing);- expect(recorder.recording, isFalse);- expect(stt.calls, isEmpty);- await tester.pumpAndSettle();- expect(fake.commands, isEmpty);- });-- testWidgets('signed out mid-take: the microphone closes and nothing is '- 'sent', (tester) async {- await pump(tester);- stt.replies.add('after sign out');- await tapMic(tester);- expect(recorder.recording, isTrue);-- account.value = null;- await frames(tester);-expect(recorder.recording, isFalse);- expect(stt.calls, isEmpty);- await tester.pumpAndSettle();+ expect(uploads.asked, isEmpty);expect(fake.commands, isEmpty);});-- testWidgets('another account signed in mid-take: the microphone closes '- 'and nothing is sent', (tester) async {- await pump(tester);- stt.replies.add('meant for the first account');- await tapMic(tester);- expect(recorder.recording, isTrue);-- account.value = 'someone-else';- await frames(tester);-- expect(recorder.recording, isFalse);- expect(stt.calls, isEmpty);- await tester.pumpAndSettle();- expect(fake.commands, isEmpty);- });-- testWidgets('covering the thread cancels its recording', (tester) async {- await pump(tester);- await tapMic(tester);- expect(recorder.recording, isTrue);-- final context = tester.element(find.byType(SwarmThreadPage));- unawaited(- Navigator.of(context).push(- MaterialPageRoute<void>(- builder: (_) => const Scaffold(body: Text('Other screen')),- ),- ),- );- await tester.pump();- await tester.pump(const Duration(seconds: 1));-- expect(recorder.recording, isFalse);- expect(stt.calls, isEmpty);- expect(fake.commands, isEmpty);- });-- testWidgets('leaving the thread mid-take closes the microphone', (- tester,- ) async {- await pump(tester);- await tapMic(tester);- expect(recorder.recording, isTrue);-- await tester.tap(find.bySemanticsLabel('Back'));- await tester.pumpAndSettle();-- expect(find.byType(SwarmThreadPage), findsNothing);- expect(recorder.recording, isFalse);- expect(stt.calls, isEmpty);- expect(fake.commands, isEmpty);- });-- group('with the phone\'s own recognizer', () {- late FakeDictation dictation;- setUp(() => dictation = FakeDictation());-- testWidgets('words show in the reply box as they are said; stop keeps '- 'them and sends nothing, and Send posts them as a reply', (- tester,- ) async {- await pump(tester, dictation: dictation);-- await tapMic(tester);- dictation.say('looks good to me');- await frames(tester);- expect(box(tester), 'looks good to me');- expect(fake.commands, isEmpty);-- await stop(tester);- await tester.pumpAndSettle();-- expect(dictation.stops, 1);- expect(box(tester), 'looks good to me');- expect(fake.commands, isEmpty, reason: 'voice never sends by itself');-- await tester.tap(key('swarm-thread-send'));- await tester.pumpAndSettle();-- expect(fake.commands.single['text'], 'looks good to me');- expect(fake.commands.single['replyTo'], 'm1');- expect(box(tester), isEmpty);- });-- testWidgets("the row's Send ends the take and posts its last words as a "- 'reply', (tester) async {- await pump(tester, dictation: dictation);-- await tapMic(tester);- expect(- tester.widget<SwarmRoundButton>(inBox(key('swarm-voice-send'))).onTap,- isNull,- reason: 'an empty box',- );- dictation.say('looks good');- await frames(tester);- dictation.lastWords = 'looks good to me';- await tester.tap(inBox(key('swarm-voice-send')));- await tester.pumpAndSettle();-- expect(dictation.stops, 1);- expect(fake.commands.single['text'], 'looks good to me');- expect(fake.commands.single['replyTo'], 'm1');- expect(box(tester), isEmpty);- });-- testWidgets('cancel takes back what the take added and keeps what was '- 'typed before it', (tester) async {- await pump(tester, dictation: dictation);- await tester.enterText(key('swarm-thread-field'), 'Nova:');- await tester.pump();-- await tapMic(tester);- dictation.say('scrap that');- await frames(tester);- expect(box(tester), 'Nova: scrap that');-- await cancel(tester);- await tester.pumpAndSettle();-- expect(box(tester), 'Nova:');- expect(dictation.cancels, 1);- expect(dictation.listening, isFalse);- expect(fake.commands, isEmpty);- });-- testWidgets('taken out of the chat while listening: the recognizer stops, '- 'and what it still says lands nowhere', (tester) async {- final host = await pump(tester, dictation: dictation);- await tapMic(tester);- dictation.say('half a');- await frames(tester);- final heard = dictation.heard.single;-- await reread(tester, host, listed: false);- heard('half a secret');- await tester.pumpAndSettle();-- expect(dictation.cancels, 1);- expect(dictation.listening, isFalse);- expect(find.text('This chat is not open to you.'), findsOneWidget);- expect(find.textContaining('secret'), findsNothing);- expect(find.textContaining('half a'), findsNothing);- expect(kept(tester), isEmpty);- expect(fake.commands, isEmpty);- });- });}diff --git a/mobile/test/swarm/swarm_voice_bubble_test.dart b/mobile/test/swarm/swarm_voice_bubble_test.dartnew file mode 100644index 000000000..b0d8fbea3--- /dev/null+++ b/mobile/test/swarm/swarm_voice_bubble_test.dart@@ -0,0 +1,227 @@+import 'dart:io';++import 'package:flutter/material.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;+import 'package:harness_mobile/swarm/model.dart';+import 'package:harness_mobile/swarm/swarm_file_links.dart';+import 'package:harness_mobile/swarm/swarm_voice_bubble.dart';+import 'package:video_player/video_player.dart';++/// A voice message as both apps draw it (task #2584):+///+/// ( ▶ ) ıllıllııılllıı.ılıı.ıll 1:21 →A+///+/// Play hears it, from a copy of the file on the phone; →A shows its words+/// under it, and a second tap hides them.+void main() {+ final clip = SuperTabAttachment.fromJson({+ 'id': 'clip',+ 'name': 'Voice message.wav',+ 'mime': 'audio/wav',+ 'size': 2592044,+ 'url': 'https://cdn.example.test/super-tabs/tab/abc/Voice_message.wav',+ 'voice': {+ 'durationMs': 81000,+ 'waveform': List.generate(48, (i) => i % 32),+ 'transcribed': true,+ },+ });+ late _Player player;+ late List<SwarmFileRef> fetched;+ File? local;++ setUp(() {+ player = _Player();+ fetched = [];+ local = File('/cache/clip.wav');+ final file = swarmVoiceFile, make = swarmVoicePlayer;+ swarmVoiceFile = (context, ref) async {+ fetched.add(ref);+ return local;+ };+ swarmVoicePlayer = (file) {+ player.source = file;+ return player;+ };+ addTearDown(() {+ swarmVoiceFile = file;+ swarmVoicePlayer = make;+ });+ });++ Future<void> pump(+ WidgetTester tester, {+ SuperTabAttachment? file,+ String words = 'Ship the parser today.',+ }) async {+ final shown = file ?? clip;+ await tester.pumpWidget(+ MaterialApp(+ theme: grid.buildAppTheme(brightness: Brightness.dark),+ home: Scaffold(+ body: SwarmVoiceBubble(+ file: shown,+ voice: shown.voice!,+ words: words,+ fileRef: SwarmFileRef(shown),+ ),+ ),+ ),+ );+ }++ Finder key(String value) => find.byKey(ValueKey(value));++ testWidgets('shows play, its waveform, its length and →A, and no words', (+ tester,+ ) async {+ await pump(tester);+ expect(key('swarm-voice-play'), findsOneWidget);+ expect(key('swarm-voice-bars'), findsOneWidget);+ expect(find.text('1:21'), findsOneWidget);+ expect(key('swarm-voice-words-toggle'), findsOneWidget);+ expect(find.text('Ship the parser today.'), findsNothing);+ });++ testWidgets('→A shows its words under it, and a second tap hides them', (+ tester,+ ) async {+ await pump(tester);+ await tester.tap(key('swarm-voice-words-toggle'));+ await tester.pump();+ expect(find.text('Ship the parser today.'), findsOneWidget);+ await tester.tap(key('swarm-voice-words-toggle'));+ await tester.pump();+ expect(find.text('Ship the parser today.'), findsNothing);+ });++ testWidgets('without words, →A says why', (tester) async {+ final quiet = SuperTabAttachment.fromJson({+ 'id': 'quiet',+ 'name': 'Voice message.wav',+ 'mime': 'audio/wav',+ 'size': 64044,+ 'url': 'https://cdn.example.test/super-tabs/tab/abc/q.wav',+ 'voice': {+ 'durationMs': 2000,+ 'waveform': List.filled(48, 3),+ 'transcribed': false,+ 'failure': 'limit',+ },+ });+ await pump(tester, file: quiet, words: '');+ await tester.tap(key('swarm-voice-words-toggle'));+ await tester.pump();+ expect(find.text(swarmVoiceNoWordsLimit), findsOneWidget);+ });++ testWidgets('Play hears a copy of the file on the phone, and shows pause', (+ tester,+ ) async {+ await pump(tester);+ await tester.tap(key('swarm-voice-play'));+ await tester.pump();+ await tester.pump();+ expect(fetched.single.file.id, 'clip');+ expect(player.source!.path, '/cache/clip.wav');+ expect(player.calls, ['initialize', 'play']);+ expect(key('swarm-voice-pause'), findsOneWidget);++ await tester.tap(key('swarm-voice-pause'));+ await tester.pump();+ expect(player.calls.last, 'pause');+ expect(key('swarm-voice-play'), findsOneWidget);+ });++ testWidgets('the part played fills its bars, and the time counts it', (+ tester,+ ) async {+ await pump(tester);+ await tester.tap(key('swarm-voice-play'));+ await tester.pump();+ await tester.pump();+ player.value = player.value.copyWith(+ position: const Duration(milliseconds: 40500),+ );+ await tester.pump();+ final bars = tester.widget<SwarmVoiceBars>(key('swarm-voice-bars'));+ expect(bars.played, closeTo(0.5, 0.01));+ expect(find.text('0:40'), findsOneWidget);+ });++ testWidgets('fits a narrow message column, its waveform drawn narrower', (+ tester,+ ) async {+ await tester.pumpWidget(+ MaterialApp(+ theme: grid.buildAppTheme(brightness: Brightness.dark),+ home: Scaffold(+ body: Center(+ child: SizedBox(+ width: 280,+ child: SwarmVoiceBubble(+ file: clip,+ voice: clip.voice!,+ words: '',+ fileRef: SwarmFileRef(clip),+ ),+ ),+ ),+ ),+ ),+ );+ expect(tester.takeException(), isNull);+ expect(+ tester.getSize(key('swarm-voice-bars')).width,+ lessThan(48 * (SwarmVoiceBars.bar + SwarmVoiceBars.gap)),+ );+ expect(find.text('1:21'), findsOneWidget);+ });++ testWidgets('a file that cannot be had plays nothing', (tester) async {+ local = null;+ await pump(tester);+ await tester.tap(key('swarm-voice-play'));+ await tester.pump();+ await tester.pump();+ expect(player.calls, isEmpty);+ expect(key('swarm-voice-play'), findsOneWidget);+ });+}++class _Player extends VideoPlayerController {+ _Player() : super.file(File('/nowhere.wav'));++ File? source;+ final List<String> calls = [];++ @override+ Future<void> initialize() async {+ calls.add('initialize');+ value = value.copyWith(+ isInitialized: true,+ duration: const Duration(seconds: 81),+ );+ }++ @override+ Future<void> play() async {+ calls.add('play');+ value = value.copyWith(isPlaying: true, isCompleted: false);+ }++ @override+ Future<void> pause() async {+ calls.add('pause');+ value = value.copyWith(isPlaying: false);+ }++ @override+ Future<void> seekTo(Duration position) async {+ value = value.copyWith(position: position);+ }++ @override+ Future<void> dispose() async {}+}diff --git a/mobile/test/swarm/swarm_voice_model_test.dart b/mobile/test/swarm/swarm_voice_model_test.dartnew file mode 100644index 000000000..bd98e5928--- /dev/null+++ b/mobile/test/swarm/swarm_voice_model_test.dart@@ -0,0 +1,86 @@+import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/swarm/model.dart';+import 'package:harness_mobile/swarm/swarm_outgoing_files.dart';+import 'package:harness_mobile/swarm/voice_length.dart';++/// A chat voice message as the backend sends it (task #2584): one WAV file+/// whose `voice` carries its length, its waveform and whether it has words.+void main() {+ Map<String, dynamic> file({Object? voice}) => {+ 'id': 'clip',+ 'name': 'Voice message.wav',+ 'mime': 'audio/wav',+ 'size': 2592044,+ 'url': 'https://cdn.example.test/super-tabs/tab/abc/Voice_message.wav',+ 'voice': ?voice,+ };++ test('reads a voice message\'s length, waveform and words flag', () {+ final clip = SuperTabAttachment.fromJson(+ file(+ voice: {+ 'durationMs': 81000,+ 'waveform': [0, 12, 31, 40, -2],+ 'transcribed': false,+ 'failure': 'limit',+ },+ ),+ );+ expect(clip.voice!.length, const Duration(seconds: 81));+ // Out of range values are drawn at the nearest height a bar has.+ expect(clip.voice!.waveform, [0, 12, 31, 31, 0]);+ expect(clip.voice!.transcribed, isFalse);+ expect(clip.voice!.overLimit, isTrue);+ });++ test('reads a malformed voice as no voice, and keeps the file', () {+ final odd = SuperTabAttachment.fromJson(+ file(voice: {'durationMs': 'long'}),+ );+ expect(odd.voice, isNull);+ expect(odd.id, 'clip');+ expect(SuperTabAttachment.fromJson(file()).voice, isNull);+ });++ test('writes a length M:SS, rounded down, and labels a voice message', () {+ expect(swarmVoiceLength(const Duration(milliseconds: 81999)), '1:21');+ expect(swarmVoiceLength(const Duration(minutes: 10)), '10:00');+ expect(swarmVoiceLength(const Duration(milliseconds: 999)), '0:00');+ expect(swarmVoiceLength(const Duration(seconds: 9)), '0:09');+ expect(+ swarmVoiceLabel(const Duration(seconds: 81)),+ 'Voice message (1:21)',+ );+ });++ test('a quote of a voice message says its length; any other, its text', () {+ Map<String, dynamic> row(String text, {bool voice = false}) => {+ 'id': 'm1',+ 'conversationId': 'g1',+ 'text': text,+ 'sequence': 1,+ 'author': {'kind': 'person', 'id': 'dee', 'name': 'dee'},+ 'attachments': [+ if (voice)+ file(+ voice: {+ 'durationMs': 81000,+ 'waveform': [3],+ 'transcribed': true,+ },+ ),+ ],+ 'reactions': <Object>[],+ 'createdAt': DateTime.utc(2026, 11, 4).toIso8601String(),+ };+ expect(+ swarmQuoteText(SuperTabMessage.fromJson(row('hello team', voice: true))),+ 'Voice message (1:21)',+ );+ expect(swarmQuoteText(SuperTabMessage.fromJson(row('Typed'))), 'Typed');+ });++ test('sends a .wav as audio/wav', () {+ expect(swarmMimeForName('Voice message.wav'), 'audio/wav');+ });+}diff --git a/mobile/test/swarm/swarm_voice_send_test.dart b/mobile/test/swarm/swarm_voice_send_test.dartnew file mode 100644index 000000000..a38c50c52--- /dev/null+++ b/mobile/test/swarm/swarm_voice_send_test.dart@@ -0,0 +1,169 @@+import 'dart:async';+import 'dart:typed_data';++import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/phone/voice_recorder.dart';+import 'package:harness_mobile/swarm/swarm_conversation.dart';+import 'package:harness_mobile/swarm/swarm_reader.dart';+import 'package:harness_mobile/swarm/swarm_request.dart';+import 'package:harness_mobile/swarm/swarm_thread.dart';+import 'package:harness_mobile/swarm/swarm_voice_send.dart';++import 'swarm_fixture.dart';++/// A take goes up as a voice message (task #2584): the WAV is uploaded, the+/// backend reads it once, and the message carries that file alone.+void main() {+ const messages = '/api/super-tabs/$tab/conversations/g1/messages';+ const commands = '/api/super-tabs/$tab/commands';+ const voicePath =+ '/api/super-tabs/$tab/attachments/id-Voice message.wav/voice';+ final VoiceTake take = (+ wav: Uint8List.fromList([1, 2, 3, 4]),+ length: const Duration(seconds: 81),+ peak: 9000,+ );+ Map<String, dynamic> facts({bool transcribed = true, String? failure}) => {+ 'voice': {+ 'durationMs': 81000,+ 'waveform': List.filled(48, 9),+ 'transcribed': transcribed,+ 'failure': ?failure,+ },+ };++ setUp(() => swarmVoiceRetry = Duration.zero);++ ({FakeSwarm swarm, SwarmReader reader, FakeUploads uploads}) rig() {+ final swarm = FakeSwarm();+ final uploads = FakeUploads();+ swarm.routes['/api/super-tabs/$tab/parts'] = (_) => {+ ...firstLoad(+ groups: [+ groupData(+ 'g1',+ title: 'Fix login',+ agentIds: const ['agent-lead'],+ lastMessageSequence: 0,+ ),+ ],+ ),+ 'members': [part(me, 1, person(me))],+ };+ swarm.routes['/api/super-tabs/$tab/changes'] = (_) => changes(cursor: 'c2');+ swarm.routes[messages] = (_) => {'messages': <Object>[], 'next': null};+ swarm.routes[commands] = (_) => {'result': <String, dynamic>{}};+ swarm.routes['/api/super-tabs/$tab/attachments/'] = (_) => facts();+ final reader = SwarmReader(+ tabId: tab,+ accountId: me,+ request: swarm.request,+ upload: uploads.upload,+ );+ addTearDown(reader.dispose);+ return (swarm: swarm, reader: reader, uploads: uploads);+ }++ Future<SwarmConversation> open(SwarmReader reader) async {+ await reader.refresh();+ final c = SwarmConversation(reader, 'g1');+ addTearDown(c.dispose);+ await c.load();+ return c;+ }++ test('a take is uploaded as audio, read once, and sent alone', () async {+ final r = rig();+ final c = await open(r.reader);+ c.draft = 'typed before';++ expect(await c.sendVoice(take, 'vi'), (sent: true, notice: null));++ expect(r.uploads.asked.single.name, swarmVoiceFileName);+ expect(r.uploads.asked.single.mime, 'audio/wav');+ expect(r.uploads.asked.single.bytes, [1, 2, 3, 4]);+ expect(r.swarm.paths.where((p) => p.contains('/voice')), [+ '$voicePath?lang=vi',+ ]);+ expect(r.swarm.commands.single, {+ 'type': 'message.send',+ 'conversationId': 'g1',+ 'text': '',+ 'replyTo': null,+ 'recipientIds': <String>[],+ 'attachmentIds': ['id-Voice message.wav'],+ });+ // What was typed stays in the box.+ expect(c.draft, 'typed before');+ });++ test('waits while the backend still transcribes it', () async {+ final r = rig();+ var asks = 0;+ r.swarm.routes['/api/super-tabs/$tab/attachments/'] = (_) {+ if (++asks < 3) {+ throw const SuperTabFailure(+ 'VOICE_TRANSCRIBING',+ 'Still transcribing',+ statusCode: 409,+ );+ }+ return facts();+ };+ final c = await open(r.reader);+ expect((await c.sendVoice(take, 'en')).sent, isTrue);+ expect(asks, 3);+ expect(r.swarm.commands, hasLength(1));+ });++ test('says when it went without words past the daily limit', () async {+ final r = rig();+ r.swarm.routes['/api/super-tabs/$tab/attachments/'] = (_) =>+ facts(transcribed: false, failure: 'limit');+ final c = await open(r.reader);+ expect(await c.sendVoice(take, 'en'), (+ sent: true,+ notice: swarmVoiceOverLimit,+ ));+ expect(r.swarm.commands, hasLength(1));+ });++ test(+ 'keeps the take and sends nothing when the read gets no answer',+ () async {+ final r = rig();+ r.swarm.routes['/api/super-tabs/$tab/attachments/'] = (_) =>+ throw TimeoutException('forwarding timed out');+ final c = await open(r.reader);+ expect(await c.sendVoice(take, 'en'), (+ sent: false,+ notice: swarmVoiceNoAnswer,+ ));+ expect(r.swarm.commands, isEmpty);+ },+ );++ test('keeps the take and sends nothing on a server without voice', () async {+ final r = rig();+ r.swarm.routes.remove('/api/super-tabs/$tab/attachments/');+ final c = await open(r.reader);+ expect(await c.sendVoice(take, 'en'), (+ sent: false,+ notice: swarmVoiceNeedsServer,+ ));+ expect(r.swarm.commands, isEmpty);+ });++ test('a thread sends a take as a reply to its root', () async {+ final r = rig();+ final c = await open(r.reader);+ final thread = SwarmThread(c, 'root-1');+ addTearDown(thread.dispose);+ expect((await thread.sendVoice(take, 'en')).sent, isTrue);+ expect(r.swarm.commands.single, containsPair('replyTo', 'root-1'));+ expect(+ r.swarm.commands.single,+ containsPair('attachmentIds', ['id-Voice message.wav']),+ );+ });+}diff --git a/mobile/test/swarm/swarm_voice_test.dart b/mobile/test/swarm/swarm_voice_test.dartnew file mode 100644index 000000000..48c8b0b30--- /dev/null+++ b/mobile/test/swarm/swarm_voice_test.dart@@ -0,0 +1,174 @@+import 'dart:async';+import 'dart:typed_data';++import 'package:fake_async/fake_async.dart';+import 'package:flutter/widgets.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/phone/voice_notice.dart';+import 'package:harness_mobile/phone/voice_recorder.dart';+import 'package:harness_mobile/swarm/swarm_voice.dart';++import '../voice_fakes.dart';++/// The chat's mic records a voice message (task #2584): a take is sent as+/// audio, never as words in the box.+void main() {+ TestWidgetsFlutterBinding.ensureInitialized();++ late FakeVoiceRecorder mic;+ late List<VoiceTake> posted;+ String? refusal;++ SwarmVoice voice() {+ mic = FakeVoiceRecorder();+ posted = [];+ refusal = null;+ return SwarmVoice(+ recorder: () => mic,+ post: (take) async {+ posted.add(take);+ return (sent: refusal == null, notice: refusal);+ },+ );+ }++ void lifecycle(AppLifecycleState state) =>+ TestWidgetsFlutterBinding.instance.handleAppLifecycleStateChanged(state);++ tearDown(() => lifecycle(AppLifecycleState.resumed));++ test('a tap records, and Send posts the take as audio', () {+ fakeAsync((async) {+ final v = voice()..tap();+ async.flushMicrotasks();+ expect(v.state, SwarmVoiceState.recording);+ expect(mic.recording, isTrue);+ v.send();+ async.flushMicrotasks();+ expect(posted.single.wav, Uint8List.fromList([1, 2, 3, 4]));+ expect(v.state, SwarmVoiceState.off);+ });+ });++ test('X throws the take away and sends nothing', () {+ fakeAsync((async) {+ final v = voice()..tap();+ async.flushMicrotasks();+ v.discard();+ async.flushMicrotasks();+ expect(mic.cancels, 1);+ expect(v.state, SwarmVoiceState.off);+ expect(posted, isEmpty);+ });+ });++ test('a take stops at 10:00 with Send ready, and sends nothing itself', () {+ fakeAsync((async) {+ expect(SwarmVoice.maxTake, const Duration(minutes: 10));+ final v = voice()..tap();+ async.flushMicrotasks();+ async.elapse(const Duration(minutes: 9, seconds: 59));+ expect(v.state, SwarmVoiceState.recording);+ async.elapse(const Duration(seconds: 1));+ async.flushMicrotasks();+ expect(v.state, SwarmVoiceState.held);+ expect(mic.stops, 1);+ expect(v.canSend, isTrue);+ expect(posted, isEmpty);+ v.send();+ async.flushMicrotasks();+ expect(posted, hasLength(1));+ });+ });++ test('leaving the chat or locking the phone stops the take and keeps it', () {+ fakeAsync((async) {+ final v = voice()..tap();+ async.flushMicrotasks();+ v.hide();+ async.flushMicrotasks();+ expect(v.state, SwarmVoiceState.held);+ expect(mic.cancels, 0);+ expect(v.canSend, isTrue);++ final locked = voice()..tap();+ async.flushMicrotasks();+ // A system prompt or Control Center only covers the app: the take goes on.+ lifecycle(AppLifecycleState.inactive);+ async.flushMicrotasks();+ expect(locked.state, SwarmVoiceState.recording);+ lifecycle(AppLifecycleState.hidden);+ async.flushMicrotasks();+ expect(locked.state, SwarmVoiceState.held);+ expect(posted, isEmpty);+ });+ });++ test('a second tap stops the take and holds it', () {+ fakeAsync((async) {+ final v = voice()..tap();+ async.flushMicrotasks();+ v.tap();+ async.flushMicrotasks();+ expect(v.state, SwarmVoiceState.held);+ expect(v.length, const Duration(seconds: 2));+ });+ });++ test('a refused microphone says so and records nothing', () {+ fakeAsync((async) {+ final v = voice();+ mic.permitted = false;+ v.tap();+ async.flushMicrotasks();+ expect(v.state, SwarmVoiceState.off);+ expect(v.off, isTrue);+ expect(v.notice, VoiceNotice.unavailable);+ expect(mic.starts, 0);+ });+ });++ test('a send that throws keeps the take, Send and X ready', () {+ fakeAsync((async) {+ final v = SwarmVoice(+ recorder: FakeVoiceRecorder.new,+ post: (_) => Future.error(TimeoutException('no answer')),+ )..tap();+ async.flushMicrotasks();+ v.send();+ async.flushMicrotasks();+ expect(v.state, SwarmVoiceState.held);+ expect(v.canSend, isTrue);+ expect(v.notice, SwarmVoice.notSent);+ v.discard();+ expect(v.state, SwarmVoiceState.off);+ });+ });++ test('a sent take can still have something to say', () {+ fakeAsync((async) {+ final v = SwarmVoice(+ recorder: FakeVoiceRecorder.new,+ post: (_) async => (sent: true, notice: 'Sent without words'),+ )..tap();+ async.flushMicrotasks();+ v.send();+ async.flushMicrotasks();+ expect(v.state, SwarmVoiceState.off);+ expect(v.notice, 'Sent without words');+ });+ });++ test('a send that fails keeps the take, with what went wrong', () {+ fakeAsync((async) {+ final v = voice()..tap();+ async.flushMicrotasks();+ refusal = 'Voice messages need a newer server';+ v.send();+ async.flushMicrotasks();+ expect(v.state, SwarmVoiceState.held);+ expect(v.notice, 'Voice messages need a newer server');+ expect(v.canSend, isTrue);+ });+ });+}diff --git a/mobile/test/swarm/swarm_you_test.dart b/mobile/test/swarm/swarm_you_test.dartindex 4c841aaca..947f316c4 100644--- a/mobile/test/swarm/swarm_you_test.dart+++ b/mobile/test/swarm/swarm_you_test.dart@@ -26,13 +26,14 @@ import 'swarm_fixture.dart';/// and Your devices with what is ready, and Settings.void main() {/// The Swarms [swarms], each with the person as [email protected], [status]- /// set when given, its models on, and [models] served by the person's+ /// and [name] set when given, its models on, and [models] served by the person's/// machine Studio, read just now; and [others], more machines read just/// now, as given.FakeSwarm backend({List<Map<String, dynamic>> models = const [],List<Map<String, dynamic>> others = const [],Map<String, dynamic>? status,+ String? name,List<({String id, String name})> swarms = const [(id: tab, name: 'Autonomous'),],@@ -55,6 +56,7 @@ void main() {part(me, 1, {...person(me),'email': '[email protected]',+ 'name': ?name,'status': ?status,}),];@@ -176,14 +178,88 @@ void main() {expect(inside('swarm-you-me', find.text('Autonomous')), findsOneWidget);expect(inside('swarm-you-me', find.byType(SwarmAvatar)), findsOneWidget);expect(inside('swarm-you-status', find.text('Active')), findsOneWidget);- expect(inside('swarm-you-status', find.text('Change')), findsOneWidget);+ expect(inside('swarm-you-profile', find.text('Change')), findsOneWidget);- await tester.tap(key('swarm-you-status'));+ await tester.tap(key('swarm-you-profile'));await tester.pumpAndSettle();expect(find.byType(SwarmProfileSheet), findsOneWidget);});+ testWidgets('Status and Name are one Profile row: the name, the status '+ 'under it, one Change, then Username', (tester) async {+ await pump(+ tester,+ backend(+ name: 'Minh Nguyen',+ status: {'text': 'Focusing', 'emoji': '🎯'},+ ),+ );++ final captions = [+ for (final caption in tester.widgetList<SwarmCaption>(+ find.byType(SwarmCaption),+ ))+ caption.text,+ ];+ expect(captions, ['Profile', 'Username']);+ expect(key('swarm-you-name'), findsNothing);+ expect(inside('swarm-you-profile', find.text('Minh Nguyen')), findsOne);+ expect(inside('swarm-you-profile', find.text('🎯 Focusing')), findsOne);+ expect(inside('swarm-you-profile', find.text('Change')), findsOne);+ expect(+ tester+ .getTopLeft(inside('swarm-you-profile', find.text('Minh Nguyen')))+ .dy,+ lessThan(tester.getTopLeft(key('swarm-you-status')).dy),+ reason: 'the status sits under the name',+ );+ expect(+ tester.getTopLeft(key('swarm-you-profile')).dy,+ lessThan(tester.getTopLeft(key('swarm-you-username')).dy),+ );+ });++ testWidgets('with no name set the row asks for one, over Active', (+ tester,+ ) async {+ await pump(tester, backend());++ expect(inside('swarm-you-profile', find.text('Add your name')), findsOne);+ expect(inside('swarm-you-status', find.text('Active')), findsOne);+ });++ testWidgets('a long name and status stay on one line each on a small '+ 'phone at large text', (tester) async {+ await pump(+ tester,+ backend(+ name: 'Maximilian Alexander Featherstonehaugh-Wolfeschlegel',+ status: {+ 'text': 'Out for the whole afternoon at the dentist, back at five',+ 'emoji': '🦷',+ },+ ),+ );+ tester.view.physicalSize = const Size(960, 1704);+ tester.platformDispatcher.textScaleFactorTestValue = 1.6;+ addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);+ await tester.pumpAndSettle();++ expect(tester.takeException(), isNull);+ final row = tester.getRect(key('swarm-you-profile'));+ expect(row.right, lessThanOrEqualTo(320));+ expect(+ tester.getRect(inside('swarm-you-profile', find.text('Change'))).right,+ lessThanOrEqualTo(row.right),+ );+ for (final line in tester.widgetList<Text>(+ inside('swarm-you-profile', find.byType(Text)),+ )) {+ expect(line.maxLines, anyOf(isNull, 1), reason: '${line.data}');+ }+ });+testWidgets('a status set shows in place of Active', (tester) async {await pump(tester,@@ -432,9 +508,17 @@ void main() {expect(inside('swarm-you-me', find.text('You')), findsOneWidget);expect(find.byType(SwarmSkeletonRows), findsNothing);expect(find.textContaining('ready now'), findsNothing);- for (final row in ['status', 'models', 'devices', 'settings']) {+ for (final row in ['profile', 'username', 'models', 'devices']) {expect(key('swarm-you-$row'), findsOneWidget, reason: row);}+ expect(key('swarm-you-settings'), findsOneWidget);+ expect(inside('swarm-you-profile', find.text('Name')), findsOne);+ expect(inside('swarm-you-status', find.text('Set a status')), findsOne);+ expect(inside('swarm-you-profile', find.text('Change')), findsNothing);++ await tester.tap(key('swarm-you-profile'));+ await tester.pumpAndSettle();+ expect(find.byType(SwarmProfileSheet), findsNothing);});testWidgets('every row takes a thumb', (tester) async {@@ -444,7 +528,7 @@ void main() {computers: FakeComputers(const [studio])..own = [own(studio)],);- for (final row in ['status', 'models', 'devices', 'settings']) {+ for (final row in ['profile', 'username', 'models', 'devices', 'settings']) {final size = tester.getSize(key('swarm-you-$row'));expect(size.height, greaterThanOrEqualTo(44), reason: row);expect(size.width, greaterThanOrEqualTo(44), reason: row);diff --git a/mobile/test/terminal_look_test.dart b/mobile/test/terminal_look_test.dartnew file mode 100644index 000000000..f62ef82e1--- /dev/null+++ b/mobile/test/terminal_look_test.dart@@ -0,0 +1,307 @@+import 'package:flutter/material.dart';+import 'package:flutter/services.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/phone/agent_swipe.dart';+import 'package:harness_mobile/phone/phone_sheet.dart';+import 'package:harness_mobile/phone/terminal_page.dart';+import 'package:harness_mobile/phone/tty.dart';+import 'package:harness_mobile/shared/theme/app_theme.dart';+import 'package:harness_mobile/shared/theme/color_palette.dart';+import 'package:harness_mobile/shared/theme/system_bars.dart';+import 'package:harness_mobile/state/app_state.dart';+import 'package:harness_mobile/terminal/terminal_look.dart';+import 'package:lucide_icons_flutter/lucide_icons.dart';+import 'package:xterm/xterm.dart' show TerminalView;++import 'agent_pager_fixture.dart';++/// A [PagerConn] already through its handshake, so the page's open goes out at once.+class _HandshakenConn extends PagerConn {+ @override+ Future<void> waitUntilReady({required Duration timeout}) async {}+}++/// Keeps its State, so a test can tell a repaint from a remount.+class _Probe extends StatefulWidget {+ const _Probe();++ @override+ State<_Probe> createState() => _ProbeState();+}++class _ProbeState extends State<_Probe> {+ Color? ground;++ @override+ Widget build(BuildContext context) {+ ground = Tty.of(context).ground;+ return const SizedBox.expand();+ }+}++/// Settings' Theme turns every screen light but the terminal's page, which keeps the dark side+/// ([TerminalLook]): its chrome, its screen, its tokens, Material's theme and the status bar. A+/// sheet opened from it is the app's, like every other screen.+void main() {+ final paper = HarnessPalette.paper;+ final graphite = HarnessPalette.graphite;++ /// Light, chosen in Theme, as `applyAppearance` leaves it: the app on Paper, the terminal on+ /// Graphite.+ void themeLight() {+ AppTheme.palette.value = paper;+ AppTheme.brightness.value = Brightness.light;+ AppTheme.terminalPaletteOverride.value = graphite;+ }++ tearDown(() {+ AppTheme.palette.value = graphite;+ AppTheme.brightness.value = Brightness.dark;+ AppTheme.terminalPaletteOverride.value = null;+ });++ /// [home] as `HarnessApp` mounts it: the app's theme for the palette on, under the scope that+ /// makes `AppTheme.watch` work.+ Widget app(Widget home) => BrightnessScope(+ child: MaterialApp(+ theme: buildAppTheme(brightness: AppTheme.palette.value.brightness),+ home: home,+ ),+ );++ BuildContext? reached;+ Widget probe() => Builder(+ builder: (context) {+ reached = context;+ return const SizedBox.expand();+ },+ );++ testWidgets("off a terminal's page, everything reads the app's palette", (+ tester,+ ) async {+ themeLight();+ await tester.pumpWidget(app(probe()));+ final context = reached!;++ expect(TerminalLook.on(context), isFalse);+ expect(TerminalLook.paletteOf(context), paper);+ expect(+ TerminalLook.read(context, () => AppPalette.windowBg),+ paper.background,+ );+ expect(Tty.of(context).ground, paper.background);+ expect(TerminalLook.outside(context), same(context));+ });++ testWidgets("on one, the terminal's: Tty, the tokens, Material's theme and "+ 'the status bar', (tester) async {+ themeLight();+ await tester.pumpWidget(app(TerminalLook(child: probe())));+ final context = reached!;++ expect(TerminalLook.on(context), isTrue);+ expect(TerminalLook.paletteOf(context), graphite);+ expect(Tty.of(context).ground, graphite.background);+ expect(Tty.of(context).isLight, isFalse);+ expect(Theme.of(context).brightness, Brightness.dark);+ expect(Theme.of(context).colorScheme.surface, graphite.background);+ expect(+ tester+ .widget<AnnotatedRegion<SystemUiOverlayStyle>>(+ find.descendant(+ of: find.byType(TerminalLook),+ matching: find.byType(AnnotatedRegion<SystemUiOverlayStyle>),+ ),+ )+ .value,+ systemBarsFor(Brightness.dark),+ reason: 'light icons over the dark page',+ );++ var broadcasts = 0;+ void heard() => broadcasts++;+ AppTheme.palette.addListener(heard);+ AppTheme.brightness.addListener(heard);+ addTearDown(() {+ AppTheme.palette.removeListener(heard);+ AppTheme.brightness.removeListener(heard);+ });+ expect(+ TerminalLook.read(+ context,+ () => (AppPalette.windowBg, AppPalette.textPrimary),+ ),+ (+ graphite.background,+ AppTheme.as(Brightness.dark, () => AppPalette.textPrimary),+ ),+ );+ expect(AppTheme.palette.value, paper, reason: 'put back');+ expect(AppTheme.brightness.value, Brightness.light, reason: 'put back');+ expect(broadcasts, 0, reason: 'a read is not a change');+ });++ testWidgets('the palettes agreeing — App colors, or Dark — it changes '+ 'nothing', (tester) async {+ await tester.pumpWidget(app(TerminalLook(child: probe())));+ final context = reached!;++ expect(TerminalLook.paletteOf(context), graphite);+ expect(Theme.of(context).brightness, Brightness.dark);+ expect(+ TerminalLook.read(context, () => AppPalette.windowBg),+ graphite.background,+ );++ // App colors' Paper dresses the terminal too, as it always has.+ AppTheme.palette.value = paper;+ AppTheme.brightness.value = Brightness.light;+ await tester.pump();+ expect(TerminalLook.paletteOf(context), paper);+ expect(Tty.of(context).ground, paper.background);+ });++ testWidgets('a Theme switched under an open terminal repaints it and never '+ 'remounts it', (tester) async {+ await tester.pumpWidget(app(const TerminalLook(child: _Probe())));+ final state = tester.state<_ProbeState>(find.byType(_Probe));+ expect(state.ground, graphite.background);++ themeLight();+ await tester.pump();+ expect(tester.state(find.byType(_Probe)), same(state));+ expect(state.ground, graphite.background, reason: 'still dark');++ // Back to App colors' Paper: the terminal follows the app again.+ AppTheme.terminalPaletteOverride.value = null;+ await tester.pump();+ expect(tester.state(find.byType(_Probe)), same(state));+ expect(state.ground, paper.background);+ });++ testWidgets("a sheet opened from the page is the app's, not the page's", (+ tester,+ ) async {+ themeLight();+ await tester.pumpWidget(+ app(+ Scaffold(+ body: TerminalLook(+ child: Builder(+ builder: (context) => Center(+ child: TextButton(+ onPressed: () => showPhoneSheet(+ context,+ title: 'Menu',+ actions: [+ PhoneSheetAction(+ icon: LucideIcons.pencil300,+ label: 'Rename',+ onTap: () {},+ ),+ ],+ ),+ child: const Text('open'),+ ),+ ),+ ),+ ),+ ),+ ),+ );+ final page = tester.element(find.text('open'));+ expect(Theme.of(page).brightness, Brightness.dark);+ final outside = TerminalLook.outside(page);+ expect(TerminalLook.on(outside), isFalse);+ expect(Theme.of(outside).brightness, Brightness.light);++ await tester.tap(find.text('open'));+ await tester.pumpAndSettle();+ final sheet = tester.element(find.text('Rename'));+ expect(TerminalLook.on(sheet), isFalse);+ expect(+ Theme.of(sheet).brightness,+ Brightness.light,+ reason: "not the page's theme, carried across",+ );+ expect(Tty.of(sheet).ground, paper.background);+ expect(+ tester.widget<BottomSheet>(find.byType(BottomSheet)).backgroundColor,+ paper.background,+ reason: "the sheet's ground is the app's too",+ );+ });++ group("an agent's page", () {+ Future<AppNotifier> pumpPager(WidgetTester tester) async {+ final app = pagerApp(_HandshakenConn());+ addTearDown(app.dispose);+ final list = pagerList(app);+ await liveAgent(app, 'b');+ await tester.pumpWidget(+ BrightnessScope(+ child: MaterialApp(+ theme: buildAppTheme(brightness: AppTheme.palette.value.brightness),+ home: AgentSwipeHost(+ notifier: app,+ machineId: 'm',+ agentId: 'b',+ neighbours: list,+ ),+ ),+ ),+ );+ await tester.pump();+ return app;+ }++ /// Past the keyframe's ack and the view's first resize — timers the binding fails a test for+ /// leaving behind.+ Future<void> settle(WidgetTester tester) async {+ await tester.pump(const Duration(seconds: 2));+ await tester.pump();+ }++ Color? pageGround(WidgetTester tester) => tester+ .widget<Scaffold>(+ find+ .descendant(+ of: find.byType(TerminalPage),+ matching: find.byType(Scaffold),+ )+ .first,+ )+ .backgroundColor;++ Color screenGround(WidgetTester tester) => tester+ .widget<TerminalView>(find.byType(TerminalView).first)+ .theme+ .background;++ testWidgets('stays dark in an app Theme turned light', (tester) async {+ themeLight();+ await pumpPager(tester);++ expect(find.byType(TerminalLook), findsOneWidget);+ expect(pageGround(tester), graphite.background);+ expect(screenGround(tester), graphite.background);+ final header = tester.element(find.byType(TerminalPage).first);+ expect(Tty.of(header).ground, graphite.background);+ expect(Theme.of(header).brightness, Brightness.dark);+ await settle(tester);+ });++ testWidgets("wears App colors' Paper, as before Theme existed", (+ tester,+ ) async {+ AppTheme.palette.value = paper;+ AppTheme.brightness.value = Brightness.light;+ await pumpPager(tester);++ expect(pageGround(tester), paper.background);+ expect(screenGround(tester), paper.background);+ await settle(tester);+ });+ });+}