From f143462d96934a57f2437ab9ebbf0ff9cf5acca7 Mon Sep 17 00:00:00 2001 From: shanyuhai123 <864299347@qq.com> Date: Tue, 17 Nov 2026 15:58:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(mobile):=20=E5=AE=8C=E5=96=84=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E7=AE=A1=E7=90=86=E4=B8=8E=E7=A7=BB=E5=8A=A8=E7=AB=AF?= =?UTF-8?q?=E4=BA=A4=E4=BA=92=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scripts/__tests__/mobile-desktop.e2e.mjs | 33 +- .../scripts/__tests__/mobile-history.e2e.mjs | 2 +- .../__tests__/mobile-task-controls.e2e.mjs | 8 + .../service/src/chat/ChatQueueService.ts | 90 +++- .../chat/__tests__/ChatQueueService.spec.ts | 142 +++++++ .../ProductActorCommandScope.ts | 6 +- .../productProtocol/ProductBuddyDirectory.ts | 43 ++ .../productProtocol/ProductHistorySearch.ts | 72 ++++ .../src/productProtocol/ProductQueries.ts | 91 +++- .../productProtocol/ProductQueryRevisions.ts | 3 + .../productProtocol/ProductQueueControls.ts | 63 +++ .../src/productProtocol/ProductReadViews.ts | 10 +- .../ConversationArchiveCommands.spec.ts | 22 + .../__tests__/ProductDiscoveryReads.spec.ts | 219 ++++++++++ .../createProductCommandHandlers.ts | 2 + .../createProductSynchronization.ts | 14 +- apps/buddy/shared/productProtocol/queries.ts | 38 +- apps/buddy/shared/productProtocol/topics.ts | 13 +- apps/buddy/src/mobile-reader/MobileReader.vue | 29 +- .../__tests__/readerProjection.spec.ts | 18 +- .../__tests__/useReaderViewport.spec.ts | 143 +++++++ apps/buddy/src/mobile-reader/readerBridge.ts | 9 +- .../src/mobile-reader/readerProjection.ts | 14 +- .../buddy/src/mobile-reader/readerProtocol.ts | 4 + apps/buddy/src/mobile-reader/readerStrings.ts | 2 + .../buddy/src/mobile-reader/useReaderTurns.ts | 8 +- .../src/mobile-reader/useReaderViewport.ts | 113 ++++- .../AttachmentComposerInstrumentedTest.kt | 2 +- .../ComposerControlsInstrumentedTest.kt | 168 ++++++++ .../__tests__/ContentInstrumentedTest.kt | 4 +- .../DesktopWorkflowInstrumentedTest.kt | 22 +- .../__tests__/DeviceSwitchInstrumentedTest.kt | 21 +- .../__tests__/DraftUiInstrumentedTest.kt | 12 +- .../__tests__/HistoryInstrumentedTest.kt | 21 +- .../__tests__/LocalFilesInstrumentedTest.kt | 30 ++ .../__tests__/LocalizationInstrumentedTest.kt | 46 ++- .../mobile/__tests__/MobileUiTestDriver.kt | 30 +- .../PaperWorkbenchInstrumentedTest.kt | 36 +- .../__tests__/PreviewInstrumentedTest.kt | 28 ++ .../ProcessRecoveryInstrumentedTest.kt | 4 +- .../QuestionRetryInstrumentedTest.kt | 182 ++++++++ .../__tests__/SettingsUiInstrumentedTest.kt | 145 +++++++ .../__tests__/SharedReaderInstrumentedTest.kt | 60 ++- .../__tests__/SubscriptionInstrumentedTest.kt | 79 +++- .../SupportPanelsInstrumentedTest.kt | 143 +++++++ .../__tests__/TaskControlsInstrumentedTest.kt | 67 ++- .../TaskDiscoveryInstrumentedTest.kt | 113 +++++ .../mobile/preview/PreviewActivity.kt | 75 +++- .../mobile/protocol/ProductProjection.kt | 6 +- .../mobile/protocol/ProductSubscription.kt | 45 +- .../mobile/session/MobileConnectionSession.kt | 5 +- .../app/uselexora/mobile/ui/AboutSettings.kt | 65 +++ .../uselexora/mobile/ui/AppearanceSettings.kt | 136 ++++-- .../mobile/ui/CommandHistoryPanel.kt | 171 ++++++++ .../mobile/ui/CommandHistoryTarget.kt | 22 + .../uselexora/mobile/ui/ComposerToolbar.kt | 28 +- .../mobile/ui/ConversationSettings.kt | 185 +++++---- .../uselexora/mobile/ui/DraftRecoveryPanel.kt | 57 ++- .../uselexora/mobile/ui/LocalFilesPanel.kt | 39 +- .../app/uselexora/mobile/ui/MainActivity.kt | 389 ++++++++++++++---- .../mobile/ui/MobileFailurePresentation.kt | 6 +- .../mobile/ui/MobileModelControls.kt | 9 + .../uselexora/mobile/ui/MobileQuestions.kt | 132 ++++-- .../app/uselexora/mobile/ui/MobileShell.kt | 241 +++++------ .../app/uselexora/mobile/ui/MobileUiState.kt | 27 ++ .../mobile/ui/NativeComposerInput.kt | 104 +++++ .../uselexora/mobile/ui/PairingStatusPanel.kt | 40 ++ .../app/uselexora/mobile/ui/PaperWorkbench.kt | 170 ++++---- .../uselexora/mobile/ui/ReaderProjection.kt | 10 + .../uselexora/mobile/ui/ServiceSwitcher.kt | 23 +- .../uselexora/mobile/ui/SettingsComponents.kt | 103 +++++ .../app/uselexora/mobile/ui/TaskDiscovery.kt | 130 ++++++ .../mobile/ui/TaskDiscoveryController.kt | 99 +++++ .../app/uselexora/mobile/ui/TaskFileCopies.kt | 32 ++ .../app/uselexora/mobile/ui/TaskFilesPanel.kt | 52 ++- .../uselexora/mobile/ui/TaskMembersPanel.kt | 66 +++ .../app/uselexora/mobile/ui/TaskOperations.kt | 185 ++++++++- .../uselexora/mobile/ui/WebMessageTimeline.kt | 23 +- .../src/main/res/values-zh/strings.xml | 101 ++++- .../android/src/main/res/values/strings.xml | 102 ++++- .../__tests__/CommandHistoryTargetTest.kt | 47 +++ .../__tests__/MobileModelControlsTest.kt | 45 ++ .../__tests__/MobileQuestionCommandsTest.kt | 49 +++ .../__tests__/MobileQueueControlsTest.kt | 66 +++ .../mobile/__tests__/ProductProjectionTest.kt | 31 ++ .../ui/__tests__/ReaderProjectionTest.kt | 19 + 86 files changed, 4898 insertions(+), 661 deletions(-) create mode 100644 apps/buddy/service/src/productProtocol/ProductBuddyDirectory.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductHistorySearch.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductQueueControls.ts create mode 100644 apps/buddy/service/src/productProtocol/__tests__/ProductDiscoveryReads.spec.ts create mode 100644 apps/buddy/src/mobile-reader/__tests__/useReaderViewport.spec.ts create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ComposerControlsInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/QuestionRetryInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/SettingsUiInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/SupportPanelsInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/TaskDiscoveryInstrumentedTest.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/AboutSettings.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/CommandHistoryPanel.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/CommandHistoryTarget.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/NativeComposerInput.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/PairingStatusPanel.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/SettingsComponents.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/TaskDiscovery.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/TaskDiscoveryController.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/TaskFileCopies.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/TaskMembersPanel.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/CommandHistoryTargetTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/MobileModelControlsTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/MobileQuestionCommandsTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/MobileQueueControlsTest.kt diff --git a/.playwright/scripts/__tests__/mobile-desktop.e2e.mjs b/.playwright/scripts/__tests__/mobile-desktop.e2e.mjs index e1d63f3c7..c91e6a1d9 100644 --- a/.playwright/scripts/__tests__/mobile-desktop.e2e.mjs +++ b/.playwright/scripts/__tests__/mobile-desktop.e2e.mjs @@ -18,6 +18,7 @@ const serviceMode = process.env.LEXORA_TEST_MOBILE_SERVICE_MODE ?? 'utility' const bindAddress = process.env.LEXORA_TEST_MOBILE_BIND_ADDRESS ?? '127.0.0.1' const useAdbReverse = bindAddress === '127.0.0.1' const networkRecovery = process.env.LEXORA_TEST_MOBILE_NETWORK_RECOVERY === '1' +const serviceInterruption = process.env.LEXORA_TEST_MOBILE_SERVICE_INTERRUPTION === '1' test('real Desktop and Android keep independent local input and submit one immutable input with attachments', async ({ buddy }) => { test.skip(!serial, 'Requires an isolated Android device with the debug and instrumentation APKs installed') @@ -103,16 +104,21 @@ test('real Desktop and Android keep independent local input and submit one immut await new Promise(resolve => listener.close(resolve)) const configuration = test.info().outputPath('pairing.json') let instrument + let instrumentOutcome try { await page.evaluate(({ bindAddress, port }) => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: true, bindAddress, port }), { bindAddress, port }) const pairing = await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.issuePairing({})) - await fs.writeFile(configuration, JSON.stringify({ pairing, networkRecovery, avatarHash, draftId: draft.draftId, originalText }), { mode: 0o600 }) + await fs.writeFile(configuration, JSON.stringify({ pairing, networkRecovery: networkRecovery || serviceInterruption, avatarHash, draftId: draft.draftId, originalText }), { mode: 0o600 }) if (useAdbReverse) await adb('reverse', `tcp:${port}`, `tcp:${port}`) await adb('push', configuration, remoteConfig) await fs.rm(configuration) instrument = adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.DesktopWorkflowInstrumentedTest', '-e', 'desktopWorkflowToken', token, `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`) .then(result => ({ result }), error => ({ error })) + .then((value) => { + instrumentOutcome = value + return value + }) let checkpoint await expect.poll(async () => { try { @@ -138,20 +144,33 @@ test('real Desktop and Android keep independent local input and submit one immut await fs.writeFile(continuation, JSON.stringify({ text: updatedText })) await adb('push', continuation, `/data/local/tmp/lexora-desktop-${token}-resume.json`) await adb('shell', 'run-as', appPackage, 'cp', `/data/local/tmp/lexora-desktop-${token}-resume.json`, resume) - if (networkRecovery) { + if (networkRecovery || serviceInterruption) { const waitMarker = stage => expect.poll(async () => { try { return (await adb('shell', 'run-as', appPackage, 'cat', `files/desktop-${token}-${stage}.json`)).stdout.trim() === 'ready' } - catch { return false } + catch { + if (instrumentOutcome) + throw new Error(instrumentOutcome.result?.stdout ?? String(instrumentOutcome.error)) + return false + } }, { timeout: 45000 }).toBe(true) - expect((await adb('shell', 'settings', 'get', 'global', 'wifi_on')).stdout.trim()).toBe('1') + if (networkRecovery) + expect((await adb('shell', 'settings', 'get', 'global', 'wifi_on')).stdout.trim()).toBe('1') await waitMarker('network-ready') - await adb('shell', 'svc', 'wifi', 'disable') + if (serviceInterruption) + await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false })) + else + await adb('shell', 'svc', 'wifi', 'disable') try { await waitMarker('offline') } - finally { await adb('shell', 'svc', 'wifi', 'enable') } + finally { + if (serviceInterruption) + await page.evaluate(({ bindAddress, port }) => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: true, bindAddress, port }), { bindAddress, port }) + else + await adb('shell', 'svc', 'wifi', 'enable') + } } const outcome = await instrument await fs.writeFile(test.info().outputPath('android-instrumentation.txt'), outcome.result?.stdout ?? String(outcome.error)) @@ -174,7 +193,7 @@ test('real Desktop and Android keep independent local input and submit one immut await expect(page.locator('.desktop-workbench-area__tasks img').filter({ visible: true }).and(page.locator(`img[src="${avatar}"]`)).first()).toBeVisible() await page.screenshot({ path: test.info().outputPath('desktop-conversation.png') }) - await fs.writeFile(test.info().outputPath('workflow.json'), JSON.stringify({ ...checkpoint, localDrafts: true, immutableInput: true, nativeAndroidUi: true, electronUi: true, coreAdapter: serviceMode, wifiInterruptionRecovery: networkRecovery, customAvatar: true, avatarHash, offlineAvatar: networkRecovery, transport: useAdbReverse ? 'adb-reverse' : 'private-network', serverAddress: bindAddress }, null, 2)) + await fs.writeFile(test.info().outputPath('workflow.json'), JSON.stringify({ ...checkpoint, localDrafts: true, immutableInput: true, nativeAndroidUi: true, electronUi: true, coreAdapter: serviceMode, wifiInterruptionRecovery: networkRecovery, serviceInterruptionRecovery: serviceInterruption, customAvatar: true, avatarHash, offlineAvatar: networkRecovery || serviceInterruption, transport: useAdbReverse ? 'adb-reverse' : 'private-network', serverAddress: bindAddress }, null, 2)) } finally { if (networkRecovery) diff --git a/.playwright/scripts/__tests__/mobile-history.e2e.mjs b/.playwright/scripts/__tests__/mobile-history.e2e.mjs index 103d1ddf5..64410997c 100644 --- a/.playwright/scripts/__tests__/mobile-history.e2e.mjs +++ b/.playwright/scripts/__tests__/mobile-history.e2e.mjs @@ -156,7 +156,7 @@ test('Android historical followup, edit and regeneration preserve branches, rest }, original.conversation_id) await signal('task-deleted') const evidence = await checkpoint('done') - expect(evidence).toMatchObject({ independentBranchEditAccepted: true, deletedSourceRejection: { decision: 'rejected', errorCode: 'SOURCE_CHANGED' } }) + expect(evidence).toMatchObject({ independentBranchEditAccepted: true, deletedSourceUiBlocked: true, deletedSourceRejection: { decision: 'rejected', errorCode: 'SOURCE_CHANGED' } }) const result = await instrument expect(result.error).toBeUndefined() expect(result.result.stdout).toMatch(/OK \(1 test\)/) diff --git a/.playwright/scripts/__tests__/mobile-task-controls.e2e.mjs b/.playwright/scripts/__tests__/mobile-task-controls.e2e.mjs index a70f4d4f3..334a8080b 100644 --- a/.playwright/scripts/__tests__/mobile-task-controls.e2e.mjs +++ b/.playwright/scripts/__tests__/mobile-task-controls.e2e.mjs @@ -195,6 +195,14 @@ test('Android direct stop, queue, branch switching and read state preserve exact } catch (error) { cleanupErrors.push(error) } } + if (started && failure) { + await cleanup(async () => { + const log = await adb('logcat', '-d', '-t', '1000', '-v', 'threadtime') + await fs.writeFile(testInfo.outputPath('android-failure-logcat.txt'), log.stdout) + const capture = await execute(process.env.ADB ?? 'adb', ['-s', serial, 'exec-out', 'screencap', '-p'], { encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 }) + await fs.writeFile(testInfo.outputPath('android-failure.png'), capture.stdout) + }) + } if (started) await cleanup(() => adb('shell', 'am', 'force-stop', appPackage)) const result = await instrument diff --git a/apps/buddy/service/src/chat/ChatQueueService.ts b/apps/buddy/service/src/chat/ChatQueueService.ts index 673888619..e2f0517a3 100644 --- a/apps/buddy/service/src/chat/ChatQueueService.ts +++ b/apps/buddy/service/src/chat/ChatQueueService.ts @@ -1,6 +1,7 @@ import type { LocalChatQueueReceipt, LocalChatQueueScope, LocalChatQueueSteerResult, LocalChatQueueTarget } from '../../../shared/conversation/chatQueueApi' import type { EventSnapshot } from '../../../shared/events/eventTypes' import type { BuddyExecutionProfile } from '../../../shared/permissions/executionProfile' +import type { ProductQueueControl } from '../../../shared/productProtocol/queries' import type { LocalRun } from '../../../shared/runs/runApi' import type { BuddyAgentRunner } from '../agent/execution/BuddyAgentRunner' import type { BuddyTurnLauncher } from '../agent/execution/BuddyTurnLauncher' @@ -103,6 +104,78 @@ export class ChatQueueService { }) } + async inspectControls(target: LocalChatQueueTarget): Promise & { steerTarget: { runId: string, generation: number } | null }> { + const result: Pick & { steerTarget: { runId: string, generation: number } | null } = { allowedActions: [], steerTarget: null, blockedReason: 'service_unavailable' } + const accepting = () => { + this.#options.control?.assertAccepting() + return !this.#disposed + } + try { + if (!accepting()) + return result + } + catch { return result } + const condition = this.#options.control?.queueTarget(target) + if (!condition) + return { ...result, blockedReason: 'target_unavailable' } + result.allowedActions.push('cancel') + if (this.#options.runner.isStopping || this.#options.eventLog.state !== 'open') + return result + const reason = (error: unknown): ProductQueueControl['blockedReason'] => { + const code = error && typeof error === 'object' && 'code' in error ? error.code : null + return code === 'INPUT_DELIVERY_TARGET_UNAVAILABLE' || code === 'SOURCE_CHANGED' + ? 'target_unavailable' + : code === 'INPUT_DELIVERY_INCOMPATIBLE' || code === 'BUDDY_CONFLICT' + ? 'input_incompatible' + : code === 'SERVICE_STOPPING' ? 'service_unavailable' : 'input_unavailable' + } + try { + const pending = this.#options.queue.pending(target) + if (!pending) + return { ...result, allowedActions: [], blockedReason: 'target_unavailable' } + result.blockedReason = 'busy' + if (this.#canDispatch(pending)) { + try { + this.#options.queue.validateDelivery(pending) + const prepared = await this.prepareContinue(target) + this.#assertContinueReady(prepared) + result.allowedActions.push('continue') + } + catch (error) { result.blockedReason = reason(error) } + } + const active = this.#options.queue.activeRun(target, pending.buddyId) + if (active) { + try { + const run = this.#options.runs.findById(active.id)! + this.#options.control?.assertRun({ runId: run.id, expectedRunGeneration: run.generation }, true) + const prepared = await this.prepareSteer(target, run.id) + this.#options.control?.assertRun({ runId: run.id, expectedRunGeneration: run.generation }, true) + this.#assertSteerReady(prepared.input, run.id) + this.#options.queue.validateDelivery(prepared.input) + result.allowedActions.push('steer') + result.steerTarget = { runId: run.id, generation: run.generation } + } + catch (error) { result.blockedReason = reason(error) } + } + else if (pending.delivery?.mode === 'current' && pending.delivery.target) { + result.blockedReason = 'target_unavailable' + } + this.#options.control?.assertQueue(condition) + if (!accepting()) + return { allowedActions: [], steerTarget: null, blockedReason: 'service_unavailable' } + if (this.#options.runner.isStopping || this.#options.eventLog.state !== 'open') + return { allowedActions: ['cancel'], steerTarget: null, blockedReason: 'service_unavailable' } + } + catch (error) { + result.blockedReason = reason(error) + result.allowedActions = result.blockedReason === 'service_unavailable' ? [] : result.allowedActions.filter(action => action === 'cancel') + result.steerTarget = null + } + if (result.allowedActions.some(action => action !== 'cancel')) + result.blockedReason = null + return result + } + cancelRun(runId: string): Promise { const pending = this.#cancellations.get(runId) if (pending) @@ -248,8 +321,7 @@ export class ChatQueueService { return { input, runId, promoteNext } } - commitSteer(prepared: Awaited>) { - const { input, runId, promoteNext } = prepared + #assertSteerReady(input: QueuedTurnRequestInput, runId: string) { const current = this.#options.runs.findById(runId) if (this.#disposed || this.#options.eventLog.state !== 'open' || this.#isCancelling(input, input.buddyId) || !current || !this.#canFollowUp(current, input) || !this.#options.queue.isCurrentInput(input) @@ -257,6 +329,13 @@ export class ChatQueueService { throw new BuddyServiceError('INPUT_DELIVERY_TARGET_UNAVAILABLE') } this.#options.availability?.assertRunExecutable(runId) + if (current.purpose !== 'chat' || !['root', 'actor_child'].includes(current.executionKind)) + throw new BuddyServiceError('INPUT_DELIVERY_TARGET_UNAVAILABLE') + } + + commitSteer(prepared: Awaited>) { + const { input, runId, promoteNext } = prepared + this.#assertSteerReady(input, runId) const reference = this.#inputReference(input) this.#options.queue.commitInRun(input, runId, this.#options.getRunPermissionCeiling(runId), promoteNext) return () => { @@ -275,13 +354,18 @@ export class ChatQueueService { return { input, sourceRunId: latest?.id ?? null, sourceGeneration: latest ? this.#options.runs.findById(latest.id)?.generation ?? null : null } } - commitContinue(prepared: Awaited>) { + #assertContinueReady(prepared: Awaited>) { const { input } = prepared const latest = this.#options.queue.latestRun(input, input.buddyId) if (!this.#canDispatch(input) || !this.#options.queue.isCurrentInput(input) || (latest?.id ?? null) !== prepared.sourceRunId || (latest ? this.#options.runs.findById(latest.id)?.generation ?? null : null) !== prepared.sourceGeneration) { throw new BuddyServiceError('INPUT_DELIVERY_TARGET_UNAVAILABLE') } + } + + commitContinue(prepared: Awaited>) { + this.#assertContinueReady(prepared) + const { input } = prepared const request = this.#options.requests.prepare({ ...input, createdAt: new Date().toISOString() }) return { request, notify: async () => { this.#changed('dispatched', input) diff --git a/apps/buddy/service/src/chat/__tests__/ChatQueueService.spec.ts b/apps/buddy/service/src/chat/__tests__/ChatQueueService.spec.ts index 72c2f1a9e..cce9578e0 100644 --- a/apps/buddy/service/src/chat/__tests__/ChatQueueService.spec.ts +++ b/apps/buddy/service/src/chat/__tests__/ChatQueueService.spec.ts @@ -3,6 +3,7 @@ import type { DatabaseSync } from 'node:sqlite' import type { BuddyPermissionSettings } from '../../../../shared/permissions/permissionMode' import type { BuddyInputReferenceV1 } from '../../agent/context/BuddyInputReference' import type { ExecutionSettled } from '../../agent/execution/ActiveRunRegistry' +import type { BuddyServiceCommands } from '../../BuddyService' import type { BuddyRunEvent } from '../../events/BuddyRunEvent' import type { RunEventObservation } from '../../events/RunEventPorts' import type { RuntimeModelProvider } from '../../providers/resolveInteractiveModelSelection' @@ -15,10 +16,14 @@ import { buddySettingsSchema } from '../../../../shared/buddies/buddySettings' import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' import { Emitter } from '../../../../shared/events/Emitter' import { resolveBuddyPermissionSettings } from '../../../../shared/permissions/permissionMode' +import { productQueueControlsInputSchema, productQueueControlsPageSchema } from '../../../../shared/productProtocol/queries' import { BuddyDefinitionResolver } from '../../buddies/BuddyDefinitionResolver' import { CommandAdmissionService, createLocalCommandPrincipal } from '../../commands/CommandAdmissionService' import { ConversationMetadataService } from '../../conversations/ConversationMetadataService' import { RunEventCorruptionError } from '../../events/RunEventFailure' +import { createProductCommandHandlers } from '../../productProtocol/createProductCommandHandlers' +import { ProductConnection } from '../../productProtocol/ProductConnection' +import { ProductQueueControls } from '../../productProtocol/ProductQueueControls' import { resolveInteractiveModelSelection } from '../../providers/resolveInteractiveModelSelection' import { toPublicRun } from '../../runs/publicRun' import { createAttachmentRepository } from '../../storage/attachmentRepository' @@ -1058,6 +1063,143 @@ describe('conditional queue and stop commands', () => { }) }) +describe('public queue control availability', () => { + const read = async (f: ReturnType, extra: Record = {}) => productQueueControlsPageSchema.parse(await new ProductQueueControls(f.database, 'instance', f.service).read(productQueueControlsInputSchema.parse({ ...f.scope, ...extra }))) + const delivery = (mode: 'current' | 'next') => ({ mode, actorId: 'default', target: { rootRunId: 'run-initial', runId: 'run-initial' } }) + const closeSource = (f: ReturnType) => { + f.database.exec('UPDATE runs SET status = \'completed\'; UPDATE execution_state SET phase = \'closed\', cleanup_state = \'clean\'') + } + + it('offers exact compatible live steering without mutating a next intent or accepting a command', async () => { + const f = commandFixture() + await f.enableDefinitions() + f.queue.enqueue({ ...f.input('next'), buddyId: 'default', delivery: delivery('next') }) + const before = f.database.prepare('SELECT total_changes() AS count').get()!.count + const page = await read(f) + expect(page.items).toMatchObject([{ id: 'next', revision: '0', delivery: delivery('next'), allowedActions: ['cancel', 'steer'], steerTarget: { runId: 'run-initial', generation: '0' }, blockedReason: null }]) + expect(f.database.prepare('SELECT total_changes() AS count').get()!.count).toBe(before) + expect(f.delivered).toEqual([]) + expect(f.launched).toEqual([]) + expect(f.database.prepare('SELECT count(*) AS count FROM command_receipts').get()!.count).toBe(0) + expect(JSON.stringify(page)).not.toMatch(/prepared_json|runInput|attachmentBindings|actorSnapshot/) + }) + + it('allows next continuation only after the bound source closes cleanly and its runtime slot releases', async () => { + const f = commandFixture() + f.queue.enqueue({ ...f.input('next'), buddyId: 'default', delivery: delivery('next') }) + f.queue.pauseItem(f.target('next')) + closeSource(f) + expect((await read(f)).items[0]).toMatchObject({ allowedActions: ['cancel'], blockedReason: 'busy' }) + f.releaseSlot() + expect((await read(f)).items[0]).toMatchObject({ allowedActions: ['cancel', 'continue'], steerTarget: null, blockedReason: null }) + f.database.exec('UPDATE execution_state SET cleanup_state = \'degraded\'') + expect((await read(f)).items[0]?.allowedActions).toEqual(['cancel']) + expect(f.launched).toEqual([]) + }) + + it.each(['closed', 'missing'] as const)('never offers fixed-current continue or steer when its source is %s', async (state) => { + const f = commandFixture() + f.queue.enqueue({ ...f.input('current'), buddyId: 'default', delivery: delivery('current') }) + f.queue.pauseItem(f.target('current')) + closeSource(f) + f.releaseSlot() + if (state === 'missing') + f.database.exec('UPDATE chat_queue SET prepared_json = json_set(prepared_json, \'$.delivery.target.runId\', \'missing-run\') WHERE id = \'current\'') + const item = (await read(f)).items[0]! + expect(item).toMatchObject({ allowedActions: ['cancel'], steerTarget: null, blockedReason: 'target_unavailable' }) + const rejected = await f.commands.execute('chat.queue.continue', f.command('continue-invalid', { ...f.target('current'), expectedRevision: Number(item.revision) })) + expect(rejected.receipt).toMatchObject({ decision: 'rejected', errorCode: 'INPUT_DELIVERY_TARGET_UNAVAILABLE' }) + expect(f.runs.findById('run-current')).toBeNull() + expect(f.queue.list(f.scope)).toMatchObject([{ id: 'current', state: 'paused' }]) + }) + + it('distinguishes a current idle intent from a fixed source and keeps archived tasks cancel-only', async () => { + const f = commandFixture() + closeSource(f) + f.releaseSlot() + f.queue.enqueue({ ...f.input('idle'), buddyId: 'default', delivery: { mode: 'current', actorId: 'default', target: null } }) + expect((await read(f)).items[0]).toMatchObject({ delivery: { target: null }, allowedActions: ['cancel', 'continue'] }) + f.database.prepare('UPDATE conversations SET archived_at = ? WHERE id = ?').run('2026-11-01T00:00:00.000Z', 'conversation') + expect((await read(f)).items[0]).toMatchObject({ allowedActions: ['cancel'], blockedReason: 'archived' }) + }) + + it('withholds private delivery identities and still allows cancelling an unusable pending input', async () => { + const f = commandFixture() + f.queue.enqueue({ ...f.input('invalid'), buddyId: 'default', delivery: delivery('current') }) + f.database.exec('UPDATE runs SET execution_kind = \'npc_child\' WHERE id = \'run-initial\'') + expect((await read(f)).items[0]).toMatchObject({ delivery: null, allowedActions: ['cancel'], steerTarget: null, blockedReason: 'target_unavailable' }) + const rejectedScope = new ProductQueueControls(f.database, 'instance', f.service).read(productQueueControlsInputSchema.parse({ conversationId: 'conversation', branchId: 'not-owned' })) + await expect(rejectedScope).rejects.toMatchObject({ code: 'OBJECT_DELETED' }) + }) + + it('keeps cancel available when input execution is unavailable but command admission is still open', async () => { + const f = commandFixture() + f.queue.enqueue(f.input('cancel-only')) + f.runner.isStopping = true + expect((await read(f)).items[0]).toMatchObject({ allowedActions: ['cancel'], blockedReason: 'service_unavailable' }) + }) + + it('rechecks run generation, service gates and queue revision after asynchronous input validation', async () => { + const f = commandFixture() + f.queue.enqueue({ ...f.input('next'), buddyId: 'default', delivery: delivery('next') }) + f.validate.mockImplementationOnce(async () => { + f.database.exec('UPDATE runs SET generation = generation + 1 WHERE id = \'run-initial\'') + }) + expect((await read(f)).items[0]).toMatchObject({ allowedActions: ['cancel'], steerTarget: null, blockedReason: 'target_unavailable' }) + f.validate.mockImplementationOnce(async () => { + f.queue.cancel(f.target('next')) + }) + await expect(read(f)).rejects.toMatchObject({ code: 'QUEUE_CHANGED' }) + f.queue.enqueue({ ...f.input('stopped'), buddyId: 'default', delivery: delivery('next') }) + f.database.exec('INSERT INTO service_admission_gates(source_instance_id, stopped_at, command_id) VALUES (\'instance\',\'2026-11-01T00:00:00.000Z\',\'stop\')') + expect((await read(f)).items[0]).toMatchObject({ allowedActions: [], blockedReason: 'service_unavailable' }) + }) + + it('keeps control pagination independent and exposes the read handler to readers without write authority', async () => { + const f = commandFixture() + f.queue.enqueue({ ...f.input('first'), buddyId: 'default', delivery: delivery('next') }) + f.queue.enqueue({ ...f.input('second'), buddyId: 'default', delivery: delivery('next') }) + const first = await read(f, { limit: 1 }) + expect(first.items.map(item => item.id)).toEqual(['first']) + expect(first.nextCursor).toBeTypeOf('string') + expect((await read(f, { limit: 1, cursor: first.nextCursor })).items.map(item => item.id)).toEqual(['second']) + const frames: Array<{ result?: unknown, error?: { code: string } }> = [] + const unexpectedDependency = (): never => { + throw new Error('Queue control reads must not access unrelated command dependencies') + } + const commands: BuddyServiceCommands = { + identity: { dataGeneration: String(f.database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation), serviceInstanceId: 'instance' }, + admission: f.admission, + control: f.commands, + queueControls: new ProductQueueControls(f.database, 'instance', f.service), + get desktop() { return unexpectedDependency() }, + get input() { return unexpectedDependency() }, + get drafts() { return unexpectedDependency() }, + get metadata() { return unexpectedDependency() }, + get uploads() { return unexpectedDependency() }, + get approvals() { return unexpectedDependency() }, + get questions() { return unexpectedDependency() }, + } + const handlers = createProductCommandHandlers({ commands, assertOwner: () => { + throw new Error('Unexpected owner-only access') + }, allowedActions: new Set(), resolveQuote: () => { + throw new Error('Unexpected quote') + } }) + const connection = new ProductConnection({ context: { serviceId: 'service', profileId: 'profile', dataGeneration: 'generation', serviceInstanceId: 'instance', connectionId: 'reader', clientSessionId: 'session', deviceId: 'device', ownerPrincipalId: 'owner', credentialId: 'credential', grantGeneration: '1', role: 'reader' }, assertCurrent: () => {}, phase: () => 'ready', handlers, send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, close: () => {} }) + try { + await connection.receive(JSON.stringify({ jsonrpc: '2.0', id: 'init', method: 'initialize', params: { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'mobile', version: '1' }, supportedContentKinds: [], supportedReviewKinds: [] } })) + await connection.receive(JSON.stringify({ jsonrpc: '2.0', id: 'controls', method: 'conversations.getQueueControls', params: f.scope })) + expect(frames.at(-1)?.result).toMatchObject({ items: [{ id: 'first' }, { id: 'second' }] }) + await connection.receive(JSON.stringify({ jsonrpc: '2.0', id: 'cancel', method: 'chat.queue.cancel', params: {} })) + expect(frames.at(-1)?.error?.code).toBe('NOT_AUTHORIZED') + } + finally { connection.close() } + }) +}) + function commandFixture() { const f = fixture(false, undefined, true) f.database.exec('UPDATE execution_state SET phase = \'working\'') diff --git a/apps/buddy/service/src/productProtocol/ProductActorCommandScope.ts b/apps/buddy/service/src/productProtocol/ProductActorCommandScope.ts index bb4577c8b..f2b47b46e 100644 --- a/apps/buddy/service/src/productProtocol/ProductActorCommandScope.ts +++ b/apps/buddy/service/src/productProtocol/ProductActorCommandScope.ts @@ -1,5 +1,6 @@ import type { DatabaseSync } from 'node:sqlite' import type { ApprovalService } from '../approvals/ApprovalService' +import type { ChatQueueService } from '../chat/ChatQueueService' import type { TrustedCommandPrincipal } from '../commands/CommandAdmissionService' import type { RemoteUploadService } from '../privateNetwork/RemoteUploadService' import type { UserQuestionService } from '../questions/UserQuestionService' @@ -12,6 +13,7 @@ import { CommandAdmissionService } from '../commands/CommandAdmissionService' import { DesktopOpenCommandService } from '../commands/DesktopOpenCommandService' import { ConversationCommandService } from '../conversations/ConversationCommandService' import { ConversationCommandRepository } from '../storage/ConversationCommandRepository' +import { ProductQueueControls } from './ProductQueueControls' export interface ProductActorCommands { identity: { dataGeneration: string, serviceInstanceId: string } @@ -20,6 +22,7 @@ export interface ProductActorCommands { input: ChatInputCommandService drafts: ComposerDraftCommandService control: ChatControlCommandService + queueControls: Pick metadata: ConversationCommandService uploads: ReturnType approvals: Pick @@ -36,7 +39,7 @@ interface ProductActorCommandDependencies { input: Pick drafts: Pick & { access: DraftOptions['drafts'] & InputOptions['drafts'] } conversations: Pick - control: Pick & { stopExecution: () => Promise } + control: Pick & { queue: ControlOptions['queue'] & Pick, stopExecution: () => Promise } uploads: Pick questions: Pick approvals: Pick @@ -88,6 +91,7 @@ export class ProductActorCommandScope { input, drafts, control, + queueControls: new ProductQueueControls(database, this.#identity.serviceInstanceId, dependencies.control.queue), metadata, uploads: dependencies.uploads.commands(principal, admission), questions: { execute: (method, input) => dependencies.questions.executeCommand(method, input, admission) }, diff --git a/apps/buddy/service/src/productProtocol/ProductBuddyDirectory.ts b/apps/buddy/service/src/productProtocol/ProductBuddyDirectory.ts new file mode 100644 index 000000000..0f67420da --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductBuddyDirectory.ts @@ -0,0 +1,43 @@ +import type { DatabaseSync } from 'node:sqlite' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { productBuddyProfileSchema } from '../../../shared/productProtocol/queries' +import { encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { createConversationMemberRepository } from '../storage/conversationMemberRepository' +import { ProductPageReader } from './ProductPageReader' + +export class ProductBuddyDirectory { + readonly #database: DatabaseSync + readonly #pages: ProductPageReader + + constructor(database: DatabaseSync, instance: string) { + this.#database = database + this.#pages = new ProductPageReader(database, instance) + } + + profile(id: string) { + const row = this.#database.prepare('SELECT id, name, description, enabled, deleted_at, revision FROM buddies WHERE id = ? AND kind = \'actor\'').get(id) + return row ? productBuddyProfileSchema.parse({ id: row.id, name: row.name, description: row.description, enabled: row.enabled === 1, deleted: row.deleted_at !== null, revision: encodeProductRevision(Number(row.revision)) }) : null + } + + list(input: Record) { + const catalog = this.#database.prepare('SELECT revision, default_buddy_id FROM buddy_catalog WHERE id = 1').get()! + const catalogRevision = encodeProductRevision(Number(catalog.revision)) + return { ...this.#pages.read('buddies.list', input, 'buddies', 'kind = \'actor\' AND deleted_at IS NULL', [], id => this.profile(id), catalogRevision), catalogRevision, defaultBuddyId: String(catalog.default_buddy_id) } + } + + members(input: Record) { + const conversationId = String(input.conversationId) + const task = this.#database.prepare('SELECT buddy_id, metadata_revision FROM conversations WHERE id = ? AND deleted_at IS NULL').get(conversationId) + if (!task) + throw new ProductProtocolError('OBJECT_DELETED') + const revision = encodeProductRevision(Number(task.metadata_revision)) + const catalog = this.#database.prepare('SELECT revision FROM buddy_catalog WHERE id = 1').get()! + const members = createConversationMemberRepository(this.#database) + const page = this.#pages.read(`conversations.listMembers:${conversationId}:${revision}`, input, '(SELECT rowid, actor_id AS id, conversation_id, removed_at FROM conversation_members)', 'conversation_id = ? AND removed_at IS NULL', [conversationId], (id) => { + const member = members.find(conversationId, id) + const profile = this.profile(id) + return member && profile ? { actorId: id, profile, modelSelection: member.modelSelection, modelSelectionSource: member.modelSelectionSource, joinedAt: member.joinedAt } : null + }, encodeProductRevision(Number(catalog.revision))) + return { ...page, conversationId, buddyId: String(task.buddy_id), revision } + } +} diff --git a/apps/buddy/service/src/productProtocol/ProductHistorySearch.ts b/apps/buddy/service/src/productProtocol/ProductHistorySearch.ts new file mode 100644 index 000000000..cfdc5c1e0 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductHistorySearch.ts @@ -0,0 +1,72 @@ +import type { DatabaseSync, SQLInputValue } from 'node:sqlite' +import type { MessageRole } from '../storage/conversationHistoryRepository' +import type { ProductReadViews } from './ProductReadViews' +import { redactSensitiveText } from '../../../shared/permissions/approvalReviewPayload' +import { ProductPageReader } from './ProductPageReader' + +const documentBytes = 262_144 + +export class ProductHistorySearch { + readonly #database: DatabaseSync + readonly #pages: ProductPageReader + readonly #views: ProductReadViews + + constructor(database: DatabaseSync, instance: string, views: ProductReadViews) { + this.#database = database + this.#pages = new ProductPageReader(database, instance) + this.#views = views + } + + search(input: Record) { + const query = String(input.query).toLowerCase() + const conditions = ['deleted_at IS NULL', input.scope === 'archived' ? 'archived_at IS NOT NULL' : 'archived_at IS NULL'] + const params: SQLInputValue[] = [] + if (input.spaceId !== undefined) { + conditions.push('space_id IS ?') + params.push(input.spaceId as string | null) + } + if (input.conversationId !== undefined) { + conditions.push('conversation_id = ?') + params.push(String(input.conversationId)) + } + const table = `(SELECT c.rowid * 2 AS rowid, 'task:' || c.id AS id, c.id AS conversation_id, c.space_id, c.deleted_at, c.archived_at FROM conversations c WHERE c.active_branch_id IS NOT NULL + UNION ALL SELECT m.rowid * 2 + 1 AS rowid, 'message:' || m.id AS id, c.id AS conversation_id, c.space_id, c.deleted_at, c.archived_at + FROM messages m JOIN conversations c ON c.id = m.conversation_id + WHERE m.role IN ('user', 'assistant') AND (m.run_id IS NULL OR EXISTS ( + SELECT 1 FROM runs r WHERE r.id = m.run_id AND r.conversation_id = m.conversation_id AND r.branch_id = m.branch_id + AND r.execution_kind IN ('root', 'actor_child') AND (r.execution_kind = 'root' OR m.role = 'assistant' OR EXISTS (SELECT 1 FROM conversation_publications p WHERE p.message_id = m.id)))))` + let skippedOversized = 0 + const page = this.#pages.read(`conversations.search:${input.scope ?? 'active'}:${query}`, input, table, conditions.join(' AND '), params, (key) => { + const task = key.startsWith('task:') + const id = key.slice(task ? 5 : 8) + const row = task + ? this.#database.prepare('SELECT id AS conversationId, active_branch_id AS branchId, title, space_id AS spaceId FROM conversations WHERE id = ?').get(id)! + : this.#database.prepare(`SELECT m.conversation_id AS conversationId, m.branch_id AS branchId, c.title, c.space_id AS spaceId, m.role, m.created_at AS createdAt, + CASE WHEN length(CAST(m.content_json AS BLOB)) <= ? THEN m.content_json ELSE NULL END AS content + FROM messages m JOIN conversations c ON c.id = m.conversation_id WHERE m.id = ?`).get(documentBytes, id)! + const identity = { conversationId: String(row.conversationId), branchId: String(row.branchId), title: row.title === null ? null : searchableText(String(row.title)), spaceId: row.spaceId as string | null } + if (task) + return identity.title?.toLowerCase().includes(query) ? { kind: 'task', ...identity } : null + if (row.content === null) { + skippedOversized++ + return null + } + const source = this.#views.messageText({ role: row.role as MessageRole, content: JSON.parse(String(row.content)) }) + if (source === null) + return null + const text = searchableText(source) + const offset = text.toLowerCase().indexOf(query) + if (offset < 0) + return null + const start = Math.max(0, offset - 40) + const snippet = text.slice(start, start + 320).replace(/[\uD800-\uDBFF]$/, '') + return { kind: 'message', ...identity, messageId: id, role: row.role, createdAt: row.createdAt, snippet } + }) + return { ...page, partial: page.nextCursor !== null || skippedOversized > 0, skippedOversized } + } +} + +function searchableText(value: string): string { + return redactSensitiveText(value) + .replace(/(?:\b[A-Z]:[\\/]|\\\\|(?"'`]+/gi, '[path]') +} diff --git a/apps/buddy/service/src/productProtocol/ProductQueries.ts b/apps/buddy/service/src/productProtocol/ProductQueries.ts index 26e4c86e2..2b8d808d6 100644 --- a/apps/buddy/service/src/productProtocol/ProductQueries.ts +++ b/apps/buddy/service/src/productProtocol/ProductQueries.ts @@ -2,6 +2,7 @@ import type { BuddyResourceQuote } from '../../../shared/conversation/buddyUserC import type { BuddyComposerDraft } from '../../../shared/conversation/composerDraft' import type { ProductMethod } from '../../../shared/productProtocol/methods' import type { ProductTopic } from '../../../shared/productProtocol/topics' +import type { MessageRecord } from '../storage/conversationHistoryRepository' import type { ProductRequestHandler } from './ProductConnection' import { createHash } from 'node:crypto' import { hostname, platform } from 'node:os' @@ -19,8 +20,10 @@ import { createRunRepository } from '../storage/runRepository' import { createTaskMarkRepository } from '../storage/taskMarkRepository' import { assertCommittedRead, withTransaction } from '../storage/transaction' import { ProductBuddyAvatars } from './ProductBuddyAvatars' +import { ProductBuddyDirectory } from './ProductBuddyDirectory' import { ProductContentReferences } from './ProductContentReferences' import { ProductDraftReadModel } from './ProductDraftReadModel' +import { ProductHistorySearch } from './ProductHistorySearch' import { ProductPageReader } from './ProductPageReader' import { productQueryRevision } from './ProductQueryRevisions' import { ProductReadViews } from './ProductReadViews' @@ -116,7 +119,7 @@ export class ProductQueries { assertTopic(topic: ProductTopic): void { if (topic.kind === 'draft' && !this.draft(topic.draftId)) throw new ProductProtocolError('OBJECT_DELETED') - if ((topic.kind === 'conversation' || topic.kind === 'questions') && (!this.task(topic.conversationId) || !this.#database.prepare('SELECT 1 FROM conversation_branches WHERE id = ? AND conversation_id = ?').get(topic.branchId, topic.conversationId))) + if ((topic.kind === 'conversation' || topic.kind === 'conversationWindow' || topic.kind === 'questions') && (!this.task(topic.conversationId) || !this.#database.prepare('SELECT 1 FROM conversation_branches WHERE id = ? AND conversation_id = ?').get(topic.branchId, topic.conversationId))) throw new ProductProtocolError('OBJECT_DELETED') } @@ -129,6 +132,8 @@ export class ProductQueries { return { items: [{ phase: 'ready' }], nextCursor: null } if (topic.kind === 'questions') return this.#read('questions.list', { conversationId: topic.conversationId, branchId: topic.branchId, limit: PRODUCT_LIMITS.pageItems, cursor }) as { items: unknown[], nextCursor: string | null } + if (topic.kind === 'conversationWindow') + return this.#windowTopicPage(topic.conversationId, topic.branchId, cursor) if (topic.kind !== 'conversation') { const method = topic.kind === 'taskIndex' ? 'conversations.list' : topic.kind === 'draftIndex' ? 'composerDrafts.list' : 'approvals.list' return this.#read(method, { limit: PRODUCT_LIMITS.pageItems, cursor, ...(topic.kind === 'approvalInbox' ? { status: 'pending' } : {}) }) as { items: unknown[], nextCursor: string | null } @@ -155,8 +160,10 @@ export class ProductQueries { handlers(): Partial> { const handlers: Partial> = {} - for (const method of Object.keys(PRODUCT_QUERY_METHODS).filter(method => !method.startsWith('providers.') && method !== 'conversations.listArchived' && method !== 'conversations.getArchivePolicy') as (keyof typeof PRODUCT_QUERY_METHODS)[]) { - handlers[method] = { handle: (input) => { + for (const method of Object.keys(PRODUCT_QUERY_METHODS).filter(method => !method.startsWith('providers.') && method !== 'conversations.listArchived' && method !== 'conversations.getArchivePolicy' && method !== 'conversations.getQueueControls') as (keyof typeof PRODUCT_QUERY_METHODS)[]) { + handlers[method] = { handle: (input, context) => { + if (method === 'conversations.search' && (input as { scope: string }).scope === 'archived' && context.role !== 'owner') + throw new ProductProtocolError('NOT_AUTHORIZED') assertCommittedRead(this.#database) return withTransaction(this.#database, () => this.#read(method, input as Record)) } } @@ -168,6 +175,11 @@ export class ProductQueries { switch (method) { case 'service.host': return { platform: ['darwin', 'win32', 'linux'].includes(platform()) ? platform() : 'other' } case 'service.info': return { name: hostname() || 'Lexora' } + case 'buddies.list': return new ProductBuddyDirectory(this.#database, this.#instance).list(input) + case 'conversations.listMembers': return new ProductBuddyDirectory(this.#database, this.#instance).members(input) + case 'conversations.search': return new ProductHistorySearch(this.#database, this.#instance, this.views).search(input) + case 'conversations.getMessageWindow': return this.#messageWindow(input) + case 'conversations.getRecentWindow': return this.#recentWindow(input) case 'buddies.getAvatars': return new ProductBuddyAvatars(this.#database).metadata(input.buddyIds as string[]) case 'questions.list': return this.#pages.read(`${method}:${input.conversationId}:${input.branchId}`, input, 'user_questions', 'EXISTS (SELECT 1 FROM runs r JOIN conversations c ON c.id = r.conversation_id WHERE r.id = user_questions.run_id AND r.conversation_id = ? AND r.branch_id = ? AND c.deleted_at IS NULL)', [String(input.conversationId), String(input.branchId)], id => this.views.question(id)) case 'spaces.list': return this.#pages.read(method, input, 'spaces', 'revoked_at IS NULL', [], id => this.#database.prepare('SELECT id, name, memory_scope AS memoryScope FROM spaces WHERE id = ?').get(id)) @@ -210,6 +222,45 @@ export class ProductQueries { } } + #windowTopicPage(conversationId: string, branchId: string, cursor: string | null): { items: unknown[], nextCursor: string | null } { + const match = cursor === null ? null : /^([0-6])\.(.*)$/.exec(cursor) + if (cursor !== null && !match) + throw new ProductProtocolError('CURSOR_EXPIRED') + const phase = match ? Number(match[1]) : 0 + const nested = match?.[2] ? decodeURIComponent(match[2]) : null + const input = { conversationId, branchId, limit: PRODUCT_LIMITS.pageItems, cursor: nested } + const entities = ['task', 'run', 'queue', 'approval', 'message', 'historyWindow', 'message'] as const + let page: { items: unknown[], nextCursor: string | null } + if (phase === 0) { + this.assertTopic({ kind: 'conversationWindow', conversationId, branchId }) + page = { items: [this.task(conversationId)], nextCursor: null } + } + else if (phase === 1) { + const active = this.#database.prepare('SELECT COUNT(*) AS count FROM runs WHERE conversation_id = ? AND branch_id = ? AND execution_kind IN (\'root\',\'actor_child\',\'maintenance\') AND status IN (\'queued\',\'running\')').get(conversationId, branchId)! + if (Number(active.count) > PRODUCT_LIMITS.pageItems) + throw new ProductProtocolError('PAGE_TOO_LARGE') + const table = `(SELECT rowid, id FROM runs WHERE conversation_id = ? AND branch_id = ? AND execution_kind IN ('root','actor_child','maintenance') ORDER BY (status IN ('queued','running')) DESC, rowid DESC LIMIT ${PRODUCT_LIMITS.pageItems})` + const rows = this.#database.prepare(`SELECT id FROM ${table}`).all(conversationId, branchId) + page = this.#pages.read(`runs.list:recent:${conversationId}:${branchId}`, input, 'runs', 'id IN (SELECT value FROM json_each(?))', [JSON.stringify(rows.map(row => row.id))], id => this.#run(id)) + } + else if (phase === 2 || phase === 3) { + const method = phase === 2 ? 'chat.queue.list' : 'approvals.list' + page = this.#read(method, { ...input, ...(phase === 3 ? { status: 'pending' } : {}) }) as typeof page + if (page.nextCursor) + throw new ProductProtocolError('PAGE_TOO_LARGE') + } + else if (phase === 4 || phase === 5) { + const { items, ...window } = this.#recentWindow(input) + page = { items: phase === 4 ? items : [window], nextCursor: null } + } + else { + page = this.streaming.page(conversationId, branchId, null) + if (page.nextCursor) + throw new ProductProtocolError('PAGE_TOO_LARGE') + } + return { items: page.items.map(value => ({ entity: entities[phase]!, value })), nextCursor: page.nextCursor ? `${phase}.${encodeURIComponent(page.nextCursor)}` : phase < 6 ? `${phase + 1}.` : null } + } + #run(id: string) { const run = createRunRepository(this.#database).findById(id) if (!run || run.executionKind === 'npc_child' || !this.task(run.conversationId)) @@ -219,11 +270,37 @@ export class ProductQueries { return productRunSchema.parse({ ...toPublicRun(run, input?.reasoning ?? null), executionKind: run.executionKind, generation: encodeProductRevision(run.generation), lastEventSequence: encodeProductRevision(Number(this.#database.prepare('SELECT next_event_sequence FROM runs WHERE id = ?').get(id)?.next_event_sequence ?? 1) - 1), errorCode: run.errorCode ? code.success ? code.data : 'INTERNAL_ERROR' : null }) } - #messagePage(input: Record) { + #recentWindow(input: Record) { + const page = this.#messagePage({ ...input, limit: 50, cursor: null }) + return { ...page, conversationId: String(input.conversationId), branchId: String(input.branchId), hasMore: page.nextCursor !== null } + } + + #messageWindow(input: Record) { + const conversationId = String(input.conversationId) + const branchId = String(input.branchId) + const messageId = String(input.messageId) + const conversations = createConversationRepository(this.#database) + const message = conversations.findMessageById(messageId) + if (!message || message.conversationId !== conversationId || (message.runId && !this.#run(message.runId))) + throw new ProductProtocolError('OBJECT_DELETED') + let page + try { + page = this.#messagePage({ ...input, cursor: null }, message) + } + catch (error) { + if (error instanceof ProductProtocolError) + throw error + throw new ProductProtocolError('OBJECT_DELETED') + } + const latest = conversations.listMessagePage(conversationId, branchId, { limit: 1 }) + return { ...page, conversationId, branchId, anchorMessageId: messageId, hasLater: latest.items[0]?.id !== messageId } + } + + #messagePage(input: Record, anchor?: MessageRecord) { const conversationId = String(input.conversationId) const branchId = String(input.branchId) const conversations = createConversationRepository(this.#database) - if (!this.task(conversationId)) + if (!this.task(conversationId) || !this.#database.prepare('SELECT 1 FROM conversation_branches WHERE id = ? AND conversation_id = ?').get(branchId, conversationId)) throw new ProductProtocolError('OBJECT_DELETED') const revision = productQueryRevision(this.#database, 'conversations.listMessages', input) const scope = digestScope(['messages', this.#instance, conversationId, branchId]) @@ -239,7 +316,9 @@ export class ProductQueries { } catch { throw new ProductProtocolError('CURSOR_EXPIRED') } } - const source = conversations.listMessagePage(conversationId, branchId, { limit: Number(input.limit), beforeMessageId }) + const source = conversations.listMessagePage(conversationId, branchId, { limit: Number(input.limit) - (anchor ? 1 : 0), beforeMessageId: anchor?.id ?? beforeMessageId }) + if (anchor) + source.items.push(anchor) const items: ReturnType[] = [] for (const message of [...source.items].reverse()) { const item = this.views.message(message) diff --git a/apps/buddy/service/src/productProtocol/ProductQueryRevisions.ts b/apps/buddy/service/src/productProtocol/ProductQueryRevisions.ts index 430c31584..191aa120e 100644 --- a/apps/buddy/service/src/productProtocol/ProductQueryRevisions.ts +++ b/apps/buddy/service/src/productProtocol/ProductQueryRevisions.ts @@ -15,6 +15,9 @@ export function productQueryRevision(database: DatabaseSync, method: string, inp case 'spaces.list': add('', 'spaces') break + case 'conversations.search': + add('', 'conversations', 'messages', 'conversation_publications', 'run_history') + break case 'conversations.list': add('', 'conversations', 'approvals', 'run_summaries', 'automation_occurrences', 'user_questions', 'task_attention', 'publication_summaries', 'message_summaries') break diff --git a/apps/buddy/service/src/productProtocol/ProductQueueControls.ts b/apps/buddy/service/src/productProtocol/ProductQueueControls.ts new file mode 100644 index 000000000..0b9559ff5 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductQueueControls.ts @@ -0,0 +1,63 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { ProductQueueControl, ProductQueueControlsInput } from '../../../shared/productProtocol/queries' +import type { ChatQueueService } from '../chat/ChatQueueService' +import { buddyInputDeliverySchema } from '../../../shared/conversation/chatApi' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { productQueueControlSchema } from '../../../shared/productProtocol/queries' +import { encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { assertCommittedRead, withTransaction } from '../storage/transaction' +import { ProductPageReader } from './ProductPageReader' + +export class ProductQueueControls { + private readonly database: DatabaseSync + private readonly instance: string + private readonly queue: Pick + + constructor(database: DatabaseSync, instance: string, queue: Pick) { + this.database = database + this.instance = instance + this.queue = queue + } + + async read(input: ProductQueueControlsInput) { + assertCommittedRead(this.database) + const assertScope = () => { + const task = this.database.prepare('SELECT c.archived_at FROM conversations c JOIN conversation_branches b ON b.conversation_id = c.id WHERE c.id = ? AND b.id = ? AND c.deleted_at IS NULL').get(input.conversationId, input.branchId) + if (!task) + throw new ProductProtocolError('OBJECT_DELETED') + return task + } + const source = withTransaction(this.database, () => { + assertScope() + return new ProductPageReader(this.database, this.instance).read(`chat.queue.list:controls:${input.conversationId}:${input.branchId}`, input, 'chat_queue', 'conversation_id = ? AND branch_id = ? AND state IN (\'waiting\',\'paused\')', [input.conversationId, input.branchId], id => this.database.prepare('SELECT id, CAST(revision AS TEXT) AS revision FROM chat_queue WHERE id = ?').get(id)) + }) + const items: ProductQueueControl[] = [] + for (const sourceItem of source.items) { + const { id, revision } = sourceItem as { id: string, revision: string } + const controls = await this.queue.inspectControls({ id, conversationId: input.conversationId, branchId: input.branchId }) + assertCommittedRead(this.database) + const task = assertScope() + const row = this.database.prepare('SELECT revision, prepared_json FROM chat_queue WHERE id = ? AND conversation_id = ? AND branch_id = ? AND state IN (\'waiting\',\'paused\')').get(id, input.conversationId, input.branchId) + if (!row || String(row.revision) !== revision) + throw new ProductProtocolError('QUEUE_CHANGED') + const prepared = JSON.parse(String(row.prepared_json)) as { delivery?: unknown } + const delivery = prepared.delivery === undefined ? null : buddyInputDeliverySchema.parse(prepared.delivery) + const target = delivery?.target && this.database.prepare('SELECT conversation_id, branch_id, execution_kind FROM runs WHERE id = ?').get(delivery.target.runId) + const root = delivery?.target && this.database.prepare('SELECT conversation_id, branch_id, execution_kind FROM runs WHERE id = ?').get(delivery.target.rootRunId) + const actor = delivery && this.database.prepare('SELECT kind FROM buddies WHERE id = ?').get(delivery.actorId) + const invalidTarget = (delivery && actor?.kind !== 'actor') + || (target && (target.conversation_id !== input.conversationId || target.branch_id !== input.branchId || !['root', 'actor_child'].includes(String(target.execution_kind)))) + || (root && (root.conversation_id !== input.conversationId || root.branch_id !== input.branchId || root.execution_kind !== 'root')) + const restricted = task.archived_at !== null || invalidTarget + items.push(productQueueControlSchema.parse({ + id, + revision, + delivery: invalidTarget ? null : delivery, + allowedActions: restricted ? controls.allowedActions.filter(action => action === 'cancel') : controls.allowedActions, + steerTarget: !restricted && controls.steerTarget ? { runId: controls.steerTarget.runId, generation: encodeProductRevision(controls.steerTarget.generation) } : null, + blockedReason: invalidTarget ? 'target_unavailable' : task.archived_at !== null ? 'archived' : controls.blockedReason, + })) + } + return { items, nextCursor: source.nextCursor } + } +} diff --git a/apps/buddy/service/src/productProtocol/ProductReadViews.ts b/apps/buddy/service/src/productProtocol/ProductReadViews.ts index 47457a3a6..80729c05a 100644 --- a/apps/buddy/service/src/productProtocol/ProductReadViews.ts +++ b/apps/buddy/service/src/productProtocol/ProductReadViews.ts @@ -54,11 +54,15 @@ export class ProductReadViews { return productQuestionSchema.parse(Object.fromEntries(Object.keys(productQuestionSchema.shape).map(key => [key, source[key]]))) } - messagePreview(message: Pick): string { + messageText(message: Pick): string | null { if (message.role !== 'user' && message.role !== 'assistant') - return '' + return null const user = message.role === 'user' ? buddyUserMessageContentV1Schema.safeParse(message.content) : null - const text = user?.success ? buddyUserContentToText(user.data.userContent) : publicMessageText(message.content) + return user?.success ? buddyUserContentToText(user.data.userContent) : publicMessageText(message.content) + } + + messagePreview(message: Pick): string { + const text = this.messageText(message) return text === null ? '' : truncateText(text.replace(/\s+/g, ' ').trim(), 240) } diff --git a/apps/buddy/service/src/productProtocol/__tests__/ConversationArchiveCommands.spec.ts b/apps/buddy/service/src/productProtocol/__tests__/ConversationArchiveCommands.spec.ts index c8d5040da..00e748140 100644 --- a/apps/buddy/service/src/productProtocol/__tests__/ConversationArchiveCommands.spec.ts +++ b/apps/buddy/service/src/productProtocol/__tests__/ConversationArchiveCommands.spec.ts @@ -97,6 +97,28 @@ function fixture(prepare: () => Promise = async () => 0) { } describe('archive product commands', () => { + it('uses the public member read revision for existing durable member commands', async () => { + const f = fixture() + const buddy = createBuddyRepository(f.database).create({ kind: 'actor', name: 'New member' }) + const client = await f.connect() + const original = (await client.send('conversations.listMembers', { conversationId: 'task' })).result! + const joined = await client.send('conversations.addMember', f.envelope('join', { conversationId: 'task', buddyId: buddy.id, expectedRevision: original.revision })) + expect(joined.result?.receipt).toMatchObject({ decision: 'accepted' }) + const members = (await client.send('conversations.listMembers', { conversationId: 'task' })).result! + expect(members.items).toContainEqual(expect.objectContaining({ actorId: buddy.id, profile: expect.objectContaining({ id: buddy.id, name: 'New member' }) })) + expect(members.revision).toBe(joined.result?.receipt.result.revision) + const stale = await client.send('conversations.setBuddy', f.envelope('stale', { conversationId: 'task', buddyId: buddy.id, expectedRevision: original.revision })) + expect(stale.result?.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + const selected = await client.send('conversations.setBuddy', f.envelope('select', { conversationId: 'task', buddyId: buddy.id, expectedRevision: members.revision })) + expect(selected.result?.receipt).toMatchObject({ decision: 'accepted' }) + const current = (await client.send('conversations.listMembers', { conversationId: 'task' })).result! + expect(current).toMatchObject({ buddyId: buddy.id, revision: selected.result?.receipt.result.revision }) + const removed = await client.send('conversations.removeMember', f.envelope('remove', { conversationId: 'task', buddyId: original.buddyId, expectedRevision: current.revision })) + expect(removed.result?.receipt).toMatchObject({ decision: 'accepted' }) + expect((await client.send('conversations.listMembers', { conversationId: 'task' })).result?.items).not.toContainEqual(expect.objectContaining({ actorId: original.buddyId })) + expect(f.effects).toEqual([]) + }) + it('keeps asynchronous preparation outside the receipt transaction and publishes only committed archive facts', async () => { const started = Promise.withResolvers() const release = Promise.withResolvers() diff --git a/apps/buddy/service/src/productProtocol/__tests__/ProductDiscoveryReads.spec.ts b/apps/buddy/service/src/productProtocol/__tests__/ProductDiscoveryReads.spec.ts new file mode 100644 index 000000000..2d5cd9007 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/__tests__/ProductDiscoveryReads.spec.ts @@ -0,0 +1,219 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { ProductSearchPage } from '../../../../shared/productProtocol/queries' +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { PRODUCT_QUERY_METHODS, productBuddyPageSchema, productMemberPageSchema, productMessageWindowSchema, productRecentWindowSchema, productSearchPageSchema } from '../../../../shared/productProtocol/queries' +import { PRODUCT_TOPIC_SCHEMAS } from '../../../../shared/productProtocol/topics' +import { SqliteRunEventWriter } from '../../events/SqliteRunEventWriter' +import { seedSqliteRun } from '../../storage/__tests__/sqliteRunFixture' +import { createBuddyRepository } from '../../storage/buddyRepository' +import { ConversationCommandRepository } from '../../storage/ConversationCommandRepository' +import { createConversationRepository } from '../../storage/conversationRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { createProductSynchronization } from '../createProductSynchronization' +import { ProductConnection } from '../ProductConnection' +import { ProductQueries } from '../ProductQueries' + +const databases: DatabaseSync[] = [] +const directories: string[] = [] +afterEach(() => { + for (const database of databases.splice(0)) database.close() + for (const directory of directories.splice(0)) rmSync(directory, { recursive: true, force: true }) +}) +const now = '2026-11-01T00:00:00.000Z' +function fixture(file = false) { + const directory = file ? mkdtempSync(join(tmpdir(), 'lexora-discovery-')) : null + if (directory) + directories.push(directory) + const database = openBuddyMigrationCandidate({ databasePath: directory ? join(directory, 'buddy.sqlite3') : ':memory:' }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + seedSqliteRun(database, 'root', { conversationId: 'task' }) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const conversations = createConversationRepository(database) + const read = (method: keyof typeof PRODUCT_QUERY_METHODS, input: unknown) => queries.read(method, PRODUCT_QUERY_METHODS[method].input.parse(input) as Record) + const message = (id: string, content: unknown, branchId = 'branch-task', runId: string | null = null) => conversations.createMessage({ id, conversationId: 'task', branchId, runId, role: 'assistant', content, createdAt: now }) + const search = (query: string, extra: Record = {}): ProductSearchPage => productSearchPageSchema.parse(read('conversations.search', { query, ...extra })) + return { database, queries, conversations, read, message, search } +} + +describe('product discovery read contracts', () => { + it('finds old full public text and opens its exact branch/message without activating it', () => { + const f = fixture() + f.message('early', { text: `${'old text '.repeat(100)}buried-needle` }) + f.message('later', 'latest preview') + f.conversations.createBranch({ id: 'fork', conversationId: 'task', parentBranchId: 'branch-task', forkedFromMessageId: 'early', createdAt: now, activate: false }) + f.message('fork-hit', 'buried-needle in fork', 'fork') + const hits = f.search('buried-needle') + expect(hits).toMatchObject({ partial: false, nextCursor: null, skippedOversized: 0 }) + expect(hits.items).toMatchObject([{ kind: 'message', messageId: 'early', branchId: 'branch-task', snippet: expect.stringContaining('buried-needle') }, { kind: 'message', messageId: 'fork-hit', branchId: 'fork' }]) + const window = productMessageWindowSchema.parse(f.read('conversations.getMessageWindow', { conversationId: 'task', branchId: 'branch-task', messageId: 'early', limit: 2 })) + expect(window.items.map(item => item.id)).toEqual(['input-root', 'early']) + expect(window).toMatchObject({ anchorMessageId: 'early', hasLater: true }) + expect(f.conversations.findById('task')?.activeBranchId).toBe('branch-task') + expect(() => f.read('conversations.getMessageWindow', { conversationId: 'task', branchId: 'fork', messageId: 'later' })).toThrow('OBJECT_DELETED') + const fork = productMessageWindowSchema.parse(f.read('conversations.getMessageWindow', { conversationId: 'task', branchId: 'fork', messageId: 'fork-hit', limit: 2 })) + expect(fork.items.map(item => item.id)).toEqual(['early', 'fork-hit']) + expect(fork.nextCursor).not.toBeNull() + const older = f.read('conversations.listMessages', { conversationId: 'task', branchId: 'fork', cursor: fork.nextCursor }) as { items: { id: string }[] } + expect(older.items.map(item => item.id)).toEqual(['input-root']) + }) + + it('never discovers private child, reasoning, credential, local path or deleted content', () => { + const f = fixture() + seedSqliteRun(f.database, 'private', { conversationId: 'task' }) + f.database.prepare('UPDATE runs SET execution_kind = \'npc_child\' WHERE id = \'private\'').run() + f.database.prepare('INSERT INTO messages (id,conversation_id,branch_id,run_id,role,content_json,created_at) VALUES (\'private-message\',\'task\',\'branch-task\',\'private\',\'assistant\',?,?)').run(JSON.stringify({ text: 'private-needle' }), now) + f.message('reasoning', { text: 'reasoning-needle', kind: 'reasoning' }) + f.message('credentials', 'token=credential-needle /home/private/path-needle visible-word') + f.message('rich-user', { userContent: createBuddyUserContent('rich-public-needle'), resourceSnapshots: [] }) + f.database.prepare('UPDATE messages SET role = \'user\' WHERE id = \'rich-user\'').run() + for (const query of ['private-needle', 'reasoning-needle', 'credential-needle', 'path-needle']) expect(f.search(query).items).toEqual([]) + expect(f.search('rich-public-needle').items).toMatchObject([{ messageId: 'rich-user' }]) + expect(f.search('visible-word').items).toMatchObject([{ snippet: 'token=[redacted] [path] visible-word' }]) + expect(() => f.read('conversations.getMessageWindow', { conversationId: 'task', branchId: 'branch-task', messageId: 'private-message' })).toThrow('OBJECT_DELETED') + f.database.prepare('UPDATE conversations SET deleted_at = ? WHERE id = \'task\'').run(now) + expect(f.search('visible-word').items).toEqual([]) + expect(() => f.read('conversations.getMessageWindow', { conversationId: 'task', branchId: 'branch-task', messageId: 'credentials' })).toThrow('OBJECT_DELETED') + }) + + it('makes candidate pagination and oversized omissions explicit and binds cursors to scope', () => { + const f = fixture() + f.message('oversized', 'omitted-needle'.repeat(30_000)) + f.message('small', 'final-needle') + const first = f.search('final-needle', { limit: 1 }) + expect(first).toMatchObject({ items: [], partial: true, skippedOversized: 0 }) + expect(first.nextCursor).not.toBeNull() + expect(() => f.search('different', { limit: 1, cursor: first.nextCursor })).toThrow('CURSOR_EXPIRED') + expect(() => f.search('final-needle', { scope: 'archived', cursor: first.nextCursor })).toThrow('CURSOR_EXPIRED') + let page = first + const items = [...page.items] + let skipped = 0 + for (let count = 0; page.nextCursor && count < 10; count++) { + page = f.search('final-needle', { limit: 1, cursor: page.nextCursor }) + items.push(...page.items) + skipped += page.skippedOversized + } + expect(items).toMatchObject([{ messageId: 'small' }]) + expect(skipped).toBe(1) + expect(page.nextCursor).toBeNull() + f.message('changed', 'change') + expect(() => f.search('final-needle', { cursor: first.nextCursor })).toThrow('CURSOR_EXPIRED') + }) + + it('enforces archived search permission through the real wire handler', async () => { + const f = fixture() + f.message('archived', 'archive-needle') + f.database.prepare('UPDATE conversations SET archived_at = ? WHERE id = \'task\'').run(now) + for (const role of ['reader', 'owner'] as const) { + const frames: Array<{ result?: unknown, error?: { code: string } }> = [] + const connection = new ProductConnection({ context: { serviceId: 'service', profileId: 'profile', dataGeneration: 'generation', serviceInstanceId: 'instance', connectionId: role, clientSessionId: 'session', deviceId: 'device', ownerPrincipalId: 'owner', credentialId: 'credential', grantGeneration: '1', role }, assertCurrent: () => {}, phase: () => 'ready', handlers: f.queries.handlers(), send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, close: () => {} }) + await connection.receive(JSON.stringify({ jsonrpc: '2.0', id: 'init', method: 'initialize', params: { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'mobile', version: '1' }, supportedContentKinds: [], supportedReviewKinds: [] } })) + await connection.receive(JSON.stringify({ jsonrpc: '2.0', id: 'search', method: 'conversations.search', params: { query: 'archive-needle', scope: 'archived' } })) + if (role === 'reader') + expect(frames.at(-1)?.error?.code).toBe('NOT_AUTHORIZED') + else + expect(frames.at(-1)?.result).toMatchObject({ items: [{ messageId: 'archived' }] }) + connection.close() + } + expect(f.search('archive-needle').items).toEqual([]) + expect(f.read('conversations.get', { conversationId: 'task' })).toMatchObject({ activeBranchId: 'branch-task' }) + expect(productRecentWindowSchema.parse(f.read('conversations.getRecentWindow', { conversationId: 'task', branchId: 'branch-task' })).items).toHaveLength(2) + }) + + it('returns current actor identities and member CAS revisions without execution settings or NPCs', () => { + const f = fixture() + const buddies = createBuddyRepository(f.database) + const first = buddies.create({ kind: 'actor', name: 'Same name', description: 'public description' }) + const second = buddies.create({ kind: 'actor', name: 'Same name' }) + buddies.create({ kind: 'npc', name: 'Private worker' }) + f.conversations.addMember({ id: 'task', buddyId: first.id, updatedAt: now }) + f.conversations.addMember({ id: 'task', buddyId: second.id, updatedAt: now }) + f.conversations.setBuddy({ id: 'task', buddyId: first.id, updatedAt: now }) + const before = f.database.prepare('SELECT buddy_id FROM runs WHERE id = \'root\'').get() + buddies.update({ buddyId: second.id, expectedRevision: second.revision, enabled: false }) + const catalog = productBuddyPageSchema.parse(f.read('buddies.list', {})) + expect(catalog.items.filter(item => item.name === 'Same name').map(item => item.id)).toEqual([first.id, second.id]) + expect(JSON.stringify(catalog)).not.toMatch(/Private worker|settings|instructions|avatar|provider/) + const page = productMemberPageSchema.parse(f.read('conversations.listMembers', { conversationId: 'task' })) + expect(page).toMatchObject({ buddyId: first.id, revision: String(new ConversationCommandRepository(f.database).conversationRevision('task')) }) + expect(page.items.find(item => item.actorId === second.id)?.profile).toMatchObject({ enabled: false, deleted: false }) + f.conversations.removeMember({ id: 'task', buddyId: second.id, updatedAt: now }) + const updated = productMemberPageSchema.parse(f.read('conversations.listMembers', { conversationId: 'task' })) + expect(updated.items.some(item => item.actorId === second.id)).toBe(false) + expect(updated.revision).not.toBe(page.revision) + expect(f.database.prepare('SELECT buddy_id FROM runs WHERE id = \'root\'').get()).toEqual(before) + }) + + it('continues live public message and run updates after the bounded snapshot completes', () => { + const f = fixture(true) + const errors: unknown[] = [] + const writer = new SqliteRunEventWriter(f.database, error => errors.push(error)) + const idle = { onDidCommit: () => ({ dispose() {} }) } + const sync = createProductSynchronization({ database: f.database, queries: f.queries, drafts: idle, metadata: idle, lifecycle: idle, events: writer }) + const frames: Array<{ method: string, params: { changes: unknown[] } }> = [] + const disconnect = sync.publisher.connect('window', { send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, close() {} }) + try { + sync.authorizeTopic('conversationWindow:task:branch-task') + let response = sync.publisher.subscribe('window', { subscriptionId: 'live', topic: 'conversationWindow:task:branch-task' }) + response.activate() + while (response.snapshot.mode === 'snapshot' && response.snapshot.nextCursor) { + response = sync.publisher.snapshotPage('window', { subscriptionId: 'live', cursor: response.snapshot.nextCursor }) + response.activate() + } + sync.publisher.ack('window', { subscriptionId: 'live', ...response.snapshot.cursor }) + writer.append({ runId: 'root', type: 'message.started', payload: { messageId: 'live-reply', role: 'assistant' } }) + writer.append({ runId: 'root', type: 'message.completed', payload: { messageId: 'live-reply', role: 'assistant', content: { text: 'new public reply' }, stopReason: 'completed' } }) + writer.append({ runId: 'root', type: 'run.completed', payload: {} }) + const changes = frames.filter(frame => frame.method === 'sync.delta').flatMap(frame => frame.params.changes).map(change => PRODUCT_TOPIC_SCHEMAS.conversationWindow.change.parse(change)) + expect(changes).toContainEqual(expect.objectContaining({ kind: 'upsert', item: expect.objectContaining({ entity: 'message', value: expect.objectContaining({ id: 'live-reply', body: expect.objectContaining({ text: 'new public reply' }) }) }) })) + expect(changes).toContainEqual(expect.objectContaining({ kind: 'upsert', item: expect.objectContaining({ entity: 'run', value: expect.objectContaining({ id: 'root', status: 'completed' }) }) })) + expect(errors).toEqual([]) + } + finally { + disconnect() + sync.dispose() + writer.dispose() + } + }) + + it('bounds the new live snapshot to recent history while retaining the old complete topic', () => { + const f = fixture(true) + for (let index = 0; index < 130; index++) f.message(`message-${String(index).padStart(3, '0')}`, `body ${index}`) + const collect = (name: string) => { + const reader = f.queries.openSnapshot(name) + const items: unknown[] = [] + let cursor: string | null = null + try { + do { + const page = reader.read(cursor) + items.push(...page.items) + cursor = page.nextCursor + } while (cursor) + } + finally { reader.close() } + return items + } + const recent = collect('conversationWindow:task:branch-task').map(item => PRODUCT_TOPIC_SCHEMAS.conversationWindow.item.parse(item)) + const messages = recent.filter(item => item.entity === 'message') + expect(messages).toHaveLength(50) + expect(messages[0]).toMatchObject({ value: { id: 'message-080' } }) + const metadata = recent.find(item => item.entity === 'historyWindow') + expect(metadata).toMatchObject({ value: { hasMore: true, nextCursor: expect.any(String) } }) + if (metadata?.entity !== 'historyWindow') + throw new Error('Missing history window') + const older = f.read('conversations.listMessages', { conversationId: 'task', branchId: 'branch-task', cursor: metadata.value.nextCursor }) as { items: unknown[] } + expect(older.items).toHaveLength(81) + const old = collect('conversation:task:branch-task').map(item => PRODUCT_TOPIC_SCHEMAS.conversation.item.parse(item)) + expect(old.filter(item => item.entity === 'message')).toHaveLength(131) + }) +}) diff --git a/apps/buddy/service/src/productProtocol/createProductCommandHandlers.ts b/apps/buddy/service/src/productProtocol/createProductCommandHandlers.ts index a08686b3b..8213b3753 100644 --- a/apps/buddy/service/src/productProtocol/createProductCommandHandlers.ts +++ b/apps/buddy/service/src/productProtocol/createProductCommandHandlers.ts @@ -9,6 +9,7 @@ import { assertProductContentActions, productEditableContentSchema, restoreProdu import { ProductProtocolError } from '../../../shared/productProtocol/errors' import { productByteLength } from '../../../shared/productProtocol/limits' import { productCommandIntents } from '../../../shared/productProtocol/methods' +import { productQueueControlsInputSchema } from '../../../shared/productProtocol/queries' import { decodeSafeProductRevision, encodeProductRevision } from '../../../shared/productProtocol/scalars' import { CommandAdmissionError } from '../commands/CommandAdmissionError' import { COMMAND_FINGERPRINT_VERSION, fingerprintCommandIntent, fingerprintProductCommandIntent, PRODUCT_COMMAND_FINGERPRINT_VERSION, productCommandFingerprint } from '../commands/commandIntent' @@ -26,6 +27,7 @@ export function createProductCommandHandlers(options: { }): Partial> { const { commands } = options const handlers: Partial> = { + 'conversations.getQueueControls': { handle: input => commands.queueControls.read(productQueueControlsInputSchema.parse(input)) }, 'conversations.listArchived': { handle: (input, context) => { options.assertOwner(context) const page = commands.metadata.listArchived(conversationArchiveQuerySchema.parse({ ...input as Record, cursor: (input as { cursor?: string | null }).cursor ?? undefined })) diff --git a/apps/buddy/service/src/productProtocol/createProductSynchronization.ts b/apps/buddy/service/src/productProtocol/createProductSynchronization.ts index 5c36aa73d..ac9d474c6 100644 --- a/apps/buddy/service/src/productProtocol/createProductSynchronization.ts +++ b/apps/buddy/service/src/productProtocol/createProductSynchronization.ts @@ -71,9 +71,13 @@ export function createProductSynchronization(options: { return changes().map(change => schema.parse(change)) }) } + const branchTopics = (conversationId: string, branchId: string) => (['conversation', 'conversationWindow'] as const).map(kind => formatProductTopic({ kind, conversationId, branchId })) + const publishConversation = (conversationId: string, branchId: string, changes: () => unknown[]) => { + for (const name of branchTopics(conversationId, branchId)) publish(name, changes) + } const conversationTopics = (conversationId: string) => [...dynamic.keys()].filter((name) => { const topic = parseProductTopic(name) - return topic.kind === 'conversation' && topic.conversationId === conversationId + return (topic.kind === 'conversation' || topic.kind === 'conversationWindow') && topic.conversationId === conversationId }) const publishTask = (id: string) => { publish('taskIndex', () => { @@ -116,8 +120,7 @@ export function createProductSynchronization(options: { const branchId = String(row.branch_id) if (['run.queued', 'run.started', 'run.completed', 'run.failed', 'run.cancelled', 'message.created', 'message.completed', 'message.block.completed', 'approval.requested', 'approval.resolved'].includes(type)) publishTask(conversationId) - const name = formatProductTopic({ kind: 'conversation', conversationId, branchId }) - publish(name, () => { + publishConversation(conversationId, branchId, () => { const event = queries.views.event(runId, encodeProductRevision(sequence)) const changes: unknown[] = event ? [{ kind: 'event', event }] : [] if (type === 'message.started' && payload && typeof payload === 'object' && 'role' in payload && payload.role === 'assistant' && 'messageId' in payload && typeof payload.messageId === 'string') { @@ -180,12 +183,11 @@ export function createProductSynchronization(options: { } if (options.queue) { subscriptions.push(options.queue.onDidChange(({ scope, committed }) => { - const name = formatProductTopic({ kind: 'conversation', conversationId: scope.conversationId, branchId: scope.branchId }) if (!committed?.queueId) { - publisher.invalidate(name) + for (const name of branchTopics(scope.conversationId, scope.branchId)) publisher.invalidate(name) return } - publish(name, () => { + publishConversation(scope.conversationId, scope.branchId, () => { const value = queries.views.queue(committed.queueId) return value && ['waiting', 'paused'].includes(value.state) ? [{ kind: 'upsert', item: { entity: 'queue', value } }] diff --git a/apps/buddy/shared/productProtocol/queries.ts b/apps/buddy/shared/productProtocol/queries.ts index 385336e7a..0cd5a6a6c 100644 --- a/apps/buddy/shared/productProtocol/queries.ts +++ b/apps/buddy/shared/productProtocol/queries.ts @@ -1,6 +1,7 @@ import type { ZodType } from 'zod' import { z } from 'zod' -import { buddyComposerDraftSchema, buddyComposerDraftScopeSchema } from '../conversation/composerDraft' +import { buddyInputDeliverySchema } from '../conversation/chatApi' +import { buddyComposerDraftModelSelectionSchema, buddyComposerDraftSchema, buddyComposerDraftScopeSchema } from '../conversation/composerDraft' import { conversationBranchSchema, conversationSchema, conversationSummarySchema, modelSelectionSchema } from '../conversation/conversationApi' import { conversationArchiveItemSchema, conversationArchivePolicySchema, conversationArchiveQuerySchema } from '../conversation/conversationArchiveApi' import { taskMarkSchema, taskMarkStateSchema } from '../conversation/taskMarkApi' @@ -31,7 +32,42 @@ export const productTaskMarkStateSchema = taskMarkStateSchema.extend({ assignmen const query = (input: ZodType, output: ZodType) => ({ input, output, role: 'reader', command: false, ready: false } as const) export const productConversationArchiveItemSchema = conversationArchiveItemSchema.omit({ metadataRevision: true }).extend({ revision }).strict() export const productConversationArchivePageSchema = productPageSchema(productConversationArchiveItemSchema) +export const productBuddyProfileSchema = z.object({ id, name: z.string().max(80), description: z.string(), enabled: z.boolean(), deleted: z.boolean(), revision }).strict() +export const productBuddyPageSchema = productPageSchema(productBuddyProfileSchema).extend({ catalogRevision: revision, defaultBuddyId: id }) +export const productMemberPageSchema = productPageSchema(z.object({ actorId: id, profile: productBuddyProfileSchema, modelSelection: buddyComposerDraftModelSelectionSchema.nullable(), modelSelectionSource: z.enum(['legacy', 'explicit', 'snapshot']).nullable(), joinedAt: z.string() }).strict()).extend({ conversationId: id, buddyId: id, revision }) +const searchIdentity = { conversationId: id, branchId: id, title: z.string().nullable(), spaceId: id.nullable() } +export const productSearchHitSchema = z.discriminatedUnion('kind', [ + z.object({ kind: z.literal('task'), ...searchIdentity }).strict(), + z.object({ kind: z.literal('message'), ...searchIdentity, messageId: id, role: z.enum(['user', 'assistant']), createdAt: z.string(), snippet: z.string().max(320) }).strict(), +]) +export const productSearchInputSchema = productPageInputSchema.extend({ query: z.string().trim().min(1).max(200), spaceId: id.nullable().optional(), conversationId: id.optional(), scope: z.enum(['active', 'archived']).default('active') }) +export const productSearchPageSchema = productPageSchema(productSearchHitSchema).extend({ partial: z.boolean(), skippedOversized: z.number().int().nonnegative() }) +export const productQueueControlSchema = z.object({ + id, + revision, + delivery: buddyInputDeliverySchema.nullable(), + allowedActions: z.array(z.enum(['cancel', 'continue', 'steer'])).max(3), + steerTarget: z.object({ runId: id, generation: revision }).strict().nullable(), + blockedReason: z.enum(['target_unavailable', 'input_incompatible', 'busy', 'service_unavailable', 'input_unavailable', 'archived']).nullable(), +}).strict() +export const productQueueControlsInputSchema = productPageInputSchema.extend({ conversationId: id, branchId: id }) +export const productQueueControlsPageSchema = productPageSchema(productQueueControlSchema) +export type ProductQueueControl = z.infer +export type ProductQueueControlsInput = z.infer +export const productRecentWindowSchema = productPageSchema(productMessageSchema).extend({ conversationId: id, branchId: id, hasMore: z.boolean() }) +export const productMessageWindowSchema = productPageSchema(productMessageSchema).extend({ conversationId: id, branchId: id, anchorMessageId: id, hasLater: z.boolean() }) +export type ProductSearchInput = z.infer +export type ProductSearchPage = z.infer +export type ProductBuddyProfile = z.infer +export type ProductMemberPage = z.infer +export type ProductMessageWindow = z.infer export const PRODUCT_QUERY_METHODS = { + 'conversations.getQueueControls': query(productQueueControlsInputSchema, productQueueControlsPageSchema), + 'buddies.list': query(productPageInputSchema, productBuddyPageSchema), + 'conversations.listMembers': query(productPageInputSchema.extend({ conversationId: id }), productMemberPageSchema), + 'conversations.search': query(productSearchInputSchema, productSearchPageSchema), + 'conversations.getRecentWindow': query(z.object({ conversationId: id, branchId: id }).strict(), productRecentWindowSchema), + 'conversations.getMessageWindow': query(z.object({ conversationId: id, branchId: id, messageId: id, limit: z.number().int().min(2).max(PRODUCT_LIMITS.pageItems).default(50) }).strict(), productMessageWindowSchema), 'buddies.getAvatars': query(z.object({ buddyIds: z.array(id).min(1).max(PRODUCT_LIMITS.pageItems) }).strict(), z.object({ items: z.array(productBuddyAvatarSchema).max(PRODUCT_LIMITS.pageItems) }).strict()), 'service.host': query(z.object({}).strict(), z.object({ platform: z.enum(['darwin', 'win32', 'linux', 'other']) }).strict()), 'service.info': query(z.object({}).strict(), z.object({ name: z.string().min(1).max(253) }).strict()), diff --git a/apps/buddy/shared/productProtocol/topics.ts b/apps/buddy/shared/productProtocol/topics.ts index 48648ef3a..94ffeccef 100644 --- a/apps/buddy/shared/productProtocol/topics.ts +++ b/apps/buddy/shared/productProtocol/topics.ts @@ -1,6 +1,6 @@ import { z } from 'zod' import { productPhaseSchema } from './identity' -import { productDraftSchema, productQuestionSchema, productRunSchema, productTaskSchema, productTaskSummarySchema } from './queries' +import { productDraftSchema, productQuestionSchema, productRecentWindowSchema, productRunSchema, productTaskSchema, productTaskSummarySchema } from './queries' import { productIdSchema } from './scalars' import { productApprovalSchema, productMessageSchema, productQueueItemSchema, productRunEventSchema } from './views' @@ -16,6 +16,10 @@ export const productConversationItemSchema = z.discriminatedUnion('entity', [ z.object({ entity: z.literal('queue'), value: productQueueItemSchema }).strict(), z.object({ entity: z.literal('approval'), value: productApprovalSchema }).strict(), ]) +export const productConversationWindowItemSchema = z.union([ + productConversationItemSchema, + z.object({ entity: z.literal('historyWindow'), value: productRecentWindowSchema.omit({ items: true }) }).strict(), +]) export const productConversationChangeSchema = z.union([ z.object({ kind: z.literal('upsert'), item: productConversationItemSchema }).strict(), z.object({ kind: z.literal('remove'), entity: z.enum(['task', 'message', 'run', 'queue', 'approval']), id: productIdSchema }).strict(), @@ -27,15 +31,16 @@ export const PRODUCT_TOPIC_SCHEMAS = { draftIndex: { item: productDraftSchema, change: productDraftIndexChangeSchema }, draft: { item: productDraftSchema, change: productDraftIndexChangeSchema }, conversation: { item: productConversationItemSchema, change: productConversationChangeSchema }, + conversationWindow: { item: productConversationWindowItemSchema, change: productConversationChangeSchema }, questions: { item: productQuestionSchema, change: productQuestionChangeSchema }, approvalInbox: { item: productApprovalSchema, change: productApprovalInboxChangeSchema }, serviceState: { item: productServiceStateSchema, change: productServiceStateSchema }, } as const -export type ProductTopic = { kind: 'taskIndex' | 'draftIndex' | 'approvalInbox' | 'serviceState' } | { kind: 'draft', draftId: string } | { kind: 'conversation' | 'questions', conversationId: string, branchId: string } +export type ProductTopic = { kind: 'taskIndex' | 'draftIndex' | 'approvalInbox' | 'serviceState' } | { kind: 'draft', draftId: string } | { kind: 'conversation' | 'conversationWindow' | 'questions', conversationId: string, branchId: string } export function formatProductTopic(topic: ProductTopic): string { if (topic.kind === 'draft') return `draft:${encodeURIComponent(productIdSchema.parse(topic.draftId))}` - if (topic.kind === 'conversation' || topic.kind === 'questions') + if (topic.kind === 'conversation' || topic.kind === 'conversationWindow' || topic.kind === 'questions') return `${topic.kind}:${encodeURIComponent(productIdSchema.parse(topic.conversationId))}:${encodeURIComponent(productIdSchema.parse(topic.branchId))}` return topic.kind } @@ -46,7 +51,7 @@ export function parseProductTopic(value: string): ProductTopic { topic = { kind: value as 'taskIndex' | 'draftIndex' | 'approvalInbox' | 'serviceState' } else if (parts.length === 2 && parts[0] === 'draft') topic = { kind: 'draft', draftId: productIdSchema.parse(decodeURIComponent(parts[1]!)) } - else if (parts.length === 3 && (parts[0] === 'conversation' || parts[0] === 'questions')) + else if (parts.length === 3 && (parts[0] === 'conversation' || parts[0] === 'conversationWindow' || parts[0] === 'questions')) topic = { kind: parts[0], conversationId: productIdSchema.parse(decodeURIComponent(parts[1]!)), branchId: productIdSchema.parse(decodeURIComponent(parts[2]!)) } else throw new Error('Unknown product topic') diff --git a/apps/buddy/src/mobile-reader/MobileReader.vue b/apps/buddy/src/mobile-reader/MobileReader.vue index 2cd498a17..1e73c678b 100644 --- a/apps/buddy/src/mobile-reader/MobileReader.vue +++ b/apps/buddy/src/mobile-reader/MobileReader.vue @@ -11,14 +11,14 @@ import { useReaderResultRead } from './useReaderResultRead' import { useReaderTurns } from './useReaderTurns' import { useReaderViewport } from './useReaderViewport' -const { entries: messages, runs, transcripts, topic, dark, fontScale, appearance, typography, language, resultRunId, send } = useReaderBridge() +const { entries: messages, runs, transcripts, topic, dark, fontScale, appearance, typography, language, resultRunId, anchor, hasLaterMessages, send } = useReaderBridge() const labels = useReaderStrings(language) -const entries = useReaderTurns(messages, runs, transcripts, topic) +const entries = useReaderTurns(messages, runs, transcripts, topic, anchor) const runBuddyIds = computed(() => new Map(runs.value.map(run => [run.id, run.buddyId]))) provideChatTranscriptPresentation(() => topic.value, () => entries.value.map(entry => entry.id)) const viewport = useTemplateRef('viewport') useReaderResultRead(viewport, resultRunId, topic, send) -const { rows, height, followsEnd, showLatest, latest, scroll, touch, measure } = useReaderViewport(viewport, entries, topic) +const { rows, height, followsEnd, showLatest, anchorStatus, locatedMessageId, latest, scroll, touch, measure } = useReaderViewport(viewport, entries, topic, anchor, hasLaterMessages, () => send({ action: 'latest' })) const expanded = shallowRef(new Map()) const style = computed(() => ({ ...appearance.value, @@ -54,8 +54,11 @@ watch(topic, () => { + + diff --git a/apps/buddy/src/mobile-reader/__tests__/readerProjection.spec.ts b/apps/buddy/src/mobile-reader/__tests__/readerProjection.spec.ts index b76088dae..732d34015 100644 --- a/apps/buddy/src/mobile-reader/__tests__/readerProjection.spec.ts +++ b/apps/buddy/src/mobile-reader/__tests__/readerProjection.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' import { ReaderProjection } from '../readerProjection' -import { readerUpdateSchema } from '../readerProtocol' +import { readerActionSchema, readerUpdateSchema } from '../readerProtocol' const update = (topic: string, extra: Record = {}) => readerUpdateSchema.parse({ topic, reset: false, dark: false, fontScale: 1, entries: [], ...extra }) const event = (sequence: number) => ({ runId: 'run', sequence, type: 'run.started', createdAt: '2026-11-11T05:00:00Z', payload: {} }) @@ -15,6 +15,22 @@ describe('reader projection', () => { expect(projection.apply(update('two', { order: ['a'], entries: [a] })).entries).toEqual([a]) expect(projection.apply(update('two', { reset: true })).entries).toEqual([]) }) + it('binds an anchor request to its original topic and message until a new request arrives', () => { + const projection = new ReaderProjection() + const first = projection.apply(update('one', { anchorMessageId: 'old', anchorRequestId: 'search-1' })).anchor + expect(first).toEqual({ topic: 'one', messageId: 'old', requestId: 'search-1' }) + expect(projection.apply(update('one', { anchorMessageId: 'changed', anchorRequestId: 'search-1' })).anchor).toBe(first) + expect(projection.apply(update('one')).anchor).toBeNull() + expect(projection.apply(update('two', { anchorMessageId: 'old', anchorRequestId: 'search-1' })).anchor).toBeNull() + expect(projection.apply(update('two', { anchorMessageId: 'old', anchorRequestId: 'search-2' })).anchor).toEqual({ topic: 'two', messageId: 'old', requestId: 'search-2' }) + }) + it('keeps legacy updates valid and validates navigation without granting other capabilities', () => { + expect(update('legacy').anchorMessageId).toBeUndefined() + expect(readerUpdateSchema.safeParse({ ...update('one'), anchorRequestId: '' }).success).toBe(false) + expect(readerUpdateSchema.safeParse({ ...update('one'), anchorMessageId: '' }).success).toBe(false) + expect(readerActionSchema.parse({ action: 'latest', topic: 'one' })).toEqual({ action: 'latest', topic: 'one' }) + expect(readerActionSchema.safeParse({ action: 'latest', topic: 'one', url: 'https://example.invalid' }).success).toBe(false) + }) it('appends transcript deltas and replaces the stream on reset', () => { const projection = new ReaderProjection() projection.apply(update('one', { transcripts: { run: { events: [event(1)] } } })) diff --git a/apps/buddy/src/mobile-reader/__tests__/useReaderViewport.spec.ts b/apps/buddy/src/mobile-reader/__tests__/useReaderViewport.spec.ts new file mode 100644 index 000000000..a2619d3be --- /dev/null +++ b/apps/buddy/src/mobile-reader/__tests__/useReaderViewport.spec.ts @@ -0,0 +1,143 @@ +// @vitest-environment jsdom +import type { ReaderEntry } from '../readerBridge' +import type { ReaderAnchor } from '../readerProjection' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { createApp, h, nextTick, shallowRef } from 'vue' +import { useReaderViewport } from '../useReaderViewport' + +const disposals: (() => void)[] = [] +const entry = (id: string): ReaderEntry => ({ id, continuation: false, messages: [{ id, role: 'assistant', text: '' } as ReaderEntry['messages'][number]] }) + +beforeEach(() => { + vi.useFakeTimers() + vi.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(function (this: HTMLElement) { + return this.className === 'viewport' ? 400 : 100 + }) + vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(300) + vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(100) + vi.spyOn(HTMLElement.prototype, 'scrollHeight', 'get').mockImplementation(function (this: HTMLElement) { + return Number(this.firstElementChild?.getAttribute('data-height') ?? 0) + }) + vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function (this: HTMLElement) { + const row = this.closest('[data-index]') + const top = row ? Number(row.dataset.start) - (this.closest('.viewport')?.scrollTop ?? 0) : 0 + return { top, bottom: top + 100, left: 0, right: 300, width: 300, height: 100, x: 0, y: top, toJSON: () => ({}) } + }) + vi.stubGlobal('ResizeObserver', class { observe() {} unobserve() {} disconnect() {} }) + Object.defineProperty(HTMLElement.prototype, 'scrollTo', { configurable: true, value(this: HTMLElement, options: ScrollToOptions) { + this.scrollTop = Math.max(0, Math.min(this.scrollHeight - this.clientHeight, options.top ?? 0)) + this.dispatchEvent(new Event('scroll')) + } }) +}) +afterEach(() => { + disposals.splice(0).forEach(dispose => dispose()) + delete (HTMLElement.prototype as Partial).scrollTo + vi.useRealTimers() +}) + +async function settle() { + await nextTick() + for (let frame = 0; frame < 16; frame++) { + await vi.advanceTimersByTimeAsync(20) + await nextTick() + } +} +function mount() { + const entries = shallowRef(Array.from({ length: 60 }, (_, index) => entry(`message-${index + 21}`))) + const topic = shallowRef('one') + const anchor = shallowRef(null) + const hasLater = shallowRef(false) + const requests: string[] = [] + let view!: ReturnType + let viewport!: HTMLElement + const root = document.createElement('div') + document.body.append(root) + const app = createApp({ + setup() { + const element = shallowRef(null) + view = useReaderViewport(element, entries, topic, anchor, hasLater, () => requests.push(topic.value)) + return () => h('div', { + class: 'viewport', + ref: (value) => { + element.value = value as HTMLElement + viewport = value as HTMLElement + }, + onScroll: view.scroll, + }, [ + h('div', { 'data-height': view.height.value }, view.rows.value.map(row => h('section', { 'key': String(row.key), 'ref': view.measure, 'data-index': row.index, 'data-start': row.start }, [h('article', { 'data-message-id': row.entry.messages[0]!.id })]))), + ]) + }, + }) + app.mount(root) + disposals.push(() => { + app.unmount() + root.remove() + }) + return { + entries, + topic, + anchor, + hasLater, + requests, + get viewport() { + return viewport + }, + view, + } +} + +describe('reader viewport navigation', () => { + it('locates only new requests and preserves an offset through prepend and ordinary updates', async () => { + const reader = mount() + await settle() + reader.anchor.value = { topic: 'one', messageId: 'message-40', requestId: 'search-1' } + reader.hasLater.value = true + await settle() + expect(reader.view.anchorStatus.value).toBe('located') + expect(reader.view.locatedMessageId.value).toBe('message-40') + reader.viewport.scrollTop += 35 + reader.viewport.dispatchEvent(new Event('scroll')) + await settle() + const offset = reader.viewport.querySelector('[data-message-id="message-40"]')!.getBoundingClientRect().top + reader.entries.value = [entry('message-19'), entry('message-20'), ...reader.entries.value] + await settle() + expect(reader.viewport.querySelector('[data-message-id="message-40"]')!.getBoundingClientRect().top).toBeCloseTo(offset) + reader.entries.value = reader.entries.value.map(item => ({ ...item })) + reader.anchor.value = { ...reader.anchor.value! } + await settle() + expect(reader.viewport.querySelector('[data-message-id="message-40"]')!.getBoundingClientRect().top).toBeCloseTo(offset) + expect(reader.view.followsEnd.value).toBe(false) + const beforeLatest = reader.viewport.scrollTop + reader.view.latest() + await settle() + expect(reader.requests).toEqual(['one']) + expect(reader.viewport.scrollTop).toBe(beforeLatest) + reader.entries.value = [...reader.entries.value, entry('latest')] + reader.anchor.value = null + reader.hasLater.value = false + await settle() + expect(reader.viewport.scrollTop).toBeCloseTo(reader.viewport.scrollHeight - reader.viewport.clientHeight) + expect(reader.view.followsEnd.value).toBe(true) + }) + + it('reports missing IDs without falling back to the end, and cancels pending navigation on a topic change', async () => { + const reader = mount() + await settle() + reader.anchor.value = { topic: 'one', messageId: 'message-40', requestId: 'search-1' } + await settle() + const top = reader.viewport.scrollTop + reader.anchor.value = { topic: 'one', messageId: 'missing', requestId: 'search-2' } + await settle() + expect(reader.view.anchorStatus.value).toBe('missing') + expect(reader.viewport.scrollTop).toBe(top) + expect(reader.view.showLatest.value).toBe(true) + reader.anchor.value = { topic: 'one', messageId: 'message-30', requestId: 'search-3' } + await nextTick() + reader.topic.value = 'two' + reader.entries.value = [entry('other')] + reader.anchor.value = null + await settle() + expect(reader.view.locatedMessageId.value).toBeNull() + expect(reader.view.anchorStatus.value).toBe('idle') + }) +}) diff --git a/apps/buddy/src/mobile-reader/readerBridge.ts b/apps/buddy/src/mobile-reader/readerBridge.ts index 51be04edf..be716bfbb 100644 --- a/apps/buddy/src/mobile-reader/readerBridge.ts +++ b/apps/buddy/src/mobile-reader/readerBridge.ts @@ -1,5 +1,6 @@ import type { LocalRun, LocalRunEvent } from '@buddy-shared/runs/runApi' import type { ChatAgentTurn } from '../modules/tasks/contracts' +import type { ReaderAnchor } from './readerProjection' import type { ReaderActionInput, ReaderWireEntry } from './readerProtocol' import { useTimeoutFn } from '@vueuse/core' import { onMounted, onUnmounted, provide, shallowRef } from 'vue' @@ -31,6 +32,8 @@ export function useReaderBridge() { const language = shallowRef('zh') provide(readerLanguageKey, language) const resultRunId = shallowRef(null) + const anchor = shallowRef(null) + const hasLaterMessages = shallowRef(false) let port: MessagePort | undefined const projection = new ReaderProjection() @@ -62,6 +65,8 @@ export function useReaderBridge() { } return } + if (!update.reset && topic.value && update.topic !== topic.value) + return if (update.appearance) appearance.value = update.appearance.variables if (update.typography) @@ -77,6 +82,8 @@ export function useReaderBridge() { runs.value = (update.runs ?? []).map(run => ({ ...run, generation: Number(run.generation) })) transcripts.value = next.transcripts topic.value = update.topic + anchor.value = next.anchor + hasLaterMessages.value = update.hasLaterMessages ?? false resultRunId.value = update.resultRunId ?? null dark.value = update.dark fontScale.value = update.fontScale @@ -95,5 +102,5 @@ export function useReaderBridge() { window.removeEventListener('message', connect) port?.close() }) - return { entries, runs, transcripts, topic, dark, fontScale, appearance, typography, language, resultRunId, send } + return { entries, runs, transcripts, topic, dark, fontScale, appearance, typography, language, resultRunId, anchor, hasLaterMessages, send } } diff --git a/apps/buddy/src/mobile-reader/readerProjection.ts b/apps/buddy/src/mobile-reader/readerProjection.ts index 4287c4170..791f6dd79 100644 --- a/apps/buddy/src/mobile-reader/readerProjection.ts +++ b/apps/buddy/src/mobile-reader/readerProjection.ts @@ -1,7 +1,10 @@ import type { ReaderUpdate, ReaderWireEntry } from './readerProtocol' +export interface ReaderAnchor { topic: string, messageId: string, requestId: string } + export class ReaderProjection { #topic = '' + #anchor: ReaderAnchor | null = null #items = new Map() #order: string[] = [] #transcripts: NonNullable = {} @@ -14,6 +17,10 @@ export class ReaderProjection { this.#transcripts = {} } this.#topic = update.topic + if (update.anchorRequestId && update.anchorMessageId) { + if (this.#anchor?.requestId !== update.anchorRequestId) + this.#anchor = { topic: update.topic, requestId: update.anchorRequestId, messageId: update.anchorMessageId } + } if (update.transcripts) { const next = { ...this.#transcripts } for (const [id, transcript] of Object.entries(update.transcripts)) @@ -30,6 +37,11 @@ export class ReaderProjection { } for (const entry of update.entries) this.#items.set(entry.id, entry) - return { reset, entries: this.#order.flatMap(id => this.#items.get(id) ?? []), transcripts: this.#transcripts } + return { + reset, + entries: this.#order.flatMap(id => this.#items.get(id) ?? []), + transcripts: this.#transcripts, + anchor: update.anchorRequestId && update.anchorMessageId && this.#anchor?.topic === update.topic ? this.#anchor : null, + } } } diff --git a/apps/buddy/src/mobile-reader/readerProtocol.ts b/apps/buddy/src/mobile-reader/readerProtocol.ts index f99627e5c..949d7984b 100644 --- a/apps/buddy/src/mobile-reader/readerProtocol.ts +++ b/apps/buddy/src/mobile-reader/readerProtocol.ts @@ -42,6 +42,9 @@ export const readerUpdateSchema = schema.object({ typography: schema.object({ bodySize: schema.number().positive(), bodyLineHeight: schema.number().positive() }).strict().optional(), avatars: schema.record(schema.string(), schema.string()).optional(), resultRunId: id.nullable().optional(), + anchorMessageId: id.nullable().optional(), + anchorRequestId: id.nullable().optional(), + hasLaterMessages: schema.boolean().optional(), topic: schema.string(), reset: schema.boolean(), dark: schema.boolean(), @@ -56,6 +59,7 @@ export const readerCopiedSchema = schema.object({ type: schema.literal('copied') const action = (name: T, fields: S) => schema.object({ action: schema.literal(name), topic: schema.string(), ...fields }).strict() export const readerActionSchema = schema.discriminatedUnion('action', [ action('ready', {}), + action('latest', {}), action('read-result', { runId: id }), action('transcript', { runId: id, retry: schema.literal('true').optional() }), action('message', { id }), diff --git a/apps/buddy/src/mobile-reader/readerStrings.ts b/apps/buddy/src/mobile-reader/readerStrings.ts index bba00fa18..578574e32 100644 --- a/apps/buddy/src/mobile-reader/readerStrings.ts +++ b/apps/buddy/src/mobile-reader/readerStrings.ts @@ -26,6 +26,7 @@ const en = { previewSaveShare: 'Preview, save or share', messages: 'Conversation messages', latest: 'Jump to latest', + anchorMissing: 'This message could not be located in the current conversation.', retryTranscript: 'Execution details unavailable · Retry', empty: 'Messages will appear here', } @@ -52,6 +53,7 @@ const zh: Record = { previewSaveShare: '预览、保存或分享', messages: '对话消息', latest: '回到最新', + anchorMissing: '无法在当前对话中定位这条消息。', retryTranscript: '执行过程暂未载入 · 重试', empty: '消息将在这里显示', } diff --git a/apps/buddy/src/mobile-reader/useReaderTurns.ts b/apps/buddy/src/mobile-reader/useReaderTurns.ts index 622bc5dee..8d5c2077d 100644 --- a/apps/buddy/src/mobile-reader/useReaderTurns.ts +++ b/apps/buddy/src/mobile-reader/useReaderTurns.ts @@ -2,15 +2,21 @@ import type { LocalRun } from '@buddy-shared/runs/runApi' import type { Ref } from 'vue' import type { ChatAgentTurn } from '../modules/tasks/contracts' import type { ReaderEntry, ReaderTranscript } from './readerBridge' +import type { ReaderAnchor } from './readerProjection' import { computed, watch } from 'vue' import { createChatAgentTurnReducer } from '../modules/tasks/reader' -export function useReaderTurns(entries: Ref, runs: Ref, transcripts: Ref>, topic: Ref) { +export function useReaderTurns(entries: Ref, runs: Ref, transcripts: Ref>, topic: Ref, anchor?: Ref) { const cache = new Map }>() watch(topic, () => cache.clear()) return computed(() => { const turns = new Map() + const anchoredRun = anchor?.value?.topic === topic.value + ? entries.value.flatMap(entry => entry.messages).find(message => message.id === anchor.value?.messageId)?.runId + : undefined for (const run of runs.value) { + if (run.id === anchoredRun) + continue const source = transcripts.value[run.id] if (!source?.loaded || !source.events.length) continue diff --git a/apps/buddy/src/mobile-reader/useReaderViewport.ts b/apps/buddy/src/mobile-reader/useReaderViewport.ts index ec4f535a0..153ad5946 100644 --- a/apps/buddy/src/mobile-reader/useReaderViewport.ts +++ b/apps/buddy/src/mobile-reader/useReaderViewport.ts @@ -1,13 +1,21 @@ import type { ComponentPublicInstance, Ref } from 'vue' import type { ReaderEntry } from './readerBridge' +import type { ReaderAnchor } from './readerProjection' import { defaultRangeExtractor, elementScroll, useVirtualizer } from '@tanstack/vue-virtual' -import { computed, onMounted, onUnmounted, shallowRef, watch } from 'vue' +import { computed, nextTick, onMounted, onUnmounted, shallowRef, watch } from 'vue' -export function useReaderViewport(viewport: Ref, entries: Ref, topic: Ref) { +export function useReaderViewport(viewport: Ref, entries: Ref, topic: Ref, anchor: Ref, hasLaterMessages: Ref, onLatest: () => void) { const followsEnd = shallowRef(true) const hasMoreBelow = shallowRef(false) - const showLatest = computed(() => !followsEnd.value && hasMoreBelow.value) + const readingHistory = shallowRef(false) + const anchorStatus = shallowRef<'idle' | 'locating' | 'located' | 'missing'>('idle') + const locatedMessageId = shallowRef(null) + const showLatest = computed(() => hasLaterMessages.value || readingHistory.value || (!followsEnd.value && hasMoreBelow.value)) const selected = shallowRef([]) + const locatingIndex = shallowRef(null) + let anchorFrame = 0 + let navigation = 0 + let lastAnchorRequest: string | null = null let frame = 0 let touching = false let lastTop = 0 @@ -30,7 +38,7 @@ export function useReaderViewport(viewport: Ref, entries: Re anchorTo: 'end' as const, followOnAppend: followsEnd.value, scrollEndThreshold: followsEnd.value ? 24 : 0, - rangeExtractor: (range: Parameters[0]) => [...new Set([...defaultRangeExtractor(range), ...selected.value])].sort((a, b) => a - b), + rangeExtractor: (range: Parameters[0]) => [...new Set([...defaultRangeExtractor(range), ...selected.value, ...(locatingIndex.value === null ? [] : [locatingIndex.value])])].sort((a, b) => a - b), onChange: () => { report() follow() @@ -62,10 +70,69 @@ export function useReaderViewport(viewport: Ref, entries: Re virtualizer.value.scrollToEnd() }) } - function latest() { + function cancelAnchor() { + navigation++ + cancelAnimationFrame(anchorFrame) + locatingIndex.value = null + } + function resumeLatest() { + cancelAnchor() + anchorStatus.value = 'idle' + locatedMessageId.value = null + readingHistory.value = false followsEnd.value = true follow() } + function latest() { + onLatest() + if (!hasLaterMessages.value) + resumeLatest() + } + async function locate(request: ReaderAnchor) { + cancelAnchor() + const current = navigation + followsEnd.value = false + readingHistory.value = true + cancelAnimationFrame(frame) + locatedMessageId.value = null + anchorStatus.value = 'locating' + const index = entries.value.findIndex(entry => entry.messages.some(message => message.id === request.messageId)) + if (request.topic !== topic.value || index < 0) { + anchorStatus.value = 'missing' + return + } + locatingIndex.value = index + await nextTick() + if (current !== navigation) + return + virtualizer.value.scrollToIndex(index, { align: 'start' }) + let attempts = 0 + const align = () => { + if (current !== navigation || request.topic !== topic.value) + return + const element = viewport.value + const message = [...element?.querySelectorAll('[data-message-id]') ?? []].find(element => element.dataset.messageId === request.messageId) + if (element && message) { + const offset = message.getBoundingClientRect().top - element.getBoundingClientRect().top + const target = Math.max(0, Math.min(element.scrollHeight - element.clientHeight, element.scrollTop + offset)) + if (Math.abs(element.scrollTop - target) <= 2) { + anchorStatus.value = 'located' + locatedMessageId.value = request.messageId + locatingIndex.value = null + return + } + virtualizer.value.scrollToOffset(target) + } + if (++attempts < 12) { + anchorFrame = requestAnimationFrame(align) + } + else { + anchorStatus.value = 'missing' + locatingIndex.value = null + } + } + anchorFrame = requestAnimationFrame(align) + } function scroll() { const element = viewport.value if (!element) @@ -74,14 +141,16 @@ export function useReaderViewport(viewport: Ref, entries: Re const atEnd = element.scrollHeight - element.clientHeight - element.scrollTop < 24 const automatic = programmaticTop !== null && Math.abs(element.scrollTop - programmaticTop) < 2 if (!automatic && (touching || element.scrollTop < lastTop - 2)) - followsEnd.value = atEnd - else if (atEnd) + followsEnd.value = atEnd && !readingHistory.value && !hasLaterMessages.value + else if (atEnd && !readingHistory.value && !hasLaterMessages.value) followsEnd.value = true programmaticTop = null lastTop = element.scrollTop } function touch(active: boolean) { touching = active + if (active) + cancelAnchor() if (!active) follow() } @@ -102,16 +171,42 @@ export function useReaderViewport(viewport: Ref, entries: Re selected.value = indexes } watch(topic, () => { - followsEnd.value = true + cancelAnchor() + anchorStatus.value = 'idle' + locatedMessageId.value = null + readingHistory.value = Boolean(anchor.value) || hasLaterMessages.value + followsEnd.value = !readingHistory.value selected.value = [] virtualizer.value.measure() follow() }) + watch(entries, (next, previous) => { + if (previous[0] && next.findIndex(entry => entry.id === previous[0]!.id) > 0) { + followsEnd.value = false + cancelAnimationFrame(frame) + } + }) + watch(() => anchor.value ? JSON.stringify([anchor.value.topic, anchor.value.requestId]) : null, (request) => { + if (anchor.value && request !== lastAnchorRequest) { + lastAnchorRequest = request + void locate(anchor.value) + } + else if (!anchor.value) { + cancelAnchor() + anchorStatus.value = 'idle' + locatedMessageId.value = null + } + }) + watch(hasLaterMessages, (current, previous) => { + if (!current && previous && !anchor.value) + resumeLatest() + }) watch(entries, follow, { flush: 'post' }) onMounted(() => document.addEventListener('selectionchange', retainSelection)) onUnmounted(() => { document.removeEventListener('selectionchange', retainSelection) cancelAnimationFrame(frame) + cancelAnchor() }) - return { rows, height, followsEnd, showLatest, latest, scroll, touch, measure } + return { rows, height, followsEnd, showLatest, anchorStatus, locatedMessageId, latest, scroll, touch, measure } } diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/AttachmentComposerInstrumentedTest.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/AttachmentComposerInstrumentedTest.kt index 1737a1cb0..644a89bb7 100644 --- a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/AttachmentComposerInstrumentedTest.kt +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/AttachmentComposerInstrumentedTest.kt @@ -75,7 +75,7 @@ class AttachmentComposerInstrumentedTest { return text } MobileUiTestDriver.openDrafts() - MobileUiTestDriver.click("草稿 · $body") + MobileUiTestDriver.clickContaining(body) MobileUiTestDriver.click("继续编辑") MobileUiTestDriver.waitUntil { input() == body && MobileUiTestDriver.exists("要移除的资料.txt") } assertTrue(MobileUiTestDriver.enabled("移除附件 要移除的资料.txt")) diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ComposerControlsInstrumentedTest.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ComposerControlsInstrumentedTest.kt new file mode 100644 index 000000000..db0e73728 --- /dev/null +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ComposerControlsInstrumentedTest.kt @@ -0,0 +1,168 @@ +package app.uselexora.mobile + +import android.widget.EditText +import androidx.compose.foundation.layout.* +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.mutableStateOf +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.test.* +import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.unit.Density +import androidx.compose.ui.unit.dp +import androidx.test.platform.app.InstrumentationRegistry +import app.uselexora.mobile.protocol.ExecutionSettings +import app.uselexora.mobile.ui.* +import org.json.JSONArray +import org.json.JSONObject +import org.junit.Assert.* +import org.junit.Rule +import org.junit.Test + +class ComposerControlsInstrumentedTest { + @get:Rule val compose = createComposeRule() + private val context get() = InstrumentationRegistry.getInstrumentation().targetContext + + @Test fun narrowLargeTextKeepsStopSeparateDuringTypingAndAttachmentOnlySend() { + val run = JSONObject().put("id", "active-run").put("generation", "generation-7") + val state = MobileUiState().apply { + connected = true + modelLabel = "A very long model name that must not displace either action" + } + val actions = mutableListOf>() + compose.setContent { + CompositionLocalProvider(LocalDensity provides Density(LocalDensity.current.density, 1.6f)) { + LexoraTheme(darkTheme = true) { + Box(Modifier.width(320.dp).padding(horizontal = 18.dp)) { + ComposerToolbar(state, { action, value -> actions.add(action to value) }, {}, false, listOf(run), {}) + } + } + } + } + for (tag in listOf("composer-attach", "composer-permission", "stop-run", "send")) { + compose.onNodeWithTag(tag).assertIsDisplayed().assertWidthIsAtLeast(48.dp).assertHeightIsAtLeast(48.dp) + } + val stopBounds = compose.onNodeWithTag("stop-run").fetchSemanticsNode().boundsInRoot + val sendBounds = compose.onNodeWithTag("send").fetchSemanticsNode().boundsInRoot + assertTrue(stopBounds.right <= sendBounds.left) + compose.onNodeWithTag("send").assertIsNotEnabled() + compose.onNodeWithTag("stop-run").performTouchInput { down(center) } + compose.runOnIdle { state.hasInput = true; state.sendEnabled = true } + compose.onNodeWithTag("stop-run").performTouchInput { up() } + compose.runOnIdle { + assertEquals(listOf(MobileAction.StopRun), actions.map { it.first }) + assertEquals("active-run", actions.single().second!!.getString("id")) + assertEquals("generation-7", actions.single().second!!.getString("generation")) + state.attachmentCount = 1 + } + assertEquals(stopBounds, compose.onNodeWithTag("stop-run").fetchSemanticsNode().boundsInRoot) + assertEquals(sendBounds, compose.onNodeWithTag("send").fetchSemanticsNode().boundsInRoot) + compose.onNodeWithTag("send").performClick() + compose.runOnIdle { assertEquals(listOf(MobileAction.StopRun, MobileAction.Send), actions.map { it.first }) } + } + + @Test fun replacingTheRunCancelsAnInProgressStopPress() { + val active = mutableStateOf(listOf(JSONObject().put("id", "old").put("generation", "generation-1"))) + val state = MobileUiState().apply { connected = true } + val actions = mutableListOf() + compose.setContent { LexoraTheme { ComposerToolbar(state, { action, _ -> actions.add(action) }, {}, false, active.value, {}) } } + compose.onNodeWithTag("stop-run").performTouchInput { down(center) } + compose.runOnIdle { active.value = listOf(JSONObject().put("id", "replacement").put("generation", "generation-2")) } + compose.onNodeWithTag("stop-run").performTouchInput { up() } + compose.runOnIdle { assertTrue(actions.isEmpty()) } + compose.onNodeWithTag("stop-run").performClick() + compose.runOnIdle { assertEquals(listOf(MobileAction.StopRun), actions) } + } + + @Test fun compactEditorKeepsTheNativeInstanceTextAndSelection() { + lateinit var input: EditText + compose.runOnUiThread { input = EditText(context).apply { showSoftInputOnFocus = false } } + val attachment = mutableStateOf(false) + compose.setContent { LexoraTheme { NativeComposerInput(input, true, attachment.value, 160.dp, false) {} } } + compose.onNodeWithTag("composer-compact").assertIsDisplayed() + compose.runOnIdle { attachment.value = true } + compose.onNodeWithTag("composer-editing").assertIsDisplayed() + compose.runOnIdle { + input.setText("A preserved local draft\nwith another line") + input.setSelection(3, 9) + } + compose.onNodeWithTag("collapse-editor").performClick() + compose.onNodeWithTag("composer-compact").assertIsDisplayed() + compose.runOnIdle { + assertEquals("A preserved local draft\nwith another line", input.text.toString()) + assertEquals(3, input.selectionStart) + assertEquals(9, input.selectionEnd) + assertEquals(1, input.maxLines) + assertNotNull(input.parent) + } + compose.onNodeWithTag("expand-editor").performClick() + compose.onNodeWithTag("composer-editing").assertIsDisplayed() + compose.runOnIdle { + assertEquals(6, input.maxLines) + assertEquals(3, input.selectionStart) + assertEquals(9, input.selectionEnd) + } + } + + @Test fun longModelListIsBoundedAndShortParametersRequireExplicitApply() { + val state = MobileUiState().apply { + modelPickerOpen = true + modelPickerShowList = true + models = (0 until 80).map { index -> JSONObject().put("providerId", "provider-$index").put("modelId", "model-$index") + .put("displayName", "Same model name").put("reasoningOptions", JSONArray()).put("serviceTiers", JSONArray()) } + } + val selections = mutableListOf() + compose.setContent { + LexoraTheme { + if (state.modelPickerOpen) ConversationModelPicker(state) { action, payload -> + if (action == MobileAction.ApplyModel) { selections.add(payload!!); state.modelSelection = payload; state.modelPickerOpen = false } + } + } + } + val longHeight = compose.onNodeWithTag("model-picker").fetchSemanticsNode().boundsInRoot.height + compose.onNodeWithTag("model-list").performScrollToNode(hasTestTag("model-option:provider-79:model-79")) + compose.onNodeWithTag("model-option:provider-79:model-79").performClick() + compose.onNodeWithTag("apply-model").assertIsDisplayed() + assertTrue(compose.onNodeWithTag("model-picker").fetchSemanticsNode().boundsInRoot.height < longHeight) + compose.runOnIdle { assertTrue(selections.isEmpty()) } + compose.onNodeWithTag("close-model-picker").performClick() + compose.runOnIdle { assertNull(state.modelSelection); assertTrue(selections.isEmpty()); state.modelPickerOpen = true } + compose.onNodeWithTag("model-option:provider-0:model-0").performClick() + compose.onNodeWithTag("apply-model").performClick() + compose.runOnIdle { + assertEquals("provider-0", selections.single().getString("providerId")) + assertEquals("model-0", selections.single().getString("modelId")) + assertTrue(selections.single().isNull("reasoning")) + assertTrue(selections.single().isNull("serviceTier")) + } + } + + @Test fun ordinaryPermissionAppliesImmediatelyButFullAccessRequiresConfirmation() { + val state = MobileUiState().apply { executionPickerOpen = true; executionSelection = ExecutionSettings.defaultConfig() } + val selections = mutableListOf() + compose.setContent { LexoraTheme { ConversationPermissionPicker(state) { action, value -> if (action == MobileAction.ApplyExecution) selections.add(value!!) } } } + compose.onNodeWithTag("permission-read_only-policy").performScrollTo().performClick() + compose.runOnIdle { assertEquals("read_only", selections.single().getString("executionProfile")) } + compose.onNodeWithTag("permission-workspace_write-policy").assertIsSelected() + compose.onNodeWithTag("permission-full_access-policy").performScrollTo().performClick() + compose.onNodeWithTag("cancel-full-access").performClick() + compose.runOnIdle { assertEquals(1, selections.size) } + compose.onNodeWithTag("permission-full_access-policy").performClick() + compose.runOnIdle { state.executionEnabled = false } + compose.onNodeWithTag("confirm-full-access").assertIsNotEnabled() + compose.runOnIdle { state.executionEnabled = true } + compose.onNodeWithTag("confirm-full-access").performClick() + compose.runOnIdle { assertEquals(listOf("read_only", "full_access"), selections.map { it.getString("executionProfile") }) } + } + + @Test fun changingThePermissionTargetDiscardsTheOldElevationConfirmation() { + val state = MobileUiState().apply { executionPickerOpen = true; executionSelection = ExecutionSettings.defaultConfig(); topic = "conversation:first:main" } + val selections = mutableListOf() + compose.setContent { LexoraTheme { ConversationPermissionPicker(state) { _, value -> selections.add(value!!) } } } + compose.onNodeWithTag("permission-full_access-policy").performScrollTo().performClick() + compose.onNodeWithTag("confirm-full-access").assertIsDisplayed() + compose.runOnIdle { state.topic = "conversation:second:main" } + compose.onNodeWithTag("confirm-full-access").assertDoesNotExist() + compose.runOnIdle { assertTrue(selections.isEmpty()) } + } +} diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ContentInstrumentedTest.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ContentInstrumentedTest.kt index 269ddc994..35523fa39 100644 --- a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ContentInstrumentedTest.kt +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ContentInstrumentedTest.kt @@ -52,7 +52,7 @@ class ContentInstrumentedTest { val opaque = store.edit(owner, "draft:$opaqueId", JSONObject().put("content", large.getJSONObject("content"))) ActivityScenario.launch(Intent(context, MainActivity::class.java)).use { scenario -> MobileUiTestDriver.awaitConnected() - MobileUiTestDriver.openDrafts(); click("草稿 · 跨端中文😀", prefix = true); click("查看内容") + MobileUiTestDriver.openDrafts(); MobileUiTestDriver.clickContaining("跨端中文😀"); click("查看内容") waitFor { var restored = false; scenario.onActivity { restored = it.findViewById(MainActivity.INPUT_ID)?.text?.toString().orEmpty().contains("引用原文😀") }; restored } assertEquals(sourceContent.toString(), store.draft(owner, local.scope)!!.value.getJSONObject("content").toString()) val commandCount = store.outbox(owner).size @@ -69,7 +69,7 @@ class ContentInstrumentedTest { instrumentation.uiAutomation.waitForIdle(700, 5000) instrumentation.uiAutomation.takeScreenshot()?.let { bitmap -> File(context.filesDir, "content-$token.png").outputStream().use { bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, it) }; bitmap.recycle() } val opaqueCount = store.outbox(owner).size - MobileUiTestDriver.openDrafts(); click("草稿 · 请在桌面端查看此内容"); click("查看内容") + MobileUiTestDriver.openDrafts(); MobileUiTestDriver.clickContaining("请在桌面端查看此内容"); click("查看内容") waitFor { var restored = false; scenario.onActivity { restored = it.findViewById(MainActivity.INPUT_ID)?.text?.toString().orEmpty() == "请在桌面端查看此内容" }; restored } assertTrue(MobileUiTestDriver.exists("发送")) assertFalse(MobileUiTestDriver.enabled("发送")) diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DesktopWorkflowInstrumentedTest.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DesktopWorkflowInstrumentedTest.kt index 39c79f18d..749fa26d1 100644 --- a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DesktopWorkflowInstrumentedTest.kt +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DesktopWorkflowInstrumentedTest.kt @@ -38,6 +38,7 @@ class DesktopWorkflowInstrumentedTest { val client = connection.connect()!! val marker = File(context.filesDir, "desktop-$token-ready.json") val resume = File(context.filesDir, "desktop-$token-resume.json") + var primaryFailure: Throwable? = null try { LocalStore(context).use { store -> TransferStore(context).use { transfers -> val owner = client.identity!!.partition val firstId = store.newId(); val scope = "draft:$firstId" @@ -77,7 +78,7 @@ class DesktopWorkflowInstrumentedTest { } fun input(): String { var value = ""; scenario.onActivity { value = it.findViewById(MainActivity.INPUT_ID)?.text?.toString().orEmpty() }; return value } MobileUiTestDriver.awaitConnected() - MobileUiTestDriver.openDrafts(); click("草稿 · $firstText"); click("继续编辑") + MobileUiTestDriver.openDrafts(); MobileUiTestDriver.clickContaining(firstText); click("继续编辑") waitFor { input() == firstText } scenario.recreate() waitFor { input() == firstText } @@ -106,7 +107,7 @@ class DesktopWorkflowInstrumentedTest { MobileUiTestDriver.openTaskSettings() MobileUiTestDriver.awaitConnected() File(context.filesDir, "desktop-$token-network-ready.json").writeText("ready") - waitFor(30000) { MobileUiTestDriver.exists("未连接") } + waitFor(30000) { !MobileUiTestDriver.connected() && (MobileUiTestDriver.contains(app.uselexora.mobile.i18n.mobileText(R.string.connection_retrying)) || MobileUiTestDriver.exists("未连接")) } MobileUiTestDriver.returnToConversation() scenario.recreate() waitFor { input() == "发送期间继续写的下一条😀" } @@ -116,6 +117,19 @@ class DesktopWorkflowInstrumentedTest { assertTrue(readCache.read(owner, "models")?.isNotEmpty() == true) assertNull(readCache.read("different-device", "models")) assertFalse(MobileUiTestDriver.enabled("发送")) + val cachedScope = "conversation:${target.getString("conversationId")}:${target.getString("branchId")}" + val cachedInput = store.draft(owner, cachedScope)!! + val commandsBeforeQueue = store.outbox(owner).map { it.id }.toSet() + MobileUiTestDriver.enqueueInput() + waitFor { + MobileUiTestDriver.contains(app.uselexora.mobile.i18n.mobileText(R.string.mobile_sync_required)) || + MobileUiTestDriver.contains(app.uselexora.mobile.i18n.mobileText(R.string.mobile_sync_failed_input)) + } + assertFalse(MobileUiTestDriver.enabled("发送")) + assertEquals(commandsBeforeQueue, store.outbox(owner).map { it.id }.toSet()) + assertEquals(cachedInput.editSequence, store.draft(owner, cachedScope)!!.editSequence) + assertEquals(cachedInput.value.toString(), store.draft(owner, cachedScope)!!.value.toString()) + assertEquals("发送期间继续写的下一条😀", input()) waitFor { avatarVisible() } File(context.filesDir, "desktop-$token-offline.json").writeText("ready") MobileUiTestDriver.openTaskSettings() @@ -133,9 +147,9 @@ class DesktopWorkflowInstrumentedTest { } instrumentation.uiAutomation.takeScreenshot()?.let { bitmap -> File(context.filesDir, "desktop-$token.png").outputStream().use { bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, it) }; bitmap.recycle() } } - } } } finally { + } } } catch (error: Throwable) { primaryFailure = error; throw error } finally { client.close() - connection.description()?.let { connection.logout(it) } + try { connection.description()?.let { connection.logout(it) } } catch (cleanup: Throwable) { if (primaryFailure != null) primaryFailure.addSuppressed(cleanup) else throw cleanup } marker.delete(); resume.delete() } } diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DeviceSwitchInstrumentedTest.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DeviceSwitchInstrumentedTest.kt index e3a494d30..29b2a9f4e 100644 --- a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DeviceSwitchInstrumentedTest.kt +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DeviceSwitchInstrumentedTest.kt @@ -214,14 +214,14 @@ class DeviceSwitchInstrumentedTest { } finally { automation.serviceInfo = automation.serviceInfo.apply { flags = previousFlags } } } fun awaitComputer(name: String, config: JSONObject) { - waitUntil(30000) { MobileUiTestDriver.contains("$name,已连接服务") } - click("搜索任务") + waitUntil(30000) { MobileUiTestDriver.contains("$name · 已连接服务") } + MobileUiTestDriver.clickTag("home-all-tasks") waitUntil { MobileUiTestDriver.contains(config.getString("assignedTitle")) } MobileUiTestDriver.openWorkbench() } fun switchComputer(current: String, next: String, config: JSONObject, captureSheet: Boolean = false) { MobileUiTestDriver.openWorkbench() - MobileUiTestDriver.clickContaining("$current,") + MobileUiTestDriver.clickContaining("$current ·") waitUntil { MobileUiTestDriver.exists("连接服务") && MobileUiTestDriver.exists(next) } if (captureSheet) capture("computer-switch", dialog = true) { MobileUiTestDriver.exists("连接服务") && MobileUiTestDriver.exists(next) } click("切换到 $next") @@ -229,21 +229,22 @@ class DeviceSwitchInstrumentedTest { } fun resume(text: String) { MobileUiTestDriver.openWorkbench() - click("工作台选项"); click("本地草稿") - click("草稿 · $text"); click("继续编辑") + MobileUiTestDriver.openDrafts() + MobileUiTestDriver.clickContaining(text); click("继续编辑") waitUntil { input() == text } } awaitComputer("验收服务 B", second) assertFalse(MobileUiTestDriver.contains(first.getString("assignedTitle"))) switchComputer("验收服务 B", "验收服务 A", first, captureSheet = true) - capture("workbench") { MobileUiTestDriver.exists("工作台选项") && MobileUiTestDriver.contains("验收服务 A,已连接服务") } + capture("workbench") { MobileUiTestDriver.existsTag("workbench-list") && MobileUiTestDriver.contains("验收服务 A · 已连接服务") } assertFalse(MobileUiTestDriver.contains(second.getString("assignedTitle"))) - click("工作台选项"); click("空间") + clickId("space-filter") waitUntil { MobileUiTestDriver.exists(first.getString("spaceName")) } assertFalse(MobileUiTestDriver.contains(second.getString("spaceName"))) - capture("spaces") { MobileUiTestDriver.exists(first.getString("spaceName")) && MobileUiTestDriver.exists("全部任务") } - click("全部任务") + capture("spaces", dialog = true) { MobileUiTestDriver.exists(first.getString("spaceName")) && MobileUiTestDriver.exists("全部空间") } + click("全部空间") + clickId("home-all-tasks") waitUntil { MobileUiTestDriver.contains(first.getString("assignedTitle")) && MobileUiTestDriver.contains(first.getString("unassignedTitle")) } clickId("space-filter"); click("未分配空间") waitUntil { MobileUiTestDriver.contains(first.getString("unassignedTitle")) && !MobileUiTestDriver.contains(first.getString("assignedTitle")) } @@ -260,7 +261,7 @@ class DeviceSwitchInstrumentedTest { capture("new-space-task") { input() == firstText && MobileUiTestDriver.exists("任务设置") } switchComputer("验收服务 A", "验收服务 B", second) - click("工作台选项"); click("空间"); click(second.getString("spaceName")) + clickId("space-filter"); click(second.getString("spaceName")); clickId("home-all-tasks") waitUntil { MobileUiTestDriver.contains(second.getString("assignedTitle")) && !MobileUiTestDriver.contains(second.getString("unassignedTitle")) } click("新任务"); waitInput() assertEquals("", input()) diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DraftUiInstrumentedTest.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DraftUiInstrumentedTest.kt index 65df989b5..29d2980fc 100644 --- a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DraftUiInstrumentedTest.kt +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DraftUiInstrumentedTest.kt @@ -26,10 +26,10 @@ class DraftUiInstrumentedTest { store.edit(partition, scope, JSONObject().put("content", MobileContentAdapter.textContent(body))) val outbox = store.outbox(partition).map { it.id }.toSet() ActivityScenario.launch(Intent(context, MainActivity::class.java).putExtra("fixture", true)).use { scenario -> - MobileUiTestDriver.openDrafts(); clickText("草稿 · $body"); clickText("继续编辑") + MobileUiTestDriver.openDrafts(); MobileUiTestDriver.clickContaining(body); clickText("继续编辑") waitFor { var ready = false; scenario.onActivity { ready = it.findViewById(MainActivity.INPUT_ID)?.text?.toString() == body }; ready } clickText("返回") - waitFor { MobileUiTestDriver.exists("工作台选项") && !MobileUiTestDriver.exists("任务设置") } + waitFor { MobileUiTestDriver.existsTag("workbench-list") && !MobileUiTestDriver.exists("任务设置") } MobileUiTestDriver.clickTag("new-task") fun blankConversation(): Boolean { var ready = false @@ -79,7 +79,7 @@ class DraftUiInstrumentedTest { .put("executionConfig", JSONObject().put("executionProfile", "read_only").put("approvalPolicy", "policy"))) } ActivityScenario.launch(Intent(context, MainActivity::class.java).putExtra("fixture", true)).use { scenario -> - MobileUiTestDriver.openDrafts(); clickText("草稿 · 保留这份输入"); clickText("继续编辑") + MobileUiTestDriver.openDrafts(); MobileUiTestDriver.clickContaining("保留这份输入"); clickText("继续编辑") fun mode(): String = LocalStore(context).use { store -> app.uselexora.mobile.protocol.ExecutionSettings.label(store.draft(partition, "global")!!.value.getJSONObject("executionConfig")) } @@ -95,7 +95,7 @@ class DraftUiInstrumentedTest { open() clickText("完全访问") clickText("取消") - clickText("返回任务设置") + clickText("关闭") waitFor { mode() == "人工审批" } LocalStore(context).use { store -> assertEquals("manual", store.draft(partition, "global")!!.value.getJSONObject("executionConfig").getString("approvalPolicy")) @@ -126,7 +126,7 @@ class DraftUiInstrumentedTest { store.edit(foreign, "global", JSONObject().put("content", MobileContentAdapter.textContent("旧连接输入-$token").apply { getJSONArray("panelResourceIds").put("foreign-attachment") })) } ActivityScenario.launch(Intent(context, MainActivity::class.java).putExtra("fixture", true)).use { scenario -> - MobileUiTestDriver.openDrafts(); clickText("草稿 · 仅附件草稿"); clickText("继续编辑") + MobileUiTestDriver.openDrafts(); MobileUiTestDriver.clickContaining("当前连接 · 新任务草稿\n1 个附件"); clickText("继续编辑") clickText("任务设置") waitFor { MobileUiTestDriver.exists("本机只读") } clickText("返回对话") @@ -136,7 +136,7 @@ class DraftUiInstrumentedTest { waitFor { MobileUiTestDriver.exists("本机只读") } clickText("返回对话") waitFor { !MobileUiTestDriver.existsTag("conversation-settings") } - MobileUiTestDriver.openDrafts(); clickText("以前的连接 · 旧连接输入-$token") + MobileUiTestDriver.openDrafts(); MobileUiTestDriver.clickContaining("旧连接输入-$token") assertFalse(MobileUiTestDriver.exists("继续编辑")) clickText("复制文字") waitFor { var ready = false; scenario.onActivity { ready = it.findViewById(MainActivity.INPUT_ID)?.text?.toString() == "旧连接输入-$token" }; ready } diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/HistoryInstrumentedTest.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/HistoryInstrumentedTest.kt index 2f7a8e84b..fdafaf0e4 100644 --- a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/HistoryInstrumentedTest.kt +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/HistoryInstrumentedTest.kt @@ -47,7 +47,14 @@ class HistoryInstrumentedTest { fun input(value: String) { scenario.onActivity { it.findViewById(MainActivity.INPUT_ID).setText(value) } } fun inputIs(value: String): Boolean { var actual = ""; scenario.onActivity { actual = it.findViewById(MainActivity.INPUT_ID)?.text?.toString().orEmpty() }; return actual == value } waitFor { MobileUiTestDriver.connected() } - MobileUiTestDriver.openTask(task().getString("title")) + MobileUiTestDriver.clickTag("home-all-tasks") + MobileUiTestDriver.setTextTag("task-search", "历史回复 1") + waitFor { MobileUiTestDriver.existsTag("search-results") && text("历史回复 1") } + MobileUiTestDriver.clickContaining("历史回复 1 😀") + waitFor { text("回到最新") && text("历史回复 1") } + assertEquals(originalBranch, task().getString("activeBranchId")) + assertEquals(2, messages(originalBranch).count { it.optString("role") == "assistant" }) + click("回到最新") waitFor { text("历史回复 2") } input("原分支保留输入") val originalScope = "conversation:$conversationId:$originalBranch" @@ -116,16 +123,24 @@ class HistoryInstrumentedTest { longClickContaining("默认分支改变仍从原来源编辑"); click("编辑") waitFor { inputIs("默认分支改变仍从原来源编辑") } input("任务已删除时必须保留的编辑") + waitFor { store.drafts().any { it.partition == partition && MobileContentAdapter.text(it.value.getJSONObject("content")) == "任务已删除时必须保留的编辑" } } + val staleDraft = store.drafts().single { it.partition == partition && MobileContentAdapter.text(it.value.getJSONObject("content")) == "任务已删除时必须保留的编辑" } + val beforeDeletion = store.outbox(partition).size signal("delete-ready", independentTarget) awaitSignal("task-deleted") - click("发送") + waitFor { !MobileUiTestDriver.enabled("发送") } + assertEquals(beforeDeletion, store.outbox(partition).size) + assertTrue(inputIs("任务已删除时必须保留的编辑")) + assertTrue(runCatching { + app.uselexora.mobile.protocol.MobileInputCommands(store).send(client, staleDraft, app.uselexora.mobile.protocol.DraftContext.target(staleDraft.scope), "chat.editUserMessage") + }.isFailure) waitFor { store.outbox(partition).any { it.method == "chat.submitInput" && it.envelope.getJSONObject("intent").getString("operation") == "edit" && it.state == "rejected" } } assertTrue(inputIs("任务已删除时必须保留的编辑")) val rejection = store.outbox(partition).single { it.method == "chat.submitInput" && it.envelope.getJSONObject("intent").getString("operation") == "edit" && it.state == "rejected" } assertEquals("SOURCE_CHANGED", rejection.receipt!!.getString("errorCode")) assertEquals(rejection.receipt.toString(), CommandOutbox(store).retry(client, rejection).toString()) assertEquals("原分支保留输入", MobileContentAdapter.text(store.draft(partition, originalScope)!!.value.getJSONObject("content"))) - signal("done", JSONObject().put("sourceMessageId", source.getString("id")).put("followup", followupTarget).put("edit", editTarget).put("regenerate", regeneratedTarget).put("independentEdit", independentTarget).put("deletedSourceRejection", rejection.receipt).put("originalInputPreserved", true).put("recreationPreservesTarget", true).put("independentBranchEditAccepted", true)) + signal("done", JSONObject().put("sourceMessageId", source.getString("id")).put("followup", followupTarget).put("edit", editTarget).put("regenerate", regeneratedTarget).put("independentEdit", independentTarget).put("deletedSourceRejection", rejection.receipt).put("originalInputPreserved", true).put("recreationPreservesTarget", true).put("independentBranchEditAccepted", true).put("deletedSourceUiBlocked", true)) instrumentation.uiAutomation.waitForIdle(300, 5000) instrumentation.uiAutomation.takeScreenshot()?.let { bitmap -> File(context.filesDir, "history-$token-branches.png").outputStream().use { bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, it) }; bitmap.recycle() } } diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LocalFilesInstrumentedTest.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LocalFilesInstrumentedTest.kt index 8aaa1d878..dfb1dac30 100644 --- a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LocalFilesInstrumentedTest.kt +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LocalFilesInstrumentedTest.kt @@ -59,6 +59,36 @@ class LocalFilesInstrumentedTest { } finally { for (entry in listOf(download, pending, adopted)) File(entry.getString("path")).delete(); link.delete(); outside.delete() } } } } + @Test fun offlineTaskCopiesRequireTheOriginalIdentityVersionAndCompleteVerifiedBytes() { + val partition = "offline-copy-${UUID.randomUUID()}" + LocalStore(context).use { store -> TransferStore(context).use { transfers -> + val download = record(partition, "download", "ready") + val file = File(download.getString("path")) + val original = file.readBytes() + val version = app.uselexora.mobile.network.MobileFiles.hash(original) + download.put("resourceId", "resource").put("expectedVersion", "7").put("contentVersion", version) + download.getJSONObject("prepared").put("sizeBytes", original.size.toString()) + val resource = JSONObject().put("resourceId", "resource").put("revision", "7") + val copies = app.uselexora.mobile.ui.TaskFileCopies(context, transfers, store) + try { + transfers.save(download) + assertEquals(file.canonicalFile, copies.find(partition, resource)?.canonicalFile) + assertNull(copies.find("another-partition", resource)) + assertNull(copies.find(partition, JSONObject(resource.toString()).put("revision", "8"))) + transfers.save(JSONObject(download.toString()).put("state", "receiving")) + assertNull(copies.find(partition, resource)) + transfers.save(download) + file.writeBytes(ByteArray(original.size) { 1 }) + assertNull(copies.find(partition, resource)) + assertTrue(file.exists()) + file.writeBytes(original) + assertEquals(setOf("resource"), copies.confirmed(partition, listOf(resource))) + } finally { + file.delete() + transfers.writableDatabase.delete("transfers", "partition=?", arrayOf(partition)) + } + } } + } @Test fun unwritableStagingIsReportedAsLocalFailureAndDoesNotEraseInput() { val partition = "storage-failure-${UUID.randomUUID()}" val source = File(File(context.filesDir, "exports").apply { mkdirs() }, "${UUID.randomUUID()}.txt").apply { writeText("attachment") } diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LocalizationInstrumentedTest.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LocalizationInstrumentedTest.kt index ec7cb96c4..31ae8973a 100644 --- a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LocalizationInstrumentedTest.kt +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LocalizationInstrumentedTest.kt @@ -24,7 +24,8 @@ class LocalizationInstrumentedTest { } } private fun resumeDraft(scenario: ActivityScenario, text: String) { - MobileUiTestDriver.click(mobileText(R.string.label_workspace_options)) + MobileUiTestDriver.click(mobileText(R.string.label_new_task)) + MobileUiTestDriver.click(mobileText(R.string.label_task_settings)) MobileUiTestDriver.click(mobileText(R.string.label_local_drafts)) MobileUiTestDriver.clickContaining(text) MobileUiTestDriver.click(mobileText(R.string.mobile_continue_editing).uppercase(app.uselexora.mobile.i18n.MobileStrings.locale)) @@ -37,6 +38,38 @@ class LocalizationInstrumentedTest { assertEquals(mobileText(R.string.label_what_would_you_like_lexora_to_do), it.findViewById(MainActivity.INPUT_ID).hint.toString()) } } + @Test fun languageChangeFromTaskSettingsReturnsToTheSameInput() { + val original = MobileAppearance.language + instrumentation.runOnMainSync { MobileAppearance.selectLanguage(context, "zh") } + try { + ActivityScenario.launch(Intent(context, MainActivity::class.java).putExtra("fixture", true)).use { scenario -> + MobileUiTestDriver.click("新任务") + val text = "${UUID.randomUUID()} 同一个任务中的未发送输入" + MobileUiTestDriver.waitUntil { + var enabled = false + scenario.onActivity { enabled = it.findViewById(MainActivity.INPUT_ID)?.isEnabled == true } + enabled + } + scenario.onActivity { it.findViewById(MainActivity.INPUT_ID).setText(text) } + MobileUiTestDriver.clickTag("task-information") + MobileUiTestDriver.click(mobileText(R.string.label_appearance_language)) + MobileUiTestDriver.clickTag("language-selector") + MobileUiTestDriver.click("English") + MobileUiTestDriver.waitUntil { MobileUiTestDriver.exists("Phone settings") } + MobileUiTestDriver.click(mobileText(R.string.label_close_settings)) + MobileUiTestDriver.waitUntil { MobileUiTestDriver.existsTag("conversation-settings") } + MobileUiTestDriver.click(mobileText(R.string.back_conversation)) + MobileUiTestDriver.waitUntil { + var restored = false + scenario.onActivity { restored = it.findViewById(MainActivity.INPUT_ID)?.text?.toString() == text } + restored + } + MobileUiTestDriver.click(mobileText(R.string.label_back)) + MobileUiTestDriver.waitUntil { MobileUiTestDriver.exists("New task") } + } + } finally { instrumentation.runOnMainSync { MobileAppearance.selectLanguage(context, original) } } + } + @Test fun actualLanguageSelectorRecreatesNativeContextAndPreservesDraft() { val original = MobileAppearance.language instrumentation.runOnMainSync { MobileAppearance.selectLanguage(context, "zh") } @@ -52,8 +85,12 @@ class LocalizationInstrumentedTest { } scenario.onActivity { it.findViewById(MainActivity.INPUT_ID).setText(text) } MobileUiTestDriver.click(mobileText(R.string.label_back)) - MobileUiTestDriver.click(mobileText(R.string.label_appearance_language)) + MobileUiTestDriver.clickTag("workbench-settings") + MobileUiTestDriver.clickTag("language-selector") MobileUiTestDriver.click("English") + MobileUiTestDriver.waitUntil { MobileUiTestDriver.exists("Phone settings") } + assertTrue(MobileUiTestDriver.exists("English")) + MobileUiTestDriver.click(mobileText(R.string.label_close_settings)) MobileUiTestDriver.waitUntil { MobileUiTestDriver.exists("New task") } scenario.onActivity { assertEquals("en", it.resources.configuration.locales[0].language) } assertTrue(MobileUiTestDriver.exists("All spaces")) @@ -61,10 +98,13 @@ class LocalizationInstrumentedTest { resumeDraft(scenario, text) capture("i18n-draft-en.png") MobileUiTestDriver.click(mobileText(R.string.label_back)) - MobileUiTestDriver.click(mobileText(R.string.label_appearance_language)) + MobileUiTestDriver.clickTag("workbench-settings") MobileUiTestDriver.waitUntil { MobileUiTestDriver.exists("English") } capture("i18n-settings-en.png") + MobileUiTestDriver.clickTag("language-selector") MobileUiTestDriver.click("简体中文") + MobileUiTestDriver.waitUntil { MobileUiTestDriver.exists("手机设置") } + MobileUiTestDriver.click(mobileText(R.string.label_close_settings)) MobileUiTestDriver.waitUntil { MobileUiTestDriver.exists("新任务") } scenario.onActivity { assertEquals("zh", it.resources.configuration.locales[0].language) } assertTrue(MobileUiTestDriver.exists("全部空间")) diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/MobileUiTestDriver.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/MobileUiTestDriver.kt index 7fa2fea98..ece0cb221 100644 --- a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/MobileUiTestDriver.kt +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/MobileUiTestDriver.kt @@ -71,6 +71,17 @@ object MobileUiTestDriver { fun clickContaining(value: String) = clickNode(value, substring = true) fun clickTag(value: String) = clickNode(value, tag = true) fun existsTag(value: String) = byId(value)?.isVisibleToUser == true + fun composerText(): String? = visit { it.isVisibleToUser && it.className?.toString() == "android.widget.EditText" }?.let { if (it.isShowingHintText) "" else it.text?.toString() } + fun setComposerText(value: String) = waitUntil { + visit { it.isVisibleToUser && it.className?.toString() == "android.widget.EditText" } + ?.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, android.os.Bundle().apply { putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, value) }) == true + } + + fun setTextTag(tag: String, value: String) { + waitUntil { + byId(tag)?.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, android.os.Bundle().apply { putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, value) }) == true + } + } private fun clickNode(value: String, prefix: Boolean = false, substring: Boolean = false, tag: Boolean = false) { var forward = true @@ -114,7 +125,7 @@ object MobileUiTestDriver { fun surface(): String? = when { exists("任务设置") -> null existsTag("task-list") -> "tasks" - exists("工作台选项") -> "workbench" + existsTag("workbench-list") -> "workbench" else -> null } fun layout(): String { @@ -146,9 +157,9 @@ object MobileUiTestDriver { click("返回") awaitStableSurface() } - if (!exists(title) && exists("工作台选项")) { - click("搜索任务") - waitUntil { existsTag("task-list") && !exists("工作台选项") } + if (!exists(title) && existsTag("workbench-list")) { + clickTag("home-all-tasks") + waitUntil { existsTag("task-list") && !existsTag("workbench-list") } } awaitStableSurface() click(title) @@ -156,8 +167,11 @@ object MobileUiTestDriver { } fun openFiles() { - if (!exists("任务设置")) click("新任务") - click("添加附件"); click("任务文件") + waitUntil { existsTag("workbench-list") || existsTag("task-information") || existsTag("conversation-settings") } + if (existsTag("conversation-settings")) returnToConversation() + if (existsTag("workbench-list")) click("新任务") + waitUntil { existsTag("task-information") } + click("添加附件"); click("浏览任务文件") } fun openTaskSettings() { @@ -199,8 +213,8 @@ object MobileUiTestDriver { } fun openDrafts() { - if (exists("任务设置")) { click("任务设置") } - else click("工作台选项") + if (existsTag("workbench-list")) click("新任务") + if (!existsTag("conversation-settings")) click("任务设置") click("本地草稿") waitUntil { exists("手机草稿") } } diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PaperWorkbenchInstrumentedTest.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PaperWorkbenchInstrumentedTest.kt index 1ded78999..b81f8b95c 100644 --- a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PaperWorkbenchInstrumentedTest.kt +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PaperWorkbenchInstrumentedTest.kt @@ -44,6 +44,12 @@ class PaperWorkbenchInstrumentedTest { } } } + val service = compose.onNodeWithTag("service-selector").fetchSemanticsNode().boundsInRoot + val space = compose.onNodeWithTag("space-filter").assertHeightIsAtLeast(48.dp).fetchSemanticsNode().boundsInRoot + val settings = compose.onNodeWithTag("workbench-settings").assertWidthIsAtLeast(48.dp).fetchSemanticsNode().boundsInRoot + assertEquals(service.center.y, space.center.y, 1f) + assertTrue(settings.bottom <= service.top) + assertTrue(service.right <= space.left) capture("service-status-connected.png") compose.onNodeWithTag("space-filter").assertIsDisplayed().performClick() compose.onNodeWithTag("connection-detail").assertDoesNotExist() @@ -58,7 +64,7 @@ class PaperWorkbenchInstrumentedTest { compose.onNodeWithTag("service-selector").assertHeightIsAtLeast(48.dp) val selector = compose.onNodeWithTag("service-selector").fetchSemanticsNode().boundsInRoot val detail = compose.onNodeWithTag("connection-detail").fetchSemanticsNode().boundsInRoot - assertTrue(selector.right < detail.left) + assertTrue(selector.bottom <= detail.top) capture("service-status-retrying.png") compose.onNodeWithTag("service-selector").performClick() compose.runOnIdle { @@ -73,16 +79,17 @@ class PaperWorkbenchInstrumentedTest { } @Test fun darkServiceStatusSupportsLargeTextAndLongFailureDetails() { + val state = state().apply { + connected = false; devicePlatform = "win32" + deviceLabel = "工作室的长名称服务" + connectionPhase = app.uselexora.mobile.session.MobileConnectionPhase.Unavailable + connectionLabel = "设备授权已失效 · 本地输入保留,请重新配对" + } compose.setContent { androidx.compose.runtime.CompositionLocalProvider(androidx.compose.ui.platform.LocalDensity provides androidx.compose.ui.unit.Density(androidx.compose.ui.platform.LocalDensity.current.density, 1.5f)) { LexoraTheme(darkTheme = true) { Box(Modifier.width(320.dp).height(780.dp).background(MaterialTheme.colorScheme.background)) { - PaperWorkbench(state().apply { - connected = false; devicePlatform = "win32" - deviceLabel = "工作室的长名称服务" - connectionPhase = app.uselexora.mobile.session.MobileConnectionPhase.Unavailable - connectionLabel = "设备授权已失效 · 本地输入保留,请重新配对" - }, emptyList(), { _, _ -> }, {}, {}) + PaperWorkbench(state, state.tasks, { _, _ -> }, {}, {}) } } } @@ -92,6 +99,18 @@ class PaperWorkbenchInstrumentedTest { val detail = compose.onNodeWithTag("connection-detail").fetchSemanticsNode().boundsInRoot assertTrue(detail.right <= bar.right && detail.bottom <= bar.bottom) capture("service-status-dark-large.png") + compose.runOnIdle { + state.connected = true; state.connectionLabel = "已连接服务" + state.selectedSpaceId = "studio"; state.selectedSpaceLabel = "工作空间中的一个长名称" + } + val service = compose.onNodeWithTag("service-selector").assertHeightIsAtLeast(48.dp).fetchSemanticsNode().boundsInRoot + val space = compose.onNodeWithTag("space-filter").assertHeightIsAtLeast(48.dp).fetchSemanticsNode().boundsInRoot + val settings = compose.onNodeWithTag("workbench-settings").assertIsDisplayed().fetchSemanticsNode().boundsInRoot + assertEquals(service.center.y, space.center.y, 1f) + assertTrue(settings.bottom <= service.top) + assertTrue(service.right <= space.left && space.right <= bar.right) + capture("home-compact-dark-large.png") + compose.onNodeWithTag("home-all-tasks").performScrollTo().assertIsDisplayed().assertHasClickAction() } @Test fun serviceSwitcherCentersItsEmptyMessageBelowTheHeader() { @@ -209,9 +228,10 @@ class PaperWorkbenchInstrumentedTest { } } } + capture("home-compact-light.png") val context = InstrumentationRegistry.getInstrumentation().targetContext for ((target, surfaceTag) in listOf( - compose.onNodeWithContentDescription("工作台选项") to "workbench-avatar-surface", + compose.onNodeWithTag("workbench-settings") to "workbench-settings-surface", compose.onNodeWithTag("space-filter") to "space-filter-surface", compose.onNodeWithTag("service-selector") to "service-selector-surface", )) { diff --git a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PreviewInstrumentedTest.kt b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PreviewInstrumentedTest.kt index 6515cf961..a01fd46fc 100644 --- a/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PreviewInstrumentedTest.kt +++ b/apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PreviewInstrumentedTest.kt @@ -120,6 +120,34 @@ class PreviewInstrumentedTest { } } finally { source.delete() } } + @Test fun failedPreviewCanRetryLocallyAndEmptyTextIsNotAFailure() { + val source = file("txt") + source.writeBytes(byteArrayOf(0xC3.toByte(), 0x28)) + val intent = Intent(context, PreviewActivity::class.java).putExtra("path", source.absolutePath).putExtra("name", "notes.txt").putExtra("mime", "text/plain") + try { + ActivityScenario.launch(intent).use { scenario -> + waitFor { + var visible = false + scenario.onActivity { visible = it.findViewById